Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use createIndexes for MongoDB >= 4.2 #155

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions Database/MongoDB/Admin.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions Database/MongoDB/GridFS.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading