adding ConnectOpts and an initial SlaveOk option

This commit is contained in:
Scott R. Parish 2010-02-26 19:06:22 -06:00
parent 2cee44d6a3
commit 08b601f124
2 changed files with 24 additions and 16 deletions

View file

@ -26,7 +26,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
module Database.MongoDB module Database.MongoDB
( (
-- * Connection -- * Connection
Connection, Connection, ConnectOpt(..),
connect, connectOnPort, conClose, disconnect, dropDatabase, connect, connectOnPort, conClose, disconnect, dropDatabase,
connectCluster, connectClusterOnPort, connectCluster, connectClusterOnPort,
serverInfo, serverShutdown, serverInfo, serverShutdown,
@ -89,26 +89,31 @@ data Connection = Connection {
cRand :: IORef [Int] cRand :: IORef [Int]
} }
data ConnectOpt
= SlaveOK -- ^ It's fine to connect to the slave
deriving (Show, Eq)
-- | Establish a connection to a MongoDB server -- | Establish a connection to a MongoDB server
connect :: HostName -> IO Connection connect :: HostName -> [ConnectOpt] -> IO Connection
connect = flip connectOnPort $ Network.PortNumber 27017 connect = flip connectOnPort (Network.PortNumber 27017)
-- | Establish connections to a list of MongoDB servers -- | Establish connections to a list of MongoDB servers
connectCluster :: [HostName] -> IO Connection connectCluster :: [HostName] -> [ConnectOpt] -> IO Connection
connectCluster xs = connectCluster xs =
connectClusterOnPort $ fmap (flip (,) $ Network.PortNumber 27017) xs connectClusterOnPort (fmap (flip (,) $ Network.PortNumber 27017) xs)
-- | Establish connections to a list of MongoDB servers specifying each port. -- | Establish connections to a list of MongoDB servers specifying each port.
connectClusterOnPort :: [(HostName, Network.PortID)] -> IO Connection connectClusterOnPort :: [(HostName, Network.PortID)] -> [ConnectOpt]
connectClusterOnPort [] = throwOpFailure "No hostnames in list" -> IO Connection
connectClusterOnPort servers = newConnection servers connectClusterOnPort [] _ = throwOpFailure "No hostnames in list"
connectClusterOnPort servers opts = newConnection servers opts
-- | Establish a connection to a MongoDB server on a non-standard port -- | Establish a connection to a MongoDB server on a non-standard port
connectOnPort :: HostName -> Network.PortID -> IO Connection connectOnPort :: HostName -> Network.PortID -> [ConnectOpt] -> IO Connection
connectOnPort host port = newConnection [(host, port)] connectOnPort host port = newConnection [(host, port)]
newConnection :: [(HostName, Network.PortID)] -> IO Connection newConnection :: [(HostName, Network.PortID)] -> [ConnectOpt] -> IO Connection
newConnection servers = do newConnection servers opts = do
r <- newStdGen r <- newStdGen
let ns = randomRs (fromIntegral (minBound :: Int32), let ns = randomRs (fromIntegral (minBound :: Int32),
fromIntegral (maxBound :: Int32)) r fromIntegral (maxBound :: Int32)) r
@ -116,7 +121,8 @@ newConnection servers = do
hRef <- openHandle (head servers) >>= newIORef hRef <- openHandle (head servers) >>= newIORef
let c = Connection hRef nsRef let c = Connection hRef nsRef
res <- isMaster c res <- isMaster c
if fromBson (fromLookup $ BSON.lookup "ismaster" res) == (1::Int) if fromBson (fromLookup $ BSON.lookup "ismaster" res) == (1::Int) ||
isJust (List.elemIndex SlaveOK opts)
then return c then return c
else case BSON.lookup "remote" res of else case BSON.lookup "remote" res of
Nothing -> throwConFailure "Couldn't find master to connect to" Nothing -> throwConFailure "Couldn't find master to connect to"
@ -268,8 +274,11 @@ splitFullCol col = (L.takeWhile (c2w '.' /=) col,
L.tail $ L.dropWhile (c2w '.' /=) col) L.tail $ L.dropWhile (c2w '.' /=) col)
splitHostPort :: String -> (HostName, Network.PortID) splitHostPort :: String -> (HostName, Network.PortID)
splitHostPort hp = (List.takeWhile (':' /=) hp, splitHostPort hp = (host, port)
Network.Service $ List.tail $ List.dropWhile (':' /=) hp) where host = List.takeWhile (':' /=) hp
port = case List.dropWhile (':' /=) hp of
"" -> Network.PortNumber 27017
pstr -> Network.Service $ List.tail pstr
-- | Run a database command. Usually this is unneeded as driver wraps -- | Run a database command. Usually this is unneeded as driver wraps
-- all of the commands for you (eg 'createCollection', -- all of the commands for you (eg 'createCollection',

3
TODO
View file

@ -19,7 +19,6 @@ MongoDB
- hint, explain, $where - hint, explain, $where
- database profiling: set/get profiling level, get profiling info - database profiling: set/get profiling level, get profiling info
- pair mode connection - pair mode connection
- allow connecting only to slave
- handle defunct servers - handle defunct servers
- connection fail over - connection fail over
- operations on database objects - operations on database objects
@ -58,7 +57,7 @@ MongoDB
optional: optional:
- automatic reconnection - automatic reconnection
- buffer pooling - buffer pooling
- advanced connection management (master-server, replica pair, Option_SlaveOk) - advanced connection management (master-server, replica pair)
- Tailable cursor support - Tailable cursor support
+ support safe operations + support safe operations
+ auto-reconnection + auto-reconnection