Skip to content

Commit

Permalink
Merge pull request #1303 from dhermes/bigtable-row-filter-3
Browse files Browse the repository at this point in the history
Adding Bigtable row filters for integer offset/limits.
  • Loading branch information
dhermes committed Dec 17, 2015
2 parents 6d01c6d + 05ed5b7 commit d77add9
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 0 deletions.
69 changes: 69 additions & 0 deletions gcloud/bigtable/row.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,72 @@ def to_pb(self):
:returns: The converted current object.
"""
return data_pb2.RowFilter(value_regex_filter=self.regex)


class _CellCountFilter(RowFilter):
"""Row filter that uses an integer count of cells.
The cell count is used as an offset or a limit for the number
of results returned.
:type num_cells: int
:param num_cells: An integer count / offset / limit.
"""

def __init__(self, num_cells):
self.num_cells = num_cells

def __eq__(self, other):
if not isinstance(other, self.__class__):
return False
return other.num_cells == self.num_cells


class CellsRowOffsetFilter(_CellCountFilter):
"""Row filter to skip cells in a row.
:type num_cells: int
:param num_cells: Skips the first N cells of the row.
"""

def to_pb(self):
"""Converts the row filter to a protobuf.
:rtype: :class:`.data_pb2.RowFilter`
:returns: The converted current object.
"""
return data_pb2.RowFilter(cells_per_row_offset_filter=self.num_cells)


class CellsRowLimitFilter(_CellCountFilter):
"""Row filter to limit cells in a row.
:type num_cells: int
:param num_cells: Matches only the first N cells of the row.
"""

def to_pb(self):
"""Converts the row filter to a protobuf.
:rtype: :class:`.data_pb2.RowFilter`
:returns: The converted current object.
"""
return data_pb2.RowFilter(cells_per_row_limit_filter=self.num_cells)


class CellsColumnLimitFilter(_CellCountFilter):
"""Row filter to limit cells in a column.
:type num_cells: int
:param num_cells: Matches only the most recent N cells within each column.
This filters a (family name, column) pair, based on
timestamps of each cell.
"""

def to_pb(self):
"""Converts the row filter to a protobuf.
:rtype: :class:`.data_pb2.RowFilter`
:returns: The converted current object.
"""
return data_pb2.RowFilter(cells_per_column_limit_filter=self.num_cells)
92 changes: 92 additions & 0 deletions gcloud/bigtable/test_row.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,95 @@ def test_to_pb(self):
pb_val = row_filter.to_pb()
expected_pb = data_pb2.RowFilter(value_regex_filter=regex)
self.assertEqual(pb_val, expected_pb)


class Test_CellCountFilter(unittest2.TestCase):

def _getTargetClass(self):
from gcloud.bigtable.row import _CellCountFilter
return _CellCountFilter

def _makeOne(self, *args, **kwargs):
return self._getTargetClass()(*args, **kwargs)

def test_constructor(self):
num_cells = object()
row_filter = self._makeOne(num_cells)
self.assertTrue(row_filter.num_cells is num_cells)

def test___eq__type_differ(self):
num_cells = object()
row_filter1 = self._makeOne(num_cells=num_cells)
row_filter2 = object()
self.assertNotEqual(row_filter1, row_filter2)

def test___eq__same_value(self):
num_cells = object()
row_filter1 = self._makeOne(num_cells=num_cells)
row_filter2 = self._makeOne(num_cells=num_cells)
self.assertEqual(row_filter1, row_filter2)

def test___ne__same_value(self):
num_cells = object()
row_filter1 = self._makeOne(num_cells=num_cells)
row_filter2 = self._makeOne(num_cells=num_cells)
comparison_val = (row_filter1 != row_filter2)
self.assertFalse(comparison_val)


class TestCellsRowOffsetFilter(unittest2.TestCase):

def _getTargetClass(self):
from gcloud.bigtable.row import CellsRowOffsetFilter
return CellsRowOffsetFilter

def _makeOne(self, *args, **kwargs):
return self._getTargetClass()(*args, **kwargs)

def test_to_pb(self):
from gcloud.bigtable._generated import bigtable_data_pb2 as data_pb2

num_cells = 76
row_filter = self._makeOne(num_cells)
pb_val = row_filter.to_pb()
expected_pb = data_pb2.RowFilter(cells_per_row_offset_filter=num_cells)
self.assertEqual(pb_val, expected_pb)


class TestCellsRowLimitFilter(unittest2.TestCase):

def _getTargetClass(self):
from gcloud.bigtable.row import CellsRowLimitFilter
return CellsRowLimitFilter

def _makeOne(self, *args, **kwargs):
return self._getTargetClass()(*args, **kwargs)

def test_to_pb(self):
from gcloud.bigtable._generated import bigtable_data_pb2 as data_pb2

num_cells = 189
row_filter = self._makeOne(num_cells)
pb_val = row_filter.to_pb()
expected_pb = data_pb2.RowFilter(cells_per_row_limit_filter=num_cells)
self.assertEqual(pb_val, expected_pb)


class TestCellsColumnLimitFilter(unittest2.TestCase):

def _getTargetClass(self):
from gcloud.bigtable.row import CellsColumnLimitFilter
return CellsColumnLimitFilter

def _makeOne(self, *args, **kwargs):
return self._getTargetClass()(*args, **kwargs)

def test_to_pb(self):
from gcloud.bigtable._generated import bigtable_data_pb2 as data_pb2

num_cells = 10
row_filter = self._makeOne(num_cells)
pb_val = row_filter.to_pb()
expected_pb = data_pb2.RowFilter(
cells_per_column_limit_filter=num_cells)
self.assertEqual(pb_val, expected_pb)

0 comments on commit d77add9

Please sign in to comment.