Skip to content

Commit 05f33ce

Browse files
author
Brian Chen
committed
WIP: last check all pass
1 parent 5dcaafd commit 05f33ce

File tree

3 files changed

+289
-48
lines changed

3 files changed

+289
-48
lines changed

Firestore/core/src/local/local_serializer.cc

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ namespace {
4848
using core::Target;
4949
using model::Document;
5050
using model::DocumentState;
51+
using model::FieldTransform;
5152
using model::FieldValue;
5253
using model::MaybeDocument;
5354
using model::Mutation;
@@ -328,11 +329,47 @@ MutationBatch LocalSerializer::DecodeMutationBatch(
328329
}
329330

330331
std::vector<Mutation> mutations;
331-
for (size_t i = 0; i < proto.writes_count; i++) {
332-
mutations.push_back(
333-
rpc_serializer_.DecodeMutation(reader, proto.writes[i]));
332+
333+
// Squash old transform mutations into existing patch of set mutations. The
334+
// replacement of representing `transforms` with `update_transforms` on the
335+
// SDK means that old `transform` mutations stored in LevelDB need to be
336+
// updated to `update_transforms`.
337+
// TODO(b/174608374): Remove this code once we perform a schema migration.
338+
for (size_t i = proto.writes_count - 1; i >= 0; --i) {
339+
_google_firestore_v1_Write mutation = proto.writes[i];
340+
if (mutation.which_operation == google_firestore_v1_Write_transform_tag) {
341+
HARD_ASSERT(
342+
i >= 1 && proto.writes[i - 1].which_operation ==
343+
google_firestore_v1_Write_update_tag,
344+
"TransformMutation should be preceded by a patch or set mutation");
345+
_google_firestore_v1_Write mutation_to_join = proto.writes[i - 1];
346+
_google_firestore_v1_Write new_mutation{mutation_to_join};
347+
new_mutation.update_transforms_count =
348+
mutation.transform.field_transforms_count;
349+
new_mutation.update_transforms =
350+
MakeArray<_google_firestore_v1_DocumentTransform_FieldTransform>(
351+
mutation.transform.field_transforms_count);
352+
for (size_t j = 0; j < mutation.transform.field_transforms_count; ++j) {
353+
new_mutation.update_transforms[j] =
354+
mutation.transform.field_transforms[j];
355+
}
356+
mutations.push_back(rpc_serializer_.DecodeMutation(reader, new_mutation));
357+
--i;
358+
} else {
359+
mutations.push_back(rpc_serializer_.DecodeMutation(reader, mutation));
360+
}
361+
362+
// TODO: figure out how to exit the loop without size_t looping around
363+
if (i == 0) {
364+
break;
365+
}
334366
}
335367

368+
// Reverse the mutations to preserve the original ordering since the above
369+
// for-loop iterates in reverse order. We use reverse() instead of prepending
370+
// the elements into the mutations array since prepending to a List is O(n).
371+
std::reverse(mutations.begin(), mutations.end());
372+
336373
return MutationBatch(batch_id, local_write_time, std::move(base_mutations),
337374
std::move(mutations));
338375
}

Firestore/core/src/remote/serializer.cc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -591,11 +591,9 @@ google_firestore_v1_Write Serializer::EncodeMutation(
591591
result.update_transforms =
592592
MakeArray<google_firestore_v1_DocumentTransform_FieldTransform>(count);
593593
int i = 0;
594-
auto crazy = mutation.field_transforms();
595-
auto crazy2 = mutation.key();
596594
for (const FieldTransform& field_transform : mutation.field_transforms()) {
597595
result.update_transforms[i] = EncodeFieldTransform(field_transform);
598-
i++;
596+
++i;
599597
}
600598

601599
switch (mutation.type()) {

0 commit comments

Comments
 (0)