2020-11-26 14:26:53 +00:00
|
|
|
package blobovnicza
|
|
|
|
|
|
|
|
import (
|
2023-11-17 07:58:04 +00:00
|
|
|
"bytes"
|
2023-03-13 11:37:35 +00:00
|
|
|
"context"
|
2022-09-07 16:40:57 +00:00
|
|
|
"errors"
|
|
|
|
|
2023-05-31 09:24:04 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-observability/tracing"
|
2023-03-07 13:38:26 +00:00
|
|
|
apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
2020-11-26 14:26:53 +00:00
|
|
|
"go.etcd.io/bbolt"
|
2023-03-13 11:37:35 +00:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
2020-11-26 14:26:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetPrm groups the parameters of Get operation.
|
|
|
|
type GetPrm struct {
|
2022-05-31 17:00:41 +00:00
|
|
|
addr oid.Address
|
2020-11-26 14:26:53 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// GetRes groups the resulting values of Get operation.
|
2020-11-26 14:26:53 +00:00
|
|
|
type GetRes struct {
|
2020-12-02 09:58:42 +00:00
|
|
|
obj []byte
|
2020-11-26 14:26:53 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// SetAddress sets the address of the requested object.
|
2022-05-31 17:00:41 +00:00
|
|
|
func (p *GetPrm) SetAddress(addr oid.Address) {
|
2020-11-26 14:26:53 +00:00
|
|
|
p.addr = addr
|
|
|
|
}
|
|
|
|
|
2020-12-02 09:58:42 +00:00
|
|
|
// Object returns binary representation of the requested object.
|
2022-05-23 14:15:16 +00:00
|
|
|
func (p GetRes) Object() []byte {
|
2020-11-26 14:26:53 +00:00
|
|
|
return p.obj
|
|
|
|
}
|
|
|
|
|
2022-09-07 16:40:57 +00:00
|
|
|
// special error for normal bbolt.Tx.ForEach interruption.
|
|
|
|
var errInterruptForEach = errors.New("interrupt for-each")
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// Get reads an object from Blobovnicza by address.
|
2020-11-26 14:26:53 +00:00
|
|
|
//
|
|
|
|
// Returns any error encountered that
|
|
|
|
// did not allow to completely read the object.
|
2020-11-30 16:39:05 +00:00
|
|
|
//
|
2022-04-21 11:28:05 +00:00
|
|
|
// Returns an error of type apistatus.ObjectNotFound if the requested object is not
|
2020-11-30 16:39:05 +00:00
|
|
|
// presented in Blobovnicza.
|
2023-03-13 11:37:35 +00:00
|
|
|
func (b *Blobovnicza) Get(ctx context.Context, prm GetPrm) (GetRes, error) {
|
|
|
|
_, span := tracing.StartSpanFromContext(ctx, "Blobovnicza.Get",
|
|
|
|
trace.WithAttributes(
|
2023-06-20 08:24:14 +00:00
|
|
|
attribute.String("path", b.path),
|
2023-03-13 11:37:35 +00:00
|
|
|
attribute.String("address", prm.addr.EncodeToString()),
|
|
|
|
))
|
|
|
|
defer span.End()
|
|
|
|
|
2020-11-26 14:26:53 +00:00
|
|
|
var (
|
|
|
|
data []byte
|
|
|
|
addrKey = addressKey(prm.addr)
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-11-26 14:26:53 +00:00
|
|
|
data = buck.Get(addrKey)
|
2022-09-07 16:40:57 +00:00
|
|
|
if data == nil {
|
|
|
|
return nil
|
2020-11-26 14:26:53 +00:00
|
|
|
}
|
|
|
|
|
2023-11-17 07:58:04 +00:00
|
|
|
data = bytes.Clone(data)
|
2022-09-07 16:40:57 +00:00
|
|
|
|
|
|
|
return errInterruptForEach
|
2020-11-26 14:26:53 +00:00
|
|
|
})
|
2022-09-07 16:40:57 +00:00
|
|
|
}); err != nil && err != errInterruptForEach {
|
2022-05-31 12:22:32 +00:00
|
|
|
return GetRes{}, err
|
2020-11-26 14:26:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if data == nil {
|
2023-08-04 11:14:07 +00:00
|
|
|
return GetRes{}, new(apistatus.ObjectNotFound)
|
2020-11-26 14:26:53 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 12:22:32 +00:00
|
|
|
return GetRes{
|
2020-12-02 09:58:42 +00:00
|
|
|
obj: data,
|
2020-11-26 14:26:53 +00:00
|
|
|
}, nil
|
|
|
|
}
|