-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Port DocumentState and UnknownDocument. #2160
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,23 @@ namespace firebase { | |
namespace firestore { | ||
namespace model { | ||
|
||
enum class DocumentState { | ||
/** | ||
* Local mutations applied via the mutation queue. Document is potentially | ||
* inconsistent. | ||
*/ | ||
kLocalMutations, | ||
|
||
/** | ||
* Mutations applied based on a write acknowledgment. Document is potentially | ||
* inconsistent. | ||
*/ | ||
kCommittedMutations, | ||
|
||
/** No mutations applied. Document was sent to us by Watch. */ | ||
kSynced, | ||
}; | ||
|
||
/** | ||
* Represents a document in Firestore with a key, version, data and whether the | ||
* data has local mutations applied to it. | ||
|
@@ -38,7 +55,7 @@ class Document : public MaybeDocument { | |
Document(FieldValue&& data, | ||
DocumentKey key, | ||
SnapshotVersion version, | ||
bool has_local_mutations); | ||
DocumentState document_state); | ||
|
||
const FieldValue& data() const { | ||
return data_; | ||
|
@@ -48,22 +65,30 @@ class Document : public MaybeDocument { | |
return data_.Get(path); | ||
} | ||
|
||
bool has_local_mutations() const { | ||
return has_local_mutations_; | ||
bool HasLocalMutations() const { | ||
return document_state_ == DocumentState::kLocalMutations; | ||
} | ||
|
||
bool HasCommittedMutations() const { | ||
return document_state_ == DocumentState::kCommittedMutations; | ||
} | ||
|
||
bool HasPendingWrites() const override { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Question: this function uses PascalCase, while the other two very similar functions use snake_case. Can these be in the same style? (no strong preference which) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. http://go/c-readability-advice#virtual-accessor-names explicitly calls out this situation and suggests PascalCase for this (because it's virtual). OTOH, the style guide says that accessors "may" be snake_case. So I've switched everything to PascalCase. |
||
return HasLocalMutations() || HasCommittedMutations(); | ||
} | ||
|
||
protected: | ||
bool Equals(const MaybeDocument& other) const override; | ||
|
||
private: | ||
FieldValue data_; // This is of type Object. | ||
bool has_local_mutations_; | ||
DocumentState document_state_; | ||
}; | ||
|
||
/** Compares against another Document. */ | ||
inline bool operator==(const Document& lhs, const Document& rhs) { | ||
return lhs.version() == rhs.version() && lhs.key() == rhs.key() && | ||
lhs.has_local_mutations() == rhs.has_local_mutations() && | ||
lhs.HasLocalMutations() == rhs.HasLocalMutations() && | ||
lhs.data() == rhs.data(); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,12 +35,20 @@ class MaybeDocument { | |
public: | ||
/** | ||
* All the different kinds of documents, including MaybeDocument and its | ||
* subclasses. This is used to provide RTTI for documents. | ||
* subclasses. This is used to provide RTTI for documents. See the docstrings | ||
* of the subclasses for details. | ||
*/ | ||
enum class Type { | ||
// An unknown subclass of MaybeDocument. This should never happen. | ||
// | ||
// TODO(rsgowman): Since it's no longer possible to directly create | ||
// MaybeDocument's, we can likely remove this value entirely. But | ||
// investigate impact on the serializers first. | ||
Unknown, | ||
|
||
Document, | ||
NoDocument, | ||
UnknownDocument, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since you are adding type here, could you add document for each of the type? In particular, it helps reader differing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer not to add documentation for each type, since that can already be found in the subclasses themselves. So instead, I've added "See the docstrings of the subclasses for details." to the docstring of the enum itself. However, "Unknown" isn't a subclass. It used to be used by MaybeDocument in situations where a MaybeDocument was created directly. That's no longer possible since MaybeDocument is now abstract. It's also used as a placeholder for an invalid value that indicates a pretty serious problem with serialization. I've added a docstring for this value. I've also added a TODO to investigate removing it entirely; I suspect the serializers can do without it. |
||
}; | ||
|
||
MaybeDocument(DocumentKey key, SnapshotVersion version); | ||
|
@@ -66,6 +74,12 @@ class MaybeDocument { | |
return version_; | ||
} | ||
|
||
/** | ||
* Whether this document has a local mutation applied that has not yet been | ||
* acknowledged by Watch. | ||
*/ | ||
virtual bool HasPendingWrites() const = 0; | ||
|
||
protected: | ||
// Only allow subclass to set their types. | ||
void set_type(Type type) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,7 +65,7 @@ std::shared_ptr<const MaybeDocument> SetMutation::ApplyToLocalView( | |
|
||
SnapshotVersion version = GetPostMutationVersion(maybe_doc.get()); | ||
return absl::make_unique<Document>(FieldValue(value_), key(), version, | ||
/*has_local_mutations=*/true); | ||
DocumentState::kLocalMutations); | ||
} | ||
|
||
PatchMutation::PatchMutation(DocumentKey&& key, | ||
|
@@ -84,17 +84,13 @@ std::shared_ptr<const MaybeDocument> PatchMutation::ApplyToLocalView( | |
VerifyKeyMatches(maybe_doc.get()); | ||
|
||
if (!precondition().IsValidFor(maybe_doc.get())) { | ||
if (maybe_doc) { | ||
return absl::make_unique<MaybeDocument>(maybe_doc->key(), | ||
maybe_doc->version()); | ||
} | ||
return nullptr; | ||
return maybe_doc; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the reason for this change? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It fixes a bug (or at least an implementation difference):
(Additionally, I can no longer create a MaybeDocument since it's now an abstract class. But that wasn't the reason I made this change.) The ownership semantics around this aren't particularly solid, as might be expected when porting from a language where that's less important. Ideally, we might go back and rework this, and then backport to the other languages, but that's not super high priority at the moment. |
||
} | ||
|
||
SnapshotVersion version = GetPostMutationVersion(maybe_doc.get()); | ||
FieldValue new_data = PatchDocument(maybe_doc.get()); | ||
return absl::make_unique<Document>(std::move(new_data), key(), version, | ||
/*has_local_mutations=*/true); | ||
DocumentState::kLocalMutations); | ||
} | ||
|
||
FieldValue PatchMutation::PatchDocument(const MaybeDocument* maybe_doc) const { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,8 +22,11 @@ namespace firebase { | |
namespace firestore { | ||
namespace model { | ||
|
||
NoDocument::NoDocument(DocumentKey key, SnapshotVersion version) | ||
: MaybeDocument(std::move(key), std::move(version)) { | ||
NoDocument::NoDocument(DocumentKey key, | ||
SnapshotVersion version, | ||
bool has_committed_mutations) | ||
: MaybeDocument(std::move(key), std::move(version)), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Optional nit: moving There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For non-primitive types, I usually just std::move it, unless it's really obvious that the thing is small. (really obvious => documented as small.) We can probably assume that with SnapshotVersion (and Timestamp), since it seems unlikely that either will grow. Somewhat related: http://go/c-readability-advice#value-param Proposal: Since you've called this optional, and since we std::move SnapshotVersion's (and probably Timestamp's too) somewhat inconsistently in the rest of the code, I'll defer this for now. But I'll create a followup inspired by the above readability link that: |
||
has_committed_mutations_(has_committed_mutations) { | ||
set_type(Type::NoDocument); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright 2018 Google | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include "Firestore/core/src/firebase/firestore/model/unknown_document.h" | ||
|
||
#include <utility> | ||
|
||
namespace firebase { | ||
namespace firestore { | ||
namespace model { | ||
|
||
UnknownDocument::UnknownDocument(DocumentKey key, SnapshotVersion version) | ||
: MaybeDocument(std::move(key), std::move(version)) { | ||
set_type(Type::UnknownDocument); | ||
} | ||
|
||
} // namespace model | ||
} // namespace firestore | ||
} // namespace firebase |
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.
Optional nit: maybe add a new line to visually separate the elements?
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.