libmdbx-0.1.0.3: Bindings for libmdbx, an embedded key/value store
Copyright(c) 2021 Francisco Vallarino
LicenseBSD-3-Clause (see the LICENSE file)
Maintainerfjvallarino@gmail.com
Stabilityexperimental
Portabilitynon-portable
Safe HaskellNone
LanguageHaskell2010

Mdbx.Types

Description

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

Re-exported from FFI

data MdbxEnv Source #

Environment object, needed for all the operations.

Instances

Instances details
Storable MdbxEnv Source # 
Instance details

Defined in Mdbx.FFI

data MdbxTxn Source #

Transaction instance. Needed for all operations with data, even reading.

Instances

Instances details
Storable MdbxTxn Source # 
Instance details

Defined in Mdbx.FFI

type MdbxDbi = CUInt Source #

Database instance. Represents a logical table in the database.

data MdbxVal Source #

Binary blob representing a key or value in the database.

Constructors

MdbxVal 

Fields

Instances

Instances details
Eq MdbxVal Source # 
Instance details

Defined in Mdbx.FFI

Methods

(==) :: MdbxVal -> MdbxVal -> Bool #

(/=) :: MdbxVal -> MdbxVal -> Bool #

Show MdbxVal Source # 
Instance details

Defined in Mdbx.FFI

Storable MdbxVal Source # 
Instance details

Defined in Mdbx.FFI

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.

data MdbxCursorOp Source #

Flags for cursor operations.

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.

The Store 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 causes issues when using cursors, since they 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.

Methods

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.

toMdbxVal :: i -> (MdbxVal -> IO b) -> IO b Source #

Instances

Instances details
MdbxItem Text Source # 
Instance details

Defined in Mdbx.Types

Methods

fromMdbxVal :: MdbxVal -> IO Text Source #

toMdbxVal :: Text -> (MdbxVal -> IO b) -> IO b Source #