Skip to content

Document/MutableDocument changes #7996

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 7 commits into from
Apr 29, 2021

Conversation

schmidt-sebastian
Copy link
Contributor

@schmidt-sebastian schmidt-sebastian commented Apr 28, 2021

This is part of #7904. It will break the feature branch, but I am planning to send out small reviewable chunks with an end goal of a feature branch that passes CI.

This PR changes MutableDocument to be copy-assignable (since ObjectValue is getting changed to be backed by a SharedMessage). It also introduces Document, as a wrapper for MutableDocuments that can no longer be mutated. These are used once documents become part of a view.

@google-oss-bot
Copy link

1 Warning
⚠️ Did you forget to add a changelog entry? (Add #no-changelog to the PR description to silence this warning.)

Generated by 🚫 Danger

@@ -17,125 +17,57 @@
#ifndef FIRESTORE_CORE_SRC_MODEL_DOCUMENT_H_
#define FIRESTORE_CORE_SRC_MODEL_DOCUMENT_H_

#include <iosfwd>
#include <memory>
#include <iostream>
Copy link
Contributor

Choose a reason for hiding this comment

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

<iostream> is a relatively heavy-weight header, so if <iosfwd> suffices, it's better to use <iosfwd>. It looks like you can do that if you move the definition of operator<< into the source file.

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 did not address this yet as this would require me to create document.cc and I am not sure if that tradeoff is worth making.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think there's any harm in creating a .cc file, even if it's just for one function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

* data has local mutations applied to it.
*/
class Document : public MaybeDocument {
/** Represents a non-mutable document in Firestore. */
Copy link
Contributor

Choose a reason for hiding this comment

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

Ultranit: s/non-mutable/immutable/?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fancy :)

value_{std::move(value)},
document_state_{document_state} {
}

DocumentKey key_;
DocumentType document_type_ = DocumentType::kInvalid;
SnapshotVersion version_;
ObjectValue value_;
std::shared_ptr<const ObjectValue> value_ = std::make_shared<ObjectValue>();
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: std::make_shared<const ObjectValue>()?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@@ -17,17 +17,19 @@
#ifndef FIRESTORE_CORE_SRC_MODEL_MUTABLE_DOCUMENT_H_
#define FIRESTORE_CORE_SRC_MODEL_MUTABLE_DOCUMENT_H_

#include <memory>
#include <ostream>
Copy link
Contributor

Choose a reason for hiding this comment

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

Note: this file could probably replace <ostream> with <iosfwd> as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

value_{std::move(value)},
document_state_{document_state} {
}

DocumentKey key_;
DocumentType document_type_ = DocumentType::kInvalid;
SnapshotVersion version_;
ObjectValue value_;
std::shared_ptr<const ObjectValue> value_ = std::make_shared<ObjectValue>();
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add a comment to explain the rationale of making this a shared pointer?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

std::string MutableDocument::ToString() const {
std::stringstream stream;
stream << "MutableDocument(key=" << key_ << ", type=" << document_type_
<< ", version=" << version_ << ", value=" << value_
<< ", version=" << version_ << ", value=" << CanonicalId(value_->Get())
Copy link
Contributor

Choose a reason for hiding this comment

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

Question: is it necessary to call CanonicalId and Get rather than just do << *value_?

#include <sstream>
#include "Firestore/core/src/model/mutable_document.h"
Copy link
Contributor

Choose a reason for hiding this comment

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

Nits:

  • the mutable_document.h include should be first (it's the related header), i.e. undo the change;
  • there has to be a blank line in-between the last standard header and the first Google header (also a blank line in-between the related header and the first standard header).

See go/cstyle#Names_and_Order_of_Includes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@@ -63,7 +59,7 @@ MutableDocument& MutableDocument::ConvertToNoDocument(
const SnapshotVersion& version) {
version_ = version;
document_type_ = DocumentType::kNoDocument;
value_ = {};
value_ = std::make_shared<ObjectValue>();
Copy link
Contributor

Choose a reason for hiding this comment

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

You want to make sure value_ is never null to avoid checking it before each access, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, otherwise I have to dynamically create a new value before each access.

@@ -212,7 +212,10 @@ class MutableDocument {
DocumentKey key_;
DocumentType document_type_ = DocumentType::kInvalid;
SnapshotVersion version_;
std::shared_ptr<const ObjectValue> value_ = std::make_shared<ObjectValue>();
// Using a shared pointer to ObjectValue makes MutableDocument copy-assignable
// without having to manually create a deep clone of its Protobuf contents
Copy link
Contributor

Choose a reason for hiding this comment

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

Ultranit: please add a full stop at the end of the sentence.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@schmidt-sebastian schmidt-sebastian merged commit f07ef0d into mrschmidt/mutabledocuments Apr 29, 2021
@schmidt-sebastian schmidt-sebastian deleted the mrschmidt/documentstype branch April 29, 2021 00:04
@firebase firebase locked and limited conversation to collaborators May 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.

3 participants