2020-11-17 17:39:43 +00:00
|
|
|
package meta
|
|
|
|
|
|
|
|
import (
|
2020-12-08 07:51:34 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2021-05-31 11:03:17 +00:00
|
|
|
cid "github.com/nspcc-dev/neofs-api-go/pkg/container/id"
|
2020-11-30 16:39:05 +00:00
|
|
|
objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/object"
|
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 {
|
|
|
|
addr *objectSDK.Address
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExistsRes groups resulting values of Exists operation.
|
|
|
|
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.
|
|
|
|
func (p *ExistsPrm) WithAddress(addr *objectSDK.Address) *ExistsPrm {
|
|
|
|
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.
|
|
|
|
func Exists(db *DB, addr *objectSDK.Address) (bool, error) {
|
|
|
|
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.
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DB) exists(tx *bbolt.Tx, addr *objectSDK.Address) (exists bool, err error) {
|
|
|
|
// check graveyard first
|
|
|
|
if inGraveyard(tx, addr) {
|
|
|
|
return false, object.ErrAlreadyRemoved
|
|
|
|
}
|
|
|
|
|
|
|
|
objKey := objectKey(addr.ObjectID())
|
|
|
|
|
|
|
|
// if graveyard is empty, then check if object exists in primary bucket
|
|
|
|
if inBucket(tx, primaryBucketName(addr.ContainerID()), objKey) {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// if primary bucket is empty, then check if object exists in parent bucket
|
|
|
|
if inBucket(tx, parentBucketName(addr.ContainerID()), objKey) {
|
2020-12-08 11:23:23 +00:00
|
|
|
splitInfo, err := getSplitInfo(tx, addr.ContainerID(), 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// if parent bucket is empty, then check if object exists in tombstone bucket
|
|
|
|
if inBucket(tx, tombstoneBucketName(addr.ContainerID()), objKey) {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// if parent bucket is empty, then check if object exists in storage group bucket
|
|
|
|
return inBucket(tx, storageGroupBucketName(addr.ContainerID()), objKey), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// inGraveyard returns true if object was marked as removed.
|
|
|
|
func inGraveyard(tx *bbolt.Tx, addr *objectSDK.Address) bool {
|
|
|
|
graveyard := tx.Bucket(graveyardBucketName)
|
|
|
|
if graveyard == nil {
|
|
|
|
return false
|
2020-11-17 17:39:43 +00:00
|
|
|
}
|
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
tombstone := graveyard.Get(addressKey(addr))
|
|
|
|
|
|
|
|
return len(tombstone) != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|