2020-11-17 12:26:03 +00:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/shard"
|
2021-11-10 07:08:33 +00:00
|
|
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/object"
|
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
|
|
|
)
|
|
|
|
|
|
|
|
// SelectPrm groups the parameters of Select operation.
|
|
|
|
type SelectPrm struct {
|
2022-05-31 17:00:41 +00:00
|
|
|
cnr cid.ID
|
2020-11-17 12:26:03 +00:00
|
|
|
filters object.SearchFilters
|
|
|
|
}
|
|
|
|
|
2022-04-21 11:28:05 +00:00
|
|
|
// SelectRes groups the resulting values of Select operation.
|
2020-11-17 12:26:03 +00:00
|
|
|
type SelectRes struct {
|
2022-05-31 17:00:41 +00:00
|
|
|
addrList []oid.Address
|
2020-11-17 12:26:03 +00:00
|
|
|
}
|
|
|
|
|
2020-12-10 13:24:27 +00:00
|
|
|
// WithContainerID is a Select option to set the container id to search in.
|
2022-05-23 13:12:32 +00:00
|
|
|
func (p *SelectPrm) WithContainerID(cnr cid.ID) {
|
2020-12-10 13:24:27 +00:00
|
|
|
if p != nil {
|
2022-05-31 17:00:41 +00:00
|
|
|
p.cnr = cnr
|
2020-12-10 13:24:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-17 12:26:03 +00:00
|
|
|
// WithFilters is a Select option to set the object filters.
|
2022-05-23 13:12:32 +00:00
|
|
|
func (p *SelectPrm) WithFilters(fs object.SearchFilters) {
|
2020-11-17 12:26:03 +00:00
|
|
|
if p != nil {
|
|
|
|
p.filters = fs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddressList returns list of addresses of the selected objects.
|
2022-05-31 17:00:41 +00:00
|
|
|
func (r *SelectRes) AddressList() []oid.Address {
|
2020-11-17 12:26:03 +00:00
|
|
|
return r.addrList
|
|
|
|
}
|
|
|
|
|
|
|
|
// Select selects the objects from local storage that match select parameters.
|
|
|
|
//
|
|
|
|
// Returns any error encountered that did not allow to completely select the objects.
|
2021-11-09 15:46:12 +00:00
|
|
|
//
|
|
|
|
// Returns an error if executions are blocked (see BlockExecution).
|
2022-05-23 13:12:32 +00:00
|
|
|
func (e *StorageEngine) Select(prm SelectPrm) (res *SelectRes, 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._select(prm)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-23 13:12:32 +00:00
|
|
|
func (e *StorageEngine) _select(prm SelectPrm) (*SelectRes, error) {
|
2021-03-16 08:14:56 +00:00
|
|
|
if e.metrics != nil {
|
|
|
|
defer elapsed(e.metrics.AddSearchDuration)()
|
2021-03-15 13:09:27 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
addrList := make([]oid.Address, 0)
|
2020-12-01 10:54:29 +00:00
|
|
|
uniqueMap := make(map[string]struct{})
|
2020-11-17 12:26:03 +00:00
|
|
|
|
2020-12-10 14:12:26 +00:00
|
|
|
var outError error
|
|
|
|
|
2022-05-20 18:08:59 +00:00
|
|
|
var shPrm shard.SelectPrm
|
|
|
|
shPrm.WithContainerID(prm.cnr)
|
|
|
|
shPrm.WithFilters(prm.filters)
|
2020-11-17 12:26:03 +00:00
|
|
|
|
2022-01-31 14:58:32 +00:00
|
|
|
e.iterateOverUnsortedShards(func(sh hashedShard) (stop bool) {
|
2020-11-17 12:26:03 +00:00
|
|
|
res, err := sh.Select(shPrm)
|
|
|
|
if err != nil {
|
2022-05-31 17:00:41 +00:00
|
|
|
e.reportShardError(sh, "could not select objects from shard", err)
|
|
|
|
return false
|
2020-11-17 12:26:03 +00:00
|
|
|
} else {
|
2020-12-01 10:54:29 +00:00
|
|
|
for _, addr := range res.AddressList() { // save only unique values
|
2022-05-31 17:00:41 +00:00
|
|
|
if _, ok := uniqueMap[addr.EncodeToString()]; !ok {
|
|
|
|
uniqueMap[addr.EncodeToString()] = struct{}{}
|
2020-12-01 10:54:29 +00:00
|
|
|
addrList = append(addrList, addr)
|
|
|
|
}
|
|
|
|
}
|
2020-11-17 12:26:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
|
|
|
return &SelectRes{
|
|
|
|
addrList: addrList,
|
2020-12-10 14:12:26 +00:00
|
|
|
}, outError
|
2020-11-17 12:26:03 +00:00
|
|
|
}
|
2020-11-19 08:15:49 +00:00
|
|
|
|
2020-12-03 12:01:45 +00:00
|
|
|
// List returns `limit` available physically storage object addresses in engine.
|
|
|
|
// If limit is zero, then returns all available object addresses.
|
2021-11-09 15:46:12 +00:00
|
|
|
//
|
|
|
|
// Returns an error if executions are blocked (see BlockExecution).
|
|
|
|
func (e *StorageEngine) List(limit uint64) (res *SelectRes, 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.list(limit)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *StorageEngine) list(limit uint64) (*SelectRes, error) {
|
2021-03-16 08:14:56 +00:00
|
|
|
if e.metrics != nil {
|
|
|
|
defer elapsed(e.metrics.AddListObjectsDuration)()
|
2021-03-15 13:09:27 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
addrList := make([]oid.Address, 0, limit)
|
2020-12-03 12:01:45 +00:00
|
|
|
uniqueMap := make(map[string]struct{})
|
|
|
|
ln := uint64(0)
|
|
|
|
|
|
|
|
// consider iterating over shuffled shards
|
2022-01-31 14:58:32 +00:00
|
|
|
e.iterateOverUnsortedShards(func(sh hashedShard) (stop bool) {
|
2020-12-03 12:01:45 +00:00
|
|
|
res, err := sh.List() // consider limit result of shard iterator
|
|
|
|
if err != nil {
|
2022-01-31 14:58:32 +00:00
|
|
|
e.reportShardError(sh, "could not select objects from shard", err)
|
2020-12-03 12:01:45 +00:00
|
|
|
} else {
|
|
|
|
for _, addr := range res.AddressList() { // save only unique values
|
2022-05-31 17:00:41 +00:00
|
|
|
if _, ok := uniqueMap[addr.EncodeToString()]; !ok {
|
|
|
|
uniqueMap[addr.EncodeToString()] = struct{}{}
|
2020-12-03 12:01:45 +00:00
|
|
|
addrList = append(addrList, addr)
|
|
|
|
|
|
|
|
ln++
|
|
|
|
if limit > 0 && ln >= limit {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
|
|
|
return &SelectRes{
|
|
|
|
addrList: addrList,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-11-19 08:15:49 +00:00
|
|
|
// Select selects objects from local storage using provided filters.
|
2022-05-31 17:00:41 +00:00
|
|
|
func Select(storage *StorageEngine, cnr cid.ID, fs object.SearchFilters) ([]oid.Address, error) {
|
2022-05-23 13:12:32 +00:00
|
|
|
var selectPrm SelectPrm
|
|
|
|
selectPrm.WithContainerID(cnr)
|
|
|
|
selectPrm.WithFilters(fs)
|
|
|
|
|
|
|
|
res, err := storage.Select(selectPrm)
|
2020-11-19 08:15:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.AddressList(), nil
|
|
|
|
}
|
|
|
|
|
2020-12-03 12:01:45 +00:00
|
|
|
// List returns `limit` available physically storage object addresses in
|
|
|
|
// engine. If limit is zero, then returns all available object addresses.
|
2022-05-31 17:00:41 +00:00
|
|
|
func List(storage *StorageEngine, limit uint64) ([]oid.Address, error) {
|
2020-12-03 12:01:45 +00:00
|
|
|
res, err := storage.List(limit)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.AddressList(), nil
|
2020-11-19 08:15:49 +00:00
|
|
|
}
|