|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 package org.mozilla.gecko.sync.repositories; |
|
6 |
|
7 import java.util.Collection; |
|
8 import java.util.Iterator; |
|
9 |
|
10 import org.mozilla.gecko.background.common.log.Logger; |
|
11 import org.mozilla.gecko.sync.repositories.delegates.RepositorySessionBeginDelegate; |
|
12 import org.mozilla.gecko.sync.repositories.delegates.RepositorySessionFinishDelegate; |
|
13 import org.mozilla.gecko.sync.repositories.domain.Record; |
|
14 |
|
15 public abstract class StoreTrackingRepositorySession extends RepositorySession { |
|
16 private static final String LOG_TAG = "StoreTrackSession"; |
|
17 protected StoreTracker storeTracker; |
|
18 |
|
19 protected static StoreTracker createStoreTracker() { |
|
20 return new HashSetStoreTracker(); |
|
21 } |
|
22 |
|
23 public StoreTrackingRepositorySession(Repository repository) { |
|
24 super(repository); |
|
25 } |
|
26 |
|
27 @Override |
|
28 public void begin(RepositorySessionBeginDelegate delegate) throws InvalidSessionTransitionException { |
|
29 RepositorySessionBeginDelegate deferredDelegate = delegate.deferredBeginDelegate(delegateQueue); |
|
30 try { |
|
31 super.sharedBegin(); |
|
32 } catch (InvalidSessionTransitionException e) { |
|
33 deferredDelegate.onBeginFailed(e); |
|
34 return; |
|
35 } |
|
36 // Or do this in your own subclass. |
|
37 storeTracker = createStoreTracker(); |
|
38 deferredDelegate.onBeginSucceeded(this); |
|
39 } |
|
40 |
|
41 @Override |
|
42 protected synchronized void trackGUID(String guid) { |
|
43 if (this.storeTracker == null) { |
|
44 throw new IllegalStateException("Store tracker not yet initialized!"); |
|
45 } |
|
46 this.storeTracker.trackRecordForExclusion(guid); |
|
47 } |
|
48 |
|
49 @Override |
|
50 protected synchronized void untrackGUID(String guid) { |
|
51 if (this.storeTracker == null) { |
|
52 throw new IllegalStateException("Store tracker not yet initialized!"); |
|
53 } |
|
54 this.storeTracker.untrackStoredForExclusion(guid); |
|
55 } |
|
56 |
|
57 @Override |
|
58 protected synchronized void untrackGUIDs(Collection<String> guids) { |
|
59 if (this.storeTracker == null) { |
|
60 throw new IllegalStateException("Store tracker not yet initialized!"); |
|
61 } |
|
62 if (guids == null) { |
|
63 return; |
|
64 } |
|
65 for (String guid : guids) { |
|
66 this.storeTracker.untrackStoredForExclusion(guid); |
|
67 } |
|
68 } |
|
69 |
|
70 protected void trackRecord(Record record) { |
|
71 |
|
72 Logger.debug(LOG_TAG, "Tracking record " + record.guid + |
|
73 " (" + record.lastModified + ") to avoid re-upload."); |
|
74 // Future: we care about the timestamp… |
|
75 trackGUID(record.guid); |
|
76 } |
|
77 |
|
78 protected void untrackRecord(Record record) { |
|
79 Logger.debug(LOG_TAG, "Un-tracking record " + record.guid + "."); |
|
80 untrackGUID(record.guid); |
|
81 } |
|
82 |
|
83 @Override |
|
84 public Iterator<String> getTrackedRecordIDs() { |
|
85 if (this.storeTracker == null) { |
|
86 throw new IllegalStateException("Store tracker not yet initialized!"); |
|
87 } |
|
88 return this.storeTracker.recordsTrackedForExclusion(); |
|
89 } |
|
90 |
|
91 @Override |
|
92 public void abort(RepositorySessionFinishDelegate delegate) { |
|
93 this.storeTracker = null; |
|
94 super.abort(delegate); |
|
95 } |
|
96 |
|
97 @Override |
|
98 public void finish(RepositorySessionFinishDelegate delegate) throws InactiveSessionException { |
|
99 super.finish(delegate); |
|
100 this.storeTracker = null; |
|
101 } |
|
102 } |