|
125 | 125 | if TYPE_CHECKING: |
126 | 126 | from google.cloud.bigtable.data._helpers import RowKeySamples |
127 | 127 | from google.cloud.bigtable.data._helpers import ShardedQuery |
| 128 | + from google.cloud.bigtable.data._metrics import ActiveOperationMetric |
128 | 129 |
|
129 | 130 | if CrossSync.is_async: |
130 | 131 | from google.cloud.bigtable.data._async.mutations_batcher import ( |
@@ -1020,14 +1021,18 @@ async def read_rows_stream( |
1020 | 1021 | ) |
1021 | 1022 | retryable_excs = _get_retryable_errors(retryable_errors, self) |
1022 | 1023 |
|
1023 | | - row_merger = CrossSync._ReadRowsOperation( |
1024 | | - query, |
1025 | | - self, |
1026 | | - operation_timeout=operation_timeout, |
1027 | | - attempt_timeout=attempt_timeout, |
1028 | | - retryable_exceptions=retryable_excs, |
1029 | | - ) |
1030 | | - return row_merger.start_operation() |
| 1024 | + with self._metrics.create_operation( |
| 1025 | + OperationType.READ_ROWS, streaming=True |
| 1026 | + ) as operation_metric: |
| 1027 | + row_merger = CrossSync._ReadRowsOperation( |
| 1028 | + query, |
| 1029 | + self, |
| 1030 | + operation_timeout=operation_timeout, |
| 1031 | + attempt_timeout=attempt_timeout, |
| 1032 | + metric=operation_metric, |
| 1033 | + retryable_exceptions=retryable_excs, |
| 1034 | + ) |
| 1035 | + return row_merger.start_operation() |
1031 | 1036 |
|
1032 | 1037 | @CrossSync.convert |
1033 | 1038 | async def read_rows( |
@@ -1077,7 +1082,9 @@ async def read_rows( |
1077 | 1082 | ) |
1078 | 1083 | return [row async for row in row_generator] |
1079 | 1084 |
|
1080 | | - @CrossSync.convert |
| 1085 | + @CrossSync.convert( |
| 1086 | + replace_symbols={"__anext__": "__next__", "StopAsyncIteration": "StopIteration"} |
| 1087 | + ) |
1081 | 1088 | async def read_row( |
1082 | 1089 | self, |
1083 | 1090 | row_key: str | bytes, |
@@ -1117,15 +1124,28 @@ async def read_row( |
1117 | 1124 | if row_key is None: |
1118 | 1125 | raise ValueError("row_key must be string or bytes") |
1119 | 1126 | query = ReadRowsQuery(row_keys=row_key, row_filter=row_filter, limit=1) |
1120 | | - results = await self.read_rows( |
1121 | | - query, |
1122 | | - operation_timeout=operation_timeout, |
1123 | | - attempt_timeout=attempt_timeout, |
1124 | | - retryable_errors=retryable_errors, |
| 1127 | + |
| 1128 | + operation_timeout, attempt_timeout = _get_timeouts( |
| 1129 | + operation_timeout, attempt_timeout, self |
1125 | 1130 | ) |
1126 | | - if len(results) == 0: |
1127 | | - return None |
1128 | | - return results[0] |
| 1131 | + retryable_excs = _get_retryable_errors(retryable_errors, self) |
| 1132 | + |
| 1133 | + with self._metrics.create_operation( |
| 1134 | + OperationType.READ_ROWS, streaming=False |
| 1135 | + ) as operation_metric: |
| 1136 | + row_merger = CrossSync._ReadRowsOperation( |
| 1137 | + query, |
| 1138 | + self, |
| 1139 | + operation_timeout=operation_timeout, |
| 1140 | + attempt_timeout=attempt_timeout, |
| 1141 | + metric=operation_metric, |
| 1142 | + retryable_exceptions=retryable_excs, |
| 1143 | + ) |
| 1144 | + results_generator = row_merger.start_operation() |
| 1145 | + try: |
| 1146 | + return results_generator.__anext__() |
| 1147 | + except StopAsyncIteration: |
| 1148 | + return None |
1129 | 1149 |
|
1130 | 1150 | @CrossSync.convert |
1131 | 1151 | async def read_rows_sharded( |
@@ -1264,20 +1284,17 @@ async def row_exists( |
1264 | 1284 | from any retries that failed |
1265 | 1285 | google.api_core.exceptions.GoogleAPIError: raised if the request encounters an unrecoverable error |
1266 | 1286 | """ |
1267 | | - if row_key is None: |
1268 | | - raise ValueError("row_key must be string or bytes") |
1269 | | - |
1270 | 1287 | strip_filter = StripValueTransformerFilter(flag=True) |
1271 | 1288 | limit_filter = CellsRowLimitFilter(1) |
1272 | 1289 | chain_filter = RowFilterChain(filters=[limit_filter, strip_filter]) |
1273 | | - query = ReadRowsQuery(row_keys=row_key, limit=1, row_filter=chain_filter) |
1274 | | - results = await self.read_rows( |
1275 | | - query, |
| 1290 | + result = await self.read_row( |
| 1291 | + row_key=row_key, |
| 1292 | + row_filter=chain_filter, |
1276 | 1293 | operation_timeout=operation_timeout, |
1277 | 1294 | attempt_timeout=attempt_timeout, |
1278 | 1295 | retryable_errors=retryable_errors, |
1279 | 1296 | ) |
1280 | | - return len(results) > 0 |
| 1297 | + return result is not None |
1281 | 1298 |
|
1282 | 1299 | @CrossSync.convert |
1283 | 1300 | async def sample_row_keys( |
@@ -1334,6 +1351,7 @@ async def sample_row_keys( |
1334 | 1351 | async with self._metrics.create_operation( |
1335 | 1352 | OperationType.SAMPLE_ROW_KEYS, backoff_generator=sleep_generator |
1336 | 1353 | ): |
| 1354 | + |
1337 | 1355 | @CrossSync.convert |
1338 | 1356 | async def execute_rpc(): |
1339 | 1357 | results = await self.client._gapic_client.sample_row_keys( |
@@ -1598,14 +1616,14 @@ async def check_and_mutate_row( |
1598 | 1616 | false_case_mutations = [false_case_mutations] |
1599 | 1617 | false_case_list = [m._to_pb() for m in false_case_mutations or []] |
1600 | 1618 |
|
1601 | | - async with self._metrics.create_operation( |
1602 | | - OperationType.CHECK_AND_MUTATE |
1603 | | - ): |
| 1619 | + async with self._metrics.create_operation(OperationType.CHECK_AND_MUTATE): |
1604 | 1620 | result = await self.client._gapic_client.check_and_mutate_row( |
1605 | 1621 | request=CheckAndMutateRowRequest( |
1606 | 1622 | true_mutations=true_case_list, |
1607 | 1623 | false_mutations=false_case_list, |
1608 | | - predicate_filter=predicate._to_pb() if predicate is not None else None, |
| 1624 | + predicate_filter=predicate._to_pb() |
| 1625 | + if predicate is not None |
| 1626 | + else None, |
1609 | 1627 | row_key=row_key.encode("utf-8") |
1610 | 1628 | if isinstance(row_key, str) |
1611 | 1629 | else row_key, |
@@ -1656,9 +1674,7 @@ async def read_modify_write_row( |
1656 | 1674 | if not rules: |
1657 | 1675 | raise ValueError("rules must contain at least one item") |
1658 | 1676 |
|
1659 | | - async with self._metrics.create_operation( |
1660 | | - OperationType.READ_MODIFY_WRITE |
1661 | | - ): |
| 1677 | + async with self._metrics.create_operation(OperationType.READ_MODIFY_WRITE): |
1662 | 1678 | result = await self.client._gapic_client.read_modify_write_row( |
1663 | 1679 | request=ReadModifyWriteRowRequest( |
1664 | 1680 | rules=[rule._to_pb() for rule in rules], |
|
0 commit comments