Skip to content

Commit 635d845

Browse files
Simplify Mutations
1 parent d18cc40 commit 635d845

File tree

3 files changed

+21
-50
lines changed

3 files changed

+21
-50
lines changed

firebase-firestore/src/main/java/com/google/firebase/firestore/model/mutation/Mutation.java

Lines changed: 17 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,12 @@
1919
import com.google.firebase.Timestamp;
2020
import com.google.firebase.firestore.model.Document;
2121
import com.google.firebase.firestore.model.DocumentKey;
22-
import com.google.firebase.firestore.model.FieldPath;
2322
import com.google.firebase.firestore.model.MutableDocument;
2423
import com.google.firebase.firestore.model.ObjectValue;
2524
import com.google.firebase.firestore.model.SnapshotVersion;
2625
import com.google.firestore.v1.Value;
2726
import java.util.ArrayList;
28-
import java.util.HashMap;
2927
import java.util.List;
30-
import java.util.Map;
3128

3229
/**
3330
* Represents a Mutation of a document. Different subclasses of Mutation will perform different
@@ -150,17 +147,14 @@ static SnapshotVersion getPostMutationVersion(MutableDocument document) {
150147
}
151148

152149
/**
153-
* Creates a list of "transform results" (a transform result is a field value representing the
154-
* result of applying a transform) for use after a mutation containing transforms has been
155-
* acknowledged by the server.
150+
* Applies the result of applying a transform by the backend.
156151
*
157-
* @param mutableDocument The current state of the document after applying all previous mutations.
152+
* @param newValue The object value to mutate.
153+
* @param existingData The current state of the document after applying all previous mutations.
158154
* @param serverTransformResults The transform results received by the server.
159-
* @return A map of fields to transform results.
160155
*/
161-
protected Map<FieldPath, Value> serverTransformResults(
162-
MutableDocument mutableDocument, List<Value> serverTransformResults) {
163-
Map<FieldPath, Value> transformResults = new HashMap<>(fieldTransforms.size());
156+
protected void applyServerTransformResults(
157+
ObjectValue newValue, Document existingData, List<Value> serverTransformResults) {
164158
hardAssert(
165159
fieldTransforms.size() == serverTransformResults.size(),
166160
"server transform count (%d) should match field transform count (%d)",
@@ -170,42 +164,28 @@ protected Map<FieldPath, Value> serverTransformResults(
170164
for (int i = 0; i < serverTransformResults.size(); i++) {
171165
FieldTransform fieldTransform = fieldTransforms.get(i);
172166
TransformOperation transform = fieldTransform.getOperation();
173-
174-
Value previousValue = null;
175-
if (mutableDocument.isFoundDocument()) {
176-
previousValue = mutableDocument.getField(fieldTransform.getFieldPath());
177-
}
178-
179-
transformResults.put(
180-
fieldTransform.getFieldPath(),
181-
transform.applyToRemoteDocument(previousValue, serverTransformResults.get(i)));
167+
Value previousValue = existingData.getField(fieldTransform.getFieldPath());
168+
Value transformedValue =
169+
transform.applyToRemoteDocument(previousValue, serverTransformResults.get(i));
170+
newValue.set(fieldTransform.getFieldPath(), transformedValue);
182171
}
183-
return transformResults;
184172
}
185173

186174
/**
187-
* Creates a list of "transform results" (a transform result is a field value representing the
188-
* result of applying a transform) for use when applying a transform locally.
175+
* Applies the result a transform locally.
189176
*
177+
* @param newValue The object value to mutate.
178+
* @param existingData The current state of the document after applying all previous mutations.
190179
* @param localWriteTime The local time of the mutation (used to generate ServerTimestampValues).
191-
* @param mutableDocument The current state of the document after applying all previous mutations.
192-
* @return A map of fields to transform results.
193180
*/
194-
protected Map<FieldPath, Value> localTransformResults(
195-
Timestamp localWriteTime, MutableDocument mutableDocument) {
196-
Map<FieldPath, Value> transformResults = new HashMap<>(fieldTransforms.size());
181+
protected void applyLocalTransformResults(
182+
ObjectValue newValue, Document existingData, Timestamp localWriteTime) {
197183
for (FieldTransform fieldTransform : fieldTransforms) {
198184
TransformOperation transform = fieldTransform.getOperation();
199-
200-
Value previousValue = null;
201-
if (mutableDocument.isFoundDocument()) {
202-
previousValue = mutableDocument.getField(fieldTransform.getFieldPath());
203-
}
204-
205-
transformResults.put(
206-
fieldTransform.getFieldPath(), transform.applyToLocalView(previousValue, localWriteTime));
185+
Value previousValue = existingData.getField(fieldTransform.getFieldPath());
186+
Value transformedValue = transform.applyToLocalView(previousValue, localWriteTime);
187+
newValue.set(fieldTransform.getFieldPath(), transformedValue);
207188
}
208-
return transformResults;
209189
}
210190

211191
public ObjectValue extractTransformBaseValue(Document document) {

firebase-firestore/src/main/java/com/google/firebase/firestore/model/mutation/PatchMutation.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,9 @@ public void applyToRemoteDocument(MutableDocument document, MutationResult mutat
115115
return;
116116
}
117117

118-
Map<FieldPath, Value> transformResults =
119-
serverTransformResults(document, mutationResult.getTransformResults());
120118
ObjectValue value = document.getData();
119+
applyServerTransformResults(value, document, mutationResult.getTransformResults());
121120
value.setAll(getPatch());
122-
value.setAll(transformResults);
123121
document
124122
.convertToFoundDocument(mutationResult.getVersion(), document.getData())
125123
.setHasCommittedMutations();
@@ -133,10 +131,9 @@ public void applyToLocalView(MutableDocument document, Timestamp localWriteTime)
133131
return;
134132
}
135133

136-
Map<FieldPath, Value> transformResults = localTransformResults(localWriteTime, document);
137134
ObjectValue value = document.getData();
135+
applyLocalTransformResults(value, document, localWriteTime);
138136
value.setAll(getPatch());
139-
value.setAll(transformResults);
140137
document
141138
.convertToFoundDocument(getPostMutationVersion(document), document.getData())
142139
.setHasLocalMutations();

firebase-firestore/src/main/java/com/google/firebase/firestore/model/mutation/SetMutation.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,10 @@
1616

1717
import com.google.firebase.Timestamp;
1818
import com.google.firebase.firestore.model.DocumentKey;
19-
import com.google.firebase.firestore.model.FieldPath;
2019
import com.google.firebase.firestore.model.MutableDocument;
2120
import com.google.firebase.firestore.model.ObjectValue;
22-
import com.google.firestore.v1.Value;
2321
import java.util.ArrayList;
2422
import java.util.List;
25-
import java.util.Map;
2623

2724
/**
2825
* A mutation that creates or replaces the document at the given key with the object value contents.
@@ -77,9 +74,7 @@ public void applyToRemoteDocument(MutableDocument document, MutationResult mutat
7774
// Unlike applyToLocalView, if we're applying a mutation to a remote document the server has
7875
// accepted the mutation so the precondition must have held.
7976
ObjectValue newData = value.clone();
80-
Map<FieldPath, Value> transformResults =
81-
serverTransformResults(document, mutationResult.getTransformResults());
82-
newData.setAll(transformResults);
77+
applyServerTransformResults(newData, document, mutationResult.getTransformResults());
8378
document
8479
.convertToFoundDocument(mutationResult.getVersion(), newData)
8580
.setHasCommittedMutations();
@@ -93,9 +88,8 @@ public void applyToLocalView(MutableDocument document, Timestamp localWriteTime)
9388
return;
9489
}
9590

96-
Map<FieldPath, Value> transformResults = localTransformResults(localWriteTime, document);
9791
ObjectValue localValue = value.clone();
98-
localValue.setAll(transformResults);
92+
applyLocalTransformResults(localValue, document, localWriteTime);
9993
document
10094
.convertToFoundDocument(getPostMutationVersion(document), localValue)
10195
.setHasLocalMutations();

0 commit comments

Comments
 (0)