Skip to content

kiwi‐cpp引擎层详细依赖关系

问问神奇海螺 edited this page Aug 6, 2025 · 1 revision

kiwi-cpp中引擎层依赖关系

store、strage、redis的关系

Store (单例)
├── DB[0] (数据库实例0)
│   └── Storage (存储引擎)
│       ├── Redis[0] (Redis实例0) -> RocksDB[0]
│       ├── Redis[1] (Redis实例1) -> RocksDB[1]
│       └── Redis[2] (Redis实例2) -> RocksDB[2]
├── DB[1] (数据库实例1)
│   └── Storage (存储引擎)
│       ├── Redis[0] (Redis实例0) -> RocksDB[0]
│       ├── Redis[1] (Redis实例1) -> RocksDB[1]
│       └── Redis[2] (Redis实例2) -> RocksDB[2]
└── DB[n] (数据库实例n)
    └── Storage (存储引擎)
        ├── Redis[0] (Redis实例0) -> RocksDB[0]
        ├── Redis[1] (Redis实例1) -> RocksDB[1]
        └── Redis[2] (Redis实例2) -> RocksDB[2]
  • Store:系统级管理器,负责多数据库实例的协调
  • Storage:存储引擎核心,实现Redis协议,管理多个Redis实例
  • Redis:单个存储实例,封装RocksDB,提供具体的数据操作

引用关系

Store 类的引用关系

Store (单例)
├── 包含: std::vector<std::unique_ptr<DB>> backends_
│   └── DB 类
│       └── 包含: std::unique_ptr<storage::Storage> storage_
│           └── Storage 类
│               └── 包含: std::vector<std::unique_ptr<Redis>> insts_
│                   └── Redis 类
│                       └── 包含: rocksdb::DB* db_

Storage 类的引用关系

Storage
├── 包含: std::vector<std::unique_ptr<Redis>> insts_ (Redis实例集合)
├── 包含: std::unique_ptr<SlotIndexer> slot_indexer_ (槽位索引器)
├── 包含: std::shared_ptr<LockMgr> lock_mgr_ (锁管理器)
├── 包含: std::unique_ptr<LRUCache<std::string, std::string>> cursors_store_ (游标缓存)
└── 包含: std::queue<BGTask> bg_tasks_queue_ (后台任务队列)

Redis 类的引用关系

Redis
├── 包含: Storage* storage_ (指向Storage的指针)
├── 包含: std::shared_ptr<LockMgr> lock_mgr_ (锁管理器)
├── 包含: rocksdb::DB* db_ (RocksDB实例)
├── 包含: std::vector<rocksdb::ColumnFamilyHandle*> handles_ (列族句柄)
├── 包含: std::unique_ptr<LRUCache<std::string, std::string>> scan_cursors_store_ (扫描游标缓存)
├── 包含: std::unique_ptr<LRUCache<std::string, size_t>> spop_counts_store_ (SPOP计数缓存)
└── 包含: std::unique_ptr<LRUCache<std::string, KeyStatistics>> statistics_store_ (统计缓存)

函数调用关系

Store类的函数调用关系

kiwi.cc::main()
└── STORE_INST.Init(g_config.databases)
    └── Store::Init(int db_number)
        ├── 创建 DB 实例: std::make_unique<DB>(i, g_config.db_path)
        └── DB::Open()
            └── DB::Open()
                └── storage_->Open(storage_options, db_path_)
                    └── Storage::Open()
                        ├── 创建 Redis 实例: std::make_unique<Redis>(this, index, lock_mgr_)
                        └── Redis::Open()

命令处理调用链

cmd_*.cc (各种命令处理文件)
└── STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->[Command]()
    ├── Store::GetBackend(int32_t index)
    ├── DB::GetStorage()
    └── Storage::[Command]() (如 Set, Get, HSet 等)
        └── Storage::GetDBInstance(key)
            └── Redis::[Command]()

数据路由调用链

Storage::[Command](key, ...)
├── Storage::GetDBInstance(key)
│   ├── SlotIndexer::GetInstanceID(GetSlotID(key))
│   └── 返回 insts_[inst_index]
└── Redis::[Command](key, ...)

数据操作调用链

Redis::[Command](key, ...)
├── 获取锁: lock_mgr_->Lock(key)
├── 执行 RocksDB 操作: db_->[Operation]()
├── 更新统计信息: UpdateSpecificKeyStatistics()
├── 检查压缩: AddCompactKeyTaskIfNeeded()
└── 释放锁: lock_mgr_->Unlock(key)

关键调用路径事例

Client::ProcessCommand("SET key value")
├── cmd_kv.cc::SetCommand()
├── STORE_INST.GetBackend(client->GetCurrentDB())->GetStorage()->Set(key, value)
│   ├── Store::GetBackend(db_index)
│   ├── DB::GetStorage()
│   └── Storage::Set(key, value)
│       ├── Storage::GetDBInstance(key)
│       │   ├── SlotIndexer::GetInstanceID(GetSlotID(key))
│       │   └── 返回对应的 Redis 实例
│       └── Redis::Set(key, value)
│           ├── LockMgr::Lock(key)
│           ├── rocksdb::DB::Put(key, value)
│           ├── 更新统计信息
│           └── LockMgr::Unlock(key)
└── 返回结果给客户端

Clone this wiki locally