Skip to content
Browse files

Add withscores to erldis zsets

  • Loading branch information...
1 parent 63b662b commit fef214ddab7e7f7ca1f8edbedb4eb999cff19ca7 @schleyfox schleyfox committed
Showing with 22 additions and 10 deletions.
  1. +4 −4 Rakefile
  2. +14 −6 src/erldis.erl
  3. +4 −0 test/erldis_tests.erl
View
8 Rakefile
@@ -20,12 +20,12 @@ end
desc "installs in $ERL_TOP/lib/"
task :install => [:build] do |t|
FileList.new('ebin/*.app').each do |dir|
- vsn = extract_version_information("vsn.config","vsn").gsub("\"","")
+ #vsn = extract_version_information("vsn.config","vsn").gsub("\"","")
name = dir.gsub("ebin/","").gsub(".app","")
- destination = "#{erlang_home}/lib/#{name}-#{vsn}"
+ destination = "#{erlang_home}/lib/#{name}"
puts "#{name} will be installed in #{destination}"
sh "mkdir -p #{destination}"
- %w{ priv ebin docs include }.each do |d|
+ %w{ebin doc include }.each do |d|
sh "cp -R #{d} #{destination}"
end
end
@@ -41,4 +41,4 @@ def extract_version_information(file, type)
informations << $1 if line =~ /\{#{type},(.*)\}/
}
informations[0]
-end
+end
View
20 src/erldis.erl
@@ -177,15 +177,14 @@ zincrby(Client, Key, By, 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])).
@@ -307,4 +306,13 @@ numeric(I) when is_list(I) ->
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]).
View
4 test/erldis_tests.erl
@@ -73,6 +73,8 @@ zset_test() ->
?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">>)),
@@ -83,6 +85,8 @@ zset_test() ->
?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)),

0 comments on commit fef214d

Please sign in to comment.
Something went wrong with that request. Please try again.