Permalink
Browse files

Almost there...

  • Loading branch information...
Tom Janssens
Tom Janssens committed Jan 24, 2014
1 parent ce820c1 commit 2b15aeef27ced6a6d14d97f9a01c9682f9047c14
Showing with 24 additions and 14 deletions.
  1. +6 −6 c_src/fdb_nifs.c
  2. +2 −1 include/fdb.hrl
  3. +15 −6 src/fdb.erl
  4. +1 −1 test/fdb_test.erl
View
@@ -306,15 +306,15 @@ static ERL_NIF_TERM nif_fdb_future_get_key(ErlNifEnv* env, int argc, const ERL_N
static ERL_NIF_TERM nif_fdb_future_get_keyvalue_array(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
enif_future_t *f;
const FDBKeyValue ** out_kv=NULL;
int* out_count = NULL;
fdb_bool_t* out_more = NULL;
const FDBKeyValue * out_kv=NULL;
int out_count = 0;
fdb_bool_t out_more = 0;

This comment has been minimized.

Show comment
Hide comment
@ToJans

ToJans Jan 24, 2014

Contributor

This is the part where you realize: 👎

Sometimes you need to stop doing what you are doing, and take a break...

@ToJans

ToJans Jan 24, 2014

Contributor

This is the part where you realize: 👎

Sometimes you need to stop doing what you are doing, and take a break...

if (argc!=1) return enif_make_badarg(env);
if (get_future(env, argv[0], &f) == 0 )
return enif_make_badarg(env);
fdb_error_t err = fdb_future_get_keyvalue_array(f->handle, out_kv, out_count, out_more);
fdb_error_t err = fdb_future_get_keyvalue_array(f->handle, &out_kv, &out_count, &out_more);
if (err != 0)
{
@@ -327,13 +327,13 @@ static ERL_NIF_TERM nif_fdb_future_get_keyvalue_array(ErlNifEnv* env, int argc,
while (out_count>0)
{
out_count = out_count - 1;
const FDBKeyValue *kv = out_kv[*out_count];
const FDBKeyValue *kv = &(out_kv[out_count]);
ERL_NIF_TERM k = make_binary(env, kv->key, kv->key_length);
ERL_NIF_TERM v = make_binary(env, kv->value, kv->value_length);
ERL_NIF_TERM elem = enif_make_tuple2(env, k, v);
result = enif_make_list_cell(env, elem, result);
}
return mk_result(env,err,result);
return mk_result(env,err,enif_make_tuple2(env,result,enif_make_int(env,out_more)));
}
}
View
@@ -16,4 +16,5 @@
tx,
iteration = 0,
data = [],
select}).
select,
out_more = 1}).
View
@@ -66,8 +66,7 @@ get(DB={db, _}, Select = #select{}) ->
transact(DB, fun(Tx) -> get(Tx, Select) end);
get(Tx={tx, _}, Select = #select{}) ->
Iterator = bind(Tx, Select),
Next = next(Iterator),
lists:map(fun(x)-> binary_to_term(x) end, Next#iterator.data);
iterate_all(Iterator);
get(FdbHandle, Key) ->
get(FdbHandle, Key, not_found).
@@ -89,8 +88,9 @@ bind({db, DB}, Select = #select{}) ->
bind({tx, Transaction}, Select = #select{}) ->
#iterator{tx = Transaction, select = Select, iteration = Select#select.iteration}.
next(Iterator = #iterator{tx = Transaction, data = OldData, iteration = Iteration, select = Select})
when Iteration == 1 orelse OldData =/= [] ->
next(Iterator = #iterator{out_more=0}) ->
Iterator#iterator{data = []};
next(Iterator = #iterator{tx = Transaction, data = OldData, iteration = Iteration, select = Select}) ->
{FstKey, FstIsEq} = extract_keys(Select#select.gt, Select#select.gte,<<0>>),
{LstKey, LstIsEq} = extract_keys(Select#select.lt, Select#select.lte,<<255,255,255,255>>),
F = fdb_nif:fdb_transaction_get_range(Transaction,
@@ -102,8 +102,17 @@ next(Iterator = #iterator{tx = Transaction, data = OldData, iteration = Iteratio
Iteration,
Select#select.is_snapshot,
Select#select.is_reverse),
{ok, Data} = future_get(F, keyvalue_array),
Iterator#iterator{ data = Data, iteration = Iteration + 1}.
{ok, {EncodedData, OutMore}} = future_get(F, keyvalue_array),
Data = lists:map(fun({X, Y})-> {tuple:unpack(X), binary_to_term(Y)} end, EncodedData),
Iterator#iterator{ data = Data, iteration = Iteration + 1, out_more = OutMore}.
iterate_all(Iterator) ->
iterate_all(Iterator, []).
iterate_all(#iterator{out_more = 0}, Result) ->
Result;
iterate_all(Iterator, Result) ->
Next = next(Iterator),
iterate_all(Next, Result++Next#iterator.data).
extract_keys(nil, nil, Default) -> {Default, true};
extract_keys(nil, Value, _) -> { tuple:pack(Value), true };
View
@@ -35,7 +35,7 @@ range_test() ->
fdb:api_version(100),
{ok, DB} = fdb:open(),
[ok = fdb:set(DB, I, I) || I <- lists:seq(1, 4)],
?assertEqual([{2, 2}, {3, 3}], fdb:get(DB, #select{gte = 2, lt =4})),
?assertEqual([{2, 2}, {3, 3}], fdb:get(DB, #select{gte = 2, lte =4})),
?assertEqual([{2, 2}, {3, 3}], fdb:get(DB, #select{gte = 2, lte =3})),
?assertEqual([{2, 2}, {3, 3}, {4, 4}], fdb:get(DB, #select{gte = 2})),
?assertEqual([{3, 3}, {4, 4}], fdb:get(DB, #select{ gt = 2})),

0 comments on commit 2b15aee

Please sign in to comment.