Skip to content

Commit

Permalink
wrote a better parser for VALUE responses
Browse files Browse the repository at this point in the history
  • Loading branch information
joewilliams committed Jan 13, 2009
1 parent 424c6a4 commit d5df736
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions src/merle.erl
Expand Up @@ -349,17 +349,20 @@ send_cmd(Socket, Cmd, Value) ->
Reply = recv_reply(),
Reply.

%% Reply format <<"VALUE SOMEKEY FLAG BYTES\r\nSOMEVALUE\r\nEND\r\n">>

%% @private
%% @doc get_term functin for extracting terms from responses
get_term(Reply) ->
List = string:tokens(binary_to_list(Reply), "\r\n"),
[_,ListTerm,_] = List,
BinTerm = list_to_binary(ListTerm),
get_term(Data) ->
Parse = io_lib:fread("~s ~s ~u ~u\r\n", binary_to_list(Data)),
{ok,[_,_,_,Bytes], Rest} = Parse,
RestBin = list_to_binary(Rest),
<<BinTerm:Bytes/binary, "\r\nEND\r\n">> = RestBin,
Term = binary_to_term(BinTerm),
Term.

%% @private
%% @doc get_response functin for extracting text from responses
%% @doc get_response function for extracting text from responses
get_response(Reply) ->
Response = string:tokens(binary_to_list(Reply), "\r\n"),
Response.
Expand All @@ -368,13 +371,15 @@ get_response(Reply) ->
%% @doc receive replies from memcached
recv_reply() ->
receive
{tcp,_,Reply} ->
case Reply of
<<"VALUE ", _/binary>> ->
get_term(Reply);
_ ->
get_response(Reply)
end
{tcp,_,Reply} ->
% check to see what type of response it is
case Reply of
<<"VALUE ", _/binary>> ->
get_term(Reply);
_ ->
get_response(Reply)
end
after 5000 ->
timeout
timeout
end.

0 comments on commit d5df736

Please sign in to comment.