Purge old objects in sync cache after upload

This commit is contained in:
Dan Stillman 2017-05-03 03:43:59 -04:00
parent aa1fc01b31
commit 5b0b874435
3 changed files with 25 additions and 1 deletions

View file

@ -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];

View file

@ -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 [];

View file

@ -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]);
}
})