-
Notifications
You must be signed in to change notification settings - Fork 38
/
exists.go
85 lines (67 loc) · 1.85 KB
/
exists.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package engine
import (
"errors"
objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object"
"github.com/nspcc-dev/neofs-node/pkg/core/object"
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/shard"
"go.uber.org/zap"
)
// ExistsPrm groups the parameters of Exists operation.
type ExistsPrm struct {
addr *objectSDK.Address
}
// ExistsRes groups resulting values of Exists operation.
type ExistsRes struct {
ex bool
}
// WithAddress is an Exists option to set object checked for existence.
func (p *ExistsPrm) WithAddress(addr *objectSDK.Address) *ExistsPrm {
if p != nil {
p.addr = addr
}
return p
}
// Exists returns the fact that the object is in the storage engine.
func (p *ExistsRes) Exists() bool {
return p.ex
}
// Exists checks if object is presented in storage engine
//
// Returns any error encountered that does not allow to
// unambiguously determine the presence of an object.
func (e *StorageEngine) Exists(prm *ExistsPrm) (*ExistsRes, error) {
if e.metrics != nil {
defer elapsed(e.metrics.AddExistsDuration)()
}
exists, err := e.exists(prm.addr)
return &ExistsRes{
ex: exists,
}, err
}
func (e *StorageEngine) exists(addr *objectSDK.Address) (bool, error) {
shPrm := new(shard.ExistsPrm).WithAddress(addr)
alreadyRemoved := false
exists := false
e.iterateOverSortedShards(addr, func(_ int, sh *shard.Shard) (stop bool) {
res, err := sh.Exists(shPrm)
if err != nil {
if errors.Is(err, object.ErrAlreadyRemoved) {
alreadyRemoved = true
return true
}
// TODO: smth wrong with shard, need to be processed
e.log.Warn("could not check existence of object in shard",
zap.Stringer("shard", sh.ID()),
zap.String("error", err.Error()),
)
}
if res != nil && !exists {
exists = res.Exists()
}
return false
})
if alreadyRemoved {
return false, object.ErrAlreadyRemoved
}
return exists, nil
}