2020-10-29 12:03:55 +00:00
|
|
|
package meta
|
|
|
|
|
|
|
|
import (
|
2020-12-08 07:51:34 +00:00
|
|
|
"bytes"
|
2021-05-18 08:12:51 +00:00
|
|
|
"errors"
|
2020-12-08 07:51:34 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/object"
|
2022-03-02 09:17:32 +00:00
|
|
|
storagelog "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/internal/log"
|
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
|
|
|
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-29 12:03:55 +00:00
|
|
|
"go.etcd.io/bbolt"
|
|
|
|
)
|
|
|
|
|
2020-12-08 09:56:14 +00:00
|
|
|
// DeletePrm groups the parameters of Delete operation.
|
|
|
|
type DeletePrm struct {
|
2022-01-26 12:11:13 +00:00
|
|
|
addrs []*addressSDK.Address
|
2020-12-08 09:56:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteRes groups resulting values of Delete operation.
|
|
|
|
type DeleteRes struct{}
|
|
|
|
|
|
|
|
// WithAddresses is a Delete option to set the addresses of the objects to delete.
|
|
|
|
//
|
|
|
|
// Option is required.
|
2022-01-26 12:11:13 +00:00
|
|
|
func (p *DeletePrm) WithAddresses(addrs ...*addressSDK.Address) *DeletePrm {
|
2020-12-08 09:56:14 +00:00
|
|
|
if p != nil {
|
|
|
|
p.addrs = addrs
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete removes objects from DB.
|
2022-01-26 12:11:13 +00:00
|
|
|
func Delete(db *DB, addrs ...*addressSDK.Address) error {
|
2020-12-08 09:56:14 +00:00
|
|
|
_, err := db.Delete(new(DeletePrm).WithAddresses(addrs...))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-02-11 17:39:48 +00:00
|
|
|
type referenceNumber struct {
|
|
|
|
all, cur int
|
|
|
|
|
2022-01-26 12:11:13 +00:00
|
|
|
addr *addressSDK.Address
|
2021-02-11 17:39:48 +00:00
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
obj *objectSDK.Object
|
2021-02-11 17:39:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type referenceCounter map[string]*referenceNumber
|
|
|
|
|
2020-12-08 11:23:23 +00:00
|
|
|
// Delete removed object records from metabase indexes.
|
2020-12-08 09:56:14 +00:00
|
|
|
func (db *DB) Delete(prm *DeletePrm) (*DeleteRes, error) {
|
2022-03-02 09:17:32 +00:00
|
|
|
err := db.boltDB.Update(func(tx *bbolt.Tx) error {
|
2021-02-11 17:39:48 +00:00
|
|
|
return db.deleteGroup(tx, prm.addrs)
|
|
|
|
})
|
2022-03-02 09:17:32 +00:00
|
|
|
if err == nil {
|
|
|
|
for i := range prm.addrs {
|
|
|
|
storagelog.Write(db.log,
|
|
|
|
storagelog.AddressField(prm.addrs[i]),
|
|
|
|
storagelog.OpField("metabase DELETE"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return new(DeleteRes), err
|
2021-02-11 17:39:48 +00:00
|
|
|
}
|
|
|
|
|
2022-01-26 12:11:13 +00:00
|
|
|
func (db *DB) deleteGroup(tx *bbolt.Tx, addrs []*addressSDK.Address) error {
|
2021-02-11 17:39:48 +00:00
|
|
|
refCounter := make(referenceCounter, len(addrs))
|
|
|
|
|
|
|
|
for i := range addrs {
|
|
|
|
err := db.delete(tx, addrs[i], refCounter)
|
|
|
|
if err != nil {
|
|
|
|
return err // maybe log and continue?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, refNum := range refCounter {
|
|
|
|
if refNum.cur == refNum.all {
|
|
|
|
err := db.deleteObject(tx, refNum.obj, true)
|
2020-12-08 07:51:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err // maybe log and continue?
|
|
|
|
}
|
|
|
|
}
|
2021-02-11 17:39:48 +00:00
|
|
|
}
|
2020-12-08 07:51:34 +00:00
|
|
|
|
2021-02-11 17:39:48 +00:00
|
|
|
return nil
|
2020-11-09 10:09:09 +00:00
|
|
|
}
|
|
|
|
|
2022-01-26 12:11:13 +00:00
|
|
|
func (db *DB) delete(tx *bbolt.Tx, addr *addressSDK.Address, refCounter referenceCounter) error {
|
2021-04-06 14:07:42 +00:00
|
|
|
// remove record from graveyard
|
|
|
|
graveyard := tx.Bucket(graveyardBucketName)
|
|
|
|
if graveyard != nil {
|
|
|
|
err := graveyard.Delete(addressKey(addr))
|
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return fmt.Errorf("could not remove from graveyard: %w", err)
|
2021-04-06 14:07:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-08 11:23:23 +00:00
|
|
|
// unmarshal object, work only with physically stored (raw == true) objects
|
|
|
|
obj, err := db.get(tx, addr, false, true)
|
2020-12-08 07:51:34 +00:00
|
|
|
if err != nil {
|
2022-03-17 08:03:58 +00:00
|
|
|
if errors.As(err, new(apistatus.ObjectNotFound)) {
|
2021-04-06 14:07:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// if object is an only link to a parent, then remove parent
|
2022-03-03 14:19:05 +00:00
|
|
|
if parent := obj.Parent(); parent != nil {
|
|
|
|
parAddr := object.AddressOf(parent)
|
2021-02-11 17:39:48 +00:00
|
|
|
sParAddr := parAddr.String()
|
|
|
|
|
|
|
|
nRef, ok := refCounter[sParAddr]
|
|
|
|
if !ok {
|
|
|
|
nRef = &referenceNumber{
|
2022-03-03 14:19:05 +00:00
|
|
|
all: parentLength(tx, parAddr),
|
2021-02-11 17:39:48 +00:00
|
|
|
addr: parAddr,
|
|
|
|
obj: parent,
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
2021-02-11 17:39:48 +00:00
|
|
|
|
|
|
|
refCounter[sParAddr] = nRef
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
2021-02-11 17:39:48 +00:00
|
|
|
|
|
|
|
nRef.cur++
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// remove object
|
2021-02-11 17:39:48 +00:00
|
|
|
return db.deleteObject(tx, obj, false)
|
2020-11-09 10:09:09 +00:00
|
|
|
}
|
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
func (db *DB) deleteObject(
|
|
|
|
tx *bbolt.Tx,
|
2022-03-03 14:19:05 +00:00
|
|
|
obj *objectSDK.Object,
|
2020-12-08 07:51:34 +00:00
|
|
|
isParent bool,
|
|
|
|
) error {
|
2022-03-21 13:59:05 +00:00
|
|
|
err := delUniqueIndexes(tx, obj, isParent)
|
2020-12-08 07:51:34 +00:00
|
|
|
if err != nil {
|
2022-03-21 13:59:05 +00:00
|
|
|
return fmt.Errorf("can't remove unique indexes")
|
2020-11-09 10:09:09 +00:00
|
|
|
}
|
2020-12-08 07:51:34 +00:00
|
|
|
|
2022-03-21 13:59:05 +00:00
|
|
|
err = updateListIndexes(tx, obj, delListIndexItem)
|
2020-12-08 07:51:34 +00:00
|
|
|
if err != nil {
|
2022-03-21 13:59:05 +00:00
|
|
|
return fmt.Errorf("can't remove list indexes: %w", err)
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
2022-03-21 13:59:05 +00:00
|
|
|
err = updateFKBTIndexes(tx, obj, delFKBTIndexItem)
|
2020-12-08 07:51:34 +00:00
|
|
|
if err != nil {
|
2022-03-21 13:59:05 +00:00
|
|
|
return fmt.Errorf("can't remove fake bucket tree indexes: %w", err)
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2020-11-09 10:09:09 +00:00
|
|
|
}
|
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
// parentLength returns amount of available children from parentid index.
|
2022-01-26 12:11:13 +00:00
|
|
|
func parentLength(tx *bbolt.Tx, addr *addressSDK.Address) int {
|
2020-12-08 07:51:34 +00:00
|
|
|
bkt := tx.Bucket(parentBucketName(addr.ContainerID()))
|
|
|
|
if bkt == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
lst, err := decodeList(bkt.Get(objectKey(addr.ObjectID())))
|
|
|
|
if err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return len(lst)
|
|
|
|
}
|
|
|
|
|
|
|
|
func delUniqueIndexItem(tx *bbolt.Tx, item namedBucketItem) {
|
|
|
|
bkt := tx.Bucket(item.name)
|
|
|
|
if bkt != nil {
|
|
|
|
_ = bkt.Delete(item.key) // ignore error, best effort there
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-21 13:59:05 +00:00
|
|
|
func delFKBTIndexItem(tx *bbolt.Tx, item namedBucketItem) error {
|
2020-12-08 07:51:34 +00:00
|
|
|
bkt := tx.Bucket(item.name)
|
|
|
|
if bkt == nil {
|
2022-03-21 13:59:05 +00:00
|
|
|
return nil
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fkbtRoot := bkt.Bucket(item.key)
|
|
|
|
if fkbtRoot == nil {
|
2022-03-21 13:59:05 +00:00
|
|
|
return nil
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_ = fkbtRoot.Delete(item.val) // ignore error, best effort there
|
2022-03-21 13:59:05 +00:00
|
|
|
return nil
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
2022-03-21 13:59:05 +00:00
|
|
|
func delListIndexItem(tx *bbolt.Tx, item namedBucketItem) error {
|
2020-12-08 07:51:34 +00:00
|
|
|
bkt := tx.Bucket(item.name)
|
|
|
|
if bkt == nil {
|
2022-03-21 13:59:05 +00:00
|
|
|
return nil
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lst, err := decodeList(bkt.Get(item.key))
|
|
|
|
if err != nil || len(lst) == 0 {
|
2022-03-21 13:59:05 +00:00
|
|
|
return nil
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// remove element from the list
|
|
|
|
for i := range lst {
|
2022-03-21 15:39:46 +00:00
|
|
|
if bytes.Equal(item.val, lst[i]) {
|
|
|
|
copy(lst[i:], lst[i+1:])
|
|
|
|
lst = lst[:len(lst)-1]
|
|
|
|
break
|
2020-10-29 12:03:55 +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
|
|
|
// if list empty, remove the key from <list> bucket
|
2022-03-21 15:39:46 +00:00
|
|
|
if len(lst) == 0 {
|
2020-12-08 07:51:34 +00:00
|
|
|
_ = bkt.Delete(item.key) // ignore error, best effort there
|
|
|
|
|
2022-03-21 13:59:05 +00:00
|
|
|
return nil
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// if list is not empty, then update it
|
|
|
|
encodedLst, err := encodeList(lst)
|
|
|
|
if err != nil {
|
2022-03-21 13:59:05 +00:00
|
|
|
return nil // ignore error, best effort there
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_ = bkt.Put(item.key, encodedLst) // ignore error, best effort there
|
2022-03-21 13:59:05 +00:00
|
|
|
return nil
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
2022-03-21 13:59:05 +00:00
|
|
|
func delUniqueIndexes(tx *bbolt.Tx, obj *objectSDK.Object, isParent bool) error {
|
2022-03-03 14:19:05 +00:00
|
|
|
addr := object.AddressOf(obj)
|
2020-12-08 07:51:34 +00:00
|
|
|
objKey := objectKey(addr.ObjectID())
|
|
|
|
addrKey := addressKey(addr)
|
|
|
|
|
|
|
|
// add value to primary unique bucket
|
|
|
|
if !isParent {
|
|
|
|
var bucketName []byte
|
|
|
|
|
|
|
|
switch obj.Type() {
|
|
|
|
case objectSDK.TypeRegular:
|
|
|
|
bucketName = primaryBucketName(addr.ContainerID())
|
|
|
|
case objectSDK.TypeTombstone:
|
|
|
|
bucketName = tombstoneBucketName(addr.ContainerID())
|
|
|
|
case objectSDK.TypeStorageGroup:
|
|
|
|
bucketName = storageGroupBucketName(addr.ContainerID())
|
2022-02-15 12:51:56 +00:00
|
|
|
case objectSDK.TypeLock:
|
|
|
|
bucketName = bucketNameLockers(*addr.ContainerID())
|
2020-12-08 07:51:34 +00:00
|
|
|
default:
|
2022-03-21 13:59:05 +00:00
|
|
|
return ErrUnknownObjectType
|
2020-10-29 12:03:55 +00:00
|
|
|
}
|
|
|
|
|
2022-03-21 13:59:05 +00:00
|
|
|
delUniqueIndexItem(tx, namedBucketItem{
|
2020-12-08 07:51:34 +00:00
|
|
|
name: bucketName,
|
|
|
|
key: objKey,
|
|
|
|
})
|
2021-02-11 17:39:48 +00:00
|
|
|
} else {
|
2022-03-21 13:59:05 +00:00
|
|
|
delUniqueIndexItem(tx, namedBucketItem{
|
2021-02-11 17:39:48 +00:00
|
|
|
name: parentBucketName(obj.ContainerID()),
|
|
|
|
key: objKey,
|
|
|
|
})
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
2022-03-21 13:59:05 +00:00
|
|
|
delUniqueIndexItem(tx, namedBucketItem{ // remove from small blobovnicza id index
|
|
|
|
name: smallBucketName(addr.ContainerID()),
|
|
|
|
key: objKey,
|
|
|
|
})
|
|
|
|
delUniqueIndexItem(tx, namedBucketItem{ // remove from root index
|
|
|
|
name: rootBucketName(addr.ContainerID()),
|
|
|
|
key: objKey,
|
|
|
|
})
|
|
|
|
delUniqueIndexItem(tx, namedBucketItem{ // remove from ToMoveIt index
|
|
|
|
name: toMoveItBucketName,
|
|
|
|
key: addrKey,
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
2020-10-29 12:03:55 +00:00
|
|
|
}
|