-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmemory.go
204 lines (160 loc) · 5.01 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
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
package dstore
import (
"bytes"
"context"
"fmt"
"io"
"net/url"
"path"
"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
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) (err error) {
m.lock.Lock()
defer m.lock.Unlock()
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()
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 fmt.Sprintf("%s/%s", strings.TrimRight(m.baseURL.String(), "/"), strings.TrimLeft(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])),
}, nil
}
return nil, ErrNotFound
}
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) {
conf := config{}
for _, opt := range opts {
opt.apply(&conf)
}
common := &commonStore{
compressionType: compressionType,
extension: extension,
overwrite: overwrite,
uncompressedReadCallback: conf.uncompressedReadCallback,
compressedReadCallback: conf.compressedReadCallback,
uncompressedWriteCallback: conf.uncompressedWriteCallback,
compressedWriteCallback: conf.compressedWriteCallback,
}
return &MemoryStore{
commonStore: common,
baseURL: baseURL,
data: map[string][]byte{},
modified: map[string]time.Time{},
}, nil
}