frostfs-node/pkg/local_object_storage/blobovnicza/get.go
Dmitrii Stepanov 0920d848d0 [] get-object: Add tracing spans
Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
2023-04-12 06:52:00 +00:00

82 lines
1.9 KiB
Go

package blobovnicza
import (
"context"
"errors"
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/pkg/tracing"
apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
"github.com/nspcc-dev/neo-go/pkg/util/slice"
"go.etcd.io/bbolt"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
)
// GetPrm groups the parameters of Get operation.
type GetPrm struct {
addr oid.Address
}
// GetRes groups the resulting values of Get operation.
type GetRes struct {
obj []byte
}
// SetAddress sets the address of the requested object.
func (p *GetPrm) SetAddress(addr oid.Address) {
p.addr = addr
}
// Object returns binary representation of the requested object.
func (p GetRes) Object() []byte {
return p.obj
}
// special error for normal bbolt.Tx.ForEach interruption.
var errInterruptForEach = errors.New("interrupt for-each")
// Get reads an object from Blobovnicza by address.
//
// Returns any error encountered that
// did not allow to completely read the object.
//
// Returns an error of type apistatus.ObjectNotFound if the requested object is not
// presented in Blobovnicza.
func (b *Blobovnicza) Get(ctx context.Context, prm GetPrm) (GetRes, error) {
_, span := tracing.StartSpanFromContext(ctx, "Blobovnicza.Get",
trace.WithAttributes(
attribute.String("address", prm.addr.EncodeToString()),
))
defer span.End()
var (
data []byte
addrKey = addressKey(prm.addr)
)
if err := b.boltDB.View(func(tx *bbolt.Tx) error {
return tx.ForEach(func(_ []byte, buck *bbolt.Bucket) error {
data = buck.Get(addrKey)
if data == nil {
return nil
}
data = slice.Copy(data)
return errInterruptForEach
})
}); err != nil && err != errInterruptForEach {
return GetRes{}, err
}
if data == nil {
var errNotFound apistatus.ObjectNotFound
return GetRes{}, errNotFound
}
return GetRes{
obj: data,
}, nil
}