Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds FDB Tenants #55

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions c_src/atom_names.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ ATOM_MAP(not_found);
ATOM_MAP(erlfdb_error);
ATOM_MAP(erlfdb_future);
ATOM_MAP(erlfdb_database);
ATOM_MAP(erlfdb_tenant);
ATOM_MAP(erlfdb_transaction);

ATOM_MAP(invalid_future_type);
Expand Down Expand Up @@ -109,6 +110,7 @@ ATOM_MAP(disallow_writes);
ATOM_MAP(include_port_in_address);
ATOM_MAP(use_provisional_proxies);
ATOM_MAP(report_conflicting_keys);
ATOM_MAP(special_key_space_enable_writes);


// Streaming mode
Expand Down
5 changes: 4 additions & 1 deletion c_src/fdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@
#ifndef ERLFDB_FDB_H
#define ERLFDB_FDB_H

// FDB_LATEST_API_VERSION
#include <foundationdb/fdb_c_apiversion.g.h>

#ifndef FDB_API_VERSION
#define FDB_API_VERSION 620
#define FDB_API_VERSION FDB_LATEST_API_VERSION
#endif // Allow command-line override of default FDB_API_VERSION

#include <foundationdb/fdb_c.h>
Expand Down
104 changes: 104 additions & 0 deletions c_src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,54 @@ erlfdb_create_database(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
}


static ERL_NIF_TERM
erlfdb_database_open_tenant(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
ErlFDBSt* st = (ErlFDBSt*) enif_priv_data(env);
ErlNifBinary bin;
ErlFDBDatabase* d;
ErlFDBTenant* ten;
FDBTenant* tenant;
fdb_error_t err;
ERL_NIF_TERM ret;
void* res;

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

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

if(!enif_get_resource(env, argv[0], ErlFDBDatabaseRes, &res)) {
return enif_make_badarg(env);
}
d = (ErlFDBDatabase*) res;

if(!enif_inspect_binary(env, argv[1], &bin)) {
return enif_make_badarg(env);
}

if(bin.size < 1 || bin.data[bin.size - 1] != 0) {
return enif_make_badarg(env);
}

err = fdb_database_open_tenant(d->database, (const unsigned char*) bin.data, bin.size-1, &tenant);
if(err != 0) {
return erlfdb_erlang_error(env, err);
}

ten = enif_alloc_resource(ErlFDBTenantRes, sizeof(ErlFDBTenant));
ten->tenant = tenant;

ret = enif_make_resource(env, ten);
enif_release_resource(ten);

return T2(env, ATOM_erlfdb_tenant, ret);
}


static ERL_NIF_TERM
erlfdb_database_set_option(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
Expand Down Expand Up @@ -887,6 +935,56 @@ erlfdb_database_create_transaction(
return T2(env, ATOM_erlfdb_transaction, ret);
}

static ERL_NIF_TERM
erlfdb_tenant_create_transaction(
ErlNifEnv* env,
int argc,
const ERL_NIF_TERM argv[]
)
{
ErlFDBSt* st = (ErlFDBSt*) enif_priv_data(env);
ErlFDBTenant* ten;
ErlFDBTransaction* t;
FDBTransaction* transaction;
ErlNifPid pid;
ERL_NIF_TERM ret;
void* res;
fdb_error_t err;

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], ErlFDBTenantRes, &res)) {
return enif_make_badarg(env);
}
ten = (ErlFDBTenant*) res;

err = fdb_tenant_create_transaction(ten->tenant, &transaction);
if(err != 0) {
return erlfdb_erlang_error(env, err);
}

t = enif_alloc_resource(ErlFDBTransactionRes, sizeof(ErlFDBTransaction));
t->transaction = transaction;

enif_self(env, &pid);
t->owner = enif_make_pid(env, &pid);

t->txid = 0;
t->read_only = true;
t->writes_allowed = true;
t->has_watches = false;

ret = enif_make_resource(env, t);
enif_release_resource(t);
return T2(env, ATOM_erlfdb_transaction, ret);
}


static ERL_NIF_TERM
erlfdb_transaction_set_option(
Expand Down Expand Up @@ -994,6 +1092,10 @@ erlfdb_transaction_set_option(
#if FDB_API_VERSION > 620
} else if(IS_ATOM(argv[1], report_conflicting_keys)) {
option = FDB_TR_OPTION_REPORT_CONFLICTING_KEYS;
#endif
#if FDB_API_VERSION > 630
} else if(IS_ATOM(argv[1], special_key_space_enable_writes)) {
option = FDB_TR_OPTION_SPECIAL_KEY_SPACE_ENABLE_WRITES;
#endif
} else {
return enif_make_badarg(env);
Expand Down Expand Up @@ -2261,7 +2363,9 @@ static ErlNifFunc funcs[] =

NIF_FUNC(erlfdb_create_database, 1),
NIF_FUNC(erlfdb_database_set_option, 3),
NIF_FUNC(erlfdb_database_open_tenant, 2),
NIF_FUNC(erlfdb_database_create_transaction, 1),
NIF_FUNC(erlfdb_tenant_create_transaction, 1),

NIF_FUNC(erlfdb_transaction_set_option, 3),
NIF_FUNC(erlfdb_transaction_set_read_version, 2),
Expand Down
24 changes: 24 additions & 0 deletions c_src/resources.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

ErlNifResourceType* ErlFDBFutureRes;
ErlNifResourceType* ErlFDBDatabaseRes;
ErlNifResourceType* ErlFDBTenantRes;
ErlNifResourceType* ErlFDBTransactionRes;


Expand Down Expand Up @@ -46,6 +47,18 @@ erlfdb_init_resources(ErlNifEnv* env)
return 0;
}

ErlFDBTenantRes = enif_open_resource_type(
env,
NULL,
"erlfdb_tenant",
erlfdb_tenant_dtor,
ERL_NIF_RT_CREATE,
NULL
);
if(ErlFDBTenantRes == NULL) {
return 0;
}

ErlFDBTransactionRes = enif_open_resource_type(
env,
NULL,
Expand Down Expand Up @@ -92,6 +105,17 @@ erlfdb_database_dtor(ErlNifEnv* env, void* obj)
}


void
erlfdb_tenant_dtor(ErlNifEnv* env, void* obj)
{
ErlFDBTenant* ten = (ErlFDBTenant*) obj;

if(ten->tenant != NULL) {
fdb_tenant_destroy(ten->tenant);
}
}


void
erlfdb_transaction_dtor(ErlNifEnv* env, void* obj)
{
Expand Down
6 changes: 6 additions & 0 deletions c_src/resources.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

extern ErlNifResourceType* ErlFDBFutureRes;
extern ErlNifResourceType* ErlFDBDatabaseRes;
extern ErlNifResourceType* ErlFDBTenantRes;
extern ErlNifResourceType* ErlFDBTransactionRes;


Expand Down Expand Up @@ -54,6 +55,10 @@ typedef struct _ErlFDBDatabase
FDBDatabase* database;
} ErlFDBDatabase;

typedef struct _ErlFDBTenant
{
FDBTenant* tenant;
} ErlFDBTenant;

typedef struct _ErlFDBTransaction
{
Expand All @@ -69,6 +74,7 @@ typedef struct _ErlFDBTransaction
int erlfdb_init_resources(ErlNifEnv* env);
void erlfdb_future_dtor(ErlNifEnv* env, void* obj);
void erlfdb_database_dtor(ErlNifEnv* env, void* obj);
void erlfdb_tenant_dtor(ErlNifEnv* env, void* obj);
void erlfdb_transaction_dtor(ErlNifEnv* env, void* obj);


Expand Down
2 changes: 1 addition & 1 deletion src/erlfdb.app.src
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
{maintainers, ["Paul J. Davis"]},
{links, [{"GitHub", "https://github.com/cloudant-labs/couchdb-erlfdb"}]},
{env, [
{api_version, 620},
{api_version, 730},
{network_options, []}
]}
]}.
15 changes: 15 additions & 0 deletions src/erlfdb.erl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@
open/0,
open/1,

open_tenant/2,

create_transaction/1,
tenant_create_transaction/1,

transactional/2,
snapshot/1,

Expand Down Expand Up @@ -127,6 +131,7 @@
-define(IS_FUTURE, {erlfdb_future, _, _}).
-define(IS_FOLD_FUTURE, {fold_info, _, _}).
-define(IS_DB, {erlfdb_database, _}).
-define(IS_TENANT, {erlfdb_tenant, _}).
-define(IS_TX, {erlfdb_transaction, _}).
-define(IS_SS, {erlfdb_snapshot, _}).
-define(GET_TX(SS), element(2, SS)).
Expand All @@ -149,13 +154,23 @@ open() ->
open(ClusterFile) ->
erlfdb_nif:create_database(ClusterFile).

open_tenant(?IS_DB = Db, TenantName) ->
erlfdb_nif:database_open_tenant(Db, TenantName).

create_transaction(?IS_DB = Db) ->
erlfdb_nif:database_create_transaction(Db).

tenant_create_transaction(?IS_TENANT = Tenant) ->
erlfdb_nif:tenant_create_transaction(Tenant).

transactional(?IS_DB = Db, UserFun) when is_function(UserFun, 1) ->
clear_erlfdb_error(),
Tx = create_transaction(Db),
do_transaction(Tx, UserFun);
transactional(?IS_TENANT = Tenant, UserFun) when is_function(UserFun, 1) ->
clear_erlfdb_error(),
Tx = tenant_create_transaction(Tenant),
do_transaction(Tx, UserFun);
transactional(?IS_TX = Tx, UserFun) when is_function(UserFun, 1) ->
UserFun(Tx);
transactional(?IS_SS = SS, UserFun) when is_function(UserFun, 1) ->
Expand Down
27 changes: 26 additions & 1 deletion src/erlfdb_nif.erl
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@
future_get/1,

create_database/1,
database_open_tenant/2,
database_set_option/2,
database_set_option/3,
database_create_transaction/1,
tenant_create_transaction/1,

transaction_set_option/2,
transaction_set_option/3,
Expand Down Expand Up @@ -62,11 +64,12 @@
error_predicate/2
]).

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

-type error() :: {erlfdb_error, Code :: integer()}.
-type future() :: {erlfdb_future, reference(), reference()}.
-type database() :: {erlfdb_database, reference()}.
-type tenant() :: {erlfdb_tenant, reference()}.
-type transaction() :: {erlfdb_transaction, reference()}.

-type option_value() :: integer() | binary().
Expand Down Expand Up @@ -224,6 +227,22 @@ create_database(ClusterFilePath) ->
end,
erlfdb_create_database(NifPath).

-spec database_open_tenant(database(), TenantName :: binary()) -> database().
database_open_tenant(Database, <<>>) ->
database_open_tenant(Database, <<0>>);
database_open_tenant({erlfdb_database, Database}, TenantName) ->
Size = size(TenantName) - 1,
% Make sure we pass a NULL-terminated string
% to FoundationDB
NifTenant =
case TenantName of
<<_:Size/binary, 0>> ->
TenantName;
_ ->
<<TenantName/binary, 0>>
end,
erlfdb_database_open_tenant(Database, NifTenant).

-spec database_set_option(database(), Option :: database_option()) -> ok.
database_set_option(Database, Option) ->
database_set_option(Database, Option, <<>>).
Expand All @@ -241,6 +260,10 @@ database_set_option({erlfdb_database, Db}, Opt, Val) ->
database_create_transaction({erlfdb_database, Db}) ->
erlfdb_database_create_transaction(Db).

-spec tenant_create_transaction(tenant()) -> transaction().
tenant_create_transaction({erlfdb_tenant, T}) ->
erlfdb_tenant_create_transaction(T).

-spec transaction_set_option(transaction(), Option :: transaction_option()) -> ok.
transaction_set_option(Transaction, Option) ->
transaction_set_option(Transaction, Option, <<>>).
Expand Down Expand Up @@ -512,8 +535,10 @@ erlfdb_future_get(_Future) -> ?NOT_LOADED.

% Databases
erlfdb_create_database(_ClusterFilePath) -> ?NOT_LOADED.
erlfdb_database_open_tenant(_Database, _TenantName) -> ?NOT_LOADED.
erlfdb_database_set_option(_Database, _DatabaseOption, _Value) -> ?NOT_LOADED.
erlfdb_database_create_transaction(_Database) -> ?NOT_LOADED.
erlfdb_tenant_create_transaction(_Tenant) -> ?NOT_LOADED.

% Transactions
erlfdb_transaction_set_option(_Transaction, _TransactionOption, _Value) -> ?NOT_LOADED.
Expand Down
Loading
Loading