94 lines
2.2 KiB
C++
94 lines
2.2 KiB
C++
#pragma once
|
|
|
|
#include <Void/Storage/Types.hpp>
|
|
|
|
#include <cstdint>
|
|
#include <optional>
|
|
#include <string_view>
|
|
#include <vector>
|
|
|
|
namespace Void::Storage
|
|
{
|
|
enum class MutationKind : std::uint8_t {
|
|
Put = 1,
|
|
Erase = 2
|
|
};
|
|
|
|
enum class PreconditionKind : std::uint8_t {
|
|
Absent = 1,
|
|
VersionEquals = 2,
|
|
ValueEquals = 3
|
|
};
|
|
|
|
struct Mutation {
|
|
MutationKind kind{MutationKind::Put};
|
|
OwnedBytes key{};
|
|
OwnedBytes value{};
|
|
};
|
|
|
|
struct Precondition {
|
|
PreconditionKind kind{PreconditionKind::Absent};
|
|
OwnedBytes key{};
|
|
Sequence expected_version{0};
|
|
OwnedBytes expected_value{};
|
|
};
|
|
|
|
/// Atomic conditional write batch owning all key and value bytes.
|
|
/// Rejects duplicate mutation keys and duplicate preconditions per key.
|
|
class Batch
|
|
{
|
|
public:
|
|
Batch() = default;
|
|
|
|
Batch& put(ByteView key, ByteView value);
|
|
Batch& put(std::string_view key, std::string_view value)
|
|
{
|
|
return put(to_byte_view(key), to_byte_view(value));
|
|
}
|
|
|
|
Batch& erase(ByteView key);
|
|
Batch& erase(std::string_view key)
|
|
{
|
|
return erase(to_byte_view(key));
|
|
}
|
|
|
|
Batch& require_absent(ByteView key);
|
|
Batch& require_absent(std::string_view key)
|
|
{
|
|
return require_absent(to_byte_view(key));
|
|
}
|
|
|
|
Batch& require_version(ByteView key, Sequence expected_version);
|
|
Batch& require_version(std::string_view key, Sequence expected_version)
|
|
{
|
|
return require_version(to_byte_view(key), expected_version);
|
|
}
|
|
|
|
Batch& require_value(ByteView key, ByteView expected_value);
|
|
Batch& require_value(std::string_view key, std::string_view expected_value)
|
|
{
|
|
return require_value(to_byte_view(key), to_byte_view(expected_value));
|
|
}
|
|
|
|
[[nodiscard]] const std::vector<Mutation>& mutations() const noexcept
|
|
{
|
|
return m_mutations;
|
|
}
|
|
[[nodiscard]] const std::vector<Precondition>& preconditions() const noexcept
|
|
{
|
|
return m_preconditions;
|
|
}
|
|
[[nodiscard]] bool empty() const noexcept
|
|
{
|
|
return m_mutations.empty() && m_preconditions.empty();
|
|
}
|
|
|
|
[[nodiscard]] Result<void> validate(const Limits& limits) const;
|
|
|
|
private:
|
|
std::vector<Mutation> m_mutations{};
|
|
std::vector<Precondition> m_preconditions{};
|
|
};
|
|
|
|
} // namespace Void::Storage
|