Skip to content

Commit

Permalink
Add const and static qualifier to Disk methods
Browse files Browse the repository at this point in the history
- Remove disk_ member in Catalog

Signed-off-by: Wenbo Li <[email protected]>
  • Loading branch information
hnjylwb committed Mar 13, 2024
1 parent 5f3c548 commit 3e58a6d
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 33 deletions.
12 changes: 6 additions & 6 deletions src/catalog/simple_catalog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@

namespace huadb {

SimpleCatalog::SimpleCatalog(Disk &disk, BufferPool &buffer_pool, LogManager &log_manager, oid_t next_oid)
: disk_(disk), buffer_pool_(buffer_pool), log_manager_(log_manager), oid_manager_(next_oid) {}
SimpleCatalog::SimpleCatalog(BufferPool &buffer_pool, LogManager &log_manager, oid_t next_oid)
: buffer_pool_(buffer_pool), log_manager_(log_manager), oid_manager_(next_oid) {}

void SimpleCatalog::CreateSystemTables() {
disk_.CreateDirectory(std::to_string(TEMP_DATABASE_OID));
Disk::CreateDirectory(std::to_string(TEMP_DATABASE_OID));
// 切换当前数据库oid
current_database_oid_ = TEMP_DATABASE_OID;
}
Expand Down Expand Up @@ -96,7 +96,7 @@ void SimpleCatalog::CreateTable(const std::string &table_name, const ColumnList
assert(db_oid != SYSTEM_DATABASE_OID);
}
if (new_table) {
disk_.CreateFile(Disk::GetFilePath(db_oid, oid));
Disk::CreateFile(Disk::GetFilePath(db_oid, oid));
}
name2oid_[table_name] = oid;
oid2table_[oid] = std::make_shared<Table>(buffer_pool_, log_manager_, oid, db_oid, column_list, new_table);
Expand All @@ -121,14 +121,14 @@ void SimpleCatalog::DropTable(const std::string &table_name) {
oid_t table_oid = oid_manager_.GetEntryOid(OidType::TABLE, table_name);
// Step2. 实际删除表
// 磁盘中删除对应项
disk_.RemoveFile(Disk::GetFilePath(current_database_oid_, table_oid));
Disk::RemoveFile(Disk::GetFilePath(current_database_oid_, table_oid));
name2oid_.erase(table_name);
oid2table_.erase(table_oid);

// Step3. OidManager删除对应项
oid_manager_.DropEntry(OidType::TABLE, table_name);
// Step4: 删除对应的meta文件
disk_.RemoveFile(std::to_string(current_database_oid_) + "/" + table_name + ".meta");
Disk::RemoveFile(std::to_string(current_database_oid_) + "/" + table_name + ".meta");
std::ofstream db_out(std::to_string(current_database_oid_) + "/tables", std::ios::app);
db_out << "~" << table_name << " ";
}
Expand Down
4 changes: 1 addition & 3 deletions src/catalog/simple_catalog.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@

namespace huadb {

class Disk;
class BufferPool;
class LogManager;
class Table;
class Index;

class SimpleCatalog {
public:
SimpleCatalog(Disk &disk, BufferPool &buffer_pool, LogManager &log_manager, oid_t next_oid = PRESERVED_OID);
SimpleCatalog(BufferPool &buffer_pool, LogManager &log_manager, oid_t next_oid = PRESERVED_OID);
// 创建系统表,仅初始化系统使用
void CreateSystemTables();
// 加载系统表,系统已经初始化过时使用
Expand Down Expand Up @@ -68,7 +67,6 @@ class SimpleCatalog {
void SetDistinct(const std::string &table_name, const std::string &column_name, uint32_t distinct);

private:
Disk &disk_;
BufferPool &buffer_pool_;
LogManager &log_manager_;

Expand Down
18 changes: 9 additions & 9 deletions src/catalog/system_catalog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@

namespace huadb {

SystemCatalog::SystemCatalog(Disk &disk, BufferPool &buffer_pool, LogManager &log_manager, oid_t next_oid)
: disk_(disk), buffer_pool_(buffer_pool), log_manager_(log_manager), oid_manager_(next_oid) {}
SystemCatalog::SystemCatalog(BufferPool &buffer_pool, LogManager &log_manager, oid_t next_oid)
: buffer_pool_(buffer_pool), log_manager_(log_manager), oid_manager_(next_oid) {}

void SystemCatalog::CreateSystemTables() {
// 创建 system 数据库
disk_.CreateDirectory(std::to_string(SYSTEM_DATABASE_OID));
Disk::CreateDirectory(std::to_string(SYSTEM_DATABASE_OID));
// 创建系统表
CreateTable(TABLE_META_NAME, table_meta_schema, TABLE_META_OID, SYSTEM_DATABASE_OID, true);
CreateTable(DATABASE_META_NAME, database_meta_schema, DATABASE_META_OID, SYSTEM_DATABASE_OID, true);
Expand Down Expand Up @@ -58,8 +58,8 @@ void SystemCatalog::CreateDatabase(const std::string &database_name, bool exists
values.emplace_back(database_name);
GetTable(DATABASE_META_OID)->InsertRecord(std::make_shared<Record>(std::move(values)), DDL_XID, DDL_CID, false);
// Step4. 实际创建数据库的文件夹
if (!disk_.DirectoryExists(std::to_string(db_oid))) {
disk_.CreateDirectory(std::to_string(db_oid));
if (!Disk::DirectoryExists(std::to_string(db_oid))) {
Disk::CreateDirectory(std::to_string(db_oid));
}
}

Expand Down Expand Up @@ -108,8 +108,8 @@ void SystemCatalog::DropDatabase(const std::string &database_name, bool missing_
// Step5. OidManager删除对应项
oid_manager_.DropEntry(OidType::DATABASE, database_name);
// Step6. 实际删除数据库的文件夹
if (disk_.DirectoryExists(std::to_string(db_oid))) {
disk_.RemoveDirectory(std::to_string(db_oid));
if (Disk::DirectoryExists(std::to_string(db_oid))) {
Disk::RemoveDirectory(std::to_string(db_oid));
}
}

Expand Down Expand Up @@ -195,7 +195,7 @@ void SystemCatalog::CreateTable(const std::string &table_name, const ColumnList
throw DbException("Cannot create table in system database");
}
if (new_table) {
disk_.CreateFile(Disk::GetFilePath(db_oid, oid));
Disk::CreateFile(Disk::GetFilePath(db_oid, oid));
}
oid2table_[oid] = std::make_shared<Table>(buffer_pool_, log_manager_, oid, db_oid, column_list, new_table);

Expand Down Expand Up @@ -225,7 +225,7 @@ void SystemCatalog::DropTable(const std::string &table_name) {
oid_t table_oid = oid_manager_.GetEntryOid(OidType::TABLE, table_name);
// Step2. 实际删除表
// 磁盘中删除对应项
disk_.RemoveFile(Disk::GetFilePath(current_database_oid_, table_oid));
Disk::RemoveFile(Disk::GetFilePath(current_database_oid_, table_oid));
oid2table_.erase(table_oid);

// Step3. OidManager删除对应项
Expand Down
4 changes: 1 addition & 3 deletions src/catalog/system_catalog.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@

namespace huadb {

class Disk;
class BufferPool;
class LogManager;
class Table;
class Index;

class SystemCatalog {
public:
SystemCatalog(Disk &disk, BufferPool &buffer_pool, LogManager &log_manager, oid_t next_oid = PRESERVED_OID);
SystemCatalog(BufferPool &buffer_pool, LogManager &log_manager, oid_t next_oid = PRESERVED_OID);

// 创建系统表,仅初始化系统使用
void CreateSystemTables();
Expand Down Expand Up @@ -82,7 +81,6 @@ class SystemCatalog {
void LoadTableMeta();
void LoadStatistics();

Disk &disk_;
BufferPool &buffer_pool_;
LogManager &log_manager_;

Expand Down
2 changes: 1 addition & 1 deletion src/database/database_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ DatabaseEngine::DatabaseEngine() {
buffer_pool_ = std::make_shared<BufferPool>(*disk_, *log_manager_);
log_manager_->SetBufferPool(buffer_pool_);

catalog_ = std::make_unique<Catalog>(*disk_, *buffer_pool_, *log_manager_, oid);
catalog_ = std::make_unique<Catalog>(*buffer_pool_, *log_manager_, oid);
log_manager_->SetCatalog(catalog_);

// 如果不存在 init 文件,创建系统表;如存在,则载入系统表
Expand Down
2 changes: 1 addition & 1 deletion src/storage/buffer_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class BufferPool {
std::shared_ptr<Page> GetPage(oid_t db_oid, oid_t table_oid, pageid_t page_id);
// 获取一个已经存在的页面
std::shared_ptr<Page> NewPage(oid_t db_oid, oid_t table_oid, pageid_t page_id);
// 将所有页面刷到磁盘
// 将所有页面刷到磁盘,regular_only 为 true 时只刷普通表页面
void Flush(bool regular_only = false);
// 清空 buffer pool,不刷脏,用于数据库故障模拟
void Clear();
Expand Down
2 changes: 1 addition & 1 deletion src/storage/disk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ void Disk::WriteLog(uint32_t offset, uint32_t count, const char *data) {
log_fs_.flush();
}

uint32_t Disk::GetAccessCount() { return access_count_; }
uint32_t Disk::GetAccessCount() const { return access_count_; }

std::string Disk::GetFilePath(oid_t db_oid, oid_t table_oid) {
return std::to_string(db_oid) + "/" + std::to_string(table_oid);
Expand Down
19 changes: 10 additions & 9 deletions src/storage/disk.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ class Disk {
public:
Disk();
~Disk();
bool DirectoryExists(const std::string &path);
void ChangeDirectory(const std::string &path);
void CreateDirectory(const std::string &path);
void RemoveDirectory(const std::string &path);

bool FileExists(const std::string &path);
void CreateFile(const std::string &path);
void RemoveFile(const std::string &path);
static bool DirectoryExists(const std::string &path);
static void ChangeDirectory(const std::string &path);
static void CreateDirectory(const std::string &path);
static void RemoveDirectory(const std::string &path);

static bool FileExists(const std::string &path);
static void CreateFile(const std::string &path);
static void RemoveFile(const std::string &path);

void OpenFile(const std::string &path);
void CloseFile(const std::string &path);

Expand All @@ -30,7 +31,7 @@ class Disk {
void ReadLog(uint32_t offset, uint32_t count, char *data);
void WriteLog(uint32_t offset, uint32_t count, const char *data);

uint32_t GetAccessCount();
uint32_t GetAccessCount() const;

static std::string GetFilePath(oid_t db_oid, oid_t table_oid);

Expand Down

0 comments on commit 3e58a6d

Please sign in to comment.