Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #4 from cloudant/12533-quorum-202

  • Loading branch information...
commit e0391bad43679f5a83f8b094e6c4a06cda10af80 2 parents 3be99e6 + 6999135
Adam Kocoloski kocolosk authored
Showing with 65 additions and 17 deletions.
  1. +59 −15 src/chttpd_db.erl
  2. +6 −2 src/chttpd_show.erl
74 src/chttpd_db.erl
View
@@ -218,6 +218,7 @@ db_req(#httpd{method='POST', path_parts=[DbName], user_ctx=Ctx}=Req, Db) ->
spawn(fun() ->
case catch(fabric:update_doc(Db, Doc2, [{user_ctx, Ctx}])) of
{ok, _} -> ok;
+ {accepted, _} -> ok;
Error ->
?LOG_INFO("Batch doc error (~s): ~p",[DocId, Error])
end
@@ -229,9 +230,14 @@ db_req(#httpd{method='POST', path_parts=[DbName], user_ctx=Ctx}=Req, Db) ->
]});
_Normal ->
% normal
- {ok, NewRev} = fabric:update_doc(Db, Doc2, [{user_ctx, Ctx}]),
DocUrl = absolute_uri(Req, [$/, DbName, $/, DocId]),
- send_json(Req, 201, [{"Location", DocUrl}], {[
+ case fabric:update_doc(Db, Doc2, [{user_ctx, Ctx}]) of
+ {ok, NewRev} ->
+ HttpCode = 201;
+ {accepted, NewRev} ->
+ HttpCode = 202
+ end,
+ send_json(Req, HttpCode, [{"Location", DocUrl}], {[
{ok, true},
{id, DocId},
{rev, couch_doc:rev_to_str(NewRev)}
@@ -295,6 +301,11 @@ db_req(#httpd{method='POST',path_parts=[_,<<"_bulk_docs">>], user_ctx=Ctx}=Req,
DocResults = lists:zipwith(fun update_doc_result_to_json/2,
Docs, Results),
send_json(Req, 201, DocResults);
+ {accepted, Results} ->
+ % output the results
+ DocResults = lists:zipwith(fun update_doc_result_to_json/2,
+ Docs, Results),
+ send_json(Req, 202, DocResults);
{aborted, Errors} ->
ErrorsJson =
lists:map(fun update_doc_result_to_json/1, Errors),
@@ -303,9 +314,14 @@ db_req(#httpd{method='POST',path_parts=[_,<<"_bulk_docs">>], user_ctx=Ctx}=Req,
false ->
Docs = [couch_doc:from_json_obj(JsonObj) || JsonObj <- DocsArray],
[validate_attachment_names(D) || D <- Docs],
- {ok, Errors} = fabric:update_docs(Db, Docs, [replicated_changes|Options]),
- ErrorsJson = lists:map(fun update_doc_result_to_json/1, Errors),
- send_json(Req, 201, ErrorsJson)
+ case fabric:update_docs(Db, Docs, [replicated_changes|Options]) of
+ {ok, Errors} ->
+ ErrorsJson = lists:map(fun update_doc_result_to_json/1, Errors),
+ send_json(Req, 201, ErrorsJson);
+ {accepted, Errors} ->
+ ErrorsJson = lists:map(fun update_doc_result_to_json/1, Errors),
+ send_json(Req, 202, ErrorsJson)
+ end
end;
db_req(#httpd{path_parts=[_,<<"_bulk_docs">>]}=Req, _Db) ->
@@ -553,9 +569,13 @@ db_doc_req(#httpd{method='POST', user_ctx=Ctx}=Req, Db, DocId) ->
NewDoc = Doc#doc{
atts = UpdatedAtts ++ OldAtts2
},
- {ok, NewRev} = fabric:update_doc(Db, NewDoc, [{user_ctx,Ctx}]),
-
- send_json(Req, 201, [{"Etag", "\"" ++ ?b2l(couch_doc:rev_to_str(NewRev)) ++ "\""}], {[
+ case fabric:update_doc(Db, NewDoc, [{user_ctx,Ctx}]) of
+ {ok, NewRev} ->
+ HttpCode = 201;
+ {accepted, NewRev} ->
+ HttpCode = 202
+ end,
+ send_json(Req, HttpCode, [{"Etag", "\"" ++ ?b2l(couch_doc:rev_to_str(NewRev)) ++ "\""}], {[
{ok, true},
{id, DocId},
{rev, couch_doc:rev_to_str(NewRev)}
@@ -595,6 +615,7 @@ db_doc_req(#httpd{method='PUT', user_ctx=Ctx}=Req, Db, DocId) ->
spawn(fun() ->
case catch(fabric:update_doc(Db, Doc, Options)) of
{ok, _} -> ok;
+ {accepted, _} -> ok;
Error ->
?LOG_INFO("Batch doc error (~s): ~p",[DocId, Error])
end
@@ -621,11 +642,16 @@ db_doc_req(#httpd{method='COPY', user_ctx=Ctx}=Req, Db, SourceDocId) ->
% open old doc
Doc = couch_doc_open(Db, SourceDocId, SourceRev, []),
% save new doc
- {ok, NewTargetRev} = fabric:update_doc(Db,
- Doc#doc{id=TargetDocId, revs=TargetRevs}, [{user_ctx,Ctx}]),
+ case fabric:update_doc(Db,
+ Doc#doc{id=TargetDocId, revs=TargetRevs}, [{user_ctx,Ctx}]) of
+ {ok, NewTargetRev} ->
+ HttpCode = 201;
+ {accepted, NewTargetRev} ->
+ HttpCode = 202
+ end,
% respond
{PartRes} = update_doc_result_to_json(TargetDocId, {ok, NewTargetRev}),
- send_json(Req, 201,
+ send_json(Req, HttpCode,
[{"Etag", "\"" ++ ?b2l(couch_doc:rev_to_str(NewTargetRev)) ++ "\""}],
{[{ok, true}] ++ PartRes});
@@ -735,10 +761,23 @@ update_doc(#httpd{user_ctx=Ctx} = Req, Db, DocId, #doc{deleted=Deleted}=Doc,
_ ->
Options = [UpdateType, {user_ctx,Ctx}, {w,W}]
end,
- {ok, NewRev} = fabric:update_doc(Db, Doc, Options),
+ case fabric:update_doc(Db, Doc, Options) of
+ {ok, NewRev} ->
+ Accepted = false;
+ {accepted, NewRev} ->
+ Accepted = true
+ end,
NewRevStr = couch_doc:rev_to_str(NewRev),
ResponseHeaders = [{"Etag", <<"\"", NewRevStr/binary, "\"">>} | Headers],
- send_json(Req, if Deleted -> 200; true -> 201 end, ResponseHeaders, {[
+ case {Accepted, Deleted} of
+ {true, _} ->
+ HttpCode = 202;
+ {false, true} ->
+ HttpCode = 200;
+ {false, false} ->
+ HttpCode = 201
+ end,
+ send_json(Req, HttpCode, ResponseHeaders, {[
{ok, true},
{id, DocId},
{rev, NewRevStr}
@@ -949,7 +988,12 @@ db_attachment_req(#httpd{method=Method, user_ctx=Ctx}=Req, Db, DocId, FileNamePa
revs = {Pos, case Revs of [] -> []; [Hd|_] -> [Hd] end},
atts = NewAtt ++ [A || A <- Atts, A#att.name /= FileName]
},
- {ok, UpdatedRev} = fabric:update_doc(Db, DocEdited, [{user_ctx,Ctx}]),
+ case fabric:update_doc(Db, DocEdited, [{user_ctx,Ctx}]) of
+ {ok, UpdatedRev} ->
+ HttpCode = 201;
+ {accepted, UpdatedRev} ->
+ HttpCode = 202
+ end,
erlang:put(mochiweb_request_recv, true),
#db{name=DbName} = Db,
@@ -957,7 +1001,7 @@ db_attachment_req(#httpd{method=Method, user_ctx=Ctx}=Req, Db, DocId, FileNamePa
'DELETE' ->
{200, []};
_ ->
- {201, [{"Location", absolute_uri(Req, [$/, DbName, $/, DocId, $/,
+ {HttpCode, [{"Location", absolute_uri(Req, [$/, DbName, $/, DocId, $/,
FileName])}]}
end,
send_json(Req,Status, Headers, {[
8 src/chttpd_show.erl
View
@@ -131,8 +131,12 @@ send_doc_update_response(Req, Db, DDoc, UpdateName, Doc, DocId) ->
Options = [{user_ctx, Req#httpd.user_ctx}]
end,
NewDoc = couch_doc:from_json_obj({NewJsonDoc}),
- Code = 201,
- {ok, _NewRev} = fabric:update_doc(Db, NewDoc, Options);
+ case fabric:update_doc(Db, NewDoc, Options) of
+ {ok, _} ->
+ Code = 201;
+ {accepted, _} ->
+ Code = 202
+ end;
[<<"up">>, _Other, JsonResp] ->
Code = 200
end,
Please sign in to comment.
Something went wrong with that request. Please try again.