anchor.diff:  %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   %% See the License for the specific language governing permissions and
   %% limitations under the License.
  +%%
  +%% @doc Store and aggregate statistics collected from memcached.
  +%%
   
   -module(stats_archiver).
   
  
positive.code:     {reply, ringbuffer:to_list(N, Samples), State}.
  
  
  handle_cast(unhandled, unhandled) ->
      unhandled.
  
  
  handle_info({stats, Bucket, Sample}, State = #state{bucket=Bucket,
                                                     samples=Samples}) ->
      gen_event:notify(ns_stats_event, {sample_archived, Bucket, Sample}),
      {noreply, State#state{samples=ringbuffer:add(Sample,
                                                   Samples)}}.
  
  
  terminate(_Reason, _State) ->
      ok.
  
  
  %% Internal functions
  
  server(Bucket) ->
      list_to_atom(?MODULE_STRING ++ "-" ++ Bucket).
  
negative.code: latest(minute, Bucket, N) ->
      gen_server:call(server(Bucket), {latest, N}).
  
  
  latest(minute, Node, Bucket, N) when is_atom(Node) ->
      gen_server:call({server(Bucket), Node}, {latest, N});
  latest(minute, Nodes, Bucket, N) ->
      gen_server:multi_call(Nodes, server(Bucket), {latest, N}).
  
  
  latest_all(minute, Bucket) ->
      gen_server:multi_call(server(Bucket), latest).
  
  
  latest_all(minute, Bucket, N) ->
      gen_server:multi_call(server(Bucket), {latest, N}).
  
  
  %%
  %% gen_server callbacks
  %%
  
  code_change(_OldVsn, State, _Extra) ->
      {ok, State}.
  
  
  init(Bucket) ->
      TableName = create_table(Bucket),
      ns_pubsub:subscribe(ns_stats_event,
                          fun ({stats, B, Stats}, {B, Pid}) ->
                                  received(Pid, Stats),
                                  {B, Pid};
                              (_, State) -> State
                          end, {Bucket, self()}),
      {ok, #state{bucket=Bucket, samples=ringbuffer:new(?NUM_SAMPLES),
                  table=TableName}}.
  
  
  handle_call(latest, _From, State = #state{samples=Samples}) ->
  