Skip to content

Commit

Permalink
Merge pull request #9687 from carlopi/storage_version
Browse files Browse the repository at this point in the history
Deserialize header fields
  • Loading branch information
Mytherin committed Nov 20, 2023
2 parents 3b81018 + 70accbd commit 31f338d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
14 changes: 13 additions & 1 deletion src/include/duckdb/storage/storage_info.hpp
Expand Up @@ -9,6 +9,7 @@
#pragma once

#include "duckdb/common/constants.hpp"
#include "duckdb/common/string.hpp"
#include "duckdb/common/vector_size.hpp"

namespace duckdb {
Expand Down Expand Up @@ -57,6 +58,7 @@ using block_id_t = int64_t;
//! The MainHeader is the first header in the storage file. The MainHeader is typically written only once for a database
//! file.
struct MainHeader {
static constexpr idx_t MAX_VERSION_SIZE = 32;
static constexpr idx_t MAGIC_BYTE_SIZE = 4;
static constexpr idx_t MAGIC_BYTE_OFFSET = Storage::BLOCK_HEADER_SIZE;
static constexpr idx_t FLAG_COUNT = 4;
Expand All @@ -67,11 +69,21 @@ struct MainHeader {
uint64_t version_number;
//! The set of flags used by the database
uint64_t flags[FLAG_COUNT];

static void CheckMagicBytes(FileHandle &handle);

string LibraryGitDesc() {
return string((char *)library_git_desc, 0, MAX_VERSION_SIZE);
}
string LibraryGitHash() {
return string((char *)library_git_hash, 0, MAX_VERSION_SIZE);
}

void Write(WriteStream &ser);
static MainHeader Read(ReadStream &source);

private:
data_t library_git_desc[MAX_VERSION_SIZE];
data_t library_git_hash[MAX_VERSION_SIZE];
};

//! The DatabaseHeader contains information about the current state of the database. Every storage file has two
Expand Down
16 changes: 11 additions & 5 deletions src/storage/single_file_block_manager.cpp
Expand Up @@ -18,11 +18,15 @@ namespace duckdb {
const char MainHeader::MAGIC_BYTES[] = "DUCK";

void SerializeVersionNumber(WriteStream &ser, const string &version_str) {
constexpr const idx_t MAX_VERSION_SIZE = 32;
data_t version[MAX_VERSION_SIZE];
memset(version, 0, MAX_VERSION_SIZE);
memcpy(version, version_str.c_str(), MinValue<idx_t>(version_str.size(), MAX_VERSION_SIZE));
ser.WriteData(version, MAX_VERSION_SIZE);
data_t version[MainHeader::MAX_VERSION_SIZE];
memset(version, 0, MainHeader::MAX_VERSION_SIZE);
memcpy(version, version_str.c_str(), MinValue<idx_t>(version_str.size(), MainHeader::MAX_VERSION_SIZE));
ser.WriteData(version, MainHeader::MAX_VERSION_SIZE);
}

void DeserializeVersionNumber(ReadStream &stream, data_t *dest) {
memset(dest, 0, MainHeader::MAX_VERSION_SIZE);
stream.ReadData(dest, MainHeader::MAX_VERSION_SIZE);
}

void MainHeader::Write(WriteStream &ser) {
Expand Down Expand Up @@ -81,6 +85,8 @@ MainHeader MainHeader::Read(ReadStream &source) {
for (idx_t i = 0; i < FLAG_COUNT; i++) {
header.flags[i] = source.Read<uint64_t>();
}
DeserializeVersionNumber(source, header.library_git_desc);
DeserializeVersionNumber(source, header.library_git_hash);
return header;
}

Expand Down

0 comments on commit 31f338d

Please sign in to comment.