Skip to content

Commit 5545dbf

Browse files
CPP Standards (#726)
* Bustub to BusTub - Naming consistency * Changing type of filename from std::string to std::filesystem::path * Fixing Format Error * Small Fix * fix Format error
1 parent e956eb0 commit 5545dbf

File tree

11 files changed

+30
-28
lines changed

11 files changed

+30
-28
lines changed

src/common/bustub_instance.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ auto BusTubInstance::MakeExecutorContext(Transaction *txn, bool is_modify) -> st
4545
lock_manager_.get(), is_modify);
4646
}
4747

48-
BusTubInstance::BusTubInstance(const std::string &db_file_name, size_t bpm_size) {
48+
BusTubInstance::BusTubInstance(const std::filesystem::path &db_file_name, size_t bpm_size) {
4949
enable_logging = false;
5050

5151
// Storage related.

src/include/common/bustub_instance.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#pragma once
1414

15+
#include <filesystem>
1516
#include <iostream>
1617
#include <memory>
1718
#include <optional>
@@ -241,7 +242,7 @@ class BusTubInstance {
241242
auto MakeExecutorContext(Transaction *txn, bool is_modify) -> std::unique_ptr<ExecutorContext>;
242243

243244
public:
244-
explicit BusTubInstance(const std::string &db_file_name, size_t bpm_size = 128);
245+
explicit BusTubInstance(const std::filesystem::path &db_file_name, size_t bpm_size = 128);
245246

246247
explicit BusTubInstance(size_t bpm_size = 128);
247248

src/include/storage/disk/disk_manager.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#pragma once
1414

1515
#include <atomic>
16+
#include <filesystem>
1617
#include <fstream>
1718
#include <future> // NOLINT
1819
#include <mutex> // NOLINT
@@ -32,7 +33,7 @@ class DiskManager {
3233
* Creates a new disk manager that writes to the specified database file.
3334
* @param db_file the file name of the database file to write to
3435
*/
35-
explicit DiskManager(const std::string &db_file);
36+
explicit DiskManager(const std::filesystem::path &db_file);
3637

3738
/** FOR TEST / LEADERBOARD ONLY, used by DiskManagerMemory */
3839
DiskManager() = default;
@@ -96,10 +97,10 @@ class DiskManager {
9697
auto GetFileSize(const std::string &file_name) -> int;
9798
// stream to write log file
9899
std::fstream log_io_;
99-
std::string log_name_;
100+
std::filesystem::path log_name_;
100101
// stream to write db file
101102
std::fstream db_io_;
102-
std::string file_name_;
103+
std::filesystem::path file_name_;
103104
int num_flushes_{0};
104105
int num_writes_{0};
105106
bool flush_log_{false};

src/include/storage/index/b_plus_tree.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include <algorithm>
1414
#include <deque>
15+
#include <filesystem>
1516
#include <iostream>
1617
#include <optional>
1718
#include <queue>
@@ -95,7 +96,7 @@ class BPlusTree {
9596
void Print(BufferPoolManager *bpm);
9697

9798
// Draw the B+ tree
98-
void Draw(BufferPoolManager *bpm, const std::string &outf);
99+
void Draw(BufferPoolManager *bpm, const std::filesystem::path &outf);
99100

100101
/**
101102
* @brief draw a B+ tree, below is a printed
@@ -111,10 +112,10 @@ class BPlusTree {
111112
auto DrawBPlusTree() -> std::string;
112113

113114
// read data from file and insert one by one
114-
void InsertFromFile(const std::string &file_name, Transaction *txn = nullptr);
115+
void InsertFromFile(const std::filesystem::path &file_name, Transaction *txn = nullptr);
115116

116117
// read data from file and remove one by one
117-
void RemoveFromFile(const std::string &file_name, Transaction *txn = nullptr);
118+
void RemoveFromFile(const std::filesystem::path &file_name, Transaction *txn = nullptr);
118119

119120
/**
120121
* @brief Read batch operations from input file, below is a sample file format
@@ -125,7 +126,7 @@ class BPlusTree {
125126
* (3) (7)
126127
* (1,2) (3,4) (5,6) (7,10,30) // The output tree example
127128
*/
128-
void BatchOpsFromFile(const std::string &file_name, Transaction *txn = nullptr);
129+
void BatchOpsFromFile(const std::filesystem::path &file_name, Transaction *txn = nullptr);
129130

130131
private:
131132
/* Debug Routines for FREE!! */

src/storage/disk/disk_manager.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,8 @@ static char *buffer_used;
3030
* Constructor: open/create a single database file & log file
3131
* @input db_file: database file name
3232
*/
33-
DiskManager::DiskManager(const std::string &db_file) : file_name_(db_file) {
34-
std::string::size_type n = file_name_.rfind('.');
35-
if (n == std::string::npos) {
36-
LOG_DEBUG("wrong file format");
37-
return;
38-
}
39-
log_name_ = file_name_.substr(0, n) + ".log";
33+
DiskManager::DiskManager(const std::filesystem::path &db_file) : file_name_(db_file) {
34+
log_name_ = file_name_.filename().stem().string() + ".log";
4035

4136
log_io_.open(log_name_, std::ios::binary | std::ios::in | std::ios::app | std::ios::out);
4237
// directory or file does not exist

src/storage/index/b_plus_tree.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ auto BPLUSTREE_TYPE::GetRootPageId() -> page_id_t { return 0; }
121121
* Read data from file and insert one by one
122122
*/
123123
INDEX_TEMPLATE_ARGUMENTS
124-
void BPLUSTREE_TYPE::InsertFromFile(const std::string &file_name, Transaction *txn) {
124+
void BPLUSTREE_TYPE::InsertFromFile(const std::filesystem::path &file_name, Transaction *txn) {
125125
int64_t key;
126126
std::ifstream input(file_name);
127127
while (input >> key) {
@@ -136,7 +136,7 @@ void BPLUSTREE_TYPE::InsertFromFile(const std::string &file_name, Transaction *t
136136
* Read data from file and remove one by one
137137
*/
138138
INDEX_TEMPLATE_ARGUMENTS
139-
void BPLUSTREE_TYPE::RemoveFromFile(const std::string &file_name, Transaction *txn) {
139+
void BPLUSTREE_TYPE::RemoveFromFile(const std::filesystem::path &file_name, Transaction *txn) {
140140
int64_t key;
141141
std::ifstream input(file_name);
142142
while (input >> key) {
@@ -151,7 +151,7 @@ void BPLUSTREE_TYPE::RemoveFromFile(const std::string &file_name, Transaction *t
151151
* Read data from file and insert/remove one by one
152152
*/
153153
INDEX_TEMPLATE_ARGUMENTS
154-
void BPLUSTREE_TYPE::BatchOpsFromFile(const std::string &file_name, Transaction *txn) {
154+
void BPLUSTREE_TYPE::BatchOpsFromFile(const std::filesystem::path &file_name, Transaction *txn) {
155155
int64_t key;
156156
char instruction;
157157
std::ifstream input(file_name);
@@ -222,7 +222,7 @@ void BPLUSTREE_TYPE::PrintTree(page_id_t page_id, const BPlusTreePage *page) {
222222
* This method is used for debug only, You don't need to modify
223223
*/
224224
INDEX_TEMPLATE_ARGUMENTS
225-
void BPLUSTREE_TYPE::Draw(BufferPoolManager *bpm, const std::string &outf) {
225+
void BPLUSTREE_TYPE::Draw(BufferPoolManager *bpm, const std::filesystem::path &outf) {
226226
if (IsEmpty()) {
227227
LOG_WARN("Drawing an empty tree");
228228
return;

test/buffer/buffer_pool_manager_test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace bustub {
2424
// NOLINTNEXTLINE
2525
// Check whether pages containing terminal characters can be recovered
2626
TEST(BufferPoolManagerTest, DISABLED_BinaryDataTest) {
27-
const std::string db_name = "test.bustub";
27+
const std::filesystem::path db_name("test.bustub");
2828
const size_t buffer_pool_size = 10;
2929
const size_t k = 5;
3030

@@ -98,7 +98,7 @@ TEST(BufferPoolManagerTest, DISABLED_BinaryDataTest) {
9898

9999
// NOLINTNEXTLINE
100100
TEST(BufferPoolManagerTest, DISABLED_SampleTest) {
101-
const std::string db_name = "test.bustub";
101+
const std::filesystem::path db_name("test.bustub");
102102
const size_t buffer_pool_size = 10;
103103
const size_t k = 5;
104104

test/container/disk/hash/hash_table_page_test.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424

2525
// // NOLINTNEXTLINE
2626
// TEST(HashTablePageTest, DISABLED_DirectoryPageSampleTest) {
27-
// auto *disk_manager = new DiskManager("test.bustub");
27+
// std::filesystem::path fname("test.bustub");
28+
// auto *disk_manager = new DiskManager(fname);
2829
// auto *bpm = new BufferPoolManager(5, disk_manager);
2930

3031
// // get a directory page from the BufferPoolManager
@@ -57,7 +58,8 @@
5758

5859
// // NOLINTNEXTLINE
5960
// TEST(HashTablePageTest, DISABLED_BucketPageSampleTest) {
60-
// auto *disk_manager = new DiskManager("test.bustub");
61+
// std::filesystem::path fname("test.bustub");
62+
// auto *disk_manager = new DiskManager(fname);
6163
// auto *bpm = new BufferPoolManager(5, disk_manager);
6264

6365
// // get a bucket page from the BufferPoolManager

test/container/disk/hash/hash_table_test.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525

2626
// // NOLINTNEXTLINE
2727
// TEST(HashTableTest, DISABLED_SampleTest) {
28-
// auto *disk_manager = new DiskManager("test.bustub");
28+
// std::filesystem::path fname("test.bustub");
29+
// auto *disk_manager = new DiskManager(fname);
2930
// auto *bpm = new BufferPoolManager(50, disk_manager);
3031
// DiskExtendibleHashTable<int, int, IntComparator> ht("blah", bpm, IntComparator(), HashFunction<int>());
3132

test/storage/disk_manager_test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class DiskManagerTest : public ::testing::Test {
3737
TEST_F(DiskManagerTest, ReadWritePageTest) {
3838
char buf[BUSTUB_PAGE_SIZE] = {0};
3939
char data[BUSTUB_PAGE_SIZE] = {0};
40-
std::string db_file("test.bustub");
40+
std::filesystem::path db_file("test.bustub");
4141
auto dm = DiskManager(db_file);
4242
std::strncpy(data, "A test string.", sizeof(data));
4343

@@ -59,7 +59,7 @@ TEST_F(DiskManagerTest, ReadWritePageTest) {
5959
TEST_F(DiskManagerTest, ReadWriteLogTest) {
6060
char buf[16] = {0};
6161
char data[16] = {0};
62-
std::string db_file("test.bustub");
62+
std::filesystem::path db_file("test.bustub");
6363
auto dm = DiskManager(db_file);
6464
std::strncpy(data, "A test string.", sizeof(data));
6565

0 commit comments

Comments
 (0)