/
multireader.go
167 lines (145 loc) · 3.82 KB
/
multireader.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
// Copyright 2017 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package blobstore // import "gopkg.in/juju/charmstore.v5/internal/blobstore"
import (
"io"
"sort"
"gopkg.in/errgo.v1"
"gopkg.in/juju/charmstore.v5/internal/mongodoc"
)
// TODO use equivalent constants in io package when we can use more
// modern Go version.
const (
seekStart = 0 // seek relative to the origin of the file
seekCurrent = 1 // seek relative to the current offset
seekEnd = 2 // seek relative to the end
)
type multiReader struct {
// store holds the underlying store.
store *Store
// endPos holds the absolute position of the end
// of each part.
endPos []int64
// hashes holds the hash of each part.
hashes []string
// size holds the size the entire multipart blob.
size int64
// r holds the currently open blob.
r ReadSeekCloser
// rindex holds the index of the currently open blob.
rindex int
// rpos holds the current position that the reader
// is at.
rpos int64
// pos holds the current seek position. This may be
// different from rpos when Seek has been called.
pos int64
}
func newMultiReader(store *Store, idx *mongodoc.MultipartIndex) (ReadSeekCloser, int64, error) {
if len(idx.Sizes) != len(idx.Hashes) {
return nil, 0, errgo.Newf("index size/length mismatch (database corruption?)")
}
switch len(idx.Sizes) {
case 0:
return emptyBlob{}, 0, nil
case 1:
// No point in going through the multireader logic if there's
// only one part.
return store.Open(idx.Hashes[0], nil)
}
endPos := make([]int64, len(idx.Sizes))
p := int64(0)
for i, size := range idx.Sizes {
p += int64(size)
endPos[i] = p
}
return &multiReader{
store: store,
endPos: endPos,
hashes: idx.Hashes,
size: p,
}, p, nil
}
func (r *multiReader) Close() error {
if r.r != nil {
return r.r.Close()
}
return nil
}
// Read implements io.Reader.Read.
func (r *multiReader) Read(buf []byte) (int, error) {
if r.pos >= r.size {
return 0, io.EOF
}
if r.r == nil || r.pos >= r.endPos[r.rindex] || r.pos < r.startPos(r.rindex) {
if r.r != nil {
r.r.Close()
r.r = nil
}
// Binary search for the index of the earliest position
// that ends after the current part. Note that because
// we know that 0 <= r.pos < r.size and the last element
// in r.endPos==r.size, we must end up with 0 <=
// r.rindex < len(r.endPos).
r.rindex = sort.Search(len(r.endPos), func(i int) bool {
return r.endPos[i] > r.pos
})
nr, _, err := r.store.Open(r.hashes[r.rindex], nil)
if err != nil {
return 0, errgo.Notef(err, "cannot open blob part")
}
r.r = nr
r.rpos = r.startPos(r.rindex)
}
if r.pos != r.rpos {
_, err := r.r.Seek(r.pos-r.startPos(r.rindex), seekStart)
if err != nil {
return 0, errgo.Notef(err, "cannot seek into blob")
}
r.rpos = r.pos
}
n, err := r.r.Read(buf)
r.pos += int64(n)
r.rpos = r.pos
// As per https://golang.org/pkg/io/#Reader a reader may return
// io.EOF with the final data bytes in that case we should not
// produce an error.
if err != nil && err != io.EOF {
return n, errgo.Notef(err, "error reading blob %q", r.hashes[r.rindex])
}
return n, nil
}
// startPos returns the start position of given part.
func (r *multiReader) startPos(part int) int64 {
if part == 0 {
return 0
}
return r.endPos[part-1]
}
// Seek implements io.Seeker.Seek.
func (r *multiReader) Seek(pos int64, whence int) (int64, error) {
switch whence {
case seekStart:
case seekEnd:
pos = r.size - pos
case seekCurrent:
pos = r.pos + pos
default:
return 0, errgo.Newf("unknown seek whence value")
}
if pos < 0 {
pos = 0
}
r.pos = pos
return pos, nil
}
type emptyBlob struct{}
func (emptyBlob) Read([]byte) (int, error) {
return 0, io.EOF
}
func (emptyBlob) Seek(pos int64, whence int) (int64, error) {
return 0, nil
}
func (emptyBlob) Close() error {
return nil
}