-
Notifications
You must be signed in to change notification settings - Fork 3
/
store.go
209 lines (178 loc) · 5.24 KB
/
store.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package httpreaderat
import (
"bytes"
"github.com/pkg/errors"
"io"
"io/ioutil"
"os"
)
// Store is the interface to a temporary byte storage. Calling ReadFrom
// with io.Reader reads data to a temporary storage and allows it to be
// read back with ReadAt. A Store must be Closed to free up the space when
// it is no longer needed. A Store can be reused by filling it with new
// data. ReadFrom is not safe to be called concurrently. ReadAt is safe
// for concurrent use.
type Store interface {
io.ReaderFrom
io.ReaderAt
io.Closer
}
// NewDefaultStore creates a Store with default settings. It buffers up to
// 1 MB in memory and if that is exceeded, up to 1 GB to a temporary file.
// Returned Store must be Closed if it is no longer needed.
func NewDefaultStore() Store {
return NewLimitedStore(
NewStoreMemory(), 1024*1024, NewLimitedStore(
NewStoreFile(), 1024*1024*1024, nil))
}
// StoreFile takes data from io.Reader and provides io.ReaderAt backed by
// a temporary file. It implements the Store interface.
type StoreFile struct {
tmpfile *os.File
size int64
}
var _ Store = (*StoreFile)(nil)
func NewStoreFile() *StoreFile {
return &StoreFile{}
}
// Read and store the contents of r to a temporary file. Previous contents
// (if any) are erased. Can not be called concurrently.
func (s *StoreFile) ReadFrom(r io.Reader) (n int64, err error) {
if s.tmpfile != nil {
s.Close()
}
s.tmpfile, err = ioutil.TempFile("", "gotmp")
if err != nil {
return 0, err
}
n, err = io.Copy(s.tmpfile, r)
s.size = n
return n, err
}
// ReadAt reads len(b) bytes from the Store starting at byte offset off. It
// returns the number of bytes read and the error, if any. ReadAt always
// returns a non-nil error when n < len(b). At end of file, that error is
// io.EOF. It is safe for concurrent use.
func (s *StoreFile) ReadAt(p []byte, off int64) (n int, err error) {
if s.tmpfile == nil {
return 0, nil
}
return s.tmpfile.ReadAt(p, off)
}
// Size returns the amount of data (in bytes) in the Store.
func (s *StoreFile) Size() int64 {
return s.size
}
// Close must be called when the StoreFile is not used any more. It
// deletes the temporary file.
func (s *StoreFile) Close() error {
if s.tmpfile == nil {
return nil
}
name := s.tmpfile.Name()
err := s.tmpfile.Close()
err2 := os.Remove(name)
s.tmpfile = nil
s.size = 0
if err == nil && err2 != nil {
err = err2
}
return err
}
// StoreMemory takes data from io.Reader and provides io.ReaderAt backed by
// a memory buffer. It implements the Store interface.
type StoreMemory struct {
rdr *bytes.Reader
}
var _ Store = (*StoreMemory)(nil)
func NewStoreMemory() *StoreMemory {
return &StoreMemory{}
}
// Read and store the contents of r to a memory buffer. Previous contents
// (if any) are erased.
func (s *StoreMemory) ReadFrom(r io.Reader) (n int64, err error) {
var buf bytes.Buffer
n, err = buf.ReadFrom(r)
s.rdr = bytes.NewReader(buf.Bytes())
return n, err
}
// ReadAt reads len(b) bytes from the Store starting at byte offset off. It
// returns the number of bytes read and the error, if any. ReadAt always
// returns a non-nil error when n < len(b). At end of file, that error is
// io.EOF. It is safe for concurrent use.
func (s *StoreMemory) ReadAt(p []byte, off int64) (n int, err error) {
if s.rdr == nil {
return 0, nil
}
return s.rdr.ReadAt(p, off)
}
// Size returns the amount of data (in bytes) in the Store.
func (s *StoreMemory) Size() int64 {
if s.rdr == nil {
return 0
}
return s.rdr.Size()
}
// Close releases the memory buffer to be garbage collected.
func (s *StoreMemory) Close() error {
s.rdr = nil
return nil
}
// LimitedStore stores to a primary store up to a size limit. If the
// size limit is exceeded, a secondary store is used. If secondary store
// is nil, error is returned if the size limit is exceeded.
type LimitedStore struct {
s Store
primary Store
limit int64
secondary Store
}
var _ Store = (*LimitedStore)(nil)
// ErrStoreLimit error is returned when LimitedStore's limit is reached
// and there is no secondary fallback Store defined.
var ErrStoreLimit = errors.New("store size limit reached")
// NewLimitedStore creates a new LimitedStore with the specified settings.
func NewLimitedStore(primary Store, limit int64, secondary Store) *LimitedStore {
return &LimitedStore{
primary: primary,
limit: limit,
secondary: secondary,
}
}
// Store the contents of r to the primary store. If the size limit is
// reached, fall back to the secondary store or return ErrStoreLimit
// if secondary store is nil.
func (s *LimitedStore) ReadFrom(r io.Reader) (n int64, err error) {
if s.s != nil {
s.s.Close()
}
s.s = s.primary
lr := io.LimitReader(r, s.limit)
n, err = s.primary.ReadFrom(lr)
if n < s.limit {
return n, err
}
if s.secondary == nil {
return n, ErrStoreLimit
}
// move already received data from primary store to secondary store
srdr := io.NewSectionReader(s.primary, 0, n)
n, err = s.secondary.ReadFrom(io.MultiReader(srdr, r))
s.primary.Close()
s.s = s.secondary
return n, err
}
func (s *LimitedStore) ReadAt(p []byte, off int64) (n int, err error) {
if s.s == nil {
return 0, nil
}
return s.s.ReadAt(p, off)
}
func (s *LimitedStore) Close() error {
if s.s == nil {
return nil
}
err := s.s.Close()
s.s = nil
return err
}