Rocker — rocksdb driver for Erlang

Maxim Molchanov
Sep 14, 2018 · 5 min read

Brief overview of current solutions

Rocker

API overview

Open database

{ok, Db} = rocker:open_default(<<”/project/priv/db_default_path”>>).
{ok, Db} = rocker:open(<<"/project/priv/db_path">>, #{create_if_missing => true,set_max_open_files => 1000,set_use_fsync => false,...set_disable_auto_compactions => true,set_compaction_style => universal}).
{ok, Db} = case rocker:list_cf(BookDbPath) of{ok, CfList} ->   rocker:open_cf_default(BookDbPath, CfList);_ ->   CfList = [],   rocker:open_default(BookDbPath)end.

Delete database

Recover database after a failure

Column family creation

rocker:create_cf_default(Db, <<”testcf1">>) -> ok.

Column family deletion

rocker:drop_cf(Db, <<”testcf”>>) -> ok.

CRUD operations

Data writing by key

rocker:put(Db, <<”key”>>, <<”value”>>) -> ok.

Data acquisition by key

rocker:get(Db, <<”key”>>) -> {ok, <<”value”>>} | notfound

Data deletion by key

rocker:delete(Db, <<”key”>>) -> ok.

Data writing by key within CF

rocker:put_cf(Db, <<”testcf”>>, <<”key”>>, <<”value”>>) -> ok.

Data acquisition by key within CF

rocker:get_cf(Db, <<”testcf”>>, <<”key”>>) -> {ok, <<”value”>>} | notfound

Data deletion by key within CF

rocker:delete_cf(Db, <<”testcf”>>, <<”key”>>) -> ok,

Iterators

Create iterator

rocker:iterator(Db, {‘start’}) -> {ok, Iter}.

Check iterator

rocker:iterator_valid(Iter) -> {ok, true} | {ok, false}.

Create iterator for CF

rocker:iterator_cf(Db, Cf, {‘start’}) -> {ok, Iter}.

Create prefix iterator

{ok, Db} = rocker:open(Path, #{prefix_length => 3}).
{ok, Iter} = rocker:prefix_iterator(Db, <<”aaa”>>),

Create prefix iterator for CF

{ok, Iter} = rocker:prefix_iterator_cf(Db, Cf, <<”aaa”>>),

Get the following element

rocker:next(Iter) -> {ok, <<”key”>>, <<”value”>>} | ok

Transactions

{ok, 6} = rocker:tx(Db, [   {put, <<"k1">>, <<"v1">>},   {put, <<"k2">>, <<"v2">>},   {delete, <<"k0">>, <<"v0">>},   {put_cf, Cf, <<"k1">>, <<"v1">>},   {put_cf, Cf, <<"k2">>, <<"v2">>},   {delete_cf, Cf, <<"k0">>, <<"v0">>}]).

Performance

Conclusion

Maxim Molchanov

Written by

Senior Software Engineer at vonmo.com, MS, PGCHE

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade