Skip to content

Commit c1c3063

Browse files
Adding a dirty bit
1 parent cd3095d commit c1c3063

File tree

16 files changed

+190
-226
lines changed

16 files changed

+190
-226
lines changed

packages/firestore/src/core/transaction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export class Transaction {
3737
constructor(private datastore: Datastore) {}
3838

3939
private recordVersion(doc: MaybeDocument): void {
40-
let docVersion = doc.remoteVersion;
40+
let docVersion = doc.version;
4141
if (doc instanceof NoDocument) {
4242
// For deleted docs, we must use baseVersion 0 when we overwrite them.
4343
docVersion = SnapshotVersion.forDeletedDoc();

packages/firestore/src/local/indexeddb_schema.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -448,12 +448,12 @@ export class DbRemoteDocument {
448448
*/
449449
public document: api.Document | null,
450450
/**
451-
* For documents that were written to the remote document store based on
452-
* a write acknowledgment, we store the commit version of the mutation. This
453-
* is used to determine if the document is 'dirty' (the commit version is
454-
* higher than `document.updateTime`).
451+
* Documents that were written to the remote document store based on
452+
* a write acknowledgment are marked with `hasCommittedMutations`. These
453+
* documents are potentially inconsistent with the document's version on
454+
* the backend.
455455
*/
456-
public commitVersion?: DbTimestamp
456+
public hasCommittedMutations?: boolean
457457
) {}
458458
}
459459

packages/firestore/src/local/local_serializer.ts

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@ export class LocalSerializer {
4343
/** Decodes a remote document from storage locally to a Document. */
4444
fromDbRemoteDocument(remoteDoc: DbRemoteDocument): MaybeDocument {
4545
if (remoteDoc.document) {
46-
const commitTime = this.fromDbTimestamp(remoteDoc.commitVersion);
47-
return this.remoteSerializer.fromDocument(remoteDoc.document, commitTime);
46+
return this.remoteSerializer.fromDocument(
47+
remoteDoc.document,
48+
!!remoteDoc.hasCommittedMutations
49+
);
4850
} else if (remoteDoc.noDocument) {
4951
const key = DocumentKey.fromSegments(remoteDoc.noDocument.path);
5052
const version = this.fromDbTimestamp(remoteDoc.noDocument.readTime);
@@ -57,12 +59,12 @@ export class LocalSerializer {
5759
/** Encodes a document for storage locally. */
5860
toDbRemoteDocument(maybeDoc: MaybeDocument): DbRemoteDocument {
5961
if (maybeDoc instanceof Document) {
60-
const commitVersion = this.toDbTimestamp(maybeDoc.commitVersion);
6162
const doc = this.remoteSerializer.toDocument(maybeDoc);
62-
return new DbRemoteDocument(null, doc, commitVersion);
63+
const hasCommittedMutations = maybeDoc.hasCommittedMutations;
64+
return new DbRemoteDocument(null, doc, hasCommittedMutations);
6365
} else {
6466
const path = maybeDoc.key.path.toArray();
65-
const readTime = this.toDbTimestamp(maybeDoc.remoteVersion);
67+
const readTime = this.toDbTimestamp(maybeDoc.version);
6668
return new DbRemoteDocument(new DbNoDocument(path, readTime), null);
6769
}
6870
}
@@ -72,16 +74,12 @@ export class LocalSerializer {
7274
return new DbTimestamp(timestamp.seconds, timestamp.nanoseconds);
7375
}
7476

75-
private fromDbTimestamp(dbTimestamp?: DbTimestamp): SnapshotVersion {
76-
if (dbTimestamp) {
77-
const timestamp = new Timestamp(
78-
dbTimestamp.seconds,
79-
dbTimestamp.nanoseconds
80-
);
81-
return SnapshotVersion.fromTimestamp(timestamp);
82-
} else {
83-
return SnapshotVersion.MIN;
84-
}
77+
private fromDbTimestamp(dbTimestamp: DbTimestamp): SnapshotVersion {
78+
const timestamp = new Timestamp(
79+
dbTimestamp.seconds,
80+
dbTimestamp.nanoseconds
81+
);
82+
return SnapshotVersion.fromTimestamp(timestamp);
8583
}
8684

8785
/** Encodes a batch of mutations into a DbMutationBatch for local storage. */

packages/firestore/src/local/local_store.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -566,9 +566,9 @@ export class LocalStore {
566566
// resolution failing).
567567
if (
568568
existingDoc == null ||
569-
doc.remoteVersion.isEqual(SnapshotVersion.MIN) ||
569+
doc.version.isEqual(SnapshotVersion.MIN) ||
570570
authoritativeUpdates.has(doc.key) ||
571-
doc.remoteVersion.compareTo(existingDoc.remoteVersion) >= 0
571+
doc.version.compareTo(existingDoc.version) >= 0
572572
) {
573573
documentBuffer.addEntry(doc);
574574
} else {
@@ -577,9 +577,9 @@ export class LocalStore {
577577
'Ignoring outdated watch update for ',
578578
key,
579579
'. Current version:',
580-
existingDoc.remoteVersion,
580+
existingDoc.version,
581581
' Watch version:',
582-
doc.remoteVersion
582+
doc.version
583583
);
584584
}
585585

@@ -1011,7 +1011,7 @@ export class LocalStore {
10111011
ackVersion !== null,
10121012
'ackVersions should contain every doc in the write.'
10131013
);
1014-
if (!doc || doc.remoteVersion.compareTo(ackVersion!) < 0) {
1014+
if (!doc || doc.version.compareTo(ackVersion!) < 0) {
10151015
doc = batch.applyToRemoteDocument(docKey, doc, batchResult);
10161016
if (!doc) {
10171017
assert(

packages/firestore/src/model/document.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,16 @@ import { FieldValue, JsonObject, ObjectValue } from './field_value';
2323
import { FieldPath } from './path';
2424

2525
export interface DocumentOptions {
26-
hasLocalMutations: boolean;
26+
hasLocalMutations?: boolean;
27+
hasCommittedMutations?: boolean;
2728
}
2829

2930
/**
3031
* The result of a lookup for a given path may be an existing document or a
3132
* marker that this document does not exist at a given version.
3233
*/
3334
export abstract class MaybeDocument {
34-
constructor(
35-
readonly key: DocumentKey,
36-
readonly remoteVersion: SnapshotVersion
37-
) {}
35+
constructor(readonly key: DocumentKey, readonly version: SnapshotVersion) {}
3836

3937
static compareByKey(d1: MaybeDocument, d2: MaybeDocument): number {
4038
return DocumentKey.comparator(d1.key, d2.key);
@@ -47,16 +45,17 @@ export abstract class MaybeDocument {
4745
*/
4846
export class Document extends MaybeDocument {
4947
readonly hasLocalMutations: boolean;
48+
readonly hasCommittedMutations: boolean;
5049

5150
constructor(
5251
key: DocumentKey,
53-
remoteVersion: SnapshotVersion,
54-
readonly commitVersion: SnapshotVersion,
52+
version: SnapshotVersion,
5553
readonly data: ObjectValue,
5654
options: DocumentOptions
5755
) {
58-
super(key, remoteVersion);
59-
this.hasLocalMutations = options.hasLocalMutations;
56+
super(key, version);
57+
this.hasLocalMutations = !!options.hasLocalMutations;
58+
this.hasCommittedMutations = !!options.hasCommittedMutations;
6059
}
6160

6261
field(path: FieldPath): FieldValue | undefined {
@@ -76,17 +75,18 @@ export class Document extends MaybeDocument {
7675
return (
7776
other instanceof Document &&
7877
this.key.isEqual(other.key) &&
79-
this.remoteVersion.isEqual(other.remoteVersion) &&
80-
this.commitVersion.isEqual(other.commitVersion) &&
78+
this.version.isEqual(other.version) &&
8179
this.data.isEqual(other.data) &&
82-
this.hasLocalMutations === other.hasLocalMutations
80+
this.hasLocalMutations === other.hasLocalMutations &&
81+
this.hasCommittedMutations === other.hasCommittedMutations
8382
);
8483
}
8584

8685
toString(): string {
8786
return (
88-
`Document(${this.key}, ${this.remoteVersion}, ${this.data.toString()}, ` +
89-
`{hasLocalMutations: ${this.hasLocalMutations}})`
87+
`Document(${this.key}, ${this.version}, ${this.data.toString()}, ` +
88+
`{hasLocalMutations: ${this.hasLocalMutations}}), ` +
89+
`{hasCommittedMutations: ${this.hasCommittedMutations}})`
9090
);
9191
}
9292

@@ -107,18 +107,18 @@ export class Document extends MaybeDocument {
107107
* denotes time we know it didn't exist at.
108108
*/
109109
export class NoDocument extends MaybeDocument {
110-
constructor(key: DocumentKey, remoteVersion: SnapshotVersion) {
111-
super(key, remoteVersion);
110+
constructor(key: DocumentKey, version: SnapshotVersion) {
111+
super(key, version);
112112
}
113113

114114
toString(): string {
115-
return `NoDocument(${this.key}, ${this.remoteVersion})`;
115+
return `NoDocument(${this.key}, ${this.version})`;
116116
}
117117

118118
isEqual(other: NoDocument): boolean {
119119
return (
120120
other &&
121-
other.remoteVersion.isEqual(this.remoteVersion) &&
121+
other.version.isEqual(this.version) &&
122122
other.key.isEqual(this.key)
123123
);
124124
}

packages/firestore/src/model/mutation.ts

Lines changed: 22 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ export class Precondition {
142142
if (this.updateTime !== undefined) {
143143
return (
144144
maybeDoc instanceof Document &&
145-
maybeDoc.remoteVersion.isEqual(this.updateTime)
145+
maybeDoc.version.isEqual(this.updateTime)
146146
);
147147
} else if (this.exists !== undefined) {
148148
if (this.exists) {
@@ -265,7 +265,7 @@ export abstract class Mutation {
265265
maybeDoc: MaybeDocument | null
266266
): SnapshotVersion {
267267
if (maybeDoc instanceof Document) {
268-
return maybeDoc.remoteVersion;
268+
return maybeDoc.version;
269269
} else {
270270
return SnapshotVersion.MIN;
271271
}
@@ -302,10 +302,9 @@ export class SetMutation extends Mutation {
302302
// document the server has accepted the mutation so the precondition must
303303
// have held.
304304

305-
const remoteVersion = Mutation.getPostMutationVersion(maybeDoc);
306-
const commitVersion = mutationResult.version;
307-
return new Document(this.key, remoteVersion, commitVersion, this.value, {
308-
hasLocalMutations: false
305+
const version = mutationResult.version;
306+
return new Document(this.key, version, this.value, {
307+
hasCommittedMutations: true
309308
});
310309
}
311310

@@ -320,16 +319,10 @@ export class SetMutation extends Mutation {
320319
return maybeDoc;
321320
}
322321

323-
const remoteVersion = Mutation.getPostMutationVersion(maybeDoc);
324-
return new Document(
325-
this.key,
326-
remoteVersion,
327-
/*commitVersion=*/ SnapshotVersion.MIN,
328-
this.value,
329-
{
330-
hasLocalMutations: true
331-
}
332-
);
322+
const version = Mutation.getPostMutationVersion(maybeDoc);
323+
return new Document(this.key, version, this.value, {
324+
hasLocalMutations: true
325+
});
333326
}
334327

335328
isEqual(other: Mutation): boolean {
@@ -388,11 +381,10 @@ export class PatchMutation extends Mutation {
388381
return maybeDoc;
389382
}
390383

391-
const remoteVersion = Mutation.getPostMutationVersion(maybeDoc);
392-
const commitVersion = mutationResult.version;
384+
const version = mutationResult.version;
393385
const newData = this.patchDocument(maybeDoc);
394-
return new Document(this.key, remoteVersion, commitVersion, newData, {
395-
hasLocalMutations: false
386+
return new Document(this.key, version, newData, {
387+
hasCommittedMutations: true
396388
});
397389
}
398390

@@ -407,17 +399,11 @@ export class PatchMutation extends Mutation {
407399
return maybeDoc;
408400
}
409401

410-
const remoteVersion = Mutation.getPostMutationVersion(maybeDoc);
402+
const version = Mutation.getPostMutationVersion(maybeDoc);
411403
const newData = this.patchDocument(maybeDoc);
412-
return new Document(
413-
this.key,
414-
remoteVersion,
415-
/*commitVersion=*/ SnapshotVersion.MIN,
416-
newData,
417-
{
418-
hasLocalMutations: true
419-
}
420-
);
404+
return new Document(this.key, version, newData, {
405+
hasLocalMutations: true
406+
});
421407
}
422408

423409
isEqual(other: Mutation): boolean {
@@ -507,11 +493,10 @@ export class TransformMutation extends Mutation {
507493
maybeDoc,
508494
mutationResult.transformResults!
509495
);
510-
const remoteVersion = Mutation.getPostMutationVersion(maybeDoc);
511-
const commitVersion = mutationResult.version;
496+
const version = mutationResult.version;
512497
const newData = this.transformObject(doc.data, transformResults);
513-
return new Document(this.key, remoteVersion, commitVersion, newData, {
514-
hasLocalMutations: false
498+
return new Document(this.key, version, newData, {
499+
hasCommittedMutations: true
515500
});
516501
}
517502

@@ -532,15 +517,9 @@ export class TransformMutation extends Mutation {
532517
baseDoc
533518
);
534519
const newData = this.transformObject(doc.data, transformResults);
535-
return new Document(
536-
this.key,
537-
doc.remoteVersion,
538-
SnapshotVersion.MIN,
539-
newData,
540-
{
541-
hasLocalMutations: true
542-
}
543-
);
520+
return new Document(this.key, doc.version, newData, {
521+
hasLocalMutations: true
522+
});
544523
}
545524

546525
isEqual(other: Mutation): boolean {

0 commit comments

Comments
 (0)