Skip to content

idempotent allocateQuery and notifyLocalViewChanges #2264

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 20 commits into from
Oct 15, 2019
Merged
Changes from all 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
96 changes: 47 additions & 49 deletions packages/firestore/src/local/local_store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -724,41 +724,40 @@ export class LocalStore {
* Notify local store of the changed views to locally pin documents.
*/
notifyLocalViewChanges(viewChanges: LocalViewChanges[]): Promise<void> {
for (const viewChange of viewChanges) {
const targetId = viewChange.targetId;

this.localViewReferences.addReferences(viewChange.addedKeys, targetId);
this.localViewReferences.removeReferences(
viewChange.removedKeys,
targetId
);

if (!viewChange.fromCache) {
const queryData = this.queryDataByTarget.get(targetId);
assert(
queryData !== null,
`Can't set limbo-free snapshot version for unknown target: ${targetId}`
);

// Advance the last limbo free snapshot version
const lastLimboFreeSnapshotVersion = queryData!.snapshotVersion;
const updatedQueryData = queryData!.withLastLimboFreeSnapshotVersion(
lastLimboFreeSnapshotVersion
);
this.queryDataByTarget = this.queryDataByTarget.insert(
targetId,
updatedQueryData
);
}
}
return this.persistence.runTransaction(
'notifyLocalViewChanges',
'readwrite',
'readwrite-idempotent',
txn => {
return PersistencePromise.forEach(
viewChanges,
(viewChange: LocalViewChanges) => {
const targetId = viewChange.targetId;

this.localViewReferences.addReferences(
viewChange.addedKeys,
targetId
);
this.localViewReferences.removeReferences(
viewChange.removedKeys,
targetId
);

if (!viewChange.fromCache) {
const queryData = this.queryDataByTarget.get(targetId);
assert(
queryData !== null,
`Can't set limbo-free snapshot version for unknown target: ${targetId}`
);

// Advance the last limbo free snapshot version
const lastLimboFreeSnapshotVersion = queryData!.snapshotVersion;
const updatedQueryData = queryData!.withLastLimboFreeSnapshotVersion(
lastLimboFreeSnapshotVersion
);
this.queryDataByTarget = this.queryDataByTarget.insert(
targetId,
updatedQueryData
);
}
return PersistencePromise.forEach(
viewChange.removedKeys,
(key: DocumentKey) =>
Expand Down Expand Up @@ -812,10 +811,8 @@ export class LocalStore {
* the store can be used to manage its view.
*/
allocateQuery(query: Query): Promise<QueryData> {
return this.persistence.runTransaction(
'Allocate query',
'readwrite',
txn => {
return this.persistence
.runTransaction('Allocate query', 'readwrite-idempotent', txn => {
let queryData: QueryData;
return this.queryCache
.getQueryData(txn, query)
Expand All @@ -825,7 +822,7 @@ export class LocalStore {
// previous targetID.
// TODO(mcg): freshen last accessed date?
queryData = cached;
return PersistencePromise.resolve();
return PersistencePromise.resolve(queryData);
} else {
return this.queryCache.allocateTargetId(txn).next(targetId => {
queryData = new QueryData(
Expand All @@ -834,24 +831,25 @@ export class LocalStore {
QueryPurpose.Listen,
txn.currentSequenceNumber
);
return this.queryCache.addQueryData(txn, queryData);
return this.queryCache
.addQueryData(txn, queryData)
.next(() => queryData);
});
}
})
.next(() => {
assert(
this.queryDataByTarget.get(queryData.targetId) === null,
'Tried to allocate an already allocated query: ' + query
);
this.queryDataByTarget = this.queryDataByTarget.insert(
queryData.targetId,
queryData
);
this.targetIdByQuery.set(query, queryData.targetId);
return queryData;
});
}
);
})
.then(queryData => {
assert(
this.queryDataByTarget.get(queryData.targetId) === null,
'Tried to allocate an already allocated query: ' + query
);
this.queryDataByTarget = this.queryDataByTarget.insert(
queryData.targetId,
queryData
);
this.targetIdByQuery.set(query, queryData.targetId);
return queryData;
});
}

/**
Expand Down