NetworkedApplications: versioned KV domain with idempotent outcomes
This commit is contained in:
1 parent
a2a0e1a4c3
commit
3253b8b4d4
4 files changed
+477
-1
No files matched your search
@@ -0,0 +1,149 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Functional/Result.hpp>
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <map>
|
||||
#include <mutex>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace NetworkedKv
|
||||
{
|
||||
using Version = std::uint64_t;
|
||||
|
||||
struct Limits {
|
||||
std::size_t max_key_bytes{512};
|
||||
std::size_t max_value_bytes{1024ULL * 1024ULL};
|
||||
std::size_t max_request_id_bytes{128};
|
||||
std::size_t max_scan_page{1000};
|
||||
std::size_t max_operation_records{1024};
|
||||
};
|
||||
|
||||
struct Entry {
|
||||
std::string value;
|
||||
Version version{1};
|
||||
};
|
||||
|
||||
enum class PreconditionKind : std::uint8_t { Absent, Version };
|
||||
|
||||
/// Explicit absent/version precondition for a mutation.
|
||||
struct Precondition {
|
||||
PreconditionKind kind{PreconditionKind::Absent};
|
||||
Version version{0};
|
||||
|
||||
[[nodiscard]] static Precondition absent() noexcept
|
||||
{
|
||||
return {};
|
||||
}
|
||||
[[nodiscard]] static Precondition at_version(Version value) noexcept
|
||||
{
|
||||
return {PreconditionKind::Version, value};
|
||||
}
|
||||
};
|
||||
|
||||
/// Idempotency key for a mutation. Replaying the same id returns the
|
||||
/// recorded outcome instead of applying the mutation twice.
|
||||
struct RequestId {
|
||||
std::string value;
|
||||
};
|
||||
|
||||
enum class WriteOutcome : std::uint8_t {
|
||||
Applied,
|
||||
Conflict,
|
||||
NotFound,
|
||||
/// The backend could not prove whether the write committed.
|
||||
Unknown
|
||||
};
|
||||
|
||||
struct WriteResult {
|
||||
WriteOutcome outcome{WriteOutcome::Unknown};
|
||||
Version version{0};
|
||||
};
|
||||
|
||||
struct ScanItem {
|
||||
std::string key;
|
||||
Entry entry;
|
||||
};
|
||||
|
||||
struct ScanPage {
|
||||
std::vector<ScanItem> items;
|
||||
/// Opaque cursor for the next page; empty when the scan is done.
|
||||
std::optional<std::string> next_cursor;
|
||||
};
|
||||
|
||||
/// Versioned key-value domain contract shared by storage backends.
|
||||
class KvStore
|
||||
{
|
||||
public:
|
||||
|
||||
KvStore() = default;
|
||||
KvStore(const KvStore&) = delete;
|
||||
KvStore& operator=(const KvStore&) = delete;
|
||||
virtual ~KvStore() = default;
|
||||
|
||||
[[nodiscard]] virtual Void::Result::Result<std::optional<Entry>>
|
||||
get(std::string_view key) const = 0;
|
||||
|
||||
[[nodiscard]] virtual Void::Result::Result<WriteResult>
|
||||
put(std::string_view key, std::string value,
|
||||
Precondition precondition, RequestId request) = 0;
|
||||
|
||||
[[nodiscard]] virtual Void::Result::Result<WriteResult>
|
||||
erase(std::string_view key, Precondition precondition,
|
||||
RequestId request) = 0;
|
||||
|
||||
[[nodiscard]] virtual Void::Result::Result<ScanPage>
|
||||
scan(std::optional<std::string_view> cursor,
|
||||
std::size_t limit = 0) const = 0;
|
||||
};
|
||||
|
||||
/// Bounded in-memory reference backend. Durable backends implement the
|
||||
/// same contract without changing the domain semantics.
|
||||
class MemoryKvStore final : public KvStore
|
||||
{
|
||||
public:
|
||||
|
||||
explicit MemoryKvStore(Limits limits = {});
|
||||
|
||||
[[nodiscard]] Void::Result::Result<std::optional<Entry>>
|
||||
get(std::string_view key) const override;
|
||||
|
||||
[[nodiscard]] Void::Result::Result<WriteResult>
|
||||
put(std::string_view key, std::string value,
|
||||
Precondition precondition, RequestId request) override;
|
||||
|
||||
[[nodiscard]] Void::Result::Result<WriteResult>
|
||||
erase(std::string_view key, Precondition precondition,
|
||||
RequestId request) override;
|
||||
|
||||
[[nodiscard]] Void::Result::Result<ScanPage>
|
||||
scan(std::optional<std::string_view> cursor,
|
||||
std::size_t limit = 0) const override;
|
||||
|
||||
[[nodiscard]] const Limits& limits() const noexcept
|
||||
{
|
||||
return m_limits;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
[[nodiscard]] Void::Result::Result<void>
|
||||
validate_key(std::string_view key) const;
|
||||
[[nodiscard]] Void::Result::Result<void>
|
||||
validate_request(const RequestId& request) const;
|
||||
[[nodiscard]] std::optional<WriteResult>
|
||||
replay(const RequestId& request) const;
|
||||
void remember(const RequestId& request, const WriteResult& result);
|
||||
|
||||
mutable std::mutex m_mutex;
|
||||
Limits m_limits;
|
||||
std::map<std::string, Entry, std::less<>> m_entries;
|
||||
std::map<std::string, WriteResult, std::less<>> m_operations;
|
||||
std::vector<std::string> m_operation_order;
|
||||
};
|
||||
} // namespace NetworkedKv
|
||||
Reference in new issue
Block a user