You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by ra...@apache.org on 2011/07/30 02:04:22 UTC

svn commit: r1152397 - /couchdb/trunk/src/couchdb/couch_btree.erl

Author: randall
Date: Sat Jul 30 00:04:22 2011
New Revision: 1152397

URL: http://svn.apache.org/viewvc?rev=1152397&view=rev
Log:
Call traversal handlers in btree folds

Previously the fold function was only invoked for leafs.
With this change it's possible to define a fold function which is called
for inner nodes and can return a tuple {skip, Acc} in order to skip
exploration of the branch.

Modified:
    couchdb/trunk/src/couchdb/couch_btree.erl

Modified: couchdb/trunk/src/couchdb/couch_btree.erl
URL: http://svn.apache.org/viewvc/couchdb/trunk/src/couchdb/couch_btree.erl?rev=1152397&r1=1152396&r2=1152397&view=diff
==============================================================================
--- couchdb/trunk/src/couchdb/couch_btree.erl (original)
+++ couchdb/trunk/src/couchdb/couch_btree.erl Sat Jul 30 00:04:22 2011
@@ -107,9 +107,17 @@ size(#btree{root = {_P, _Red, Size}}) ->
 
 % wraps a 2 arity function with the proper 3 arity function
 convert_fun_arity(Fun) when is_function(Fun, 2) ->
-    fun(KV, _Reds, AccIn) -> Fun(KV, AccIn) end;
+    fun
+        (visit, KV, _Reds, AccIn) -> Fun(KV, AccIn);
+        (traverse, _K, _Red, AccIn) -> {ok, AccIn}
+    end;
 convert_fun_arity(Fun) when is_function(Fun, 3) ->
-    Fun.    % Already arity 3
+    fun
+        (visit, KV, Reds, AccIn) -> Fun(KV, Reds, AccIn);
+        (traverse, _K, _Red, AccIn) -> {ok, AccIn}
+    end;
+convert_fun_arity(Fun) when is_function(Fun, 4) ->
+    Fun.    % Already arity 4
 
 make_key_in_end_range_function(#btree{less=Less}, fwd, Options) ->
     case couch_util:get_value(end_key_gt, Options) of
@@ -624,13 +632,18 @@ stream_node(Bt, Reds, Node, InRange, Dir
 
 stream_kp_node(_Bt, _Reds, [], _InRange, _Dir, _Fun, Acc) ->
     {ok, Acc};
-stream_kp_node(Bt, Reds, [{_Key, Node} | Rest], InRange, Dir, Fun, Acc) ->
+stream_kp_node(Bt, Reds, [{Key, Node} | Rest], InRange, Dir, Fun, Acc) ->
     Red = element(2, Node),
-    case stream_node(Bt, Reds, Node, InRange, Dir, Fun, Acc) of
+    case Fun(traverse, Key, Red, Acc) of
     {ok, Acc2} ->
-        stream_kp_node(Bt, [Red | Reds], Rest, InRange, Dir, Fun, Acc2);
-    {stop, LastReds, Acc2} ->
-        {stop, LastReds, Acc2}
+        case stream_node(Bt, Reds, Node, InRange, Dir, Fun, Acc2) of
+        {ok, Acc3} ->
+            stream_kp_node(Bt, [Red | Reds], Rest, InRange, Dir, Fun, Acc3);
+        {stop, LastReds, Acc3} ->
+            {stop, LastReds, Acc3}
+        end;
+    {skip, Acc2} ->
+        stream_kp_node(Bt, [Red | Reds], Rest, InRange, Dir, Fun, Acc2)
     end.
 
 drop_nodes(_Bt, Reds, _StartKey, []) ->
@@ -694,7 +707,7 @@ stream_kv_node2(Bt, Reds, PrevKVs, [{K,V
         {stop, {PrevKVs, Reds}, Acc};
     true ->
         AssembledKV = assemble(Bt, K, V),
-        case Fun(AssembledKV, {PrevKVs, Reds}, Acc) of
+        case Fun(visit, AssembledKV, {PrevKVs, Reds}, Acc) of
         {ok, Acc2} ->
             stream_kv_node2(Bt, Reds, [AssembledKV | PrevKVs], RestKVs, InRange, Dir, Fun, Acc2);
         {stop, Acc2} ->