2020-10-28 14:49:30 +00:00
|
|
|
package meta
|
|
|
|
|
|
|
|
import (
|
2020-12-08 07:51:34 +00:00
|
|
|
"fmt"
|
|
|
|
|
2022-10-26 12:23:12 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/util/logicerr"
|
2022-03-17 08:03:58 +00:00
|
|
|
apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status"
|
2021-11-10 07:08:33 +00:00
|
|
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
|
|
|
objectSDK "github.com/nspcc-dev/neofs-sdk-go/object"
|
2022-05-31 17:00:41 +00:00
|
|
|
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
2020-10-28 14:49:30 +00:00
|
|
|
"go.etcd.io/bbolt"
|
|
|
|
)
|
|
|
|
|
2020-12-08 09:56:14 +00:00
|
|
|
// GetPrm groups the parameters of Get operation.
|
|
|
|
type GetPrm struct {
|
2022-05-31 17:00:41 +00:00
|
|
|
addr oid.Address
|
2020-12-08 11:23:23 +00:00
|
|
|
raw bool
|
2020-12-08 09:56:14 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// GetRes groups the resulting values of Get operation.
|
2020-12-08 09:56:14 +00:00
|
|
|
type GetRes struct {
|
2022-03-03 14:19:05 +00:00
|
|
|
hdr *objectSDK.Object
|
2020-12-08 09:56:14 +00:00
|
|
|
}
|
|
|
|
|
2022-07-12 14:59:37 +00:00
|
|
|
// SetAddress is a Get option to set the address of the requested object.
|
2020-12-08 09:56:14 +00:00
|
|
|
//
|
|
|
|
// Option is required.
|
2022-07-12 14:59:37 +00:00
|
|
|
func (p *GetPrm) SetAddress(addr oid.Address) {
|
|
|
|
p.addr = addr
|
2020-12-08 09:56:14 +00:00
|
|
|
}
|
|
|
|
|
2022-07-12 14:59:37 +00:00
|
|
|
// SetRaw is a Get option to set raw flag value. If flag is unset, then Get
|
2020-12-08 11:23:23 +00:00
|
|
|
// returns header of virtual object, otherwise it returns SplitInfo of virtual
|
|
|
|
// object.
|
2022-07-12 14:59:37 +00:00
|
|
|
func (p *GetPrm) SetRaw(raw bool) {
|
|
|
|
p.raw = raw
|
2020-12-08 11:23:23 +00:00
|
|
|
}
|
|
|
|
|
2020-12-08 09:56:14 +00:00
|
|
|
// Header returns the requested object header.
|
2022-05-31 11:43:08 +00:00
|
|
|
func (r GetRes) Header() *objectSDK.Object {
|
2020-12-08 09:56:14 +00:00
|
|
|
return r.hdr
|
|
|
|
}
|
|
|
|
|
2020-10-28 14:49:30 +00:00
|
|
|
// Get returns object header for specified address.
|
2022-03-17 08:03:58 +00:00
|
|
|
//
|
2022-03-17 13:26:17 +00:00
|
|
|
// Returns an error of type apistatus.ObjectNotFound if object is missing in DB.
|
|
|
|
// Returns an error of type apistatus.ObjectAlreadyRemoved if object has been placed in graveyard.
|
2022-07-27 18:38:28 +00:00
|
|
|
// Returns the object.ErrObjectIsExpired if the object is presented but already expired.
|
2022-05-31 11:43:08 +00:00
|
|
|
func (db *DB) Get(prm GetPrm) (res GetRes, err error) {
|
2022-11-15 12:43:38 +00:00
|
|
|
db.modeMtx.RLock()
|
|
|
|
defer db.modeMtx.RUnlock()
|
|
|
|
|
|
|
|
if db.mode.NoMetabase() {
|
|
|
|
return res, ErrDegradedMode
|
|
|
|
}
|
2022-07-21 13:26:25 +00:00
|
|
|
|
2022-07-27 18:38:28 +00:00
|
|
|
currEpoch := db.epochState.CurrentEpoch()
|
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
err = db.boltDB.View(func(tx *bbolt.Tx) error {
|
2022-09-08 11:54:21 +00:00
|
|
|
key := make([]byte, addressKeySize)
|
|
|
|
res.hdr, err = db.get(tx, prm.addr, key, true, prm.raw, currEpoch)
|
2020-12-08 07:51:34 +00:00
|
|
|
|
|
|
|
return err
|
|
|
|
})
|
2020-10-28 14:49:30 +00:00
|
|
|
|
2020-12-08 09:56:14 +00:00
|
|
|
return
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 11:54:21 +00:00
|
|
|
func (db *DB) get(tx *bbolt.Tx, addr oid.Address, key []byte, checkStatus, raw bool, currEpoch uint64) (*objectSDK.Object, error) {
|
2022-07-27 18:38:28 +00:00
|
|
|
if checkStatus {
|
|
|
|
switch objectStatus(tx, addr, currEpoch) {
|
2021-09-24 15:18:04 +00:00
|
|
|
case 1:
|
2022-10-26 12:23:12 +00:00
|
|
|
return nil, logicerr.Wrap(apistatus.ObjectNotFound{})
|
2021-09-24 15:18:04 +00:00
|
|
|
case 2:
|
2022-10-26 12:23:12 +00:00
|
|
|
return nil, logicerr.Wrap(apistatus.ObjectAlreadyRemoved{})
|
2022-07-27 18:38:28 +00:00
|
|
|
case 3:
|
2022-10-31 07:08:30 +00:00
|
|
|
return nil, ErrObjectIsExpired
|
2021-09-24 15:18:04 +00:00
|
|
|
}
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
2020-10-29 12:03:55 +00:00
|
|
|
|
2022-09-08 11:54:21 +00:00
|
|
|
key = objectKey(addr.Object(), key)
|
2022-05-31 17:00:41 +00:00
|
|
|
cnr := addr.Container()
|
2022-05-12 16:37:46 +00:00
|
|
|
obj := objectSDK.New()
|
2022-09-08 11:54:21 +00:00
|
|
|
bucketName := make([]byte, bucketKeySize)
|
2022-05-12 16:37:46 +00:00
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
// check in primary index
|
2022-09-08 11:54:21 +00:00
|
|
|
data := getFromBucket(tx, primaryBucketName(cnr, bucketName), key)
|
2020-12-08 07:51:34 +00:00
|
|
|
if len(data) != 0 {
|
|
|
|
return obj, obj.Unmarshal(data)
|
|
|
|
}
|
2020-10-28 14:49:30 +00:00
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
// if not found then check in tombstone index
|
2022-09-08 11:54:21 +00:00
|
|
|
data = getFromBucket(tx, tombstoneBucketName(cnr, bucketName), key)
|
2020-12-08 07:51:34 +00:00
|
|
|
if len(data) != 0 {
|
|
|
|
return obj, obj.Unmarshal(data)
|
|
|
|
}
|
2020-10-28 14:49:30 +00:00
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
// if not found then check in storage group index
|
2022-09-08 11:54:21 +00:00
|
|
|
data = getFromBucket(tx, storageGroupBucketName(cnr, bucketName), key)
|
2020-12-08 07:51:34 +00:00
|
|
|
if len(data) != 0 {
|
|
|
|
return obj, obj.Unmarshal(data)
|
|
|
|
}
|
2020-10-28 14:49:30 +00:00
|
|
|
|
2022-02-15 12:51:56 +00:00
|
|
|
// if not found then check in locker index
|
2022-09-08 11:54:21 +00:00
|
|
|
data = getFromBucket(tx, bucketNameLockers(cnr, bucketName), key)
|
2022-02-15 12:51:56 +00:00
|
|
|
if len(data) != 0 {
|
|
|
|
return obj, obj.Unmarshal(data)
|
|
|
|
}
|
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
// if not found then check if object is a virtual
|
2022-05-31 17:00:41 +00:00
|
|
|
return getVirtualObject(tx, cnr, key, raw)
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
2020-11-16 09:43:52 +00:00
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
func getFromBucket(tx *bbolt.Tx, name, key []byte) []byte {
|
|
|
|
bkt := tx.Bucket(name)
|
|
|
|
if bkt == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2020-10-28 14:49:30 +00:00
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
return bkt.Get(key)
|
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
func getVirtualObject(tx *bbolt.Tx, cnr cid.ID, key []byte, raw bool) (*objectSDK.Object, error) {
|
2020-12-08 11:23:23 +00:00
|
|
|
if raw {
|
2022-05-31 17:00:41 +00:00
|
|
|
return nil, getSplitInfoError(tx, cnr, key)
|
2020-12-08 11:23:23 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 11:54:21 +00:00
|
|
|
bucketName := make([]byte, bucketKeySize)
|
|
|
|
parentBucket := tx.Bucket(parentBucketName(cnr, bucketName))
|
2020-12-08 07:51:34 +00:00
|
|
|
if parentBucket == nil {
|
2022-10-26 12:23:12 +00:00
|
|
|
return nil, logicerr.Wrap(apistatus.ObjectNotFound{})
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
relativeLst, err := decodeList(parentBucket.Get(key))
|
|
|
|
if err != nil {
|
2020-10-28 14:49:30 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
if len(relativeLst) == 0 { // this should never happen though
|
2022-10-26 12:23:12 +00:00
|
|
|
return nil, logicerr.Wrap(apistatus.ObjectNotFound{})
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// pick last item, for now there is not difference which address to pick
|
|
|
|
// but later list might be sorted so first or last value can be more
|
|
|
|
// prioritized to choose
|
|
|
|
virtualOID := relativeLst[len(relativeLst)-1]
|
2022-09-08 11:54:21 +00:00
|
|
|
data := getFromBucket(tx, primaryBucketName(cnr, bucketName), virtualOID)
|
2020-12-08 07:51:34 +00:00
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
child := objectSDK.New()
|
2020-12-08 07:51:34 +00:00
|
|
|
|
|
|
|
err = child.Unmarshal(data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("can't unmarshal child with parent: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
par := child.Parent()
|
|
|
|
|
|
|
|
if par == nil { // this should never happen though
|
2022-10-26 12:23:12 +00:00
|
|
|
return nil, logicerr.Wrap(apistatus.ObjectNotFound{})
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
return par, nil
|
2020-10-28 14:49:30 +00:00
|
|
|
}
|
2020-12-08 11:23:23 +00:00
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
func getSplitInfoError(tx *bbolt.Tx, cnr cid.ID, key []byte) error {
|
|
|
|
splitInfo, err := getSplitInfo(tx, cnr, key)
|
2020-12-08 11:23:23 +00:00
|
|
|
if err == nil {
|
2022-10-26 12:23:12 +00:00
|
|
|
return logicerr.Wrap(objectSDK.NewSplitInfoError(splitInfo))
|
2020-12-08 11:23:23 +00:00
|
|
|
}
|
|
|
|
|
2022-10-26 12:23:12 +00:00
|
|
|
return logicerr.Wrap(apistatus.ObjectNotFound{})
|
2020-12-08 11:23:23 +00:00
|
|
|
}
|