Improve D&D validation reliability

This commit is contained in:
2026-08-29 01:24:45 +00:00
parent 4da9360d74
commit 917d150279
55 changed files with 1300 additions and 565 deletions

View File

@@ -0,0 +1,48 @@
package shared
import "gitea.maximumdirect.net/eric/notarius/internal/core/source"
// ChunkCoverage is an immutable snapshot of the source units materialized in
// one extraction chunk.
type ChunkCoverage struct {
sourceID string
unitIDs map[int]struct{}
}
// NewChunkCoverage snapshots chunk without retaining or mutating it.
func NewChunkCoverage(chunk *source.Chunk) ChunkCoverage {
if chunk == nil {
return ChunkCoverage{}
}
coverage := ChunkCoverage{
sourceID: chunk.SourceID,
unitIDs: make(map[int]struct{}, len(chunk.Units)),
}
for _, unit := range chunk.Units {
coverage.unitIDs[unit.ID] = struct{}{}
}
return coverage
}
// Contains reports whether ref identifies a valid inclusive span in index and
// every unit in that document-ordered span is present in the chunk snapshot.
func (c ChunkCoverage) Contains(index source.DocumentIndex, doc *source.SourceDocument, ref source.SourceRef) bool {
documentID, ok := index.DocumentID()
if !ok || doc == nil || doc.ID != documentID || c.sourceID == "" || ref.SourceID != c.sourceID || ref.SourceID != documentID {
return false
}
start, startOK := index.Position(ref.StartUnitID)
end, endOK := index.Position(ref.EndUnitID)
if !startOK || !endOK || start > end {
return false
}
for position := start; position <= end; position++ {
if position >= len(doc.Units) {
return false
}
if _, found := c.unitIDs[doc.Units[position].ID]; !found {
return false
}
}
return true
}