Skip to content

Commit 6ee3647

Browse files
Index-free: Add readTime to the MemoryRemoteDocumentCache (#2157)
1 parent 330abde commit 6ee3647

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

packages/firestore/src/local/memory_remote_document_cache.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,26 @@ import { RemoteDocumentChangeBuffer } from './remote_document_change_buffer';
4141

4242
export type DocumentSizer = (doc: MaybeDocument) => number;
4343

44-
type DocumentSizeMap = SortedMap<DocumentKey, DocumentSizeEntry>;
45-
function documentSizeMap(): DocumentSizeMap {
46-
return new SortedMap<DocumentKey, DocumentSizeEntry>(DocumentKey.comparator);
44+
/** Miscellaneous collection types / constants. */
45+
interface MemoryRemoteDocumentCacheEntry extends DocumentSizeEntry {
46+
readTime: SnapshotVersion;
47+
}
48+
49+
type DocumentEntryMap = SortedMap<DocumentKey, MemoryRemoteDocumentCacheEntry>;
50+
function documentEntryMap(): DocumentEntryMap {
51+
return new SortedMap<DocumentKey, MemoryRemoteDocumentCacheEntry>(
52+
DocumentKey.comparator
53+
);
4754
}
4855

4956
export class MemoryRemoteDocumentCache implements RemoteDocumentCache {
50-
private docs = documentSizeMap();
57+
/** Underlying cache of documents and their read times. */
58+
private docs = documentEntryMap();
59+
60+
/** Set of documents changed since last call to `getNewDocumentChanges()`. */
5161
private newDocumentChanges = documentKeySet();
62+
63+
/** Size of all cached documents. */
5264
private size = 0;
5365

5466
/**
@@ -68,7 +80,8 @@ export class MemoryRemoteDocumentCache implements RemoteDocumentCache {
6880
*/
6981
private addEntry(
7082
transaction: PersistenceTransaction,
71-
doc: MaybeDocument
83+
doc: MaybeDocument,
84+
readTime: SnapshotVersion
7285
): PersistencePromise<void> {
7386
const key = doc.key;
7487
const entry = this.docs.get(key);
@@ -77,7 +90,8 @@ export class MemoryRemoteDocumentCache implements RemoteDocumentCache {
7790

7891
this.docs = this.docs.insert(key, {
7992
maybeDocument: doc,
80-
size: currentSize
93+
size: currentSize,
94+
readTime
8195
});
8296

8397
this.newDocumentChanges = this.newDocumentChanges.add(key);
@@ -204,7 +218,9 @@ export class MemoryRemoteDocumentCache implements RemoteDocumentCache {
204218
const promises: Array<PersistencePromise<void>> = [];
205219
this.changes.forEach((key, doc) => {
206220
if (doc) {
207-
promises.push(this.documentCache.addEntry(transaction, doc));
221+
promises.push(
222+
this.documentCache.addEntry(transaction, doc, this.readTime)
223+
);
208224
} else {
209225
this.documentCache.removeEntry(key);
210226
}

0 commit comments

Comments
 (0)