Skip to content

Commit bf45a15

Browse files
zxu123wilhuff
authored andcommitted
port Firestore SnapshotVersion in C++ (#767)
* implement SnapshotVersion and test
1 parent d70c23e commit bf45a15

File tree

6 files changed

+171
-0
lines changed

6 files changed

+171
-0
lines changed

Firestore/Example/Firestore.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@
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 */; };
143143
AB7BAB342012B519001E0872 /* geo_point_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = AB7BAB332012B519001E0872 /* geo_point_test.cc */; };
144+
ABA495BB202B7E80008A7851 /* snapshot_version_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = ABA495B9202B7E79008A7851 /* snapshot_version_test.cc */; };
144145
ABC1D7DC2023A04B00BA84F0 /* credentials_provider_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = AB38D9342023966E000A432D /* credentials_provider_test.cc */; };
145146
ABC1D7DD2023A04F00BA84F0 /* empty_credentials_provider_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = AB38D93620239689000A432D /* empty_credentials_provider_test.cc */; };
146147
ABC1D7DE2023A05300BA84F0 /* user_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = AB38D93220239654000A432D /* user_test.cc */; };
@@ -346,6 +347,7 @@
346347
AB38D93620239689000A432D /* empty_credentials_provider_test.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = empty_credentials_provider_test.cc; sourceTree = "<group>"; };
347348
AB71064B201FA60300344F18 /* database_id_test.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = database_id_test.cc; sourceTree = "<group>"; };
348349
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>"; };
350+
ABA495B9202B7E79008A7851 /* snapshot_version_test.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = snapshot_version_test.cc; sourceTree = "<group>"; };
349351
ABC1D7DF2023A3EF00BA84F0 /* token_test.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = token_test.cc; sourceTree = "<group>"; };
350352
ABC1D7E22023CDC500BA84F0 /* firebase_credentials_provider_test.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = firebase_credentials_provider_test.mm; sourceTree = "<group>"; };
351353
ABF6506B201131F8005F2C74 /* timestamp_test.cc */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = timestamp_test.cc; sourceTree = "<group>"; };
@@ -584,6 +586,7 @@
584586
B686F2AD2023DDB20028D6BE /* field_path_test.cc */,
585587
AB71064B201FA60300344F18 /* database_id_test.cc */,
586588
AB356EF6200EA5EB0089B766 /* field_value_test.cc */,
589+
ABA495B9202B7E79008A7851 /* snapshot_version_test.cc */,
587590
ABF6506B201131F8005F2C74 /* timestamp_test.cc */,
588591
);
589592
name = model;
@@ -1329,6 +1332,7 @@
13291332
ABC1D7DC2023A04B00BA84F0 /* credentials_provider_test.cc in Sources */,
13301333
5492E059202154AB00B64F25 /* FIRQuerySnapshotTests.mm in Sources */,
13311334
5492E050202154AA00B64F25 /* FIRCollectionReferenceTests.mm in Sources */,
1335+
ABA495BB202B7E80008A7851 /* snapshot_version_test.cc in Sources */,
13321336
5492E0A02021552D00B64F25 /* FSTLevelDBMutationQueueTests.mm in Sources */,
13331337
54EB764D202277B30088B8F3 /* array_sorted_map_test.cc in Sources */,
13341338
5492E03420213FFC00B64F25 /* FSTMemorySpecTests.mm in Sources */,

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ cc_library(
2222
field_path.h
2323
field_value.cc
2424
field_value.h
25+
snapshot_version.cc
26+
snapshot_version.h
2527
resource_path.cc
2628
resource_path.h
2729
timestamp.cc
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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/snapshot_version.h"
18+
19+
namespace firebase {
20+
namespace firestore {
21+
namespace model {
22+
23+
SnapshotVersion::SnapshotVersion(const Timestamp& timestamp)
24+
: timestamp_(timestamp) {
25+
}
26+
27+
const SnapshotVersion& SnapshotVersion::None() {
28+
static const SnapshotVersion kNone(Timestamp{});
29+
return kNone;
30+
}
31+
32+
} // namespace model
33+
} // namespace firestore
34+
} // namespace firebase
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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_SNAPSHOT_VERSION_H_
18+
#define FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_MODEL_SNAPSHOT_VERSION_H_
19+
20+
#include "Firestore/core/src/firebase/firestore/model/timestamp.h"
21+
22+
namespace firebase {
23+
namespace firestore {
24+
namespace model {
25+
26+
/**
27+
* A version of a document in Firestore. This corresponds to the version
28+
* timestamp, such as update_time or read_time.
29+
*/
30+
class SnapshotVersion {
31+
public:
32+
explicit SnapshotVersion(const Timestamp& timestamp);
33+
34+
const Timestamp& timestamp() const {
35+
return timestamp_;
36+
}
37+
38+
/** Creates a new version that is smaller than all other versions. */
39+
static const SnapshotVersion& None();
40+
41+
private:
42+
Timestamp timestamp_;
43+
};
44+
45+
/** Compares against another SnapshotVersion. */
46+
inline bool operator<(const SnapshotVersion& lhs, const SnapshotVersion& rhs) {
47+
return lhs.timestamp() < rhs.timestamp();
48+
}
49+
50+
inline bool operator>(const SnapshotVersion& lhs, const SnapshotVersion& rhs) {
51+
return lhs.timestamp() > rhs.timestamp();
52+
}
53+
54+
inline bool operator>=(const SnapshotVersion& lhs, const SnapshotVersion& rhs) {
55+
return lhs.timestamp() >= rhs.timestamp();
56+
}
57+
58+
inline bool operator<=(const SnapshotVersion& lhs, const SnapshotVersion& rhs) {
59+
return lhs.timestamp() <= rhs.timestamp();
60+
}
61+
62+
inline bool operator!=(const SnapshotVersion& lhs, const SnapshotVersion& rhs) {
63+
return lhs.timestamp() != rhs.timestamp();
64+
}
65+
66+
inline bool operator==(const SnapshotVersion& lhs, const SnapshotVersion& rhs) {
67+
return lhs.timestamp() == rhs.timestamp();
68+
}
69+
70+
} // namespace model
71+
} // namespace firestore
72+
} // namespace firebase
73+
74+
#endif // FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_MODEL_SNAPSHOT_VERSION_H_

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ cc_test(
1818
database_id_test.cc
1919
field_path_test.cc
2020
field_value_test.cc
21+
snapshot_version_test.cc
2122
timestamp_test.cc
2223
resource_path_test.cc
2324
DEPENDS
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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/snapshot_version.h"
18+
19+
#include "gtest/gtest.h"
20+
21+
namespace firebase {
22+
namespace firestore {
23+
namespace model {
24+
25+
TEST(SnapshotVersion, Getter) {
26+
SnapshotVersion version(Timestamp(123, 456));
27+
EXPECT_EQ(Timestamp(123, 456), version.timestamp());
28+
29+
const SnapshotVersion& no_version = SnapshotVersion::None();
30+
EXPECT_EQ(Timestamp(), no_version.timestamp());
31+
}
32+
33+
TEST(SnapshotVersion, Comparison) {
34+
EXPECT_LT(SnapshotVersion::None(), SnapshotVersion(Timestamp(123, 456)));
35+
36+
EXPECT_LT(SnapshotVersion(Timestamp(123, 456)),
37+
SnapshotVersion(Timestamp(456, 123)));
38+
EXPECT_GT(SnapshotVersion(Timestamp(456, 123)),
39+
SnapshotVersion(Timestamp(123, 456)));
40+
EXPECT_LE(SnapshotVersion(Timestamp(123, 456)),
41+
SnapshotVersion(Timestamp(456, 123)));
42+
EXPECT_LE(SnapshotVersion(Timestamp(123, 456)),
43+
SnapshotVersion(Timestamp(123, 456)));
44+
EXPECT_GE(SnapshotVersion(Timestamp(456, 123)),
45+
SnapshotVersion(Timestamp(123, 456)));
46+
EXPECT_GE(SnapshotVersion(Timestamp(123, 456)),
47+
SnapshotVersion(Timestamp(123, 456)));
48+
EXPECT_EQ(SnapshotVersion(Timestamp(123, 456)),
49+
SnapshotVersion(Timestamp(123, 456)));
50+
EXPECT_NE(SnapshotVersion(Timestamp(123, 456)),
51+
SnapshotVersion(Timestamp(456, 123)));
52+
}
53+
54+
} // namespace model
55+
} // namespace firestore
56+
} // namespace firebase

0 commit comments

Comments
 (0)