Skip to content

Commit

Permalink
Remove unnecessary fstream close code
Browse files Browse the repository at this point in the history
  • Loading branch information
hnjylwb committed Mar 10, 2024
1 parent 46d2e85 commit d7bc952
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 27 deletions.
10 changes: 6 additions & 4 deletions src/bin/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
if(NOT EMSCRIPTEN)
add_executable(shell shell.cpp)
target_link_libraries(shell huadb linenoise)
add_executable(server server.cpp)
target_link_libraries(server huadb)
add_executable(client client.cpp)
target_link_libraries(client huadb linenoise)
add_executable(huadb-parser huadb-parser.cpp)
target_link_libraries(huadb-parser huadb)
add_executable(server server.cpp)
target_link_libraries(server huadb)
add_executable(shell shell.cpp)
target_link_libraries(shell huadb linenoise)
else()
add_executable(web-shell web-shell.cpp)
target_compile_options(web-shell PRIVATE -fwasm-exceptions)
Expand Down
51 changes: 51 additions & 0 deletions src/bin/huadb-parser.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>

#include "argparse/argparse.hpp"

namespace fs = std::filesystem;

enum class ParseMode { CONTROL, DATA, LOG };

int main(int argc, char *argv[]) {
argparse::ArgumentParser program("huadb-parser");
program.add_argument("-c", "--control").flag();
program.add_argument("-d", "--data").flag();
program.add_argument("-l", "--log").flag();
program.add_argument("filename").nargs(1);
try {
program.parse_args(argc, argv);
} catch (const std::exception &err) {
std::cerr << err.what() << std::endl;
std::cerr << program;
std::exit(1);
}
if (program.get<bool>("-c") + program.get<bool>("-d") + program.get<bool>("-l") != 1) {
std::cerr << "Exactly one of -c, -d, -l must be specified" << std::endl;
std::exit(1);
}

ParseMode mode;
if (program.get<bool>("-c")) {
mode = ParseMode::CONTROL;
} else if (program.get<bool>("-d")) {
mode = ParseMode::DATA;
} else {
mode = ParseMode::LOG;
}

auto filename = program.get<std::string>("filename");
if (!fs::is_regular_file(filename)) {
std::cerr << "File not found: " << filename << std::endl;
std::exit(1);
}
std::ifstream file(filename);
if (file.fail()) {
std::cerr << "Failed to open file: " << filename << std::endl;
std::exit(1);
}

return 0;
}
1 change: 1 addition & 0 deletions src/bin/shell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,5 @@ int main(int argc, char *argv[]) {
} else {
LinenoiseShell();
}
return 0;
}
5 changes: 0 additions & 5 deletions src/catalog/simple_catalog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ void SimpleCatalog::LoadSystemTables() {
table_names.insert(table_name);
}
}
db_in.close();
for (const auto &table_name : table_names) {
std::ifstream table_in(std::to_string(current_database_oid_) + "/" + table_name + ".meta");
oid_t oid, db_oid;
Expand All @@ -51,7 +50,6 @@ void SimpleCatalog::LoadSystemTables() {
ColumnList column_list;
column_list.FromString(desc);
CreateTable(name, column_list, oid, db_oid, false);
table_in.close();
}
}

Expand Down Expand Up @@ -111,10 +109,8 @@ void SimpleCatalog::CreateTable(const std::string &table_name, const ColumnList
// Step4. 写入到持久化文件table_name.meta
std::ofstream out(std::to_string(current_database_oid_) + "/" + table_name + ".meta");
out << oid << " " << current_database_oid_ << " " << table_name << " " << column_list.ToString();
out.close();
std::ofstream db_out(std::to_string(current_database_oid_) + "/tables", std::ios::app);
db_out << table_name << " ";
db_out.close();
}

void SimpleCatalog::DropTable(const std::string &table_name) {
Expand All @@ -135,7 +131,6 @@ void SimpleCatalog::DropTable(const std::string &table_name) {
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 << " ";
db_out.close();
}

void SimpleCatalog::CreateIndex(const std::string &index_name, const std::string &table_name) {
Expand Down
3 changes: 0 additions & 3 deletions src/log/log_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ lsn_t LogManager::Checkpoint(bool async) {
Flush(end_lsn);
std::ofstream out(MASTER_RECORD_NAME);
out << begin_lsn;
out.close();
return end_lsn;
}

Expand Down Expand Up @@ -204,13 +203,11 @@ void LogManager::Analyze() {
in >> next_lsn;
next_lsn_ = next_lsn;
flushed_lsn_ = next_lsn_ - 1;
in.close();
lsn_t checkpoint_lsn = 0;

if (disk_.FileExists(MASTER_RECORD_NAME)) {
std::ifstream in(MASTER_RECORD_NAME);
in >> checkpoint_lsn;
in.close();
}
// 根据 Checkpoint 日志恢复脏页表、活跃事务表等元信息
// LAB 2 BEGIN
Expand Down
12 changes: 2 additions & 10 deletions src/storage/disk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,7 @@ Disk::Disk() {
std::filesystem::resize_file(LOG_NAME, LOG_SEGMENT_SIZE);
}

Disk::~Disk() {
for (auto &entry : hashmap_) {
entry.second.close();
}
ChangeDirectory("..");
}
Disk::~Disk() { ChangeDirectory(".."); }

bool Disk::DirectoryExists(const std::string &path) { return std::filesystem::is_directory(path); }

Expand All @@ -46,10 +41,7 @@ void Disk::RemoveDirectory(const std::string &path) { std::filesystem::remove_al

bool Disk::FileExists(const std::string &path) { return std::filesystem::is_regular_file(path); }

void Disk::CreateFile(const std::string &path) {
std::ofstream ofs(path);
ofs.close();
}
void Disk::CreateFile(const std::string &path) { std::ofstream ofs(path); }

void Disk::RemoveFile(const std::string &path) { std::filesystem::remove(path); }

Expand Down
7 changes: 2 additions & 5 deletions test/sqllogictest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,17 @@ bool CompareResult(const std::string &result, const std::string &expected_result
if (!correct) {
error_stream << "Your Result:" << std::endl;
error_stream << result << std::endl;
std::fstream result_stream("../yours.log", std::fstream::out | std::fstream::trunc);
std::ofstream result_stream("../yours.log");
for (const auto &line : result_lines) {
result_stream << line << std::endl;
}
result_stream.close();

error_stream << "Expected Result:" << std::endl;
error_stream << expected_result;
std::fstream expected_stream("../expected.log", std::fstream::out | std::fstream::trunc);
std::ofstream expected_stream("../expected.log");
for (const auto &line : expected_lines) {
expected_stream << line << std::endl;
}
expected_stream.close();
}
return correct;
}
Expand Down Expand Up @@ -131,7 +129,6 @@ void ReportResult(const std::vector<std::string> &success_cases, const std::vect
}
}
report << "]}";
report.close();
}

int main(int argc, char *argv[]) {
Expand Down

0 comments on commit d7bc952

Please sign in to comment.