Skip to content

Commit 14fc75a

Browse files
committed
Merge Upper limit for batch size in Query.splitAtLimit
PR#148
2 parents 1eb056f + 0b27853 commit 14fc75a

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

Database/MongoDB/Query.hs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -658,19 +658,20 @@ splitAtLimit maxSize maxCount list = chop (go 0 0 []) list
658658
where
659659
go :: Int -> Int -> [Document] -> [Document] -> (Either Failure [Document], [Document])
660660
go _ _ res [] = (Right $ reverse res, [])
661-
go curSize curCount [] (x:xs) |
662-
(curSize + sizeOfDocument x + 2 + curCount) > maxSize =
663-
(Left $ WriteFailure 0 0 "One document is too big for the message", xs)
664-
go curSize curCount res (x:xs) =
665-
if ((curSize + sizeOfDocument x + 2 + curCount) > maxSize)
666-
-- we have ^ 2 brackets and curCount commas in
667-
-- the document that we need to take into
668-
-- account
669-
|| ((curCount + 1) > maxCount)
670-
then
671-
(Right $ reverse res, x:xs)
672-
else
673-
go (curSize + sizeOfDocument x) (curCount + 1) (x:res) xs
661+
go curSize curCount res (x : xs) =
662+
let size = sizeOfDocument x + 8
663+
in {- 8 bytes =
664+
1 byte: element type.
665+
6 bytes: key name. |key| <= log (maxWriteBatchSize = 100000)
666+
1 byte: \x00.
667+
See https://bsonspec.org/spec.html
668+
-}
669+
if (curSize + size > maxSize) || (curCount + 1 > maxCount)
670+
then
671+
if curCount == 0
672+
then (Left $ WriteFailure 0 0 "One document is too big for the message", xs)
673+
else (Right $ reverse res, x : xs)
674+
else go (curSize + size) (curCount + 1) (x : res) xs
674675

675676
chop :: ([a] -> (b, [a])) -> [a] -> [b]
676677
chop _ [] = []

0 commit comments

Comments
 (0)