forked from minio/minio
-
Notifications
You must be signed in to change notification settings - Fork 14
/
gateway-s3x-ledger.go
100 lines (90 loc) · 2.72 KB
/
gateway-s3x-ledger.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
package s3x
import (
"context"
"sort"
"strings"
"github.com/ipfs/go-datastore"
"github.com/ipfs/go-datastore/query"
)
/* Design Notes
---------------
Internal functions should never claim or release locks.
Any claiming or releasing of locks should be done in the public setter+getter functions.
The reason for this is so that we can enable easy reuse of internal code.
*/
// Close shuts down the ledger datastore
func (ls *ledgerStore) Close() error {
//todo: clean up caches
return ls.ds.Close()
}
/////////////////////
// GETTER FUNCTINS //
/////////////////////
// GetObjectInfos returns a list of ordered ObjectInfos with given prefix ordered by name
func (ls *ledgerStore) GetObjectInfos(ctx context.Context, bucket, prefix, startsFrom string, max int) ([]ObjectInfo, error) {
defer ls.locker.read(bucket)()
b, err := ls.getBucketLoaded(ctx, bucket)
if err != nil {
return nil, err
}
var names []string
objs := b.GetBucket().GetObjects()
for name := range objs {
if strings.HasPrefix(name, prefix) && strings.Compare(startsFrom, name) <= 0 {
names = append(names, name)
}
}
sort.Strings(names)
if max > 0 && len(names) > max {
names = names[:max]
}
list := make([]ObjectInfo, 0, len(names))
for _, name := range names {
obj, err := ls.object(ctx, bucket, name)
if err != nil {
return nil, err
}
list = append(list, obj.GetObjectInfo())
}
return list, nil
}
// GetObjectHash is used to retrieve the corresponding IPFS CID for an object
func (ls *ledgerStore) GetObjectHash(ctx context.Context, bucket, object string) (string, error) {
objs, unlock, err := ls.GetObjectHashes(ctx, bucket)
if err != nil {
return "", err
}
defer unlock()
h, ok := objs[object]
if !ok {
return "", ErrLedgerObjectDoesNotExist
}
return h, nil
}
// GetObjectHashes gets a map of object names to object hashes for all objects in a bucket.
// The returned function must be called to release a read lock, iff an error is not returned.
func (ls *ledgerStore) GetObjectHashes(ctx context.Context, bucket string) (map[string]string, func(), error) {
unlock := ls.locker.read(bucket)
b, err := ls.getBucketLoaded(ctx, bucket)
if err != nil {
unlock()
return nil, nil, err
}
return b.Bucket.Objects, unlock, nil
}
// GetBucketNames is used to get a slice of all bucket names our ledger currently tracks
func (ls *ledgerStore) GetBucketNames() ([]string, error) {
//this only reads from the datastore, which have it's own synchronization, so no locking is needed.
rs, err := ls.ds.Query(query.Query{
Prefix: dsBucketKey.String(),
KeysOnly: true,
})
if err != nil {
return nil, err
}
names := []string{}
for r := range rs.Next() {
names = append(names, datastore.NewKey(r.Key).BaseNamespace())
}
return names, nil
}