-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(bigtable): Rerouted RowSet and RowRange to use ReadRows from data client #18196
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -14,7 +14,15 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """User-friendly container for Google Cloud Bigtable RowSet""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from google.cloud._helpers import _to_bytes # type: ignore | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from google.cloud._helpers import _to_bytes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from google.cloud.bigtable.data.read_rows_query import ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ReadRowsQuery, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from google.cloud.bigtable.data.read_rows_query import ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| RowRange as BaseRowRange, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from google.cloud.bigtable.helpers import _MappableAttributesMixin | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class RowSet(object): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -25,30 +33,25 @@ class RowSet(object): | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def __init__(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.row_keys = [] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.row_ranges = [] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self._read_rows_query = ReadRowsQuery() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def __eq__(self, other): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if not isinstance(other, self.__class__): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return NotImplemented | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if len(other.row_keys) != len(self.row_keys): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return False | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if len(other.row_ranges) != len(self.row_ranges): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return False | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if not set(other.row_keys) == set(self.row_keys): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return False | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if not set(other.row_ranges) == set(self.row_ranges): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return False | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return True | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return self._read_rows_query == other._read_rows_query | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def __ne__(self, other): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return not self == other | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @property | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def row_keys(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return self._read_rows_query.row_keys | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @property | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def row_ranges(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return self._read_rows_query.row_ranges | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def add_row_key(self, row_key): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """Add row key to row_keys list. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -62,7 +65,7 @@ def add_row_key(self, row_key): | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| :type row_key: bytes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| :param row_key: The key of a row to read | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.row_keys.append(row_key) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self._read_rows_query.add_key(row_key) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def add_row_range(self, row_range): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """Add row_range to row_ranges list. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -77,7 +80,7 @@ def add_row_range(self, row_range): | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| :type row_range: class:`RowRange` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| :param row_range: The row range object having start and end key | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.row_ranges.append(row_range) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self._read_rows_query.add_range(row_range) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def add_row_range_from_keys( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self, start_key=None, end_key=None, start_inclusive=True, end_inclusive=False | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -109,7 +112,7 @@ def add_row_range_from_keys( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| considered inclusive. The default is False (exclusive). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| row_range = RowRange(start_key, end_key, start_inclusive, end_inclusive) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.row_ranges.append(row_range) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self._read_rows_query.add_range(row_range) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def add_row_range_with_prefix(self, row_key_prefix): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """Add row range to row_ranges list that start with the row_key_prefix from the row keys | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -135,22 +138,21 @@ def _update_message_request(self, message): | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| :type message: class:`data_messages_v2_pb2.ReadRowsRequest` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| :param message: The ``ReadRowsRequest`` protobuf | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for each in self.row_keys: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for each in self._read_rows_query.row_keys: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| message.rows.row_keys._pb.append(_to_bytes(each)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for each in self.row_ranges: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| r_kwrags = each.get_range_kwargs() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| message.rows.row_ranges.append(r_kwrags) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for each in self._read_rows_query.row_ranges: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| message.rows.row_ranges.append(each._to_pb()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class RowRange(object): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class RowRange(_MappableAttributesMixin, BaseRowRange): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """Convenience wrapper of google.bigtable.v2.RowRange | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| :type start_key: bytes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| :type start_key: str | bytes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| :param start_key: (Optional) Start key of the row range. If left empty, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| will be interpreted as the empty string. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| :type end_key: bytes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| :type end_key: str | bytes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| :param end_key: (Optional) End key of the row range. If left empty, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| will be interpreted as the empty string and range will | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| be unbounded on the high end. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -164,49 +166,26 @@ class RowRange(object): | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| considered inclusive. The default is False (exclusive). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def __init__( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self, start_key=None, end_key=None, start_inclusive=True, end_inclusive=False | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.start_key = start_key | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.start_inclusive = start_inclusive | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.end_key = end_key | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.end_inclusive = end_inclusive | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _attribute_map = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "start_inclusive": "start_is_inclusive", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "end_inclusive": "end_is_inclusive", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def _key(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """A tuple key that uniquely describes this field. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Used to compute this instance's hashcode and evaluate equality. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Returns: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Tuple[str]: The contents of this :class:`.RowRange`. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return (self.start_key, self.start_inclusive, self.end_key, self.end_inclusive) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.start_key, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.end_key, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.start_is_inclusive, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.end_is_inclusive, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def __hash__(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return hash(self._key()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def __eq__(self, other): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if not isinstance(other, self.__class__): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return NotImplemented | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return self._key() == other._key() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def __ne__(self, other): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return not self == other | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def get_range_kwargs(self): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """Convert row range object to dict which can be passed to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| google.bigtable.v2.RowRange add method. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| range_kwargs = {} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if self.start_key is not None: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| start_key_key = "start_key_open" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if self.start_inclusive: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| start_key_key = "start_key_closed" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| range_kwargs[start_key_key] = _to_bytes(self.start_key) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if self.end_key is not None: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end_key_key = "end_key_open" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if self.end_inclusive: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end_key_key = "end_key_closed" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| range_kwargs[end_key_key] = _to_bytes(self.end_key) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return range_kwargs | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| descriptor.name: value for descriptor, value in self._pb._pb.ListFields() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
185
to
+191
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Accessing the private attributes
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Accessing
self._attribute_mapinside__getattr__can lead to infinite recursion (maximum recursion depth exceeded) if_attribute_mapis not defined on the class or instance, or if it is accessed before initialization. To prevent this, add a guard to check if the requested attribute name is_attribute_mapand raise anAttributeErrorimmediately. Similarly, guard__setattr__to handle_attribute_mapsafely.