Skip to content

Commit 7055d2a

Browse files
author
Brian Chen
authored
Iterate forward on local serializer mutation batch decoding (#2375)
1 parent 119d149 commit 7055d2a

File tree

2 files changed

+12
-26
lines changed

2 files changed

+12
-26
lines changed

firebase-firestore/src/main/java/com/google/firebase/firestore/local/LocalSerializer.java

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import com.google.firestore.v1.Write.Builder;
3535
import com.google.protobuf.ByteString;
3636
import java.util.ArrayList;
37-
import java.util.Collections;
3837
import java.util.List;
3938

4039
/** Serializer for values stored in the LocalStore. */
@@ -182,28 +181,27 @@ MutationBatch decodeMutationBatch(com.google.firebase.firestore.proto.WriteBatch
182181
// representing `transforms` with `update_transforms` on the SDK means that old `transform`
183182
// mutations stored in IndexedDB need to be updated to `update_transforms`.
184183
// TODO(b/174608374): Remove this code once we perform a schema migration.
185-
for (int i = batch.getWritesCount() - 1; i >= 0; --i) {
186-
Write mutation = batch.getWrites(i);
187-
if (mutation.hasTransform()) {
184+
for (int i = 0; i < batch.getWritesCount(); ++i) {
185+
Write currentMutation = batch.getWrites(i);
186+
boolean hasTransform =
187+
i + 1 < batch.getWritesCount() && batch.getWrites(i + 1).hasTransform();
188+
if (hasTransform) {
188189
hardAssert(
189-
i >= 1 && !batch.getWrites(i - 1).hasTransform() && batch.getWrites(i - 1).hasUpdate(),
190+
batch.getWrites(i).hasUpdate(),
190191
"TransformMutation should be preceded by a patch or set mutation");
191-
Write mutationToJoin = batch.getWrites(i - 1);
192-
Builder newMutationBuilder = Write.newBuilder(mutationToJoin);
193-
for (FieldTransform fieldTransform : mutation.getTransform().getFieldTransformsList()) {
192+
Builder newMutationBuilder = Write.newBuilder(currentMutation);
193+
Write transformMutation = batch.getWrites(i + 1);
194+
for (FieldTransform fieldTransform :
195+
transformMutation.getTransform().getFieldTransformsList()) {
194196
newMutationBuilder.addUpdateTransforms(fieldTransform);
195197
}
196198
mutations.add(rpcSerializer.decodeMutation(newMutationBuilder.build()));
197-
--i;
199+
++i;
198200
} else {
199-
mutations.add(rpcSerializer.decodeMutation(mutation));
201+
mutations.add(rpcSerializer.decodeMutation(currentMutation));
200202
}
201203
}
202204

203-
// Reverse the mutations to preserve the original ordering since the above for-loop iterates in
204-
// reverse order. We use reverse() instead of prepending the elements into the mutations array
205-
// since prepending to a List is O(n).
206-
Collections.reverse(mutations);
207205
return new MutationBatch(batchId, localWriteTime, baseMutations, mutations);
208206
}
209207

firebase-firestore/src/test/java/com/google/firebase/firestore/local/LocalSerializerTest.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -202,18 +202,6 @@ public void testTransformAndTransformThrowError() {
202202
assertThrows(AssertionError.class, () -> serializer.decodeMutationBatch(batchProto));
203203
}
204204

205-
// TODO(b/174608374): Remove these tests once we perform a schema migration.
206-
@Test
207-
public void testOnlyTransformThrowsError() {
208-
WriteBatch batchProto =
209-
com.google.firebase.firestore.proto.WriteBatch.newBuilder()
210-
.setBatchId(42)
211-
.addAllWrites(asList(transformProto))
212-
.setLocalWriteTime(writeTimeProto)
213-
.build();
214-
assertThrows(AssertionError.class, () -> serializer.decodeMutationBatch(batchProto));
215-
}
216-
217205
// TODO(b/174608374): Remove these tests once we perform a schema migration.
218206
@Test
219207
public void testDeleteAndTransformThrowError() {

0 commit comments

Comments
 (0)