anchor.diff:  -define(REP_ID_TO_DOC_ID_MAP, rep_id_to_rep_doc_id).
   
   -record(state, {
  -    changes_feed_loop,
  -    changes_queue,
  -    changes_processor
  +    changes_feed_loop = nil,
  +    changes_queue = nil,
  +    changes_processor = nil,
  +    db_notifier = nil
   }).
   
   
  
positive.code:     {ok, Pid}.
  
  
  changes_processor(ChangesQueue) ->
      Pid = spawn_link(
          fun() ->
  
negative.code:     Server = self(),
      ok = couch_config:register(
          fun("replicator", "db") ->
              ok = gen_server:cast(Server, rep_db_changed)
          end
      ),
      {ok, #state{
          changes_feed_loop = Loop,
          changes_queue = Queue,
          changes_processor = Processor,
          db_notifier = db_update_notifier()}
      }.
  
  
  handle_call(Msg, From, State) ->
      ?LOG_ERROR("Replicator DB listener receive unexpected call ~p from ~p",
          [Msg, From]),
      {stop, {error, {unexpected_call, Msg}}, State}.
  
  
  handle_cast(rep_db_changed, State) ->
      #state{
          changes_feed_loop = Loop,
          changes_queue = Queue
      } = State,
      exit(Loop, rep_db_changed),
      couch_work_queue:queue(Queue, stop_all_replications),
      {ok, NewLoop} = changes_feed_loop(Queue),
      {noreply, State#state{changes_feed_loop = NewLoop}};
  
  handle_cast(rep_db_created, #state{changes_feed_loop = nil} = State) ->
      {ok, NewLoop} = changes_feed_loop(State#state.changes_queue),
      {noreply, State#state{changes_feed_loop = NewLoop}};
  
  handle_cast(Msg, State) ->
      ?LOG_ERROR("Replicator DB listener receive unexpected cast ~p", [Msg]),
      {stop, {error, {unexpected_cast, Msg}}, State}.
  
  
  handle_info({'EXIT', From, normal}, #state{changes_feed_loop = From} = State) ->
      % replicator DB deleted
      couch_work_queue:queue(State#state.changes_queue, stop_all_replications),
      {noreply, State#state{changes_feed_loop = nil}};
  
  handle_info({'EXIT', From, Reason}, #state{db_notifier = From} = State) ->
      ?LOG_ERROR("Database update notifier died. Reason: ~p", [Reason]),
      {stop, {db_update_notifier_died, Reason}, State};
  
  handle_info({'EXIT', _OldChangesLoop, rep_db_changed}, State) ->
      {noreply, State};
  
  handle_info({'EXIT', From, Reason}, #state{changes_processor = From} = State) ->
      ?LOG_ERROR("Replicator DB changes processor died. Reason: ~p", [Reason]),
      {stop, {rep_db_changes_processor_died, Reason}, State}.
  
  
  terminate(_Reason, State) ->
  