diff --git a/src/hanoidb_nursery.erl b/src/hanoidb_nursery.erl index 070861a..d670961 100644 --- a/src/hanoidb_nursery.erl +++ b/src/hanoidb_nursery.erl @@ -130,15 +130,14 @@ do_add(Nursery=#nursery{log_file=File, cache=Cache, total_size=TotalSize, count= ok = file:write(File, Data), Nursery1 = do_sync(File, Nursery), - {ok, Nursery2} = do_inc_merge(Nursery1#nursery{ cache=Cache2, total_size=TotalSize + erlang:iolist_size(Data), count=Count + 1 }, 1, Top), - - if Count+1 >= ?BTREE_SIZE(?TOP_LEVEL) -> - {full, Nursery2}; - true -> - {ok, Nursery2} + case has_room(Nursery2, 1) of + true -> + {ok, Nursery2}; + false -> + {full, Nursery2} end. do_sync(File, Nursery) -> @@ -257,7 +256,7 @@ flush(Nursery=#nursery{ dir=Dir, max_level=MaxLevel, config=Config }, Top) -> hanoidb_nursery:new(Dir, MaxLevel, Config). has_room(#nursery{ count=Count }, N) -> - (Count+N) < ?BTREE_SIZE(?TOP_LEVEL). + (Count + N + 1) < ?BTREE_SIZE(?TOP_LEVEL). ensure_space(Nursery, NeededRoom, Top) -> case has_room(Nursery, NeededRoom) of diff --git a/src/hanoidb_writer.erl b/src/hanoidb_writer.erl index cd9b06b..becccc1 100644 --- a/src/hanoidb_writer.erl +++ b/src/hanoidb_writer.erl @@ -211,6 +211,12 @@ flush_nodes(#state{ nodes=[], last_node_pos=LastNodePos, last_node_size=_LastNod ok = file:close(IdxFile), {ok, State#state{ index_file=undefined, index_file_pos=undefined }}; + +flush_nodes(State=#state{ nodes=[#node{level=N, members=[{_,{Pos,_Len}}]}], last_node_pos=Pos }) + when N > 0 -> + %% Ignore this node, its stack consists of one node with one {pos,len} member + flush_nodes(State#state{ nodes=[] }); + flush_nodes(State) -> {ok, State2} = close_node(State), flush_nodes(State2).