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>
359 lines
8.6 KiB
C++
359 lines
8.6 KiB
C++
#include "reader.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <limits>
|
|
#include <utility>
|
|
|
|
namespace exception_reader {
|
|
|
|
Store::Store(std::size_t capacity) : capacity_(capacity) {}
|
|
|
|
void Store::put(const std::string &name, const std::string &bytes)
|
|
{
|
|
files_[name] = bytes;
|
|
}
|
|
|
|
void Store::append(const std::string &name, const std::string &bytes)
|
|
{
|
|
auto found = files_.find(name);
|
|
if (found == files_.end()) {
|
|
throw Missing();
|
|
}
|
|
found->second.append(bytes);
|
|
}
|
|
|
|
bool Store::contains(const std::string &name) const
|
|
{
|
|
return files_.find(name) != files_.end();
|
|
}
|
|
|
|
std::size_t Store::file_size(const std::string &name) const
|
|
{
|
|
const auto found = files_.find(name);
|
|
if (found == files_.end()) {
|
|
throw Missing();
|
|
}
|
|
return found->second.size();
|
|
}
|
|
|
|
int Store::open(const std::string &name)
|
|
{
|
|
if (files_.find(name) == files_.end()) {
|
|
throw Missing();
|
|
}
|
|
if (cursors_.size() >= capacity_) {
|
|
throw Exhausted();
|
|
}
|
|
int handle = 1;
|
|
while (cursors_.find(handle) != cursors_.end()) {
|
|
++handle;
|
|
}
|
|
cursors_.emplace(handle, Cursor{name, 0});
|
|
events_.push_back(Event{'O', handle, 0});
|
|
return handle;
|
|
}
|
|
|
|
int Store::clone(int handle)
|
|
{
|
|
const auto source = cursors_.find(handle);
|
|
if (source == cursors_.end()) {
|
|
throw BadHandle();
|
|
}
|
|
if (cursors_.size() >= capacity_) {
|
|
throw Exhausted();
|
|
}
|
|
int next = 1;
|
|
while (cursors_.find(next) != cursors_.end()) {
|
|
++next;
|
|
}
|
|
cursors_.emplace(next, source->second);
|
|
events_.push_back(Event{'O', next, 0});
|
|
return next;
|
|
}
|
|
|
|
std::string Store::read(int handle, std::size_t limit, long long fault_after)
|
|
{
|
|
const auto found = cursors_.find(handle);
|
|
if (found == cursors_.end()) {
|
|
throw BadHandle();
|
|
}
|
|
if (limit == 0) {
|
|
throw std::invalid_argument("zero read limit");
|
|
}
|
|
Cursor &cursor = found->second;
|
|
const std::string &data = files_.at(cursor.name);
|
|
if (cursor.offset == data.size()) {
|
|
events_.push_back(Event{'R', handle, 0});
|
|
return {};
|
|
}
|
|
if (fault_after >= 0 &&
|
|
cursor.offset >= static_cast<unsigned long long>(fault_after)) {
|
|
throw ReadFault();
|
|
}
|
|
std::size_t amount = std::min(limit, data.size() - cursor.offset);
|
|
if (fault_after >= 0) {
|
|
const std::size_t before_fault =
|
|
static_cast<std::size_t>(fault_after) - cursor.offset;
|
|
amount = std::min(amount, before_fault);
|
|
}
|
|
const std::string piece = data.substr(cursor.offset, amount);
|
|
cursor.offset += amount;
|
|
events_.push_back(Event{'R', handle, amount});
|
|
return piece;
|
|
}
|
|
|
|
void Store::close(int handle)
|
|
{
|
|
const auto found = cursors_.find(handle);
|
|
if (found == cursors_.end()) {
|
|
throw BadHandle();
|
|
}
|
|
cursors_.erase(found);
|
|
events_.push_back(Event{'C', handle, 0});
|
|
}
|
|
|
|
void Store::seek(int handle, std::size_t offset)
|
|
{
|
|
const auto found = cursors_.find(handle);
|
|
if (found == cursors_.end()) {
|
|
throw BadHandle();
|
|
}
|
|
const std::size_t length = files_.at(found->second.name).size();
|
|
if (offset > length) {
|
|
throw std::invalid_argument("seek past end");
|
|
}
|
|
found->second.offset = offset;
|
|
}
|
|
|
|
std::size_t Store::position(int handle) const
|
|
{
|
|
const auto found = cursors_.find(handle);
|
|
if (found == cursors_.end()) {
|
|
throw BadHandle();
|
|
}
|
|
return found->second.offset;
|
|
}
|
|
|
|
std::size_t Store::remaining(int handle) const
|
|
{
|
|
const auto found = cursors_.find(handle);
|
|
if (found == cursors_.end()) {
|
|
throw BadHandle();
|
|
}
|
|
return files_.at(found->second.name).size() - found->second.offset;
|
|
}
|
|
|
|
std::size_t Store::live() const
|
|
{
|
|
return cursors_.size();
|
|
}
|
|
|
|
std::vector<Event> Store::events() const
|
|
{
|
|
return events_;
|
|
}
|
|
|
|
std::size_t Store::event_count() const
|
|
{
|
|
return events_.size();
|
|
}
|
|
|
|
const char *status_name(Status value)
|
|
{
|
|
switch (value) {
|
|
case Status::OK: return "OK";
|
|
case Status::BAD_REQUEST: return "BAD_REQUEST";
|
|
case Status::READ_ERROR: return "READ_ERROR";
|
|
case Status::INDEX_ERROR: return "INDEX_ERROR";
|
|
case Status::MISSING: return "MISSING";
|
|
case Status::EXHAUSTED: return "EXHAUSTED";
|
|
}
|
|
return "UNKNOWN";
|
|
}
|
|
|
|
namespace {
|
|
|
|
class Lines {
|
|
public:
|
|
void append(const std::string &piece)
|
|
{
|
|
for (unsigned char byte : piece) {
|
|
++bytes_;
|
|
digest_ ^= byte;
|
|
digest_ *= 16777619u;
|
|
if (byte == '\n') {
|
|
++lines_;
|
|
longest_ = std::max(longest_, current_);
|
|
current_ = 0;
|
|
} else {
|
|
++current_;
|
|
}
|
|
}
|
|
}
|
|
|
|
void finish(Result &result) const
|
|
{
|
|
result.lines = lines_ + (current_ != 0 ? 1 : 0);
|
|
result.bytes = bytes_;
|
|
result.longest = std::max(longest_, current_);
|
|
result.digest = digest_;
|
|
}
|
|
|
|
private:
|
|
std::size_t lines_ = 0;
|
|
std::size_t bytes_ = 0;
|
|
std::size_t current_ = 0;
|
|
std::size_t longest_ = 0;
|
|
std::uint32_t digest_ = 2166136261u;
|
|
};
|
|
|
|
class IndexNumber {
|
|
public:
|
|
bool append(const std::string &piece)
|
|
{
|
|
for (unsigned char byte : piece) {
|
|
if (!accept(byte)) {
|
|
valid_ = false;
|
|
}
|
|
}
|
|
return valid_;
|
|
}
|
|
|
|
bool finish(long long &number) const
|
|
{
|
|
if (!valid_ || !have_digit_) {
|
|
return false;
|
|
}
|
|
number = static_cast<long long>(value_);
|
|
return true;
|
|
}
|
|
|
|
private:
|
|
bool accept(unsigned char byte)
|
|
{
|
|
if (!valid_) {
|
|
return false;
|
|
}
|
|
if (byte >= '0' && byte <= '9' && !seen_lf_) {
|
|
have_digit_ = true;
|
|
const unsigned digit = static_cast<unsigned>(byte - '0');
|
|
if (value_ > (1000000u - digit) / 10u) {
|
|
return false;
|
|
}
|
|
value_ = value_ * 10u + digit;
|
|
return true;
|
|
}
|
|
if (byte == '\n' && have_digit_ && !seen_lf_) {
|
|
seen_lf_ = true;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool valid_ = true;
|
|
bool have_digit_ = false;
|
|
bool seen_lf_ = false;
|
|
unsigned value_ = 0;
|
|
};
|
|
|
|
void recount(const Store &store, std::size_t start, Result &result)
|
|
{
|
|
const std::vector<Event> all = store.events();
|
|
for (std::size_t at = start; at < all.size(); ++at) {
|
|
if (all[at].kind == 'O') {
|
|
++result.opened;
|
|
} else if (all[at].kind == 'C') {
|
|
++result.closed;
|
|
}
|
|
}
|
|
result.live = store.live();
|
|
}
|
|
|
|
}
|
|
|
|
Reader::Reader(Store &store) : store_(store) {}
|
|
|
|
Result Reader::run(const Request &request)
|
|
{
|
|
Result result;
|
|
const std::size_t start = store_.event_count();
|
|
if (request.chunk == 0 || request.chunk > 64 ||
|
|
request.fault_after < -1) {
|
|
result.status = Status::BAD_REQUEST;
|
|
recount(store_, start, result);
|
|
return result;
|
|
}
|
|
|
|
int primary = 0;
|
|
try {
|
|
primary = store_.open(request.primary_path);
|
|
} catch (const Missing &) {
|
|
result.status = Status::MISSING;
|
|
} catch (const Exhausted &) {
|
|
result.status = Status::EXHAUSTED;
|
|
}
|
|
if (primary == 0) {
|
|
recount(store_, start, result);
|
|
return result;
|
|
}
|
|
|
|
Lines lines;
|
|
try {
|
|
for (;;) {
|
|
const std::string piece =
|
|
store_.read(primary, request.chunk, request.fault_after);
|
|
if (piece.empty()) {
|
|
break;
|
|
}
|
|
lines.append(piece);
|
|
}
|
|
} catch (const ReadFault &) {
|
|
result.status = Status::READ_ERROR;
|
|
lines.finish(result);
|
|
store_.close(primary);
|
|
recount(store_, start, result);
|
|
return result;
|
|
}
|
|
lines.finish(result);
|
|
store_.close(primary);
|
|
|
|
if (!request.with_index) {
|
|
recount(store_, start, result);
|
|
return result;
|
|
}
|
|
|
|
int index = 0;
|
|
try {
|
|
index = store_.open(request.index_path);
|
|
} catch (const Missing &) {
|
|
result.status = Status::MISSING;
|
|
} catch (const Exhausted &) {
|
|
result.status = Status::EXHAUSTED;
|
|
}
|
|
if (index == 0) {
|
|
recount(store_, start, result);
|
|
return result;
|
|
}
|
|
|
|
IndexNumber parser;
|
|
bool syntax_ok = true;
|
|
for (;;) {
|
|
const std::string piece = store_.read(index, request.chunk, -1);
|
|
if (piece.empty()) {
|
|
break;
|
|
}
|
|
syntax_ok = parser.append(piece) && syntax_ok;
|
|
}
|
|
if (!syntax_ok || !parser.finish(result.index_value)) {
|
|
result.status = Status::INDEX_ERROR;
|
|
result.index_value = -1;
|
|
recount(store_, start, result);
|
|
return result;
|
|
}
|
|
store_.close(index);
|
|
recount(store_, start, result);
|
|
return result;
|
|
}
|
|
|
|
}
|