Read preference option per query (#3865)

This commit is contained in:
Antonio Davi Macedo Coelho de Castro
2017-06-21 17:18:10 -03:00
committed by Natan Rolnik
parent 422723fa31
commit b6298feaa7
6 changed files with 832 additions and 12 deletions

View File

@@ -13,13 +13,13 @@ export default class MongoCollection {
// none, then build the geoindex.
// This could be improved a lot but it's not clear if that's a good
// idea. Or even if this behavior is a good idea.
find(query, { skip, limit, sort, keys, maxTimeMS } = {}) {
find(query, { skip, limit, sort, keys, maxTimeMS, readPreference } = {}) {
// Support for Full Text Search - $text
if(keys && keys.$score) {
delete keys.$score;
keys.score = {$meta: 'textScore'};
}
return this._rawFind(query, { skip, limit, sort, keys, maxTimeMS })
return this._rawFind(query, { skip, limit, sort, keys, maxTimeMS, readPreference })
.catch(error => {
// Check for "no geoindex" error
if (error.code != 17007 && !error.message.match(/unable to find index for .geoNear/)) {
@@ -35,13 +35,13 @@ export default class MongoCollection {
index[key] = '2d';
return this._mongoCollection.createIndex(index)
// Retry, but just once.
.then(() => this._rawFind(query, { skip, limit, sort, keys, maxTimeMS }));
.then(() => this._rawFind(query, { skip, limit, sort, keys, maxTimeMS, readPreference }));
});
}
_rawFind(query, { skip, limit, sort, keys, maxTimeMS } = {}) {
_rawFind(query, { skip, limit, sort, keys, maxTimeMS, readPreference } = {}) {
let findOperation = this._mongoCollection
.find(query, { skip, limit, sort })
.find(query, { skip, limit, sort, readPreference })
if (keys) {
findOperation = findOperation.project(keys);
@@ -54,8 +54,8 @@ export default class MongoCollection {
return findOperation.toArray();
}
count(query, { skip, limit, sort, maxTimeMS } = {}) {
const countOperation = this._mongoCollection.count(query, { skip, limit, sort, maxTimeMS });
count(query, { skip, limit, sort, maxTimeMS, readPreference } = {}) {
const countOperation = this._mongoCollection.count(query, { skip, limit, sort, maxTimeMS, readPreference });
return countOperation;
}

View File

@@ -17,6 +17,7 @@ import defaults from '../../../defaults';
const mongodb = require('mongodb');
const MongoClient = mongodb.MongoClient;
const ReadPreference = mongodb.ReadPreference;
const MongoSchemaCollectionName = '_SCHEMA';
@@ -332,7 +333,7 @@ export class MongoStorageAdapter {
}
// Executes a find. Accepts: className, query in Parse format, and { skip, limit, sort }.
find(className, schema, query, { skip, limit, sort, keys }) {
find(className, schema, query, { skip, limit, sort, keys, readPreference }) {
schema = convertParseSchemaToMongoSchema(schema);
const mongoWhere = transformWhere(className, query, schema);
const mongoSort = _.mapKeys(sort, (value, fieldName) => transformKey(className, fieldName, schema));
@@ -340,6 +341,7 @@ export class MongoStorageAdapter {
memo[transformKey(className, key, schema)] = 1;
return memo;
}, {});
readPreference = this._parseReadPreference(readPreference);
return this._adaptiveCollection(className)
.then(collection => collection.find(mongoWhere, {
skip,
@@ -347,6 +349,7 @@ export class MongoStorageAdapter {
sort: mongoSort,
keys: mongoKeys,
maxTimeMS: this._maxTimeMS,
readPreference,
}))
.then(objects => objects.map(object => mongoObjectToParseObject(className, object, schema)))
}
@@ -382,14 +385,41 @@ export class MongoStorageAdapter {
}
// Executes a count.
count(className, schema, query) {
count(className, schema, query, readPreference) {
schema = convertParseSchemaToMongoSchema(schema);
readPreference = this._parseReadPreference(readPreference);
return this._adaptiveCollection(className)
.then(collection => collection.count(transformWhere(className, query, schema), {
maxTimeMS: this._maxTimeMS,
readPreference,
}));
}
_parseReadPreference(readPreference) {
if (readPreference) {
switch (readPreference) {
case 'PRIMARY':
readPreference = ReadPreference.PRIMARY;
break;
case 'PRIMARY_PREFERRED':
readPreference = ReadPreference.PRIMARY_PREFERRED;
break;
case 'SECONDARY':
readPreference = ReadPreference.SECONDARY;
break;
case 'SECONDARY_PREFERRED':
readPreference = ReadPreference.SECONDARY_PREFERRED;
break;
case 'NEAREST':
readPreference = ReadPreference.NEAREST;
break;
default:
throw new Parse.Error(Parse.Error.INVALID_QUERY, 'Not supported read preference.');
}
}
return readPreference;
}
performInitialization() {
return Promise.resolve();
}

View File

@@ -770,7 +770,8 @@ DatabaseController.prototype.find = function(className, query, {
sort = {},
count,
keys,
op
op,
readPreference
} = {}) {
const isMaster = acl === undefined;
const aclGroup = acl || [];
@@ -836,13 +837,13 @@ DatabaseController.prototype.find = function(className, query, {
if (!classExists) {
return 0;
} else {
return this.adapter.count(className, schema, query);
return this.adapter.count(className, schema, query, readPreference);
}
} else {
if (!classExists) {
return [];
} else {
return this.adapter.find(className, schema, query, { skip, limit, sort, keys })
return this.adapter.find(className, schema, query, { skip, limit, sort, keys, readPreference })
.then(objects => objects.map(object => {
object = untransformObjectACL(object);
return filterSensitiveData(isMaster, aclGroup, className, object)

View File

@@ -88,6 +88,7 @@ function RestQuery(config, auth, className, restWhere = {}, restOptions = {}, cl
break;
case 'skip':
case 'limit':
case 'readPreference':
this.findOptions[option] = restOptions[option];
break;
case 'order':
@@ -128,6 +129,9 @@ function RestQuery(config, auth, className, restWhere = {}, restOptions = {}, cl
this.redirectKey = restOptions.redirectClassNameForKey;
this.redirectClassName = null;
break;
case 'includeReadPreference':
case 'subqueryReadPreference':
break;
default:
throw new Parse.Error(Parse.Error.INVALID_JSON,
'bad option: ' + option);
@@ -260,6 +264,11 @@ RestQuery.prototype.replaceInQuery = function() {
redirectClassNameForKey: inQueryValue.redirectClassNameForKey
};
if (this.restOptions.subqueryReadPreference) {
additionalOptions.readPreference = this.restOptions.subqueryReadPreference;
additionalOptions.subqueryReadPreference = this.restOptions.subqueryReadPreference;
}
var subquery = new RestQuery(
this.config, this.auth, inQueryValue.className,
inQueryValue.where, additionalOptions);
@@ -308,6 +317,11 @@ RestQuery.prototype.replaceNotInQuery = function() {
redirectClassNameForKey: notInQueryValue.redirectClassNameForKey
};
if (this.restOptions.subqueryReadPreference) {
additionalOptions.readPreference = this.restOptions.subqueryReadPreference;
additionalOptions.subqueryReadPreference = this.restOptions.subqueryReadPreference;
}
var subquery = new RestQuery(
this.config, this.auth, notInQueryValue.className,
notInQueryValue.where, additionalOptions);
@@ -358,6 +372,11 @@ RestQuery.prototype.replaceSelect = function() {
redirectClassNameForKey: selectValue.query.redirectClassNameForKey
};
if (this.restOptions.subqueryReadPreference) {
additionalOptions.readPreference = this.restOptions.subqueryReadPreference;
additionalOptions.subqueryReadPreference = this.restOptions.subqueryReadPreference;
}
var subquery = new RestQuery(
this.config, this.auth, selectValue.query.className,
selectValue.query.where, additionalOptions);
@@ -406,6 +425,11 @@ RestQuery.prototype.replaceDontSelect = function() {
redirectClassNameForKey: dontSelectValue.query.redirectClassNameForKey
};
if (this.restOptions.subqueryReadPreference) {
additionalOptions.readPreference = this.restOptions.subqueryReadPreference;
additionalOptions.subqueryReadPreference = this.restOptions.subqueryReadPreference;
}
var subquery = new RestQuery(
this.config, this.auth, dontSelectValue.query.className,
dontSelectValue.query.where, additionalOptions);
@@ -605,6 +629,11 @@ function includePath(config, auth, response, path, restOptions = {}) {
}
}
if (restOptions.includeReadPreference) {
includeRestOptions.readPreference = restOptions.includeReadPreference;
includeRestOptions.includeReadPreference = restOptions.includeReadPreference;
}
const queryPromises = Object.keys(pointersHash).map((className) => {
const where = {'objectId': {'$in': Array.from(pointersHash[className])}};
var query = new RestQuery(config, auth, className, where, includeRestOptions);

View File

@@ -343,6 +343,18 @@ export function maybeRunQueryTrigger(triggerType, className, restWhere, restOpti
restOptions = restOptions || {};
restOptions.keys = jsonQuery.keys;
}
if (requestObject.readPreference) {
restOptions = restOptions || {};
restOptions.readPreference = requestObject.readPreference;
}
if (requestObject.includeReadPreference) {
restOptions = restOptions || {};
restOptions.includeReadPreference = requestObject.includeReadPreference;
}
if (requestObject.subqueryReadPreference) {
restOptions = restOptions || {};
restOptions.subqueryReadPreference = requestObject.subqueryReadPreference;
}
return {
restWhere,
restOptions