couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rnew...@apache.org
Subject mem3 commit: updated refs/heads/1843-feature-bigcouch to 87d9bed
Date Wed, 07 May 2014 14:18:08 GMT
Repository: couchdb-mem3
Updated Branches:
  refs/heads/1843-feature-bigcouch 2c3b9cf45 -> 87d9bedcd


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
calls.


Project: http://git-wip-us.apache.org/repos/asf/couchdb-mem3/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-mem3/commit/87d9bedc
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-mem3/tree/87d9bedc
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-mem3/diff/87d9bedc

Branch: refs/heads/1843-feature-bigcouch
Commit: 87d9bedcdf38db970508088bda34757129f1b139
Parents: 2c3b9cf
Author: Robert Newson <rnewson@apache.org>
Authored: Wed May 7 14:48:25 2014 +0100
Committer: Robert Newson <rnewson@apache.org>
Committed: Wed May 7 15:14:59 2014 +0100

----------------------------------------------------------------------
 src/mem3.erl | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-mem3/blob/87d9bedc/src/mem3.erl
----------------------------------------------------------------------
diff --git a/src/mem3.erl b/src/mem3.erl
index c9b4793..4b1b2ab 100644
--- a/src/mem3.erl
+++ b/src/mem3.erl
@@ -19,6 +19,7 @@
 -export([compare_nodelists/0, compare_shards/1]).
 -export([quorum/1, group_by_proximity/1]).
 -export([live_shards/2]).
+-export([belongs/2]).
 
 -include_lib("mem3/include/mem3.hrl").
 -include_lib("couch/include/couch_db.hrl").
@@ -192,6 +193,26 @@ dbname(DbName) when is_binary(DbName) ->
 dbname(_) ->
     erlang:error(badarg).
 
+%% @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) ->
+    true.
+
+belongs(Begin, End, DocId) ->
+    HashKey = mem3_util:hash(DocId),
+    HashKey =< Begin andalso End =< HashKey.
+
+range(#shard{range = Range}) ->
+    Range;
+range(<<"shards/", Start:8/binary, "-", End:8/binary, "/", _/binary>>) ->
+    [httpd_util:hexlist_to_integer(binary_to_list(Start)),
+     httpd_util:hexlist_to_integer(binary_to_list(End))].
+
 nodes_in_zone(Nodes, Zone) ->
     [Node || Node <- Nodes, Zone == mem3:node_info(Node, <<"zone">>)].
 


Mime
View raw message