Skip to content

Commit

Permalink
implement list_streams function with coverage call
Browse files Browse the repository at this point in the history
  • Loading branch information
marianoguerra committed Nov 25, 2014
1 parent 022de97 commit 5a2ca66
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
12 changes: 11 additions & 1 deletion src/flavio.erl
Expand Up @@ -2,7 +2,7 @@
-include("flavio.hrl").
-include_lib("riak_core/include/riak_core_vnode.hrl").

-export([ping/0, add/2, stats/0, post_msg/3, get_msgs/4]).
-export([ping/0, add/2, stats/0, post_msg/3, get_msgs/4, list_streams/1]).

-ignore_xref([ping/0, add/2, stats/0]).

Expand Down Expand Up @@ -41,6 +41,16 @@ get_msgs(Username, Stream, Id, Count) ->
{Username, Stream}),
wait_for_reqid(ReqID, Timeout).

list_streams(Username) ->
Timeout = 5000,
case flavio_coverage_fsm:start({list_streams, Username}, Timeout) of
{ok, Responses} ->
{ok, lists:filter(fun ({_Partition, _Node, {ok, []}}) -> false;
({_Partition, _Node, _Streams}) -> true
end, Responses)};
Other -> Other
end.

stats() ->
Timeout = 5000,
flavio_coverage_fsm:start(stats, Timeout).
Expand Down
20 changes: 17 additions & 3 deletions src/flavio_vnode.erl
Expand Up @@ -17,9 +17,7 @@
handle_coverage/4,
handle_exit/3]).

-ignore_xref([
start_vnode/1
]).
-ignore_xref([start_vnode/1]).

-record(state, {partition, ops_count=0, base_dir}).

Expand Down Expand Up @@ -90,6 +88,11 @@ delete(State) ->

handle_coverage(stats, _KeySpaces, {_, RefId, _}, State=#state{ops_count=OpsCount}) ->
{reply, {RefId, [{ops_count, OpsCount}]}, State};

handle_coverage({list_streams, Username}, _KeySpaces, {_, RefId, _}, State) ->
Streams = lists:sort(list_streams(State, Username)),
{reply, {RefId, {ok, Streams}}, State};

handle_coverage(Req, _KeySpaces, _Sender, State) ->
lager:warning("unknown coverage received ~p", [Req]),
{norepl, State}.
Expand All @@ -102,6 +105,17 @@ terminate(_Reason, _State) ->

%% Private API

list_dir(Path) ->
case file:list_dir(Path) of
{error, enoent} -> [];
{ok, Names} -> Names
end.

list_streams(#state{partition=Partition, base_dir=BaseDir}, Username) ->
PartitionStr = integer_to_list(Partition),
UserPath = filename:join([BaseDir, PartitionStr, Username]),
lists:map(fun list_to_binary/1, list_dir(UserPath)).

get_stream(#state{partition=Partition, base_dir=BaseDir}, Username, Stream) ->
PartitionStr = integer_to_list(Partition),
StreamPath = filename:join([BaseDir, PartitionStr, Username, Stream, "msgs"]),
Expand Down

0 comments on commit 5a2ca66

Please sign in to comment.