Permalink
Browse files

sync from japerk's bitbucket

  • Loading branch information...
1 parent 7182451 commit 8eab955a78727b1b61c0e890c7cb06e08fbd1f49 @cstar cstar committed Jan 21, 2010
View
17 AUTHORS
@@ -0,0 +1,17 @@
+Valentino Volonghi
+ - initial codebase
+ http://bitbucket.org/adroll/erldis/
+
+Jacob Perkins
+ - synchronous client
+ - stdlib emulation modules
+ http://bitbucket.org/japerk/erldis/
+
+Eric Cestari
+ - binary communication
+ - MULTI/EXEC
+ http://github.com/cstar/erldis
+
+tonyg
+ - BLPOP & BRPOP
+ https://bitbucket.org/tonyg/erldis/
View
@@ -2,6 +2,10 @@ Copyright (c) 2009
adroll.com
Valentino Volonghi
+Copyright (c) 2009, 2010
+weotta.com
+Jacob Perkins
+
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
View
@@ -14,7 +14,7 @@ clean_tests:
(cd test;$(MAKE) clean)
rm -rf erl_crash.dump *.beam
-test: clean
+test: FORCE
mkdir -p ebin/
(cd src;$(MAKE))
(cd test;$(MAKE))
@@ -37,4 +37,6 @@ plt:
@dialyzer --build_plt --output_plt .plt -q -r . -I include/
check: all
- @dialyzer --check_plt --plt .plt -q -r . -I include/
+ @dialyzer --check_plt --plt .plt -q -r . -I include/
+
+FORCE:
View
@@ -1,12 +1,12 @@
{application, erldis, [
{description, "Erlang Redis application"},
- {vsn, "0.1.1"},
+ {vsn, "0.1.3"},
{registered, [erldis_sup]},
{mod, {erldis_app, []}},
{applications, [kernel, stdlib]},
{modules, [
erldis_client, erldis, erldis_proto, erldis_app, erldis_sup,
- erldis_sets, erldis_dict, erldis_list, gen_server2
+ erldis_sets, erldis_dict, erldis_list, gen_server2, erldis_sync_client
]},
{env, [{host, "localhost"}, {port, 6379}, {timeout, 500}]}
]}.
View
@@ -1,4 +1,12 @@
-{"0.1.1", [
+{"0.1.3", [
+ {"0.1.2", [{load_module, erldis_client}]},
+ {"0.1.1", [
+ {load_module, erldis},
+ {load_module, erldis_client},
+ {load_module, erldis_dict},
+ {load_module, erldis_list},
+ {load_module, erldis_sets}
+ ]},
{"0.1.0", [
{load_module, erldis_client},
{load_module, erldis}
@@ -48,6 +56,14 @@
{add_module, erldis_sets}
]}
], [
+ {"0.1.2", [{load_module, erldis_client}]},
+ {"0.1.1", [
+ {load_module, erldis},
+ {load_module, erldis_client},
+ {load_module, erldis_dict},
+ {load_module, erldis_list},
+ {load_module, erldis_sets}
+ ]},
{"0.1.0", [
{load_module, erldis_client},
{load_module, erldis}
View
@@ -43,6 +43,10 @@ internal_set_like(Client, Command, Key, Value) when is_binary(Value) ->
[R] when R == ok; R == nil; R == true; R == false -> R;
R -> R
end;
+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(Client, Command, Key, erldis_client:bin(Value));
internal_set_like(_, _, _, _) ->
{error, badarg}.
@@ -61,28 +65,57 @@ exec(Client, Fun) ->
{error, unsupported}
end.
+numeric(false) -> 0;
+numeric(true) -> 1;
+numeric(I) -> I.
+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Commands operating on every value %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-exists(Client, Key) -> erldis_client:sr_scall(Client, <<"exists ", Key/binary>>).
-
-del(Client, Key) -> erldis_client:sr_scall(Client, <<"del ", Key/binary>>).
-
-type(Client, Key) -> erldis_client:sr_scall(Client, <<"type ", Key/binary>>).
-
-keys(Client, Pattern) -> erldis_client:scall(Client, <<"keys ", Pattern/binary>>).
-
-randomkey(Client, Key) -> erldis_client:sr_scall(Client, <<"randomkey ", Key/binary>>).
-
+exists(Client, Key) when not is_binary(Key) ->
+ exists(Client, erldis_client:bin(Key));
+exists(Client, Key) ->
+ erldis_client:sr_scall(Client, <<"exists ", Key/binary>>).
+
+del(Client, Key) when not is_binary(Key) ->
+ del(Client, erldis_client:bin(Key));
+del(Client, Key) ->
+ erldis_client:sr_scall(Client, <<"del ", Key/binary>>).
+
+type(Client, Key) when not is_binary(Key) ->
+ type(Client, erldis_client:bin(Key));
+type(Client, Key) ->
+ erldis_client:sr_scall(Client, <<"type ", Key/binary>>).
+
+keys(Client, Pattern) when not is_binary(Pattern) ->
+ keys(Client, erldis_client:bin(Pattern));
+keys(Client, Pattern) ->
+ erldis_client:scall(Client, <<"keys ", Pattern/binary>>).
+
+randomkey(Client, Key) when not is_binary(Key) ->
+ randomkey(Client, erldis_client:bin(Key));
+randomkey(Client, Key) ->
+ erldis_client:sr_scall(Client, <<"randomkey ", Key/binary>>).
+
+rename(Client, OldKey, NewKey) when not is_binary(OldKey) ->
+ rename(Client, erldis_client:bin(OldKey), NewKey);
+rename(Client, OldKey, NewKey) when not is_binary(NewKey) ->
+ rename(Client, OldKey, erldis_client:bin(NewKey));
rename(Client, OldKey, NewKey) ->
erldis_client:sr_scall(Client, <<"rename ", OldKey/binary, " ", NewKey/binary>>).
+renamenx(Client, OldKey, NewKey) when not is_binary(OldKey) ->
+ renamenx(Client, erldis_client:bin(OldKey), NewKey);
+renamenx(Client, OldKey, NewKey) when not is_binary(NewKey) ->
+ renamenx(Client, OldKey, erldis_client:bin(NewKey));
renamenx(Client, OldKey, NewKey) ->
erldis_client:sr_scall(Client, <<"renamenx ", OldKey/binary, " ", NewKey/binary>>).
-dbsize(Client) -> erldis_client:sr_scall(Client, <<"dbsize ">>).
+dbsize(Client) -> numeric(erldis_client:sr_scall(Client, <<"dbsize ">>)).
+expire(Client, Key, Seconds) when not is_binary(Key) ->
+ expire(Client, erldis_client:bin(Key), Seconds);
expire(Client, Key, Seconds) ->
erldis_client:sr_scall(Client, <<"expire ", Key/binary, " ", Seconds/binary>>).
@@ -94,21 +127,32 @@ ttl(Client, Key) -> erldis_client:sr_scall(Client, <<"ttl ", Key/binary>>).
set(Client, Key, Value) -> internal_set_like(Client, <<"set ">>, Key, Value).
-get(Client, Key) -> erldis_client:sr_scall(Client, <<"get ", Key/binary>>).
+get(Client, Key) when not is_binary(Key) ->
+ get(Client, erldis_client:bin(Key));
+get(Client, Key) ->
+ erldis_client:sr_scall(Client, <<"get ", Key/binary>>).
getset(Client, Key, Value) -> internal_set_like(Client, <<"getset ">>, Key, Value).
mget(Client, Keys) -> erldis_client:scall(Client, <<"mget ">>, Keys).
setnx(Client, Key, Value) -> internal_set_like(Client, <<"setnx ">>, Key, Value).
-incr(Client, Key) -> erldis_client:sr_scall(Client, <<"incr ", Key/binary>>).
+incr(Client, Key) when not is_binary(Key) ->
+ incr(Client, erldis_client:bin(Key));
+incr(Client, Key) ->
+ numeric(erldis_client:sr_scall(Client, <<"incr ", Key/binary>>)).
-incrby(Client, Key, By) -> erldis_client:sr_scall(Client, <<"incrby ">>, [Key, By]).
+incrby(Client, Key, By) ->
+ numeric(erldis_client:sr_scall(Client, <<"incrby ">>, [Key, By])).
-decr(Client, Key) -> erldis_client:sr_scall(Client, <<"decr ", Key/binary>>).
+decr(Client, Key) when not is_binary(Key) ->
+ decr(Client, erldis_client:bin(Key));
+decr(Client, Key) ->
+ numeric(erldis_client:sr_scall(Client, <<"decr ", Key/binary>>)).
-decrby(Client, Key, By) -> erldis_client:sr_scall(Client, <<"decrby ">>, [Key, By]).
+decrby(Client, Key, By) ->
+ numeric(erldis_client:sr_scall(Client, <<"decrby ">>, [Key, By])).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Commands operating on lists %%
@@ -118,7 +162,7 @@ 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:sr_scall(Client, <<"llen ">>, [Key]).
+llen(Client, Key) -> numeric(erldis_client:sr_scall(Client, <<"llen ">>, [Key])).
lrange(Client, Key, Start, End) ->
erldis_client:scall(Client, <<"lrange ">>, [Key, Start, End]).
@@ -127,17 +171,21 @@ ltrim(Client, Key, Start, End) ->
erldis_client:scall(Client, <<"ltrim ">>, [Key, Start, End]).
lindex(Client, Key, Index) ->
- erldis_client:scall(Client, <<"lindex ">>, [Key, Index]).
+ erldis_client:sr_scall(Client, <<"lindex ">>, [Key, Index]).
+lset(Client, Key, Index, Value) when not is_binary(Value) ->
+ lset(Client, Key, Index, erldis_client:bin(Value));
lset(Client, Key, Index, Value) ->
- erldis_client:send(Client, <<"lset ">>, [[Key, Index, size(Value)], [Value]]).
+ erldis_client:call(Client, <<"lset ">>, [[Key, Index, size(Value)], [Value]]).
+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:send(Client, <<"lrem ">>, [[Key, Number, size(Value)], [Value]]).
+ erldis_client:call(Client, <<"lrem ">>, [[Key, Number, size(Value)], [Value]]).
-lpop(Client, Key) -> erldis_client:scall(Client, <<"lpop ">>, [Key]).
+lpop(Client, Key) -> erldis_client:sr_scall(Client, <<"lpop ">>, [Key]).
-rpop(Client, Key) -> erldis_client:scall(Client, <<"rpop ">>, [Key]).
+rpop(Client, Key) -> erldis_client:sr_scall(Client, <<"rpop ">>, [Key]).
blpop(Client, Keys) -> erldis_client:bcall(Client, <<"blpop ">>, Keys, infinity).
blpop(Client, Keys, Timeout) -> erldis_client:bcall(Client, <<"blpop ">>, Keys, Timeout).
@@ -153,10 +201,12 @@ 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) when not is_binary(Member) ->
+ smove(Client, SrcKey, DstKey, erldis_client:bin(Member));
smove(Client, SrcKey, DstKey, Member) ->
erldis_client:call(Client, <<"smove ">>, [[SrcKey, DstKey, size(Member)], [Member]]).
-scard(Client, Key) -> erldis_client:scall(Client, <<"scard ">>, [Key]).
+scard(Client, Key) -> numeric(erldis_client:sr_scall(Client, <<"scard ">>, [Key])).
sismember(Client, Key, Value) -> internal_set_like(Client, <<"sismember ">>, Key, Value).
@@ -193,14 +243,14 @@ sort(Client, Key, Extra) -> erldis_client:scall(Client, <<"sort ">>, [Key, Extra
%% Multiple DB commands %%
%%%%%%%%%%%%%%%%%%%%%%%%%%
-select(Client, Index) -> erldis_client:scall(Client, <<"select ">>, [Index]).
+select(Client, Index) -> erldis_client:sr_scall(Client, <<"select ">>, [Index]).
move(Client, Key, DBIndex) ->
erldis_client:scall(Client, <<"move ">>, [Key, DBIndex]).
-flushdb(Client) -> erldis_client:scall(Client, <<"flushdb ">>).
+flushdb(Client) -> erldis_client:sr_scall(Client, <<"flushdb ">>).
-flushall(Client) -> erldis_client:scall(Client, <<"flushall ">>).
+flushall(Client) -> erldis_client:sr_scall(Client, <<"flushall ">>).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Persistence control commands %%
View
@@ -15,7 +15,8 @@
-include("erldis.hrl").
--export([scall/2, scall/3, scall/4, call/2, call/3, call/4, bcall/4, sr_scall/2, sr_scall/3]).
+-export([sr_scall/2, sr_scall/3, scall/2, scall/3, scall/4, call/2, call/3, call/4,
+ bcall/4, set_call/4, send/3]).
-export([stop/1, transact/1, transact/2, select/2, info/1]).
-export([connect/0, connect/1, connect/2, connect/3, connect/4]).
-export([start_link/0, start_link/1, start_link/2, start_link/3, start_link/4]).
@@ -127,6 +128,11 @@ call(Client, Cmd, Args, Timeout) ->
bcall(Client, Cmd, Args, Timeout) ->
scall(Client, Cmd, Args ++ [server_timeout(Timeout)], erlang_timeout(Timeout)).
+set_call(Client, Cmd, Key, Val) when is_binary(Val) ->
+ call(Client, Cmd, [[Key, erlang:size(Val)], [Val]]);
+set_call(Client, Cmd, Key, Val) ->
+ set_call(Client, Cmd, Key, bin(Val)).
+
% Erlang uses milliseconds, with symbol "infinity" for "wait forever";
% redis uses seconds, with 0 for "wait forever".
server_timeout(infinity) -> 0;
@@ -183,26 +189,26 @@ info(Client) ->
end
end,
- [S] = scall(Client, info),
+ [S] = scall(Client, <<"info ">>),
elists:mapfilter(F, string:tokens(binary_to_list(S), ?EOL)).
-parse_stat(<<"redis_version:",Vsn/binary>>) ->
+parse_stat("redis_version:"++Vsn) ->
{version, Vsn};
-parse_stat(<<"uptime_in_seconds:",Val/binary>>) ->
+parse_stat("uptime_in_seconds:"++Val) ->
{uptime, list_to_integer(Val)};
-parse_stat(<<"connected_clients:",Val/binary>>) ->
+parse_stat("connected_clients:"++Val) ->
{clients, list_to_integer(Val)};
-parse_stat(<<"connected_slaves:",Val/binary>>) ->
+parse_stat("connected_slaves:"++Val) ->
{slaves, list_to_integer(Val)};
-parse_stat(<<"used_memory:",Val/binary>>) ->
+parse_stat("used_memory:"++Val) ->
{memory, list_to_integer(Val)};
-parse_stat(<<"changes_since_last_save:",Val/binary>>) ->
+parse_stat("changes_since_last_save:"++Val) ->
{changes, list_to_integer(Val)};
-parse_stat(<<"last_save_time:",Val/binary>>) ->
+parse_stat("last_save_time:"++Val) ->
{last_save, list_to_integer(Val)};
-parse_stat(<<"total_connections_received:",Val/binary>>) ->
+parse_stat("total_connections_received:"++Val) ->
{connections, list_to_integer(Val)};
-parse_stat(<<"total_commands_processed:",Val/binary>>) ->
+parse_stat("total_commands_processed:"++Val) ->
{commands, list_to_integer(Val)};
parse_stat(_) ->
undefined.
@@ -274,7 +280,9 @@ ensure_started(#redis{socket=undefined, db=DB}=State) ->
Report = [{?MODULE, unable_to_connect}, {error, Why}, State],
error_logger:warning_report(Report),
State;
- {ok, #redis{socket=Socket, _='_'}=NewState} ->
+ {ok, NewState} ->
+ Socket = NewState#redis.socket,
+
if
DB == <<"0">> ->
ok;
@@ -359,10 +367,8 @@ handle_cast({send, Cmd}, #redis{remaining=Remaining, calls=Calls} = State1) ->
gen_tcp:send(State#redis.socket, [Cmd|End]),
case Remaining of
- 0 ->
- {noreply, State#redis{remaining=1, calls=Queue}};
- _ ->
- {noreply,State#redis{calls=Queue}}
+ 0 -> {noreply, State#redis{remaining=1, calls=Queue}};
+ _ -> {noreply, State#redis{calls=Queue}}
end;
handle_cast(_, State) ->
{noreply, State}.
Oops, something went wrong.

0 comments on commit 8eab955

Please sign in to comment.