Commits

Jacob Perkins committed 2618b8f

more cleanup

Comments (0)

Files changed (3)

 
 smembers(Client, Key) -> erldis_client:scall(Client, <<"smembers ">>, [Key]).
 
+% TODO: ordered sets
+
 %%%%%%%%%%%%%
 %% Sorting %%
 %%%%%%%%%%%%%

src/erldis_client.erl

 -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]).
--export([str/1, format/2, format/1, sformat/1]).
+-export([bin/1, format/2, format/1, sformat/1]).
 -define(EOL, "\r\n").
 
 %%%%%%%%%%%%%
 %% helpers %%
 %%%%%%%%%%%%%
 
-str(X) when is_list(X) ->
+bin(X) when is_list(X) ->
 	list_to_binary(X);
-str(X) when is_atom(X) ->
+bin(X) when is_atom(X) ->
 	list_to_binary(atom_to_list(X));
-str(X) when is_binary(X) ->
+bin(X) when is_binary(X) ->
 	X;
-str(X) when is_integer(X) ->
+bin(X) when is_integer(X) ->
 	list_to_binary(integer_to_list(X));
-str(X) when is_float(X) ->
+bin(X) when is_float(X) ->
 	list_to_binary(float_to_list(X));
-str(X) ->
+bin(X) ->
 	term_to_binary(X).
 
+format(Lines) ->
+	format(Lines, <<>>).
+
 format([], Result) ->
 	Result;
 format([Line|Rest], <<>>) ->
 	JoinedLine = binary_join(Line, <<" ">>),
 	format(Rest, JoinedLine);
-
 format([Line|Rest], Result) ->
 	Sep = <<?EOL>>,
 	JoinedLine = binary_join(Line, <<" ">>),
 	format(Rest, <<Result/binary, Sep/binary, JoinedLine/binary>>).
 
-format(Lines) ->
-	format(Lines, <<>>).
-
 sformat(<<>>)->
 	<<>>;
 sformat(Line) ->
 	<<>>;
 binary_join(Array, Sep)->
 	F = fun(Elem, Acc) ->
-			E2 = str(Elem),
+			E2 = bin(Elem),
 			<<Acc/binary, Sep/binary, E2/binary>>
 		end,
 	
 			V
 	end.
 
-ensure_started(#redis{socket=undefined, host=Host, port=Port, timeout=Timeout, db=DB}=State)->
-	Opts = [binary, {active, false}, {packet, line}, {nodelay, true},
-			{send_timeout, Timeout}],
-	
-	case gen_tcp:connect(Host, Port, Opts, Timeout) of
-		{ok, Socket} ->
-			End = <<?EOL>>,
-			
-			if
-				DB == <<"0">> ->
-					ok;
-				true ->
-					gen_tcp:send(Socket, <<"select ", DB/binary, End/binary>>),
-					{ok, <<"+OK", _R/binary>>} = gen_tcp:recv(Socket, 10)
-			end,
-			
-			inet:setopts(Socket, [{active, once}]),
-			State#redis{socket=Socket};
-		{error, Why} ->
-			Report = [{?MODULE, unable_to_connect}, {error, Why}, State],
-			error_logger:warning_report(Report),
-			State
-	end;
-ensure_started(State)->
-	State.
+%%%%%%%%%%%%%%%%%%%
+%% call commands %%
+%%%%%%%%%%%%%%%%%%%
 
-%%%%%%%%%%%%%%%%%%
-%% call command %%
-%%%%%%%%%%%%%%%%%%
+select(Client, DB) ->
+	DBB = list_to_binary(integer_to_list(DB)),
+	[ok] = scall(Client, <<"select ", DBB/binary>>),
+	Client.
 
 sr_scall(Client, Cmd) -> sr_scall(Client, Cmd, []).
 
 			end
 	end.
 
-% stop is synchronous so can be sure that client is shutdown
-stop(Client) -> gen_server2:call(Client, disconnect).
-
+% TODO: use multi/exec for transact (and move transact to erldis.erl)
 transact(F) ->
 	case connect() of
 		{error, Error} -> {error, Error};
 		exit:Result -> stop(Client), exit(Result)
 	end.
 
-select(Client, DB) ->
-	DBB = list_to_binary(integer_to_list(DB)),
-	[ok] = scall(Client, <<"select ", DBB/binary>>),
-	Client.
+%%%%%%%%%%%%%%%%
+%% redis info %%
+%%%%%%%%%%%%%%%%
 
 info(Client) ->
 	F = fun(Stat) ->
 		end,
 
 	[S] = scall(Client, info),
-	elists:mapfilter(F, string:tokens(binary_to_list(S), "\r\n")).
+	elists:mapfilter(F, string:tokens(binary_to_list(S), ?EOL)).
 
 parse_stat(<<"redis_version:",Vsn/binary>>) ->
 	{version, Vsn};
 parse_stat(_) ->
 	undefined.
 
-%%%%%%%%%%
-%% init %%
-%%%%%%%%%%
+%%%%%%%%%%%%%%%%%%%%
+%% connect & init %%
+%%%%%%%%%%%%%%%%%%%%
 
 connect() ->
 	{ok, Host} = app_get_env(erldis, host, "localhost"),
 		Other -> Other
 	end.
 
+% stop is synchronous so can be sure that client is shutdown
+stop(Client) -> gen_server2:call(Client, disconnect).
+
 init([Host, Port]) ->
 	process_flag(trap_exit, true),
 	{ok, Timeout} = app_get_env(erldis, timeout, 500),
-	% presence of send_timeout_close Opt causes {error, badarg}
-	Opts = [binary, {active, once}, {packet, line}, {nodelay, true},
+	State = #redis{calls=queue:new(), host=Host, port=Port, timeout=Timeout},
+	
+	case connect_socket(State, once) of
+		{error, Why} -> {stop, {socket_error, Why}};
+		{ok, NewState} -> {ok, NewState}
+	end.
+
+ensure_started(#redis{socket=undefined, db=DB}=State) ->
+	case connect_socket(State, false) of
+		{error, Why} ->
+			Report = [{?MODULE, unable_to_connect}, {error, Why}, State],
+			error_logger:warning_report(Report),
+			State;
+		{ok, #redis{socket=Socket, _='_'}=NewState} ->
+			if
+				DB == <<"0">> ->
+					ok;
+				true ->
+					gen_tcp:send(Socket, <<"select ", DB/binary, ?EOL>>),
+					{ok, <<"+OK", _R/binary>>} = gen_tcp:recv(Socket, 10)
+			end,
+			
+			inet:setopts(Socket, [{active, once}]),
+			NewState
+	end;
+ensure_started(State)->
+	State.
+
+connect_socket(#redis{socket=undefined, host=Host, port=Port, timeout=Timeout}=State, Active) ->
+	% NOTE: send_timeout_close not used because causes {error, badarg}
+	Opts = [binary, {active, Active}, {packet, line}, {nodelay, true},
 			{send_timeout, Timeout}],
 	% without timeout, default is infinity
 	case gen_tcp:connect(Host, Port, Opts, Timeout) of
-		{error, Why} ->
-			{stop, {socket_error, Why}};
-		{ok, Socket} ->
-			% calls is a queue instead of a count
-			{ok, #redis{socket=Socket, calls=queue:new(), host=Host, port=Port, timeout=Timeout}}
-	end.
+		{ok, Socket} -> {ok, State#redis{socket=Socket}};
+		{error, Why} -> {error, Why}
+	end;
+connect_socket(State, _) ->
+	{ok, State}.
 
 %%%%%%%%%%%%%%%%%
 %% handle_call %%
 		0 ->
 			% answers came earlier than we could start listening...
 			% Very unlikely but totally possible.
-			{reply, lists:reverse(State#redis.results), State#redis{results=[], calls=Calls}};
+			Reply = lists:reverse(State#redis.results),
+			{reply, Reply, State#redis{results=[], calls=Calls}};
 		_ ->
 			% We are here earlier than results came, so just make
 			% ourselves wait until stuff is ready.
-			{noreply, State#redis{reply_caller=fun(V) -> gen_server2:reply(From, V) end}}
+			R = fun(V) -> gen_server2:reply(From, V) end,
+			{noreply, State#redis{reply_caller=R}}
 	end;
 handle_call({send, Cmd}, From, State1) ->
 	% NOTE: redis ignores sent commands it doesn't understand, which means
 	% we don't get a reply, which means callers will timeout
-	End = <<?EOL>>,
 	State = ensure_started(State1),
 	
-	case gen_tcp:send(State#redis.socket, [Cmd|End]) of
+	case gen_tcp:send(State#redis.socket, [Cmd | <<?EOL>>]) of
 		ok ->
 			Queue = queue:in(From, State#redis.calls),
 			

test/erldis_tests.erl

 %	 false = is_process_alive(Client).
 %
 utils_test() ->
-	?assertEqual(erldis_client:str(1), <<"1">>),
-	?assertEqual(erldis_client:str(atom), <<"atom">>),
+	?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">>).