Enable bulk deletes

Add _bulk_delete path to _index so that users can bulk delete
indexes via POST. Users will pass in a list of docids via the POST body.
We delete each index one by one until an error is thrown or all indexes
in the list are deleted. We return a list of successful deletes
and failed deletes along with the ids. For a single delete, we still
throw the same errors as before to be backwards compatible
with any applications expecting an error.

Fixes COUCHDB-2651
diff --git a/src/mango_error.erl b/src/mango_error.erl
index babf564..6dcf7c8 100644
--- a/src/mango_error.erl
+++ b/src/mango_error.erl
@@ -110,6 +110,13 @@
         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,
diff --git a/src/mango_httpd.erl b/src/mango_httpd.erl
index 28a1578..671ffd2 100644
--- a/src/mango_httpd.erl
+++ b/src/mango_httpd.erl
@@ -65,7 +65,7 @@
         {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,28 @@
     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],
@@ -87,36 +109,22 @@
 
 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 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})
+    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})
     end;
 
 handle_index_req(Req, _Db) ->
@@ -148,7 +156,7 @@
     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)}];
@@ -157,6 +165,15 @@
     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"),
@@ -167,6 +184,13 @@
     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\":[").
 
diff --git a/src/mango_idx.erl b/src/mango_idx.erl
index 1c15894..bf81679 100644
--- a/src/mango_idx.erl
+++ b/src/mango_idx.erl
@@ -41,7 +41,8 @@
     end_key/2,
     cursor_mod/1,
     idx_mod/1,
-    to_json/1
+    to_json/1,
+    delete/4
 ]).
 
 
@@ -134,6 +135,28 @@
     {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),
diff --git a/src/mango_opts.erl b/src/mango_opts.erl
index f7874a6..0c66a21 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,22 @@
     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 ->
@@ -192,6 +209,14 @@
     ?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] ->
diff --git a/test/01-index-crud-test.py b/test/01-index-crud-test.py
index 459566b..a44376c 100644
--- a/test/01-index-crud-test.py
+++ b/test/01-index-crud-test.py
@@ -150,6 +150,36 @@
         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):
diff --git a/test/mango.py b/test/mango.py
index 57dfffb..b39c916 100644
--- a/test/mango.py
+++ b/test/mango.py
@@ -135,6 +135,15 @@
         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):