Skip to content

Commit 500b555

Browse files
Review feedback
1 parent 41cf21b commit 500b555

10 files changed

+57
-37
lines changed

packages/firestore/src/local/indexeddb_persistence.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ import { QueryData } from './query_data';
7070
import { ReferenceSet } from './reference_set';
7171
import { ClientId } from './shared_client_state';
7272
import { SimpleDb, SimpleDbStore, SimpleDbTransaction } from './simple_db';
73+
import { SnapshotVersion } from '../core/snapshot_version';
7374

7475
const LOG_TAG = 'IndexedDbPersistence';
7576

@@ -1224,7 +1225,10 @@ export class IndexedDbLruDelegate implements ReferenceDelegate, LruDelegate {
12241225
// Our size accounting requires us to read all documents before
12251226
// removing them.
12261227
return changeBuffer.getEntry(txn, docKey).next(() => {
1227-
changeBuffer.removeEntry(docKey);
1228+
changeBuffer.removeEntry(
1229+
docKey,
1230+
SnapshotVersion.forDeletedDoc()
1231+
);
12281232
return documentTargetStore(txn).delete(sentinelKey(docKey));
12291233
});
12301234
}

packages/firestore/src/local/indexeddb_remote_document_cache.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ export class IndexedDbRemoteDocumentCache implements RemoteDocumentCache {
442442
if (maybeDocument) {
443443
const doc = this.documentCache.serializer.toDbRemoteDocument(
444444
maybeDocument,
445-
this.readTime!
445+
this.readTime
446446
);
447447
const size = dbDocumentSize(doc);
448448
sizeDelta += size - previousSize!;
@@ -456,7 +456,7 @@ export class IndexedDbRemoteDocumentCache implements RemoteDocumentCache {
456456
// preserved in `getNewDocumentChanges()`.
457457
const deletedDoc = this.documentCache.serializer.toDbRemoteDocument(
458458
new NoDocument(key, SnapshotVersion.forDeletedDoc()),
459-
this.readTime!
459+
this.readTime
460460
);
461461
promises.push(
462462
this.documentCache.addEntry(transaction, key, deletedDoc)

packages/firestore/src/local/local_store.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ export class LocalStore {
535535
// NoDocuments with SnapshotVersion.MIN are used in manufactured events (e.g. in the
536536
// case of a limbo document resolution failing). We remove these documents from cache
537537
// since we lost access.
538-
documentBuffer.removeEntry(key);
538+
documentBuffer.removeEntry(key, remoteVersion);
539539
changedDocs = changedDocs.insert(key, doc);
540540
} else {
541541
log.debug(

packages/firestore/src/local/memory_persistence.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import { PersistencePromise } from './persistence_promise';
4949
import { QueryData } from './query_data';
5050
import { ReferenceSet } from './reference_set';
5151
import { ClientId } from './shared_client_state';
52+
import { SnapshotVersion } from '../core/snapshot_version';
5253

5354
const LOG_TAG = 'MemoryPersistence';
5455

@@ -276,7 +277,7 @@ export class MemoryEagerDelegate implements ReferenceDelegate {
276277
(key: DocumentKey) => {
277278
return this.isReferenced(txn, key).next(isReferenced => {
278279
if (!isReferenced) {
279-
changeBuffer.removeEntry(key);
280+
changeBuffer.removeEntry(key, SnapshotVersion.forDeletedDoc());
280281
}
281282
});
282283
}
@@ -416,7 +417,7 @@ export class MemoryLruDelegate implements ReferenceDelegate, LruDelegate {
416417
return this.isPinned(txn, key, upperBound).next(isPinned => {
417418
if (!isPinned) {
418419
count++;
419-
changeBuffer.removeEntry(key);
420+
changeBuffer.removeEntry(key, SnapshotVersion.forDeletedDoc());
420421
}
421422
});
422423
});

packages/firestore/src/local/remote_document_change_buffer.ts

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export abstract class RemoteDocumentChangeBuffer {
4848
> = new ObjectMap(key => key.toString());
4949

5050
// The read time to use for all added documents in this change buffer.
51-
protected readTime: SnapshotVersion | undefined;
51+
private _readTime: SnapshotVersion | undefined;
5252

5353
private changesApplied = false;
5454

@@ -66,6 +66,24 @@ export abstract class RemoteDocumentChangeBuffer {
6666
transaction: PersistenceTransaction
6767
): PersistencePromise<void>;
6868

69+
protected set readTime(value: SnapshotVersion) {
70+
// Right now (for simplicity) we just track a single readTime for all the
71+
// added entries since we expect them to all be the same, but we could
72+
// rework to store per-entry readTimes if necessary.
73+
assert(
74+
this._readTime === undefined || this._readTime.isEqual(value),
75+
'All changes in a RemoteDocumentChangeBuffer must have the same read time'
76+
);
77+
this._readTime = value;
78+
}
79+
80+
protected get readTime(): SnapshotVersion {
81+
assert(
82+
this._readTime !== undefined,
83+
'Read time is not set. Did you call addEntry/removeEntry?'
84+
);
85+
return this._readTime!;
86+
}
6987
/**
7088
* Buffers a `RemoteDocumentCache.addEntry()` call.
7189
*
@@ -74,15 +92,6 @@ export abstract class RemoteDocumentChangeBuffer {
7492
*/
7593
addEntry(maybeDocument: MaybeDocument, readTime: SnapshotVersion): void {
7694
this.assertNotApplied();
77-
78-
// Right now (for simplicity) we just track a single readTime for all the
79-
// added entries since we expect them to all be the same, but we could
80-
// rework to store per-entry readTimes if necessary.
81-
assert(
82-
this.readTime === undefined || this.readTime.isEqual(readTime),
83-
'All changes in a RemoteDocumentChangeBuffer must have the same read time'
84-
);
85-
8695
this.readTime = readTime;
8796
this.changes.set(maybeDocument.key, maybeDocument);
8897
}
@@ -93,8 +102,9 @@ export abstract class RemoteDocumentChangeBuffer {
93102
* You can only remove documents that have already been retrieved via
94103
* `getEntry()/getEntries()` (enforced via IndexedDbs `apply()`).
95104
*/
96-
removeEntry(key: DocumentKey): void {
105+
removeEntry(key: DocumentKey, readTime: SnapshotVersion): void {
97106
this.assertNotApplied();
107+
this.readTime = readTime;
98108
this.changes.set(key, null);
99109
}
100110

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -821,8 +821,8 @@ describe('IndexedDbSchema: createOrUpgradeDb', () => {
821821
});
822822

823823
it('can get recent document changes in a collection', async () => {
824-
const oldDocPaths = ['coll/doc1', 'coll/doc2', 'ignored/doc1'];
825-
const newDocPaths = ['coll/doc3', 'coll/doc4', 'ignored/doc2'];
824+
const oldDocPaths = ['coll/doc1', 'coll/doc2', 'abc/doc1'];
825+
const newDocPaths = ['coll/doc3', 'coll/doc4', 'abc/doc2'];
826826

827827
await withDb(9, db => {
828828
const sdb = new SimpleDb(db);

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,12 @@ function eagerRemoteDocumentCacheTests(
133133
const totalSize = await cache.getSize();
134134
expect(totalSize).to.equal(0);
135135

136-
await cache.removeEntry(doc2.key);
136+
await cache.removeEntry(doc2.key, version(3));
137137

138138
const currentSize = await cache.getSize();
139139
expect(currentSize).to.equal(0);
140140

141-
await cache.removeEntry(doc1.key);
141+
await cache.removeEntry(doc1.key, version(4));
142142

143143
const finalSize = await cache.getSize();
144144
expect(finalSize).to.equal(0);
@@ -171,12 +171,12 @@ function lruRemoteDocumentCacheTests(
171171
const totalSize = await cache.getSize();
172172
expect(totalSize).to.be.greaterThan(doc1Size);
173173

174-
await cache.removeEntry(doc2.key);
174+
await cache.removeEntry(doc2.key, version(3));
175175

176176
const currentSize = await cache.getSize();
177177
expect(currentSize).to.equal(doc1Size);
178178

179-
await cache.removeEntry(doc1.key);
179+
await cache.removeEntry(doc1.key, version(4));
180180

181181
const finalSize = await cache.getSize();
182182
expect(finalSize).to.equal(0);
@@ -282,7 +282,7 @@ function genericRemoteDocumentCacheTests(
282282
return cache
283283
.addEntry(doc(DOC_PATH, VERSION, DOC_DATA))
284284
.then(() => {
285-
return cache.removeEntry(key(DOC_PATH));
285+
return cache.removeEntry(key(DOC_PATH), version(VERSION));
286286
})
287287
.then(() => {
288288
return cache.getEntry(key(DOC_PATH));
@@ -294,7 +294,7 @@ function genericRemoteDocumentCacheTests(
294294

295295
it('can remove nonexistent document', () => {
296296
// no-op, but make sure it doesn't fail.
297-
return cache.removeEntry(key(DOC_PATH));
297+
return cache.removeEntry(key(DOC_PATH), version(1));
298298
});
299299

300300
it('can get documents matching query', async () => {
@@ -341,9 +341,9 @@ function genericRemoteDocumentCacheTests(
341341
changedDocs
342342
);
343343

344-
await cache.addEntry(doc('c/1', 3, DOC_DATA));
344+
await cache.addEntry(doc('c/1', 4, DOC_DATA));
345345
changedDocs = await cache.getNewDocumentChanges();
346-
assertMatches([doc('c/1', 3, DOC_DATA)], changedDocs);
346+
assertMatches([doc('c/1', 4, DOC_DATA)], changedDocs);
347347
});
348348

349349
it('can get empty changes', async () => {
@@ -360,7 +360,7 @@ function genericRemoteDocumentCacheTests(
360360
],
361361
version(3)
362362
);
363-
await cache.removeEntry(key('a/2'));
363+
await cache.removeEntry(key('a/2'), version(4));
364364

365365
const changedDocs = await cache.getNewDocumentChanges();
366366
assertMatches(

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ describe('RemoteDocumentChangeBuffer', () => {
7373
const newADoc = doc('coll/a', 43, { new: 'data' });
7474
buffer.addEntry(newADoc, newADoc.version);
7575
expect(await buffer.getEntry(key('coll/a'))).to.not.be.null;
76-
buffer.removeEntry(newADoc.key);
76+
buffer.removeEntry(newADoc.key, newADoc.version);
7777
expect(await buffer.getEntry(key('coll/a'))).to.be.null;
7878
});
7979

@@ -94,7 +94,7 @@ describe('RemoteDocumentChangeBuffer', () => {
9494

9595
it('can apply changes with removal', async () => {
9696
expectEqual(await buffer.getEntry(key('coll/a')), INITIAL_DOC);
97-
buffer.removeEntry(INITIAL_DOC.key);
97+
buffer.removeEntry(INITIAL_DOC.key, INITIAL_DOC.version);
9898
// Reading directly against the cache should still yield the old result.
9999
expectEqual(await cache.getEntry(key('coll/a')), INITIAL_DOC);
100100

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,17 @@ export class TestRemoteDocumentCache {
7676
return this.addEntries([maybeDocument], maybeDocument.version);
7777
}
7878

79-
removeEntry(documentKey: DocumentKey): Promise<void> {
79+
removeEntry(
80+
documentKey: DocumentKey,
81+
readTime: SnapshotVersion
82+
): Promise<void> {
8083
return this.persistence.runTransaction(
8184
'removeEntry',
8285
'readwrite-primary',
8386
txn => {
84-
const changeBuffer = this.newChangeBuffer();
87+
const changeBuffer = this.newChangeBuffer({ trackRemovals: true });
8588
return changeBuffer.getEntry(txn, documentKey).next(() => {
86-
changeBuffer.removeEntry(documentKey);
89+
changeBuffer.removeEntry(documentKey, readTime);
8790
return changeBuffer.apply(txn);
8891
});
8992
}
@@ -128,7 +131,9 @@ export class TestRemoteDocumentCache {
128131
);
129132
}
130133

131-
newChangeBuffer(): RemoteDocumentChangeBuffer {
132-
return this.cache.newChangeBuffer();
134+
newChangeBuffer(options?: {
135+
trackRemovals: boolean;
136+
}): RemoteDocumentChangeBuffer {
137+
return this.cache.newChangeBuffer(options);
133138
}
134139
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ export class TestRemoteDocumentChangeBuffer {
3535
this.buffer.addEntry(maybeDocument, readTime);
3636
}
3737

38-
removeEntry(key: DocumentKey): void {
39-
this.buffer.removeEntry(key);
38+
removeEntry(key: DocumentKey, readTime: SnapshotVersion): void {
39+
this.buffer.removeEntry(key, readTime);
4040
}
4141

4242
getEntry(documentKey: DocumentKey): Promise<MaybeDocument | null> {

0 commit comments

Comments
 (0)