2023-08-24 17:44:09 +03:00
|
|
|
package blobtree
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2023-10-04 14:35:13 +03:00
|
|
|
"time"
|
2023-08-24 17:44:09 +03:00
|
|
|
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor/common"
|
2023-10-04 16:08:33 +03:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-observability/tracing"
|
2023-08-24 17:44:09 +03:00
|
|
|
apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
2023-10-04 16:08:33 +03:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
2023-08-24 17:44:09 +03:00
|
|
|
)
|
|
|
|
|
2023-10-04 16:08:33 +03:00
|
|
|
func (b *BlobTree) Exists(ctx context.Context, prm common.ExistsPrm) (common.ExistsRes, error) {
|
2023-10-04 14:35:13 +03:00
|
|
|
var (
|
|
|
|
startedAt = time.Now()
|
|
|
|
success = false
|
|
|
|
)
|
|
|
|
defer func() {
|
|
|
|
b.cfg.metrics.Exists(time.Since(startedAt), success, prm.StorageID != nil)
|
|
|
|
}()
|
|
|
|
|
2023-10-04 16:08:33 +03:00
|
|
|
_, span := tracing.StartSpanFromContext(ctx, "BlobTree.Exists",
|
|
|
|
trace.WithAttributes(
|
|
|
|
attribute.String("path", b.cfg.rootPath),
|
|
|
|
attribute.String("address", prm.Address.EncodeToString()),
|
|
|
|
attribute.String("storage_id", storageIDToIdxStringSafe(prm.StorageID)),
|
|
|
|
))
|
|
|
|
defer span.End()
|
|
|
|
|
2023-10-04 14:35:13 +03:00
|
|
|
var res common.ExistsRes
|
|
|
|
var err error
|
2023-10-04 16:08:33 +03:00
|
|
|
if idx, ok := tryParseIdxFromStorageID(prm.StorageID); ok {
|
|
|
|
res, err = b.existsFromIdx(prm.Address, idx)
|
2023-10-04 14:35:13 +03:00
|
|
|
} else {
|
|
|
|
res, err = b.findAndCheck(prm.Address)
|
2023-08-24 17:44:09 +03:00
|
|
|
}
|
2023-10-04 14:35:13 +03:00
|
|
|
success = err == nil
|
|
|
|
return res, err
|
2023-08-24 17:44:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BlobTree) existsFromIdx(addr oid.Address, idx uint64) (common.ExistsRes, error) {
|
|
|
|
dir := b.getDirectoryPath(addr)
|
|
|
|
path := b.getFilePath(dir, idx)
|
|
|
|
|
|
|
|
b.fileLock.RLock(path)
|
|
|
|
defer b.fileLock.RUnlock(path)
|
|
|
|
|
|
|
|
records, err := b.readFileContent(path)
|
|
|
|
if err != nil {
|
|
|
|
return common.ExistsRes{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range records {
|
|
|
|
if records[i].Address.Equals(addr) {
|
|
|
|
return common.ExistsRes{
|
|
|
|
Exists: true,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return common.ExistsRes{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BlobTree) findAndCheck(addr oid.Address) (common.ExistsRes, error) {
|
|
|
|
dir := b.getDirectoryPath(addr)
|
|
|
|
_, err := b.findFileIdx(dir, addr)
|
|
|
|
if err != nil {
|
|
|
|
var notFound *apistatus.ObjectNotFound
|
|
|
|
if errors.As(err, ¬Found) {
|
|
|
|
return common.ExistsRes{}, nil
|
|
|
|
}
|
|
|
|
return common.ExistsRes{}, err
|
|
|
|
}
|
|
|
|
return common.ExistsRes{Exists: true}, nil
|
|
|
|
}
|