Index source positions during chunk plan validation

This commit is contained in:
2026-11-22 09:40:42 +00:00
parent 7ee7b997f0
commit 2429f0ae6a
5 changed files with 86 additions and 37 deletions

View File

@@ -54,54 +54,62 @@ func (i DocumentIndex) ValidateRef(ref SourceRef) error {
}
func ValidateDocument(doc *SourceDocument) error {
_, err := validateDocument(doc)
return err
}
// validateDocument builds the position index while checking unit identities,
// then uses it for all self-references and callers' bulk range operations.
func validateDocument(doc *SourceDocument) (DocumentIndex, error) {
if doc == nil {
return fmt.Errorf("source document must not be nil")
return DocumentIndex{}, fmt.Errorf("source document must not be nil")
}
if isBlank(doc.ID) {
return fmt.Errorf("source document id must not be empty")
return DocumentIndex{}, fmt.Errorf("source document id must not be empty")
}
if hasSurroundingWhitespace(doc.ID) {
return fmt.Errorf("source document id %q must not contain leading or trailing whitespace", doc.ID)
return DocumentIndex{}, fmt.Errorf("source document id %q must not contain leading or trailing whitespace", doc.ID)
}
if isBlank(doc.Kind) {
return fmt.Errorf("source document kind must not be empty")
return DocumentIndex{}, fmt.Errorf("source document kind must not be empty")
}
if isBlank(doc.Format) {
return fmt.Errorf("source document format must not be empty")
return DocumentIndex{}, fmt.Errorf("source document format must not be empty")
}
if isBlank(doc.Digest) {
return fmt.Errorf("source document digest must not be empty")
return DocumentIndex{}, fmt.Errorf("source document digest must not be empty")
}
if len(doc.Units) == 0 {
return fmt.Errorf("source document units must not be empty")
return DocumentIndex{}, fmt.Errorf("source document units must not be empty")
}
seenUnitIDs := make(map[int]struct{}, len(doc.Units))
positions := make(map[int]int, len(doc.Units))
for i, unit := range doc.Units {
if unit.ID <= 0 {
return fmt.Errorf("source unit[%d].id must be positive", i)
return DocumentIndex{}, fmt.Errorf("source unit[%d].id must be positive", i)
}
if isBlank(unit.Kind) {
return fmt.Errorf("source unit[%d].kind must not be empty", i)
return DocumentIndex{}, fmt.Errorf("source unit[%d].kind must not be empty", i)
}
if isBlank(unit.Text) {
return fmt.Errorf("source unit[%d].text must not be empty", i)
return DocumentIndex{}, fmt.Errorf("source unit[%d].text must not be empty", i)
}
if _, ok := seenUnitIDs[unit.ID]; ok {
return fmt.Errorf("source unit id %d is duplicated", unit.ID)
if _, ok := positions[unit.ID]; ok {
return DocumentIndex{}, fmt.Errorf("source unit id %d is duplicated", unit.ID)
}
seenUnitIDs[unit.ID] = struct{}{}
positions[unit.ID] = i
}
index := DocumentIndex{documentID: doc.ID, positions: positions, hasDocument: true}
for i, unit := range doc.Units {
if err := ValidateRef(doc, unit.Ref); err != nil {
return fmt.Errorf("source unit[%d].ref: %w", i, err)
if err := index.ValidateRef(unit.Ref); err != nil {
return DocumentIndex{}, fmt.Errorf("source unit[%d].ref: %w", i, err)
}
if unit.Ref.StartUnitID != unit.ID || unit.Ref.EndUnitID != unit.ID {
return fmt.Errorf("source unit[%d].ref must identify source unit id %d", i, unit.ID)
return DocumentIndex{}, fmt.Errorf("source unit[%d].ref must identify source unit id %d", i, unit.ID)
}
}
return nil
return index, nil
}
func ValidateRef(doc *SourceDocument, ref SourceRef) error {