Skip to content

Commit 0b27853

Browse files
committed
fix: document size upper limit in Query.splitAtLimit
1 parent 35ee53a commit 0b27853

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
@@ -625,19 +625,20 @@ splitAtLimit maxSize maxCount list = chop (go 0 0 []) list
625625
where
626626
go :: Int -> Int -> [Document] -> [Document] -> (Either Failure [Document], [Document])
627627
go _ _ res [] = (Right $ reverse res, [])
628-
go curSize curCount [] (x:xs) |
629-
(curSize + sizeOfDocument x + 2 + curCount) > maxSize =
630-
(Left $ WriteFailure 0 0 "One document is too big for the message", xs)
631-
go curSize curCount res (x:xs) =
632-
if ((curSize + sizeOfDocument x + 2 + curCount) > maxSize)
633-
-- we have ^ 2 brackets and curCount commas in
634-
-- the document that we need to take into
635-
-- account
636-
|| ((curCount + 1) > maxCount)
637-
then
638-
(Right $ reverse res, x:xs)
639-
else
640-
go (curSize + sizeOfDocument x) (curCount + 1) (x:res) xs
628+
go curSize curCount res (x : xs) =
629+
let size = sizeOfDocument x + 8
630+
in {- 8 bytes =
631+
1 byte: element type.
632+
6 bytes: key name. |key| <= log (maxWriteBatchSize = 100000)
633+
1 byte: \x00.
634+
See https://bsonspec.org/spec.html
635+
-}
636+
if (curSize + size > maxSize) || (curCount + 1 > maxCount)
637+
then
638+
if curCount == 0
639+
then (Left $ WriteFailure 0 0 "One document is too big for the message", xs)
640+
else (Right $ reverse res, x : xs)
641+
else go (curSize + size) (curCount + 1) (x : res) xs
641642

642643
chop :: ([a] -> (b, [a])) -> [a] -> [b]
643644
chop _ [] = []

0 commit comments

Comments
 (0)