Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/couch/include/couch_db.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@

-type branch() :: {Key::term(), Value::term(), Tree::term()}.
-type path() :: {Start::pos_integer(), branch()}.
-type update_type() :: replicated_changes | interactive_edit.

-record(rev_info, {
rev,
Expand Down
8 changes: 4 additions & 4 deletions src/couch/src/couch_db.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1148,7 +1148,7 @@ update_docs(Db, Docs0, Options, replicated_changes) ->
end,

{ok, DocBuckets, NonRepDocs, DocErrors}
= before_docs_update(Db, Docs, PrepValidateFun),
= before_docs_update(Db, Docs, PrepValidateFun, replicated_changes),

DocBuckets2 = [[doc_flush_atts(Db, check_dup_atts(Doc))
|| Doc <- Bucket] || Bucket <- DocBuckets],
Expand All @@ -1166,7 +1166,7 @@ update_docs(Db, Docs0, Options, interactive_edit) ->
end,

{ok, DocBuckets, NonRepDocs, DocErrors}
= before_docs_update(Db, Docs, PrepValidateFun),
= before_docs_update(Db, Docs, PrepValidateFun, interactive_edit),

if (AllOrNothing) and (DocErrors /= []) ->
RefErrorDict = dict:from_list([{doc_tag(Doc), Doc} || Doc <- Docs]),
Expand Down Expand Up @@ -1309,7 +1309,7 @@ prepare_doc_summaries(Db, BucketList) ->
Bucket) || Bucket <- BucketList].


before_docs_update(#db{validate_doc_funs = VDFuns} = Db, Docs, PVFun) ->
before_docs_update(#db{validate_doc_funs = VDFuns} = Db, Docs, PVFun, UpdateType) ->
increment_stat(Db, [couchdb, database_writes]),

% Separate _local docs from normal docs
Expand All @@ -1324,7 +1324,7 @@ before_docs_update(#db{validate_doc_funs = VDFuns} = Db, Docs, PVFun) ->
DocBuckets = lists:map(fun(Bucket) ->
lists:map(fun(Doc) ->
DocWithBody = couch_doc:with_ejson_body(Doc),
couch_db_plugin:before_doc_update(Db, DocWithBody)
couch_db_plugin:before_doc_update(Db, DocWithBody, UpdateType)
end, Bucket)
end, BucketList),

Expand Down
12 changes: 7 additions & 5 deletions src/couch/src/couch_db_plugin.erl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

-export([
validate_dbname/3,
before_doc_update/2,
before_doc_update/3,
after_doc_read/2,
validate_docid/1,
check_is_admin/1,
Expand All @@ -34,11 +34,13 @@
validate_dbname(DbName, Normalized, Default) ->
maybe_handle(validate_dbname, [DbName, Normalized], Default).

before_doc_update(Db, Doc0) ->
before_doc_update(Db, Doc0, UpdateType) ->
Fun = couch_db:get_before_doc_update_fun(Db),
case with_pipe(before_doc_update, [Doc0, Db]) of
[Doc1, _Db] when is_function(Fun) -> Fun(Doc1, Db);
[Doc1, _Db] -> Doc1
case with_pipe(before_doc_update, [Doc0, Db, UpdateType]) of
[Doc1, _Db, UpdateType1] when is_function(Fun) ->
Fun(Doc1, Db, UpdateType1);
[Doc1, _Db, _UpdateType] ->
Doc1
end.

after_doc_read(Db, Doc0) ->
Expand Down
4 changes: 2 additions & 2 deletions src/couch/src/couch_server.erl
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,11 @@ maybe_add_sys_db_callbacks(DbName, Options) ->
DbName == NodesDbName ->
[sys_db | Options];
IsReplicatorDb ->
[{before_doc_update, fun couch_replicator_docs:before_doc_update/2},
[{before_doc_update, fun couch_replicator_docs:before_doc_update/3},
{after_doc_read, fun couch_replicator_docs:after_doc_read/2},
sys_db | Options];
IsUsersDb ->
[{before_doc_update, fun couch_users_db:before_doc_update/2},
[{before_doc_update, fun couch_users_db:before_doc_update/3},
{after_doc_read, fun couch_users_db:after_doc_read/2},
sys_db | Options];
true ->
Expand Down
4 changes: 2 additions & 2 deletions src/couch/src/couch_users_db.erl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

-module(couch_users_db).

-export([before_doc_update/2, after_doc_read/2, strip_non_public_fields/1]).
-export([before_doc_update/3, after_doc_read/2, strip_non_public_fields/1]).

-include_lib("couch/include/couch_db.hrl").

Expand All @@ -39,7 +39,7 @@
% -> 404 // Not Found
% Else
% -> save_doc
before_doc_update(Doc, Db) ->
before_doc_update(Doc, Db, _UpdateType) ->
#user_ctx{name=Name} = couch_db:get_user_ctx(Db),
DocName = get_doc_name(Doc),
case (catch couch_db:check_is_admin(Db)) of
Expand Down
17 changes: 10 additions & 7 deletions src/couch/test/couch_db_plugin_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

-export([
validate_dbname/2,
before_doc_update/2,
before_doc_update/3,
after_doc_read/2,
validate_docid/1,
check_is_admin/1,
Expand Down Expand Up @@ -58,9 +58,9 @@ validate_dbname({false, _Db}, _) -> {decided, false};
validate_dbname({fail, _Db}, _) -> throw(validate_dbname);
validate_dbname({pass, _Db}, _) -> no_decision.

before_doc_update({fail, _Doc}, _Db) -> throw(before_doc_update);
before_doc_update({true, Doc}, Db) -> [{true, [before_doc_update|Doc]}, Db];
before_doc_update({false, Doc}, Db) -> [{false, Doc}, Db].
before_doc_update({fail, _Doc}, _Db, interactive_edit) -> throw(before_doc_update);
before_doc_update({true, Doc}, Db, interactive_edit) -> [{true, [before_doc_update|Doc]}, Db, interactive_edit];
before_doc_update({false, Doc}, Db, interactive_edit) -> [{false, Doc}, Db, interactive_edit].

after_doc_read({fail, _Doc}, _Db) -> throw(after_doc_read);
after_doc_read({true, Doc}, Db) -> [{true, [after_doc_read|Doc]}, Db];
Expand Down Expand Up @@ -134,17 +134,20 @@ validate_dbname_pass() ->
before_doc_update_match() ->
?assertMatch(
{true, [before_doc_update, doc]},
couch_db_plugin:before_doc_update(fake_db(), {true, [doc]})).
couch_db_plugin:before_doc_update(
fake_db(), {true, [doc]}, interactive_edit)).

before_doc_update_no_match() ->
?assertMatch(
{false, [doc]},
couch_db_plugin:before_doc_update(fake_db(), {false, [doc]})).
couch_db_plugin:before_doc_update(
fake_db(), {false, [doc]}, interactive_edit)).

before_doc_update_throw() ->
?assertThrow(
before_doc_update,
couch_db_plugin:before_doc_update(fake_db(), {fail, [doc]})).
couch_db_plugin:before_doc_update(
fake_db(), {fail, [doc]}, interactive_edit)).


after_doc_read_match() ->
Expand Down
8 changes: 4 additions & 4 deletions src/couch_replicator/src/couch_replicator_docs.erl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
parse_rep_db/3,
parse_rep_doc_without_id/1,
parse_rep_doc_without_id/2,
before_doc_update/2,
before_doc_update/3,
after_doc_read/2,
ensure_rep_db_exists/0,
ensure_rep_ddoc_exists/1,
Expand Down Expand Up @@ -615,10 +615,10 @@ ssl_verify_options(false) ->
[{verify, verify_none}].


-spec before_doc_update(#doc{}, Db::any()) -> #doc{}.
before_doc_update(#doc{id = <<?DESIGN_DOC_PREFIX, _/binary>>} = Doc, _Db) ->
-spec before_doc_update(#doc{}, Db::any(), couch_db:update_type()) -> #doc{}.
before_doc_update(#doc{id = <<?DESIGN_DOC_PREFIX, _/binary>>} = Doc, _Db, _UpdateType) ->
Doc;
before_doc_update(#doc{body = {Body}} = Doc, Db) ->
before_doc_update(#doc{body = {Body}} = Doc, Db, _UpdateType) ->
#user_ctx{
roles = Roles,
name = Name
Expand Down
6 changes: 4 additions & 2 deletions src/fabric/src/fabric_doc_update.erl
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,13 @@ before_doc_update(DbName, Docs, Opts) ->
{true, _} ->
%% fake db is expensive to create so we only do it if we have to
Db = fabric_util:fake_db(DbName, Opts),
[couch_replicator_docs:before_doc_update(Doc, Db) || Doc <- Docs];
[couch_replicator_docs:before_doc_update(Doc, Db, replicated_changes)
|| Doc <- Docs];
{_, true} ->
%% fake db is expensive to create so we only do it if we have to
Db = fabric_util:fake_db(DbName, Opts),
[couch_users_db:before_doc_update(Doc, Db) || Doc <- Docs];
[couch_users_db:before_doc_update(Doc, Db, interactive_edit)
|| Doc <- Docs];
_ ->
Docs
end.
Expand Down