#include "sincekit_compact.h" #include #include #include #include #include #include #include #include #include namespace { using sincekit::CompactIndex; using Change = std::tuple; using Rows = std::vector; void require(bool value) { if (!value) throw std::runtime_error("compact journal check failed"); } template void expect(Fn&& fn) { try { fn(); } catch (const Error&) { return; } throw std::runtime_error("expected exception was not thrown"); } Rows read(const CompactIndex& index, std::uint64_t cursor) { Rows rows; const auto watermark = index.since(cursor, [&](auto id, auto rev, auto deleted) { rows.emplace_back(id, rev, deleted); }); require(watermark == index.revision()); return rows; } void boundaries() { static_assert(!std::is_copy_constructible_v); static_assert(!std::is_move_constructible_v); if constexpr (std::numeric_limits::digits > 32) { expect([] { CompactIndex oversized(std::size_t{(std::numeric_limits::max)()} + 1); }); } CompactIndex zero(0); require(zero.capacity() == 0 && read(zero, 0).empty()); expect([&] { zero.erase(0); }); expect([&] { read(zero, 1); }); CompactIndex one(1); require(one.erase(0) == 1); require(one.erase(0) == 2); require(one.touch(0) == 3); require(read(one, 0) == Rows{{0, 3, false}}); require(read(one, 3).empty()); expect([&] { one.touch(1); }); expect([&] { read(one, (std::numeric_limits::max)()); }); require(one.revision() == 3); } // Keep every event and rebuild results from scratch. The oracle has neither a // compacted generation nor a latest-revision array and can check any old cursor. Rows expected(const Rows& history, std::size_t capacity, std::size_t cursor) { std::vector seen(capacity); Rows rows; for (auto r = history.size(); r > cursor; --r) { const auto& event = history[r - 1]; const auto id = std::get<0>(event); if (!seen[id]) { rows.push_back(event); seen[id] = 1; } } return rows; } void generations(std::uint32_t capacity) { std::mt19937 random(67291 + capacity); CompactIndex index(capacity); Rows history; for (std::size_t step = 0; step < 30 * capacity + 40; ++step) { const auto offset = static_cast(step % capacity); std::uint32_t id = offset; switch ((step / capacity) % 4) { case 1: id = 0; break; // An entire tail of overwrites. case 2: id = capacity - 1 - offset; break; case 3: id = random() % capacity; break; } const bool deleted = (random() % 3 == 0); const auto rev = deleted ? index.erase(id) : index.touch(id); history.emplace_back(id, history.size() + 1, deleted); require(index.capacity() == capacity && rev == history.size()); if (capacity <= 3 || offset == 0 || offset == capacity - 1) { // Both sides of each compaction, including old cursors inside the // generation that was just replaced. for (std::size_t cursor = 0; cursor <= history.size(); ++cursor) { require(read(index, cursor) == expected(history, capacity, cursor)); } } else { auto cursor = static_cast(random()) % (history.size() + 1); require(read(index, cursor) == expected(history, capacity, cursor)); } } } struct Interrupted {}; void reentry() { CompactIndex a(3), b(1); a.touch(0); a.erase(1); a.touch(0); // A write during this traversal would trigger compaction. const auto before = read(a, 0); expect([&] { a.since(0, [&](auto, auto, auto) { b.touch(0); b.since(0, [&](auto, auto, auto) { require(read(a, 0) == before); expect([&] { a.erase(1); }); expect([&] { b.touch(0); }); throw Interrupted{}; }); }); }); require(a.revision() == 3 && read(a, 0) == before); require(a.erase(2) == 4 && b.touch(0) == 2); require(read(a, 0) == Rows{{2, 4, true}, {0, 3, false}, {1, 2, true}}); } } // namespace int main() { boundaries(); reentry(); for (auto capacity : {1U, 2U, 3U, 17U, 65U}) generations(capacity); std::cout << "compact journal tests passed\n"; }