2020-11-26 14:26:53 +00:00
|
|
|
package blobovnicza
|
|
|
|
|
|
|
|
import (
|
2022-03-03 14:18:52 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util/slice"
|
2022-03-17 08:03:58 +00:00
|
|
|
apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status"
|
2022-05-31 17:00:41 +00:00
|
|
|
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
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-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:21:14 +00:00
|
|
|
func (p *GetRes) Object() []byte {
|
2020-11-26 14:26:53 +00:00
|
|
|
return p.obj
|
|
|
|
}
|
|
|
|
|
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.
|
2022-05-23 14:15:16 +00:00
|
|
|
func (b *Blobovnicza) Get(prm GetPrm) (*GetRes, error) {
|
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 {
|
|
|
|
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)),
|
|
|
|
)
|
2022-03-03 14:18:52 +00:00
|
|
|
data = slice.Copy(data)
|
2020-11-26 14:26:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return stop, nil
|
|
|
|
})
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if data == nil {
|
2022-03-17 08:03:58 +00:00
|
|
|
var errNotFound apistatus.ObjectNotFound
|
|
|
|
|
|
|
|
return nil, 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
|
|
|
|
}
|