1515"""User-friendly container for Google Cloud Bigtable RowSet """
1616
1717
18- from google .cloud ._helpers import _to_bytes # type: ignore
18+ from google .cloud ._helpers import _to_bytes
19+ from google .cloud .bigtable .data .read_rows_query import (
20+ RowRange as BaseRowRange ,
21+ ReadRowsQuery ,
22+ )
23+ from google .cloud .bigtable .helpers import _MappableAttributesMixin
1924
2025
2126class RowSet (object ):
@@ -26,30 +31,25 @@ class RowSet(object):
2631 """
2732
2833 def __init__ (self ):
29- self .row_keys = []
30- self .row_ranges = []
34+ self ._read_rows_query = ReadRowsQuery ()
3135
3236 def __eq__ (self , other ):
3337 if not isinstance (other , self .__class__ ):
3438 return NotImplemented
3539
36- if len (other .row_keys ) != len (self .row_keys ):
37- return False
38-
39- if len (other .row_ranges ) != len (self .row_ranges ):
40- return False
41-
42- if not set (other .row_keys ) == set (self .row_keys ):
43- return False
44-
45- if not set (other .row_ranges ) == set (self .row_ranges ):
46- return False
47-
48- return True
40+ return self ._read_rows_query == other ._read_rows_query
4941
5042 def __ne__ (self , other ):
5143 return not self == other
5244
45+ @property
46+ def row_keys (self ):
47+ return self ._read_rows_query .row_keys
48+
49+ @property
50+ def row_ranges (self ):
51+ return self ._read_rows_query .row_ranges
52+
5353 def add_row_key (self , row_key ):
5454 """Add row key to row_keys list.
5555
@@ -63,7 +63,7 @@ def add_row_key(self, row_key):
6363 :type row_key: bytes
6464 :param row_key: The key of a row to read
6565 """
66- self .row_keys . append (row_key )
66+ self ._read_rows_query . add_key (row_key )
6767
6868 def add_row_range (self , row_range ):
6969 """Add row_range to row_ranges list.
@@ -78,7 +78,7 @@ def add_row_range(self, row_range):
7878 :type row_range: class:`RowRange`
7979 :param row_range: The row range object having start and end key
8080 """
81- self .row_ranges . append (row_range )
81+ self ._read_rows_query . add_range (row_range )
8282
8383 def add_row_range_from_keys (
8484 self , start_key = None , end_key = None , start_inclusive = True , end_inclusive = False
@@ -110,7 +110,7 @@ def add_row_range_from_keys(
110110 considered inclusive. The default is False (exclusive).
111111 """
112112 row_range = RowRange (start_key , end_key , start_inclusive , end_inclusive )
113- self .row_ranges . append (row_range )
113+ self ._read_rows_query . add_range (row_range )
114114
115115 def add_row_range_with_prefix (self , row_key_prefix ):
116116 """Add row range to row_ranges list that start with the row_key_prefix from the row keys
@@ -136,22 +136,21 @@ def _update_message_request(self, message):
136136 :type message: class:`data_messages_v2_pb2.ReadRowsRequest`
137137 :param message: The ``ReadRowsRequest`` protobuf
138138 """
139- for each in self .row_keys :
139+ for each in self ._read_rows_query . _row_set . row_keys :
140140 message .rows .row_keys ._pb .append (_to_bytes (each ))
141141
142- for each in self .row_ranges :
143- r_kwrags = each .get_range_kwargs ()
144- message .rows .row_ranges .append (r_kwrags )
142+ for each in self ._read_rows_query ._row_set .row_ranges :
143+ message .rows .row_ranges .append (each )
145144
146145
147- class RowRange (object ):
146+ class RowRange (_MappableAttributesMixin , BaseRowRange ):
148147 """Convenience wrapper of google.bigtable.v2.RowRange
149148
150- :type start_key: bytes
149+ :type start_key: str | bytes
151150 :param start_key: (Optional) Start key of the row range. If left empty,
152151 will be interpreted as the empty string.
153152
154- :type end_key: bytes
153+ :type end_key: str | bytes
155154 :param end_key: (Optional) End key of the row range. If left empty,
156155 will be interpreted as the empty string and range will
157156 be unbounded on the high end.
@@ -165,49 +164,26 @@ class RowRange(object):
165164 considered inclusive. The default is False (exclusive).
166165 """
167166
168- def __init__ (
169- self , start_key = None , end_key = None , start_inclusive = True , end_inclusive = False
170- ):
171- self .start_key = start_key
172- self .start_inclusive = start_inclusive
173- self .end_key = end_key
174- self .end_inclusive = end_inclusive
167+ _attribute_map = {
168+ "start_inclusive" : "start_is_inclusive" ,
169+ "end_inclusive" : "end_is_inclusive" ,
170+ }
175171
176172 def _key (self ):
177- """A tuple key that uniquely describes this field.
178-
179- Used to compute this instance's hashcode and evaluate equality.
180-
181- Returns:
182- Tuple[str]: The contents of this :class:`.RowRange`.
183- """
184- return (self .start_key , self .start_inclusive , self .end_key , self .end_inclusive )
173+ return (
174+ self .start_key ,
175+ self .end_key ,
176+ self .start_is_inclusive ,
177+ self .end_is_inclusive ,
178+ )
185179
186180 def __hash__ (self ):
187181 return hash (self ._key ())
188182
189- def __eq__ (self , other ):
190- if not isinstance (other , self .__class__ ):
191- return NotImplemented
192- return self ._key () == other ._key ()
193-
194- def __ne__ (self , other ):
195- return not self == other
196-
197183 def get_range_kwargs (self ):
198184 """Convert row range object to dict which can be passed to
199185 google.bigtable.v2.RowRange add method.
200186 """
201- range_kwargs = {}
202- if self .start_key is not None :
203- start_key_key = "start_key_open"
204- if self .start_inclusive :
205- start_key_key = "start_key_closed"
206- range_kwargs [start_key_key ] = _to_bytes (self .start_key )
207-
208- if self .end_key is not None :
209- end_key_key = "end_key_open"
210- if self .end_inclusive :
211- end_key_key = "end_key_closed"
212- range_kwargs [end_key_key ] = _to_bytes (self .end_key )
213- return range_kwargs
187+ return {
188+ descriptor .name : value for descriptor , value in self ._pb ._pb .ListFields ()
189+ }
0 commit comments