couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From robertkowal...@apache.org
Subject [2/5] couchdb-mango git commit: more hacking
Date Wed, 27 Jan 2016 13:30:23 GMT
more hacking


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

Branch: refs/heads/mango-catch-all
Commit: c0a822fe8df87d370ebc96fa0ec1c5272fdc4095
Parents: 4821d3f
Author: Robert Kowalski <rok@kowalski.gd>
Authored: Mon Jan 18 00:13:35 2016 +0100
Committer: Robert Kowalski <rok@kowalski.gd>
Committed: Mon Jan 18 00:13:35 2016 +0100

----------------------------------------------------------------------
 src/mango_cursor.erl |  6 ++++--
 src/mango_httpd.erl  | 35 ++++++++++++++++++++++++++++++++---
 2 files changed, 36 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-mango/blob/c0a822fe/src/mango_cursor.erl
----------------------------------------------------------------------
diff --git a/src/mango_cursor.erl b/src/mango_cursor.erl
index 40b7208..5e7053b 100644
--- a/src/mango_cursor.erl
+++ b/src/mango_cursor.erl
@@ -16,7 +16,8 @@
 -export([
     create/3,
     explain/1,
-    execute/3
+    execute/3,
+    maybe_filter_indexes/2
 ]).
 
 
@@ -53,7 +54,8 @@ create(Db, Selector0, Opts) ->
         0 ->
             % fallback to _id > null for better usability
             NewSelector = {[{<<"$and">>, [Selector, {[{<<"_id">>,
{[{<<"$gt">>, null}]}}]}]}]},
-            couch_log:notice("no matching index found, create an index to optimize query
time", []),
+            couch_log:warning("no matching index found, create an index to optimize query
time", []),
+            couch_log:warning("~p", [Opts]),
             create(Db, NewSelector, Opts);
         _ ->
             create_cursor(Db, UsableIndexes, Selector, Opts)

http://git-wip-us.apache.org/repos/asf/couchdb-mango/blob/c0a822fe/src/mango_httpd.erl
----------------------------------------------------------------------
diff --git a/src/mango_httpd.erl b/src/mango_httpd.erl
index ef7135c..ac10d77 100644
--- a/src/mango_httpd.erl
+++ b/src/mango_httpd.erl
@@ -181,7 +181,7 @@ handle_find_req(#httpd{method='POST'}=Req, Db) ->
     chttpd:validate_ctype(Req, "application/json"),
     {ok, Opts0} = mango_opts:validate_find(chttpd:json_body_obj(Req)),
     {value, {selector, Sel}, Opts} = lists:keytake(selector, 1, Opts0),
-    {ok, Resp0} = start_find_resp(Req),
+    {ok, Resp0} = start_find_resp(Req, Db, Sel, Opts),
     {ok, AccOut} = run_find(Resp0, Db, Sel, Opts),
     end_find_resp(AccOut);
 
@@ -228,8 +228,37 @@ convert_to_design_id(DDocId) ->
     end.
 
 
-start_find_resp(Req) ->
-    chttpd:start_delayed_json_response(Req, 200, [], "{\"docs\":[").
+start_find_resp(Req, Db, Sel, Opts) ->
+    chttpd:start_delayed_json_response(Req, 200, [], maybeAddWarning(Db, Sel, Opts)).
+
+
+maybeAddWarning(Db, Selector0, Opts) ->
+    Selector = mango_selector:normalize(Selector0),
+
+    ExistingIndexes = mango_idx:list(Db),
+    if ExistingIndexes /= [] -> ok; true ->
+        ?MANGO_ERROR({no_usable_index, no_indexes_defined})
+    end,
+
+    FilteredIndexes = mango_cursor:maybe_filter_indexes(ExistingIndexes, Opts),
+    if FilteredIndexes /= [] -> ok; true ->
+        ?MANGO_ERROR({no_usable_index, no_index_matching_name})
+    end,
+
+    SortIndexes = mango_idx:for_sort(FilteredIndexes, Opts),
+    if SortIndexes /= [] -> ok; true ->
+        ?MANGO_ERROR({no_usable_index, missing_sort_index})
+    end,
+
+    UsableFilter = fun(I) -> mango_idx:is_usable(I, Selector) end,
+    UsableIndexes = lists:filter(UsableFilter, SortIndexes),
+
+    case length(UsableIndexes) of
+        0 ->
+            "{\"_warning\":\"no matching index found, create an index to optimize query time\",\r\n\"docs\":[";
+        _ ->
+            "{\"docs\":["
+    end.
 
 
 end_find_resp(Acc0) ->


Mime
View raw message