Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

sync from japerk's bitbucket

  • Loading branch information...
commit 8eab955a78727b1b61c0e890c7cb06e08fbd1f49 1 parent 7182451
Eric Cestari authored
17 AUTHORS
View
@@ -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/
4 LICENSE
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
6 Makefile
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:
4 ebin/erldis.app
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}]}
]}.
18 ebin/erldis.appup
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}
102 src/erldis.erl
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,7 +127,10 @@ 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).
@@ -102,13 +138,21 @@ 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 %%
38 src/erldis_client.erl
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}.
40 src/erldis_dict.erl
View
@@ -5,21 +5,21 @@
update_counter/2, update_counter/3]).
% NOTE: use erldis_lists instead, fetch & find won't work for lists
-append(Key, Value, Client) -> set_call(Client, <<"rpush ">>, Key, Value).
+append(Key, Value, Client) -> erldis:rpush(Client, Key, Value).
append_list(Key, Values, Client) ->
lists:foreach(fun(Value) -> append(Key, Value, Client) end, Values).
-erase(Key, Client) -> scall(Client, <<"del ">>, [Key]).
+erase(Key, Client) -> erldis:del(Client, Key).
fetch(Key, Client) ->
- case scall(Client, <<"get ">>, [Key]) of
- [nil] -> undefined;
- [Value] -> Value
+ case erldis:get(Client, Key) of
+ nil -> undefined;
+ Value -> Value
end.
% NOTE: this is only useful if keys have a known prefix
-fetch_keys(Pattern, Client) -> scall(Client, <<"keys ">>, [Pattern]).
+fetch_keys(Pattern, Client) -> erldis:keys(Client, Pattern).
%filter(Pred, Client) -> ok.
@@ -33,13 +33,12 @@ find(Key, Client) ->
%from_list(List, Client) -> ok.
-is_key(Key, Client) -> hd(scall(Client, <<"exists ">>, [Key])).
+is_key(Key, Client) -> erldis:exists(Client, Key).
-size(Client) ->
- numeric_value(erldis_sync_client:scall(Client, <<"dbsize ">>)).
+size(Client) -> erldis:dbsize(Client).
store(Key, [], Client) -> erase(Key, Client);
-store(Key, Value, Client) -> set_call(Client, <<"set ">>, Key, Value).
+store(Key, Value, Client) -> erldis:set(Client, Key, Value).
%to_list(Client) -> ok.
@@ -56,22 +55,5 @@ update(Key, Fun, Initial, Client) ->
update_counter(Key, Client) -> update_counter(Key, 1, Client).
% NOTE: this returns new count value, not a modified dict
-update_counter(Key, 1, Client) ->
- numeric_value(scall(Client, <<"incr ">>, [Key]));
-update_counter(Key, Incr, Client) ->
- numeric_value(scall(Client, <<"incrby ">>, [Key, Incr])).
-
-%%%%%%%%%%%%%
-%% helpers %%
-%%%%%%%%%%%%%
-
-numeric_value([false]) -> 0;
-numeric_value([true]) -> 1;
-numeric_value([Val]) -> Val.
-
-% TODO: copied from erldis_sets, abstract if possible, maybe use a macro
-
-scall(Client, Cmd, Args) -> erldis_sync_client:scall(Client, Cmd, Args).
-
-set_call(Client, Cmd, Key, Val) ->
- erldis_sync_client:call(Client, Cmd, [[Key, erlang:size(Val)], [Val]]).
+update_counter(Key, 1, Client) -> erldis:incr(Client, Key);
+update_counter(Key, Incr, Client) -> erldis:incrby(Client, Key, Incr).
43 src/erldis_list.erl
View
@@ -22,27 +22,22 @@ is_queue(Key, Client) -> is_list(Key, Client).
is_empty(Key, Client) -> len(Key, Client) == 0.
-len(Key, Client) ->
- case scall(Client, <<"llen ">>, [Key]) of
- [false] -> 0;
- [true] -> 1;
- [Len] -> Len
- end.
+len(Key, Client) -> erldis:llen(Client, Key).
in(Item, Key, Client) ->
- [ok] = set_call(Client, <<"rpush ">>, Key, Item).
+ ok = erldis:rpush(Client, Key, Item).
in_r(Item, Key, Client) ->
- [ok] = set_call(Client, <<"lpush ">>, Key, Item).
+ ok = erldis:lpush(Client, Key, Item).
out(Key, Client) ->
- case hd(scall(Client, <<"lpop ">>, [Key])) of
+ case erldis:lpop(Client, Key) of
nil -> empty;
Item -> {value, Item}
end.
out_r(Key, Client) ->
- case hd(scall(Client, <<"rpop ">>, [Key])) of
+ case erldis:rpop(Client, Key) of
nil -> empty;
Item -> {value, Item}
end.
@@ -100,11 +95,11 @@ peek_r(Key, Client) ->
%% array like api %%
%%%%%%%%%%%%%%%%%%%%
-get(I, Key, Client) -> hd(scall(Client, <<"lindex ">>, [Key, I])).
+get(I, Key, Client) -> erldis:lindex(Client, Key, I).
is_array(Key, Client) -> is_list(Key, Client).
-set(I, Value, Key, Client) -> call(Client, <<"lset ">>, Key, I, Value).
+set(I, Value, Key, Client) -> erldis:lset(Client, Key, I, Value).
size(Key, Client) -> len(Key, Client).
@@ -116,7 +111,7 @@ size(Key, Client) -> len(Key, Client).
% any
% append
-delete(Elem, Key, Client) -> call(Client, <<"lrem ">>, Key, 1, Elem).
+delete(Elem, Key, Client) -> erldis:lrem(Client, Key, 1, Elem).
% dropwhile
@@ -128,7 +123,7 @@ foreach(I, F, Key, Client) ->
Item -> F(Item), foreach(I+1, F, Key, Client)
end.
-is_list(Key, Client) -> [<<"list">>] == scall(Client, <<"type ">>, [Key]).
+is_list(Key, Client) -> <<"list">> == erldis:type(Client, Key).
% keysort
@@ -171,10 +166,10 @@ sublist(Key, Client, Start, 1) ->
end;
sublist(Key, Client, Start, Len) when Start > 0, Len > 1 ->
% erlang lists are 1-indexed
- scall(Client, <<"lrange ">>, [Key, Start - 1, Start + Len - 2]);
+ erldis:lrange(Client, Key, Start - 1, Start + Len - 2);
sublist(Key, Client, Start, Len) when Start < 0, Len > 1 ->
% can give a negative start where -1 is the last element
- scall(Client, <<"lrange ">>, [Key, Start, Start - Len + 1]).
+ erldis:lrange(Client, Key, Start, Start - Len + 1).
% sort
% takewhile
@@ -228,7 +223,7 @@ foldr(I, F, Acc0, Key, Client) ->
end.
from_list(L, Key, Client) ->
- scall(Client, <<"del ">>, [Key]),
+ erldis:del(Client, Key),
lists:foreach(fun(Item) -> in(Item, Key, Client) end, L).
to_list(Key, Client) -> foldr(fun(Item, L) -> [Item | L] end, [], Key, Client).
@@ -237,16 +232,4 @@ reverse(Key, Client) -> foldl(fun(Item, L) -> [Item | L] end, [], Key, Client).
% filter
% map
-% member
-
-%%%%%%%%%%%%%
-%% helpers %%
-%%%%%%%%%%%%%
-
-call(Client, Cmd, Key, N, Val) ->
- erldis_client:call(Client, Cmd, [[Key, N, size(Val)], [Val]]).
-
-scall(Client, Cmd, Args) -> erldis_client:scall(Client, Cmd, Args).
-
-set_call(Client, Cmd, Key, Val) ->
- erldis_client:call(Client, Cmd, [[Key, size(Val)], [Val]]).
+% member
41 src/erldis_sets.erl
View
@@ -14,59 +14,38 @@
delete(Client) -> erldis_client:stop(Client).
-is_set(Client, Key) -> [<<"set">>] == scall(Client, <<"type ">>, [Key]).
+is_set(Client, Key) -> <<"set">> == erldis:type(Client, Key).
-size(Client, Key) ->
- case scall(Client, <<"scard ">>, [Key]) of
- % redis actually returns 0 & 1, but those are interpreted as false & true
- [false] -> 0;
- [true] -> 1;
- [Size] -> Size
- end.
+size(Client, Key) -> erldis:scard(Client, Key).
-to_list(Client, Key) -> scall(Client, <<"smembers ">>, [Key]).
+to_list(Client, Key) -> erldis:smembers(Client, Key).
from_list(Client, Key, List) ->
% delete existing set
- scall(Client, <<"del ">>, [Key]),
+ erldis:del(Client, Key),
lists:foreach(fun(Elem) -> add_element(Elem, Client, Key) end, List),
Client.
-is_element(Elem, Client, Key) ->
- case set_call(Client, <<"sismember ">>, Key, Elem) of
- [false] -> false;
- [true] -> true
- end.
+is_element(Elem, Client, Key) -> erldis:sismember(Client, Key, Elem).
-add_element(Elem, Client, Key) -> set_call(Client, <<"sadd ">>, Key, Elem).
+add_element(Elem, Client, Key) -> erldis:sadd(Client, Key, Elem).
-del_element(Elem, Client, Key) -> set_call(Client, <<"srem ">>, Key, Elem).
+del_element(Elem, Client, Key) -> erldis:srem(Client, Key, Elem).
-union(Client, Keys) -> scall(Client, <<"sunion ">>, Keys).
+union(Client, Keys) -> erldis:sunion(Client, Keys).
intersection(Client, Key1, Key2) -> intersection(Client, [Key1, Key2]).
-intersection(Client, Keys) -> scall(Client, <<"sinter ">>, Keys).
+intersection(Client, Keys) -> erldis:sintersect(Client, Keys).
is_disjoint(Client, Key1, Key2) -> [] == intersection(Client, [Key1, Key2]).
subtract(Client, Key1, Key2) -> subtract(Client, [Key1, Key2]).
-subtract(Client, Keys) -> scall(Client, <<"sdiff ">>, Keys).
+subtract(Client, Keys) -> erldis:sdiff(Client, Keys).
is_subset(Client, Key1, Key2) -> [] == subtract(Client, [Key2, Key1]).
fold(F, Acc0, Client, Key) -> lists:foldl(F, Acc0, to_list(Client, Key)).
filter(Pred, Client, Key) -> lists:filter(Pred, to_list(Client, Key)).
-
-%%%%%%%%%%%%%
-%% helpers %%
-%%%%%%%%%%%%%
-
-% TODO: handle {error, Reason}. throw exception?
-
-scall(Client, Cmd, Args) -> erldis_client:scall(Client, Cmd, Args).
-
-set_call(Client, Cmd, Key, Val) ->
- erldis_client:call(Client, Cmd, [[Key, erlang:size(Val)], [Val]]).
2  test/erldis_dict_tests.erl
View
@@ -6,7 +6,7 @@ dict_test() ->
% setup
application:load(erldis),
{ok, Client} = erldis_client:connect(),
- ?assertEqual(erldis_client:call(Client, <<"flushdb">>), [ok]),
+ ?assertEqual(erldis:flushdb(Client), ok),
% empty dict
?assertEqual(0, erldis_dict:size(Client)),
?assertEqual(undefined, erldis_dict:fetch(<<"foo">>, Client)),
74 test/erldis_list_compatibility_tests.erl
View
@@ -0,0 +1,74 @@
+-module(erldis_list_compatibility_tests).
+
+-include_lib("eunit/include/eunit.hrl").
+
+getset_test() ->
+ Client = setup(),
+ ?assertEqual(erldis:exists(Client, <<"foo">>), false),
+ ?assertEqual(erldis:exists(Client, "foo"), false),
+ ?assertEqual(erldis:get(Client, <<"foo">>), nil),
+ ?assertEqual(erldis:get(Client, "foo"), nil),
+ ?assertEqual(erldis:set(Client, <<"foo">>, <<"bar">>), ok),
+ ?assertEqual(erldis:get(Client, <<"foo">>), <<"bar">>),
+ ?assertEqual(erldis:get(Client, "foo"), <<"bar">>),
+ ?assertEqual(erldis:exists(Client, <<"foo">>), true),
+ ?assertEqual(erldis:exists(Client, "foo"), true),
+ ?assertEqual(erldis:del(Client, "foo"), true),
+ ?assertEqual(erldis:exists(Client, <<"foo">>), false),
+ ?assertEqual(erldis:exists(Client, "foo"), false),
+ ?assertEqual(erldis:set(Client, "fool", "baz"), ok),
+ ?assertEqual(erldis:get(Client, "fool"), <<"baz">>),
+ ?assertEqual(erldis:get(Client, <<"fool">>), <<"baz">>),
+ ?assertEqual(erldis:exists(Client, <<"fool">>), true),
+ ?assertEqual(erldis:exists(Client, "fool"), true),
+ ?assertEqual(erldis:del(Client, <<"fool">>), true),
+ ?assertEqual(erldis:exists(Client, <<"fool">>), false),
+ ?assertEqual(erldis:exists(Client, "fool"), false).
+
+incrdecr_test() ->
+ Client = setup(),
+ ?assertEqual(erldis:incr(Client, <<"foo">>), 1),
+ ?assertEqual(erldis:incr(Client, "foo"), 2),
+ ?assertEqual(erldis:incrby(Client, <<"foo">>, 2), 4),
+ ?assertEqual(erldis:incrby(Client, "foo", 3), 7),
+ ?assertEqual(erldis:decr(Client, <<"foo">>), 6),
+ ?assertEqual(erldis:decr(Client, "foo"), 5),
+ ?assertEqual(erldis:decrby(Client, <<"foo">>, 2), 3),
+ ?assertEqual(erldis:decrby(Client, "foo", 3), 0).
+
+list_test() ->
+ Client = setup(),
+ ?assertEqual(erldis:exists(Client, "foo"), false),
+ ?assertEqual(erldis:lpush(Client, "foo", "a"), ok),
+ ?assertEqual(erldis:rpush(Client, "foo", "b"), ok),
+ ?assertEqual(erldis:lindex(Client, "foo", 0), <<"a">>),
+ ?assertEqual(erldis:lindex(Client, "foo", 1), <<"b">>),
+ ?assertEqual(erldis:llen(Client, "foo"), 2),
+ ?assertEqual(erldis_list:is_list("foo", Client), true).
+
+set_test() ->
+ Client = setup(),
+ ?assertEqual(erldis:exists(Client, "foo"), false),
+ ?assertEqual(erldis:sadd(Client, "foo", "bar"), true),
+ ?assertEqual(erldis:scard(Client, "foo"), 1),
+ ?assertEqual(erldis:smembers(Client, "foo"), [<<"bar">>]),
+ ?assertEqual(erldis:sismember(Client, "foo", "bar"), true),
+ ?assertEqual(erldis_sets:is_set(Client, "foo"), true).
+
+json_test() ->
+ % requires yaws json module
+ Arr = {array, ["a", "b", "c"]},
+ Json = json:encode(Arr),
+ Client = setup(),
+ ?assertEqual(erldis:set(Client, "json", Json), ok),
+ GetJson = erldis:get(Client, "json"),
+ ?assertEqual(GetJson, list_to_binary(Json)),
+ {ok, Arr2} = json:decode_string(binary_to_list(GetJson)),
+ ?assertEqual(Arr, Arr2).
+
+setup() ->
+ % setup
+ application:load(erldis),
+ {ok, Client} = erldis_client:connect(),
+ ?assertEqual(erldis:flushdb(Client), ok),
+ Client.
16 test/erldis_list_tests.erl
View
@@ -146,6 +146,20 @@ merge_test() ->
erldis_client:stop(Client).
+umerge_test() ->
+ Client = setup(),
+ Key = <<"foo">>,
+ F = fun(A, B) -> A =< B end,
+ ?assertEqual(0, erldis_list:len(Key, Client)),
+ L1 = [<<"a">>, <<"c">>, <<"e">>],
+ erldis_list:umerge(F, L1, Key, Client),
+ L1 = erldis_list:to_list(Key, Client),
+ erldis_list:umerge(F, L1, Key, Client),
+ L1 = erldis_list:to_list(Key, Client),
+ erldis_list:umerge(F, [<<"a">>], Key, Client),
+ L1 = erldis_list:to_list(Key, Client),
+ erldis_client:stop(Client).
+
common_test() ->
Client = setup(),
?assertEqual(0, erldis_list:len(<<"foo">>, Client)),
@@ -184,5 +198,5 @@ setup() ->
% setup
application:load(erldis),
{ok, Client} = erldis_client:connect(),
- ?assertEqual(erldis_client:call(Client, <<"flushdb ">>), [ok]),
+ ?assertEqual(erldis:flushdb(Client), ok),
Client.
2  test/erldis_sets_tests.erl
View
@@ -6,7 +6,7 @@ sets_test() ->
% setup
application:load(erldis),
{ok, Client} = erldis_client:connect(),
- ?assertEqual(erldis_client:call(Client, <<"flushdb">>), [ok]),
+ ?assertEqual(erldis:flushdb(Client), ok),
% non existent set
?assertEqual(erldis_sets:is_set(Client, <<"foo">>), false),
?assertEqual(erldis_sets:to_list(Client, <<"foo">>), []),
Please sign in to comment.
Something went wrong with that request. Please try again.