Skip to content

Commit 2dbe8cf

Browse files
authored
idempotent allocateQuery and notifyLocalViewChanges (#2264)
1 parent 70e75b5 commit 2dbe8cf

File tree

1 file changed

+47
-49
lines changed

1 file changed

+47
-49
lines changed

packages/firestore/src/local/local_store.ts

+47-49
Original file line numberDiff line numberDiff line change
@@ -731,41 +731,40 @@ export class LocalStore {
731731
* Notify local store of the changed views to locally pin documents.
732732
*/
733733
notifyLocalViewChanges(viewChanges: LocalViewChanges[]): Promise<void> {
734+
for (const viewChange of viewChanges) {
735+
const targetId = viewChange.targetId;
736+
737+
this.localViewReferences.addReferences(viewChange.addedKeys, targetId);
738+
this.localViewReferences.removeReferences(
739+
viewChange.removedKeys,
740+
targetId
741+
);
742+
743+
if (!viewChange.fromCache) {
744+
const queryData = this.queryDataByTarget.get(targetId);
745+
assert(
746+
queryData !== null,
747+
`Can't set limbo-free snapshot version for unknown target: ${targetId}`
748+
);
749+
750+
// Advance the last limbo free snapshot version
751+
const lastLimboFreeSnapshotVersion = queryData!.snapshotVersion;
752+
const updatedQueryData = queryData!.withLastLimboFreeSnapshotVersion(
753+
lastLimboFreeSnapshotVersion
754+
);
755+
this.queryDataByTarget = this.queryDataByTarget.insert(
756+
targetId,
757+
updatedQueryData
758+
);
759+
}
760+
}
734761
return this.persistence.runTransaction(
735762
'notifyLocalViewChanges',
736-
'readwrite',
763+
'readwrite-idempotent',
737764
txn => {
738765
return PersistencePromise.forEach(
739766
viewChanges,
740767
(viewChange: LocalViewChanges) => {
741-
const targetId = viewChange.targetId;
742-
743-
this.localViewReferences.addReferences(
744-
viewChange.addedKeys,
745-
targetId
746-
);
747-
this.localViewReferences.removeReferences(
748-
viewChange.removedKeys,
749-
targetId
750-
);
751-
752-
if (!viewChange.fromCache) {
753-
const queryData = this.queryDataByTarget.get(targetId);
754-
assert(
755-
queryData !== null,
756-
`Can't set limbo-free snapshot version for unknown target: ${targetId}`
757-
);
758-
759-
// Advance the last limbo free snapshot version
760-
const lastLimboFreeSnapshotVersion = queryData!.snapshotVersion;
761-
const updatedQueryData = queryData!.withLastLimboFreeSnapshotVersion(
762-
lastLimboFreeSnapshotVersion
763-
);
764-
this.queryDataByTarget = this.queryDataByTarget.insert(
765-
targetId,
766-
updatedQueryData
767-
);
768-
}
769768
return PersistencePromise.forEach(
770769
viewChange.removedKeys,
771770
(key: DocumentKey) =>
@@ -819,10 +818,8 @@ export class LocalStore {
819818
* the store can be used to manage its view.
820819
*/
821820
allocateQuery(query: Query): Promise<QueryData> {
822-
return this.persistence.runTransaction(
823-
'Allocate query',
824-
'readwrite',
825-
txn => {
821+
return this.persistence
822+
.runTransaction('Allocate query', 'readwrite-idempotent', txn => {
826823
let queryData: QueryData;
827824
return this.queryCache
828825
.getQueryData(txn, query)
@@ -832,7 +829,7 @@ export class LocalStore {
832829
// previous targetID.
833830
// TODO(mcg): freshen last accessed date?
834831
queryData = cached;
835-
return PersistencePromise.resolve();
832+
return PersistencePromise.resolve(queryData);
836833
} else {
837834
return this.queryCache.allocateTargetId(txn).next(targetId => {
838835
queryData = new QueryData(
@@ -841,24 +838,25 @@ export class LocalStore {
841838
QueryPurpose.Listen,
842839
txn.currentSequenceNumber
843840
);
844-
return this.queryCache.addQueryData(txn, queryData);
841+
return this.queryCache
842+
.addQueryData(txn, queryData)
843+
.next(() => queryData);
845844
});
846845
}
847-
})
848-
.next(() => {
849-
assert(
850-
this.queryDataByTarget.get(queryData.targetId) === null,
851-
'Tried to allocate an already allocated query: ' + query
852-
);
853-
this.queryDataByTarget = this.queryDataByTarget.insert(
854-
queryData.targetId,
855-
queryData
856-
);
857-
this.targetIdByQuery.set(query, queryData.targetId);
858-
return queryData;
859846
});
860-
}
861-
);
847+
})
848+
.then(queryData => {
849+
assert(
850+
this.queryDataByTarget.get(queryData.targetId) === null,
851+
'Tried to allocate an already allocated query: ' + query
852+
);
853+
this.queryDataByTarget = this.queryDataByTarget.insert(
854+
queryData.targetId,
855+
queryData
856+
);
857+
this.targetIdByQuery.set(query, queryData.targetId);
858+
return queryData;
859+
});
862860
}
863861

864862
/**

0 commit comments

Comments
 (0)