15
15
16
16
from __future__ import annotations
17
17
18
- from typing import (
19
- TYPE_CHECKING ,
20
- AsyncGenerator ,
21
- AsyncIterable ,
22
- Awaitable ,
23
- Sequence ,
24
- )
18
+ from typing import Sequence , TYPE_CHECKING
25
19
26
20
from google .cloud .bigtable_v2 .types import ReadRowsRequest as ReadRowsRequestPB
27
21
from google .cloud .bigtable_v2 .types import ReadRowsResponse as ReadRowsResponsePB
32
26
from google .cloud .bigtable .data .read_rows_query import ReadRowsQuery
33
27
from google .cloud .bigtable .data .exceptions import InvalidChunk
34
28
from google .cloud .bigtable .data .exceptions import _RowSetComplete
29
+ from google .cloud .bigtable .data .exceptions import _ResetRow
35
30
from google .cloud .bigtable .data ._helpers import _attempt_timeout_generator
36
31
from google .cloud .bigtable .data ._helpers import _retry_exception_factory
37
32
38
33
from google .api_core import retry as retries
39
34
from google .api_core .retry import exponential_sleep_generator
40
35
41
- if TYPE_CHECKING :
42
- from google .cloud .bigtable .data ._async .client import TableAsync
36
+ from google .cloud .bigtable .data ._cross_sync import CrossSync
43
37
38
+ if TYPE_CHECKING :
39
+ if CrossSync .is_async :
40
+ from google .cloud .bigtable .data ._async .client import TableAsync as TableType
41
+ else :
42
+ from google .cloud .bigtable .data ._sync_autogen .client import Table as TableType # type: ignore
44
43
45
- class _ResetRow (Exception ):
46
- def __init__ (self , chunk ):
47
- self .chunk = chunk
44
+ __CROSS_SYNC_OUTPUT__ = "google.cloud.bigtable.data._sync_autogen._read_rows"
48
45
49
46
47
+ @CrossSync .convert_class ("_ReadRowsOperation" )
50
48
class _ReadRowsOperationAsync :
51
49
"""
52
50
ReadRowsOperation handles the logic of merging chunks from a ReadRowsResponse stream
@@ -80,7 +78,7 @@ class _ReadRowsOperationAsync:
80
78
def __init__ (
81
79
self ,
82
80
query : ReadRowsQuery ,
83
- table : "TableAsync" ,
81
+ table : TableType ,
84
82
operation_timeout : float ,
85
83
attempt_timeout : float ,
86
84
retryable_exceptions : Sequence [type [Exception ]] = (),
@@ -102,22 +100,22 @@ def __init__(
102
100
self ._last_yielded_row_key : bytes | None = None
103
101
self ._remaining_count : int | None = self .request .rows_limit or None
104
102
105
- def start_operation (self ) -> AsyncGenerator [Row , None ]:
103
+ def start_operation (self ) -> CrossSync . Iterable [Row ]:
106
104
"""
107
105
Start the read_rows operation, retrying on retryable errors.
108
106
109
107
Yields:
110
108
Row: The next row in the stream
111
109
"""
112
- return retries . retry_target_stream_async (
110
+ return CrossSync . retry_target_stream (
113
111
self ._read_rows_attempt ,
114
112
self ._predicate ,
115
113
exponential_sleep_generator (0.01 , 60 , multiplier = 2 ),
116
114
self .operation_timeout ,
117
115
exception_factory = _retry_exception_factory ,
118
116
)
119
117
120
- def _read_rows_attempt (self ) -> AsyncGenerator [Row , None ]:
118
+ def _read_rows_attempt (self ) -> CrossSync . Iterable [Row ]:
121
119
"""
122
120
Attempt a single read_rows rpc call.
123
121
This function is intended to be wrapped by retry logic,
@@ -152,9 +150,10 @@ def _read_rows_attempt(self) -> AsyncGenerator[Row, None]:
152
150
chunked_stream = self .chunk_stream (gapic_stream )
153
151
return self .merge_rows (chunked_stream )
154
152
153
+ @CrossSync .convert ()
155
154
async def chunk_stream (
156
- self , stream : Awaitable [AsyncIterable [ReadRowsResponsePB ]]
157
- ) -> AsyncGenerator [ReadRowsResponsePB .CellChunk , None ]:
155
+ self , stream : CrossSync . Awaitable [CrossSync . Iterable [ReadRowsResponsePB ]]
156
+ ) -> CrossSync . Iterable [ReadRowsResponsePB .CellChunk ]:
158
157
"""
159
158
process chunks out of raw read_rows stream
160
159
@@ -204,9 +203,12 @@ async def chunk_stream(
204
203
current_key = None
205
204
206
205
@staticmethod
206
+ @CrossSync .convert (
207
+ replace_symbols = {"__aiter__" : "__iter__" , "__anext__" : "__next__" },
208
+ )
207
209
async def merge_rows (
208
- chunks : AsyncGenerator [ReadRowsResponsePB .CellChunk , None ] | None
209
- ) -> AsyncGenerator [Row , None ]:
210
+ chunks : CrossSync . Iterable [ReadRowsResponsePB .CellChunk ] | None ,
211
+ ) -> CrossSync . Iterable [Row ]:
210
212
"""
211
213
Merge chunks into rows
212
214
@@ -222,7 +224,7 @@ async def merge_rows(
222
224
while True :
223
225
try :
224
226
c = await it .__anext__ ()
225
- except StopAsyncIteration :
227
+ except CrossSync . StopIteration :
226
228
# stream complete
227
229
return
228
230
row_key = c .row_key
@@ -315,7 +317,7 @@ async def merge_rows(
315
317
):
316
318
raise InvalidChunk ("reset row with data" )
317
319
continue
318
- except StopAsyncIteration :
320
+ except CrossSync . StopIteration :
319
321
raise InvalidChunk ("premature end of stream" )
320
322
321
323
@staticmethod
0 commit comments