This is an automated email from the ASF dual-hosted git repository. rnewson pushed a commit to branch user-partitioned-dbs-wip in repository https://gitbox.apache.org/repos/asf/couchdb.git
commit 2b63513f50a364a196e3e5ba9ceb8b325cfd4517 Author: Robert Newson <[email protected]> AuthorDate: Wed Jul 4 17:15:16 2018 +0100 introduce mem3_util:docid_hash/1 and docid_hash/2 mem3_util:docid_hash/1 is identical to mem3_util:hash/1 mem3_util:docid_hash/2 allows the user to control the result with specially-formatted doc ids. --- src/mem3/src/mem3.erl | 2 +- src/mem3/src/mem3_shards.erl | 4 ++-- src/mem3/src/mem3_util.erl | 15 +++++++++++++++ 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/mem3/src/mem3.erl b/src/mem3/src/mem3.erl index 0e5eabf..ca083c4 100644 --- a/src/mem3/src/mem3.erl +++ b/src/mem3/src/mem3.erl @@ -239,7 +239,7 @@ belongs(DbName, DocId) when is_binary(DbName), is_binary(DocId) -> true. belongs(Begin, End, DocId) -> - HashKey = mem3_util:hash(DocId), + HashKey = mem3_util:docid_hash(DocId), Begin =< HashKey andalso HashKey =< End. range(#shard{range = Range}) -> diff --git a/src/mem3/src/mem3_shards.erl b/src/mem3/src/mem3_shards.erl index da3b69a..0559149 100644 --- a/src/mem3/src/mem3_shards.erl +++ b/src/mem3/src/mem3_shards.erl @@ -67,7 +67,7 @@ for_docid(DbName, DocId) -> for_docid(DbName, DocId, []). for_docid(DbName, DocId, Options) -> - HashKey = mem3_util:hash(DocId), + HashKey = mem3_util:docid_hash(DocId), ShardHead = #shard{ dbname = DbName, range = ['$1', '$2'], @@ -397,7 +397,7 @@ load_shards_from_db(ShardDb, DbName) -> load_shards_from_disk(DbName, DocId)-> Shards = load_shards_from_disk(DbName), - HashKey = mem3_util:hash(DocId), + HashKey = mem3_util:docid_hash(DocId), [S || S <- Shards, in_range(S, HashKey)]. in_range(Shard, HashKey) -> diff --git a/src/mem3/src/mem3_util.erl b/src/mem3/src/mem3_util.erl index 0b69d79..fc6123d 100644 --- a/src/mem3/src/mem3_util.erl +++ b/src/mem3/src/mem3_util.erl @@ -16,6 +16,7 @@ n_val/2, to_atom/1, to_integer/1, write_db_doc/1, delete_db_doc/1, shard_info/1, ensure_exists/1, open_db_doc/1]). -export([is_deleted/1, rotate_list/2]). +-export([docid_hash/1, docid_hash/2]). %% do not use outside mem3. -export([build_ordered_shards/2, downcast/1]). @@ -34,6 +35,20 @@ hash(Item) when is_binary(Item) -> hash(Item) -> erlang:crc32(term_to_binary(Item)). + +docid_hash(DocId) when is_binary(DocId) -> + docid_hash(DocId, []). + +docid_hash(DocId, Options) when is_binary(DocId), is_list(Options) -> + Data = case lists:member(partitioned, Options) of + true -> + hd(binary:split(DocId, <<":">>)); + false -> + DocId + end, + erlang:crc32(Data). + + name_shard(Shard) -> name_shard(Shard, "").
