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-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
|
|
|
}
|
|
|
|
|
|
|
|
// WithAddress is a Get option to set the address of the requested object.
|
|
|
|
//
|
|
|
|
// Option is required.
|
2022-05-20 16:48:14 +00:00
|
|
|
func (p *GetPrm) WithAddress(addr oid.Address) {
|
2020-12-08 09:56:14 +00:00
|
|
|
if p != nil {
|
|
|
|
p.addr = addr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
2022-05-20 16:48:14 +00:00
|
|
|
func (p *GetPrm) WithRaw(raw bool) {
|
2020-12-08 11:23:23 +00:00
|
|
|
if p != nil {
|
|
|
|
p.raw = raw
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-12-08 11:23:23 +00:00
|
|
|
// Get reads the object from DB.
|
2022-05-31 17:00:41 +00:00
|
|
|
func Get(db *DB, addr oid.Address) (*objectSDK.Object, error) {
|
2022-05-20 16:48:14 +00:00
|
|
|
var getPrm GetPrm
|
|
|
|
getPrm.WithAddress(addr)
|
|
|
|
|
|
|
|
r, err := db.Get(getPrm)
|
2020-12-08 09:56:14 +00:00
|
|
|
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-05-31 17:00:41 +00:00
|
|
|
func GetRaw(db *DB, addr oid.Address, raw bool) (*objectSDK.Object, error) {
|
2022-05-20 16:48:14 +00:00
|
|
|
var getPrm GetPrm
|
|
|
|
getPrm.WithAddress(addr)
|
|
|
|
getPrm.WithRaw(raw)
|
|
|
|
|
|
|
|
r, err := db.Get(getPrm)
|
2020-12-08 11:23:23 +00:00
|
|
|
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.
|
2022-05-31 11:43:08 +00:00
|
|
|
func (db *DB) Get(prm GetPrm) (res GetRes, err error) {
|
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-05-31 17:00:41 +00:00
|
|
|
func (db *DB) get(tx *bbolt.Tx, addr oid.Address, checkGraveyard, raw bool) (*objectSDK.Object, error) {
|
|
|
|
key := objectKey(addr.Object())
|
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
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
cnr := addr.Container()
|
2022-05-12 16:37:46 +00:00
|
|
|
obj := objectSDK.New()
|
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
// check in primary index
|
2022-05-31 17:00:41 +00:00
|
|
|
data := getFromBucket(tx, primaryBucketName(cnr), 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-05-31 17:00:41 +00:00
|
|
|
data = getFromBucket(tx, tombstoneBucketName(cnr), 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-05-31 17:00:41 +00:00
|
|
|
data = getFromBucket(tx, storageGroupBucketName(cnr), 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-05-12 16:37:46 +00:00
|
|
|
data = getFromBucket(tx, bucketNameLockers(cnr), 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-05-31 17:00:41 +00:00
|
|
|
parentBucket := tx.Bucket(parentBucketName(cnr))
|
2020-12-08 07:51:34 +00:00
|
|
|
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]
|
2022-05-31 17:00:41 +00:00
|
|
|
data := getFromBucket(tx, primaryBucketName(cnr), 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-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
|
|
|
|
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 {
|
|
|
|
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
|
|
|
}
|