-
Notifications
You must be signed in to change notification settings - Fork 0
/
oss.go
323 lines (276 loc) · 6.75 KB
/
oss.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
package oss
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"path"
"strconv"
"strings"
"sync"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
ds "github.com/ipfs/go-datastore"
"github.com/ipfs/go-datastore/query"
)
const (
// listMax is the largest amount of objects you can request from S3 in a list
// call.
listMax = 1000
// deleteMax is the largest amount of objects you can delete from S3 in a
// delete objects call.
deleteMax = 1000
defaultWorkers = 100
)
type Config struct {
Endpoint string
AccessKeyID string
AccessKeySecret string
BucketName string
RootDirectory string
Workers int
}
type datastore struct {
Config
Bucket *oss.Bucket
}
const NoSuchKey = "NoSuchKey"
func (s *datastore) Sync(ctx context.Context, _ ds.Key) error {
return nil
}
func (s *datastore) Batch(ctx context.Context) (ds.Batch, error) {
return &ossBatch{
s: s,
ops: make(map[string]batchOp),
numWorkers: s.Workers,
}, nil
}
func newDataStore(config Config, bucket *oss.Bucket) *datastore {
return &datastore{Config: config, Bucket: bucket}
}
func NewOssDatastore(config Config) (*datastore, error) {
if config.Workers == 0 {
config.Workers = defaultWorkers
}
client, err := oss.New(config.Endpoint, config.AccessKeyID, config.AccessKeySecret)
if err != nil {
return nil, fmt.Errorf("failed to create new client: %s", err)
}
bucket, err := client.Bucket(config.BucketName)
if err != nil {
return nil, fmt.Errorf("failed to get bucket: %s", err)
}
return newDataStore(config, bucket), nil
}
func (s *datastore) Put(ctx context.Context, key ds.Key, value []byte) (err error) {
err = s.Bucket.PutObject(s.ossPath(key.String()), bytes.NewBuffer(value))
return err
}
func (s *datastore) Get(ctx context.Context, key ds.Key) (value []byte, err error) {
resp, err := s.Bucket.GetObject(s.ossPath(key.String()))
if err != nil {
if isNotFound(err) {
return nil, ds.ErrNotFound
}
return nil, err
}
defer resp.Close()
return ioutil.ReadAll(resp)
}
func (s *datastore) GetSize(ctx context.Context, key ds.Key) (size int, err error) {
headers, err := s.Bucket.GetObjectMeta(s.ossPath(key.String()))
if err != nil {
if ossErr, ok := err.(oss.ServiceError); ok && ossErr.StatusCode == 404 {
return -1, ds.ErrNotFound
}
return -1, err
}
length := headers.Get("Content-Length")
u, err := strconv.ParseUint(length, 10, 64)
if err != nil {
return -1, err
}
return int(u), nil
}
func (s *datastore) Close() error {
return nil
}
func (s *datastore) Has(ctx context.Context, key ds.Key) (exists bool, err error) {
_, err = s.GetSize(ctx, key)
if err != nil {
if err == ds.ErrNotFound {
return false, nil
}
return false, err
}
return true, nil
}
func (s *datastore) Delete(ctx context.Context, key ds.Key) (err error) {
err = s.Bucket.DeleteObject(s.ossPath(key.String()))
if isNotFound(err) {
// delete is idempotent
err = nil
}
return err
}
func (s *datastore) Query(ctx context.Context, q query.Query) (query.Results, error) {
if q.Orders != nil || q.Filters != nil {
return nil, fmt.Errorf("ossds: filters or orders are not supported")
}
limit := q.Limit + q.Offset
if limit == 0 || limit > listMax {
limit = listMax
}
lsRes, err := s.Bucket.ListObjects(oss.MaxKeys(limit), oss.Prefix(s.ossPath(q.Prefix)))
if err != nil {
return nil, err
}
index := q.Offset
nextValue := func() (query.Result, bool) {
for index >= len(lsRes.Objects) {
if !lsRes.IsTruncated {
return query.Result{}, false
}
index -= len(lsRes.Objects)
lsRes, err = s.Bucket.ListObjects(
oss.Prefix(s.ossPath(q.Prefix)),
oss.MaxKeys(listMax),
oss.Delimiter("/"),
oss.Marker(lsRes.NextMarker),
)
if err != nil {
return query.Result{Error: err}, false
}
}
entry := query.Entry{
//Key: ds.NewKey(lsRes.Objects[index].Key).String(),
Key: s.ossKey(lsRes.Objects[index].Key),
Size: int(lsRes.Objects[index].Size),
}
if !q.KeysOnly {
value, err := s.Get(ctx, ds.NewKey(entry.Key))
if err != nil {
return query.Result{Error: err}, false
}
entry.Value = value
}
index++
return query.Result{Entry: entry}, true
}
return query.ResultsFromIterator(
q, query.Iterator{
Close: func() error {
return nil
},
Next: nextValue,
},
), nil
}
func (s *datastore) ossKey(p string) string {
return strings.Replace(p, s.RootDirectory, "", 1)
}
func (s *datastore) ossPath(p string) string {
return path.Join(s.RootDirectory, p)
}
type ossBatch struct {
s *datastore
ops map[string]batchOp
numWorkers int
}
type batchOp struct {
val []byte
delete bool
}
func (b *ossBatch) Put(ctx context.Context, key ds.Key, val []byte) error {
b.ops[key.String()] = batchOp{
val: val,
delete: false,
}
return nil
}
func (b *ossBatch) Delete(ctx context.Context, key ds.Key) error {
b.ops[key.String()] = batchOp{
val: nil,
delete: true,
}
return nil
}
func (b *ossBatch) Commit(ctx context.Context) error {
var (
deleteObjs []string
putKeys []ds.Key
)
for k, op := range b.ops {
if op.delete {
deleteObjs = append(deleteObjs, k)
} else {
putKeys = append(putKeys, ds.NewKey(k))
}
}
numJobs := len(putKeys) + (len(deleteObjs) / deleteMax)
jobs := make(chan func() error, numJobs)
results := make(chan error, numJobs)
numWorkers := b.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 <- b.newPutJob(ctx, k, b.ops[k.String()].val)
}
if len(deleteObjs) > 0 {
for i := 0; i < len(deleteObjs); i += deleteMax {
limit := deleteMax
if len(deleteObjs[i:]) < limit {
limit = len(deleteObjs[i:])
}
jobs <- b.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("ossds: failed batch operation:\n%s", strings.Join(errs, "\n"))
}
return nil
}
func (b *ossBatch) newPutJob(ctx context.Context, key ds.Key, value []byte) func() error {
return func() error {
return b.s.Put(ctx, key, value)
}
}
func isNotFound(err error) bool {
ossErr, ok := err.(oss.ServiceError)
return ok && ossErr.Code == NoSuchKey
}
func (b *ossBatch) newDeleteJob(ctx context.Context, objs []string) func() error {
return func() error {
_, err := b.s.Bucket.DeleteObjects(objs, oss.DeleteObjectsQuiet(true))
if err != nil {
return err
}
if err != nil && !isNotFound(err) {
return err
}
return nil
}
}
func worker(jobs <-chan func() error, results chan<- error) {
for j := range jobs {
results <- j()
}
}
var _ ds.Batching = (*datastore)(nil)