Skip to content

Commit 724fd9d

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

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

gcloud/bigtable/row.py

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,23 +46,18 @@ def __init__(self, row_key, table, filter_=None):
4646

4747
def append_cell_value(self, column_family_id, column, value):
4848
"""Appends a value to an existing cell.
49-
5049
.. note::
51-
5250
This method adds a read-modify rule protobuf to the accumulated
5351
read-modify rules on this :class:`Row`, but does not make an API
5452
request. To actually send an API request (with the rules) to the
5553
Google Cloud Bigtable API, call :meth:`commit_modifications`.
56-
5754
:type column_family_id: str
5855
:param column_family_id: The column family that contains the column.
5956
Must be of the form
6057
``[_a-zA-Z0-9][-_.a-zA-Z0-9]*``.
61-
6258
:type column: bytes
6359
:param column: The column within the column family where the cell
6460
is located.
65-
6661
:type value: bytes
6762
:param value: The value to append to the existing value in the cell. If
6863
the targeted cell is unset, it will be treated as
@@ -75,6 +70,36 @@ def append_cell_value(self, column_family_id, column, value):
7570
append_value=value)
7671
self._rule_pb_list.append(rule_pb)
7772

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

79104
class RowFilter(object):
80105
"""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)