Skip to content

Commit

Permalink
Fix launch database using homedir
Browse files Browse the repository at this point in the history
  • Loading branch information
acquamarin committed Mar 22, 2024
1 parent 8f37501 commit 64a728e
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 8 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ endif ()
if(${BUILD_KUZU})
add_definitions(-DKUZU_ROOT_DIRECTORY="${PROJECT_SOURCE_DIR}")
add_definitions(-DKUZU_CMAKE_VERSION="${CMAKE_PROJECT_VERSION}")
add_definitions(-DKUZU_EXTENSION_VERSION="0.2.3")
add_definitions(-DKUZU_EXTENSION_VERSION="0.2.4")

include_directories(src/include)
include_directories(third_party/antlr4_cypher/include)
Expand Down
5 changes: 5 additions & 0 deletions src/common/file_system/file_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ bool FileSystem::fileOrPathExists(const std::string& /*path*/) const {
KU_UNREACHABLE;
}

std::string FileSystem::expandPath(

Check warning on line 26 in src/common/file_system/file_system.cpp

View check run for this annotation

Codecov / codecov/patch

src/common/file_system/file_system.cpp#L26

Added line #L26 was not covered by tests
main::ClientContext* /*context*/, const std::string& path) const {
return path;

Check warning on line 28 in src/common/file_system/file_system.cpp

View check run for this annotation

Codecov / codecov/patch

src/common/file_system/file_system.cpp#L28

Added line #L28 was not covered by tests
}

std::string FileSystem::joinPath(const std::string& base, const std::string& part) {
return (std::filesystem::path{base} / part).string();
}
Expand Down
18 changes: 12 additions & 6 deletions src/common/file_system/local_file_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,7 @@ LocalFileInfo::~LocalFileInfo() {

std::unique_ptr<FileInfo> LocalFileSystem::openFile(
const std::string& path, int flags, main::ClientContext* context, FileLockType lock_type) {
auto fullPath = path;
if (path.starts_with('~')) {
fullPath =
context->getCurrentSetting(main::HomeDirectorySetting::name).getValue<std::string>() +
fullPath.substr(1);
}
auto fullPath = expandPath(context, path);
#if defined(_WIN32)
auto dwDesiredAccess = 0ul;
auto dwCreationDisposition = (flags & O_CREAT) ? OPEN_ALWAYS : OPEN_EXISTING;
Expand Down Expand Up @@ -194,6 +189,17 @@ bool LocalFileSystem::fileOrPathExists(const std::string& path) const {
return std::filesystem::exists(path);
}

std::string LocalFileSystem::expandPath(
main::ClientContext* context, const std::string& path) const {
auto fullPath = path;
if (path.starts_with('~')) {
fullPath =
context->getCurrentSetting(main::HomeDirectorySetting::name).getValue<std::string>() +
fullPath.substr(1);
}
return fullPath;
}

void LocalFileSystem::readFromFile(
FileInfo* fileInfo, void* buffer, uint64_t numBytes, uint64_t position) const {
auto localFileInfo = ku_dynamic_cast<FileInfo*, LocalFileInfo*>(fileInfo);
Expand Down
5 changes: 5 additions & 0 deletions src/common/file_system/virtual_file_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ bool VirtualFileSystem::fileOrPathExists(const std::string& path) const {
return findFileSystem(path)->fileOrPathExists(path);
}

std::string VirtualFileSystem::expandPath(
main::ClientContext* context, const std::string& path) const {
return findFileSystem(path)->expandPath(context, path);
}

void VirtualFileSystem::readFromFile(
FileInfo* /*fileInfo*/, void* /*buffer*/, uint64_t /*numBytes*/, uint64_t /*position*/) const {
KU_UNREACHABLE;
Expand Down
2 changes: 2 additions & 0 deletions src/include/common/file_system/file_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class KUZU_API FileSystem {

virtual bool fileOrPathExists(const std::string& path) const;

virtual std::string expandPath(main::ClientContext* context, const std::string& path) const;

static std::string joinPath(const std::string& base, const std::string& part);

static std::string getFileExtension(const std::filesystem::path& path);
Expand Down
2 changes: 2 additions & 0 deletions src/include/common/file_system/local_file_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class LocalFileSystem final : public FileSystem {

bool fileOrPathExists(const std::string& path) const override;

std::string expandPath(main::ClientContext* context, const std::string& path) const override;

protected:
void readFromFile(
FileInfo* fileInfo, void* buffer, uint64_t numBytes, uint64_t position) const override;
Expand Down
2 changes: 2 additions & 0 deletions src/include/common/file_system/virtual_file_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class VirtualFileSystem final : public FileSystem {

bool fileOrPathExists(const std::string& path) const override;

std::string expandPath(main::ClientContext* context, const std::string& path) const override;

protected:
void readFromFile(
FileInfo* fileInfo, void* buffer, uint64_t numBytes, uint64_t position) const override;
Expand Down
8 changes: 7 additions & 1 deletion src/main/database.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "main/database.h"

#include "common/random_engine.h"
#include "main/database_manager.h"

#if defined(_WIN32)
Expand Down Expand Up @@ -72,10 +73,15 @@ static void getLockFileFlagsAndType(bool readOnly, bool createNew, int& flags, F
}

Database::Database(std::string_view databasePath, SystemConfig systemConfig)
: databasePath{databasePath}, systemConfig{systemConfig} {
: systemConfig{systemConfig} {
initLoggers();
logger = LoggerUtils::getLogger(LoggerConstants::LoggerEnum::DATABASE);
vfs = std::make_unique<VirtualFileSystem>();
// To expand a path with home directory(~), we have to pass in a dummy clientContext which
// handles the home directory expansion.
auto clientContext = ClientContext(this);
auto dbPathStr = std::string(databasePath);
this->databasePath = vfs->expandPath(&clientContext, dbPathStr);
bufferManager = std::make_unique<BufferManager>(
this->systemConfig.bufferPoolSize, this->systemConfig.maxDBSize);
memoryManager = std::make_unique<MemoryManager>(bufferManager.get(), vfs.get());
Expand Down
6 changes: 6 additions & 0 deletions test/c_api/database_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,9 @@ TEST_F(CApiDatabaseTest, CreationInvalidPath) {
auto database = kuzu_database_init(databasePathCStr, kuzu_default_system_config());
ASSERT_EQ(database, nullptr);
}

TEST_F(CApiDatabaseTest, CreationHomeDir) {
auto databasePathCStr = (char*)"~/ku_test.db";
auto database = kuzu_database_init(databasePathCStr, kuzu_default_system_config());
ASSERT_NE(database, nullptr);
}

0 comments on commit 64a728e

Please sign in to comment.