Commits

Jacob Perkins  committed 22765f8

util modules all use erldis

  • Participants
  • Parent commits 83f69e2

Comments (0)

Files changed (6)

File ebin/erldis.appup

 {"0.1.2", [
 	{"0.1.1", [
-		{load_module, erldis_client}
+		{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},
 	]}
 ], [
 	{"0.1.1", [
-		{load_module, erldis_client}
+		{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},

File src/erldis.erl

 	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) ->
-	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) ->
-	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).
 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) -> erldis_client:sr_scall(Client, <<"scard ">>, [Key]).
 
 sismember(Client, Key, Value) -> internal_set_like(Client, <<"sismember ">>, Key, Value).
 

File src/erldis_client.erl

 
 -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]).
 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;

File src/erldis_dict.erl

 		 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.
 
 
 %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) -> numeric_value(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.
 
 
 % NOTE: this returns new count value, not a modified dict
 update_counter(Key, 1, Client) ->
-	numeric_value(scall(Client, <<"incr ">>, [Key]));
+	numeric_value(erldis:incr(Client, Key));
 update_counter(Key, Incr, Client) ->
-	numeric_value(scall(Client, <<"incrby ">>, [Key, Incr])).
+	numeric_value(erldis:incrby(Client, 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]]).
+numeric_value(false) -> 0;
+numeric_value(true) -> 1;
+numeric_value(Val) -> Val.

File src/erldis_list.erl

 is_empty(Key, Client) -> len(Key, Client) == 0.
 
 len(Key, Client) ->
-	case scall(Client, <<"llen ">>, [Key]) of
-		[false] -> 0;
-		[true] -> 1;
-		[Len] -> Len
+	case erldis:llen(Client, Key) of
+		false -> 0;
+		true -> 1;
+		Len -> Len
 	end.
 
 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.
 %% 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).
 
 % any
 % append
 
-delete(Elem, Key, Client) -> call(Client, <<"lrem ">>, Key, 1, Elem).
+delete(Elem, Key, Client) -> erldis:lrem(Client, Key, 1, Elem).
 
 % dropwhile
 
 		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
 
 	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
 	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).
 
 % 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

File src/erldis_sets.erl

 
 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
+	case erldis:scard(Client, Key) of
 		% redis actually returns 0 & 1, but those are interpreted as false & true
-		[false] -> 0;
-		[true] -> 1;
-		[Size] -> Size
+		false -> 0;
+		true -> 1;
+		Size -> Size
 	end.
 
-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
+	case erldis:sismember(Client, Key, Elem) of
+		false -> false;
+		true -> true
 	end.
 
-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]]).