Skip to content

Commit

Permalink
separate shadow pages from wal records; rework wal to use serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
ray6080 committed Apr 7, 2024
1 parent 8006723 commit 1f4a573
Show file tree
Hide file tree
Showing 21 changed files with 528 additions and 546 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.15)

project(Kuzu VERSION 0.3.2.5 LANGUAGES CXX C)
project(Kuzu VERSION 0.3.2.6 LANGUAGES CXX C)

find_package(Threads REQUIRED)

Expand Down
13 changes: 11 additions & 2 deletions src/common/serializer/buffered_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <cstring>

#include "common/assert.h"
#include "common/file_system/file_info.h"

namespace kuzu {
Expand All @@ -15,7 +16,9 @@ BufferedFileWriter::BufferedFileWriter(std::unique_ptr<FileInfo> fileInfo)
: buffer(std::make_unique<uint8_t[]>(BUFFER_SIZE)), fileOffset(0), bufferOffset(0),
fileInfo(std::move(fileInfo)) {}

// TODO: write assumes size is always less than BUFFER_SIZE here. should change this when needed.
void BufferedFileWriter::write(const uint8_t* data, uint64_t size) {
KU_ASSERT(size <= BUFFER_SIZE);
if (bufferOffset + size <= BUFFER_SIZE) {
memcpy(&buffer[bufferOffset], data, size);
bufferOffset += size;
Expand All @@ -39,7 +42,8 @@ void BufferedFileWriter::flush() {

BufferedFileReader::BufferedFileReader(std::unique_ptr<FileInfo> fileInfo)
: buffer(std::make_unique<uint8_t[]>(BUFFER_SIZE)), fileOffset(0), bufferOffset(0),
fileInfo(std::move(fileInfo)) {
fileInfo(std::move(fileInfo)), bufferSize{0} {
fileSize = this->fileInfo->getFileSize();
readNextPage();
}

Expand All @@ -58,9 +62,14 @@ void BufferedFileReader::read(uint8_t* data, uint64_t size) {
}
}

bool BufferedFileReader::finished() {
return bufferOffset >= bufferSize && fileSize <= fileOffset;
}

void BufferedFileReader::readNextPage() {
fileInfo->readFromFile(buffer.get(), BUFFER_SIZE, fileOffset);
fileOffset += BUFFER_SIZE;
bufferSize = std::min(fileSize - fileOffset, BUFFER_SIZE);
fileOffset += bufferSize;
bufferOffset = 0;
}

Expand Down
14 changes: 9 additions & 5 deletions src/include/common/cast.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <typeinfo>

#include "common/assert.h"

namespace kuzu {
Expand All @@ -8,11 +10,13 @@ namespace common {
template<typename FROM, typename TO>
TO ku_dynamic_cast(FROM old) {
#if defined(KUZU_RUNTIME_CHECKS) || !defined(NDEBUG)
TO newVal = dynamic_cast<TO>(old);
if constexpr (std::is_pointer<FROM>()) {
KU_ASSERT(newVal != nullptr);
}
return newVal;
try {
TO newVal = dynamic_cast<TO>(old);
if constexpr (std::is_pointer<FROM>()) {
KU_ASSERT(newVal != nullptr);
}
return newVal;
} catch (std::bad_cast& e) { KU_ASSERT(false); }
#else
return reinterpret_cast<TO>(old);
#endif
Expand Down
1 change: 1 addition & 0 deletions src/include/common/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ struct BufferPoolConstants {
struct StorageConstants {
static constexpr char OVERFLOW_FILE_SUFFIX[] = ".ovf";
static constexpr char WAL_FILE_SUFFIX[] = ".wal";
static constexpr char SHADOWING_SUFFIX[] = ".shadow";
static constexpr char INDEX_FILE_SUFFIX[] = ".hindex";
static constexpr char NODES_STATISTICS_AND_DELETED_IDS_FILE_NAME[] =
"nodes.statistics_and_deleted.ids";
Expand Down
1 change: 1 addition & 0 deletions src/include/common/file_system/file_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ struct KUZU_API FileInfo {

virtual ~FileInfo() = default;

// TODO: This function should be marked as const.
uint64_t getFileSize();

void readFromFile(void* buffer, uint64_t numBytes, uint64_t position);
Expand Down
22 changes: 18 additions & 4 deletions src/include/common/serializer/buffered_file.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#pragma once

#include <cstdint>
#include <memory>

#include "common/constants.h"
Expand All @@ -19,13 +18,22 @@ class BufferedFileWriter final : public Writer {

void write(const uint8_t* data, uint64_t size) override;

void flush();

void resetOffsets() {
fileOffset = 0;
bufferOffset = 0;
}

uint64_t getFileOffset() const { return fileOffset; }

FileInfo& getFileInfo() { return *fileInfo; }

protected:
std::unique_ptr<uint8_t[]> buffer;
uint64_t fileOffset, bufferOffset;
std::unique_ptr<FileInfo> fileInfo;
static const uint64_t BUFFER_SIZE = BufferPoolConstants::PAGE_4KB_SIZE;

void flush();
};

class BufferedFileReader final : public Reader {
Expand All @@ -34,12 +42,18 @@ class BufferedFileReader final : public Reader {

void read(uint8_t* data, uint64_t size) override;

bool finished() override;

private:
static constexpr uint64_t BUFFER_SIZE = BufferPoolConstants::PAGE_4KB_SIZE;

std::unique_ptr<uint8_t[]> buffer;
uint64_t fileOffset, bufferOffset;
std::unique_ptr<FileInfo> fileInfo;
static const uint64_t BUFFER_SIZE = BufferPoolConstants::PAGE_4KB_SIZE;
uint64_t fileSize;
uint64_t bufferSize;

private:
void readNextPage();
};

Expand Down
3 changes: 2 additions & 1 deletion src/include/common/serializer/deserializer.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#pragma once

#include <cstdint>
#include <memory>
#include <string>
#include <unordered_map>
Expand All @@ -16,6 +15,8 @@ class Deserializer {
public:
explicit Deserializer(std::unique_ptr<Reader> reader) : reader(std::move(reader)) {}

bool finished() { return reader->finished(); }

template<typename T>
requires std::is_trivially_destructible<T>::value || std::is_same<std::string, T>::value
void deserializeValue(T& value) {
Expand Down
2 changes: 2 additions & 0 deletions src/include/common/serializer/reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class Reader {
public:
virtual void read(uint8_t* data, uint64_t size) = 0;
virtual ~Reader() = default;

virtual bool finished() = 0;
};

} // namespace common
Expand Down
3 changes: 0 additions & 3 deletions src/include/main/client_context.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
#pragma once

#include <atomic>
#include <cstdint>
#include <memory>
#include <mutex>

#include "client_config.h"
#include "common/task_system/progress_bar.h"
#include "common/timer.h"
#include "common/types/value/value.h"
#include "function/function.h"
#include "function/table/scan_replacement.h"
#include "main/kuzu_fwd.h"
#include "parser/statement.h"
#include "prepared_statement.h"
#include "query_result.h"
Expand Down
1 change: 1 addition & 0 deletions src/include/storage/stats/table_statistics_collection.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class TablesStatistics {

inline void checkpointInMemoryIfNecessary() {
std::unique_lock lck{mtx};
KU_ASSERT(readWriteVersion);
readOnlyVersion = std::move(readWriteVersion);
resetToNotUpdated();
}
Expand Down
13 changes: 8 additions & 5 deletions src/include/storage/storage_structure/db_file_utils.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
#pragma once

#include <cstdint>
#include <functional>

#include "common/types/types.h"
#include "storage/buffer_manager/bm_file_handle.h"
#include "storage/buffer_manager/buffer_manager.h"
#include "storage/wal/wal.h"
#include "transaction/transaction.h"

namespace kuzu {
namespace transaction {
enum class TransactionType : uint8_t;
} // namespace transaction

namespace storage {

struct DBFileID;
class BMFileHandle;
class BufferManager;
class WAL;
class DBFileUtils {
public:
constexpr static common::page_idx_t NULL_PAGE_IDX = common::INVALID_PAGE_IDX;
Expand Down
Loading

0 comments on commit 1f4a573

Please sign in to comment.