Permalink
Browse files

Fists steps to implement +inf and -inf values

  • Loading branch information...
1 parent c5391f4 commit e6983e452f638f878f0c441f9bc6210550df4255 @amilkr amilkr committed Dec 19, 2011
Showing with 33 additions and 40 deletions.
  1. +3 −0 include/edis.hrl
  2. +6 −17 src/edis_command_runner.erl
  3. +2 −2 src/edis_db.erl
  4. +20 −11 src/edis_util.erl
  5. +2 −10 src/zsets.erl
View
@@ -1,5 +1,8 @@
-include("elog.hrl").
+-define(POS_INFINITY,1.7976931348623157e308).
+-define(NEG_INFINITY,-1.7976931348623157e308).
+
-type edis_sort_field() :: self | binary() | {binary(), binary()}.
-record(edis_sort_options, {by = self :: edis_sort_field(),
limit = undefined :: undefined | {integer(), integer()},
@@ -883,6 +883,10 @@ tcp_number(Number, State) ->
-spec tcp_float(undefined | float(), state()) -> {noreply, state(), hibernate} | {stop, normal | {error, term()}, state()}.
tcp_float(undefined, State) ->
tcp_bulk(undefined, State);
+tcp_float(?POS_INFINITY, State) ->
+ tcp_bulk("inf",State);
+tcp_float(?NEG_INFINITY, State) ->
+ tcp_bulk("-inf",State);
tcp_float(Float, State) ->
case erlang:trunc(Float) * 1.0 of
Float -> tcp_bulk(integer_to_list(erlang:trunc(Float)), State);
@@ -927,23 +931,8 @@ tcp_send(Message, State) ->
{stop, normal, State}
end.
-parse_float_limit(Bin) ->
- do_parse_float_limit(edis_util:lower(Bin)).
-
-do_parse_float_limit(<<"(-inf">>) -> neg_infinity;
-do_parse_float_limit(<<"-inf">>) -> neg_infinity;
-do_parse_float_limit(<<"(inf">>) -> infinity;
-do_parse_float_limit(<<"inf">>) -> infinity;
-do_parse_float_limit(<<"(+inf">>) -> infinity;
-do_parse_float_limit(<<"+inf">>) -> infinity;
-do_parse_float_limit(<<"(-infinity">>) -> neg_infinity;
-do_parse_float_limit(<<"-infinity">>) -> neg_infinity;
-do_parse_float_limit(<<"(infinity">>) -> infinity;
-do_parse_float_limit(<<"infinity">>) -> infinity;
-do_parse_float_limit(<<"(+infinity">>) -> infinity;
-do_parse_float_limit(<<"+infinity">>) -> infinity;
-do_parse_float_limit(<<$(, Rest/binary>>) -> {exc, edis_util:binary_to_float(Rest)};
-do_parse_float_limit(Bin) -> {inc, edis_util:binary_to_float(Bin)}.
+parse_float_limit(<<$(, Rest/binary>>) -> {exc, edis_util:binary_to_float(Rest)};
+parse_float_limit(Bin) -> {inc, edis_util:binary_to_float(Bin)}.
parse_zstore_command(C) ->
[Destination, NumKeys | Rest] = C#edis_command.args,
View
@@ -1405,7 +1405,7 @@ handle_call(#edis_command{cmd = <<"ZRANK">>, args = [Key, Member]}, _From, State
#edis_item{value = Value} ->
case zsets:find(Member, Value) of
error -> {ok, undefined};
- {ok, Score} -> {ok, zsets:count(neg_infinity, {exc, Score}, Value)}
+ {ok, Score} -> {ok, zsets:count({inc,?NEG_INFINITY}, {exc, Score}, Value)}
end;
not_found -> {ok, undefined};
{error, Reason} -> {error, Reason}
@@ -1502,7 +1502,7 @@ handle_call(#edis_command{cmd = <<"ZREVRANK">>, args = [Key, Member]}, _From, St
#edis_item{value = Value} ->
case zsets:find(Member, Value) of
error -> {ok, undefined};
- {ok, Score} -> {ok, zsets:count(infinity, {exc, Score}, Value, backwards)}
+ {ok, Score} -> {ok, zsets:count({inc,?POS_INFINITY}, {exc, Score}, Value, backwards)}
end;
not_found -> {ok, undefined};
{error, Reason} -> {error, Reason}
View
@@ -13,7 +13,7 @@
integer_to_binary/1, binary_to_float/1, binary_to_float/2, reverse_tuple_to_list/1,
make_pairs/1, glob_to_re/1,random_binary/0, join/2, load_config/1]).
--include("elog.hrl").
+-include("edis.hrl").
-define(EPOCH, 62167219200).
@@ -70,17 +70,26 @@ binary_to_integer(Bin) ->
end.
%% @doc returns a float whose binary representation is Bin
--spec binary_to_float(binary()) -> integer().
+-spec binary_to_float(binary()) -> float().
binary_to_float(Bin) ->
- try list_to_float(binary_to_list(Bin))
- catch
- _:badarg ->
- try 1.0 * list_to_integer(binary_to_list(Bin))
- catch
- _:badarg ->
- throw(not_float)
- end
- end.
+ case lower(Bin) of
+ <<"inf">> -> ?POS_INFINITY;
+ <<"infinity">> -> ?POS_INFINITY;
+ <<"+infinity">> -> ?POS_INFINITY;
+ <<"+inf">> -> ?POS_INFINITY;
+ <<"-inf">> -> ?NEG_INFINITY;
+ <<"-infinity">> -> ?NEG_INFINITY;
+ _ ->
+ try list_to_float(binary_to_list(Bin))
+ catch
+ _:badarg ->
+ try 1.0 * list_to_integer(binary_to_list(Bin))
+ catch
+ _:badarg ->
+ throw(not_float)
+ end
+ end
+ end.
%% @doc returns an integer whose binary representation is Bin.
%% If Bin is not a integer, Default is returned
View
@@ -9,11 +9,11 @@
-author('Fernando Benavides <fernando.benavides@inakanetworks.com>').
-author('Chad DePue <chad@inakanetworks.com>').
--record(zset, {dict :: dict(),
+-record(zset, {dict :: dict(),
tree :: edis_gb_trees:edis_gb_tree()}).
-opaque zset(_Scores, _Members) :: #zset{}.
--type limit(Scores) :: neg_infinity | infinity | {exc, Scores} | {inc, Scores}.
+-type limit(Scores) :: {exc, Scores} | {inc, Scores}.
-type aggregate() :: sum | max | min.
-export_type([limit/1, aggregate/0]).
@@ -210,26 +210,18 @@ range(Start, Stop, {Score, Member, Iter}, Pos, Acc) when Pos =< Stop ->
range(_, _, _, _, Acc) -> Acc.
%% @private
-check_limit(min, neg_infinity, _, forward) -> in;
-check_limit(min, infinity, _, forward) -> out;
check_limit(min, {exc, Min}, Score, forward) when Min < Score -> in;
check_limit(min, {exc, Min}, Score, forward) when Min >= Score -> out;
check_limit(min, {inc, Min}, Score, forward) when Min =< Score -> in;
check_limit(min, {inc, Min}, Score, forward) when Min > Score -> out;
-check_limit(max, neg_infinity, _, forward) -> out;
-check_limit(max, infinity, _, forward) -> in;
check_limit(max, {exc, Max}, Score, forward) when Max > Score -> in;
check_limit(max, {exc, Max}, Score, forward) when Max =< Score -> out;
check_limit(max, {inc, Max}, Score, forward) when Max >= Score -> in;
check_limit(max, {inc, Max}, Score, forward) when Max < Score -> out;
-check_limit(min, neg_infinity, _, backwards) -> out;
-check_limit(min, infinity, _, backwards) -> in;
check_limit(min, {exc, Min}, Score, backwards) when Min > Score -> in;
check_limit(min, {exc, Min}, Score, backwards) when Min =< Score -> out;
check_limit(min, {inc, Min}, Score, backwards) when Min >= Score -> in;
check_limit(min, {inc, Min}, Score, backwards) when Min < Score -> out;
-check_limit(max, neg_infinity, _, backwards) -> in;
-check_limit(max, infinity, _, backwards) -> out;
check_limit(max, {exc, Max}, Score, backwards) when Max < Score -> in;
check_limit(max, {exc, Max}, Score, backwards) when Max >= Score -> out;
check_limit(max, {inc, Max}, Score, backwards) when Max =< Score -> in;

0 comments on commit e6983e4

Please sign in to comment.