frostfs-node/pkg/local_object_storage/blobovnicza/delete.go
Pavel Karpy 1667ec9e6d [#1131] *: Adopt SDK changes
`object.Address` has been moved to `object/address`
`object.ID` has been moved to `object/id`

Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
2022-02-08 09:45:38 +03:00

72 lines
1.6 KiB
Go

package blobovnicza
import (
"github.com/nspcc-dev/neofs-node/pkg/core/object"
addressSDK "github.com/nspcc-dev/neofs-sdk-go/object/address"
"go.etcd.io/bbolt"
"go.uber.org/zap"
)
// DeletePrm groups the parameters of Delete operation.
type DeletePrm struct {
addr *addressSDK.Address
}
// DeleteRes groups resulting values of Delete operation.
type DeleteRes struct {
}
// SetAddress sets address of the requested object.
func (p *DeletePrm) SetAddress(addr *addressSDK.Address) {
p.addr = addr
}
// Delete removes object from Blobovnicza by address.
//
// Returns any error encountered that
// did not allow to completely delete the object.
//
// Returns ErrNotFound if the object to be deleted is not in blobovnicza.
//
// Should not be called in read-only configuration.
func (b *Blobovnicza) Delete(prm *DeletePrm) (*DeleteRes, error) {
addrKey := addressKey(prm.addr)
removed := false
err := b.boltDB.Update(func(tx *bbolt.Tx) error {
return b.iterateBuckets(tx, func(lower, upper uint64, buck *bbolt.Bucket) (bool, error) {
objData := buck.Get(addrKey)
if objData == nil {
// object is not in bucket => continue iterating
return false, nil
}
sz := uint64(len(objData))
// decrease fullness counter
b.decSize(sz)
// remove object from the bucket
err := buck.Delete(addrKey)
if err == nil {
b.log.Debug("object was removed from bucket",
zap.String("binary size", stringifyByteSize(sz)),
zap.String("range", stringifyBounds(lower, upper)),
)
}
removed = true
// stop iteration
return true, err
})
})
if err == nil && !removed {
err = object.ErrNotFound
}
return nil, err
}