mobile/android/base/sync/repositories/StoreTrackingRepositorySession.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/sync/repositories/StoreTrackingRepositorySession.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,102 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +package org.mozilla.gecko.sync.repositories;
     1.9 +
    1.10 +import java.util.Collection;
    1.11 +import java.util.Iterator;
    1.12 +
    1.13 +import org.mozilla.gecko.background.common.log.Logger;
    1.14 +import org.mozilla.gecko.sync.repositories.delegates.RepositorySessionBeginDelegate;
    1.15 +import org.mozilla.gecko.sync.repositories.delegates.RepositorySessionFinishDelegate;
    1.16 +import org.mozilla.gecko.sync.repositories.domain.Record;
    1.17 +
    1.18 +public abstract class StoreTrackingRepositorySession extends RepositorySession {
    1.19 +  private static final String LOG_TAG = "StoreTrackSession";
    1.20 +  protected StoreTracker storeTracker;
    1.21 +
    1.22 +  protected static StoreTracker createStoreTracker() {
    1.23 +    return new HashSetStoreTracker();
    1.24 +  }
    1.25 +
    1.26 +  public StoreTrackingRepositorySession(Repository repository) {
    1.27 +    super(repository);
    1.28 +  }
    1.29 +
    1.30 +  @Override
    1.31 +  public void begin(RepositorySessionBeginDelegate delegate) throws InvalidSessionTransitionException {
    1.32 +    RepositorySessionBeginDelegate deferredDelegate = delegate.deferredBeginDelegate(delegateQueue);
    1.33 +    try {
    1.34 +      super.sharedBegin();
    1.35 +    } catch (InvalidSessionTransitionException e) {
    1.36 +      deferredDelegate.onBeginFailed(e);
    1.37 +      return;
    1.38 +    }
    1.39 +    // Or do this in your own subclass.
    1.40 +    storeTracker = createStoreTracker();
    1.41 +    deferredDelegate.onBeginSucceeded(this);
    1.42 +  }
    1.43 +
    1.44 +  @Override
    1.45 +  protected synchronized void trackGUID(String guid) {
    1.46 +    if (this.storeTracker == null) {
    1.47 +      throw new IllegalStateException("Store tracker not yet initialized!");
    1.48 +    }
    1.49 +    this.storeTracker.trackRecordForExclusion(guid);
    1.50 +  }
    1.51 +
    1.52 +  @Override
    1.53 +  protected synchronized void untrackGUID(String guid) {
    1.54 +    if (this.storeTracker == null) {
    1.55 +      throw new IllegalStateException("Store tracker not yet initialized!");
    1.56 +    }
    1.57 +    this.storeTracker.untrackStoredForExclusion(guid);
    1.58 +  }
    1.59 +
    1.60 +  @Override
    1.61 +  protected synchronized void untrackGUIDs(Collection<String> guids) {
    1.62 +    if (this.storeTracker == null) {
    1.63 +      throw new IllegalStateException("Store tracker not yet initialized!");
    1.64 +    }
    1.65 +    if (guids == null) {
    1.66 +      return;
    1.67 +    }
    1.68 +    for (String guid : guids) {
    1.69 +      this.storeTracker.untrackStoredForExclusion(guid);
    1.70 +    }
    1.71 +  }
    1.72 +
    1.73 +  protected void trackRecord(Record record) {
    1.74 +
    1.75 +    Logger.debug(LOG_TAG, "Tracking record " + record.guid +
    1.76 +                           " (" + record.lastModified + ") to avoid re-upload.");
    1.77 +    // Future: we care about the timestamp…
    1.78 +    trackGUID(record.guid);
    1.79 +  }
    1.80 +
    1.81 +  protected void untrackRecord(Record record) {
    1.82 +    Logger.debug(LOG_TAG, "Un-tracking record " + record.guid + ".");
    1.83 +    untrackGUID(record.guid);
    1.84 +  }
    1.85 +
    1.86 +  @Override
    1.87 +  public Iterator<String> getTrackedRecordIDs() {
    1.88 +    if (this.storeTracker == null) {
    1.89 +      throw new IllegalStateException("Store tracker not yet initialized!");
    1.90 +    }
    1.91 +    return this.storeTracker.recordsTrackedForExclusion();
    1.92 +  }
    1.93 +
    1.94 +  @Override
    1.95 +  public void abort(RepositorySessionFinishDelegate delegate) {
    1.96 +    this.storeTracker = null;
    1.97 +    super.abort(delegate);
    1.98 +  }
    1.99 +
   1.100 +  @Override
   1.101 +  public void finish(RepositorySessionFinishDelegate delegate) throws InactiveSessionException {
   1.102 +    super.finish(delegate);
   1.103 +    this.storeTracker = null;
   1.104 +  }
   1.105 +}

mercurial