You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by va...@apache.org on 2020/04/23 15:52:11 UTC
[couchdb-erlfdb] 01/02: Allow setting some default transaction
options on the db handle
This is an automated email from the ASF dual-hosted git repository.
vatamane pushed a commit to branch add-more-database-options
in repository https://gitbox.apache.org/repos/asf/couchdb-erlfdb.git
commit 792a664ec4870e8a253c6760326a8eb0570d31d4
Author: Nick Vatamaniuc <va...@apache.org>
AuthorDate: Thu Apr 23 00:48:48 2020 -0400
Allow setting some default transaction options on the db handle
Since FDB version 6.1 [1] it is possible to set default transaction
options on database handles, and any transactions created from that
handle will inherit those options.
[1] https://apple.github.io/foundationdb/old-release-notes/release-notes-610.html
---
c_src/main.c | 8 ++++++++
src/erlfdb_nif.erl | 16 +++++++++++-----
test/erlfdb_03_transaction_options_test.erl | 8 ++++++++
3 files changed, 27 insertions(+), 5 deletions(-)
diff --git a/c_src/main.c b/c_src/main.c
index 674b92b..17e15cf 100644
--- a/c_src/main.c
+++ b/c_src/main.c
@@ -752,6 +752,14 @@ erlfdb_database_set_option(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
option = FDB_DB_OPTION_MACHINE_ID;
} else if(IS_ATOM(argv[1], datacenter_id)) {
option = FDB_DB_OPTION_DATACENTER_ID;
+ } else if(IS_ATOM(argv[1], timeout)) {
+ option = FDB_DB_OPTION_TRANSACTION_TIMEOUT;
+ } else if(IS_ATOM(argv[1], retry_limit)) {
+ option = FDB_DB_OPTION_TRANSACTION_RETRY_LIMIT;
+ } else if(IS_ATOM(argv[1], max_retry_delay)) {
+ option = FDB_DB_OPTION_TRANSACTION_MAX_RETRY_DELAY;
+ } else if(IS_ATOM(argv[1], size_limit)) {
+ option = FDB_DB_OPTION_TRANSACTION_SIZE_LIMIT;
} else {
return enif_make_badarg(env);
}
diff --git a/src/erlfdb_nif.erl b/src/erlfdb_nif.erl
index 2c0a944..7ec8e52 100644
--- a/src/erlfdb_nif.erl
+++ b/src/erlfdb_nif.erl
@@ -238,7 +238,8 @@ database_set_option(Database, Option) ->
Value::option_value()
) -> ok.
database_set_option({erlfdb_database, Db}, Opt, Val) ->
- erlfdb_database_set_option(Db, Opt, Val).
+ BinVal = option_val_to_binary(Val),
+ erlfdb_database_set_option(Db, Opt, BinVal).
-spec database_create_transaction(database()) ->
@@ -258,10 +259,7 @@ transaction_set_option(Transaction, Option) ->
Value::option_value()
) -> ok.
transaction_set_option({erlfdb_transaction, Tx}, Opt, Val) ->
- BinVal = case Val of
- B when is_binary(B) -> B;
- I when is_integer(I) -> <<I:8/little-unsigned-integer-unit:8>>
- end,
+ BinVal = option_val_to_binary(Val),
erlfdb_transaction_set_option(Tx, Opt, BinVal).
@@ -452,6 +450,14 @@ error_predicate(Predicate, Error) ->
erlfdb_error_predicate(Predicate, Error).
+-spec option_val_to_binary(binary() | integer()) -> binary().
+option_val_to_binary(Val) when is_binary(Val) ->
+ Val;
+
+option_val_to_binary(Val) when is_integer(Val) ->
+ <<Val:8/little-unsigned-integer-unit:8>>.
+
+
init() ->
PrivDir = case code:priv_dir(?MODULE) of
{error, _} ->
diff --git a/test/erlfdb_03_transaction_options_test.erl b/test/erlfdb_03_transaction_options_test.erl
index 83c7fe7..58447af 100644
--- a/test/erlfdb_03_transaction_options_test.erl
+++ b/test/erlfdb_03_transaction_options_test.erl
@@ -82,5 +82,13 @@ cannot_set_watches_if_writes_disallowed_test() ->
end)).
+size_limit_on_db_handle_test() ->
+ Db1 = erlfdb_util:get_test_db(),
+ erlfdb:set_option(Db1, size_limit, 10000),
+ ?assertError({erlfdb_error, 2101}, erlfdb:transactional(Db1, fun(Tx) ->
+ erlfdb:set(Tx, gen(10), gen(11000))
+ end)).
+
+
gen(Size) ->
crypto:strong_rand_bytes(Size).