-
Notifications
You must be signed in to change notification settings - Fork 387
/
list.go
204 lines (174 loc) · 5.81 KB
/
list.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
// Copyright (C) 2020 Storj Labs, Inc.
// See LICENSE for copying information.
package metabase
import (
"context"
"strings"
"time"
"github.com/zeebo/errs"
"storj.io/common/storj"
"storj.io/common/uuid"
)
// ObjectEntry contains information about an item in a bucket.
type ObjectEntry struct {
IsPrefix bool
ObjectKey ObjectKey
Version Version
StreamID uuid.UUID
CreatedAt time.Time
ExpiresAt *time.Time
Status ObjectStatus
SegmentCount int32
EncryptedMetadataNonce []byte
EncryptedMetadata []byte
EncryptedMetadataEncryptedKey []byte
TotalPlainSize int64
TotalEncryptedSize int64
FixedSegmentSize int32
Encryption storj.EncryptionParameters
}
// StreamVersionID returns byte representation of object stream version id.
func (entry ObjectEntry) StreamVersionID() StreamVersionID {
return newStreamVersionID(entry.Version, entry.StreamID)
}
// Less implements sorting on object entries.
func (entry ObjectEntry) Less(other ObjectEntry) bool {
return ObjectStream{
ObjectKey: entry.ObjectKey,
Version: entry.Version,
StreamID: entry.StreamID,
}.Less(ObjectStream{
ObjectKey: other.ObjectKey,
Version: other.Version,
StreamID: other.StreamID,
})
}
// LessVersionAsc implements sorting on object entries.
func (entry ObjectEntry) LessVersionAsc(other ObjectEntry) bool {
return ObjectStream{
ObjectKey: entry.ObjectKey,
Version: entry.Version,
StreamID: entry.StreamID,
}.LessVersionAsc(ObjectStream{
ObjectKey: other.ObjectKey,
Version: other.Version,
StreamID: other.StreamID,
})
}
// ObjectsIterator iterates over a sequence of ObjectEntry items.
type ObjectsIterator interface {
Next(ctx context.Context, item *ObjectEntry) bool
}
// IterateCursor is a cursor used during iteration through objects.
//
// The cursor is exclusive.
type IterateCursor struct {
Key ObjectKey
Version Version
}
// StreamIDCursor is a cursor used during iteration through streamIDs of a pending object.
type StreamIDCursor struct {
StreamID uuid.UUID
}
// IterateObjectsWithStatus contains arguments necessary for listing objects in a bucket.
type IterateObjectsWithStatus struct {
ProjectID uuid.UUID
BucketName string
Recursive bool
BatchSize int
Prefix ObjectKey
Cursor IterateCursor
Pending bool
IncludeCustomMetadata bool
IncludeSystemMetadata bool
}
// IterateObjectsAllVersionsWithStatus iterates through all versions of all objects with specified status.
func (db *DB) IterateObjectsAllVersionsWithStatus(ctx context.Context, opts IterateObjectsWithStatus, fn func(context.Context, ObjectsIterator) error) (err error) {
defer mon.Task()(&ctx)(&err)
if err = opts.Verify(); err != nil {
return err
}
return iterateAllVersionsWithStatusDescending(ctx, db, opts, fn)
}
// IterateObjectsAllVersionsWithStatusAscending iterates through all versions of all objects with specified status. Ordered from oldest to latest.
// TODO this method was copied (and renamed) from v1.95.1 as a workaround for issues with metabase.ListObject performance. It should be removed
// when problem with metabase.ListObject will be fixed.
func (db *DB) IterateObjectsAllVersionsWithStatusAscending(ctx context.Context, opts IterateObjectsWithStatus, fn func(context.Context, ObjectsIterator) error) (err error) {
defer mon.Task()(&ctx)(&err)
if err = opts.Verify(); err != nil {
return err
}
return iterateAllVersionsWithStatusAscending(ctx, db, opts, fn)
}
// Verify verifies get object request fields.
func (opts *IterateObjectsWithStatus) Verify() error {
switch {
case opts.ProjectID.IsZero():
return ErrInvalidRequest.New("ProjectID missing")
case opts.BucketName == "":
return ErrInvalidRequest.New("BucketName missing")
case opts.BatchSize < 0:
return ErrInvalidRequest.New("BatchSize is negative")
}
return nil
}
// ListObjectsWithIterator lists objects.
func (db *DB) ListObjectsWithIterator(ctx context.Context, opts ListObjects) (result ListObjectsResult, err error) {
defer mon.Task()(&ctx)(&err)
if err := opts.Verify(); err != nil {
return ListObjectsResult{}, err
}
if opts.Pending || opts.AllVersions {
return ListObjectsResult{}, errs.New("not implemented")
}
ListLimit.Ensure(&opts.Limit)
err = db.IterateObjectsAllVersionsWithStatus(ctx,
IterateObjectsWithStatus{
ProjectID: opts.ProjectID,
BucketName: opts.BucketName,
Prefix: opts.Prefix,
Cursor: IterateCursor{
Key: opts.Cursor.Key,
Version: MaxVersion,
},
Recursive: opts.Recursive,
// TODO we may need to increase batch size to optimize number
// of DB calls for objects with multiple versions
BatchSize: opts.Limit + 1,
Pending: false,
IncludeCustomMetadata: opts.IncludeCustomMetadata,
IncludeSystemMetadata: opts.IncludeSystemMetadata,
}, func(ctx context.Context, it ObjectsIterator) error {
var previousLatestSet bool
var entry, previousLatest ObjectEntry
prefix := opts.Prefix
if prefix != "" && !strings.HasSuffix(string(prefix), "/") {
prefix += "/"
}
for len(result.Objects) < opts.Limit && it.Next(ctx, &entry) {
objectKey := prefix + entry.ObjectKey
if opts.Cursor.Key == objectKey && opts.Cursor.Version >= entry.Version {
previousLatestSet = true
previousLatest = entry
continue
}
if entry.Status.IsDeleteMarker() && (!previousLatestSet || prefix+previousLatest.ObjectKey != objectKey) {
previousLatestSet = true
previousLatest = entry
continue
}
if !previousLatestSet || prefix+previousLatest.ObjectKey != objectKey {
previousLatestSet = true
previousLatest = entry
result.Objects = append(result.Objects, entry)
}
}
result.More = it.Next(ctx, &entry)
return nil
},
)
if err != nil {
return ListObjectsResult{}, err
}
return result, nil
}