Skip to content

Commit d9b945f

Browse files
author
Brian Chen
authored
Remove unused baseDoc arg and iterate fowards in local serializer (#4367)
1 parent 31f8bd5 commit d9b945f

File tree

6 files changed

+32
-108
lines changed

6 files changed

+32
-108
lines changed

packages/firestore/src/local/local_documents_view.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,6 @@ export class LocalDocumentsView {
268268
const mutatedDoc = applyMutationToLocalView(
269269
mutation,
270270
baseDoc,
271-
baseDoc,
272271
batch.localWriteTime
273272
);
274273
if (mutatedDoc instanceof Document) {

packages/firestore/src/local/local_serializer.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -201,19 +201,21 @@ export function fromDbMutationBatch(
201201
// on the SDK means that old `transform` mutations stored in IndexedDB need
202202
// to be updated to `update_transforms`.
203203
// TODO(b/174608374): Remove this code once we perform a schema migration.
204-
for (let i = dbBatch.mutations.length - 1; i >= 0; --i) {
205-
const mutationProto = dbBatch.mutations[i];
206-
if (mutationProto?.transform !== undefined) {
204+
for (let i = 0; i < dbBatch.mutations.length - 1; ++i) {
205+
const currentMutation = dbBatch.mutations[i];
206+
const hasTransform =
207+
i + 1 < dbBatch.mutations.length &&
208+
dbBatch.mutations[i + 1].transform !== undefined;
209+
if (hasTransform) {
207210
debugAssert(
208-
i >= 1 &&
209-
dbBatch.mutations[i - 1].transform === undefined &&
210-
dbBatch.mutations[i - 1].update !== undefined,
211+
dbBatch.mutations[i].transform === undefined &&
212+
dbBatch.mutations[i].update !== undefined,
211213
'TransformMutation should be preceded by a patch or set mutation'
212214
);
213-
const mutationToJoin = dbBatch.mutations[i - 1];
214-
mutationToJoin.updateTransforms = mutationProto.transform.fieldTransforms;
215-
dbBatch.mutations.splice(i, 1);
216-
--i;
215+
const transformMutation = dbBatch.mutations[i + 1];
216+
currentMutation.updateTransforms = transformMutation.transform!.fieldTransforms;
217+
dbBatch.mutations.splice(i + 1, 1);
218+
++i;
217219
}
218220
}
219221

packages/firestore/src/model/mutation.ts

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -270,9 +270,6 @@ export function applyMutationToRemoteDocument(
270270
* @param mutation - The mutation to apply.
271271
* @param maybeDoc - The document to mutate. The input document can be null if
272272
* the client has no knowledge of the pre-mutation state of the document.
273-
* @param baseDoc - The state of the document prior to this mutation batch. The
274-
* input document can be null if the client has no knowledge of the
275-
* pre-mutation state of the document.
276273
* @param localWriteTime - A timestamp indicating the local write time of the
277274
* batch this mutation is a part of.
278275
* @returns The mutated document. The returned document may be null, but only
@@ -281,25 +278,14 @@ export function applyMutationToRemoteDocument(
281278
export function applyMutationToLocalView(
282279
mutation: Mutation,
283280
maybeDoc: MaybeDocument | null,
284-
baseDoc: MaybeDocument | null,
285281
localWriteTime: Timestamp
286282
): MaybeDocument | null {
287283
verifyMutationKeyMatches(mutation, maybeDoc);
288284

289285
if (mutation instanceof SetMutation) {
290-
return applySetMutationToLocalView(
291-
mutation,
292-
maybeDoc,
293-
localWriteTime,
294-
baseDoc
295-
);
286+
return applySetMutationToLocalView(mutation, maybeDoc, localWriteTime);
296287
} else if (mutation instanceof PatchMutation) {
297-
return applyPatchMutationToLocalView(
298-
mutation,
299-
maybeDoc,
300-
localWriteTime,
301-
baseDoc
302-
);
288+
return applyPatchMutationToLocalView(mutation, maybeDoc, localWriteTime);
303289
} else {
304290
debugAssert(
305291
mutation instanceof DeleteMutation,
@@ -469,8 +455,7 @@ function applySetMutationToRemoteDocument(
469455
function applySetMutationToLocalView(
470456
mutation: SetMutation,
471457
maybeDoc: MaybeDocument | null,
472-
localWriteTime: Timestamp,
473-
baseDoc: MaybeDocument | null
458+
localWriteTime: Timestamp
474459
): MaybeDocument | null {
475460
if (!preconditionIsValidForDocument(mutation.precondition, maybeDoc)) {
476461
return maybeDoc;
@@ -480,8 +465,7 @@ function applySetMutationToLocalView(
480465
const transformResults = localTransformResults(
481466
mutation.fieldTransforms,
482467
localWriteTime,
483-
maybeDoc,
484-
baseDoc
468+
maybeDoc
485469
);
486470
newData = transformObject(
487471
mutation.fieldTransforms,
@@ -551,8 +535,7 @@ function applyPatchMutationToRemoteDocument(
551535
function applyPatchMutationToLocalView(
552536
mutation: PatchMutation,
553537
maybeDoc: MaybeDocument | null,
554-
localWriteTime: Timestamp,
555-
baseDoc: MaybeDocument | null
538+
localWriteTime: Timestamp
556539
): MaybeDocument | null {
557540
if (!preconditionIsValidForDocument(mutation.precondition, maybeDoc)) {
558541
return maybeDoc;
@@ -562,8 +545,7 @@ function applyPatchMutationToLocalView(
562545
const transformResults = localTransformResults(
563546
mutation.fieldTransforms,
564547
localWriteTime,
565-
maybeDoc,
566-
baseDoc
548+
maybeDoc
567549
);
568550
const newData = patchDocument(mutation, maybeDoc, transformResults);
569551
return new Document(mutation.key, version, newData, {
@@ -613,13 +595,14 @@ function patchObject(mutation: PatchMutation, data: ObjectValue): ObjectValue {
613595
* containing transforms has been acknowledged by the server.
614596
*
615597
* @param fieldTransforms - The field transforms to apply the result to.
616-
* @param baseDoc - The document prior to applying this mutation batch.
598+
* @param maybeDoc - The current state of the document after applying all
599+
* previous mutations.
617600
* @param serverTransformResults - The transform results received by the server.
618601
* @returns The transform results list.
619602
*/
620603
function serverTransformResults(
621604
fieldTransforms: FieldTransform[],
622-
baseDoc: MaybeDocument | null,
605+
maybeDoc: MaybeDocument | null,
623606
serverTransformResults: Array<ProtoValue | null>
624607
): ProtoValue[] {
625608
const transformResults: ProtoValue[] = [];
@@ -633,8 +616,8 @@ function serverTransformResults(
633616
const fieldTransform = fieldTransforms[i];
634617
const transform = fieldTransform.transform;
635618
let previousValue: ProtoValue | null = null;
636-
if (baseDoc instanceof Document) {
637-
previousValue = baseDoc.field(fieldTransform.field);
619+
if (maybeDoc instanceof Document) {
620+
previousValue = maybeDoc.field(fieldTransform.field);
638621
}
639622
transformResults.push(
640623
applyTransformOperationToRemoteDocument(
@@ -657,14 +640,12 @@ function serverTransformResults(
657640
* generate ServerTimestampValues).
658641
* @param maybeDoc - The current state of the document after applying all
659642
* previous mutations.
660-
* @param baseDoc - The document prior to applying this mutation batch.
661643
* @returns The transform results list.
662644
*/
663645
function localTransformResults(
664646
fieldTransforms: FieldTransform[],
665647
localWriteTime: Timestamp,
666-
maybeDoc: MaybeDocument | null,
667-
baseDoc: MaybeDocument | null
648+
maybeDoc: MaybeDocument | null
668649
): ProtoValue[] {
669650
const transformResults: ProtoValue[] = [];
670651
for (const fieldTransform of fieldTransforms) {

packages/firestore/src/model/mutation_batch.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,21 +132,17 @@ export class MutationBatch {
132132
maybeDoc = applyMutationToLocalView(
133133
mutation,
134134
maybeDoc,
135-
maybeDoc,
136135
this.localWriteTime
137136
);
138137
}
139138
}
140139

141-
const baseDoc = maybeDoc;
142-
143140
// Second, apply all user-provided mutations.
144141
for (const mutation of this.mutations) {
145142
if (mutation.key.isEqual(docKey)) {
146143
maybeDoc = applyMutationToLocalView(
147144
mutation,
148145
maybeDoc,
149-
baseDoc,
150146
this.localWriteTime
151147
);
152148
}

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

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -129,21 +129,6 @@ describe('Local Serializer', () => {
129129
);
130130
});
131131

132-
it('TransformMutation (legacy) on its own throws assertion', () => {
133-
const dbMutationBatch = new DbMutationBatch(
134-
userId,
135-
batchId,
136-
1000,
137-
[],
138-
[transformMutationWrite]
139-
);
140-
expect(() =>
141-
fromDbMutationBatch(localSerializer, dbMutationBatch)
142-
).to.throw(
143-
'TransformMutation should be preceded by a patch or set mutation'
144-
);
145-
});
146-
147132
it('DeleteMutation + TransformMutation (legacy) on its own throws assertion', () => {
148133
const dbMutationBatch = new DbMutationBatch(
149134
userId,

packages/firestore/test/unit/model/mutation.test.ts

Lines changed: 8 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ describe('Mutation', () => {
6161
const baseDoc = doc('collection/key', 0, docData);
6262

6363
const set = setMutation('collection/key', { bar: 'bar-value' });
64-
const setDoc = applyMutationToLocalView(set, baseDoc, baseDoc, timestamp);
64+
const setDoc = applyMutationToLocalView(set, baseDoc, timestamp);
6565
expect(setDoc).to.deep.equal(
6666
doc(
6767
'collection/key',
@@ -80,12 +80,7 @@ describe('Mutation', () => {
8080
'foo.bar': 'new-bar-value'
8181
});
8282

83-
const patchedDoc = applyMutationToLocalView(
84-
patch,
85-
baseDoc,
86-
baseDoc,
87-
timestamp
88-
);
83+
const patchedDoc = applyMutationToLocalView(patch, baseDoc, timestamp);
8984
expect(patchedDoc).to.deep.equal(
9085
doc(
9186
'collection/key',
@@ -106,12 +101,7 @@ describe('Mutation', () => {
106101
Precondition.none()
107102
);
108103

109-
const patchedDoc = applyMutationToLocalView(
110-
patch,
111-
baseDoc,
112-
baseDoc,
113-
timestamp
114-
);
104+
const patchedDoc = applyMutationToLocalView(patch, baseDoc, timestamp);
115105
expect(patchedDoc).to.deep.equal(
116106
doc(
117107
'collection/key',
@@ -132,12 +122,7 @@ describe('Mutation', () => {
132122
Precondition.none()
133123
);
134124

135-
const patchedDoc = applyMutationToLocalView(
136-
patch,
137-
baseDoc,
138-
baseDoc,
139-
timestamp
140-
);
125+
const patchedDoc = applyMutationToLocalView(patch, baseDoc, timestamp);
141126
expect(patchedDoc).to.deep.equal(
142127
doc(
143128
'collection/key',
@@ -156,12 +141,7 @@ describe('Mutation', () => {
156141
'foo.bar': FieldValue.delete()
157142
});
158143

159-
const patchedDoc = applyMutationToLocalView(
160-
patch,
161-
baseDoc,
162-
baseDoc,
163-
timestamp
164-
);
144+
const patchedDoc = applyMutationToLocalView(patch, baseDoc, timestamp);
165145
expect(patchedDoc).to.deep.equal(
166146
doc(
167147
'collection/key',
@@ -181,12 +161,7 @@ describe('Mutation', () => {
181161
'foo.bar': 'new-bar-value'
182162
});
183163

184-
const patchedDoc = applyMutationToLocalView(
185-
patch,
186-
baseDoc,
187-
baseDoc,
188-
timestamp
189-
);
164+
const patchedDoc = applyMutationToLocalView(patch, baseDoc, timestamp);
190165
expect(patchedDoc).to.deep.equal(
191166
doc(
192167
'collection/key',
@@ -200,12 +175,7 @@ describe('Mutation', () => {
200175
it('patching a NoDocument yields a NoDocument', () => {
201176
const baseDoc = deletedDoc('collection/key', 0);
202177
const patch = patchMutation('collection/key', { foo: 'bar' });
203-
const patchedDoc = applyMutationToLocalView(
204-
patch,
205-
baseDoc,
206-
baseDoc,
207-
timestamp
208-
);
178+
const patchedDoc = applyMutationToLocalView(patch, baseDoc, timestamp);
209179
expect(patchedDoc).to.deep.equal(baseDoc);
210180
});
211181

@@ -220,7 +190,6 @@ describe('Mutation', () => {
220190
const transformedDoc = applyMutationToLocalView(
221191
transform,
222192
baseDoc,
223-
baseDoc,
224193
timestamp
225194
);
226195

@@ -397,7 +366,6 @@ describe('Mutation', () => {
397366
transformedDoc = applyMutationToLocalView(
398367
transform,
399368
transformedDoc,
400-
transformedDoc,
401369
timestamp
402370
);
403371
}
@@ -552,12 +520,7 @@ describe('Mutation', () => {
552520
const baseDoc = doc('collection/key', 0, { foo: 'bar' });
553521

554522
const mutation = deleteMutation('collection/key');
555-
const result = applyMutationToLocalView(
556-
mutation,
557-
baseDoc,
558-
baseDoc,
559-
Timestamp.now()
560-
);
523+
const result = applyMutationToLocalView(mutation, baseDoc, Timestamp.now());
561524
expect(result).to.deep.equal(deletedDoc('collection/key', 0));
562525
});
563526

@@ -722,13 +685,11 @@ describe('Mutation', () => {
722685
let mutatedDoc = applyMutationToLocalView(
723686
transform,
724687
baseDoc,
725-
baseDoc,
726688
Timestamp.now()
727689
);
728690
mutatedDoc = applyMutationToLocalView(
729691
transform,
730692
mutatedDoc,
731-
baseDoc,
732693
Timestamp.now()
733694
);
734695

0 commit comments

Comments
 (0)