Improve D&D validation reliability
This commit is contained in:
48
internal/modules/dnd/shared/source_ref_coverage.go
Normal file
48
internal/modules/dnd/shared/source_ref_coverage.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user