Permalink
Browse files

fixing stats bug

  • Loading branch information...
1 parent bc3b5c9 commit dd1ba63f44fc23d09f3cbd4a81e4317da2361dca Jacob Vorreuter committed Jun 2, 2009
Showing with 31 additions and 8 deletions.
  1. +25 −6 src/mcerlang.erl
  2. +2 −2 t/mcerlang_t_001.t
  3. +4 −0 t/mcerlang_t_002.t
View
31 src/mcerlang.erl
@@ -35,7 +35,7 @@
%% api callbacks
-export([get/1, get_many/1, add/2, add/3, set/2, set/3,
replace/2, replace/3, delete/1, increment/4, decrement/4,
- append/2, prepend/2, stat/0, flush/1, quit/0, version/0]).
+ append/2, prepend/2, stats/0, flush/1, quit/0, version/0]).
-export([find_next_largest/2]).
@@ -92,8 +92,8 @@ append(Key, Value) when is_binary(Value) ->
prepend(Key, Value) when is_binary(Value) ->
gen_server:call(?MODULE, {prepend, Key, Value}).
-stat() ->
- gen_server:call(?MODULE, stat).
+stats() ->
+ gen_server:call(?MODULE, stats).
flush(Expiration) when is_integer(Expiration) ->
gen_server:call(?MODULE, {flush, Expiration}).
@@ -226,8 +226,16 @@ handle_call({prepend, Key0, Value}, _From, State) ->
Resp = send_recv(Socket, #request{op_code=?OP_Prepend, key=list_to_binary(Key), value=Value}),
{reply, Resp#response.value, State};
-handle_call(stat, _From, State) ->
- Reply = send_all(State, #request{op_code=?OP_Stat}),
+handle_call(stats, _From, State) ->
+ Sockets = [begin
+ {{Host, Port}, begin
+ send(Socket, #request{op_code=?OP_Stat}),
+ Socket
+ end}
+ end || {{Host, Port}, [Socket|_]} <- State#state.sockets],
+ Reply = [begin
+ {HostPortKey, collect_stats_from_socket(Socket)}
+ end || {HostPortKey, Socket} <- Sockets],
{reply, Reply, State};
handle_call({flush, Expiration}, _From, State) ->
@@ -289,7 +297,18 @@ send_all(State, Request) ->
Resp#response.value
end}
end || {{Host, Port}, [Socket|_]} <- State#state.sockets].
-
+
+collect_stats_from_socket(Socket) ->
+ collect_stats_from_socket(Socket, []).
+
+collect_stats_from_socket(Socket, Acc) ->
+ case recv(Socket) of
+ #response{body_size=0} ->
+ Acc;
+ #response{key=Key, value=Value} ->
+ collect_stats_from_socket(Socket, [{binary_to_atom(Key, utf8), binary_to_list(Value)}|Acc])
+ end.
+
send_recv(Socket, Request) ->
ok = send(Socket, Request),
recv(Socket).
View
4 t/mcerlang_t_001.t
@@ -16,8 +16,8 @@ main(_) ->
start() ->
{ok, Socket} = gen_tcp:connect("localhost", 11211, [binary, {packet, 0}, {active, false}]),
- gen_tcp:send(Socket, <<128,16,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0>>),
- etap:ok((fun({ok, _}) -> true; (_) -> false end)(gen_tcp:recv(Socket, 0, 2000)), "stats"),
+ gen_tcp:send(Socket, <<128,10,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0>>),
+ etap:ok((fun({ok, _}) -> true; (_) -> false end)(gen_tcp:recv(Socket, 0, 2000)), "noop"),
etap:is(mcerlang:find_next_largest(4, [{1,a}, {2,b}, {5,c}, {7,d}, {14,e}]), c, "find next largest"),
etap:is(mcerlang:find_next_largest(1, [{1,a}, {2,b}, {5,c}, {7,d}, {14,e}]), b, "find next largest"),
View
4 t/mcerlang_t_002.t
@@ -26,11 +26,15 @@ start() ->
etap:is(mcerlang:get("Hello"), <<"World2!!!">>, "get ok"),
etap:is(mcerlang:prepend("Hello", <<"$$$">>), <<>>, "prepend ok"),
etap:is(mcerlang:get("Hello"), <<"$$$World2!!!">>, "get ok"),
+ etap:is(mcerlang:delete("Hello"), <<>>, "delete ok"),
+ etap:is(mcerlang:get("Hello"), <<>>, "get ok"),
mcerlang:set("One", <<"A">>),
mcerlang:set("Two", <<"B">>),
mcerlang:set("Three", <<"C">>),
+ io:format("stats ~p~n", [mcerlang:stats()]),
+
etap:is(mcerlang:get_many(["One", "Two", "Two-and-a-half", "Three"]), [{"One",<<"A">>},{"Two",<<"B">>},{"Two-and-a-half",<<>>},{"Three",<<"C">>}], "get_many ok"),
etap:is(mcerlang:flush(0), [{{"127.0.0.1",11211},<<>>}], "flush ok"),

0 comments on commit dd1ba63

Please sign in to comment.