Skip to content

Commit ee91261

Browse files
committed
Adding Bigtable Row.increment_cell_value.
Similar to googleapis#1388. The API accepts integers and then encodes them as bytes when stored in the table.
1 parent 3f18953 commit ee91261

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

gcloud/bigtable/row.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,43 @@ def __init__(self, row_key, table, filter_=None):
4242
self._row_key = _to_bytes(row_key)
4343
self._table = table
4444
self._filter = filter_
45+
self._rule_pb_list = []
46+
47+
def increment_cell_value(self, column_family_id, column, int_value):
48+
"""Increments a value in an existing cell.
49+
50+
Assumes the value in the cell is stored as a 64 bit integer
51+
serialized to bytes.
52+
53+
.. note::
54+
55+
This method adds a read-modify rule protobuf to the accumulated
56+
read-modify rules on this :class:`Row`, but does not make an API
57+
request. To actually send an API request (with the rules) to the
58+
Google Cloud Bigtable API, call :meth:`commit_modifications`.
59+
60+
:type column_family_id: str
61+
:param column_family_id: The column family that contains the column.
62+
Must be of the form
63+
``[_a-zA-Z0-9][-_.a-zA-Z0-9]*``.
64+
65+
:type column: bytes
66+
:param column: The column within the column family where the cell
67+
is located.
68+
69+
:type int_value: int
70+
:param int_value: The value to increment the existing value in the cell
71+
by. If the targeted cell is unset, it will be treated
72+
as containing a zero. Otherwise, the targeted cell
73+
must contain an 8-byte value (interpreted as a 64-bit
74+
big-endian signed integer), or the entire request
75+
will fail.
76+
"""
77+
column = _to_bytes(column)
78+
rule_pb = data_pb2.ReadModifyWriteRule(family_name=column_family_id,
79+
column_qualifier=column,
80+
increment_amount=int_value)
81+
self._rule_pb_list.append(rule_pb)
4582

4683

4784
class RowFilter(object):

gcloud/bigtable/test_row.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,23 @@ def test_constructor_with_non_bytes(self):
4949
with self.assertRaises(TypeError):
5050
self._makeOne(row_key, None)
5151

52+
def test_increment_cell_value(self):
53+
from gcloud.bigtable._generated import bigtable_data_pb2 as data_pb2
54+
55+
table = object()
56+
row_key = b'row_key'
57+
row = self._makeOne(row_key, table)
58+
self.assertEqual(row._rule_pb_list, [])
59+
60+
column = b'column'
61+
column_family_id = u'column_family_id'
62+
int_value = 281330
63+
row.increment_cell_value(column_family_id, column, int_value)
64+
expected_pb = data_pb2.ReadModifyWriteRule(
65+
family_name=column_family_id, column_qualifier=column,
66+
increment_amount=int_value)
67+
self.assertEqual(row._rule_pb_list, [expected_pb])
68+
5269

5370
class Test_BoolFilter(unittest2.TestCase):
5471

0 commit comments

Comments
 (0)