summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjiangph <jiangph@cn.ibm.com>2018-12-13 14:34:47 +0800
committerjiangph <jiangph@cn.ibm.com>2019-01-05 09:53:21 +0800
commitc68863aec58642fc610ede62906609ba6c816a92 (patch)
treebd437af129f3a5c737496a742a842530fc7c4eb9
parentdc931567ae5141e24305baac6c3b4844929aa615 (diff)
downloadcouchdb-c68863aec58642fc610ede62906609ba6c816a92.tar.gz
Update before_doc_update/2 to before_doc_update/3before_doc_update
- Pass UpdateType to before_doc_update/3
-rw-r--r--src/couch/include/couch_db.hrl1
-rw-r--r--src/couch/src/couch_db.erl8
-rw-r--r--src/couch/src/couch_db_plugin.erl12
-rw-r--r--src/couch/src/couch_server.erl4
-rw-r--r--src/couch/src/couch_users_db.erl4
-rw-r--r--src/couch/test/couch_db_plugin_tests.erl17
-rw-r--r--src/couch_replicator/src/couch_replicator_docs.erl8
-rw-r--r--src/fabric/src/fabric_doc_update.erl6
8 files changed, 34 insertions, 26 deletions
diff --git a/src/couch/include/couch_db.hrl b/src/couch/include/couch_db.hrl
index 801483aa1..830b9bcf4 100644
--- a/src/couch/include/couch_db.hrl
+++ b/src/couch/include/couch_db.hrl
@@ -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,
diff --git a/src/couch/src/couch_db.erl b/src/couch/src/couch_db.erl
index 1f8bc422f..2f63fcfe8 100644
--- a/src/couch/src/couch_db.erl
+++ b/src/couch/src/couch_db.erl
@@ -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],
@@ -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]),
@@ -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
@@ -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),
diff --git a/src/couch/src/couch_db_plugin.erl b/src/couch/src/couch_db_plugin.erl
index e25866ec4..c3684c6e3 100644
--- a/src/couch/src/couch_db_plugin.erl
+++ b/src/couch/src/couch_db_plugin.erl
@@ -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,
@@ -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) ->
diff --git a/src/couch/src/couch_server.erl b/src/couch/src/couch_server.erl
index 619ef08ea..df447d1c7 100644
--- a/src/couch/src/couch_server.erl
+++ b/src/couch/src/couch_server.erl
@@ -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 ->
diff --git a/src/couch/src/couch_users_db.erl b/src/couch/src/couch_users_db.erl
index c7b41f1fc..e6e1c4892 100644
--- a/src/couch/src/couch_users_db.erl
+++ b/src/couch/src/couch_users_db.erl
@@ -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").
@@ -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
diff --git a/src/couch/test/couch_db_plugin_tests.erl b/src/couch/test/couch_db_plugin_tests.erl
index 52533fec2..93551adbc 100644
--- a/src/couch/test/couch_db_plugin_tests.erl
+++ b/src/couch/test/couch_db_plugin_tests.erl
@@ -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,
@@ -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];
@@ -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() ->
diff --git a/src/couch_replicator/src/couch_replicator_docs.erl b/src/couch_replicator/src/couch_replicator_docs.erl
index 013475683..bbf9694d7 100644
--- a/src/couch_replicator/src/couch_replicator_docs.erl
+++ b/src/couch_replicator/src/couch_replicator_docs.erl
@@ -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,
@@ -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
diff --git a/src/fabric/src/fabric_doc_update.erl b/src/fabric/src/fabric_doc_update.erl
index ce85aa6a9..b7a27bb4a 100644
--- a/src/fabric/src/fabric_doc_update.erl
+++ b/src/fabric/src/fabric_doc_update.erl
@@ -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.