#pragma once #include #include #include #include #include #include namespace Void::Storage::detail { class TableReader final { public: [[nodiscard]] static Result> open( std::shared_ptr 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& 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> lookup( ByteView key, Sequence snap_seq) const; /// Read and decode block `block_idx` while verifying its CRC32C. [[nodiscard]] Result> read_block( std::size_t block_idx) const; /// Verify every block's CRC32C, key ordering, AND full-file CRC32C. [[nodiscard]] Result verify_all_blocks_and_file_crc() const; private: TableReader() = default; std::shared_ptr m_port{}; std::filesystem::path m_path{}; TableMeta m_meta{}; TableHeader m_header{}; TableFooter m_footer{}; Limits m_limits{}; std::vector m_index{}; mutable std::mutex m_handle_mu{}; mutable std::unique_ptr m_cached_handle{}; }; [[nodiscard]] Result 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& 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 entries{}; }; struct WalSegmentScanResult { WalSegmentHeader header{}; std::vector batches{}; std::uint64_t file_size{0}; std::optional truncate_offset{}; }; [[nodiscard]] Result scan_wal_segment( FilePort& port, const std::filesystem::path& path, Void::Uuid::Uuid expected_uuid, std::uint64_t expected_seg_id, std::optional 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 salvage_wal_segment( FilePort& port, const std::filesystem::path& path, Void::Uuid::Uuid expected_uuid, const Limits& limits); } // namespace Void::Storage::detail