/** - (void) beginReplicating in CBL_Replicator.m */
  @Override
  @InterfaceAudience.Private
  public void beginReplicating() {
    // If we're still waiting to create the remote db, do nothing now. (This method will be
    // re-invoked after that request finishes; see -maybeCreateRemoteDB above.)

    Log.d(Log.TAG_SYNC, "%s: beginReplicating() called", this);

    // If we're still waiting to create the remote db, do nothing now. (This method will be
    // re-invoked after that request finishes; see maybeCreateRemoteDB() above.)
    if (creatingTarget) {
      Log.d(Log.TAG_SYNC, "%s: creatingTarget == true, doing nothing", this);
      return;
    }

    pendingSequences = Collections.synchronizedSortedSet(new TreeSet<Long>());
    try {
      maxPendingSequence = Long.parseLong(lastSequence);
    } catch (NumberFormatException e) {
      Log.w(Log.TAG_SYNC, "Error converting lastSequence: %s to long.  Using 0", lastSequence);
      maxPendingSequence = new Long(0);
    }

    filter = compilePushReplicationFilter();
    if (filterName != null && filter == null) {
      Log.w(
          Log.TAG_SYNC,
          "%s: No ReplicationFilter registered for filter '%s'; ignoring",
          this,
          filterName);
    }

    // Process existing changes since the last push:
    long lastSequenceLong = 0;
    if (lastSequence != null) {
      lastSequenceLong = Long.parseLong(lastSequence);
    }
    ChangesOptions options = new ChangesOptions();
    options.setIncludeConflicts(true);
    Log.d(Log.TAG_SYNC, "%s: Getting changes since %s", this, lastSequence);
    RevisionList changes = db.changesSince(lastSequenceLong, options, filter, filterParams);
    if (changes.size() > 0) {
      Log.d(Log.TAG_SYNC, "%s: Queuing %d changes since %s", this, changes.size(), lastSequence);
      int remaining = changes.size();
      int size = batcher.getCapacity();
      int start = 0;
      while (remaining > 0) {
        if (size > remaining) size = remaining;
        RevisionList subChanges = new RevisionList(changes.subList(start, start + size));
        batcher.queueObjects(subChanges);
        start += size;
        remaining -= size;
        pauseOrResume();
        waitIfPaused();
      }
    } else {
      Log.d(Log.TAG_SYNC, "%s: No changes since %s", this, lastSequence);
    }

    // Now listen for future changes (in continuous mode):
    if (isContinuous()) {
      observing = true;
      db.addChangeListener(this);
    }
  }