2020-11-17 12:26:03 +00:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
2022-02-15 22:25:27 +00:00
|
|
|
"errors"
|
|
|
|
|
2020-11-17 12:26:03 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/shard"
|
2022-02-15 22:25:27 +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-17 12:26:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// DeletePrm groups the parameters of Delete operation.
|
|
|
|
type DeletePrm struct {
|
2022-05-31 17:00:41 +00:00
|
|
|
addr []oid.Address
|
2020-11-17 12:26:03 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// DeleteRes groups the resulting values of Delete operation.
|
2020-11-17 12:26:03 +00:00
|
|
|
type DeleteRes struct{}
|
|
|
|
|
2020-12-03 09:53:08 +00:00
|
|
|
// WithAddresses is a Delete option to set the addresses of the objects to delete.
|
2020-11-17 12:26:03 +00:00
|
|
|
//
|
|
|
|
// Option is required.
|
2022-05-23 13:12:32 +00:00
|
|
|
func (p *DeletePrm) WithAddresses(addr ...oid.Address) {
|
2020-11-17 12:26:03 +00:00
|
|
|
if p != nil {
|
2020-12-01 10:26:53 +00:00
|
|
|
p.addr = append(p.addr, addr...)
|
2020-11-17 12:26:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-15 11:42:54 +00:00
|
|
|
// Delete marks the objects to be removed.
|
2021-11-09 15:46:12 +00:00
|
|
|
//
|
|
|
|
// Returns an error if executions are blocked (see BlockExecution).
|
2022-02-15 22:25:27 +00:00
|
|
|
//
|
|
|
|
// Returns apistatus.ObjectLocked if at least one object is locked.
|
|
|
|
// In this case no object from the list is marked to be deleted.
|
2022-05-31 11:56:59 +00:00
|
|
|
func (e *StorageEngine) Delete(prm DeletePrm) (res DeleteRes, err error) {
|
2021-11-10 15:00:30 +00:00
|
|
|
err = e.execIfNotBlocked(func() error {
|
2021-11-09 15:46:12 +00:00
|
|
|
res, err = e.delete(prm)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-31 11:56:59 +00:00
|
|
|
func (e *StorageEngine) delete(prm DeletePrm) (DeleteRes, error) {
|
2021-03-16 08:14:56 +00:00
|
|
|
if e.metrics != nil {
|
|
|
|
defer elapsed(e.metrics.AddDeleteDuration)()
|
2021-03-15 13:09:27 +00:00
|
|
|
}
|
|
|
|
|
2022-02-15 22:25:27 +00:00
|
|
|
var locked struct {
|
|
|
|
is bool
|
|
|
|
err apistatus.ObjectLocked
|
|
|
|
}
|
2021-02-15 11:42:54 +00:00
|
|
|
|
|
|
|
for i := range prm.addr {
|
2022-01-31 14:58:32 +00:00
|
|
|
e.iterateOverSortedShards(prm.addr[i], func(_ int, sh hashedShard) (stop bool) {
|
2022-05-23 13:12:32 +00:00
|
|
|
var existsPrm shard.ExistsPrm
|
2022-05-20 18:08:59 +00:00
|
|
|
existsPrm.WithAddress(prm.addr[i])
|
|
|
|
|
|
|
|
resExists, err := sh.Exists(existsPrm)
|
2021-02-15 11:42:54 +00:00
|
|
|
if err != nil {
|
2022-01-31 14:58:32 +00:00
|
|
|
e.reportShardError(sh, "could not check object existence", err)
|
2021-02-15 11:42:54 +00:00
|
|
|
return false
|
|
|
|
} else if !resExists.Exists() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-05-23 13:12:32 +00:00
|
|
|
var shPrm shard.InhumePrm
|
2022-05-20 18:08:59 +00:00
|
|
|
shPrm.MarkAsGarbage(prm.addr[i])
|
|
|
|
|
|
|
|
_, err = sh.Inhume(shPrm)
|
2021-02-15 11:42:54 +00:00
|
|
|
if err != nil {
|
2022-01-31 14:58:32 +00:00
|
|
|
e.reportShardError(sh, "could not inhume object in shard", err)
|
2022-02-15 22:25:27 +00:00
|
|
|
|
|
|
|
locked.is = errors.As(err, &locked.err)
|
|
|
|
|
|
|
|
return locked.is
|
2021-02-15 11:42:54 +00:00
|
|
|
}
|
|
|
|
|
2022-02-15 22:25:27 +00:00
|
|
|
return true
|
2021-02-15 11:42:54 +00:00
|
|
|
})
|
|
|
|
}
|
2020-11-17 12:26:03 +00:00
|
|
|
|
2022-02-15 22:25:27 +00:00
|
|
|
if locked.is {
|
2022-05-31 11:56:59 +00:00
|
|
|
return DeleteRes{}, locked.err
|
2022-02-15 22:25:27 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 11:56:59 +00:00
|
|
|
return DeleteRes{}, nil
|
2020-11-17 12:26:03 +00:00
|
|
|
}
|