diff --git a/pkg/services/object/search/service.go b/pkg/services/object/search/service.go index 33dae8ee..87c371d7 100644 --- a/pkg/services/object/search/service.go +++ b/pkg/services/object/search/service.go @@ -3,6 +3,7 @@ package searchsvc import ( "context" "crypto/ecdsa" + "io" "sync" "github.com/nspcc-dev/neofs-api-go/pkg/object" @@ -10,7 +11,9 @@ import ( "github.com/nspcc-dev/neofs-node/pkg/core/netmap" "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/localstore" "github.com/nspcc-dev/neofs-node/pkg/network" + "github.com/nspcc-dev/neofs-node/pkg/services/object/search/query/v1" "github.com/nspcc-dev/neofs-node/pkg/util" + "github.com/pkg/errors" ) type Service struct { @@ -61,6 +64,46 @@ func (p *Service) Search(ctx context.Context, prm *Prm) (*Streamer, error) { }, nil } +func (p *Service) SearchRightChild(ctx context.Context, addr *object.Address) (*object.ID, error) { + streamer, err := p.Search(ctx, new(Prm). + WithContainerID(addr.GetContainerID()). + WithSearchQuery( + query.NewRightChildQuery(addr.GetObjectID()), + ), + ) + if err != nil { + return nil, errors.Wrapf(err, "(%T) could not create search streamer", p) + } + + res, err := readFullStream(streamer, 1) + if err != nil { + return nil, errors.Wrapf(err, "(%T) could not read full search stream", p) + } else if ln := len(res); ln != 1 { + return nil, errors.Errorf("(%T) unexpected amount of found objects %d", p, ln) + } + + return res[0], nil +} + +func readFullStream(s *Streamer, cap int) ([]*object.ID, error) { + res := make([]*object.ID, 0, cap) + + for { + r, err := s.Recv() + if err != nil { + if errors.Is(errors.Cause(err), io.EOF) { + break + } + + return nil, errors.Wrapf(err, "(%s) could not receive search result", "readFullStream") + } + + res = append(res, r.IDList()...) + } + + return res, nil +} + func WithKey(v *ecdsa.PrivateKey) Option { return func(c *cfg) { c.key = v