-
Notifications
You must be signed in to change notification settings - Fork 35
/
memory.go
139 lines (123 loc) · 2.68 KB
/
memory.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
package storage
import (
"bytes"
"context"
"fmt"
"io"
"os"
"sort"
"strings"
"sync"
"time"
"github.com/bnb-chain/greenfield-storage-provider/pkg/log"
)
type memoryStore struct {
name string
objects map[string]*memoryObject
sync.Mutex
DefaultObjectStorage
}
type memoryObject struct {
data []byte
modTime time.Time
}
func newMemoryStore(cfg ObjectStorageConfig) (ObjectStorage, error) {
store := &memoryStore{name: cfg.BucketURL}
store.objects = make(map[string]*memoryObject)
return store, nil
}
func (m *memoryStore) String() string {
return fmt.Sprintf("memory://%s/", m.name)
}
func (m *memoryStore) GetObject(ctx context.Context, key string, offset, limit int64) (io.ReadCloser, error) {
m.Lock()
defer m.Unlock()
// Minimum length is 1
if key == "" {
return nil, ErrInvalidObjectKey
}
d, ok := m.objects[key]
if !ok {
return nil, ErrNoSuchObject
}
if offset > int64(len(d.data)) {
offset = int64(len(d.data))
}
data := d.data[offset:]
log.Infow("uu", "limit", limit, "length", len(data))
if limit > 0 && limit < int64(len(data)) {
data = data[:limit]
}
return io.NopCloser(bytes.NewBuffer(data)), nil
}
func (m *memoryStore) PutObject(ctx context.Context, key string, reader io.Reader) error {
m.Lock()
defer m.Unlock()
// Minimum length is 1
if key == "" {
return ErrInvalidObjectKey
}
if _, ok := m.objects[key]; ok {
log.Info("overwrite key: ", key)
}
data, err := io.ReadAll(reader)
if err != nil {
return err
}
m.objects[key] = &memoryObject{data: data, modTime: time.Now()}
return nil
}
func (m *memoryStore) DeleteObject(ctx context.Context, key string) error {
m.Lock()
defer m.Unlock()
delete(m.objects, key)
return nil
}
func (m *memoryStore) HeadBucket(ctx context.Context) error {
return nil
}
func (m *memoryStore) HeadObject(ctx context.Context, key string) (Object, error) {
m.Lock()
defer m.Unlock()
// Minimum length is 1
if key == "" {
return nil, ErrInvalidObjectKey
}
o, ok := m.objects[key]
if !ok {
return nil, os.ErrNotExist
}
return &object{
key,
int64(len(o.data)),
o.modTime,
false,
}, nil
}
func (m *memoryStore) ListObjects(ctx context.Context, prefix, marker, delimiter string, limit int64) ([]Object, error) {
if delimiter != "" {
return nil, ErrUnsupportedDelimiter
}
m.Lock()
defer m.Unlock()
objs := make([]Object, 0)
for k := range m.objects {
if strings.HasPrefix(k, prefix) && k > marker {
o := m.objects[k]
f := &object{
k,
int64(len(o.data)),
o.modTime,
false,
}
objs = append(objs, f)
}
}
sort.Slice(objs, func(i, j int) bool {
return objs[i].Key() < objs[j].Key()
})
if int64(len(objs)) > limit {
objs = objs[:limit]
}
return objs, nil
}