From 2a205db9fe7c54f24a78ec1c1d498261d0d41dce Mon Sep 17 00:00:00 2001 From: Kresten Krab Thorup Date: Mon, 22 Oct 2012 10:51:08 +0200 Subject: [PATCH] Use backend API in nursery also --- src/hanoidb.hrl | 4 +++- src/hanoidb_han2_backend.erl | 3 +++ src/hanoidb_nursery.erl | 23 ++++++++++++----------- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/hanoidb.hrl b/src/hanoidb.hrl index 0264518..7bde0c6 100644 --- a/src/hanoidb.hrl +++ b/src/hanoidb.hrl @@ -57,7 +57,9 @@ max_level :: integer(), config=[] :: [{atom(), term()}], step=0 :: integer(), - merge_done=0 :: integer()}). + merge_done=0 :: integer(), + backend = hanoidb_han2_backend :: atom() + }). -type kventry() :: { key(), expvalue() } | [ kventry() ]. -type key() :: binary(). diff --git a/src/hanoidb_han2_backend.erl b/src/hanoidb_han2_backend.erl index 1cd820b..54cd989 100644 --- a/src/hanoidb_han2_backend.erl +++ b/src/hanoidb_han2_backend.erl @@ -50,6 +50,9 @@ close_batch_reader(Reader) -> open_batch_writer(Name, Options) -> hanoidb_writer:init([Name, Options]). +write_next( {Key, Value, infinity}, Writer) -> + {noreply, Writer2} = hanoidb_writer:handle_cast({add, Key, Value}, Writer), + {ok, Writer2}; write_next( {Key, Value, Expiry}, Writer) -> {noreply, Writer2} = hanoidb_writer:handle_cast({add, Key, {Value, Expiry}}, Writer), {ok, Writer2}. diff --git a/src/hanoidb_nursery.erl b/src/hanoidb_nursery.erl index 07f1093..91596cf 100644 --- a/src/hanoidb_nursery.erl +++ b/src/hanoidb_nursery.erl @@ -99,7 +99,7 @@ read_nursery_from_log(Directory, MaxLevel, Config) -> -spec do_add(#nursery{}, binary(), binary()|?TOMBSTONE, non_neg_integer() | infinity, pid()) -> {ok, #nursery{}} | {full, #nursery{}}. do_add(Nursery, Key, Value, infinity, Top) -> do_add(Nursery, Key, Value, 0, Top); -do_add(Nursery=#nursery{log_file=File, cache=Cache, total_size=TotalSize, count=Count, config=Config}, Key, Value, KeyExpiryTime, Top) -> +do_add(Nursery=#nursery{log_file=File, cache=Cache, total_size=TotalSize, count=Count, config=Config}, Key, Value, KeyExpiryTime, Top) when is_integer(KeyExpiryTime) -> DatabaseExpiryTime = hanoidb:get_opt(expiry_secs, Config), {Data, Cache2} = @@ -175,7 +175,7 @@ lookup(Key, #nursery{cache=Cache}) -> %% @end -spec finish(Nursery::#nursery{}, TopLevel::pid()) -> ok. finish(#nursery{ dir=Dir, cache=Cache, log_file=LogFile, merge_done=DoneMerge, - count=Count, config=Config }, TopLevel) -> + count=Count, config=Config, backend=Backend }, TopLevel) -> hanoidb_util:ensure_expiry(Config), @@ -189,16 +189,17 @@ finish(#nursery{ dir=Dir, cache=Cache, log_file=LogFile, merge_done=DoneMerge, N when N > 0 -> %% next, flush cache to a new BTree BTreeFileName = filename:join(Dir, "nursery.data"), - {ok, BT} = hanoidb_writer:open(BTreeFileName, [{size, ?BTREE_SIZE(?TOP_LEVEL)}, + {ok, BT} = Backend:open_batch_writer(BTreeFileName, [{size, ?BTREE_SIZE(?TOP_LEVEL)}, {compress, none} | Config]), - try - ok = gb_trees_ext:fold(fun(Key, Value, Acc) -> - ok = hanoidb_writer:add(BT, Key, Value), - Acc - end, ok, Cache) - after - ok = hanoidb_writer:close(BT) - end, + + BT3 = gb_trees_ext:fold(fun(Key, {Value,Expiry}, BT1) -> + {ok, BT2} = Backend:write_next({Key,Value,Expiry}, BT1), + BT2; + (Key, Value, BT1) -> + {ok, BT2} = Backend:write_next({Key,Value,infinity}, BT1), + BT2 + end, BT, Cache), + ok = Backend:close_batch_writer(BT3), %% Inject the B-Tree (blocking RPC) ok = hanoidb_level:inject(TopLevel, BTreeFileName),