-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
memstore.go
359 lines (331 loc) · 8.57 KB
/
memstore.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// Copyright 2022 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package storage
import (
"bytes"
"context"
"io"
"path"
"sort"
"strings"
"sync"
"github.com/pingcap/errors"
"go.uber.org/atomic"
)
type memFile struct {
Data atomic.Pointer[[]byte]
}
// GetData gets the underlying byte slice of the atomic pointer
func (f *memFile) GetData() []byte {
var fileData []byte
if p := f.Data.Load(); p != nil {
fileData = *p
}
return fileData
}
// MemStorage represents a in-memory storage.
type MemStorage struct {
rwm sync.RWMutex
dataStore map[string]*memFile
}
// NewMemStorage creates a new in-memory storage.
func NewMemStorage() *MemStorage {
return &MemStorage{
dataStore: make(map[string]*memFile),
}
}
func (s *MemStorage) loadMap(name string) (*memFile, bool) {
s.rwm.RLock()
defer s.rwm.RUnlock()
theFile, ok := s.dataStore[name]
return theFile, ok
}
// DeleteFile delete the file in storage
// It implements the `ExternalStorage` interface
func (s *MemStorage) DeleteFile(ctx context.Context, name string) error {
select {
case <-ctx.Done():
return ctx.Err()
default:
// continue on
}
if !path.IsAbs(name) {
return errors.Errorf("file name is not an absolute path: %s", name)
}
s.rwm.Lock()
defer s.rwm.Unlock()
if _, ok := s.dataStore[name]; !ok {
return errors.Errorf("cannot find the file: %s", name)
}
delete(s.dataStore, name)
return nil
}
// DeleteFiles delete the files in storage
// It implements the `ExternalStorage` interface
func (s *MemStorage) DeleteFiles(ctx context.Context, names []string) error {
for _, name := range names {
err := s.DeleteFile(ctx, name)
if err != nil {
return err
}
}
return nil
}
// WriteFile file to storage.
// It implements the `ExternalStorage` interface
func (s *MemStorage) WriteFile(ctx context.Context, name string, data []byte) error {
select {
case <-ctx.Done():
return ctx.Err()
default:
// continue on
}
if !path.IsAbs(name) {
return errors.Errorf("file name is not an absolute path: %s", name)
}
fileData := append([]byte{}, data...)
s.rwm.Lock()
defer s.rwm.Unlock()
theFile, ok := s.dataStore[name]
if ok {
theFile.Data.Store(&fileData)
} else {
theFile := new(memFile)
theFile.Data.Store(&fileData)
s.dataStore[name] = theFile
}
return nil
}
// ReadFile reads the storage file.
// It implements the `ExternalStorage` interface
func (s *MemStorage) ReadFile(ctx context.Context, name string) ([]byte, error) {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
// continue on
}
if !path.IsAbs(name) {
return nil, errors.Errorf("file name is not an absolute path: %s", name)
}
theFile, ok := s.loadMap(name)
if !ok {
return nil, errors.Errorf("cannot find the file: %s", name)
}
fileData := theFile.GetData()
return append([]byte{}, fileData...), nil
}
// FileExists return true if file exists.
// It implements the `ExternalStorage` interface
func (s *MemStorage) FileExists(ctx context.Context, name string) (bool, error) {
select {
case <-ctx.Done():
return false, ctx.Err()
default:
// continue on
}
if !path.IsAbs(name) {
return false, errors.Errorf("file name is not an absolute path: %s", name)
}
_, ok := s.loadMap(name)
return ok, nil
}
// Open opens a Reader by file path.
// It implements the `ExternalStorage` interface
func (s *MemStorage) Open(ctx context.Context, filePath string, o *ReaderOption) (ExternalFileReader, error) {
if err := ctx.Err(); err != nil {
return nil, errors.Trace(err)
}
if !path.IsAbs(filePath) {
return nil, errors.Errorf("file name is not an absolute path: %s", filePath)
}
theFile, ok := s.loadMap(filePath)
if !ok {
return nil, errors.Errorf("cannot find the file: %s", filePath)
}
data := theFile.GetData()
// just for simplicity, different from other implementation, MemStorage can't
// seek beyond [o.StartOffset, o.EndOffset)
start, end := 0, len(data)
if o != nil {
if o.StartOffset != nil {
start = int(*o.StartOffset)
}
if o.EndOffset != nil {
end = int(*o.EndOffset)
}
}
r := bytes.NewReader(data[start:end])
return &memFileReader{
br: r,
size: int64(len(data)),
}, nil
}
// WalkDir traverse all the files in a dir.
// It implements the `ExternalStorage` interface
func (s *MemStorage) WalkDir(ctx context.Context, opt *WalkOption, fn func(string, int64) error) error {
allFileNames := func() []string {
fileNames := []string{}
s.rwm.RLock()
defer s.rwm.RUnlock()
for fileName := range s.dataStore {
if opt != nil {
if len(opt.SubDir) > 0 {
if !strings.HasPrefix(fileName, opt.SubDir) {
continue
}
}
if len(opt.ObjPrefix) > 0 {
baseName := path.Base(fileName)
if !strings.HasPrefix(baseName, opt.ObjPrefix) {
continue
}
}
}
fileNames = append(fileNames, fileName)
}
return fileNames
}()
sort.Strings(allFileNames)
for _, fileName := range allFileNames {
select {
case <-ctx.Done():
return ctx.Err()
default:
// continue on
}
theFile, ok := s.loadMap(fileName)
if !ok {
continue
}
fileSize := len(theFile.GetData())
if err := fn(fileName, int64(fileSize)); err != nil {
return err
}
}
return nil
}
// URI returns the URI of the storage.
func (*MemStorage) URI() string {
return "memstore://"
}
// Create creates a file and returning a writer to write data into.
// When the writer is closed, the data is stored in the file.
// It implements the `ExternalStorage` interface
func (s *MemStorage) Create(ctx context.Context, name string, _ *WriterOption) (ExternalFileWriter, error) {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
// continue on
}
if !path.IsAbs(name) {
return nil, errors.Errorf("file name is not an absolute path: %s", name)
}
s.rwm.Lock()
defer s.rwm.Unlock()
theFile := new(memFile)
s.dataStore[name] = theFile
return &memFileWriter{
file: theFile,
}, nil
}
// Rename renames a file name to another file name.
// It implements the `ExternalStorage` interface
func (s *MemStorage) Rename(ctx context.Context, oldFileName, newFileName string) error {
select {
case <-ctx.Done():
return ctx.Err()
default:
// continue on
}
if !path.IsAbs(newFileName) {
return errors.Errorf("new file name is not an absolute path: %s", newFileName)
}
s.rwm.Lock()
defer s.rwm.Unlock()
theFile, ok := s.dataStore[oldFileName]
if !ok {
return errors.Errorf("the file doesn't exist: %s", oldFileName)
}
s.dataStore[newFileName] = theFile
delete(s.dataStore, oldFileName)
return nil
}
// Close implements ExternalStorage interface.
func (s *MemStorage) Close() {
s.dataStore = nil
}
// memFileReader is the struct to read data from an opend mem storage file
type memFileReader struct {
br *bytes.Reader
size int64
isClosed atomic.Bool
}
// Read reads the mem storage file data
// It implements the `io.Reader` interface
func (r *memFileReader) Read(p []byte) (int, error) {
if r.isClosed.Load() {
return 0, io.EOF
}
return r.br.Read(p)
}
// Close closes the mem storage file data
// It implements the `io.Closer` interface
func (r *memFileReader) Close() error {
r.isClosed.Store(true)
return nil
}
// Seek seeks the offset inside the mem storage file
// It implements the `io.Seeker` interface
func (r *memFileReader) Seek(offset int64, whence int) (int64, error) {
if r.isClosed.Load() {
return 0, errors.New("reader closed")
}
return r.br.Seek(offset, whence)
}
func (r *memFileReader) GetFileSize() (int64, error) {
return r.size, nil
}
// memFileReader is the struct to write data into the opened mem storage file
type memFileWriter struct {
buf bytes.Buffer
file *memFile
isClosed atomic.Bool
}
// Write writes the data into the mem storage file buffer.
// It implements the `ExternalFileWriter` interface
func (w *memFileWriter) Write(ctx context.Context, p []byte) (int, error) {
select {
case <-ctx.Done():
return 0, ctx.Err()
default:
// continue on
}
if w.isClosed.Load() {
return 0, errors.New("writer closed")
}
return w.buf.Write(p)
}
func (w *memFileWriter) Close(ctx context.Context) error {
select {
case <-ctx.Done():
return ctx.Err()
default:
// continue on
}
fileData := append([]byte{}, w.buf.Bytes()...)
w.file.Data.Store(&fileData)
w.isClosed.Store(true)
return nil
}