2020-11-25 09:40:41 +00:00
|
|
|
package blobstor
|
|
|
|
|
2020-11-25 11:18:54 +00:00
|
|
|
import (
|
2021-05-18 08:12:51 +00:00
|
|
|
"errors"
|
|
|
|
|
2021-04-08 13:53:25 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/blobstor/fstree"
|
2022-05-31 17:00:41 +00:00
|
|
|
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
2022-03-03 14:16:49 +00:00
|
|
|
"go.uber.org/zap"
|
2020-11-25 11:18:54 +00:00
|
|
|
)
|
|
|
|
|
2020-11-25 09:40:41 +00:00
|
|
|
// ExistsPrm groups the parameters of Exists operation.
|
|
|
|
type ExistsPrm struct {
|
|
|
|
address
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// ExistsRes groups the resulting values of Exists operation.
|
2020-11-25 09:40:41 +00:00
|
|
|
type ExistsRes struct {
|
|
|
|
exists bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exists returns the fact that the object is in BLOB storage.
|
|
|
|
func (r ExistsRes) Exists() bool {
|
|
|
|
return r.exists
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// Exists checks if the object is presented in BLOB storage.
|
2020-11-25 09:40:41 +00:00
|
|
|
//
|
|
|
|
// Returns any error encountered that did not allow
|
|
|
|
// to completely check object existence.
|
2022-05-31 12:18:32 +00:00
|
|
|
func (b *BlobStor) Exists(prm ExistsPrm) (ExistsRes, error) {
|
2020-11-25 11:18:54 +00:00
|
|
|
// check presence in shallow dir first (cheaper)
|
|
|
|
exists, err := b.existsBig(prm.addr)
|
2022-03-03 14:16:49 +00:00
|
|
|
|
|
|
|
// If there was an error during existence check below,
|
|
|
|
// it will be returned unless object was found in blobovnicza.
|
|
|
|
// Otherwise, it is logged and the latest error is returned.
|
|
|
|
// FSTree | Blobovnicza | Behaviour
|
|
|
|
// found | (not tried) | return true, nil
|
|
|
|
// not found | any result | return the result
|
|
|
|
// error | found | log the error, return true, nil
|
|
|
|
// error | not found | return the error
|
|
|
|
// error | error | log the first error, return the second
|
2020-11-25 11:18:54 +00:00
|
|
|
if !exists {
|
2022-03-03 14:16:49 +00:00
|
|
|
var smallErr error
|
2020-11-25 11:18:54 +00:00
|
|
|
|
2022-03-03 14:16:49 +00:00
|
|
|
exists, smallErr = b.existsSmall(prm.addr)
|
|
|
|
if err != nil && (smallErr != nil || exists) {
|
|
|
|
b.log.Warn("error occured during object existence checking",
|
|
|
|
zap.Stringer("address", prm.addr),
|
|
|
|
zap.String("error", err.Error()))
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
err = smallErr
|
|
|
|
}
|
2020-11-25 11:18:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2022-05-31 12:18:32 +00:00
|
|
|
return ExistsRes{}, err
|
2020-11-25 11:18:54 +00:00
|
|
|
}
|
2022-05-31 12:18:32 +00:00
|
|
|
|
|
|
|
return ExistsRes{exists: exists}, err
|
2020-11-25 11:18:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// checks if object is presented in shallow dir.
|
2022-05-31 17:00:41 +00:00
|
|
|
func (b *BlobStor) existsBig(addr oid.Address) (bool, error) {
|
2021-04-08 13:53:25 +00:00
|
|
|
_, err := b.fsTree.Exists(addr)
|
|
|
|
if errors.Is(err, fstree.ErrFileNotFound) {
|
2020-11-25 11:18:54 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return err == nil, err
|
|
|
|
}
|
|
|
|
|
2022-03-03 14:16:49 +00:00
|
|
|
// existsSmall checks if object is presented in blobovnicza.
|
2022-05-31 17:00:41 +00:00
|
|
|
func (b *BlobStor) existsSmall(addr oid.Address) (bool, error) {
|
2022-07-05 13:47:39 +00:00
|
|
|
return b.blobovniczas.Exists(addr)
|
2020-11-25 11:18:54 +00:00
|
|
|
}
|