2020-09-22 06:51:47 +00:00
|
|
|
package searchsvc
|
|
|
|
|
|
|
|
import (
|
2021-05-18 08:12:51 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-04-29 08:54:40 +00:00
|
|
|
"io"
|
|
|
|
"sync"
|
|
|
|
|
2021-05-31 11:03:17 +00:00
|
|
|
cid "github.com/nspcc-dev/neofs-api-go/pkg/container/id"
|
2020-10-01 12:12:39 +00:00
|
|
|
objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object"
|
2021-05-31 11:03:17 +00:00
|
|
|
sessionsdk "github.com/nspcc-dev/neofs-api-go/pkg/session"
|
2021-04-29 08:54:40 +00:00
|
|
|
rpcclient "github.com/nspcc-dev/neofs-api-go/rpc/client"
|
2020-12-10 12:26:40 +00:00
|
|
|
objectV2 "github.com/nspcc-dev/neofs-api-go/v2/object"
|
2021-04-29 08:54:40 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/rpc"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/session"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/signature"
|
2021-06-21 14:13:08 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/client"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/network"
|
2020-12-10 12:26:40 +00:00
|
|
|
objectSvc "github.com/nspcc-dev/neofs-node/pkg/services/object"
|
2020-09-22 06:51:47 +00:00
|
|
|
searchsvc "github.com/nspcc-dev/neofs-node/pkg/services/object/search"
|
2020-09-29 15:05:22 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/object/util"
|
2020-09-22 06:51:47 +00:00
|
|
|
)
|
|
|
|
|
2020-12-10 12:26:40 +00:00
|
|
|
func (s *Service) toPrm(req *objectV2.SearchRequest, stream objectSvc.SearchStream) (*searchsvc.Prm, error) {
|
|
|
|
meta := req.GetMetaHeader()
|
2020-09-22 06:51:47 +00:00
|
|
|
|
2021-05-31 11:03:17 +00:00
|
|
|
key, err := s.keyStorage.GetKey(sessionsdk.NewTokenFromV2(meta.GetSessionToken()))
|
2020-12-10 12:26:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-09-22 06:51:47 +00:00
|
|
|
}
|
|
|
|
|
2021-01-12 14:55:02 +00:00
|
|
|
commonPrm, err := util.CommonPrmFromV2(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-12-10 12:26:40 +00:00
|
|
|
p := new(searchsvc.Prm)
|
2021-01-12 14:55:02 +00:00
|
|
|
p.SetCommonParameters(commonPrm.
|
2020-12-11 11:59:16 +00:00
|
|
|
WithPrivateKey(key),
|
|
|
|
)
|
|
|
|
|
2020-12-10 12:26:40 +00:00
|
|
|
p.SetWriter(&streamWriter{
|
|
|
|
stream: stream,
|
|
|
|
})
|
|
|
|
|
2021-04-29 08:54:40 +00:00
|
|
|
if !commonPrm.LocalOnly() {
|
|
|
|
var onceResign sync.Once
|
|
|
|
|
2021-06-22 12:19:30 +00:00
|
|
|
p.SetRequestForwarder(groupAddressRequestForwarder(func(addr network.Address, c client.Client) ([]*objectSDK.ID, error) {
|
2021-04-29 08:54:40 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
// once compose and resign forwarding request
|
|
|
|
onceResign.Do(func() {
|
|
|
|
// compose meta header of the local server
|
|
|
|
metaHdr := new(session.RequestMetaHeader)
|
|
|
|
metaHdr.SetTTL(meta.GetTTL() - 1)
|
|
|
|
// TODO: think how to set the other fields
|
|
|
|
metaHdr.SetOrigin(meta)
|
|
|
|
|
|
|
|
req.SetMetaHeader(metaHdr)
|
|
|
|
|
|
|
|
err = signature.SignServiceMessage(key, req)
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-06-21 14:13:08 +00:00
|
|
|
stream, err := rpc.SearchObjects(c.RawForAddress(addr), req, rpcclient.WithContext(stream.Context()))
|
2021-04-29 08:54:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// code below is copy-pasted from c.SearchObjects implementation,
|
|
|
|
// perhaps it is worth highlighting the utility function in neofs-api-go
|
|
|
|
var (
|
|
|
|
searchResult []*objectSDK.ID
|
|
|
|
resp = new(objectV2.SearchResponse)
|
|
|
|
)
|
|
|
|
|
|
|
|
for {
|
|
|
|
// receive message from server stream
|
|
|
|
err := stream.Read(resp)
|
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
if errors.Is(err, io.EOF) {
|
2021-04-29 08:54:40 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("reading the response failed: %w", err)
|
2021-04-29 08:54:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// verify response structure
|
|
|
|
if err := signature.VerifyServiceMessage(resp); err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not verify %T: %w", resp, err)
|
2021-04-29 08:54:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
chunk := resp.GetBody().GetIDList()
|
|
|
|
for i := range chunk {
|
|
|
|
searchResult = append(searchResult, objectSDK.NewIDFromV2(chunk[i]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return searchResult, nil
|
2021-06-22 12:19:30 +00:00
|
|
|
}))
|
2021-04-29 08:54:40 +00:00
|
|
|
}
|
|
|
|
|
2020-12-10 12:26:40 +00:00
|
|
|
body := req.GetBody()
|
2021-05-31 11:03:17 +00:00
|
|
|
p.WithContainerID(cid.NewFromV2(body.GetContainerID()))
|
2020-12-10 12:26:40 +00:00
|
|
|
p.WithSearchFilters(objectSDK.NewSearchFiltersFromV2(body.GetFilters()))
|
|
|
|
|
|
|
|
return p, nil
|
2020-09-22 06:51:47 +00:00
|
|
|
}
|
2021-06-22 12:19:30 +00:00
|
|
|
|
|
|
|
func groupAddressRequestForwarder(f func(network.Address, client.Client) ([]*objectSDK.ID, error)) searchsvc.RequestForwarder {
|
|
|
|
return func(addrGroup network.AddressGroup, c client.Client) ([]*objectSDK.ID, error) {
|
|
|
|
var (
|
|
|
|
firstErr error
|
|
|
|
res []*objectSDK.ID
|
|
|
|
)
|
|
|
|
|
|
|
|
addrGroup.IterateAddresses(func(addr network.Address) (stop bool) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
stop = err == nil
|
|
|
|
|
|
|
|
if stop || firstErr == nil {
|
|
|
|
firstErr = err
|
|
|
|
}
|
|
|
|
|
|
|
|
// would be nice to log otherwise
|
|
|
|
}()
|
|
|
|
|
|
|
|
res, err = f(addr, c)
|
|
|
|
|
|
|
|
return
|
|
|
|
})
|
|
|
|
|
|
|
|
return res, firstErr
|
|
|
|
}
|
|
|
|
}
|