Skip to content

Commit

Permalink
Update from upstream's master (#4)
Browse files Browse the repository at this point in the history
* Added methods for retrieving initiation parameters and checking of compatibility.

* Properly handle dialyzer warnings

* Properly use opaques

* Remove author

Co-authored-by: Christian Dahlqvist <[email protected]>
Co-authored-by: Brian Sparrow <[email protected]>
  • Loading branch information
3 people authored Feb 17, 2021
1 parent fbf31a5 commit 0fc61f3
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 16 deletions.
15 changes: 15 additions & 0 deletions c_src/bloom_filter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
98 changes: 86 additions & 12 deletions c_src/ebloom_nifs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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[]);
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
40 changes: 36 additions & 4 deletions src/ebloom.erl
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@

-module(ebloom).

-author('Dave Smith <[email protected]>').

-export([new/3, insert/2, contains/2, clear/1, size/1, elements/1, effective_fpp/1,
intersect/2, union/2, difference/2, serialize/1, deserialize/1]).
-export([new/3, insert/2, contains/2, clear/1, compatible/2, predicted_elements/1,
desired_fpp/1, random_seed/1, size/1, elements/1, effective_fpp/1, intersect/2, union/2,
difference/2, serialize/1, deserialize/1]).

-on_load init/0.

Expand Down Expand Up @@ -69,6 +68,22 @@ contains(_Ref, _Bin) ->
clear(_Ref) ->
erlang:nif_error({error, not_loaded}).

-spec compatible(t(), t()) -> boolean().
compatible(_Ref1, _Ref2) ->
erlang:nif_error({error, not_loaded}).

-spec predicted_elements(t()) -> integer().
predicted_elements(_Ref) ->
erlang:nif_error({error, not_loaded}).

-spec desired_fpp(t()) -> float().
desired_fpp(_Ref) ->
erlang:nif_error({error, not_loaded}).

-spec random_seed(t()) -> integer().
random_seed(_Ref) ->
erlang:nif_error({error, not_loaded}).

-spec size(t()) -> integer().
size(_Ref) ->
erlang:nif_error({error, not_loaded}).
Expand Down Expand Up @@ -144,4 +159,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.

0 comments on commit 0fc61f3

Please sign in to comment.