Skip to content

Commit aaf1bf5

Browse files
committed
Adding Bigtable PartialRowData.
These objects will be used when data is read from a table.
1 parent 4ca596c commit aaf1bf5

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed

gcloud/bigtable/row_data.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
"""Container for Google Cloud Bigtable Cells and Streaming Row Contents."""
1616

1717

18+
import six
19+
1820
from gcloud._helpers import _datetime_from_microseconds
21+
from gcloud._helpers import _to_bytes
1922

2023

2124
class Cell(object):
@@ -61,3 +64,48 @@ def __eq__(self, other):
6164

6265
def __ne__(self, other):
6366
return not self.__eq__(other)
67+
68+
69+
class PartialRowData(object):
70+
"""Representation of partial row in a Google Cloud Bigtable Table.
71+
72+
These are expected to be updated directly from a
73+
:class:`._generated.bigtable_service_messages_pb2.ReadRowsResponse`
74+
75+
:type row_key: bytes
76+
:param row_key: The key for the row holding the (partial) data.
77+
"""
78+
79+
def __init__(self, row_key):
80+
self._row_key = row_key
81+
self._cells = {}
82+
self._committed = False
83+
self._chunks_encountered = False
84+
85+
def __eq__(self, other):
86+
if not isinstance(other, self.__class__):
87+
return False
88+
return (other._row_key == self._row_key and
89+
other._committed == self._committed and
90+
other._chunks_encountered == self._chunks_encountered and
91+
other._cells == self._cells)
92+
93+
def __ne__(self, other):
94+
return not self.__eq__(other)
95+
96+
def to_dict(self):
97+
"""Convert the cells to a dictionary.
98+
99+
This is intended to be used with HappyBase, so the column family and
100+
column qualiers are combined (with ``:``).
101+
102+
:rtype: dict
103+
:returns: Dictionary containing all the data in the cells of this row.
104+
"""
105+
result = {}
106+
for column_family_id, columns in six.iteritems(self._cells):
107+
for column_qual, cells in six.iteritems(columns):
108+
key = (_to_bytes(column_family_id) + b':' +
109+
_to_bytes(column_qual))
110+
result[key] = cells
111+
return result

gcloud/bigtable/test_row_data.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,90 @@ def test___ne__(self):
8989
cell1 = self._makeOne(value1, timestamp)
9090
cell2 = self._makeOne(value2, timestamp)
9191
self.assertNotEqual(cell1, cell2)
92+
93+
94+
class TestPartialRowData(unittest2.TestCase):
95+
96+
def _getTargetClass(self):
97+
from gcloud.bigtable.row_data import PartialRowData
98+
return PartialRowData
99+
100+
def _makeOne(self, *args, **kwargs):
101+
return self._getTargetClass()(*args, **kwargs)
102+
103+
def test_constructor(self):
104+
row_key = object()
105+
partial_row_data = self._makeOne(row_key)
106+
self.assertTrue(partial_row_data._row_key is row_key)
107+
self.assertEqual(partial_row_data._cells, {})
108+
self.assertFalse(partial_row_data._committed)
109+
self.assertFalse(partial_row_data._chunks_encountered)
110+
111+
def test___eq__(self):
112+
row_key = object()
113+
partial_row_data1 = self._makeOne(row_key)
114+
partial_row_data2 = self._makeOne(row_key)
115+
self.assertEqual(partial_row_data1, partial_row_data2)
116+
117+
def test___eq__type_differ(self):
118+
partial_row_data1 = self._makeOne(None)
119+
partial_row_data2 = object()
120+
self.assertNotEqual(partial_row_data1, partial_row_data2)
121+
122+
def test___ne__same_value(self):
123+
row_key = object()
124+
partial_row_data1 = self._makeOne(row_key)
125+
partial_row_data2 = self._makeOne(row_key)
126+
comparison_val = (partial_row_data1 != partial_row_data2)
127+
self.assertFalse(comparison_val)
128+
129+
def test___ne__(self):
130+
row_key1 = object()
131+
partial_row_data1 = self._makeOne(row_key1)
132+
row_key2 = object()
133+
partial_row_data2 = self._makeOne(row_key2)
134+
self.assertNotEqual(partial_row_data1, partial_row_data2)
135+
136+
def test___ne__committed(self):
137+
row_key = object()
138+
partial_row_data1 = self._makeOne(row_key)
139+
partial_row_data1._committed = object()
140+
partial_row_data2 = self._makeOne(row_key)
141+
self.assertNotEqual(partial_row_data1, partial_row_data2)
142+
143+
def test___ne__cells(self):
144+
row_key = object()
145+
partial_row_data1 = self._makeOne(row_key)
146+
partial_row_data1._cells = object()
147+
partial_row_data2 = self._makeOne(row_key)
148+
self.assertNotEqual(partial_row_data1, partial_row_data2)
149+
150+
def test_to_dict(self):
151+
cell1 = object()
152+
cell2 = object()
153+
cell3 = object()
154+
155+
family_name1 = u'name1'
156+
family_name2 = u'name2'
157+
qual1 = b'col1'
158+
qual2 = b'col2'
159+
qual3 = b'col3'
160+
161+
partial_row_data = self._makeOne(None)
162+
partial_row_data._cells = {
163+
family_name1: {
164+
qual1: cell1,
165+
qual2: cell2,
166+
},
167+
family_name2: {
168+
qual3: cell3,
169+
},
170+
}
171+
172+
result = partial_row_data.to_dict()
173+
expected_result = {
174+
b'name1:col1': cell1,
175+
b'name1:col2': cell2,
176+
b'name2:col3': cell3,
177+
}
178+
self.assertEqual(result, expected_result)

0 commit comments

Comments
 (0)