Skip to content

Commit

Permalink
Adding Bigtable Row.increment_cell_value.
Browse files Browse the repository at this point in the history
Similar to googleapis#1388. The API accepts integers and then encodes them
as bytes when stored in the table.
  • Loading branch information
dhermes committed Jan 15, 2016
1 parent cfc20f2 commit 724fd9d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
35 changes: 30 additions & 5 deletions gcloud/bigtable/row.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,18 @@ def __init__(self, row_key, table, filter_=None):

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
Expand All @@ -75,6 +70,36 @@ def append_cell_value(self, column_family_id, column, value):
append_value=value)
self._rule_pb_list.append(rule_pb)

def increment_cell_value(self, column_family_id, column, int_value):
"""Increments a value in an existing cell.
Assumes the value in the cell is stored as a 64 bit integer
serialized to bytes.
.. 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 int_value: int
:param int_value: The value to increment the existing value in the cell
by. If the targeted cell is unset, it will be treated
as containing a zero. Otherwise, the targeted cell
must contain an 8-byte value (interpreted as a 64-bit
big-endian signed integer), or the entire request
will fail.
"""
column = _to_bytes(column)
rule_pb = data_pb2.ReadModifyWriteRule(family_name=column_family_id,
column_qualifier=column,
increment_amount=int_value)
self._rule_pb_list.append(rule_pb)


class RowFilter(object):
"""Basic filter to apply to cells in a row.
Expand Down
17 changes: 17 additions & 0 deletions gcloud/bigtable/test_row.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,23 @@ def test_append_cell_value(self):
append_value=value)
self.assertEqual(row._rule_pb_list, [expected_pb])

def test_increment_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'
int_value = 281330
row.increment_cell_value(column_family_id, column, int_value)
expected_pb = data_pb2.ReadModifyWriteRule(
family_name=column_family_id, column_qualifier=column,
increment_amount=int_value)
self.assertEqual(row._rule_pb_list, [expected_pb])


class Test_BoolFilter(unittest2.TestCase):

Expand Down

0 comments on commit 724fd9d

Please sign in to comment.