Skip to content

Commit

Permalink
Using the low-level table on HappyBase table whenever possible.
Browse files Browse the repository at this point in the history
  • Loading branch information
dhermes committed Sep 11, 2015
1 parent 8b5c351 commit c2bf350
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 77 deletions.
4 changes: 1 addition & 3 deletions gcloud_bigtable/happybase/batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

from gcloud_bigtable._helpers import _microseconds_to_timestamp
from gcloud_bigtable.row import TimestampRange
from gcloud_bigtable.table import Table as _LowLevelTable


_WAL_SENTINEL = object()
Expand Down Expand Up @@ -170,8 +169,7 @@ def _get_row(self, row_key):
:returns: The newly created or stored row that will hold mutations.
"""
if row_key not in self._row_map:
table = _LowLevelTable(self._table.name,
self._table.connection._cluster)
table = self._table._low_level_table
self._row_map[row_key] = table.row(row_key)

return self._row_map[row_key]
Expand Down
6 changes: 2 additions & 4 deletions gcloud_bigtable/happybase/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,7 @@ def families(self):
:returns: Mapping from column family name to garbage collection rule
for a column family.
"""
table = _LowLevelTable(self.name, self.connection._cluster)
column_family_map = table.list_column_families()
column_family_map = self._low_level_table.list_column_families()
return {col_fam: _gc_rule_to_dict(col_fam_obj.gc_rule)
for col_fam, col_fam_obj in column_family_map.items()}

Expand Down Expand Up @@ -539,8 +538,7 @@ def counter_inc(self, row, column, value=1):
:rtype: int
:returns: Counter value after incrementing.
"""
table = _LowLevelTable(self.name, self.connection._cluster)
row = table.row(row)
row = self._low_level_table.row(row)
column_family_id, column_qualifier = column.split(':')
row.increment_cell_value(column_family_id, column_qualifier, value)
modified_cells = row.commit_modifications()
Expand Down
40 changes: 8 additions & 32 deletions gcloud_bigtable/happybase/test_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,41 +208,24 @@ def test__get_row_exists(self):
self.assertEqual(result, row_obj)

def test__get_row_create_new(self):
from gcloud_bigtable._testing import _Monkey
from gcloud_bigtable.happybase import batch as MUT

# Make mock batch and make sure we can create a low-level table.
cluster = object()
connection = _MockConnection(cluster)
name = 'name'
table = _MockTable(name, connection)
low_level_table = _MockLowLevelTable()
table = _MockTable(low_level_table)
batch = self._makeOne(table)

# Make sure row map is empty.
self.assertEqual(batch._row_map, {})

# Customize/capture mock table creation.
row_key = 'row-key'
mock_row = object()
tables_constructed = []

def make_low_level_table(*args, **kwargs):
result = _MockLowLevelTable(*args, **kwargs)
tables_constructed.append(result)
result.mock_row = mock_row
return result
low_level_table.mock_row = mock_row = object()

# Actually get the row (which creates a row via a low-level table).
with _Monkey(MUT, _LowLevelTable=make_low_level_table):
result = batch._get_row(row_key)

row_key = 'row-key'
result = batch._get_row(row_key)
self.assertEqual(result, mock_row)

# Check all the things that were constructed.
table_instance, = tables_constructed
self.assertEqual(table_instance.args, (name, cluster))
self.assertEqual(table_instance.kwargs, {})
self.assertEqual(table_instance.rows_made, [row_key])
self.assertEqual(low_level_table.rows_made, [row_key])
# Check how the batch was updated.
self.assertEqual(batch._row_map, {row_key: mock_row})

Expand Down Expand Up @@ -506,17 +489,10 @@ def delete_cells(self, *args, **kwargs):
self.delete_cells_calls.append((args, kwargs))


class _MockConnection(object):

def __init__(self, cluster):
self._cluster = cluster


class _MockTable(object):

def __init__(self, name, connection):
self.name = name
self.connection = connection
def __init__(self, low_level_table):
self._low_level_table = low_level_table


class _MockLowLevelTable(object):
Expand Down
51 changes: 13 additions & 38 deletions gcloud_bigtable/happybase/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,35 +211,24 @@ def test_families(self):
from gcloud_bigtable.happybase import table as MUT

name = 'table-name'
cluster = object()
connection = _Connection(cluster)
connection = None
table = self._makeOne(name, connection)
table._low_level_table = _MockLowLevelTable()

tables_constructed = []
# Mock the column families to be returned.
col_fam_name = 'fam'
gc_rule = object()
col_fam = _MockLowLevelColumnFamily(col_fam_name, gc_rule=gc_rule)
col_fams = {col_fam_name: col_fam}

def make_low_level_table(*args, **kwargs):
result = _MockLowLevelTable(*args, **kwargs)
result.column_families = col_fams
tables_constructed.append(result)
return result
table._low_level_table.column_families = col_fams

to_dict_result = object()
mock_gc_rule_to_dict = _MockCalled(to_dict_result)
with _Monkey(MUT, _LowLevelTable=make_low_level_table,
_gc_rule_to_dict=mock_gc_rule_to_dict):
with _Monkey(MUT, _gc_rule_to_dict=mock_gc_rule_to_dict):
result = table.families()

self.assertEqual(result, {col_fam_name: to_dict_result})

# Just one table would have been created.
table_instance, = tables_constructed
self.assertEqual(table_instance.args, (name, cluster))
self.assertEqual(table_instance.kwargs, {})
self.assertEqual(table_instance.list_column_families_calls, 1)
self.assertEqual(table._low_level_table.list_column_families_calls, 1)

# Check the input to our mock.
mock_gc_rule_to_dict.check_called(self, [(gc_rule,)])
Expand Down Expand Up @@ -487,35 +476,21 @@ def counter_inc(self, row, column, value=1):
self.assertEqual(TableWithInc.incremented, [(row, column, -dec_value)])

def _counter_inc_helper(self, row, column, value, commit_result):
from gcloud_bigtable._testing import _Monkey
from gcloud_bigtable.happybase import table as MUT

name = 'table-name'
cluster = object()
connection = _Connection(cluster)
connection = None
table = self._makeOne(name, connection)
tables_constructed = []
# Mock the return values.
table._low_level_table = _MockLowLevelTable()
table._low_level_table.row_values[row] = _MockLowLevelRow(
row, commit_result=commit_result)

def make_low_level_table(*args, **kwargs):
result = _MockLowLevelTable(*args, **kwargs)
tables_constructed.append(result)
result.row_values[row] = _MockLowLevelRow(
row, commit_result=commit_result)
return result

with _Monkey(MUT, _LowLevelTable=make_low_level_table):
result = table.counter_inc(row, column, value=value)
result = table.counter_inc(row, column, value=value)

incremented_value = value + _MockLowLevelRow.COUNTER_DEFAULT
self.assertEqual(result, incremented_value)

# Just one table would have been created.
table_instance, = tables_constructed
self.assertEqual(table_instance.args, (name, cluster))
self.assertEqual(table_instance.kwargs, {})

# Check the row values returned.
row_obj = table_instance.row_values[row]
row_obj = table._low_level_table.row_values[row]
self.assertEqual(row_obj.counts,
{tuple(column.split(':')): incremented_value})

Expand Down

0 comments on commit c2bf350

Please sign in to comment.