@@ -26,50 +26,144 @@ namespace firebase {
26
26
namespace firestore {
27
27
namespace model {
28
28
29
- std::ostream& operator <<(std::ostream& os, DocumentState state) {
30
- switch (state) {
31
- case DocumentState::kCommittedMutations :
32
- return os << " kCommittedMutations" ;
33
- case DocumentState::kLocalMutations :
34
- return os << " kLocalMutations" ;
35
- case DocumentState::kSynced :
36
- return os << " kLocalMutations" ;
29
+ static_assert (
30
+ sizeof (MaybeDocument) == sizeof (Document),
31
+ " Document may not have additional members (everything goes in Rep)" );
32
+
33
+ class Document ::Rep : public MaybeDocument::Rep {
34
+ public:
35
+ Rep (ObjectValue&& data,
36
+ DocumentKey&& key,
37
+ SnapshotVersion version,
38
+ DocumentState document_state)
39
+ : MaybeDocument::Rep(Type::Document, std::move(key), version),
40
+ data_ (std::move(data)),
41
+ document_state_(document_state) {
37
42
}
38
43
39
- UNREACHABLE ();
40
- }
44
+ Rep (ObjectValue&& data,
45
+ DocumentKey&& key,
46
+ SnapshotVersion version,
47
+ DocumentState document_state,
48
+ absl::any proto)
49
+ : Rep(std::move(data), std::move(key), version, document_state) {
50
+ proto_ = std::move (proto);
51
+ }
52
+
53
+ const ObjectValue& data () const {
54
+ return data_;
55
+ }
56
+
57
+ DocumentState document_state () const {
58
+ return document_state_;
59
+ }
60
+
61
+ bool has_local_mutations () const {
62
+ return document_state_ == DocumentState::kLocalMutations ;
63
+ }
64
+
65
+ bool has_committed_mutations () const {
66
+ return document_state_ == DocumentState::kCommittedMutations ;
67
+ }
68
+
69
+ bool has_pending_writes () const override {
70
+ return has_local_mutations () || has_committed_mutations ();
71
+ }
72
+
73
+ bool Equals (const MaybeDocument::Rep& other) const override {
74
+ if (!MaybeDocument::Rep::Equals (other)) return false ;
75
+
76
+ const auto & other_rep = static_cast <const Rep&>(other);
77
+ return document_state_ == other_rep.document_state_ &&
78
+ data_ == other_rep.data_ ;
79
+ }
80
+
81
+ size_t Hash () const override {
82
+ return util::Hash (MaybeDocument::Rep::Hash (), data_, document_state_);
83
+ }
84
+
85
+ std::string ToString () const override {
86
+ return absl::StrCat (
87
+ " Document(key=" , key ().ToString (), " , version=" , version ().ToString (),
88
+ " , document_state=" , document_state_, " , data=" , data_.ToString (), " )" );
89
+ }
41
90
42
- Document::Document (ObjectValue&& data,
91
+ private:
92
+ friend class Document ;
93
+
94
+ ObjectValue data_;
95
+ DocumentState document_state_;
96
+ absl::any proto_;
97
+ };
98
+
99
+ Document::Document (ObjectValue data,
43
100
DocumentKey key,
44
101
SnapshotVersion version,
45
102
DocumentState document_state)
46
- : MaybeDocument(std::move(key), std::move(version)),
47
- data_ (std::move(data)),
48
- document_state_(document_state) {
49
- set_type (Type::Document);
103
+ : MaybeDocument(std::make_shared<Rep>(
104
+ std::move (data), std::move(key), version, document_state)) {
105
+ }
106
+
107
+ Document::Document (ObjectValue data,
108
+ DocumentKey key,
109
+ SnapshotVersion version,
110
+ DocumentState document_state,
111
+ absl::any proto)
112
+ : MaybeDocument(std::make_shared<Rep>(std::move(data),
113
+ std::move(key),
114
+ version,
115
+ document_state,
116
+ std::move(proto))) {
117
+ }
118
+
119
+ Document::Document (const MaybeDocument& document) : MaybeDocument(document) {
120
+ HARD_ASSERT (type () == Type::Document);
50
121
}
51
122
52
- std::string Document::ToString () const {
53
- std::ostringstream out;
54
- out << *this ;
55
- return out.str ();
123
+ const ObjectValue& Document::data () const {
124
+ return doc_rep ().data ();
56
125
}
57
126
58
- std::ostream& operator <<(std::ostream& os, const Document& doc) {
59
- return os << " Document(key=" << doc.key ()
60
- << " , version=" << doc.version ().timestamp ()
61
- << " , document_state=" << doc.document_state_
62
- << " , data=" << doc.data () << " )" ;
127
+ absl::optional<FieldValue> Document::field (const FieldPath& path) const {
128
+ return data ().Get (path);
63
129
}
64
130
65
- bool Document::Equals (const MaybeDocument& other) const {
66
- if (other.type () != Type::Document) {
67
- return false ;
131
+ DocumentState Document::document_state () const {
132
+ return doc_rep ().document_state_ ;
133
+ }
134
+
135
+ bool Document::has_local_mutations () const {
136
+ return doc_rep ().has_local_mutations ();
137
+ }
138
+
139
+ bool Document::has_committed_mutations () const {
140
+ return doc_rep ().has_committed_mutations ();
141
+ }
142
+
143
+ const absl::any& Document::proto () const {
144
+ return doc_rep ().proto_ ;
145
+ }
146
+
147
+ const Document::Rep& Document::doc_rep () const {
148
+ return static_cast <const Rep&>(MaybeDocument::rep ());
149
+ }
150
+
151
+ std::ostream& operator <<(std::ostream& os, DocumentState state) {
152
+ switch (state) {
153
+ case DocumentState::kCommittedMutations :
154
+ return os << " kCommittedMutations" ;
155
+ case DocumentState::kLocalMutations :
156
+ return os << " kLocalMutations" ;
157
+ case DocumentState::kSynced :
158
+ return os << " kLocalSynced" ;
68
159
}
69
- auto & other_doc = static_cast <const Document&>(other);
70
- return MaybeDocument::Equals (other) &&
71
- document_state_ == other_doc.document_state_ &&
72
- data_ == other_doc.data_ ;
160
+
161
+ UNREACHABLE ();
162
+ }
163
+
164
+ /* * Compares against another Document. */
165
+ bool operator ==(const Document& lhs, const Document& rhs) {
166
+ return lhs.doc_rep ().Equals (rhs.doc_rep ());
73
167
}
74
168
75
169
} // namespace model
0 commit comments