Skip to content

Commit b5f7a1f

Browse files
committed
Uses one query for installation dedup
1 parent 9bf21ef commit b5f7a1f

File tree

1 file changed

+52
-38
lines changed

1 file changed

+52
-38
lines changed

src/RestWrite.js

Lines changed: 52 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -581,65 +581,79 @@ RestWrite.prototype.handleInstallation = function() {
581581
var promise = Promise.resolve();
582582

583583
var idMatch; // Will be a match on either objectId or installationId
584+
var objectIdMatch;
585+
var installationIdMatch;
584586
var deviceTokenMatches = [];
585587

588+
// Instead of issuing 3 reads, let's do it with one OR.
589+
let orQueries = [];
586590
if (this.query && this.query.objectId) {
587-
promise = promise.then(() => {
588-
return this.config.database.find('_Installation', {
591+
orQueries.push({
589592
objectId: this.query.objectId
590-
}, {}).then((results) => {
591-
if (!results.length) {
592-
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND,
593+
});
594+
}
595+
if (this.data.installationId) {
596+
orQueries.push({
597+
'installationId': this.data.installationId
598+
});
599+
}
600+
if (this.data.deviceToken) {
601+
orQueries.push({'deviceToken': this.data.deviceToken});
602+
}
603+
604+
promise = promise.then(() => {
605+
return this.config.database.find('_Installation', {
606+
'$or': orQueries
607+
}, {});
608+
}).then((results) => {
609+
results.forEach((result) => {
610+
if (this.query && this.query.objectId && result.objectId == this.query.objectId) {
611+
objectIdMatch = result;
612+
}
613+
if (result.installationId == this.data.installationId) {
614+
installationIdMatch = result;
615+
}
616+
if (result.deviceToken == this.data.deviceToken) {
617+
deviceTokenMatches.push(result);
618+
}
619+
});
620+
621+
// Sanity checks when running a query
622+
if (this.query && this.query.objectId) {
623+
if (!objectIdMatch) {
624+
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND,
593625
'Object not found for update.');
594-
}
595-
idMatch = results[0];
596-
if (this.data.installationId && idMatch.installationId &&
597-
this.data.installationId !== idMatch.installationId) {
626+
}
627+
if (this.data.installationId && objectIdMatch.installationId &&
628+
this.data.installationId !== objectIdMatch.installationId) {
598629
throw new Parse.Error(136,
599630
'installationId may not be changed in this ' +
600631
'operation');
601632
}
602-
if (this.data.deviceToken && idMatch.deviceToken &&
603-
this.data.deviceToken !== idMatch.deviceToken &&
604-
!this.data.installationId && !idMatch.installationId) {
633+
if (this.data.deviceToken && objectIdMatch.deviceToken &&
634+
this.data.deviceToken !== objectIdMatch.deviceToken &&
635+
!this.data.installationId && !objectIdMatch.installationId) {
605636
throw new Parse.Error(136,
606637
'deviceToken may not be changed in this ' +
607638
'operation');
608639
}
609640
if (this.data.deviceType && this.data.deviceType &&
610-
this.data.deviceType !== idMatch.deviceType) {
641+
this.data.deviceType !== objectIdMatch.deviceType) {
611642
throw new Parse.Error(136,
612643
'deviceType may not be changed in this ' +
613644
'operation');
614645
}
615-
return;
616-
});
617-
});
618-
}
619-
620-
// Check if we already have installations for the installationId/deviceToken
621-
promise = promise.then(() => {
622-
if (this.data.installationId) {
623-
return this.config.database.find('_Installation', {
624-
'installationId': this.data.installationId
625-
});
626646
}
627-
return Promise.resolve([]);
628-
}).then((results) => {
629-
if (results && results.length) {
630-
// We only take the first match by installationId
631-
idMatch = results[0];
632-
}
633-
if (this.data.deviceToken) {
634-
return this.config.database.find(
635-
'_Installation',
636-
{'deviceToken': this.data.deviceToken});
647+
648+
if (this.query && this.query.objectId && objectIdMatch) {
649+
idMatch = objectIdMatch;
637650
}
638-
return Promise.resolve([]);
639-
}).then((results) => {
640-
if (results) {
641-
deviceTokenMatches = results;
651+
652+
if (this.data.installationId && installationIdMatch) {
653+
idMatch = installationIdMatch;
642654
}
655+
656+
}).then(() => {
643657
if (!idMatch) {
644658
if (!deviceTokenMatches.length) {
645659
return;

0 commit comments

Comments
 (0)