Fix overflow in chunk and reconciliation ranges
This commit is contained in:
@@ -63,12 +63,13 @@ func (c *Chunker) PrepareChunk(ctx context.Context, req contracts.ChunkInput) (c
|
||||
}
|
||||
|
||||
step := c.options.MaxUnits - c.options.OverlapUnits
|
||||
ranges := make([]source.ChunkRange, 0, (len(req.Source.Units)+step-1)/step)
|
||||
count := len(req.Source.Units) / step
|
||||
if len(req.Source.Units)%step != 0 {
|
||||
count++
|
||||
}
|
||||
ranges := make([]source.ChunkRange, 0, count)
|
||||
for start := 0; start < len(req.Source.Units); start += step {
|
||||
end := start + c.options.MaxUnits
|
||||
if end > len(req.Source.Units) {
|
||||
end = len(req.Source.Units)
|
||||
}
|
||||
end := start + min(c.options.MaxUnits, len(req.Source.Units)-start)
|
||||
ranges = append(ranges, source.ChunkRange{
|
||||
StartUnitID: req.Source.Units[start].ID,
|
||||
EndUnitID: req.Source.Units[end-1].ID,
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -93,6 +94,22 @@ func TestPlanOverlap(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPlanMaximumUnitCount(t *testing.T) {
|
||||
for _, overlap := range []int{0, math.MaxInt - 1} {
|
||||
t.Run(fmt.Sprint(overlap), func(t *testing.T) {
|
||||
chunker := newChunker(t, map[string]any{"max_units": math.MaxInt, "overlap_units": overlap})
|
||||
result, err := runChunk(context.Background(), chunker, contracts.ChunkInput{Source: testSource(3)})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := []source.ChunkRange{{StartUnitID: 1, EndUnitID: 3}}
|
||||
if !reflect.DeepEqual(result.Value.Ranges, want) {
|
||||
t.Fatalf("ranges = %#v, want %#v", result.Value.Ranges, want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPlanRejectsInvalidOptions(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
|
||||
Reference in New Issue
Block a user