Skip to content
Open
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
44 changes: 44 additions & 0 deletions object/transformer/channel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package transformer

import (
objectSDK "github.com/TrueCloudLab/frostfs-sdk-go/object"
"github.com/nspcc-dev/neo-go/pkg/util/slice"
)

type chanTarget struct {
header *objectSDK.Object
payload []byte
ch chan<- *objectSDK.Object
}

// NewChannelTarget returns ObjectTarget which writes
// object parts to a provided channel.
func NewChannelTarget(ch chan<- *objectSDK.Object) ObjectTarget {
return &chanTarget{
ch: ch,
}
}

// WriteHeader implements the ObjectTarget interface.
func (c *chanTarget) WriteHeader(object *objectSDK.Object) error {
c.header = object
return nil
}

// Write implements the ObjectTarget interface.
func (c *chanTarget) Write(p []byte) (n int, err error) {
c.payload = append(c.payload, p...)
return len(p), nil
}

// Close implements the ObjectTarget interface.
func (c *chanTarget) Close() (*AccessIdentifiers, error) {
if len(c.payload) != 0 {
c.header.SetPayload(slice.Copy(c.payload))
}
c.ch <- c.header

c.header = nil
c.payload = nil
return new(AccessIdentifiers), nil
}
55 changes: 55 additions & 0 deletions object/transformer/channel_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package transformer

import (
"crypto/rand"
"testing"

cidtest "github.com/TrueCloudLab/frostfs-sdk-go/container/id/test"
objectSDK "github.com/TrueCloudLab/frostfs-sdk-go/object"
"github.com/TrueCloudLab/frostfs-sdk-go/version"
"github.com/stretchr/testify/require"
)

func TestChannelTarget(t *testing.T) {
const maxSize = 100

ch := make(chan *objectSDK.Object, 10)
tt := new(testTarget)

chTarget, _ := newPayloadSizeLimiter(maxSize, NewChannelTarget(ch))
testTarget, _ := newPayloadSizeLimiter(maxSize, tt)

ver := version.Current()
cnr := cidtest.ID()
hdr := objectSDK.New()
hdr.SetContainerID(cnr)
hdr.SetType(objectSDK.TypeRegular)
hdr.SetVersion(&ver)

payload := make([]byte, maxSize*2+maxSize/2)
_, _ = rand.Read(payload)

expectedIDs := writeObject(t, testTarget, hdr, payload)
actualIDs := writeObject(t, chTarget, hdr, payload)
_ = expectedIDs
_ = actualIDs
//require.Equal(t, expectedIDs, actualIDs)

for i := range tt.objects {
select {
case obj := <-ch:
// Because of the split ID objects can be different.
// However, payload and attributes must be the same.
require.Equal(t, tt.objects[i].Payload(), obj.Payload())
require.Equal(t, tt.objects[i].Attributes(), obj.Attributes())
default:
require.FailNow(t, "received less parts than expected")
}
}

select {
case <-ch:
require.FailNow(t, "received more parts than expected")
default:
}
}
38 changes: 38 additions & 0 deletions object/transformer/hasher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package transformer

import (
"crypto/sha256"
"hash"

"github.com/TrueCloudLab/frostfs-sdk-go/checksum"
objectSDK "github.com/TrueCloudLab/frostfs-sdk-go/object"
"github.com/TrueCloudLab/tzhash/tz"
)

type payloadChecksumHasher struct {
hasher hash.Hash
typ checksum.Type
}

func (h payloadChecksumHasher) writeChecksum(obj *objectSDK.Object) {
switch h.typ {
case checksum.SHA256:
csSHA := [sha256.Size]byte{}
h.hasher.Sum(csSHA[:0])
Comment on lines +20 to +21
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too tricky i think.


var cs checksum.Checksum
cs.SetSHA256(csSHA)

obj.SetPayloadChecksum(cs)
case checksum.TZ:
csTZ := [tz.Size]byte{}
h.hasher.Sum(csTZ[:0])

var cs checksum.Checksum
cs.SetTillichZemor(csTZ)

obj.SetPayloadHomomorphicHash(cs)
default:
panic("unreachable")
}
}
Loading