2020-11-17 17:39:43 +00:00
|
|
|
package meta
|
|
|
|
|
|
|
|
import (
|
2020-12-08 07:51:34 +00:00
|
|
|
"errors"
|
|
|
|
"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-12-08 07:51:34 +00:00
|
|
|
"go.etcd.io/bbolt"
|
2020-11-17 17:39:43 +00:00
|
|
|
)
|
|
|
|
|
2020-12-08 09:56:14 +00:00
|
|
|
// ExistsPrm groups the parameters of Exists operation.
|
|
|
|
type ExistsPrm struct {
|
2022-01-26 12:11:13 +00:00
|
|
|
addr *addressSDK.Address
|
2020-12-08 09:56:14 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// ExistsRes groups the resulting values of Exists operation.
|
2020-12-08 09:56:14 +00:00
|
|
|
type ExistsRes struct {
|
|
|
|
exists bool
|
|
|
|
}
|
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
var ErrLackSplitInfo = errors.New("no split info on parent object")
|
|
|
|
|
2020-12-08 09:56:14 +00:00
|
|
|
// WithAddress is an Exists option to set object checked for existence.
|
2022-01-26 12:11:13 +00:00
|
|
|
func (p *ExistsPrm) WithAddress(addr *addressSDK.Address) *ExistsPrm {
|
2020-12-08 09:56:14 +00:00
|
|
|
if p != nil {
|
|
|
|
p.addr = addr
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exists returns the fact that the object is in the metabase.
|
|
|
|
func (p *ExistsRes) Exists() bool {
|
|
|
|
return p.exists
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exists checks if object is presented in DB.
|
2022-03-17 08:03:58 +00:00
|
|
|
//
|
|
|
|
// See DB.Exists docs.
|
2022-01-26 12:11:13 +00:00
|
|
|
func Exists(db *DB, addr *addressSDK.Address) (bool, error) {
|
2020-12-08 09:56:14 +00:00
|
|
|
r, err := db.Exists(new(ExistsPrm).WithAddress(addr))
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return r.Exists(), nil
|
|
|
|
}
|
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
// Exists returns ErrAlreadyRemoved if addr was marked as removed. Otherwise it
|
|
|
|
// returns true if addr is in primary index or false if it is not.
|
2022-03-17 08:03:58 +00:00
|
|
|
//
|
2022-03-17 13:26:17 +00:00
|
|
|
// 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) Exists(prm *ExistsPrm) (res *ExistsRes, err error) {
|
|
|
|
res = new(ExistsRes)
|
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
err = db.boltDB.View(func(tx *bbolt.Tx) error {
|
2020-12-08 09:56:14 +00:00
|
|
|
res.exists, err = db.exists(tx, prm.addr)
|
2020-12-08 07:51:34 +00:00
|
|
|
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
2020-12-08 09:56:14 +00:00
|
|
|
return
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
2022-01-26 12:11:13 +00:00
|
|
|
func (db *DB) exists(tx *bbolt.Tx, addr *addressSDK.Address) (exists bool, err error) {
|
2020-12-08 07:51:34 +00:00
|
|
|
// check graveyard first
|
2021-09-24 15:18:04 +00:00
|
|
|
switch inGraveyard(tx, addr) {
|
|
|
|
case 1:
|
2022-03-17 08:03:58 +00:00
|
|
|
var errNotFound apistatus.ObjectNotFound
|
|
|
|
|
|
|
|
return false, errNotFound
|
2021-09-24 15:18:04 +00:00
|
|
|
case 2:
|
2022-03-17 08:03:58 +00:00
|
|
|
var errRemoved apistatus.ObjectAlreadyRemoved
|
|
|
|
|
|
|
|
return false, errRemoved
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
obj, _ := addr.ObjectID()
|
|
|
|
objKey := objectKey(&obj)
|
|
|
|
|
|
|
|
cnr, _ := addr.ContainerID()
|
2020-12-08 07:51:34 +00:00
|
|
|
|
|
|
|
// if graveyard is empty, then check if object exists in primary bucket
|
2022-05-12 16:37:46 +00:00
|
|
|
if inBucket(tx, primaryBucketName(&cnr), objKey) {
|
2020-12-08 07:51:34 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// if primary bucket is empty, then check if object exists in parent bucket
|
2022-05-12 16:37:46 +00:00
|
|
|
if inBucket(tx, parentBucketName(&cnr), objKey) {
|
|
|
|
splitInfo, err := getSplitInfo(tx, &cnr, objKey)
|
2020-12-08 07:51:34 +00:00
|
|
|
if err != nil {
|
2020-12-08 11:23:23 +00:00
|
|
|
return false, err
|
2020-11-17 17:39:43 +00:00
|
|
|
}
|
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
return false, objectSDK.NewSplitInfoError(splitInfo)
|
|
|
|
}
|
|
|
|
|
2022-02-15 12:51:56 +00:00
|
|
|
// if parent bucket is empty, then check if object exists in typed buckets
|
2022-05-12 16:37:46 +00:00
|
|
|
return firstIrregularObjectType(tx, cnr, objKey) != objectSDK.TypeRegular, nil
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
2021-09-24 15:18:04 +00:00
|
|
|
// inGraveyard returns:
|
2021-04-08 14:19:31 +00:00
|
|
|
// * 0 if object is not marked for deletion;
|
|
|
|
// * 1 if object with GC mark;
|
|
|
|
// * 2 if object is covered with tombstone.
|
2022-01-26 12:11:13 +00:00
|
|
|
func inGraveyard(tx *bbolt.Tx, addr *addressSDK.Address) uint8 {
|
2020-12-08 07:51:34 +00:00
|
|
|
graveyard := tx.Bucket(graveyardBucketName)
|
|
|
|
if graveyard == nil {
|
2021-04-08 14:19:31 +00:00
|
|
|
// incorrect metabase state, does not make
|
|
|
|
// sense to check garbage bucket
|
2021-09-24 15:18:04 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
val := graveyard.Get(addressKey(addr))
|
|
|
|
if val == nil {
|
2021-04-08 14:19:31 +00:00
|
|
|
garbageBCK := tx.Bucket(garbageBucketName)
|
|
|
|
if garbageBCK == nil {
|
|
|
|
// incorrect node state
|
|
|
|
return 0
|
|
|
|
}
|
2020-11-17 17:39:43 +00:00
|
|
|
|
2021-04-08 14:19:31 +00:00
|
|
|
val = garbageBCK.Get(addressKey(addr))
|
|
|
|
if val != nil {
|
|
|
|
// object has been marked with GC
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// neither in the graveyard
|
|
|
|
// nor was marked with GC mark
|
|
|
|
return 0
|
2021-09-24 15:18:04 +00:00
|
|
|
}
|
2020-12-08 07:51:34 +00:00
|
|
|
|
2021-04-08 14:19:31 +00:00
|
|
|
// object in the graveyard
|
2021-09-24 15:18:04 +00:00
|
|
|
return 2
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// inBucket checks if key <key> is present in bucket <name>.
|
|
|
|
func inBucket(tx *bbolt.Tx, name, key []byte) bool {
|
|
|
|
bkt := tx.Bucket(name)
|
|
|
|
if bkt == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// using `get` as `exists`: https://github.com/boltdb/bolt/issues/321
|
|
|
|
val := bkt.Get(key)
|
|
|
|
|
|
|
|
return len(val) != 0
|
2020-11-17 17:39:43 +00:00
|
|
|
}
|
2020-12-08 11:23:23 +00:00
|
|
|
|
|
|
|
// getSplitInfo returns SplitInfo structure from root index. Returns error
|
|
|
|
// if there is no `key` record in root index.
|
2021-05-31 11:03:17 +00:00
|
|
|
func getSplitInfo(tx *bbolt.Tx, cid *cid.ID, key []byte) (*objectSDK.SplitInfo, error) {
|
2020-12-08 11:23:23 +00:00
|
|
|
rawSplitInfo := getFromBucket(tx, rootBucketName(cid), key)
|
|
|
|
if len(rawSplitInfo) == 0 {
|
|
|
|
return nil, ErrLackSplitInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
splitInfo := objectSDK.NewSplitInfo()
|
|
|
|
|
|
|
|
err := splitInfo.Unmarshal(rawSplitInfo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("can't unmarshal split info from root index: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return splitInfo, nil
|
|
|
|
}
|