in vhdcore/common/indexRange.go [61:105]
func ChunkRangesBySize(ranges []*IndexRange, chunkSizeInBytes int64) []*IndexRange {
var chunks = make([]*IndexRange, 0)
length := len(ranges)
var remaining *IndexRange
for i, current := range ranges {
if remaining != nil {
if remaining.Adjacent(current) {
requiredBytes := chunkSizeInBytes - remaining.Length()
availableBytes := current.Length()
if requiredBytes < availableBytes {
remaining.End += requiredBytes
current.Start += requiredBytes
chunks = append(chunks, remaining)
remaining = nil
} else {
remaining.End += availableBytes
current = nil
}
} else {
chunks = append(chunks, remaining)
remaining = nil
}
}
if current != nil {
chunksSet := current.PartitionBy(chunkSizeInBytes)
lastChunkIndex := len(chunksSet) - 1
lastChunk := chunksSet[lastChunkIndex]
if (lastChunk.Length() != chunkSizeInBytes) && (i+1 < length) && lastChunk.Adjacent(ranges[i+1]) {
remaining = lastChunk
chunks = append(chunks, chunksSet[:lastChunkIndex]...)
} else {
chunks = append(chunks, chunksSet...)
}
}
}
if remaining != nil {
chunks = append(chunks, remaining)
}
return chunks
}