anchor.diff:      ets:update_counter(Stats, docs_read, length(Docs)),
       
       %% save them (maybe in a buffer)
  -    {NewBuffer, NewContext} = case couch_util:should_flush() of
  +    {NewBuffer, NewContext} = 
  +    case should_flush(lists:flatlength([Docs|Buffer])) of
           true ->
               Docs2 = lists:flatten([Docs|Buffer]),
               {ok, Errors} = update_docs(Target, Docs2, [], replicated_changes),
  
positive.code: 
  -export([replicate/2]).
  
  -include("couch_db.hrl").
  
  %% @spec replicate(Source::binary(), Target::binary()) -> 
  %%      {ok, Stats} | {error, Reason}
  
negative.code: open_doc_revs(Db, DocId, Revs, Options) ->
      couch_db:open_doc_revs(Db, DocId, Revs, Options).
  
  %% @spec should_flush() -> true | false
  %% @doc Calculates whether it's time to flush the document buffer. Considers
  %%        - memory utilization
  %%        - number of pending document writes
  %%        - approximate number of pending attachment writes
  should_flush(DocCount) when DocCount > ?BUFFER_NDOCS ->
      true;
  should_flush(_DocCount) ->
      MeAndMyLinks = [self()|element(2,process_info(self(),links))],
      
      case length(MeAndMyLinks)/2 > ?BUFFER_NATTACHMENTS of
      true -> true;
      false ->
          case memory_footprint(MeAndMyLinks) > 2*?BUFFER_MEMORY of
          true ->
              [garbage_collect(Pid) || Pid <- MeAndMyLinks],
              memory_footprint(MeAndMyLinks) > ?BUFFER_MEMORY;
          false -> false
          end
      end.
  
  %% @spec memory_footprint([pid()]) -> integer()
  %% @doc Sum of process and binary memory utilization for all processes in list
  memory_footprint(PidList) ->
      ProcessMemory = lists:foldl(fun(Pid, Acc) ->
          Acc + element(2,process_info(Pid, memory))
      end, 0, PidList),
      
      BinaryMemory = lists:foldl(fun(Pid, Acc) ->
          Acc + binary_memory(Pid)
      end, 0, PidList),
      
      ?LOG_DEBUG("ProcessMem ~p BinaryMem ~p", [ProcessMemory, BinaryMemory]),
      ProcessMemory + BinaryMemory.
  
  %% @spec binary_memory(pid()) -> integer()
  %% @doc Memory utilization of all binaries referenced by this process.
  binary_memory(Pid) ->
      lists:foldl(fun({_Id, Size, _NRefs}, Acc) -> Size+Acc end,
          0, element(2,process_info(Pid, binary))).
  
  update_doc(#http_db{uri=DbUrl, headers=Headers}, #doc{id=DocId}=Doc, Options) ->
      [] = Options,
  