Skip to content
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

Fix Firestore failing to return empty results from the local cache #6624

Merged
merged 20 commits into from
Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions .changeset/cool-grapes-attend.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
'@firebase/firestore': patch
dconeybe marked this conversation as resolved.
Show resolved Hide resolved
'firebase': patch
---

Fix Firestore failing to raise initial snapshot from empty local cache result
8 changes: 4 additions & 4 deletions packages/firestore/src/core/event_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ export class QueryListener {
snap.fromCache,
snap.syncStateChanged,
/* excludesMetadataChanges= */ true,
snap.resumeToken
snap.hasCachedResults
);
}
let raisedEvent = false;
Expand Down Expand Up @@ -372,11 +372,11 @@ export class QueryListener {
return false;
}

// Raise data from cache if we have any documents or resume token,
// Raise data from cache if we have any documents, have cached results before,
// or we are offline.
return (
dconeybe marked this conversation as resolved.
Show resolved Hide resolved
!snap.docs.isEmpty() ||
snap.resumeToken.approximateByteSize() > 0 ||
snap.hasCachedResults ||
onlineState === OnlineState.Offline
);
}
Expand Down Expand Up @@ -412,7 +412,7 @@ export class QueryListener {
snap.docs,
snap.mutatedKeys,
snap.fromCache,
snap.resumeToken
snap.hasCachedResults
);
this.raisedInitialEvent = true;
this.queryObserver.next(snap);
Expand Down
8 changes: 4 additions & 4 deletions packages/firestore/src/core/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import { DocumentKey } from '../model/document_key';
import { DocumentSet } from '../model/document_set';
import { TargetChange } from '../remote/remote_event';
import { debugAssert, fail } from '../util/assert';
import { ByteString } from '../util/byte_string';

import { LimitType, newQueryComparator, Query, queryMatches } from './query';
import { OnlineState } from './types';
Expand Down Expand Up @@ -73,7 +72,7 @@ export interface ViewChange {
*/
export class View {
private syncState: SyncState | null = null;
private resumeToken: ByteString | null = null;
private hasCachedResults: boolean = false;
/**
* A flag whether the view is current with the backend. A view is considered
* current after it has seen the current flag from the backend and did not
Expand Down Expand Up @@ -322,7 +321,8 @@ export class View {
newSyncState === SyncState.Local,
syncStateChanged,
/* excludesMetadataChanges= */ false,
targetChange?.resumeToken ?? ByteString.EMPTY_BYTE_STRING
targetChange !== undefined &&
dconeybe marked this conversation as resolved.
Show resolved Hide resolved
targetChange.resumeToken.approximateByteSize() > 0
);
return {
snapshot: snap,
Expand Down Expand Up @@ -472,7 +472,7 @@ export class View {
this.documentSet,
this.mutatedKeys,
this.syncState === SyncState.Local,
this.resumeToken ?? ByteString.EMPTY_BYTE_STRING
this.hasCachedResults
);
}
}
Expand Down
8 changes: 4 additions & 4 deletions packages/firestore/src/core/view_snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import { Document } from '../model/document';
import { DocumentKey } from '../model/document_key';
import { DocumentSet } from '../model/document_set';
import { fail } from '../util/assert';
import { ByteString } from '../util/byte_string';
import { SortedMap } from '../util/sorted_map';

import { Query, queryEquals } from './query';
Expand Down Expand Up @@ -148,7 +147,7 @@ export class ViewSnapshot {
readonly fromCache: boolean,
readonly syncStateChanged: boolean,
readonly excludesMetadataChanges: boolean,
readonly resumeToken: ByteString
readonly hasCachedResults: boolean
) {}

/** Returns a view snapshot as if all documents in the snapshot were added. */
Expand All @@ -157,7 +156,7 @@ export class ViewSnapshot {
documents: DocumentSet,
mutatedKeys: DocumentKeySet,
fromCache: boolean,
resumeToken: ByteString
hasCachedResults: boolean
): ViewSnapshot {
const changes: DocumentViewChange[] = [];
documents.forEach(doc => {
Expand All @@ -173,7 +172,7 @@ export class ViewSnapshot {
fromCache,
/* syncStateChanged= */ true,
/* excludesMetadataChanges= */ false,
resumeToken
hasCachedResults
);
}

Expand All @@ -184,6 +183,7 @@ export class ViewSnapshot {
isEqual(other: ViewSnapshot): boolean {
if (
this.fromCache !== other.fromCache ||
this.hasCachedResults !== other.hasCachedResults ||
this.syncStateChanged !== other.syncStateChanged ||
!this.mutatedKeys.isEqual(other.mutatedKeys) ||
!queryEquals(this.query, other.query) ||
Expand Down
89 changes: 45 additions & 44 deletions packages/firestore/test/unit/api/database.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ import {
} from '../../../src';
import { EmulatorAuthCredentialsProvider } from '../../../src/api/credentials';
import { User } from '../../../src/auth/user';
import { encodeBase64 } from '../../../src/platform/base64';
import { ByteString } from '../../../src/util/byte_string';
import {
collectionReference,
documentReference,
Expand Down Expand Up @@ -179,48 +177,51 @@ describe('QuerySnapshot', () => {
querySnapshot('foo', {}, { a: { a: 1 } }, keys('foo/a'), false, true)
)
).to.be.false;
});

it('resume token should not effect querySnapshot equality', () => {
const resumeToken1 = ByteString.fromBase64String(
encodeBase64('ResumeToken1')
);
const resumeToken1Copy = ByteString.fromBase64String(
encodeBase64('ResumeToken1')
);
const resumeToken2 = ByteString.fromBase64String(
encodeBase64('ResumeToken2')
);

const snapshot1 = querySnapshot(
'foo',
{},
{ a: { a: 1 } },
keys(),
false,
false,
resumeToken1
);
const snapshot1Copy = querySnapshot(
'foo',
{},
{ a: { a: 1 } },
keys(),
false,
false,
resumeToken1Copy
);
const snapshot2 = querySnapshot(
'foo',
{},
{ a: { a: 1 } },
keys(),
false,
false,
resumeToken2
);
expect(snapshotEqual(snapshot1, snapshot1Copy)).to.be.true;
expect(snapshotEqual(snapshot1, snapshot2)).to.be.true;
// hasCachedResults should effect querySnapshot equality
dconeybe marked this conversation as resolved.
Show resolved Hide resolved
expect(
snapshotEqual(
querySnapshot(
'foo',
{},
{ a: { a: 1 } },
keys('foo/a'),
false,
false,
true
),
querySnapshot(
'foo',
{},
{ a: { a: 1 } },
keys('foo/a'),
false,
false,
true
)
)
).to.be.true;
expect(
snapshotEqual(
querySnapshot(
'foo',
{},
{ a: { a: 1 } },
keys('foo/a'),
false,
false,
true
),
querySnapshot(
'foo',
{},
{ a: { a: 1 } },
keys('foo/a'),
false,
false,
false
)
)
).to.be.false;
});

it('JSON.stringify() does not throw', () => {
Expand Down
14 changes: 7 additions & 7 deletions packages/firestore/test/unit/core/event_manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ describe('QueryListener', () => {
fromCache: snap2.fromCache,
syncStateChanged: true,
mutatedKeys: keys(),
resumeToken: snap2.resumeToken
hasCachedResults: snap2.hasCachedResults
};
expect(otherEvents).to.deep.equal([expectedSnap2]);
});
Expand Down Expand Up @@ -398,7 +398,7 @@ describe('QueryListener', () => {
fromCache: snap2.fromCache,
syncStateChanged: snap2.syncStateChanged,
mutatedKeys: snap2.mutatedKeys,
resumeToken: snap2.resumeToken
hasCachedResults: snap2.hasCachedResults
};
expect(filteredEvents).to.deep.equal([snap1, expectedSnap2]);
}
Expand Down Expand Up @@ -485,7 +485,7 @@ describe('QueryListener', () => {
fromCache: false,
syncStateChanged: true,
mutatedKeys: keys(),
resumeToken: snap3.resumeToken
hasCachedResults: snap3.hasCachedResults
};
expect(events).to.deep.equal([expectedSnap]);
});
Expand Down Expand Up @@ -521,7 +521,7 @@ describe('QueryListener', () => {
fromCache: true,
syncStateChanged: true,
mutatedKeys: keys(),
resumeToken: snap1.resumeToken
hasCachedResults: snap1.hasCachedResults
};
const expectedSnap2 = {
query: query1,
Expand All @@ -531,7 +531,7 @@ describe('QueryListener', () => {
fromCache: true,
syncStateChanged: false,
mutatedKeys: keys(),
resumeToken: snap2.resumeToken
hasCachedResults: snap2.hasCachedResults
};
expect(events).to.deep.equal([expectedSnap1, expectedSnap2]);
});
Expand All @@ -558,7 +558,7 @@ describe('QueryListener', () => {
fromCache: true,
syncStateChanged: true,
mutatedKeys: keys(),
resumeToken: snap1.resumeToken
hasCachedResults: snap1.hasCachedResults
};
expect(events).to.deep.equal([expectedSnap]);
});
Expand All @@ -584,7 +584,7 @@ describe('QueryListener', () => {
fromCache: true,
syncStateChanged: true,
mutatedKeys: keys(),
resumeToken: snap1.resumeToken
hasCachedResults: snap1.hasCachedResults
};
expect(events).to.deep.equal([expectedSnap]);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/firestore/test/unit/specs/listen_spec.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1764,7 +1764,7 @@ describeSpec('Listens:', [], () => {
.client(1)
// Re-listen to the query in second client and verify that the empty
// snapshot is raised from cache.
.userListens(query1, { resumeToken: 'resume-token-1000' })
.userListens(query1)
.expectEvents(query1, { fromCache: true })
.client(0)
.expectListen(query1, { resumeToken: 'resume-token-1000' })
Expand Down
5 changes: 2 additions & 3 deletions packages/firestore/test/util/api_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ import {
import { DocumentKeySet } from '../../src/model/collections';
import { DocumentSet } from '../../src/model/document_set';
import { JsonObject } from '../../src/model/object_value';
import { ByteString } from '../../src/util/byte_string';
import { TEST_PROJECT } from '../unit/local/persistence_test_helpers';

import { doc, key, path as pathFrom } from './helpers';
Expand Down Expand Up @@ -132,7 +131,7 @@ export function querySnapshot(
mutatedKeys: DocumentKeySet,
fromCache: boolean,
syncStateChanged: boolean,
resumeToken?: ByteString
hasCachedResults?: boolean
): QuerySnapshot {
const query: InternalQuery = newQueryForPath(pathFrom(path));
let oldDocuments: DocumentSet = new DocumentSet();
Expand All @@ -155,7 +154,7 @@ export function querySnapshot(
fromCache,
syncStateChanged,
false,
resumeToken ?? ByteString.EMPTY_BYTE_STRING
hasCachedResults ?? false
);
const db = firestore();
return new QuerySnapshot(
Expand Down