Index source positions during chunk plan validation
This commit is contained in:
@@ -117,51 +117,63 @@ func CanonicalizeChunkPlan(plan ChunkPlan) (ChunkPlan, error) {
|
||||
// ValidateChunkPlan validates a canonical plan against the current source.
|
||||
// Ranges may contain gaps or overlap, but their start positions must increase.
|
||||
func ValidateChunkPlan(doc *SourceDocument, plan ChunkPlan) error {
|
||||
if err := ValidateDocument(doc); err != nil {
|
||||
return fmt.Errorf("source document: %w", err)
|
||||
_, err := validateChunkPlan(doc, plan)
|
||||
return err
|
||||
}
|
||||
|
||||
type chunkPositions struct{ start, end int }
|
||||
|
||||
// Retain resolved positions so materialization does not rescan the document or
|
||||
// repeat validation. Ordering is by document position, never numeric unit ID.
|
||||
func validateChunkPlan(doc *SourceDocument, plan ChunkPlan) ([]chunkPositions, error) {
|
||||
index, err := validateDocument(doc)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("source document: %w", err)
|
||||
}
|
||||
if plan.SourceDigest != doc.Digest {
|
||||
return fmt.Errorf("chunk plan source_digest %q does not match source document digest %q", plan.SourceDigest, doc.Digest)
|
||||
return nil, fmt.Errorf("chunk plan source_digest %q does not match source document digest %q", plan.SourceDigest, doc.Digest)
|
||||
}
|
||||
if len(plan.Ranges) == 0 {
|
||||
return fmt.Errorf("chunk plan ranges must not be empty")
|
||||
return nil, fmt.Errorf("chunk plan ranges must not be empty")
|
||||
}
|
||||
if err := ValidateChunkAnnotations(plan.Annotations); err != nil {
|
||||
return fmt.Errorf("chunk plan annotations: %w", err)
|
||||
return nil, fmt.Errorf("chunk plan annotations: %w", err)
|
||||
}
|
||||
positions := make([]chunkPositions, len(plan.Ranges))
|
||||
previousStart := -1
|
||||
for i, chunkRange := range plan.Ranges {
|
||||
start, ok := UnitIndex(doc, chunkRange.StartUnitID)
|
||||
start, ok := index.Position(chunkRange.StartUnitID)
|
||||
if !ok {
|
||||
return fmt.Errorf("chunk plan range[%d] start_unit_id %d was not found", i, chunkRange.StartUnitID)
|
||||
return nil, fmt.Errorf("chunk plan range[%d] start_unit_id %d was not found", i, chunkRange.StartUnitID)
|
||||
}
|
||||
end, ok := UnitIndex(doc, chunkRange.EndUnitID)
|
||||
end, ok := index.Position(chunkRange.EndUnitID)
|
||||
if !ok {
|
||||
return fmt.Errorf("chunk plan range[%d] end_unit_id %d was not found", i, chunkRange.EndUnitID)
|
||||
return nil, fmt.Errorf("chunk plan range[%d] end_unit_id %d was not found", i, chunkRange.EndUnitID)
|
||||
}
|
||||
if start > end {
|
||||
return fmt.Errorf("chunk plan range[%d] start_unit_id %d appears after end_unit_id %d", i, chunkRange.StartUnitID, chunkRange.EndUnitID)
|
||||
return nil, fmt.Errorf("chunk plan range[%d] start_unit_id %d appears after end_unit_id %d", i, chunkRange.StartUnitID, chunkRange.EndUnitID)
|
||||
}
|
||||
if start <= previousStart {
|
||||
return fmt.Errorf("chunk plan range[%d] start_unit_id %d does not appear after the previous range start", i, chunkRange.StartUnitID)
|
||||
return nil, fmt.Errorf("chunk plan range[%d] start_unit_id %d does not appear after the previous range start", i, chunkRange.StartUnitID)
|
||||
}
|
||||
if err := ValidateChunkAnnotations(chunkRange.Annotations); err != nil {
|
||||
return fmt.Errorf("chunk plan range[%d] annotations: %w", i, err)
|
||||
return nil, fmt.Errorf("chunk plan range[%d] annotations: %w", i, err)
|
||||
}
|
||||
positions[i] = chunkPositions{start: start, end: end}
|
||||
previousStart = start
|
||||
}
|
||||
return nil
|
||||
return positions, nil
|
||||
}
|
||||
|
||||
// MaterializeChunkPlan deterministically expands a validated plan into chunks.
|
||||
func MaterializeChunkPlan(doc *SourceDocument, plan ChunkPlan) ([]Chunk, error) {
|
||||
if err := ValidateChunkPlan(doc, plan); err != nil {
|
||||
positions, err := validateChunkPlan(doc, plan)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
chunks := make([]Chunk, 0, len(plan.Ranges))
|
||||
for index, chunkRange := range plan.Ranges {
|
||||
start, _ := UnitIndex(doc, chunkRange.StartUnitID)
|
||||
end, _ := UnitIndex(doc, chunkRange.EndUnitID)
|
||||
start, end := positions[index].start, positions[index].end
|
||||
units, err := cloneSourceUnits(doc.Units[start : end+1])
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("clone chunk plan range[%d] units: %w", index, err)
|
||||
|
||||
Reference in New Issue
Block a user