2020-09-21 14:31:31 +00:00
|
|
|
package putsvc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-05-18 08:12:51 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-09-21 14:31:31 +00:00
|
|
|
|
2024-08-30 09:09:14 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/object/common/target"
|
|
|
|
objectwriter "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/object/common/writer"
|
2023-04-27 15:46:42 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/transformer"
|
2020-09-21 14:31:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Streamer struct {
|
2024-08-30 09:09:14 +00:00
|
|
|
*objectwriter.Config
|
2023-03-14 10:31:30 +00:00
|
|
|
|
2023-07-11 14:32:00 +00:00
|
|
|
target transformer.ChunkedObjectWriter
|
2020-09-21 14:31:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var errNotInit = errors.New("stream not initialized")
|
|
|
|
|
|
|
|
var errInitRecall = errors.New("init recall")
|
|
|
|
|
2023-04-12 14:01:29 +00:00
|
|
|
func (p *Streamer) Init(ctx context.Context, prm *PutInitPrm) error {
|
2020-09-21 14:31:31 +00:00
|
|
|
if p.target != nil {
|
|
|
|
return errInitRecall
|
|
|
|
}
|
|
|
|
|
2024-08-30 09:09:14 +00:00
|
|
|
// initialize destination target
|
|
|
|
prmTarget := &objectwriter.Params{
|
|
|
|
Config: p.Config,
|
|
|
|
Common: prm.common,
|
|
|
|
Header: prm.hdr,
|
|
|
|
Container: prm.cnr,
|
|
|
|
TraverseOpts: prm.traverseOpts,
|
2024-09-24 08:46:15 +00:00
|
|
|
Relay: prm.relay,
|
2022-04-04 13:04:18 +00:00
|
|
|
}
|
2020-09-21 14:31:31 +00:00
|
|
|
|
|
|
|
var err error
|
2024-08-30 09:09:14 +00:00
|
|
|
p.target, err = target.New(prmTarget)
|
2020-09-21 14:31:31 +00:00
|
|
|
if err != nil {
|
2024-08-30 09:09:14 +00:00
|
|
|
return fmt.Errorf("(%T) could not initialize object target: %w", p, err)
|
2022-05-12 16:37:46 +00:00
|
|
|
}
|
|
|
|
|
2024-08-30 09:09:14 +00:00
|
|
|
if err := p.target.WriteHeader(ctx, prm.hdr); err != nil {
|
|
|
|
return fmt.Errorf("(%T) could not write header to target: %w", p, err)
|
2020-09-21 14:31:31 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-03 11:23:53 +00:00
|
|
|
func (p *Streamer) SendChunk(ctx context.Context, prm *PutChunkPrm) error {
|
2020-09-21 14:31:31 +00:00
|
|
|
if p.target == nil {
|
|
|
|
return errNotInit
|
|
|
|
}
|
|
|
|
|
2023-04-03 11:23:53 +00:00
|
|
|
if _, err := p.target.Write(ctx, prm.chunk); err != nil {
|
2021-05-19 15:36:03 +00:00
|
|
|
return fmt.Errorf("(%T) could not write payload chunk to target: %w", p, err)
|
|
|
|
}
|
2020-09-21 14:31:31 +00:00
|
|
|
|
2021-05-19 15:36:03 +00:00
|
|
|
return nil
|
2020-09-21 14:31:31 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 11:23:53 +00:00
|
|
|
func (p *Streamer) Close(ctx context.Context) (*PutResponse, error) {
|
2020-09-21 14:31:31 +00:00
|
|
|
if p.target == nil {
|
|
|
|
return nil, errNotInit
|
|
|
|
}
|
|
|
|
|
2023-04-03 11:23:53 +00:00
|
|
|
ids, err := p.target.Close(ctx)
|
2020-09-21 14:31:31 +00:00
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("(%T) could not close object target: %w", p, err)
|
2020-09-21 14:31:31 +00:00
|
|
|
}
|
|
|
|
|
2023-04-27 15:46:42 +00:00
|
|
|
id := ids.ParentID
|
2022-05-31 17:00:41 +00:00
|
|
|
if id != nil {
|
|
|
|
return &PutResponse{
|
|
|
|
id: *id,
|
|
|
|
}, nil
|
2020-09-21 14:31:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &PutResponse{
|
2023-04-27 15:46:42 +00:00
|
|
|
id: ids.SelfID,
|
2020-09-21 14:31:31 +00:00
|
|
|
}, nil
|
|
|
|
}
|