Skip to content

Commit ac6f9ae

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 cfc20f2 commit ac6f9ae

2 files changed

Lines changed: 47 additions & 0 deletions

File tree

gcloud/bigtable/row.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,36 @@ def append_cell_value(self, column_family_id, column, value):
7575
append_value=value)
7676
self._rule_pb_list.append(rule_pb)
7777

78+
def increment_cell_value(self, column_family_id, column, int_value):
79+
"""Increments a value in an existing cell.
80+
Assumes the value in the cell is stored as a 64 bit integer
81+
serialized to bytes.
82+
.. note::
83+
This method adds a read-modify rule protobuf to the accumulated
84+
read-modify rules on this :class:`Row`, but does not make an API
85+
request. To actually send an API request (with the rules) to the
86+
Google Cloud Bigtable API, call :meth:`commit_modifications`.
87+
:type column_family_id: str
88+
:param column_family_id: The column family that contains the column.
89+
Must be of the form
90+
``[_a-zA-Z0-9][-_.a-zA-Z0-9]*``.
91+
:type column: bytes
92+
:param column: The column within the column family where the cell
93+
is located.
94+
:type int_value: int
95+
:param int_value: The value to increment the existing value in the cell
96+
by. If the targeted cell is unset, it will be treated
97+
as containing a zero. Otherwise, the targeted cell
98+
must contain an 8-byte value (interpreted as a 64-bit
99+
big-endian signed integer), or the entire request
100+
will fail.
101+
"""
102+
column = _to_bytes(column)
103+
rule_pb = data_pb2.ReadModifyWriteRule(family_name=column_family_id,
104+
column_qualifier=column,
105+
increment_amount=int_value)
106+
self._rule_pb_list.append(rule_pb)
107+
78108

79109
class RowFilter(object):
80110
"""Basic filter to apply to cells in a row.

gcloud/bigtable/test_row.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,23 @@ def test_append_cell_value(self):
6666
append_value=value)
6767
self.assertEqual(row._rule_pb_list, [expected_pb])
6868

69+
def test_increment_cell_value(self):
70+
from gcloud.bigtable._generated import bigtable_data_pb2 as data_pb2
71+
72+
table = object()
73+
row_key = b'row_key'
74+
row = self._makeOne(row_key, table)
75+
self.assertEqual(row._rule_pb_list, [])
76+
77+
column = b'column'
78+
column_family_id = u'column_family_id'
79+
int_value = 281330
80+
row.increment_cell_value(column_family_id, column, int_value)
81+
expected_pb = data_pb2.ReadModifyWriteRule(
82+
family_name=column_family_id, column_qualifier=column,
83+
increment_amount=int_value)
84+
self.assertEqual(row._rule_pb_list, [expected_pb])
85+
6986

7087
class Test_BoolFilter(unittest2.TestCase):
7188

0 commit comments

Comments
 (0)