From 357998103dc360d9d8e444b0b7118941d210e8c0 Mon Sep 17 00:00:00 2001 From: firest Date: Fri, 24 Nov 2023 18:28:22 +0800 Subject: [PATCH] fix: improve codes && binding tester --- c_src/main.c | 4 --- c_src/resources.c | 13 --------- c_src/resources.h | 2 -- rebar.config | 6 ++--- src/erlfdb.erl | 5 ---- src/erlfdb_nif.erl | 14 +++++----- src/erlfdb_nif_option.erl | 29 +++++++++++++++----- src/erlfdb_tenant.erl | 57 +++++++++++++++++++++++++++++++++++++++ test/tester.es | 16 +++++++++++ 9 files changed, 105 insertions(+), 41 deletions(-) create mode 100644 src/erlfdb_tenant.erl diff --git a/c_src/main.c b/c_src/main.c index 28271a6..a604b05 100644 --- a/c_src/main.c +++ b/c_src/main.c @@ -804,7 +804,6 @@ erlfdb_database_open_tenant( ErlFDBDatabase* d; ErlFDBTenant* t; FDBTenant* tenant; - ErlNifPid pid; ERL_NIF_TERM ret; void* res; ErlNifBinary bin; @@ -835,9 +834,6 @@ erlfdb_database_open_tenant( t = enif_alloc_resource(ErlFDBTenantRes, sizeof(ErlFDBTenant)); t->tenant = tenant; - enif_self(env, &pid); - t->owner = enif_make_pid(env, &pid); - ret = enif_make_resource(env, t); enif_release_resource(t); return T2(env, ATOM_erlfdb_tenant, ret); diff --git a/c_src/resources.c b/c_src/resources.c index 38487d3..7c4e392 100644 --- a/c_src/resources.c +++ b/c_src/resources.c @@ -137,16 +137,3 @@ erlfdb_transaction_is_owner(ErlNifEnv* env, ErlFDBTransaction* t) return enif_compare(t->owner, self) == 0; } - - -int -erlfdb_tenant_is_owner(ErlNifEnv* env, ErlFDBTenant* t) -{ - ErlNifPid pid; - ERL_NIF_TERM self; - - enif_self(env, &pid); - self = enif_make_pid(env, &pid); - - return enif_compare(t->owner, self) == 0; -} diff --git a/c_src/resources.h b/c_src/resources.h index c0a6caa..8dd9f7c 100644 --- a/c_src/resources.h +++ b/c_src/resources.h @@ -70,7 +70,6 @@ typedef struct _ErlFDBTransaction typedef struct _ErlFDBTenant { FDBTenant* tenant; - ERL_NIF_TERM owner; } ErlFDBTenant; int erlfdb_init_resources(ErlNifEnv* env); @@ -80,7 +79,6 @@ void erlfdb_transaction_dtor(ErlNifEnv* env, void* obj); void erlfdb_tenant_dtor(ErlNifEnv* env, void* obj); int erlfdb_transaction_is_owner(ErlNifEnv* env, ErlFDBTransaction* t); -int erlfdb_tenant_is_owner(ErlNifEnv* env, ErlFDBTenant* t); #endif // Included resources.h diff --git a/rebar.config b/rebar.config index 19e7b28..0ba483f 100644 --- a/rebar.config +++ b/rebar.config @@ -1,7 +1,7 @@ {plugins, [ coveralls, - pc, - {rebar3_dynamic_plugin, {git, "https://github.com/lafirest/rebar3_dynamic_plugin.git"}} + pc +% {rebar3_dynamic_plugin, {git, "https://github.com/lafirest/rebar3_dynamic_plugin.git"}} ]}. {project_plugins, [ @@ -12,7 +12,7 @@ {provider_hooks, [ {pre, [ - {compile, dynamic_plugin}, +% {compile, dynamic_plugin}, {compile, {pc, compile}}, {clean, {pc, clean}} ]} diff --git a/src/erlfdb.erl b/src/erlfdb.erl index d114d68..90a319b 100644 --- a/src/erlfdb.erl +++ b/src/erlfdb.erl @@ -18,8 +18,6 @@ open/0, open/1, - open_tenant/2, - create_transaction/1, transactional/2, snapshot/1, @@ -157,9 +155,6 @@ open() -> open(ClusterFile) -> erlfdb_nif:create_database(ClusterFile). -open_tenant(?IS_DB = Db, Name) -> - erlfdb_nif:database_open_tenant(Db, Name). - create_transaction(?IS_DB = Db) -> erlfdb_nif:database_create_transaction(Db); create_transaction(?IS_TENANT = Tenant) -> diff --git a/src/erlfdb_nif.erl b/src/erlfdb_nif.erl index a3efa8c..1d97554 100644 --- a/src/erlfdb_nif.erl +++ b/src/erlfdb_nif.erl @@ -194,13 +194,13 @@ | disallow_writes. -type streaming_mode() :: - stream_want_all - | stream_iterator - | stream_exact - | stream_small - | stream_medium - | stream_large - | stream_serial. + want_all + | iterator + | exact + | small + | medium + | large + | serial. -type atomic_mode() :: add diff --git a/src/erlfdb_nif_option.erl b/src/erlfdb_nif_option.erl index 88156f5..88dc917 100644 --- a/src/erlfdb_nif_option.erl +++ b/src/erlfdb_nif_option.erl @@ -235,19 +235,34 @@ to_transaction_option(disallow_writes) -> to_transaction_option(_) -> error(badarg). -to_stream_mode(stream_want_all) -> +to_stream_mode(want_all) -> ?FDB_STREAMING_MODE_WANT_ALL; -to_stream_mode(stream_iterator) -> +to_stream_mode(iterator) -> ?FDB_STREAMING_MODE_ITERATOR; -to_stream_mode(stream_exact) -> +to_stream_mode(exact) -> ?FDB_STREAMING_MODE_EXACT; -to_stream_mode(stream_small) -> +to_stream_mode(small) -> ?FDB_STREAMING_MODE_SMALL; -to_stream_mode(stream_medium) -> +to_stream_mode(medium) -> ?FDB_STREAMING_MODE_MEDIUM; -to_stream_mode(stream_large) -> +to_stream_mode(large) -> ?FDB_STREAMING_MODE_LARGE; -to_stream_mode(stream_serial) -> +to_stream_mode(serial) -> + ?FDB_STREAMING_MODE_SERIAL; +%% the below codes are special for the bindingtester +to_stream_mode(?FDB_STREAMING_MODE_WANT_ALL) -> + ?FDB_STREAMING_MODE_WANT_ALL; +to_stream_mode(?FDB_STREAMING_MODE_ITERATOR) -> + ?FDB_STREAMING_MODE_ITERATOR; +to_stream_mode(?FDB_STREAMING_MODE_EXACT) -> + ?FDB_STREAMING_MODE_EXACT; +to_stream_mode(?FDB_STREAMING_MODE_SMALL) -> + ?FDB_STREAMING_MODE_SMALL; +to_stream_mode(?FDB_STREAMING_MODE_MEDIUM) -> + ?FDB_STREAMING_MODE_MEDIUM; +to_stream_mode(?FDB_STREAMING_MODE_LARGE) -> + ?FDB_STREAMING_MODE_LARGE; +to_stream_mode(?FDB_STREAMING_MODE_SERIAL) -> ?FDB_STREAMING_MODE_SERIAL; to_stream_mode(_) -> error(badarg). diff --git a/src/erlfdb_tenant.erl b/src/erlfdb_tenant.erl new file mode 100644 index 0000000..b78612a --- /dev/null +++ b/src/erlfdb_tenant.erl @@ -0,0 +1,57 @@ +% 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_tenant). + +-export([create_tenant/2, open_tenant/2, delete_tenant/2]). + +-define(IS_DB, {erlfdb_database, _}). +-define(TENANT_MAP_PREFIX, <<16#FF, 16#FF, "/management/tenant_map/">>). + +-import(erlfdb, [get/2, clear/2, set/3, wait/1, transactional/2, set_option/2]). + +create_tenant(?IS_DB = Db, Tenant) -> + transactional(Db, fun(Tx) -> + create_tenant(Tx, Tenant) + end); +create_tenant(Tx, Tenant) -> + set_option(Tx, special_key_space_enable_writes), + Key = tenant_key(Tenant), + case check_tenant_existence(Tx, Key) of + not_found -> + set(Tx, Key, <<>>); + _ -> + {error, tenant_already_exists} + end. + +open_tenant(?IS_DB = Db, Name) -> + erlfdb_nif:database_open_tenant(Db, Name). + +delete_tenant(?IS_DB = Db, Tenant) -> + transactional(Db, fun(Tx) -> + delete_tenant(Tx, Tenant) + end); +delete_tenant(Tx, Tenant) -> + set_option(Tx, special_key_space_enable_writes), + Key = tenant_key(Tenant), + case check_tenant_existence(Tx, Key) of + not_found -> + {error, tenant_not_found}; + _ -> + clear(Tx, Key) + end. + +check_tenant_existence(Tx, Key) -> + wait(get(Tx, Key)). + +tenant_key(Tenant) -> + <>. diff --git a/test/tester.es b/test/tester.es index 17250ad..f27ec84 100755 --- a/test/tester.es +++ b/test/tester.es @@ -756,6 +756,22 @@ execute(TxObj, St, <<"GET_RANGE_SPLIT_POINTS">>) -> Result = erlfdb:get_range_split_points(TxObj, Start, End, ChunkSize), stack_push_range(St, Result), St; +execute(TxObj, St, <<"TENANT_CREATE">>) -> + Tenant = stack_pop(St), + _Result = erlfdb_tenant:create_tenant(TxObj, Tenant), + stack_push(St, <<"RESULT_NOT_PRESENT">>), + St; +execute(TxObj, St, <<"TENANT_DELETE">>) -> + Tenant = stack_pop(St), + _Result = erlfdb_tenant:delete_tenant(TxObj, Tenant), + stack_push(St, <<"RESULT_NOT_PRESENT">>), + St; +execute(TxObj, St, <<"TENANT_SET_ACTIVE">>) -> + Tenant = stack_pop(St), + _Result = erlfdb_tenant:open_tenant(TxObj, Tenant), + St; +execute(_TxObj, St, <<"TENANT_CLEAR_ACTIVE">>) -> + St; execute(_TxObj, _St, UnknownOp) -> erlang:error({unknown_op, UnknownOp}).