commit 5b0b874435146b7b0aae2ee1372a7823bfc0949e
parent aa1fc01b310b2d4880e841f0eea45c23b42e1c7d
Author: Dan Stillman <dstillman@zotero.org>
Date: Wed, 3 May 2017 03:43:59 -0400
Purge old objects in sync cache after upload
Diffstat:
3 files changed, 25 insertions(+), 1 deletion(-)
diff --git a/chrome/content/zotero/xpcom/sync/syncEngine.js b/chrome/content/zotero/xpcom/sync/syncEngine.js
@@ -204,7 +204,7 @@ Zotero.Sync.Data.Engine.prototype.stop = function () {
/**
- * Download updated objects from API and save to local cache
+ * Download updated objects from API and save to DB
*
* @return {Promise<Integer>} - A download result code (this.DOWNLOAD_RESULT_*)
*/
@@ -1107,6 +1107,11 @@ Zotero.Sync.Data.Engine.prototype._uploadObjects = Zotero.Promise.coroutine(func
objectsClass.updateSynced(updateSyncedIDs, true);
}.bind(this));
+ // Purge older objects in sync cache
+ if (toSave.length) {
+ yield Zotero.Sync.Data.Local.purgeCache(objectType, this.libraryID);
+ }
+
// Handle failed objects
for (let index in results.failed) {
let { code, message, data } = results.failed[index];
diff --git a/chrome/content/zotero/xpcom/sync/syncLocal.js b/chrome/content/zotero/xpcom/sync/syncLocal.js
@@ -1104,6 +1104,21 @@ Zotero.Sync.Data.Local = {
},
+ /**
+ * Delete entries from sync cache that don't exist or are less than the current object version
+ */
+ purgeCache: Zotero.Promise.coroutine(function* (objectType, libraryID) {
+ var syncObjectTypeID = Zotero.Sync.Data.Utilities.getSyncObjectTypeID(objectType);
+ var table = Zotero.DataObjectUtilities.getObjectsClassForObjectType(objectType).table;
+ var sql = "DELETE FROM syncCache WHERE ROWID IN ("
+ + "SELECT SC.ROWID FROM syncCache SC "
+ + `LEFT JOIN ${table} O USING (libraryID, key, version) `
+ + "WHERE syncObjectTypeID=? AND SC.libraryID=? AND "
+ + "(O.libraryID IS NULL OR SC.version < O.version)";
+ yield Zotero.DB.queryAsync(sql, [syncObjectTypeID, libraryID]);
+ }),
+
+
processConflicts: Zotero.Promise.coroutine(function* (objectType, libraryID, conflicts, options = {}) {
if (!conflicts.length) return [];
diff --git a/test/tests/syncEngineTest.js b/test/tests/syncEngineTest.js
@@ -682,6 +682,10 @@ describe("Zotero.Sync.Data.Engine", function () {
assert.isArray(cached.data.conditions);
break;
}
+
+ // Make sure older versions have been removed from the cache
+ let versions = yield Zotero.Sync.Data.Local.getCacheObjectVersions(type, libraryID, key);
+ assert.sameMembers(versions, [version]);
}
})