anchor.diff:      Root = config:get("couchdb", "database_dir", "."),
       NormRoot = couch_util:normpath(Root),
       filelib:fold_files(Root, "_replicator(\\.[0-9]{10,})?.couch$", true,
  -        fun(Filename, _) ->
  +        fun(Filename, Acc) ->
   	    % shamelessly stolen from couch_server.erl
               NormFilename = couch_util:normpath(Filename),
               case NormFilename -- NormRoot of
  
positive.code:     nil ->
          ok;
      #rep_state{rep = #rep{db_name = DbName, doc_id = DocId}} ->
          ok = add_error_jitter(),
          update_rep_doc(DbName, DocId, [
              {<<"_replication_state">>, <<"error">>},
              {<<"_replication_state_reason">>, to_binary(error_reason(Error))},
              {<<"_replication_id">>, ?l2b(BaseId)}]),
          ok = gen_server:call(?MODULE, {rep_error, RepId, Error}, infinity)
      end.
  
  % Add random delay proportional to the number of replications
  % on current node, in order to prevent a stampede when a source
  % with multiple replication targets fails
  add_error_jitter() ->
      RepCount = ets:info(?REP_TO_STATE, size),
      Range = min(2 * RepCount * ?AVG_ERROR_DELAY_MSEC, ?MAX_ERROR_DELAY_MSEC),
      timer:sleep(random:uniform(Range)).
  
  
  continue(#rep{doc_id = null}) ->
      {true, no_owner};
  
negative.code:                 RelativeFilename -> ok
              end,
              DbName = ?l2b(filename:rootname(RelativeFilename, ".couch")),
              Jitter = jitter(Acc),
              spawn_link(fun() ->
                  timer:sleep(Jitter),
                  gen_server:cast(Server, {resume_scan, DbName})
              end),
  	    Acc + 1
  	end, 1).
  
  % calculate random delay proportional to the number of replications
  % on current node, in order to prevent a stampede:
  %   - when a source with multiple replication targets fails
  %   - when we restart couch_replication_manager
  jitter(N) ->
      Range = min(2 * N * ?AVG_DELAY_MSEC, ?MAX_DELAY_MSEC),
      random:uniform(Range).
  
  is_replicator_db(DbName) ->
      ?REPLICATOR_DB =:= couch_db:dbname_suffix(DbName).
  