Skip to content

Commit

Permalink
propogate file flags
Browse files Browse the repository at this point in the history
  • Loading branch information
ray6080 committed Jun 20, 2024
1 parent 5d05575 commit bfc182f
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 43 deletions.
5 changes: 3 additions & 2 deletions extension/httpfs/src/cached_file_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ std::unique_ptr<FileInfo> CachedFileManager::getCachedFileInfo(HTTPFileInfo* htt
auto fileName = FileSystem::getFileName(httpFileInfo->path);
auto cacheFilePath = getCachedFilePath(fileName, transactionID);
if (!vfs->fileOrPathExists(cacheFilePath)) {
auto cacheFileInfo = vfs->openFile(cacheFilePath, O_CREAT | O_RDWR);
auto cacheFileInfo = vfs->openFile(cacheFilePath,
FileFlags::CREATE_IF_NOT_EXISTS | FileFlags::READ_ONLY | FileFlags::WRITE);
downloadFile(httpFileInfo, cacheFileInfo.get());
}
return vfs->openFile(cacheFilePath, O_RDONLY);
return vfs->openFile(cacheFilePath, FileFlags::READ_ONLY);
}

void CachedFileManager::cleanUP(main::ClientContext* context) {
Expand Down
2 changes: 1 addition & 1 deletion extension/httpfs/src/httpfs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ bool HTTPFileSystem::canHandleFile(const std::string& path) const {

bool HTTPFileSystem::fileOrPathExists(const std::string& path, main::ClientContext* context) {
try {
auto fileInfo = openFile(path, O_RDONLY, context, FileLockType::READ_LOCK);
auto fileInfo = openFile(path, FileFlags::READ_ONLY, context, FileLockType::READ_LOCK);
auto httpFileInfo = fileInfo->constPtrCast<HTTPFileInfo>();
if (httpFileInfo->length == 0) {
return false;
Expand Down
3 changes: 2 additions & 1 deletion src/binder/bind/bind_import_database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ static std::string getQueryFromFile(common::VirtualFileSystem* vfs, const std::s
if (!vfs->fileOrPathExists(filePath, context)) {
throw BinderException(stringFormat("File {} does not exist.", filePath));
}
auto fileInfo = vfs->openFile(filePath, O_RDONLY
auto fileInfo = vfs->openFile(filePath, FileFlags::READ_ONLY
// TODO(Ziyi): We need to handle O_BINARY here.
#ifdef _WIN32
| _O_BINARY
#endif
Expand Down
8 changes: 4 additions & 4 deletions src/catalog/catalog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -413,8 +413,8 @@ static void writeMagicBytes(Serializer& serializer) {
void Catalog::saveToFile(const std::string& directory, common::VirtualFileSystem* fs,
common::FileVersionType versionType) {
auto catalogPath = StorageUtils::getCatalogFilePath(fs, directory, versionType);
Serializer serializer(
std::make_unique<BufferedFileWriter>(fs->openFile(catalogPath, O_WRONLY | O_CREAT)));
Serializer serializer(std::make_unique<BufferedFileWriter>(fs->openFile(catalogPath,
FileFlags::CREATE_IF_NOT_EXISTS | FileFlags::READ_ONLY | FileFlags::WRITE)));
writeMagicBytes(serializer);
serializer.serializeValue(StorageVersionInfo::getStorageVersion());
tables->serialize(serializer);
Expand All @@ -426,8 +426,8 @@ void Catalog::saveToFile(const std::string& directory, common::VirtualFileSystem
void Catalog::readFromFile(const std::string& directory, common::VirtualFileSystem* fs,
common::FileVersionType versionType, main::ClientContext* context) {
auto catalogPath = StorageUtils::getCatalogFilePath(fs, directory, versionType);
Deserializer deserializer(
std::make_unique<BufferedFileReader>(fs->openFile(catalogPath, O_RDONLY, context)));
Deserializer deserializer(std::make_unique<BufferedFileReader>(
fs->openFile(catalogPath, FileFlags::READ_ONLY, context)));
validateMagicBytes(deserializer);
storage_version_t savedStorageVersion;
deserializer.deserializeValue(savedStorageVersion);
Expand Down
4 changes: 1 addition & 3 deletions src/include/storage/file_handle.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ class FileHandle {

protected:
virtual common::page_idx_t addNewPageWithoutLock();
void constructExistingFileHandle(const std::string& path, common::VirtualFileSystem* vfs,
main::ClientContext* context);
void constructNewFileHandle(const std::string& path, common::VirtualFileSystem* vfs,
void constructFileHandle(const std::string& path, common::VirtualFileSystem* vfs,
main::ClientContext* context);

protected:
Expand Down
5 changes: 3 additions & 2 deletions src/main/attached_database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ void AttachedKuzuDatabase::initCatalog(const std::string& path, ClientContext* c
}

static void validateEmptyWAL(const std::string& path, ClientContext* context) {
auto walFile = context->getVFSUnsafe()->openFile(
path + "/" + common::StorageConstants::WAL_FILE_SUFFIX, O_RDONLY, context);
auto walFile =
context->getVFSUnsafe()->openFile(path + "/" + common::StorageConstants::WAL_FILE_SUFFIX,
common::FileFlags::READ_ONLY, context);
if (walFile->getFileSize() > 0) {
throw common::RuntimeException(
common::stringFormat("Cannot attach a remote kuzu database with non-empty wal file."));
Expand Down
4 changes: 1 addition & 3 deletions src/processor/operator/simple/export_db.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include "processor/operator/simple/export_db.h"

#include <fcntl.h>

#include <sstream>

#include "catalog/catalog.h"
Expand All @@ -26,7 +24,7 @@ using std::stringstream;

static void writeStringStreamToFile(VirtualFileSystem* vfs, std::string ssString,
const std::string& path) {
auto fileInfo = vfs->openFile(path, O_WRONLY | O_CREAT);
auto fileInfo = vfs->openFile(path, FileFlags::WRITE | FileFlags::CREATE_IF_NOT_EXISTS);
fileInfo->writeFile(reinterpret_cast<const uint8_t*>(ssString.c_str()), ssString.size(),
0 /* offset */);
}
Expand Down
31 changes: 13 additions & 18 deletions src/storage/file_handle.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include "storage/file_handle.h"

#include <fcntl.h>

#include <cmath>
#include <mutex>

Expand All @@ -15,38 +13,35 @@ namespace storage {
FileHandle::FileHandle(const std::string& path, uint8_t flags, VirtualFileSystem* vfs,
main::ClientContext* context)
: flags{flags} {
if (!isNewTmpFile()) {
constructExistingFileHandle(path, vfs, context);
} else {
constructNewFileHandle(path, vfs, context);
}
constructFileHandle(path, vfs, context);
}

void FileHandle::constructExistingFileHandle(const std::string& path, VirtualFileSystem* vfs,
void FileHandle::constructFileHandle(const std::string& path, VirtualFileSystem* vfs,
main::ClientContext* context) {
if (isNewTmpFile()) {
numPages = 0;
pageCapacity = 0;
fileInfo = std::make_unique<FileInfo>(path, nullptr /* fileSystem */);
return;
}
int openFlags;
if (isReadOnlyFile()) {
openFlags = FileFlags::READ_ONLY;
} else {
openFlags = FileFlags::WRITE | FileFlags::READ_ONLY |
((createFileIfNotExists()) ? FileFlags::CREATE_IF_NOT_EXISTS : 0x00000000);
openFlags = FileFlags::WRITE | FileFlags::READ_ONLY;
if (createFileIfNotExists()) {
openFlags |= FileFlags::CREATE_IF_NOT_EXISTS;
}
}
fileInfo = vfs->openFile(path, openFlags, context);
auto fileLength = fileInfo->getFileSize();
numPages = ceil((double)fileLength / (double)getPageSize());
numPages = ceil(static_cast<double>(fileLength) / static_cast<double>(getPageSize()));
pageCapacity = 0;
while (pageCapacity < numPages) {
pageCapacity += StorageConstants::PAGE_GROUP_SIZE;
}
}

void FileHandle::constructNewFileHandle(const std::string& path, VirtualFileSystem* vfs,
main::ClientContext* context) {
fileInfo = vfs->openFile(path, O_CREAT | O_RDWR, context);
numPages = 0;
pageCapacity = 0;
}

page_idx_t FileHandle::addNewPage() {
return addNewPages(1 /* numNewPages */);
}
Expand Down
2 changes: 1 addition & 1 deletion src/storage/storage_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ void StorageManager::recover(main::ClientContext& clientContext) {
WALReplayMode::RECOVERY_CHECKPOINT);
walReplayer->replay();
// Truncate .wal and .shadow to empty. Remove catalog and stats wal files.
auto walFileInfo = vfs->openFile(walFilePath, O_RDWR);
auto walFileInfo = vfs->openFile(walFilePath, FileFlags::READ_ONLY | FileFlags::WRITE);
if (walFileInfo->getFileSize() > 0) {
walFileInfo->truncate(0);
}
Expand Down
6 changes: 3 additions & 3 deletions src/storage/wal/wal.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include "storage/wal/wal.h"

#include <fcntl.h>

#include "binder/ddl/bound_alter_info.h"
#include "catalog/catalog_entry/sequence_catalog_entry.h"
#include "common/file_system/file_info.h"
Expand All @@ -22,7 +20,9 @@ WAL::WAL(const std::string& directory, bool readOnly, BufferManager& bufferManag
: directory{directory}, bufferManager{bufferManager}, vfs{vfs} {
auto fileInfo =
vfs->openFile(vfs->joinPath(directory, std::string(StorageConstants::WAL_FILE_SUFFIX)),
readOnly ? O_RDONLY : O_CREAT | O_RDWR, context);
readOnly ? FileFlags::READ_ONLY :
FileFlags::CREATE_IF_NOT_EXISTS | FileFlags::READ_ONLY | FileFlags::WRITE,
context);
bufferedWriter = std::make_shared<BufferedFileWriter>(std::move(fileInfo));
shadowingFH = bufferManager.getBMFileHandle(
vfs->joinPath(directory, std::string(StorageConstants::SHADOWING_SUFFIX)),
Expand Down
2 changes: 1 addition & 1 deletion src/storage/wal_replayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void WALReplayer::replay() {
if (!clientContext.getVFSUnsafe()->fileOrPathExists(walFilePath, &clientContext)) {
return;
}
auto fileInfo = clientContext.getVFSUnsafe()->openFile(walFilePath, O_RDONLY);
auto fileInfo = clientContext.getVFSUnsafe()->openFile(walFilePath, FileFlags::READ_ONLY);
auto walFileSize = fileInfo->getFileSize();
// Check if the wal file is empty or corrupted. so nothing to read.
if (walFileSize == 0) {
Expand Down
7 changes: 3 additions & 4 deletions tools/shell/shell_runner.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#include <fcntl.h>

#include <iostream>

#include "args.hxx"
Expand Down Expand Up @@ -64,9 +62,10 @@ int main(int argc, char* argv[]) {
pathToHistory += "history.txt";
auto localFileSystem = std::make_unique<LocalFileSystem>();
try {
localFileSystem->openFile(pathToHistory, O_CREAT);
localFileSystem->openFile(pathToHistory,
FileFlags::CREATE_IF_NOT_EXISTS | FileFlags::WRITE | FileFlags::READ_ONLY);
} catch (Exception& e) {
std::cerr << "Invalid path to directory for history file" << '\n';
std::cerr << "Invalid path " + pathToHistory + " to directory for history file" << '\n';
return 1;
}
std::cout << "Opened the database at path: " << databasePath << " in "
Expand Down

0 comments on commit bfc182f

Please sign in to comment.