Skip to content

Commit

Permalink
Uses one query for installation dedup (#2281)
Browse files Browse the repository at this point in the history
  • Loading branch information
flovilmart authored and drew-gross committed Jul 19, 2016
1 parent 6f29205 commit 4f89ec3
Showing 1 changed file with 56 additions and 38 deletions.
94 changes: 56 additions & 38 deletions src/RestWrite.js
Original file line number Diff line number Diff line change
@@ -582,65 +582,83 @@ RestWrite.prototype.handleInstallation = function() {
var promise = Promise.resolve();

var idMatch; // Will be a match on either objectId or installationId
var objectIdMatch;
var installationIdMatch;
var deviceTokenMatches = [];

// Instead of issuing 3 reads, let's do it with one OR.
let orQueries = [];
if (this.query && this.query.objectId) {
promise = promise.then(() => {
return this.config.database.find('_Installation', {
orQueries.push({
objectId: this.query.objectId
}, {}).then((results) => {
if (!results.length) {
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND,
});
}
if (this.data.installationId) {
orQueries.push({
'installationId': this.data.installationId
});
}
if (this.data.deviceToken) {
orQueries.push({'deviceToken': this.data.deviceToken});
}

if (orQueries.length == 0) {
return;
}

promise = promise.then(() => {
return this.config.database.find('_Installation', {
'$or': orQueries
}, {});
}).then((results) => {
results.forEach((result) => {
if (this.query && this.query.objectId && result.objectId == this.query.objectId) {
objectIdMatch = result;
}
if (result.installationId == this.data.installationId) {
installationIdMatch = result;
}
if (result.deviceToken == this.data.deviceToken) {
deviceTokenMatches.push(result);
}
});

// Sanity checks when running a query
if (this.query && this.query.objectId) {
if (!objectIdMatch) {
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND,
'Object not found for update.');
}
idMatch = results[0];
if (this.data.installationId && idMatch.installationId &&
this.data.installationId !== idMatch.installationId) {
}
if (this.data.installationId && objectIdMatch.installationId &&
this.data.installationId !== objectIdMatch.installationId) {
throw new Parse.Error(136,
'installationId may not be changed in this ' +
'operation');
}
if (this.data.deviceToken && idMatch.deviceToken &&
this.data.deviceToken !== idMatch.deviceToken &&
!this.data.installationId && !idMatch.installationId) {
if (this.data.deviceToken && objectIdMatch.deviceToken &&
this.data.deviceToken !== objectIdMatch.deviceToken &&
!this.data.installationId && !objectIdMatch.installationId) {
throw new Parse.Error(136,
'deviceToken may not be changed in this ' +
'operation');
}
if (this.data.deviceType && this.data.deviceType &&
this.data.deviceType !== idMatch.deviceType) {
this.data.deviceType !== objectIdMatch.deviceType) {
throw new Parse.Error(136,
'deviceType may not be changed in this ' +
'operation');
}
return;
});
});
}

// Check if we already have installations for the installationId/deviceToken
promise = promise.then(() => {
if (this.data.installationId) {
return this.config.database.find('_Installation', {
'installationId': this.data.installationId
});
}
return Promise.resolve([]);
}).then((results) => {
if (results && results.length) {
// We only take the first match by installationId
idMatch = results[0];
}
if (this.data.deviceToken) {
return this.config.database.find(
'_Installation',
{'deviceToken': this.data.deviceToken});

if (this.query && this.query.objectId && objectIdMatch) {
idMatch = objectIdMatch;
}
return Promise.resolve([]);
}).then((results) => {
if (results) {
deviceTokenMatches = results;

if (this.data.installationId && installationIdMatch) {
idMatch = installationIdMatch;
}

}).then(() => {
if (!idMatch) {
if (!deviceTokenMatches.length) {
return;

0 comments on commit 4f89ec3

Please sign in to comment.