couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kxe...@apache.org
Subject [1/2] couch-mrview commit: updated refs/heads/master to 58a0de5
Date Fri, 25 Sep 2015 18:16:42 GMT
Repository: couchdb-couch-mrview
Updated Branches:
  refs/heads/master 52cc9c5f1 -> 58a0de512


Improve validation of group and group_level view parameters.

Jira: COUCH-2824

Parameters group and group_level currently were
overriding each other such that last one in the list "won".

Currently behavior has changed such that :

  * group_level overrides group parameter if both are set
  * but if group=false and group_level>0, throw 400

That happens regardless of the order of those arguments, or
how many times each one was specified in the parameter list.


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

Branch: refs/heads/master
Commit: a25d590d07128b78632095e191c47f2f815ab6d7
Parents: dfa6f7e
Author: Nick Vatamaniuc <vatamane@gmail.com>
Authored: Fri Sep 25 13:44:10 2015 -0400
Committer: Nick Vatamaniuc <vatamane@gmail.com>
Committed: Fri Sep 25 14:05:03 2015 -0400

----------------------------------------------------------------------
 include/couch_mrview.hrl         |  1 +
 src/couch_mrview_http.erl        | 10 ++++-----
 src/couch_mrview_util.erl        | 20 +++++++++++++++---
 test/couch_mrview_http_tests.erl | 27 ++++++++++++++++++++++++
 test/couch_mrview_util_tests.erl | 39 +++++++++++++++++++++++++++++++++++
 5 files changed, 88 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-couch-mrview/blob/a25d590d/include/couch_mrview.hrl
----------------------------------------------------------------------
diff --git a/include/couch_mrview.hrl b/include/couch_mrview.hrl
index c176839..7dc7190 100644
--- a/include/couch_mrview.hrl
+++ b/include/couch_mrview.hrl
@@ -76,6 +76,7 @@
     limit = 16#10000000,
     skip = 0,
     group_level = 0,
+    group = undefined,
     stale = false,
     multi_get = false,
     inclusive_end = true,

http://git-wip-us.apache.org/repos/asf/couchdb-couch-mrview/blob/a25d590d/src/couch_mrview_http.erl
----------------------------------------------------------------------
diff --git a/src/couch_mrview_http.erl b/src/couch_mrview_http.erl
index c6c1ecf..65e3197 100644
--- a/src/couch_mrview_http.erl
+++ b/src/couch_mrview_http.erl
@@ -432,10 +432,11 @@ parse_params(Props, Keys) ->
 
 
 parse_params(Props, Keys, #mrargs{}=Args0) ->
-    Args = Args0#mrargs{keys=Keys},
+    % group_level set to undefined to detect if explicitly set by user
+    Args1 = Args0#mrargs{keys=Keys, group=undefined, group_level=undefined},
     lists:foldl(fun({K, V}, Acc) ->
         parse_param(K, V, Acc)
-    end, Args, Props).
+    end, Args1, Props).
 
 
 parse_param(Key, Val, Args) when is_binary(Key) ->
@@ -483,10 +484,7 @@ parse_param(Key, Val, Args) ->
         "skip" ->
             Args#mrargs{skip=parse_pos_int(Val)};
         "group" ->
-            case parse_boolean(Val) of
-                true -> Args#mrargs{group_level=exact};
-                _ -> Args#mrargs{group_level=0}
-            end;
+            Args#mrargs{group=parse_boolean(Val)};
         "group_level" ->
             Args#mrargs{group_level=parse_pos_int(Val)};
         "inclusive_end" ->

http://git-wip-us.apache.org/repos/asf/couchdb-couch-mrview/blob/a25d590d/src/couch_mrview_util.erl
----------------------------------------------------------------------
diff --git a/src/couch_mrview_util.erl b/src/couch_mrview_util.erl
index f41aed6..78d9a9c 100644
--- a/src/couch_mrview_util.erl
+++ b/src/couch_mrview_util.erl
@@ -439,6 +439,7 @@ fold_reduce({NthRed, Lang, View}, Fun,  Acc, Options) ->
 
 
 validate_args(Args) ->
+    GroupLevel = determine_group_level(Args),
     Reduce = Args#mrargs.reduce,
     case Reduce == undefined orelse is_boolean(Reduce) of
         true -> ok;
@@ -450,7 +451,7 @@ validate_args(Args) ->
         _ -> ok
     end,
 
-    case {Args#mrargs.view_type, Args#mrargs.group_level, Args#mrargs.keys} of
+    case {Args#mrargs.view_type, GroupLevel, Args#mrargs.keys} of
         {red, exact, _} -> ok;
         {red, _, KeyList} when is_list(KeyList) ->
             Msg = <<"Multi-key fetchs for reduce views must use `group=true`">>,
@@ -502,7 +503,7 @@ validate_args(Args) ->
         _ -> ok
     end,
 
-    case {Args#mrargs.view_type, Args#mrargs.group_level} of
+    case {Args#mrargs.view_type, GroupLevel} of
         {red, exact} -> ok;
         {_, 0} -> ok;
         {red, Int} when is_integer(Int), Int >= 0 -> ok;
@@ -550,10 +551,23 @@ validate_args(Args) ->
 
     Args#mrargs{
         start_key_docid=SKDocId,
-        end_key_docid=EKDocId
+        end_key_docid=EKDocId,
+        group_level=GroupLevel
     }.
 
 
+determine_group_level(#mrargs{group=undefined, group_level=undefined}) ->
+    0;
+determine_group_level(#mrargs{group=false, group_level=undefined}) ->
+    0;
+determine_group_level(#mrargs{group=false, group_level=Level}) when Level > 0 ->
+    mrverror(<<"Can't specify group=false and group_level>0 at the same time">>);
+determine_group_level(#mrargs{group=true, group_level=undefined}) ->
+    exact;
+determine_group_level(#mrargs{group_level=GroupLevel}) ->
+    GroupLevel.
+
+
 check_range(#mrargs{start_key=undefined}, _Cmp) ->
     ok;
 check_range(#mrargs{end_key=undefined}, _Cmp) ->

http://git-wip-us.apache.org/repos/asf/couchdb-couch-mrview/blob/a25d590d/test/couch_mrview_http_tests.erl
----------------------------------------------------------------------
diff --git a/test/couch_mrview_http_tests.erl b/test/couch_mrview_http_tests.erl
new file mode 100644
index 0000000..0b01450
--- /dev/null
+++ b/test/couch_mrview_http_tests.erl
@@ -0,0 +1,27 @@
+% Licensed under the Apache License, Version 2.0 (the "License"); you may not
+% use this file except in compliance with the License. You may obtain a copy of
+% the License at
+%
+%   http://www.apache.org/licenses/LICENSE-2.0
+%
+% Unless required by applicable law or agreed to in writing, software
+% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+% License for the specific language governing permissions and limitations under
+% the License.
+
+-module(couch_mrview_http_tests).
+
+-include_lib("couch/include/couch_eunit.hrl").
+-include_lib("couch_mrview/include/couch_mrview.hrl").
+
+
+mrview_http_test_() ->
+    [
+         ?_assertEqual(#mrargs{group_level=undefined, group=true},
+                       couch_mrview_http:parse_params([{"group", "true"}],
+                                            undefined, #mrargs{})),
+
+         ?_assertEqual(#mrargs{group_level=1, group=undefined},
+                       couch_mrview_http:parse_params([{"group_level", "1"}]))
+    ].

http://git-wip-us.apache.org/repos/asf/couchdb-couch-mrview/blob/a25d590d/test/couch_mrview_util_tests.erl
----------------------------------------------------------------------
diff --git a/test/couch_mrview_util_tests.erl b/test/couch_mrview_util_tests.erl
new file mode 100644
index 0000000..7046c9b
--- /dev/null
+++ b/test/couch_mrview_util_tests.erl
@@ -0,0 +1,39 @@
+% Licensed under the Apache License, Version 2.0 (the "License"); you may not
+% use this file except in compliance with the License. You may obtain a copy of
+% the License at
+%
+%   http://www.apache.org/licenses/LICENSE-2.0
+%
+% Unless required by applicable law or agreed to in writing, software
+% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+% License for the specific language governing permissions and limitations under
+% the License.
+
+-module(couch_mrview_util_tests).
+
+-include_lib("couch/include/couch_eunit.hrl").
+-include_lib("couch_mrview/include/couch_mrview.hrl").
+
+
+
+couch_mrview_util_test_() ->
+    [
+         ?_assertEqual(0, validate_group_level(undefined, undefined)),
+         ?_assertEqual(exact, validate_group_level(true, undefined)),
+         ?_assertEqual(0, validate_group_level(false, undefined)),
+         ?_assertEqual(1, validate_group_level(undefined, 1)),
+         ?_assertEqual(0, validate_group_level(true, 0)),
+         ?_assertEqual(0, validate_group_level(undefined, 0)),
+         ?_assertEqual(1, validate_group_level(true, 1)),
+         ?_assertEqual(0, validate_group_level(false, 0)),
+         ?_assertThrow({query_parse_error,
+              <<"Can't specify group=false and group_level>0 at the same time">>},
+              validate_group_level(false,1))
+    ].
+
+validate_group_level(Group, GroupLevel) ->
+    Args0 = #mrargs{group=Group, group_level=GroupLevel, view_type=red},
+    Args1 = couch_mrview_util:validate_args(Args0),
+    Args1#mrargs.group_level.
+


Mime
View raw message