Code Monkey home page Code Monkey logo

lmdb-streaming's Introduction

lmdb-streaming

Stream data to or from an LMDB database using Haskell.

First steps

A common practice with LMDB is to create an environment and database handle and simply leave them open for the remainder of your program’s execution. This is how you do it with the lmdb library:

import Database.LMDB.Raw (MDB_dbi', MDB_env, mdb_dbi_open',
                          mdb_env_create, mdb_env_open,
                          mdb_env_set_mapsize, mdb_txn_begin,
                          mdb_txn_commit)

main :: IO ()
main = do
    -- Create an environment.
    env <- mdb_env_create

    -- The map size needs to be set. If you think 1 TiB will
    -- be more than enough, you can specify it like this.
    mdb_env_set_mapsize env (1024 * 1024 * 1024 * 1024)

    -- Open the environment. We are here assuming an existing directory
    -- that is either empty or already contains an LMDB database.
    mdb_env_open env "/somewhere/great-lmdb-dir" []

    -- Obtain the database handle.
    txn <- mdb_txn_begin env Nothing True
    dbi <- mdb_dbi_open' txn Nothing []
    mdb_txn_commit txn

Congratulations. You now have env (of type MDB_env) and dbi (of type MDB_dbi') that you can use with the functions provided by this library.

Using this library

If you are familiar with the streaming library, you will feel right at home. The following functions, located in Database.LMDB.Streaming, allow you to stream key-value pairs to and from an LMDB database:

import Control.Monad.Trans.Resource (MonadResource)
import Data.ByteString (ByteString)
import Database.LMDB.Raw (MDB_dbi', MDB_env)
import Streaming.Prelude (Of, Stream)

readLMDB :: (MonadResource m)
         => MDB_env
         -> MDB_dbi'
         -> Stream (Of (ByteString, ByteString)) m ()

writeLMDB :: (MonadResource m)
          => MDB_env
          -> MDB_dbi'
          -> Bool     -- If True, an exception will be thrown when attempting to re-insert a key.
          -> Stream (Of (ByteString, ByteString)) m r
          -> m r

Do not supply empty keys in writeLMDB; this is not allowed by LMDB. Note also that an LMDB transaction (read-only for readLMDB and read-write for writeLMDB) is kept open for the duration of the stream; so when using this library, you should bear in mind LMDB’s caveats regarding long-lived transactions.

lmdb-streaming's People

Contributors

shlok avatar

Watchers

 avatar James Cloos avatar  avatar

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.