You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by ii...@apache.org on 2019/10/01 18:08:27 UTC
[couchdb] 04/08: Set request_ctx on database operations
This is an automated email from the ASF dual-hosted git repository.
iilyak pushed a commit to branch opentracing
in repository https://gitbox.apache.org/repos/asf/couchdb.git
commit f4def40cdeb4d2b01083366c3cb03b0dfe622978
Author: ILYA Khlopotov <ii...@apache.org>
AuthorDate: Tue Oct 1 13:59:09 2019 +0000
Set request_ctx on database operations
---
src/fabric/src/fabric2_db.erl | 37 ++++++++++++++++++++++++++++++++++---
src/fabric/src/fabric2_fdb.erl | 33 ++++++++++++++++++++++++++++-----
2 files changed, 62 insertions(+), 8 deletions(-)
diff --git a/src/fabric/src/fabric2_db.erl b/src/fabric/src/fabric2_db.erl
index 9ef0bd3..2c7222c 100644
--- a/src/fabric/src/fabric2_db.erl
+++ b/src/fabric/src/fabric2_db.erl
@@ -45,6 +45,7 @@
get_security/1,
get_update_seq/1,
get_user_ctx/1,
+ get_request_ctx/1,
get_uuid/1,
%% get_purge_seq/1,
%% get_oldest_purge_seq/1,
@@ -123,7 +124,9 @@
validate_dbname/1,
%% make_doc/5,
- new_revid/2
+ new_revid/2,
+ contexts_to_list/1,
+ set_contexts/2
]).
@@ -176,8 +179,9 @@ open(DbName, Options) ->
case fabric2_server:fetch(DbName) of
#{} = Db ->
Db1 = maybe_set_user_ctx(Db, Options),
- ok = check_is_member(Db1),
- {ok, Db1};
+ Db2 = maybe_set_request_ctx(Db1, Options),
+ ok = check_is_member(Db2),
+ {ok, Db2};
undefined ->
Result = fabric2_fdb:transactional(DbName, Options, fun(TxDb) ->
fabric2_fdb:open(TxDb, Options)
@@ -385,6 +389,8 @@ get_user_ctx(#{user_ctx := UserCtx}) ->
get_uuid(#{uuid := UUID}) ->
UUID.
+get_request_ctx(#{request_ctx := RequestCtx}) ->
+ RequestCtx.
is_clustered(#{}) ->
false.
@@ -988,6 +994,13 @@ maybe_set_user_ctx(Db, Options) ->
Db
end.
+maybe_set_request_ctx(Db, Options) ->
+ case fabric2_util:get_value(request_ctx, Options) of
+ #{} = RequestCtx ->
+ Db#{request_ctx => RequestCtx};
+ undefined ->
+ Db
+ end.
is_member(Db, {SecProps}) when is_list(SecProps) ->
case is_admin(Db, {SecProps}) of
@@ -1747,3 +1760,21 @@ stem_revisions(#{} = Db, #doc{} = Doc) ->
true -> Doc#doc{revs = {RevPos, lists:sublist(Revs, RevsLimit)}};
false -> Doc
end.
+
+
+contexts_to_list(Db) ->
+ set_contexts(Db, []).
+
+set_contexts(#{} = Db, KVList0) ->
+ %% Supplied options win
+ RequestCtx = fabric2_fdb:get_request_ctx(Db),
+ UserCtx = fabric2_fdb:get_user_ctx(Db),
+ KVList1 = set_if_not_present(KVList0, request_ctx, RequestCtx),
+ KVList2 = set_if_not_present(KVList1, user_ctx, UserCtx),
+ KVList2.
+
+set_if_not_present(KVList, Key, Value) ->
+ case lists:keymember(Key, 1, KVList) of
+ true -> KVList;
+ false -> [{Key, Value} | KVList]
+ end.
\ No newline at end of file
diff --git a/src/fabric/src/fabric2_fdb.erl b/src/fabric/src/fabric2_fdb.erl
index 5c58da4..8d464b6 100644
--- a/src/fabric/src/fabric2_fdb.erl
+++ b/src/fabric/src/fabric2_fdb.erl
@@ -31,6 +31,10 @@
get_config/2,
set_config/3,
+ get_contexts/1,
+ get_request_ctx/1,
+ get_user_ctx/1,
+
get_stat/2,
incr_stat/3,
@@ -165,6 +169,8 @@ create(#{} = Db0, Options) ->
UserCtx = fabric2_util:get_value(user_ctx, Options, #user_ctx{}),
Options1 = lists:keydelete(user_ctx, 1, Options),
+ RequestCtx = fabric2_util:get_value(request_ctx, Options, #{}),
+ Options2 = lists:keydelete(request_ctx, 1, Options),
Db#{
uuid => UUID,
@@ -174,13 +180,14 @@ create(#{} = Db0, Options) ->
revs_limit => 1000,
security_doc => {[]},
user_ctx => UserCtx,
+ request_ctx => RequestCtx,
validate_doc_update_funs => [],
before_doc_update => undefined,
after_doc_read => undefined,
% All other db things as we add features,
- db_options => Options1
+ db_options => Options2
}.
@@ -202,6 +209,8 @@ open(#{} = Db0, Options) ->
UserCtx = fabric2_util:get_value(user_ctx, Options, #user_ctx{}),
Options1 = lists:keydelete(user_ctx, 1, Options),
+ RequestCtx = fabric2_util:get_value(request_ctx, Options1, #{}),
+ Options2 = lists:keydelete(request_ctx, 1, Options1),
Db2 = Db1#{
db_prefix => DbPrefix,
@@ -210,14 +219,14 @@ open(#{} = Db0, Options) ->
revs_limit => 1000,
security_doc => {[]},
user_ctx => UserCtx,
+ request_ctx => RequestCtx,
% Place holders until we implement these
% bits.
validate_doc_update_funs => [],
before_doc_update => undefined,
after_doc_read => undefined,
-
- db_options => Options1
+ db_options => Options2
},
Db3 = lists:foldl(fun({Key, Val}, DbAcc) ->
@@ -240,10 +249,12 @@ reopen(#{} = OldDb) ->
tx := Tx,
name := DbName,
db_options := Options,
- user_ctx := UserCtx
+ user_ctx := UserCtx,
+ request_ctx := RequestCtx
} = OldDb,
Options1 = lists:keystore(user_ctx, 1, Options, {user_ctx, UserCtx}),
- open(init_db(Tx, DbName, Options1), Options1).
+ Options2 = lists:keystore(request_ctx, 1, Options1, {request_ctx, RequestCtx}),
+ open(init_db(Tx, DbName, Options2), Options2).
delete(#{} = Db) ->
@@ -374,6 +385,18 @@ set_config(#{} = Db, ConfigKey, ConfigVal) ->
bump_db_version(Db).
+get_contexts(#{} = Db) ->
+ [
+ {user_ctx, get_user_ctx(Db)},
+ {request_ctx, get_request_ctx(Db)}
+ ].
+
+get_request_ctx(#{} = Db) ->
+ maps:get(request_ctx, Db, #{}).
+
+get_user_ctx(#{} = Db) ->
+ maps:get(user_ctx, Db, #user_ctx{}).
+
get_stat(#{} = Db, StatKey) ->
#{
tx := Tx,