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(hanoidb_tests).
|
2012-01-04 14:05:31 +00:00
|
|
|
|
2012-05-07 15:22:55 +00:00
|
|
|
-include("include/hanoidb.hrl").
|
|
|
|
-include("src/hanoidb.hrl").
|
2012-04-15 10:40:43 +00:00
|
|
|
|
2012-01-04 14:05:31 +00:00
|
|
|
-ifdef(TEST).
|
2012-01-07 14:44:31 +00:00
|
|
|
-ifdef(TRIQ).
|
|
|
|
-include_lib("triq/include/triq.hrl").
|
|
|
|
-include_lib("triq/include/triq_statem.hrl").
|
|
|
|
-else.
|
2012-01-06 21:56:23 +00:00
|
|
|
-include_lib("proper/include/proper.hrl").
|
2012-01-07 14:44:31 +00:00
|
|
|
-endif.
|
2012-01-04 14:05:31 +00:00
|
|
|
-include_lib("eunit/include/eunit.hrl").
|
|
|
|
-endif.
|
|
|
|
|
2012-01-07 14:44:31 +00:00
|
|
|
-ifdef(PROPER).
|
2012-01-06 21:56:23 +00:00
|
|
|
-behaviour(proper_statem).
|
2012-01-07 14:44:31 +00:00
|
|
|
-endif.
|
2012-01-04 14:05:31 +00:00
|
|
|
|
2012-01-06 21:56:23 +00:00
|
|
|
-compile(export_all).
|
2012-01-04 14:05:31 +00:00
|
|
|
|
2012-01-06 21:56:23 +00:00
|
|
|
-export([command/1, initial_state/0,
|
|
|
|
next_state/3, postcondition/3,
|
|
|
|
precondition/2]).
|
2012-01-04 14:05:31 +00:00
|
|
|
|
2012-01-21 14:15:19 +00:00
|
|
|
-record(tree, { elements = dict:new() }).
|
2012-01-06 21:56:23 +00:00
|
|
|
-record(state, { open = dict:new(),
|
|
|
|
closed = dict:new() }).
|
2012-05-07 15:22:55 +00:00
|
|
|
-define(SERVER, hanoidb_drv).
|
2012-01-04 14:05:31 +00:00
|
|
|
|
2012-01-06 21:56:23 +00:00
|
|
|
full_test_() ->
|
2012-06-21 22:12:32 +00:00
|
|
|
{setup, spawn, fun () -> ok end, fun (_) -> ok end,
|
2012-01-07 13:21:38 +00:00
|
|
|
[
|
2012-01-07 00:02:09 +00:00
|
|
|
?_test(test_tree_simple_1()),
|
2012-01-07 11:43:49 +00:00
|
|
|
?_test(test_tree_simple_2()),
|
2012-05-01 14:27:39 +00:00
|
|
|
?_test(test_tree_simple_4()),
|
2012-06-19 14:03:42 +00:00
|
|
|
?_test(test_tree_simple_5())
|
2012-04-27 07:59:09 +00:00
|
|
|
]}.
|
|
|
|
|
2012-06-19 14:03:42 +00:00
|
|
|
longer_test_() ->
|
2012-04-27 07:59:09 +00:00
|
|
|
{setup,
|
|
|
|
spawn,
|
|
|
|
fun () -> ok end,
|
|
|
|
fun (_) -> ok end,
|
|
|
|
[
|
2012-06-19 14:03:42 +00:00
|
|
|
{timeout, 300, ?_test(test_tree())},
|
|
|
|
{timeout, 120, ?_test(test_qc())}
|
2012-01-07 13:21:38 +00:00
|
|
|
]}.
|
2012-01-04 14:05:31 +00:00
|
|
|
|
2012-01-07 14:44:31 +00:00
|
|
|
-ifdef(TRIQ).
|
|
|
|
test_qc() ->
|
|
|
|
[?assertEqual(true, triq:module(?MODULE))].
|
|
|
|
-else.
|
2012-01-07 00:02:09 +00:00
|
|
|
qc_opts() -> [{numtests, 800}].
|
2012-01-07 14:44:31 +00:00
|
|
|
test_qc() ->
|
2012-01-06 21:56:23 +00:00
|
|
|
[?assertEqual([], proper:module(?MODULE, qc_opts()))].
|
2012-01-07 14:44:31 +00:00
|
|
|
-endif.
|
2012-01-04 14:05:31 +00:00
|
|
|
|
2012-01-07 11:43:49 +00:00
|
|
|
%% Generators
|
|
|
|
%% ----------------------------------------------------------------------
|
2012-01-04 14:05:31 +00:00
|
|
|
|
2012-01-07 14:16:57 +00:00
|
|
|
-define(NUM_TREES, 10).
|
|
|
|
|
2012-01-07 11:43:49 +00:00
|
|
|
%% Generate a name for a btree
|
2012-01-06 21:56:23 +00:00
|
|
|
g_btree_name() ->
|
2012-01-07 14:44:31 +00:00
|
|
|
?LET(I, choose(1,?NUM_TREES),
|
2012-01-07 14:16:57 +00:00
|
|
|
btree_name(I)).
|
2012-01-04 14:05:31 +00:00
|
|
|
|
2012-01-07 21:58:38 +00:00
|
|
|
%% Generate a key for the Tree
|
|
|
|
g_key() ->
|
|
|
|
binary().
|
|
|
|
|
|
|
|
%% Generate a value for the Tree
|
|
|
|
g_value() ->
|
|
|
|
binary().
|
|
|
|
|
|
|
|
g_fail_key() ->
|
|
|
|
?LET(T, choose(1,999999999999),
|
|
|
|
term_to_binary(T)).
|
|
|
|
|
2012-01-07 22:35:27 +00:00
|
|
|
g_open_tree(Open) ->
|
|
|
|
oneof(dict:fetch_keys(Open)).
|
|
|
|
|
2012-01-07 11:43:49 +00:00
|
|
|
%% Pick a name of a non-empty Btree
|
2012-01-07 22:35:27 +00:00
|
|
|
g_non_empty_btree(Open) ->
|
2012-01-21 14:15:19 +00:00
|
|
|
?LET(TreesWithKeys, dict:filter(fun(_K, #tree { elements = D}) ->
|
|
|
|
dict:size(D) > 0
|
|
|
|
end,
|
|
|
|
Open),
|
2012-01-07 22:35:27 +00:00
|
|
|
oneof(dict:fetch_keys(TreesWithKeys))).
|
2012-01-07 21:58:38 +00:00
|
|
|
|
|
|
|
g_existing_key(Name, Open) ->
|
2012-01-21 14:15:19 +00:00
|
|
|
#tree { elements = Elems } = dict:fetch(Name, Open),
|
|
|
|
oneof(dict:fetch_keys(Elems)).
|
2012-01-07 21:58:38 +00:00
|
|
|
|
|
|
|
g_non_existing_key(Name, Open) ->
|
|
|
|
?SUCHTHAT(Key, g_fail_key(),
|
|
|
|
begin
|
2012-01-21 14:15:19 +00:00
|
|
|
#tree { elements = D } = dict:fetch(Name, Open),
|
2012-01-07 21:58:38 +00:00
|
|
|
not dict:is_key(Key, D)
|
|
|
|
end).
|
2012-01-07 00:02:09 +00:00
|
|
|
|
2012-01-22 11:34:22 +00:00
|
|
|
g_fold_operation() ->
|
|
|
|
oneof([{fun (K, V, Acc) -> [{K, V} | Acc] end, []}]).
|
|
|
|
|
2012-01-07 14:16:57 +00:00
|
|
|
btree_name(I) ->
|
|
|
|
"Btree_" ++ integer_to_list(I).
|
|
|
|
|
2012-01-07 11:43:49 +00:00
|
|
|
%% Statem test
|
|
|
|
%% ----------------------------------------------------------------------
|
|
|
|
initial_state() ->
|
2012-01-07 14:16:57 +00:00
|
|
|
ClosedBTrees = lists:foldl(fun(N, Closed) ->
|
|
|
|
dict:store(btree_name(N),
|
2012-01-21 14:15:19 +00:00
|
|
|
#tree { },
|
2012-01-07 14:16:57 +00:00
|
|
|
Closed)
|
|
|
|
end,
|
|
|
|
dict:new(),
|
|
|
|
lists:seq(1,?NUM_TREES)),
|
|
|
|
#state { closed=ClosedBTrees }.
|
2012-01-07 00:02:09 +00:00
|
|
|
|
|
|
|
|
2012-01-07 14:16:57 +00:00
|
|
|
command(#state { open = Open, closed = Closed } = S) ->
|
2012-01-06 21:56:23 +00:00
|
|
|
frequency(
|
2012-06-19 14:03:42 +00:00
|
|
|
[ {20, {call, ?SERVER, open, [oneof(dict:fetch_keys(Closed))]}}
|
2012-01-21 14:15:19 +00:00
|
|
|
|| closed_dicts(S)]
|
|
|
|
++ [ {20, {call, ?SERVER, close, [oneof(dict:fetch_keys(Open))]}}
|
|
|
|
|| open_dicts(S)]
|
|
|
|
++ [ {2000, {call, ?SERVER, put, cmd_put_args(S)}}
|
|
|
|
|| open_dicts(S)]
|
2012-04-15 14:35:39 +00:00
|
|
|
++ [ {1500, {call, ?SERVER, get_fail, cmd_get_fail_args(S)}}
|
2012-01-21 14:15:19 +00:00
|
|
|
|| open_dicts(S)]
|
2012-04-15 14:35:39 +00:00
|
|
|
++ [ {1500, {call, ?SERVER, get_exist, cmd_get_args(S)}}
|
2012-01-21 14:15:19 +00:00
|
|
|
|| open_dicts(S), open_dicts_with_keys(S)]
|
|
|
|
++ [ {500, {call, ?SERVER, delete_exist, cmd_delete_args(S)}}
|
|
|
|
|| open_dicts(S), open_dicts_with_keys(S)]
|
2012-04-27 07:59:09 +00:00
|
|
|
++ [ {125, {call, ?SERVER, fold_range, cmd_sync_fold_range_args(S)}}
|
2012-01-21 20:16:24 +00:00
|
|
|
|| open_dicts(S), open_dicts_with_keys(S)]
|
2012-01-21 14:15:19 +00:00
|
|
|
).
|
2012-01-07 11:43:49 +00:00
|
|
|
|
|
|
|
%% Precondition (abstract)
|
2012-04-27 07:59:09 +00:00
|
|
|
precondition(S, {call, ?SERVER, fold_range, [_Tree, _F, _A0, Range]}) ->
|
2012-04-15 10:40:43 +00:00
|
|
|
is_valid_range(Range) andalso open_dicts(S) andalso open_dicts_with_keys(S);
|
2012-01-21 14:15:19 +00:00
|
|
|
precondition(S, {call, ?SERVER, delete_exist, [_Name, _K]}) ->
|
|
|
|
open_dicts(S) andalso open_dicts_with_keys(S);
|
2012-04-15 14:35:39 +00:00
|
|
|
precondition(S, {call, ?SERVER, get_fail, [_Name, _K]}) ->
|
2012-01-21 14:15:19 +00:00
|
|
|
open_dicts(S);
|
2012-04-15 14:35:39 +00:00
|
|
|
precondition(S, {call, ?SERVER, get_exist, [_Name, _K]}) ->
|
2012-01-21 14:15:19 +00:00
|
|
|
open_dicts(S) andalso open_dicts_with_keys(S);
|
2012-01-07 11:43:49 +00:00
|
|
|
precondition(#state { open = Open }, {call, ?SERVER, put, [Name, _K, _V]}) ->
|
2012-01-06 21:56:23 +00:00
|
|
|
dict:is_key(Name, Open);
|
2012-01-21 14:15:19 +00:00
|
|
|
precondition(#state { open = Open, closed = Closed },
|
|
|
|
{call, ?SERVER, open, [Name]}) ->
|
2012-01-07 14:16:57 +00:00
|
|
|
(not (dict:is_key(Name, Open))) and (dict:is_key(Name, Closed));
|
2012-01-21 14:15:19 +00:00
|
|
|
precondition(#state { open = Open, closed = Closed },
|
|
|
|
{call, ?SERVER, close, [Name]}) ->
|
2012-01-07 14:16:57 +00:00
|
|
|
(dict:is_key(Name, Open)) and (not dict:is_key(Name, Closed)).
|
|
|
|
|
2012-05-07 15:22:55 +00:00
|
|
|
is_valid_range(#key_range{ from_key=FromKey, from_inclusive=FromIncl,
|
2012-04-15 10:40:43 +00:00
|
|
|
to_key=ToKey, to_inclusive=ToIncl,
|
|
|
|
limit=Limit })
|
|
|
|
when
|
|
|
|
(Limit == undefined) orelse (Limit > 0),
|
|
|
|
is_binary(FromKey),
|
|
|
|
(ToKey == undefined) orelse is_binary(ToKey),
|
|
|
|
FromKey =< ToKey,
|
|
|
|
is_boolean(FromIncl),
|
|
|
|
is_boolean(ToIncl)
|
|
|
|
->
|
|
|
|
if (FromKey == ToKey) ->
|
|
|
|
(FromIncl == true) and (ToIncl == true);
|
|
|
|
true ->
|
|
|
|
true
|
|
|
|
end;
|
|
|
|
is_valid_range(_) ->
|
|
|
|
false.
|
|
|
|
|
|
|
|
|
2012-01-07 11:43:49 +00:00
|
|
|
%% Next state manipulation (abstract / concrete)
|
2012-04-27 07:59:09 +00:00
|
|
|
next_state(S, _Res, {call, ?SERVER, fold_range, [_Tree, _F, _A0, _Range]}) ->
|
2012-01-21 14:15:19 +00:00
|
|
|
S;
|
2012-04-15 14:35:39 +00:00
|
|
|
next_state(S, _Res, {call, ?SERVER, get_fail, [_Name, _Key]}) ->
|
2012-01-07 21:58:38 +00:00
|
|
|
S;
|
2012-04-15 14:35:39 +00:00
|
|
|
next_state(S, _Res, {call, ?SERVER, get_exist, [_Name, _Key]}) ->
|
2012-01-07 00:02:09 +00:00
|
|
|
S;
|
2012-01-07 11:43:49 +00:00
|
|
|
next_state(#state { open = Open} = S, _Res,
|
|
|
|
{call, ?SERVER, delete_exist, [Name, Key]}) ->
|
|
|
|
S#state { open = dict:update(Name,
|
2012-01-21 14:15:19 +00:00
|
|
|
fun(#tree { elements = Dict}) ->
|
|
|
|
#tree { elements =
|
2012-04-15 14:35:39 +00:00
|
|
|
dict:erase(Key, Dict)}
|
2012-01-07 11:43:49 +00:00
|
|
|
end,
|
|
|
|
Open)};
|
2012-01-06 21:56:23 +00:00
|
|
|
next_state(#state { open = Open} = S, _Res,
|
|
|
|
{call, ?SERVER, put, [Name, Key, Value]}) ->
|
2012-01-21 14:15:19 +00:00
|
|
|
S#state { open = dict:update(
|
|
|
|
Name,
|
|
|
|
fun(#tree { elements = Dict}) ->
|
|
|
|
#tree { elements =
|
|
|
|
dict:store(Key, Value, Dict) }
|
|
|
|
end,
|
|
|
|
Open)};
|
|
|
|
next_state(#state { open = Open, closed=Closed} = S,
|
|
|
|
_Res, {call, ?SERVER, open, [Name]}) ->
|
2012-01-07 14:16:57 +00:00
|
|
|
S#state { open = dict:store(Name, dict:fetch(Name, Closed) , Open),
|
|
|
|
closed = dict:erase(Name, Closed) };
|
2012-01-21 14:15:19 +00:00
|
|
|
next_state(#state { open = Open, closed=Closed} = S, _Res,
|
|
|
|
{call, ?SERVER, close, [Name]}) ->
|
2012-01-07 14:16:57 +00:00
|
|
|
S#state { closed = dict:store(Name, dict:fetch(Name, Open) , Closed),
|
|
|
|
open = dict:erase(Name, Open) }.
|
2012-01-04 14:05:31 +00:00
|
|
|
|
2012-01-07 11:43:49 +00:00
|
|
|
%% Postcondition check (concrete)
|
2012-01-22 11:34:22 +00:00
|
|
|
postcondition(#state { open = Open},
|
2012-04-27 07:59:09 +00:00
|
|
|
{call, ?SERVER, fold_range, [Tree, F, A0, Range]}, Result) ->
|
2012-01-22 11:34:22 +00:00
|
|
|
#tree { elements = TDict } = dict:fetch(Tree, Open),
|
2012-04-15 10:40:43 +00:00
|
|
|
DictResult = lists:sort(dict_range_query(TDict, F, A0, Range)),
|
|
|
|
CallResult = lists:sort(Result),
|
|
|
|
DictResult == CallResult;
|
2012-01-07 22:35:27 +00:00
|
|
|
postcondition(_S,
|
2012-04-16 11:31:14 +00:00
|
|
|
{call, ?SERVER, get_fail, [_Name, _Key]}, not_found) ->
|
2012-01-07 21:58:38 +00:00
|
|
|
true;
|
2012-01-07 00:02:09 +00:00
|
|
|
postcondition(#state { open = Open },
|
2012-04-15 14:35:39 +00:00
|
|
|
{call, ?SERVER, get_exist, [Name, Key]}, {ok, Value}) ->
|
2012-01-21 14:15:19 +00:00
|
|
|
#tree { elements = Elems } = dict:fetch(Name, Open),
|
|
|
|
dict:fetch(Key, Elems) == Value;
|
2012-01-07 11:43:49 +00:00
|
|
|
postcondition(_S, {call, ?SERVER, delete_exist, [_Name, _Key]}, ok) ->
|
|
|
|
true;
|
2012-01-06 21:56:23 +00:00
|
|
|
postcondition(_S, {call, ?SERVER, put, [_Name, _Key, _Value]}, ok) ->
|
|
|
|
true;
|
|
|
|
postcondition(_S, {call, ?SERVER, open, [_Name]}, ok) ->
|
|
|
|
true;
|
2012-01-07 14:16:57 +00:00
|
|
|
postcondition(_S, {call, ?SERVER, close, [_Name]}, ok) ->
|
|
|
|
true;
|
2012-05-05 16:56:03 +00:00
|
|
|
postcondition(_State, _Call, _Result) ->
|
|
|
|
% error_logger:error_report([{not_matching_any_postcondition, _State, _Call, _Result}]),
|
2012-01-06 21:56:23 +00:00
|
|
|
false.
|
2012-01-04 14:05:31 +00:00
|
|
|
|
|
|
|
|
2012-01-07 11:43:49 +00:00
|
|
|
%% Main property. Running a random set of commands is in agreement
|
|
|
|
%% with a dict.
|
2012-01-06 21:56:23 +00:00
|
|
|
prop_dict_agree() ->
|
|
|
|
?FORALL(Cmds, commands(?MODULE),
|
|
|
|
?TRAPEXIT(
|
|
|
|
begin
|
2012-05-07 15:22:55 +00:00
|
|
|
hanoidb_drv:start_link(),
|
2012-01-06 21:56:23 +00:00
|
|
|
{History,State,Result} = run_commands(?MODULE, Cmds),
|
2012-05-07 15:22:55 +00:00
|
|
|
hanoidb_drv:stop(),
|
2012-01-07 00:02:09 +00:00
|
|
|
cleanup_test_trees(State),
|
2012-01-06 21:56:23 +00:00
|
|
|
?WHENFAIL(io:format("History: ~w\nState: ~w\nResult: ~w\n",
|
|
|
|
[History,State,Result]),
|
2012-01-07 14:44:31 +00:00
|
|
|
Result =:= ok)
|
2012-01-06 21:56:23 +00:00
|
|
|
end)).
|
2012-01-04 14:05:31 +00:00
|
|
|
|
2012-01-07 11:43:49 +00:00
|
|
|
%% UNIT TESTS
|
2012-01-06 21:56:23 +00:00
|
|
|
%% ----------------------------------------------------------------------
|
2012-01-07 00:02:09 +00:00
|
|
|
test_tree_simple_1() ->
|
2012-05-07 15:22:55 +00:00
|
|
|
{ok, Tree} = hanoidb:open("simple"),
|
|
|
|
ok = hanoidb:put(Tree, <<>>, <<"data", 77:128>>),
|
|
|
|
{ok, <<"data", 77:128>>} = hanoidb:get(Tree, <<>>),
|
|
|
|
ok = hanoidb:close(Tree).
|
2012-01-07 00:02:09 +00:00
|
|
|
|
2012-01-07 11:43:49 +00:00
|
|
|
test_tree_simple_2() ->
|
2012-05-07 15:22:55 +00:00
|
|
|
{ok, Tree} = hanoidb:open("simple"),
|
|
|
|
ok = hanoidb:put(Tree, <<"ã">>, <<"µ">>),
|
2012-06-21 22:12:32 +00:00
|
|
|
{ok, <<"µ">>} = hanoidb:get(Tree, <<"ã">>),
|
2012-05-07 15:22:55 +00:00
|
|
|
ok = hanoidb:delete(Tree, <<"ã">>),
|
2012-06-21 22:12:32 +00:00
|
|
|
not_found = hanoidb:get(Tree, <<"ã">>),
|
2012-05-07 15:22:55 +00:00
|
|
|
ok = hanoidb:close(Tree).
|
2012-01-04 14:05:31 +00:00
|
|
|
|
2012-01-21 20:16:24 +00:00
|
|
|
test_tree_simple_4() ->
|
|
|
|
Key = <<56,11,62,42,35,163,16,100,9,224,8,228,130,94,198,2,126,117,243,
|
|
|
|
1,122,175,79,159,212,177,30,153,71,91,85,233,41,199,190,58,3,
|
|
|
|
173,220,9>>,
|
|
|
|
Value = <<212,167,12,6,105,152,17,80,243>>,
|
2012-05-07 15:22:55 +00:00
|
|
|
{ok, Tree} = hanoidb:open("simple"),
|
|
|
|
ok = hanoidb:put(Tree, Key, Value),
|
|
|
|
?assertEqual({ok, Value}, hanoidb:get(Tree, Key)),
|
2012-06-14 17:15:47 +00:00
|
|
|
ok = hanoidb:close(Tree).
|
|
|
|
|
|
|
|
test_tree_simple_5() ->
|
|
|
|
{ok, Tree} = hanoidb:open("simple"),
|
|
|
|
ok = hanoidb:put(Tree, <<"foo">>, <<"bar">>, 2),
|
|
|
|
{ok, <<"bar">>} = hanoidb:get(Tree, <<"foo">>),
|
|
|
|
ok = timer:sleep(3000),
|
|
|
|
not_found = hanoidb:get(Tree, <<"foo">>),
|
2012-05-07 15:22:55 +00:00
|
|
|
ok = hanoidb:close(Tree).
|
2012-01-21 20:16:24 +00:00
|
|
|
|
|
|
|
test_tree() ->
|
2012-05-07 15:22:55 +00:00
|
|
|
{ok, Tree} = hanoidb:open("simple2"),
|
2012-01-05 10:48:14 +00:00
|
|
|
lists:foldl(fun(N,_) ->
|
2012-06-19 14:03:42 +00:00
|
|
|
ok = hanoidb:put(Tree, <<N:128>>, <<"data",N:128>>)
|
2012-01-05 10:48:14 +00:00
|
|
|
end,
|
|
|
|
ok,
|
2012-05-05 16:56:03 +00:00
|
|
|
lists:seq(2,10000,1)),
|
2012-06-14 17:15:47 +00:00
|
|
|
% io:format(user, "INSERT DONE 1~n", []),
|
2012-04-27 07:59:09 +00:00
|
|
|
|
2012-01-15 23:37:52 +00:00
|
|
|
lists:foldl(fun(N,_) ->
|
2012-06-19 14:03:42 +00:00
|
|
|
ok = hanoidb:put(Tree, <<N:128>>, <<"data",N:128>>)
|
2012-01-15 23:37:52 +00:00
|
|
|
end,
|
|
|
|
ok,
|
|
|
|
lists:seq(4000,6000,1)),
|
2012-06-14 17:15:47 +00:00
|
|
|
% io:format(user, "INSERT DONE 2~n", []),
|
2012-04-27 07:59:09 +00:00
|
|
|
|
2012-05-07 15:22:55 +00:00
|
|
|
hanoidb:delete(Tree, <<1500:128>>),
|
2012-06-19 14:03:42 +00:00
|
|
|
% io:format(user, "DELETE DONE 3~n", []),
|
2012-01-15 23:37:52 +00:00
|
|
|
|
2012-06-19 18:30:11 +00:00
|
|
|
{Time1,{ok,Count1}} = timer:tc(?MODULE, run_fold, [Tree,1000,2000,9]),
|
|
|
|
% error_logger:info_msg("time to fold: ~p/sec (time=~p, count=~p)~n", [1000000/(Time1/Count1), Time1/1000000, Count1]),
|
2012-04-27 07:59:09 +00:00
|
|
|
|
2012-06-19 18:30:11 +00:00
|
|
|
{Time2,{ok,Count2}} = timer:tc(?MODULE, run_fold, [Tree,1000,2000,1000]),
|
|
|
|
% error_logger:info_msg("time to fold: ~p/sec (time=~p, count=~p)~n", [1000000/(Time2/Count2), Time2/1000000, Count2]),
|
2012-05-07 15:22:55 +00:00
|
|
|
ok = hanoidb:close(Tree).
|
2012-01-05 10:48:14 +00:00
|
|
|
|
2012-06-14 17:15:47 +00:00
|
|
|
run_fold(Tree,From,To,Limit) ->
|
2012-05-07 15:22:55 +00:00
|
|
|
{_, Count} = hanoidb:fold_range(Tree,
|
2012-06-19 14:03:42 +00:00
|
|
|
fun(<<N:128>>, _Value, {N, C}) ->
|
|
|
|
{N + 1, C + 1};
|
|
|
|
(<<1501:128>>, _Value, {1500, C}) ->
|
|
|
|
{1502, C + 1}
|
|
|
|
end,
|
|
|
|
{From, 0},
|
|
|
|
#key_range{from_key= <<From:128>>, to_key= <<(To+1):128>>, limit=Limit}),
|
2012-04-27 07:59:09 +00:00
|
|
|
{ok, Count}.
|
2012-01-15 23:37:52 +00:00
|
|
|
|
|
|
|
|
2012-01-07 11:43:49 +00:00
|
|
|
%% Command processing
|
|
|
|
%% ----------------------------------------------------------------------
|
|
|
|
cmd_close_args(#state { open = Open }) ->
|
|
|
|
oneof(dict:fetch_keys(Open)).
|
2012-01-04 14:05:31 +00:00
|
|
|
|
2012-01-07 11:43:49 +00:00
|
|
|
cmd_put_args(#state { open = Open }) ->
|
|
|
|
?LET({Name, Key, Value},
|
2012-01-07 21:58:38 +00:00
|
|
|
{oneof(dict:fetch_keys(Open)), g_key(), g_value()},
|
2012-01-07 11:43:49 +00:00
|
|
|
[Name, Key, Value]).
|
|
|
|
|
|
|
|
|
2012-04-15 14:35:39 +00:00
|
|
|
cmd_get_fail_args(#state { open = Open}) ->
|
2012-01-07 22:35:27 +00:00
|
|
|
?LET(Name, g_open_tree(Open),
|
2012-01-07 21:58:38 +00:00
|
|
|
?LET(Key, g_non_existing_key(Name, Open),
|
|
|
|
[Name, Key])).
|
|
|
|
|
2012-04-15 14:35:39 +00:00
|
|
|
cmd_get_args(#state { open = Open}) ->
|
2012-01-07 22:35:27 +00:00
|
|
|
?LET(Name, g_non_empty_btree(Open),
|
2012-01-07 21:58:38 +00:00
|
|
|
?LET(Key, g_existing_key(Name, Open),
|
2012-01-07 11:43:49 +00:00
|
|
|
[Name, Key])).
|
|
|
|
|
|
|
|
cmd_delete_args(#state { open = Open}) ->
|
2012-01-07 22:35:27 +00:00
|
|
|
?LET(Name, g_non_empty_btree(Open),
|
2012-01-07 21:58:38 +00:00
|
|
|
?LET(Key, g_existing_key(Name, Open),
|
2012-01-07 11:43:49 +00:00
|
|
|
[Name, Key])).
|
|
|
|
|
2012-01-21 14:15:19 +00:00
|
|
|
cmd_sync_range_args(#state { open = Open }) ->
|
|
|
|
?LET(Tree, g_non_empty_btree(Open),
|
|
|
|
?LET({K1, K2}, {g_existing_key(Tree, Open),
|
|
|
|
g_existing_key(Tree, Open)},
|
2012-05-07 15:22:55 +00:00
|
|
|
[Tree, #key_range{from_key=K1, to_key=K2}])).
|
2012-01-07 11:43:49 +00:00
|
|
|
|
2012-01-22 11:34:22 +00:00
|
|
|
cmd_sync_fold_range_args(State) ->
|
2012-04-15 10:40:43 +00:00
|
|
|
?LET([Tree, Range], cmd_sync_range_args(State),
|
2012-01-22 11:34:22 +00:00
|
|
|
?LET({F, Acc0}, g_fold_operation(),
|
2012-04-15 10:40:43 +00:00
|
|
|
[Tree, F, Acc0, Range])).
|
2012-01-22 11:34:22 +00:00
|
|
|
|
2012-01-07 11:43:49 +00:00
|
|
|
%% Context management
|
|
|
|
%% ----------------------------------------------------------------------
|
2012-01-21 20:16:24 +00:00
|
|
|
cleanup_test_trees(#state { open = Open, closed = Closed }) ->
|
|
|
|
[cleanup_tree(N) || N <- dict:fetch_keys(Open)],
|
|
|
|
[cleanup_tree(N) || N <- dict:fetch_keys(Closed)].
|
2012-01-07 11:43:49 +00:00
|
|
|
|
|
|
|
cleanup_tree(Tree) ->
|
2012-01-21 20:16:24 +00:00
|
|
|
case file:list_dir(Tree) of
|
|
|
|
{error, enoent} ->
|
|
|
|
ok;
|
|
|
|
{ok, FileNames} ->
|
|
|
|
[ok = file:delete(filename:join([Tree, Fname]))
|
|
|
|
|| Fname <- FileNames],
|
|
|
|
file:del_dir(Tree)
|
|
|
|
end.
|
2012-01-07 11:43:49 +00:00
|
|
|
|
|
|
|
%% Various Helper routines
|
|
|
|
%% ----------------------------------------------------------------------
|
|
|
|
|
2012-01-07 21:58:38 +00:00
|
|
|
open_dicts_with_keys(#state { open = Open}) ->
|
2012-01-21 14:15:19 +00:00
|
|
|
lists:any(fun({_, #tree { elements = D}}) ->
|
|
|
|
dict:size(D) > 0
|
|
|
|
end,
|
2012-01-07 23:01:18 +00:00
|
|
|
dict:to_list(Open)).
|
2012-01-07 21:58:38 +00:00
|
|
|
|
|
|
|
open_dicts(#state { open = Open}) ->
|
|
|
|
dict:size(Open) > 0.
|
|
|
|
|
|
|
|
closed_dicts(#state { closed = Closed}) ->
|
|
|
|
dict:size(Closed) > 0.
|
|
|
|
|
2012-04-15 10:40:43 +00:00
|
|
|
dict_range_query(Dict, Fun, Acc0, Range) ->
|
|
|
|
KVs = dict_range_query(Dict, Range),
|
2012-01-22 11:34:22 +00:00
|
|
|
lists:foldl(fun({K, V}, Acc) ->
|
|
|
|
Fun(K, V, Acc)
|
|
|
|
end,
|
|
|
|
Acc0,
|
|
|
|
KVs).
|
|
|
|
|
2012-04-15 10:40:43 +00:00
|
|
|
dict_range_query(Dict, Range) ->
|
2012-01-21 14:15:19 +00:00
|
|
|
[{K, V} || {K, V} <- dict:to_list(Dict),
|
2012-04-15 10:40:43 +00:00
|
|
|
?KEY_IN_RANGE(K, Range)].
|