Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementing Bigtable ValueRangeFilter. #1309

Merged
merged 1 commit into from
Dec 22, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions gcloud/bigtable/row.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,93 @@ def to_pb(self):
return data_pb2.RowFilter(value_regex_filter=self.regex)


class ValueRangeFilter(RowFilter):
"""A range of values to restrict to in a row filter.
Will only match cells that have values in this range.
Both the start and end value can be included or excluded in the range.
By default, we include them both, but this can be changed with optional
flags.
:type start_value: bytes
:param start_value: The start of the range of values. If no value is used,
the backend applies no lower bound to the values.
:type end_value: bytes
:param end_value: The end of the range of values. If no value is used,
the backend applies no upper bound to the values.
:type inclusive_start: bool
:param inclusive_start: Boolean indicating if the start value should be
included in the range (or excluded). Defaults
to :data:`True` if ``start_value`` is passed and
no ``inclusive_start`` was given.
:type inclusive_end: bool
:param inclusive_end: Boolean indicating if the end value should be
included in the range (or excluded). Defaults
to :data:`True` if ``end_value`` is passed and
no ``inclusive_end`` was given.
:raises: :class:`ValueError <exceptions.ValueError>` if ``inclusive_start``
is set but no ``start_value`` is given or if ``inclusive_end``
is set but no ``end_value`` is given
"""

def __init__(self, start_value=None, end_value=None,
inclusive_start=None, inclusive_end=None):
if inclusive_start is None:
inclusive_start = True
elif start_value is None:
raise ValueError('Inclusive start was specified but no '
'start value was given.')
self.start_value = start_value
self.inclusive_start = inclusive_start

if inclusive_end is None:
inclusive_end = True
elif end_value is None:
raise ValueError('Inclusive end was specified but no '
'end value was given.')
self.end_value = end_value
self.inclusive_end = inclusive_end

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.


def __eq__(self, other):
if not isinstance(other, self.__class__):
return False
return (other.start_value == self.start_value and
other.end_value == self.end_value and
other.inclusive_start == self.inclusive_start and
other.inclusive_end == self.inclusive_end)

def to_pb(self):
"""Converts the row filter to a protobuf.
First converts to a :class:`.data_pb2.ValueRange` and then uses
it to create a row filter protobuf.
:rtype: :class:`.data_pb2.RowFilter`
:returns: The converted current object.
"""
value_range_kwargs = {}
if self.start_value is not None:
if self.inclusive_start:
key = 'start_value_inclusive'
else:
key = 'start_value_exclusive'
value_range_kwargs[key] = _to_bytes(self.start_value)
if self.end_value is not None:
if self.inclusive_end:
key = 'end_value_inclusive'
else:
key = 'end_value_exclusive'
value_range_kwargs[key] = _to_bytes(self.end_value)

value_range = data_pb2.ValueRange(**value_range_kwargs)
return data_pb2.RowFilter(value_range_filter=value_range)


class _CellCountFilter(RowFilter):
"""Row filter that uses an integer count of cells.
Expand Down
101 changes: 101 additions & 0 deletions gcloud/bigtable/test_row.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,107 @@ def test_to_pb(self):
self.assertEqual(pb_val, expected_pb)


class TestValueRangeFilter(unittest2.TestCase):

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

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

def test_constructor_defaults(self):
row_filter = self._makeOne()
self.assertEqual(row_filter.start_value, None)
self.assertEqual(row_filter.end_value, None)
self.assertTrue(row_filter.inclusive_start)
self.assertTrue(row_filter.inclusive_end)

def test_constructor_explicit(self):
start_value = object()
end_value = object()
inclusive_start = object()
inclusive_end = object()
row_filter = self._makeOne(start_value=start_value,
end_value=end_value,
inclusive_start=inclusive_start,
inclusive_end=inclusive_end)
self.assertTrue(row_filter.start_value is start_value)
self.assertTrue(row_filter.end_value is end_value)
self.assertTrue(row_filter.inclusive_start is inclusive_start)
self.assertTrue(row_filter.inclusive_end is inclusive_end)

def test_constructor_bad_start(self):
self.assertRaises(ValueError, self._makeOne, inclusive_start=True)

def test_constructor_bad_end(self):
self.assertRaises(ValueError, self._makeOne, inclusive_end=True)

def test___eq__(self):
start_value = object()
end_value = object()
inclusive_start = object()
inclusive_end = object()
row_filter1 = self._makeOne(start_value=start_value,
end_value=end_value,
inclusive_start=inclusive_start,
inclusive_end=inclusive_end)
row_filter2 = self._makeOne(start_value=start_value,
end_value=end_value,
inclusive_start=inclusive_start,
inclusive_end=inclusive_end)
self.assertEqual(row_filter1, row_filter2)

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

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

row_filter = self._makeOne()
expected_pb = data_pb2.RowFilter(
value_range_filter=data_pb2.ValueRange())
self.assertEqual(row_filter.to_pb(), expected_pb)

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

value = b'some-value'
row_filter = self._makeOne(start_value=value)
val_range_pb = data_pb2.ValueRange(start_value_inclusive=value)
expected_pb = data_pb2.RowFilter(value_range_filter=val_range_pb)
self.assertEqual(row_filter.to_pb(), expected_pb)

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

value = b'some-value'
row_filter = self._makeOne(start_value=value, inclusive_start=False)
val_range_pb = data_pb2.ValueRange(start_value_exclusive=value)
expected_pb = data_pb2.RowFilter(value_range_filter=val_range_pb)
self.assertEqual(row_filter.to_pb(), expected_pb)

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

value = b'some-value'
row_filter = self._makeOne(end_value=value)
val_range_pb = data_pb2.ValueRange(end_value_inclusive=value)
expected_pb = data_pb2.RowFilter(value_range_filter=val_range_pb)
self.assertEqual(row_filter.to_pb(), expected_pb)

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

value = b'some-value'
row_filter = self._makeOne(end_value=value, inclusive_end=False)
val_range_pb = data_pb2.ValueRange(end_value_exclusive=value)
expected_pb = data_pb2.RowFilter(value_range_filter=val_range_pb)
self.assertEqual(row_filter.to_pb(), expected_pb)


class Test_CellCountFilter(unittest2.TestCase):

def _getTargetClass(self):
Expand Down