anchor.diff:          end
       end.
   
  +
  +is_owner(<<"shards/", _/binary>>=DbName, DocId) ->
  +    mem3_util:owner(DbName, DocId);
  +is_owner(_, _) ->
  +    true.
  +
  +
   rep_db_update_error(Error, DbName, DocId) ->
       case Error of
       {bad_rep_doc, Reason} ->
  
positive.code: 
  
  maybe_tag_rep_doc(DbName, DocId, {RepProps}, RepId) ->
      case get_value(<<"_replication_id">>, RepProps) of
      RepId ->
          ok;
      _ ->
  
negative.code:         State#rep_state{retries_left = Left - 1, wait = Wait2}
      end.
  
  
  before_doc_update(#doc{id = <<?DESIGN_DOC_PREFIX, _/binary>>} = Doc, _Db) ->
      Doc;
  before_doc_update(#doc{body = {Body}} = Doc, #db{user_ctx=UserCtx} = Db) ->
      #user_ctx{roles = Roles, name = Name} = UserCtx,
      case lists:member(<<"_replicator">>, Roles) of
      true ->
          Doc;
      false ->
          case couch_util:get_value(?OWNER, Body) of
          undefined ->
              Doc#doc{body = {?replace(Body, ?OWNER, Name)}};
          Name ->
              Doc;
          Other ->
              case (catch couch_db:check_is_admin(Db)) of
              ok when Other =:= null ->
                  Doc#doc{body = {?replace(Body, ?OWNER, Name)}};
              ok ->
                  Doc;
              _ ->
                  throw({forbidden, <<"Can't update replication documents",
                      " from other users.">>})
              end
          end
      end.
  
  
  after_doc_read(#doc{id = <<?DESIGN_DOC_PREFIX, _/binary>>} = Doc, _Db) ->
      Doc;
  after_doc_read(#doc{body = {Body}} = Doc, #db{user_ctx=UserCtx} = Db) ->
      #user_ctx{name = Name} = UserCtx,
      case (catch couch_db:check_is_admin(Db)) of
      ok ->
          Doc;
      _ ->
          case couch_util:get_value(?OWNER, Body) of
          Name ->
              Doc;
          _Other ->
              Source = strip_credentials(couch_util:get_value(<<"source">>,
  Body)),
              Target = strip_credentials(couch_util:get_value(<<"target">>,
  Body)),
              NewBody0 = ?replace(Body, <<"source">>, Source),
              NewBody = ?replace(NewBody0, <<"target">>, Target),
              #doc{revs = {Pos, [_ | Revs]}} = Doc,
              NewDoc = Doc#doc{body = {NewBody}, revs = {Pos - 1, Revs}},
              NewRevId = couch_db:new_revid(NewDoc),
              NewDoc#doc{revs = {Pos, [NewRevId | Revs]}}
          end
      end.
  
  strip_credentials(Url) when is_binary(Url) ->
      re:replace(Url,
          "http(s)?://(?:[^:]+):[^@]+@(.*)$",
          "http\\1://\\2",
          [{return, binary}]);
  strip_credentials({Props}) ->
      {lists:keydelete(<<"oauth">>, 1, Props)}.
  
  scan_all_dbs(Server) when is_pid(Server) ->
      {ok, Db} = mem3_util:ensure_exists(config:get("mem3", "shard_db", "dbs")),
      ChangesFun = couch_changes:handle_changes(#changes_args{}, nil, Db),
      IsReplicatorDbFun = is_replicator_db_fun(),
      ChangesFun(fun({change, {Change}, _}, _) ->
          DbName = get_json_value(<<"id">>, Change),
          case DbName of <<"_design/", _/binary>> -> ok; _Else ->
              case couch_replicator_utils:is_deleted(Change) of
              true ->
                  ok;
              false ->
                  case IsReplicatorDbFun(DbName) of
                      true ->
                          ensure_rep_ddoc_exists(DbName),
                          gen_server:call(Server, {resume_scan, DbName});
                      false ->
                          ok
                  end
              end
          end;
          (_, _) -> ok
  