99 lines
3.0 KiB
C++
99 lines
3.0 KiB
C++
#pragma once
|
|
|
|
#include <Void/Storage/FilePort.hpp>
|
|
#include <Void/Storage/Format.hpp>
|
|
|
|
#include <filesystem>
|
|
#include <memory>
|
|
#include <mutex>
|
|
#include <vector>
|
|
|
|
namespace Void::Storage::detail
|
|
{
|
|
class TableReader final
|
|
{
|
|
public:
|
|
[[nodiscard]] static Result<std::shared_ptr<TableReader>> open(
|
|
std::shared_ptr<FilePort> port,
|
|
std::filesystem::path path,
|
|
Void::Uuid::Uuid expected_uuid,
|
|
const TableMeta& meta,
|
|
const Limits& limits);
|
|
|
|
[[nodiscard]] const TableMeta& meta() const noexcept { return m_meta; }
|
|
[[nodiscard]] const std::filesystem::path& path() const noexcept { return m_path; }
|
|
[[nodiscard]] const std::vector<TableBlockIndexEntry>& index() const noexcept
|
|
{
|
|
return m_index;
|
|
}
|
|
|
|
/// Lookup highest version <= snap_seq for exact `key`.
|
|
/// Returns std::nullopt if `key` is absent at <= snap_seq in this table;
|
|
/// returns TableEntryRecord{key, seq, erased, value} if present (including tombstone).
|
|
[[nodiscard]] Result<std::optional<TableEntryRecord>> lookup(
|
|
ByteView key,
|
|
Sequence snap_seq) const;
|
|
|
|
/// Read and decode block `block_idx` while verifying its CRC32C.
|
|
[[nodiscard]] Result<std::vector<TableEntryRecord>> read_block(
|
|
std::size_t block_idx) const;
|
|
|
|
/// Verify every block's CRC32C, key ordering, AND full-file CRC32C.
|
|
[[nodiscard]] Result<std::uint64_t> verify_all_blocks_and_file_crc() const;
|
|
|
|
private:
|
|
TableReader() = default;
|
|
|
|
std::shared_ptr<FilePort> m_port{};
|
|
std::filesystem::path m_path{};
|
|
TableMeta m_meta{};
|
|
TableHeader m_header{};
|
|
TableFooter m_footer{};
|
|
Limits m_limits{};
|
|
std::vector<TableBlockIndexEntry> m_index{};
|
|
mutable std::mutex m_handle_mu{};
|
|
mutable std::unique_ptr<FileHandle> m_cached_handle{};
|
|
};
|
|
|
|
[[nodiscard]] Result<TableMeta> write_sorted_table(
|
|
FilePort& port,
|
|
const std::filesystem::path& tmp_path,
|
|
const std::filesystem::path& final_path,
|
|
Void::Uuid::Uuid db_uuid,
|
|
std::uint64_t table_id,
|
|
std::uint32_t level,
|
|
const std::vector<TableEntryRecord>& sorted_entries,
|
|
const Limits& limits);
|
|
|
|
/// Validate and replay a WAL segment, filling `recovered_batches` with valid committed batches.
|
|
struct WalRecoveredBatch {
|
|
Sequence sequence{0};
|
|
std::vector<TableEntryRecord> entries{};
|
|
};
|
|
|
|
struct WalSegmentScanResult {
|
|
WalSegmentHeader header{};
|
|
std::vector<WalRecoveredBatch> batches{};
|
|
std::uint64_t file_size{0};
|
|
std::optional<std::uint64_t> truncate_offset{};
|
|
};
|
|
|
|
[[nodiscard]] Result<WalSegmentScanResult> scan_wal_segment(
|
|
FilePort& port,
|
|
const std::filesystem::path& path,
|
|
Void::Uuid::Uuid expected_uuid,
|
|
std::uint64_t expected_seg_id,
|
|
std::optional<std::uint64_t> expected_prev_seg_id,
|
|
bool is_final_active_segment,
|
|
const Limits& limits,
|
|
bool allow_uncommitted_tail = true);
|
|
|
|
/// Salvage scan: recovers as many valid complete committed batches as possible before stopping.
|
|
[[nodiscard]] Result<WalSegmentScanResult> salvage_wal_segment(
|
|
FilePort& port,
|
|
const std::filesystem::path& path,
|
|
Void::Uuid::Uuid expected_uuid,
|
|
const Limits& limits);
|
|
|
|
} // namespace Void::Storage::detail
|