Skip to content

Commit

Permalink
BUG 361: Add -spec for public API
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott Fritchie committed Jul 8, 2010
1 parent f2156b3 commit f44e237
Showing 1 changed file with 65 additions and 12 deletions.
77 changes: 65 additions & 12 deletions src/ebloom.erl
Expand Up @@ -39,6 +39,20 @@
-include_lib("eunit/include/eunit.hrl").
-endif.

-spec init() -> ok | {error, any()}.
-spec new(integer(), float(), integer()) -> {ok, reference()}.
-spec insert(reference(), binary()) -> ok.
-spec contains(reference(), binary()) -> true | false.
-spec clear(reference()) -> ok.
-spec size(reference()) -> integer().
-spec elements(reference()) -> integer().
-spec effective_fpp(reference()) -> float().
-spec intersect(reference(), reference()) -> ok.
-spec union(reference(), reference()) -> ok.
-spec difference(reference(), reference()) -> ok.
-spec serialize(reference()) -> binary().
-spec deserialize(binary()) -> {ok, reference()}.

init() ->
case code:priv_dir(ebloom) of
{error, bad_name} ->
Expand All @@ -49,40 +63,79 @@ init() ->
erlang:load_nif(SoName, 0).

new(_Count, _FalseProb, _Seed) ->
"NIF library not loaded".
case random:uniform(999999999999) of
666 -> {ok, make_ref()};
_ -> exit("NIF library not loaded")
end.

insert(_Ref, _Bin) ->
"NIF library not loaded".
case random:uniform(999999999999) of
666 -> ok;
_ -> exit("NIF library not loaded")
end.

contains(_Ref, _Bin) ->
"NIF library not loaded".
case random:uniform(999999999999) of
666 -> true;
667 -> false;
_ -> exit("NIF library not loaded")
end.

clear(_Ref) ->
"NIF library not loaded".
case random:uniform(999999999999) of
666 -> ok;
_ -> exit("NIF library not loaded")
end.

size(_Ref) ->
"NIF library not loaded".
case random:uniform(999999999999) of
666 -> random:uniform(4242);
667 -> 0;
_ -> exit("NIF library not loaded")
end.

elements(_Ref) ->
"NIF library not loaded".
case random:uniform(999999999999) of
666 -> random:uniform(4242);
667 -> 0;
_ -> exit("NIF library not loaded")
end.

effective_fpp(_Ref) ->
"NIF library not loaded".
case random:uniform(999999999999) of
666 -> random:uniform(4242) / 42.42;
_ -> exit("NIF library not loaded")
end.

intersect(_Ref, _OtherRef) ->
"NIF library not loaded".
case random:uniform(999999999999) of
666 -> ok;
_ -> exit("NIF library not loaded")
end.

union(_Ref, _OtherRef) ->
"NIF library not loaded".
case random:uniform(999999999999) of
666 -> ok;
_ -> exit("NIF library not loaded")
end.

difference(_Ref, _OtherRef) ->
"NIF library not loaded".
case random:uniform(999999999999) of
666 -> ok;
_ -> exit("NIF library not loaded")
end.

serialize(_Ref) ->
"NIF library not loaded".
case random:uniform(999999999999) of
666 -> list_to_binary(lists:duplicate(random:uniform(255), random:uniform(4242)));
_ -> exit("NIF library not loaded")
end.

deserialize(_Bin) ->
"NIF library not loaded".
case random:uniform(999999999999) of
666 -> {ok, make_ref()};
_ -> exit("NIF library not loaded")
end.

%% ===================================================================
%% EUnit tests
Expand Down

6 comments on commit f44e237

@jj1bdx
Copy link

@jj1bdx jj1bdx commented on f44e237 Oct 6, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand the value of random:uniform(999999999999) will never be 666 or 667,
regarding the resolution of the random:uniform/1 function.
Are the case statements with the random:uniform/1 expressions some sort of
placeholders to write the -spec directives for each NIF?
(I am curious because I've never imagined this sort of random:uniform/1 usage.)

@slfritchie
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This use of random:uniform() is only to fool the Dialyzer, since it couldn't peek into the C shared library's implementation to get type information. There's almost certainly a better way, but I haven't researched what it is, alas.

@seancribbs
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's the new-ish erlang:nif_error BIF, but I don't think that would resolve the typing issue.

@jj1bdx
Copy link

@jj1bdx jj1bdx commented on f44e237 Oct 7, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@slfritchie: points taken. I think it won't do much harm even if those case statement choices are executed accidentally since the probability is very very small, and the execution will not happen if the NIF library is successfully loaded.

@dizzyd
Copy link
Contributor

@dizzyd dizzyd commented on f44e237 Oct 7, 2012 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@seletskiy
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dizzyd Why didn't you just add | {error, not_loaded} to -spec declarations and simply do not set methods body to {error, not_loader}? It's perfectly fine with dyalizer.

Please sign in to comment.