couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kxe...@apache.org
Subject [3/4] couch commit: updated refs/heads/master to ad867d4
Date Fri, 30 Jan 2015 18:10:51 GMT
Implement namespaced enum_docs (needed for _design_docs)

COUCHDB-1145


Project: http://git-wip-us.apache.org/repos/asf/couchdb-couch/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-couch/commit/9ea89a45
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-couch/tree/9ea89a45
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-couch/diff/9ea89a45

Branch: refs/heads/master
Commit: 9ea89a45b653370dad8fddba00b92c91f933e1db
Parents: f253bb5
Author: ILYA Khlopotov <iilyak@ca.ibm.com>
Authored: Thu Dec 18 12:20:44 2014 -0800
Committer: ILYA Khlopotov <iilyak@ca.ibm.com>
Committed: Thu Dec 18 12:33:38 2014 -0800

----------------------------------------------------------------------
 src/couch_db.erl | 49 ++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 48 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-couch/blob/9ea89a45/src/couch_db.erl
----------------------------------------------------------------------
diff --git a/src/couch_db.erl b/src/couch_db.erl
index a1c457a..0d4857f 100644
--- a/src/couch_db.erl
+++ b/src/couch_db.erl
@@ -1223,7 +1223,15 @@ enum_docs(Db, <<"_local">>, InFun, InAcc, Options) ->
     FoldFun = pipe([fun skip_deleted/4], InFun),
     {ok, _LastReduce, OutAcc} = couch_btree:fold(
         Db#db.local_tree, FoldFun, InAcc, Options),
-    {ok, 0, OutAcc}.
+    {ok, 0, OutAcc};
+enum_docs(Db, NS, InFun, InAcc, Options0) ->
+    FoldFun = pipe([
+        fun skip_deleted/4,
+        stop_on_leaving_namespace(NS)], InFun),
+    Options = set_namespace_range(Options0, NS),
+    {ok, LastReduce, OutAcc} = couch_btree:fold(
+        Db#db.id_tree, FoldFun, InAcc, Options),
+    {ok, enum_docs_reduce_to_count(LastReduce), OutAcc}.
 
 extract_namespace(Options0) ->
     case proplists:split(Options0, [namespace]) of
@@ -1396,6 +1404,28 @@ skip_deleted(traverse, LK, {Undeleted, _, _} = Reds, Acc) when Undeleted
== 0 ->
 skip_deleted(Case, A, B, C) ->
     {Case, A, B, C}.
 
+stop_on_leaving_namespace(NS) ->
+    fun
+        (visit, #full_doc_info{id = Key} = FullInfo, Reds, Acc) ->
+            case has_prefix(Key, NS) of
+                true ->
+                    {visit, FullInfo, Reds, Acc};
+                false ->
+                    {stop, FullInfo, Reds, Acc}
+            end;
+        (Case, KV, Reds, Acc) ->
+            {Case, KV, Reds, Acc}
+    end.
+
+has_prefix(Bin, Prefix) ->
+    S = byte_size(Prefix),
+    case Bin of
+        <<Prefix:S/binary, "/", _/binary>> ->
+            true;
+        _Else ->
+            false
+    end.
+
 pipe(Filters, Final) ->
     Wrap =
         fun
@@ -1417,3 +1447,20 @@ do_pipe([Filter|Rest], F0) ->
         F0(C, KV, Reds, Acc)
     end,
     do_pipe(Rest, F1).
+
+set_namespace_range(Options, undefined) -> Options;
+set_namespace_range(Options, NS) ->
+    %% FIXME depending on order we might need to swap keys
+    SK = select_gt(
+           proplists:get_value(start_key, Options, <<"">>),
+           <<NS/binary, "/">>),
+    EK = select_lt(
+           proplists:get_value(end_key, Options, <<NS/binary, "0">>),
+           <<NS/binary, "0">>),
+    [{start_key, SK}, {end_key_gt, EK}].
+
+select_gt(V1, V2) when V1 < V2 -> V2;
+select_gt(V1, _V2) -> V1.
+
+select_lt(V1, V2) when V1 > V2 -> V2;
+select_lt(V1, _V2) -> V1.


Mime
View raw message