Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Added functions for parameter retrieval and compatibility check #9

Open
wants to merge 1 commit into from

1 participant

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 11, 2012
  1. @whitenode
This page is out of date. Refresh to see the latest.
Showing with 150 additions and 12 deletions.
  1. +15 −0 c_src/bloom_filter.hpp
  2. +86 −12 c_src/ebloom_nifs.cpp
  3. +49 −0 src/ebloom.erl
View
15 c_src/bloom_filter.hpp
@@ -205,6 +205,21 @@ class bloom_filter
{
return table_size_;
}
+
+ inline virtual std::size_t predicted_elements() const
+ {
+ return predicted_element_count_;
+ }
+
+ inline virtual double desired_fpp() const
+ {
+ return desired_false_positive_probability_;
+ }
+
+ inline virtual std::size_t random_seed() const
+ {
+ return random_seed_;
+ }
inline std::size_t element_count() const
{
View
98 c_src/ebloom_nifs.cpp
@@ -36,6 +36,11 @@ extern "C"
ERL_NIF_TERM ebloom_contains(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]);
ERL_NIF_TERM ebloom_clear(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]);
+
+ ERL_NIF_TERM ebloom_compatible(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]);
+ ERL_NIF_TERM ebloom_predicted_elements(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]);
+ ERL_NIF_TERM ebloom_desired_fpp(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]);
+ ERL_NIF_TERM ebloom_random_seed(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]);
ERL_NIF_TERM ebloom_size(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]);
ERL_NIF_TERM ebloom_elements(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]);
@@ -54,18 +59,22 @@ extern "C"
static ErlNifFunc nif_funcs[] =
{
- {"new", 3, ebloom_new_filter},
- {"insert", 2, ebloom_insert},
- {"contains", 2, ebloom_contains},
- {"clear", 1, ebloom_clear},
- {"size", 1, ebloom_size},
- {"elements", 1, ebloom_elements},
- {"effective_fpp", 1, ebloom_effective_fpp},
- {"intersect", 2, ebloom_filter_intersect},
- {"union", 2, ebloom_filter_union},
- {"difference", 2, ebloom_filter_difference},
- {"serialize", 1, ebloom_serialize},
- {"deserialize", 1, ebloom_deserialize}
+ {"new", 3, ebloom_new_filter},
+ {"insert", 2, ebloom_insert},
+ {"contains", 2, ebloom_contains},
+ {"clear", 1, ebloom_clear},
+ {"compatible", 2, ebloom_compatible},
+ {"predicted_elements", 1, ebloom_predicted_elements},
+ {"desired_fpp", 1, ebloom_desired_fpp},
+ {"random_seed", 1, ebloom_random_seed},
+ {"size", 1, ebloom_size},
+ {"elements", 1, ebloom_elements},
+ {"effective_fpp", 1, ebloom_effective_fpp},
+ {"intersect", 2, ebloom_filter_intersect},
+ {"union", 2, ebloom_filter_union},
+ {"difference", 2, ebloom_filter_difference},
+ {"serialize", 1, ebloom_serialize},
+ {"deserialize", 1, ebloom_deserialize}
};
ERL_NIF_INIT(ebloom, nif_funcs, &on_load, NULL, NULL, NULL);
@@ -148,6 +157,71 @@ ERL_NIF_TERM ebloom_clear(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
}
}
+ERL_NIF_TERM ebloom_compatible(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
+{
+ bhandle* handle1;
+ bhandle* handle2;
+ if (enif_get_resource(env, argv[0], BLOOM_FILTER_RESOURCE, (void**)&handle1) &&
+ enif_get_resource(env, argv[1], BLOOM_FILTER_RESOURCE, (void**)&handle2))
+ {
+ if((handle1->filter->predicted_elements() == handle2->filter->predicted_elements()) &&
+ (handle1->filter->desired_fpp() == handle2->filter->desired_fpp()) &&
+ (handle1->filter->random_seed() == handle2->filter->random_seed())) {
+ return enif_make_atom(env, "true");
+ }
+ else
+ {
+ return enif_make_atom(env, "false");
+ }
+ }
+ else
+ {
+ return enif_make_badarg(env);
+ }
+}
+
+ERL_NIF_TERM ebloom_predicted_elements(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
+{
+ bhandle* handle;
+ if (enif_get_resource(env, argv[0], BLOOM_FILTER_RESOURCE, (void**)&handle))
+ {
+ long result = handle->filter->predicted_elements();
+ return enif_make_long(env, result);
+ }
+ else
+ {
+ return enif_make_badarg(env);
+ }
+}
+
+ERL_NIF_TERM ebloom_desired_fpp(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
+{
+ bhandle* handle;
+ if (enif_get_resource(env, argv[0], BLOOM_FILTER_RESOURCE, (void**)&handle))
+ {
+ double result = handle->filter->desired_fpp();
+ return enif_make_double(env, result);
+ }
+ else
+ {
+ return enif_make_badarg(env);
+ }
+}
+
+ERL_NIF_TERM ebloom_random_seed(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
+{
+ bhandle* handle;
+ if (enif_get_resource(env, argv[0], BLOOM_FILTER_RESOURCE, (void**)&handle))
+ {
+ long result = handle->filter->random_seed();
+ return enif_make_long(env, result);
+ }
+ else
+ {
+ return enif_make_badarg(env);
+ }
+}
+
ERL_NIF_TERM ebloom_size(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
bhandle* handle;
View
49 src/ebloom.erl
@@ -24,6 +24,10 @@
insert/2,
contains/2,
clear/1,
+ compatible/2,
+ predicted_elements/1,
+ desired_fpp/1,
+ random_seed/1,
size/1,
elements/1,
effective_fpp/1,
@@ -44,6 +48,10 @@
-spec insert(reference(), binary()) -> ok.
-spec contains(reference(), binary()) -> true | false.
-spec clear(reference()) -> ok.
+-spec compatible(reference(), reference()) -> true | false.
+-spec predicted_elements(reference()) -> integer().
+-spec desired_fpp(reference()) -> float().
+-spec random_seed(reference()) -> integer().
-spec size(reference()) -> integer().
-spec elements(reference()) -> integer().
-spec effective_fpp(reference()) -> float().
@@ -91,6 +99,30 @@ clear(_Ref) ->
666 -> ok;
_ -> exit("NIF library not loaded")
end.
+
+compatible(_Ref1, _Ref2) ->
+ case random:uniform(999999999999) of
+ 666 -> ok;
+ _ -> exit("NIF library not loaded")
+ end.
+
+predicted_elements(_Ref) ->
+ case random:uniform(999999999999) of
+ 666 -> ok;
+ _ -> exit("NIF library not loaded")
+ end.
+
+desired_fpp(_Ref) ->
+ case random:uniform(999999999999) of
+ 666 -> ok;
+ _ -> exit("NIF library not loaded")
+ end.
+
+random_seed(_Ref) ->
+ case random:uniform(999999999999) of
+ 666 -> ok;
+ _ -> exit("NIF library not loaded")
+ end.
size(_Ref) ->
case random:uniform(999999999999) of
@@ -185,4 +217,21 @@ clear_test() ->
0 = elements(Ref),
false = contains(Ref, <<"1">>).
+compatibility_test() ->
+ {ok, Ref1} = new(5, 0.01, 123),
+ {ok, Ref2} = new(5, 0.01, 123),
+ {ok, Ref3} = new(6, 0.01, 123),
+ {ok, Ref4} = new(5, 0.02, 123),
+ {ok, Ref5} = new(5, 0.01, 124),
+ true = compatible(Ref1, Ref2),
+ false = compatible(Ref1, Ref3),
+ false = compatible(Ref1, Ref4),
+ false = compatible(Ref1, Ref5).
+
+parameter_test() ->
+ {ok, Ref1} = new(5, 0.01, 123),
+ 5 = predicted_elements(Ref1),
+ 0.01 = desired_fpp(Ref1),
+ 123 = random_seed(Ref1).
+
-endif.
Something went wrong with that request. Please try again.