From e9fd9c5b1462be4eb8b9f42295923a493fa8f721 Mon Sep 17 00:00:00 2001 From: payemo Date: Thu, 20 Apr 2023 01:42:11 +0300 Subject: [PATCH] Rename filesystem to db_pathadn refactor code a bit --- db/db_impl.cc | 8 +-- db/db_impl.h | 6 +- include/leveldb/db_path.h | 73 ++++++++++++++++++++++++ include/leveldb/filesystem.h | 105 ----------------------------------- util/db_path.cc | 72 ++++++++++++++++++++++++ util/filesystem.cc | 79 -------------------------- 6 files changed, 150 insertions(+), 193 deletions(-) create mode 100644 include/leveldb/db_path.h delete mode 100644 include/leveldb/filesystem.h create mode 100644 util/db_path.cc delete mode 100644 util/filesystem.cc diff --git a/db/db_impl.cc b/db/db_impl.cc index 7e04196..2c4779e 100644 --- a/db/db_impl.cc +++ b/db/db_impl.cc @@ -37,7 +37,7 @@ namespace leveldb { -using namespace filesystem; +using namespace path; const int kNumNonTableCacheFiles = 10; @@ -301,11 +301,7 @@ Status DBImpl::Recover(VersionEdit* edit, bool* save_manifest) { // Ignore error from CreateDir since the creation of the DB is // committed only when the descriptor is created, and this directory // may already exist from a previous failed creation attempt. - if (path_->IsDirectory()) { - path_->CreateDirs(); - } - - env_->CreateDir(dbname_); + env_->CreateDir(path_->Name()); assert(db_lock_ == nullptr); Status s = env_->LockFile(LockFileName(dbname_), &db_lock_); if (!s.ok()) { diff --git a/db/db_impl.h b/db/db_impl.h index a14a452..030b673 100644 --- a/db/db_impl.h +++ b/db/db_impl.h @@ -17,11 +17,11 @@ #include "leveldb/env.h" #include "port/port.h" #include "port/thread_annotations.h" -#include "leveldb/filesystem.h" +#include "leveldb/db_path.h" namespace leveldb { -using namespace filesystem; +using namespace path; class MemTable; class TableCache; @@ -167,7 +167,7 @@ class DBImpl : public DB { const bool owns_cache_; // TODO: replace with Path; const std::string dbname_; - Path* const path_; + DbPath* const path_; // table_cache_ provides its own synchronization TableCache* const table_cache_; diff --git a/include/leveldb/db_path.h b/include/leveldb/db_path.h new file mode 100644 index 0000000..0f8ec8a --- /dev/null +++ b/include/leveldb/db_path.h @@ -0,0 +1,73 @@ +#ifndef STORAGE_LEVELDB_INCLUDE_DB_PATH_H_ +#define STORAGE_LEVELDB_INCLUDE_DB_PATH_H_ + +#include +#include + +#include "leveldb/export.h" +#include "leveldb/status.h" + +namespace leveldb { + +namespace path { + +class DbPath { +public: + // Constants + static const char kDirectorySeparator = '\\'; + static const char kAltDirecttorySeparator = '/'; + static const char kVolumeSeparatorChar = ':'; + + virtual ~DbPath() {} + + const std::string& Name() const { return path_; } + const char* CName() const { return path_.c_str(); } + + static bool IsDirectorySeparator(const char c); + + virtual bool IsAbsolute() const = 0; + virtual bool IsRelative() const = 0; + + inline size_t Size() const { return path_.size(); } + inline bool IsEmpty() const { return path_.empty(); } + + +protected: + DbPath() : path_("") {} + DbPath(const std::string& path) : path_(path) {} + + std::string path_; + + virtual void Normalize() = 0; +}; + +class WindowsDbPath : public DbPath { +public: + explicit WindowsDbPath(const std::string& path) : DbPath(path) { + Normalize(); + } + ~WindowsDbPath() {} + + static bool IsValidDriveChar(const char c); + + bool IsAbsolute() const override; + bool IsRelative() const override; + +protected: + void Normalize() override; +}; + + +// Factory +class PathFactory { +public: + PathFactory() = delete; + ~PathFactory() = delete; + + static DbPath* Create(const std::string& path); +}; + +} // namespace path +} // namespace leveldb + +#endif // STORAGE_LEVELDB_INCLUDE_DB_PATH_H_ diff --git a/include/leveldb/filesystem.h b/include/leveldb/filesystem.h deleted file mode 100644 index 7c87bef..0000000 --- a/include/leveldb/filesystem.h +++ /dev/null @@ -1,105 +0,0 @@ -#ifndef STORAGE_LEVELDB_INCLUDE_FILE_SYSTEM_H_ -#define STORAGE_LEVELDB_INCLUDE_FILE_SYSTEM_H_ - -#include -#include - -#include "leveldb/export.h" -#include "leveldb/status.h" - -namespace leveldb { - -namespace filesystem { - -bool IsDirectorySeparator(const char c); - -class Path { -public: - // Constants - static const char kDirectorySeparator = '\\'; - static const char kAltDirecttorySeparator = '/'; - static const char kVolumeSeparatorChar = ':'; - - Path() : is_dir_{false}, path_("") {} - Path(const std::string& path) : path_(path) { - is_dir_ = !IsEmpty() && IsDirectorySeparator(path_[Size() - 1]); - } - virtual ~Path() {} - - const std::string& ToString() const { return path_; } - const char* ToCString() const { return path_.c_str(); } - - virtual bool IsAbsolute() const = 0; - virtual bool IsRelative() const = 0; - - virtual Status CreateDirs() = 0; - virtual Status CreateDir() = 0; - - inline size_t Size() const { return path_.size(); } - inline bool IsEmpty() const { return path_.empty(); } - inline bool IsDirectory() const { return is_dir_; } - - // Utility functions - - inline bool HasExtension() { - if (!IsEmpty() && !is_dir_) { - std::string::reverse_iterator& path_iter = path_.rbegin(); - - while (path_iter != path_.rend()) { - char c = *path_iter; - - if (c == '.') { - return true; - } - if (IsDirectorySeparator(c)) { - break; - } - } - } - - return false; - } - -protected: - bool is_dir_; - std::string path_; - - virtual void Normalize() = 0; -}; - -class WindowsFilePath : public Path { -public: - explicit WindowsFilePath(const std::string& path) : Path(path) { - Normalize(); - } - - ~WindowsFilePath() {} - - bool IsAbsolute() const override; - bool IsRelative() const override; - - Status CreateDirs() override; - Status CreateDir() override; - -protected: - void Normalize() override; -}; - -inline bool IsValidDriveChar(const char c) { - const char drive_char = std::toupper(c); - return drive_char >= 'A' && drive_char <= 'Z'; -} - -// Factory -class PathFactory { -public: - PathFactory() = delete; - ~PathFactory() = delete; - - static Path* Create(const std::string& path); -}; - -} // namespace filesystem -} // namespace leveldb - -#endif // STORAGE_LEVELDB_INCLUDE_FILE_SYSTEM_H_ diff --git a/util/db_path.cc b/util/db_path.cc new file mode 100644 index 0000000..578b6b5 --- /dev/null +++ b/util/db_path.cc @@ -0,0 +1,72 @@ +#include "leveldb/db_path.h" + +namespace leveldb { +namespace path { + +bool DbPath::IsDirectorySeparator(const char c) { + return (c == DbPath::kDirectorySeparator || c == DbPath::kAltDirecttorySeparator); +} + +// Windows + +bool WindowsDbPath::IsAbsolute() const { + return path_.size() >= 3 && IsValidDriveChar(path_[0]) && + path_[1] == DbPath::kVolumeSeparatorChar; +}; + +bool WindowsDbPath::IsRelative() const { + if (path_.size() < 2) { + return true; + } + + if (IsDirectorySeparator(path_[0])) { + if (path_[1] != '?') { + return !IsDirectorySeparator(path_[1]); + } + return false; + } + if (path_.size() >= 3 && path_[1] == DbPath::kVolumeSeparatorChar && + IsDirectorySeparator(path_[2])) { + return IsValidDriveChar(path_[0]); + } + return true; +}; + +void WindowsDbPath::Normalize() { + auto out = path_.begin(); + + for (const char c : path_) { + if (!IsDirectorySeparator(c)) { + *(out++) = c; + } + else if (out == path_.begin() || !IsDirectorySeparator(*std::prev(out))) { + *(out++) = kDirectorySeparator; + } + else { + continue; + } + } + + path_.erase(out, path_.end()); +} + +bool WindowsDbPath::IsValidDriveChar(const char c) { + const char drive_char = std::toupper(c); + return drive_char >= 'A' && drive_char <= 'Z'; +} + +// Windows path + +DbPath* PathFactory::Create(const std::string& path) { +#ifdef LEVELDB_PLATFORM_WINDOWS + return new WindowsDbPath(path); +#elif LEVELDB_PLATFORM_POSIX + return nullptr; +#endif + return nullptr; +} + +} +} + + diff --git a/util/filesystem.cc b/util/filesystem.cc deleted file mode 100644 index 8af38ae..0000000 --- a/util/filesystem.cc +++ /dev/null @@ -1,79 +0,0 @@ -#include "leveldb/filesystem.h" - -namespace leveldb { -namespace filesystem { - -bool IsDirectorySeparator(const char c) { - return (c == Path::kDirectorySeparator || c == Path::kAltDirecttorySeparator); -} - -#ifdef LEVELDB_PLATFORM_WINDOWS - -#include - -bool WindowsFilePath::IsAbsolute() const { - return path_.size() >= 3 && IsValidDriveChar(path_[0]) && - path_[1] == Path::kVolumeSeparatorChar; -}; - -bool WindowsFilePath::IsRelative() const { - if (path_.size() < 2) { - return true; - } - - if (IsDirectorySeparator(path_[0])) { - if (path_[1] != '?') { - return !IsDirectorySeparator(path_[1]); - } - return false; - } - if (path_.size() >= 3 && path_[1] == Path::kVolumeSeparatorChar && - IsDirectorySeparator(path_[2])) { - return IsValidDriveChar(path_[0]); - } - return true; -}; - -void WindowsFilePath::Normalize() { - auto out = path_.begin(); - - for (const char c : path_) { - if (!IsDirectorySeparator(c)) { - *(out++) = c; - } else if (out == path_.begin() || !IsDirectorySeparator(*std::prev(out))) { - *(out++) = Path::kDirectorySeparator; - } else { - continue; - } - } - - path_.erase(out, path_.end()); -} - -Status WindowsFilePath::CreateDirs() { return Status::OK(); } - -Status WindowsFilePath::CreateDir() { - if (!CreateDirectoryA(Path::ToCString(), nullptr)) { - DWORD error_code = GetLastError(); - if (error_code == ERROR_FILE_NOT_FOUND || error_code == ERROR_PATH_NOT_FOUND) - return Status::NotFound(path_ + "not foud."); - return Status::IOError("I/O error occured during " + path_ + " creation"); - } - return Status::OK(); -} - -#endif - -Path* PathFactory::Create(const std::string& path) { -#ifdef LEVELDB_PLATFORM_WINDOWS - return new WindowsFilePath(path); -#elif LEVELDB_PLATFORM_POSIX - return nullptr; -#endif - return nullptr; -} - -} -} - -