8000 Merge pull request #1389 from dhermes/bigtable-increment-cell-value · googleapis/google-cloud-python@ffe5869 · GitHub
[go: up one dir, main page]

Skip to content

Commit ffe5869

Browse files
committed
Merge pull request #1389 from dhermes/bigtable-increment-cell-value
Adding Bigtable Row.increment_cell_value.
2 parents 5cd168c + 46c6e5a commit ffe5869

File tree

2 files changed

+75
-28
lines changed

2 files changed

+75
-28
lines changed

gcloud/bigtable/row.py

Lines changed: 58 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,36 @@ def __init__(self, row_key, table, filter_=None):
6262
self._true_pb_mutations = []
6363
self._false_pb_mutations = []
6464

65+
def _get_mutations(self, state=None):
66+
"""Gets the list of mutations for a given state.
67+
68+
If the state is :data`None` but there is a filter set, then we've
69+
reached an invalid state. Similarly if no filter is set but the
70+
state is not :data:`None`.
71+
72+
:type state: bool
73+
:param state: (Optional) The state that the mutation should be
74+
applied in. Unset if the mutation is not conditional,
75+
otherwise :data:`True` or :data:`False`.
76+
77+
:rtype: list
78+
:returns: The list to add new mutations to (for the current state).
79+
:raises: :class:`ValueError <exceptions.ValueError>`
80+
"""
81+
if state is None:
82+
if self._filter is not None:
83+
raise ValueError('A filter is set on the current row, but no '
84+
'state given for the mutation')
85+
return self._pb_mutations
86+
else:
87+
if self._filter is None:
88+
raise ValueError('No filter was set on the current row, but a '
89+
'state was given for the mutation')
90+
if state:
91+
return self._true_pb_mutations
92+
else:
93+
return self._false_pb_mutations
94+
6595
def append_cell_value(self, column_family_id, column, value):
6696
"""Appends a value to an existing cell.
6797
@@ -93,35 +123,35 @@ def append_cell_value(self, column_family_id, column, value):
93123
append_value=value)
94124
self._rule_pb_list.append(rule_pb)
95125

96-
def _get_mutations(self, state=None):
97-
"""Gets the list of mutations for a given state.
98-
99-
If the state is :data`None` but there is a filter set, then we've
100-
reached an invalid state. Similarly if no filter is set but the
101-
state is not :data:`None`.
102-
103-
:type state: bool
104-
:param state: (Optional) The state that the mutation should be
105-
applied in. Unset if the mutation is not conditional,
106-
otherwise :data:`True` or :data:`False`.
107-
108-
:rtype: list
109-
:returns: The list to add new mutations to (for the current state).
110-
:raises: :class:`ValueError <exceptions.ValueError>`
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.
111149
"""
112-
if state is None:
113-
if self._filter is not None:
114-
raise ValueError('A filter is set on the current row, but no '
115-
'state given for the mutation')
116-
return self._pb_mutations
117-
else:
118-
if self._filter is None:
119-
raise ValueError('No filter was set on the current row, but a '
120-
'state was given for the mutation')
121-
if state:
122-
return self._true_pb_mutations
123-
else:
124-
return self._false_pb_mutations
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)
125155

126156

127157
class RowFilter(object):

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