Repository navigation
Expand file tree
/
Copy pathresult_batch.py
More file actions
49 lines (37 loc) · 1.6 KB
/
Copy pathresult_batch.py
File metadata and controls
49 lines (37 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""Decode complete V2 result envelopes and retain their original chunk order."""
from collections import deque
from e6data_python_connector.datainputstream import read_rows_from_chunk
def decode_result_batches(columns, payloads):
"""Return all nonempty chunks only after every payload decodes successfully."""
chunks = []
for payload in payloads:
if not payload:
raise ValueError("Empty serialized result chunk.")
rows = read_rows_from_chunk(columns, payload, strict=True)
if rows:
chunks.append(rows)
return chunks
class ResultBatchBuffer:
"""Keep one decoded envelope until its chunks have been delivered."""
def __init__(self):
self._chunks = deque()
self._end_of_stream = False
def accept(self, chunks, end_of_stream):
"""Accept an envelope only after a nonterminal predecessor is drained."""
if self._chunks or self._end_of_stream:
raise ValueError("Cannot replace pending chunks or a terminal result stream.")
self._chunks = deque(chunks)
self._end_of_stream = end_of_stream
def pop(self):
"""Return the next original chunk, or None when none remain."""
return self._chunks.popleft() if self._chunks else None
def clear(self):
"""Release pending chunks and reset the buffer for another query."""
self._chunks.clear()
self._end_of_stream = False
@property
def needs_fetch(self):
return not self._chunks and not self._end_of_stream
@property
def finished(self):
return not self._chunks and self._end_of_stream