Skip to content

Post-Merge cleanup of MutableDocuments #2468

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 5 commits into from
Feb 26, 2021
Merged

Conversation

schmidt-sebastian
Copy link
Contributor

Some small fixes to MutableDocuments that I noticed while porting to JS (more likely coming). I also added the often asked for factory functions since it helps with readability (otherwise the reader does not know what new MutableDocument means).

@google-oss-bot
Copy link
Contributor

google-oss-bot commented Feb 26, 2021

Coverage Report

Affected SDKs

  • firebase-firestore

    SDK overall coverage changed from 48.73% (979000c) to 48.73% (0952cb9c) by +0.00%.

    Filename Base (979000c) Head (0952cb9c) Diff
    LocalSerializer.java 97.58% 97.60% +0.02%
    MutableDocument.java 98.31% 98.44% +0.13%
    MutationBatch.java 88.89% 88.33% -0.56%
    SyncEngine.java 93.59% 93.57% -0.02%
    WatchChangeAggregator.java 98.19% 98.18% -0.01%

Test Logs

Notes

HTML coverage reports can be produced locally with ./gradlew <product>:checkCoverage.
Report files are located at <product-build-dir>/reports/jacoco/.

Head commit (0952cb9c) is created by Prow via merging commits: 979000c a27fe2c.

@google-oss-bot
Copy link
Contributor

google-oss-bot commented Feb 26, 2021

Binary Size Report

Affected SDKs

  • firebase-firestore

    Type Base (979000c) Head (0952cb9c) Diff
    aar 1.10 MB 1.10 MB +25 B (+0.0%)
    apk (release) 3.19 MB 3.19 MB -80 B (-0.0%)
  • firebase-firestore-ktx

    Type Base (979000c) Head (0952cb9c) Diff
    apk (release) 3.67 MB 3.67 MB -260 B (-0.0%)

Test Logs

Notes

Head commit (0952cb9c) is created by Prow via merging commits: 979000c a27fe2c.

Copy link
Contributor

@dconeybe dconeybe left a comment

Choose a reason for hiding this comment

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

♥ the new factory methods!

@@ -17,6 +17,15 @@
import androidx.annotation.NonNull;
import com.google.firestore.v1.Value;

/**
* Represents a document in Firestore with a key, version, data and whether it has local mutations
* applied to it. Documents can start out as {@link DocumentType#INVALID} and transition to a valid
Copy link
Contributor

Choose a reason for hiding this comment

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

"Documents can start out as..." is not as relevant now since factory methods create the object directly into the desired state. Also, DocumentType is a private implementation detail that should not be mentioned in the javadocs for the class. This entire paragraph about starting in an invalid state and transitioning to a different state can probably just be deleted.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated the comment.

* mutations.
*/
public static MutableDocument newInvalidDocument(DocumentKey documentKey) {
return new MutableDocument(
Copy link
Contributor

Choose a reason for hiding this comment

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

These factory methods appear to duplicate the logic defined in their corresponding convertToXXX() methods. To be a bit more "dry" (go/dry), consider reviving the constructor that takes the DocumentKey, changing its visibility to private, using that constructor, and then calling the corresponding convertToXXX() method, returning its result.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I opted to not call into the one argument constructor since keeping it initializes some of the values twice. The one argument constructor would call the default constructor, which then creates new objects, only to be immediately destroyed by the mutating function.

Furthermore, as least some of the code duplication would have to be moved to the one argument constructor.

Copy link
Contributor

Choose a reason for hiding this comment

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

How about this idea. The only constructor is MutableDocument(DocumentKey), which initializes this.key and does not assign anything to the other instance variables (they will be initialized to null). If this constructor is called from the factory methods, followed by the corresponding convertToXXX() method, then all instance variables will be initialized and none will be multiply initialized. I realize that this leaves the object temporarily in a weird state after calling the constructor; however, since it is private you have complete control over how the newly-constructed object is used and can prevent ever exposing an object in this intermediate state.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I like that. Updated.

* mutations.
*/
public static MutableDocument newInvalidDocument(DocumentKey documentKey) {
return new MutableDocument(
Copy link
Contributor

Choose a reason for hiding this comment

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

How about this idea. The only constructor is MutableDocument(DocumentKey), which initializes this.key and does not assign anything to the other instance variables (they will be initialized to null). If this constructor is called from the factory methods, followed by the corresponding convertToXXX() method, then all instance variables will be initialized and none will be multiply initialized. I realize that this leaves the object temporarily in a weird state after calling the constructor; however, since it is private you have complete control over how the newly-constructed object is used and can prevent ever exposing an object in this intermediate state.

* <p>Invalid documents serve as base documents for mutations. If a document remains invalid even
* after all mutations have been applied, it should be removed from all views.
* <p>Documents can transition between states via {@link
* #convertToFoundDocument(SnapshotVersion,ObjectValue)}, {@link
Copy link
Contributor

Choose a reason for hiding this comment

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

Optional: For maintainability and brevity of this documentation, you can omit the arguments in the @link tags. For example, {@link #convertToFoundDocument(SnapshotVersion,ObjectValue)} can simply be {@link #convertToFoundDocument}. Both javadoc generation and IDE code navigation will infer the correct arguments.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Interesting. I thought that is how it worked, but I couldn't get the linking to work. It turns out it only works without ().

@google-oss-bot
Copy link
Contributor

@schmidt-sebastian: The following test failed, say /retest to rerun them all:

Test name Commit Details Rerun command
smoke-tests a27fe2c link /test smoke-tests

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here.

@schmidt-sebastian schmidt-sebastian merged commit 7be699f into master Feb 26, 2021
@schmidt-sebastian schmidt-sebastian deleted the mrschmidt/randomfixes branch February 26, 2021 20:19
@firebase firebase locked and limited conversation to collaborators Mar 29, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants