2020-11-26 14:26:53 +00:00
|
|
|
package blobovnicza
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/object"
|
2022-01-26 12:11:13 +00:00
|
|
|
addressSDK "github.com/nspcc-dev/neofs-sdk-go/object/address"
|
2020-11-26 14:26:53 +00:00
|
|
|
"go.etcd.io/bbolt"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetPrm groups the parameters of Get operation.
|
|
|
|
type GetPrm struct {
|
2022-01-26 12:11:13 +00:00
|
|
|
addr *addressSDK.Address
|
2020-11-26 14:26:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetRes groups resulting values of Get operation.
|
|
|
|
type GetRes struct {
|
2020-12-02 09:58:42 +00:00
|
|
|
obj []byte
|
2020-11-26 14:26:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetAddress sets address of the requested object.
|
2022-01-26 12:11:13 +00:00
|
|
|
func (p *GetPrm) SetAddress(addr *addressSDK.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.
|
|
|
|
func (p *GetRes) Object() []byte {
|
2020-11-26 14:26:53 +00:00
|
|
|
return p.obj
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get reads the object from Blobovnicza by address.
|
|
|
|
//
|
|
|
|
// Returns any error encountered that
|
|
|
|
// did not allow to completely read the object.
|
2020-11-30 16:39:05 +00:00
|
|
|
//
|
|
|
|
// Returns ErrNotFound if requested object is not
|
|
|
|
// presented in Blobovnicza.
|
2020-11-26 14:26:53 +00:00
|
|
|
func (b *Blobovnicza) Get(prm *GetPrm) (*GetRes, error) {
|
|
|
|
var (
|
|
|
|
data []byte
|
|
|
|
addrKey = addressKey(prm.addr)
|
|
|
|
)
|
|
|
|
|
|
|
|
if err := b.boltDB.View(func(tx *bbolt.Tx) error {
|
|
|
|
return b.iterateBuckets(tx, func(lower, upper uint64, buck *bbolt.Bucket) (bool, error) {
|
|
|
|
data = buck.Get(addrKey)
|
|
|
|
|
|
|
|
stop := data != nil
|
|
|
|
|
|
|
|
if stop {
|
|
|
|
b.log.Debug("object is found in bucket",
|
|
|
|
zap.String("binary size", stringifyByteSize(uint64(len(data)))),
|
|
|
|
zap.String("range", stringifyBounds(lower, upper)),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return stop, nil
|
|
|
|
})
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if data == nil {
|
2020-11-30 16:39:05 +00:00
|
|
|
return nil, object.ErrNotFound
|
2020-11-26 14:26:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &GetRes{
|
2020-12-02 09:58:42 +00:00
|
|
|
obj: data,
|
2020-11-26 14:26:53 +00:00
|
|
|
}, nil
|
|
|
|
}
|