8000 Adding `PartialRowData.get_cells()`. · googleapis/google-cloud-python@b4fdfc3 · GitHub
[go: up one dir, main page]

Skip to content

Commit b4fdfc3

Browse files
committed
Adding PartialRowData.get_cells().
1 parent 49645a1 commit b4fdfc3

File tree

2 files changed

+108
-10
lines changed

2 files changed

+108
-10
lines changed

bigtable/google/cloud/bigtable/row_data.py

Lines changed: 60 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,43 @@ def row_key(self):
181181
"""
182182
return self._row_key
183183

184+
def _get_cells_no_copy(self, column_family_id, column):
185+
"""Get a time series of cells stored on this instance.
186+
187+
Args:
188+
column_family_id (str): The ID of the column family. Must be of the
189+
form ``[_a-zA-Z0-9][-_.a-zA-Z0-9]*``.
190+
column (bytes): The column within the column family where the cells
191+
are located.
192+
193+
Returns:
194+
List[~google.cloud.bigtable.row_data.Cell]: The cells stored in the
195+
specified column.
196+
197+
Raises:
198+
KeyError: If ``column_family_id`` is not among the cells stored
199+
in this row.
200+
KeyError: If ``column`` is not among the cells stored in this row
201+
for the given ``column_family_id``.
202+
"""
203+
try:
204+
column_family = self._cells[column_family_id]
205+
except KeyError:
206+
raise KeyError(_MISSING_COLUMN_FAMILY.format(column_family_id))
207+
208+
try:
209+
cells = column_family[column]
210+
except KeyError:
211+
raise KeyError(_MISSING_COLUMN.format(column, column_family_id))
212+
213+
return cells
214+
184215
def get_cell(self, column_family_id, column, index=0):
185216
"""Get a single cell stored on this instance.
186217
187218
.. note::
188219
189-
This returns a copy of the actual ``Cell`` (so that the
220+
This returns a copy of the actual cell (so that the
190221
caller cannot mutate internal state).
191222
192223
Args:
@@ -210,15 +241,7 @@ def get_cell(self, column_family_id, column, index=0):
210241
in this row for the given ``column_family_id``, ``column``
211242
pair.
212243
"""
213-
try:
214-
column_family = self._cells[column_family_id]
215-
except KeyError:
216-
raise KeyError(_MISSING_COLUMN_FAMILY.format(column_family_id))
217-
218-
try:
219-
cells = column_family[column]
220-
except KeyError:
221-
raise KeyError(_MISSING_COLUMN.format(column, column_family_id))
244+
cells = self._get_cells_no_copy(column_family_id, column)
222245

223246
try:
224247
cell = cells[index]
@@ -230,6 +253,33 @@ def get_cell(self, column_family_id, column, index=0):
230253

231254
return copy.deepcopy(cell)
232255

256+
def get_cells(self, column_family_id, column):
257+
"""Get a time series of cells stored on this instance.
258+
259+
.. note::
260+
261+
This returns a copy of the actual cells (so that the
262+
caller cannot mutate internal state).
263+
264+
Args:
265+
column_family_id (str): The ID of the column family. Must be of the
266+
form ``[_a-zA-Z0-9][-_.a-zA-Z0-9]*``.
267+
column (bytes): The column within the column family where the cells
268+
are located.
269+
270+
Returns:
271+
List[~google.cloud.bigtable.row_data.Cell]: The cells stored in the
272+
specified column.
273+
274+
Raises:
275+
KeyError: If ``column_family_id`` is not among the cells stored
276+
in this row.
277+
KeyError: If ``column`` is not among the cells stored in this row
278+
for the given ``column_family_id``.
279+
"""
280+
cells = self._get_cells_no_copy(column_family_id, column)
281+
return copy.deepcopy(cells)
282+
233283

234284
class InvalidReadRowsResponse(RuntimeError):
235285
"""Exception raised to to invalid response data from back-end."""

bigtable/tests/unit/test_row_data.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,54 @@ def test_get_cell_bad_index(self):
261261
index, qual, family_name, 0)
262262
self.assertEqual(exc_info.exception.args, (expected_arg,))
263263

264+
def test_get_cells(self):
265+
family_name = u'name1'
266+
qual = b'col1'
267+
cell = _make_cell(b'hi-mom')
268+
269+
partial_row_data = self._make_one(None)
270+
cells = [cell]
271+
partial_row_data._cells = {
272+
family_name: {
273+
qual: cells,
274+
},
275+
}
276+
277+
result = partial_row_data.get_cells(family_name, qual)
278+
# Make sure we get a copy, not the original.
279+
self.assertIsNot(result, cells)
280+
self.assertEqual(result, cells)
281+
self.assertIsNot(result[0], cell)
282+
self.assertEqual(result[0], cell)
283+
284+
def test_get_cells_bad_family(self):
285+
from google.cloud.bigtable import row_data
286+
287+
family_name = u'name1'
288+
partial_row_data = self._make_one(None)
289+
self.assertEqual(partial_row_data._cells, {})
290+
291+
with self.assertRaises(KeyError) as exc_info:
292+
partial_row_data.get_cells(family_name, None)
293+
294+
expected_arg = row_data._MISSING_COLUMN_FAMILY.format(family_name)
295+
self.assertEqual(exc_info.exception.args, (expected_arg,))
296+
297+
def test_get_cell_bad_column(self):
298+
from google.cloud.bigtable import row_data
299+
300+
family_name = u'name1'
301+
qual = b'col1'
302+
303+
partial_row_data = self._make_one(None)
304+
partial_row_data._cells = {family_name: {}}
305+
306+
with self.assertRaises(KeyError) as exc_info:
307+
partial_row_data.get_cells(family_name, qual)
308+
309+
expected_arg = row_data._MISSING_COLUMN.format(qual, family_name)
310+
self.assertEqual(exc_info.exception.args, (expected_arg,))
311+
264312
def test_cells_property(self):
265313
partial_row_data = self._make_one(None)
266314
cells = {1: 2}

0 commit comments

Comments
 (0)
0