@@ -658,19 +658,20 @@ splitAtLimit maxSize maxCount list = chop (go 0 0 []) list
658
658
where
659
659
go :: Int -> Int -> [Document ] -> [Document ] -> (Either Failure [Document ], [Document ])
660
660
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
674
675
675
676
chop :: ([a ] -> (b , [a ])) -> [a ] -> [b ]
676
677
chop _ [] = []
0 commit comments