This repository has been archived by the owner on Jan 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
chunkaligned.go
186 lines (158 loc) · 4.08 KB
/
chunkaligned.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Most of the contribution goes to Brad Fitzpatrick.
// See http://talks.golang.org/2013/oscon-dl.slide
package chunkaligned
import (
"errors"
"io"
"math"
"sort"
"sync"
)
// An io.SectionReader implements SizeReaderAt.
type SizeReaderAt interface {
Size() int64
io.ReaderAt
}
type offsetAndSource struct {
off int64
SizeReaderAt
}
type multi struct {
parts []offsetAndSource
size int64
}
func (m *multi) Size() int64 { return m.size }
func (m *multi) ReadAt(p []byte, off int64) (n int, err error) {
if off < 0 || off >= m.size {
return 0, io.EOF
}
wantN := len(p)
// Skip past the requested offset.
skipParts := sort.Search(len(m.parts), func(i int) bool {
// This function returns whether parts[i] will
// contribute any bytes to our output.
part := m.parts[i]
return part.off+part.Size() > off
})
parts := m.parts[skipParts:]
// How far to skip in the first part.
needSkip := off
if len(parts) > 0 {
needSkip -= parts[0].off
}
for len(parts) > 0 && len(p) > 0 {
readP := p
partSize := parts[0].Size()
if int64(len(readP)) > partSize-needSkip {
readP = readP[:partSize-needSkip]
}
pn, err0 := parts[0].ReadAt(readP, needSkip)
if err0 != nil {
return n, err0
}
n += pn
p = p[pn:]
if int64(pn)+needSkip == partSize {
parts = parts[1:]
}
needSkip = 0
}
if n != wantN {
err = io.ErrUnexpectedEOF
}
return
}
const (
chunkSizeLimit = 4 * 1024 * 1024
)
// fixed-length []byte pool, they will not grow as needed
var fixedBytePool = sync.Pool{
New: func() interface{} { return make([]byte, chunkSizeLimit) },
}
// NOTE(wenjianhn): Clients of chunkReadAt cannot execute parallel
// ReadAt calls on the same chunk, beacause there is no lock to guard the cache.
type chunkReaderAt struct {
size int
base int64
cache []byte
r SizeReaderAt
}
func (c *chunkReaderAt) Size() int64 {
return int64(c.size)
}
func (c *chunkReaderAt) ReadAt(p []byte, off int64) (n int, err error) {
wantN := len(p)
if len(c.cache) == 0 {
c.cache = fixedBytePool.Get().([]byte)
// the offset is aligned
readN, err := c.r.ReadAt(c.cache[:c.size], c.base)
if err != nil {
if err == io.EOF {
if readN < wantN {
fixedBytePool.Put(c.cache)
c.cache = nil
// We always know when EOF is coming.
// If the caller asked for a chunk, there should be a chunk.
return 0, io.ErrUnexpectedEOF
}
} else {
fixedBytePool.Put(c.cache)
c.cache = nil
return 0, err
}
}
}
needSkip := int(off - c.base)
n = copy(p, c.cache[needSkip:c.size])
if (needSkip + n) == c.size {
fixedBytePool.Put(c.cache)
c.cache = nil
}
if n != wantN {
err = io.ErrUnexpectedEOF
}
return
}
// NewChunkAlignedReaderAt returns a ReaderAt wrapper that is backed
// by a ReaderAt r of size totalSize where the wrapper guarantees that
// all ReadAt calls are aligned to chunkSize boundaries and of size
// chunkSize (except for the final chunk, which may be shorter).
//
// A chunk-aligned reader is good for caching, letting upper layers have
// any access pattern, but guarantees that the wrapped ReaderAt sees
// only nicely-cacheable access patterns & sizes.
func NewChunkAlignedReaderAt(r SizeReaderAt, chunkSize int) (SizeReaderAt, error) {
if chunkSize > chunkSizeLimit {
// NOTE(wenjianhn): Do you really need a chunk that is such large?
return &multi{}, errors.New("chunkaligned: chunk size limit exceeded")
}
totalSize := r.Size()
partN := int(math.Ceil(float64(totalSize) / float64(chunkSize)))
m := &multi{
parts: make([]offsetAndSource, partN),
size: totalSize,
}
left := m.Size()
var offset int64
for i, _ := range m.parts {
partSize := chunkSize
if left < int64(chunkSize) {
// the final chunk is shorter
partSize = int(left)
}
f := &chunkReaderAt{
size: partSize,
base: offset,
cache: nil,
r: r,
}
m.parts[i] = offsetAndSource{offset,
io.NewSectionReader(f, offset, f.Size())}
offset += f.Size()
left -= f.Size()
}
return m, nil
}