diff --git a/Database/MongoDB/Admin.hs b/Database/MongoDB/Admin.hs index da9f197..b687511 100644 --- a/Database/MongoDB/Admin.hs +++ b/Database/MongoDB/Admin.hs @@ -33,6 +33,7 @@ import Control.Applicative ((<$>)) #endif import Control.Concurrent (forkIO, threadDelay) import Control.Monad (forever, unless, liftM) +import Control.Monad.Reader (asks) import Data.IORef (IORef, newIORef, readIORef, writeIORef) import Data.Maybe (maybeToList) import Data.Set (Set) @@ -48,12 +49,12 @@ import Data.Text (Text) import qualified Data.Text as T import Database.MongoDB.Connection (Host, showHostPort) -import Database.MongoDB.Internal.Protocol (pwHash, pwKey) +import Database.MongoDB.Internal.Protocol (maxWireVersion, pwHash, pwKey, serverData) import Database.MongoDB.Internal.Util ((<.>), true1) import Database.MongoDB.Query (Action, Database, Collection, Username, Password, Order, Query(..), accessMode, master, runCommand, useDb, thisDatabase, rest, select, find, findOne, - insert_, save, delete) + insert_, save, delete, mongoPipe) -- * Admin @@ -118,7 +119,7 @@ genName :: Order -> IndexName genName keys = T.intercalate "_" (map f keys) where f (k := v) = k `T.append` "_" `T.append` T.pack (show v) -ensureIndex :: (MonadIO m) => Index -> Action m () +ensureIndex :: (MonadFail m, MonadIO m) => Index -> Action m () -- ^ Create index if we did not already create one. May be called repeatedly with practically no performance hit, because we remember if we already called this for the same index (although this memory gets wiped out every 15 minutes, in case another client drops the index and we want to create it again). ensureIndex idx = let k = (iColl idx, iName idx) in do icache <- fetchIndexCache @@ -127,9 +128,24 @@ ensureIndex idx = let k = (iColl idx, iName idx) in do accessMode master (createIndex idx) liftIO $ writeIORef icache (Set.insert k set) -createIndex :: (MonadIO m) => Index -> Action m () +createIndex :: (MonadFail m, MonadIO m) => Index -> Action m () -- ^ Create index on the server. This call goes to the server every time. -createIndex idx = insert_ "system.indexes" . idxDocument idx =<< thisDatabase +createIndex idx = do + pipe <- asks mongoPipe + if maxWireVersion (serverData pipe) < 8 + then + insert_ "system.indexes" . idxDocument idx =<< thisDatabase + else do + -- Starting in MongoDB 4.2 (Wire version 8), system.indexes has been removed + idxDoc <- idxDocument idx <$> thisDatabase + resp <- runCommand + [ "createIndexes" =: iColl idx, + "indexes" =: [idxDoc], + "writeConcern" =: ["w" =: (1 :: Int), "j" =: True] + ] + if true1 "ok" resp + then pure () + else fail $ "createIndex: " <> show resp dropIndex :: (MonadIO m) => Collection -> IndexName -> Action m Document -- ^ Remove the index from the given collection. diff --git a/Database/MongoDB/GridFS.hs b/Database/MongoDB/GridFS.hs index a78bb6c..d2d116b 100644 --- a/Database/MongoDB/GridFS.hs +++ b/Database/MongoDB/GridFS.hs @@ -53,11 +53,11 @@ md5BlockSizeInBytes = 64 data Bucket = Bucket {files :: Text, chunks :: Text} -- ^ Files are stored in "buckets". You open a bucket with openDefaultBucket or openBucket -openDefaultBucket :: (Monad m, MonadIO m) => Action m Bucket +openDefaultBucket :: (MonadFail m, MonadIO m) => Action m Bucket -- ^ Open the default 'Bucket' (named "fs") openDefaultBucket = openBucket "fs" -openBucket :: (Monad m, MonadIO m) => Text -> Action m Bucket +openBucket :: (MonadFail m, MonadIO m) => Text -> Action m Bucket -- ^ Open a 'Bucket' openBucket name = do let filesCollection = name `append` ".files"