1515
1616from __future__ import annotations
1717
18- from typing import (
19- TYPE_CHECKING ,
20- AsyncGenerator ,
21- AsyncIterable ,
22- Awaitable ,
23- Sequence ,
24- )
18+ from typing import Sequence , TYPE_CHECKING
2519
2620from google .cloud .bigtable_v2 .types import ReadRowsRequest as ReadRowsRequestPB
2721from google .cloud .bigtable_v2 .types import ReadRowsResponse as ReadRowsResponsePB
3226from google .cloud .bigtable .data .read_rows_query import ReadRowsQuery
3327from google .cloud .bigtable .data .exceptions import InvalidChunk
3428from google .cloud .bigtable .data .exceptions import _RowSetComplete
29+ from google .cloud .bigtable .data .exceptions import _ResetRow
3530from google .cloud .bigtable .data ._helpers import _attempt_timeout_generator
3631from google .cloud .bigtable .data ._helpers import _retry_exception_factory
3732
3833from google .api_core import retry as retries
3934from google .api_core .retry import exponential_sleep_generator
4035
41- if TYPE_CHECKING :
42- from google .cloud .bigtable .data ._async .client import TableAsync
36+ from google .cloud .bigtable .data ._cross_sync import CrossSync
4337
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
4443
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"
4845
4946
47+ @CrossSync .convert_class ("_ReadRowsOperation" )
5048class _ReadRowsOperationAsync :
5149 """
5250 ReadRowsOperation handles the logic of merging chunks from a ReadRowsResponse stream
@@ -80,7 +78,7 @@ class _ReadRowsOperationAsync:
8078 def __init__ (
8179 self ,
8280 query : ReadRowsQuery ,
83- table : "TableAsync" ,
81+ table : TableType ,
8482 operation_timeout : float ,
8583 attempt_timeout : float ,
8684 retryable_exceptions : Sequence [type [Exception ]] = (),
@@ -102,22 +100,22 @@ def __init__(
102100 self ._last_yielded_row_key : bytes | None = None
103101 self ._remaining_count : int | None = self .request .rows_limit or None
104102
105- def start_operation (self ) -> AsyncGenerator [Row , None ]:
103+ def start_operation (self ) -> CrossSync . Iterable [Row ]:
106104 """
107105 Start the read_rows operation, retrying on retryable errors.
108106
109107 Yields:
110108 Row: The next row in the stream
111109 """
112- return retries . retry_target_stream_async (
110+ return CrossSync . retry_target_stream (
113111 self ._read_rows_attempt ,
114112 self ._predicate ,
115113 exponential_sleep_generator (0.01 , 60 , multiplier = 2 ),
116114 self .operation_timeout ,
117115 exception_factory = _retry_exception_factory ,
118116 )
119117
120- def _read_rows_attempt (self ) -> AsyncGenerator [Row , None ]:
118+ def _read_rows_attempt (self ) -> CrossSync . Iterable [Row ]:
121119 """
122120 Attempt a single read_rows rpc call.
123121 This function is intended to be wrapped by retry logic,
@@ -152,9 +150,10 @@ def _read_rows_attempt(self) -> AsyncGenerator[Row, None]:
152150 chunked_stream = self .chunk_stream (gapic_stream )
153151 return self .merge_rows (chunked_stream )
154152
153+ @CrossSync .convert ()
155154 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 ]:
158157 """
159158 process chunks out of raw read_rows stream
160159
@@ -204,9 +203,12 @@ async def chunk_stream(
204203 current_key = None
205204
206205 @staticmethod
206+ @CrossSync .convert (
207+ replace_symbols = {"__aiter__" : "__iter__" , "__anext__" : "__next__" },
208+ )
207209 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 ]:
210212 """
211213 Merge chunks into rows
212214
@@ -222,7 +224,7 @@ async def merge_rows(
222224 while True :
223225 try :
224226 c = await it .__anext__ ()
225- except StopAsyncIteration :
227+ except CrossSync . StopIteration :
226228 # stream complete
227229 return
228230 row_key = c .row_key
@@ -315,7 +317,7 @@ async def merge_rows(
315317 ):
316318 raise InvalidChunk ("reset row with data" )
317319 continue
318- except StopAsyncIteration :
320+ except CrossSync . StopIteration :
319321 raise InvalidChunk ("premature end of stream" )
320322
321323 @staticmethod
0 commit comments