{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
module Network.Riak.Basic
(
ClientID
, Client(..)
, defaultClient
, Connection(..)
, connect
, disconnect
, ping
, getClientID
, setClientID
, getServerInfo
, Quorum(..)
, get
, put
, put_
, delete
, listBuckets
, foldKeys
, getBucket
, setBucket
, getBucketType
, mapReduce
) where
#if __GLASGOW_HASKELL__ < 710
import Control.Applicative ((<$>))
#endif
import Control.Monad.IO.Class
import qualified Data.Foldable as F
import Data.Maybe (fromMaybe)
import qualified Data.Sequence as Seq
import Network.Riak.Connection.Internal
import Network.Riak.Escape (unescape)
import Network.Riak.Protocol.BucketProps
import Network.Riak.Protocol.Content
import Network.Riak.Protocol.ListKeysResponse
import Network.Riak.Protocol.MapReduce as MapReduce
import Network.Riak.Protocol.ServerInfo
import qualified Network.Riak.Request as Req
import qualified Network.Riak.Response as Resp
import Network.Riak.Types.Internal hiding (MessageTag (..))
import qualified Network.Riak.Types.Internal as T
ping :: Connection -> IO ()
ping conn = exchange_ conn Req.ping
getClientID :: Connection -> IO ClientID
getClientID conn = Resp.getClientID <$> exchange conn Req.getClientID
getServerInfo :: Connection -> IO ServerInfo
getServerInfo conn = exchange conn Req.getServerInfo
get :: Connection -> Maybe T.BucketType -> T.Bucket -> T.Key -> R
-> IO (Maybe (Seq.Seq Content, VClock))
get conn btype bucket key r = Resp.get <$> exchangeMaybe conn (Req.get btype bucket key r)
put :: Connection -> Maybe T.BucketType -> T.Bucket -> T.Key -> Maybe T.VClock
-> Content -> W -> DW
-> IO (Seq.Seq Content, VClock)
put conn btype bucket key mvclock cont w dw =
Resp.put <$> exchange conn (Req.put btype bucket key mvclock cont w dw True)
put_ :: Connection -> Maybe T.BucketType -> T.Bucket -> T.Key -> Maybe T.VClock
-> Content -> W -> DW
-> IO ()
put_ conn btype bucket key mvclock cont w dw =
exchange_ conn (Req.put btype bucket key mvclock cont w dw False)
delete :: Connection -> Maybe T.BucketType -> T.Bucket -> T.Key -> RW -> IO ()
delete conn btype bucket key rw = exchange_ conn $ Req.delete btype bucket key rw
listBuckets :: Connection -> Maybe BucketType -> IO (Seq.Seq T.Bucket)
listBuckets conn btype = Resp.listBuckets <$> exchange conn (Req.listBuckets btype)
foldKeys :: (MonadIO m) => Connection -> Maybe BucketType -> Bucket
-> (a -> Key -> m a) -> a -> m a
foldKeys conn btype bucket f z0 = do
liftIO $ sendRequest conn $ Req.listKeys btype bucket
let g z = f z . unescape
loop z = do
ListKeysResponse{..} <- liftIO $ recvResponse conn
z1 <- F.foldlM g z keys
if fromMaybe False done
then return z1
else loop z1
loop z0
getBucket :: Connection -> Maybe BucketType -> Bucket -> IO BucketProps
getBucket conn btype bucket = Resp.getBucket <$> exchange conn (Req.getBucket btype bucket)
setBucket :: Connection -> Maybe BucketType -> Bucket -> BucketProps -> IO ()
setBucket conn btype bucket props = exchange_ conn $ Req.setBucket btype bucket props
getBucketType :: Connection -> T.BucketType -> IO BucketProps
getBucketType conn btype = Resp.getBucket <$> exchange conn (Req.getBucketType btype)
mapReduce :: Connection -> Job -> (a -> MapReduce -> a) -> a -> IO a
mapReduce conn job f z0 = loop z0 =<< (exchange conn . Req.mapReduce $ job)
where
loop z mr = do
let !z' = f z mr
if fromMaybe False . MapReduce.done $ mr
then return z'
else loop z' =<< recvResponse conn