summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Vatamaniuc <vatamane@apache.org>2019-11-14 13:03:12 -0500
committerNick Vatamaniuc <nickva@users.noreply.github.com>2019-11-14 18:09:53 -0500
commit44f660f157a61a87e773990abc907f8c55883160 (patch)
treedee434a1fe3af6cbc18c2585189e9faffeba896e
parentb71cbe214e3202aee4d4923b5dbd17414e3dc91e (diff)
downloadcouchdb-44f660f157a61a87e773990abc907f8c55883160.tar.gz
Update fabric2_fdb's set_config to take un-encoding values
Previously `set_config/3` needed keys and values to be transalted to binaries, now that is done inside the function. It's a bit more consistent as binary config values and encodings are better encapsulated in the `fabric2_fdb` module. Since `set_config` does, it made sense to update get_config as well. There, it turns out it was used only to load configuration setting after a db open, so the function was renamed to `load_config` and was made private.
-rw-r--r--src/fabric/src/fabric2_db.erl20
-rw-r--r--src/fabric/src/fabric2_fdb.erl40
2 files changed, 26 insertions, 34 deletions
diff --git a/src/fabric/src/fabric2_db.erl b/src/fabric/src/fabric2_db.erl
index ff5371fc3..d957ec954 100644
--- a/src/fabric/src/fabric2_db.erl
+++ b/src/fabric/src/fabric2_db.erl
@@ -455,32 +455,26 @@ is_users_db(DbName) when is_binary(DbName) ->
IsAuthCache orelse IsCfgUsersDb orelse IsGlobalUsersDb.
-set_revs_limit(#{} = Db0, RevsLimit) ->
+set_revs_limit(#{} = Db0, RevsLimit) when is_integer(RevsLimit) ->
Db1 = require_admin_check(Db0),
- RevsLimBin = ?uint2bin(max(1, RevsLimit)),
Resp = fabric2_fdb:transactional(Db1, fun(TxDb) ->
- fabric2_fdb:set_config(TxDb, <<"revs_limit">>, RevsLimBin)
+ fabric2_fdb:set_config(TxDb, revs_limit, RevsLimit)
end),
case Resp of
- {ok, #{} = Db2} ->
- fabric2_server:store(Db2#{revs_limit := RevsLimit});
- Err ->
- Err
+ {ok, #{} = Db2} -> fabric2_server:store(Db2);
+ Err -> Err
end.
set_security(#{} = Db0, Security) ->
Db1 = require_admin_check(Db0),
ok = fabric2_util:validate_security_object(Security),
- SecBin = ?JSON_ENCODE(Security),
Resp = fabric2_fdb:transactional(Db1, fun(TxDb) ->
- fabric2_fdb:set_config(TxDb, <<"security_doc">>, SecBin)
+ fabric2_fdb:set_config(TxDb, security_doc, Security)
end),
case Resp of
- {ok, #{} = Db2} ->
- fabric2_server:store(Db2#{security_doc := Security});
- Err ->
- Err
+ {ok, #{} = Db2} -> fabric2_server:store(Db2);
+ Err -> Err
end.
diff --git a/src/fabric/src/fabric2_fdb.erl b/src/fabric/src/fabric2_fdb.erl
index a3dd7e28b..97f0bc921 100644
--- a/src/fabric/src/fabric2_fdb.erl
+++ b/src/fabric/src/fabric2_fdb.erl
@@ -29,7 +29,6 @@
list_dbs/4,
get_info/1,
- get_config/1,
set_config/3,
get_stat/2,
@@ -226,16 +225,7 @@ open(#{} = Db0, Options) ->
db_options => Options1
},
- Db3 = lists:foldl(fun({Key, Val}, DbAcc) ->
- case Key of
- <<"uuid">> ->
- DbAcc#{uuid => Val};
- <<"revs_limit">> ->
- DbAcc#{revs_limit => ?bin2uint(Val)};
- <<"security_doc">> ->
- DbAcc#{security_doc => ?JSON_DECODE(Val)}
- end
- end, Db2, get_config(Db2)),
+ Db3 = load_config(Db2),
load_validate_doc_funs(Db3).
@@ -367,31 +357,39 @@ get_info(#{} = Db) ->
[CProp | MProps].
-get_config(#{} = Db) ->
+load_config(#{} = Db) ->
#{
tx := Tx,
db_prefix := DbPrefix
- } = ensure_current(Db),
+ } = Db,
{Start, End} = erlfdb_tuple:range({?DB_CONFIG}, DbPrefix),
Future = erlfdb:get_range(Tx, Start, End),
- lists:map(fun({K, V}) ->
+ lists:foldl(fun({K, V}, DbAcc) ->
{?DB_CONFIG, Key} = erlfdb_tuple:unpack(K, DbPrefix),
- {Key, V}
- end, erlfdb:wait(Future)).
+ case Key of
+ <<"uuid">> -> DbAcc#{uuid => V};
+ <<"revs_limit">> -> DbAcc#{revs_limit => ?bin2uint(V)};
+ <<"security_doc">> -> DbAcc#{security_doc => ?JSON_DECODE(V)}
+ end
+ end, Db, erlfdb:wait(Future)).
-set_config(#{} = Db0, ConfigKey, ConfigVal) ->
+set_config(#{} = Db0, Key, Val) when is_atom(Key) ->
#{
tx := Tx,
db_prefix := DbPrefix
} = Db = ensure_current(Db0),
-
- Key = erlfdb_tuple:pack({?DB_CONFIG, ConfigKey}, DbPrefix),
- erlfdb:set(Tx, Key, ConfigVal),
+ {BinKey, BinVal} = case Key of
+ uuid -> {<<"uuid">>, Val};
+ revs_limit -> {<<"revs_limit">>, ?uint2bin(max(1, Val))};
+ security_doc -> {<<"security_doc">>, ?JSON_ENCODE(Val)}
+ end,
+ DbKey = erlfdb_tuple:pack({?DB_CONFIG, BinKey}, DbPrefix),
+ erlfdb:set(Tx, DbKey, BinVal),
{ok, DbVersion} = bump_db_version(Db),
- {ok, Db#{db_version := DbVersion}}.
+ {ok, Db#{db_version := DbVersion, Key := Val}}.
get_stat(#{} = Db, StatKey) ->