Commits

Jacob Perkins  committed 7b16171

full sync patch from @cstar

  • Participants
  • Parent commits ffbc103

Comments (0)

Files changed (4)

File src/erldis.erl

     lists:flatten(List).
 
 %% exposed API
+connect() ->
+    erldis_sync_client:connect().
 connect(Host) ->
-    erldis_client:connect(Host).
+    erldis_sync_client:connect(Host).
 connect(Host, Port) ->
-    erldis_client:connect(Host, Port).
+    erldis_sync_client:connect(Host, Port).
 connect(Host, Port, Options) ->
-    erldis_client:connect(Host, Port, Options).
+    erldis_sync_client:connect(Host, Port, Options).
 
-quit(Client) ->
-    erldis_client:asend(Client, "QUIT"),
-    erldis_client:disconnect(Client).
+%quit(Client) ->
+%    erldis_sync_client:scall(Client, "QUIT"),
+%    erldis_sync_client:disconnect(Client).
 
 %% Commands operating on string values
 internal_set_like(Client, Command, Key, Value) ->
-    erldis_client:send(Client, Command, [[Key, length(Value)],
-                                  [Value]]).
+    case erldis_sync_client:call(Client, Command, [[Key, length(Value)], [Value]]) of
+      [{error, _}=Error]->Error;
+      [R] when R == ok orelse R==nil orelse R == true orelse R == false -> R;
+      R -> R
+    end.
 
-get_all_results(Client) -> erldis_client:get_all_results(Client).
+%get_all_results(Client) -> erldis_client:get_all_results(Client).
 
-auth(Client, Password) -> erldis_client:ssend(Client, auth, [Password]).
+auth(Client, Password) -> erldis_sync_client:scall(Client, auth, [Password]).
 
 set(Client, Key, Value) -> internal_set_like(Client, set, Key, Value).
-get(Client, Key) -> erldis_client:ssend(Client, get, [Key]).
+get(Client, Key) -> erldis_sync_client:sr_scall(Client, get, [Key]).
 getset(Client, Key, Value) -> internal_set_like(Client, getset, Key, Value).
-mget(Client, Keys) -> erldis_client:ssend(Client, mget, Keys).
+mget(Client, Keys) -> erldis_sync_client:scall(Client, mget, Keys).
 setnx(Client, Key, Value) -> internal_set_like(Client, setnx, Key, Value).
-incr(Client, Key) -> erldis_client:ssend(Client, incr, [Key]).
-incrby(Client, Key, By) -> erldis_client:ssend(Client, incrby, [Key, By]).
-decr(Client, Key) -> erldis_client:ssend(Client, decr, [Key]).
-decrby(Client, Key, By) -> erldis_client:ssend(Client, decrby, [Key, By]).
+incr(Client, Key) -> erldis_sync_client:sr_scall(Client, incr, [Key]).
+incrby(Client, Key, By) -> erldis_sync_client:sr_scall(Client, incrby, [Key, By]).
+decr(Client, Key) -> erldis_sync_client:sr_scall(Client, decr, [Key]).
+decrby(Client, Key, By) -> erldis_sync_client:sr_scall(Client, decrby, [Key, By]).
 
 
 
 %% Commands operating on every value
-exists(Client, Key) -> erldis_client:ssend(Client, exists, [Key]).
-del(Client, Key) -> erldis_client:ssend(Client, del, [Key]).
-type(Client, Key) -> erldis_client:ssend(Client, type, [Key]).
-keys(Client, Pattern) -> erldis_client:ssend(Client, keys, [Pattern]).
-randomkey(Client, Key) -> erldis_client:ssend(Client, randomkey, [Key]).
-rename(Client, OldKey, NewKey) -> erldis_client:ssend(Client, rename, [OldKey, NewKey]).
-renamenx(Client, OldKey, NewKey) -> erldis_client:ssend(Client, renamenx, [OldKey, NewKey]).
-dbsize(Client) -> erldis_client:ssend(Client, dbsize).
-expire(Client, Key, Seconds) -> erldis_client:ssend(Client, expire, [Key, Seconds]).
-ttl(Client, Key) -> erldis_client:ssend(Client, ttl, [Key]).
+exists(Client, Key) -> erldis_sync_client:sr_scall(Client, exists, [Key]).
+del(Client, Key) -> erldis_sync_client:sr_scall(Client, del, [Key]).
+type(Client, Key) -> erldis_sync_client:sr_scall(Client, type, [Key]).
+keys(Client, Pattern) -> erldis_sync_client:scall(Client, keys, [Pattern]).
+randomkey(Client, Key) -> erldis_sync_client:sr_scall(Client, randomkey, [Key]).
+rename(Client, OldKey, NewKey) -> erldis_sync_client:sr_scall(Client, rename, [OldKey, NewKey]).
+renamenx(Client, OldKey, NewKey) -> erldis_sync_client:sr_scall(Client, renamenx, [OldKey, NewKey]).
+dbsize(Client) -> erldis_sync_client:sr_scall(Client, dbsize).
+expire(Client, Key, Seconds) -> erldis_sync_client:sr_scall(Client, expire, [Key, Seconds]).
+ttl(Client, Key) -> erldis_sync_client:sr_scall(Client, ttl, [Key]).
 
 
 
 %% Commands operating on lists
 rpush(Client, Key, Value) -> internal_set_like(Client, rpush, Key, Value).
 lpush(Client, Key, Value) -> internal_set_like(Client, lpush, Key, Value).
-llen(Client, Key) -> erldis_client:ssend(Client, llen, [Key]).
-lrange(Client, Key, Start, End) -> erldis_client:ssend(Client, lrange, [Key, Start, End]).
-ltrim(Client, Key, Start, End) -> erldis_client:ssend(Client, ltrim, [Key, Start, End]).
-lindex(Client, Key, Index) -> erldis_client:ssend(Client, lindex, [Key, Index]).
+llen(Client, Key) -> erldis_sync_client:sr_scall(Client, llen, [Key]).
+lrange(Client, Key, Start, End) -> erldis_sync_client:scall(Client, lrange, [Key, Start, End]).
+ltrim(Client, Key, Start, End) -> erldis_sync_client:scall(Client, ltrim, [Key, Start, End]).
+lindex(Client, Key, Index) -> erldis_sync_client:scall(Client, lindex, [Key, Index]).
 lset(Client, Key, Index, Value) ->
     erldis_client:send(Client, lset, [[Key, Index, length(Value)],
                                [Value]]).
 lrem(Client, Key, Number, Value) ->
     erldis_client:send(Client, lrem, [[Key, Number, length(Value)],
                                [Value]]).
-lpop(Client, Key) -> erldis_client:ssend(Client, lpop, [Key]).
-rpop(Client, Key) -> erldis_client:ssend(Client, rpop, [Key]).
+lpop(Client, Key) -> erldis_sync_client:scall(Client, lpop, [Key]).
+rpop(Client, Key) -> erldis_sync_client:scall(Client, rpop, [Key]).
 
 
 
 %% Commands operating on sets
 sadd(Client, Key, Value) -> internal_set_like(Client, sadd, Key, Value).
 srem(Client, Key, Value) -> internal_set_like(Client, srem, Key, Value).
-smove(Client, SrcKey, DstKey, Member) -> erldis_client:send(Client, smove, [[SrcKey, DstKey, length(Member)],
+smove(Client, SrcKey, DstKey, Member) -> erldis_sync_client:call(Client, smove, [[SrcKey, DstKey, length(Member)],
                                                                      [Member]]).
-scard(Client, Key) -> erldis_client:ssend(Client, scard, [Key]).
+scard(Client, Key) -> erldis_sync_client:scall(Client, scard, [Key]).
 sismember(Client, Key, Value) -> internal_set_like(Client, sismember, Key, Value).
-sintersect(Client, Keys) -> erldis_client:ssend(Client, sinter, Keys).
+sintersect(Client, Keys) -> erldis_sync_client:scall(Client, sinter, Keys).
 sinter(Client, Keys) -> sintersect(Client, Keys).
-sinterstore(Client, DstKey, Keys) -> erldis_client:ssend(Client, sinterstore, [DstKey|Keys]).
-sunion(Client, Keys) -> erldis_client:ssend(Client, sunion, Keys).
-sunionstore(Client, DstKey, Keys) -> erldis_client:ssend(Client, sunionstore, [DstKey|Keys]).
-sdiff(Client, Keys) -> erldis_client:ssend(Client, sdiff, Keys).
-sdiffstore(Client, DstKey, Keys) -> erldis_client:ssend(Client, sdiffstore, [DstKey|Keys]).
-smembers(Client, Key) -> erldis_client:ssend(Client, smembers, [Key]).
+sinterstore(Client, DstKey, Keys) -> erldis_sync_client:scall(Client, sinterstore, [DstKey|Keys]).
+sunion(Client, Keys) -> erldis_sync_client:scall(Client, sunion, Keys).
+sunionstore(Client, DstKey, Keys) -> erldis_sync_client:scall(Client, sunionstore, [DstKey|Keys]).
+sdiff(Client, Keys) -> erldis_sync_client:scall(Client, sdiff, Keys).
+sdiffstore(Client, DstKey, Keys) -> erldis_sync_client:scall(Client, sdiffstore, [DstKey|Keys]).
+smembers(Client, Key) -> erldis_sync_client:scall(Client, smembers, [Key]).
 
 
 %% Multiple DB commands
-select(Client, Index) -> erldis_client:ssend(Client, select, [Index]).
-move(Client, Key, DBIndex) -> erldis_client:ssend(Client, move, [Key, DBIndex]).
-flushdb(Client) -> erldis_client:ssend(Client, flushdb).
-flushall(Client) -> erldis_client:ssend(Client, flushall).
+select(Client, Index) -> erldis_sync_client:scall(Client, select, [Index]).
+move(Client, Key, DBIndex) -> erldis_sync_client:scall(Client, move, [Key, DBIndex]).
+flushdb(Client) -> erldis_sync_client:scall(Client, flushdb).
+flushall(Client) -> erldis_sync_client:scall(Client, flushall).
 
 
 %% Commands operating on both lists and sets
-sort(Client, Key) -> erldis_client:ssend(Client, sort, [Key]).
-sort(Client, Key, Extra) -> erldis_client:ssend(Client, sort, [Key, Extra]).    
+sort(Client, Key) -> erldis_sync_client:scall(Client, sort, [Key]).
+sort(Client, Key, Extra) -> erldis_sync_client:scall(Client, sort, [Key, Extra]).    
 
 
 %% Persistence control commands
-save(Client) -> erldis_client:ssend(Client, save).
-bgsave(Client) -> erldis_client:ssend(Client, bgsave).
-lastsave(Client) -> erldis_client:ssend(Client, lastsave).
-shutdown(Client) -> erldis_client:asend(Client, shutdown).
+save(Client) -> erldis_sync_client:scall(Client, save).
+bgsave(Client) -> erldis_sync_client:scall(Client, bgsave).
+lastsave(Client) -> erldis_sync_client:scall(Client, lastsave).
+shutdown(Client) -> erldis_client:scall(Client, shutdown).
 
 
 %% Remote server control commands
-info(Client) -> erldis_client:ssend(Client, info).
-slaveof(Client, Host, Port) -> erldis_client:ssend(Client, slaveof, [Host, Port]).
-slaveof(Client) -> erldis_client:ssend(Client, slaveof, ["no one"]).
+info(Client) -> erldis_sync_client:scall(Client, info).
+slaveof(Client, Host, Port) -> erldis_sync_client:scall(Client, slaveof, [Host, Port]).
+slaveof(Client) -> erldis_sync_client:scall(Client, slaveof, ["no one"]).

File src/erldis_proto.erl

     {read, nil};
 parse(empty, "*-1") ->
     {hold, nil};
+parse(empty, "*0") ->
+    {read, 0};
 parse(empty, "$" ++ BulkSize) ->
     {read, list_to_integer(BulkSize)};
 parse(read, "$" ++ BulkSize) ->

File src/erldis_sync_client.erl

 
 -include("erldis.hrl").
 
--export([scall/2, scall/3, call/2, call/3, stop/1, transact/1, transact/2, select/2, info/1]).
+-export([scall/2, scall/3, call/2, call/3, stop/1, transact/1, transact/2, select/2, info/1,  sr_scall/2, sr_scall/3]).
 -export([connect/0, connect/1, connect/2, connect/3, connect/4]).
 -export([init/1, handle_call/3, handle_cast/2, handle_info/2,
 		 terminate/2, code_change/3]).
 %%%%%%%%%%%%%%%%%%
 %% call command %%
 %%%%%%%%%%%%%%%%%%
+sr_scall(Client, Cmd) -> sr_scall(Client, Cmd, []).
+sr_scall(Client, Cmd, Args) -> 
+  [R] = scall(Client, Cmd, Args),
+  R.
 
 % This is the simple send with a single row of commands
 scall(Client, Cmd) -> scall(Client, Cmd, []).
 		{_, {read, nil}} ->
 			% reply with nil
 			send_reply(State#redis{buffer=[nil]});
+	  {_, {read, 0}} ->
+			% reply with nil
+			send_reply(State#redis{buffer=[]});
 		{0, {read, NBytes}} ->
 			% reply with Value added to buffer
 			Value = recv_value(Socket, NBytes),

File 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),
+%    ok = erldis:quit(Client),
+%    false = is_process_alive(Client).
+%    
 utils_test() ->
     ?assertEqual(erldis_client:str(1), "1"),
     ?assertEqual(erldis_client:str(atom), "atom"),
 basic_test() ->
     {ok, Client} = erldis:connect("localhost", 6379),
     erldis:flushall(Client),
-    erldis:get(Client, "pippo"),
-    erldis:set(Client, "hello", "kitty!"),
-    erldis:setnx(Client, "foo", "bar"),
-    erldis:setnx(Client, "foo", "bar"),
-    [ok, nil, ok, true, false] = erldis:get_all_results(Client),
+    nil = erldis:get(Client, "pippo"),
+    ok = erldis:set(Client, "hello", "kitty!"),
+    true = erldis:setnx(Client, "foo", "bar"),
+    false = erldis:setnx(Client, "foo", "bar"),
 
-    erldis:exists(Client, "hello"),
-    erldis:exists(Client, "foo"),
-    erldis:get(Client, "foo"),
-    erldis:mget(Client, ["hello", "foo"]),
-    erldis:del(Client, "hello"),
-    erldis:del(Client, "foo"),
-    erldis:exists(Client, "hello"),
-    erldis:exists(Client, "foo"),
-    [true, true, "bar", ["kitty!", "bar"], true, true, false, false] = erldis:get_all_results(Client),
+    true = erldis:exists(Client, "hello"),
+    true = erldis:exists(Client, "foo"),
+    "bar" = erldis:get(Client, "foo"),
+    ["kitty!", "bar"] = erldis:mget(Client, ["hello", "foo"]),
+    true = erldis:del(Client, "hello"),
+    true = erldis:del(Client, "foo"),
+    false = erldis:exists(Client, "hello"),
+    false = erldis:exists(Client, "foo"),
     
-    erldis:set(Client, "pippo", "pluto"),
-    erldis:sadd(Client, "pippo", "paperino"),
-    % foo doesn't exist, the result will be nil
-    erldis:lrange(Client, "foo", 1, 2),
-    erldis:lrange(Client, "pippo", 1, 2),
-    [ok,
-     {error, "ERR Operation against a key holding the wrong kind of value"},
-     nil,
-     {error, "ERR Operation against a key holding the wrong kind of value"}
-    ] = erldis:get_all_results(Client),
-    erldis:del(Client, "pippo"),
-    [true] = erldis:get_all_results(Client),
-
-    erldis:rpush(Client, "a_list", "1"),
-    erldis:rpush(Client, "a_list", "2"),
-    erldis:rpush(Client, "a_list", "3"),
-    erldis:rpush(Client, "a_list", "1"),
-    erldis:lrem(Client, "a_list", 1, "1"),
-    erldis:lrange(Client, "a_list", 0, 2),
-    [ok, ok, ok, ok, true, ["2", "3", "1"]] = erldis:get_all_results(Client),
-
-    erldis:sort(Client, "a_list"),
-    erldis:sort(Client, "a_list", "DESC"), 
-    erldis:lrange(Client, "a_list", 0, 2),
-    erldis:sort(Client, "a_list", "LIMIT 0 2 ASC"),
-    [["1", "2", "3"], ["3", "2", "1"], ["2", "3", "1"],
-     ["1", "2"]] = erldis:get_all_results(Client),
-
-    ok = erldis:quit(Client).
+    erldis:sadd(Client, "set", "toto"),
+    ["toto"] = erldis:smembers(Client, "set"),
+    erldis:srem(Client,"set", "toto"),
+    [] = erldis:smembers(Client, "set").
+    
+    %%% 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).