From 753a6a2de567266fc659d8fa5e9ad32460dd613d Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Wed, 23 Sep 2020 13:50:20 +0300 Subject: [PATCH] [#39] service/object: Implement right child searcher on Search service Signed-off-by: Leonard Lyubich --- pkg/services/object/search/service.go | 43 +++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/pkg/services/object/search/service.go b/pkg/services/object/search/service.go index 33dae8ee2..87c371d75 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