anchor.diff:  
   
   handle_call({latest, Period, Bucket}, _From, State) ->
  -    Reply = try mnesia:transaction(
  +    Reply = try mnesia:activity(
  +                  async_dirty,
                     fun () ->
                             Tab = table(Bucket, Period),
                             Key = mnesia:last(Tab),
                             hd(mnesia:read(Tab, Key))
  -                  end, ?RETRIES) of
  -                {atomic, Result} ->
  -                    {ok, Result};
  -                Err ->
  -                    {error, Err}
  +                  end, []) of
  +                Result ->
  +                    {ok, Result}
               catch
                   Type:Err -> {error, {Type, Err}}
               end,
       {reply, Reply, State};
   handle_call({latest, Period, Bucket, N}, _From, State) ->
  -    Reply = try walk(table(Bucket, Period), N) of
  -                Result -> {ok, Result}
  -            catch
  -                Type:Err -> {error, {Type, Err}}
  -            end,
  +    Reply = fetch_latest(Bucket, Period, N),
       {reply, Reply, State};
   handle_call({latest, Period, Bucket, Step, N}, _From, State) ->
  -    Reply = try resample(table(Bucket, Period), Step, N) of
  -                Result -> {ok, Result}
  -            catch
  -                Type:Err -> {error, {Type, Err}}
  -            end,
  +    Reply = resample(Bucket, Period, Step, N),
       {reply, Reply, State}.
   
   
  
positive.code: %% @doc Truncate a timestamp to the nearest multiple of N seconds.
  trunc_ts(TS, N) ->
      TS - (TS rem (N*1000)).
  
  
  %% @doc Return the last N records starting with the given key from Tab.
  walk(Tab, N) ->
      {atomic, Results} = mnesia:transaction(
                            fun () -> walk(Tab, mnesia:last(Tab), N, []) end,
                            ?RETRIES),
      Results.
  
  
  walk(_Tab, Key, N, Records) when N == 0; Key == '$end_of_table' ->
      Records;
  walk(Tab, Key, N, Records) ->
      [Record] = mnesia:read(Tab, Key),
      NextKey = mnesia:prev(Tab, Key),
      walk(Tab, NextKey, N - 1, [Record | Records]).
  
negative.code: 
  
  %% @doc Resample the stats in a table. Only reads the necessary number of rows.
  resample(Bucket, Period, Step, N) ->
      Seconds = N * Step,
      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]),
              F = fun (#stat_entry{timestamp = T} = Sample,
                       {T1, Acc, Chunk}) ->
                          case trunc_ts(T, Step) of
                              T1 ->
                                  {T1, Acc, [Sample|Chunk]};
                              T2 when T1 == undefined ->
                                  {T2, Acc, [Sample]};
                              T2 ->
                                  {T2, [avg(T1, Chunk)|Acc], [Sample]}
                          end
                  end,
              case mnesia:activity(async_dirty, fun qlc:fold/3,
                                   [F, {undefined, [], []},
                                    Handle]) of
                  {error, _, _} = Error ->
                      Error;
                  {undefined, [], []} ->
                      {ok, []};
                  {T, Acc, LastChunk} ->
                      {ok, lists:reverse([avg(T, LastChunk)|Acc])}
              end
      end.
  
  
  