Skip to content

Commit 821fb90

Browse files
authored
implement TargetIdGenerator in C++ for Firestore (#701)
* implement `TargetIdGenerator` * address changes
1 parent 5fdda3f commit 821fb90

File tree

8 files changed

+310
-0
lines changed

8 files changed

+310
-0
lines changed

Firestore/Example/Firestore.xcodeproj/project.pbxproj

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
71719F9F1E33DC2100824A3D /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 71719F9D1E33DC2100824A3D /* LaunchScreen.storyboard */; };
6666
873B8AEB1B1F5CCA007FD442 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 873B8AEA1B1F5CCA007FD442 /* Main.storyboard */; };
6767
AB356EF7200EA5EB0089B766 /* field_value_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = AB356EF6200EA5EB0089B766 /* field_value_test.cc */; };
68+
AB380CFB2019388600D97691 /* target_id_generator_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = AB380CF82019382300D97691 /* target_id_generator_test.cc */; };
6869
AB380CFE201A2F4500D97691 /* string_util_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = AB380CFC201A2EE200D97691 /* string_util_test.cc */; };
6970
AB382F7C1FE02A1F007CA955 /* FIRDocumentReferenceTests.m in Sources */ = {isa = PBXBuildFile; fileRef = AB382F7B1FE02A1F007CA955 /* FIRDocumentReferenceTests.m */; };
7071
AB382F7E1FE03059007CA955 /* FIRFieldPathTests.m in Sources */ = {isa = PBXBuildFile; fileRef = AB382F7D1FE03059007CA955 /* FIRFieldPathTests.m */; };
@@ -249,6 +250,7 @@
249250
9D52E67EE96AA7E5D6F69748 /* Pods-Firestore_IntegrationTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Firestore_IntegrationTests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-Firestore_IntegrationTests/Pods-Firestore_IntegrationTests.debug.xcconfig"; sourceTree = "<group>"; };
250251
9EF477AD4B2B643FD320867A /* Pods-Firestore_Example.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Firestore_Example.debug.xcconfig"; path = "Pods/Target Support Files/Pods-Firestore_Example/Pods-Firestore_Example.debug.xcconfig"; sourceTree = "<group>"; };
251252
AB356EF6200EA5EB0089B766 /* field_value_test.cc */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = field_value_test.cc; sourceTree = "<group>"; };
253+
AB380CF82019382300D97691 /* target_id_generator_test.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = target_id_generator_test.cc; sourceTree = "<group>"; };
252254
AB380CFC201A2EE200D97691 /* string_util_test.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = string_util_test.cc; path = ../../core/test/firebase/firestore/util/string_util_test.cc; sourceTree = "<group>"; };
253255
AB382F7B1FE02A1F007CA955 /* FIRDocumentReferenceTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FIRDocumentReferenceTests.m; sourceTree = "<group>"; };
254256
AB382F7D1FE03059007CA955 /* FIRFieldPathTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FIRFieldPathTests.m; sourceTree = "<group>"; };
@@ -416,6 +418,7 @@
416418
54764FAC1FAA0C390085E60A /* GoogleTests */ = {
417419
isa = PBXGroup;
418420
children = (
421+
AB380CF7201937B800D97691 /* core */,
419422
AB356EF5200E9D1A0089B766 /* model */,
420423
54764FAD1FAA0C650085E60A /* Port */,
421424
54740A561FC913EB00713A1A /* util */,
@@ -559,6 +562,15 @@
559562
path = ../../core/test/firebase/firestore/model;
560563
sourceTree = "<group>";
561564
};
565+
AB380CF7201937B800D97691 /* core */ = {
566+
isa = PBXGroup;
567+
children = (
568+
AB380CF82019382300D97691 /* target_id_generator_test.cc */,
569+
);
570+
name = core;
571+
path = ../../core/test/firebase/firestore/core;
572+
sourceTree = "<group>";
573+
};
562574
DE0761E51F2FE611003233AF /* SwiftBuildTest */ = {
563575
isa = PBXGroup;
564576
children = (
@@ -1227,6 +1239,7 @@
12271239
DE51B1EB1F0D490D0013853F /* FSTWriteGroupTests.mm in Sources */,
12281240
54C2294F1FECABAE007D065B /* log_test.cc in Sources */,
12291241
DE51B2011F0D493E0013853F /* FSTHelpers.m in Sources */,
1242+
AB380CFB2019388600D97691 /* target_id_generator_test.cc in Sources */,
12301243
DE51B1F61F0D491B0013853F /* FSTSerializerBetaTests.m in Sources */,
12311244
DE51B1F01F0D49140013853F /* FSTFieldValueTests.m in Sources */,
12321245
AB9945281FE2DE0C00DFC1E6 /* FIRSnapshotMetadataTests.m in Sources */,

Firestore/core/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313
# limitations under the License.
1414

1515
add_subdirectory(src/firebase/firestore)
16+
add_subdirectory(src/firebase/firestore/core)
1617
add_subdirectory(src/firebase/firestore/model)
1718
add_subdirectory(src/firebase/firestore/remote)
1819
add_subdirectory(src/firebase/firestore/util)
1920

2021
add_subdirectory(test/firebase/firestore)
22+
add_subdirectory(test/firebase/firestore/core)
2123
add_subdirectory(test/firebase/firestore/model)
2224
add_subdirectory(test/firebase/firestore/remote)
2325
add_subdirectory(test/firebase/firestore/util)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright 2018 Google
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
cc_library(
16+
firebase_firestore_core
17+
SOURCES
18+
target_id_generator.cc
19+
target_id_generator.h
20+
)
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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/core/target_id_generator.h"
18+
19+
namespace firebase {
20+
namespace firestore {
21+
namespace core {
22+
23+
TargetIdGenerator::TargetIdGenerator(const TargetIdGenerator& value)
24+
: generator_id_(value.generator_id_), previous_id_(value.previous_id_) {
25+
}
26+
27+
TargetIdGenerator::TargetIdGenerator(TargetIdGeneratorId generator_id,
28+
TargetId after)
29+
: generator_id_(generator_id) {
30+
const TargetId after_without_generator = (after >> kReservedBits)
31+
<< kReservedBits;
32+
const TargetId after_generator = after - after_without_generator;
33+
const TargetId generator = static_cast<TargetId>(generator_id);
34+
if (after_generator >= generator) {
35+
// For example, if:
36+
// self.generatorID = 0b0000
37+
// after = 0b1011
38+
// afterGenerator = 0b0001
39+
// Then:
40+
// previous = 0b1010
41+
// next = 0b1100
42+
previous_id_ = after_without_generator | generator;
43+
} else {
44+
// For example, if:
45+
// self.generatorID = 0b0001
46+
// after = 0b1010
47+
// afterGenerator = 0b0000
48+
// Then:
49+
// previous = 0b1001
50+
// next = 0b1011
51+
previous_id_ = (after_without_generator | generator) - (1 << kReservedBits);
52+
}
53+
}
54+
55+
TargetId TargetIdGenerator::NextId() {
56+
previous_id_ += 1 << kReservedBits;
57+
return previous_id_;
58+
}
59+
60+
} // namespace core
61+
} // namespace firestore
62+
} // namespace firebase
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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_CORE_TARGET_ID_GENERATOR_H_
18+
#define FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_CORE_TARGET_ID_GENERATOR_H_
19+
20+
#include "Firestore/core/src/firebase/firestore/core/types.h"
21+
22+
namespace firebase {
23+
namespace firestore {
24+
namespace core {
25+
26+
/** The set of all valid generators. */
27+
enum class TargetIdGeneratorId { LocalStore = 0, SyncEngine = 1 };
28+
29+
/**
30+
* Generates monotonically increasing integer IDs. There are separate generators
31+
* for different scopes. While these generators will operate independently of
32+
* each other, they are scoped, such that no two generators will ever produce
33+
* the same ID. This is useful, because sometimes the backend may group IDs from
34+
* separate parts of the client into the same ID space.
35+
*
36+
* Not thread-safe.
37+
*/
38+
class TargetIdGenerator {
39+
public:
40+
TargetIdGenerator(const TargetIdGenerator& value);
41+
42+
/**
43+
* Creates and returns the TargetIdGenerator for the local store.
44+
*
45+
* @param after An ID to start at. Every call to NextId returns a larger id.
46+
* @return An instance of TargetIdGenerator.
47+
*/
48+
static TargetIdGenerator LocalStoreTargetIdGenerator(TargetId after) {
49+
return TargetIdGenerator(TargetIdGeneratorId::LocalStore, after);
50+
}
51+
52+
/**
53+
* Creates and returns the TargetIdGenerator for the sync engine.
54+
*
55+
* @param after An ID to start at. Every call to NextId returns a larger id.
56+
* @return An instance of TargetIdGenerator.
57+
*/
58+
static TargetIdGenerator SyncEngineTargetIdGenerator(TargetId after) {
59+
return TargetIdGenerator(TargetIdGeneratorId::SyncEngine, after);
60+
}
61+
62+
TargetIdGeneratorId generator_id() {
63+
return generator_id_;
64+
}
65+
66+
TargetId NextId();
67+
68+
private:
69+
TargetIdGenerator(TargetIdGeneratorId generator_id, TargetId after);
70+
TargetIdGeneratorId generator_id_;
71+
TargetId previous_id_;
72+
73+
static const int kReservedBits = 1;
74+
};
75+
76+
} // namespace core
77+
} // namespace firestore
78+
} // namespace firebase
79+
80+
#endif // FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_CORE_TARGET_ID_GENERATOR_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+
#ifndef FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_CORE_TYPES_H_
18+
#define FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_CORE_TYPES_H_
19+
20+
#include <stdint.h>
21+
22+
namespace firebase {
23+
namespace firestore {
24+
namespace core {
25+
26+
typedef int32_t TargetId;
27+
28+
} // namespace core
29+
} // namespace firestore
30+
} // namespace firebase
31+
32+
#endif // FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_CORE_TYPES_H_
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright 2018 Google
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
cc_test(
16+
firebase_firestore_core_test
17+
SOURCES
18+
target_id_generator_test.cc
19+
DEPENDS
20+
firebase_firestore_core
21+
)
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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/core/target_id_generator.h"
18+
19+
#include "gtest/gtest.h"
20+
21+
namespace firebase {
22+
namespace firestore {
23+
namespace core {
24+
25+
TEST(TargetIdGenerator, Constructor) {
26+
TargetIdGenerator local_store_generator =
27+
TargetIdGenerator::LocalStoreTargetIdGenerator(0);
28+
TargetIdGenerator sync_engine_generator =
29+
TargetIdGenerator::SyncEngineTargetIdGenerator(0);
30+
EXPECT_EQ(TargetIdGeneratorId::LocalStore,
31+
local_store_generator.generator_id());
32+
EXPECT_EQ(2, local_store_generator.NextId());
33+
EXPECT_EQ(TargetIdGeneratorId::SyncEngine,
34+
sync_engine_generator.generator_id());
35+
EXPECT_EQ(1, sync_engine_generator.NextId());
36+
}
37+
38+
TEST(TargetIdGenerator, SkipPast) {
39+
EXPECT_EQ(1, TargetIdGenerator::SyncEngineTargetIdGenerator(-1).NextId());
40+
EXPECT_EQ(3, TargetIdGenerator::SyncEngineTargetIdGenerator(2).NextId());
41+
EXPECT_EQ(5, TargetIdGenerator::SyncEngineTargetIdGenerator(4).NextId());
42+
43+
for (int i = 4; i < 12; ++i) {
44+
TargetIdGenerator a = TargetIdGenerator::LocalStoreTargetIdGenerator(i);
45+
TargetIdGenerator b = TargetIdGenerator::SyncEngineTargetIdGenerator(i);
46+
EXPECT_EQ(i + 2 & ~1, a.NextId());
47+
EXPECT_EQ(i + 1 | 1, b.NextId());
48+
}
49+
50+
EXPECT_EQ(13, TargetIdGenerator::SyncEngineTargetIdGenerator(12).NextId());
51+
EXPECT_EQ(24, TargetIdGenerator::LocalStoreTargetIdGenerator(22).NextId());
52+
}
53+
54+
TEST(TargetIdGenerator, Increment) {
55+
TargetIdGenerator a = TargetIdGenerator::LocalStoreTargetIdGenerator(0);
56+
EXPECT_EQ(2, a.NextId());
57+
EXPECT_EQ(4, a.NextId());
58+
EXPECT_EQ(6, a.NextId());
59+
60+
TargetIdGenerator b = TargetIdGenerator::LocalStoreTargetIdGenerator(46);
61+
EXPECT_EQ(48, b.NextId());
62+
EXPECT_EQ(50, b.NextId());
63+
EXPECT_EQ(52, b.NextId());
64+
EXPECT_EQ(54, b.NextId());
65+
66+
TargetIdGenerator c = TargetIdGenerator::SyncEngineTargetIdGenerator(0);
67+
EXPECT_EQ(1, c.NextId());
68+
EXPECT_EQ(3, c.NextId());
69+
EXPECT_EQ(5, c.NextId());
70+
71+
TargetIdGenerator d = TargetIdGenerator::SyncEngineTargetIdGenerator(46);
72+
EXPECT_EQ(47, d.NextId());
73+
EXPECT_EQ(49, d.NextId());
74+
EXPECT_EQ(51, d.NextId());
75+
EXPECT_EQ(53, d.NextId());
76+
}
77+
78+
} // namespace core
79+
} // namespace firestore
80+
} // namespace firebase

0 commit comments

Comments
 (0)