Adds optimization for related relations (#4345)

* Adds optimization for related relations

* Makes MongoStorageAdapter only able to sort on Join tables
This commit is contained in:
Florent Vilmart
2017-11-14 14:46:51 -05:00
committed by GitHub
parent 7223add446
commit 09fee7d12b
3 changed files with 47 additions and 9 deletions

View File

@@ -163,6 +163,33 @@ describe('Parse.Relation testing', () => {
}); });
}); });
it("related at ordering optimizations", (done) => {
var ChildObject = Parse.Object.extend("ChildObject");
var childObjects = [];
for (var i = 0; i < 10; i++) {
childObjects.push(new ChildObject({x: i}));
}
var parent;
var relation;
Parse.Object.saveAll(childObjects).then(function() {
var ParentObject = Parse.Object.extend('ParentObject');
parent = new ParentObject();
parent.set('x', 4);
relation = parent.relation('child');
relation.add(childObjects);
return parent.save();
}).then(function() {
const query = relation.query();
query.descending('createdAt');
query.skip(1);
query.limit(3);
return query.find();
}).then(function(list) {
expect(list.length).toBe(3);
}).then(done, done.fail);
});
it_exclude_dbs(['postgres'])("queries with relations", (done) => { it_exclude_dbs(['postgres'])("queries with relations", (done) => {

View File

@@ -86,7 +86,7 @@ export class MongoStorageAdapter {
// Public // Public
connectionPromise; connectionPromise;
database; database;
canSortOnJoinTables;
constructor({ constructor({
uri = defaults.DefaultMongoURI, uri = defaults.DefaultMongoURI,
collectionPrefix = '', collectionPrefix = '',
@@ -98,6 +98,7 @@ export class MongoStorageAdapter {
// MaxTimeMS is not a global MongoDB client option, it is applied per operation. // MaxTimeMS is not a global MongoDB client option, it is applied per operation.
this._maxTimeMS = mongoOptions.maxTimeMS; this._maxTimeMS = mongoOptions.maxTimeMS;
this.canSortOnJoinTables = true;
delete mongoOptions.maxTimeMS; delete mongoOptions.maxTimeMS;
} }

View File

@@ -599,8 +599,16 @@ DatabaseController.prototype.deleteEverything = function() {
// Returns a promise for a list of related ids given an owning id. // Returns a promise for a list of related ids given an owning id.
// className here is the owning className. // className here is the owning className.
DatabaseController.prototype.relatedIds = function(className, key, owningId) { DatabaseController.prototype.relatedIds = function(className, key, owningId, queryOptions) {
return this.adapter.find(joinTableName(className, key), relationSchema, { owningId }, {}) const { skip, limit, sort } = queryOptions;
const findOptions = {};
if (sort && sort.createdAt && this.adapter.canSortOnJoinTables) {
findOptions.sort = { '_id' : sort.createdAt };
findOptions.limit = limit;
findOptions.skip = skip;
queryOptions.skip = 0;
}
return this.adapter.find(joinTableName(className, key), relationSchema, { owningId }, findOptions)
.then(results => results.map(result => result.relatedId)); .then(results => results.map(result => result.relatedId));
}; };
@@ -693,11 +701,11 @@ DatabaseController.prototype.reduceInRelation = function(className, query, schem
// Modifies query so that it no longer has $relatedTo // Modifies query so that it no longer has $relatedTo
// Returns a promise that resolves when query is mutated // Returns a promise that resolves when query is mutated
DatabaseController.prototype.reduceRelationKeys = function(className, query) { DatabaseController.prototype.reduceRelationKeys = function(className, query, queryOptions) {
if (query['$or']) { if (query['$or']) {
return Promise.all(query['$or'].map((aQuery) => { return Promise.all(query['$or'].map((aQuery) => {
return this.reduceRelationKeys(className, aQuery); return this.reduceRelationKeys(className, aQuery, queryOptions);
})); }));
} }
@@ -706,11 +714,12 @@ DatabaseController.prototype.reduceRelationKeys = function(className, query) {
return this.relatedIds( return this.relatedIds(
relatedTo.object.className, relatedTo.object.className,
relatedTo.key, relatedTo.key,
relatedTo.object.objectId) relatedTo.object.objectId,
queryOptions)
.then((ids) => { .then((ids) => {
delete query['$relatedTo']; delete query['$relatedTo'];
this.addInObjectIdsIds(ids, query); this.addInObjectIdsIds(ids, query);
return this.reduceRelationKeys(className, query); return this.reduceRelationKeys(className, query, queryOptions);
}); });
} }
}; };
@@ -831,8 +840,9 @@ DatabaseController.prototype.find = function(className, query, {
throw new Parse.Error(Parse.Error.INVALID_KEY_NAME, `Invalid field name: ${fieldName}.`); throw new Parse.Error(Parse.Error.INVALID_KEY_NAME, `Invalid field name: ${fieldName}.`);
} }
}); });
const queryOptions = { skip, limit, sort, keys, readPreference };
return (isMaster ? Promise.resolve() : schemaController.validatePermission(className, aclGroup, op)) return (isMaster ? Promise.resolve() : schemaController.validatePermission(className, aclGroup, op))
.then(() => this.reduceRelationKeys(className, query)) .then(() => this.reduceRelationKeys(className, query, queryOptions))
.then(() => this.reduceInRelation(className, query, schemaController)) .then(() => this.reduceInRelation(className, query, schemaController))
.then(() => { .then(() => {
if (!isMaster) { if (!isMaster) {
@@ -871,7 +881,7 @@ DatabaseController.prototype.find = function(className, query, {
if (!classExists) { if (!classExists) {
return []; return [];
} else { } else {
return this.adapter.find(className, schema, query, { skip, limit, sort, keys, readPreference }) return this.adapter.find(className, schema, query, queryOptions)
.then(objects => objects.map(object => { .then(objects => objects.map(object => {
object = untransformObjectACL(object); object = untransformObjectACL(object);
return filterSensitiveData(isMaster, aclGroup, className, object) return filterSensitiveData(isMaster, aclGroup, className, object)