Code Monkey home page Code Monkey logo

libmdbx-hs's Introduction

libmdbx-hs

A libmdbx wrapper, providing low level access to its API plus a set of high level functions for common operations.

Excerpt from libmdbx's documentation:

libmdbx is an extremely fast, compact, powerful, embedded, transactional key-value database, with permissive license.

Historically, libmdbx is a deeply revised and extended descendant of the amazing Lightning Memory-Mapped Database. libmdbx inherits all benefits from LMDB, but resolves some issues and adds a set of improvements.

Usage

Low level interface

Using libmdbx's low level interface involves the following steps:

  • Opening an environment. This is the equivalent of a database.
  • Opening a database. This is the equivalent of a table.
  • Creating a transaction.
  • Performing CRUD operations, or using a cursor.
  • Committing or aborting the transaction.

See Hackage for the low level interface or libmdbx's documentation for more details on internals.

High level interface

Alternatively you can use the high level interface which, although providing a limited set of operations, takes care of transaction handling and makes the common use cases really simple.

data User = User {
  _username :: !Text,
  _password :: !Text
} deriving (Eq, Show, Generic, Binary)

deriving via (MdbxItemBinary User) instance MdbxItem User

openEnvDbi :: IO MdbxEnv
openEnvDbi = envOpen "./test.db" def [MdbxNosubdir, MdbxCoalesce, MdbxLiforeclaim, MdbxNotls]

userKey :: User -> Text
userKey user = "user-" <> _username user

main :: IO ()
main = bracket openEnvDbi envClose $ \env -> do
  db <- dbiOpen env Nothing []

  putItem env db (userKey user1) user1
  putItem env db (userKey user2) user2

  getItem env db (userKey user2) >>= print @(Maybe User)
  getRange env db (userKey user1) (userKey user2) >>= print @[User]
  where
    user1 = User "john" "secret"
    user2 = User "mark" "password"

For the high level interface see Hackage or the sample application here.

Common

In both scenarios, you will want to check Hackage for information on how to make your data types compatible with libmdbx-hs.

It is recommended that your serializable data types have strict fields, to avoid issues related to lazy IO. The library loads data from pointers that are valid only during a transaction; delaying the operation may cause an invalid read and consequently a crash.

Write operations should always be performed from the same OS thread.

When using the multi-threaded runtime, the MdbxNotls flag is required at environment creation. Failing to include it in the list of flags will result in a random crash.

Dependencies

Source code for libmdbx is included in the repository and built with the rest of the project, to avoid requiring a separate library install.

License

libmdbx is licensed under the The OpenLDAP Public License.

libmdbx-hs is licensed under the BSD-3 License.

libmdbx-hs's People

Contributors

fjvallarino avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

iteratee

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.