Make erlfdb_get_key_selector more user friendly
Previously it was not possible to combine the `lt`/`lteq`/`gt`/`gteq`
behaviors with an offset without duplicating all of the logic in
`erlfdb_get_key_selector`. This changes that so a skip can be applied
onto any of those atom based selectors.
diff --git a/c_src/util.c b/c_src/util.c
index cbb5331..0ede4a2 100644
--- a/c_src/util.c
+++ b/c_src/util.c
@@ -52,6 +52,7 @@
const ERL_NIF_TERM* tuple;
int arity;
int number_or_equal;
+ int add_offset;
if(!enif_get_tuple(env, selector, &arity, &tuple)) {
return 0;
@@ -65,7 +66,7 @@
return 0;
}
- if(arity == 2) {
+ if(arity >= 2) {
if(IS_ATOM(tuple[1], lt)) {
*or_equal = 0;
*offset = 0;
@@ -78,15 +79,11 @@
} else if(IS_ATOM(tuple[1], gteq)) {
*or_equal = 0;
*offset = 1;
- } else {
- return 0;
- }
- } else if(arity == 3) {
- if(IS_ATOM(tuple[1], true)) {
+ } else if(IS_ATOM(tuple[1], true)) {
*or_equal = 1;
} else if(IS_ATOM(tuple[1], false)) {
*or_equal = 0;
- } if(enif_get_int(env, tuple[1], &number_or_equal)) {
+ } else if(enif_get_int(env, tuple[1], &number_or_equal)) {
if(number_or_equal) {
*or_equal = 1;
} else {
@@ -95,16 +92,23 @@
} else {
return 0;
}
-
- if(!enif_get_int(env, tuple[2], offset)) {
- return 0;
- }
- } else {
- // Technically this is dead code, but keeping
- // it here in case the arity conditional earlier
- // in this function is ever changed.
- return 0;
}
- return 1;
+ if(arity == 2) {
+ return 1;
+ }
+
+ if(arity == 3) {
+ if(!enif_get_int(env, tuple[2], &add_offset)) {
+ return 0;
+ }
+
+ *offset += add_offset;
+ return 1;
+ }
+
+ // Technically this is dead code, but keeping
+ // it here in case the arity conditional earlier
+ // in this function is ever changed.
+ return 0;
}
\ No newline at end of file