From 80ebc6f756ca666b647f4b64caaead6d42d53619 Mon Sep 17 00:00:00 2001 From: Victor Denisov Date: Thu, 24 Nov 2016 14:18:00 -0800 Subject: [PATCH] Throw exception when insertAll encounters big doc - insertAll should throw an exception if it encounters a document too big for one message. However all other documents will be inserted. - slightly edit docs --- Database/MongoDB/Query.hs | 13 ++++++++++--- test/QuerySpec.hs | 2 +- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/Database/MongoDB/Query.hs b/Database/MongoDB/Query.hs index 381803d..c05c110 100644 --- a/Database/MongoDB/Query.hs +++ b/Database/MongoDB/Query.hs @@ -433,7 +433,11 @@ insert_ :: (MonadIO m) => Collection -> Document -> Action m () insert_ col doc = insert col doc >> return () insertMany :: (MonadIO m) => Collection -> [Document] -> Action m [Value] --- ^ Insert documents into collection and return their \"_id\" values, which are created automatically if not supplied. If a document fails to be inserted (eg. due to duplicate key) then remaining docs are aborted, and LastError is set. +-- ^ Insert documents into collection and return their \"_id\" values, +-- which are created automatically if not supplied. +-- If a document fails to be inserted (eg. due to duplicate key) +-- then remaining docs are aborted, and LastError is set. +-- An exception will be throw if any error occurs. insertMany = insert' [] insertMany_ :: (MonadIO m) => Collection -> [Document] -> Action m () @@ -441,7 +445,10 @@ insertMany_ :: (MonadIO m) => Collection -> [Document] -> Action m () insertMany_ col docs = insertMany col docs >> return () insertAll :: (MonadIO m) => Collection -> [Document] -> Action m [Value] --- ^ Insert documents into collection and return their \"_id\" values, which are created automatically if not supplied. If a document fails to be inserted (eg. due to duplicate key) then remaining docs are still inserted. LastError is set if any doc fails, not just last one. +-- ^ Insert documents into collection and return their \"_id\" values, +-- which are created automatically if not supplied. If a document fails +-- to be inserted (eg. due to duplicate key) then remaining docs +-- are still inserted. insertAll = insert' [KeepGoing] insertAll_ :: (MonadIO m) => Collection -> [Document] -> Action m () @@ -493,7 +500,7 @@ insert' opts col docs = do chunkResults <- interruptibleFor ordered (zip lSums chunks) $ insertBlock opts col let lchunks = lefts preChunks - when ((not $ null lchunks) && ordered) $ do + when (not $ null lchunks) $ do liftIO $ throwIO $ head lchunks let lresults = lefts chunkResults diff --git a/test/QuerySpec.hs b/test/QuerySpec.hs index c049ebb..fb0712f 100644 --- a/test/QuerySpec.hs +++ b/test/QuerySpec.hs @@ -171,7 +171,7 @@ spec = around withCleanDatabase $ do liftIO $ (length returnedDocs) `shouldBe` 1000 it "skips one too big document" $ do - db $ insertAll_ "hugeDocCollection" [hugeDocument] + (db $ insertAll_ "hugeDocCollection" [hugeDocument]) `shouldThrow` anyException db $ do cur <- find $ (select [] "hugeDocCollection") {limit = 100000, batchSize = 100000} returnedDocs <- rest cur