summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Newson <rnewson@apache.org>2018-07-04 17:15:16 +0100
committerRobert Newson <rnewson@apache.org>2018-07-10 15:30:14 +0100
commit2b63513f50a364a196e3e5ba9ceb8b325cfd4517 (patch)
treeb64ad6899df8f28c5b023819a2c9fc821c91dd6b
parent79834c20e8f07d9cf418de9fa2b6d949e6790d67 (diff)
downloadcouchdb-2b63513f50a364a196e3e5ba9ceb8b325cfd4517.tar.gz
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.
-rw-r--r--src/mem3/src/mem3.erl2
-rw-r--r--src/mem3/src/mem3_shards.erl4
-rw-r--r--src/mem3/src/mem3_util.erl15
3 files changed, 18 insertions, 3 deletions
diff --git a/src/mem3/src/mem3.erl b/src/mem3/src/mem3.erl
index 0e5eabfe3..ca083c4d4 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 da3b69a61..0559149cc 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 0b69d790d..fc6123d39 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, "").