From 2b9876b1ed6e2e2c3fa4d652d7889053b448d692 Mon Sep 17 00:00:00 2001 From: zakons Date: Wed, 10 Jan 2018 00:13:25 -0500 Subject: [PATCH 1/4] Performance enhancement for Cell timestamps. --- bigtable/google/cloud/bigtable/row_data.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/bigtable/google/cloud/bigtable/row_data.py b/bigtable/google/cloud/bigtable/row_data.py index 9bde1c0cb5a3..abc2191d159f 100644 --- a/bigtable/google/cloud/bigtable/row_data.py +++ b/bigtable/google/cloud/bigtable/row_data.py @@ -35,9 +35,9 @@ class Cell(object): :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 @@ -50,17 +50,20 @@ 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): From 588b0576c4f39b6a6523bfac316ec2758390b0e1 Mon Sep 17 00:00:00 2001 From: zakons Date: Thu, 11 Jan 2018 00:25:34 -0500 Subject: [PATCH 2/4] Unit test case fixups. --- bigtable/google/cloud/bigtable/row_data.py | 4 ++-- bigtable/tests/unit/test_row_data.py | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/bigtable/google/cloud/bigtable/row_data.py b/bigtable/google/cloud/bigtable/row_data.py index abc2191d159f..1687dc44f097 100644 --- a/bigtable/google/cloud/bigtable/row_data.py +++ b/bigtable/google/cloud/bigtable/row_data.py @@ -28,8 +28,8 @@ 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. diff --git a/bigtable/tests/unit/test_row_data.py b/bigtable/tests/unit/test_row_data.py index 7cfb1dc45d4e..3bb126b3206b 100644 --- a/bigtable/tests/unit/test_row_data.py +++ b/bigtable/tests/unit/test_row_data.py @@ -42,15 +42,16 @@ def _from_pb_test_helper(self, labels=None): 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() From 1943d726c4fdb7889dc83381d262bd2760dcee85 Mon Sep 17 00:00:00 2001 From: zakons Date: Thu, 11 Jan 2018 01:21:47 -0500 Subject: [PATCH 3/4] Unit test case fixup timestamp_micros. --- bigtable/tests/unit/test_row_data.py | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/bigtable/tests/unit/test_row_data.py b/bigtable/tests/unit/test_row_data.py index 3bb126b3206b..375097c54e79 100644 --- a/bigtable/tests/unit/test_row_data.py +++ b/bigtable/tests/unit/test_row_data.py @@ -19,6 +19,7 @@ class TestCell(unittest.TestCase): + timestamp_micros = 18738724000 # Make sure millis granularity @staticmethod def _get_target_class(): @@ -35,7 +36,7 @@ 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' @@ -62,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): @@ -81,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) From bd2c79c67b7186a15edad3434e40cb81a1dd16d3 Mon Sep 17 00:00:00 2001 From: zakons Date: Thu, 11 Jan 2018 23:35:46 -0500 Subject: [PATCH 4/4] Line length. --- bigtable/google/cloud/bigtable/row_data.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bigtable/google/cloud/bigtable/row_data.py b/bigtable/google/cloud/bigtable/row_data.py index 1687dc44f097..8dbce7a68c67 100644 --- a/bigtable/google/cloud/bigtable/row_data.py +++ b/bigtable/google/cloud/bigtable/row_data.py @@ -51,7 +51,8 @@ def from_pb(cls, cell_pb): :returns: The cell corresponding to the protobuf. """ if cell_pb.labels: - return cls(cell_pb.value, cell_pb.timestamp_micros, labels=cell_pb.labels) + return cls(cell_pb.value, cell_pb.timestamp_micros, + labels=cell_pb.labels) else: return cls(cell_pb.value, cell_pb.timestamp_micros)