2020-11-26 14:26:53 +00:00
|
|
|
package blobovnicza
|
|
|
|
|
|
|
|
import (
|
2024-03-05 13:08:49 +00:00
|
|
|
"bytes"
|
2023-05-28 19:37:37 +00:00
|
|
|
"context"
|
2021-05-18 08:12:51 +00:00
|
|
|
"fmt"
|
2023-08-16 08:12:19 +00:00
|
|
|
"math"
|
2021-05-18 08:12:51 +00:00
|
|
|
|
2023-06-20 08:24:14 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-observability/tracing"
|
2023-03-07 13:38:26 +00:00
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
2020-11-26 14:26:53 +00:00
|
|
|
"go.etcd.io/bbolt"
|
2023-06-20 08:24:14 +00:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
2020-11-26 14:26:53 +00:00
|
|
|
)
|
|
|
|
|
2023-09-27 13:25:15 +00:00
|
|
|
// iterateAllDataBuckets iterates all buckets in db
|
2023-08-16 08:12:19 +00:00
|
|
|
//
|
|
|
|
// If the maximum size of the object (b.objSizeLimit) has been changed to lower value,
|
|
|
|
// then there may be more buckets than the current limit of the object size.
|
2023-09-27 13:25:15 +00:00
|
|
|
func (b *Blobovnicza) iterateAllDataBuckets(tx *bbolt.Tx, f func(uint64, uint64, *bbolt.Bucket) (bool, error)) error {
|
2023-08-16 08:12:19 +00:00
|
|
|
return b.iterateBucketKeys(false, func(lower uint64, upper uint64, key []byte) (bool, error) {
|
2020-11-26 14:26:53 +00:00
|
|
|
buck := tx.Bucket(key)
|
|
|
|
if buck == nil {
|
2023-08-16 08:12:19 +00:00
|
|
|
return true, nil
|
2020-11-26 14:26:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return f(lower, upper, buck)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-08-16 08:12:19 +00:00
|
|
|
func (b *Blobovnicza) iterateBucketKeys(useObjLimitBound bool, f func(uint64, uint64, []byte) (bool, error)) error {
|
|
|
|
return b.iterateBounds(useObjLimitBound, func(lower, upper uint64) (bool, error) {
|
2020-11-26 14:26:53 +00:00
|
|
|
return f(lower, upper, bucketKeyFromBounds(upper))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-08-16 08:12:19 +00:00
|
|
|
func (b *Blobovnicza) iterateBounds(useObjLimitBound bool, f func(uint64, uint64) (bool, error)) error {
|
|
|
|
var objLimitBound uint64 = math.MaxUint64
|
|
|
|
if useObjLimitBound {
|
|
|
|
objLimitBound = upperPowerOfTwo(b.objSizeLimit)
|
|
|
|
}
|
2020-12-14 09:25:05 +00:00
|
|
|
|
|
|
|
for upper := firstBucketBound; upper <= max(objLimitBound, firstBucketBound); upper *= 2 {
|
2020-11-26 14:26:53 +00:00
|
|
|
var lower uint64
|
|
|
|
|
2022-01-19 13:17:36 +00:00
|
|
|
if upper != firstBucketBound {
|
2020-11-26 14:26:53 +00:00
|
|
|
lower = upper/2 + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if stop, err := f(lower, upper); err != nil {
|
|
|
|
return err
|
|
|
|
} else if stop {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-11-30 07:21:28 +00:00
|
|
|
|
2021-09-13 10:13:18 +00:00
|
|
|
// IterationElement represents a unit of elements through which Iterate operation passes.
|
|
|
|
type IterationElement struct {
|
2022-05-31 17:00:41 +00:00
|
|
|
addr oid.Address
|
2021-09-14 14:14:25 +00:00
|
|
|
|
2021-09-13 10:13:18 +00:00
|
|
|
data []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// ObjectData returns stored object in a binary representation.
|
|
|
|
func (x IterationElement) ObjectData() []byte {
|
|
|
|
return x.data
|
|
|
|
}
|
|
|
|
|
2021-09-14 14:14:25 +00:00
|
|
|
// Address returns address of the stored object.
|
2022-05-31 17:00:41 +00:00
|
|
|
func (x IterationElement) Address() oid.Address {
|
2021-09-14 14:14:25 +00:00
|
|
|
return x.addr
|
|
|
|
}
|
|
|
|
|
2021-09-13 10:13:18 +00:00
|
|
|
// IterationHandler is a generic processor of IterationElement.
|
|
|
|
type IterationHandler func(IterationElement) error
|
|
|
|
|
|
|
|
// IteratePrm groups the parameters of Iterate operation.
|
|
|
|
type IteratePrm struct {
|
2021-09-14 14:14:25 +00:00
|
|
|
decodeAddresses bool
|
|
|
|
|
|
|
|
withoutData bool
|
|
|
|
|
2021-09-13 10:13:18 +00:00
|
|
|
handler IterationHandler
|
2022-01-20 15:39:16 +00:00
|
|
|
|
|
|
|
ignoreErrors bool
|
2021-09-13 10:13:18 +00:00
|
|
|
}
|
|
|
|
|
2021-09-14 14:14:25 +00:00
|
|
|
// DecodeAddresses sets flag to unmarshal object addresses.
|
|
|
|
func (x *IteratePrm) DecodeAddresses() {
|
|
|
|
x.decodeAddresses = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithoutData sets flag to not read data of the objects.
|
|
|
|
func (x *IteratePrm) WithoutData() {
|
|
|
|
x.withoutData = true
|
|
|
|
}
|
|
|
|
|
2021-09-13 10:13:18 +00:00
|
|
|
// SetHandler sets handler to be called iteratively.
|
|
|
|
func (x *IteratePrm) SetHandler(h IterationHandler) {
|
|
|
|
x.handler = h
|
|
|
|
}
|
|
|
|
|
2022-01-20 15:39:16 +00:00
|
|
|
// IgnoreErrors makes all errors to be ignored.
|
|
|
|
func (x *IteratePrm) IgnoreErrors() {
|
|
|
|
x.ignoreErrors = true
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// IterateRes groups the resulting values of Iterate operation.
|
2023-10-31 11:56:55 +00:00
|
|
|
type IterateRes struct{}
|
2021-09-13 10:13:18 +00:00
|
|
|
|
2021-09-14 14:14:25 +00:00
|
|
|
// Iterate goes through all stored objects, and passes IterationElement to parameterized handler until error return.
|
|
|
|
//
|
|
|
|
// Decodes object addresses if DecodeAddresses was called. Don't read object data if WithoutData was called.
|
2021-09-13 10:13:18 +00:00
|
|
|
//
|
|
|
|
// Returns handler's errors directly. Returns nil after iterating finish.
|
|
|
|
//
|
|
|
|
// Handler should not retain object data. Handler must not be nil.
|
2023-05-28 19:37:37 +00:00
|
|
|
func (b *Blobovnicza) Iterate(ctx context.Context, prm IteratePrm) (IterateRes, error) {
|
2023-06-20 08:24:14 +00:00
|
|
|
ctx, span := tracing.StartSpanFromContext(ctx, "Blobovnicza.Iterate",
|
|
|
|
trace.WithAttributes(
|
|
|
|
attribute.String("path", b.path),
|
|
|
|
attribute.Bool("decode_addresses", prm.decodeAddresses),
|
|
|
|
attribute.Bool("without_data", prm.withoutData),
|
|
|
|
attribute.Bool("ignore_errors", prm.ignoreErrors),
|
|
|
|
))
|
|
|
|
defer span.End()
|
|
|
|
|
2024-08-15 12:39:22 +00:00
|
|
|
b.controlMtx.RLock()
|
|
|
|
defer b.controlMtx.RUnlock()
|
|
|
|
|
2021-09-13 10:13:18 +00:00
|
|
|
var elem IterationElement
|
|
|
|
|
|
|
|
if err := b.boltDB.View(func(tx *bbolt.Tx) error {
|
2023-09-27 13:25:15 +00:00
|
|
|
return tx.ForEach(func(bucketName []byte, buck *bbolt.Bucket) error {
|
2023-10-12 12:25:16 +00:00
|
|
|
if isNonDataBucket(bucketName) {
|
2023-09-27 13:25:15 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-09-14 14:32:05 +00:00
|
|
|
return buck.ForEach(func(k, v []byte) error {
|
2023-05-28 19:37:37 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
default:
|
|
|
|
}
|
2021-09-14 14:14:25 +00:00
|
|
|
if prm.decodeAddresses {
|
2022-05-31 17:00:41 +00:00
|
|
|
if err := addressFromKey(&elem.addr, k); err != nil {
|
2022-01-20 15:39:16 +00:00
|
|
|
if prm.ignoreErrors {
|
|
|
|
return nil
|
|
|
|
}
|
2021-09-14 14:14:25 +00:00
|
|
|
return fmt.Errorf("could not decode address key: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !prm.withoutData {
|
2024-03-05 13:08:49 +00:00
|
|
|
elem.data = bytes.Clone(v)
|
2021-09-14 14:14:25 +00:00
|
|
|
}
|
|
|
|
|
2021-09-13 10:13:18 +00:00
|
|
|
return prm.handler(elem)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}); err != nil {
|
2022-05-31 12:22:32 +00:00
|
|
|
return IterateRes{}, err
|
2021-09-13 10:13:18 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 12:22:32 +00:00
|
|
|
return IterateRes{}, nil
|
2021-09-13 10:13:18 +00:00
|
|
|
}
|
|
|
|
|
2021-09-14 14:14:25 +00:00
|
|
|
// IterateAddresses is a helper function which iterates over Blobovnicza and passes addresses of the objects to f.
|
2023-05-28 19:37:37 +00:00
|
|
|
func IterateAddresses(ctx context.Context, blz *Blobovnicza, f func(oid.Address) error) error {
|
2021-09-14 14:14:25 +00:00
|
|
|
var prm IteratePrm
|
|
|
|
|
|
|
|
prm.DecodeAddresses()
|
|
|
|
prm.WithoutData()
|
|
|
|
|
|
|
|
prm.SetHandler(func(elem IterationElement) error {
|
|
|
|
return f(elem.Address())
|
|
|
|
})
|
|
|
|
|
2023-05-28 19:37:37 +00:00
|
|
|
_, err := blz.Iterate(ctx, prm)
|
2021-09-14 14:14:25 +00:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|