Skip to content

Iterate forward on local serializer mutation batch decoding #2375

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import com.google.firestore.v1.Write.Builder;
import com.google.protobuf.ByteString;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/** Serializer for values stored in the LocalStore. */
Expand Down Expand Up @@ -182,28 +181,27 @@ MutationBatch decodeMutationBatch(com.google.firebase.firestore.proto.WriteBatch
// representing `transforms` with `update_transforms` on the SDK means that old `transform`
// mutations stored in IndexedDB need to be updated to `update_transforms`.
// TODO(b/174608374): Remove this code once we perform a schema migration.
for (int i = batch.getWritesCount() - 1; i >= 0; --i) {
Write mutation = batch.getWrites(i);
if (mutation.hasTransform()) {
for (int i = 0; i < batch.getWritesCount(); ++i) {
Write currentMutation = batch.getWrites(i);
boolean hasTransform =
i + 1 < batch.getWritesCount() && batch.getWrites(i + 1).hasTransform();
if (hasTransform) {
hardAssert(
i >= 1 && !batch.getWrites(i - 1).hasTransform() && batch.getWrites(i - 1).hasUpdate(),
batch.getWrites(i).hasUpdate(),
"TransformMutation should be preceded by a patch or set mutation");
Write mutationToJoin = batch.getWrites(i - 1);
Builder newMutationBuilder = Write.newBuilder(mutationToJoin);
for (FieldTransform fieldTransform : mutation.getTransform().getFieldTransformsList()) {
Builder newMutationBuilder = Write.newBuilder(currentMutation);
Write transformMutation = batch.getWrites(i + 1);
for (FieldTransform fieldTransform :
transformMutation.getTransform().getFieldTransformsList()) {
newMutationBuilder.addUpdateTransforms(fieldTransform);
}
mutations.add(rpcSerializer.decodeMutation(newMutationBuilder.build()));
--i;
++i;
} else {
mutations.add(rpcSerializer.decodeMutation(mutation));
mutations.add(rpcSerializer.decodeMutation(currentMutation));
}
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,18 +202,6 @@ public void testTransformAndTransformThrowError() {
assertThrows(AssertionError.class, () -> serializer.decodeMutationBatch(batchProto));
}

// TODO(b/174608374): Remove these tests once we perform a schema migration.
@Test
public void testOnlyTransformThrowsError() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW You could still assert that the first mutation is not a transform. Optional.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eh, I'll pass on this one since I don't want to back-back-backport it to iOS again.

WriteBatch batchProto =
com.google.firebase.firestore.proto.WriteBatch.newBuilder()
.setBatchId(42)
.addAllWrites(asList(transformProto))
.setLocalWriteTime(writeTimeProto)
.build();
assertThrows(AssertionError.class, () -> serializer.decodeMutationBatch(batchProto));
}

// TODO(b/174608374): Remove these tests once we perform a schema migration.
@Test
public void testDeleteAndTransformThrowError() {
Expand Down