google-cloud-bigtable 2.30.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- google/cloud/bigtable/__init__.py +25 -0
- google/cloud/bigtable/app_profile.py +377 -0
- google/cloud/bigtable/backup.py +490 -0
- google/cloud/bigtable/batcher.py +414 -0
- google/cloud/bigtable/client.py +475 -0
- google/cloud/bigtable/cluster.py +541 -0
- google/cloud/bigtable/column_family.py +362 -0
- google/cloud/bigtable/data/__init__.py +103 -0
- google/cloud/bigtable/data/_async/__init__.py +25 -0
- google/cloud/bigtable/data/_async/_mutate_rows.py +226 -0
- google/cloud/bigtable/data/_async/_read_rows.py +363 -0
- google/cloud/bigtable/data/_async/client.py +1522 -0
- google/cloud/bigtable/data/_async/mutations_batcher.py +534 -0
- google/cloud/bigtable/data/_cross_sync/__init__.py +20 -0
- google/cloud/bigtable/data/_cross_sync/_decorators.py +441 -0
- google/cloud/bigtable/data/_cross_sync/_mapping_meta.py +64 -0
- google/cloud/bigtable/data/_cross_sync/cross_sync.py +334 -0
- google/cloud/bigtable/data/_helpers.py +249 -0
- google/cloud/bigtable/data/_sync_autogen/_mutate_rows.py +182 -0
- google/cloud/bigtable/data/_sync_autogen/_read_rows.py +304 -0
- google/cloud/bigtable/data/_sync_autogen/client.py +1293 -0
- google/cloud/bigtable/data/_sync_autogen/mutations_batcher.py +449 -0
- google/cloud/bigtable/data/exceptions.py +340 -0
- google/cloud/bigtable/data/execute_query/__init__.py +43 -0
- google/cloud/bigtable/data/execute_query/_async/__init__.py +13 -0
- google/cloud/bigtable/data/execute_query/_async/execute_query_iterator.py +291 -0
- google/cloud/bigtable/data/execute_query/_byte_cursor.py +123 -0
- google/cloud/bigtable/data/execute_query/_checksum.py +43 -0
- google/cloud/bigtable/data/execute_query/_parameters_formatting.py +155 -0
- google/cloud/bigtable/data/execute_query/_query_result_parsing_utils.py +134 -0
- google/cloud/bigtable/data/execute_query/_reader.py +122 -0
- google/cloud/bigtable/data/execute_query/_sync_autogen/execute_query_iterator.py +239 -0
- google/cloud/bigtable/data/execute_query/metadata.py +399 -0
- google/cloud/bigtable/data/execute_query/values.py +123 -0
- google/cloud/bigtable/data/mutations.py +380 -0
- google/cloud/bigtable/data/read_modify_write_rules.py +112 -0
- google/cloud/bigtable/data/read_rows_query.py +536 -0
- google/cloud/bigtable/data/row.py +535 -0
- google/cloud/bigtable/data/row_filters.py +968 -0
- google/cloud/bigtable/encryption_info.py +64 -0
- google/cloud/bigtable/enums.py +223 -0
- google/cloud/bigtable/error.py +64 -0
- google/cloud/bigtable/gapic_version.py +16 -0
- google/cloud/bigtable/helpers.py +31 -0
- google/cloud/bigtable/instance.py +789 -0
- google/cloud/bigtable/policy.py +255 -0
- google/cloud/bigtable/row.py +1267 -0
- google/cloud/bigtable/row_data.py +380 -0
- google/cloud/bigtable/row_filters.py +838 -0
- google/cloud/bigtable/row_merger.py +250 -0
- google/cloud/bigtable/row_set.py +213 -0
- google/cloud/bigtable/table.py +1409 -0
- google/cloud/bigtable_admin/__init__.py +410 -0
- google/cloud/bigtable_admin/gapic_version.py +16 -0
- google/cloud/bigtable_admin/py.typed +2 -0
- google/cloud/bigtable_admin_v2/__init__.py +250 -0
- google/cloud/bigtable_admin_v2/gapic_metadata.json +962 -0
- google/cloud/bigtable_admin_v2/gapic_version.py +16 -0
- google/cloud/bigtable_admin_v2/py.typed +2 -0
- google/cloud/bigtable_admin_v2/services/__init__.py +15 -0
- google/cloud/bigtable_admin_v2/services/bigtable_instance_admin/__init__.py +22 -0
- google/cloud/bigtable_admin_v2/services/bigtable_instance_admin/async_client.py +3465 -0
- google/cloud/bigtable_admin_v2/services/bigtable_instance_admin/client.py +3944 -0
- google/cloud/bigtable_admin_v2/services/bigtable_instance_admin/pagers.py +681 -0
- google/cloud/bigtable_admin_v2/services/bigtable_instance_admin/transports/__init__.py +40 -0
- google/cloud/bigtable_admin_v2/services/bigtable_instance_admin/transports/base.py +751 -0
- google/cloud/bigtable_admin_v2/services/bigtable_instance_admin/transports/grpc.py +1249 -0
- google/cloud/bigtable_admin_v2/services/bigtable_instance_admin/transports/grpc_asyncio.py +1579 -0
- google/cloud/bigtable_admin_v2/services/bigtable_instance_admin/transports/rest.py +6820 -0
- google/cloud/bigtable_admin_v2/services/bigtable_instance_admin/transports/rest_base.py +1746 -0
- google/cloud/bigtable_admin_v2/services/bigtable_table_admin/__init__.py +22 -0
- google/cloud/bigtable_admin_v2/services/bigtable_table_admin/async_client.py +3472 -0
- google/cloud/bigtable_admin_v2/services/bigtable_table_admin/client.py +3949 -0
- google/cloud/bigtable_admin_v2/services/bigtable_table_admin/pagers.py +669 -0
- google/cloud/bigtable_admin_v2/services/bigtable_table_admin/transports/__init__.py +40 -0
- google/cloud/bigtable_admin_v2/services/bigtable_table_admin/transports/base.py +708 -0
- google/cloud/bigtable_admin_v2/services/bigtable_table_admin/transports/grpc.py +1257 -0
- google/cloud/bigtable_admin_v2/services/bigtable_table_admin/transports/grpc_asyncio.py +1549 -0
- google/cloud/bigtable_admin_v2/services/bigtable_table_admin/transports/rest.py +6609 -0
- google/cloud/bigtable_admin_v2/services/bigtable_table_admin/transports/rest_base.py +1714 -0
- google/cloud/bigtable_admin_v2/types/__init__.py +248 -0
- google/cloud/bigtable_admin_v2/types/bigtable_instance_admin.py +1364 -0
- google/cloud/bigtable_admin_v2/types/bigtable_table_admin.py +1715 -0
- google/cloud/bigtable_admin_v2/types/common.py +81 -0
- google/cloud/bigtable_admin_v2/types/instance.py +819 -0
- google/cloud/bigtable_admin_v2/types/table.py +1028 -0
- google/cloud/bigtable_admin_v2/types/types.py +776 -0
- google/cloud/bigtable_v2/__init__.py +136 -0
- google/cloud/bigtable_v2/gapic_metadata.json +193 -0
- google/cloud/bigtable_v2/gapic_version.py +16 -0
- google/cloud/bigtable_v2/py.typed +2 -0
- google/cloud/bigtable_v2/services/__init__.py +15 -0
- google/cloud/bigtable_v2/services/bigtable/__init__.py +22 -0
- google/cloud/bigtable_v2/services/bigtable/async_client.py +1741 -0
- google/cloud/bigtable_v2/services/bigtable/client.py +2194 -0
- google/cloud/bigtable_v2/services/bigtable/transports/__init__.py +38 -0
- google/cloud/bigtable_v2/services/bigtable/transports/base.py +343 -0
- google/cloud/bigtable_v2/services/bigtable/transports/grpc.py +660 -0
- google/cloud/bigtable_v2/services/bigtable/transports/grpc_asyncio.py +762 -0
- google/cloud/bigtable_v2/services/bigtable/transports/rest.py +2489 -0
- google/cloud/bigtable_v2/services/bigtable/transports/rest_base.py +711 -0
- google/cloud/bigtable_v2/types/__init__.py +138 -0
- google/cloud/bigtable_v2/types/bigtable.py +1531 -0
- google/cloud/bigtable_v2/types/data.py +1612 -0
- google/cloud/bigtable_v2/types/feature_flags.py +119 -0
- google/cloud/bigtable_v2/types/request_stats.py +171 -0
- google/cloud/bigtable_v2/types/response_params.py +64 -0
- google/cloud/bigtable_v2/types/types.py +579 -0
- google_cloud_bigtable-2.30.0.data/scripts/fixup_bigtable_admin_v2_keywords.py +233 -0
- google_cloud_bigtable-2.30.0.data/scripts/fixup_bigtable_v2_keywords.py +186 -0
- google_cloud_bigtable-2.30.0.dist-info/LICENSE +202 -0
- google_cloud_bigtable-2.30.0.dist-info/METADATA +154 -0
- google_cloud_bigtable-2.30.0.dist-info/RECORD +115 -0
- google_cloud_bigtable-2.30.0.dist-info/WHEEL +5 -0
- google_cloud_bigtable-2.30.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,536 @@
|
|
|
1
|
+
# Copyright 2023 Google LLC
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
#
|
|
15
|
+
from __future__ import annotations
|
|
16
|
+
from typing import TYPE_CHECKING, Any
|
|
17
|
+
from bisect import bisect_left
|
|
18
|
+
from bisect import bisect_right
|
|
19
|
+
from collections import defaultdict
|
|
20
|
+
from google.cloud.bigtable.data.row_filters import RowFilter
|
|
21
|
+
|
|
22
|
+
from google.cloud.bigtable_v2.types import RowRange as RowRangePB
|
|
23
|
+
from google.cloud.bigtable_v2.types import RowSet as RowSetPB
|
|
24
|
+
from google.cloud.bigtable_v2.types import ReadRowsRequest as ReadRowsRequestPB
|
|
25
|
+
|
|
26
|
+
if TYPE_CHECKING:
|
|
27
|
+
from google.cloud.bigtable.data import RowKeySamples
|
|
28
|
+
from google.cloud.bigtable.data import ShardedQuery
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class RowRange:
|
|
32
|
+
"""
|
|
33
|
+
Represents a range of keys in a ReadRowsQuery
|
|
34
|
+
|
|
35
|
+
Args:
|
|
36
|
+
start_key: The start key of the range. If empty, the range is unbounded on the left.
|
|
37
|
+
end_key: The end key of the range. If empty, the range is unbounded on the right.
|
|
38
|
+
start_is_inclusive: Whether the start key is inclusive. If None, the start key is
|
|
39
|
+
inclusive.
|
|
40
|
+
end_is_inclusive: Whether the end key is inclusive. If None, the end key is not inclusive.
|
|
41
|
+
Raises:
|
|
42
|
+
ValueError: if start_key is greater than end_key, or start_is_inclusive
|
|
43
|
+
ValueError: if end_is_inclusive is set when the corresponding key is None
|
|
44
|
+
ValueError: if start_key or end_key is not a string or bytes.
|
|
45
|
+
"""
|
|
46
|
+
|
|
47
|
+
__slots__ = ("_pb",)
|
|
48
|
+
|
|
49
|
+
def __init__(
|
|
50
|
+
self,
|
|
51
|
+
start_key: str | bytes | None = None,
|
|
52
|
+
end_key: str | bytes | None = None,
|
|
53
|
+
start_is_inclusive: bool | None = None,
|
|
54
|
+
end_is_inclusive: bool | None = None,
|
|
55
|
+
):
|
|
56
|
+
# convert empty key inputs to None for consistency
|
|
57
|
+
start_key = None if not start_key else start_key
|
|
58
|
+
end_key = None if not end_key else end_key
|
|
59
|
+
# check for invalid combinations of arguments
|
|
60
|
+
if start_is_inclusive is None:
|
|
61
|
+
start_is_inclusive = True
|
|
62
|
+
|
|
63
|
+
if end_is_inclusive is None:
|
|
64
|
+
end_is_inclusive = False
|
|
65
|
+
# ensure that start_key and end_key are bytes
|
|
66
|
+
if isinstance(start_key, str):
|
|
67
|
+
start_key = start_key.encode()
|
|
68
|
+
elif start_key is not None and not isinstance(start_key, bytes):
|
|
69
|
+
raise ValueError("start_key must be a string or bytes")
|
|
70
|
+
if isinstance(end_key, str):
|
|
71
|
+
end_key = end_key.encode()
|
|
72
|
+
elif end_key is not None and not isinstance(end_key, bytes):
|
|
73
|
+
raise ValueError("end_key must be a string or bytes")
|
|
74
|
+
# ensure that start_key is less than or equal to end_key
|
|
75
|
+
if start_key is not None and end_key is not None and start_key > end_key:
|
|
76
|
+
raise ValueError("start_key must be less than or equal to end_key")
|
|
77
|
+
|
|
78
|
+
init_dict = {}
|
|
79
|
+
if start_key is not None:
|
|
80
|
+
if start_is_inclusive:
|
|
81
|
+
init_dict["start_key_closed"] = start_key
|
|
82
|
+
else:
|
|
83
|
+
init_dict["start_key_open"] = start_key
|
|
84
|
+
if end_key is not None:
|
|
85
|
+
if end_is_inclusive:
|
|
86
|
+
init_dict["end_key_closed"] = end_key
|
|
87
|
+
else:
|
|
88
|
+
init_dict["end_key_open"] = end_key
|
|
89
|
+
self._pb = RowRangePB(**init_dict)
|
|
90
|
+
|
|
91
|
+
@property
|
|
92
|
+
def start_key(self) -> bytes | None:
|
|
93
|
+
"""
|
|
94
|
+
Returns the start key of the range. If None, the range is unbounded on the left.
|
|
95
|
+
"""
|
|
96
|
+
return self._pb.start_key_closed or self._pb.start_key_open or None
|
|
97
|
+
|
|
98
|
+
@property
|
|
99
|
+
def end_key(self) -> bytes | None:
|
|
100
|
+
"""
|
|
101
|
+
Returns the end key of the range. If None, the range is unbounded on the right.
|
|
102
|
+
|
|
103
|
+
Returns:
|
|
104
|
+
bytes | None: The end key of the range, or None if the range is unbounded on the right.
|
|
105
|
+
"""
|
|
106
|
+
return self._pb.end_key_closed or self._pb.end_key_open or None
|
|
107
|
+
|
|
108
|
+
@property
|
|
109
|
+
def start_is_inclusive(self) -> bool:
|
|
110
|
+
"""
|
|
111
|
+
Indicates if the range is inclusive of the start key.
|
|
112
|
+
|
|
113
|
+
If the range is unbounded on the left, this will return True.
|
|
114
|
+
|
|
115
|
+
Returns:
|
|
116
|
+
bool: Whether the range is inclusive of the start key.
|
|
117
|
+
"""
|
|
118
|
+
return not bool(self._pb.start_key_open)
|
|
119
|
+
|
|
120
|
+
@property
|
|
121
|
+
def end_is_inclusive(self) -> bool:
|
|
122
|
+
"""
|
|
123
|
+
Indicates if the range is inclusive of the end key.
|
|
124
|
+
|
|
125
|
+
If the range is unbounded on the right, this will return True.
|
|
126
|
+
|
|
127
|
+
Returns:
|
|
128
|
+
bool: Whether the range is inclusive of the end key.
|
|
129
|
+
"""
|
|
130
|
+
return not bool(self._pb.end_key_open)
|
|
131
|
+
|
|
132
|
+
def _to_pb(self) -> RowRangePB:
|
|
133
|
+
"""
|
|
134
|
+
Converts this object to a protobuf
|
|
135
|
+
|
|
136
|
+
Returns:
|
|
137
|
+
RowRangePB: The protobuf representation of this object
|
|
138
|
+
"""
|
|
139
|
+
return self._pb
|
|
140
|
+
|
|
141
|
+
@classmethod
|
|
142
|
+
def _from_pb(cls, data: RowRangePB) -> RowRange:
|
|
143
|
+
"""
|
|
144
|
+
Creates a RowRange from a protobuf
|
|
145
|
+
|
|
146
|
+
Args:
|
|
147
|
+
data (RowRangePB): The protobuf to convert
|
|
148
|
+
Returns:
|
|
149
|
+
RowRange: The converted RowRange
|
|
150
|
+
"""
|
|
151
|
+
instance = cls()
|
|
152
|
+
instance._pb = data
|
|
153
|
+
return instance
|
|
154
|
+
|
|
155
|
+
@classmethod
|
|
156
|
+
def _from_dict(cls, data: dict[str, bytes | str]) -> RowRange:
|
|
157
|
+
"""
|
|
158
|
+
Creates a RowRange from a protobuf
|
|
159
|
+
|
|
160
|
+
Args:
|
|
161
|
+
data (dict[str, bytes | str]): The dictionary to convert
|
|
162
|
+
Returns:
|
|
163
|
+
RowRange: The converted RowRange
|
|
164
|
+
"""
|
|
165
|
+
formatted_data = {
|
|
166
|
+
k: v.encode() if isinstance(v, str) else v for k, v in data.items()
|
|
167
|
+
}
|
|
168
|
+
instance = cls()
|
|
169
|
+
instance._pb = RowRangePB(**formatted_data)
|
|
170
|
+
return instance
|
|
171
|
+
|
|
172
|
+
def __bool__(self) -> bool:
|
|
173
|
+
"""
|
|
174
|
+
Empty RowRanges (representing a full table scan) are falsy, because
|
|
175
|
+
they can be substituted with None. Non-empty RowRanges are truthy.
|
|
176
|
+
|
|
177
|
+
Returns:
|
|
178
|
+
bool: True if the RowRange is not empty, False otherwise
|
|
179
|
+
"""
|
|
180
|
+
return bool(
|
|
181
|
+
self._pb.start_key_closed
|
|
182
|
+
or self._pb.start_key_open
|
|
183
|
+
or self._pb.end_key_closed
|
|
184
|
+
or self._pb.end_key_open
|
|
185
|
+
)
|
|
186
|
+
|
|
187
|
+
def __eq__(self, other: Any) -> bool:
|
|
188
|
+
if not isinstance(other, RowRange):
|
|
189
|
+
return NotImplemented
|
|
190
|
+
return self._pb == other._pb
|
|
191
|
+
|
|
192
|
+
def __str__(self) -> str:
|
|
193
|
+
"""
|
|
194
|
+
Represent range as a string, e.g. "[b'a', b'z)"
|
|
195
|
+
|
|
196
|
+
Unbounded start or end keys are represented as "-inf" or "+inf"
|
|
197
|
+
|
|
198
|
+
Returns:
|
|
199
|
+
str: The string representation of the range
|
|
200
|
+
"""
|
|
201
|
+
left = "[" if self.start_is_inclusive else "("
|
|
202
|
+
right = "]" if self.end_is_inclusive else ")"
|
|
203
|
+
start = repr(self.start_key) if self.start_key is not None else "-inf"
|
|
204
|
+
end = repr(self.end_key) if self.end_key is not None else "+inf"
|
|
205
|
+
return f"{left}{start}, {end}{right}"
|
|
206
|
+
|
|
207
|
+
def __repr__(self) -> str:
|
|
208
|
+
args_list = []
|
|
209
|
+
args_list.append(f"start_key={self.start_key!r}")
|
|
210
|
+
args_list.append(f"end_key={self.end_key!r}")
|
|
211
|
+
if self.start_is_inclusive is False:
|
|
212
|
+
# only show start_is_inclusive if it is different from the default
|
|
213
|
+
args_list.append(f"start_is_inclusive={self.start_is_inclusive}")
|
|
214
|
+
if self.end_is_inclusive is True and self.end_key is not None:
|
|
215
|
+
# only show end_is_inclusive if it is different from the default
|
|
216
|
+
args_list.append(f"end_is_inclusive={self.end_is_inclusive}")
|
|
217
|
+
return f"RowRange({', '.join(args_list)})"
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
class ReadRowsQuery:
|
|
221
|
+
"""
|
|
222
|
+
Class to encapsulate details of a read row request
|
|
223
|
+
|
|
224
|
+
Args:
|
|
225
|
+
row_keys: row keys to include in the query
|
|
226
|
+
a query can contain multiple keys, but ranges should be preferred
|
|
227
|
+
row_ranges: ranges of rows to include in the query
|
|
228
|
+
limit: the maximum number of rows to return. None or 0 means no limit
|
|
229
|
+
default: None (no limit)
|
|
230
|
+
row_filter: a RowFilter to apply to the query
|
|
231
|
+
"""
|
|
232
|
+
|
|
233
|
+
slots = ("_limit", "_filter", "_row_set")
|
|
234
|
+
|
|
235
|
+
def __init__(
|
|
236
|
+
self,
|
|
237
|
+
row_keys: list[str | bytes] | str | bytes | None = None,
|
|
238
|
+
row_ranges: list[RowRange] | RowRange | None = None,
|
|
239
|
+
limit: int | None = None,
|
|
240
|
+
row_filter: RowFilter | None = None,
|
|
241
|
+
):
|
|
242
|
+
if row_keys is None:
|
|
243
|
+
row_keys = []
|
|
244
|
+
if row_ranges is None:
|
|
245
|
+
row_ranges = []
|
|
246
|
+
if not isinstance(row_ranges, list):
|
|
247
|
+
row_ranges = [row_ranges]
|
|
248
|
+
if not isinstance(row_keys, list):
|
|
249
|
+
row_keys = [row_keys]
|
|
250
|
+
row_keys = [key.encode() if isinstance(key, str) else key for key in row_keys]
|
|
251
|
+
self._row_set = RowSetPB(
|
|
252
|
+
row_keys=row_keys, row_ranges=[r._pb for r in row_ranges]
|
|
253
|
+
)
|
|
254
|
+
self.limit = limit or None
|
|
255
|
+
self.filter = row_filter
|
|
256
|
+
|
|
257
|
+
@property
|
|
258
|
+
def row_keys(self) -> list[bytes]:
|
|
259
|
+
"""
|
|
260
|
+
Return the row keys in this query
|
|
261
|
+
|
|
262
|
+
Returns:
|
|
263
|
+
list[bytes]: the row keys in this query
|
|
264
|
+
"""
|
|
265
|
+
return list(self._row_set.row_keys)
|
|
266
|
+
|
|
267
|
+
@property
|
|
268
|
+
def row_ranges(self) -> list[RowRange]:
|
|
269
|
+
"""
|
|
270
|
+
Return the row ranges in this query
|
|
271
|
+
|
|
272
|
+
Returns:
|
|
273
|
+
list[RowRange]: the row ranges in this query
|
|
274
|
+
"""
|
|
275
|
+
return [RowRange._from_pb(r) for r in self._row_set.row_ranges]
|
|
276
|
+
|
|
277
|
+
@property
|
|
278
|
+
def limit(self) -> int | None:
|
|
279
|
+
"""
|
|
280
|
+
Return the maximum number of rows to return by this query
|
|
281
|
+
|
|
282
|
+
None or 0 means no limit
|
|
283
|
+
|
|
284
|
+
Returns:
|
|
285
|
+
int | None: the maximum number of rows to return by this query
|
|
286
|
+
"""
|
|
287
|
+
return self._limit or None
|
|
288
|
+
|
|
289
|
+
@limit.setter
|
|
290
|
+
def limit(self, new_limit: int | None):
|
|
291
|
+
"""
|
|
292
|
+
Set the maximum number of rows to return by this query.
|
|
293
|
+
|
|
294
|
+
None or 0 means no limit
|
|
295
|
+
|
|
296
|
+
Args:
|
|
297
|
+
new_limit: the new limit to apply to this query
|
|
298
|
+
Raises:
|
|
299
|
+
ValueError: if new_limit is < 0
|
|
300
|
+
"""
|
|
301
|
+
if new_limit is not None and new_limit < 0:
|
|
302
|
+
raise ValueError("limit must be >= 0")
|
|
303
|
+
self._limit = new_limit
|
|
304
|
+
|
|
305
|
+
@property
|
|
306
|
+
def filter(self) -> RowFilter | None:
|
|
307
|
+
"""
|
|
308
|
+
Return the RowFilter applied to this query
|
|
309
|
+
|
|
310
|
+
Returns:
|
|
311
|
+
RowFilter | None: the RowFilter applied to this query
|
|
312
|
+
"""
|
|
313
|
+
return self._filter
|
|
314
|
+
|
|
315
|
+
@filter.setter
|
|
316
|
+
def filter(self, row_filter: RowFilter | None):
|
|
317
|
+
"""
|
|
318
|
+
Set a RowFilter to apply to this query
|
|
319
|
+
|
|
320
|
+
Args:
|
|
321
|
+
row_filter: a RowFilter to apply to this query
|
|
322
|
+
"""
|
|
323
|
+
self._filter = row_filter
|
|
324
|
+
|
|
325
|
+
def add_key(self, row_key: str | bytes):
|
|
326
|
+
"""
|
|
327
|
+
Add a row key to this query
|
|
328
|
+
|
|
329
|
+
A query can contain multiple keys, but ranges should be preferred
|
|
330
|
+
|
|
331
|
+
Args:
|
|
332
|
+
row_key: a key to add to this query
|
|
333
|
+
Raises:
|
|
334
|
+
ValueError: if an input is not a string or bytes
|
|
335
|
+
"""
|
|
336
|
+
if isinstance(row_key, str):
|
|
337
|
+
row_key = row_key.encode()
|
|
338
|
+
elif not isinstance(row_key, bytes):
|
|
339
|
+
raise ValueError("row_key must be string or bytes")
|
|
340
|
+
if row_key not in self._row_set.row_keys:
|
|
341
|
+
self._row_set.row_keys.append(row_key)
|
|
342
|
+
|
|
343
|
+
def add_range(
|
|
344
|
+
self,
|
|
345
|
+
row_range: RowRange,
|
|
346
|
+
):
|
|
347
|
+
"""
|
|
348
|
+
Add a range of row keys to this query.
|
|
349
|
+
|
|
350
|
+
Args:
|
|
351
|
+
row_range: a range of row keys to add to this query
|
|
352
|
+
"""
|
|
353
|
+
if row_range not in self.row_ranges:
|
|
354
|
+
self._row_set.row_ranges.append(row_range._pb)
|
|
355
|
+
|
|
356
|
+
def shard(self, shard_keys: RowKeySamples) -> ShardedQuery:
|
|
357
|
+
"""
|
|
358
|
+
Split this query into multiple queries that can be evenly distributed
|
|
359
|
+
across nodes and run in parallel
|
|
360
|
+
|
|
361
|
+
Args:
|
|
362
|
+
shard_keys: a list of row keys that define the boundaries of segments.
|
|
363
|
+
Returns:
|
|
364
|
+
ShardedQuery: a ShardedQuery that can be used in sharded_read_rows calls
|
|
365
|
+
Raises:
|
|
366
|
+
AttributeError: if the query contains a limit
|
|
367
|
+
"""
|
|
368
|
+
if self.limit is not None:
|
|
369
|
+
raise AttributeError("Cannot shard query with a limit")
|
|
370
|
+
if len(self.row_keys) == 0 and len(self.row_ranges) == 0:
|
|
371
|
+
# empty query represents full scan
|
|
372
|
+
# ensure that we have at least one key or range
|
|
373
|
+
full_scan_query = ReadRowsQuery(
|
|
374
|
+
row_ranges=RowRange(), row_filter=self.filter
|
|
375
|
+
)
|
|
376
|
+
return full_scan_query.shard(shard_keys)
|
|
377
|
+
|
|
378
|
+
sharded_queries: dict[int, ReadRowsQuery] = defaultdict(
|
|
379
|
+
lambda: ReadRowsQuery(row_filter=self.filter)
|
|
380
|
+
)
|
|
381
|
+
# the split_points divde our key space into segments
|
|
382
|
+
# each split_point defines last key that belongs to a segment
|
|
383
|
+
# our goal is to break up the query into subqueries that each operate in a single segment
|
|
384
|
+
split_points = [sample[0] for sample in shard_keys if sample[0]]
|
|
385
|
+
|
|
386
|
+
# handle row_keys
|
|
387
|
+
# use binary search to find the segment that each key belongs to
|
|
388
|
+
for this_key in list(self.row_keys):
|
|
389
|
+
# bisect_left: in case of exact match, pick left side (keys are inclusive ends)
|
|
390
|
+
segment_index = bisect_left(split_points, this_key)
|
|
391
|
+
sharded_queries[segment_index].add_key(this_key)
|
|
392
|
+
|
|
393
|
+
# handle row_ranges
|
|
394
|
+
for this_range in self.row_ranges:
|
|
395
|
+
# defer to _shard_range helper
|
|
396
|
+
for segment_index, added_range in self._shard_range(
|
|
397
|
+
this_range, split_points
|
|
398
|
+
):
|
|
399
|
+
sharded_queries[segment_index].add_range(added_range)
|
|
400
|
+
# return list of queries ordered by segment index
|
|
401
|
+
# pull populated segments out of sharded_queries dict
|
|
402
|
+
keys = sorted(list(sharded_queries.keys()))
|
|
403
|
+
# return list of queries
|
|
404
|
+
return [sharded_queries[k] for k in keys]
|
|
405
|
+
|
|
406
|
+
@staticmethod
|
|
407
|
+
def _shard_range(
|
|
408
|
+
orig_range: RowRange, split_points: list[bytes]
|
|
409
|
+
) -> list[tuple[int, RowRange]]:
|
|
410
|
+
"""
|
|
411
|
+
Helper function for sharding row_range into subranges that fit into
|
|
412
|
+
segments of the key-space, determined by split_points
|
|
413
|
+
|
|
414
|
+
Args:
|
|
415
|
+
orig_range: a row range to split
|
|
416
|
+
split_points: a list of row keys that define the boundaries of segments.
|
|
417
|
+
each point represents the inclusive end of a segment
|
|
418
|
+
Returns:
|
|
419
|
+
list[tuple[int, RowRange]]: a list of tuples, containing a segment index and a new sub-range.
|
|
420
|
+
"""
|
|
421
|
+
# 1. find the index of the segment the start key belongs to
|
|
422
|
+
if orig_range.start_key is None:
|
|
423
|
+
# if range is open on the left, include first segment
|
|
424
|
+
start_segment = 0
|
|
425
|
+
else:
|
|
426
|
+
# use binary search to find the segment the start key belongs to
|
|
427
|
+
# bisect method determines how we break ties when the start key matches a split point
|
|
428
|
+
# if inclusive, bisect_left to the left segment, otherwise bisect_right
|
|
429
|
+
bisect = bisect_left if orig_range.start_is_inclusive else bisect_right
|
|
430
|
+
start_segment = bisect(split_points, orig_range.start_key)
|
|
431
|
+
|
|
432
|
+
# 2. find the index of the segment the end key belongs to
|
|
433
|
+
if orig_range.end_key is None:
|
|
434
|
+
# if range is open on the right, include final segment
|
|
435
|
+
end_segment = len(split_points)
|
|
436
|
+
else:
|
|
437
|
+
# use binary search to find the segment the end key belongs to.
|
|
438
|
+
end_segment = bisect_left(
|
|
439
|
+
split_points, orig_range.end_key, lo=start_segment
|
|
440
|
+
)
|
|
441
|
+
# note: end_segment will always bisect_left, because split points represent inclusive ends
|
|
442
|
+
# whether the end_key is includes the split point or not, the result is the same segment
|
|
443
|
+
# 3. create new range definitions for each segment this_range spans
|
|
444
|
+
if start_segment == end_segment:
|
|
445
|
+
# this_range is contained in a single segment.
|
|
446
|
+
# Add this_range to that segment's query only
|
|
447
|
+
return [(start_segment, orig_range)]
|
|
448
|
+
else:
|
|
449
|
+
results: list[tuple[int, RowRange]] = []
|
|
450
|
+
# this_range spans multiple segments. Create a new range for each segment's query
|
|
451
|
+
# 3a. add new range for first segment this_range spans
|
|
452
|
+
# first range spans from start_key to the split_point representing the last key in the segment
|
|
453
|
+
last_key_in_first_segment = split_points[start_segment]
|
|
454
|
+
start_range = RowRange(
|
|
455
|
+
start_key=orig_range.start_key,
|
|
456
|
+
start_is_inclusive=orig_range.start_is_inclusive,
|
|
457
|
+
end_key=last_key_in_first_segment,
|
|
458
|
+
end_is_inclusive=True,
|
|
459
|
+
)
|
|
460
|
+
results.append((start_segment, start_range))
|
|
461
|
+
# 3b. add new range for last segment this_range spans
|
|
462
|
+
# we start the final range using the end key from of the previous segment, with is_inclusive=False
|
|
463
|
+
previous_segment = end_segment - 1
|
|
464
|
+
last_key_before_segment = split_points[previous_segment]
|
|
465
|
+
end_range = RowRange(
|
|
466
|
+
start_key=last_key_before_segment,
|
|
467
|
+
start_is_inclusive=False,
|
|
468
|
+
end_key=orig_range.end_key,
|
|
469
|
+
end_is_inclusive=orig_range.end_is_inclusive,
|
|
470
|
+
)
|
|
471
|
+
results.append((end_segment, end_range))
|
|
472
|
+
# 3c. add new spanning range to all segments other than the first and last
|
|
473
|
+
for this_segment in range(start_segment + 1, end_segment):
|
|
474
|
+
prev_segment = this_segment - 1
|
|
475
|
+
prev_end_key = split_points[prev_segment]
|
|
476
|
+
this_end_key = split_points[prev_segment + 1]
|
|
477
|
+
new_range = RowRange(
|
|
478
|
+
start_key=prev_end_key,
|
|
479
|
+
start_is_inclusive=False,
|
|
480
|
+
end_key=this_end_key,
|
|
481
|
+
end_is_inclusive=True,
|
|
482
|
+
)
|
|
483
|
+
results.append((this_segment, new_range))
|
|
484
|
+
return results
|
|
485
|
+
|
|
486
|
+
def _to_pb(self, table) -> ReadRowsRequestPB:
|
|
487
|
+
"""
|
|
488
|
+
Convert this query into a dictionary that can be used to construct a
|
|
489
|
+
ReadRowsRequest protobuf
|
|
490
|
+
"""
|
|
491
|
+
return ReadRowsRequestPB(
|
|
492
|
+
table_name=table.table_name,
|
|
493
|
+
app_profile_id=table.app_profile_id,
|
|
494
|
+
filter=self.filter._to_pb() if self.filter else None,
|
|
495
|
+
rows_limit=self.limit or 0,
|
|
496
|
+
rows=self._row_set,
|
|
497
|
+
)
|
|
498
|
+
|
|
499
|
+
def __eq__(self, other):
|
|
500
|
+
"""
|
|
501
|
+
RowRanges are equal if they have the same row keys, row ranges,
|
|
502
|
+
filter and limit, or if they both represent a full scan with the
|
|
503
|
+
same filter and limit
|
|
504
|
+
|
|
505
|
+
Args:
|
|
506
|
+
other: the object to compare to
|
|
507
|
+
Returns:
|
|
508
|
+
bool: True if the objects are equal, False otherwise
|
|
509
|
+
"""
|
|
510
|
+
if not isinstance(other, ReadRowsQuery):
|
|
511
|
+
return False
|
|
512
|
+
# empty queries are equal
|
|
513
|
+
if len(self.row_keys) == 0 and len(other.row_keys) == 0:
|
|
514
|
+
this_range_empty = len(self.row_ranges) == 0 or all(
|
|
515
|
+
[bool(r) is False for r in self.row_ranges]
|
|
516
|
+
)
|
|
517
|
+
other_range_empty = len(other.row_ranges) == 0 or all(
|
|
518
|
+
[bool(r) is False for r in other.row_ranges]
|
|
519
|
+
)
|
|
520
|
+
if this_range_empty and other_range_empty:
|
|
521
|
+
return self.filter == other.filter and self.limit == other.limit
|
|
522
|
+
# otherwise, sets should have same sizes
|
|
523
|
+
if len(self.row_keys) != len(other.row_keys):
|
|
524
|
+
return False
|
|
525
|
+
if len(self.row_ranges) != len(other.row_ranges):
|
|
526
|
+
return False
|
|
527
|
+
ranges_match = all([row in other.row_ranges for row in self.row_ranges])
|
|
528
|
+
return (
|
|
529
|
+
self.row_keys == other.row_keys
|
|
530
|
+
and ranges_match
|
|
531
|
+
and self.filter == other.filter
|
|
532
|
+
and self.limit == other.limit
|
|
533
|
+
)
|
|
534
|
+
|
|
535
|
+
def __repr__(self):
|
|
536
|
+
return f"ReadRowsQuery(row_keys={list(self.row_keys)}, row_ranges={list(self.row_ranges)}, row_filter={self.filter}, limit={self.limit})"
|