Skip to content
This repository has been archived by the owner. It is now read-only.
Browse files
Add function to determine shard membership locally
mem3:belongs/2 allows you to determine if a given doc id belongs to a
given shard (whether a #shard{} record or just the filename of a
shard) without looking up the shard map or making any remote
  • Loading branch information
rnewson committed May 7, 2014
1 parent 2c3b9cf commit af8b04bd348ef3459ee0a7084becaae45d1fa9f2
Showing 1 changed file with 21 additions and 0 deletions.
@@ -19,6 +19,7 @@
-export([compare_nodelists/0, compare_shards/1]).
-export([quorum/1, group_by_proximity/1]).

@@ -192,6 +193,26 @@ dbname(DbName) when is_binary(DbName) ->
dbname(_) ->

%% @doc Determine if DocId belongs in shard (identified by record or filename)
belongs(#shard{}=Shard, DocId) when is_binary(DocId) ->
[Begin, End] = range(Shard),
belongs(Begin, End, DocId);
belongs(<<"shards/", _/binary>> = ShardName, DocId) when is_binary(DocId) ->
[Begin, End] = range(ShardName),
belongs(Begin, End, DocId);
belongs(DbName, DocId) when is_binary(DbName), is_binary(DocId) ->

belongs(Begin, End, DocId) ->
HashKey = mem3_util:hash(DocId),
Begin =< HashKey andalso HashKey =< End.

range(#shard{range = Range}) ->
range(<<"shards/", Start:8/binary, "-", End:8/binary, "/", _/binary>>) ->

nodes_in_zone(Nodes, Zone) ->
[Node || Node <- Nodes, Zone == mem3:node_info(Node, <<"zone">>)].

0 comments on commit af8b04b

Please sign in to comment.