-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetds.go
354 lines (294 loc) · 8.65 KB
/
netds.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
package netds
import (
"context"
"errors"
"fmt"
"github.com/glin-gogogo/go-net-datastore/datastore"
"github.com/glin-gogogo/go-net-datastore/utils"
ds "github.com/ipfs/go-datastore"
dsQuery "github.com/ipfs/go-datastore/query"
logging "github.com/ipfs/go-log"
"io"
"path"
"strings"
"sync"
)
var log = logging.Logger("netds")
var (
ErrDatastoreExists = errors.New("datastore already exists")
ErrDatastoreDoesNotExist = errors.New("datastore directory does not exist")
ErrShardingFileMissing = fmt.Errorf("%s file not found in datastore", ShardingFn)
ErrClosed = errors.New("datastore closed")
ErrInvalidKey = errors.New("key not supported by netds")
)
type ShardFunc func(string) string
type NetDataStore struct {
shardStr string
getDir ShardFunc
ds datastore.DataStorage
numWorkers int
}
func CreateOrOpen(ctx context.Context, basePath string, fun *ShardIdV1, cfg utils.DataStoreConfig) (*NetDataStore, error) {
backendDs, err := New(cfg)
if err != nil {
return nil, err
}
exist, err := backendDs.IsObjectExist(ctx, basePath, true)
if err != nil {
return nil, err
}
if !exist {
upperPath := path.Dir(strings.TrimSuffix(basePath, "/"))
exist, _ = backendDs.IsObjectExist(ctx, upperPath, true)
if !exist {
err := backendDs.CreateFolder(ctx, upperPath, true)
if err != nil {
return nil, err
}
}
err := backendDs.CreateFolder(ctx, basePath, true)
if err != nil {
return nil, err
}
dsFun, err := ReadShardFunc(backendDs)
switch {
case errors.Is(err, ErrShardingFileMissing):
err = WriteShardFunc(backendDs, fun)
if err != nil {
return nil, err
}
case err == nil:
if fun.String() != dsFun.String() {
return nil, fmt.Errorf("specified shard func '%s' does not match repo shard func '%s'",
fun.String(), dsFun.String())
}
return nil, ErrDatastoreExists
default:
return nil, err
}
}
shardId, err := ReadShardFunc(backendDs)
if err != nil {
return nil, err
}
if cfg.Workers <= 0 {
cfg.Workers = utils.MaxBatchWorkers
}
return &NetDataStore{
shardStr: shardId.String(),
getDir: shardId.Func(),
ds: backendDs,
numWorkers: cfg.Workers,
}, nil
}
func (nds *NetDataStore) RootDir() string {
return nds.ds.RootDir()
}
func (nds *NetDataStore) encode(key ds.Key) (dir, file string) {
noSlash := key.String()[1:]
dir = nds.getDir(noSlash)
file = path.Join(dir, noSlash+utils.Extension)
return dir, file
}
func (nds *NetDataStore) decode(file string) (key ds.Key, ok bool) {
if !strings.HasSuffix(file, utils.Extension) {
return ds.Key{}, false
}
name := file[:len(file)-len(utils.Extension)]
return ds.NewKey(name), true
}
func (nds *NetDataStore) Put(ctx context.Context, k ds.Key, value []byte) error {
if !utils.KeyIsValid(k) {
return fmt.Errorf("when putting '%q': %v", k, ErrInvalidKey)
}
dir, file := nds.encode(k)
rootDir := nds.RootDir()
metadata, exist, _ := nds.GetObjectMetadata(ctx, path.Join(rootDir, file))
//- fixme later
//if exist && metadata.ContentType == utils.BlocksContentType && metadata.ContentLength == utils.BlocksContentLength {
if exist && metadata.ContentLength == utils.BlocksContentLength {
return nil
}
if !strings.HasSuffix(dir, "/") {
dir += "/"
}
exist, _ = nds.IsObjectExist(ctx, path.Join(rootDir, dir), true)
if !exist {
if err := nds.CreateFolder(ctx, path.Join(rootDir, dir), true); err != nil {
return err
}
}
return nds.ds.Put(ctx, ds.NewKey(file), value)
}
func (nds *NetDataStore) Get(ctx context.Context, k ds.Key) ([]byte, error) {
if !utils.KeyIsValid(k) {
return nil, ds.ErrNotFound
}
_, file := nds.encode(k)
return nds.ds.Get(ctx, ds.NewKey(file))
}
func (nds *NetDataStore) Has(ctx context.Context, k ds.Key) (bool, error) {
if !utils.KeyIsValid(k) {
return false, nil
}
_, file := nds.encode(k)
return nds.ds.Has(ctx, ds.NewKey(file))
}
func (nds *NetDataStore) GetSize(ctx context.Context, k ds.Key) (int, error) {
if !utils.KeyIsValid(k) {
return -1, ds.ErrNotFound
}
_, file := nds.encode(k)
return nds.ds.GetSize(ctx, ds.NewKey(file))
}
func (nds *NetDataStore) Delete(ctx context.Context, k ds.Key) error {
if !utils.KeyIsValid(k) {
return nil
}
_, file := nds.encode(k)
return nds.ds.Delete(ctx, ds.NewKey(file))
}
func (nds *NetDataStore) Query(ctx context.Context, q dsQuery.Query) (dsQuery.Results, error) {
return nds.ds.Query(ctx, q)
}
func (nds *NetDataStore) Sync(ctx context.Context, prefix ds.Key) error {
return nds.ds.Sync(ctx, prefix)
}
func (nds *NetDataStore) Close() error {
return nds.ds.Close()
}
type NetDataStoreBatch struct {
nds *NetDataStore
ops map[string]batchOp
numWorkers int
}
type batchOp struct {
val []byte
delete bool
}
func (nds *NetDataStore) Batch(_ context.Context) (ds.Batch, error) {
return &NetDataStoreBatch{
nds: nds,
ops: make(map[string]batchOp),
numWorkers: nds.numWorkers,
}, nil
}
func (n *NetDataStoreBatch) Put(ctx context.Context, k ds.Key, val []byte) error {
n.ops[k.String()] = batchOp{
val: val,
delete: false,
}
return nil
}
func (n *NetDataStoreBatch) Delete(ctx context.Context, k ds.Key) error {
n.ops[k.String()] = batchOp{
val: nil,
delete: true,
}
return nil
}
func (n *NetDataStoreBatch) Commit(ctx context.Context) error {
var (
deleteObjs []ds.Key
putKeys []ds.Key
)
for k, op := range n.ops {
if op.delete {
deleteObjs = append(deleteObjs, ds.NewKey(k))
} else {
putKeys = append(putKeys, ds.NewKey(k))
}
}
numJobs := len(putKeys) + (len(deleteObjs) / utils.DefaultDeleteMax)
jobs := make(chan func() error, numJobs)
results := make(chan error, numJobs)
numWorkers := n.numWorkers
if numJobs < numWorkers {
numWorkers = numJobs
}
var wg sync.WaitGroup
wg.Add(numWorkers)
defer wg.Wait()
for w := 0; w < numWorkers; w++ {
go func() {
defer wg.Done()
worker(jobs, results)
}()
}
for _, k := range putKeys {
jobs <- n.newPutJob(ctx, k, n.ops[k.String()].val)
}
if len(deleteObjs) > 0 {
for i := 0; i < len(deleteObjs); i += utils.DefaultDeleteMax {
limit := utils.DefaultDeleteMax
if len(deleteObjs[i:]) < limit {
limit = len(deleteObjs[i:])
}
jobs <- n.newDeleteJob(ctx, deleteObjs[i:i+limit])
}
}
close(jobs)
var errs []string
for i := 0; i < numJobs; i++ {
err := <-results
if err != nil {
errs = append(errs, err.Error())
}
}
if len(errs) > 0 {
return fmt.Errorf("netds: failed batch operation:\n%s", strings.Join(errs, "\n"))
}
return nil
}
func (n *NetDataStoreBatch) newPutJob(ctx context.Context, k ds.Key, value []byte) func() error {
return func() error {
return n.nds.Put(ctx, k, value)
}
}
func (n *NetDataStoreBatch) newDeleteJob(ctx context.Context, objs []ds.Key) func() error {
var objects []*datastore.ObjectMetadata
for _, obj := range objs {
_, file := n.nds.encode(obj)
objects = append(objects, &datastore.ObjectMetadata{
Key: file,
})
}
return func() error {
return n.nds.DeleteObjects(ctx, objects)
}
}
func worker(jobs <-chan func() error, results chan<- error) {
for j := range jobs {
results <- j()
}
}
func (nds *NetDataStore) GetObjectMetadata(ctx context.Context, objectKey string) (*datastore.ObjectMetadata, bool, error) {
return nds.ds.GetObjectMetadata(ctx, objectKey)
}
func (nds *NetDataStore) GetObject(ctx context.Context, objectKey string) (io.ReadCloser, error) {
return nds.ds.GetObject(ctx, objectKey)
}
func (nds *NetDataStore) PutObject(ctx context.Context, objectKey, digest string, totalLength int64, reader io.Reader) error {
return nds.ds.PutObject(ctx, objectKey, digest, totalLength, reader)
}
func (nds *NetDataStore) DeleteObject(ctx context.Context, objectKey string) error {
return nds.ds.DeleteObject(ctx, objectKey)
}
func (nds *NetDataStore) DelUncompletedDirtyObject(ctx context.Context, objectKey string) error {
return nds.ds.DelUncompletedDirtyObject(ctx, objectKey)
}
func (nds *NetDataStore) DeleteObjects(ctx context.Context, objects []*datastore.ObjectMetadata) error {
return nds.ds.DeleteObjects(ctx, objects)
}
func (nds *NetDataStore) ListFolderObjects(ctx context.Context, prefix string) ([]*datastore.ObjectMetadata, error) {
return nds.ds.ListFolderObjects(ctx, prefix)
}
func (nds *NetDataStore) IsObjectExist(ctx context.Context, objectKey string, isFolder bool) (bool, error) {
return nds.ds.IsObjectExist(ctx, objectKey, isFolder)
}
func (nds *NetDataStore) CreateFolder(ctx context.Context, folderName string, isEmptyFolder bool) error {
return nds.ds.CreateFolder(ctx, folderName, isEmptyFolder)
}
func (nds *NetDataStore) GetFolderMetadata(ctx context.Context, folderKey string) (*datastore.ObjectMetadata, bool, error) {
return nds.ds.GetFolderMetadata(ctx, folderKey)
}