forked from TrueCloudLab/frostfs-sdk-go
[#19] transformer: Add a target which sends parts to a channel
Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
This commit is contained in:
parent
e45647de3c
commit
94c0a607b5
2 changed files with 99 additions and 0 deletions
44
object/transformer/channel.go
Normal file
44
object/transformer/channel.go
Normal file
|
@ -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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue