Repository: couchdb-mango
Updated Branches:
  refs/heads/2651-delete-bulk-docs [created] 9642daa37


Add _bulk_delete endpoint

We apend _bulk_delete to _index so that users can delete all their
mango indexes via _index/_bulk_delete. This will be via a POST request.
The body of the post request will be used for options in the future.
The only option available so far will be for the w value.

FIXES 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/de9346e7
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-mango/tree/de9346e7
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-mango/diff/de9346e7

Branch: refs/heads/2651-delete-bulk-docs
Commit: de9346e79cbaef783d4e8ec55ece23bd0a4cfce7
Parents: f6d4901
Author: Tony Sun <tony....@cloudant.com>
Authored: Wed Apr 1 15:30:51 2015 -0700
Committer: Tony Sun <tony....@cloudant.com>
Committed: Wed Apr 1 15:30:51 2015 -0700

----------------------------------------------------------------------
 src/mango_httpd.erl | 45 +++++++++++++++++++++++++++++++++++----------
 src/mango_opts.erl  | 15 ++++++++++++++-
 2 files changed, 49 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-mango/blob/de9346e7/src/mango_httpd.erl
----------------------------------------------------------------------
diff --git a/src/mango_httpd.erl b/src/mango_httpd.erl
index 28a1578..029ad3a 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_create_opts(Opts),
+            CreateOpts = get_idx_w_opts(Opts),
             case mango_crud:insert(Db, NewDDoc, CreateOpts) of
                 {ok, [{RespProps}]} ->
                     case lists:keyfind(error, 1, RespProps) of
@@ -80,6 +80,27 @@ handle_index_req(#httpd{method='POST', path_parts=[_, 
_]}=Req, Db) ->
     end,
        chttpd:send_json(Req, {[{result, Status}, {id, Id}, {name, Name}]});
 
+handle_index_req(#httpd{method='POST', path_parts=[_, <<"_index">>,
+        <<"_bulk_delete">>]}=Req, Db) ->
+    Indexes = mango_idx:list(Db),
+    {ok, Opts} = mango_opts:validate_bulk_delete(chttpd:json_body_obj(Req)),
+    DelOpts = get_idx_w_opts(Opts),
+    lists:foreach(fun(Index) ->
+        case mango_idx:type(Index) of
+            Type when Type == <<"json">>; Type == <<"text">> ->
+                FinalDDoc = get_delete_doc(Db, Index),
+                case mango_crud:insert(Db, FinalDDoc, DelOpts) of
+                    {ok, _} ->
+                        ok;
+                _ ->
+                    ?MANGO_ERROR(error_saving_ddoc)
+                end;
+            _ ->
+                ok
+        end
+    end, Indexes),
+    chttpd:send_json(Req, {[{result, <<"Indexes Deleted">>}]});
+
 handle_index_req(#httpd{method='DELETE',
         path_parts=[A, B, <<"_design">>, DDocId0, Type, Name]}=Req, Db) ->
     PathParts = [A, B, <<"_design/", DDocId0/binary>>, Type, Name],
@@ -100,14 +121,7 @@ handle_index_req(#httpd{method='DELETE',
     end,
     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,
+            FinalDDoc = get_delete_doc(Db, Idx),
             DelOpts = get_idx_del_opts(Req),
             case mango_crud:insert(Db, FinalDDoc, DelOpts) of
                 {ok, _} ->
@@ -148,7 +162,7 @@ set_user_ctx(#httpd{user_ctx=Ctx}, Db) ->
     Db#db{user_ctx=Ctx}.
 
 
-get_idx_create_opts(Opts) ->
+get_idx_w_opts(Opts) ->
     case lists:keyfind(w, 1, Opts) of
         {w, N} when is_integer(N), N > 0 ->
             [{w, integer_to_list(N)}];
@@ -167,6 +181,17 @@ get_idx_del_opts(Req) ->
     end.
 
 
+get_delete_doc(Db, Idx) ->
+    {ok, DDoc} = mango_util:load_ddoc(Db, mango_idx:ddoc(Idx)),
+    {ok, NewDDoc} = mango_idx:remove(DDoc, Idx),
+    case NewDDoc#doc.body of
+        {[{<<"language">>, <<"query">>}]} ->
+            NewDDoc#doc{deleted = true, body = {[]}};
+        _ ->
+            NewDDoc
+    end.
+
+
 start_find_resp(Req) ->
     chttpd:start_delayed_json_response(Req, 200, [], "{\"docs\":[").
 

http://git-wip-us.apache.org/repos/asf/couchdb-mango/blob/de9346e7/src/mango_opts.erl
----------------------------------------------------------------------
diff --git a/src/mango_opts.erl b/src/mango_opts.erl
index f7874a6..52656ee 100644
--- a/src/mango_opts.erl
+++ b/src/mango_opts.erl
@@ -31,7 +31,8 @@
     validate_use_index/1,
     validate_bookmark/1,
     validate_sort/1,
-    validate_fields/1
+    validate_fields/1,
+    validate_bulk_delete/1
 ]).
 
 
@@ -129,6 +130,18 @@ validate_find({Props}) ->
     validate(Props, Opts).
 
 
+validate_bulk_delete({Props}) ->
+    Opts = [
+        {<<"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 ->

Reply via email to