Skip to content

Commit d77add9

Browse files
committed
Merge pull request #1303 from dhermes/bigtable-row-filter-3
Adding Bigtable row filters for integer offset/limits.
2 parents 6d01c6d + 05ed5b7 commit d77add9

2 files changed

Lines changed: 161 additions & 0 deletions

File tree

gcloud/bigtable/row.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,72 @@ def to_pb(self):
184184
:returns: The converted current object.
185185
"""
186186
return data_pb2.RowFilter(value_regex_filter=self.regex)
187+
188+
189+
class _CellCountFilter(RowFilter):
190+
"""Row filter that uses an integer count of cells.
191+
192+
The cell count is used as an offset or a limit for the number
193+
of results returned.
194+
195+
:type num_cells: int
196+
:param num_cells: An integer count / offset / limit.
197+
"""
198+
199+
def __init__(self, num_cells):
200+
self.num_cells = num_cells
201+
202+
def __eq__(self, other):
203+
if not isinstance(other, self.__class__):
204+
return False
205+
return other.num_cells == self.num_cells
206+
207+
208+
class CellsRowOffsetFilter(_CellCountFilter):
209+
"""Row filter to skip cells in a row.
210+
211+
:type num_cells: int
212+
:param num_cells: Skips the first N cells of the row.
213+
"""
214+
215+
def to_pb(self):
216+
"""Converts the row filter to a protobuf.
217+
218+
:rtype: :class:`.data_pb2.RowFilter`
219+
:returns: The converted current object.
220+
"""
221+
return data_pb2.RowFilter(cells_per_row_offset_filter=self.num_cells)
222+
223+
224+
class CellsRowLimitFilter(_CellCountFilter):
225+
"""Row filter to limit cells in a row.
226+
227+
:type num_cells: int
228+
:param num_cells: Matches only the first N cells of the row.
229+
"""
230+
231+
def to_pb(self):
232+
"""Converts the row filter to a protobuf.
233+
234+
:rtype: :class:`.data_pb2.RowFilter`
235+
:returns: The converted current object.
236+
"""
237+
return data_pb2.RowFilter(cells_per_row_limit_filter=self.num_cells)
238+
239+
240+
class CellsColumnLimitFilter(_CellCountFilter):
241+
"""Row filter to limit cells in a column.
242+
243+
:type num_cells: int
244+
:param num_cells: Matches only the most recent N cells within each column.
245+
This filters a (family name, column) pair, based on
246+
timestamps of each cell.
247+
"""
248+
249+
def to_pb(self):
250+
"""Converts the row filter to a protobuf.
251+
252+
:rtype: :class:`.data_pb2.RowFilter`
253+
:returns: The converted current object.
254+
"""
255+
return data_pb2.RowFilter(cells_per_column_limit_filter=self.num_cells)

gcloud/bigtable/test_row.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,95 @@ def test_to_pb(self):
156156
pb_val = row_filter.to_pb()
157157
expected_pb = data_pb2.RowFilter(value_regex_filter=regex)
158158
self.assertEqual(pb_val, expected_pb)
159+
160+
161+
class Test_CellCountFilter(unittest2.TestCase):
162+
163+
def _getTargetClass(self):
164+
from gcloud.bigtable.row import _CellCountFilter
165+
return _CellCountFilter
166+
167+
def _makeOne(self, *args, **kwargs):
168+
return self._getTargetClass()(*args, **kwargs)
169+
170+
def test_constructor(self):
171+
num_cells = object()
172+
row_filter = self._makeOne(num_cells)
173+
self.assertTrue(row_filter.num_cells is num_cells)
174+
175+
def test___eq__type_differ(self):
176+
num_cells = object()
177+
row_filter1 = self._makeOne(num_cells=num_cells)
178+
row_filter2 = object()
179+
self.assertNotEqual(row_filter1, row_filter2)
180+
181+
def test___eq__same_value(self):
182+
num_cells = object()
183+
row_filter1 = self._makeOne(num_cells=num_cells)
184+
row_filter2 = self._makeOne(num_cells=num_cells)
185+
self.assertEqual(row_filter1, row_filter2)
186+
187+
def test___ne__same_value(self):
188+
num_cells = object()
189+
row_filter1 = self._makeOne(num_cells=num_cells)
190+
row_filter2 = self._makeOne(num_cells=num_cells)
191+
comparison_val = (row_filter1 != row_filter2)
192+
self.assertFalse(comparison_val)
193+
194+
195+
class TestCellsRowOffsetFilter(unittest2.TestCase):
196+
197+
def _getTargetClass(self):
198+
from gcloud.bigtable.row import CellsRowOffsetFilter
199+
return CellsRowOffsetFilter
200+
201+
def _makeOne(self, *args, **kwargs):
202+
return self._getTargetClass()(*args, **kwargs)
203+
204+
def test_to_pb(self):
205+
from gcloud.bigtable._generated import bigtable_data_pb2 as data_pb2
206+
207+
num_cells = 76
208+
row_filter = self._makeOne(num_cells)
209+
pb_val = row_filter.to_pb()
210+
expected_pb = data_pb2.RowFilter(cells_per_row_offset_filter=num_cells)
211+
self.assertEqual(pb_val, expected_pb)
212+
213+
214+
class TestCellsRowLimitFilter(unittest2.TestCase):
215+
216+
def _getTargetClass(self):
217+
from gcloud.bigtable.row import CellsRowLimitFilter
218+
return CellsRowLimitFilter
219+
220+
def _makeOne(self, *args, **kwargs):
221+
return self._getTargetClass()(*args, **kwargs)
222+
223+
def test_to_pb(self):
224+
from gcloud.bigtable._generated import bigtable_data_pb2 as data_pb2
225+
226+
num_cells = 189
227+
row_filter = self._makeOne(num_cells)
228+
pb_val = row_filter.to_pb()
229+
expected_pb = data_pb2.RowFilter(cells_per_row_limit_filter=num_cells)
230+
self.assertEqual(pb_val, expected_pb)
231+
232+
233+
class TestCellsColumnLimitFilter(unittest2.TestCase):
234+
235+
def _getTargetClass(self):
236+
from gcloud.bigtable.row import CellsColumnLimitFilter
237+
return CellsColumnLimitFilter
238+
239+
def _makeOne(self, *args, **kwargs):
240+
return self._getTargetClass()(*args, **kwargs)
241+
242+
def test_to_pb(self):
243+
from gcloud.bigtable._generated import bigtable_data_pb2 as data_pb2
244+
245+
num_cells = 10
246+
row_filter = self._makeOne(num_cells)
247+
pb_val = row_filter.to_pb()
248+
expected_pb = data_pb2.RowFilter(
249+
cells_per_column_limit_filter=num_cells)
250+
self.assertEqual(pb_val, expected_pb)

0 commit comments

Comments
 (0)