diff --git a/src/common/bustub_instance.cpp b/src/common/bustub_instance.cpp index 45aeaf974..1ec6e9a97 100644 --- a/src/common/bustub_instance.cpp +++ b/src/common/bustub_instance.cpp @@ -45,7 +45,7 @@ auto BusTubInstance::MakeExecutorContext(Transaction *txn, bool is_modify) -> st lock_manager_.get(), is_modify); } -BusTubInstance::BusTubInstance(const std::string &db_file_name, size_t bpm_size) { +BusTubInstance::BusTubInstance(const std::filesystem::path &db_file_name, size_t bpm_size) { enable_logging = false; // Storage related. diff --git a/src/include/common/bustub_instance.h b/src/include/common/bustub_instance.h index 2b4c77ba9..f0f9e1617 100644 --- a/src/include/common/bustub_instance.h +++ b/src/include/common/bustub_instance.h @@ -12,6 +12,7 @@ #pragma once +#include #include #include #include @@ -241,7 +242,7 @@ class BusTubInstance { auto MakeExecutorContext(Transaction *txn, bool is_modify) -> std::unique_ptr; public: - explicit BusTubInstance(const std::string &db_file_name, size_t bpm_size = 128); + explicit BusTubInstance(const std::filesystem::path &db_file_name, size_t bpm_size = 128); explicit BusTubInstance(size_t bpm_size = 128); diff --git a/src/include/storage/disk/disk_manager.h b/src/include/storage/disk/disk_manager.h index 04e0ee6d6..34cb8acc0 100644 --- a/src/include/storage/disk/disk_manager.h +++ b/src/include/storage/disk/disk_manager.h @@ -13,6 +13,7 @@ #pragma once #include +#include #include #include // NOLINT #include // NOLINT @@ -32,7 +33,7 @@ class DiskManager { * Creates a new disk manager that writes to the specified database file. * @param db_file the file name of the database file to write to */ - explicit DiskManager(const std::string &db_file); + explicit DiskManager(const std::filesystem::path &db_file); /** FOR TEST / LEADERBOARD ONLY, used by DiskManagerMemory */ DiskManager() = default; @@ -96,10 +97,10 @@ class DiskManager { auto GetFileSize(const std::string &file_name) -> int; // stream to write log file std::fstream log_io_; - std::string log_name_; + std::filesystem::path log_name_; // stream to write db file std::fstream db_io_; - std::string file_name_; + std::filesystem::path file_name_; int num_flushes_{0}; int num_writes_{0}; bool flush_log_{false}; diff --git a/src/include/storage/index/b_plus_tree.h b/src/include/storage/index/b_plus_tree.h index 09e2cbdbf..a2aa961d7 100644 --- a/src/include/storage/index/b_plus_tree.h +++ b/src/include/storage/index/b_plus_tree.h @@ -12,6 +12,7 @@ #include #include +#include #include #include #include @@ -95,7 +96,7 @@ class BPlusTree { void Print(BufferPoolManager *bpm); // Draw the B+ tree - void Draw(BufferPoolManager *bpm, const std::string &outf); + void Draw(BufferPoolManager *bpm, const std::filesystem::path &outf); /** * @brief draw a B+ tree, below is a printed @@ -111,10 +112,10 @@ class BPlusTree { auto DrawBPlusTree() -> std::string; // read data from file and insert one by one - void InsertFromFile(const std::string &file_name, Transaction *txn = nullptr); + void InsertFromFile(const std::filesystem::path &file_name, Transaction *txn = nullptr); // read data from file and remove one by one - void RemoveFromFile(const std::string &file_name, Transaction *txn = nullptr); + void RemoveFromFile(const std::filesystem::path &file_name, Transaction *txn = nullptr); /** * @brief Read batch operations from input file, below is a sample file format @@ -125,7 +126,7 @@ class BPlusTree { * (3) (7) * (1,2) (3,4) (5,6) (7,10,30) // The output tree example */ - void BatchOpsFromFile(const std::string &file_name, Transaction *txn = nullptr); + void BatchOpsFromFile(const std::filesystem::path &file_name, Transaction *txn = nullptr); private: /* Debug Routines for FREE!! */ diff --git a/src/storage/disk/disk_manager.cpp b/src/storage/disk/disk_manager.cpp index 8faaa1ad4..712a2aa82 100644 --- a/src/storage/disk/disk_manager.cpp +++ b/src/storage/disk/disk_manager.cpp @@ -30,13 +30,8 @@ static char *buffer_used; * Constructor: open/create a single database file & log file * @input db_file: database file name */ -DiskManager::DiskManager(const std::string &db_file) : file_name_(db_file) { - std::string::size_type n = file_name_.rfind('.'); - if (n == std::string::npos) { - LOG_DEBUG("wrong file format"); - return; - } - log_name_ = file_name_.substr(0, n) + ".log"; +DiskManager::DiskManager(const std::filesystem::path &db_file) : file_name_(db_file) { + log_name_ = file_name_.filename().stem().string() + ".log"; log_io_.open(log_name_, std::ios::binary | std::ios::in | std::ios::app | std::ios::out); // directory or file does not exist diff --git a/src/storage/index/b_plus_tree.cpp b/src/storage/index/b_plus_tree.cpp index d4f09b71e..9afdfffd5 100644 --- a/src/storage/index/b_plus_tree.cpp +++ b/src/storage/index/b_plus_tree.cpp @@ -121,7 +121,7 @@ auto BPLUSTREE_TYPE::GetRootPageId() -> page_id_t { return 0; } * Read data from file and insert one by one */ INDEX_TEMPLATE_ARGUMENTS -void BPLUSTREE_TYPE::InsertFromFile(const std::string &file_name, Transaction *txn) { +void BPLUSTREE_TYPE::InsertFromFile(const std::filesystem::path &file_name, Transaction *txn) { int64_t key; std::ifstream input(file_name); while (input >> key) { @@ -136,7 +136,7 @@ void BPLUSTREE_TYPE::InsertFromFile(const std::string &file_name, Transaction *t * Read data from file and remove one by one */ INDEX_TEMPLATE_ARGUMENTS -void BPLUSTREE_TYPE::RemoveFromFile(const std::string &file_name, Transaction *txn) { +void BPLUSTREE_TYPE::RemoveFromFile(const std::filesystem::path &file_name, Transaction *txn) { int64_t key; std::ifstream input(file_name); while (input >> key) { @@ -151,7 +151,7 @@ void BPLUSTREE_TYPE::RemoveFromFile(const std::string &file_name, Transaction *t * Read data from file and insert/remove one by one */ INDEX_TEMPLATE_ARGUMENTS -void BPLUSTREE_TYPE::BatchOpsFromFile(const std::string &file_name, Transaction *txn) { +void BPLUSTREE_TYPE::BatchOpsFromFile(const std::filesystem::path &file_name, Transaction *txn) { int64_t key; char instruction; std::ifstream input(file_name); @@ -222,7 +222,7 @@ void BPLUSTREE_TYPE::PrintTree(page_id_t page_id, const BPlusTreePage *page) { * This method is used for debug only, You don't need to modify */ INDEX_TEMPLATE_ARGUMENTS -void BPLUSTREE_TYPE::Draw(BufferPoolManager *bpm, const std::string &outf) { +void BPLUSTREE_TYPE::Draw(BufferPoolManager *bpm, const std::filesystem::path &outf) { if (IsEmpty()) { LOG_WARN("Drawing an empty tree"); return; diff --git a/test/buffer/buffer_pool_manager_test.cpp b/test/buffer/buffer_pool_manager_test.cpp index 78f8c9a26..3a55ddb29 100644 --- a/test/buffer/buffer_pool_manager_test.cpp +++ b/test/buffer/buffer_pool_manager_test.cpp @@ -24,7 +24,7 @@ namespace bustub { // NOLINTNEXTLINE // Check whether pages containing terminal characters can be recovered TEST(BufferPoolManagerTest, DISABLED_BinaryDataTest) { - const std::string db_name = "test.bustub"; + const std::filesystem::path db_name("test.bustub"); const size_t buffer_pool_size = 10; const size_t k = 5; @@ -98,7 +98,7 @@ TEST(BufferPoolManagerTest, DISABLED_BinaryDataTest) { // NOLINTNEXTLINE TEST(BufferPoolManagerTest, DISABLED_SampleTest) { - const std::string db_name = "test.bustub"; + const std::filesystem::path db_name("test.bustub"); const size_t buffer_pool_size = 10; const size_t k = 5; diff --git a/test/container/disk/hash/hash_table_page_test.cpp b/test/container/disk/hash/hash_table_page_test.cpp index 92f7d2c24..429026ba6 100644 --- a/test/container/disk/hash/hash_table_page_test.cpp +++ b/test/container/disk/hash/hash_table_page_test.cpp @@ -24,7 +24,8 @@ // // NOLINTNEXTLINE // TEST(HashTablePageTest, DISABLED_DirectoryPageSampleTest) { -// auto *disk_manager = new DiskManager("test.bustub"); +// std::filesystem::path fname("test.bustub"); +// auto *disk_manager = new DiskManager(fname); // auto *bpm = new BufferPoolManager(5, disk_manager); // // get a directory page from the BufferPoolManager @@ -57,7 +58,8 @@ // // NOLINTNEXTLINE // TEST(HashTablePageTest, DISABLED_BucketPageSampleTest) { -// auto *disk_manager = new DiskManager("test.bustub"); +// std::filesystem::path fname("test.bustub"); +// auto *disk_manager = new DiskManager(fname); // auto *bpm = new BufferPoolManager(5, disk_manager); // // get a bucket page from the BufferPoolManager diff --git a/test/container/disk/hash/hash_table_test.cpp b/test/container/disk/hash/hash_table_test.cpp index d6279ce7a..a488d4bf3 100644 --- a/test/container/disk/hash/hash_table_test.cpp +++ b/test/container/disk/hash/hash_table_test.cpp @@ -25,7 +25,8 @@ // // NOLINTNEXTLINE // TEST(HashTableTest, DISABLED_SampleTest) { -// auto *disk_manager = new DiskManager("test.bustub"); +// std::filesystem::path fname("test.bustub"); +// auto *disk_manager = new DiskManager(fname); // auto *bpm = new BufferPoolManager(50, disk_manager); // DiskExtendibleHashTable ht("blah", bpm, IntComparator(), HashFunction()); diff --git a/test/storage/disk_manager_test.cpp b/test/storage/disk_manager_test.cpp index d0f7ad9f3..4ae4c825f 100644 --- a/test/storage/disk_manager_test.cpp +++ b/test/storage/disk_manager_test.cpp @@ -37,7 +37,7 @@ class DiskManagerTest : public ::testing::Test { TEST_F(DiskManagerTest, ReadWritePageTest) { char buf[BUSTUB_PAGE_SIZE] = {0}; char data[BUSTUB_PAGE_SIZE] = {0}; - std::string db_file("test.bustub"); + std::filesystem::path db_file("test.bustub"); auto dm = DiskManager(db_file); std::strncpy(data, "A test string.", sizeof(data)); @@ -59,7 +59,7 @@ TEST_F(DiskManagerTest, ReadWritePageTest) { TEST_F(DiskManagerTest, ReadWriteLogTest) { char buf[16] = {0}; char data[16] = {0}; - std::string db_file("test.bustub"); + std::filesystem::path db_file("test.bustub"); auto dm = DiskManager(db_file); std::strncpy(data, "A test string.", sizeof(data)); diff --git a/test/table/tuple_test.cpp b/test/table/tuple_test.cpp index ac953db4b..12449d215 100644 --- a/test/table/tuple_test.cpp +++ b/test/table/tuple_test.cpp @@ -37,7 +37,8 @@ TEST(TupleTest, DISABLED_TableHeapTest) { Tuple tuple = ConstructTuple(&schema); // create transaction - auto *disk_manager = new DiskManager("test.bustub"); + std::filesystem::path fname("test.bustub"); + auto *disk_manager = new DiskManager(fname); auto *buffer_pool_manager = new BufferPoolManager(50, disk_manager); auto *table = new TableHeap(buffer_pool_manager);