Skip to content

Don't update query target metadata for updates #1063

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 4 commits into from
Jul 30, 2018
Merged
Show file tree
Hide file tree
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
20 changes: 8 additions & 12 deletions packages/firestore/src/local/indexeddb_query_cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,16 @@ export class IndexedDbQueryCache implements QueryCache {
});
}

setLastRemoteSnapshotVersion(
setTargetsMetadata(
transaction: PersistenceTransaction,
snapshotVersion: SnapshotVersion
highestListenSequenceNumber: number,
lastRemoteSnapshotVersion?: SnapshotVersion
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this optional? Do we ever not supply it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Local Store doesn't always call setLastRemoteSnapshotVersion. Do you still want to update the listen sequence number?

See https://github.com/firebase/firebase-js-sdk/blob/master/packages/firestore/src/local/local_store.ts#L524

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. I do still want to update the sequence number. I guess optional is ok, although it looks like in that case we could also pass in the highest snapshot version and have it be a no-op. I don't feel too strongly either way though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can refine the API when we start porting to Web, but I believe the highest snapshot version that you know of could be SnapshotVersion.MIN (unless you pass the version that you just got from getLastRemoteSnapshotVersion).

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I meant the one you just read. But yeah, we can update later if need be.

): PersistencePromise<void> {
return this.retrieveMetadata(transaction).next(metadata => {
metadata.lastRemoteSnapshotVersion = snapshotVersion.toTimestamp();
metadata.highestListenSequenceNumber = highestListenSequenceNumber;
if (lastRemoteSnapshotVersion) {
metadata.lastRemoteSnapshotVersion = lastRemoteSnapshotVersion.toTimestamp();
}
return this.saveMetadata(transaction, metadata);
});
}
Expand All @@ -114,15 +118,7 @@ export class IndexedDbQueryCache implements QueryCache {
transaction: PersistenceTransaction,
queryData: QueryData
): PersistencePromise<void> {
return this.saveQueryData(transaction, queryData).next(() => {
return this.retrieveMetadata(transaction).next(metadata => {
if (this.updateMetadataFromQueryData(queryData, metadata)) {
return this.saveMetadata(transaction, metadata);
} else {
return PersistencePromise.resolve();
}
});
});
return this.saveQueryData(transaction, queryData);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You will still potentially need to update the metadata. Once QueryCache starts tracking the highest sequence number, it is likely that an update will trigger a metadata write.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the PR to allow future Greg to set the sequence number as part of the setLastRemoteSnapshotVersion call. Let me know if that will work as is for GC.

I am also still accepting nominations for the API name voting challenge.

}

removeQueryData(
Expand Down
3 changes: 2 additions & 1 deletion packages/firestore/src/local/local_store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -605,8 +605,9 @@ export class LocalStore {
' < ' +
lastRemoteVersion
);
return this.queryCache.setLastRemoteSnapshotVersion(
return this.queryCache.setTargetsMetadata(
txn,
/*highestSequenceNumber=*/ 0,
remoteVersion
);
});
Expand Down
9 changes: 6 additions & 3 deletions packages/firestore/src/local/memory_query_cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,14 @@ export class MemoryQueryCache implements QueryCache {
return PersistencePromise.resolve(nextTargetId);
}

setLastRemoteSnapshotVersion(
setTargetsMetadata(
transaction: PersistenceTransaction,
snapshotVersion: SnapshotVersion
highestListenSequenceNumber: number,
lastRemoteSnapshotVersion?: SnapshotVersion
): PersistencePromise<void> {
this.lastRemoteSnapshotVersion = snapshotVersion;
if (lastRemoteSnapshotVersion) {
this.lastRemoteSnapshotVersion = lastRemoteSnapshotVersion;
}
return PersistencePromise.resolve();
}

Expand Down
13 changes: 8 additions & 5 deletions packages/firestore/src/local/query_cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,17 @@ export interface QueryCache extends GarbageSource {
): PersistencePromise<SnapshotVersion>;

/**
* Set the snapshot version representing the last consistent snapshot received
* from the backend. (see getLastRemoteSnapshotVersion() for more details).
* Set the highest listen sequence number and optionally updates the
* snapshot version of the last consistent snapshot received from the backend
* (see getLastRemoteSnapshotVersion() for more details).
*
* @param snapshotVersion The new snapshot version.
* @param highestListenSequenceNumber The new maximum listen sequence number.
* @param lastRemoteSnapshotVersion The new snapshot version. Optional.
*/
setLastRemoteSnapshotVersion(
setTargetsMetadata(
transaction: PersistenceTransaction,
snapshotVersion: SnapshotVersion
highestListenSequenceNumber: number,
lastRemoteSnapshotVersion?: SnapshotVersion
): PersistencePromise<void>;

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/firestore/test/unit/local/query_cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,14 +325,14 @@ function genericQueryCacheTests(): void {
expect(await otherCache.allocateTargetId()).to.deep.equal(50);
});

it('can get / set lastRemoteSnapshotVersion', async () => {
it('can get / set targets metadata', async () => {
expect(await cache.getLastRemoteSnapshotVersion()).to.deep.equal(
SnapshotVersion.MIN
);

// Can set the snapshot version.
return cache
.setLastRemoteSnapshotVersion(version(42))
.setTargetsMetadata(/* highestListenSequenceNumber= */ 0, version(42))
.then(async () => {
expect(await cache.getLastRemoteSnapshotVersion()).to.deep.equal(
version(42)
Expand Down
15 changes: 10 additions & 5 deletions packages/firestore/test/unit/local/test_query_cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,16 @@ export class TestQueryCache {
});
}

setLastRemoteSnapshotVersion(version: SnapshotVersion): Promise<void> {
return this.persistence.runTransaction(
'setLastRemoteSnapshotVersion',
true,
txn => this.cache.setLastRemoteSnapshotVersion(txn, version)
setTargetsMetadata(
highestListenSequenceNumber: number,
lastRemoteSnapshotVersion?: SnapshotVersion
): Promise<void> {
return this.persistence.runTransaction('setTargetsMetadata', true, txn =>
this.cache.setTargetsMetadata(
txn,
highestListenSequenceNumber,
lastRemoteSnapshotVersion
)
);
}
}