Skip to content

Commit d436a6f

Browse files
committed
restored default client-side chunking behavior
1 parent 099406e commit d436a6f

File tree

3 files changed

+14
-17
lines changed

3 files changed

+14
-17
lines changed

django_mongodb_backend/compiler.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from django.db.models.functions.math import Power
1212
from django.db.models.lookups import IsNull
1313
from django.db.models.sql import compiler
14-
from django.db.models.sql.constants import MULTI, SINGLE
14+
from django.db.models.sql.constants import GET_ITERATOR_CHUNK_SIZE, MULTI, SINGLE
1515
from django.db.models.sql.datastructures import BaseTable
1616
from django.utils.functional import cached_property
1717
from pymongo import ASCENDING, DESCENDING
@@ -235,7 +235,9 @@ def pre_sql_setup(self, with_col_aliases=False):
235235
self.order_by_objs = [expr.replace_expressions(all_replacements) for expr, _ in order_by]
236236
return extra_select, order_by, group_by
237237

238-
def execute_sql(self, result_type=MULTI, chunked_fetch=False, chunk_size=None):
238+
def execute_sql(
239+
self, result_type=MULTI, chunked_fetch=False, chunk_size=GET_ITERATOR_CHUNK_SIZE
240+
):
239241
self.pre_sql_setup()
240242
try:
241243
query = self.build_query(
@@ -315,16 +317,15 @@ def _make_result(self, entity, columns):
315317
result.append(obj.get(name))
316318
return result
317319

318-
def cursor_iter(self, cursor, _, columns):
319-
"""
320-
Yield chunks of results from cursor.
321-
Ignore any chunk_size/batch_size overrides and abide by MongoDB's
322-
default chunking behavior. This maximizes performance on document
323-
retrieval.
324-
Read more here: https://www.mongodb.com/docs/manual/core/cursors/#cursor-batches
325-
"""
320+
def cursor_iter(self, cursor, chunk_size, columns):
321+
"""Yield chunks of results from cursor."""
322+
chunk = []
326323
for row in cursor:
327-
yield [self._make_result(row, columns)]
324+
chunk.append(self._make_result(row, columns))
325+
if len(chunk) == chunk_size:
326+
yield chunk
327+
chunk = []
328+
yield chunk
328329

329330
def check_query(self):
330331
"""Check if the current query is supported by the database."""

docs/source/releases/5.1.x.rst

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,8 @@ Bug fixes
1515
the ``base_field`` uses a database converter.
1616
- Fixed ``RecursionError`` when using ``Trunc`` database functions on non-MongoDB
1717
databases.
18-
- Removed user configurable request chunking. Allowing MongoDB to
18+
- Removed user configurable server-side request chunking. Allowing MongoDB to
1919
handle chunking automatically improves performance and reduces complexity.
20-
As a result, :meth:`QuerySet.iterator() <django.db.models.query.QuerySet.iterator>`
21-
ignores the ``chunk_size`` parameter.
2220

2321

2422
5.1.0 beta 3

docs/source/releases/5.2.x.rst

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,8 @@ Bug fixes
2525
databases.
2626
- :meth:`QuerySet.explain() <django.db.models.query.QuerySet.explain>` now
2727
:ref:`returns a string that can be parsed as JSON <queryset-explain>`.
28-
- Removed user configurable request chunking. Allowing MongoDB to
28+
- Removed user configurable server-side request chunking. Allowing MongoDB to
2929
handle chunking automatically improves performance and reduces complexity.
30-
As a result, :meth:`QuerySet.iterator() <django.db.models.query.QuerySet.iterator>`
31-
ignores the ``chunk_size`` parameter.
3230

3331

3432
5.2.0 beta 1

0 commit comments

Comments
 (0)