Skip to content

Commit

Permalink
add save_docs and delete_docs functions (bulk_doc api).
Browse files Browse the repository at this point in the history
  • Loading branch information
benoitc committed Jun 23, 2011
1 parent 0ca49c8 commit 8e89c22
Showing 1 changed file with 66 additions and 13 deletions.
79 changes: 66 additions & 13 deletions couchc.erl
Expand Up @@ -22,6 +22,8 @@
open_doc/2, open_doc/3, open_doc_rev/4,
save_doc/2, save_doc/3,
delete_doc/2, delete_doc/3,
save_docs/2, save_docs/3,
delete_docs/2, delete_docs/3,
db_exec/2, db_admin_exec/2]).


Expand Down Expand Up @@ -319,13 +321,72 @@ delete_doc(Db, {DocProps}, Options) ->
save_doc(Db, Doc, Options).


save_docs(Db, Docs) ->
save_docs(Db, Docs, []).

%%save_docs(Db, Docs) ->
%% save_docs(Db, Docs, []).

%%save_docs(Db, Docs, Options) ->
%% Docs1 = [maybe_docid(Doc) || Doc <- Docs],
%% ok.
%% @doc
%% options = replicated_changes, all_or_nothing, delay_commit,
%% full_commmit
save_docs(Db, Docs, Options) ->
db_exec(Db, fun(Db0) ->
case proplists:get_value(replicated_changes, Options) of
true ->
Docs1 = lists:map(fun(JsonObj) ->
Doc = couch_doc:from_json_obj(JsonObj),
validate_attachment_names(Doc),
Doc
end, Docs),
Options1 = proplists:delete(all_or_nothing, Options),
{ok, Errors} = couch_db:update_docs(Db0, Docs1, Options1,
replicated_changes),
lists:map(fun couch_httpd_db:update_doc_result_to_json/1,
Errors);
_ ->
Docs1 = lists:map(fun({ObjProps} = JsonObj) ->
Doc = couch_doc:from_json_obj(JsonObj),
validate_attachment_names(Doc),
Id = case Doc#doc.id of
<<>> -> couch_uuids:new();
Id0 -> Id0
end,
case couch_util:get_value(<<"_rev">>, ObjProps) of
undefined ->
Revs = {0, []};
Rev ->
{Pos, RevId} = couch_doc:parse_rev(Rev),
Revs = {Pos, [RevId]}
end,
Doc#doc{id=Id,revs=Revs}
end, Docs),
case couch_db:update_docs(Db0, Docs1, Options) of
{ok, Results} ->
lists:zipwith(fun couch_httpd_db:update_doc_result_to_json/2,
Docs1, Results);
{aborted, Errors} ->
lists:map(fun couch_httpd_db:update_doc_result_to_json/1,
Errors)
end
end
end).

delete_docs(Db, Docs) ->
delete_docs(Db, Docs, []).

delete_docs(Db, Docs, Options) ->
Docs0 = lists:map(fun(Doc) ->
case Doc of
{DocId, DocRev}
when is_binary(DocId) andalso is_binary(DocRev) ->
{[
{<<"_id">>, DocId},
{<<"_rev">>, DocRev},
{<<"_deleted">>, true}]};
{DocProps} ->
{[{<<"_deleted">>, true}|DocProps]}
end
end, Docs),
save_docs(Db, Docs0, Options).

%% utility functions

Expand Down Expand Up @@ -355,14 +416,6 @@ db_admin_exec(Db, Fun) ->
parse_ids_revs(IdsRevs) ->
[{Id, couch_doc:parse_revs(Revs)} || {Id, Revs} <- IdsRevs].

maybe_docid({DocProps}=Doc) ->
case couch_util:get_value(<<"_id">>, DocProps) of
undefined ->
{[{<<"_id">>, get_uuid()}|DocProps]};
_ ->
Doc
end.

dbname(DbName) ->
couch_util:to_binary(DbName).

Expand Down

0 comments on commit 8e89c22

Please sign in to comment.