Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

* tests/memcached_tests.erl (getbig_test): Added tests to check for

  big data.

* src/memcached.erl (fetchmore): Added a function to deal with large
  chunks of data coming back from the server.
  • Loading branch information...
commit c33b8a050daf00e9d4cb71720a5917f62bdf5cbd 1 parent 57d4db6
davidw authored
View
10 lib/memcached/ChangeLog
@@ -0,0 +1,10 @@
+2007-10-02 David N. Welton <davidw@dedasys.com>
+
+ * tests/memcached_tests.erl (getbig_test): Added tests to check
+ for big data.
+
+2007-10-02 David N. Welton <davidw@efoninc.com>
+
+ * src/memcached.erl (fetchmore): Added a function to deal with
+ large chunks of data coming back from the server.
+
View
37 lib/memcached/src/memcached.erl
@@ -211,24 +211,43 @@ to_list(Key) when is_atom(Key) ->
to_list(Key) when is_list(Key) ->
Key.
+%% parse_response helper to fetch data if there's a lot of it.
+fetchmore(Sock, Len, More) ->
+ %% Read what we need to grab the data.
+ {ok, <<Data/binary>>} = gen_tcp:recv(Sock, Len - size(More)),
+ Combined = <<More/binary, Data/binary>>,
+ if
+ size(Combined) < Len ->
+ {Bytes, Rest} = fetchmore(Sock, Len, Combined);
+ true ->
+ <<Bytes:Len/binary>> = Combined,
+ %% Read anything left.
+ {ok, <<"\r\n", Rest/binary>>} = gen_tcp:recv(Sock, 0)
+ end,
+ {Bytes, Rest}.
+
%% Parse the get response.
-parse_responses(<<"END\r\n", _/binary>>, Acc) ->
+parse_responses(_, <<"END\r\n", _/binary>>, Acc) ->
Acc;
-parse_responses(<<"\r\n", Data/binary>>, Acc) ->
- parse_responses(Data, Acc);
-parse_responses(<<"VALUE ", Data/binary>>, Acc) ->
+parse_responses(Sock, <<"\r\n", Data/binary>>, Acc) ->
+ parse_responses(Sock, Data, Acc);
+parse_responses(Sock, <<"VALUE ", Data/binary>>, Acc) ->
{ok, [_, _, Len], More} = io_lib:fread("~s ~u ~u\r\n", binary_to_list(Data)),
- <<Bytes:Len/binary, Rest/binary>> = list_to_binary(More),
- parse_responses(Rest, Acc ++ [binary_to_term(Bytes)]).
+ if
+ length(More) < Len ->
+ %% If we didn't read all the data, fetch the rest.
+ {Bytes, Rest} = fetchmore(Sock, Len, list_to_binary(More));
+ true ->
+ <<Bytes:Len/binary, Rest/binary>> = list_to_binary(More)
+ end,
+ parse_responses(Sock, Rest, Acc ++ [binary_to_term(Bytes)]).
%% Send get and handle the response.
process_get(Sock, Keys) ->
- io:format("Keys ~p ~n", [Keys]),
KeyList = [to_list(X) || X <- Keys],
- io:format("CHECK ~p ~n", [KeyList]),
ok = gen_tcp:send(Sock, list_to_binary(["get ", string_join(KeyList), "\r\n"])),
{ok, <<Data/binary>>} = gen_tcp:recv(Sock, 0),
- {ok, parse_responses(Data, [])}.
+ {ok, parse_responses(Sock, Data, [])}.
%% Set set and handle the response.
process_set(Sock, Operation, Key, Flags, Expire, Data) ->
View
12 lib/memcached/tests/memcached_tests.erl
@@ -9,6 +9,11 @@
start() ->
test().
+%% Generate lots of data to utilize for tests.
+generate_data() ->
+ lists:flatten([io_lib:format("~p", [X]) ||
+ X <- lists:seq(1, 1000)]).
+
get_server_pid() ->
{ok, Pid} = memcached:start_link('localhost', 11211),
Pid.
@@ -47,5 +52,12 @@ delete_test() ->
deletefail_test() ->
not_found = memcached:mcdelete(get_server_pid(), doesntexist).
+setbig_test() ->
+ memcached:mcset(get_server_pid(), big,
+ generate_data()).
+getbig_test() ->
+ Data = generate_data(),
+ {ok, Data} = memcached:mcget(get_server_pid(), big).
+
quit_test() ->
memcached:mcquit(get_server_pid()).
Please sign in to comment.
Something went wrong with that request. Please try again.