Copyright | (c) 2021 Francisco Vallarino |
---|---|
License | BSD-3-Clause (see the LICENSE file) |
Maintainer | fjvallarino@gmail.com |
Stability | experimental |
Portability | non-portable |
Safe Haskell | None |
Language | Haskell2010 |
Types used by the library. Mainly re exports the types generated by c2hs in the FFI module, while also adding some types used by the high level interface.
Synopsis
- data MdbxEnv
- data MdbxTxn
- type MdbxDbi = CUInt
- data MdbxVal = MdbxVal {}
- type MdbxEnvMode = CUInt
- data MdbxEnvFlags
- data MdbxTxnFlags
- data MdbxDbFlags
- data MdbxPutFlags
- data MdbxCursorOp
- class MdbxItem i where
Re-exported from FFI
Environment object, needed for all the operations.
Transaction instance. Needed for all operations with data, even reading.
Binary blob representing a key or value in the database.
Instances
Eq MdbxVal Source # | |
Show MdbxVal Source # | |
Storable MdbxVal Source # | |
type MdbxEnvMode = CUInt Source #
UNIX permissions to set on created files. Zero value means to open existing, but do not create.
data MdbxEnvFlags Source #
Flags for opening and environment.
Instances
data MdbxTxnFlags Source #
Flags for a transaction.
Instances
data MdbxDbFlags Source #
Flags for a database.
MdbxDbDefaults | |
MdbxReversekey | |
MdbxDupsort | |
MdbxIntegerkey | |
MdbxDupfixed | |
MdbxIntegerdup | |
MdbxReversedup | |
MdbxCreate | |
MdbxDbAccede |
Instances
data MdbxPutFlags Source #
Flags for all data related operations.
MdbxUpsert | |
MdbxNooverwrite | |
MdbxNodupdata | |
MdbxCurrent | |
MdbxAlldups | |
MdbxReserve | |
MdbxAppend | |
MdbxAppenddup | |
MdbxMultiple |
Instances
data MdbxCursorOp Source #
Flags for cursor operations.
Instances
High level interface
class MdbxItem i where Source #
Converts an instance to/from the representation needed by libmdbx. This type is used for both keys and values.
Only the Text instance is provided, since it is commonly used as the key when storing/retrieving a value.
For your own types, in general, you will want to use a serialization library such as store, cereal, etc, and apply the newtype deriving via trick.
For store (this can be found in the example application), the instance can be defined as:
newtype MdbxItemStore a = MdbxItemStore { unwrapStore :: a } instance Store a => MdbxItem (MdbxItemStore a) where fromMdbxVal item = MdbxItemStore $ fromMdbxStore item toMdbxVal item = withMdbxStore (unwrapStore item) fromMdbxStore :: Store v => MdbxVal -> IO v fromMdbxStore (MdbxVal size ptr) = do bs <- unsafePackCStringLen (castPtr ptr, fromIntegral size) decodeIO bs withMdbxStore :: Store v => v -> (MdbxVal -> IO a) -> IO a withMdbxStore val fn = unsafeUseAsCStringLen bsV $ (ptrV, sizeV) -> do let mval = MdbxVal (fromIntegral sizeV) (castPtr ptrV) fn mval where bsV = encode val
This code can be adaptad to other serialization libraries. It is not provided as part of libmdbx-hs itself to avoid forcing dependencies.
Then, to derive the instance for your owwn type:
data User = User { _username :: Text, _password :: Text } deriving (Eq, Show, Generic, Store) deriving via (MdbxItemStore User) instance MdbxItem User
Note: if you plan on using a custom type as the key, be careful if it contains Text or Bytestring instances, since these types have a length field which is, in general, before the data. This can issues when using cursors, since those depend on key ordering and the length field will make shorter instances lower than longer ones, even if the content indicates the opposite. In general, it is simpler to use Text as the key.
fromMdbxVal :: MdbxVal -> IO i Source #
Converts a block of memory provided by libmdbx to a user data type. There are no guarantees provided by the library that the block of memory matches the expected type, and a crash can happen if not careful.