Skip to content

Commit e83e967

Browse files
WIP
1 parent f82fc64 commit e83e967

11 files changed

+46
-56
lines changed

packages/firestore/src/local/indexeddb_remote_document_cache.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ class IndexedDbRemoteDocumentCacheImpl implements IndexedDbRemoteDocumentCache {
189189
documentKeys,
190190
(key, dbRemoteDoc) => {
191191
const doc = this.maybeDecodeDocument(key, dbRemoteDoc);
192-
if (doc) {
192+
if (doc.isValidDocument()) {
193193
results = results.insert(key, doc);
194194
sizeMap = sizeMap.insert(key, dbDocumentSize(dbRemoteDoc!));
195195
} else {

packages/firestore/src/local/local_documents_view.ts

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,12 @@ export class LocalDocumentsView {
9292
private applyLocalMutationsToDocuments(
9393
docs: MutableDocumentMap,
9494
batches: MutationBatch[]
95-
): MutableDocumentMap {
96-
let results = mutableDocumentMap();
95+
): void {
9796
docs.forEach((key, localView) => {
9897
for (const batch of batches) {
9998
batch.applyToLocalView(localView);
10099
}
101-
results = results.insert(key, localView);
102100
});
103-
return results;
104101
}
105102

106103
/**
@@ -113,29 +110,23 @@ export class LocalDocumentsView {
113110
transaction: PersistenceTransaction,
114111
keys: DocumentKeySet
115112
): PersistencePromise<DocumentMap> {
116-
return this.remoteDocumentCache
117-
.getEntries(transaction, keys)
118-
.next(docs => this.getLocalViewOfDocuments(transaction, docs));
113+
return this.remoteDocumentCache.getEntries(transaction, keys).next(docs => {
114+
this.applyLocalViewToDocuments(transaction, docs);
115+
return docs as DocumentMap;
116+
});
119117
}
120118

121119
/**
122-
* Similar to `getDocuments`, but creates the local view from the given
123-
* `baseDocs` without retrieving documents from the local store.
120+
* Applies the local view the given `baseDocs` without retrieving documents
121+
* from the local store.
124122
*/
125-
getLocalViewOfDocuments(
123+
applyLocalViewToDocuments(
126124
transaction: PersistenceTransaction,
127125
baseDocs: MutableDocumentMap
128-
): PersistencePromise<DocumentMap> {
126+
): PersistencePromise<void> {
129127
return this.mutationQueue
130128
.getAllMutationBatchesAffectingDocumentKeys(transaction, baseDocs)
131-
.next(batches => {
132-
const docs = this.applyLocalMutationsToDocuments(baseDocs, batches);
133-
let results = mutableDocumentMap();
134-
docs.forEach((key, document) => {
135-
results = results.insert(key, document);
136-
});
137-
return results as DocumentMap;
138-
});
129+
.next(batches => this.applyLocalMutationsToDocuments(baseDocs, batches));
139130
}
140131

141132
/**

packages/firestore/src/local/local_store_impl.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,8 @@ export function localStoreWriteLocally(
346346
});
347347
})
348348
.then(batch => {
349-
const changes = batch.applyToLocalDocumentSet(existingDocs);
350-
return { batchId: batch.batchId, changes };
349+
batch.applyToLocalDocumentSet(existingDocs);
350+
return { batchId: batch.batchId, changes: existingDocs };
351351
});
352352
}
353353

@@ -575,10 +575,11 @@ export function localStoreApplyRemoteEventToLocalCache(
575575
return PersistencePromise.waitFor(promises)
576576
.next(() => documentBuffer.apply(txn))
577577
.next(() => {
578-
return localStoreImpl.localDocuments.getLocalViewOfDocuments(
578+
localStoreImpl.localDocuments.applyLocalViewToDocuments(
579579
txn,
580580
changedDocs
581581
);
582+
return changedDocs;
582583
});
583584
})
584585
.then(changedDocs => {
@@ -1254,12 +1255,13 @@ export async function localStoreApplyBundledDocuments(
12541255
umbrellaTargetData.targetId
12551256
)
12561257
)
1257-
.next(() =>
1258-
localStoreImpl.localDocuments.getLocalViewOfDocuments(
1258+
.next(() => {
1259+
localStoreImpl.localDocuments.applyLocalViewToDocuments(
12591260
txn,
12601261
changedDocs
1261-
)
1262-
);
1262+
);
1263+
return changedDocs;
1264+
});
12631265
});
12641266
}
12651267
);

packages/firestore/src/local/memory_remote_document_cache.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class MemoryRemoteDocumentCacheImpl implements MemoryRemoteDocumentCache {
9999
const currentSize = this.sizer(doc);
100100

101101
this.docs = this.docs.insert(key, {
102-
document: doc,
102+
document: doc.clone(),
103103
size: currentSize,
104104
readTime
105105
});
@@ -181,7 +181,7 @@ class MemoryRemoteDocumentCacheImpl implements MemoryRemoteDocumentCache {
181181
if (readTime.compareTo(sinceReadTime) <= 0) {
182182
continue;
183183
}
184-
if (queryMatches(query, document)) {
184+
if (!queryMatches(query, document)) {
185185
continue;
186186
}
187187
results = results.insert(document.key, document);

packages/firestore/src/model/mutable_document.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ const enum DocumentType {
4545

4646
/** Describes the `hasPendingWrites` state of a document. */
4747
const enum DocumentState {
48+
/** No mutations applied. Document was sent to us by Watch. */
49+
SYNCED,
4850
/**
4951
* Local mutations applied via the mutation queue. Document is potentially
5052
* inconsistent.
@@ -54,9 +56,7 @@ const enum DocumentState {
5456
* Mutations applied based on a write acknowledgment. Document is potentially
5557
* inconsistent.
5658
*/
57-
HAS_COMMITTED_MUTATIONS,
58-
/** No mutations applied. Document was sent to us by Watch. */
59-
SYNCED
59+
HAS_COMMITTED_MUTATIONS
6060
}
6161

6262
/**
@@ -243,7 +243,9 @@ export class MutableDocument implements Document {
243243

244244
toString(): string {
245245
return (
246-
`Document(${this.key}, ${this.version}, ${this.data.toString()}, ` +
246+
`Document(${this.key}, ${this.version}, ${JSON.stringify(
247+
this.data.toProto()
248+
)}, ` +
247249
`{documentType: ${this.documentType}}), ` +
248250
`{documentState: ${this.documentState}})`
249251
);

packages/firestore/src/model/mutation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ function verifyMutationKeyMatches(
372372
* post-mutation version of SnapshotVersion.min().
373373
*/
374374
function getPostMutationVersion(document: MutableDocument): SnapshotVersion {
375-
return document.version;
375+
return document.isFoundDocument() ? document.version : SnapshotVersion.min();
376376
}
377377

378378
/**

packages/firestore/src/model/mutation_batch.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,20 +120,20 @@ export class MutationBatch {
120120
* Computes the local view for all provided documents given the mutations in
121121
* this batch.
122122
*/
123-
applyToLocalDocumentSet(documentMap: DocumentMap): DocumentMap {
123+
applyToLocalDocumentSet(documentMap: DocumentMap): void {
124124
// TODO(mrschmidt): This implementation is O(n^2). If we apply the mutations
125125
// directly (as done in `applyToLocalView()`), we can reduce the complexity
126126
// to O(n).
127127
this.mutations.forEach(m => {
128128
const document = documentMap.get(m.key)!;
129129
// TODO(mutabledocuments): This method should take a MutableDocumentMap
130130
// and we should remove this cast.
131-
this.applyToLocalView(document as MutableDocument);
131+
const mutableDocument = document as MutableDocument;
132+
this.applyToLocalView(mutableDocument);
132133
if (!document.isValidDocument()) {
133-
documentMap = documentMap.remove(m.key);
134+
mutableDocument.convertToNoDocument(SnapshotVersion.min());
134135
}
135136
});
136-
return documentMap;
137137
}
138138

139139
keys(): DocumentKeySet {

packages/firestore/test/unit/local/local_store.test.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -388,13 +388,7 @@ class LocalStoreTester {
388388
expect(this.lastChanges.size).to.equal(docs.length, 'number of changes');
389389
for (const doc of docs) {
390390
const returned = this.lastChanges.get(doc.key);
391-
expectEqual(
392-
doc,
393-
returned,
394-
`Expected '${
395-
returned ? returned.toString() : null
396-
}' to equal '${doc.toString()}'.`
397-
);
391+
expectEqual(doc, returned, `Expected '${doc}' to equal '${returned}'.`);
398392
}
399393
this.lastChanges = null;
400394
});
@@ -752,9 +746,7 @@ function genericLocalStoreTests(
752746
.after(
753747
docUpdateRemoteEvent(doc('foo/bar', 2, { foo: 'bar', it: 'base' }), [2])
754748
)
755-
.toReturnChanged(
756-
doc('foo/bar', 2, { foo: 'bar', it: 'base' }).setHasLocalMutations()
757-
)
749+
.toReturnChanged(doc('foo/bar', 2, { foo: 'bar', it: 'base' }))
758750
.toContain(doc('foo/bar', 2, { foo: 'bar', it: 'base' }))
759751
.finish();
760752
});
@@ -1391,7 +1383,10 @@ function genericLocalStoreTests(
13911383
)
13921384
.afterAcknowledgingMutation({ documentVersion: 1 })
13931385
.toReturnChanged(
1394-
doc('foo/bar', 1, { sum: 0, arrayUnion: [] }).setHasLocalMutations()
1386+
doc('foo/bar', 1, {
1387+
sum: 0,
1388+
arrayUnion: []
1389+
}).setHasCommittedMutations()
13951390
)
13961391
.afterRemoteEvent(
13971392
docAddedRemoteEvent(

packages/firestore/test/unit/local/lru_garbage_collector.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ function genericLruGarbageCollectorTests(
528528
toBeRemoved.forEach(docKey => {
529529
p = p.next(() => {
530530
return documentCache.getEntry(txn, docKey).next(maybeDoc => {
531-
expect(maybeDoc).to.be.null;
531+
expect(maybeDoc.isValidDocument()).to.be.false;
532532
});
533533
});
534534
});
@@ -837,7 +837,7 @@ function genericLruGarbageCollectorTests(
837837
p = p
838838
.next(() => documentCache.getEntry(txn, key))
839839
.next(maybeDoc => {
840-
expect(maybeDoc).to.be.null;
840+
expect(maybeDoc.isValidDocument()).to.be.false;
841841
});
842842
});
843843
expectedRetained.forEach(key => {

packages/firestore/test/unit/local/remote_document_cache.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -269,9 +269,9 @@ function genericRemoteDocumentCacheTests(
269269
cache = await cachePromise();
270270
});
271271

272-
it('returns null for document not in cache', () => {
272+
it('returns an invalid document for documents not in cache', () => {
273273
return cache.getEntry(key(DOC_PATH)).then(doc => {
274-
expect(doc).to.equal(null);
274+
expect(doc.isValidDocument()).to.be.false;
275275
});
276276
});
277277

@@ -329,7 +329,7 @@ function genericRemoteDocumentCacheTests(
329329
.then(read => {
330330
expectEqual(read.get(key1), docs[0]);
331331
expectEqual(read.get(key2), docs[1]);
332-
expect(read.get(missingKey)).to.be.null;
332+
expect(read.get(missingKey)?.isValidDocument()).to.be.false;
333333
});
334334
});
335335

@@ -343,7 +343,7 @@ function genericRemoteDocumentCacheTests(
343343
return cache.getEntry(key(DOC_PATH));
344344
})
345345
.then(read => {
346-
expect(read).to.equal(null);
346+
expect(read.isValidDocument()).to.be.false;
347347
});
348348
});
349349

packages/firestore/test/unit/local/test_remote_document_cache.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export class TestRemoteDocumentCache {
9494
);
9595
}
9696

97-
getEntry(documentKey: DocumentKey): Promise<MutableDocument | null> {
97+
getEntry(documentKey: DocumentKey): Promise<MutableDocument> {
9898
return this.persistence.runTransaction('getEntry', 'readonly', txn => {
9999
return this.cache.getEntry(txn, documentKey);
100100
});

0 commit comments

Comments
 (0)