From d8d1ed8dbdc7fe64f66a1006bd03d1a6cc6a5960 Mon Sep 17 00:00:00 2001 From: Danny Hermes Date: Fri, 15 Jan 2016 01:55:31 -0800 Subject: [PATCH] Adding Bigtable Row.append_cell_value. This accumulates a row mutation without actually sending a request (this is similar to a batch in datastore). --- gcloud/bigtable/row.py | 32 ++++++++++++++++++++++++++++++++ gcloud/bigtable/test_row.py | 17 +++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/gcloud/bigtable/row.py b/gcloud/bigtable/row.py index 2f58e3f4c102..ef4b7ef6fa6c 100644 --- a/gcloud/bigtable/row.py +++ b/gcloud/bigtable/row.py @@ -42,6 +42,38 @@ def __init__(self, row_key, table, filter_=None): self._row_key = _to_bytes(row_key) self._table = table self._filter = filter_ + self._rule_pb_list = [] + + def append_cell_value(self, column_family_id, column, value): + """Appends a value to an existing cell. + + .. note:: + + This method adds a read-modify rule protobuf to the accumulated + read-modify rules on this :class:`Row`, but does not make an API + request. To actually send an API request (with the rules) to the + Google Cloud Bigtable API, call :meth:`commit_modifications`. + + :type column_family_id: str + :param column_family_id: The column family that contains the column. + Must be of the form + ``[_a-zA-Z0-9][-_.a-zA-Z0-9]*``. + + :type column: bytes + :param column: The column within the column family where the cell + is located. + + :type value: bytes + :param value: The value to append to the existing value in the cell. If + the targeted cell is unset, it will be treated as + containing the empty string. + """ + column = _to_bytes(column) + value = _to_bytes(value) + rule_pb = data_pb2.ReadModifyWriteRule(family_name=column_family_id, + column_qualifier=column, + append_value=value) + self._rule_pb_list.append(rule_pb) class RowFilter(object): diff --git a/gcloud/bigtable/test_row.py b/gcloud/bigtable/test_row.py index 1920f2539b49..ea41c5d6ccc5 100644 --- a/gcloud/bigtable/test_row.py +++ b/gcloud/bigtable/test_row.py @@ -49,6 +49,23 @@ def test_constructor_with_non_bytes(self): with self.assertRaises(TypeError): self._makeOne(row_key, None) + def test_append_cell_value(self): + from gcloud.bigtable._generated import bigtable_data_pb2 as data_pb2 + + table = object() + row_key = b'row_key' + row = self._makeOne(row_key, table) + self.assertEqual(row._rule_pb_list, []) + + column = b'column' + column_family_id = u'column_family_id' + value = b'bytes-val' + row.append_cell_value(column_family_id, column, value) + expected_pb = data_pb2.ReadModifyWriteRule( + family_name=column_family_id, column_qualifier=column, + append_value=value) + self.assertEqual(row._rule_pb_list, [expected_pb]) + class Test_BoolFilter(unittest2.TestCase):