2020-10-28 14:49:30 +00:00
|
|
|
package meta
|
|
|
|
|
|
|
|
import (
|
2020-12-08 07:51:34 +00:00
|
|
|
"fmt"
|
|
|
|
|
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-01-26 12:11:13 +00:00
|
|
|
addressSDK "github.com/nspcc-dev/neofs-sdk-go/object/address"
|
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-01-26 12:11:13 +00:00
|
|
|
addr *addressSDK.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
|
|
|
}
|
|
|
|
|
|
|
|
// WithAddress is a Get option to set the address of the requested object.
|
|
|
|
//
|
|
|
|
// Option is required.
|
2022-01-26 12:11:13 +00:00
|
|
|
func (p *GetPrm) WithAddress(addr *addressSDK.Address) *GetPrm {
|
2020-12-08 09:56:14 +00:00
|
|
|
if p != nil {
|
|
|
|
p.addr = addr
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2020-12-08 11:23:23 +00:00
|
|
|
// WithRaw is a Get option to set raw flag value. If flag is unset, then Get
|
|
|
|
// returns header of virtual object, otherwise it returns SplitInfo of virtual
|
|
|
|
// object.
|
|
|
|
func (p *GetPrm) WithRaw(raw bool) *GetPrm {
|
|
|
|
if p != nil {
|
|
|
|
p.raw = raw
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2020-12-08 09:56:14 +00:00
|
|
|
// Header returns the requested object header.
|
2022-03-03 14:19:05 +00:00
|
|
|
func (r *GetRes) Header() *objectSDK.Object {
|
2020-12-08 09:56:14 +00:00
|
|
|
return r.hdr
|
|
|
|
}
|
|
|
|
|
2020-12-08 11:23:23 +00:00
|
|
|
// Get reads the object from DB.
|
2022-03-03 14:19:05 +00:00
|
|
|
func Get(db *DB, addr *addressSDK.Address) (*objectSDK.Object, error) {
|
2020-12-08 09:56:14 +00:00
|
|
|
r, err := db.Get(new(GetPrm).WithAddress(addr))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return r.Header(), nil
|
|
|
|
}
|
|
|
|
|
2020-12-08 11:23:23 +00:00
|
|
|
// GetRaw reads physically stored object from DB.
|
2022-03-03 14:19:05 +00:00
|
|
|
func GetRaw(db *DB, addr *addressSDK.Address, raw bool) (*objectSDK.Object, error) {
|
2020-12-08 11:23:23 +00:00
|
|
|
r, err := db.Get(new(GetPrm).WithAddress(addr).WithRaw(raw))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return r.Header(), nil
|
|
|
|
}
|
|
|
|
|
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.
|
2020-12-08 09:56:14 +00:00
|
|
|
func (db *DB) Get(prm *GetPrm) (res *GetRes, err error) {
|
|
|
|
res = new(GetRes)
|
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
err = db.boltDB.View(func(tx *bbolt.Tx) error {
|
2020-12-08 11:23:23 +00:00
|
|
|
res.hdr, err = db.get(tx, prm.addr, true, prm.raw)
|
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-03-03 14:19:05 +00:00
|
|
|
func (db *DB) get(tx *bbolt.Tx, addr *addressSDK.Address, checkGraveyard, raw bool) (*objectSDK.Object, error) {
|
|
|
|
obj := objectSDK.New()
|
2020-12-08 07:51:34 +00:00
|
|
|
key := objectKey(addr.ObjectID())
|
|
|
|
cid := addr.ContainerID()
|
2020-10-29 12:03:55 +00:00
|
|
|
|
2021-09-24 15:18:04 +00:00
|
|
|
if checkGraveyard {
|
|
|
|
switch inGraveyard(tx, addr) {
|
|
|
|
case 1:
|
2022-03-17 08:03:58 +00:00
|
|
|
var errNotFound apistatus.ObjectNotFound
|
|
|
|
|
|
|
|
return nil, errNotFound
|
2021-09-24 15:18:04 +00:00
|
|
|
case 2:
|
2022-03-17 08:03:58 +00:00
|
|
|
var errRemoved apistatus.ObjectAlreadyRemoved
|
|
|
|
|
|
|
|
return nil, errRemoved
|
2021-09-24 15:18:04 +00:00
|
|
|
}
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
2020-10-29 12:03:55 +00:00
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
// check in primary index
|
|
|
|
data := getFromBucket(tx, primaryBucketName(cid), key)
|
|
|
|
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
|
|
|
|
data = getFromBucket(tx, tombstoneBucketName(cid), key)
|
|
|
|
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
|
|
|
|
data = getFromBucket(tx, storageGroupBucketName(cid), key)
|
|
|
|
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
|
|
|
|
data = getFromBucket(tx, bucketNameLockers(*cid), key)
|
|
|
|
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
|
2020-12-08 11:23:23 +00:00
|
|
|
return getVirtualObject(tx, cid, 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-03-03 14:19:05 +00:00
|
|
|
func getVirtualObject(tx *bbolt.Tx, cid *cid.ID, key []byte, raw bool) (*objectSDK.Object, error) {
|
2020-12-08 11:23:23 +00:00
|
|
|
if raw {
|
|
|
|
return nil, getSplitInfoError(tx, cid, key)
|
|
|
|
}
|
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
parentBucket := tx.Bucket(parentBucketName(cid))
|
|
|
|
if parentBucket == nil {
|
2022-03-17 08:03:58 +00:00
|
|
|
var errNotFound apistatus.ObjectNotFound
|
|
|
|
|
|
|
|
return nil, errNotFound
|
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-03-17 08:03:58 +00:00
|
|
|
var errNotFound apistatus.ObjectNotFound
|
|
|
|
|
|
|
|
return nil, errNotFound
|
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]
|
|
|
|
data := getFromBucket(tx, primaryBucketName(cid), virtualOID)
|
|
|
|
|
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-03-17 08:03:58 +00:00
|
|
|
var errNotFound apistatus.ObjectNotFound
|
|
|
|
|
|
|
|
return nil, errNotFound
|
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
|
|
|
|
2021-05-31 11:03:17 +00:00
|
|
|
func getSplitInfoError(tx *bbolt.Tx, cid *cid.ID, key []byte) error {
|
2020-12-08 11:23:23 +00:00
|
|
|
splitInfo, err := getSplitInfo(tx, cid, key)
|
|
|
|
if err == nil {
|
|
|
|
return objectSDK.NewSplitInfoError(splitInfo)
|
|
|
|
}
|
|
|
|
|
2022-03-17 08:03:58 +00:00
|
|
|
var errNotFound apistatus.ObjectNotFound
|
|
|
|
|
|
|
|
return errNotFound
|
2020-12-08 11:23:23 +00:00
|
|
|
}
|