-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Replace usage of transforms with update transforms #7276
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
Conversation
11e07ae
to
05f33ce
Compare
05f33ce
to
a0e7d50
Compare
6b64d5e
to
78a79e5
Compare
78a79e5
to
edc3f47
Compare
edc3f47
to
7257be4
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am surprised by how pretty this looks. Thanks for doing this!
for (size_t i = proto.writes_count; i > 0; --i) { | ||
// Calculate appropriate index from `i`. We do this because decrementing | ||
// `i` when i == 0 results in wraparound. | ||
size_t index = i - 1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you use a more specific name? "i" and "index" both mean the same thing. Also maybe "underflow" instead of "wraparound?"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
N/A now since we're iterating in the normal direction.
// 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). | ||
std::reverse(mutations.begin(), mutations.end()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could probably get rid of the underflow issue and the reversing if we flipped the order of iterations and looked ahead in line 344 (index +1 < proto.writes_count && which_operation ...
).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Big brain suggestion!! Done, and removed the test with only a transformMutation as well.
for (size_t j = 0; j < mutation.transform.field_transforms_count; ++j) { | ||
new_mutation.update_transforms[j] = | ||
mutation.transform.field_transforms[j]; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know the answer here - but could we just assign the old array to the new location?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
Firestore/core/src/model/mutation.cc
Outdated
for (const FieldTransform& transform : field_transforms_) { | ||
absl::optional<FieldValue> existing_value; | ||
if (maybe_doc && maybe_doc->is_document()) { | ||
existing_value = Document(*maybe_doc).field(transform.path()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This creates a new Document for every iteration. We should probably do this outside of th eloop.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.
Firestore/core/src/model/mutation.h
Outdated
* @return The transform results array. | ||
*/ | ||
virtual std::vector<FieldValue> ServerTransformResults( | ||
const absl::optional<MaybeDocument>& base_doc, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that we removed base_doc
from LocalTransformResults, should this be maybe_doc
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, done.
EXPECT_EQ(google_firestore_v1_Write_update_tag, encoded.which_operation); | ||
EXPECT_EQ(2, encoded.update.fields_count); | ||
EXPECT_EQ("a", nanopb::MakeString(encoded.update.fields[0].key)); | ||
EXPECT_EQ("b", | ||
nanopb::MakeString(encoded.update.fields[0].value.string_value)); | ||
EXPECT_EQ("num", nanopb::MakeString(encoded.update.fields[1].key)); | ||
EXPECT_EQ(1, encoded.update.fields[1].value.integer_value); | ||
EXPECT_FALSE(encoded.has_update_mask); | ||
EXPECT_FALSE(encoded.has_current_document); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fun :/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for your help in helping me debug this earlier!
// WriteMutations({ | ||
// testutil::PatchMutation("foo/bar", Map(), {}), | ||
// testutil::PatchMutation("foo/bar", Map(), | ||
// {testutil::Increment("sum", Value(1))}), | ||
// }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh no! Such a great PR, ruined by this careless mistake. I guess I gotta press 'Request Changes' now :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
aw shucks I was so close
|
||
ExpectRoundTrip(model, proto); | ||
PatchMutation patch_model = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test could be split in two. Both verification chains are mostly independent and smaller tests are usually better. You might also be able to create some helper methods for some of the state manipulation that is duplicated in this test and the test below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I kept them together in order to keep parity with Web/Android. There aren't that many helpers I could pull, and these two tests don't share much else in common with the other tests in this file, which would end up making the tests harder to read.
|
||
ExpectRoundTrip(model, proto); | ||
PatchMutation patch_model = testutil::PatchMutation( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comment as above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks!
Firestore/core/src/model/mutation.h
Outdated
* @return The transform results array. | ||
*/ | ||
virtual std::vector<FieldValue> ServerTransformResults( | ||
const absl::optional<MaybeDocument>& base_doc, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, done.
// WriteMutations({ | ||
// testutil::PatchMutation("foo/bar", Map(), {}), | ||
// testutil::PatchMutation("foo/bar", Map(), | ||
// {testutil::Increment("sum", Value(1))}), | ||
// }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
aw shucks I was so close
Firestore/core/src/model/mutation.cc
Outdated
for (const FieldTransform& transform : field_transforms_) { | ||
absl::optional<FieldValue> existing_value; | ||
if (maybe_doc && maybe_doc->is_document()) { | ||
existing_value = Document(*maybe_doc).field(transform.path()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.
EXPECT_EQ(google_firestore_v1_Write_update_tag, encoded.which_operation); | ||
EXPECT_EQ(2, encoded.update.fields_count); | ||
EXPECT_EQ("a", nanopb::MakeString(encoded.update.fields[0].key)); | ||
EXPECT_EQ("b", | ||
nanopb::MakeString(encoded.update.fields[0].value.string_value)); | ||
EXPECT_EQ("num", nanopb::MakeString(encoded.update.fields[1].key)); | ||
EXPECT_EQ(1, encoded.update.fields[1].value.integer_value); | ||
EXPECT_FALSE(encoded.has_update_mask); | ||
EXPECT_FALSE(encoded.has_current_document); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for your help in helping me debug this earlier!
// 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). | ||
std::reverse(mutations.begin(), mutations.end()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Big brain suggestion!! Done, and removed the test with only a transformMutation as well.
for (size_t j = 0; j < mutation.transform.field_transforms_count; ++j) { | ||
new_mutation.update_transforms[j] = | ||
mutation.transform.field_transforms[j]; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
for (size_t i = proto.writes_count; i > 0; --i) { | ||
// Calculate appropriate index from `i`. We do this because decrementing | ||
// `i` when i == 0 results in wraparound. | ||
size_t index = i - 1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
N/A now since we're iterating in the normal direction.
|
||
ExpectRoundTrip(model, proto); | ||
PatchMutation patch_model = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I kept them together in order to keep parity with Web/Android. There aren't that many helpers I could pull, and these two tests don't share much else in common with the other tests in this file, which would end up making the tests harder to read.
new_mutation.update_transforms = | ||
MakeArray<_google_firestore_v1_DocumentTransform_FieldTransform>( | ||
transform_mutation.transform.field_transforms_count); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we still need this call?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed.
Porting from web and Android.
First commit contains initial port changes. Second commit changes contains code removal for
localTransformResults
, which was done in Android and backported to web.