anchor.diff:  %% See the License for the specific language governing permissions and
   %% limitations under the License.
   %%
  -%% @doc Store and aggregate statistics collected from memcached.
  +%% @doc Store statistics collected from memcached.
   %%
   
   -module(stats_archiver).
  
positive.code:       end, archives()).
  
  
  %% @doc Return the last N records starting with the given key from Tab.
  fetch_latest(Bucket, Period, N) ->
      case lists:keyfind(Period, 1, archives()) of
          false ->
              {error, bad_period, Period};
          {_, Interval, _} ->
              Seconds = N * Interval,
              Tab = table(Bucket, Period),
              case mnesia:dirty_last(Tab) of
                  '$end_of_table' ->
                      {ok, []};
                  Key ->
                      Oldest = Key - Seconds * 1000 + 500,
                      Handle = qlc:q([Sample || #stat_entry{timestamp=TS} = Sample
                                                    <- mnesia:table(Tab), TS > Oldest]),
                      case mnesia:activity(async_dirty, fun qlc:eval/1, [Handle]) of
                          {error, _, _} = Error ->
                              Error;
                          Results ->
                              {ok, Results}
                      end
              end
      end.
  
  
  last_chunk(Tab, Step) ->
      case mnesia:last(Tab) of
          '$end_of_table' ->
  
negative.code:     {ok, #state{bucket=Bucket}}.
  
  
  handle_call(Request, _From, State) ->
      {reply, {unhandled, Request}, State}.
  
  
  handle_cast(_Msg, State) ->
      {noreply, State}.
  
  
  handle_info({stats, Bucket, Sample}, State = #state{bucket=Bucket}) ->
  