couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tonysu...@apache.org
Subject [1/2] couchdb-mango git commit: Revert "Enable bulk deletes"
Date Wed, 03 Jun 2015 19:58:22 GMT
Repository: couchdb-mango
Updated Branches:
  refs/heads/2651-delete-bulk-docs 6490184bf -> 3a8efbee5


Revert "Enable bulk deletes"

This reverts commit 6490184bf36c6eb04587d5fb3bd86923e62ec2b5.

COUCHDB-2651


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

Branch: refs/heads/2651-delete-bulk-docs
Commit: c8a2194c5e0121d5876e7917990e3983961b9681
Parents: 6490184
Author: Tony Sun <tony.sun@cloudant.com>
Authored: Wed Jun 3 13:44:29 2015 -0400
Committer: Tony Sun <tony.sun@cloudant.com>
Committed: Wed Jun 3 13:44:29 2015 -0400

----------------------------------------------------------------------
 src/mango_error.erl        |  7 ----
 src/mango_httpd.erl        | 74 ++++++++++++++---------------------------
 src/mango_idx.erl          | 25 +-------------
 src/mango_opts.erl         | 27 +--------------
 test/01-index-crud-test.py | 30 -----------------
 test/mango.py              |  9 -----
 6 files changed, 27 insertions(+), 145 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-mango/blob/c8a2194c/src/mango_error.erl
----------------------------------------------------------------------
diff --git a/src/mango_error.erl b/src/mango_error.erl
index 6dcf7c8..babf564 100644
--- a/src/mango_error.erl
+++ b/src/mango_error.erl
@@ -110,13 +110,6 @@ info(mango_idx_view, {index_not_found, BadIdx}) ->
         fmt("JSON index ~s not found in this design doc.", [BadIdx])
     };
 
-info(mango_opts, {invalid_bulk_docs, Val}) ->
-    {
-        400,
-        <<"invalid_bulk_docs">>,
-        fmt("Bulk Delete requires an array of non-null docids. Docids: ~w",
-            [Val])
-    };
 info(mango_opts, {invalid_ejson, Val}) ->
     {
         400,

http://git-wip-us.apache.org/repos/asf/couchdb-mango/blob/c8a2194c/src/mango_httpd.erl
----------------------------------------------------------------------
diff --git a/src/mango_httpd.erl b/src/mango_httpd.erl
index 671ffd2..28a1578 100644
--- a/src/mango_httpd.erl
+++ b/src/mango_httpd.erl
@@ -65,7 +65,7 @@ handle_index_req(#httpd{method='POST', path_parts=[_, _]}=Req, Db) ->
         {ok, DDoc} ->
             <<"exists">>;
         {ok, NewDDoc} ->
-            CreateOpts = get_idx_w_opts(Opts),
+            CreateOpts = get_idx_create_opts(Opts),
             case mango_crud:insert(Db, NewDDoc, CreateOpts) of
                 {ok, [{RespProps}]} ->
                     case lists:keyfind(error, 1, RespProps) of
@@ -80,28 +80,6 @@ handle_index_req(#httpd{method='POST', path_parts=[_, _]}=Req, Db) ->
     end,
 	chttpd:send_json(Req, {[{result, Status}, {id, Id}, {name, Name}]});
 
-%% Essentially we just iterate through the list of ddoc ids passed in and
-%% delete one by one. If an error occurs, all previous documents will be
-%% deleted, but an error will be thrown for the current ddoc id.
-handle_index_req(#httpd{method='POST', path_parts=[_, <<"_index">>,
-        <<"_bulk_delete">>]}=Req, Db) ->
-    {ok, Opts} = mango_opts:validate_bulk_delete(chttpd:json_body_obj(Req)),
-    Idxs = mango_idx:list(Db),
-    DDocs = get_bulk_delete_ddocs(Opts),
-    DelOpts = get_idx_w_opts(Opts),
-    {Success, Fail} = lists:foldl(fun(DDocId0, {Success0, Fail0}) ->
-        DDocId = convert_to_design_id(DDocId0),
-        Filt = fun(Idx) -> mango_idx:ddoc(Idx) == DDocId end,
-        Id = {<<"id">>, DDocId},
-        case mango_idx:delete(Filt, Db, Idxs, DelOpts) of
-            {ok, true} ->
-                {[{[Id, {<<"ok">>, true}]} | Success0], Fail0};
-            {error, Error} ->
-                {Success0, [{[Id, {<<"error">>, Error}]} | Fail0]}
-        end
-    end, {[], []}, DDocs),
-    chttpd:send_json(Req, {[{<<"success">>, Success}, {<<"fail">>,
Fail}]});
-
 handle_index_req(#httpd{method='DELETE',
         path_parts=[A, B, <<"_design">>, DDocId0, Type, Name]}=Req, Db) ->
     PathParts = [A, B, <<"_design/", DDocId0/binary>>, Type, Name],
@@ -109,22 +87,36 @@ handle_index_req(#httpd{method='DELETE',
 
 handle_index_req(#httpd{method='DELETE',
         path_parts=[_, _, DDocId0, Type, Name]}=Req, Db) ->
+    DDocId = case DDocId0 of
+        <<"_design/", _/binary>> -> DDocId0;
+        _ -> <<"_design/", DDocId0/binary>>
+    end,
     Idxs = mango_idx:list(Db),
-    DDocId = convert_to_design_id(DDocId0),
-    DelOpts = get_idx_del_opts(Req),
     Filt = fun(Idx) ->
         IsDDoc = mango_idx:ddoc(Idx) == DDocId,
         IsType = mango_idx:type(Idx) == Type,
         IsName = mango_idx:name(Idx) == Name,
         IsDDoc andalso IsType andalso IsName
     end,
-    case mango_idx:delete(Filt, Db, Idxs, DelOpts) of
-        {ok, true} ->
-            chttpd:send_json(Req, {[{ok, true}]});
-        {error, not_found} ->
-            throw({not_found, missing});
-        {error, Error} ->
-            ?MANGO_ERROR({error_saving_ddoc, Error})
+    case lists:filter(Filt, Idxs) of
+        [Idx] ->
+            {ok, DDoc} = mango_util:load_ddoc(Db, mango_idx:ddoc(Idx)),
+            {ok, NewDDoc} = mango_idx:remove(DDoc, Idx),
+            FinalDDoc = case NewDDoc#doc.body of
+                {[{<<"language">>, <<"query">>}]} ->
+                    NewDDoc#doc{deleted = true, body = {[]}};
+                _ ->
+                    NewDDoc
+            end,
+            DelOpts = get_idx_del_opts(Req),
+            case mango_crud:insert(Db, FinalDDoc, DelOpts) of
+                {ok, _} ->
+                    chttpd:send_json(Req, {[{ok, true}]});
+                _ ->
+                    ?MANGO_ERROR(error_saving_ddoc)
+            end;
+        [] ->
+            throw({not_found, missing})
     end;
 
 handle_index_req(Req, _Db) ->
@@ -156,7 +148,7 @@ set_user_ctx(#httpd{user_ctx=Ctx}, Db) ->
     Db#db{user_ctx=Ctx}.
 
 
-get_idx_w_opts(Opts) ->
+get_idx_create_opts(Opts) ->
     case lists:keyfind(w, 1, Opts) of
         {w, N} when is_integer(N), N > 0 ->
             [{w, integer_to_list(N)}];
@@ -165,15 +157,6 @@ get_idx_w_opts(Opts) ->
     end.
 
 
-get_bulk_delete_ddocs(Opts) ->
-    case lists:keyfind(docids, 1, Opts) of
-        {docids, DDocs} when is_list(DDocs) ->
-            DDocs;
-        _ ->
-            []
-    end.
-
-
 get_idx_del_opts(Req) ->
     try
         WStr = chttpd:qs_value(Req, "w", "2"),
@@ -184,13 +167,6 @@ get_idx_del_opts(Req) ->
     end.
 
 
-convert_to_design_id(DDocId) ->
-    case DDocId of
-        <<"_design/", _/binary>> -> DDocId;
-        _ -> <<"_design/", DDocId/binary>>
-    end.
-
-
 start_find_resp(Req) ->
     chttpd:start_delayed_json_response(Req, 200, [], "{\"docs\":[").
 

http://git-wip-us.apache.org/repos/asf/couchdb-mango/blob/c8a2194c/src/mango_idx.erl
----------------------------------------------------------------------
diff --git a/src/mango_idx.erl b/src/mango_idx.erl
index bf81679..1c15894 100644
--- a/src/mango_idx.erl
+++ b/src/mango_idx.erl
@@ -41,8 +41,7 @@
     end_key/2,
     cursor_mod/1,
     idx_mod/1,
-    to_json/1,
-    delete/4
+    to_json/1
 ]).
 
 
@@ -135,28 +134,6 @@ remove(DDoc, Idx) ->
     {ok, NewDDoc#doc{body = Body}}.
 
 
-delete(Filt, Db, Indexes, DelOpts) ->
-    case lists:filter(Filt, Indexes) of
-        [Idx] ->
-            {ok, DDoc} = mango_util:load_ddoc(Db, mango_idx:ddoc(Idx)),
-            {ok, NewDDoc} = mango_idx:remove(DDoc, Idx),
-            FinalDDoc = case NewDDoc#doc.body of
-                {[{<<"language">>, <<"query">>}]} ->
-                    NewDDoc#doc{deleted = true, body = {[]}};
-                _ ->
-                    NewDDoc
-            end,
-            case mango_crud:insert(Db, FinalDDoc, DelOpts) of
-                {ok, _} ->
-                    {ok, true};
-                Error ->
-                    {error, Error}
-            end;
-        [] ->
-            {error, not_found}
-    end.
-
-
 from_ddoc(Db, {Props}) ->
     DbName = db_to_name(Db),
     DDoc = proplists:get_value(<<"_id">>, Props),

http://git-wip-us.apache.org/repos/asf/couchdb-mango/blob/c8a2194c/src/mango_opts.erl
----------------------------------------------------------------------
diff --git a/src/mango_opts.erl b/src/mango_opts.erl
index 0c66a21..f7874a6 100644
--- a/src/mango_opts.erl
+++ b/src/mango_opts.erl
@@ -31,8 +31,7 @@
     validate_use_index/1,
     validate_bookmark/1,
     validate_sort/1,
-    validate_fields/1,
-    validate_bulk_delete/1
+    validate_fields/1
 ]).
 
 
@@ -130,22 +129,6 @@ validate_find({Props}) ->
     validate(Props, Opts).
 
 
-validate_bulk_delete({Props}) ->
-    Opts = [
-        {<<"docids">>, [
-            {tag, docids},
-            {validator, fun validate_bulk_docs/1}
-        ]},
-        {<<"w">>, [
-            {tag, w},
-            {optional, true},
-            {default, 2},
-            {validator, fun is_pos_integer/1}
-        ]}
-    ],
-    validate(Props, Opts).
-
-
 validate(Props, Opts) ->
     case mango_util:assert_ejson({Props}) of
         true ->
@@ -209,14 +192,6 @@ validate_selector(Else) ->
     ?MANGO_ERROR({invalid_selector_json, Else}).
 
 
-%% We re-use validate_use_index to make sure the index names are valid
-validate_bulk_docs(Docs) when is_list(Docs) ->
-    lists:foreach(fun validate_use_index/1, Docs),
-    {ok, Docs};
-validate_bulk_docs(Else) ->
-    ?MANGO_ERROR({invalid_bulk_docs, Else}).
-
-
 validate_use_index(IndexName) when is_binary(IndexName) ->
     case binary:split(IndexName, <<"/">>) of
         [DesignId] ->

http://git-wip-us.apache.org/repos/asf/couchdb-mango/blob/c8a2194c/test/01-index-crud-test.py
----------------------------------------------------------------------
diff --git a/test/01-index-crud-test.py b/test/01-index-crud-test.py
index a44376c..459566b 100644
--- a/test/01-index-crud-test.py
+++ b/test/01-index-crud-test.py
@@ -150,36 +150,6 @@ class IndexCrudTests(mango.DbPerClass):
         post_indexes = self.db.list_indexes()
         assert pre_indexes == post_indexes
 
-    def test_bulk_delete(self):
-        fields = ["field1"]
-        ret = self.db.create_index(fields, name="idx_01")
-        assert ret is True
-
-        fields = ["field2"]
-        ret = self.db.create_index(fields, name="idx_02")
-        assert ret is True
-
-        fields = ["field3"]
-        ret = self.db.create_index(fields, name="idx_03")
-        assert ret is True
-
-        docids = []
-
-        for idx in self.db.list_indexes():
-            if idx["ddoc"] is not None:
-                docids.append(idx["ddoc"])
-
-        docids.append("_design/this_is_not_an_index_name")
-
-        ret = self.db.bulk_delete(docids)
-
-        assert ret["fail"][0]["id"] == "_design/this_is_not_an_index_name"
-        assert len(ret["success"]) == 3
-
-        for idx in self.db.list_indexes():
-            assert idx["type"] != "json"
-            assert idx["type"] != "text"
-
     def test_recreate_index(self):
         pre_indexes = self.db.list_indexes()
         for i in range(5):

http://git-wip-us.apache.org/repos/asf/couchdb-mango/blob/c8a2194c/test/mango.py
----------------------------------------------------------------------
diff --git a/test/mango.py b/test/mango.py
index b39c916..57dfffb 100644
--- a/test/mango.py
+++ b/test/mango.py
@@ -135,15 +135,6 @@ class Database(object):
         r = self.sess.delete(self.path(path), params={"w":"3"})
         r.raise_for_status()
 
-    def bulk_delete(self, docs):
-        body = {
-            "docids" : docs,
-            "w": 3
-        }
-        body = json.dumps(body)
-        r = self.sess.post(self.path("_index/_bulk_delete"), data=body)
-        return r.json()
-
     def find(self, selector, limit=25, skip=0, sort=None, fields=None,
                 r=1, conflicts=False, use_index=None, explain=False,
                 bookmark=None, return_raw=False):


Mime
View raw message