8000 Adding Bigtable Row.increment_cell_value. · googleapis/google-cloud-python@51743b3 · GitHub < 10000 /head>
[go: up one dir, main page]

Skip to content

Commit 51743b3

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 5cd168c commit 51743b3

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

gcloud/bigtable/row.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,36 @@ def _get_mutations(self, state=None):
123123
else:
124124
return self._false_pb_mutations
125125

126+
def increment_cell_value(self, column_family_id, column, int_value):
127+
"""Increments a value in an existing cell.
128+
Assumes the value in the cell is stored as a 64 bit integer
129+
serialized to bytes.
130+
.. note::
131+
This method adds a read-modify rule protobuf to the accumulated
132+
read-modify rules on this :class:`Row`, but does not make an API
133+
request. To actually send an API request (with the rules) to the
134+
Google Cloud Bigtable API, call :meth:`commit_modifications`.
135+
:type column_family_id: str
136+
:param column_family_id: The column family that contains the column.
137+
Must be of the form
138+
``[_a-zA-Z0-9][-_.a-zA-Z0-9]*``.
139+
:type column: bytes
140+
:param column: The column within the column family where the cell
141+
is located.
142+
:type int_value: int
143+
:param int_value: The value to increment the existing value in the cell
144+
by. If the targeted cell is unset, it will be treated
145+
as containing a zero. Otherwise, the targeted cell
146+
must contain an 8-byte value (interpreted as a 64-bit
147+
big-endian signed integer), or the entire request
148+
will fail.
149+
"""
150+
column = _to_bytes(column)
151+
rule_pb = data_pb2.ReadModifyWriteRule(family_name=column_family_id,
152+
column_qualifier=column,
153+
increment_amount=int_value)
154+
self._rule_pb_list.append(rule_pb)
155+
126156

127157
class RowFilter(object):
128158
"""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
@@ -106,6 +106,23 @@ def test_append_cell_value(self):
106106
append_value=value)
107107
self.assertEqual(row._rule_pb_list, [expected_pb])
108108

109+
def test_increment_cell_value(self):
110+
from gcloud.bigtable._generated import bigtable_data_pb2 as data_pb2
111+
112+
table = object()
113+
row_key = b'row_key'
114+
row = self._makeOne(row_key, table)
115+
self.assertEqual(row._rule_pb_list, [])
116+
117+
column = b'column'
118+
column_family_id = u'column_family_id'
119+
int_value = 281330
120+
row.increment_cell_value(column_family_id, column, int_value)
121+
expected_pb = data_pb2.ReadModifyWriteRule(
122+
family_name=column_family_id, column_qualifier=column,
123+
increment_amount=int_value)
124+
self.assertEqual(row._rule_pb_list, [expected_pb])
125+
109126

110127
class Test_BoolFilter(unittest2.TestCase):
111128

0 commit comments

Comments
 (0)
0