1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.gecko.sync.stage;
import java.net.URISyntaxException;
import org.mozilla.gecko.background.common.log.Logger;
import org.mozilla.gecko.sync.InfoCollections;
import org.mozilla.gecko.sync.InfoConfiguration;
import org.mozilla.gecko.sync.InfoCounts;
import org.mozilla.gecko.sync.JSONRecordFetcher;
import org.mozilla.gecko.sync.net.AuthHeaderProvider;
import org.mozilla.gecko.sync.repositories.ConstrainedServer11Repository;
import org.mozilla.gecko.sync.repositories.Repository;
import org.mozilla.gecko.sync.repositories.Server11RepositorySession;
import org.mozilla.gecko.sync.repositories.delegates.RepositorySessionCreationDelegate;
import android.content.Context;
/**
* This is a constrained repository -- one which fetches a limited number
* of records -- that additionally refuses to sync if the limit will
* be exceeded on a first sync by the number of records on the server.
*
* You must pass an {@link InfoCounts} instance, which will be interrogated
* in the event of a first sync.
*
* "First sync" means that our sync timestamp is not greater than zero.
*/
public class SafeConstrainedServer11Repository extends ConstrainedServer11Repository {
// This can be lazily evaluated if we need it.
private final JSONRecordFetcher countFetcher;
public SafeConstrainedServer11Repository(String collection,
String storageURL,
AuthHeaderProvider authHeaderProvider,
InfoCollections infoCollections,
InfoConfiguration infoConfiguration,
long batchLimit,
long totalLimit,
String sort,
JSONRecordFetcher countFetcher)
throws URISyntaxException {
super(collection, storageURL, authHeaderProvider, infoCollections, infoConfiguration,
batchLimit, totalLimit, sort);
if (countFetcher == null) {
throw new IllegalArgumentException("countFetcher must not be null");
}
this.countFetcher = countFetcher;
}
@Override
public void createSession(RepositorySessionCreationDelegate delegate,
Context context) {
delegate.onSessionCreated(new CountCheckingServer11RepositorySession(this, this.getDefaultBatchLimit()));
}
public class CountCheckingServer11RepositorySession extends Server11RepositorySession {
private static final String LOG_TAG = "CountCheckingServer11RepositorySession";
/**
* The session will report no data available if this is a first sync
* and the server has more data available than this limit.
*/
private final long fetchLimit;
public CountCheckingServer11RepositorySession(Repository repository, long fetchLimit) {
super(repository);
this.fetchLimit = fetchLimit;
}
@Override
public boolean shouldSkip() {
// If this is a first sync, verify that we aren't going to blow through our limit.
final long lastSyncTimestamp = getLastSyncTimestamp();
if (lastSyncTimestamp > 0) {
Logger.info(LOG_TAG, "Collection " + collection + " has already had a first sync: " +
"timestamp is " + lastSyncTimestamp + "; " +
"ignoring any updated counts and syncing as usual.");
} else {
Logger.info(LOG_TAG, "Collection " + collection + " is starting a first sync; checking counts.");
final InfoCounts counts;
try {
// This'll probably be the same object, but best to obey the API.
counts = new InfoCounts(countFetcher.fetchBlocking());
} catch (Exception e) {
Logger.warn(LOG_TAG, "Skipping " + collection + " until we can fetch counts.", e);
return true;
}
Integer c = counts.getCount(collection);
if (c == null) {
Logger.info(LOG_TAG, "Fetched counts does not include collection " + collection + "; syncing as usual.");
return false;
}
Logger.info(LOG_TAG, "First sync for " + collection + ": " + c + " items.");
if (c > fetchLimit) {
Logger.warn(LOG_TAG, "Too many items to sync safely. Skipping.");
return true;
}
}
return super.shouldSkip();
}
}
}
|