gdritter repos tansu-berkeleydb / f9d1a2d
Updated bdb backend for new tansu backend API Getty Ritter 8 years ago
1 changed file(s) with 22 addition(s) and 10 deletion(s). Collapse all Expand all
11 module Database.Tansu.Backend.BerkeleyDb
22 (withBerkeleyDb) where
33
4 import Control.Exception (catch)
45 import Database.Berkeley.Db
56 import Data.ByteString (ByteString)
67 import System.Directory (createDirectoryIfMissing)
78
8 import Database.Tansu.Internal (TansuDb(..))
9 import Database.Tansu.Internal
910
10 bdbSet :: Db -> ByteString -> ByteString -> IO ()
11 catchIO :: IO a -> IO (Either TansuError a)
12 catchIO mote = fmap return mote `catch` go
13 where go :: DbException -> IO (Either TansuError a)
14 go = return . Left . OtherError . show
15
16 bdbSet :: Db -> ByteString -> ByteString -> IO (Either TansuError ())
1117 bdbSet db key val =
12 db_put [] db Nothing key val
18 catchIO $ db_put [] db Nothing key val
1319
14 bdbGet :: Db -> ByteString -> IO (Maybe ByteString)
15 bdbGet db key =
16 db_get [] db Nothing key
20 bdbGet :: Db -> ByteString -> IO (Either TansuError ByteString)
21 bdbGet db key = do
22 rs <- catchIO $ db_get [] db Nothing key
23 case rs of
24 Right Nothing -> return (Left (KeyNotFound key))
25 Right (Just x) -> return (return x)
26 Left err -> return (Left err)
1727
18 bdbDel :: Db -> ByteString -> IO ()
28 bdbDel :: Db -> ByteString -> IO (Either TansuError ())
1929 bdbDel db key =
20 db_del [] db Nothing key
30 catchIO $ db_del [] db Nothing key
2131
2232 -- | Open or create a database at the supplied path
23 -- using the BerkeleyDB library.
24 withBerkeleyDb :: FilePath -> (TansuDb -> IO a) -> IO a
33 -- using the BerkeleyDB library. Right now, this uses
34 -- a consistent set of options, but probably should
35 -- become configurable at some point.
36 withBerkeleyDb :: FilePath -> (TansuDb k v -> IO a) -> IO a
2537 withBerkeleyDb path comp = do
2638 createDirectoryIfMissing True path
2739 env <- dbEnv_create []