memdb

Build Status][gh-actions-badge]][gh-actions]
[![Erlang Versions][erlang-badge] [ Tags

A simple in-memory K/V store with MVCC semantics

Contents

Overview

memdb is K/V store built on top of ETS. memdb compared to ETS provides concurrent access to the database using MVCC allowing multiple reader to read concurrently a consitent view of the database without locking.

All writes are serialized for now.

A MemDB's memory consumption increases monotonically, even if keys are deleted or values are updated. Compaction can be triggered manually or automatically using the auto-vacuum. A full snapshot can be stored or copied in another database if needed.

Documentation

Full doc is available in memdb.

Build

$ rebar3 compile

Usage

Start up an Erlang shell:

$ rebar3 shell

Create a database

Name = mydb.
Db = memdb:open(Name).

Store a values

Storing a value associated to a key using memdb:put/3:

Key = <<"a">>,
Value = 1,
ok =  memdb:put(Key, Value, Db).

Retrieve a value

Use the memdb:get/2 function to retrieve a value.

Value = memdb:get(Key, Db).

Value should be 1. Note that you can use memdb:contains/2 to check ahead of time:

memdb:contains(Key, Db).

Delete a value

Use memdb:delete/2 to delete a value:

ok = memdb:delete(Key, Db).

Working with Multiple Values

Storing

Using memdb:write_batch/2 you can write and delete multiple values in one pass:

ok =  memdb:write_batch([{put, <<"a">>, 1},
                         {put, <<"b">>, 2},
                         {put, <<"c">>, 3}], Db),

ok =  memdb:write_batch([{put, <<"d">>, 4},
                         {delete, <<"b">>},
                         {put, <<"e">>, 5}], Db).

Retrieving

Use memdb:fold/4 to retrieve multiples K/Vs

Close Db

Close a storage using memdb:close/1:

memdb:close(Db)

License

Copyright © 2015 Benoît Chesneau
Copyright © 2024 Duncan McGreggor

Distributed under the Mozilla Public License Version 2.0.