anchor.diff:          {error, missing_source}
       end.
   
  -go(#db{name=DbName} = Db, #shard{} = Target, LocalId) ->
  +go(#db{name = DbName, seq_tree = Bt} = Db, #shard{} = Target, LocalId) ->
       erlang:put(io_priority, {internal_repl, DbName}),
       Seq = calculate_start_seq(Db, Target, LocalId),
       Acc0 = #acc{source=Db, target=Target, seq=Seq, localid=LocalId},
  -    Fun = fun ?MODULE:changes_enumerator/2,
  -    {ok, AccOut} = couch_db:changes_since(Db, all_docs, Seq, Fun, [], Acc0),
  +    Fun = fun ?MODULE:changes_enumerator/3,
  +    {ok, _LastReduction, AccOut} = couch_btree:fold(Bt, Fun, Acc0,
  +        [{start_key, Seq + 1}]),
       {ok, _} = replicate_batch(AccOut).
   
   make_local_id(#shard{node=SourceNode}, #shard{node=TargetNode}) ->
  
positive.code:     T = couch_util:encodeBase64Url(couch_util:md5(term_to_binary(TargetNode))),
      <<"_local/shard-sync-", S/binary, "-", T/binary>>.
  
  changes_enumerator(DocInfo, #acc{revcount = C} = Acc) when C >= 99 ->
      Seq = DocInfo#doc_info.high_seq,
      replicate_batch(Acc#acc{seq = Seq, infos = [DocInfo | Acc#acc.infos]});
  
  changes_enumerator(DocInfo, #acc{revcount = C, infos = Infos} = Acc) ->
      RevCount = C + length(DocInfo#doc_info.revs),
      Seq = DocInfo#doc_info.high_seq,
      {ok, Acc#acc{seq = Seq, revcount = RevCount, infos = [DocInfo | Infos]}}.
  
  replicate_batch(#acc{target = #shard{node=Node, name=Name}} = Acc) ->
      case find_missing_revs(Acc) of
      [] ->
          ok;
      Missing ->
          ok = save_on_target(Node, Name, open_docs(Acc#acc.source, Missing))
      end,
      update_locals(Acc),
      {ok, Acc#acc{revcount=0, infos=[]}}.
  
  find_missing_revs(Acc) ->
      #acc{target = #shard{node=Node, name=Name}, infos = Infos} = Acc,
      IdsRevs = lists:map(fun(#doc_info{id=Id, revs=RevInfos}) ->
          {Id, [R || #rev_info{rev=R} <- RevInfos]}
      end, Infos),
      Options = [{io_priority, {internal_repl, Name}}],
      rexi_call(Node, {fabric_rpc, get_missing_revs, [Name, IdsRevs, Options]}).
  
  open_docs(Db, Missing) ->
      lists:flatmap(fun({Id, Revs, _}) ->
          {ok, Docs} = couch_db:open_doc_revs(Db, Id, Revs, [latest]),
          [Doc || {ok, Doc} <- Docs]
      end, Missing).
  
  save_on_target(Node, Name, Docs) ->
  
negative.code: -module(mem3_rep).
  
  -export([go/2, changes_enumerator/3, make_local_id/2]).
  
  -include("mem3.hrl").
  -include_lib("couch/include/couch_db.hrl").
  