Skip to content

Commit

Permalink
share segment store accross all nodes in hashtree_tree
Browse files Browse the repository at this point in the history
this ensures that we don't create an unbounded number of segment
stores (leveldb databases), instead only creating one.
  • Loading branch information
jrwest committed Nov 12, 2013
1 parent 68c7bdf commit 4c21b3a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/hashtree.erl
Expand Up @@ -103,6 +103,7 @@

-export([new/0,
new/2,
new/3,
insert/3,
insert/4,
delete/2,
Expand Down
28 changes: 19 additions & 9 deletions src/hashtree_tree.erl
Expand Up @@ -445,16 +445,27 @@ lookup_node(NodeName, Tree=#hashtree_tree{nodes=Nodes}) ->
end.

%% @private
create_node(?ROOT, Tree) ->
NodeId = node_id(?ROOT, Tree),
NodePath = node_path(Tree),
NumSegs = node_num_segs(?ROOT),
Width = node_width(?ROOT),
Opts = [{segment_path, NodePath}, {segments, NumSegs}, {width, Width}],
%% destroy any data that previously existed because its lingering from
%% a tree that was not properly destroyed
ok = hashtree:destroy(NodePath),
Node = hashtree:new(NodeId, Opts),
set_node(?ROOT, Node, Tree);
create_node([], Tree) ->
create_node(?ROOT, Tree);
create_node(NodeName, Tree) ->
NodeId = node_id(NodeName, Tree),
NodePath = node_path(NodeId, Tree),
RootNode = get_node(?ROOT, Tree),
NumSegs = node_num_segs(NodeName),
Width = node_width(NodeName),
Opts = [{segment_path, NodePath}, {segments, NumSegs}, {width, Width}],

%% remove any existing node data in case of crash
ok = hashtree:destroy(NodePath),
Node = hashtree:new(NodeId, Opts),
Opts = [{segments, NumSegs}, {width, Width}],
%% share segment store accross all nodes
Node = hashtree:new(NodeId, RootNode, Opts),
set_node(NodeName, Node, Tree).

%% @private
Expand Down Expand Up @@ -494,9 +505,8 @@ node_num_segs(NodeName) ->
end.

%% @private
node_path({_, <<NodeInt:176/integer>>}, #hashtree_tree{data_root=DataRoot}) ->
NodeMD5 = riak_core_util:integer_to_list(NodeInt, 16),
filename:join(DataRoot, NodeMD5).
node_path(#hashtree_tree{data_root=DataRoot}) ->
DataRoot.

%% @private
node_key(NodeName, #hashtree_tree{id=TreeId}) ->
Expand Down

0 comments on commit 4c21b3a

Please sign in to comment.