Spaces:
Running
Running
File size: 4,811 Bytes
ef8334d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | #include "sincekit_compact.h"
#include <algorithm>
#include <cstdint>
#include <iostream>
#include <limits>
#include <random>
#include <stdexcept>
#include <tuple>
#include <type_traits>
#include <vector>
namespace {
using sincekit::CompactIndex;
using Change = std::tuple<std::uint32_t, std::uint64_t, bool>;
using Rows = std::vector<Change>;
void require(bool value) {
if (!value) throw std::runtime_error("compact journal check failed");
}
template<class Error, class Fn>
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<CompactIndex>);
static_assert(!std::is_move_constructible_v<CompactIndex>);
if constexpr (std::numeric_limits<std::size_t>::digits > 32) {
expect<std::length_error>([] {
CompactIndex oversized(std::size_t{(std::numeric_limits<std::uint32_t>::max)()} + 1);
});
}
CompactIndex zero(0);
require(zero.capacity() == 0 && read(zero, 0).empty());
expect<std::out_of_range>([&] { zero.erase(0); });
expect<std::out_of_range>([&] { 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<std::out_of_range>([&] { one.touch(1); });
expect<std::out_of_range>([&] { read(one, (std::numeric_limits<std::uint64_t>::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<unsigned char> 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<std::uint32_t>(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<std::size_t>(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<Interrupted>([&] {
a.since(0, [&](auto, auto, auto) {
b.touch(0);
b.since(0, [&](auto, auto, auto) {
require(read(a, 0) == before);
expect<std::logic_error>([&] { a.erase(1); });
expect<std::logic_error>([&] { 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";
}
|