@@ -625,19 +625,20 @@ splitAtLimit maxSize maxCount list = chop (go 0 0 []) list
625
625
where
626
626
go :: Int -> Int -> [Document ] -> [Document ] -> (Either Failure [Document ], [Document ])
627
627
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
641
642
642
643
chop :: ([a ] -> (b , [a ])) -> [a ] -> [b ]
643
644
chop _ [] = []
0 commit comments