From 04ea0e8f6a38c9dc9761fe1514ec6e01585d3dc1 Mon Sep 17 00:00:00 2001 From: Evgenii Baidakov Date: Thu, 4 May 2023 11:28:31 +0400 Subject: [PATCH] client: Accept rng object.Range into PrmObjectRange Signed-off-by: Evgenii Baidakov --- client/object_get.go | 10 ++++++++-- client/object_get_test.go | 36 ++++++++++++++++++++++++++++++++++++ pool/pool.go | 19 +++++++++++++------ pool/pool_test.go | 30 ++++++++++++++++++++++++++++++ 4 files changed, 87 insertions(+), 8 deletions(-) diff --git a/client/object_get.go b/client/object_get.go index 10587f7d..6511578b 100644 --- a/client/object_get.go +++ b/client/object_get.go @@ -485,17 +485,23 @@ type PrmObjectRange struct { } // SetOffset sets offset of the payload range to be read. -// Zero by default. +// Zero by default. It is an alternative to [PrmObjectRange.SetRange]. func (x *PrmObjectRange) SetOffset(off uint64) { x.rng.SetOffset(off) } // SetLength sets length of the payload range to be read. -// Must be positive. +// Must be positive. It is an alternative to [PrmObjectRange.SetRange]. func (x *PrmObjectRange) SetLength(ln uint64) { x.rng.SetLength(ln) } +// SetRange sets range of the payload to be read. +// It is an alternative to [PrmObjectRange.SetOffset], [PrmObjectRange.SetLength]. +func (x *PrmObjectRange) SetRange(rng object.Range) { + x.rng = *rng.ToV2() +} + // UseSigner specifies private signer to sign the requests. // If signer is not provided, then Client default signer is used. func (x *PrmObjectRange) UseSigner(signer neofscrypto.Signer) { diff --git a/client/object_get_test.go b/client/object_get_test.go index 13bf61ad..d967658c 100644 --- a/client/object_get_test.go +++ b/client/object_get_test.go @@ -2,10 +2,12 @@ package client import ( "bytes" + "math/rand" "testing" v2refs "github.com/nspcc-dev/neofs-api-go/v2/refs" cid "github.com/nspcc-dev/neofs-sdk-go/container/id" + "github.com/nspcc-dev/neofs-sdk-go/object" oid "github.com/nspcc-dev/neofs-sdk-go/object/id" "github.com/stretchr/testify/require" ) @@ -48,3 +50,37 @@ func TestPrmObjectRead_ByAddress(t *testing.T) { require.True(t, bytes.Equal(cidV2.GetValue(), prm.addr.GetContainerID().GetValue())) }) } + +func TestPrmObjectRange_SetRange(t *testing.T) { + var prm PrmObjectRange + + var ( + ln = rand.Uint64() + off = rand.Uint64() + rng *object.Range + ) + + t.Run("SetLength", func(t *testing.T) { + prm.SetLength(ln) + rng = object.NewRangeFromV2(&prm.rng) + + require.Equal(t, ln, rng.GetLength()) + }) + + t.Run("SetOffset", func(t *testing.T) { + prm.SetOffset(off) + rng = object.NewRangeFromV2(&prm.rng) + + require.Equal(t, off, rng.GetOffset()) + }) + + t.Run("SetRange", func(t *testing.T) { + var tmp object.Range + tmp.SetLength(ln) + tmp.SetOffset(off) + + prm.SetRange(tmp) + require.Equal(t, ln, tmp.ToV2().GetLength()) + require.Equal(t, off, tmp.ToV2().GetOffset()) + }) +} diff --git a/pool/pool.go b/pool/pool.go index 58637873..236f5c9f 100644 --- a/pool/pool.go +++ b/pool/pool.go @@ -829,8 +829,7 @@ func (c *clientWrapper) objectRange(ctx context.Context, prm PrmObjectRange) (Re var cliPrm sdkClient.PrmObjectRange cliPrm.ByAddress(prm.addr) - cliPrm.SetOffset(prm.off) - cliPrm.SetLength(prm.ln) + cliPrm.SetRange(prm.rng) if prm.stoken != nil { cliPrm.WithinSession(*prm.stoken) @@ -1298,8 +1297,8 @@ func (x *PrmObjectHead) MarkRaw() { type PrmObjectRange struct { prmCommon - addr oid.Address - off, ln uint64 + addr oid.Address + rng object.Range } // SetAddress specifies NeoFS address of the object. @@ -1308,13 +1307,21 @@ func (x *PrmObjectRange) SetAddress(addr oid.Address) { } // SetOffset sets offset of the payload range to be read. +// Zero by default. It is an alternative to [PrmObjectRange.SetRange]. func (x *PrmObjectRange) SetOffset(offset uint64) { - x.off = offset + x.rng.SetOffset(offset) } // SetLength sets length of the payload range to be read. +// Must be positive. It is an alternative to [PrmObjectRange.SetRange]. func (x *PrmObjectRange) SetLength(length uint64) { - x.ln = length + x.rng.SetLength(length) +} + +// SetRange sets range of the payload to be read. +// It is an alternative to [PrmObjectRange.SetOffset], [PrmObjectRange.SetLength]. +func (x *PrmObjectRange) SetRange(rng object.Range) { + x.rng = rng } // PrmObjectSearch groups parameters of SearchObjects operation. diff --git a/pool/pool_test.go b/pool/pool_test.go index a0f67502..930ebdb3 100644 --- a/pool/pool_test.go +++ b/pool/pool_test.go @@ -3,6 +3,7 @@ package pool import ( "context" "errors" + "math/rand" "strconv" "testing" "time" @@ -655,3 +656,32 @@ func TestSwitchAfterErrorThreshold(t *testing.T) { _, err = conn.objectGet(ctx, PrmObjectGet{}) require.NoError(t, err) } + +func TestPrmObjectRange_SetRange(t *testing.T) { + var prm PrmObjectRange + + ln := rand.Uint64() + off := rand.Uint64() + + t.Run("SetLength", func(t *testing.T) { + prm.SetLength(ln) + + require.Equal(t, ln, prm.rng.ToV2().GetLength()) + }) + + t.Run("SetOffset", func(t *testing.T) { + prm.SetOffset(off) + + require.Equal(t, off, prm.rng.ToV2().GetOffset()) + }) + + t.Run("SetRange", func(t *testing.T) { + var tmp object.Range + tmp.SetLength(ln) + tmp.SetOffset(off) + + prm.SetRange(tmp) + require.Equal(t, ln, tmp.ToV2().GetLength()) + require.Equal(t, off, tmp.ToV2().GetOffset()) + }) +}