Commits

Jacob Perkins committed 4d84604

initial ordered set support

Comments (0)

Files changed (2)

 
 set_pipelining(Client, Bool) -> gen_server2:cast(Client, {pipelining, Bool}).
 
-quit(Client) ->
-	erldis_client:scall(Client, <<"QUIT">>),
-	erldis_client:disconnect(Client).
+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) ->
 	Size = list_to_binary(integer_to_list(size(Value))),
-	Cmd = <<Command/binary, Key/binary, " ", Size/binary, "\r\n", Value/binary>>,
-	
-	case erldis_client:call(Client, Cmd) of
-		[{error, _}=Error] -> Error;
-		[R] when R == ok; R == nil; R == true; R == false -> R;
-		R -> R
-	end;
+	Cmd = <<Command/binary, Key/binary, " ", Size/binary, ?EOL, Value/binary>>,
+	internal_set_like_reply(erldis_client:call(Client, Cmd));
 internal_set_like(Client, Command, Key, Value) when is_binary(Value) ->
-	case erldis_client:call(Client, Command, [[Key, size(Value)], [Value]]) of
-		[{error, _}=Error] -> Error;
-		[R] when R == ok; R == nil; R == true; R == false -> R;
-		R -> R
-	end;
+	Reply = erldis_client:call(Client, Command, [[Key, size(Value)], [Value]]),
+	internal_set_like_reply(Reply);
 internal_set_like(Client, Command, Key, Value) when not is_binary(Key) ->
 	internal_set_like(Client, Command, erldis_client:bin(Key), Value);
 internal_set_like(Client, Command, Key, Value) when not is_binary(Value) ->
 internal_set_like(_, _, _, _) ->
 	{error, badarg}.
 
+internal_zset_like(Client, Command, Key, Score, Value) when is_binary(Value) ->
+	Reply = erldis_client:call(Client, Command, [[Key, Score, size(Value)], [Value]]),
+	internal_set_like_reply(Reply).
+
+internal_set_like_reply([{error, _}=Error]) -> Error;
+internal_set_like_reply([R]) when is_atom(R) -> R;
+internal_set_like_reply(R) -> R.
+
 auth(Client, Password) ->
 	erldis_client:scall(Client, <<"auth ", Password/binary>>).
 
 			{error, unsupported}
 	end.
 
+numeric([I]) -> numeric(I);
 numeric(false) -> 0;
 numeric(true) -> 1;
 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) ->
-	erldis_client:call(Client, <<"lrem ">>, [[Key, Number, size(Value)], [Value]]).
-	
+	numeric(erldis_client:call(Client, <<"lrem ">>, [[Key, Number, size(Value)], [Value]])).
+
 lpop(Client, Key) -> erldis_client:sr_scall(Client, <<"lpop ">>, [Key]).
 
 rpop(Client, Key) -> erldis_client:sr_scall(Client, <<"rpop ">>, [Key]).
 %% Commands operating on sets %%
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
-sadd(Client, Key, Value) -> internal_set_like(Client, <<"sadd ">>, Key, Value).
+sadd(Client, Key, Member) -> internal_set_like(Client, <<"sadd ">>, Key, Member).
 
-srem(Client, Key, Value) -> internal_set_like(Client, <<"srem ">>, Key, Value).
+srem(Client, Key, Member) -> internal_set_like(Client, <<"srem ">>, Key, Member).
 
 smove(Client, SrcKey, DstKey, Member) when not is_binary(Member) ->
 	smove(Client, SrcKey, DstKey, erldis_client:bin(Member));
 
 scard(Client, Key) -> numeric(erldis_client:sr_scall(Client, <<"scard ">>, [Key])).
 
-sismember(Client, Key, Value) -> internal_set_like(Client, <<"sismember ">>, Key, Value).
+sismember(Client, Key, Member) -> internal_set_like(Client, <<"sismember ">>, Key, Member).
 
 sintersect(Client, Keys) -> erldis_client:scall(Client, <<"sinter ">>, Keys).
 
 
 smembers(Client, Key) -> erldis_client:scall(Client, <<"smembers ">>, [Key]).
 
-% TODO: ordered sets
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%% Commands operating on ordered sets %%
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+zadd(Client, Key, Score, Member) ->
+	internal_zset_like(Client, <<"zadd ">>, Key, 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)).
+
+zrange(Client, Key, Start, End) ->
+	erldis_client:scall(Client, <<"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">>]).
+
+zrevrange(Client, Key, Start, End) ->
+	erldis_client:scall(Client, <<"zrevrange ">>, [Key, Start, End]).
+
+%zrevrange_withscores(Client, Key, Start, End) ->
+%	erldis_client:scall(Client, <<"zrevrange ">>, [Key, Start, End, <<"withscores">>]).
+
+%zrangebyscore(Client, Key, Min, Max) ->
+%	erldis_client:scall(Client, <<"zrangebyscore ">>, [Key, Min, Max]).
+
+%zrangebyscore(Client, Key, Min, Max, Offset, Count) ->
+%	Args = [Key, Min, Max, <<"limit">>, Offset, Count],
+%	erldis_client:scall(Client, <<"zrangebyscore ">>, Args).
+
+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)).
+
+%zremrangebyscore(Client, Key, Min, Max) ->
+%	numeric(erldis_client:sr_call(Client, <<"zremrangebyscore ">>, [Key, Min, Max])).
 
 %%%%%%%%%%%%%
 %% Sorting %%

test/erldis_tests.erl

 -include_lib("eunit/include/eunit.hrl").
 -include("erldis.hrl").
 
-%quit_test() ->
-%	 {ok, Client} = erldis:connect(<<"localhost">>, 6379),
-%	 ok = erldis:quit(Client),
-%	 false = is_process_alive(Client).
-%
+quit_test() ->
+	{ok, Client} = erldis:connect("localhost", 6379),
+	?assertEqual(shutdown, erldis:quit(Client)),
+	false = is_process_alive(Client).
+
 utils_test() ->
-	?assertEqual(erldis_client:bin(1), <<"1">>),
-	?assertEqual(erldis_client:bin(atom), <<"atom">>),
-	?assertEqual(erldis_client:format([[1, 2, 3]]), <<"1 2 3">>),
-	?assertEqual(erldis_client:format([[1,2,3], [4,5,6]]), <<"1 2 3\r\n4 5 6">>).
+	?assertEqual(<<"1">>, erldis_client:bin(1)),
+	?assertEqual(<<"atom">>, erldis_client:bin(atom)),
+	?assertEqual(<<"1 2 3">>, erldis_client:format([[1, 2, 3]])),
+	?assertEqual(<<"1 2 3\r\n4 5 6">>, erldis_client:format([[1,2,3], [4,5,6]])).
 
 basic_test() ->
 	{ok, Client} = erldis:connect("localhost", 6379),
-	?assertEqual(erldis:flushdb(Client), ok),
+	?assertEqual(ok, erldis:flushdb(Client)),
 
-	?assertEqual(erldis:get(Client, <<"pippo">>), nil),
+	?assertEqual(nil, erldis:get(Client, <<"pippo">>)),
 	ok = erldis:set(Client, <<"hello">>, <<"kitty!">>),
 	?assert(erldis:setnx(Client, <<"foo">>, <<"bar">>)),
 	?assertNot(erldis:setnx(Client, <<"foo">>, <<"bar">>)),
 	
 	?assert(erldis:exists(Client, <<"hello">>)),
 	?assert(erldis:exists(Client, <<"foo">>)),
-	?assertEqual(erldis:get(Client, <<"foo">>), <<"bar">>),
-	?assertEqual(erldis:mget(Client, [<<"hello">>, <<"foo">>]), [<<"kitty!">>, <<"bar">>]),
-	?assertEqual(erldis:keys(Client, <<"f*">>), [<<"foo">>]),
+	?assertEqual(<<"bar">>, erldis:get(Client, <<"foo">>)),
+	?assertEqual([<<"kitty!">>, <<"bar">>], erldis:mget(Client, [<<"hello">>, <<"foo">>])),
+	?assertEqual([<<"foo">>], erldis:keys(Client, <<"f*">>)),
 	
 	?assert(erldis:del(Client, <<"hello">>)),
 	?assert(erldis:del(Client, <<"foo">>)),
 	?assertNot(erldis:exists(Client, <<"hello">>)),
 	?assertNot(erldis:exists(Client, <<"foo">>)),
+	
+	?assertEqual(shutdown, erldis:quit(Client)).
 
+set_test() ->
+	{ok, Client} = erldis:connect("localhost", 6379),
+	?assertEqual(ok, erldis:flushdb(Client)),
 	erldis:sadd(Client, <<"set">>, <<"toto">>),
-	?assertEqual(erldis:smembers(Client, <<"set">>), [<<"toto">>]),
+	?assertEqual([<<"toto">>], erldis:smembers(Client, <<"set">>)),
 	erldis:srem(Client, <<"set">>, <<"toto">>),
-	?assertEqual(erldis:smembers(Client, <<"set">>), []).
+	?assertEqual([], erldis:smembers(Client, <<"set">>)),
+	?assertEqual(shutdown, erldis:quit(Client)).
 
-	%%% Commented out. Using the new erldis_set, erldis_list.
-	%ok = erldis:set(Client, <<"pippo">>, <<"pluto">>),
-	%{error, <<"ERR Operation against a key holding the wrong kind of value"} = erldis:sadd(Client, <<"pippo">>, <<"paperino">>),
-	%% foo doesn't exist, the result will be nil
-	%[] = erldis:lrange(Client, <<"foo">>, 1, 2),
-	%true = erldis:del(Client, <<"pippo">>),
-	%
-	%ok = erldis:rpush(Client, <<"a_list">>, <<"1">>),
-	%ok = erldis:rpush(Client, <<"a_list">>, <<"2">>),
-	%ok = erldis:rpush(Client, <<"a_list">>, <<"3">>),
-	%%ok = erldis:rpush(Client, <<"a_list">>, <<"1">>),
-	%%true = erldis:lrem(Client, <<"a_list">>, 1, <<"1">>).
-	%[<<"2">>, <<"3">>, <<"1">>] = erldis:lrange(Client, <<"a_list">>, 0, 2),
-	%
-	%[<<"1">>, <<"2">>, <<"3">>] = erldis:sort(Client, <<"a_list">>),
-	%[<<"3">>, <<"2">>, <<"1">>] = erldis:sort(Client, <<"a_list">>, <<"DESC">>),
-	%[<<"2">>, <<"3">>, <<"1">>] = erldis:lrange(Client, <<"a_list">>, 0, 2),
-	%[<<"1">>, <<"2">>] = erldis:sort(Client, <<"a_list">>, <<"LIMIT 0 2 ASC">>).
-	%ok = erldis:quit(Client).
+list_test() ->
+	{ok, Client} = erldis:connect("localhost", 6379),
+	?assertEqual(ok, erldis:flushdb(Client)),
+	
+	?assertEqual([], erldis:lrange(Client, <<"foo">>, 1, 2)),
+	ok = erldis:rpush(Client, <<"a_list">>, <<"1">>),
+	ok = erldis:rpush(Client, <<"a_list">>, <<"2">>),
+	ok = erldis:rpush(Client, <<"a_list">>, <<"3">>),
+	ok = erldis:rpush(Client, <<"a_list">>, <<"1">>),
+	?assertEqual(1, erldis:lrem(Client, <<"a_list">>, 1, <<"1">>)),
+	?assertEqual([<<"2">>, <<"3">>, <<"1">>], erldis:lrange(Client, <<"a_list">>, 0, 2)),
+	?assertEqual([<<"1">>, <<"2">>, <<"3">>], erldis:sort(Client, <<"a_list">>)),
+	?assertEqual([<<"3">>, <<"2">>, <<"1">>], erldis:sort(Client, <<"a_list">>, <<"DESC">>)),
+	?assertEqual([<<"1">>, <<"2">>], erldis:sort(Client, <<"a_list">>, <<"LIMIT 0 2 ASC">>)),
+	
+	?assertEqual(shutdown, erldis:quit(Client)).
 
-
+zset_test() ->
+	{ok, Client} = erldis:connect("localhost", 6379),
+	?assertEqual(ok, erldis:flushdb(Client)),
+	
+	?assertEqual(0, erldis:zcard(Client, <<"foo">>)),
+	?assertEqual([], erldis:zrange(Client, <<"foo">>, 0, 1)),
+	?assertEqual(true, erldis:zadd(Client, <<"foo">>, 5, <<"elem1">>)),
+	?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(true, erldis:zrem(Client, <<"foo">>, <<"elem1">>)),
+	?assertEqual(false, erldis:zrem(Client, <<"foo">>, <<"elem1">>)),
+	?assertEqual(0, erldis:zcard(Client, <<"foo">>)),
+	?assertEqual([], erldis:zrange(Client, <<"foo">>, 0, 2)),
+	
+	?assertEqual(shutdown, erldis:quit(Client)).
 
 % inline_tests(Client) ->
 %	  [?_assertMatch(ok, erldis:set(Client, <<"hello">>, <<"kitty!">>)),