This is an automated email from the ASF dual-hosted git repository.

kocolosk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit 5fa3c43f2c7313b18a63c0100ab2b1843bd8ab94
Author: Adam Kocoloski <[email protected]>
AuthorDate: Wed Jun 6 18:10:12 2018 -0400

    Use finalize operation to simplify _stats
    
    Rather than packing the stats into an ejson object on each write
    we use a more compact tuple format on disk and then turn it into
    ejson at query time.
---
 src/couch/src/couch_query_servers.erl | 31 +++++++++++++++++--------------
 src/fabric/src/fabric_view.erl        |  2 +-
 2 files changed, 18 insertions(+), 15 deletions(-)

diff --git a/src/couch/src/couch_query_servers.erl 
b/src/couch/src/couch_query_servers.erl
index 02d90f1..fe04533 100644
--- a/src/couch/src/couch_query_servers.erl
+++ b/src/couch/src/couch_query_servers.erl
@@ -17,7 +17,7 @@
 -export([reduce/3, rereduce/3,validate_doc_update/5]).
 -export([filter_docs/5]).
 -export([filter_view/3]).
--export([finalize/1]).
+-export([finalize/2]).
 -export([rewrite/3]).
 
 -export([with_ddoc_proc/2, proc_prompt/2, ddoc_prompt/3, ddoc_proc_prompt/3, 
json_doc/1]).
@@ -87,15 +87,16 @@ group_reductions_results(List) ->
      [Heads | group_reductions_results(Tails)]
     end.
 
-finalize(Reductions) ->
-    {ok, lists:map(fun(Reduction) ->
-        case hyper:is_hyper(Reduction) of
-            true ->
-                round(hyper:card(Reduction));
-            false ->
-                Reduction
-        end
-    end, Reductions)}.
+finalize(<<"_approx_count_distinct">>, Reduction) ->
+    true = hyper:is_hyper(Reduction),
+    {ok, round(hyper:card(Reduction))};
+finalize(<<"_stats">>, {_, _, _, _, _} = Unpacked) ->
+    {ok, pack_stats(Unpacked)};
+finalize(<<"_stats">>, {Packed}) ->
+    % Legacy code path before we had the finalize operation
+    {ok, {Packed}};
+finalize(_RedSrc, Reduction) ->
+    {ok, Reduction}.
 
 rereduce(_Lang, [], _ReducedValues) ->
     {ok, []};
@@ -250,11 +251,11 @@ sum_arrays(Else, _) ->
     throw_sum_error(Else).
 
 builtin_stats(_, []) ->
-    {[{sum,0}, {count,0}, {min,0}, {max,0}, {sumsqr,0}]};
+    {0, 0, 0, 0, 0};
 builtin_stats(_, [[_,First]|Rest]) ->
-    Unpacked = lists:foldl(fun([_Key, Value], Acc) -> stat_values(Value, Acc) 
end,
-                           build_initial_accumulator(First), Rest),
-    pack_stats(Unpacked).
+    lists:foldl(fun([_Key, Value], Acc) ->
+        stat_values(Value, Acc)
+    end, build_initial_accumulator(First), Rest).
 
 stat_values(Value, Acc) when is_list(Value), is_list(Acc) ->
     lists:zipwith(fun stat_values/2, Value, Acc);
@@ -281,6 +282,8 @@ build_initial_accumulator(L) when is_list(L) ->
     [build_initial_accumulator(X) || X <- L];
 build_initial_accumulator(X) when is_number(X) ->
     {X, 1, X, X, X*X};
+build_initial_accumulator({_, _, _, _, _} = AlreadyUnpacked) ->
+    AlreadyUnpacked;
 build_initial_accumulator({Props}) ->
     unpack_stats({Props});
 build_initial_accumulator(Else) ->
diff --git a/src/fabric/src/fabric_view.erl b/src/fabric/src/fabric_view.erl
index 4d8d0e9..69f4290 100644
--- a/src/fabric/src/fabric_view.erl
+++ b/src/fabric/src/fabric_view.erl
@@ -230,7 +230,7 @@ get_next_row(#collector{reducer = RedSrc} = St) when RedSrc 
=/= undefined ->
         end, Counters0, Records),
         Wrapped = [[V] || #view_row{value=V} <- Records],
         {ok, [Reduced]} = couch_query_servers:rereduce(Lang, [RedSrc], 
Wrapped),
-        {ok, [Finalized]} = couch_query_servers:finalize([Reduced]),
+        {ok, Finalized} = couch_query_servers:finalize(RedSrc, Reduced),
         NewSt = St#collector{keys=RestKeys, rows=NewRowDict, 
counters=Counters},
         {#view_row{key=Key, id=reduced, value=Finalized}, NewSt};
     error ->

-- 
To stop receiving notification emails like this one, please contact
[email protected].

Reply via email to