Rename filesystem to db_pathadn refactor code a bit

This commit is contained in:
payemo 2023-04-20 01:42:11 +03:00
parent 7cfbcb7aef
commit e9fd9c5b14
6 changed files with 150 additions and 193 deletions

View File

@ -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()) {

View File

@ -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_;

73
include/leveldb/db_path.h Normal file
View File

@ -0,0 +1,73 @@
#ifndef STORAGE_LEVELDB_INCLUDE_DB_PATH_H_
#define STORAGE_LEVELDB_INCLUDE_DB_PATH_H_
#include <cstdint>
#include <string>
#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_

View File

@ -1,105 +0,0 @@
#ifndef STORAGE_LEVELDB_INCLUDE_FILE_SYSTEM_H_
#define STORAGE_LEVELDB_INCLUDE_FILE_SYSTEM_H_
#include <cstdint>
#include <string>
#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_

72
util/db_path.cc Normal file
View File

@ -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;
}
}
}

View File

@ -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 <windows.h>
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;
}
}
}