Skip to content

Commit 64ea2b0

Browse files
authored
fix MergeRanges [#225] (#226)
* correctly handle negative distance to next range
1 parent 9034408 commit 64ea2b0

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

pmtiles/extract.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -145,37 +145,37 @@ type overfetchListItem struct {
145145
}
146146

147147
// MergeRanges takes a slice of SrcDstRanges, that:
148-
// * is non-contiguous, and is sorted by NewOffset
148+
// * is non-contiguous, and is sorted by DstOffset
149149
// * an Overfetch parameter
150150
// - overfetch = 0.2 means we can request an extra 20%
151151
// - overfetch = 1.00 means we can double our total transfer size
152152
//
153-
// Return a slice of OverfetchRanges
153+
// Return a list of OverfetchRanges
154154
//
155155
// Each OverfetchRange is one or more input ranges
156156
// input ranges are merged in order of smallest byte distance to next range
157157
// until the overfetch budget is consumed.
158-
// The slice is sorted by Length
158+
// The list is sorted by Length
159159
func MergeRanges(ranges []srcDstRange, overfetch float32) (*list.List, uint64) {
160160
totalSize := 0
161161

162162
shortest := make([]*overfetchListItem, len(ranges))
163163

164164
// create the heap items
165165
for i, rng := range ranges {
166-
var bytesToNext uint64
166+
var bytesToNext int64
167167
if i == len(ranges)-1 {
168-
bytesToNext = math.MaxUint64
168+
bytesToNext = math.MaxInt64
169169
} else {
170-
bytesToNext = ranges[i+1].SrcOffset - (rng.SrcOffset + rng.Length)
170+
bytesToNext = int64(ranges[i+1].SrcOffset) - (int64(rng.SrcOffset) + int64(rng.Length))
171171
if bytesToNext < 0 {
172-
bytesToNext = math.MaxUint64
172+
bytesToNext = math.MaxInt64
173173
}
174174
}
175175

176176
shortest[i] = &overfetchListItem{
177177
Rng: rng,
178-
BytesToNext: bytesToNext,
178+
BytesToNext: uint64(bytesToNext),
179179
CopyDiscards: []copyDiscard{{uint64(rng.Length), 0}},
180180
}
181181
totalSize += int(rng.Length)

pmtiles/extract_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,3 +164,12 @@ func TestMergeRangesMultiple(t *testing.T) {
164164
assert.Equal(t, srcDstRange{0, 0, 90}, front.Rng)
165165
assert.Equal(t, 3, len(front.CopyDiscards))
166166
}
167+
168+
func TestMergeRangesNonSrcOrdered(t *testing.T) {
169+
ranges := make([]srcDstRange, 0)
170+
ranges = append(ranges, srcDstRange{20, 0, 50})
171+
ranges = append(ranges, srcDstRange{0, 60, 50})
172+
173+
result, _ := MergeRanges(ranges, 0.1)
174+
assert.Equal(t, 2, result.Len())
175+
}

0 commit comments

Comments
 (0)