forked from TrueCloudLab/frostfs-node
Airat Arifullin
9b13a18aac
* Update version within go.mod; * Fix deprecated frostfs-api-go/v2 package and use frostfs-sdk-go/api instead. Signed-off-by: Airat Arifullin <a.arifullin@yadro.com>
186 lines
4.4 KiB
Go
186 lines
4.4 KiB
Go
package object
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/util/response"
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/object"
|
|
)
|
|
|
|
type ResponseService struct {
|
|
respSvc *response.Service
|
|
|
|
svc ServiceServer
|
|
}
|
|
|
|
type searchStreamResponser struct {
|
|
SearchStream
|
|
|
|
respSvc *response.Service
|
|
}
|
|
|
|
type getStreamResponser struct {
|
|
GetObjectStream
|
|
|
|
respSvc *response.Service
|
|
}
|
|
|
|
type getRangeStreamResponser struct {
|
|
GetObjectRangeStream
|
|
|
|
respSvc *response.Service
|
|
}
|
|
|
|
type putStreamResponser struct {
|
|
stream PutObjectStream
|
|
respSvc *response.Service
|
|
}
|
|
|
|
type patchStreamResponser struct {
|
|
stream PatchObjectStream
|
|
respSvc *response.Service
|
|
}
|
|
|
|
// NewResponseService returns object service instance that passes internal service
|
|
// call to response service.
|
|
func NewResponseService(objSvc ServiceServer, respSvc *response.Service) *ResponseService {
|
|
return &ResponseService{
|
|
respSvc: respSvc,
|
|
svc: objSvc,
|
|
}
|
|
}
|
|
|
|
func (s *getStreamResponser) Send(resp *object.GetResponse) error {
|
|
s.respSvc.SetMeta(resp)
|
|
return s.GetObjectStream.Send(resp)
|
|
}
|
|
|
|
func (s *ResponseService) Get(req *object.GetRequest, stream GetObjectStream) error {
|
|
return s.svc.Get(req, &getStreamResponser{
|
|
GetObjectStream: stream,
|
|
respSvc: s.respSvc,
|
|
})
|
|
}
|
|
|
|
func (s *putStreamResponser) Send(ctx context.Context, req *object.PutRequest) error {
|
|
if err := s.stream.Send(ctx, req); err != nil {
|
|
return fmt.Errorf("could not send the request: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *putStreamResponser) CloseAndRecv(ctx context.Context) (*object.PutResponse, error) {
|
|
r, err := s.stream.CloseAndRecv(ctx)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("could not close stream and receive response: %w", err)
|
|
}
|
|
|
|
s.respSvc.SetMeta(r)
|
|
return r, nil
|
|
}
|
|
|
|
func (s *ResponseService) Put() (PutObjectStream, error) {
|
|
stream, err := s.svc.Put()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("could not create Put object streamer: %w", err)
|
|
}
|
|
|
|
return &putStreamResponser{
|
|
stream: stream,
|
|
respSvc: s.respSvc,
|
|
}, nil
|
|
}
|
|
|
|
func (s *patchStreamResponser) Send(ctx context.Context, req *object.PatchRequest) error {
|
|
if err := s.stream.Send(ctx, req); err != nil {
|
|
return fmt.Errorf("could not send the request: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *patchStreamResponser) CloseAndRecv(ctx context.Context) (*object.PatchResponse, error) {
|
|
r, err := s.stream.CloseAndRecv(ctx)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("could not close stream and receive response: %w", err)
|
|
}
|
|
|
|
s.respSvc.SetMeta(r)
|
|
return r, nil
|
|
}
|
|
|
|
func (s *ResponseService) Patch() (PatchObjectStream, error) {
|
|
stream, err := s.svc.Patch()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("could not create Put object streamer: %w", err)
|
|
}
|
|
|
|
return &patchStreamResponser{
|
|
stream: stream,
|
|
respSvc: s.respSvc,
|
|
}, nil
|
|
}
|
|
|
|
func (s *ResponseService) PutSingle(ctx context.Context, req *object.PutSingleRequest) (*object.PutSingleResponse, error) {
|
|
resp, err := s.svc.PutSingle(ctx, req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
s.respSvc.SetMeta(resp)
|
|
return resp, nil
|
|
}
|
|
|
|
func (s *ResponseService) Head(ctx context.Context, req *object.HeadRequest) (*object.HeadResponse, error) {
|
|
resp, err := s.svc.Head(ctx, req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
s.respSvc.SetMeta(resp)
|
|
return resp, nil
|
|
}
|
|
|
|
func (s *searchStreamResponser) Send(resp *object.SearchResponse) error {
|
|
s.respSvc.SetMeta(resp)
|
|
return s.SearchStream.Send(resp)
|
|
}
|
|
|
|
func (s *ResponseService) Search(req *object.SearchRequest, stream SearchStream) error {
|
|
return s.svc.Search(req, &searchStreamResponser{
|
|
SearchStream: stream,
|
|
respSvc: s.respSvc,
|
|
})
|
|
}
|
|
|
|
func (s *ResponseService) Delete(ctx context.Context, req *object.DeleteRequest) (*object.DeleteResponse, error) {
|
|
resp, err := s.svc.Delete(ctx, req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
s.respSvc.SetMeta(resp)
|
|
return resp, nil
|
|
}
|
|
|
|
func (s *getRangeStreamResponser) Send(resp *object.GetRangeResponse) error {
|
|
s.respSvc.SetMeta(resp)
|
|
return s.GetObjectRangeStream.Send(resp)
|
|
}
|
|
|
|
func (s *ResponseService) GetRange(req *object.GetRangeRequest, stream GetObjectRangeStream) error {
|
|
return s.svc.GetRange(req, &getRangeStreamResponser{
|
|
GetObjectRangeStream: stream,
|
|
respSvc: s.respSvc,
|
|
})
|
|
}
|
|
|
|
func (s *ResponseService) GetRangeHash(ctx context.Context, req *object.GetRangeHashRequest) (*object.GetRangeHashResponse, error) {
|
|
resp, err := s.svc.GetRangeHash(ctx, req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
s.respSvc.SetMeta(resp)
|
|
return resp, nil
|
|
}
|