Commits

Jacob Perkins committed c89921f

zscore and zincrby working

  • Participants
  • Parent commits 4d84604

Comments (0)

Files changed (2)

File src/erldis.erl

 set_pipelining(Client, Bool) -> gen_server2:cast(Client, {pipelining, Bool}).
 
 quit(Client) -> erldis_client:stop(Client).
-%	erldis_client:scall(Client, <<"QUIT">>),
-%	erldis_client:disconnect(Client).
 
 %% Commands operating on string values
 internal_set_like(Client, Command, Key, Value) when is_binary(Key), is_binary(Value) ->
 			{error, unsupported}
 	end.
 
-numeric([I]) -> numeric(I);
 numeric(false) -> 0;
 numeric(true) -> 1;
+numeric(I) when is_binary(I) -> numeric(binary_to_list(I));
+numeric(I) when is_list(I) ->
+	try list_to_integer(I)
+	catch
+		error:badarg ->
+			try list_to_float(I)
+			catch error:badarg -> I
+			end
+	end;
 numeric(I) -> I.
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 lrem(Client, Key, Number, Value) when not is_binary(Value) ->
 	lrem(Client, Key, Number, erldis_client:bin(Value));
 lrem(Client, Key, Number, Value) ->
-	numeric(erldis_client:call(Client, <<"lrem ">>, [[Key, Number, size(Value)], [Value]])).
+	% TODO: needs sr_scall like single results
+	numeric(hd(erldis_client:call(Client, <<"lrem ">>, [[Key, Number, size(Value)], [Value]]))).
 
 lpop(Client, Key) -> erldis_client:sr_scall(Client, <<"lpop ">>, [Key]).
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
 zadd(Client, Key, Score, Member) ->
-	internal_zset_like(Client, <<"zadd ">>, Key, Score, Member).
+	internal_zset_like(Client, <<"zadd ">>, Key, erldis_client:bin(Score), Member).
 
 zrem(Client, Key, Member) -> internal_set_like(Client, <<"zrem ">>, Key, Member).
 
-% TODO: test is failing because returns a binary string instead of a number
-%zincrby(Client, Key, By, Member) ->
-%	numeric(internal_zset_like(Client, <<"zincrby ">>, Key, By, Member)).
+zincrby(Client, Key, By, Member) ->
+	% TODO: this & zscore need sr_scall like results instead of hd()
+	numeric(hd(internal_zset_like(Client, <<"zincrby ">>, Key, erldis_client:bin(By), Member))).
 
 zrange(Client, Key, Start, End) ->
 	erldis_client:scall(Client, <<"zrange ">>, [Key, Start, End]).
 
 zcard(Client, Key) -> numeric(erldis_client:sr_scall(Client, <<"zcard ">>, [Key])).
 
-% TODO: test is failing because returns a binary string instead of a number
-%zscore(Client, Key, Member) ->
-%	numeric(internal_set_like(Client, <<"zscore ">>, Key, Member)).
+zscore(Client, Key, Member) ->
+	numeric(hd(internal_set_like(Client, <<"zscore ">>, Key, Member))).
 
 %zremrangebyscore(Client, Key, Min, Max) ->
 %	numeric(erldis_client:sr_call(Client, <<"zremrangebyscore ">>, [Key, Min, Max])).

File test/erldis_tests.erl

 	?assertEqual([<<"elem1">>], erldis:zrange(Client, <<"foo">>, 0, 1)),
 	?assertEqual(false, erldis:zadd(Client, <<"foo">>, 6, <<"elem1">>)),
 	?assertEqual(1, erldis:zcard(Client, <<"foo">>)),
-	%?assertEqual(6, erldis:zscore(Client, <<"foo">>, <<"elem1">>)),
-	%?assertEqual(8, erldis:zincrby(Client, <<"foo">>, 2, <<"elem1">>)),
+	?assertEqual(6, erldis:zscore(Client, <<"foo">>, <<"elem1">>)),
+	?assertEqual(8, erldis:zincrby(Client, <<"foo">>, 2, <<"elem1">>)),
 	?assertEqual(true, erldis:zrem(Client, <<"foo">>, <<"elem1">>)),
 	?assertEqual(false, erldis:zrem(Client, <<"foo">>, <<"elem1">>)),
 	?assertEqual(0, erldis:zcard(Client, <<"foo">>)),