2020-12-10 12:26:40 +00:00
|
|
|
package searchsvc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
2021-06-21 14:13:08 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/client"
|
2021-01-12 14:55:02 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/netmap"
|
2020-12-10 12:26:40 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/engine"
|
2021-11-01 08:35:33 +00:00
|
|
|
internalclient "github.com/nspcc-dev/neofs-node/pkg/services/object/internal/client"
|
2020-12-10 12:26:40 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/object/util"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/object_manager/placement"
|
2021-11-10 07:08:33 +00:00
|
|
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
2022-05-31 17:00:41 +00:00
|
|
|
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
2020-12-10 12:26:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type uniqueIDWriter struct {
|
|
|
|
mtx sync.Mutex
|
|
|
|
|
|
|
|
written map[string]struct{}
|
|
|
|
|
|
|
|
writer IDListWriter
|
|
|
|
}
|
|
|
|
|
2021-03-23 18:40:36 +00:00
|
|
|
type clientConstructorWrapper struct {
|
|
|
|
constructor ClientConstructor
|
2020-12-10 12:26:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type clientWrapper struct {
|
2022-01-13 15:01:50 +00:00
|
|
|
client client.MultiAddressClient
|
2020-12-10 12:26:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type storageEngineWrapper engine.StorageEngine
|
|
|
|
|
|
|
|
type traverseGeneratorWrapper util.TraverserGenerator
|
|
|
|
|
2021-01-12 14:55:02 +00:00
|
|
|
type nmSrcWrapper struct {
|
|
|
|
nmSrc netmap.Source
|
|
|
|
}
|
|
|
|
|
2020-12-10 12:26:40 +00:00
|
|
|
func newUniqueAddressWriter(w IDListWriter) IDListWriter {
|
|
|
|
return &uniqueIDWriter{
|
|
|
|
written: make(map[string]struct{}),
|
|
|
|
writer: w,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
func (w *uniqueIDWriter) WriteIDs(list []oid.ID) error {
|
2020-12-10 12:26:40 +00:00
|
|
|
w.mtx.Lock()
|
|
|
|
|
|
|
|
for i := 0; i < len(list); i++ { // don't use range, slice mutates in body
|
2022-05-31 17:00:41 +00:00
|
|
|
s := list[i].EncodeToString()
|
2020-12-10 12:26:40 +00:00
|
|
|
// standard stringer is quite costly, it is better
|
|
|
|
// to facilitate the calculation of the key
|
|
|
|
|
|
|
|
if _, ok := w.written[s]; !ok {
|
|
|
|
// mark address as processed
|
|
|
|
w.written[s] = struct{}{}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// exclude processed address
|
|
|
|
list = append(list[:i], list[i+1:]...)
|
|
|
|
i--
|
|
|
|
}
|
|
|
|
|
|
|
|
w.mtx.Unlock()
|
|
|
|
|
|
|
|
return w.writer.WriteIDs(list)
|
|
|
|
}
|
|
|
|
|
2021-09-28 04:46:10 +00:00
|
|
|
func (c *clientConstructorWrapper) get(info client.NodeInfo) (searchClient, error) {
|
|
|
|
clt, err := c.constructor.Get(info)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-12-10 12:26:40 +00:00
|
|
|
|
|
|
|
return &clientWrapper{
|
|
|
|
client: clt,
|
2021-09-28 04:46:10 +00:00
|
|
|
}, nil
|
2020-12-10 12:26:40 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
func (c *clientWrapper) searchObjects(exec *execCtx, info client.NodeInfo) ([]oid.ID, error) {
|
2021-05-20 08:05:18 +00:00
|
|
|
if exec.prm.forwarder != nil {
|
2021-09-28 04:46:10 +00:00
|
|
|
return exec.prm.forwarder(info, c.client)
|
2021-05-20 08:05:18 +00:00
|
|
|
}
|
|
|
|
|
2022-05-18 15:20:08 +00:00
|
|
|
var sessionInfo *util.SessionInfo
|
|
|
|
|
|
|
|
if tok := exec.prm.common.SessionToken(); tok != nil {
|
|
|
|
sessionInfo = &util.SessionInfo{
|
|
|
|
ID: tok.ID(),
|
2022-05-25 16:09:12 +00:00
|
|
|
Owner: tok.Issuer(),
|
2022-05-18 15:20:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
key, err := exec.svc.keyStore.GetKey(sessionInfo)
|
2021-10-26 12:07:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-11-01 08:35:33 +00:00
|
|
|
var prm internalclient.SearchObjectsPrm
|
|
|
|
|
|
|
|
prm.SetContext(exec.context())
|
|
|
|
prm.SetClient(c.client)
|
|
|
|
prm.SetPrivateKey(key)
|
|
|
|
prm.SetSessionToken(exec.prm.common.SessionToken())
|
|
|
|
prm.SetBearerToken(exec.prm.common.BearerToken())
|
|
|
|
prm.SetTTL(exec.prm.common.TTL())
|
|
|
|
prm.SetXHeaders(exec.prm.common.XHeaders())
|
|
|
|
prm.SetNetmapEpoch(exec.curProcEpoch)
|
|
|
|
prm.SetContainerID(exec.containerID())
|
|
|
|
prm.SetFilters(exec.searchFilters())
|
|
|
|
|
|
|
|
res, err := internalclient.SearchObjects(prm)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.IDList(), nil
|
2020-12-10 12:26:40 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
func (e *storageEngineWrapper) search(exec *execCtx) ([]oid.ID, error) {
|
2020-12-10 12:26:40 +00:00
|
|
|
r, err := (*engine.StorageEngine)(e).Select(new(engine.SelectPrm).
|
2020-12-10 14:38:45 +00:00
|
|
|
WithFilters(exec.searchFilters()).
|
|
|
|
WithContainerID(exec.containerID()),
|
2020-12-10 12:26:40 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return idsFromAddresses(r.AddressList()), nil
|
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
func idsFromAddresses(addrs []oid.Address) []oid.ID {
|
|
|
|
ids := make([]oid.ID, len(addrs))
|
2020-12-10 12:26:40 +00:00
|
|
|
|
|
|
|
for i := range addrs {
|
2022-05-31 17:00:41 +00:00
|
|
|
ids[i] = addrs[i].Object()
|
2020-12-10 12:26:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ids
|
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
func (e *traverseGeneratorWrapper) generateTraverser(cnr cid.ID, epoch uint64) (*placement.Traverser, error) {
|
|
|
|
return (*util.TraverserGenerator)(e).GenerateTraverser(cnr, nil, epoch)
|
2021-01-12 14:55:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *nmSrcWrapper) currentEpoch() (uint64, error) {
|
|
|
|
return n.nmSrc.Epoch()
|
2020-12-10 12:26:40 +00:00
|
|
|
}
|