[#658] client: Refactor PrmObjectGet/Head/Range usage

Signed-off-by: Airat Arifullin <a.arifullin@yadro.com>
This commit is contained in:
Airat Arifullin 2023-08-29 13:25:54 +03:00 committed by Evgenii Stratonikov
parent 1daef2ceeb
commit 806cc13d9f
4 changed files with 71 additions and 113 deletions

View file

@ -563,28 +563,19 @@ func (x GetObjectRes) Header() *objectSDK.Object {
// Returns any error which prevented the operation from completing correctly in error return. // Returns any error which prevented the operation from completing correctly in error return.
// For raw reading, returns *object.SplitInfoError error if object is virtual. // For raw reading, returns *object.SplitInfoError error if object is virtual.
func GetObject(ctx context.Context, prm GetObjectPrm) (*GetObjectRes, error) { func GetObject(ctx context.Context, prm GetObjectPrm) (*GetObjectRes, error) {
var getPrm client.PrmObjectGet cnr := prm.objAddr.Container()
getPrm.FromContainer(prm.objAddr.Container()) obj := prm.objAddr.Object()
getPrm.ByID(prm.objAddr.Object())
if prm.sessionToken != nil { getPrm := client.PrmObjectGet{
getPrm.WithinSession(*prm.sessionToken) XHeaders: prm.xHeaders,
BearerToken: prm.bearerToken,
Session: prm.sessionToken,
Raw: prm.raw,
Local: prm.local,
ContainerID: &cnr,
ObjectID: &obj,
} }
if prm.bearerToken != nil {
getPrm.WithBearerToken(*prm.bearerToken)
}
if prm.raw {
getPrm.MarkRaw()
}
if prm.local {
getPrm.MarkLocal()
}
getPrm.WithXHeaders(prm.xHeaders...)
rdr, err := prm.cli.ObjectGetInit(ctx, getPrm) rdr, err := prm.cli.ObjectGetInit(ctx, getPrm)
if err != nil { if err != nil {
return nil, fmt.Errorf("init object reading on client: %w", err) return nil, fmt.Errorf("init object reading on client: %w", err)
@ -639,29 +630,20 @@ func (x HeadObjectRes) Header() *objectSDK.Object {
// Returns any error which prevented the operation from completing correctly in error return. // Returns any error which prevented the operation from completing correctly in error return.
// For raw reading, returns *object.SplitInfoError error if object is virtual. // For raw reading, returns *object.SplitInfoError error if object is virtual.
func HeadObject(ctx context.Context, prm HeadObjectPrm) (*HeadObjectRes, error) { func HeadObject(ctx context.Context, prm HeadObjectPrm) (*HeadObjectRes, error) {
var cliPrm client.PrmObjectHead cnr := prm.objAddr.Container()
cliPrm.FromContainer(prm.objAddr.Container()) obj := prm.objAddr.Object()
cliPrm.ByID(prm.objAddr.Object())
if prm.sessionToken != nil { headPrm := client.PrmObjectHead{
cliPrm.WithinSession(*prm.sessionToken) XHeaders: prm.xHeaders,
BearerToken: prm.bearerToken,
Session: prm.sessionToken,
Raw: prm.raw,
Local: prm.local,
ContainerID: &cnr,
ObjectID: &obj,
} }
if prm.bearerToken != nil { res, err := prm.cli.ObjectHead(ctx, headPrm)
cliPrm.WithBearerToken(*prm.bearerToken)
}
if prm.raw {
cliPrm.MarkRaw()
}
if prm.local {
cliPrm.MarkLocal()
}
cliPrm.WithXHeaders(prm.xHeaders...)
res, err := prm.cli.ObjectHead(ctx, cliPrm)
if err != nil { if err != nil {
return nil, fmt.Errorf("read object header via client: %w", err) return nil, fmt.Errorf("read object header via client: %w", err)
} }
@ -862,32 +844,22 @@ type PayloadRangeRes struct{}
// Returns any error which prevented the operation from completing correctly in error return. // Returns any error which prevented the operation from completing correctly in error return.
// For raw reading, returns *object.SplitInfoError error if object is virtual. // For raw reading, returns *object.SplitInfoError error if object is virtual.
func PayloadRange(ctx context.Context, prm PayloadRangePrm) (*PayloadRangeRes, error) { func PayloadRange(ctx context.Context, prm PayloadRangePrm) (*PayloadRangeRes, error) {
var cliPrm client.PrmObjectRange cnr := prm.objAddr.Container()
cliPrm.FromContainer(prm.objAddr.Container()) obj := prm.objAddr.Object()
cliPrm.ByID(prm.objAddr.Object())
if prm.sessionToken != nil { rangePrm := client.PrmObjectRange{
cliPrm.WithinSession(*prm.sessionToken) XHeaders: prm.xHeaders,
BearerToken: prm.bearerToken,
Session: prm.sessionToken,
Raw: prm.raw,
Local: prm.local,
ContainerID: &cnr,
ObjectID: &obj,
Offset: prm.rng.GetOffset(),
Length: prm.rng.GetLength(),
} }
if prm.bearerToken != nil { rdr, err := prm.cli.ObjectRangeInit(ctx, rangePrm)
cliPrm.WithBearerToken(*prm.bearerToken)
}
if prm.raw {
cliPrm.MarkRaw()
}
if prm.local {
cliPrm.MarkLocal()
}
cliPrm.SetOffset(prm.rng.GetOffset())
cliPrm.SetLength(prm.rng.GetLength())
cliPrm.WithXHeaders(prm.xHeaders...)
rdr, err := prm.cli.ObjectRangeInit(ctx, cliPrm)
if err != nil { if err != nil {
return nil, fmt.Errorf("init payload reading: %w", err) return nil, fmt.Errorf("init payload reading: %w", err)
} }

2
go.mod
View file

@ -6,7 +6,7 @@ require (
git.frostfs.info/TrueCloudLab/frostfs-api-go/v2 v2.15.1-0.20230802075510-964c3edb3f44 git.frostfs.info/TrueCloudLab/frostfs-api-go/v2 v2.15.1-0.20230802075510-964c3edb3f44
git.frostfs.info/TrueCloudLab/frostfs-contract v0.0.0-20230627134746-36f3d39c406a git.frostfs.info/TrueCloudLab/frostfs-contract v0.0.0-20230627134746-36f3d39c406a
git.frostfs.info/TrueCloudLab/frostfs-observability v0.0.0-20230531082742-c97d21411eb6 git.frostfs.info/TrueCloudLab/frostfs-observability v0.0.0-20230531082742-c97d21411eb6
git.frostfs.info/TrueCloudLab/frostfs-sdk-go v0.0.0-20230821090303-202412230a05 git.frostfs.info/TrueCloudLab/frostfs-sdk-go v0.0.0-20230828082657-84e7e69f98ac
git.frostfs.info/TrueCloudLab/hrw v1.2.1 git.frostfs.info/TrueCloudLab/hrw v1.2.1
git.frostfs.info/TrueCloudLab/tzhash v1.8.0 git.frostfs.info/TrueCloudLab/tzhash v1.8.0
github.com/cheggaaa/pb v1.0.29 github.com/cheggaaa/pb v1.0.29

BIN
go.sum

Binary file not shown.

View file

@ -88,7 +88,7 @@ func (x *readPrmCommon) SetNetmapEpoch(_ uint64) {
type GetObjectPrm struct { type GetObjectPrm struct {
readPrmCommon readPrmCommon
cliPrm client.PrmObjectGet ClientParams client.PrmObjectGet
obj oid.ID obj oid.ID
} }
@ -97,7 +97,7 @@ type GetObjectPrm struct {
// //
// By default request will not be raw. // By default request will not be raw.
func (x *GetObjectPrm) SetRawFlag() { func (x *GetObjectPrm) SetRawFlag() {
x.cliPrm.MarkRaw() x.ClientParams.Raw = true
} }
// SetAddress sets object address. // SetAddress sets object address.
@ -105,8 +105,10 @@ func (x *GetObjectPrm) SetRawFlag() {
// Required parameter. // Required parameter.
func (x *GetObjectPrm) SetAddress(addr oid.Address) { func (x *GetObjectPrm) SetAddress(addr oid.Address) {
x.obj = addr.Object() x.obj = addr.Object()
x.cliPrm.FromContainer(addr.Container()) cnr := addr.Container()
x.cliPrm.ByID(x.obj)
x.ClientParams.ContainerID = &cnr
x.ClientParams.ObjectID = &x.obj
} }
// GetObjectRes groups the resulting values of GetObject operation. // GetObjectRes groups the resulting values of GetObject operation.
@ -134,23 +136,15 @@ func GetObject(ctx context.Context, prm GetObjectPrm) (*GetObjectRes, error) {
// request will almost definitely fail. The case can occur, for example, // request will almost definitely fail. The case can occur, for example,
// when session is bound to the parent object and child object is requested. // when session is bound to the parent object and child object is requested.
if prm.tokenSession != nil && prm.tokenSession.AssertObject(prm.obj) { if prm.tokenSession != nil && prm.tokenSession.AssertObject(prm.obj) {
prm.cliPrm.WithinSession(*prm.tokenSession) prm.ClientParams.Session = prm.tokenSession
} }
if prm.tokenBearer != nil { prm.ClientParams.XHeaders = prm.xHeaders
prm.cliPrm.WithBearerToken(*prm.tokenBearer) prm.ClientParams.BearerToken = prm.tokenBearer
} prm.ClientParams.Local = prm.local
prm.ClientParams.Key = prm.key
if prm.local { rdr, err := prm.cli.ObjectGetInit(ctx, prm.ClientParams)
prm.cliPrm.MarkLocal()
}
prm.cliPrm.WithXHeaders(prm.xHeaders...)
if prm.key != nil {
prm.cliPrm.UseKey(*prm.key)
}
rdr, err := prm.cli.ObjectGetInit(ctx, prm.cliPrm)
if err != nil { if err != nil {
return nil, fmt.Errorf("init object reading: %w", err) return nil, fmt.Errorf("init object reading: %w", err)
} }
@ -187,7 +181,7 @@ func GetObject(ctx context.Context, prm GetObjectPrm) (*GetObjectRes, error) {
type HeadObjectPrm struct { type HeadObjectPrm struct {
readPrmCommon readPrmCommon
cliPrm client.PrmObjectHead ClientParams client.PrmObjectHead
obj oid.ID obj oid.ID
} }
@ -196,7 +190,7 @@ type HeadObjectPrm struct {
// //
// By default request will not be raw. // By default request will not be raw.
func (x *HeadObjectPrm) SetRawFlag() { func (x *HeadObjectPrm) SetRawFlag() {
x.cliPrm.MarkRaw() x.ClientParams.Raw = true
} }
// SetAddress sets object address. // SetAddress sets object address.
@ -204,8 +198,10 @@ func (x *HeadObjectPrm) SetRawFlag() {
// Required parameter. // Required parameter.
func (x *HeadObjectPrm) SetAddress(addr oid.Address) { func (x *HeadObjectPrm) SetAddress(addr oid.Address) {
x.obj = addr.Object() x.obj = addr.Object()
x.cliPrm.FromContainer(addr.Container()) cnr := addr.Container()
x.cliPrm.ByID(x.obj)
x.ClientParams.ContainerID = &cnr
x.ClientParams.ObjectID = &x.obj
} }
// HeadObjectRes groups the resulting values of GetObject operation. // HeadObjectRes groups the resulting values of GetObject operation.
@ -230,22 +226,16 @@ func (x HeadObjectRes) Header() *objectSDK.Object {
// //
// HeadObject ignores the provided session if it is not related to the requested objectSDK. // HeadObject ignores the provided session if it is not related to the requested objectSDK.
func HeadObject(ctx context.Context, prm HeadObjectPrm) (*HeadObjectRes, error) { func HeadObject(ctx context.Context, prm HeadObjectPrm) (*HeadObjectRes, error) {
if prm.local {
prm.cliPrm.MarkLocal()
}
// see details in same statement of GetObject // see details in same statement of GetObject
if prm.tokenSession != nil && prm.tokenSession.AssertObject(prm.obj) { if prm.tokenSession != nil && prm.tokenSession.AssertObject(prm.obj) {
prm.cliPrm.WithinSession(*prm.tokenSession) prm.ClientParams.Session = prm.tokenSession
} }
if prm.tokenBearer != nil { prm.ClientParams.BearerToken = prm.tokenBearer
prm.cliPrm.WithBearerToken(*prm.tokenBearer) prm.ClientParams.Local = prm.local
} prm.ClientParams.XHeaders = prm.xHeaders
prm.cliPrm.WithXHeaders(prm.xHeaders...) cliRes, err := prm.cli.ObjectHead(ctx, prm.ClientParams)
cliRes, err := prm.cli.ObjectHead(ctx, prm.cliPrm)
if err == nil { if err == nil {
// pull out an error from status // pull out an error from status
err = apistatus.ErrFromStatus(cliRes.Status()) err = apistatus.ErrFromStatus(cliRes.Status())
@ -272,7 +262,7 @@ type PayloadRangePrm struct {
ln uint64 ln uint64
cliPrm client.PrmObjectRange ClientParams client.PrmObjectRange
obj oid.ID obj oid.ID
} }
@ -281,7 +271,7 @@ type PayloadRangePrm struct {
// //
// By default request will not be raw. // By default request will not be raw.
func (x *PayloadRangePrm) SetRawFlag() { func (x *PayloadRangePrm) SetRawFlag() {
x.cliPrm.MarkRaw() x.ClientParams.Raw = true
} }
// SetAddress sets object address. // SetAddress sets object address.
@ -289,15 +279,17 @@ func (x *PayloadRangePrm) SetRawFlag() {
// Required parameter. // Required parameter.
func (x *PayloadRangePrm) SetAddress(addr oid.Address) { func (x *PayloadRangePrm) SetAddress(addr oid.Address) {
x.obj = addr.Object() x.obj = addr.Object()
x.cliPrm.FromContainer(addr.Container()) cnr := addr.Container()
x.cliPrm.ByID(x.obj)
x.ClientParams.ContainerID = &cnr
x.ClientParams.ObjectID = &x.obj
} }
// SetRange range of the object payload to be read. // SetRange range of the object payload to be read.
// //
// Required parameter. // Required parameter.
func (x *PayloadRangePrm) SetRange(rng *objectSDK.Range) { func (x *PayloadRangePrm) SetRange(rng *objectSDK.Range) {
x.cliPrm.SetOffset(rng.GetOffset()) x.ClientParams.Offset = rng.GetOffset()
x.ln = rng.GetLength() x.ln = rng.GetLength()
} }
@ -329,23 +321,17 @@ const maxInitialBufferSize = 1024 * 1024 // 1 MiB
// //
// PayloadRange ignores the provided session if it is not related to the requested objectSDK. // PayloadRange ignores the provided session if it is not related to the requested objectSDK.
func PayloadRange(ctx context.Context, prm PayloadRangePrm) (*PayloadRangeRes, error) { func PayloadRange(ctx context.Context, prm PayloadRangePrm) (*PayloadRangeRes, error) {
if prm.local {
prm.cliPrm.MarkLocal()
}
// see details in same statement of GetObject // see details in same statement of GetObject
if prm.tokenSession != nil && prm.tokenSession.AssertObject(prm.obj) { if prm.tokenSession != nil && prm.tokenSession.AssertObject(prm.obj) {
prm.cliPrm.WithinSession(*prm.tokenSession) prm.ClientParams.Session = prm.tokenSession
} }
if prm.tokenBearer != nil { prm.ClientParams.XHeaders = prm.xHeaders
prm.cliPrm.WithBearerToken(*prm.tokenBearer) prm.ClientParams.BearerToken = prm.tokenBearer
} prm.ClientParams.Local = prm.local
prm.ClientParams.Length = prm.ln
prm.cliPrm.SetLength(prm.ln) rdr, err := prm.cli.ObjectRangeInit(ctx, prm.ClientParams)
prm.cliPrm.WithXHeaders(prm.xHeaders...)
rdr, err := prm.cli.ObjectRangeInit(ctx, prm.cliPrm)
if err != nil { if err != nil {
return nil, fmt.Errorf("init payload reading: %w", err) return nil, fmt.Errorf("init payload reading: %w", err)
} }