-
Notifications
You must be signed in to change notification settings - Fork 41
/
bucket.go
315 lines (277 loc) · 8.35 KB
/
bucket.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
package bsdb
import (
"errors"
"time"
"cosmossdk.io/math"
"github.com/forbole/juno/v4/common"
"github.com/shopspring/decimal"
"gorm.io/gorm"
"github.com/bnb-chain/greenfield/x/storage/types"
)
// GetUserBuckets get buckets info by a user address
func (b *BsDBImpl) GetUserBuckets(accountID common.Address, includeRemoved bool) ([]*Bucket, error) {
var (
buckets []*Bucket
err error
)
startTime := time.Now()
methodName := currentFunction()
defer func() {
if err != nil {
MetadataDatabaseFailureMetrics(err, startTime, methodName)
} else {
MetadataDatabaseSuccessMetrics(startTime, methodName)
}
}()
if includeRemoved {
err = b.db.Table((&Bucket{}).TableName()).
Select("*").
Where("owner = ?", accountID).
Order("create_at desc").
Limit(GetUserBucketsLimitSize).
Find(&buckets).Error
} else {
err = b.db.Table((&Bucket{}).TableName()).
Select("*").
Where("owner = ? and removed = false", accountID).
Order("create_at desc").
Limit(GetUserBucketsLimitSize).
Find(&buckets).Error
}
return buckets, err
}
// GetBucketByName get buckets info by a bucket name
func (b *BsDBImpl) GetBucketByName(bucketName string, includePrivate bool) (*Bucket, error) {
var (
bucket *Bucket
err error
)
startTime := time.Now()
methodName := currentFunction()
defer func() {
if err != nil {
MetadataDatabaseFailureMetrics(err, startTime, methodName)
} else {
MetadataDatabaseSuccessMetrics(startTime, methodName)
}
}()
if includePrivate {
err = b.db.Take(&bucket, "bucket_name = ? and removed = false", bucketName).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
return bucket, err
}
err = b.db.Take(&bucket, "bucket_name = ? and visibility = ? and removed = false", bucketName, types.VISIBILITY_TYPE_PUBLIC_READ.String()).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
return bucket, err
}
// GetBucketInfoByBucketName retrieves a bucket by its name from the database.
// It returns information for any bucket, including deleted and private ones,
// without performing permission checks. The function returns a pointer to a
// Bucket object if found, or gorm.ErrRecordNotFound if not found. In case of database errors, an
// error is returned.
func (b *BsDBImpl) GetBucketInfoByBucketName(bucketName string) (*Bucket, error) {
var (
bucket *Bucket
err error
)
startTime := time.Now()
methodName := currentFunction()
defer func() {
if err != nil {
MetadataDatabaseFailureMetrics(err, startTime, methodName)
} else {
MetadataDatabaseSuccessMetrics(startTime, methodName)
}
}()
err = b.db.Take(&bucket, "bucket_name = ?", bucketName).Error
return bucket, err
}
// GetBucketByID get buckets info by a bucket id
func (b *BsDBImpl) GetBucketByID(bucketID int64, includePrivate bool) (*Bucket, error) {
var (
bucket *Bucket
err error
bucketIDHash common.Hash
)
startTime := time.Now()
methodName := currentFunction()
defer func() {
if err != nil {
MetadataDatabaseFailureMetrics(err, startTime, methodName)
} else {
MetadataDatabaseSuccessMetrics(startTime, methodName)
}
}()
bucketIDHash = common.BigToHash(math.NewInt(bucketID).BigInt())
if includePrivate {
err = b.db.Take(&bucket, "bucket_id = ? and removed = false", bucketIDHash).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
return bucket, err
}
err = b.db.Take(&bucket, "bucket_id = ? and visibility = ? and removed = false", bucketIDHash, types.VISIBILITY_TYPE_PUBLIC_READ.String()).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
return bucket, err
}
// GetBucketSizeByID get bucket size by a bucket id
func (b *BsDBImpl) GetBucketSizeByID(bucketID uint64) (decimal.Decimal, error) {
var (
size decimal.Decimal
err error
bucketIDHash common.Hash
)
startTime := time.Now()
methodName := currentFunction()
defer func() {
if err != nil {
MetadataDatabaseFailureMetrics(err, startTime, methodName)
} else {
MetadataDatabaseSuccessMetrics(startTime, methodName)
}
}()
bucketIDHash = common.BigToHash(math.NewUint(bucketID).BigInt())
err = b.db.Table((&Bucket{}).TableName()).
Select("storage_size").
Take(&size, "bucket_id = ? and removed = false", bucketIDHash).Error
return size, err
}
// GetUserBucketsCount get buckets count by a user address
func (b *BsDBImpl) GetUserBucketsCount(accountID common.Address, includeRemoved bool) (int64, error) {
var (
count int64
err error
)
startTime := time.Now()
methodName := currentFunction()
defer func() {
if err != nil {
MetadataDatabaseFailureMetrics(err, startTime, methodName)
} else {
MetadataDatabaseSuccessMetrics(startTime, methodName)
}
}()
if includeRemoved {
err = b.db.Table((&Bucket{}).TableName()).Select("count(1)").Take(&count, "owner = ?", accountID).Error
} else {
err = b.db.Table((&Bucket{}).TableName()).Select("count(1)").Take(&count, "owner = ? and removed = false", accountID).Error
}
return count, err
}
// ListExpiredBucketsBySp lists expired buckets
func (b *BsDBImpl) ListExpiredBucketsBySp(createAt int64, primarySpID uint32, limit int64) ([]*Bucket, error) {
var (
buckets []*Bucket
err error
)
startTime := time.Now()
methodName := currentFunction()
defer func() {
if err != nil {
MetadataDatabaseFailureMetrics(err, startTime, methodName)
} else {
MetadataDatabaseSuccessMetrics(startTime, methodName)
}
}()
if limit < 1 || limit > ExpiredBucketsDefaultSize {
limit = ExpiredBucketsDefaultSize
}
err = b.db.Raw(`
SELECT buckets.*
FROM buckets
INNER JOIN global_virtual_group_families
ON buckets.global_virtual_group_family_id = global_virtual_group_families.global_virtual_group_family_id
WHERE global_virtual_group_families.primary_sp_id = ?
AND buckets.status = 'BUCKET_STATUS_CREATED'
AND buckets.create_time < ?
AND buckets.removed = false
LIMIT ?`,
primarySpID, createAt, limit).
Find(&buckets).Error
return buckets, err
}
func (b *BsDBImpl) GetBucketMetaByName(bucketName string, includePrivate bool) (*BucketFullMeta, error) {
var (
bucketFullMeta *BucketFullMeta
err error
)
startTime := time.Now()
methodName := currentFunction()
defer func() {
if err != nil {
MetadataDatabaseFailureMetrics(err, startTime, methodName)
} else {
MetadataDatabaseSuccessMetrics(startTime, methodName)
}
}()
if includePrivate {
err = b.db.Table((&Bucket{}).TableName()).
Select("*").
Joins("left join stream_records on buckets.payment_address = stream_records.account").
Where("buckets.bucket_name = ? and buckets.removed = false", bucketName).
Take(&bucketFullMeta).Error
} else {
err = b.db.Table((&Bucket{}).TableName()).
Select("*").
Joins("left join stream_records on buckets.payment_address = stream_records.account").
Where("buckets.bucket_name = ? and buckets.removed = false and"+
"buckets.visibility='VISIBILITY_TYPE_PUBLIC_READ'", bucketName).
Take(&bucketFullMeta).Error
}
return bucketFullMeta, err
}
// ListBucketsByIDs list buckets by bucket ids
func (b *BsDBImpl) ListBucketsByIDs(ids []common.Hash, includeRemoved bool) ([]*Bucket, error) {
var (
buckets []*Bucket
err error
filters []func(*gorm.DB) *gorm.DB
)
startTime := time.Now()
methodName := currentFunction()
defer func() {
if err != nil {
MetadataDatabaseFailureMetrics(err, startTime, methodName)
} else {
MetadataDatabaseSuccessMetrics(startTime, methodName)
}
}()
if !includeRemoved {
filters = append(filters, RemovedFilter(includeRemoved))
}
err = b.db.Table((&Bucket{}).TableName()).
Select("*").
Where("bucket_id in (?)", ids).
Scopes(filters...).
Find(&buckets).Error
return buckets, err
}
func (b *BsDBImpl) ListBucketsByVgfID(vgfIDs []uint32, startAfter common.Hash, limit int) ([]*Bucket, error) {
var (
buckets []*Bucket
err error
filters []func(*gorm.DB) *gorm.DB
)
startTime := time.Now()
methodName := currentFunction()
defer func() {
if err != nil {
MetadataDatabaseFailureMetrics(err, startTime, methodName)
} else {
MetadataDatabaseSuccessMetrics(startTime, methodName)
}
}()
filters = append(filters, ObjectIDStartAfterFilter(startAfter), RemovedFilter(false), WithLimit(limit))
err = b.db.Table((&Bucket{}).TableName()).
Select("*").
Where("global_virtual_group_family_id in (?)", vgfIDs).
Scopes(filters...).
Find(&buckets).Error
return buckets, err
}