Skip to content
This repository was archived by the owner on May 25, 2021. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions src/fabric_db_create.erl
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,25 @@ generate_shard_map(DbName, Options) ->
{MegaSecs, Secs, _} = now(),
Suffix = "." ++ integer_to_list(MegaSecs*1000000 + Secs),
Shards = mem3:choose_shards(DbName, [{shard_suffix,Suffix} | Options]),
case mem3_util:open_db_doc(DbName) of
{ok, Doc} ->
HashAlgo = case couch_util:get_value(hash_algo, Options) of
undefined ->
<<"crc32">>;
HashAlgo0 ->
{ok, HashAlgos} = application:get_env(mem3, hash_algorithms),
case couch_util:get_value(HashAlgo0, HashAlgos) of
undefined ->
throw({bad_request, <<"unknown hash algorithm">>});
_ ->
list_to_binary(HashAlgo0)
end
end,

Doc = case mem3_util:open_db_doc(DbName) of
{ok, Doc0} ->
% the DB already exists, and may have a different Suffix
ok;
Doc0;
{not_found, _} ->
Doc = make_document(Shards, Suffix)
make_document(Shards, Suffix, HashAlgo)
end,
{Shards, Doc}.

Expand Down Expand Up @@ -149,7 +162,7 @@ maybe_stop(W, Counters) ->
end
end.

make_document([#shard{dbname=DbName}|_] = Shards, Suffix) ->
make_document([#shard{dbname=DbName}|_] = Shards, Suffix, HashAlgo) ->
{RawOut, ByNodeOut, ByRangeOut} =
lists:foldl(fun(#shard{node=N, range=[B,E]}, {Raw, ByNode, ByRange}) ->
Range = ?l2b([couch_util:to_hex(<<B:32/integer>>), "-",
Expand All @@ -160,6 +173,7 @@ make_document([#shard{dbname=DbName}|_] = Shards, Suffix) ->
end, {[], [], []}, Shards),
#doc{id=DbName, body = {[
{<<"shard_suffix">>, Suffix},
{<<"hash_algorithm">>, HashAlgo},
{<<"changelog">>, lists:sort(RawOut)},
{<<"by_node">>, {[{K,lists:sort(V)} || {K,V} <- ByNodeOut]}},
{<<"by_range">>, {[{K,lists:sort(V)} || {K,V} <- ByRangeOut]}}
Expand Down