Skip to content

Commit 33eb483

Browse files
committed
Removing state argument from DirectRow.delete_cell(s).
Factoring behavior out into a non-public helper _delete_cells() and using that helper in both DirectRow and ConditionalRow. Also making sure ConditionalRow.delete_cell(s) get tested (codepaths were only previously traversed via the parent class).
1 parent bca6f88 commit 33eb483

File tree

2 files changed

+149
-52
lines changed

2 files changed

+149
-52
lines changed

gcloud/bigtable/row.py

Lines changed: 126 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -217,48 +217,12 @@ def delete(self):
217217
"""
218218
self._delete(state=None)
219219

220-
def delete_cell(self, column_family_id, column, time_range=None,
221-
state=None):
222-
"""Deletes cell in this row.
223-
224-
.. note::
225-
226-
This method adds a mutation to the accumulated mutations on this
227-
:class:`Row`, but does not make an API request. To actually
228-
send an API request (with the mutations) to the Google Cloud
229-
Bigtable API, call :meth:`commit`.
230-
231-
:type column_family_id: str
232-
:param column_family_id: The column family that contains the column
233-
or columns with cells being deleted. Must be
234-
of the form ``[_a-zA-Z0-9][-_.a-zA-Z0-9]*``.
235-
236-
:type column: bytes
237-
:param column: The column within the column family that will have a
238-
cell deleted.
239-
240-
:type time_range: :class:`TimestampRange`
241-
:param time_range: (Optional) The range of time within which cells
242-
should be deleted.
243-
244-
:type state: bool
245-
:param state: (Optional) The state that the mutation should be
246-
applied in. Unset if the mutation is not conditional,
247-
otherwise :data:`True` or :data:`False`.
248-
"""
249-
self.delete_cells(column_family_id, [column], time_range=time_range,
250-
state=state)
251-
252-
def delete_cells(self, column_family_id, columns, time_range=None,
253-
state=None):
254-
"""Deletes cells in this row.
255-
256-
.. note::
220+
def _delete_cells(self, column_family_id, columns, time_range=None,
221+
state=None):
222+
"""Helper for :meth:`delete_cell` and :meth:`delete_cells`.
257223
258-
This method adds a mutation to the accumulated mutations on this
259-
:class:`Row`, but does not make an API request. To actually
260-
send an API request (with the mutations) to the Google Cloud
261-
Bigtable API, call :meth:`commit`.
224+
``state`` is unused by :class:`DirectRow` but is used by
225+
subclasses.
262226
263227
:type column_family_id: str
264228
:param column_family_id: The column family that contains the column
@@ -268,17 +232,16 @@ def delete_cells(self, column_family_id, columns, time_range=None,
268232
:type columns: :class:`list` of :class:`str` /
269233
:func:`unicode <unicode>`, or :class:`object`
270234
:param columns: The columns within the column family that will have
271-
cells deleted. If :attr:`Row.ALL_COLUMNS` is used then
235+
cells deleted. If :attr:`ALL_COLUMNS` is used then
272236
the entire column family will be deleted from the row.
273237
274238
:type time_range: :class:`TimestampRange`
275239
:param time_range: (Optional) The range of time within which cells
276240
should be deleted.
277241
278242
:type state: bool
279-
:param state: (Optional) The state that the mutation should be
280-
applied in. Unset if the mutation is not conditional,
281-
otherwise :data:`True` or :data:`False`.
243+
:param state: (Optional) The state that is passed along to
244+
:meth:`_get_mutations`.
282245
"""
283246
mutations_list = self._get_mutations(state)
284247
if columns is self.ALL_COLUMNS:
@@ -311,6 +274,60 @@ def delete_cells(self, column_family_id, columns, time_range=None,
311274
# processed without error.
312275
mutations_list.extend(to_append)
313276

277+
def delete_cell(self, column_family_id, column, time_range=None):
278+
"""Deletes cell in this row.
279+
280+
.. note::
281+
282+
This method adds a mutation to the accumulated mutations on this
283+
row, but does not make an API request. To actually
284+
send an API request (with the mutations) to the Google Cloud
285+
Bigtable API, call :meth:`commit`.
286+
287+
:type column_family_id: str
288+
:param column_family_id: The column family that contains the column
289+
or columns with cells being deleted. Must be
290+
of the form ``[_a-zA-Z0-9][-_.a-zA-Z0-9]*``.
291+
292+
:type column: bytes
293+
:param column: The column within the column family that will have a
294+
cell deleted.
295+
296+
:type time_range: :class:`TimestampRange`
297+
:param time_range: (Optional) The range of time within which cells
298+
should be deleted.
299+
"""
300+
self._delete_cells(column_family_id, [column], time_range=time_range,
301+
state=None)
302+
303+
def delete_cells(self, column_family_id, columns, time_range=None):
304+
"""Deletes cells in this row.
305+
306+
.. note::
307+
308+
This method adds a mutation to the accumulated mutations on this
309+
row, but does not make an API request. To actually
310+
send an API request (with the mutations) to the Google Cloud
311+
Bigtable API, call :meth:`commit`.
312+
313+
:type column_family_id: str
314+
:param column_family_id: The column family that contains the column
315+
or columns with cells being deleted. Must be
316+
of the form ``[_a-zA-Z0-9][-_.a-zA-Z0-9]*``.
317+
318+
:type columns: :class:`list` of :class:`str` /
319+
:func:`unicode <unicode>`, or :class:`object`
320+
:param columns: The columns within the column family that will have
321+
cells deleted. If :attr:`ALL_COLUMNS` is used then
322+
the entire column family will be deleted from the row.
323+
324+
:type time_range: :class:`TimestampRange`
325+
:param time_range: (Optional) The range of time within which cells
326+
should be deleted.
327+
"""
328+
self._delete_cells(column_family_id, columns, time_range=time_range,
329+
state=None)
330+
314331
def commit(self):
315332
"""Makes a ``MutateRow`` API request.
316333
@@ -518,6 +535,70 @@ def delete(self, state=True):
518535
applied in. Defaults to :data:`True`.
519536
"""
520537
self._delete(state=state)
538+
539+
def delete_cell(self, column_family_id, column, time_range=None,
540+
state=True):
541+
"""Deletes cell in this row.
542+
543+
.. note::
544+
545+
This method adds a mutation to the accumulated mutations on this
546+
row, but does not make an API request. To actually
547+
send an API request (with the mutations) to the Google Cloud
548+
Bigtable API, call :meth:`commit`.
549+
550+
:type column_family_id: str
551+
:param column_family_id: The column family that contains the column
552+
or columns with cells being deleted. Must be
553+
of the form ``[_a-zA-Z0-9][-_.a-zA-Z0-9]*``.
554+
555+
:type column: bytes
556+
:param column: The column within the column family that will have a
557+
cell deleted.
558+
559+
:type time_range: :class:`TimestampRange`
560+
:param time_range: (Optional) The range of time within which cells
561+
should be deleted.
562+
563+
:type state: bool
564+
:param state: (Optional) The state that the mutation should be
565+
applied in. Defaults to :data:`True`.
566+
"""
567+
self._delete_cells(column_family_id, [column], time_range=time_range,
568+
state=state)
569+
570+
def delete_cells(self, column_family_id, columns, time_range=None,
571+
state=True):
572+
"""Deletes cells in this row.
573+
574+
.. note::
575+
576+
This method adds a mutation to the accumulated mutations on this
577+
row, but does not make an API request. To actually
578+
send an API request (with the mutations) to the Google Cloud
579+
Bigtable API, call :meth:`commit`.
580+
581+
:type column_family_id: str
582+
:param column_family_id: The column family that contains the column
583+
or columns with cells being deleted. Must be
584+
of the form ``[_a-zA-Z0-9][-_.a-zA-Z0-9]*``.
585+
586+
:type columns: :class:`list` of :class:`str` /
587+
:func:`unicode <unicode>`, or :class:`object`
588+
:param columns: The columns within the column family that will have
589+
cells deleted. If :attr:`Row.ALL_COLUMNS` is used then
590+
the entire column family will be deleted from the row.
591+
592+
:type time_range: :class:`TimestampRange`
593+
:param time_range: (Optional) The range of time within which cells
594+
should be deleted.
595+
596+
:type state: bool
597+
:param state: (Optional) The state that the mutation should be
598+
applied in. Defaults to :data:`True`.
599+
"""
600+
self._delete_cells(column_family_id, columns, time_range=time_range,
601+
state=state)
521602
# pylint: enable=arguments-differ
522603

523604
def clear(self):

gcloud/bigtable/test_row.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ def __init__(self, *args, **kwargs):
142142
self._kwargs = []
143143

144144
# Replace the called method with one that logs arguments.
145-
def delete_cells(self, *args, **kwargs):
145+
def _delete_cells(self, *args, **kwargs):
146146
self._args.append(args)
147147
self._kwargs.append(kwargs)
148148

@@ -414,8 +414,11 @@ def test_commit(self):
414414

415415
row_key = b'row_key'
416416
table_name = 'projects/more-stuff'
417-
column_family_id = u'column_family_id'
418-
column = b'column'
417+
column_family_id1 = u'column_family_id1'
418+
column_family_id2 = u'column_family_id2'
419+
column_family_id3 = u'column_family_id3'
420+
column1 = b'column1'
421+
column2 = b'column2'
419422
timeout_seconds = 262
420423
client = _Client(timeout_seconds=timeout_seconds)
421424
table = _Table(table_name, client=client)
@@ -426,20 +429,31 @@ def test_commit(self):
426429
value1 = b'bytes-value'
427430
mutation1 = data_pb2.Mutation(
428431
set_cell=data_pb2.Mutation.SetCell(
429-
family_name=column_family_id,
430-
column_qualifier=column,
432+
family_name=column_family_id1,
433+
column_qualifier=column1,
431434
timestamp_micros=-1, # Default value.
432435
value=value1,
433436
),
434437
)
435438
mutation2 = data_pb2.Mutation(
436439
delete_from_row=data_pb2.Mutation.DeleteFromRow(),
437440
)
441+
mutation3 = data_pb2.Mutation(
442+
delete_from_column=data_pb2.Mutation.DeleteFromColumn(
443+
family_name=column_family_id2,
444+
column_qualifier=column2,
445+
),
446+
)
447+
mutation4 = data_pb2.Mutation(
448+
delete_from_family=data_pb2.Mutation.DeleteFromFamily(
449+
family_name=column_family_id3,
450+
),
451+
)
438452
request_pb = messages_pb2.CheckAndMutateRowRequest(
439453
table_name=table_name,
440454
row_key=row_key,
441455
predicate_filter=row_filter.to_pb(),
442-
true_mutations=[mutation1],
456+
true_mutations=[mutation1, mutation3, mutation4],
443457
false_mutations=[mutation2],
444458
)
445459

@@ -455,8 +469,10 @@ def test_commit(self):
455469
expected_result = predicate_matched
456470

457471
# Perform the method and check the result.
458-
row.set_cell(column_family_id, column, value1, state=True)
472+
row.set_cell(column_family_id1, column1, value1, state=True)
459473
row.delete(state=False)
474+
row.delete_cell(column_family_id2, column2, state=True)
475+
row.delete_cells(column_family_id3, row.ALL_COLUMNS, state=True)
460476
result = row.commit()
461477
self.assertEqual(result, expected_result)
462478
self.assertEqual(stub.method_calls, [(

0 commit comments

Comments
 (0)