couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jch...@apache.org
Subject svn commit: r755926 - in /couchdb/trunk: share/www/script/test/view_collation.js src/couchdb/couch_db.hrl src/couchdb/couch_httpd_view.erl
Date Thu, 19 Mar 2009 11:20:37 GMT
Author: jchris
Date: Thu Mar 19 11:20:36 2009
New Revision: 755926

URL: http://svn.apache.org/viewvc?rev=755926&view=rev
Log:
inclusive_end query option for views, defaults to true (does not change current behavior).
inclusive_end=false treats endkey as an open interval. 
this is progress on COUCHDB-194. I think it is enough for 0.9, we can discuss switching the
default behavior, and adding a similar option for startkey.

Modified:
    couchdb/trunk/share/www/script/test/view_collation.js
    couchdb/trunk/src/couchdb/couch_db.hrl
    couchdb/trunk/src/couchdb/couch_httpd_view.erl

Modified: couchdb/trunk/share/www/script/test/view_collation.js
URL: http://svn.apache.org/viewvc/couchdb/trunk/share/www/script/test/view_collation.js?rev=755926&r1=755925&r2=755926&view=diff
==============================================================================
--- couchdb/trunk/share/www/script/test/view_collation.js (original)
+++ couchdb/trunk/share/www/script/test/view_collation.js Thu Mar 19 11:20:36 2009
@@ -85,4 +85,28 @@
     rows = db.query(queryFun, null, queryOptions).rows;
     T(rows.length == 1 && equals(rows[0].key, values[i]));
   }
+  
+  // test inclusive_end=true (the default)
+  // the inclusive_end=true functionality is limited to endkey currently
+  // if you need inclusive_start=false for startkey, please do implement. ;)
+  var rows = db.query(queryFun, null, {endkey : "b", inclusive_end:true}).rows;
+  T(rows[rows.length-1].key == "b")
+  // descending=true
+  var rows = db.query(queryFun, null, {endkey : "b", 
+    descending:true, inclusive_end:true}).rows;
+  T(rows[rows.length-1].key == "b")
+
+  // test inclusive_end=false
+  var rows = db.query(queryFun, null, {endkey : "b", inclusive_end:false}).rows;
+  T(rows[rows.length-1].key == "aa")
+  // descending=true
+  var rows = db.query(queryFun, null, {endkey : "b", 
+    descending:true, inclusive_end:false}).rows;
+  T(rows[rows.length-1].key == "B")
+  
+  // inclusive_end=false overrides endkey_docid
+  var rows = db.query(queryFun, null, {
+    endkey : "b", endkey_docid: "b", 
+    inclusive_end:false}).rows;
+  T(rows[rows.length-1].key == "aa")
 };

Modified: couchdb/trunk/src/couchdb/couch_db.hrl
URL: http://svn.apache.org/viewvc/couchdb/trunk/src/couchdb/couch_db.hrl?rev=755926&r1=755925&r2=755926&view=diff
==============================================================================
--- couchdb/trunk/src/couchdb/couch_db.hrl (original)
+++ couchdb/trunk/src/couchdb/couch_db.hrl Thu Mar 19 11:20:36 2009
@@ -155,6 +155,7 @@
     skip = 0,
     group_level = 0,
     reduce = true,
+    inclusive_end=true, % aka a closed-interval
     include_docs = false
 }).
 

Modified: couchdb/trunk/src/couchdb/couch_httpd_view.erl
URL: http://svn.apache.org/viewvc/couchdb/trunk/src/couchdb/couch_httpd_view.erl?rev=755926&r1=755925&r2=755926&view=diff
==============================================================================
--- couchdb/trunk/src/couchdb/couch_httpd_view.erl (original)
+++ couchdb/trunk/src/couchdb/couch_httpd_view.erl Thu Mar 19 11:20:36 2009
@@ -259,7 +259,7 @@
                 Args#view_query_args{start_key=JsonKey,end_key=JsonKey};
             _ ->
                 Msg = io_lib:format("Query parameter \"~s\" not compatible with multi key
mode.", [Key]),
-                throw({query_parse_error, Msg})
+                throw({query_parse_error, ?l2b(Msg)})
             end;
         {"startkey_docid", DocId} ->
             Args#view_query_args{start_docid=list_to_binary(DocId)};
@@ -271,7 +271,7 @@
                 Args#view_query_args{start_key=?JSON_DECODE(Value)};
             _ ->
                 Msg = io_lib:format("Query parameter \"~s\" not compatible with multi key
mode.", [Key]),
-                throw({query_parse_error, Msg})
+                throw({query_parse_error, ?l2b(Msg)})
             end;
         {"endkey", Value} ->
             case Keys of
@@ -279,27 +279,27 @@
                 Args#view_query_args{end_key=?JSON_DECODE(Value)};
             _ ->
                 Msg = io_lib:format("Query parameter \"~s\" not compatible with multi key
mode.", [Key]),
-                throw({query_parse_error, Msg})
+                throw({query_parse_error, ?l2b(Msg)})
             end;
         {"limit", Value} ->
             case (catch list_to_integer(Value)) of
             Limit when is_integer(Limit) ->
                 if Limit < 0 ->
                     Msg = io_lib:format("Limit must be a positive integer: limit=~s", [Value]),
-                    throw({query_parse_error, Msg});
+                    throw({query_parse_error, ?l2b(Msg)});
                 true ->
                     Args#view_query_args{limit=Limit}
                 end;
             _Error ->
                 Msg = io_lib:format("Bad URL query value, number expected: limit=~s", [Value]),
-                throw({query_parse_error, Msg})
+                throw({query_parse_error, ?l2b(Msg)})
             end;
         {"count", Value} ->
-            throw({query_parse_error, "URL query parameter 'count' has been changed to 'limit'."});
+            throw({query_parse_error, <<"URL query parameter 'count' has been changed
to 'limit'.">>});
         {"stale", "ok"} ->
             Args#view_query_args{stale=ok};
         {"update", "false"} ->
-            throw({query_parse_error, "URL query parameter 'update=false' has been changed
to 'stale=ok'."});
+            throw({query_parse_error, <<"URL query parameter 'update=false' has been
changed to 'stale=ok'.">>});
         {"descending", "true"} ->
             case Args#view_query_args.direction of
             fwd ->
@@ -329,7 +329,7 @@
             _Error ->
                 Msg = lists:flatten(io_lib:format(
                 "Bad URL query value, number expected: skip=~s", [Value])),
-                throw({query_parse_error, Msg})
+                throw({query_parse_error, ?l2b(Msg)})
             end;
         {"group", Value} ->
             case Value of
@@ -339,7 +339,7 @@
                 Args#view_query_args{group_level=0};
             _ ->
                 Msg = "Bad URL query value for 'group' expected \"true\" or \"false\".",
-                throw({query_parse_error, Msg})
+                throw({query_parse_error, ?l2b(Msg)})
             end;
         {"group_level", LevelStr} ->
             case Keys of
@@ -347,8 +347,12 @@
                 Args#view_query_args{group_level=list_to_integer(LevelStr)};
             _ ->
                 Msg = lists:flatten(io_lib:format("Multi-key fetches for a reduce view must
include group=true", [])),
-                throw({query_parse_error, Msg})
+                throw({query_parse_error, ?l2b(Msg)})
             end;
+        {"inclusive_end", "true"} ->
+            Args#view_query_args{inclusive_end=true};
+        {"inclusive_end", "false"} ->
+            Args#view_query_args{inclusive_end=false};
         {"reduce", "true"} ->
             Args#view_query_args{reduce=true};
         {"reduce", "false"} ->
@@ -361,7 +365,7 @@
                 Args#view_query_args{include_docs=false};
             _ ->
                 Msg1 = "Bad URL query value for 'include_docs' expected \"true\" or \"false\".",
-                throw({query_parse_error, Msg1})
+                throw({query_parse_error, ?l2b(Msg1)})
             end;
         {"format", _} ->
             % we just ignore format, so that JS can have it
@@ -373,7 +377,7 @@
             false ->
                 Msg = lists:flatten(io_lib:format(
                     "Bad URL query key:~s", [Key])),
-                throw({query_parse_error, Msg})
+                throw({query_parse_error, ?l2b(Msg)})
             end
         end
     end, #view_query_args{}, QueryList),
@@ -381,7 +385,7 @@
     true ->
         case QueryArgs#view_query_args.include_docs and QueryArgs#view_query_args.reduce
of
         true ->
-            ErrMsg = "Bad URL query key for reduce operation: include_docs",
+            ErrMsg = <<"Bad URL query key for reduce operation: include_docs">>,
             throw({query_parse_error, ErrMsg});
         _ ->
             ok
@@ -404,8 +408,7 @@
                 #view_query_args{reduce=OptReduce} = QueryArgs,
                 case OptReduce of
                 true ->
-                    Msg = lists:flatten(io_lib:format(
-                        "Multi-key fetches for a reduce view must include group=true", [])),
+                    Msg = <<"Multi-key fetches for a reduce view must include group=true">>,
                     throw({query_parse_error, Msg});
                 _ -> 
                     QueryArgs
@@ -419,6 +422,7 @@
     #view_query_args{
         end_key = EndKey,
         end_docid = EndDocId,
+        inclusive_end = InclusiveEnd,
         direction = Dir
     } = QueryArgs,
     
@@ -427,7 +431,8 @@
         start_response = StartRespFun,
         send_row = SendRowFun,
         reduce_count = ReduceCountFun
-    } = apply_default_helper_funs(HelperFuns, {Dir, EndKey, EndDocId}),
+    } = apply_default_helper_funs(HelperFuns, 
+        {Dir, EndKey, EndDocId, InclusiveEnd}),
 
     #view_query_args{
         include_docs = IncludeDocs
@@ -467,9 +472,9 @@
     passed_end = PassedEnd,
     start_response = StartResp,
     send_row = SendRow
-}=Helpers, {Dir, EndKey, EndDocId}) ->
+}=Helpers, {Dir, EndKey, EndDocId, InclusiveEnd}) ->
     PassedEnd2 = case PassedEnd of
-    undefined -> make_passed_end_fun(Dir, EndKey, EndDocId);
+    undefined -> make_passed_end_fun(Dir, EndKey, EndDocId, InclusiveEnd);
     _ -> PassedEnd
     end,
 
@@ -508,15 +513,33 @@
         send_row = SendRow2
     }.
 
-make_passed_end_fun(Dir, EndKey, EndDocId) ->
-    case Dir of
-    fwd ->
+make_passed_end_fun(fwd, EndKey, EndDocId, InclusiveEnd) ->
+    case InclusiveEnd of
+    true ->
         fun(ViewKey, ViewId) ->
             couch_view:less_json([EndKey, EndDocId], [ViewKey, ViewId])
         end;
-    rev->
+    false ->
+        fun
+            (ViewKey, _ViewId) when ViewKey == EndKey ->
+                true;
+            (ViewKey, ViewId) ->
+                couch_view:less_json([EndKey, EndDocId], [ViewKey, ViewId])
+        end
+    end;
+
+make_passed_end_fun(rev, EndKey, EndDocId, InclusiveEnd) ->
+    case InclusiveEnd of
+    true ->
         fun(ViewKey, ViewId) ->
             couch_view:less_json([ViewKey, ViewId], [EndKey, EndDocId])
+        end;
+    false->
+        fun
+            (ViewKey, _ViewId) when ViewKey == EndKey ->
+                true;
+            (ViewKey, ViewId) ->
+                couch_view:less_json([ViewKey, ViewId], [EndKey, EndDocId])
         end
     end.
 



Mime
View raw message