Skip to content

Commit 9504582

Browse files
authored
Port Firestore Document to C++ (firebase#777)
* implement SnapshotVersion and test * update project * implement MaybeDocument and test * move snapshot-version from core to model * fix a bug * implement Document and test * implement NoDocument * adding type tag and fix style * fix a few bugs, discovered after merging and test run. * add assert to check FieldValue type and more test for comparision. * address changes * allow moving FieldValue to construct Document. * address changes * add document tests to project * use std::less convention * make Type::Unknown static initializer
1 parent adf9fb3 commit 9504582

File tree

12 files changed

+545
-3
lines changed

12 files changed

+545
-3
lines changed

Firestore/Example/Firestore.xcodeproj/project.pbxproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@
140140
AB380D02201BC69F00D97691 /* bits_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = AB380D01201BC69F00D97691 /* bits_test.cc */; };
141141
AB380D04201BC6E400D97691 /* ordered_code_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = AB380D03201BC6E400D97691 /* ordered_code_test.cc */; };
142142
AB38D93020236E21000A432D /* database_info_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = AB38D92E20235D22000A432D /* database_info_test.cc */; };
143+
AB6B908420322E4D00CC290A /* document_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = AB6B908320322E4D00CC290A /* document_test.cc */; };
144+
AB6B908620322E6D00CC290A /* maybe_document_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = AB6B908520322E6D00CC290A /* maybe_document_test.cc */; };
145+
AB6B908820322E8800CC290A /* no_document_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = AB6B908720322E8800CC290A /* no_document_test.cc */; };
143146
AB7BAB342012B519001E0872 /* geo_point_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = AB7BAB332012B519001E0872 /* geo_point_test.cc */; };
144147
ABA495BB202B7E80008A7851 /* snapshot_version_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = ABA495B9202B7E79008A7851 /* snapshot_version_test.cc */; };
145148
ABC1D7DC2023A04B00BA84F0 /* credentials_provider_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = AB38D9342023966E000A432D /* credentials_provider_test.cc */; };
@@ -346,6 +349,9 @@
346349
AB38D93220239654000A432D /* user_test.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = user_test.cc; sourceTree = "<group>"; };
347350
AB38D9342023966E000A432D /* credentials_provider_test.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = credentials_provider_test.cc; sourceTree = "<group>"; };
348351
AB38D93620239689000A432D /* empty_credentials_provider_test.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = empty_credentials_provider_test.cc; sourceTree = "<group>"; };
352+
AB6B908320322E4D00CC290A /* document_test.cc */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = document_test.cc; sourceTree = "<group>"; };
353+
AB6B908520322E6D00CC290A /* maybe_document_test.cc */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = maybe_document_test.cc; sourceTree = "<group>"; };
354+
AB6B908720322E8800CC290A /* no_document_test.cc */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = no_document_test.cc; sourceTree = "<group>"; };
349355
AB71064B201FA60300344F18 /* database_id_test.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = database_id_test.cc; sourceTree = "<group>"; };
350356
AB7BAB332012B519001E0872 /* geo_point_test.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = geo_point_test.cc; path = ../../core/test/firebase/firestore/geo_point_test.cc; sourceTree = "<group>"; };
351357
ABA495B9202B7E79008A7851 /* snapshot_version_test.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = snapshot_version_test.cc; sourceTree = "<group>"; };
@@ -585,10 +591,13 @@
585591
isa = PBXGroup;
586592
children = (
587593
B6152AD5202A5385000E5744 /* document_key_test.cc */,
594+
AB6B908320322E4D00CC290A /* document_test.cc */,
588595
B686F2B02024FFD70028D6BE /* resource_path_test.cc */,
589596
B686F2AD2023DDB20028D6BE /* field_path_test.cc */,
590597
AB71064B201FA60300344F18 /* database_id_test.cc */,
591598
AB356EF6200EA5EB0089B766 /* field_value_test.cc */,
599+
AB6B908520322E6D00CC290A /* maybe_document_test.cc */,
600+
AB6B908720322E8800CC290A /* no_document_test.cc */,
592601
ABA495B9202B7E79008A7851 /* snapshot_version_test.cc */,
593602
ABF6506B201131F8005F2C74 /* timestamp_test.cc */,
594603
);
@@ -1310,13 +1319,15 @@
13101319
5492E0AD2021552D00B64F25 /* FSTMemoryMutationQueueTests.mm in Sources */,
13111320
5492E051202154AA00B64F25 /* FIRQueryTests.mm in Sources */,
13121321
5492E054202154AB00B64F25 /* FIRFieldValueTests.mm in Sources */,
1322+
AB6B908620322E6D00CC290A /* maybe_document_test.cc in Sources */,
13131323
5492E09F2021552D00B64F25 /* FSTLevelDBMigrationsTests.mm in Sources */,
13141324
5492E053202154AB00B64F25 /* FIRDocumentReferenceTests.mm in Sources */,
13151325
5492E09D2021552D00B64F25 /* FSTLocalStoreTests.mm in Sources */,
13161326
5492E0A32021552D00B64F25 /* FSTLocalSerializerTests.mm in Sources */,
13171327
5492E0A72021552D00B64F25 /* FSTLevelDBKeyTests.mm in Sources */,
13181328
5492E0A22021552D00B64F25 /* FSTQueryCacheTests.mm in Sources */,
13191329
5492E0A52021552D00B64F25 /* FSTMemoryRemoteDocumentCacheTests.mm in Sources */,
1330+
AB6B908820322E8800CC290A /* no_document_test.cc in Sources */,
13201331
5492E0BD2021555100B64F25 /* FSTDocumentTests.mm in Sources */,
13211332
5492E0B92021555100B64F25 /* FSTDocumentKeyTests.mm in Sources */,
13221333
DE2EF0871F3D0B6E003D0CDC /* FSTImmutableSortedSet+Testing.m in Sources */,
@@ -1331,6 +1342,7 @@
13311342
5492E0AB2021552D00B64F25 /* StringViewTests.mm in Sources */,
13321343
5492E0C92021557E00B64F25 /* FSTRemoteEventTests.mm in Sources */,
13331344
ABC1D7E42024AFDE00BA84F0 /* firebase_credentials_provider_test.mm in Sources */,
1345+
AB6B908420322E4D00CC290A /* document_test.cc in Sources */,
13341346
ABF6506C201131F8005F2C74 /* timestamp_test.cc in Sources */,
13351347
5492E0AE2021552D00B64F25 /* FSTLevelDBQueryCacheTests.mm in Sources */,
13361348
ABC1D7DC2023A04B00BA84F0 /* credentials_provider_test.cc in Sources */,

Firestore/core/src/firebase/firestore/model/CMakeLists.txt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,22 @@ cc_library(
1818
base_path.h
1919
database_id.cc
2020
database_id.h
21+
document.cc
22+
document.h
2123
document_key.cc
2224
document_key.h
2325
field_path.cc
2426
field_path.h
2527
field_value.cc
2628
field_value.h
27-
snapshot_version.cc
28-
snapshot_version.h
29+
maybe_document.cc
30+
maybe_document.h
31+
no_document.cc
32+
no_document.h
2933
resource_path.cc
3034
resource_path.h
35+
snapshot_version.cc
36+
snapshot_version.h
3137
timestamp.cc
3238
timestamp.h
3339
DEPENDS
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2018 Google
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "Firestore/core/src/firebase/firestore/model/document.h"
18+
19+
#include <utility>
20+
21+
#include "Firestore/core/src/firebase/firestore/util/firebase_assert.h"
22+
23+
namespace firebase {
24+
namespace firestore {
25+
namespace model {
26+
27+
Document::Document(FieldValue&& data,
28+
DocumentKey key,
29+
SnapshotVersion version,
30+
bool has_local_mutations)
31+
: MaybeDocument(std::move(key), std::move(version)),
32+
data_(std::move(data)),
33+
has_local_mutations_(has_local_mutations) {
34+
set_type(Type::Document);
35+
FIREBASE_ASSERT(FieldValue::Type::Object == data.type());
36+
}
37+
38+
bool Document::Equals(const MaybeDocument& other) const {
39+
if (other.type() != Type::Document) {
40+
return false;
41+
}
42+
const Document& other_doc = static_cast<const Document&>(other);
43+
return MaybeDocument::Equals(other) &&
44+
has_local_mutations_ == other_doc.has_local_mutations_ &&
45+
data_ == other_doc.data_;
46+
}
47+
48+
} // namespace model
49+
} // namespace firestore
50+
} // namespace firebase
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2018 Google
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_MODEL_DOCUMENT_H_
18+
#define FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_MODEL_DOCUMENT_H_
19+
20+
#include "Firestore/core/src/firebase/firestore/model/field_value.h"
21+
#include "Firestore/core/src/firebase/firestore/model/maybe_document.h"
22+
23+
namespace firebase {
24+
namespace firestore {
25+
namespace model {
26+
27+
/**
28+
* Represents a document in Firestore with a key, version, data and whether the
29+
* data has local mutations applied to it.
30+
*/
31+
class Document : public MaybeDocument {
32+
public:
33+
/**
34+
* Construct a document. FieldValue must be passed by rvalue.
35+
*/
36+
Document(FieldValue&& data,
37+
DocumentKey key,
38+
SnapshotVersion version,
39+
bool has_local_mutations);
40+
41+
const FieldValue& data() const {
42+
return data_;
43+
}
44+
45+
bool has_local_mutations() const {
46+
return has_local_mutations_;
47+
}
48+
49+
protected:
50+
bool Equals(const MaybeDocument& other) const override;
51+
52+
private:
53+
FieldValue data_; // This is of type Object.
54+
bool has_local_mutations_;
55+
};
56+
57+
/** Compares against another Document. */
58+
inline bool operator==(const Document& lhs, const Document& rhs) {
59+
return lhs.version() == rhs.version() && lhs.key() == rhs.key() &&
60+
lhs.has_local_mutations() == rhs.has_local_mutations() &&
61+
lhs.data() == rhs.data();
62+
}
63+
64+
inline bool operator!=(const Document& lhs, const Document& rhs) {
65+
return !(lhs == rhs);
66+
}
67+
68+
} // namespace model
69+
} // namespace firestore
70+
} // namespace firebase
71+
72+
#endif // FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_MODEL_DOCUMENT_H_
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2018 Google
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "Firestore/core/src/firebase/firestore/model/maybe_document.h"
18+
19+
#include <utility>
20+
21+
#include "Firestore/core/src/firebase/firestore/util/firebase_assert.h"
22+
23+
namespace firebase {
24+
namespace firestore {
25+
namespace model {
26+
27+
MaybeDocument::MaybeDocument(DocumentKey key, SnapshotVersion version)
28+
: key_(std::move(key)), version_(std::move(version)) {
29+
}
30+
31+
bool MaybeDocument::Equals(const MaybeDocument& other) const {
32+
return type_ == other.type_ && version_ == other.version_ &&
33+
key_ == other.key_;
34+
}
35+
36+
} // namespace model
37+
} // namespace firestore
38+
} // namespace firebase
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Copyright 2018 Google
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_MODEL_MAYBE_DOCUMENT_H_
18+
#define FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_MODEL_MAYBE_DOCUMENT_H_
19+
20+
#include <functional>
21+
22+
#include "Firestore/core/src/firebase/firestore/model/document_key.h"
23+
#include "Firestore/core/src/firebase/firestore/model/snapshot_version.h"
24+
25+
namespace firebase {
26+
namespace firestore {
27+
namespace model {
28+
29+
/**
30+
* The result of a lookup for a given path may be an existing document or a
31+
* tombstone that marks the path deleted.
32+
*/
33+
class MaybeDocument {
34+
public:
35+
/**
36+
* All the different kinds of documents, including MaybeDocument and its
37+
* subclasses. This is used to provide RTTI for documents.
38+
*/
39+
enum class Type {
40+
Unknown,
41+
Document,
42+
NoDocument,
43+
};
44+
45+
MaybeDocument(DocumentKey key, SnapshotVersion version);
46+
47+
/** The runtime type of this document. */
48+
Type type() const {
49+
return type_;
50+
}
51+
52+
/** The key for this document. */
53+
const DocumentKey& key() const {
54+
return key_;
55+
}
56+
57+
/**
58+
* Returns the version of this document if it exists or a version at which
59+
* this document was guaranteed to not exist.
60+
*/
61+
const SnapshotVersion& version() const {
62+
return version_;
63+
}
64+
65+
protected:
66+
// Only allow subclass to set their types.
67+
void set_type(Type type) {
68+
type_ = type;
69+
}
70+
71+
virtual bool Equals(const MaybeDocument& other) const;
72+
73+
friend bool operator==(const MaybeDocument& lhs, const MaybeDocument& rhs);
74+
75+
private:
76+
Type type_ = Type::Unknown;
77+
DocumentKey key_;
78+
SnapshotVersion version_;
79+
};
80+
81+
inline bool operator==(const MaybeDocument& lhs, const MaybeDocument& rhs) {
82+
return lhs.Equals(rhs);
83+
}
84+
85+
inline bool operator!=(const MaybeDocument& lhs, const MaybeDocument& rhs) {
86+
return !(lhs == rhs);
87+
}
88+
89+
/** Compares against another MaybeDocument by keys only. */
90+
struct DocumentKeyComparator : public std::less<MaybeDocument> {
91+
bool operator()(const MaybeDocument& lhs, const MaybeDocument& rhs) const {
92+
return lhs.key() < rhs.key();
93+
}
94+
};
95+
96+
} // namespace model
97+
} // namespace firestore
98+
} // namespace firebase
99+
100+
#endif // FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_MODEL_MAYBE_DOCUMENT_H_
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2018 Google
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "Firestore/core/src/firebase/firestore/model/no_document.h"
18+
19+
#include <utility>
20+
21+
namespace firebase {
22+
namespace firestore {
23+
namespace model {
24+
25+
NoDocument::NoDocument(DocumentKey key, SnapshotVersion version)
26+
: MaybeDocument(std::move(key), std::move(version)) {
27+
set_type(Type::NoDocument);
28+
}
29+
30+
} // namespace model
31+
} // namespace firestore
32+
} // namespace firebase

0 commit comments

Comments
 (0)