Skip to content

Commit

Permalink
BigTable: Cell.from_pb() performance improvement (#4745)
Browse files Browse the repository at this point in the history
  • Loading branch information
zakons authored and tseaver committed Jan 19, 2018
1 parent 662489b commit 1ef5db6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
20 changes: 12 additions & 8 deletions bigtable/google/cloud/bigtable/row_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ class Cell(object):
:type value: bytes
:param value: The value stored in the cell.
:type timestamp: :class:`datetime.datetime`
:param timestamp: The timestamp when the cell was stored.
:type timestamp_micros: int
:param timestamp_micros: The timestamp_micros when the cell was stored.
:type labels: list
:param labels: (Optional) List of strings. Labels applied to the cell.
"""

def __init__(self, value, timestamp, labels=()):
def __init__(self, value, timestamp_micros, labels=()):
self.value = value
self.timestamp = timestamp
self.timestamp_micros = timestamp_micros
self.labels = list(labels)

@classmethod
Expand All @@ -50,17 +50,21 @@ def from_pb(cls, cell_pb):
:rtype: :class:`Cell`
:returns: The cell corresponding to the protobuf.
"""
timestamp = _datetime_from_microseconds(cell_pb.timestamp_micros)
if cell_pb.labels:
return cls(cell_pb.value, timestamp, labels=cell_pb.labels)
return cls(cell_pb.value, cell_pb.timestamp_micros,
labels=cell_pb.labels)
else:
return cls(cell_pb.value, timestamp)
return cls(cell_pb.value, cell_pb.timestamp_micros)

@property
def timestamp(self):
return _datetime_from_microseconds(self.timestamp_micros)

def __eq__(self, other):
if not isinstance(other, self.__class__):
return NotImplemented
return (other.value == self.value and
other.timestamp == self.timestamp and
other.timestamp_micros == self.timestamp_micros and
other.labels == self.labels)

def __ne__(self, other):
Expand Down
27 changes: 12 additions & 15 deletions bigtable/tests/unit/test_row_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@


class TestCell(unittest.TestCase):
timestamp_micros = 18738724000 # Make sure millis granularity

@staticmethod
def _get_target_class():
Expand All @@ -35,22 +36,23 @@ def _from_pb_test_helper(self, labels=None):
from google.cloud.bigtable._generated import (
data_pb2 as data_v2_pb2)

timestamp_micros = 18738724000 # Make sure millis granularity
timestamp_micros = TestCell.timestamp_micros
timestamp = _EPOCH + datetime.timedelta(microseconds=timestamp_micros)
value = b'value-bytes'

if labels is None:
cell_pb = data_v2_pb2.Cell(
value=value, timestamp_micros=timestamp_micros)
cell_expected = self._make_one(value, timestamp)
cell_expected = self._make_one(value, timestamp_micros)
else:
cell_pb = data_v2_pb2.Cell(
value=value, timestamp_micros=timestamp_micros, labels=labels)
cell_expected = self._make_one(value, timestamp, labels=labels)
cell_expected = self._make_one(value, timestamp_micros, labels=labels)

klass = self._get_target_class()
result = klass.from_pb(cell_pb)
self.assertEqual(result, cell_expected)
self.assertEqual(result.timestamp, timestamp)

def test_from_pb(self):
self._from_pb_test_helper()
Expand All @@ -61,16 +63,13 @@ def test_from_pb_with_labels(self):

def test_constructor(self):
value = object()
timestamp = object()
cell = self._make_one(value, timestamp)
cell = self._make_one(value, TestCell.timestamp_micros)
self.assertEqual(cell.value, value)
self.assertEqual(cell.timestamp, timestamp)

def test___eq__(self):
value = object()
timestamp = object()
cell1 = self._make_one(value, timestamp)
cell2 = self._make_one(value, timestamp)
cell1 = self._make_one(value, TestCell.timestamp_micros)
cell2 = self._make_one(value, TestCell.timestamp_micros)
self.assertEqual(cell1, cell2)

def test___eq__type_differ(self):
Expand All @@ -80,18 +79,16 @@ def test___eq__type_differ(self):

def test___ne__same_value(self):
value = object()
timestamp = object()
cell1 = self._make_one(value, timestamp)
cell2 = self._make_one(value, timestamp)
cell1 = self._make_one(value, TestCell.timestamp_micros)
cell2 = self._make_one(value, TestCell.timestamp_micros)
comparison_val = (cell1 != cell2)
self.assertFalse(comparison_val)

def test___ne__(self):
value1 = 'value1'
value2 = 'value2'
timestamp = object()
cell1 = self._make_one(value1, timestamp)
cell2 = self._make_one(value2, timestamp)
cell1 = self._make_one(value1, TestCell.timestamp_micros)
cell2 = self._make_one(value2, TestCell.timestamp_micros)
self.assertNotEqual(cell1, cell2)


Expand Down

0 comments on commit 1ef5db6

Please sign in to comment.