diff --git a/c_src/main.c b/c_src/main.c index a604b05..006303b 100644 --- a/c_src/main.c +++ b/c_src/main.c @@ -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); @@ -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(); @@ -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); } @@ -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); } @@ -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 @@ -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 @@ -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 @@ -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); } @@ -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 @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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); } diff --git a/c_src/resources.h b/c_src/resources.h index 8dd9f7c..42e0630 100644 --- a/c_src/resources.h +++ b/c_src/resources.h @@ -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 diff --git a/scripts/gen_ccls.sh b/scripts/gen_ccls.sh new file mode 100755 index 0000000..ed98915 --- /dev/null +++ b/scripts/gen_ccls.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +template=$(cat < 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).