2013-05-19 11:25:35 +00:00
|
|
|
-module(basho_bench_driver_lmdb).
|
2013-05-19 04:19:01 +00:00
|
|
|
|
|
|
|
-record(state, {
|
|
|
|
handle
|
|
|
|
}).
|
|
|
|
|
|
|
|
-export([new/1,
|
|
|
|
run/4]).
|
|
|
|
|
|
|
|
-include_lib("basho_bench/include/basho_bench.hrl").
|
|
|
|
|
|
|
|
|
|
|
|
%% ====================================================================
|
|
|
|
%% API
|
|
|
|
%% ====================================================================
|
|
|
|
|
|
|
|
new(1) ->
|
2013-05-19 11:25:35 +00:00
|
|
|
%% Make sure lmdb is available
|
|
|
|
case code:which(lmdb) of
|
2013-05-19 04:19:01 +00:00
|
|
|
non_existing ->
|
2013-05-19 11:25:35 +00:00
|
|
|
?FAIL_MSG("~s requires lmdb to be available on code path.\n",
|
2013-05-19 04:19:01 +00:00
|
|
|
[?MODULE]);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end,
|
2013-05-19 11:25:35 +00:00
|
|
|
%{ok, _} = lmdb_sup:start_link(),
|
2013-05-19 04:19:01 +00:00
|
|
|
setup(1);
|
|
|
|
new(Id) ->
|
|
|
|
setup(Id).
|
|
|
|
|
|
|
|
setup(_Id) ->
|
|
|
|
%% Get the target directory
|
2013-05-19 11:25:35 +00:00
|
|
|
Dir = basho_bench_config:get(lmdb_dir, "/tmp"),
|
|
|
|
%Config = basho_bench_config:get(lmdb, []),
|
2013-05-19 04:19:01 +00:00
|
|
|
|
|
|
|
%% Start Lightning MDB
|
2013-05-19 11:25:35 +00:00
|
|
|
case lmdb:open(Dir, 32212254720, 16#10000 bor 16#40000 bor 16#80000) of
|
2013-05-19 04:19:01 +00:00
|
|
|
{ok, H} ->
|
|
|
|
{ok, #state{handle=H}};
|
|
|
|
{error, Reason} ->
|
2013-05-19 11:25:35 +00:00
|
|
|
?FAIL_MSG("Failed to establish a Lightning MDB connection, lmdb backend unable to start: ~p\n", [Reason]),
|
2013-05-19 04:19:01 +00:00
|
|
|
{error, Reason}
|
|
|
|
end.
|
|
|
|
|
|
|
|
run(get, KeyGen, _ValueGen, #state{handle=Handle}=State) ->
|
2013-05-19 11:25:35 +00:00
|
|
|
case lmdb:get(Handle, KeyGen()) of
|
2013-05-19 04:19:01 +00:00
|
|
|
{ok, _Value} ->
|
|
|
|
{ok, State};
|
|
|
|
not_found ->
|
|
|
|
{ok, State};
|
|
|
|
{error, Reason} ->
|
|
|
|
{error, Reason}
|
|
|
|
end;
|
|
|
|
run(put, KeyGen, ValueGen, #state{handle=Handle}=State) ->
|
|
|
|
Key = KeyGen(),
|
|
|
|
Val = ValueGen(),
|
2013-05-19 11:25:35 +00:00
|
|
|
case lmdb:upd(Handle, Key, Val) of
|
2013-05-19 04:19:01 +00:00
|
|
|
ok ->
|
|
|
|
{ok, State};
|
|
|
|
{error, Reason} ->
|
|
|
|
{error, Reason}
|
|
|
|
end;
|
|
|
|
run(delete, KeyGen, _ValueGen, #state{handle=Handle}=State) ->
|
2013-05-19 11:25:35 +00:00
|
|
|
case lmdb:del(Handle, KeyGen()) of
|
2013-05-19 04:19:01 +00:00
|
|
|
ok ->
|
|
|
|
{ok, State};
|
|
|
|
not_found ->
|
|
|
|
{ok, State};
|
|
|
|
{error, Reason} ->
|
|
|
|
{error, Reason}
|
|
|
|
end.
|
|
|
|
|
|
|
|
%% config_value(Key, Config, Default) ->
|
|
|
|
%% case proplists:get_value(Key, Config) of
|
|
|
|
%% undefined ->
|
|
|
|
%% Default;
|
|
|
|
%% Value ->
|
|
|
|
%% Value
|
|
|
|
%% end.
|