From 3140f9f281d4e995f1ae062bc306adad4f6b1422 Mon Sep 17 00:00:00 2001 From: Gregory Burd Date: Mon, 25 Jun 2012 11:55:07 +0530 Subject: [PATCH] Cleanup --- src/hanoidb_reader.erl | 15 +++++++++------ src/hanoidb_writer.erl | 7 +++---- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/hanoidb_reader.erl b/src/hanoidb_reader.erl index 60c7b63..1a58d7c 100644 --- a/src/hanoidb_reader.erl +++ b/src/hanoidb_reader.erl @@ -252,7 +252,6 @@ lookup_node(File,FromKey,#node{members=Members,level=N},_) -> end. - first_node(#index{file=File}) -> case read_node(File, ?FIRST_BLOCK_POS) of {ok, #node{level=0, members=Members}} -> @@ -370,21 +369,25 @@ find_start(K, KVs) -> -read_node(File,{Pos,Size}) -> -% error_logger:info_msg("read_node ~p ~p ~p~n", [File, Pos, Size]), +read_node(File, {Pos, Size}) -> +% error_logger:info_msg("read_node ~p ~p ~p~n", [File, Pos, Size]), {ok, <<_:32, Level:16/unsigned, Data/binary>>} = file:pread(File, Pos, Size), hanoidb_util:decode_index_node(Level, Data); -read_node(File,Pos) -> +read_node(File, Pos) -> +% error_logger:info_msg("read_node ~p ~p~n", [File, Pos]), {ok, Pos} = file:position(File, Pos), Result = read_node(File), -% error_logger:info_msg("decoded ~p ~p~n", [Pos, Result]), +% error_logger:info_msg("decoded ~p ~p~n", [Pos, Result]), Result. read_node(File) -> +% error_logger:info_msg("read_node ~p~n", [File]), {ok, <>} = file:read(File, 6), +% error_logger:info_msg("decoded ~p ~p~n", [Len, Level]), case Len of - 0 -> eof; + 0 -> + eof; _ -> {ok, Data} = file:read(File, Len-2), hanoidb_util:decode_index_node(Level, Data) diff --git a/src/hanoidb_writer.erl b/src/hanoidb_writer.erl index 6345d99..2fc5088 100644 --- a/src/hanoidb_writer.erl +++ b/src/hanoidb_writer.erl @@ -93,7 +93,7 @@ init([Name, Options]) -> case do_open(Name, Options, [exclusive]) of {ok, IdxFile} -> - file:write(IdxFile, ?FILE_FORMAT), + ok = file:write(IdxFile, ?FILE_FORMAT), Bloom = bloom:new(erlang:min(Size, 16#ffffffff), 0.001), BlockSize = hanoidb:get_opt(block_size, Options, ?NODE_SIZE), {ok, #state{ name=Name, @@ -268,10 +268,9 @@ add_record(Level, Key, Value, #state{ nodes=[ #node{level=Level, members=List, s {ok, State2} end. -close_node(#state{nodes=[#node{ level=Level, members=NodeMembers }|RestNodes], compress=Compress} = State) -> +close_node(#state{nodes=[#node{ level=Level, members=NodeMembers }|RestNodes], compress=Compress, index_file_pos=NodePos} = State) -> OrderedMembers = lists:reverse(NodeMembers), {ok, BlockData} = hanoidb_util:encode_index_node(OrderedMembers, Compress), - NodePos = State#state.index_file_pos, BlockSize = erlang:iolist_size(BlockData), Data = [ <<(BlockSize+2):32/unsigned, Level:16/unsigned>> | BlockData ], @@ -280,7 +279,7 @@ close_node(#state{nodes=[#node{ level=Level, members=NodeMembers }|RestNodes], c ok = file:write(State#state.index_file, Data), {FirstKey, _} = hd(OrderedMembers), - add_record(Level+1, FirstKey, {NodePos, DataSize}, + add_record(Level + 1, FirstKey, {NodePos, DataSize}, State#state{ nodes = RestNodes, index_file_pos = NodePos + DataSize, last_node_pos = NodePos,