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

feat: use function pointers instead of function enums #12

Merged
merged 2 commits into from
Feb 28, 2024
Merged
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
50 changes: 19 additions & 31 deletions c_src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ erlfdb_future_cb(FDBFuture* fdb_future, void* data)


static ERL_NIF_TERM
erlfdb_create_future(ErlNifEnv* env, FDBFuture* future, ErlFDBFutureType ftype)
erlfdb_create_future(ErlNifEnv* env, FDBFuture* future, ErlFDBFutureGetter getter)
{
ErlFDBFuture* f;
ERL_NIF_TERM ref = enif_make_ref(env);
Expand All @@ -117,7 +117,7 @@ erlfdb_create_future(ErlNifEnv* env, FDBFuture* future, ErlFDBFutureType ftype)

f = enif_alloc_resource(ErlFDBFutureRes, sizeof(ErlFDBFuture));
f->future = future;
f->ftype = ftype;
f->fgetter = getter;
enif_self(env, &(f->pid));
f->pid_env = env;
f->msg_env = enif_alloc_env();
Expand Down Expand Up @@ -681,23 +681,11 @@ erlfdb_future_get(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
}
f = (ErlFDBFuture*) res;

if(f->ftype == ErlFDB_FT_VOID) {
return erlfdb_future_get_void(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) {
return erlfdb_future_get_value(env, f);
} else if(f->ftype == ErlFDB_FT_STRING_ARRAY) {
return erlfdb_future_get_string_array(env, f);
} else if(f->ftype == ErlFDB_FT_KEYVALUE_ARRAY) {
return erlfdb_future_get_keyvalue_array(env, f);
} else if(f->ftype == ErlFDB_FT_KEY_ARRAY) {
return erlfdb_future_get_key_array(env, f);
}

return enif_raise_exception(env, ATOM_invalid_future_type);
if(f->fgetter == NULL) {
return enif_raise_exception(env, ATOM_invalid_future_type);
}

return f->fgetter(env, f);
}


Expand Down Expand Up @@ -1085,7 +1073,7 @@ erlfdb_transaction_get_read_version(

future = fdb_transaction_get_read_version(t->transaction);

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


Expand Down Expand Up @@ -1135,7 +1123,7 @@ erlfdb_transaction_get(
snapshot
);

return erlfdb_create_future(env, future, ErlFDB_FT_VALUE);
return erlfdb_create_future(env, future, erlfdb_future_get_value);
}

#if FDB_API_VERSION >= 630
Expand Down Expand Up @@ -1186,7 +1174,7 @@ erlfdb_transaction_get_estimated_range_size(
ekey.size
);

return erlfdb_create_future(env, future, ErlFDB_FT_INT64);
return erlfdb_create_future(env, future, erlfdb_future_get_int64);
}
#endif

Expand Down Expand Up @@ -1243,7 +1231,7 @@ erlfdb_transaction_get_range_split_points(
chunkSize
);

return erlfdb_create_future(env, future, ErlFDB_FT_KEY_ARRAY);
return erlfdb_create_future(env, future, erlfdb_future_get_key_array);
}

static ERL_NIF_TERM
Expand Down Expand Up @@ -1296,7 +1284,7 @@ erlfdb_transaction_get_key(
snapshot
);

return erlfdb_create_future(env, future, ErlFDB_FT_KEY);
return erlfdb_create_future(env, future, erlfdb_future_get_key);
}


Expand Down Expand Up @@ -1340,7 +1328,7 @@ erlfdb_transaction_get_addresses_for_key(
key.size
);

return erlfdb_create_future(env, future, ErlFDB_FT_STRING_ARRAY);
return erlfdb_create_future(env, future, erlfdb_future_get_string_array);
}

static ERL_NIF_TERM
Expand Down Expand Up @@ -1442,7 +1430,7 @@ erlfdb_transaction_get_range(
reverse
);

return erlfdb_create_future(env, future, ErlFDB_FT_KEYVALUE_ARRAY);
return erlfdb_create_future(env, future, erlfdb_future_get_keyvalue_array);
}


Expand Down Expand Up @@ -1699,7 +1687,7 @@ erlfdb_transaction_commit(

future = fdb_transaction_commit(t->transaction);

return erlfdb_create_future(env, future, ErlFDB_FT_VOID);
return erlfdb_create_future(env, future, erlfdb_future_get_void);
}


Expand Down Expand Up @@ -1775,7 +1763,7 @@ erlfdb_transaction_get_versionstamp(

future = fdb_transaction_get_versionstamp(t->transaction);

return erlfdb_create_future(env, future, ErlFDB_FT_KEY);
return erlfdb_create_future(env, future, erlfdb_future_get_key);
}


Expand Down Expand Up @@ -1827,7 +1815,7 @@ erlfdb_transaction_watch(
);

t->has_watches = true;
return erlfdb_create_future(env, future, ErlFDB_FT_VOID);
return erlfdb_create_future(env, future, erlfdb_future_get_void);
}


Expand Down Expand Up @@ -1870,7 +1858,7 @@ erlfdb_transaction_on_error(

future = fdb_transaction_on_error(t->transaction, fdb_err);

return erlfdb_create_future(env, future, ErlFDB_FT_VOID);
return erlfdb_create_future(env, future, erlfdb_future_get_void);
}


Expand Down Expand Up @@ -2049,7 +2037,7 @@ erlfdb_transaction_get_approximate_size(

future = fdb_transaction_get_approximate_size(t->transaction);

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


Expand Down
22 changes: 6 additions & 16 deletions c_src/resources.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,20 @@ extern ErlNifResourceType* ErlFDBTransactionRes;
extern ErlNifResourceType* ErlFDBTenantRes;


typedef enum _ErlFDBFutureType
{
ErlFDB_FT_NONE = 0,
ErlFDB_FT_VOID,
ErlFDB_FT_INT64,
ErlFDB_FT_KEY,
ErlFDB_FT_VALUE,
ErlFDB_FT_STRING_ARRAY,
ErlFDB_FT_KEYVALUE_ARRAY,
ErlFDB_FT_KEY_ARRAY
} ErlFDBFutureType;


typedef struct _ErlFDBFuture
typedef struct _ErlFDBFuture ErlFDBFuture;
typedef ERL_NIF_TERM (*ErlFDBFutureGetter)(ErlNifEnv*, ErlFDBFuture*);
struct _ErlFDBFuture
{

FDBFuture* future;
ErlFDBFutureType ftype;
ErlFDBFutureGetter fgetter;
ErlNifPid pid;
ErlNifEnv* pid_env;
ErlNifEnv* msg_env;
ERL_NIF_TERM msg_ref;
ErlNifMutex* lock;
bool cancelled;
} ErlFDBFuture;
};


typedef struct _ErlFDBDatabase
Expand Down
11 changes: 11 additions & 0 deletions scripts/gen_ccls.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

template=$(cat <<EOF
clang
%c -std=c11
-I./include/
-I/usr/include/foundationdb/
-I$(erl -eval 'io:format("~s", [lists:concat([code:root_dir(), "/erts-", erlang:system_info(version), "/include"])])' -s init stop -noshell)
EOF
)
echo "$template"
8 changes: 8 additions & 0 deletions src/erlfdb.erl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@

% Future Specific functions
is_ready/1,
ready_ref/1,
ready_event/1,
get/1,
get_error/1,
block_until_ready/1,
Expand Down Expand Up @@ -208,6 +210,12 @@ cancel(?IS_FUTURE = Future, Options) ->
is_ready(?IS_FUTURE = Future) ->
erlfdb_nif:future_is_ready(Future).

ready_ref({erlfdb_future, MsgRef, _Res}) ->
MsgRef.

ready_event({erlfdb_future, MsgRef, _Res}) ->
{MsgRef, ready}.

get_error(?IS_FUTURE = Future) ->
erlfdb_nif:future_get_error(Future).

Expand Down
Loading