2020-11-26 14:26:53 +00:00
|
|
|
package blobovnicza
|
|
|
|
|
|
|
|
import (
|
2022-03-17 08:03:58 +00:00
|
|
|
apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status"
|
2022-05-31 17:00:41 +00:00
|
|
|
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
2020-11-26 14:26:53 +00:00
|
|
|
"go.etcd.io/bbolt"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DeletePrm groups the parameters of Delete operation.
|
|
|
|
type DeletePrm struct {
|
2022-05-31 17:00:41 +00:00
|
|
|
addr oid.Address
|
2020-11-26 14:26:53 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// DeleteRes groups the resulting values of Delete operation.
|
2020-11-26 14:26:53 +00:00
|
|
|
type DeleteRes struct {
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// SetAddress sets the address of the requested object.
|
2022-05-31 17:00:41 +00:00
|
|
|
func (p *DeletePrm) SetAddress(addr oid.Address) {
|
2020-11-26 14:26:53 +00:00
|
|
|
p.addr = addr
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// Delete removes an object from Blobovnicza by address.
|
2020-11-26 14:26:53 +00:00
|
|
|
//
|
|
|
|
// Returns any error encountered that
|
|
|
|
// did not allow to completely delete the object.
|
|
|
|
//
|
2022-03-17 13:26:17 +00:00
|
|
|
// Returns an error of type apistatus.ObjectNotFound if the object to be deleted is not in blobovnicza.
|
2021-09-14 14:44:07 +00:00
|
|
|
//
|
|
|
|
// Should not be called in read-only configuration.
|
2020-11-26 14:26:53 +00:00
|
|
|
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 {
|
2022-03-17 08:03:58 +00:00
|
|
|
var errNotFound apistatus.ObjectNotFound
|
|
|
|
|
|
|
|
return nil, errNotFound
|
2020-11-26 14:26:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
}
|