| 1 |
/* |
| 2 |
Copyright The containerd Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
/* |
| 18 |
Copyright 2019 The Go Authors. All rights reserved. |
| 19 |
Use of this source code is governed by a BSD-style |
| 20 |
license that can be found in the NOTICE.md file. |
| 21 |
*/ |
| 22 |
|
| 23 |
package remote |
| 24 |
|
| 25 |
// region is HTTP-range-request-compliant range. |
| 26 |
// "b" is beginning byte of the range and "e" is the end. |
| 27 |
// "e" is must be inclusive along with HTTP's range expression. |
| 28 |
type region struct{ b, e int64 } |
| 29 |
|
| 30 |
func (c region) size() int64 { |
| 31 |
return c.e - c.b + 1 |
| 32 |
} |
| 33 |
|
| 34 |
func superRegion(regs []region) region { |
| 35 |
s := regs[0] |
| 36 |
for _, reg := range regs { |
| 37 |
if reg.b < s.b { |
| 38 |
s.b = reg.b |
| 39 |
} |
| 40 |
if reg.e > s.e { |
| 41 |
s.e = reg.e |
| 42 |
} |
| 43 |
} |
| 44 |
return s |
| 45 |
} |
| 46 |
|
| 47 |
// regionSet is a set of regions |
| 48 |
type regionSet struct { |
| 49 |
rs []region // must be kept sorted |
| 50 |
} |
| 51 |
|
| 52 |
// add attempts to merge r to rs.rs with squashing the regions as |
| 53 |
// small as possible. This operation takes O(n). |
| 54 |
// TODO: more efficient way to do it. |
| 55 |
func (rs *regionSet) add(r region) { |
| 56 |
// Iterate over the sorted region slice from the tail. |
| 57 |
// a) When an overwrap occurs, adjust `r` to fully contain the looking region |
| 58 |
// `l` and remove `l` from region slice. |
| 59 |
// b) Once l.e become less than r.b, no overwrap will occur again. So immediately |
| 60 |
// insert `r` which fully contains all overwrapped regions, to the region slice. |
| 61 |
// Here, `r` is inserted to the region slice with keeping it sorted, without |
| 62 |
// overwrapping to any regions. |
| 63 |
// *) If any `l` contains `r`, we don't need to do anything so return immediately. |
| 64 |
for i := len(rs.rs) - 1; i >= 0; i-- { |
| 65 |
l := &rs.rs[i] |
| 66 |
|
| 67 |
// *) l contains r |
| 68 |
if l.b <= r.b && r.e <= l.e { |
| 69 |
return |
| 70 |
} |
| 71 |
|
| 72 |
// a) r overwraps to l so adjust r to fully contain l and reomve l |
| 73 |
// from region slice. |
| 74 |
if l.b <= r.b && r.b <= l.e+1 && l.e <= r.e { |
| 75 |
r.b = l.b |
| 76 |
rs.rs = append(rs.rs[:i], rs.rs[i+1:]...) |
| 77 |
continue |
| 78 |
} |
| 79 |
if r.b <= l.b && l.b <= r.e+1 && r.e <= l.e { |
| 80 |
r.e = l.e |
| 81 |
rs.rs = append(rs.rs[:i], rs.rs[i+1:]...) |
| 82 |
continue |
| 83 |
} |
| 84 |
if r.b <= l.b && l.e <= r.e { |
| 85 |
rs.rs = append(rs.rs[:i], rs.rs[i+1:]...) |
| 86 |
continue |
| 87 |
} |
| 88 |
|
| 89 |
// b) No overwrap will occur after this iteration. Instert r to the |
| 90 |
// region slice immediately. |
| 91 |
if l.e < r.b { |
| 92 |
rs.rs = append(rs.rs[:i+1], append([]region{r}, rs.rs[i+1:]...)...) |
| 93 |
return |
| 94 |
} |
| 95 |
|
| 96 |
// No overwrap occurs yet. See the next region. |
| 97 |
} |
| 98 |
|
| 99 |
// r is the topmost region among regions in the slice. |
| 100 |
rs.rs = append([]region{r}, rs.rs...) |
| 101 |
} |
| 102 |
|
| 103 |
func (rs *regionSet) totalSize() int64 { |
| 104 |
var sz int64 |
| 105 |
for _, f := range rs.rs { |
| 106 |
sz += f.size() |
| 107 |
} |
| 108 |
return sz |
| 109 |
} |