@@ -98,14 +98,72 @@ struct Save {
9898 }
9999};
100100
101- template <bool Owning> struct Load {
101+ class FoldSource final : public detail::ScratchByteSource {
102+ const detail::ScratchByteSource& source_;
103+ public:
104+ mutable Status failure = Status::ok;
105+ mutable std::optional<detail::ReportScratchIoError> carried;
106+ explicit FoldSource (const detail::ScratchByteSource& source) : source_(source) {}
107+ std::uint64_t size () const noexcept override { return source_.size (); }
108+ Status read (std::uint64_t offset, detail::ScratchBuffer out) const noexcept override {
109+ const auto status = source_.read (offset,out);
110+ if (status != Status::ok && failure == Status::ok) {
111+ failure = status;
112+ if (status == Status::io_error)
113+ if (const auto * error = source_.carried_failure ()) carried = *error;
114+ }
115+ return status;
116+ }
117+ };
118+ struct CanonicalFold {
119+ std::uint64_t digest;
120+ std::uint64_t after_sequence;
121+ bool active = true ;
122+ bool validation_skipped = false ;
123+ void bytes (const char * data, std::size_t size) noexcept {
124+ for (std::size_t i = 0 ; i != size; ++i)
125+ digest = (digest ^ static_cast <unsigned char >(data[i])) * 1099511628211ULL ;
126+ }
127+ void word (std::uint64_t value) noexcept {
128+ for (unsigned i = 0 ; i != 8 ; ++i)
129+ digest = (digest ^ ((value >> (8 * i)) & 255 )) * 1099511628211ULL ;
130+ }
131+ template <class T > void scalar (const T& value) noexcept {
132+ if constexpr (std::is_same_v<T, double >) {
133+ std::uint64_t bits = 0 ;
134+ std::memcpy (&bits, &value, sizeof bits);
135+ if ((bits & 0x7fffffffffffffffULL ) == 0 ) bits = 0 ;
136+ else if ((bits & 0x7ff0000000000000ULL ) == 0x7ff0000000000000ULL
137+ && (bits & 0x000fffffffffffffULL )) bits = 0x7ff8000000000000ULL ;
138+ word (bits);
139+ } else word (static_cast <std::uint64_t >(value));
140+ }
141+ };
142+ constexpr std::size_t kJournalFoldWorkspaceLimit = 1024 ;
143+ constexpr std::size_t kJournalFoldFieldBytes = 256 ;
144+ // Fixed candidate/read-failure state, two diagnostic transport slots, two
145+ // summaries and eight words for scalar/pointer controls, plus the existing
146+ // comparison buffer reused for text. Nothing is retained between calls.
147+ // ReportScratchIoError copies share their prepared message/argument payload.
148+ // The Reader page and non-owning schema frame remain the existing workspace.
149+ constexpr std::size_t kJournalFoldWorkspaceBytes = sizeof (CanonicalFold) + sizeof (FoldSource)
150+ + 2 * sizeof (std::optional<detail::ReportScratchIoError>) + 2 * sizeof (JournalRecordSummary)
151+ + 8 * sizeof (std::uint64_t ) + kJournalFoldFieldBytes ;
152+ static_assert (kJournalFoldWorkspaceBytes <= kJournalFoldWorkspaceLimit ,
153+ " fixed admission fold workspace" );
154+ static_assert (std::is_nothrow_copy_constructible_v<detail::ReportScratchIoError>
155+ && std::is_nothrow_copy_assignable_v<detail::ReportScratchIoError>,
156+ " diagnostic transport must be nonthrowing" );
157+
158+ template <bool Owning, bool Folding = false > struct Load {
102159 static constexpr bool loading = true ;
103160 static constexpr bool owning = Owning;
104161 jc::Reader& in;
105162 std::uint64_t rows = 0 , expected_rows = 0 ;
106163 JournalRecordSummary summary;
107164 Outcome outcome = Outcome::NoAdmission;
108165 int bar = -1 ;
166+ CanonicalFold* fold = nullptr ;
109167 bool good () const { return in.status == Status::ok; }
110168 template <class T > void field (const Path& path, const char * name, T& value) {
111169 if (!good ()) return ;
@@ -114,13 +172,39 @@ template<bool Owning> struct Load {
114172 const auto length = in.word ();
115173 if (!good ()) return ;
116174 if (length != key.length ) { in.status = Status::corrupt; return ; }
117- std::array<char , 256 > bytes{};
175+ std::array<char , kJournalFoldFieldBytes > bytes{};
118176 in.bytes (bytes.data (), key.length );
119177 if (!good ()) return ;
120178 if (std::memcmp (bytes.data (), key.bytes .data (), key.length ) || in.word () != tag<T>()) {
121179 in.status = Status::corrupt; return ;
122180 }
123- if constexpr (std::is_same_v<T, std::string>) in.text (value, Owning);
181+ if constexpr (Folding) {
182+ if (fold->active ) {
183+ fold->word (length); fold->bytes (bytes.data (), key.length ); fold->word (tag<T>());
184+ }
185+ }
186+ if constexpr (std::is_same_v<T, std::string>) {
187+ if constexpr (!Folding) in.text (value, Owning);
188+ else {
189+ const auto size = in.word ();
190+ if (!good ()) return ;
191+ if (size > in.remaining () || size > value.max_size ()) {
192+ in.status = Status::corrupt; return ;
193+ }
194+ if (!fold->active ) in.skip (size);
195+ else {
196+ fold->word (size);
197+ const auto end = in.position + size;
198+ if (end > in.cached_at + in.cached_size ) fold->validation_skipped = true ;
199+ while (in.position != end && good ()) {
200+ const auto count = static_cast <std::size_t >(
201+ std::min<std::uint64_t >(bytes.size (), end - in.position ));
202+ in.bytes (bytes.data (), count);
203+ if (good ()) fold->bytes (bytes.data (), count);
204+ }
205+ }
206+ }
207+ }
124208 else if constexpr (std::is_same_v<T, double >) in.real (value);
125209 else if constexpr (std::is_same_v<T, bool >) {
126210 const auto raw = in.word ();
@@ -130,11 +214,16 @@ template<bool Owning> struct Load {
130214 std::underlying_type_t <T> raw{}; in.integer (raw); value = static_cast <T>(raw);
131215 } else in.integer (value);
132216 if constexpr (std::is_integral_v<T> || std::is_enum_v<T>) {
133- if (key.is (" event.observation.command" ) || key.is (" event.receipt.sequence" ))
217+ if (key.is (" event.observation.command" ) || key.is (" event.receipt.sequence" )) {
134218 summary.sequence = static_cast <std::uint64_t >(value);
219+ if constexpr (Folding)
220+ if (summary.sequence <= fold->after_sequence ) fold->active = false ;
221+ }
135222 if (key.is (" event.outcome" )) outcome = static_cast <Outcome>(value);
136223 if (key.is (" event.observation.bar" )) bar = static_cast <int >(value);
137224 }
225+ if constexpr (Folding && !std::is_same_v<T, std::string>)
226+ if (good () && fold->active ) fold->scalar (value);
138227 }
139228};
140229
@@ -328,17 +417,18 @@ template<class A> struct Schema {
328417 }
329418};
330419
331- template <bool Owning> Status decode (const detail::ScratchByteSource& source,
420+ template <bool Owning, bool Folding = false > Status decode (const detail::ScratchByteSource& source,
332421 detail::ScratchBuffer page, Event& event, JournalRecordSummary& summary,
333- const detail::ReportScratchIoError*& failure) noexcept {
422+ const detail::ReportScratchIoError*& failure, CanonicalFold* fold = nullptr ) noexcept {
334423 failure = nullptr ;
335424 if (!page.data || !page.size ) return Status::programming_error;
336425 try {
337426 jc::Reader in{source,page};
338- Load<Owning> load{in};
427+ Load<Owning,Folding> load{in};
428+ load.fold = fold;
339429 load.expected_rows = in.word ();
340430 if (load.expected_rows > in.remaining () / 24 ) return Status::corrupt;
341- Schema<Load<Owning>>{load}.event (event);
431+ Schema<Load<Owning,Folding >>{load}.event (event);
342432 if (load.rows != load.expected_rows && in.status == Status::ok) in.status = Status::corrupt;
343433 const auto status = in.finish ();
344434 if (status == Status::ok) {
@@ -356,6 +446,7 @@ template<bool Owning> Status decode(const detail::ScratchByteSource& source,
356446} // namespace
357447
358448JournalRecordSource::JournalRecordSource (const Event& event) noexcept : event_(&event) {
449+ jc::observe_work (jc::WorkOperation::payload_source);
359450 try {
360451 jc::Writer counter;
361452 Save count{counter,true };
@@ -375,6 +466,7 @@ detail::ScratchStatus JournalRecordSource::read(std::uint64_t offset,
375466 if (offset > length_ || buffer.size > length_ - offset || (buffer.size && !buffer.data ))
376467 return Status::programming_error;
377468 if (!buffer.size ) return Status::ok;
469+ jc::observe_work (jc::WorkOperation::payload_read, buffer.size );
378470 try {
379471 // Forward pages resume within the current array element. A seek before
380472 // the saved field boundary (including the second pass) starts anew.
@@ -412,6 +504,38 @@ detail::ScratchStatus scan_journal_record(const detail::ScratchByteSource& sourc
412504 const detail::ReportScratchIoError* failure = nullptr ;
413505 return decode<false >(source,page,scratch,summary,failure);
414506}
507+ detail::ScratchStatus fold_validated_journal_record (const detail::ScratchByteSource& source,
508+ detail::ScratchBuffer page, JournalRecordSummary& summary, std::uint64_t & digest,
509+ std::uint64_t after_sequence, std::optional<detail::ReportScratchIoError>* failure) noexcept {
510+ if (failure) failure->reset ();
511+ Event scratch;
512+ FoldSource input (source);
513+ CanonicalFold fold{digest,after_sequence};
514+ JournalRecordSummary candidate;
515+ const detail::ReportScratchIoError* unused = nullptr ;
516+ const auto status = decode<false ,true >(input,page,scratch,candidate,unused,&fold);
517+ if (input.failure != Status::ok && fold.validation_skipped ) {
518+ // A skipped text region changes the old scan's refill boundaries.
519+ // On a read failure only, replay that scan to retain its validation
520+ // priority (including its tag-read error handling). Never retry the
521+ // hash or turn the original read failure into a successful query.
522+ JournalRecordSummary checked;
523+ const auto validation = scan_journal_record (source,page,checked);
524+ if (validation != Status::ok) {
525+ if (failure && validation == Status::io_error)
526+ if (const auto * error = source.carried_failure ()) *failure = *error;
527+ return validation;
528+ }
529+ if (failure) *failure = input.carried ;
530+ return input.failure ;
531+ }
532+ if (failure && status == Status::io_error) *failure = input.carried ;
533+ if (status == Status::ok) {
534+ summary = candidate;
535+ if (summary.sequence > after_sequence) digest = fold.digest ;
536+ }
537+ return status;
538+ }
415539detail::ScratchStatus fold_journal_record (const detail::ScratchByteSource& source,
416540 detail::ScratchBuffer page, std::uint64_t & digest) noexcept {
417541 if (!page.data || !page.size ) return Status::programming_error;
0 commit comments