From 42b353ecfd47b62d0221fa8dd1613cc5dfabc82d Mon Sep 17 00:00:00 2001 From: Kresten Krab Thorup Date: Mon, 23 Jan 2012 00:49:07 +0100 Subject: [PATCH] Implement sequential/random reader API --- src/lsm_btree_level.erl | 14 +++++++------- src/lsm_btree_merger.erl | 4 ++-- src/lsm_btree_reader.erl | 15 +++++++++++++-- src/lsm_btree_writer.erl | 2 +- 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/lsm_btree_level.erl b/src/lsm_btree_level.erl index f25ab13..5d28932 100644 --- a/src/lsm_btree_level.erl +++ b/src/lsm_btree_level.erl @@ -119,14 +119,14 @@ initialize(State) -> file:delete(BFileName), ok = file:rename(CFileName, AFileName), - {ok, BT} = lsm_btree_reader:open(CFileName), + {ok, BT} = lsm_btree_reader:open(CFileName, random), main_loop(State#state{ a= BT, b=undefined }); {error, enoent} -> case file:read_file_info(BFileName) of {ok, _} -> - {ok, BT1} = lsm_btree_reader:open(AFileName), - {ok, BT2} = lsm_btree_reader:open(BFileName), + {ok, BT1} = lsm_btree_reader:open(AFileName, random), + {ok, BT2} = lsm_btree_reader:open(BFileName, random), check_begin_merge_then_loop(State#state{ a=BT1, b=BT2 }); @@ -134,7 +134,7 @@ initialize(State) -> case file:read_file_info(AFileName) of {ok, _} -> - {ok, BT1} = lsm_btree_reader:open(AFileName), + {ok, BT1} = lsm_btree_reader:open(AFileName, random), main_loop(State#state{ a=BT1 }); {error, enoent} -> @@ -173,7 +173,7 @@ main_loop(State = #state{ next=Next }) -> SetPos = #state.b end, ok = file:rename(FileName, ToFileName), - {ok, BT} = lsm_btree_reader:open(ToFileName), + {ok, BT} = lsm_btree_reader:open(ToFileName, random), reply(From, ok), check_begin_merge_then_loop(setelement(SetPos, State, BT)); @@ -276,7 +276,7 @@ main_loop(State = #state{ next=Next }) -> % then, rename C to A, and open it AFileName = filename("A",State2), ok = file:rename(CFileName, AFileName), - {ok, BT} = lsm_btree_reader:open(AFileName), + {ok, BT} = lsm_btree_reader:open(AFileName, random), main_loop(State2#state{ a=BT, b=undefined, merge_pid=undefined }); @@ -398,7 +398,7 @@ start_range_fold(FileName, WorkerPID, FromKey, ToKey) -> PID = proc_lib:spawn( fun() -> erlang:link(WorkerPID), - {ok, File} = lsm_btree_reader:open(FileName), + {ok, File} = lsm_btree_reader:open(FileName, sequential), do_range_fold(File, WorkerPID, self(), FromKey, ToKey), erlang:unlink(WorkerPID), diff --git a/src/lsm_btree_merger.erl b/src/lsm_btree_merger.erl index 58cab47..51af987 100644 --- a/src/lsm_btree_merger.erl +++ b/src/lsm_btree_merger.erl @@ -16,8 +16,8 @@ -define(LOCAL_WRITER, true). merge(A,B,C, Size, IsLastLevel) -> - {ok, BT1} = lsm_btree_reader:open(A), - {ok, BT2} = lsm_btree_reader:open(B), + {ok, BT1} = lsm_btree_reader:open(A, sequential), + {ok, BT2} = lsm_btree_reader:open(B, sequential), case ?LOCAL_WRITER of true -> {ok, Out} = lsm_btree_writer:init([C, Size]); diff --git a/src/lsm_btree_reader.erl b/src/lsm_btree_reader.erl index b00bb9e..894d686 100644 --- a/src/lsm_btree_reader.erl +++ b/src/lsm_btree_reader.erl @@ -3,15 +3,26 @@ -include_lib("kernel/include/file.hrl"). -include("lsm_btree.hrl"). --export([open/1,close/1,lookup/2,fold/3,range_fold/5]). +-export([open/1, open/2,close/1,lookup/2,fold/3,range_fold/5]). -export([first_node/1,next_node/1]). -record(node, { level, members=[] }). -record(index, {file, root, bloom}). open(Name) -> + open(Name, random). - {ok, File} = file:open(Name, [raw,read,read_ahead,binary]), +%% this is how to open a btree for sequential scanning (merge, fold) +open(Name, sequential) -> + {ok, File} = file:open(Name, [raw,read,{read_ahead, 1024 * 512},binary]), + open2(Name, File); + +%% this is how to open a btree for random access +open(Name, random) -> + {ok, File} = file:open(Name, [raw,read,binary]), + open2(Name, File). + +open2(Name, File) -> {ok, FileInfo} = file:read_file_info(Name), %% read root position diff --git a/src/lsm_btree_writer.erl b/src/lsm_btree_writer.erl index 2998a8d..2502a13 100644 --- a/src/lsm_btree_writer.erl +++ b/src/lsm_btree_writer.erl @@ -56,7 +56,7 @@ init([Name,Size]) -> % io:format("got name: ~p~n", [Name]), case file:open( lsm_btree_util:index_file_name(Name), - [raw, exclusive, write, delayed_write]) of + [raw, exclusive, write, {delayed_write, 512 * 1024, 2000}]) of {ok, IdxFile} -> {ok, BloomFilter} = ebloom:new(erlang:min(Size,16#ffffffff), 0.01, 123), {ok, #state{ name=Name,