[#39] service/object: Implement right child searcher on Search service

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2020-09-23 13:50:20 +03:00 committed by Alex Vanin
parent 39c324bd6d
commit 753a6a2de5

View file

@ -3,6 +3,7 @@ package searchsvc
import ( import (
"context" "context"
"crypto/ecdsa" "crypto/ecdsa"
"io"
"sync" "sync"
"github.com/nspcc-dev/neofs-api-go/pkg/object" "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/core/netmap"
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/localstore" "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/network"
"github.com/nspcc-dev/neofs-node/pkg/services/object/search/query/v1"
"github.com/nspcc-dev/neofs-node/pkg/util" "github.com/nspcc-dev/neofs-node/pkg/util"
"github.com/pkg/errors"
) )
type Service struct { type Service struct {
@ -61,6 +64,46 @@ func (p *Service) Search(ctx context.Context, prm *Prm) (*Streamer, error) {
}, nil }, 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 { func WithKey(v *ecdsa.PrivateKey) Option {
return func(c *cfg) { return func(c *cfg) {
c.key = v c.key = v