Skip to content

Commit 97b85a4

Browse files
committed
Adding helper to parse a Bigtable ReadModifyWriteRow response.
1 parent e361f79 commit 97b85a4

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed

gcloud/bigtable/row.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,6 +1195,46 @@ def to_pb(self):
11951195
return data_pb2.RowFilter(condition=condition)
11961196

11971197

1198+
def _parse_rmw_row_response(row_response):
1199+
"""Parses the response to a ``ReadModifyWriteRow`` request.
1200+
1201+
:type row_response: :class:`.data_pb2.Row`
1202+
:param row_response: The response row (with only modified cells) from a
1203+
``ReadModifyWriteRow`` request.
1204+
1205+
:rtype: dict
1206+
:returns: The new contents of all modified cells. Returned as a
1207+
dictionary of column families, each of which holds a
1208+
dictionary of columns. Each column contains a list of cells
1209+
modified. Each cell is represented with a two-tuple with the
1210+
value (in bytes) and the timestamp for the cell. For example:
1211+
1212+
.. code:: python
1213+
1214+
{
1215+
u'col-fam-id': {
1216+
b'col-name1': [
1217+
(b'cell-val', datetime.datetime(...)),
1218+
(b'cell-val-newer', datetime.datetime(...)),
1219+
],
1220+
b'col-name2': [
1221+
(b'altcol-cell-val', datetime.datetime(...)),
1222+
],
1223+
},
1224+
u'col-fam-id2': {
1225+
b'col-name3-but-other-fam': [
1226+
(b'foo', datetime.datetime(...)),
1227+
],
1228+
},
1229+
}
1230+
"""
1231+
result = {}
1232+
for column_family in row_response.families:
1233+
column_family_id, curr_family = _parse_family_pb(column_family)
1234+
result[column_family_id] = curr_family
1235+
return result
1236+
1237+
11981238
def _parse_family_pb(family_pb):
11991239
"""Parses a Family protobuf into a dictionary.
12001240

gcloud/bigtable/test_row.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1535,6 +1535,92 @@ def test_to_pb_false_only(self):
15351535
self.assertEqual(filter_pb, expected_pb)
15361536

15371537

1538+
class Test__parse_rmw_row_response(unittest2.TestCase):
1539+
1540+
def _callFUT(self, row_response):
1541+
from gcloud.bigtable.row import _parse_rmw_row_response
1542+
return _parse_rmw_row_response(row_response)
1543+
1544+
def test_it(self):
1545+
from gcloud._helpers import _datetime_from_microseconds
1546+
from gcloud.bigtable._generated import bigtable_data_pb2 as data_pb2
1547+
1548+
col_fam1 = u'col-fam-id'
1549+
col_fam2 = u'col-fam-id2'
1550+
col_name1 = b'col-name1'
1551+
col_name2 = b'col-name2'
1552+
col_name3 = b'col-name3-but-other-fam'
1553+
cell_val1 = b'cell-val'
1554+
cell_val2 = b'cell-val-newer'
1555+
cell_val3 = b'altcol-cell-val'
1556+
cell_val4 = b'foo'
1557+
1558+
microseconds = 1000871
1559+
timestamp = _datetime_from_microseconds(microseconds)
1560+
expected_output = {
1561+
col_fam1: {
1562+
col_name1: [
1563+
(cell_val1, timestamp),
1564+
(cell_val2, timestamp),
1565+
],
1566+
col_name2: [
1567+
(cell_val3, timestamp),
1568+
],
1569+
},
1570+
col_fam2: {
1571+
col_name3: [
1572+
(cell_val4, timestamp),
1573+
],
1574+
},
1575+
}
1576+
sample_input = data_pb2.Row(
1577+
families=[
1578+
data_pb2.Family(
1579+
name=col_fam1,
1580+
columns=[
1581+
data_pb2.Column(
1582+
qualifier=col_name1,
1583+
cells=[
1584+
data_pb2.Cell(
1585+
value=cell_val1,
1586+
timestamp_micros=microseconds,
1587+
),
1588+
data_pb2.Cell(
1589+
value=cell_val2,
1590+
timestamp_micros=microseconds,
1591+
),
1592+
],
1593+
),
1594+
data_pb2.Column(
1595+
qualifier=col_name2,
1596+
cells=[
1597+
data_pb2.Cell(
1598+
value=cell_val3,
1599+
timestamp_micros=microseconds,
1600+
),
1601+
],
1602+
),
1603+
],
1604+
),
1605+
data_pb2.Family(
1606+
name=col_fam2,
1607+
columns=[
1608+
data_pb2.Column(
1609+
qualifier=col_name3,
1610+
cells=[
1611+
data_pb2.Cell(
1612+
value=cell_val4,
1613+
timestamp_micros=microseconds,
1614+
),
1615+
],
1616+
),
1617+
],
1618+
),
1619+
],
1620+
)
1621+
self.assertEqual(expected_output, self._callFUT(sample_input))
1622+
1623+
15381624
class Test__parse_family_pb(unittest2.TestCase):
15391625

15401626
def _callFUT(self, family_pb):

0 commit comments

Comments
 (0)