91 lines
3.2 KiB
C++
91 lines
3.2 KiB
C++
#pragma once
|
|
|
|
#include "evo/world/chunks.hpp"
|
|
#include <cstdint>
|
|
#include <expected>
|
|
#include <filesystem>
|
|
#include <memory>
|
|
#include <optional>
|
|
#include <span>
|
|
|
|
namespace evo::world {
|
|
enum class LocalFileError : std::uint8_t {
|
|
invalid_identity,
|
|
busy,
|
|
empty,
|
|
corrupt_manifest,
|
|
corrupt_payload,
|
|
identity_mismatch,
|
|
invalid_snapshot,
|
|
invalid_journal,
|
|
stale_checkpoint,
|
|
journal_already_published,
|
|
generation_exhausted,
|
|
io,
|
|
interrupted,
|
|
poisoned
|
|
};
|
|
enum class PublicationInterruption : std::uint8_t {
|
|
none,
|
|
after_payload_sync,
|
|
after_manifest_sync,
|
|
after_manifest_publish
|
|
};
|
|
enum class PublicationFailure : std::uint8_t {
|
|
none,
|
|
payload_out_of_space,
|
|
manifest_out_of_space,
|
|
manifest_directory_sync
|
|
};
|
|
struct FileCommit {
|
|
std::uint64_t generation{}, covered_command{}, snapshot_checksum{};
|
|
bool has_journal{};
|
|
auto operator<=>(const FileCommit&) const = default;
|
|
};
|
|
class LocalWorldFiles {
|
|
public:
|
|
[[nodiscard]] static std::expected<std::unique_ptr<LocalWorldFiles>, LocalFileError>
|
|
open(const std::filesystem::path& trusted_root, WorldIdentity identity, std::size_t byte_budget,
|
|
PublicationInterruption interrupt_once = PublicationInterruption::none,
|
|
PublicationFailure fail_once = PublicationFailure::none);
|
|
LocalWorldFiles(const LocalWorldFiles&) = delete;
|
|
LocalWorldFiles& operator=(const LocalWorldFiles&) = delete;
|
|
LocalWorldFiles(LocalWorldFiles&&) = delete;
|
|
LocalWorldFiles& operator=(LocalWorldFiles&&) = delete;
|
|
~LocalWorldFiles();
|
|
// An ambiguous publication failure poisons this handle; reopen before recovery.
|
|
[[nodiscard]] std::expected<std::unique_ptr<ChunkStore>, LocalFileError> recover() const;
|
|
// While a journal is committed, compaction requires its byte-identical replay result.
|
|
[[nodiscard]] std::expected<FileCommit, LocalFileError> checkpoint(const ChunkStore& store);
|
|
[[nodiscard]] std::expected<FileCommit, LocalFileError>
|
|
publish_journal(std::span<const std::uint8_t> checkpoint,
|
|
std::span<const std::uint8_t> journal);
|
|
// Empty on an uninitialized or poisoned handle.
|
|
[[nodiscard]] std::optional<FileCommit> current() const noexcept;
|
|
|
|
private:
|
|
struct Lock;
|
|
struct Manifest {
|
|
std::uint64_t generation{}, snapshot_checksum{}, covered_command{};
|
|
std::uint64_t journal_checksum{}, journal_end{};
|
|
bool has_journal{};
|
|
};
|
|
LocalWorldFiles(std::filesystem::path directory, WorldIdentity identity, std::size_t budget,
|
|
PublicationInterruption interruption, PublicationFailure failure,
|
|
std::unique_ptr<Lock> lock);
|
|
[[nodiscard]] std::expected<FileCommit, LocalFileError>
|
|
publish(std::span<const std::uint8_t> payload, bool journal, std::uint64_t covered,
|
|
std::uint64_t payload_checksum);
|
|
std::filesystem::path directory_;
|
|
WorldIdentity identity_;
|
|
std::size_t budget_;
|
|
PublicationInterruption interruption_;
|
|
PublicationFailure failure_;
|
|
std::unique_ptr<Lock> lock_;
|
|
std::optional<Manifest> manifest_;
|
|
std::size_t active_slot_{};
|
|
std::uint64_t generation_high_water_{};
|
|
bool poisoned_{};
|
|
};
|
|
} // namespace evo::world
|