2020-10-29 12:03:55 +00:00
|
|
|
package meta
|
|
|
|
|
|
|
|
import (
|
2020-12-08 07:51:34 +00:00
|
|
|
"bytes"
|
2023-04-12 14:01:29 +00:00
|
|
|
"context"
|
2021-05-18 08:12:51 +00:00
|
|
|
"errors"
|
2020-12-08 07:51:34 +00:00
|
|
|
"fmt"
|
2023-06-06 09:27:19 +00:00
|
|
|
"time"
|
2020-12-08 07:51:34 +00:00
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/object"
|
|
|
|
storagelog "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/internal/log"
|
2023-06-15 10:19:36 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/internal/metaerr"
|
2023-05-31 09:24:04 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-observability/tracing"
|
2023-08-04 11:14:07 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client"
|
2023-11-01 17:53:24 +00:00
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
2023-03-07 13:38:26 +00:00
|
|
|
objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
2020-10-29 12:03:55 +00:00
|
|
|
"go.etcd.io/bbolt"
|
2023-04-12 14:01:29 +00:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
2020-10-29 12:03:55 +00:00
|
|
|
)
|
|
|
|
|
2020-12-08 09:56:14 +00:00
|
|
|
// DeletePrm groups the parameters of Delete operation.
|
|
|
|
type DeletePrm struct {
|
2022-05-31 17:00:41 +00:00
|
|
|
addrs []oid.Address
|
2020-12-08 09:56:14 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// DeleteRes groups the resulting values of Delete operation.
|
2022-08-19 16:49:09 +00:00
|
|
|
type DeleteRes struct {
|
2022-09-09 11:33:38 +00:00
|
|
|
rawRemoved uint64
|
|
|
|
availableRemoved uint64
|
2023-12-05 14:00:16 +00:00
|
|
|
userRemoved uint64
|
2022-12-01 11:59:22 +00:00
|
|
|
sizes []uint64
|
2023-02-02 20:28:42 +00:00
|
|
|
availableSizes []uint64
|
2023-11-02 10:50:52 +00:00
|
|
|
removedByCnrID map[cid.ID]ObjectCounters
|
2022-08-19 16:49:09 +00:00
|
|
|
}
|
|
|
|
|
2022-09-09 11:33:38 +00:00
|
|
|
// AvailableObjectsRemoved returns the number of removed available
|
|
|
|
// objects.
|
|
|
|
func (d DeleteRes) AvailableObjectsRemoved() uint64 {
|
|
|
|
return d.availableRemoved
|
|
|
|
}
|
|
|
|
|
2023-12-05 14:00:16 +00:00
|
|
|
func (d DeleteRes) UserObjectsRemoved() uint64 {
|
|
|
|
return d.userRemoved
|
|
|
|
}
|
|
|
|
|
2023-11-02 10:50:52 +00:00
|
|
|
// RemovedByCnrID returns the number of removed objects by container ID.
|
|
|
|
func (d DeleteRes) RemovedByCnrID() map[cid.ID]ObjectCounters {
|
|
|
|
return d.removedByCnrID
|
|
|
|
}
|
|
|
|
|
2022-09-09 11:33:38 +00:00
|
|
|
// RawObjectsRemoved returns the number of removed raw objects.
|
|
|
|
func (d DeleteRes) RawObjectsRemoved() uint64 {
|
|
|
|
return d.rawRemoved
|
2022-08-19 16:49:09 +00:00
|
|
|
}
|
2020-12-08 09:56:14 +00:00
|
|
|
|
2023-02-02 20:28:42 +00:00
|
|
|
// RemovedPhysicalObjectSizes returns the sizes of removed physical objects.
|
|
|
|
func (d DeleteRes) RemovedPhysicalObjectSizes() []uint64 {
|
2022-12-01 11:59:22 +00:00
|
|
|
return d.sizes
|
|
|
|
}
|
|
|
|
|
2023-02-02 20:28:42 +00:00
|
|
|
// RemovedLogicalObjectSizes returns the sizes of removed logical objects.
|
|
|
|
func (d DeleteRes) RemovedLogicalObjectSizes() []uint64 {
|
|
|
|
return d.availableSizes
|
|
|
|
}
|
|
|
|
|
2022-07-12 14:59:37 +00:00
|
|
|
// SetAddresses is a Delete option to set the addresses of the objects to delete.
|
2020-12-08 09:56:14 +00:00
|
|
|
//
|
|
|
|
// Option is required.
|
2022-07-12 14:59:37 +00:00
|
|
|
func (p *DeletePrm) SetAddresses(addrs ...oid.Address) {
|
|
|
|
p.addrs = addrs
|
2020-12-08 09:56:14 +00:00
|
|
|
}
|
|
|
|
|
2021-02-11 17:39:48 +00:00
|
|
|
type referenceNumber struct {
|
|
|
|
all, cur int
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
addr oid.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.
|
2023-04-12 14:01:29 +00:00
|
|
|
func (db *DB) Delete(ctx context.Context, prm DeletePrm) (DeleteRes, error) {
|
2023-06-06 09:27:19 +00:00
|
|
|
var (
|
|
|
|
startedAt = time.Now()
|
|
|
|
deleted = false
|
|
|
|
)
|
|
|
|
defer func() {
|
|
|
|
db.metrics.AddMethodDuration("Delete", time.Since(startedAt), deleted)
|
|
|
|
}()
|
|
|
|
|
2023-04-12 14:01:29 +00:00
|
|
|
_, span := tracing.StartSpanFromContext(ctx, "metabase.Delete",
|
|
|
|
trace.WithAttributes(
|
|
|
|
attribute.Int("addr_count", len(prm.addrs)),
|
|
|
|
))
|
|
|
|
defer span.End()
|
|
|
|
|
2022-07-21 13:26:25 +00:00
|
|
|
db.modeMtx.RLock()
|
|
|
|
defer db.modeMtx.RUnlock()
|
|
|
|
|
2022-11-15 12:46:32 +00:00
|
|
|
if db.mode.NoMetabase() {
|
|
|
|
return DeleteRes{}, ErrDegradedMode
|
2022-11-15 14:53:23 +00:00
|
|
|
} else if db.mode.ReadOnly() {
|
|
|
|
return DeleteRes{}, ErrReadOnlyMode
|
2022-11-15 12:46:32 +00:00
|
|
|
}
|
|
|
|
|
2022-08-19 16:49:09 +00:00
|
|
|
var err error
|
2023-11-02 10:50:52 +00:00
|
|
|
var res DeleteRes
|
2022-08-19 16:49:09 +00:00
|
|
|
|
|
|
|
err = db.boltDB.Update(func(tx *bbolt.Tx) error {
|
2023-11-02 10:50:52 +00:00
|
|
|
res, err = db.deleteGroup(tx, prm.addrs)
|
2022-08-19 16:49:09 +00:00
|
|
|
return err
|
2021-02-11 17:39:48 +00:00
|
|
|
})
|
2022-03-02 09:17:32 +00:00
|
|
|
if err == nil {
|
2023-06-06 09:27:19 +00:00
|
|
|
deleted = true
|
2022-03-02 09:17:32 +00:00
|
|
|
for i := range prm.addrs {
|
|
|
|
storagelog.Write(db.log,
|
|
|
|
storagelog.AddressField(prm.addrs[i]),
|
|
|
|
storagelog.OpField("metabase DELETE"))
|
|
|
|
}
|
|
|
|
}
|
2023-11-02 10:50:52 +00:00
|
|
|
return res, metaerr.Wrap(err)
|
2021-02-11 17:39:48 +00:00
|
|
|
}
|
|
|
|
|
2022-09-09 11:33:38 +00:00
|
|
|
// deleteGroup deletes object from the metabase. Handles removal of the
|
|
|
|
// references of the split objects.
|
2023-11-02 10:50:52 +00:00
|
|
|
func (db *DB) deleteGroup(tx *bbolt.Tx, addrs []oid.Address) (DeleteRes, error) {
|
|
|
|
res := DeleteRes{
|
|
|
|
sizes: make([]uint64, len(addrs)),
|
|
|
|
availableSizes: make([]uint64, len(addrs)),
|
|
|
|
removedByCnrID: make(map[cid.ID]ObjectCounters),
|
|
|
|
}
|
2021-02-11 17:39:48 +00:00
|
|
|
refCounter := make(referenceCounter, len(addrs))
|
2022-07-27 18:38:28 +00:00
|
|
|
currEpoch := db.epochState.CurrentEpoch()
|
2021-02-11 17:39:48 +00:00
|
|
|
|
|
|
|
for i := range addrs {
|
2023-12-05 14:00:16 +00:00
|
|
|
r, err := db.delete(tx, addrs[i], refCounter, currEpoch)
|
2021-02-11 17:39:48 +00:00
|
|
|
if err != nil {
|
2023-11-02 10:50:52 +00:00
|
|
|
return DeleteRes{}, err // maybe log and continue?
|
2021-02-11 17:39:48 +00:00
|
|
|
}
|
2022-08-19 12:36:37 +00:00
|
|
|
|
2023-12-05 14:00:16 +00:00
|
|
|
if r.Removed {
|
2023-11-02 10:50:52 +00:00
|
|
|
if v, ok := res.removedByCnrID[addrs[i].Container()]; ok {
|
2023-12-04 14:35:11 +00:00
|
|
|
v.Phy++
|
2023-11-02 10:50:52 +00:00
|
|
|
res.removedByCnrID[addrs[i].Container()] = v
|
2023-11-01 17:53:24 +00:00
|
|
|
} else {
|
2023-11-02 10:50:52 +00:00
|
|
|
res.removedByCnrID[addrs[i].Container()] = ObjectCounters{
|
2023-12-04 14:35:11 +00:00
|
|
|
Phy: 1,
|
2023-11-01 17:53:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-02 10:50:52 +00:00
|
|
|
res.rawRemoved++
|
2023-12-05 14:00:16 +00:00
|
|
|
res.sizes[i] = r.Size
|
2022-08-19 12:36:37 +00:00
|
|
|
}
|
2022-09-09 11:33:38 +00:00
|
|
|
|
2023-12-05 14:00:16 +00:00
|
|
|
if r.Available {
|
2023-11-02 10:50:52 +00:00
|
|
|
if v, ok := res.removedByCnrID[addrs[i].Container()]; ok {
|
2023-12-04 14:35:11 +00:00
|
|
|
v.Logic++
|
2023-11-02 10:50:52 +00:00
|
|
|
res.removedByCnrID[addrs[i].Container()] = v
|
2023-11-01 17:53:24 +00:00
|
|
|
} else {
|
2023-11-02 10:50:52 +00:00
|
|
|
res.removedByCnrID[addrs[i].Container()] = ObjectCounters{
|
2023-12-04 14:35:11 +00:00
|
|
|
Logic: 1,
|
2023-11-01 17:53:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-02 10:50:52 +00:00
|
|
|
res.availableRemoved++
|
2023-12-05 14:00:16 +00:00
|
|
|
res.availableSizes[i] = r.Size
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.User {
|
|
|
|
if v, ok := res.removedByCnrID[addrs[i].Container()]; ok {
|
|
|
|
v.User++
|
|
|
|
res.removedByCnrID[addrs[i].Container()] = v
|
|
|
|
} else {
|
|
|
|
res.removedByCnrID[addrs[i].Container()] = ObjectCounters{
|
|
|
|
User: 1,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res.userRemoved++
|
2022-09-09 11:33:38 +00:00
|
|
|
}
|
2022-08-19 12:36:37 +00:00
|
|
|
}
|
|
|
|
|
2023-11-02 10:50:52 +00:00
|
|
|
if res.rawRemoved > 0 {
|
|
|
|
err := db.updateShardObjectCounter(tx, phy, res.rawRemoved, false)
|
2022-09-09 11:33:38 +00:00
|
|
|
if err != nil {
|
2023-11-02 10:50:52 +00:00
|
|
|
return DeleteRes{}, fmt.Errorf("could not decrease phy object counter: %w", err)
|
2022-09-09 11:33:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-02 10:50:52 +00:00
|
|
|
if res.availableRemoved > 0 {
|
|
|
|
err := db.updateShardObjectCounter(tx, logical, res.availableRemoved, false)
|
2022-09-09 11:33:38 +00:00
|
|
|
if err != nil {
|
2023-11-02 10:50:52 +00:00
|
|
|
return DeleteRes{}, fmt.Errorf("could not decrease logical object counter: %w", err)
|
2022-09-09 11:33:38 +00:00
|
|
|
}
|
2021-02-11 17:39:48 +00:00
|
|
|
}
|
|
|
|
|
2023-12-05 14:00:16 +00:00
|
|
|
if res.userRemoved > 0 {
|
|
|
|
err := db.updateShardObjectCounter(tx, user, res.userRemoved, false)
|
|
|
|
if err != nil {
|
|
|
|
return DeleteRes{}, fmt.Errorf("could not decrease user object counter: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-02 10:50:52 +00:00
|
|
|
if err := db.updateContainerCounter(tx, res.removedByCnrID, false); err != nil {
|
|
|
|
return DeleteRes{}, fmt.Errorf("could not decrease container object counter: %w", err)
|
2023-11-01 17:53:24 +00:00
|
|
|
}
|
|
|
|
|
2021-02-11 17:39:48 +00:00
|
|
|
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 {
|
2023-11-02 10:50:52 +00:00
|
|
|
return DeleteRes{}, err // maybe log and continue?
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-11 17:39:48 +00:00
|
|
|
}
|
2020-12-08 07:51:34 +00:00
|
|
|
|
2023-11-02 10:50:52 +00:00
|
|
|
return res, nil
|
2020-11-09 10:09:09 +00:00
|
|
|
}
|
|
|
|
|
2023-12-05 14:00:16 +00:00
|
|
|
type deleteSingleResult struct {
|
|
|
|
Removed bool
|
|
|
|
Available bool
|
|
|
|
User bool
|
|
|
|
Size uint64
|
|
|
|
}
|
|
|
|
|
2022-09-09 11:33:38 +00:00
|
|
|
// delete removes object indexes from the metabase. Counts the references
|
|
|
|
// of the object that is being removed.
|
|
|
|
// The first return value indicates if an object has been removed. (removing a
|
|
|
|
// non-exist object is error-free). The second return value indicates if an
|
|
|
|
// object was available before the removal (for calculating the logical object
|
2023-12-05 14:00:16 +00:00
|
|
|
// counter). The third return value The fourth return value is removed object payload size.
|
|
|
|
func (db *DB) delete(tx *bbolt.Tx, addr oid.Address, refCounter referenceCounter, currEpoch uint64) (deleteSingleResult, error) {
|
2022-09-08 11:54:21 +00:00
|
|
|
key := make([]byte, addressKeySize)
|
|
|
|
addrKey := addressKey(addr, key)
|
2021-04-08 14:19:31 +00:00
|
|
|
garbageBKT := tx.Bucket(garbageBucketName)
|
2022-09-09 11:33:38 +00:00
|
|
|
graveyardBKT := tx.Bucket(graveyardBucketName)
|
|
|
|
|
|
|
|
removeAvailableObject := inGraveyardWithKey(addrKey, graveyardBKT, garbageBKT) == 0
|
|
|
|
|
|
|
|
// remove record from the garbage bucket
|
2021-04-08 14:19:31 +00:00
|
|
|
if garbageBKT != nil {
|
2022-09-09 11:33:38 +00:00
|
|
|
err := garbageBKT.Delete(addrKey)
|
2021-04-06 14:07:42 +00:00
|
|
|
if err != nil {
|
2023-12-05 14:00:16 +00:00
|
|
|
return deleteSingleResult{}, fmt.Errorf("could not remove from garbage bucket: %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
|
2022-09-08 11:54:21 +00:00
|
|
|
obj, err := db.get(tx, addr, key, false, true, currEpoch)
|
2020-12-08 07:51:34 +00:00
|
|
|
if err != nil {
|
2022-09-04 08:52:01 +00:00
|
|
|
var siErr *objectSDK.SplitInfoError
|
|
|
|
|
2023-08-04 11:14:07 +00:00
|
|
|
if client.IsErrObjectNotFound(err) || errors.As(err, &siErr) {
|
2023-12-05 14:00:16 +00:00
|
|
|
return deleteSingleResult{}, nil
|
2021-04-06 14:07:42 +00:00
|
|
|
}
|
|
|
|
|
2023-12-05 14:00:16 +00:00
|
|
|
return deleteSingleResult{}, err
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
2022-09-08 11:54:21 +00:00
|
|
|
sParAddr := addressKey(parAddr, key)
|
|
|
|
k := string(sParAddr)
|
2021-02-11 17:39:48 +00:00
|
|
|
|
2022-09-08 11:54:21 +00:00
|
|
|
nRef, ok := refCounter[k]
|
2021-02-11 17:39:48 +00:00
|
|
|
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
|
|
|
|
2022-09-08 11:54:21 +00:00
|
|
|
refCounter[k] = 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
|
|
|
}
|
|
|
|
|
2023-12-05 14:00:16 +00:00
|
|
|
isUserObject := IsUserObject(obj)
|
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
// remove object
|
2022-08-19 12:36:37 +00:00
|
|
|
err = db.deleteObject(tx, obj, false)
|
|
|
|
if err != nil {
|
2023-12-05 14:00:16 +00:00
|
|
|
return deleteSingleResult{}, fmt.Errorf("could not remove object: %w", err)
|
2022-08-19 12:36:37 +00:00
|
|
|
}
|
|
|
|
|
2023-12-05 14:00:16 +00:00
|
|
|
return deleteSingleResult{
|
|
|
|
Removed: true,
|
|
|
|
Available: removeAvailableObject,
|
|
|
|
User: isUserObject && removeAvailableObject,
|
|
|
|
Size: obj.PayloadSize(),
|
|
|
|
}, nil
|
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-05-31 17:00:41 +00:00
|
|
|
func parentLength(tx *bbolt.Tx, addr oid.Address) int {
|
2022-09-08 11:54:21 +00:00
|
|
|
bucketName := make([]byte, bucketKeySize)
|
|
|
|
|
|
|
|
bkt := tx.Bucket(parentBucketName(addr.Container(), bucketName[:]))
|
2020-12-08 07:51:34 +00:00
|
|
|
if bkt == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2022-09-08 11:54:21 +00:00
|
|
|
lst, err := decodeList(bkt.Get(objectKey(addr.Object(), bucketName[:])))
|
2020-12-08 07:51:34 +00:00
|
|
|
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)
|
2022-05-12 16:37:46 +00:00
|
|
|
|
2022-09-08 11:54:21 +00:00
|
|
|
objKey := objectKey(addr.Object(), make([]byte, objectKeySize))
|
|
|
|
addrKey := addressKey(addr, make([]byte, addressKeySize))
|
2022-05-31 17:00:41 +00:00
|
|
|
cnr := addr.Container()
|
2022-09-08 11:54:21 +00:00
|
|
|
bucketName := make([]byte, bucketKeySize)
|
2020-12-08 07:51:34 +00:00
|
|
|
|
|
|
|
// add value to primary unique bucket
|
|
|
|
if !isParent {
|
|
|
|
switch obj.Type() {
|
|
|
|
case objectSDK.TypeRegular:
|
2022-09-08 11:54:21 +00:00
|
|
|
bucketName = primaryBucketName(cnr, bucketName)
|
2020-12-08 07:51:34 +00:00
|
|
|
case objectSDK.TypeTombstone:
|
2022-09-08 11:54:21 +00:00
|
|
|
bucketName = tombstoneBucketName(cnr, bucketName)
|
2022-02-15 12:51:56 +00:00
|
|
|
case objectSDK.TypeLock:
|
2022-09-08 11:54:21 +00:00
|
|
|
bucketName = bucketNameLockers(cnr, bucketName)
|
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{
|
2022-09-08 11:54:21 +00:00
|
|
|
name: parentBucketName(cnr, bucketName),
|
2021-02-11 17:39:48 +00:00
|
|
|
key: objKey,
|
|
|
|
})
|
2020-12-08 07:51:34 +00:00
|
|
|
}
|
|
|
|
|
2022-07-06 14:09:50 +00:00
|
|
|
delUniqueIndexItem(tx, namedBucketItem{ // remove from storage id index
|
2022-09-08 11:54:21 +00:00
|
|
|
name: smallBucketName(cnr, bucketName),
|
2022-03-21 13:59:05 +00:00
|
|
|
key: objKey,
|
|
|
|
})
|
|
|
|
delUniqueIndexItem(tx, namedBucketItem{ // remove from root index
|
2022-09-08 11:54:21 +00:00
|
|
|
name: rootBucketName(cnr, bucketName),
|
2022-03-21 13:59:05 +00:00
|
|
|
key: objKey,
|
|
|
|
})
|
|
|
|
delUniqueIndexItem(tx, namedBucketItem{ // remove from ToMoveIt index
|
|
|
|
name: toMoveItBucketName,
|
|
|
|
key: addrKey,
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
2020-10-29 12:03:55 +00:00
|
|
|
}
|