Skip to content

Commit 7e3b50c

Browse files
committed
More refactor and optimization
1 parent 038a63b commit 7e3b50c

File tree

3 files changed

+443
-649
lines changed

3 files changed

+443
-649
lines changed

bindings/python/pymongoarrow/api.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,6 @@
3838
from pymongoarrow.schema import Schema
3939
from pymongoarrow.types import _validate_schema, get_numpy_type
4040

41-
try: # noqa: SIM105
42-
from pymongoarrow.lib import process_bson_stream
43-
except ImportError:
44-
pass
45-
4641
__all__ = [
4742
"aggregate_arrow_all",
4843
"find_arrow_all",
@@ -108,7 +103,7 @@ def find_arrow_all(collection, query, *, schema=None, **kwargs):
108103

109104
raw_batch_cursor = collection.find_raw_batches(query, **kwargs)
110105
for batch in raw_batch_cursor:
111-
process_bson_stream(batch, context)
106+
context.process_bson_stream(batch)
112107

113108
return context.finish()
114109

@@ -152,7 +147,7 @@ def aggregate_arrow_all(collection, pipeline, *, schema=None, **kwargs):
152147

153148
raw_batch_cursor = collection.aggregate_raw_batches(pipeline, **kwargs)
154149
for batch in raw_batch_cursor:
155-
process_bson_stream(batch, context)
150+
context.process_bson_stream(batch)
156151

157152
return context.finish()
158153

bindings/python/pymongoarrow/context.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from pymongoarrow.lib import (
2222
BinaryBuilder,
2323
BoolBuilder,
24+
BuilderManager,
2425
CodeBuilder,
2526
Date32Builder,
2627
Date64Builder,
@@ -75,6 +76,7 @@ def __init__(self, schema, builder_map, codec_options=None):
7576
self.tzinfo = codec_options.tzinfo
7677
else:
7778
self.tzinfo = None
79+
self.manager = BuilderManager(builder_map, self.schema is not None, self.tzinfo)
7880

7981
@classmethod
8082
def from_schema(cls, schema, codec_options=DEFAULT_CODEC_OPTIONS):
@@ -95,6 +97,9 @@ def from_schema(cls, schema, codec_options=DEFAULT_CODEC_OPTIONS):
9597
_parse_types(str_type_map, builder_map, tzinfo)
9698
return cls(schema, builder_map)
9799

100+
def process_bson_stream(self, stream):
101+
self.manager.process_bson_stream(stream, len(stream))
102+
98103
def finish(self):
99104
return self._finish(self.builder_map, self.schema)
100105

0 commit comments

Comments
 (0)