One user-space repair task per eligible HWE debug-diag / compile-deploy base ID: 320 admitted (C 218, C++ 97, build scripts 5), 1 skipped by the content filter. Each ships a public spec with a check-to-basis table, at least two defects with single-fix fixtures, a symptom-masking fixture and an alternative correct implementation. Claude re-checked all 320: structure, the HWE quality items and trace6 Docker calibration receipts against current digests (320/320 USABLE). Difficulty is unmeasured. Known follow-up: 212 tasks use 120 ad-hoc family names; the next batch maps them to the canonical list. The batch's 1,256-line progress log stays in its factory report and the 36-local branch batch15-hwe. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
211 lines
7.1 KiB
C++
211 lines
7.1 KiB
C++
#include "topic.hpp"
|
|
#include <algorithm>
|
|
#include <map>
|
|
#include <vector>
|
|
namespace retained {
|
|
namespace {
|
|
struct Offered {
|
|
int topic;
|
|
Policy policy;
|
|
std::uint64_t generation;
|
|
std::uint64_t count = 0;
|
|
std::uint64_t floor = 1;
|
|
};
|
|
struct Requested {
|
|
int topic;
|
|
int durability;
|
|
int capacity;
|
|
std::uint64_t dropped = 0;
|
|
std::uint64_t delivered = 0;
|
|
std::vector<Sample> queue;
|
|
};
|
|
bool id_ok(int id) { return id >= 1 && id <= 64; }
|
|
bool topic_ok(int topic) { return topic >= 1 && topic <= 16; }
|
|
bool cap_ok(int size) { return size >= 1 && size <= 16; }
|
|
void retain_suffix(Requested &reader)
|
|
{
|
|
std::size_t extra = reader.queue.size() > static_cast<std::size_t>(reader.capacity)
|
|
? reader.queue.size() - reader.capacity : 0;
|
|
reader.dropped += extra;
|
|
reader.queue.erase(reader.queue.begin(), reader.queue.begin() + extra);
|
|
}
|
|
void cutoff(Offered &writer)
|
|
{
|
|
if (writer.count >= static_cast<std::uint64_t>(writer.policy.depth)) {
|
|
writer.floor = std::max(writer.floor, writer.count - writer.policy.depth + 1);
|
|
}
|
|
}
|
|
}
|
|
struct Broker::State {
|
|
std::map<int, Offered> writers;
|
|
std::map<int, Requested> readers;
|
|
std::vector<Sample> journal;
|
|
std::uint64_t generations = 0;
|
|
std::uint64_t orders = 0;
|
|
std::vector<Sample> history(int id) const {
|
|
std::vector<Sample> result;
|
|
const Offered &writer = writers.at(id);
|
|
if (writer.policy.durability == 1) {
|
|
for (const Sample &sample : journal) {
|
|
if (sample.writer == id && sample.generation == writer.generation
|
|
&& sample.sequence >= writer.floor) result.push_back(sample);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
};
|
|
Broker::Broker() : state_(new State) {}
|
|
Broker::~Broker() = default;
|
|
int Broker::open_writer(int id, int topic, const Policy &policy)
|
|
{
|
|
if (!id_ok(id) || !topic_ok(topic) || !valid_policy(policy)) return invalid;
|
|
if (state_->writers.count(id)) return exists;
|
|
++state_->generations;
|
|
state_->writers.emplace(id, Offered{topic, policy, state_->generations, 0, 1});
|
|
return 0;
|
|
}
|
|
int Broker::open_profile(int id, int topic, const std::string &profile)
|
|
{
|
|
Policy policy;
|
|
int rc = profile_policy(profile, policy);
|
|
return rc == 0 ? open_writer(id, topic, policy) : rc;
|
|
}
|
|
int Broker::close_writer(int id)
|
|
{
|
|
if (!state_->writers.erase(id)) return missing;
|
|
return 0;
|
|
}
|
|
int Broker::publish(int id, std::int32_t value, Sample &out)
|
|
{
|
|
auto at = state_->writers.find(id);
|
|
if (at == state_->writers.end()) return missing;
|
|
Offered &writer = at->second;
|
|
Sample sample{id, writer.generation, ++writer.count, ++state_->orders, value, 0};
|
|
cutoff(writer);
|
|
if (writer.policy.durability == 1) state_->journal.push_back(sample);
|
|
for (auto &entry : state_->readers) {
|
|
Requested &reader = entry.second;
|
|
if (reader.topic == writer.topic && (reader.durability == 0 || writer.policy.durability == 1)) {
|
|
reader.queue.push_back(sample);
|
|
retain_suffix(reader);
|
|
}
|
|
}
|
|
out = sample;
|
|
return 0;
|
|
}
|
|
int Broker::open_reader(int id, int topic, int durability, int capacity)
|
|
{
|
|
if (!id_ok(id) || !topic_ok(topic) || (durability != 0 && durability != 1) || !cap_ok(capacity)) return invalid;
|
|
if (state_->readers.count(id)) return exists;
|
|
Requested reader{topic, durability, capacity, 0, 0, {}};
|
|
if (durability == 1) {
|
|
// Scan the global journal once; per-generation lower watermarks express
|
|
// irrevocably retired cache entries, even after a later depth increase.
|
|
for (const Sample &sample : state_->journal) {
|
|
auto writer = state_->writers.find(sample.writer);
|
|
if (writer == state_->writers.end()) continue;
|
|
const Offered &offered = writer->second;
|
|
if (offered.topic == topic && offered.generation == sample.generation
|
|
&& sample.sequence >= offered.floor) {
|
|
Sample historical = sample;
|
|
historical.historical = 1;
|
|
reader.queue.push_back(historical);
|
|
}
|
|
}
|
|
retain_suffix(reader);
|
|
}
|
|
state_->readers.emplace(id, std::move(reader));
|
|
return 0;
|
|
}
|
|
int Broker::close_reader(int id)
|
|
{
|
|
return state_->readers.erase(id) ? 0 : missing;
|
|
}
|
|
int Broker::resize_reader(int id, int capacity)
|
|
{
|
|
auto reader = state_->readers.find(id);
|
|
if (reader == state_->readers.end()) return missing;
|
|
if (!cap_ok(capacity)) return invalid;
|
|
reader->second.capacity = capacity;
|
|
retain_suffix(reader->second);
|
|
return 0;
|
|
}
|
|
int Broker::set_history(int id, int depth)
|
|
{
|
|
auto writer = state_->writers.find(id);
|
|
if (writer == state_->writers.end()) return missing;
|
|
if (depth < 1 || depth > 8) return invalid;
|
|
writer->second.policy.depth = depth;
|
|
cutoff(writer->second);
|
|
return 0;
|
|
}
|
|
int Broker::take(int id, int maximum, std::vector<Sample> &out)
|
|
{
|
|
auto reader = state_->readers.find(id);
|
|
if (reader == state_->readers.end()) return missing;
|
|
if (maximum < 0 || maximum > 16) return invalid;
|
|
Requested &r = reader->second;
|
|
auto stop = r.queue.begin() + std::min(r.queue.size(), static_cast<std::size_t>(maximum));
|
|
std::vector<Sample> next(r.queue.begin(), stop);
|
|
r.queue.erase(r.queue.begin(), stop);
|
|
r.delivered += next.size();
|
|
out.swap(next);
|
|
return 0;
|
|
}
|
|
int Broker::peek(int id, std::vector<Sample> &out) const
|
|
{
|
|
auto reader = state_->readers.find(id);
|
|
if (reader == state_->readers.end()) return missing;
|
|
out = reader->second.queue;
|
|
return 0;
|
|
}
|
|
int Broker::writer_view(int id, WriterView &out) const
|
|
{
|
|
auto writer = state_->writers.find(id);
|
|
if (writer == state_->writers.end()) return missing;
|
|
const Offered &w = writer->second;
|
|
out = WriterView{w.topic, w.policy.durability, w.policy.depth, w.generation,
|
|
w.count, state_->history(id).size()};
|
|
return 0;
|
|
}
|
|
int Broker::reader_view(int id, ReaderView &out) const
|
|
{
|
|
auto reader = state_->readers.find(id);
|
|
if (reader == state_->readers.end()) return missing;
|
|
const Requested &r = reader->second;
|
|
out = ReaderView{r.topic, r.durability, r.capacity, r.queue.size(), r.dropped, r.delivered};
|
|
return 0;
|
|
}
|
|
int Broker::topic_view(int topic, TopicView &out) const
|
|
{
|
|
if (!topic_ok(topic)) return invalid;
|
|
TopicView next;
|
|
for (const auto &w : state_->writers) {
|
|
if (w.second.topic == topic) {
|
|
++next.writers;
|
|
next.cached += state_->history(w.first).size();
|
|
}
|
|
}
|
|
for (const auto &r : state_->readers) {
|
|
if (r.second.topic == topic) {
|
|
++next.readers;
|
|
next.pending += r.second.queue.size();
|
|
}
|
|
}
|
|
out = next;
|
|
return 0;
|
|
}
|
|
Summary Broker::summary() const
|
|
{
|
|
Summary out;
|
|
out.writers = state_->writers.size();
|
|
out.readers = state_->readers.size();
|
|
for (const auto &w : state_->writers) out.cached += state_->history(w.first).size();
|
|
for (const auto &r : state_->readers) out.pending += r.second.queue.size();
|
|
out.publications = state_->orders;
|
|
out.generations = state_->generations;
|
|
return out;
|
|
}
|
|
void Broker::clear() { state_.reset(new State); }
|
|
}
|