Skip to content

Commit

Permalink
Adds optimization for related relations (#4345)
Browse files Browse the repository at this point in the history
* Adds optimization for related relations

* Makes MongoStorageAdapter only able to sort on Join tables
  • Loading branch information
flovilmart authored Nov 14, 2017
1 parent 7223add commit 09fee7d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 9 deletions.
27 changes: 27 additions & 0 deletions spec/ParseRelation.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {

Expand Down
3 changes: 2 additions & 1 deletion src/Adapters/Storage/Mongo/MongoStorageAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export class MongoStorageAdapter {
// Public
connectionPromise;
database;

canSortOnJoinTables;
constructor({
uri = defaults.DefaultMongoURI,
collectionPrefix = '',
Expand All @@ -98,6 +98,7 @@ export class MongoStorageAdapter {

// MaxTimeMS is not a global MongoDB client option, it is applied per operation.
this._maxTimeMS = mongoOptions.maxTimeMS;
this.canSortOnJoinTables = true;
delete mongoOptions.maxTimeMS;
}

Expand Down
26 changes: 18 additions & 8 deletions src/Controllers/DatabaseController.js
Original file line number Diff line number Diff line change
Expand Up @@ -599,8 +599,16 @@ DatabaseController.prototype.deleteEverything = function() {

// Returns a promise for a list of related ids given an owning id.
// className here is the owning className.
DatabaseController.prototype.relatedIds = function(className, key, owningId) {
return this.adapter.find(joinTableName(className, key), relationSchema, { owningId }, {})
DatabaseController.prototype.relatedIds = function(className, key, owningId, queryOptions) {
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));
};

Expand Down Expand Up @@ -693,11 +701,11 @@ DatabaseController.prototype.reduceInRelation = function(className, query, schem

// Modifies query so that it no longer has $relatedTo
// 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']) {
return Promise.all(query['$or'].map((aQuery) => {
return this.reduceRelationKeys(className, aQuery);
return this.reduceRelationKeys(className, aQuery, queryOptions);
}));
}

Expand All @@ -706,11 +714,12 @@ DatabaseController.prototype.reduceRelationKeys = function(className, query) {
return this.relatedIds(
relatedTo.object.className,
relatedTo.key,
relatedTo.object.objectId)
relatedTo.object.objectId,
queryOptions)
.then((ids) => {
delete query['$relatedTo'];
this.addInObjectIdsIds(ids, query);
return this.reduceRelationKeys(className, query);
return this.reduceRelationKeys(className, query, queryOptions);
});
}
};
Expand Down Expand Up @@ -831,8 +840,9 @@ DatabaseController.prototype.find = function(className, query, {
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))
.then(() => this.reduceRelationKeys(className, query))
.then(() => this.reduceRelationKeys(className, query, queryOptions))
.then(() => this.reduceInRelation(className, query, schemaController))
.then(() => {
if (!isMaster) {
Expand Down Expand Up @@ -871,7 +881,7 @@ DatabaseController.prototype.find = function(className, query, {
if (!classExists) {
return [];
} 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 => {
object = untransformObjectACL(object);
return filterSensitiveData(isMaster, aclGroup, className, object)
Expand Down

0 comments on commit 09fee7d

Please sign in to comment.