Skip to content

Commit c859d61

Browse files
添加对比起实现说明
1 parent c86265d commit c859d61

File tree

11 files changed

+103
-114
lines changed

11 files changed

+103
-114
lines changed

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ else (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
7272
endif (NOT CMAKE_C_FLAGS MATCHES "-Wstrict-prototypes")
7373

7474
# Disable C++ exceptions.
75-
string(REGEX REPLACE "-fexceptions" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
76-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions")
75+
# string(REGEX REPLACE "-fexceptions" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
76+
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions")
7777

7878
# Disable RTTI.
7979
string(REGEX REPLACE "-frtti" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")

db/db_impl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ namespace leveldb {
219219
bool background_compaction_scheduled_ GUARDED_BY(mutex_);
220220

221221
ManualCompaction *manual_compaction_ GUARDED_BY(mutex_);
222-
222+
// GUARDED_BY(mutex_); clang中用于编译器的静态检查
223223
VersionSet *const versions_ GUARDED_BY(mutex_);
224224

225225
// Have we encountered a background error in paranoid mode?

demo/leveldb_exec.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ int main(int argc, char *argv[]) {
8888
// 为数据库创建快照
8989
leveldb::ReadOptions readOptions;
9090
readOptions.snapshot = db->GetSnapshot();
91+
// 这里创建的迭代器中缩影的数据是稳定版本的数据,而不是实时更新的值
9192
leveldb::Iterator* iter = db->NewIterator(readOptions);
9293
delete iter;
9394
db->ReleaseSnapshot(readOptions.snapshot);

demo/test.cpp

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -28,52 +28,52 @@
2828
#include <cstdlib> // 用于Windows上的_mkdir函数
2929
#include <sys/stat.h> // 用于Linux上的mkdir函数
3030

31-
using namespace std;
32-
3331

34-
struct ConsumptionOffset {
35-
std::map<TopicName, std::map<PatitionNo, Offset>> consumeOffsets; // 单个消费者组偏移量信息
36-
};
32+
#include <optional>
33+
#include <string>
34+
#include <iostream>
3735

38-
struct ConsumerInfo {
39-
std::map<TopicName, std::vector<partitionNo>> consumerTopics; // 消费主题信息
40-
uint64_t arbVersion; //< 消费者仲裁版本号
41-
}
36+
using namespace std;
4237

43-
struct ConsumerCoordinatorInfo {
44-
std::map<ConsumerInstanceId, ConsumerInfo> consumerCoordinatorResults; //< 消费协调结果
45-
};
4638

47-
class ConsumerCoordinator {
39+
template<typename EnvType>
40+
class SingletonEnv {
4841
public:
49-
ConsumerCoordinator(std::string &groupId, ConsumerCoordinatorInfo &consumerCoordinatorInfo) {}
50-
// 新增消费者
51-
int32_t AddConsumer();
52-
// 减少消费者
53-
int32_t DeleteConsumer();
54-
// 网络中断
55-
int32_t NetworkDisconnected();
42+
SingletonEnv() {
43+
static_assert(sizeof(env_storage_) >= sizeof(EnvType),
44+
"env_storage_ will not fit the Env");
45+
static_assert(alignof(decltype(env_storage_)) >= alignof(EnvType),
46+
"env_storage_ does not meet the Env's alignment needs");
47+
// std::aligned_storage<sizeof(EnvType), alignof(EnvType)>::type 保证了内存大小刚好是 EnvType 大小
48+
new(&env_storage_) EnvType();
49+
}
5650

51+
~SingletonEnv() = default;
5752

53+
SingletonEnv(const SingletonEnv &) = delete;
5854

59-
private:
60-
std::string m_groupId; //< 消费者组 uuid
61-
ConsumerCoordinatorInfo m_consumerInfo; //< 消费者实例id <==> 消费者实例消费分配方案
62-
63-
};
55+
SingletonEnv &operator=(const SingletonEnv &) = delete;
6456

57+
Env *env() { return reinterpret_cast<Env *>(&env_storage_); }
6558

66-
class ConsumerCoordinatorOffsetMng {
67-
public:
68-
uint64_t GetOffset(std::string& groupId, std::string& topicName, int32_t partitionNo) { }
59+
static void AssertEnvNotInitialized() {
60+
}
6961

7062
private:
71-
std::map<GroupId, ConsumptionOffset> m_consumerrCoordinatorOffset; //< GroupId <==> ConsumerCoordinatorOffset
63+
typename std::aligned_storage<sizeof(EnvType), alignof(EnvType)>::type
64+
env_storage_;
7265
};
7366

7467

7568

69+
70+
7671
int main() {
72+
Derived derived;
73+
derived.a();
74+
75+
derived.name = 12;
76+
7777

7878

7979
return 0;

0 commit comments

Comments
 (0)