Skip to content

Commit

Permalink
Use 'Table.mutate_rows()' rather than 'Row.commit()' in 'Batch.send()…
Browse files Browse the repository at this point in the history
…'. (#54)
  • Loading branch information
sumit-ql authored and tseaver committed Dec 3, 2018
1 parent 28ff85e commit 50c1562
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
6 changes: 2 additions & 4 deletions src/google/cloud/happybase/batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,8 @@ def __init__(self, table, timestamp=None, batch_size=None,

def send(self):
"""Send / commit the batch of mutations to the server."""
for row in self._row_map.values():
# commit() does nothing if row hasn't accumulated any mutations.
row.commit()

table = self._table._low_level_table
table.mutate_rows(self._row_map.values())
self._row_map.clear()
self._mutation_count = 0

Expand Down
17 changes: 9 additions & 8 deletions unit_tests/test_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ def test_constructor_with_batch_size_and_transactional(self):
table, batch_size=batch_size, transaction=transaction)

def test_send(self):
table = object()
low_level_table = _MockLowLevelTable()
table = _MockTable(low_level_table)
batch = self._make_one(table)

batch._row_map = row_map = _MockRowMap()
Expand All @@ -107,16 +108,15 @@ def test_send(self):
batch._mutation_count = 1337

self.assertEqual(row_map.clear_count, 0)
self.assertEqual(row1.commits, 0)
self.assertEqual(row2.commits, 0)
self.assertNotEqual(batch._mutation_count, 0)
self.assertNotEqual(row_map, {})
self.assertEqual(len(table._low_level_table.rows_mutate), 0)

batch.send()
self.assertEqual(row_map.clear_count, 1)
self.assertEqual(row1.commits, 1)
self.assertEqual(row2.commits, 1)
self.assertEqual(batch._mutation_count, 0)
self.assertEqual(table._low_level_table.rows_mutate,
[row1, row2])
self.assertEqual(row_map, {})

def test__try_send_no_batch_size(self):
Expand Down Expand Up @@ -514,9 +514,6 @@ def __init__(self):
self.delete_cell_calls = []
self.delete_cells_calls = []

def commit(self):
self.commits += 1

def delete(self):
self.deletes += 1

Expand All @@ -542,8 +539,12 @@ def __init__(self, *args, **kwargs):
self.args = args
self.kwargs = kwargs
self.rows_made = []
self.rows_mutate = []
self.mock_row = None

def row(self, row_key):
self.rows_made.append(row_key)
return self.mock_row

def mutate_rows(self, rows):
self.rows_mutate.extend(rows)

0 comments on commit 50c1562

Please sign in to comment.