Skip to content
Permalink
Browse files
Bump API level to 620
This comes with a few changes:

 * `fdb_future_get_version` was renamed to `fdb_future_get_int64`
 * a new API: `fdb_transaction_get_approximate_size`
 * `size_limit` transaction option

Reference: https://apple.github.io/foundationdb/release-notes.html#bindings
  • Loading branch information
nickva committed Jan 7, 2020
1 parent 85a8253 commit c89b2d6e2294efd19696d391c43861ecc1fae323
Showing 9 changed files with 116 additions and 14 deletions.
@@ -97,6 +97,7 @@ ATOM_MAP(snapshot_ryw_disable);
ATOM_MAP(lock_aware);
ATOM_MAP(used_during_commit_protection_disable);
ATOM_MAP(read_lock_aware);
ATOM_MAP(size_limit);


// Streaming mode
@@ -13,7 +13,7 @@
#ifndef ERLFDB_FDB_H
#define ERLFDB_FDB_H

#define FDB_API_VERSION 610
#define FDB_API_VERSION 620
#include <foundationdb/fdb_c.h>

#endif // Included fdb.h
@@ -178,20 +178,20 @@ erlfdb_future_get_void(ErlNifEnv* env, ErlFDBFuture* f)


static inline ERL_NIF_TERM
erlfdb_future_get_version(ErlNifEnv* env, ErlFDBFuture* f)
erlfdb_future_get_int64(ErlNifEnv* env, ErlFDBFuture* f)
{
int64_t fdb_vsn;
ErlNifSInt64 nif_vsn;
int64_t fdb_res;
ErlNifSInt64 nif_res;
fdb_error_t err;

err = fdb_future_get_version(f->future, &fdb_vsn);
err = fdb_future_get_int64(f->future, &fdb_res);
if(err != 0) {
return erlfdb_erlang_error(env, err);
}

nif_vsn = fdb_vsn;
nif_res = fdb_res;

return enif_make_int64(env, nif_vsn);
return enif_make_int64(env, nif_res);
}


@@ -663,8 +663,8 @@ erlfdb_future_get(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])

if(f->ftype == ErlFDB_FT_VOID) {
return erlfdb_future_get_void(env, f);
} else if(f->ftype == ErlFDB_FT_VERSION) {
return erlfdb_future_get_version(env, f);
} else if(f->ftype == ErlFDB_FT_INT64) {
return erlfdb_future_get_int64(env, f);
} else if(f->ftype == ErlFDB_FT_KEY) {
return erlfdb_future_get_key(env, f);
} else if(f->ftype == ErlFDB_FT_VALUE) {
@@ -903,6 +903,8 @@ erlfdb_transaction_set_option(
option = FDB_TR_OPTION_USED_DURING_COMMIT_PROTECTION_DISABLE;
} else if(IS_ATOM(argv[1], read_lock_aware)) {
option = FDB_TR_OPTION_READ_LOCK_AWARE;
} else if(IS_ATOM(argv[1], size_limit)) {
option = FDB_TR_OPTION_SIZE_LIMIT;
} else {
return enif_make_badarg(env);
}
@@ -999,7 +1001,7 @@ erlfdb_transaction_get_read_version(

future = fdb_transaction_get_read_version(t->transaction);

return erlfdb_create_future(env, future, ErlFDB_FT_VERSION);
return erlfdb_create_future(env, future, ErlFDB_FT_INT64);
}


@@ -1825,6 +1827,41 @@ erlfdb_transaction_add_conflict_range(
}


static ERL_NIF_TERM
erlfdb_transaction_get_approximate_size(
ErlNifEnv* env,
int argc,
const ERL_NIF_TERM argv[]
)
{
ErlFDBSt* st = (ErlFDBSt*) enif_priv_data(env);
ErlFDBTransaction* t;
FDBFuture* future;
void* res;

if(st->lib_state != ErlFDB_CONNECTED) {
return enif_make_badarg(env);
}

if(argc != 1) {
return enif_make_badarg(env);
}

if(!enif_get_resource(env, argv[0], ErlFDBTransactionRes, &res)) {
return enif_make_badarg(env);
}
t = (ErlFDBTransaction*) res;

if(!erlfdb_transaction_is_owner(env, t)) {
return enif_make_badarg(env);
}

future = fdb_transaction_get_approximate_size(t->transaction);

return erlfdb_create_future(env, future, ErlFDB_FT_INT64);
}


static ERL_NIF_TERM
erlfdb_transaction_get_next_tx_id(
ErlNifEnv* env,
@@ -2003,6 +2040,7 @@ static ErlNifFunc funcs[] =
NIF_FUNC(erlfdb_transaction_reset, 1),
NIF_FUNC(erlfdb_transaction_cancel, 1),
NIF_FUNC(erlfdb_transaction_add_conflict_range, 4),
NIF_FUNC(erlfdb_transaction_get_approximate_size, 1),
NIF_FUNC(erlfdb_transaction_get_next_tx_id, 1),
NIF_FUNC(erlfdb_transaction_is_read_only, 1),

@@ -28,7 +28,7 @@ typedef enum _ErlFDBFutureType
{
ErlFDB_FT_NONE = 0,
ErlFDB_FT_VOID,
ErlFDB_FT_VERSION,
ErlFDB_FT_INT64,
ErlFDB_FT_KEY,
ErlFDB_FT_VALUE,
ErlFDB_FT_STRING_ARRAY,
@@ -18,7 +18,7 @@
{maintainers, ["Paul J. Davis"]},
{links, [{"GitHub", "https://github.com/cloudant-labs/couchdb-erlfdb"}]},
{env, [
{api_version, 610},
{api_version, 620},
{network_options, []}
]}
]}.
@@ -103,6 +103,9 @@
get_committed_version/1,
get_versionstamp/1,

% Transaction size info
get_approximate_size/1,

% Transaction status
get_next_tx_id/1,
is_read_only/1,
@@ -568,6 +571,13 @@ get_versionstamp(?IS_SS = SS) ->
get_versionstamp(?GET_TX(SS)).


get_approximate_size(?IS_TX = Tx) ->
erlfdb_nif:transaction_get_approximate_size(Tx);

get_approximate_size(?IS_SS = SS) ->
get_approximate_size(?GET_TX(SS)).


get_next_tx_id(?IS_TX = Tx) ->
erlfdb_nif:transaction_get_next_tx_id(Tx);

@@ -52,13 +52,14 @@
transaction_add_conflict_range/4,
transaction_get_next_tx_id/1,
transaction_is_read_only/1,
transaction_get_approximate_size/1,

get_error/1,
error_predicate/2
]).


-define(DEFAULT_API_VERSION, 610).
-define(DEFAULT_API_VERSION, 620).


-type error() :: {erlfdb_error, Code::integer()}.
@@ -140,7 +141,9 @@
snapshot_ryw_disable |
lock_aware |
used_during_commit_protection_disable |
read_lock_aware.
read_lock_aware |
size_limit.


-type streaming_mode() ::
stream_want_all |
@@ -409,6 +412,11 @@ transaction_add_conflict_range(
erlfdb_transaction_add_conflict_range(Tx, StartKey, EndKey, ConflictType).


-spec transaction_get_approximate_size(transaction()) -> non_neg_integer().
transaction_get_approximate_size({erlfdb_transaction, Tx}) ->
erlfdb_transaction_get_approximate_size(Tx).


-spec transaction_get_next_tx_id(transaction()) -> non_neg_integer().
transaction_get_next_tx_id({erlfdb_transaction, Tx}) ->
erlfdb_transaction_get_next_tx_id(Tx).
@@ -559,6 +567,7 @@ erlfdb_transaction_add_conflict_range(
) -> ?NOT_LOADED.
erlfdb_transaction_get_next_tx_id(_Transaction) -> ?NOT_LOADED.
erlfdb_transaction_is_read_only(_Transaction) -> ?NOT_LOADED.
erlfdb_transaction_get_approximate_size(_Transaction) -> ?NOT_LOADED.


% Misc
@@ -0,0 +1,39 @@
% Licensed under the Apache License, Version 2.0 (the "License"); you may not
% use this file except in compliance with the License. You may obtain a copy of
% the License at
%
% http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
% License for the specific language governing permissions and limitations under
% the License.

-module(erlfdb_03_transaction_size_test).

-include_lib("eunit/include/eunit.hrl").


get_approximate_tx_size_test() ->
Db1 = erlfdb_util:get_test_db(),
erlfdb:transactional(Db1, fun(Tx) ->
ok = erlfdb:set(Tx, gen(10), gen(5000)),
TxSize1 = erlfdb:wait(erlfdb:get_approximate_size(Tx)),
?assert(TxSize1 > 5000 andalso TxSize1 < 6000),
ok = erlfdb:set(Tx, gen(10), gen(5000)),
TxSize2 = erlfdb:wait(erlfdb:get_approximate_size(Tx)),
?assert(TxSize2 > 10000)
end).


size_limit_test() ->
Db1 = erlfdb_util:get_test_db(),
?assertError({erlfdb_error, 2101}, erlfdb:transactional(Db1, fun(Tx) ->
erlfdb:set_option(Tx, size_limit, 10000),
erlfdb:set(Tx, gen(10), gen(11000))
end)).


gen(Size) ->
crypto:strong_rand_bytes(Size).
@@ -651,6 +651,11 @@ execute(TxObj, St, <<"GET_VERSIONSTAMP">>) ->
stack_push(St, VS),
St;

execute(TxObj, St, <<"GET_APPROXIMATE_SIZE">>) ->
erlfdb:wait(erlfdb:get_approximate_size(TxObj)),
stack_push(St, <<"GOT_APPROXIMATE_SIZE">>),
St;

execute(_TxObj, St, <<"TUPLE_PACK">>) ->
Count = stack_pop(St),
Elems = stack_pop(St, Count),

0 comments on commit c89b2d6

Please sign in to comment.