Skip to content

Optimize redundant logic used in queries #7061

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Dec 16, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### master
[Full Changelog](https://github.com/parse-community/parse-server/compare/4.4.0...master)
- IMPROVE: Optimize queries on classes with pointer permissions.

### 4.4.0
[Full Changelog](https://github.com/parse-community/parse-server/compare/4.3.0...4.4.0)
Expand Down
38 changes: 37 additions & 1 deletion src/Controllers/DatabaseController.js
Original file line number Diff line number Diff line change
Expand Up @@ -1365,6 +1365,32 @@ class DatabaseController {
});
}

objectToEntriesStrings(query: any): Array<string> {
return Object.entries(query).map(a => a.map(s => JSON.stringify(s)).join(':'));
}

reduceOrOperation(query: { $or: Array<any> }): any {
const queries = query.$or.map(q => this.objectToEntriesStrings(q));
for (let i = 0; i < queries.length - 1; i++) {
for (let j = i + 1; j < queries.length; j++) {
const [shorter, longer] = queries[i].length > queries[j].length ? [j, i] : [i, j];
const foundEntries = queries[longer].reduce(
(acc, entry) => acc + (queries[shorter].includes(entry) ? 1 : 0),
0
);
if (foundEntries === queries[shorter].length) {
// If the shorter query is completely contained on the longer one, we can skip it.
query.$or.splice(longer, 1);
queries.splice(longer, 1);
}
}
}

if (query.$or.length === 1) return query.$or[0];

return query;
}

// Constraints query using CLP's pointer permissions (PP) if any.
// 1. Etract the user id from caller's ACLgroup;
// 2. Exctract a list of field names that are PP for target collection and operation;
Expand Down Expand Up @@ -1448,13 +1474,23 @@ class DatabaseController {
}
// if we already have a constraint on the key, use the $and
if (Object.prototype.hasOwnProperty.call(query, key)) {
const queryClauseEntries = this.objectToEntriesStrings(queryClause);
const queryEntries = this.objectToEntriesStrings(query);
const foundEntries = queryEntries.reduce(
(acc, entry) => acc + (queryClauseEntries.includes(entry) ? 1 : 0),
0
);
if (foundEntries === queryClauseEntries.length) {
return query;
}

return { $and: [queryClause, query] };
}
// otherwise just add the constaint
return Object.assign({}, query, queryClause);
});

return queries.length === 1 ? queries[0] : { $or: queries };
return queries.length === 1 ? queries[0] : this.reduceOrOperation({ $or: queries });
} else {
return query;
}
Expand Down