[Persistence] Refactor model loading

...such that ids are only parsed for space/key pairs once.
This commit is contained in:
Victor Woeltjen 2015-11-05 17:11:24 -08:00
parent 9f383ab101
commit c8cfbf5281

View File

@ -54,14 +54,13 @@ define(
var persistenceService = this.persistenceService, var persistenceService = this.persistenceService,
$q = this.$q, $q = this.$q,
now = this.now, now = this.now,
defaultSpace = this.defaultSpace; defaultSpace = this.defaultSpace,
parsedIds;
// Load a single object model from any persistence spaces // Load a single object model from any persistence spaces
function loadModel(id) { function loadModel(parsedId) {
var parts = id.split(":"), return persistenceService
space = parts.length > 1 ? parts[0] : defaultSpace, .readObject(parsedId.space, parsedId.key);
key = parts.length > 1 ? parts[1] : id;
return persistenceService.readObject(space, key);
} }
// Ensure that models read from persistence have some // Ensure that models read from persistence have some
@ -86,26 +85,32 @@ define(
return result; return result;
} }
// Filter down to o "namespaced" identifiers; these are function loadModels(parsedIds) {
// not expected to be found in database. See WTD-659. return $q.all(parsedIds.map(loadModel))
ids = ids.filter(function (id) { .then(function (models) {
return id.indexOf(":") !== -1; return packageResult(
parsedIds,
models.map(addPersistedTimestamp)
);
});
}
function restrictToSpaces(spaces) {
return parsedIds.filter(function (parsedId) {
return spaces.indexOf(parsedId.space) !== -1;
});
}
parsedIds = ids.map(function (id) {
var parts = id.split(":");
return (parts.length > 1) ?
{ space: parts[0], key: parts[1] } :
{ space: defaultSpace, key: id };
}); });
return persistenceService.listSpaces().then(function (spaces) { return persistenceService.listSpaces()
return ids.filter(function (id) { .then(restrictToSpaces)
var parts = id.split(":"); .then(loadModels);
return parts.length === 1 ||
spaces.indexOf(parts[0]) !== -1;
});
}).then(function (ids) {
return $q.all(ids.map(loadModel)).then(function (models) {
return packageResult(
ids,
models.map(addPersistedTimestamp)
);
});
});
}; };
return PersistedModelProvider; return PersistedModelProvider;