Skip to content

Commit

Permalink
Rename filesystem to db_pathadn refactor code a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
payemo committed Apr 19, 2023
1 parent 7cfbcb7 commit 45f5efd
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 114 deletions.
8 changes: 2 additions & 6 deletions db/db_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

namespace leveldb {

using namespace filesystem;
using namespace path;

const int kNumNonTableCacheFiles = 10;

Expand Down Expand Up @@ -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()) {
Expand Down
6 changes: 3 additions & 3 deletions db/db_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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_;
Expand Down
73 changes: 73 additions & 0 deletions include/leveldb/db_path.h
Original file line number Diff line number Diff line change
@@ -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_
105 changes: 0 additions & 105 deletions include/leveldb/filesystem.h

This file was deleted.

72 changes: 72 additions & 0 deletions util/db_path.cc
Original file line number Diff line number Diff line change
@@ -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;
}

}
}


0 comments on commit 45f5efd

Please sign in to comment.