Skip to content

Commit d332809

Browse files
committed
Merge pull request #1490 from dhermes/bigtable-partial-row-props
Adding clear() to Bigtable row data.
2 parents 6712b79 + 163e98b commit d332809

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

gcloud/bigtable/row_data.py

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

1717

18+
import copy
1819
import six
1920

2021
from gcloud._helpers import _datetime_from_microseconds
@@ -110,6 +111,42 @@ def to_dict(self):
110111
result[key] = cells
111112
return result
112113

114+
@property
115+
def cells(self):
116+
"""Property returning all the cells accumulated on this partial row.
117+
118+
:rtype: dict
119+
:returns: Dictionary of the :class:`Cell` objects accumulated. This
120+
dictionary has two-levels of keys (first for column families
121+
and second for column names/qualifiers within a family). For
122+
a given column, a list of :class:`Cell` objects is stored.
123+
"""
124+
return copy.deepcopy(self._cells)
125+
126+
@property
127+
def row_key(self):
128+
"""Getter for the current (partial) row's key.
129+
130+
:rtype: bytes
131+
:returns: The current (partial) row's key.
132+
"""
133+
return self._row_key
134+
135+
@property
136+
def committed(self):
137+
"""Getter for the committed status of the (partial) row.
138+
139+
:rtype: bool
140+
:returns: The committed status of the (partial) row.
141+
"""
142+
return self._committed
143+
144+
def clear(self):
145+
"""Clears all cells that have been added."""
146+
self._committed = False
147+
self._chunks_encountered = False
148+
self._cells.clear()
149+
113150

114151
class PartialRowsData(object):
115152
"""Convenience wrapper for consuming a ``ReadRows`` streaming response.

gcloud/bigtable/test_row_data.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,36 @@ def test_to_dict(self):
177177
}
178178
self.assertEqual(result, expected_result)
179179

180+
def test_cells_property(self):
181+
partial_row_data = self._makeOne(None)
182+
cells = {1: 2}
183+
partial_row_data._cells = cells
184+
# Make sure we get a copy, not the original.
185+
self.assertFalse(partial_row_data.cells is cells)
186+
self.assertEqual(partial_row_data.cells, cells)
187+
188+
def test_row_key_getter(self):
189+
row_key = object()
190+
partial_row_data = self._makeOne(row_key)
191+
self.assertTrue(partial_row_data.row_key is row_key)
192+
193+
def test_committed_getter(self):
194+
partial_row_data = self._makeOne(None)
195+
partial_row_data._committed = value = object()
196+
self.assertTrue(partial_row_data.committed is value)
197+
198+
def test_clear(self):
199+
partial_row_data = self._makeOne(None)
200+
cells = {1: 2}
201+
partial_row_data._cells = cells
202+
self.assertEqual(partial_row_data.cells, cells)
203+
partial_row_data._committed = True
204+
partial_row_data._chunks_encountered = True
205+
partial_row_data.clear()
206+
self.assertFalse(partial_row_data.committed)
207+
self.assertFalse(partial_row_data._chunks_encountered)
208+
self.assertEqual(partial_row_data.cells, {})
209+
180210

181211
class TestPartialRowsData(unittest2.TestCase):
182212

0 commit comments

Comments
 (0)