Skip to content

Commit

Permalink
Aggregate count
Browse files Browse the repository at this point in the history
  • Loading branch information
MasseR committed May 2, 2013
1 parent 7c2de89 commit 20ce972
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/itemstats_srv.erl
Expand Up @@ -82,6 +82,8 @@ init([]) ->
%% {stop, Reason, State}
%% @end
%%--------------------------------------------------------------------
handle_call({get, Key}, _From, State) ->
{reply, internal_aggregate_value(Key), State};
handle_call({get, Key, TurtleId}, _From, State) ->
{reply, internal_value(Key, TurtleId), State};
handle_call(_Request, _From, State) ->
Expand Down Expand Up @@ -186,4 +188,12 @@ internal_value(Key, TurtleId) ->
_Res -> 0
end.

% Full scan for now
internal_aggregate_value(Key) ->
Turtles = case dets:lookup(?MODULE, turtles) of
[ {turtles, X} ] -> X;
[] -> sets:new()
end,
sets:fold(fun(Turtle, Acc) -> Acc + internal_value(Key, Turtle) end, 0, Turtles).

make_key(Key, TurtleId) -> TurtleId ++ "-" ++ Key.

0 comments on commit 20ce972

Please sign in to comment.