You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by to...@apache.org on 2015/09/10 02:34:22 UTC

[20/26] couchdb-mango git commit: Return 503 if dreyfus service does not exist

Return 503 if dreyfus service does not exist

References to text will throw a 503 if the dreyfus_index module
does not exist.

COUCHDB-2787


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

Branch: refs/heads/master
Commit: 6100a0b68956fdea9a5ab06f3c0cfc275320542b
Parents: 838c632
Author: Tony Sun <to...@cloudant.com>
Authored: Wed Sep 2 18:51:40 2015 -0700
Committer: Tony Sun <to...@cloudant.com>
Committed: Wed Sep 2 23:34:08 2015 -0700

----------------------------------------------------------------------
 src/mango_cursor.erl                   | 10 ++++++----
 src/mango_error.erl                    |  6 ++++++
 src/mango_idx.erl                      | 30 +++++++++++++++++++++++------
 src/mango_util.erl                     | 18 ++++++++++++++++-
 test/01-index-crud-test.py             |  2 +-
 test/04-key-tests.py                   |  2 +-
 test/06-basic-text-test.py             |  8 ++++----
 test/06-text-default-field-test.py     |  2 +-
 test/07-text-custom-field-list-test.py |  2 +-
 test/08-text-limit-test.py             |  2 +-
 10 files changed, 62 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-mango/blob/6100a0b6/src/mango_cursor.erl
----------------------------------------------------------------------
diff --git a/src/mango_cursor.erl b/src/mango_cursor.erl
index 545a863..d73761e 100644
--- a/src/mango_cursor.erl
+++ b/src/mango_cursor.erl
@@ -123,10 +123,12 @@ group_indexes_by_type(Indexes) ->
     % used to service this query. This is so that we
     % don't suddenly switch indexes for existing client
     % queries.
-    CursorModules = [
-        mango_cursor_view,
-        mango_cursor_text
-    ],
+    CursorModules = case mango_util:module_exists(dreyfus_index) of
+        true ->
+            [mango_cursor_view, mango_cursor_text];
+        false ->
+            [mango_cursor_view]
+    end,
     lists:flatmap(fun(CMod) ->
         case dict:find(CMod, IdxDict) of
             {ok, CModIndexes} ->

http://git-wip-us.apache.org/repos/asf/couchdb-mango/blob/6100a0b6/src/mango_error.erl
----------------------------------------------------------------------
diff --git a/src/mango_error.erl b/src/mango_error.erl
index 6ad76bb..4704261 100644
--- a/src/mango_error.erl
+++ b/src/mango_error.erl
@@ -134,6 +134,12 @@ info(mango_idx, {index_not_implemented, IndexName}) ->
         <<"index_not_implemented">>,
         fmt("~s", [IndexName])
     };
+info(mango_idx, {index_service_unavailable, IndexName}) ->
+    {
+        503,
+        <<"required index service unavailable">>,
+        fmt("~s", [IndexName])
+    };
 
 info(mango_idx_view, {invalid_index_json, BadIdx}) ->
     {

http://git-wip-us.apache.org/repos/asf/couchdb-mango/blob/6100a0b6/src/mango_idx.erl
----------------------------------------------------------------------
diff --git a/src/mango_idx.erl b/src/mango_idx.erl
index 71a55a9..6f73af1 100644
--- a/src/mango_idx.erl
+++ b/src/mango_idx.erl
@@ -166,8 +166,12 @@ from_ddoc(Db, {Props}) ->
         _ ->
             ?MANGO_ERROR(invalid_query_ddoc_language)
     end,
-
-    IdxMods = [mango_idx_view, mango_idx_text],
+    IdxMods = case mango_util:module_exists(dreyfus_index) of
+        true ->
+            [mango_idx_view, mango_idx_text];
+        false ->
+            [mango_idx_view]
+    end,
     Idxs = lists:flatmap(fun(Mod) -> Mod:from_ddoc({Props}) end, IdxMods),
     lists:map(fun(Idx) ->
         Idx#idx{
@@ -243,7 +247,12 @@ cursor_mod(#idx{type = <<"json">>}) ->
 cursor_mod(#idx{def = all_docs, type= <<"special">>}) ->
     mango_cursor_view;
 cursor_mod(#idx{type = <<"text">>}) ->
-    mango_cursor_text.
+    case mango_util:module_exists(dreyfus_index) of
+        true ->
+            mango_cursor_text;
+        false ->
+            ?MANGO_ERROR({index_service_unavailable, <<"text">>})
+    end.
 
 
 idx_mod(#idx{type = <<"json">>}) ->
@@ -251,7 +260,12 @@ idx_mod(#idx{type = <<"json">>}) ->
 idx_mod(#idx{type = <<"special">>}) ->
     mango_idx_special;
 idx_mod(#idx{type = <<"text">>}) ->
-    mango_idx_text.
+    case mango_util:module_exists(dreyfus_index) of
+        true ->
+            mango_idx_text;
+        false ->
+            ?MANGO_ERROR({index_service_unavailable, <<"text">>})
+    end.
 
 
 db_to_name(#db{name=Name}) ->
@@ -274,8 +288,12 @@ get_idx_def(Opts) ->
 get_idx_type(Opts) ->
     case proplists:get_value(type, Opts) of
         <<"json">> -> <<"json">>;
-        <<"text">> ->
-            ?MANGO_ERROR({index_not_implemented, <<"text">>});
+        <<"text">> -> case mango_util:module_exists(dreyfus_index) of
+            true ->
+                <<"text">>;
+            false ->
+                ?MANGO_ERROR({index_service_unavailable, <<"text">>})
+            end;
         %<<"geo">> -> <<"geo">>;
         undefined -> <<"json">>;
         BadType ->

http://git-wip-us.apache.org/repos/asf/couchdb-mango/blob/6100a0b6/src/mango_util.erl
----------------------------------------------------------------------
diff --git a/src/mango_util.erl b/src/mango_util.erl
index 94265cd..553535d 100644
--- a/src/mango_util.erl
+++ b/src/mango_util.erl
@@ -42,7 +42,9 @@
 
     parse_field/1,
 
-    cached_re/2
+    cached_re/2,
+
+    module_exists/1
 ]).
 
 
@@ -395,6 +397,20 @@ check_non_empty(Field, Parts) ->
             Parts
     end.
 
+module_exists(Module) ->
+    case is_atom(Module) of
+        true ->
+            try Module:module_info() of
+                _InfoList ->
+                    true
+            catch
+                _:_ ->
+                    false
+            end;
+        false ->
+            false
+    end.
+
 -ifdef(TEST).
 -include_lib("eunit/include/eunit.hrl").
 

http://git-wip-us.apache.org/repos/asf/couchdb-mango/blob/6100a0b6/test/01-index-crud-test.py
----------------------------------------------------------------------
diff --git a/test/01-index-crud-test.py b/test/01-index-crud-test.py
index 71ea920..021d66e 100644
--- a/test/01-index-crud-test.py
+++ b/test/01-index-crud-test.py
@@ -18,7 +18,7 @@ import unittest
 class IndexCrudTests(mango.DbPerClass):
     @classmethod
     def setUpClass(klass):
-        raise unittest.SkipTest('text index is not supported yet')
+        raise unittest.SkipTest('text index service not available')
         super(KeyTests, klass).setUpClass()
 
     def test_bad_fields(self):

http://git-wip-us.apache.org/repos/asf/couchdb-mango/blob/6100a0b6/test/04-key-tests.py
----------------------------------------------------------------------
diff --git a/test/04-key-tests.py b/test/04-key-tests.py
index ac0d3ec..5174d0b 100644
--- a/test/04-key-tests.py
+++ b/test/04-key-tests.py
@@ -56,7 +56,7 @@ TEST_DOCS = [
 class KeyTests(mango.DbPerClass):
     @classmethod
     def setUpClass(klass):
-        raise unittest.SkipTest('text index is not supported yet')
+        raise unittest.SkipTest('text index service not available')
         super(KeyTests, klass).setUpClass()
         klass.db.save_docs(TEST_DOCS, w=3)
         klass.db.create_index(["type"], ddoc="view")

http://git-wip-us.apache.org/repos/asf/couchdb-mango/blob/6100a0b6/test/06-basic-text-test.py
----------------------------------------------------------------------
diff --git a/test/06-basic-text-test.py b/test/06-basic-text-test.py
index b1be671..72a3918 100644
--- a/test/06-basic-text-test.py
+++ b/test/06-basic-text-test.py
@@ -27,14 +27,14 @@ class TextIndexCheckTests(mango.DbPerClass):
             'type': 'text'
         })
         resp = self.db.sess.post(self.db.path("_index"), data=body)
-        assert resp.status_code == 501, resp
+        assert resp.status_code == 503, resp
 
 
 class BasicTextTests(mango.UserDocsTextTests):
 
     @classmethod
     def setUpClass(klass):
-        raise unittest.SkipTest('text index is not supported yet')
+        raise unittest.SkipTest('text index service not available')
 
     def test_simple(self):
         docs = self.db.find({"$text": "Stephanie"})
@@ -433,7 +433,7 @@ class ElemMatchTests(mango.FriendDocsTextTests):
 
     @classmethod
     def setUpClass(klass):
-        raise unittest.SkipTest('text index is not supported yet')
+        raise unittest.SkipTest('text index service not available')
 
     def test_elem_match_non_object(self):
         q = {"bestfriends":{
@@ -568,7 +568,7 @@ class NumStringTests(mango.NumStringDocsTextTests):
 
     @classmethod
     def setUpClass(klass):
-        raise unittest.SkipTest('text index is not supported yet')
+        raise unittest.SkipTest('text index service not available')
 
     def test_floating_point_val(self):
         float_point_string = num_string_docs.DOCS[2]["number_string"]

http://git-wip-us.apache.org/repos/asf/couchdb-mango/blob/6100a0b6/test/06-text-default-field-test.py
----------------------------------------------------------------------
diff --git a/test/06-text-default-field-test.py b/test/06-text-default-field-test.py
index b0fa911..f4aaf9a 100644
--- a/test/06-text-default-field-test.py
+++ b/test/06-text-default-field-test.py
@@ -20,7 +20,7 @@ class NoDefaultFieldTest(mango.UserDocsTextTests):
 
     @classmethod
     def setUpClass(klass):
-        raise unittest.SkipTest('text not supported')
+        raise unittest.SkipTest('text index service not available')
 
     def test_basic(self):
         docs = self.db.find({"$text": "Ramona"})

http://git-wip-us.apache.org/repos/asf/couchdb-mango/blob/6100a0b6/test/07-text-custom-field-list-test.py
----------------------------------------------------------------------
diff --git a/test/07-text-custom-field-list-test.py b/test/07-text-custom-field-list-test.py
index c1c96db..8d68a1f 100644
--- a/test/07-text-custom-field-list-test.py
+++ b/test/07-text-custom-field-list-test.py
@@ -18,7 +18,7 @@ class CustomFieldsTest(mango.UserDocsTextTests):
 
     @classmethod
     def setUpClass(klass):
-        raise unittest.SkipTest('text index is not supported yet')
+        raise unittest.SkipTest('text index service not available')
 
     FIELDS = [
         {"name": "favorites.[]", "type": "string"},

http://git-wip-us.apache.org/repos/asf/couchdb-mango/blob/6100a0b6/test/08-text-limit-test.py
----------------------------------------------------------------------
diff --git a/test/08-text-limit-test.py b/test/08-text-limit-test.py
index 45c0bc4..36bf4a9 100644
--- a/test/08-text-limit-test.py
+++ b/test/08-text-limit-test.py
@@ -18,7 +18,7 @@ class LimitTests(mango.LimitDocsTextTests):
 
     @classmethod
     def setUpClass(klass):
-        raise unittest.SkipTest('text index is not supported yet')
+        raise unittest.SkipTest('text index service not available')
 
     def test_limit_field(self):
         q = {"$or": [{"user_id" : {"$lt" : 10}}, {"filtered_array.[]": 1}]}