Skip to content

Commit cf91ba6

Browse files
Address feedback
1 parent 7cb9e46 commit cf91ba6

16 files changed

+267
-135
lines changed

packages/firestore/src/local/indexeddb_persistence.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ 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';
7473

7574
const LOG_TAG = 'IndexedDbPersistence';
7675

@@ -1240,7 +1239,7 @@ export class IndexedDbLruDelegate implements ReferenceDelegate, LruDelegate {
12401239

12411240
return iteration
12421241
.next(() => PersistencePromise.waitFor(promises))
1243-
.next(() => changeBuffer.apply(txn, SnapshotVersion.forDeletedDoc()))
1242+
.next(() => changeBuffer.apply(txn))
12441243
.next(() => documentCount);
12451244
}
12461245

packages/firestore/src/local/indexeddb_remote_document_cache.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -424,8 +424,7 @@ export class IndexedDbRemoteDocumentCache implements RemoteDocumentCache {
424424
}
425425

426426
protected applyChanges(
427-
transaction: PersistenceTransaction,
428-
readTime: SnapshotVersion
427+
transaction: PersistenceTransaction
429428
): PersistencePromise<void> {
430429
const promises: Array<PersistencePromise<void>> = [];
431430

@@ -440,7 +439,7 @@ export class IndexedDbRemoteDocumentCache implements RemoteDocumentCache {
440439
if (maybeDocument) {
441440
const doc = this.documentCache.serializer.toDbRemoteDocument(
442441
maybeDocument,
443-
readTime
442+
this.readTime!
444443
);
445444
const size = dbDocumentSize(doc);
446445
sizeDelta += size - previousSize!;

packages/firestore/src/local/indexeddb_schema.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ export class DbTimestamp {
311311
}
312312

313313
/** A timestamp type that can be used in IndexedDb keys. */
314-
export type DbTimestampKey = number[];
314+
export type DbTimestampKey = [/* seconds */ number, /* nanos */ number];
315315

316316
// The key for the singleton object in the DbPrimaryClient is a single string.
317317
export type DbPrimaryClientKey = typeof DbPrimaryClient.key;
@@ -623,7 +623,7 @@ export class DbRemoteDocument {
623623
* An index that provides access to documents in a collection sorted by read
624624
* time.
625625
*
626-
* This index is used to allow Index-Free queries to fetch newly changed
626+
* This index is used to allow the RemoteDocumentCache to fetch newly changed
627627
* documents in a collection.
628628
*/
629629
static collectionReadTimeIndex = 'collectionReadTimeIndex';
@@ -655,11 +655,17 @@ export class DbRemoteDocument {
655655
*/
656656
public hasCommittedMutations: boolean | undefined,
657657

658-
/** When the document was read from the backend. */
658+
/**
659+
* When the document was read from the backend. Undefined for data written
660+
* prior to schema version 9.
661+
*/
659662
public readTime: DbTimestampKey | undefined,
660663

661-
/** The path of the collection this document is part of. */
662-
public parentPath: string | undefined
664+
/**
665+
* The path of the collection this document is part of. Undefined for data
666+
* written prior to schema version 9.
667+
*/
668+
public parentPath: string[] | undefined
663669
) {}
664670
}
665671

packages/firestore/src/local/local_serializer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export class LocalSerializer {
7676
readTime: SnapshotVersion
7777
): DbRemoteDocument {
7878
const dbReadTime = this.toDbTimestampKey(readTime);
79-
const parentPath = maybeDoc.key.path.popLast().canonicalString();
79+
const parentPath = maybeDoc.key.path.popLast().toArray();
8080
if (maybeDoc instanceof Document) {
8181
const doc = maybeDoc.proto
8282
? maybeDoc.proto
@@ -114,7 +114,7 @@ export class LocalSerializer {
114114
parentPath
115115
);
116116
} else {
117-
return fail('Unexpected MaybeDocumment');
117+
return fail('Unexpected MaybeDocument');
118118
}
119119
}
120120

packages/firestore/src/local/local_store.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ export class LocalStore {
353353
.next(() =>
354354
this.applyWriteToRemoteDocuments(txn, batchResult, documentBuffer)
355355
)
356-
.next(() => documentBuffer.apply(txn, batchResult.commitVersion))
356+
.next(() => documentBuffer.apply(txn))
357357
.next(() => this.mutationQueue.performConsistencyCheck(txn))
358358
.next(() => this.localDocuments.getDocuments(txn, affected));
359359
}
@@ -526,7 +526,7 @@ export class LocalStore {
526526
(doc.version.compareTo(existingDoc.version) === 0 &&
527527
existingDoc.hasPendingWrites)
528528
) {
529-
documentBuffer.addEntry(doc);
529+
documentBuffer.addEntry(doc, remoteVersion);
530530
changedDocs = changedDocs.insert(key, doc);
531531
} else if (
532532
doc instanceof NoDocument &&
@@ -586,7 +586,7 @@ export class LocalStore {
586586
}
587587

588588
return PersistencePromise.waitFor(promises)
589-
.next(() => documentBuffer.apply(txn, remoteVersion))
589+
.next(() => documentBuffer.apply(txn))
590590
.next(() => {
591591
return this.localDocuments.getLocalViewOfDocuments(
592592
txn,
@@ -867,7 +867,10 @@ export class LocalStore {
867867
' resulted in null'
868868
);
869869
} else {
870-
documentBuffer.addEntry(doc);
870+
// We use the commitVersion as the readTime rather than the
871+
// document's updateTime since the updateTime is not advanced
872+
// for updates that do not modify the underlying document.
873+
documentBuffer.addEntry(doc, batchResult.commitVersion);
871874
}
872875
}
873876
});

packages/firestore/src/local/memory_persistence.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ 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';
5352

5453
const LOG_TAG = 'MemoryPersistence';
5554

@@ -285,7 +284,7 @@ export class MemoryEagerDelegate implements ReferenceDelegate {
285284
}
286285
).next(() => {
287286
this._orphanedDocuments = null;
288-
return changeBuffer.apply(txn, SnapshotVersion.forDeletedDoc());
287+
return changeBuffer.apply(txn);
289288
});
290289
}
291290

@@ -425,9 +424,7 @@ export class MemoryLruDelegate implements ReferenceDelegate, LruDelegate {
425424
}
426425
});
427426
});
428-
return p
429-
.next(() => changeBuffer.apply(txn, SnapshotVersion.forDeletedDoc()))
430-
.next(() => count);
427+
return p.next(() => changeBuffer.apply(txn)).next(() => count);
431428
}
432429

433430
removeMutationReference(
@@ -475,7 +472,7 @@ export class MemoryLruDelegate implements ReferenceDelegate, LruDelegate {
475472
documentSize(maybeDoc: MaybeDocument): number {
476473
const remoteDocument = this.serializer.toDbRemoteDocument(
477474
maybeDoc,
478-
SnapshotVersion.forDeletedDoc()
475+
maybeDoc.version
479476
);
480477
let value: unknown;
481478
if (remoteDocument.document) {

packages/firestore/src/local/remote_document_change_buffer.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ export abstract class RemoteDocumentChangeBuffer {
4747
MaybeDocument | null
4848
> = new ObjectMap(key => key.toString());
4949

50+
protected readTime: SnapshotVersion | undefined;
51+
5052
private changesApplied = false;
5153

5254
protected abstract getFromCache(
@@ -60,8 +62,7 @@ export abstract class RemoteDocumentChangeBuffer {
6062
): PersistencePromise<NullableMaybeDocumentMap>;
6163

6264
protected abstract applyChanges(
63-
transaction: PersistenceTransaction,
64-
readTime: SnapshotVersion
65+
transaction: PersistenceTransaction
6566
): PersistencePromise<void>;
6667

6768
/**
@@ -70,8 +71,17 @@ export abstract class RemoteDocumentChangeBuffer {
7071
* You can only modify documents that have already been retrieved via
7172
* `getEntry()/getEntries()` (enforced via IndexedDbs `apply()`).
7273
*/
73-
addEntry(maybeDocument: MaybeDocument): void {
74+
addEntry(maybeDocument: MaybeDocument, readTime: SnapshotVersion): void {
7475
this.assertNotApplied();
76+
77+
// Assert that every read time matches since we only track a single read
78+
// time per document change set.
79+
assert(
80+
this.readTime === undefined || this.readTime.isEqual(readTime),
81+
'All changes in a RemoteDocumentChangeBuffer must have the same read time'
82+
);
83+
84+
this.readTime = readTime;
7585
this.changes.set(maybeDocument.key, maybeDocument);
7686
}
7787

@@ -132,13 +142,10 @@ export abstract class RemoteDocumentChangeBuffer {
132142
* Applies buffered changes to the underlying RemoteDocumentCache, using
133143
* the provided transaction.
134144
*/
135-
apply(
136-
transaction: PersistenceTransaction,
137-
readTime: SnapshotVersion
138-
): PersistencePromise<void> {
145+
apply(transaction: PersistenceTransaction): PersistencePromise<void> {
139146
this.assertNotApplied();
140147
this.changesApplied = true;
141-
return this.applyChanges(transaction, readTime);
148+
return this.applyChanges(transaction);
142149
}
143150

144151
/** Helper to assert this.changes is not null */

packages/firestore/src/local/simple_db.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,6 @@ export class SimpleDb {
108108
event.oldVersion
109109
);
110110
const db = (event.target as IDBOpenDBRequest).result;
111-
// We are provided a version upgrade transaction from the request, so
112-
// we wrap that in a SimpleDbTransaction to allow use of our friendlier
113-
// API for schema migration operations.
114111
schemaConverter
115112
.createOrUpgrade(
116113
db,

packages/firestore/test/integration/api/batch_writes.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ apiDescribe('Database batch writes', (persistence: boolean) => {
125125
});
126126

127127
it('can delete documents', () => {
128+
// TODO(#1865): This test fails with node:persistence against Prod
128129
return integrationHelpers.withTestDoc(persistence, doc => {
129130
return doc
130131
.set({ foo: 'bar' })

packages/firestore/test/integration/api/database.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ apiDescribe('Database', (persistence: boolean) => {
7070
});
7171

7272
it('can delete a document', () => {
73+
// TODO(#1865): This test fails with node:persistence against Prod
7374
return withTestDoc(persistence, docRef => {
7475
return docRef
7576
.set({ foo: 'bar' })

0 commit comments

Comments
 (0)