refactor: support UTF-8 file paths via std::filesystem (#359)

Rewrite file/path handling to use std::filesystem and UTF-8-safe helpers.
Switch Windows file open/create paths to wide-char APIs, replace manual
separator concatenation with PathJoin, and enable RocksDB UTF-8 filenames.

Also add UTF-8 path coverage for file IO, version manager recovery, and
collection open/flush/reopen flows.
This commit is contained in:
Jalin Wang
2026-05-08 17:44:22 +08:00
committed by GitHub
parent 68a497efdb
commit 1d4ae0b1b5
19 changed files with 937 additions and 432 deletions
+2 -2
View File
@@ -14,6 +14,7 @@
#include <atomic>
#include <cstdint>
#include <filesystem>
#include <memory>
#include <mutex>
#include <shared_mutex>
@@ -25,7 +26,6 @@
#include <zvec/ailego/logger/logger.h>
#include <zvec/ailego/pattern/expected.hpp>
#include <zvec/ailego/utility/file_helper.h>
#include <zvec/ailego/utility/string_helper.h>
#include <zvec/db/collection.h>
#include <zvec/db/doc.h>
#include <zvec/db/options.h>
@@ -1875,7 +1875,7 @@ Status CollectionImpl::init_writing_segment() {
}
Status CollectionImpl::acquire_file_lock(bool create) {
std::string lock_file_path = ailego::StringHelper::Concat(path_, "/", "LOCK");
std::string lock_file_path = ailego::FileHelper::PathJoin(path_, "LOCK");
if (create) {
if (!lock_file_.create(lock_file_path.c_str(), 0)) {
+19 -101
View File
@@ -13,20 +13,12 @@
// limitations under the License.
#include "file_helper.h"
#include <errno.h>
#include <string.h>
#include <algorithm>
#include <cstdio>
#ifdef _MSC_VER
#include <cstring>
#include <filesystem>
#include <fstream>
#else
#include <sys/stat.h>
#include <dirent.h>
#include <fcntl.h>
#include <unistd.h>
#endif
#include <ailego/pattern/defer.h>
#include <vector>
namespace zvec {
@@ -37,132 +29,58 @@ const std::string FileHelper::RECOVER_SUFFIX = ".recovering";
bool FileHelper::CopyFile(const std::string &src_file_path,
const std::string &dst_file_path) {
#ifdef _MSC_VER
std::string dst_file_path_tmp = dst_file_path + ".tmp";
std::error_code ec;
std::filesystem::copy_file(src_file_path, dst_file_path_tmp,
std::filesystem::copy_options::overwrite_existing,
ec);
std::filesystem::copy_file(
ailego::FileHelper::PathFromUtf8(src_file_path),
ailego::FileHelper::PathFromUtf8(dst_file_path_tmp),
std::filesystem::copy_options::overwrite_existing, ec);
if (ec) {
return false;
}
std::filesystem::rename(dst_file_path_tmp, dst_file_path, ec);
std::filesystem::rename(ailego::FileHelper::PathFromUtf8(dst_file_path_tmp),
ailego::FileHelper::PathFromUtf8(dst_file_path), ec);
return !ec;
#else
int src_fd = open(src_file_path.c_str(), O_RDONLY, 0);
if (src_fd < 0) {
return false;
}
AILEGO_DEFER([src_fd] { close(src_fd); });
std::string dst_file_path_tmp = dst_file_path + ".tmp";
int dst_fd =
open(dst_file_path_tmp.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (dst_fd < 0) {
return false;
}
AILEGO_DEFER([dst_fd] { close(dst_fd); });
ssize_t size;
char buf[BUFSIZ];
while ((size = read(src_fd, buf, BUFSIZ)) > 0) {
if (size != write(dst_fd, buf, size)) {
return false;
}
}
return rename(dst_file_path_tmp.c_str(), dst_file_path.c_str()) == 0;
#endif
}
bool FileHelper::CopyDirectory(const std::string &src_dir_path,
const std::string &dst_dir_path) {
#ifdef _MSC_VER
std::error_code ec;
std::filesystem::copy(src_dir_path, dst_dir_path,
std::filesystem::copy(ailego::FileHelper::PathFromUtf8(src_dir_path),
ailego::FileHelper::PathFromUtf8(dst_dir_path),
std::filesystem::copy_options::recursive |
std::filesystem::copy_options::overwrite_existing,
ec);
return !ec;
#else
DIR *dir = opendir(src_dir_path.c_str());
if (!dir) {
return false;
}
AILEGO_DEFER([dir] { closedir(dir); });
if (!ailego::FileHelper::IsExist(dst_dir_path.c_str())) {
if (!ailego::FileHelper::MakePath(dst_dir_path.c_str())) {
return false;
}
}
struct dirent *dent;
while ((dent = readdir(dir)) != nullptr) {
if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, "..")) {
continue;
}
std::string src_full_path =
ailego::StringHelper::Concat(src_dir_path, "/", dent->d_name);
std::string dst_full_path =
ailego::StringHelper::Concat(dst_dir_path, "/", dent->d_name);
if (ailego::FileHelper::IsDirectory(src_full_path.c_str())) {
if (!CopyDirectory(src_full_path, dst_full_path)) {
return false;
}
} else {
if (!CopyFile(src_full_path, dst_full_path)) {
return false;
}
}
}
return true;
#endif
}
void FileHelper::CleanupDirectory(const std::string &backup_dir,
size_t max_backup_count,
const char *prefix_name) {
if (max_backup_count <= 0) {
if (max_backup_count == 0) {
return;
}
#ifdef _MSC_VER
size_t prefix_len = strlen(prefix_name);
std::vector<std::string> candidates;
std::error_code ec;
for (const auto &entry :
std::filesystem::directory_iterator(backup_dir, ec)) {
std::string name = entry.path().filename().string();
for (const auto &entry : std::filesystem::directory_iterator(
ailego::FileHelper::PathFromUtf8(backup_dir), ec)) {
std::string name = entry.path().filename().u8string();
if (name.compare(0, prefix_len, prefix_name) == 0) {
candidates.emplace_back(name);
}
}
#else
DIR *dir = opendir(backup_dir.c_str());
if (!dir) {
if (ec) {
return;
}
AILEGO_DEFER([dir] { closedir(dir); });
size_t prefix_len = strlen(prefix_name);
std::vector<std::string> candidates;
struct dirent *dent;
while ((dent = readdir(dir)) != nullptr) {
if (strncmp(dent->d_name, prefix_name, prefix_len) == 0) {
candidates.emplace_back(dent->d_name);
}
}
#endif
if (candidates.size() <= max_backup_count) {
return;
}
std::sort(candidates.begin(), candidates.end());
for (size_t i = 0; i < candidates.size() - max_backup_count; ++i) {
std::string path =
ailego::StringHelper::Concat(backup_dir, "/", candidates[i].c_str());
ailego::FileHelper::RemovePath(path.c_str());
std::string path_str =
ailego::FileHelper::PathJoin(backup_dir, candidates[i]);
ailego::FileHelper::RemovePath(path_str.c_str());
}
}
+41 -34
View File
@@ -73,16 +73,17 @@ class FileHelper {
public:
static const std::string MakeWalPath(const std::string &path, uint32_t seg_id,
uint32_t block_id) {
return ailego::StringHelper::Concat(path, "/", seg_id, "/", block_id,
".wal");
return ailego::FileHelper::PathJoin(
path, seg_id, ailego::StringHelper::Concat(block_id, ".wal"));
}
static std::string MakeSegmentPath(const std::string &path, uint32_t id,
const std::string &suffix = "") {
if (suffix.empty()) {
return ailego::StringHelper::Concat(path, "/", id);
return ailego::FileHelper::PathJoin(path, id);
}
return ailego::StringHelper::Concat(path, "/", id, ".", suffix);
return ailego::FileHelper::PathJoin(
path, ailego::StringHelper::Concat(id, ".", suffix));
}
static std::string MakeTempSegmentPath(const std::string &path, uint32_t id) {
@@ -94,99 +95,105 @@ class FileHelper {
uint32_t seg_id,
uint32_t block_id,
bool use_parquet = false) {
return use_parquet ? MakeForwardBlockPath(path, seg_id, block_id,
std::string("parquet"))
: MakeForwardBlockPath(path, seg_id, block_id,
std::string("ipc"));
return MakeForwardBlockPath(path, seg_id, block_id,
std::string(use_parquet ? "parquet" : "ipc"));
}
static const std::string MakeForwardBlockPath(const std::string &path,
uint32_t seg_id,
uint32_t block_id,
const std::string &suffix) {
return ailego::StringHelper::Concat(path, "/", seg_id, "/scalar.", block_id,
".", suffix);
return ailego::FileHelper::PathJoin(
path, seg_id,
ailego::StringHelper::Concat("scalar.", block_id, ".", suffix));
}
static const std::string MakeForwardBlockPath(const std::string &seg_path,
uint32_t block_id,
bool use_parquet = false) {
return use_parquet ? ailego::StringHelper::Concat(seg_path, "/scalar.",
block_id, ".parquet")
: ailego::StringHelper::Concat(seg_path, "/scalar.",
block_id, ".ipc");
return MakeForwardBlockPath(seg_path, block_id,
std::string(use_parquet ? "parquet" : "ipc"));
}
static const std::string MakeForwardBlockPath(const std::string &seg_path,
uint32_t block_id,
const std::string &suffix) {
return ailego::StringHelper::Concat(seg_path, "/scalar.", block_id, ".",
suffix);
return ailego::FileHelper::PathJoin(
seg_path,
ailego::StringHelper::Concat("scalar.", block_id, ".", suffix));
}
// e.g.: **/seg1/scalar.index.block.1.rocksdb
static const std::string MakeInvertIndexPath(const std::string &path,
uint32_t seg_id,
uint32_t block_id) {
return ailego::StringHelper::Concat(path, "/", seg_id, "/scalar.index.",
block_id, ".rocksdb");
return ailego::FileHelper::PathJoin(
path, seg_id,
ailego::StringHelper::Concat("scalar.index.", block_id, ".rocksdb"));
}
static const std::string MakeInvertIndexPath(const std::string &seg_path,
uint32_t block_id) {
return ailego::StringHelper::Concat(seg_path, "/scalar.index.", block_id,
".rocksdb");
return ailego::FileHelper::PathJoin(
seg_path,
ailego::StringHelper::Concat("scalar.index.", block_id, ".rocksdb"));
}
static const std::string MakeVectorIndexPath(const std::string &path,
const std::string &column,
uint32_t seg_id,
uint32_t block_id) {
return ailego::StringHelper::Concat(path, "/", seg_id, "/", column,
".index.", block_id, ".proxima");
return ailego::FileHelper::PathJoin(
path, seg_id,
ailego::StringHelper::Concat(column, ".index.", block_id, ".proxima"));
}
static const std::string MakeVectorIndexPath(const std::string &seg_path,
const std::string &column,
uint32_t block_id) {
return ailego::StringHelper::Concat(seg_path, "/", column, ".index.",
block_id, ".proxima");
return ailego::FileHelper::PathJoin(
seg_path,
ailego::StringHelper::Concat(column, ".index.", block_id, ".proxima"));
}
// e.g.: **/{seg_id}/{column}.index.block.{block_id}.proxima
static const std::string MakeQuantizeVectorIndexPath(
const std::string &path, const std::string &column, uint32_t seg_id,
uint32_t block_id) {
return ailego::StringHelper::Concat(path, "/", seg_id, "/", column,
".qindex.", block_id, ".proxima");
return ailego::FileHelper::PathJoin(
path, seg_id,
ailego::StringHelper::Concat(column, ".qindex.", block_id, ".proxima"));
}
static const std::string MakeQuantizeVectorIndexPath(
const std::string &seg_path, const std::string &column,
uint32_t block_id) {
return ailego::StringHelper::Concat(seg_path, "/", column, ".qindex.",
block_id, ".proxima");
return ailego::FileHelper::PathJoin(
seg_path,
ailego::StringHelper::Concat(column, ".qindex.", block_id, ".proxima"));
}
//! Make file path with ${prefix_path}/${file_name}
static std::string MakeFilePath(const std::string &prefix_path,
FileID file_id) {
return ailego::StringHelper::Concat(prefix_path, "/", GetFileName(file_id));
return ailego::FileHelper::PathJoin(prefix_path, GetFileName(file_id));
}
//! Make file path with ${prefix_path}/${file_name}.${number}
static std::string MakeFilePath(const std::string &prefix_path,
FileID file_id, uint32_t number) {
return ailego::StringHelper::Concat(prefix_path, "/", GetFileName(file_id),
".", number);
return ailego::FileHelper::PathJoin(
prefix_path,
ailego::StringHelper::Concat(GetFileName(file_id), ".", number));
}
//! Make file path with ${prefix_path}/${file_name}.${suffix_name}.${number}
static std::string MakeFilePath(const std::string &prefix_path,
FileID file_id, uint32_t number,
const std::string &suffix_name) {
return ailego::StringHelper::Concat(prefix_path, "/", GetFileName(file_id),
".", suffix_name, ".", number);
return ailego::FileHelper::PathJoin(
prefix_path, ailego::StringHelper::Concat(GetFileName(file_id), ".",
suffix_name, ".", number));
}
//! Create directory
@@ -273,4 +280,4 @@ class FileHelper {
};
} // namespace zvec
} // namespace zvec
+3 -1
View File
@@ -18,6 +18,7 @@
#include <zvec/ailego/io/file.h>
#include <zvec/ailego/logger/logger.h>
#include <zvec/ailego/pattern/factory.h>
#include <zvec/ailego/utility/file_helper.h>
#include <zvec/db/status.h>
#include "db/common/constants.h"
#include "error_code.h"
@@ -50,7 +51,8 @@ class LogUtil {
if (logger_type == FILE_LOG_TYPE_NAME) {
params.set("proxima.file.logger.log_dir", log_dir);
params.set("proxima.file.logger.log_file", log_file);
params.set("proxima.file.logger.path", log_dir + "/" + log_file);
params.set("proxima.file.logger.path",
ailego::FileHelper::PathJoin(log_dir, log_file));
std::string program_name = ailego::File::BaseName(gflags::GetArgv0());
params.set("proxima.program.program_name", program_name);
params.set("proxima.file.logger.file_size", log_file_size);
+10 -7
View File
@@ -34,7 +34,8 @@
namespace zvec {
Status Version::Load(const std::string &path, Version *version) {
std::ifstream ifs(path, std::ios::binary);
std::ifstream ifs;
ailego::FileHelper::OpenIfstream(ifs, path, std::ios::binary);
if (!ifs.is_open()) {
LOG_ERROR("Failed to open file: %s", path.c_str());
return Status::InternalError("Failed to open file");
@@ -74,7 +75,8 @@ Status Version::Load(const std::string &path, Version *version) {
}
Status Version::Save(const std::string &path, const Version &version) {
std::ofstream ofs(path, std::ios::binary);
std::ofstream ofs;
ailego::FileHelper::OpenOfstream(ofs, path, std::ios::binary);
if (!ofs.is_open()) {
LOG_ERROR("Failed to open file: %s, err: %s", path.c_str(),
ailego::FileHelper::GetLastErrorString().c_str());
@@ -187,12 +189,13 @@ std::string Version::to_string_formatted(int indent_level) const {
Result<VersionManager::Ptr> VersionManager::Recovery(const std::string &path) {
namespace fs = std::filesystem;
if (!fs::exists(path)) {
auto u8path = ailego::FileHelper::PathFromUtf8(path);
if (!fs::exists(u8path)) {
LOG_ERROR("VersionManager::Recovery: path %s does not exist", path.c_str());
return tl::make_unexpected(
Status::NotFound("path ", path, " does not exist"));
}
if (!fs::is_directory(path)) {
if (!fs::is_directory(u8path)) {
LOG_ERROR("VersionManager::Recovery: path %s is not a directory",
path.c_str());
return tl::make_unexpected(
@@ -207,14 +210,14 @@ Result<VersionManager::Ptr> VersionManager::Recovery(const std::string &path) {
uint64_t max_id = UINT64_MAX;
std::string version_path;
for (const auto &entry : fs::directory_iterator(path)) {
for (const auto &entry : fs::directory_iterator(u8path)) {
if (entry.is_regular_file()) {
std::string filename = entry.path().filename().string();
std::string filename = entry.path().filename().u8string();
if (std::regex_match(filename, match, regex)) {
uint64_t id = std::stoull(match[1].str());
if (id > max_id || max_id == UINT64_MAX) {
max_id = id;
version_path = entry.path().string();
version_path = entry.path().u8string();
}
}
}
+1 -1
View File
@@ -164,7 +164,7 @@ class Version {
private:
CollectionSchema::Ptr schema_;
bool enable_mmap_;
bool enable_mmap_{false};
std::unordered_map<SegmentID, SegmentMeta::Ptr> persisted_segment_metas_map_;
+2 -1
View File
@@ -40,7 +40,8 @@
namespace zvec {
inline FileFormat InferFileFormat(const std::string &file_path) {
std::string ext = std::filesystem::path(file_path).extension().string();
std::string ext =
ailego::FileHelper::PathFromUtf8(file_path).extension().u8string();
std::transform(ext.begin(), ext.end(), ext.begin(), ::tolower);
if (ext == ".parquet") {
return FileFormat::PARQUET;
+3 -1
View File
@@ -22,6 +22,7 @@
#include <typeinfo>
#include <zvec/ailego/encoding/json.h>
#include <zvec/ailego/logger/logger.h>
#include <zvec/ailego/utility/file_helper.h>
#include <zvec/ailego/utility/string_helper.h>
#include "db/sqlengine/common/util.h"
#include "tree/ParseTree.h"
@@ -118,7 +119,8 @@ std::string ZVecParser::to_formatted_string_tree(void *tree, void *parser) {
void ZVecParser::save_to_file(const std::string &file_name,
const std::string &formatted) {
std::ofstream outfile(file_name);
std::ofstream outfile;
ailego::FileHelper::OpenOfstream(outfile, file_name);
outfile << formatted;
outfile << std::endl;
outfile.close();