File size: 4,860 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
#ifndef SINCEKIT_COMPACT_H_INCLUDED
#define SINCEKIT_COMPACT_H_INCLUDED

#include <cstddef>
#include <cstdint>
#include <limits>
#include <stdexcept>
#include <vector>

namespace sincekit {

// A bounded chronological journal for dense slots. Each write appends one ID.
// Every capacity writes, compact the preceding generations in place: O(capacity)
// worst-case write, O(1) amortized. since(c) scans O(min(revision-c, capacity))
// entries, up to two generations. Hot overwrites can cost more reads than Index.
// Fixed storage: two ID arrays, one 64-bit revision per slot and a deletion bitmap.
// Serialize writers against all accesses; concurrent read-only traversals are safe.
class CompactIndex {
public:
    explicit CompactIndex(std::size_t capacity)
        : latest_(checked_capacity(capacity)),
          prefix_(capacity), tail_(capacity),
          deleted_(capacity / 64 + (capacity % 64 != 0)) {}

    CompactIndex(const CompactIndex&) = delete;
    CompactIndex& operator=(const CompactIndex&) = delete;

    std::size_t capacity() const noexcept { return latest_.size(); }
    std::uint64_t revision() const noexcept { return revision_; }
    std::uint64_t touch(std::uint32_t id) { return publish(id, false); }
    std::uint64_t erase(std::uint32_t id) { return publish(id, true); }

    template<class Fn>
    std::uint64_t since(std::uint64_t cursor, Fn&& callback) const {
        if (cursor > revision_) throw std::out_of_range("sincekit: cursor exceeds current revision");
        if (cursor == revision_) return revision_;
        ReadScope reading(this);
        auto stamp = revision_;
        for (auto i = tail_size_; i > 0 && stamp > cursor; --i, --stamp) {
            const auto id = tail_[i - 1];
            if (latest_[id] == stamp) emit(callback, id, stamp);
        }
        if (cursor < prefix_revision_) {
            for (auto i = prefix_size_; i > 0; --i) {
                const auto id = prefix_[i - 1];
                const auto last = latest_[id];
                if (last > prefix_revision_) continue;  // Superseded in the tail.
                if (last <= cursor) break;
                emit(callback, id, last);
            }
        }
        return revision_;
    }

private:
    struct ReadScope {
        inline static thread_local const ReadScope* current = nullptr;
        const CompactIndex* index;
        const ReadScope* previous;

        explicit ReadScope(const CompactIndex* owner) noexcept
            : index(owner), previous(current) { current = this; }
        ~ReadScope() { current = previous; }
    };

    static std::size_t checked_capacity(std::size_t capacity) {
        if (capacity > (std::numeric_limits<std::uint32_t>::max)()) {
            throw std::length_error("sincekit: capacity exceeds uint32 slot limit");
        }
        return capacity;
    }

    template<class Fn>
    void emit(Fn& callback, std::uint32_t id, std::uint64_t stamp) const {
        callback(std::uint32_t{id}, std::uint64_t{stamp},
                 (deleted_[id / 64] & (std::uint64_t{1} << (id % 64))) != 0);
    }

    void compact() noexcept {
        std::size_t out = 0;
        for (std::size_t i = 0; i < prefix_size_; ++i) {
            const auto id = prefix_[i];
            if (latest_[id] <= prefix_revision_) prefix_[out++] = id;
        }
        auto stamp = prefix_revision_;
        for (std::size_t i = 0; i < tail_size_; ++i) {
            const auto id = tail_[i];
            if (latest_[id] == ++stamp) prefix_[out++] = id;
        }
        // Each surviving ID occurs once, so the destination always fits.
        prefix_size_ = out;
        prefix_revision_ = revision_;
        tail_size_ = 0;
    }

    std::uint64_t publish(std::uint32_t id, bool deleted) {
        if (id >= capacity()) throw std::out_of_range("sincekit: slot outside capacity");
        for (auto* scope = ReadScope::current; scope; scope = scope->previous) {
            if (scope->index == this) throw std::logic_error("sincekit: write during traversal");
        }
        if (revision_ == (std::numeric_limits<std::uint64_t>::max)()) {
            throw std::overflow_error("sincekit: revision exhausted");
        }
        if (tail_size_ == capacity()) compact();
        tail_[tail_size_++] = id;
        auto& bits = deleted_[id / 64];
        const auto mask = std::uint64_t{1} << (id % 64);
        if (deleted) bits |= mask;
        else bits &= ~mask;
        latest_[id] = ++revision_;
        return revision_;
    }

    std::vector<std::uint64_t> latest_;
    std::vector<std::uint32_t> prefix_;
    std::vector<std::uint32_t> tail_;
    std::vector<std::uint64_t> deleted_;
    std::size_t prefix_size_ = 0;
    std::size_t tail_size_ = 0;
    std::uint64_t revision_ = 0;
    std::uint64_t prefix_revision_ = 0;
};

}  // namespace sincekit

#endif  // SINCEKIT_COMPACT_H_INCLUDED