Repository navigation
Expand file tree
/
Copy pathmemory.go
More file actions
326 lines (262 loc) · 7.99 KB
/
Copy pathmemory.go
File metadata and controls
326 lines (262 loc) · 7.99 KB
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
package dstore
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"net/url"
"path"
"sort"
"strings"
"sync"
"time"
)
// MemoryStore is a store that keeps all data in memory. Useful for unit testing where a store is required.
// Work in progress, not all methods are implemented
type MemoryStore struct {
*commonStore
baseURL *url.URL
data map[string][]byte
metadata map[string]map[string]string
modified map[string]time.Time
lock sync.RWMutex
}
func (m *MemoryStore) OpenObject(ctx context.Context, name string) (out io.ReadCloser, err error) {
m.lock.RLock()
defer m.lock.RUnlock()
if _, ok := m.data[name]; !ok {
return nil, ErrNotFound
}
reader := io.NopCloser(bytes.NewReader(m.data[name]))
out, err = m.uncompressedReader(ctx, reader)
return
}
func (m *MemoryStore) WriteObject(ctx context.Context, base string, f io.Reader, metadataKeyValues ...string) (err error) {
m.lock.Lock()
defer m.lock.Unlock()
// Parse metadataKeyValues array
if len(metadataKeyValues)%2 != 0 {
return fmt.Errorf("metadataKeyValues must have an even number of strings (key-value pairs), got %d", len(metadataKeyValues))
}
if _, exists := m.data[base]; !m.overwrite && exists {
return nil
}
w := bytes.NewBuffer(nil)
if err := m.compressedCopy(ctx, w, f); err != nil {
return err
}
m.data[base] = w.Bytes()
m.modified[base] = time.Now()
// Set metadata if provided
if len(metadataKeyValues) > 0 {
if m.metadata == nil {
m.metadata = make(map[string]map[string]string)
}
metadata := make(map[string]string)
for i := 0; i < len(metadataKeyValues); i += 2 {
key := metadataKeyValues[i]
value := metadataKeyValues[i+1]
metadata[key] = value
}
m.metadata[base] = metadata
}
return nil
}
func (m *MemoryStore) FileExists(_ context.Context, base string) (bool, error) {
m.lock.RLock()
defer m.lock.RUnlock()
_, exists := m.data[base]
return exists, nil
}
func (m *MemoryStore) ObjectPath(name string) string {
return path.Join(strings.TrimLeft(m.baseURL.Path, "/"), m.pathWithExt(name))
}
func (m *MemoryStore) ObjectURL(name string) string {
return objectURL(m.baseURL, m.pathWithExt(name))
}
func (m *MemoryStore) ObjectAttributes(_ context.Context, base string) (*ObjectAttributes, error) {
m.lock.RLock()
defer m.lock.RUnlock()
if !m.modified[base].IsZero() {
return &ObjectAttributes{
LastModified: m.modified[base],
Size: int64(len(m.data[base])),
Metadata: m.metadata[base],
}, nil
}
return nil, ErrNotFound
}
func (m *MemoryStore) SetMetadata(ctx context.Context, base string, metadata map[string]string) error {
m.lock.Lock()
defer m.lock.Unlock()
if _, exists := m.data[base]; !exists {
return ErrNotFound
}
if m.metadata == nil {
m.metadata = make(map[string]map[string]string)
}
// Create a copy of the metadata to avoid external modifications
metadataCopy := make(map[string]string)
for k, v := range metadata {
metadataCopy[k] = v
}
m.metadata[base] = metadataCopy
return nil
}
func (m *MemoryStore) PushLocalFile(ctx context.Context, localFile, toBaseName string) (err error) {
remove, err := pushLocalFile(ctx, m, localFile, toBaseName)
if err != nil {
return err
}
return remove()
}
func (m *MemoryStore) CopyObject(_ context.Context, src, dest string) error {
m.lock.Lock()
defer m.lock.Unlock()
if _, ok := m.data[src]; !ok {
return ErrNotFound
}
m.data[dest] = m.data[src]
return nil
}
func (m *MemoryStore) WalkFrom(_ context.Context, prefix, startingPoint string, f func(filename string) (err error)) error {
panic("not yet supported for this store type")
}
func (m *MemoryStore) WalkFromTo(_ context.Context, prefix, startingPoint, exclusiveEndPoint string, f func(filename string) (err error)) error {
panic("not yet supported for this store type")
}
func (m *MemoryStore) Walk(ctx context.Context, prefix string, f func(filename string) (err error)) error {
panic("not yet supported for this store type")
}
func (m *MemoryStore) ListFiles(ctx context.Context, prefix string, max int) ([]string, error) {
panic("not yet supported for this store type")
}
func (m *MemoryStore) DeleteObject(ctx context.Context, base string) error {
m.lock.Lock()
defer m.lock.Unlock()
delete(m.data, base)
delete(m.modified, base)
return nil
}
func (m *MemoryStore) BaseURL() *url.URL {
return &url.URL{}
}
func (m *MemoryStore) SubStore(subFolder string) (Store, error) {
m.lock.RLock()
defer m.lock.RUnlock()
newFiles := map[string][]byte{}
newModified := map[string]time.Time{}
for k, v := range m.data {
if !strings.HasPrefix(k, subFolder) {
continue
}
newFiles[strings.TrimPrefix(k, subFolder)] = v
newModified[strings.TrimPrefix(k, subFolder)] = m.modified[k]
}
return &MemoryStore{
commonStore: m.commonStore,
baseURL: m.baseURL,
data: newFiles,
modified: newModified,
}, nil
}
func (m *MemoryStore) Clone(ctx context.Context, opts ...Option) (Store, error) {
m.lock.RLock()
defer m.lock.RUnlock()
ms, err := newMemoryStoreContext(ctx, m.baseURL, m.extension, m.compressionType, m.overwrite, opts...)
if err != nil {
return nil, err
}
ms.data = m.data
ms.modified = m.modified
return ms, nil
}
func NewMemoryStore(baseURL *url.URL, extension, compressionType string, overwrite bool, opts ...Option) (*MemoryStore, error) {
return newMemoryStoreContext(context.Background(), baseURL, extension, compressionType, overwrite, opts...)
}
func newMemoryStoreContext(_ context.Context, baseURL *url.URL, extension, compressionType string, overwrite bool, opts ...Option) (*MemoryStore, error) {
common, err := newCommonStore(baseURL, extension, compressionType, overwrite, opts...)
if err != nil {
return nil, err
}
return &MemoryStore{
commonStore: common,
baseURL: baseURL,
data: map[string][]byte{},
modified: map[string]time.Time{},
}, nil
}
// WalkAttributes walks the objects held in memory. It reads the map directly rather than going
// through Walk, which this store does not implement yet.
func (m *MemoryStore) WalkAttributes(ctx context.Context, prefix string, f func(entry ObjectEntry) error) error {
m.lock.RLock()
entries := make([]ObjectEntry, 0, len(m.data))
for _, name := range m.sortedFiles() {
if !strings.HasPrefix(name, prefix) {
continue
}
entries = append(entries, ObjectEntry{
Name: name,
Size: int64(len(m.data[name])),
LastModified: m.modified[name],
})
}
m.lock.RUnlock()
for _, entry := range entries {
if err := f(entry); err != nil {
if errors.Is(err, StopIteration) {
return nil
}
return err
}
}
return nil
}
// sortedFiles returns the object names in lexicographic order, the order a listing would use.
// The caller must hold the lock.
func (m *MemoryStore) sortedFiles() []string {
names := make([]string, 0, len(m.data))
for name := range m.data {
names = append(names, name)
}
sort.Strings(names)
return names
}
// ListFolders returns the immediate sub-folders of prefix, reading them off the keys already
// held in memory.
func (m *MemoryStore) ListFolders(ctx context.Context, prefix string, max int) ([]string, error) {
return m.ListFoldersFromTo(ctx, prefix, "", "", max)
}
// ListFoldersFromTo filters the keys held in memory.
func (m *MemoryStore) ListFoldersFromTo(ctx context.Context, prefix, inclusiveFrom, exclusiveTo string, max int) ([]string, error) {
prefix = asFolderPrefix(prefix)
if err := checkFolderRange(prefix, inclusiveFrom, exclusiveTo); err != nil {
return nil, err
}
folders := newLimitedFolders(max)
if folders.full() {
return folders.folders, nil
}
m.lock.RLock()
defer m.lock.RUnlock()
seen := map[string]bool{}
for _, name := range m.sortedFiles() {
if !strings.HasPrefix(name, prefix) {
continue
}
folder, _, isFolder := strings.Cut(strings.TrimPrefix(name, prefix), "/")
if !isFolder || folder == "" || seen[folder] {
continue
}
seen[folder] = true
if !folderInRange(prefix+folder+"/", inclusiveFrom, exclusiveTo) {
continue
}
folders.add(prefix + folder + "/")
if folders.full() {
break
}
}
return folders.folders, nil
}