Skip to content

Commit

Permalink
Changing type of filename from std::string to std::filesystem::path
Browse files Browse the repository at this point in the history
  • Loading branch information
prashanthduvvada committed Aug 21, 2024
1 parent edd5bfc commit 0c6365b
Show file tree
Hide file tree
Showing 11 changed files with 30 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/common/bustub_instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion src/include/common/bustub_instance.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#pragma once

#include <filesystem>
#include <iostream>
#include <memory>
#include <optional>
Expand Down Expand Up @@ -241,7 +242,7 @@ class BusTubInstance {
auto MakeExecutorContext(Transaction *txn, bool is_modify) -> std::unique_ptr<ExecutorContext>;

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);

Expand Down
7 changes: 4 additions & 3 deletions src/include/storage/disk/disk_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#pragma once

#include <atomic>
#include <filesystem>
#include <fstream>
#include <future> // NOLINT
#include <mutex> // NOLINT
Expand All @@ -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;
Expand Down Expand Up @@ -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};
Expand Down
9 changes: 5 additions & 4 deletions src/include/storage/index/b_plus_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include <algorithm>
#include <deque>
#include <filesystem>
#include <iostream>
#include <optional>
#include <queue>
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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!! */
Expand Down
9 changes: 2 additions & 7 deletions src/storage/disk/disk_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() / ".log";

log_io_.open(log_name_, std::ios::binary | std::ios::in | std::ios::app | std::ios::out);
// directory or file does not exist
Expand Down
8 changes: 4 additions & 4 deletions src/storage/index/b_plus_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions test/buffer/buffer_pool_manager_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down
6 changes: 4 additions & 2 deletions test/container/disk/hash/hash_table_page_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion test/container/disk/hash/hash_table_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, int, IntComparator> ht("blah", bpm, IntComparator(), HashFunction<int>());

Expand Down
4 changes: 2 additions & 2 deletions test/storage/disk_manager_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand All @@ -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));

Expand Down
3 changes: 2 additions & 1 deletion test/table/tuple_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 0c6365b

Please sign in to comment.