Browse Source

remove filesystem and fix bug for version_3

xxy
小人鱼 9 months ago
parent
commit
547e33d362
4 changed files with 45 additions and 36 deletions
  1. +2
    -2
      CMakeLists.txt
  2. +1
    -1
      benchmarks/db_bench.cc
  3. +13
    -14
      db/db_impl.cc
  4. +29
    -19
      util/coding.cc

+ 2
- 2
CMakeLists.txt View File

@ -10,7 +10,7 @@ project(leveldb VERSION 1.23.0 LANGUAGES C CXX)
if(NOT CMAKE_C_STANDARD) if(NOT CMAKE_C_STANDARD)
# This project can use C11, but will gracefully decay down to C89. # This project can use C11, but will gracefully decay down to C89.
# 17 # 17
set(CMAKE_C_STANDARD 17)
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED OFF) set(CMAKE_C_STANDARD_REQUIRED OFF)
set(CMAKE_C_EXTENSIONS OFF) set(CMAKE_C_EXTENSIONS OFF)
endif(NOT CMAKE_C_STANDARD) endif(NOT CMAKE_C_STANDARD)
@ -18,7 +18,7 @@ endif(NOT CMAKE_C_STANDARD)
# C++ standard can be overridden when this is used as a sub-project. # C++ standard can be overridden when this is used as a sub-project.
if(NOT CMAKE_CXX_STANDARD) if(NOT CMAKE_CXX_STANDARD)
# This project requires C++17. # This project requires C++17.
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF) set(CMAKE_CXX_EXTENSIONS OFF)
endif(NOT CMAKE_CXX_STANDARD) endif(NOT CMAKE_CXX_STANDARD)

+ 1
- 1
benchmarks/db_bench.cc View File

@ -74,7 +74,7 @@ static int FLAGS_reads = -1;
static int FLAGS_threads = 1; static int FLAGS_threads = 1;
// Size of each value // Size of each value
static int FLAGS_value_size = 1000;
static int FLAGS_value_size = 5000;
// Arrange to generate values that shrink to this fraction of // Arrange to generate values that shrink to this fraction of
// their original size after compression // their original size after compression

+ 13
- 14
db/db_impl.cc View File

@ -18,7 +18,6 @@
#include <atomic> #include <atomic>
#include <cstdint> #include <cstdint>
#include <cstdio> #include <cstdio>
#include <filesystem>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <set> #include <set>
@ -39,7 +38,6 @@
#include "util/coding.h" #include "util/coding.h"
#include "util/logging.h" #include "util/logging.h"
#include "util/mutexlock.h" #include "util/mutexlock.h"
namespace fs = std::filesystem;
namespace leveldb { namespace leveldb {
@ -1740,21 +1738,16 @@ void DBImpl::GarbageCollect() {
gc_mutex_.AssertHeld(); gc_mutex_.AssertHeld();
// 遍历数据库目录,找到所有 valuelog 文件 // 遍历数据库目录,找到所有 valuelog 文件
Log(options_.info_log, "start gc "); Log(options_.info_log, "start gc ");
auto files_set = fs::directory_iterator(dbname_);
std::vector<std::string> filenames;
Status s = env_->GetChildren(dbname_, &filenames);
assert(s.ok());
std::set<std::string> valuelog_set; std::set<std::string> valuelog_set;
// std::string cur_valuelog_name =
// ValueLogFileName(dbname_, valuelogfile_number_);
for (const auto& cur_log_file : files_set) {
if (fs::exists(cur_log_file) &&
fs::is_regular_file(fs::status(cur_log_file)) &&
IsValueLogFile(cur_log_file.path().filename().string())) {
// if (cur_valuelog_name == cur_log_file.path().filename().string())
// continue;
valuelog_set.emplace(cur_log_file.path().filename().string());
for (const auto& filename:filenames) {
if (IsValueLogFile(filename)) {
valuelog_set.emplace(filename);
} }
} }
for (std::string valuelog_name : valuelog_set) { for (std::string valuelog_name : valuelog_set) {
// std::cout << valuelog_name << std::endl;
uint64_t cur_log_number = GetValueLogID(valuelog_name); uint64_t cur_log_number = GetValueLogID(valuelog_name);
valuelog_name = ValueLogFileName(dbname_, cur_log_number); valuelog_name = ValueLogFileName(dbname_, cur_log_number);
if (cur_log_number == valuelogfile_number_) { if (cur_log_number == valuelogfile_number_) {
@ -1887,6 +1880,10 @@ void DBImpl::GarbageCollect() {
// Key 不存在,忽略此记录 // Key 不存在,忽略此记录
continue; continue;
} }
else if(stored_value.data()[0]==(char)(0x00)){
//value is too small
continue;
}
if (!status.ok()) { if (!status.ok()) {
std::cerr << "Error accessing sstable: " << status.ToString() std::cerr << "Error accessing sstable: " << status.ToString()
@ -1923,7 +1920,9 @@ void DBImpl::GarbageCollect() {
// 清理旧文件(如果需要) // 清理旧文件(如果需要)
cur_valuelog.close(); cur_valuelog.close();
fs::remove(valuelog_name.c_str()); // 删除旧的 ValueLog 文件
env_->RemoveFile(valuelog_name);
Log(options_.info_log, "remove file during gc %s", valuelog_name.c_str()); Log(options_.info_log, "remove file during gc %s", valuelog_name.c_str());
} }
} }

+ 29
- 19
util/coding.cc View File

@ -3,8 +3,7 @@
// found in the LICENSE file. See the AUTHORS file for names of contributors. // found in the LICENSE file. See the AUTHORS file for names of contributors.
#include "util/coding.h" #include "util/coding.h"
#include <filesystem>
#include <sstream>
namespace leveldb { namespace leveldb {
@ -174,27 +173,38 @@ void ParseStoredValue(const std::string& stored_value, uint64_t& valuelog_id,
} }
// 示例:获取 ValueLog 文件 ID // 示例:获取 ValueLog 文件 ID
// 示例:获取 ValueLog 文件 ID
uint64_t GetValueLogID(const std::string& valuelog_name) { uint64_t GetValueLogID(const std::string& valuelog_name) {
// 使用 std::filesystem::path 解析文件名
std::filesystem::path file_path(valuelog_name);
std::string filename = file_path.filename().string(); // 获取文件名部分
// 查找文件名中的 '.' 位置,提取数字部分
auto pos = filename.find('.');
if (pos == std::string::npos) {
assert(0);
}
// 提取数字部分
std::string id_str = filename.substr(0, pos);
// 检查提取的部分是否为有效数字
for (char c : id_str) {
if (!isdigit(c)) {
assert(0);
// 获取文件名部分(假设文件名格式为 "number.extension")
size_t pos = valuelog_name.find_last_of('/');
std::string filename;
if (pos != std::string::npos) {
filename = valuelog_name.substr(pos + 1);
} else {
filename = valuelog_name;
}
// 查找文件名中的 '.' 位置,提取数字部分
pos = filename.find('.');
assert(pos != std::string::npos);
// 提取数字部分
std::string id_str = filename.substr(0, pos);
// 检查文件扩展名是否为 .valuelog
if (filename.substr(pos + 1) != "valuelog") {
assert(0);
}
// 转换为 uint64_t
uint64_t id;
std::istringstream iss(id_str);
if (!(iss >> id)) {
assert(0);
} }
}
return std::stoull(id_str); // 转换为 uint64_t
return id;
} }
// Helper function to split the set of files into chunks // Helper function to split the set of files into chunks

Loading…
Cancel
Save