Skip to content

Commit

Permalink
fix: improve codes && binding tester
Browse files Browse the repository at this point in the history
  • Loading branch information
lafirest committed Nov 24, 2023
1 parent 002bfd9 commit ffcc831
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 47 deletions.
4 changes: 0 additions & 4 deletions c_src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,6 @@ erlfdb_database_open_tenant(
ErlFDBDatabase* d;
ErlFDBTenant* t;
FDBTenant* tenant;
ErlNifPid pid;
ERL_NIF_TERM ret;
void* res;
ErlNifBinary bin;
Expand Down Expand Up @@ -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);
Expand Down
13 changes: 0 additions & 13 deletions c_src/resources.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 0 additions & 2 deletions c_src/resources.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ typedef struct _ErlFDBTransaction
typedef struct _ErlFDBTenant
{
FDBTenant* tenant;
ERL_NIF_TERM owner;
} ErlFDBTenant;

int erlfdb_init_resources(ErlNifEnv* env);
Expand All @@ -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
6 changes: 3 additions & 3 deletions rebar.config
Original file line number Diff line number Diff line change
@@ -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, [
Expand All @@ -12,7 +12,7 @@

{provider_hooks, [
{pre, [
{compile, dynamic_plugin},
% {compile, dynamic_plugin},
{compile, {pc, compile}},
{clean, {pc, clean}}
]}
Expand Down
5 changes: 0 additions & 5 deletions src/erlfdb.erl
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
open/0,
open/1,

open_tenant/2,

create_transaction/1,
transactional/2,
snapshot/1,
Expand Down Expand Up @@ -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) ->
Expand Down
14 changes: 7 additions & 7 deletions src/erlfdb_nif.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 22 additions & 7 deletions src/erlfdb_nif_option.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
57 changes: 57 additions & 0 deletions src/erlfdb_tenant.erl
Original file line number Diff line number Diff line change
@@ -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) ->
<<?TENANT_MAP_PREFIX/binary, Tenant/binary>>.
37 changes: 31 additions & 6 deletions test/tester.es
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

-record(st, {
db,
tenant,
tx_mgr,
tx_name,
instructions,
Expand Down Expand Up @@ -227,14 +228,21 @@ stack_pop_tuples(St, Count) ->
get_transaction(TxName) ->
get({'$erlfdb_tx', TxName}).

new_transaction(Db, TxName) ->
Tx = erlfdb:create_transaction(Db),
new_transaction(#st{db = Db, tenant = Tenant}, TxName) ->
Src =
case Tenant of
undefined ->
Db;
_ ->
Tenant
end,
Tx = erlfdb:create_transaction(Src),
put({'$erlfdb_tx', TxName}, Tx).

switch_transaction(Db, TxName) ->
switch_transaction(St, TxName) ->
case get_transaction(TxName) of
undefined ->
new_transaction(Db, TxName);
new_transaction(St, TxName);
_ ->
ok
end.
Expand Down Expand Up @@ -305,6 +313,7 @@ init_run_loop(Db, Prefix) ->
{StartKey, EndKey} = erlfdb_tuple:range({Prefix}),
St = #st{
db = Db,
tenant = undefined,
tx_name = Prefix,
instructions = erlfdb:get_range(Db, StartKey, EndKey),
op_tuple = undefined,
Expand Down Expand Up @@ -443,11 +452,11 @@ execute(_TxObj, St, <<"WAIT_FUTURE">>) ->
stack_push(St#st.stack, Value),
St;
execute(_TxObj, St, <<"NEW_TRANSACTION">>) ->
new_transaction(St#st.db, St#st.tx_name),
new_transaction(St, St#st.tx_name),
St;
execute(_TxObj, St, <<"USE_TRANSACTION">>) ->
TxName = stack_pop(St),
switch_transaction(St#st.db, TxName),
switch_transaction(St, TxName),
St#st{
tx_name = TxName
};
Expand Down Expand Up @@ -756,6 +765,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{db = Db} = St, <<"TENANT_SET_ACTIVE">>) ->
Tenant = stack_pop(St),
Result = erlfdb_tenant:open_tenant(Db, Tenant),
St#st{tenant = Result};
execute(_TxObj, St, <<"TENANT_CLEAR_ACTIVE">>) ->
St#st{tenant = undefined};
execute(_TxObj, _St, UnknownOp) ->
erlang:error({unknown_op, UnknownOp}).

Expand Down

0 comments on commit ffcc831

Please sign in to comment.