Skip to content

Commit ad2d178

Browse files
Remove Held Write Acks (#1156)
1 parent d99c197 commit ad2d178

39 files changed

+1104
-790
lines changed

packages/firestore/src/api/database.ts

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -593,11 +593,27 @@ export class Transaction implements firestore.Transaction {
593593
}
594594
const doc = docs[0];
595595
if (doc instanceof NoDocument) {
596-
return new DocumentSnapshot(this._firestore, ref._key, null, false);
596+
return new DocumentSnapshot(
597+
this._firestore,
598+
ref._key,
599+
null,
600+
/* fromCache= */ false,
601+
/* hasPendingWrites= */ false
602+
);
597603
} else if (doc instanceof Document) {
598-
return new DocumentSnapshot(this._firestore, ref._key, doc, false);
604+
return new DocumentSnapshot(
605+
this._firestore,
606+
ref._key,
607+
doc,
608+
/* fromCache= */ false,
609+
/* hasPendingWrites= */ false
610+
);
599611
} else {
600-
throw fail('MaybeDocument is neither Document nor NoDocument');
612+
throw fail(
613+
`BatchGetDocumentsRequest returned unexpected document type: ${
614+
doc.constructor.name
615+
}`
616+
);
601617
}
602618
});
603619
}
@@ -1040,7 +1056,8 @@ export class DocumentReference implements firestore.DocumentReference {
10401056
this.firestore,
10411057
this._key,
10421058
doc,
1043-
snapshot.fromCache
1059+
snapshot.fromCache,
1060+
snapshot.hasPendingWrites
10441061
)
10451062
);
10461063
}
@@ -1082,7 +1099,8 @@ export class DocumentReference implements firestore.DocumentReference {
10821099
this.firestore,
10831100
this._key,
10841101
doc,
1085-
/*fromCache=*/ true
1102+
/*fromCache=*/ true,
1103+
doc instanceof Document ? doc.hasLocalMutations : false
10861104
)
10871105
);
10881106
}, reject);
@@ -1173,7 +1191,8 @@ export class DocumentSnapshot implements firestore.DocumentSnapshot {
11731191
private _firestore: Firestore,
11741192
private _key: DocumentKey,
11751193
public _document: Document | null,
1176-
private _fromCache: boolean
1194+
private _fromCache: boolean,
1195+
private _hasPendingWrites: boolean
11771196
) {}
11781197

11791198
data(
@@ -1228,10 +1247,7 @@ export class DocumentSnapshot implements firestore.DocumentSnapshot {
12281247
}
12291248

12301249
get metadata(): firestore.SnapshotMetadata {
1231-
return new SnapshotMetadata(
1232-
this._document !== null && this._document.hasLocalMutations,
1233-
this._fromCache
1234-
);
1250+
return new SnapshotMetadata(this._hasPendingWrites, this._fromCache);
12351251
}
12361252

12371253
isEqual(other: firestore.DocumentSnapshot): boolean {
@@ -1299,9 +1315,10 @@ export class QueryDocumentSnapshot extends DocumentSnapshot
12991315
firestore: Firestore,
13001316
key: DocumentKey,
13011317
document: Document,
1302-
fromCache: boolean
1318+
fromCache: boolean,
1319+
hasPendingWrites: boolean
13031320
) {
1304-
super(firestore, key, document, fromCache);
1321+
super(firestore, key, document, fromCache, hasPendingWrites);
13051322
}
13061323

13071324
data(options?: SnapshotOptions): firestore.DocumentData {
@@ -1945,7 +1962,8 @@ export class QuerySnapshot implements firestore.QuerySnapshot {
19451962
this._firestore,
19461963
doc.key,
19471964
doc,
1948-
this.metadata.fromCache
1965+
this.metadata.fromCache,
1966+
this._snapshot.mutatedKeys.has(doc.key)
19491967
);
19501968
}
19511969
}
@@ -2133,7 +2151,8 @@ export function changesFromSnapshot(
21332151
firestore,
21342152
change.doc.key,
21352153
change.doc,
2136-
snapshot.fromCache
2154+
snapshot.fromCache,
2155+
snapshot.mutatedKeys.has(change.doc.key)
21372156
);
21382157
assert(
21392158
change.type === ChangeType.Added,
@@ -2164,7 +2183,8 @@ export function changesFromSnapshot(
21642183
firestore,
21652184
change.doc.key,
21662185
change.doc,
2167-
snapshot.fromCache
2186+
snapshot.fromCache,
2187+
snapshot.mutatedKeys.has(change.doc.key)
21682188
);
21692189
let oldIndex = -1;
21702190
let newIndex = -1;

packages/firestore/src/core/event_manager.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,8 @@ export class QueryListener {
195195
snap.docs,
196196
snap.oldDocs,
197197
docChanges,
198+
snap.mutatedKeys,
198199
snap.fromCache,
199-
snap.hasPendingWrites,
200200
snap.syncStateChanged,
201201
/* excludesMetadataChanges= */ true
202202
);
@@ -288,8 +288,8 @@ export class QueryListener {
288288
snap = ViewSnapshot.fromInitialDocuments(
289289
snap.query,
290290
snap.docs,
291-
snap.fromCache,
292-
snap.hasPendingWrites
291+
snap.mutatedKeys,
292+
snap.fromCache
293293
);
294294
this.raisedInitialEvent = true;
295295
this.queryObserver.next(snap);

packages/firestore/src/core/firestore_client.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,8 +366,7 @@ export class FirestoreClient {
366366
this.localStore = new LocalStore(
367367
this.persistence,
368368
user,
369-
this.garbageCollector,
370-
this.sharedClientState
369+
this.garbageCollector
371370
);
372371
const serializer = this.platform.newSerializer(
373372
this.databaseInfo.databaseId

packages/firestore/src/core/sync_engine.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
245245
.remoteDocumentKeys(queryData.targetId)
246246
.then(remoteKeys => {
247247
const view = new View(query, remoteKeys);
248-
const viewDocChanges = view.computeDocChanges(docs);
248+
const viewDocChanges = view.computeInitialChanges(docs);
249249
const synthesizedTargetChange = TargetChange.createSynthesizedTargetChangeForCurrentChange(
250250
queryData.targetId,
251251
current && this.onlineState !== OnlineState.Offline
@@ -606,6 +606,7 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
606606
return this.localStore
607607
.acknowledgeBatch(mutationBatchResult)
608608
.then(changes => {
609+
this.sharedClientState.updateMutationState(batchId, 'acknowledged');
609610
return this.emitNewSnapsAndNotifyLocalStore(changes);
610611
})
611612
.catch(err => this.ignoreIfPrimaryLeaseLoss(err));

packages/firestore/src/core/transaction.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@
1717
import { ParsedSetData, ParsedUpdateData } from '../api/user_data_converter';
1818
import { SnapshotVersion } from './snapshot_version';
1919
import { documentVersionMap } from '../model/collections';
20-
import { NoDocument } from '../model/document';
20+
import { NoDocument, Document } from '../model/document';
2121
import { MaybeDocument } from '../model/document';
2222
import { DocumentKey } from '../model/document_key';
2323
import { DeleteMutation, Mutation, Precondition } from '../model/mutation';
2424
import { Datastore } from '../remote/datastore';
2525
import { Code, FirestoreError } from '../util/error';
26+
import { fail } from '../util/assert';
2627

2728
/**
2829
* Internal transaction object responsible for accumulating the mutations to
@@ -37,11 +38,17 @@ export class Transaction {
3738
constructor(private datastore: Datastore) {}
3839

3940
private recordVersion(doc: MaybeDocument): void {
40-
let docVersion = doc.version;
41-
if (doc instanceof NoDocument) {
41+
let docVersion: SnapshotVersion;
42+
43+
if (doc instanceof Document) {
44+
docVersion = doc.version;
45+
} else if (doc instanceof NoDocument) {
4246
// For deleted docs, we must use baseVersion 0 when we overwrite them.
4347
docVersion = SnapshotVersion.forDeletedDoc();
48+
} else {
49+
throw fail('Document in a transaction was a ' + doc.constructor.name);
4450
}
51+
4552
const existingVersion = this.readVersions.get(doc.key);
4653
if (existingVersion !== null) {
4754
if (!docVersion.isEqual(existingVersion)) {
@@ -68,7 +75,13 @@ export class Transaction {
6875
);
6976
}
7077
return this.datastore.lookup(keys).then(docs => {
71-
docs.forEach(doc => this.recordVersion(doc));
78+
docs.forEach(doc => {
79+
if (doc instanceof NoDocument || doc instanceof Document) {
80+
this.recordVersion(doc);
81+
} else {
82+
fail('Document in a transaction was a ' + doc.constructor.name);
83+
}
84+
});
7285
return docs;
7386
});
7487
}

0 commit comments

Comments
 (0)