Commits

Jacob Perkins committed 855590a

add spop and zrange_withscores (by @schleyfox via @cstar)

Comments (0)

Files changed (2)

 srem(Client, Key, Member) ->
 	erldis_client:sr_scall(Client, bulk_cmd([<<"srem">>, Key], Member)).
 
+spop(Client, Key) ->
+	erldis_client:sr_scall(Client, inline_cmd(<<"spop">>, Key)).
+
 % TODO: test
 smove(Client, SrcKey, DstKey, Member) ->
 	erldis_client:sr_scall(Client, bulk_cmd([<<"smove">>, SrcKey, DstKey], Member)).
 zrange(Client, Key, Start, End) ->
 	erldis_client:scall(Client, inline_cmd([<<"zrange">>, Key, Start, End])).
 
-% TODO: return [{member, score}] for withscores functions
-%zrange_withscores(Client, Key, Start, End) ->
-%	erldis_client:scall(Client, <<"zrange ">>, [Key, Start, End, <<"withscores">>]).
+zrange_withscores(Client, Key, Start, End) ->
+	withscores(erldis_client:scall(Client, <<"zrange ">>, [Key, Start, End, <<"withscores">>])).
 
 zrevrange(Client, Key, Start, End) ->
 	erldis_client:scall(Client, inline_cmd([<<"zrevrange">>, Key, Start, End])).
 
-%zrevrange_withscores(Client, Key, Start, End) ->
-%	erldis_client:scall(Client, <<"zrevrange ">>, [Key, Start, End, <<"withscores">>]).
+zrevrange_withscores(Client, Key, Start, End) ->
+	withscores(erldis_client:scall(Client, <<"zrevrange ">>, [Key, Start, End, <<"withscores">>])).
 
 zrangebyscore(Client, Key, Min, Max) ->
 	erldis_client:scall(Client, inline_cmd([<<"zrangebyscore ">>, Key, Min, Max])).
 			catch error:badarg -> I
 			end
 	end;
-numeric(I) -> I.
+numeric(I) -> I.
+
+withscores(L) -> 
+	withscores(L,[]).
+withscores([], Acc) ->
+	lists:reverse(Acc);
+withscores([_], _Acc) ->
+	throw({error, invalid_scores_list});
+withscores([Member | [Score | T]], Acc) ->
+	withscores(T, [{Member, numeric(Score)} | Acc]).

test/erldis_tests.erl

 	?assertEqual(true, erldis:zadd(Client, <<"foo">>, 5, <<"elem1">>)),
 	?assertEqual([<<"elem1">>], erldis:zrange(Client, <<"foo">>, 0, 1)),
 	?assertEqual([<<"elem1">>], erldis:zrevrange(Client, <<"foo">>, 0, 1)),
+	?assertEqual([{<<"elem1">>, 5}], erldis:zrange_withscores(Client, <<"foo">>, 0, 1)),
+	?assertEqual([{<<"elem1">>, 5}], erldis:zrevrange_withscores(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(1.5, erldis:zscore(Client, "foo", "a-elem")),
 	?assertEqual([<<"a-elem">>, <<"elem1">>], erldis:zrange(Client, "foo", 0, 2)),
 	?assertEqual([<<"elem1">>, <<"a-elem">>], erldis:zrevrange(Client, "foo", 0, 2)),
+	?assertEqual([{<<"a-elem">>, 1.5}, {<<"elem1">>, 8}], erldis:zrange_withscores(Client, "foo", 0, 2)),
+	?assertEqual([{<<"elem1">>, 8}, {<<"a-elem">>, 1.5}], erldis:zrevrange_withscores(Client, "foo", 0, 2)),
 	?assertEqual([<<"a-elem">>], erldis:zrangebyscore(Client, "foo", 1.0, 2.0)),
 	?assertEqual([<<"a-elem">>], erldis:zrangebyscore(Client, "foo", 1, 10, 0, 1)),
 	?assertEqual([<<"a-elem">>, <<"elem1">>], erldis:zrangebyscore(Client, "foo", 1, 10, 0, 2)),