packages feed

lmdb-simple 0.2.0.0 → 0.3.0.0

raw patch · 7 files changed

+109/−104 lines, 7 filesdep +serialisedep −binaryPVP ok

version bump matches the API change (PVP)

Dependencies added: serialise

Dependencies removed: binary

API changes (from Hackage documentation)

- Database.LMDB.Simple.View: getView :: Database k v -> IO (View k v)
+ Database.LMDB.Simple.View: newView :: Database k v -> IO (View k v)
- Database.LMDB.Simple: get :: (Binary k, Binary v) => Database k v -> k -> Transaction mode (Maybe v)
+ Database.LMDB.Simple: get :: (Serialise k, Serialise v) => Database k v -> k -> Transaction mode (Maybe v)
- Database.LMDB.Simple: put :: (Binary k, Binary v) => Database k v -> k -> Maybe v -> Transaction ReadWrite ()
+ Database.LMDB.Simple: put :: (Serialise k, Serialise v) => Database k v -> k -> Maybe v -> Transaction ReadWrite ()
- Database.LMDB.Simple.DBRef: modifyDBRef :: Binary a => DBRef ReadWrite a -> (Maybe a -> (Maybe a, b)) -> IO b
+ Database.LMDB.Simple.DBRef: modifyDBRef :: Serialise a => DBRef ReadWrite a -> (Maybe a -> (Maybe a, b)) -> IO b
- Database.LMDB.Simple.DBRef: modifyDBRef_ :: Binary a => DBRef ReadWrite a -> (Maybe a -> Maybe a) -> IO ()
+ Database.LMDB.Simple.DBRef: modifyDBRef_ :: Serialise a => DBRef ReadWrite a -> (Maybe a -> Maybe a) -> IO ()
- Database.LMDB.Simple.DBRef: newDBRef :: Binary k => Environment mode -> Database k a -> k -> IO (DBRef mode a)
+ Database.LMDB.Simple.DBRef: newDBRef :: Serialise k => Environment mode -> Database k a -> k -> IO (DBRef mode a)
- Database.LMDB.Simple.DBRef: readDBRef :: Binary a => DBRef mode a -> IO (Maybe a)
+ Database.LMDB.Simple.DBRef: readDBRef :: Serialise a => DBRef mode a -> IO (Maybe a)
- Database.LMDB.Simple.DBRef: writeDBRef :: Binary a => DBRef ReadWrite a -> Maybe a -> IO ()
+ Database.LMDB.Simple.DBRef: writeDBRef :: Serialise a => DBRef ReadWrite a -> Maybe a -> IO ()
- Database.LMDB.Simple.Extra: adjust :: (Binary k, Binary v) => (v -> v) -> k -> Database k v -> Transaction ReadWrite Bool
+ Database.LMDB.Simple.Extra: adjust :: (Serialise k, Serialise v) => (v -> v) -> k -> Database k v -> Transaction ReadWrite Bool
- Database.LMDB.Simple.Extra: adjustWithKey :: (Binary k, Binary v) => (k -> v -> v) -> k -> Database k v -> Transaction ReadWrite Bool
+ Database.LMDB.Simple.Extra: adjustWithKey :: (Serialise k, Serialise v) => (k -> v -> v) -> k -> Database k v -> Transaction ReadWrite Bool
- Database.LMDB.Simple.Extra: alter :: (Binary k, Binary v) => (Maybe v -> Maybe v) -> k -> Database k v -> Transaction ReadWrite ()
+ Database.LMDB.Simple.Extra: alter :: (Serialise k, Serialise v) => (Maybe v -> Maybe v) -> k -> Database k v -> Transaction ReadWrite ()
- Database.LMDB.Simple.Extra: delete :: Binary k => k -> Database k v -> Transaction ReadWrite Bool
+ Database.LMDB.Simple.Extra: delete :: Serialise k => k -> Database k v -> Transaction ReadWrite Bool
- Database.LMDB.Simple.Extra: elems :: Binary v => Database k v -> Transaction mode [v]
+ Database.LMDB.Simple.Extra: elems :: Serialise v => Database k v -> Transaction mode [v]
- Database.LMDB.Simple.Extra: findWithDefault :: (Binary k, Binary v) => v -> k -> Database k v -> Transaction mode v
+ Database.LMDB.Simple.Extra: findWithDefault :: (Serialise k, Serialise v) => v -> k -> Database k v -> Transaction mode v
- Database.LMDB.Simple.Extra: foldDatabaseWithKey :: (Monoid m, Binary k, Binary v) => (k -> v -> m) -> Database k v -> Transaction mode m
+ Database.LMDB.Simple.Extra: foldDatabaseWithKey :: (Monoid m, Serialise k, Serialise v) => (k -> v -> m) -> Database k v -> Transaction mode m
- Database.LMDB.Simple.Extra: foldl :: Binary v => (a -> v -> a) -> a -> Database k v -> Transaction mode a
+ Database.LMDB.Simple.Extra: foldl :: Serialise v => (a -> v -> a) -> a -> Database k v -> Transaction mode a
- Database.LMDB.Simple.Extra: foldlWithKey :: (Binary k, Binary v) => (a -> k -> v -> a) -> a -> Database k v -> Transaction mode a
+ Database.LMDB.Simple.Extra: foldlWithKey :: (Serialise k, Serialise v) => (a -> k -> v -> a) -> a -> Database k v -> Transaction mode a
- Database.LMDB.Simple.Extra: foldr :: Binary v => (v -> b -> b) -> b -> Database k v -> Transaction mode b
+ Database.LMDB.Simple.Extra: foldr :: Serialise v => (v -> b -> b) -> b -> Database k v -> Transaction mode b
- Database.LMDB.Simple.Extra: foldrWithKey :: (Binary k, Binary v) => (k -> v -> b -> b) -> b -> Database k v -> Transaction mode b
+ Database.LMDB.Simple.Extra: foldrWithKey :: (Serialise k, Serialise v) => (k -> v -> b -> b) -> b -> Database k v -> Transaction mode b
- Database.LMDB.Simple.Extra: insert :: (Binary k, Binary v) => k -> v -> Database k v -> Transaction ReadWrite ()
+ Database.LMDB.Simple.Extra: insert :: (Serialise k, Serialise v) => k -> v -> Database k v -> Transaction ReadWrite ()
- Database.LMDB.Simple.Extra: insertLookupWithKey :: (Binary k, Binary v) => (k -> v -> v -> v) -> k -> v -> Database k v -> Transaction ReadWrite (Maybe v)
+ Database.LMDB.Simple.Extra: insertLookupWithKey :: (Serialise k, Serialise v) => (k -> v -> v -> v) -> k -> v -> Database k v -> Transaction ReadWrite (Maybe v)
- Database.LMDB.Simple.Extra: insertWith :: (Binary k, Binary v) => (v -> v -> v) -> k -> v -> Database k v -> Transaction ReadWrite ()
+ Database.LMDB.Simple.Extra: insertWith :: (Serialise k, Serialise v) => (v -> v -> v) -> k -> v -> Database k v -> Transaction ReadWrite ()
- Database.LMDB.Simple.Extra: insertWithKey :: (Binary k, Binary v) => (k -> v -> v -> v) -> k -> v -> Database k v -> Transaction ReadWrite ()
+ Database.LMDB.Simple.Extra: insertWithKey :: (Serialise k, Serialise v) => (k -> v -> v -> v) -> k -> v -> Database k v -> Transaction ReadWrite ()
- Database.LMDB.Simple.Extra: keys :: Binary k => Database k v -> Transaction mode [k]
+ Database.LMDB.Simple.Extra: keys :: Serialise k => Database k v -> Transaction mode [k]
- Database.LMDB.Simple.Extra: lookup :: (Binary k, Binary v) => k -> Database k v -> Transaction mode (Maybe v)
+ Database.LMDB.Simple.Extra: lookup :: (Serialise k, Serialise v) => k -> Database k v -> Transaction mode (Maybe v)
- Database.LMDB.Simple.Extra: member :: Binary k => k -> Database k v -> Transaction mode Bool
+ Database.LMDB.Simple.Extra: member :: Serialise k => k -> Database k v -> Transaction mode Bool
- Database.LMDB.Simple.Extra: notMember :: Binary k => k -> Database k v -> Transaction mode Bool
+ Database.LMDB.Simple.Extra: notMember :: Serialise k => k -> Database k v -> Transaction mode Bool
- Database.LMDB.Simple.Extra: toList :: (Binary k, Binary v) => Database k v -> Transaction mode [(k, v)]
+ Database.LMDB.Simple.Extra: toList :: (Serialise k, Serialise v) => Database k v -> Transaction mode [(k, v)]
- Database.LMDB.Simple.Extra: update :: (Binary k, Binary v) => (v -> Maybe v) -> k -> Database k v -> Transaction ReadWrite Bool
+ Database.LMDB.Simple.Extra: update :: (Serialise k, Serialise v) => (v -> Maybe v) -> k -> Database k v -> Transaction ReadWrite Bool
- Database.LMDB.Simple.Extra: updateLookupWithKey :: (Binary k, Binary v) => (k -> v -> Maybe v) -> k -> Database k v -> Transaction ReadWrite (Maybe v)
+ Database.LMDB.Simple.Extra: updateLookupWithKey :: (Serialise k, Serialise v) => (k -> v -> Maybe v) -> k -> Database k v -> Transaction ReadWrite (Maybe v)
- Database.LMDB.Simple.Extra: updateWithKey :: (Binary k, Binary v) => (k -> v -> Maybe v) -> k -> Database k v -> Transaction ReadWrite Bool
+ Database.LMDB.Simple.Extra: updateWithKey :: (Serialise k, Serialise v) => (k -> v -> Maybe v) -> k -> Database k v -> Transaction ReadWrite Bool
- Database.LMDB.Simple.View: (!) :: (Binary k, Binary v) => View k v -> k -> v
+ Database.LMDB.Simple.View: (!) :: (Serialise k, Serialise v) => View k v -> k -> v
- Database.LMDB.Simple.View: (!?) :: (Binary k, Binary v) => View k v -> k -> Maybe v
+ Database.LMDB.Simple.View: (!?) :: (Serialise k, Serialise v) => View k v -> k -> Maybe v
- Database.LMDB.Simple.View: elems :: Binary v => View k v -> [v]
+ Database.LMDB.Simple.View: elems :: Serialise v => View k v -> [v]
- Database.LMDB.Simple.View: findWithDefault :: (Binary k, Binary v) => v -> k -> View k v -> v
+ Database.LMDB.Simple.View: findWithDefault :: (Serialise k, Serialise v) => v -> k -> View k v -> v
- Database.LMDB.Simple.View: foldViewWithKey :: (Monoid m, Binary k, Binary v) => (k -> v -> m) -> View k v -> m
+ Database.LMDB.Simple.View: foldViewWithKey :: (Monoid m, Serialise k, Serialise v) => (k -> v -> m) -> View k v -> m
- Database.LMDB.Simple.View: foldl :: Binary v => (a -> v -> a) -> a -> View k v -> a
+ Database.LMDB.Simple.View: foldl :: Serialise v => (a -> v -> a) -> a -> View k v -> a
- Database.LMDB.Simple.View: foldlWithKey :: (Binary k, Binary v) => (a -> k -> v -> a) -> a -> View k v -> a
+ Database.LMDB.Simple.View: foldlWithKey :: (Serialise k, Serialise v) => (a -> k -> v -> a) -> a -> View k v -> a
- Database.LMDB.Simple.View: foldr :: Binary v => (v -> b -> b) -> b -> View k v -> b
+ Database.LMDB.Simple.View: foldr :: Serialise v => (v -> b -> b) -> b -> View k v -> b
- Database.LMDB.Simple.View: foldrWithKey :: (Binary k, Binary v) => (k -> v -> b -> b) -> b -> View k v -> b
+ Database.LMDB.Simple.View: foldrWithKey :: (Serialise k, Serialise v) => (k -> v -> b -> b) -> b -> View k v -> b
- Database.LMDB.Simple.View: keys :: Binary k => View k v -> [k]
+ Database.LMDB.Simple.View: keys :: Serialise k => View k v -> [k]
- Database.LMDB.Simple.View: lookup :: (Binary k, Binary v) => k -> View k v -> Maybe v
+ Database.LMDB.Simple.View: lookup :: (Serialise k, Serialise v) => k -> View k v -> Maybe v
- Database.LMDB.Simple.View: member :: Binary k => k -> View k v -> Bool
+ Database.LMDB.Simple.View: member :: Serialise k => k -> View k v -> Bool
- Database.LMDB.Simple.View: notMember :: Binary k => k -> View k v -> Bool
+ Database.LMDB.Simple.View: notMember :: Serialise k => k -> View k v -> Bool
- Database.LMDB.Simple.View: toList :: (Binary k, Binary v) => View k v -> [(k, v)]
+ Database.LMDB.Simple.View: toList :: (Serialise k, Serialise v) => View k v -> [(k, v)]

Files

lmdb-simple.cabal view
@@ -1,6 +1,6 @@  name:                lmdb-simple-version:             0.2.0.0+version:             0.3.0.0  synopsis:            Simple API for LMDB @@ -41,8 +41,8 @@   other-modules:       Database.LMDB.Simple.Internal    build-depends:       base >= 4.7 && < 5-                     , binary >= 0.8 && < 0.9                      , bytestring >= 0.10 && < 0.11+                     , serialise >= 0.1 && < 0.2                      , lmdb >= 0.2 && < 0.3   ghc-options:         -Wall -Wno-name-shadowing -Wno-unused-do-bind   default-language:    Haskell2010
src/Database/LMDB/Simple.hs view
@@ -98,10 +98,6 @@   , void   ) -import Data.Binary-  ( Binary-  )- import Database.LMDB.Raw   ( LMDB_Error (LMDB_Error, e_code)   , MDB_EnvFlag (MDB_NOSUBDIR, MDB_RDONLY)@@ -128,6 +124,7 @@   , Environment (Env)   , Transaction (Txn)   , Database (Db)+  , Serialise   , isReadOnlyEnvironment   , isReadOnlyTransaction   , isReadWriteTransaction@@ -324,12 +321,13 @@  -- | Lookup a key in a database and return the corresponding value, or return -- 'Nothing' if the key does not exist in the database.-get :: (Binary k, Binary v) => Database k v -> k -> Transaction mode (Maybe v)+get :: (Serialise k, Serialise v)+    => Database k v -> k -> Transaction mode (Maybe v) get = Internal.get  -- | Insert the given key/value pair into a database, or delete the key from -- the database if 'Nothing' is given for the value.-put :: (Binary k, Binary v)+put :: (Serialise k, Serialise v)     => Database k v -> k -> Maybe v -> Transaction ReadWrite () put db key = maybe (void $ Internal.delete db key) (Internal.put db key) 
src/Database/LMDB/Simple/DBRef.hs view
@@ -21,11 +21,6 @@   ( MonadIO (liftIO)   ) -import Data.Binary-  ( Binary-  , encode-  )- import Data.ByteString   ( ByteString   )@@ -56,6 +51,8 @@   , Database (..)   , ReadWrite   , ReadOnly+  , Serialise+  , serialise   , marshalIn   , marshalOut   , defaultWriteFlags@@ -77,44 +74,45 @@  -- | Create a new 'DBRef' for the given key and database within the given -- environment.-newDBRef :: Binary k+newDBRef :: Serialise k          => Environment mode -> Database k a -> k -> IO (DBRef mode a)-newDBRef env (Db _ dbi) = return . Ref env dbi . toStrict . encode+newDBRef env (Db _ dbi) = return . Ref env dbi . toStrict . serialise  withVal :: ByteString -> (MDB_val -> IO a) -> IO a withVal bs f = unsafeUseAsCStringLen bs $ \(ptr, len) ->   f $ MDB_val (fromIntegral len) (castPtr ptr)  -- | Read the current value of a 'DBRef'.-readDBRef :: Binary a => DBRef mode a -> IO (Maybe a)+readDBRef :: Serialise a => DBRef mode a -> IO (Maybe a) readDBRef ref@(Ref env dbi key) = transaction env (tx ref) -  where tx :: Binary a => DBRef mode a -> Transaction ReadOnly (Maybe a)+  where tx :: Serialise a => DBRef mode a -> Transaction ReadOnly (Maybe a)         tx _ = Txn $ \txn -> withVal key $ mdb_get' txn dbi >=>           maybe (return Nothing) (liftIO . fmap Just . marshalIn)  -- | Write a new value into a 'DBRef'.-writeDBRef :: Binary a => DBRef ReadWrite a -> Maybe a -> IO ()+writeDBRef :: Serialise a => DBRef ReadWrite a -> Maybe a -> IO () writeDBRef (Ref env dbi key) = transaction env . maybe delKey putKey    where delKey :: Transaction ReadWrite ()         delKey = Txn $ \txn -> withVal key $ \kval ->           void $ mdb_del' txn dbi kval Nothing -        putKey :: Binary a => a -> Transaction ReadWrite ()+        putKey :: Serialise a => a -> Transaction ReadWrite ()         putKey value = Txn $ \txn -> withVal key $ \kval ->           marshalOut value $ \vval ->  -- FIXME: use mdb_reserve'           void $ mdb_put' defaultWriteFlags txn dbi kval vval  -- | Atomically mutate the contents of a 'DBRef'.-modifyDBRef_ :: Binary a => DBRef ReadWrite a -> (Maybe a -> Maybe a) -> IO ()+modifyDBRef_ :: Serialise a => DBRef ReadWrite a -> (Maybe a -> Maybe a) -> IO () modifyDBRef_ ref f = modifyDBRef ref $ \x -> (f x, ())  -- | Atomically mutate the contents of a 'DBRef' and return a value.-modifyDBRef :: Binary a => DBRef ReadWrite a -> (Maybe a -> (Maybe a, b)) -> IO b+modifyDBRef :: Serialise a+            => DBRef ReadWrite a -> (Maybe a -> (Maybe a, b)) -> IO b modifyDBRef (Ref env dbi key) = transaction env . tx -  where tx :: Binary a => (Maybe a -> (Maybe a, b)) -> Transaction ReadWrite b+  where tx :: Serialise a => (Maybe a -> (Maybe a, b)) -> Transaction ReadWrite b         tx f = Txn $ \txn -> withVal key $ \kval -> mdb_get' txn dbi kval >>=           maybe (return Nothing) (fmap Just . marshalIn) >>= \x ->           let (x', r) = f x in maybe (mdb_del' txn dbi kval Nothing)
src/Database/LMDB/Simple/Extra.hs view
@@ -52,10 +52,6 @@   ( void   ) -import Data.Binary-  ( Binary-  )- import Data.Maybe   ( fromMaybe   , isJust@@ -77,6 +73,7 @@   ( ReadWrite   , Transaction (Txn)   , Database (Db)+  , Serialise   , forEachForward   , forEachReverse   , marshalOut@@ -97,12 +94,13 @@ -- -- The function will return the corresponding value as @('Just' value)@, or -- 'Nothing' if the key isn't in the database.-lookup :: (Binary k, Binary v) => k -> Database k v -> Transaction mode (Maybe v)+lookup :: (Serialise k, Serialise v)+       => k -> Database k v -> Transaction mode (Maybe v) lookup = flip Internal.get  -- | The expression @('findWithDefault' def k db)@ returns the value at key -- @k@ or returns default value @def@ when the key is not in the database.-findWithDefault :: (Binary k, Binary v)+findWithDefault :: (Serialise k, Serialise v)                 => v -> k -> Database k v -> Transaction mode v findWithDefault def key db = fromMaybe def <$> lookup key db @@ -119,26 +117,26 @@   return (fromIntegral $ ms_entries stat)  -- | Is the key a member of the database? See also 'notMember'.-member :: Binary k => k -> Database k v -> Transaction mode Bool+member :: Serialise k => k -> Database k v -> Transaction mode Bool member key db = isJust <$> Internal.get' db key  -- | Is the key not a member of the database? See also 'member'.-notMember :: Binary k => k -> Database k v -> Transaction mode Bool+notMember :: Serialise k => k -> Database k v -> Transaction mode Bool notMember key db = not <$> member key db  -- | Insert a new key and value in the database. If the key is already present -- in the database, the associated value is replaced with the supplied -- value. 'insert' is equivalent to @'insertWith' 'const'@.-insert :: (Binary k, Binary v) => k -> v -> Database k v-       -> Transaction ReadWrite ()+insert :: (Serialise k, Serialise v)+       => k -> v -> Database k v -> Transaction ReadWrite () insert key value db = Internal.put db key value  -- | Insert with a function, combining new value and old value. @'insertWith' -- f key value db@ will insert the pair @(key, value)@ into @db@ if key does -- not exist in the database. If the key does exist, the function will insert -- the pair @(key, f new_value old_value)@.-insertWith :: (Binary k, Binary v) => (v -> v -> v) -> k -> v -> Database k v-           -> Transaction ReadWrite ()+insertWith :: (Serialise k, Serialise v)+           => (v -> v -> v) -> k -> v -> Database k v -> Transaction ReadWrite () insertWith f = insertWithKey (const f)  -- | Insert with a function, combining key, new value and old@@ -146,15 +144,17 @@ -- into @db@ if key does not exist in the database. If the key does exist, the -- function will insert the pair @(key, f key new_value old_value)@. Note that -- the key passed to @f@ is the same key passed to 'insertWithKey'.-insertWithKey :: (Binary k, Binary v) => (k -> v -> v -> v) -> k -> v-              -> Database k v -> Transaction ReadWrite ()+insertWithKey :: (Serialise k, Serialise v)+              => (k -> v -> v -> v) -> k -> v -> Database k v+              -> Transaction ReadWrite () insertWithKey f key value = void . insertLookupWithKey f key value  -- | Combines insert operation with old value retrieval. The monadic action -- @('insertLookupWithKey' f k x db)@ returns the same value as @('lookup' k -- db)@ but has the same effect as @('insertWithKey' f k x db)@.-insertLookupWithKey :: (Binary k, Binary v) => (k -> v -> v -> v) -> k -> v-                    -> Database k v -> Transaction ReadWrite (Maybe v)+insertLookupWithKey :: (Serialise k, Serialise v)+                    => (k -> v -> v -> v) -> k -> v -> Database k v+                    -> Transaction ReadWrite (Maybe v) insertLookupWithKey f key value (Db _ dbi) = Txn $ \txn ->   withCursor txn dbi $ \cursor -> marshalOut key $ \kval ->   with kval $ \kptr -> alloca $ \vptr -> do@@ -166,17 +166,17 @@       else do cursorPut cursor defaultWriteFlags kval value               return  Nothing -  where cursorPut :: Binary v => MDB_cursor' -> MDB_WriteFlags -> MDB_val -> v-                  -> IO Bool+  where cursorPut :: Serialise v+                  => MDB_cursor' -> MDB_WriteFlags -> MDB_val -> v -> IO Bool         cursorPut cursor writeFlags kval value = marshalOut value $ \vval ->           mdb_cursor_put' writeFlags cursor kval vval  -- | Return all elements of the database in the order of their keys.-elems :: Binary v => Database k v -> Transaction mode [v]+elems :: Serialise v => Database k v -> Transaction mode [v] elems = foldr (:) []  -- | Return all keys of the database in the order they are stored on disk.-keys :: Binary k => Database k v -> Transaction mode [k]+keys :: Serialise k => Database k v -> Transaction mode [k] keys (Db _ dbi) = Txn $ \txn ->   alloca $ \kptr ->   forEachForward txn dbi kptr nullPtr [] $ \rest ->@@ -184,12 +184,12 @@  -- | Convert the database to a list of key/value pairs. Note that this will -- make a copy of the entire database in memory.-toList :: (Binary k, Binary v) => Database k v -> Transaction mode [(k, v)]+toList :: (Serialise k, Serialise v) => Database k v -> Transaction mode [(k, v)] toList = foldrWithKey (\k v -> ((k, v) :)) []  -- | Fold the values in the database using the given right-associative binary -- operator.-foldr :: Binary v => (v -> b -> b) -> b -> Database k v -> Transaction mode b+foldr :: Serialise v => (v -> b -> b) -> b -> Database k v -> Transaction mode b foldr f z (Db _ dbi) = Txn $ \txn ->   alloca $ \vptr ->   forEachForward txn dbi nullPtr vptr z $ \rest ->@@ -197,7 +197,7 @@  -- | Fold the keys and values in the database using the given -- right-associative binary operator.-foldrWithKey :: (Binary k, Binary v)+foldrWithKey :: (Serialise k, Serialise v)              => (k -> v -> b -> b) -> b -> Database k v -> Transaction mode b foldrWithKey f z (Db _ dbi) = Txn $ \txn ->   alloca $ \kptr ->@@ -207,7 +207,7 @@  -- | Fold the values in the database using the given left-associative binary -- operator.-foldl :: Binary v => (a -> v -> a) -> a -> Database k v -> Transaction mode a+foldl :: Serialise v => (a -> v -> a) -> a -> Database k v -> Transaction mode a foldl f z (Db _ dbi) = Txn $ \txn ->   alloca $ \vptr ->   forEachReverse txn dbi nullPtr vptr z $ \rest ->@@ -215,7 +215,7 @@  -- | Fold the keys and values in the database using the given left-associative -- binary operator.-foldlWithKey :: (Binary k, Binary v)+foldlWithKey :: (Serialise k, Serialise v)              => (a -> k -> v -> a) -> a -> Database k v -> Transaction mode a foldlWithKey f z (Db _ dbi) = Txn $ \txn ->   alloca $ \kptr ->@@ -224,58 +224,61 @@   (\k v a -> f a k v) <$> peekVal kptr <*> peekVal vptr <*> rest  -- | Fold the keys and values in the database using the given monoid.-foldDatabaseWithKey :: (Monoid m, Binary k, Binary v)+foldDatabaseWithKey :: (Monoid m, Serialise k, Serialise v)                     => (k -> v -> m) -> Database k v -> Transaction mode m foldDatabaseWithKey f = foldrWithKey (\k v a -> f k v `mappend` a) mempty  -- | Delete a key and its value from the database. If the key was not present -- in the database, this returns 'False'; otherwise it returns 'True'.-delete :: Binary k => k -> Database k v -> Transaction ReadWrite Bool+delete :: Serialise k => k -> Database k v -> Transaction ReadWrite Bool delete = flip Internal.delete  -- | Update a value at a specific key with the result of the provided -- function. When the key is not a member of the database, this returns -- 'False'; otherwise it returns 'True'.-adjust :: (Binary k, Binary v) => (v -> v) -> k-       -> Database k v -> Transaction ReadWrite Bool+adjust :: (Serialise k, Serialise v)+       => (v -> v) -> k -> Database k v -> Transaction ReadWrite Bool adjust f = adjustWithKey (const f)  -- | Adjust a value at a specific key. When the key is not a member of the -- database, this returns 'False'; otherwise it returns 'True'.-adjustWithKey :: (Binary k, Binary v) => (k -> v -> v) -> k-              -> Database k v -> Transaction ReadWrite Bool+adjustWithKey :: (Serialise k, Serialise v)+              => (k -> v -> v) -> k -> Database k v -> Transaction ReadWrite Bool adjustWithKey f = updateWithKey (\k v -> Just $ f k v)  -- | The monadic action @('update' f k db)@ updates the value @x@ at @k@ (if -- it is in the database). If @(f x)@ is 'Nothing', the element is deleted. If -- it is @('Just' y)@, the key @k@ is bound to the new value @y@.-update :: (Binary k, Binary v) => (v -> Maybe v) -> k-       -> Database k v -> Transaction ReadWrite Bool+update :: (Serialise k, Serialise v)+       => (v -> Maybe v) -> k -> Database k v -> Transaction ReadWrite Bool update f = updateWithKey (const f)  -- | The monadic action @('updateWithKey' f k db)@ updates the value @x@ at -- @k@ (if it is in the database). If @(f k x)@ is 'Nothing', the element is -- deleted. If it is @('Just' y)@, the key @k@ is bound to the new value @y@.-updateWithKey :: (Binary k, Binary v) => (k -> v -> Maybe v) -> k-              -> Database k v -> Transaction ReadWrite Bool+updateWithKey :: (Serialise k, Serialise v)+              => (k -> v -> Maybe v) -> k -> Database k v+              -> Transaction ReadWrite Bool updateWithKey f key db = isJust <$> updateLookupWithKey f key db  -- | Lookup and update. See also 'updateWithKey'. The function returns changed -- value, if it is updated. Returns the original key value if the database -- entry is deleted.-updateLookupWithKey :: (Binary k, Binary v) => (k -> v -> Maybe v) -> k-                    -> Database k v -> Transaction ReadWrite (Maybe v)+updateLookupWithKey :: (Serialise k, Serialise v)+                    => (k -> v -> Maybe v) -> k -> Database k v+                    -> Transaction ReadWrite (Maybe v) updateLookupWithKey f = alterWithKey (maybe Nothing . f)  -- | The monadic action @('alter' f k db)@ alters the value @x@ at @k@, or -- absence thereof. 'alter' can be used to insert, delete, or update a value -- in a database.-alter :: (Binary k, Binary v) => (Maybe v -> Maybe v) -> k-      -> Database k v -> Transaction ReadWrite ()+alter :: (Serialise k, Serialise v)+      => (Maybe v -> Maybe v) -> k -> Database k v -> Transaction ReadWrite () alter f key db = void $ alterWithKey (const f) key db -alterWithKey :: (Binary k, Binary v) => (k -> Maybe v -> Maybe v) -> k-             -> Database k v -> Transaction ReadWrite (Maybe v)+alterWithKey :: (Serialise k, Serialise v)+             => (k -> Maybe v -> Maybe v) -> k -> Database k v+             -> Transaction ReadWrite (Maybe v) alterWithKey f key (Db _ dbi) = Txn $ \txn ->   withCursor txn dbi $ \cursor -> marshalOut key $ \kval ->   with kval $ \kptr -> alloca $ \vptr -> do
src/Database/LMDB/Simple/Internal.hs view
@@ -10,9 +10,11 @@   , Environment (..)   , Transaction (..)   , Database (..)+  , Serialise   , isReadOnlyEnvironment   , isReadOnlyTransaction   , isReadWriteTransaction+  , serialise   , marshalOut   , marshalIn   , peekVal@@ -27,6 +29,12 @@   , delete   ) where +import Codec.Serialise+  ( Serialise+  , serialise+  , deserialise+  )+ import Control.Exception   ( assert   , bracket@@ -41,12 +49,6 @@   ( MonadIO (liftIO)   ) -import Data.Binary-  ( Binary-  , encode-  , decode-  )- import Data.ByteString   ( packCStringLen   )@@ -155,20 +157,22 @@ instance MonadIO (Transaction mode) where   liftIO = Txn . const --- | A database maps arbitrary keys to values. This API uses the 'Binary'--- class to serialize keys and values for LMDB to store on disk.+-- | A database maps arbitrary keys to values. This API uses the 'Serialise'+-- class to encode and decode keys and values for LMDB to store on disk. For+-- details on creating your own instances of this class, see+-- "Codec.Serialise.Tutorial". data Database k v = Db MDB_env MDB_dbi' -peekVal :: Binary v => Ptr MDB_val -> IO v+peekVal :: Serialise v => Ptr MDB_val -> IO v peekVal = peek >=> marshalIn -marshalIn :: Binary v => MDB_val -> IO v+marshalIn :: Serialise v => MDB_val -> IO v marshalIn (MDB_val len ptr) =-  decode . fromStrict <$> packCStringLen (castPtr ptr, fromIntegral len)+  deserialise . fromStrict <$> packCStringLen (castPtr ptr, fromIntegral len) -marshalOut :: Binary v => v -> (MDB_val -> IO a) -> IO a+marshalOut :: Serialise v => v -> (MDB_val -> IO a) -> IO a marshalOut value f =-  unsafeUseAsCStringLen (toStrict $ encode value) $ \(ptr, len) ->+  unsafeUseAsCStringLen (toStrict $ serialise value) $ \(ptr, len) ->   f $ MDB_val (fromIntegral len) (castPtr ptr)  copyLazyBS :: BSL.ByteString -> Ptr Word8 -> Int -> IO ()@@ -205,23 +209,24 @@ defaultWriteFlags = compileWriteFlags [] overwriteFlags    = compileWriteFlags [MDB_CURRENT] -get :: (Binary k, Binary v) => Database k v -> k -> Transaction mode (Maybe v)+get :: (Serialise k, Serialise v)+    => Database k v -> k -> Transaction mode (Maybe v) get db key = get' db key >>=   maybe (return Nothing) (liftIO . fmap Just . marshalIn) -get' :: Binary k => Database k v -> k -> Transaction mode (Maybe MDB_val)+get' :: Serialise k => Database k v -> k -> Transaction mode (Maybe MDB_val) get' (Db _ dbi) key = Txn $ \txn -> marshalOut key $ mdb_get' txn dbi -put :: (Binary k, Binary v)+put :: (Serialise k, Serialise v)     => Database k v -> k -> v -> Transaction ReadWrite () put (Db _ dbi) key value = Txn $ \txn ->   marshalOut key $ \kval -> do-  let bs = encode value+  let bs = serialise value       sz = fromIntegral (BSL.length bs)   MDB_val len ptr <- mdb_reserve' defaultWriteFlags txn dbi kval sz   let len' = fromIntegral len   assert (len' == sz) $ copyLazyBS bs ptr len' -delete :: Binary k => Database k v -> k -> Transaction ReadWrite Bool+delete :: Serialise k => Database k v -> k -> Transaction ReadWrite Bool delete (Db _ dbi) key = Txn $ \txn ->   marshalOut key $ \kval -> mdb_del' txn dbi kval Nothing
src/Database/LMDB/Simple/View.hs view
@@ -14,7 +14,7 @@ module Database.LMDB.Simple.View   ( -- * Creating     View-  , getView+  , newView      -- * Operators   , (!)@@ -60,10 +60,6 @@   ( (>=>)   ) -import Data.Binary-  ( Binary-  )- import Database.LMDB.Raw   ( MDB_txn   , MDB_dbi'@@ -80,6 +76,7 @@  import Database.LMDB.Simple.Internal   ( Database (Db)+  , Serialise   , forEachForward   , forEachReverse   , marshalOut@@ -108,8 +105,8 @@ -- | Create and return a read-only 'View' for the given LMDB database. -- Internally, a read-only transaction is opened and kept alive until the -- 'View' is garbage collected.-getView :: Database k v -> IO (View k v)-getView (Db env dbi) = do+newView :: Database k v -> IO (View k v)+newView (Db env dbi) = do   txn <- mdb_txn_begin env Nothing True   var <- newMVar (txn, dbi)   mkWeakMVar var $ finalize var@@ -136,22 +133,22 @@   return (fromIntegral $ ms_entries stat)  -- | Is the key a member of the view? See also 'notMember'.-member :: Binary k => k -> View k v -> Bool+member :: Serialise k => k -> View k v -> Bool member key view = viewIO view $ \(txn, dbi) ->   marshalOut key $ \kval -> isJust <$> mdb_get' txn dbi kval  -- | Is the key not a member of the view? See also 'member'.-notMember :: Binary k => k -> View k v -> Bool+notMember :: Serialise k => k -> View k v -> Bool notMember key view = not (member key view)  -- | Find the value at a key. Calls 'error' when the element can not be found.-(!) :: (Binary k, Binary v) => View k v -> k -> v+(!) :: (Serialise k, Serialise v) => View k v -> k -> v view ! key = fromMaybe notFoundError $ lookup key view   where notFoundError = error "View.!: given key is not found in the database" infixl 9 !  -- | Find the value at a key. Returns 'Nothing' when the element can not be found.-(!?) :: (Binary k, Binary v) => View k v -> k -> Maybe v+(!?) :: (Serialise k, Serialise v) => View k v -> k -> Maybe v (!?) = flip lookup infixl 9 !? @@ -159,18 +156,18 @@ -- -- The function will return the corresponding value as @('Just' value)@, or -- 'Nothing' if the key isn't in the view.-lookup :: (Binary k, Binary v) => k -> View k v -> Maybe v+lookup :: (Serialise k, Serialise v) => k -> View k v -> Maybe v lookup key view = viewIO view $ \(txn, dbi) -> marshalOut key $   mdb_get' txn dbi >=> maybe (return Nothing) (fmap Just . marshalIn)  -- | The expression @('findWithDefault' def k view)@ returns the value at key -- @k@ or returns default value @def@ when the key is not in the view.-findWithDefault :: (Binary k, Binary v) => v -> k -> View k v -> v+findWithDefault :: (Serialise k, Serialise v) => v -> k -> View k v -> v findWithDefault def key = fromMaybe def . lookup key  -- | Fold the values in the view using the given right-associative binary -- operator, such that @'foldr' f z == 'Prelude.foldr' f z . 'elems'@.-foldr :: Binary v => (v -> b -> b) -> b -> View k v -> b+foldr :: Serialise v => (v -> b -> b) -> b -> View k v -> b foldr f z view = viewIO view $ \(txn, dbi) ->   alloca $ \vptr ->   forEachForward txn dbi nullPtr vptr z $ \rest ->@@ -179,7 +176,7 @@ -- | Fold the keys and values in the view using the given right-associative -- binary operator, such that @'foldrWithKey' f z == 'Prelude.foldr' -- ('uncurry' f) z . 'toList'@.-foldrWithKey :: (Binary k, Binary v)+foldrWithKey :: (Serialise k, Serialise v)              => (k -> v -> b -> b) -> b -> View k v -> b foldrWithKey f z view = viewIO view $ \(txn, dbi) ->   alloca $ \kptr ->@@ -189,7 +186,7 @@  -- | Fold the values in the view using the given left-associative binary -- operator, such that @'foldl' f z == 'Prelude.foldl' f z . 'elems'@.-foldl :: Binary v => (a -> v -> a) -> a -> View k v -> a+foldl :: Serialise v => (a -> v -> a) -> a -> View k v -> a foldl f z view = viewIO view $ \(txn, dbi) ->   alloca $ \vptr ->   forEachReverse txn dbi nullPtr vptr z $ \rest ->@@ -198,7 +195,7 @@ -- | Fold the keys and values in the view using the given left-associative -- binary operator, such that @'foldlWithKey' f z == 'Prelude.foldl' (\\z' -- (kx, x) -> f z' kx x) z . 'toList'@.-foldlWithKey :: (Binary k, Binary v)+foldlWithKey :: (Serialise k, Serialise v)              => (a -> k -> v -> a) -> a -> View k v -> a foldlWithKey f z view = viewIO view $ \(txn, dbi) ->   alloca $ \kptr ->@@ -207,22 +204,22 @@   (\k v a -> f a k v) <$> peekVal kptr <*> peekVal vptr <*> rest  -- | Fold the keys and values in the view using the given monoid.-foldViewWithKey :: (Monoid m, Binary k, Binary v)+foldViewWithKey :: (Monoid m, Serialise k, Serialise v)                 => (k -> v -> m) -> View k v -> m foldViewWithKey f = foldrWithKey (\k v a -> f k v `mappend` a) mempty  -- | Return all elements of the view in the order of their keys.-elems :: Binary v => View k v -> [v]+elems :: Serialise v => View k v -> [v] elems = foldr (:) []  -- | Return all keys of the view in the order they are stored in the -- underlying LMDB database.-keys :: Binary k => View k v -> [k]+keys :: Serialise k => View k v -> [k] keys view = viewIO view $ \(txn, dbi) ->   alloca $ \kptr ->   forEachForward txn dbi kptr nullPtr [] $ \rest ->   (:) <$> peekVal kptr <*> rest  -- | Convert the view to a list of key/value pairs.-toList :: (Binary k, Binary v) => View k v -> [(k, v)]+toList :: (Serialise k, Serialise v) => View k v -> [(k, v)] toList = foldrWithKey (\k v -> ((k, v) :)) []
stack.yaml view
@@ -39,10 +39,14 @@ - . # Dependency packages to be pulled from upstream that are not in the resolver # (e.g., acme-missiles-0.3)-extra-deps: []+extra-deps:+- cborg-0.1.1.0+- serialise-0.1.0.0  # Override default flag values for local packages and extra-deps-flags: {}+flags:+  serialise:+    newtime15: true  # Extra package databases containing global packages extra-package-dbs: []