148 lines
3.9 KiB
C++
148 lines
3.9 KiB
C++
#pragma once
|
|
|
|
#include <Void/Storage/Batch.hpp>
|
|
#include <Void/Storage/FilePort.hpp>
|
|
#include <Void/Storage/Types.hpp>
|
|
|
|
#include <expected>
|
|
#include <filesystem>
|
|
#include <memory>
|
|
|
|
namespace Void::Storage
|
|
{
|
|
namespace detail
|
|
{
|
|
class EngineControl;
|
|
struct SourceView;
|
|
} // namespace detail
|
|
|
|
class Engine;
|
|
class Cursor;
|
|
|
|
/// Move-only reader pin of a committed sequence and immutable source set.
|
|
class Snapshot
|
|
{
|
|
public:
|
|
Snapshot() noexcept = default;
|
|
~Snapshot();
|
|
Snapshot(Snapshot&&) noexcept;
|
|
Snapshot& operator=(Snapshot&&) noexcept;
|
|
Snapshot(const Snapshot&) = delete;
|
|
Snapshot& operator=(const Snapshot&) = delete;
|
|
|
|
[[nodiscard]] Sequence sequence() const noexcept { return m_sequence; }
|
|
[[nodiscard]] bool valid() const noexcept { return static_cast<bool>(m_control); }
|
|
void reset() noexcept;
|
|
|
|
private:
|
|
friend class Engine;
|
|
friend class Cursor;
|
|
friend class detail::EngineControl;
|
|
|
|
Snapshot(std::shared_ptr<detail::EngineControl> control,
|
|
std::shared_ptr<const detail::SourceView> view,
|
|
Sequence seq,
|
|
std::uint64_t pin_id) noexcept;
|
|
|
|
std::shared_ptr<detail::EngineControl> m_control{};
|
|
std::shared_ptr<const detail::SourceView> m_view{};
|
|
Sequence m_sequence{0};
|
|
std::uint64_t m_pin_id{0};
|
|
};
|
|
|
|
/// Move-only ordered byte-lexicographic iterator retaining its Snapshot.
|
|
class Cursor
|
|
{
|
|
public:
|
|
Cursor() noexcept = default;
|
|
~Cursor();
|
|
Cursor(Cursor&&) noexcept;
|
|
Cursor& operator=(Cursor&&) noexcept;
|
|
Cursor(const Cursor&) = delete;
|
|
Cursor& operator=(const Cursor&) = delete;
|
|
|
|
[[nodiscard]] Result<ScanPage> next_page();
|
|
[[nodiscard]] bool valid() const noexcept { return !m_closed && m_snapshot.valid(); }
|
|
void close() noexcept;
|
|
|
|
private:
|
|
friend class Engine;
|
|
|
|
Cursor(Snapshot snap,
|
|
ScanBounds bounds,
|
|
std::size_t page_bytes,
|
|
std::size_t page_rows,
|
|
std::uint64_t cursor_id) noexcept;
|
|
|
|
Snapshot m_snapshot{};
|
|
ScanBounds m_bounds{};
|
|
std::size_t m_page_bytes{0};
|
|
std::size_t m_page_rows{0};
|
|
std::uint64_t m_cursor_id{0};
|
|
std::optional<OwnedBytes> m_resume_after{};
|
|
bool m_finished{false};
|
|
bool m_closed{false};
|
|
};
|
|
|
|
class Engine
|
|
{
|
|
public:
|
|
Engine() noexcept = default;
|
|
~Engine();
|
|
Engine(Engine&&) noexcept;
|
|
Engine& operator=(Engine&&) noexcept;
|
|
Engine(const Engine&) = delete;
|
|
Engine& operator=(const Engine&) = delete;
|
|
|
|
[[nodiscard]] static Result<Engine> open(OpenOptions options);
|
|
|
|
[[nodiscard]] Result<Snapshot> snapshot();
|
|
|
|
[[nodiscard]] std::expected<CommitReceipt, WriteFailure> write(
|
|
Batch batch,
|
|
Durability durability);
|
|
|
|
[[nodiscard]] std::expected<CommitReceipt, WriteFailure> write(Batch batch)
|
|
{
|
|
return write(std::move(batch), default_durability());
|
|
}
|
|
|
|
[[nodiscard]] Result<OwnedValue> get(
|
|
const Snapshot& snap,
|
|
ByteView key) const;
|
|
|
|
[[nodiscard]] Result<OwnedValue> get(
|
|
const Snapshot& snap,
|
|
std::string_view key) const
|
|
{
|
|
return get(snap, to_byte_view(key));
|
|
}
|
|
|
|
[[nodiscard]] Result<Cursor> scan(
|
|
Snapshot snap,
|
|
ScanBounds bounds = ScanBounds::all()) const;
|
|
|
|
[[nodiscard]] Result<void> checkpoint();
|
|
[[nodiscard]] Result<void> compact();
|
|
[[nodiscard]] Result<void> sync();
|
|
[[nodiscard]] Result<void> close();
|
|
|
|
/// Copy consistent manifest, tables, and necessary WAL prefix into
|
|
/// `destination` while keeping this engine open and pinning sources.
|
|
[[nodiscard]] Result<void> export_backup(
|
|
const std::filesystem::path& destination);
|
|
|
|
[[nodiscard]] EngineState state() const noexcept;
|
|
[[nodiscard]] EngineStats stats() const noexcept;
|
|
[[nodiscard]] Durability default_durability() const noexcept;
|
|
[[nodiscard]] const Limits& limits() const noexcept;
|
|
|
|
private:
|
|
friend class AsyncStorageAdapter;
|
|
explicit Engine(std::shared_ptr<detail::EngineControl> control) noexcept;
|
|
|
|
std::shared_ptr<detail::EngineControl> m_control{};
|
|
};
|
|
|
|
} // namespace Void::Storage
|