Skip to content

Commit 3d8643e

Browse files
committed
use BSON.encode to determine payload
1 parent 3751e4a commit 3d8643e

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

django_mongodb_backend/compiler.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import itertools
2-
import sys
32
from collections import defaultdict
43

5-
from bson import SON, json_util
4+
from bson import SON, encode, json_util
65
from django.core.exceptions import EmptyResultSet, FieldError, FullResultSet
76
from django.db import IntegrityError, NotSupportedError
87
from django.db.models import Count
@@ -335,9 +334,10 @@ def cursor_iter(self, cursor, _, columns):
335334
if len(chunk) == chunk_size:
336335
if i == chunk_size - 1: # First chunk complete
337336
# Using current row as representation, approximate
338-
# how many rows can fit in a 16MB payload (MongoDB batch_size max)
339-
# then set that as the new chunk size.
340-
chunk_size = MAX_BATCH_SIZE_MB // sys.getsizeof(row)
337+
# how many rows can fit in a 16MB payload
338+
# (MongoDB batch_size max), then set that as the new size.
339+
approx_chunk_size = MAX_BATCH_SIZE_MB // len(encode(row))
340+
chunk_size = max(chunk_size, approx_chunk_size)
341341
yield chunk
342342
chunk = []
343343
yield chunk

0 commit comments

Comments
 (0)