#include "../include/afterkey.hpp" #include "../include/afterkey_journal.hpp" #include #include #include #include #include #include #include #include #include #include #include #include namespace { using Revision = std::uint64_t; using Key = std::uint32_t; using Change = std::tuple>; using Changes = std::vector; void check(bool ok, const char* message) { if (!ok) throw std::runtime_error(message); } template void expect_exception(F action, const char* message) { try { action(); } catch (const std::exception&) { return; } throw std::runtime_error(message); } template Changes read(const Index& index, Revision since) { Changes result; index.since(since, [&](Revision rev, Key key, const int* value) { result.emplace_back(rev, key, value ? std::optional(*value) : std::nullopt); }); return result; } // Retain every write, then independently compute last-write-wins in the query window. Changes oracle(const Changes& history, Revision cursor, Key capacity) { std::vector seen(capacity); Changes answer; for (auto it = history.rbegin(); it != history.rend(); ++it) { const auto& [revision, key, value] = *it; if (revision <= cursor) break; if (!seen[key]) { seen[key] = true; answer.push_back(*it); } } std::reverse(answer.begin(), answer.end()); return answer; } template void expect_read(const Index& index, Revision cursor, const Changes& expected, const char* source) { const Changes actual = read(index, cursor); auto mismatch = std::mismatch(expected.begin(), expected.end(), actual.begin(), actual.end()); if (mismatch.first != expected.end() || mismatch.second != actual.end()) { auto describe = [](auto it, auto end) -> std::string { if (it == end) return ""; const auto& [rev, key, value] = *it; return "(rev=" + std::to_string(rev) + ", key=" + std::to_string(key) + ", value=" + (value ? std::to_string(*value) : "tombstone") + ")"; }; throw std::runtime_error(std::string(source) + " at version " + std::to_string(index.version()) + ", cursor " + std::to_string(cursor) + ", row " + std::to_string(mismatch.first - expected.begin()) + ": expected " + describe(mismatch.first, expected.end()) + ", found " + describe(mismatch.second, actual.end())); } } template void check_read(const Index& index, const Changes& history, Revision cursor, Key capacity) { expect_read(index, cursor, oracle(history, cursor, capacity), "full-history comparison"); } void compaction_boundaries() { // Warm every key, then alternate sweeps (recent contiguous revisions) with // repeated writes to one key (holes in both packed prefix and recent tail). // Capacity one exercises a compaction whose entire previous prefix is stale. for (Key capacity : {1u, 2u, 7u, 13u}) { afterkey::Index linked(capacity); afterkey::Journal journal(capacity); Changes history; for (Revision rev = 1; rev <= 23 * capacity; ++rev) { const auto cycle = (rev - 1) / capacity; Key key = static_cast((rev - 1) % capacity); if (cycle % 3 == 1) key = static_cast(cycle % capacity); std::optional value; if (rev % 4 == 0) { linked.erase(key); journal.erase(key); } else { value = static_cast(rev); linked.put(key, *value); journal.put(key, *value); } history.emplace_back(rev, key, value); // All cursors at every intermediate state include exact cuts on // either side of each compaction and each subsequent tail append. for (Revision cursor = 0; cursor <= rev; ++cursor) { const Changes expected = oracle(history, cursor, capacity); expect_read(linked, cursor, expected, "Index compaction workload"); expect_read(journal, cursor, expected, "Journal compaction workload"); } } } } template