| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -23,6 +23,16 @@ | |||
| 23 | 23 | class Row(object): | |
| 24 | 24 | """Representation of a Google Cloud Bigtable Row. | |
| 25 | 25 | ||
| 26 | + .. note:: | ||
| 27 | + | ||
| 28 | + A :class:`Row` accumulates mutations locally via the :meth:`set_cell`, | ||
| 29 | + :meth:`delete`, :meth:`delete_cell` and :meth:`delete_cells` methods. | ||
| 30 | + To actually send these mutations to the Google Cloud Bigtable API, you | ||
| 31 | + must call :meth:`commit`. If a ``filter_`` is set on the :class:`Row`, | ||
| 32 | + the mutations must have an associated state: :data:`True` or | ||
| 33 | + :data:`False`. The mutations will be applied conditionally, based on | ||
| 34 | + whether the filter matches any cells in the :class:`Row` or not. | ||
| 35 | + | ||
| 26 | 36 | :type row_key: bytes | |
| 27 | 37 | :param row_key: The key for the current row. | |
| 28 | 38 | ||
@@ -43,6 +53,14 @@ def __init__(self, row_key, table, filter_=None): | |||
| 43 | 53 | self._table = table | |
| 44 | 54 | self._filter = filter_ | |
| 45 | 55 | self._rule_pb_list = [] | |
| 56 | + if self._filter is None: | ||
| 57 | + self._pb_mutations = [] | ||
| 58 | + self._true_pb_mutations = None | ||
| 59 | + self._false_pb_mutations = None | ||
| 60 | + else: | ||
| 61 | + self._pb_mutations = None | ||
| 62 | + self._true_pb_mutations = [] | ||
| 63 | + self._false_pb_mutations = [] | ||
| 46 | 64 | ||
| 47 | 65 | def append_cell_value(self, column_family_id, column, value): | |
| 48 | 66 | """Appends a value to an existing cell. | |
@@ -75,6 +93,36 @@ def append_cell_value(self, column_family_id, column, value): | |||
| 75 | 93 | append_value=value) | |
| 76 | 94 | self._rule_pb_list.append(rule_pb) | |
| 77 | 95 | ||
| 96 | + def _get_mutations(self, state=None): | ||
| 97 | + """Gets the list of mutations for a given state. | ||
| 98 | + | ||
| 99 | + If the state is :data`None` but there is a filter set, then we've | ||
| 100 | + reached an invalid state. Similarly if no filter is set but the | ||
| 101 | + state is not :data:`None`. | ||
| 102 | + | ||
| 103 | + :type state: bool | ||
| 104 | + :param state: (Optional) The state that the mutation should be | ||
| 105 | + applied in. Unset if the mutation is not conditional, | ||
| 106 | + otherwise :data:`True` or :data:`False`. | ||
| 107 | + | ||
| 108 | + :rtype: list | ||
| 109 | + :returns: The list to add new mutations to (for the current state). | ||
| 110 | + :raises: :class:`ValueError <exceptions.ValueError>` | ||
| 111 | + """ | ||
| 112 | + if state is None: | ||
| 113 | + if self._filter is not None: | ||
| 114 | + raise ValueError('A filter is set on the current row, but no ' | ||
| 115 | + 'state given for the mutation') | ||
| 116 | + return self._pb_mutations | ||
| 117 | + else: | ||
| 118 | + if self._filter is None: | ||
| 119 | + raise ValueError('No filter was set on the current row, but a ' | ||
| 120 | + 'state was given for the mutation') | ||
| 121 | + if state: | ||
| 122 | + return self._true_pb_mutations | ||
| 123 | + else: | ||
| 124 | + return self._false_pb_mutations | ||
| 125 | + | ||
| 78 | 126 | ||
| 79 | 127 | class RowFilter(object): | |
| 80 | 128 | """Basic filter to apply to cells in a row. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -49,6 +49,46 @@ def test_constructor_with_non_bytes(self): | |||
| 49 | 49 | with self.assertRaises(TypeError): | |
| 50 | 50 | self._makeOne(row_key, None) | |
| 51 | 51 | ||
| 52 | + def _get_mutations_helper(self, filter_=None, state=None): | ||
| 53 | + row_key = b'row_key' | ||
| 54 | + row = self._makeOne(row_key, None, filter_=filter_) | ||
| 55 | + # Mock the mutations with unique objects so we can compare. | ||
| 56 | + row._pb_mutations = no_bool = object() | ||
| 57 | + row._true_pb_mutations = true_mutations = object() | ||
| 58 | + row._false_pb_mutations = false_mutations = object() | ||
| 59 | + | ||
| 60 | + mutations = row._get_mutations(state) | ||
| 61 | + return (no_bool, true_mutations, false_mutations), mutations | ||
| 62 | + | ||
| 63 | + def test__get_mutations_no_filter(self): | ||
| 64 | + (no_bool, _, _), mutations = self._get_mutations_helper() | ||
| 65 | + self.assertTrue(mutations is no_bool) | ||
| 66 | + | ||
| 67 | + def test__get_mutations_no_filter_bad_state(self): | ||
| 68 | + state = object() # State should be null when no filter. | ||
| 69 | + with self.assertRaises(ValueError): | ||
| 70 | + self._get_mutations_helper(state=state) | ||
| 71 | + | ||
| 72 | + def test__get_mutations_with_filter_true_state(self): | ||
| 73 | + filter_ = object() | ||
| 74 | + state = True | ||
| 75 | + (_, true_filter, _), mutations = self._get_mutations_helper( | ||
| 76 | + filter_=filter_, state=state) | ||
| 77 | + self.assertTrue(mutations is true_filter) | ||
| 78 | + | ||
| 79 | + def test__get_mutations_with_filter_false_state(self): | ||
| 80 | + filter_ = object() | ||
| 81 | + state = False | ||
| 82 | + (_, _, false_filter), mutations = self._get_mutations_helper( | ||
| 83 | + filter_=filter_, state=state) | ||
| 84 | + self.assertTrue(mutations is false_filter) | ||
| 85 | + | ||
| 86 | + def test__get_mutations_with_filter_bad_state(self): | ||
| 87 | + filter_ = object() | ||
| 88 | + state = None | ||
| 89 | + with self.assertRaises(ValueError): | ||
| 90 | + self._get_mutations_helper(filter_=filter_, state=state) | ||
| 91 | + | ||
| 52 | 92 | def test_append_cell_value(self): | |
| 53 | 93 | from gcloud.bigtable._generated import bigtable_data_pb2 as data_pb2 | |
| 54 | 94 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments