diff --git a/platform/persistence/cache/src/CachingPersistenceDecorator.js b/platform/persistence/cache/src/CachingPersistenceDecorator.js index 92e5c8b885..f495c03ca9 100644 --- a/platform/persistence/cache/src/CachingPersistenceDecorator.js +++ b/platform/persistence/cache/src/CachingPersistenceDecorator.js @@ -116,9 +116,9 @@ define( * @returns {Promise.} an indicator of the success or * failure of this request */ - createObject: function (space, key, value, options) { + createObject: function (space, key, value) { addToCache(space, key, value); - return persistenceService.createObject(space, key, value, options); + return persistenceService.createObject(space, key, value); }, /** * Read an object from a specific space. This will read from a @@ -126,21 +126,14 @@ define( * @memberof CachingPersistenceDecorator# * @param {string} space the space in which to create the object * @param {string} key the key which identifies the object - * @param {*} options optional parameters * @returns {Promise.} a promise for the object; may * resolve to undefined (if the object does not exist * in this space) */ - readObject: function (space, key, options) { - // Ignore cache upon request - if ((options || {}).cache === false) { - return persistenceService.readObject(space, key, options); - } - // Otherwise, use the cache if it's there (and put new - // values into the cache, as well.) + readObject: function (space, key) { return (cache[space] && cache[space][key]) ? fastPromise(cache[space][key].value) : - persistenceService.readObject(space, key, options) + persistenceService.readObject(space, key) .then(putCache(space, key)); }, /** @@ -154,8 +147,8 @@ define( * @returns {Promise.} an indicator of the success or * failure of this request */ - updateObject: function (space, key, value, options) { - return persistenceService.updateObject(space, key, value, options) + updateObject: function (space, key, value) { + return persistenceService.updateObject(space, key, value) .then(function (result) { addToCache(space, key, value); return result; @@ -172,7 +165,7 @@ define( * @returns {Promise.} an indicator of the success or * failure of this request */ - deleteObject: function (space, key, value, options) { + deleteObject: function (space, key, value) { if (cache[space]) { delete cache[space][key]; } diff --git a/platform/persistence/elastic/src/ElasticPersistenceProvider.js b/platform/persistence/elastic/src/ElasticPersistenceProvider.js index ec6b32d80a..9c4ab35fc1 100644 --- a/platform/persistence/elastic/src/ElasticPersistenceProvider.js +++ b/platform/persistence/elastic/src/ElasticPersistenceProvider.js @@ -155,12 +155,11 @@ define( * of the success (true) or failure (false) of this * operation */ - updateObject: function (space, key, value, options) { - var check = (options || {}).check; + updateObject: function (space, key, value) { function checkUpdate(response) { return checkResponse(response, key); } - return put(key, value, check && { version: revs[key] }) + return put(key, value, { version: revs[key] }) .then(checkUpdate); }, /** diff --git a/platform/persistence/queue/src/PersistenceFailureHandler.js b/platform/persistence/queue/src/PersistenceFailureHandler.js index fc492e0b17..449c1757d2 100644 --- a/platform/persistence/queue/src/PersistenceFailureHandler.js +++ b/platform/persistence/queue/src/PersistenceFailureHandler.js @@ -7,16 +7,10 @@ define( function PersistenceFailureHandler($q, dialogService, persistenceService) { // Refresh revision information for the domain object associated - // with htis persistence failure + // with this persistence failure function refresh(failure) { - // Perform a new read; this should update the persistence - // service's local revision records, so that the next request - // should permit the overwrite - return persistenceService.readObject( - failure.persistence.getSpace(), - failure.id, - { cache: false } // Disallow caching - ); + // Refresh the domain object to the latest from persistence + return failure.persistence.refresh(); } // Issue a new persist call for the domain object associated with @@ -25,15 +19,42 @@ define( var decoratedPersistence = failure.domainObject.getCapability('persistence'); return decoratedPersistence && - decoratedPersistence.persist(true); + decoratedPersistence.persist(); } - // Retry persistence for this set of failed attempts + // Retry persistence (overwrite) for this set of failed attempts function retry(failures) { - // Refresh all objects within the persistenceService to - // get up-to-date revision information; once complete, - // reissue the persistence request. + var models = {}; + + // Cache a copy of the model + function cacheModel(failure) { + // Clone... + models[failure.id] = JSON.parse(JSON.stringify( + failure.domainObject.getModel() + )); + } + + // Mutate a domain object to restore its model + function remutate(failure) { + var model = models[failure.id]; + return failure.domainObject.useCapability( + "mutation", + function () { return model; }, + model.modified + ); + } + + // Cache the object models we might want to save + failures.forEach(cacheModel); + + // Strategy here: + // * Cache all of the models we might want to save (above) + // * Refresh all domain objects (so they are latest versions) + // * Re-insert the cached domain object models + // * Invoke persistence again return $q.all(failures.map(refresh)).then(function () { + return $q.all(failures.map(remutate)); + }).then(function () { return $q.all(failures.map(persist)); }); }