2020-11-26 14:26:53 +00:00
|
|
|
package blobovnicza
|
|
|
|
|
|
|
|
import (
|
2023-03-13 11:37:35 +00:00
|
|
|
"context"
|
2022-09-07 16:40:57 +00:00
|
|
|
"errors"
|
|
|
|
|
2023-03-13 11:37:35 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/pkg/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"
|
2022-03-03 14:18:52 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util/slice"
|
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(
|
|
|
|
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 {
|
2022-09-07 16:40:57 +00:00
|
|
|
return tx.ForEach(func(_ []byte, buck *bbolt.Bucket) error {
|
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
|
|
|
}
|
|
|
|
|
2022-09-07 16:40:57 +00:00
|
|
|
data = slice.Copy(data)
|
|
|
|
|
|
|
|
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 {
|
2022-03-17 08:03:58 +00:00
|
|
|
var errNotFound apistatus.ObjectNotFound
|
|
|
|
|
2022-05-31 12:22:32 +00:00
|
|
|
return GetRes{}, errNotFound
|
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
|
|
|
|
}
|