Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

slicer: Do not drop link object's payload #560

Merged
merged 1 commit into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions object/slicer/slicer.go
Original file line number Diff line number Diff line change
Expand Up @@ -573,13 +573,18 @@ func (x *PayloadWriter) _writeChild(ctx context.Context, meta dynamicObjectMetad
linkObj.SetObjects(x.writtenChildren)
obj.WriteLink(linkObj)

meta.reset()
obj.ResetPreviousID()
// we reuse already written object, we should reset these fields, to eval them one more time in writeInMemObject.
obj.ResetID()
obj.SetSignature(nil)

_, err = writeInMemObject(ctx, x.signer, x.stream, obj, nil, meta, x.prmObjectPutInit)
payload := obj.Payload()
payloadAsBuffers := [][]byte{obj.Payload()}

meta.reset()
meta.accumulateNextPayloadChunk(payload)

_, err = writeInMemObject(ctx, x.signer, x.stream, obj, payloadAsBuffers, meta, x.prmObjectPutInit)
if err != nil {
return fmt.Errorf("write linking object: %w", err)
}
Expand Down
44 changes: 22 additions & 22 deletions object/slicer/slicer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,15 +479,14 @@ func (x *slicedObjectChecker) ObjectPutInit(_ context.Context, hdr object.Object
}

type writeSizeChecker struct {
tb testing.TB
hdr object.Object
limit uint64
processed uint64
base io.Writer
payloadSeen bool
tb testing.TB
hdr object.Object
limit uint64
processed uint64
base *bytes.Buffer
}

func newSizeChecker(tb testing.TB, hdr object.Object, base io.Writer, sizeLimit uint64) *writeSizeChecker {
func newSizeChecker(tb testing.TB, hdr object.Object, base *bytes.Buffer, sizeLimit uint64) *writeSizeChecker {
return &writeSizeChecker{
tb: tb,
hdr: hdr,
Expand All @@ -497,28 +496,29 @@ func newSizeChecker(tb testing.TB, hdr object.Object, base io.Writer, sizeLimit
}

func (x *writeSizeChecker) Write(p []byte) (int, error) {
if !x.payloadSeen && len(p) > 0 {
x.payloadSeen = true
}

if x.payloadSeen {
if len(x.hdr.Children()) == 0 {
// only linking objects should be streamed with
// empty payload
require.NotZero(x.tb, len(p))
} else {
// linking object should have empty payload
require.Zero(x.tb, x.hdr.PayloadSize())
}
}
require.NotZero(x.tb, len(p), "non of the split object should be empty")

n, err := x.base.Write(p)
x.processed += uint64(n)
return n, err
}

func (x *writeSizeChecker) Close() error {
require.LessOrEqual(x.tb, x.processed, x.limit, "object payload must not overflow the limit")
if x.hdr.Type() == object.TypeLink {
payload := x.base.Bytes()

var testLink object.Link
require.NoError(x.tb, testLink.Unmarshal(payload), "link object's payload must be structured")
} else {
require.LessOrEqual(x.tb, x.processed, x.limit, "object payload must not overflow the limit")
}

require.Equal(x.tb, x.processed, x.hdr.PayloadSize())

// deprecated things
require.Nil(x.tb, x.hdr.SplitID(), "no split ID should be presented")
require.Empty(x.tb, x.hdr.Children(), "no child should be stored in the headers")

return nil
}

Expand Down
Loading