19
19
import com .google .firebase .Timestamp ;
20
20
import com .google .firebase .firestore .model .Document ;
21
21
import com .google .firebase .firestore .model .DocumentKey ;
22
- import com .google .firebase .firestore .model .FieldPath ;
23
22
import com .google .firebase .firestore .model .MutableDocument ;
24
23
import com .google .firebase .firestore .model .ObjectValue ;
25
24
import com .google .firebase .firestore .model .SnapshotVersion ;
26
25
import com .google .firestore .v1 .Value ;
27
26
import java .util .ArrayList ;
28
- import java .util .HashMap ;
29
27
import java .util .List ;
30
- import java .util .Map ;
31
28
32
29
/**
33
30
* Represents a Mutation of a document. Different subclasses of Mutation will perform different
@@ -150,17 +147,14 @@ static SnapshotVersion getPostMutationVersion(MutableDocument document) {
150
147
}
151
148
152
149
/**
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.
156
151
*
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.
158
154
* @param serverTransformResults The transform results received by the server.
159
- * @return A map of fields to transform results.
160
155
*/
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 ) {
164
158
hardAssert (
165
159
fieldTransforms .size () == serverTransformResults .size (),
166
160
"server transform count (%d) should match field transform count (%d)" ,
@@ -170,42 +164,28 @@ protected Map<FieldPath, Value> serverTransformResults(
170
164
for (int i = 0 ; i < serverTransformResults .size (); i ++) {
171
165
FieldTransform fieldTransform = fieldTransforms .get (i );
172
166
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 );
182
171
}
183
- return transformResults ;
184
172
}
185
173
186
174
/**
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.
189
176
*
177
+ * @param newValue The object value to mutate.
178
+ * @param existingData The current state of the document after applying all previous mutations.
190
179
* @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.
193
180
*/
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 ) {
197
183
for (FieldTransform fieldTransform : fieldTransforms ) {
198
184
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 );
207
188
}
208
- return transformResults ;
209
189
}
210
190
211
191
public ObjectValue extractTransformBaseValue (Document document ) {
0 commit comments