131 lines
3.9 KiB
C++
131 lines
3.9 KiB
C++
#include "networked_kv/domain.hpp"
|
|
|
|
#include <cassert>
|
|
#include <cstdio>
|
|
#include <optional>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
namespace
|
|
{
|
|
using namespace NetworkedKv;
|
|
|
|
void versioned_lifecycle()
|
|
{
|
|
MemoryKvStore store;
|
|
auto created = store.put("a", "one", Precondition::absent(), {"r1"});
|
|
assert(created.has_value());
|
|
assert(created->outcome == WriteOutcome::Applied);
|
|
assert(created->version == 1);
|
|
|
|
auto got = store.get("a");
|
|
assert(got.has_value() && got->has_value());
|
|
assert((*got)->value == "one");
|
|
|
|
// Absent precondition no longer holds.
|
|
auto conflict = store.put("a", "two", Precondition::absent(), {"r2"});
|
|
assert(conflict.has_value());
|
|
assert(conflict->outcome == WriteOutcome::Conflict);
|
|
|
|
auto updated =
|
|
store.put("a", "two", Precondition::at_version(1), {"r3"});
|
|
assert(updated.has_value());
|
|
assert(updated->outcome == WriteOutcome::Applied);
|
|
assert(updated->version == 2);
|
|
|
|
// Replaying the same request id returns the recorded outcome without
|
|
// applying the mutation again.
|
|
auto replay =
|
|
store.put("a", "ignored", Precondition::at_version(2), {"r3"});
|
|
assert(replay.has_value());
|
|
assert(replay->outcome == WriteOutcome::Applied);
|
|
assert(replay->version == 2);
|
|
auto current = store.get("a");
|
|
assert(current.has_value() && current->has_value());
|
|
assert((*current)->value == "two");
|
|
assert((*current)->version == 2);
|
|
|
|
auto stale = store.erase("a", Precondition::at_version(1), {"r4"});
|
|
assert(stale.has_value());
|
|
assert(stale->outcome == WriteOutcome::Conflict);
|
|
|
|
auto erased = store.erase("a", Precondition::at_version(2), {"r5"});
|
|
assert(erased.has_value());
|
|
assert(erased->outcome == WriteOutcome::Applied);
|
|
assert(!store.get("a")->has_value());
|
|
|
|
auto missing = store.erase("missing", Precondition::absent(), {"r6"});
|
|
assert(missing.has_value());
|
|
assert(missing->outcome == WriteOutcome::NotFound);
|
|
}
|
|
|
|
void input_validation()
|
|
{
|
|
MemoryKvStore store;
|
|
assert(!store.put("", "x", Precondition::absent(), {"r"}).has_value());
|
|
assert(!store.put("k", "x", Precondition::absent(), {""}).has_value());
|
|
|
|
MemoryKvStore small{Limits{.max_key_bytes = 2, .max_value_bytes = 2}};
|
|
assert(!small.put("key", "x", Precondition::absent(), {"r"}).has_value());
|
|
assert(!small.put("k", "long", Precondition::absent(), {"r"}).has_value());
|
|
}
|
|
|
|
void bounded_scan()
|
|
{
|
|
MemoryKvStore store{Limits{.max_scan_page = 2}};
|
|
for (int i = 0; i < 5; ++i) {
|
|
const std::string key(1, static_cast<char>('a' + i));
|
|
auto written = store.put(key, "v", Precondition::absent(),
|
|
{"s" + std::to_string(i)});
|
|
assert(written.has_value());
|
|
}
|
|
|
|
std::vector<std::string> keys;
|
|
std::optional<std::string> cursor;
|
|
for (;;) {
|
|
auto page = store.scan(
|
|
cursor ? std::optional<std::string_view>{*cursor} : std::nullopt);
|
|
assert(page.has_value());
|
|
assert(page->items.size() <= 2);
|
|
for (const auto& item : page->items) {
|
|
keys.push_back(item.key);
|
|
}
|
|
if (!page->next_cursor) {
|
|
break;
|
|
}
|
|
cursor = page->next_cursor;
|
|
}
|
|
assert(keys.size() == 5);
|
|
assert(keys.front() == "a");
|
|
assert(keys.back() == "e");
|
|
}
|
|
|
|
void bounded_operation_records()
|
|
{
|
|
MemoryKvStore store{Limits{.max_operation_records = 2}};
|
|
for (int i = 0; i < 5; ++i) {
|
|
const auto precondition = i == 0 ? Precondition::absent()
|
|
: Precondition::at_version(
|
|
static_cast<Version>(i));
|
|
auto written = store.put("k", "v" + std::to_string(i), precondition,
|
|
{"b" + std::to_string(i)});
|
|
assert(written.has_value());
|
|
assert(written->outcome == WriteOutcome::Applied);
|
|
}
|
|
// The store keeps working after evicting old operation records.
|
|
auto got = store.get("k");
|
|
assert(got.has_value() && got->has_value());
|
|
assert((*got)->version == 5);
|
|
}
|
|
} // namespace
|
|
|
|
int main()
|
|
{
|
|
versioned_lifecycle();
|
|
input_validation();
|
|
bounded_scan();
|
|
bounded_operation_records();
|
|
std::puts("networked-kv domain ok");
|
|
return 0;
|
|
}
|