module Database.TokyoCabinet.BDB
(
BDB
, ECODE(..)
, OpenMode(..)
, TuningOption(..)
, CMP(..)
, new
, delete
, ecode
, errmsg
, tune
, setcache
, setxmsiz
, setcmpfunc
, open
, close
, put
, putkeep
, putcat
, putdup
, putlist
, out
, outlist
, get
, getlist
, vnum
, vsiz
, range
, fwmkeys
, addint
, adddouble
, sync
, optimize
, vanish
, copy
, tranbegin
, trancommit
, tranabort
, path
, rnum
, fsiz
) where
import Data.Int
import Data.Word
import Data.ByteString (ByteString)
import Data.ByteString.Unsafe (unsafePackCStringLen)
import Foreign.Ptr
import Foreign.ForeignPtr
import Foreign.Storable (peek)
import Foreign.Marshal (alloca)
import Database.TokyoCabinet.Error
import Database.TokyoCabinet.BDB.C
import Database.TokyoCabinet.List.C
import Database.TokyoCabinet.Internal
import Database.TokyoCabinet.Storable
import Database.TokyoCabinet.Sequence
data CMP = CMPLEXICAL |
CMPDECIMAL |
CMPINT32 |
CMPINT64 |
CMP (ByteString -> ByteString -> Ordering)
new :: IO BDB
new = BDB `fmap` (c_tcbdbnew >>= newForeignPtr tcbdbFinalizer)
delete :: BDB -> IO ()
delete bdb = finalizeForeignPtr (unTCBDB bdb)
ecode :: BDB -> IO ECODE
ecode bdb = cintToError `fmap` withForeignPtr (unTCBDB bdb) c_tcbdbecode
tune :: BDB
-> Int32
-> Int32
-> Int64
-> Int8
-> Int8
-> [TuningOption]
-> IO Bool
tune bdb lmemb nmemb bnum apow fpow opts =
withForeignPtr (unTCBDB bdb) $ \bdb' ->
c_tcbdbtune bdb' lmemb nmemb bnum apow fpow (combineTuningOption opts)
setcache :: BDB
-> Int32
-> Int32
-> IO Bool
setcache bdb lcnum ncnum =
withForeignPtr (unTCBDB bdb) $ \bdb' -> c_tcbdbsetcache bdb' lcnum ncnum
setxmsiz :: BDB -> Int64 -> IO Bool
setxmsiz bdb xmsiz =
withForeignPtr (unTCBDB bdb) $ \bdb' -> c_tcbdbsetxmsiz bdb' xmsiz
setcmpfunc :: BDB -> CMP -> IO Bool
setcmpfunc bdb CMPLEXICAL =
withForeignPtr (unTCBDB bdb) $ flip c_tcbdbsetcmpfunc c_tccmplexical
setcmpfunc bdb CMPDECIMAL =
withForeignPtr (unTCBDB bdb) $ flip c_tcbdbsetcmpfunc c_tccmpdecimal
setcmpfunc bdb CMPINT32 =
withForeignPtr (unTCBDB bdb) $ flip c_tcbdbsetcmpfunc c_tccmpint32
setcmpfunc bdb CMPINT64 =
withForeignPtr (unTCBDB bdb) $ flip c_tcbdbsetcmpfunc c_tccmpint64
setcmpfunc bdb (CMP func) =
withForeignPtr (unTCBDB bdb) $ \bdb' ->
do cmpfunc <- mkCMP mycmpfunc
c_tcbdbsetcmpfunc bdb' cmpfunc
where
mycmpfunc :: TCCMP'
mycmpfunc k1buf k1siz k2buf k2siz _ =
do key1 <- unsafePackCStringLen (k1buf, fromIntegral k1siz)
key2 <- unsafePackCStringLen (k2buf, fromIntegral k2siz)
case func key1 key2 of
LT -> return (1)
EQ -> return 0
GT -> return 1
open :: BDB -> String -> [OpenMode] -> IO Bool
open = openHelper c_tcbdbopen unTCBDB combineOpenMode
close :: BDB -> IO Bool
close bdb = withForeignPtr (unTCBDB bdb) c_tcbdbclose
put :: (Storable k, Storable v) => BDB -> k -> v -> IO Bool
put = putHelper c_tcbdbput unTCBDB
putkeep :: (Storable k, Storable v) => BDB -> k -> v -> IO Bool
putkeep = putHelper c_tcbdbputkeep unTCBDB
putcat :: (Storable k, Storable v) => BDB -> k -> v -> IO Bool
putcat = putHelper c_tcbdbputcat unTCBDB
putdup :: (Storable k, Storable v) => BDB -> k -> v -> IO Bool
putdup = putHelper c_tcbdbputdup unTCBDB
putlist :: (Storable k, Storable v, Sequence q) =>
BDB -> k -> q v -> IO Bool
putlist bdb key vals =
withForeignPtr (unTCBDB bdb) $ \bdb' ->
withList vals $ \tcls ->
withPtrLen key $ \(kbuf, ksize) ->
alloca $ \sizbuf -> do
num <- c_tclistnum tcls
putlist' bdb' (kbuf, ksize) tcls sizbuf num
where
putlist' bdb' (kbuf, ksize) tcls sizbuf = put' 0
where
put' n num
| n < num = do vbuf <- c_tclistval tcls n sizbuf
vsize <- fromIntegral `fmap` peek sizbuf
res <- c_tcbdbputdup bdb' kbuf ksize vbuf vsize
if res then put' (n+1) num else return False
| otherwise = return True
out :: (Storable k) => BDB -> k -> IO Bool
out = outHelper c_tcbdbout unTCBDB
outlist :: (Storable k) => BDB -> k -> IO Bool
outlist = outHelper c_tcbdbout3 unTCBDB
get :: (Storable k, Storable v) => BDB -> k -> IO (Maybe v)
get = getHelper c_tcbdbget unTCBDB
getlist :: (Storable k, Storable v, Sequence q) => BDB -> k -> IO (q v)
getlist bdb key =
withForeignPtr (unTCBDB bdb) $ \bdb' ->
withPtrLen key $ \(kbuf, ksize) -> do
ptr <- c_tcbdbget4 bdb' kbuf ksize
if ptr == nullPtr
then empty
else peekList' ptr
vnum :: (Storable k) => BDB -> k -> IO (Maybe Int)
vnum bdb key =
withForeignPtr (unTCBDB bdb) $ \bdb' ->
withPtrLen key $ \(kbuf, ksize) -> do
res <- c_tcbdbvnum bdb' kbuf ksize
return $ if res == 0
then Nothing
else Just $ fromIntegral res
vsiz :: (Storable k) => BDB -> k -> IO (Maybe Int)
vsiz = vsizHelper c_tcbdbvsiz unTCBDB
range :: (Storable k, Sequence q) =>
BDB
-> Maybe k
-> Bool
-> Maybe k
-> Bool
-> Int
-> IO (q k)
range bdb bkey binc ekey einc maxn =
withForeignPtr (unTCBDB bdb) $ \bdb' ->
withPtrLen' bkey $ \(bkbuf, bksiz) ->
withPtrLen' ekey $ \(ekbuf, eksiz) ->
c_tcbdbrange bdb' bkbuf bksiz binc ekbuf eksiz einc
(fromIntegral maxn) >>= peekList'
where
withPtrLen' (Just key) action = withPtrLen key action
withPtrLen' Nothing action = action (nullPtr, 0)
fwmkeys :: (Storable k1, Storable k2, Sequence q) =>
BDB
-> k1
-> Int
-> IO (q k2)
fwmkeys = fwmHelper c_tcbdbfwmkeys unTCBDB
addint :: (Storable k) =>
BDB
-> k
-> Int
-> IO (Maybe Int)
addint = addHelper c_tcbdbaddint unTCBDB fromIntegral fromIntegral (== cINT_MIN)
adddouble :: (Storable k) =>
BDB
-> k
-> Double
-> IO (Maybe Double)
adddouble = addHelper c_tcbdbadddouble unTCBDB realToFrac realToFrac isNaN
sync :: BDB -> IO Bool
sync bdb = withForeignPtr (unTCBDB bdb) c_tcbdbsync
optimize :: BDB
-> Int32
-> Int32
-> Int64
-> Int8
-> Int8
-> [TuningOption]
-> IO Bool
optimize bdb lmemb nmemb bnum apow fpow opts =
withForeignPtr (unTCBDB bdb) $ \bdb' ->
c_tcbdboptimize bdb' lmemb nmemb bnum apow fpow
(combineTuningOption opts)
vanish :: BDB -> IO Bool
vanish bdb = withForeignPtr (unTCBDB bdb) c_tcbdbvanish
copy :: BDB -> String -> IO Bool
copy = copyHelper c_tcbdbcopy unTCBDB
tranbegin :: BDB -> IO Bool
tranbegin bdb = withForeignPtr (unTCBDB bdb) c_tcbdbtranbegin
trancommit :: BDB -> IO Bool
trancommit bdb = withForeignPtr (unTCBDB bdb) c_tcbdbtrancommit
tranabort :: BDB -> IO Bool
tranabort bdb = withForeignPtr (unTCBDB bdb) c_tcbdbtranabort
path :: BDB -> IO (Maybe String)
path = pathHelper c_tcbdbpath unTCBDB
rnum :: BDB -> IO Word64
rnum bdb = withForeignPtr (unTCBDB bdb) c_tcbdbrnum
fsiz :: BDB -> IO Word64
fsiz bdb = withForeignPtr (unTCBDB bdb) c_tcbdbfsiz