2012-04-19 22:09:01 +00:00
|
|
|
%% ----------------------------------------------------------------------------
|
|
|
|
%%
|
2012-05-07 15:22:55 +00:00
|
|
|
%% hanoidb: LSM-trees (Log-Structured Merge Trees) Indexed Storage
|
2012-04-19 22:09:01 +00:00
|
|
|
%%
|
|
|
|
%% Copyright 2011-2012 (c) Trifork A/S. All Rights Reserved.
|
|
|
|
%% http://trifork.com/ info@trifork.com
|
|
|
|
%%
|
|
|
|
%% Copyright 2012 (c) Basho Technologies, Inc. All Rights Reserved.
|
|
|
|
%% http://basho.com/ info@basho.com
|
|
|
|
%%
|
|
|
|
%% This file is provided to you under the Apache License, Version 2.0 (the
|
|
|
|
%% "License"); you may not use this file except in compliance with the License.
|
|
|
|
%% You may obtain a copy of the License at
|
|
|
|
%%
|
|
|
|
%% http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
%%
|
|
|
|
%% Unless required by applicable law or agreed to in writing, software
|
|
|
|
%% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
%% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
%% License for the specific language governing permissions and limitations
|
|
|
|
%% under the License.
|
|
|
|
%%
|
|
|
|
%% ----------------------------------------------------------------------------
|
|
|
|
|
2012-05-07 15:22:55 +00:00
|
|
|
-module(basho_bench_driver_hanoidb).
|
2012-01-05 14:28:39 +00:00
|
|
|
|
|
|
|
-record(state, { tree,
|
|
|
|
filename,
|
|
|
|
flags,
|
|
|
|
sync_interval,
|
|
|
|
last_sync }).
|
|
|
|
|
|
|
|
-export([new/1,
|
|
|
|
run/4]).
|
|
|
|
|
2012-05-07 15:22:55 +00:00
|
|
|
-include("hanoidb.hrl").
|
2012-01-05 14:28:39 +00:00
|
|
|
-include_lib("basho_bench/include/basho_bench.hrl").
|
|
|
|
|
2012-05-07 15:22:55 +00:00
|
|
|
-record(key_range, { from_key = <<>> :: binary(),
|
2012-04-15 10:40:43 +00:00
|
|
|
from_inclusive = true :: boolean(),
|
|
|
|
to_key :: binary() | undefined,
|
|
|
|
to_inclusive = false :: boolean(),
|
|
|
|
limit :: pos_integer() | undefined }).
|
2012-01-05 14:28:39 +00:00
|
|
|
|
|
|
|
%% ====================================================================
|
|
|
|
%% API
|
|
|
|
%% ====================================================================
|
|
|
|
|
|
|
|
new(_Id) ->
|
|
|
|
%% Make sure bitcask is available
|
2012-05-07 15:22:55 +00:00
|
|
|
case code:which(hanoidb) of
|
2012-01-05 14:28:39 +00:00
|
|
|
non_existing ->
|
2012-05-07 15:22:55 +00:00
|
|
|
?FAIL_MSG("~s requires hanoidb to be available on code path.\n",
|
2012-01-05 14:28:39 +00:00
|
|
|
[?MODULE]);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
|
|
|
|
%% Get the target directory
|
2012-05-07 15:22:55 +00:00
|
|
|
Dir = basho_bench_config:get(hanoidb_dir, "."),
|
|
|
|
Filename = filename:join(Dir, "test.hanoidb"),
|
2012-01-05 14:28:39 +00:00
|
|
|
|
|
|
|
%% Look for sync interval config
|
2012-05-07 15:22:55 +00:00
|
|
|
case basho_bench_config:get(hanoidb_sync_interval, infinity) of
|
2012-01-05 14:28:39 +00:00
|
|
|
Value when is_integer(Value) ->
|
|
|
|
SyncInterval = Value;
|
|
|
|
infinity ->
|
|
|
|
SyncInterval = infinity
|
|
|
|
end,
|
|
|
|
|
|
|
|
%% Get any bitcask flags
|
2012-05-07 15:22:55 +00:00
|
|
|
case hanoidb:open(Filename) of
|
2012-01-05 14:28:39 +00:00
|
|
|
{error, Reason} ->
|
2012-05-07 15:22:55 +00:00
|
|
|
?FAIL_MSG("Failed to open hanoidb in ~s: ~p\n", [Filename, Reason]);
|
2012-01-05 14:28:39 +00:00
|
|
|
{ok, FBTree} ->
|
|
|
|
{ok, #state { tree = FBTree,
|
|
|
|
filename = Filename,
|
|
|
|
sync_interval = SyncInterval,
|
|
|
|
last_sync = os:timestamp() }}
|
|
|
|
end.
|
|
|
|
|
|
|
|
run(get, KeyGen, _ValueGen, State) ->
|
2012-05-07 15:22:55 +00:00
|
|
|
case hanoidb:lookup(State#state.tree, KeyGen()) of
|
2012-01-05 14:28:39 +00:00
|
|
|
{ok, _Value} ->
|
|
|
|
{ok, State};
|
2012-04-15 19:34:42 +00:00
|
|
|
not_found ->
|
2012-01-05 14:28:39 +00:00
|
|
|
{ok, State};
|
|
|
|
{error, Reason} ->
|
|
|
|
{error, Reason}
|
|
|
|
end;
|
|
|
|
run(put, KeyGen, ValueGen, State) ->
|
2012-05-07 15:22:55 +00:00
|
|
|
case hanoidb:put(State#state.tree, KeyGen(), ValueGen()) of
|
2012-01-05 14:28:39 +00:00
|
|
|
ok ->
|
|
|
|
{ok, State};
|
|
|
|
{error, Reason} ->
|
|
|
|
{error, Reason}
|
|
|
|
end;
|
|
|
|
run(delete, KeyGen, _ValueGen, State) ->
|
2012-05-07 15:22:55 +00:00
|
|
|
case hanoidb:delete(State#state.tree, KeyGen()) of
|
2012-01-05 14:28:39 +00:00
|
|
|
ok ->
|
|
|
|
{ok, State};
|
|
|
|
{error, Reason} ->
|
|
|
|
{error, Reason}
|
2012-04-15 10:40:43 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
run(fold_100, KeyGen, _ValueGen, State) ->
|
|
|
|
[From,To] = lists:usort([KeyGen(), KeyGen()]),
|
2012-05-07 15:22:55 +00:00
|
|
|
case hanoidb:sync_fold_range(State#state.tree,
|
2012-04-15 10:40:43 +00:00
|
|
|
fun(_Key,_Value,Count) ->
|
|
|
|
Count+1
|
|
|
|
end,
|
|
|
|
0,
|
2012-05-07 15:22:55 +00:00
|
|
|
#key_range{ from_key=From,
|
2012-04-15 10:40:43 +00:00
|
|
|
to_key=To,
|
|
|
|
limit=100 }) of
|
|
|
|
Count when Count >= 0; Count =< 100 ->
|
|
|
|
{ok,State};
|
|
|
|
Count ->
|
|
|
|
{error, {bad_fold_count, Count}}
|
2012-01-05 14:28:39 +00:00
|
|
|
end.
|