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,193 @@
|
||||
#include "networked_kv/domain.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
namespace NetworkedKv
|
||||
{
|
||||
namespace
|
||||
{
|
||||
[[nodiscard]] Void::Result::Error invalid(std::string message)
|
||||
{
|
||||
return Void::Result::Error{Void::Result::ErrorKind::invalid_argument,
|
||||
std::move(message)};
|
||||
}
|
||||
|
||||
[[nodiscard]] bool matches(const std::optional<Entry>& current,
|
||||
const Precondition& precondition) noexcept
|
||||
{
|
||||
if (precondition.kind == PreconditionKind::Absent) {
|
||||
return !current.has_value();
|
||||
}
|
||||
return current.has_value() &&
|
||||
current->version == precondition.version;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
MemoryKvStore::MemoryKvStore(Limits limits)
|
||||
: m_limits(limits)
|
||||
{
|
||||
}
|
||||
|
||||
Void::Result::Result<void>
|
||||
MemoryKvStore::validate_key(std::string_view key) const
|
||||
{
|
||||
if (key.empty()) {
|
||||
return std::unexpected{invalid("KvStore: key must not be empty")};
|
||||
}
|
||||
if (key.size() > m_limits.max_key_bytes) {
|
||||
return std::unexpected{
|
||||
invalid("KvStore: key exceeds max_key_bytes")};
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
Void::Result::Result<void>
|
||||
MemoryKvStore::validate_request(const RequestId& request) const
|
||||
{
|
||||
if (request.value.empty()) {
|
||||
return std::unexpected{
|
||||
invalid("KvStore: request id must not be empty")};
|
||||
}
|
||||
if (request.value.size() > m_limits.max_request_id_bytes) {
|
||||
return std::unexpected{
|
||||
invalid("KvStore: request id exceeds max_request_id_bytes")};
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
std::optional<WriteResult>
|
||||
MemoryKvStore::replay(const RequestId& request) const
|
||||
{
|
||||
const auto found = m_operations.find(request.value);
|
||||
if (found == m_operations.end()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return found->second;
|
||||
}
|
||||
|
||||
void MemoryKvStore::remember(const RequestId& request,
|
||||
const WriteResult& result)
|
||||
{
|
||||
if (m_limits.max_operation_records == 0) {
|
||||
return;
|
||||
}
|
||||
if (m_operations.size() >= m_limits.max_operation_records &&
|
||||
!m_operation_order.empty()) {
|
||||
m_operations.erase(m_operation_order.front());
|
||||
m_operation_order.erase(m_operation_order.begin());
|
||||
}
|
||||
m_operations.emplace(request.value, result);
|
||||
m_operation_order.push_back(request.value);
|
||||
}
|
||||
|
||||
Void::Result::Result<std::optional<Entry>>
|
||||
MemoryKvStore::get(std::string_view key) const
|
||||
{
|
||||
if (auto valid = validate_key(key); !valid) {
|
||||
return std::unexpected{std::move(valid).error()};
|
||||
}
|
||||
std::lock_guard lock{m_mutex};
|
||||
const auto found = m_entries.find(key);
|
||||
if (found == m_entries.end()) {
|
||||
return std::optional<Entry>{};
|
||||
}
|
||||
return std::optional<Entry>{found->second};
|
||||
}
|
||||
|
||||
Void::Result::Result<WriteResult>
|
||||
MemoryKvStore::put(std::string_view key, std::string value,
|
||||
Precondition precondition, RequestId request)
|
||||
{
|
||||
if (auto valid = validate_key(key); !valid) {
|
||||
return std::unexpected{std::move(valid).error()};
|
||||
}
|
||||
if (auto valid = validate_request(request); !valid) {
|
||||
return std::unexpected{std::move(valid).error()};
|
||||
}
|
||||
if (value.size() > m_limits.max_value_bytes) {
|
||||
return std::unexpected{
|
||||
invalid("KvStore: value exceeds max_value_bytes")};
|
||||
}
|
||||
|
||||
std::lock_guard lock{m_mutex};
|
||||
if (const auto replayed = replay(request)) {
|
||||
return *replayed;
|
||||
}
|
||||
|
||||
const auto found = m_entries.find(key);
|
||||
const std::optional<Entry> current =
|
||||
found == m_entries.end() ? std::nullopt
|
||||
: std::optional<Entry>{found->second};
|
||||
if (!matches(current, precondition)) {
|
||||
WriteResult result{WriteOutcome::Conflict,
|
||||
current ? current->version : 0};
|
||||
remember(request, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
const Version version = current ? current->version + 1 : 1;
|
||||
m_entries.insert_or_assign(
|
||||
std::string{key}, Entry{std::move(value), version});
|
||||
WriteResult result{WriteOutcome::Applied, version};
|
||||
remember(request, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
Void::Result::Result<WriteResult>
|
||||
MemoryKvStore::erase(std::string_view key, Precondition precondition,
|
||||
RequestId request)
|
||||
{
|
||||
if (auto valid = validate_key(key); !valid) {
|
||||
return std::unexpected{std::move(valid).error()};
|
||||
}
|
||||
if (auto valid = validate_request(request); !valid) {
|
||||
return std::unexpected{std::move(valid).error()};
|
||||
}
|
||||
|
||||
std::lock_guard lock{m_mutex};
|
||||
if (const auto replayed = replay(request)) {
|
||||
return *replayed;
|
||||
}
|
||||
|
||||
const auto found = m_entries.find(key);
|
||||
if (found == m_entries.end()) {
|
||||
WriteResult result{WriteOutcome::NotFound, 0};
|
||||
remember(request, result);
|
||||
return result;
|
||||
}
|
||||
const std::optional<Entry> current = found->second;
|
||||
if (!matches(current, precondition)) {
|
||||
WriteResult result{WriteOutcome::Conflict, current->version};
|
||||
remember(request, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
m_entries.erase(found);
|
||||
WriteResult result{WriteOutcome::Applied, 0};
|
||||
remember(request, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
Void::Result::Result<ScanPage>
|
||||
MemoryKvStore::scan(std::optional<std::string_view> cursor,
|
||||
std::size_t limit) const
|
||||
{
|
||||
const std::size_t page =
|
||||
std::min(limit == 0 ? m_limits.max_scan_page : limit,
|
||||
m_limits.max_scan_page);
|
||||
|
||||
std::lock_guard lock{m_mutex};
|
||||
ScanPage out;
|
||||
auto it = cursor ? m_entries.upper_bound(*cursor) : m_entries.begin();
|
||||
while (it != m_entries.end() && out.items.size() < page) {
|
||||
out.items.push_back(ScanItem{it->first, it->second});
|
||||
++it;
|
||||
}
|
||||
if (it != m_entries.end() && !out.items.empty()) {
|
||||
out.next_cursor = out.items.back().key;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
} // namespace NetworkedKv
|
||||
Reference in new issue
Block a user