Implement sequential/random reader API

This commit is contained in:
Kresten Krab Thorup 2012-01-23 00:49:07 +01:00
parent 739fd03715
commit 42b353ecfd
4 changed files with 23 additions and 12 deletions

View file

@ -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),

View file

@ -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]);

View file

@ -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

View file

@ -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,