Skip to content

Commit 0532822

Browse files
Merge 3883ff4 into 222f264
2 parents 222f264 + 3883ff4 commit 0532822

13 files changed

+150
-208
lines changed

packages/firestore/src/local/indexeddb_remote_document_cache.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,10 @@ class IndexedDbRemoteDocumentCacheImpl implements IndexedDbRemoteDocumentCache {
289289
return;
290290
}
291291

292-
const document = fromDbRemoteDocument(this.serializer, dbRemoteDoc);
292+
const document = this.maybeDecodeDocument(
293+
DocumentKey.fromSegments(key),
294+
dbRemoteDoc
295+
);
293296
if (!query.path.isPrefixOf(document.key.path)) {
294297
control.done();
295298
} else if (queryMatches(query, document)) {
@@ -331,16 +334,15 @@ class IndexedDbRemoteDocumentCacheImpl implements IndexedDbRemoteDocumentCache {
331334
}
332335

333336
/**
334-
* Decodes `remoteDoc` and returns the document (or null, if the document
335-
* corresponds to the format used for sentinel deletes).
337+
* Decodes `dbRemoteDoc` and returns the document (or an invalid document if
338+
* the document corresponds to the format used for sentinel deletes).
336339
*/
337340
private maybeDecodeDocument(
338341
documentKey: DocumentKey,
339342
dbRemoteDoc: DbRemoteDocument | null
340343
): MutableDocument {
341344
if (dbRemoteDoc) {
342345
const doc = fromDbRemoteDocument(this.serializer, dbRemoteDoc);
343-
344346
// Whether the document is a sentinel removal and should only be used in the
345347
// `getNewDocumentChanges()`
346348
const isSentinelRemoval =
@@ -480,15 +482,14 @@ class IndexedDbRemoteDocumentChangeBuffer extends RemoteDocumentChangeBuffer {
480482
previousSize !== undefined,
481483
`Cannot modify a document that wasn't read (for ${key})`
482484
);
483-
if (documentChange.document.isValidDocument()) {
485+
if (documentChange.isValidDocument()) {
484486
debugAssert(
485-
!this.getReadTime(key).isEqual(SnapshotVersion.min()),
487+
!documentChange.readTime.isEqual(SnapshotVersion.min()),
486488
'Cannot add a document with a read time of zero'
487489
);
488490
const doc = toDbRemoteDocument(
489491
this.documentCache.serializer,
490-
documentChange.document,
491-
this.getReadTime(key)
492+
documentChange
492493
);
493494
collectionParents = collectionParents.add(key.path.popLast());
494495

@@ -504,8 +505,7 @@ class IndexedDbRemoteDocumentChangeBuffer extends RemoteDocumentChangeBuffer {
504505
// preserved in `getNewDocumentChanges()`.
505506
const deletedDoc = toDbRemoteDocument(
506507
this.documentCache.serializer,
507-
MutableDocument.newNoDocument(key, SnapshotVersion.min()),
508-
this.getReadTime(key)
508+
documentChange.convertToNoDocument(SnapshotVersion.min())
509509
);
510510
promises.push(
511511
this.documentCache.addEntry(transaction, key, deletedDoc)

packages/firestore/src/local/local_serializer.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,35 +69,41 @@ export function fromDbRemoteDocument(
6969
localSerializer: LocalSerializer,
7070
remoteDoc: DbRemoteDocument
7171
): MutableDocument {
72+
let doc: MutableDocument;
7273
if (remoteDoc.document) {
73-
return fromDocument(
74+
doc = fromDocument(
7475
localSerializer.remoteSerializer,
7576
remoteDoc.document,
7677
!!remoteDoc.hasCommittedMutations
7778
);
7879
} else if (remoteDoc.noDocument) {
7980
const key = DocumentKey.fromSegments(remoteDoc.noDocument.path);
8081
const version = fromDbTimestamp(remoteDoc.noDocument.readTime);
81-
const document = MutableDocument.newNoDocument(key, version);
82-
return remoteDoc.hasCommittedMutations
83-
? document.setHasCommittedMutations()
84-
: document;
82+
doc = MutableDocument.newNoDocument(key, version);
83+
if (remoteDoc.hasCommittedMutations) {
84+
doc.setHasCommittedMutations();
85+
}
8586
} else if (remoteDoc.unknownDocument) {
8687
const key = DocumentKey.fromSegments(remoteDoc.unknownDocument.path);
8788
const version = fromDbTimestamp(remoteDoc.unknownDocument.version);
88-
return MutableDocument.newUnknownDocument(key, version);
89+
doc = MutableDocument.newUnknownDocument(key, version);
8990
} else {
9091
return fail('Unexpected DbRemoteDocument');
9192
}
93+
94+
if (remoteDoc.readTime) {
95+
doc.setReadTime(fromDbTimestampKey(remoteDoc.readTime));
96+
}
97+
98+
return doc;
9299
}
93100

94101
/** Encodes a document for storage locally. */
95102
export function toDbRemoteDocument(
96103
localSerializer: LocalSerializer,
97-
document: MutableDocument,
98-
readTime: SnapshotVersion
104+
document: MutableDocument
99105
): DbRemoteDocument {
100-
const dbReadTime = toDbTimestampKey(readTime);
106+
const dbReadTime = toDbTimestampKey(document.readTime);
101107
const parentPath = document.key.path.popLast().toArray();
102108
if (document.isFoundDocument()) {
103109
const doc = toDocument(localSerializer.remoteSerializer, document);
@@ -112,11 +118,11 @@ export function toDbRemoteDocument(
112118
);
113119
} else if (document.isNoDocument()) {
114120
const path = document.key.path.toArray();
115-
const readTime = toDbTimestamp(document.version);
121+
const version = toDbTimestamp(document.version);
116122
const hasCommittedMutations = document.hasCommittedMutations;
117123
return new DbRemoteDocument(
118124
/* unknownDocument= */ null,
119-
new DbNoDocument(path, readTime),
125+
new DbNoDocument(path, version),
120126
/* document= */ null,
121127
hasCommittedMutations,
122128
dbReadTime,

packages/firestore/src/local/local_store_impl.ts

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ import {
2626
documentKeySet,
2727
DocumentKeySet,
2828
DocumentMap,
29-
DocumentVersionMap,
30-
documentVersionMap,
3129
mutableDocumentMap,
3230
MutableDocumentMap
3331
} from '../model/collections';
@@ -533,7 +531,7 @@ export function localStoreApplyRemoteEventToLocalCache(
533531
});
534532

535533
let changedDocs = mutableDocumentMap();
536-
remoteEvent.documentUpdates.forEach((key, doc) => {
534+
remoteEvent.documentUpdates.forEach(key => {
537535
if (remoteEvent.resolvedLimboDocuments.has(key)) {
538536
promises.push(
539537
localStoreImpl.persistence.referenceDelegate.updateLimboDocument(
@@ -550,9 +548,7 @@ export function localStoreApplyRemoteEventToLocalCache(
550548
populateDocumentChangeBuffer(
551549
txn,
552550
documentBuffer,
553-
remoteEvent.documentUpdates,
554-
remoteVersion,
555-
undefined
551+
remoteEvent.documentUpdates
556552
).next(result => {
557553
changedDocs = result;
558554
})
@@ -617,19 +613,14 @@ export function localStoreApplyRemoteEventToLocalCache(
617613
function populateDocumentChangeBuffer(
618614
txn: PersistenceTransaction,
619615
documentBuffer: RemoteDocumentChangeBuffer,
620-
documents: MutableDocumentMap,
621-
globalVersion: SnapshotVersion,
622-
// TODO(wuandy): We could add `readTime` to MaybeDocument instead to remove
623-
// this parameter.
624-
documentVersions: DocumentVersionMap | undefined
616+
documents: MutableDocumentMap
625617
): PersistencePromise<MutableDocumentMap> {
626618
let updatedKeys = documentKeySet();
627619
documents.forEach(k => (updatedKeys = updatedKeys.add(k)));
628620
return documentBuffer.getEntries(txn, updatedKeys).next(existingDocs => {
629621
let changedDocs = mutableDocumentMap();
630622
documents.forEach((key, doc) => {
631623
const existingDoc = existingDocs.get(key)!;
632-
const docReadTime = documentVersions?.get(key) || globalVersion;
633624

634625
// Note: The order of the steps below is important, since we want
635626
// to ensure that rejected limbo resolutions (which fabricate
@@ -639,7 +630,7 @@ function populateDocumentChangeBuffer(
639630
// NoDocuments with SnapshotVersion.min() are used in manufactured
640631
// events. We remove these documents from cache since we lost
641632
// access.
642-
documentBuffer.removeEntry(key, docReadTime);
633+
documentBuffer.removeEntry(key, doc.readTime);
643634
changedDocs = changedDocs.insert(key, doc);
644635
} else if (
645636
!existingDoc.isValidDocument() ||
@@ -648,10 +639,10 @@ function populateDocumentChangeBuffer(
648639
existingDoc.hasPendingWrites)
649640
) {
650641
debugAssert(
651-
!SnapshotVersion.min().isEqual(docReadTime),
642+
!SnapshotVersion.min().isEqual(doc.readTime),
652643
'Cannot add a document when the remote version is zero'
653644
);
654-
documentBuffer.addEntry(doc, docReadTime);
645+
documentBuffer.addEntry(doc);
655646
changedDocs = changedDocs.insert(key, doc);
656647
} else {
657648
logDebug(
@@ -1043,7 +1034,8 @@ function applyWriteToRemoteDocuments(
10431034
// We use the commitVersion as the readTime rather than the
10441035
// document's updateTime since the updateTime is not advanced
10451036
// for updates that do not modify the underlying document.
1046-
documentBuffer.addEntry(doc, batchResult.commitVersion);
1037+
doc.setReadTime(batchResult.commitVersion);
1038+
documentBuffer.addEntry(doc);
10471039
}
10481040
}
10491041
});
@@ -1207,20 +1199,16 @@ export async function localStoreApplyBundledDocuments(
12071199
const localStoreImpl = debugCast(localStore, LocalStoreImpl);
12081200
let documentKeys = documentKeySet();
12091201
let documentMap = mutableDocumentMap();
1210-
let versionMap = documentVersionMap();
12111202
for (const bundleDoc of documents) {
12121203
const documentKey = bundleConverter.toDocumentKey(bundleDoc.metadata.name!);
12131204
if (bundleDoc.document) {
12141205
documentKeys = documentKeys.add(documentKey);
12151206
}
1216-
documentMap = documentMap.insert(
1217-
documentKey,
1218-
bundleConverter.toMutableDocument(bundleDoc)
1219-
);
1220-
versionMap = versionMap.insert(
1221-
documentKey,
1207+
const doc = bundleConverter.toMutableDocument(bundleDoc);
1208+
doc.setReadTime(
12221209
bundleConverter.toSnapshotVersion(bundleDoc.metadata.readTime!)
12231210
);
1211+
documentMap = documentMap.insert(documentKey, doc);
12241212
}
12251213

12261214
const documentBuffer = localStoreImpl.remoteDocuments.newChangeBuffer({
@@ -1237,13 +1225,7 @@ export async function localStoreApplyBundledDocuments(
12371225
'Apply bundle documents',
12381226
'readwrite',
12391227
txn => {
1240-
return populateDocumentChangeBuffer(
1241-
txn,
1242-
documentBuffer,
1243-
documentMap,
1244-
SnapshotVersion.min(),
1245-
versionMap
1246-
)
1228+
return populateDocumentChangeBuffer(txn, documentBuffer, documentMap)
12471229
.next(changedDocs => {
12481230
documentBuffer.apply(txn);
12491231
return changedDocs;

packages/firestore/src/local/memory_remote_document_cache.ts

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ export type DocumentSizer = (doc: Document) => number;
3939
interface MemoryRemoteDocumentCacheEntry {
4040
document: Document;
4141
size: number;
42-
readTime: SnapshotVersion;
4342
}
4443

4544
type DocumentEntryMap = SortedMap<DocumentKey, MemoryRemoteDocumentCacheEntry>;
@@ -85,11 +84,10 @@ class MemoryRemoteDocumentCacheImpl implements MemoryRemoteDocumentCache {
8584
*/
8685
addEntry(
8786
transaction: PersistenceTransaction,
88-
doc: MutableDocument,
89-
readTime: SnapshotVersion
87+
doc: MutableDocument
9088
): PersistencePromise<void> {
9189
debugAssert(
92-
!readTime.isEqual(SnapshotVersion.min()),
90+
!doc.readTime.isEqual(SnapshotVersion.min()),
9391
'Cannot add a document with a read time of zero'
9492
);
9593

@@ -100,8 +98,7 @@ class MemoryRemoteDocumentCacheImpl implements MemoryRemoteDocumentCache {
10098

10199
this.docs = this.docs.insert(key, {
102100
document: doc.mutableCopy(),
103-
size: currentSize,
104-
readTime
101+
size: currentSize
105102
});
106103

107104
this.size += currentSize - previousSize;
@@ -173,12 +170,12 @@ class MemoryRemoteDocumentCacheImpl implements MemoryRemoteDocumentCache {
173170
while (iterator.hasNext()) {
174171
const {
175172
key,
176-
value: { document, readTime }
173+
value: { document }
177174
} = iterator.getNext();
178175
if (!query.path.isPrefixOf(key.path)) {
179176
break;
180177
}
181-
if (readTime.compareTo(sinceReadTime) <= 0) {
178+
if (document.readTime.compareTo(sinceReadTime) <= 0) {
182179
continue;
183180
}
184181
if (!queryMatches(query, document)) {
@@ -237,14 +234,8 @@ class MemoryRemoteDocumentChangeBuffer extends RemoteDocumentChangeBuffer {
237234
): PersistencePromise<void> {
238235
const promises: Array<PersistencePromise<void>> = [];
239236
this.changes.forEach((key, doc) => {
240-
if (doc.document.isValidDocument()) {
241-
promises.push(
242-
this.documentCache.addEntry(
243-
transaction,
244-
doc.document,
245-
this.getReadTime(key)
246-
)
247-
);
237+
if (doc.isValidDocument()) {
238+
promises.push(this.documentCache.addEntry(transaction, doc));
248239
} else {
249240
this.documentCache.removeEntry(key);
250241
}

0 commit comments

Comments
 (0)