2020-12-02 23:45:25 +00:00
|
|
|
package getsvc
|
|
|
|
|
|
|
|
import (
|
2023-03-09 08:02:27 +00:00
|
|
|
"context"
|
2021-01-11 13:50:49 +00:00
|
|
|
"io"
|
2020-12-02 23:45:25 +00:00
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
2020-12-02 23:45:25 +00:00
|
|
|
)
|
|
|
|
|
2020-12-09 10:32:33 +00:00
|
|
|
type SimpleObjectWriter struct {
|
2022-03-03 14:19:05 +00:00
|
|
|
obj *object.Object
|
2020-12-02 23:45:25 +00:00
|
|
|
|
2020-12-07 17:49:47 +00:00
|
|
|
pld []byte
|
2020-12-02 23:45:25 +00:00
|
|
|
}
|
|
|
|
|
2020-12-09 10:32:33 +00:00
|
|
|
type partWriter struct {
|
2020-12-07 17:49:47 +00:00
|
|
|
ObjectWriter
|
|
|
|
|
2020-12-09 10:32:33 +00:00
|
|
|
headWriter HeaderWriter
|
|
|
|
|
2020-12-07 17:49:47 +00:00
|
|
|
chunkWriter ChunkWriter
|
|
|
|
}
|
|
|
|
|
2020-12-08 16:18:24 +00:00
|
|
|
type hasherWrapper struct {
|
2021-01-11 13:50:49 +00:00
|
|
|
hash io.Writer
|
2020-12-08 16:18:24 +00:00
|
|
|
}
|
|
|
|
|
2020-12-09 10:32:33 +00:00
|
|
|
func NewSimpleObjectWriter() *SimpleObjectWriter {
|
|
|
|
return &SimpleObjectWriter{
|
2022-03-03 14:19:05 +00:00
|
|
|
obj: object.New(),
|
2020-12-07 17:49:47 +00:00
|
|
|
}
|
2020-12-02 23:45:25 +00:00
|
|
|
}
|
|
|
|
|
2023-03-09 08:02:27 +00:00
|
|
|
func (s *SimpleObjectWriter) WriteHeader(_ context.Context, obj *object.Object) error {
|
2022-03-03 14:19:05 +00:00
|
|
|
s.obj = obj
|
2020-12-02 23:45:25 +00:00
|
|
|
|
2020-12-07 17:49:47 +00:00
|
|
|
s.pld = make([]byte, 0, obj.PayloadSize())
|
2020-12-02 23:45:25 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-09 08:02:27 +00:00
|
|
|
func (s *SimpleObjectWriter) WriteChunk(_ context.Context, p []byte) error {
|
2020-12-07 17:49:47 +00:00
|
|
|
s.pld = append(s.pld, p...)
|
2020-12-02 23:45:25 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-09 10:32:33 +00:00
|
|
|
func (s *SimpleObjectWriter) Object() *object.Object {
|
2020-12-07 17:49:47 +00:00
|
|
|
if len(s.pld) > 0 {
|
|
|
|
s.obj.SetPayload(s.pld)
|
2020-12-02 23:45:25 +00:00
|
|
|
}
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
return s.obj
|
2020-12-02 23:45:25 +00:00
|
|
|
}
|
|
|
|
|
2023-03-09 08:02:27 +00:00
|
|
|
func (w *partWriter) WriteChunk(ctx context.Context, p []byte) error {
|
|
|
|
return w.chunkWriter.WriteChunk(ctx, p)
|
2020-12-02 23:45:25 +00:00
|
|
|
}
|
2020-12-07 17:49:47 +00:00
|
|
|
|
2023-03-09 08:02:27 +00:00
|
|
|
func (w *partWriter) WriteHeader(ctx context.Context, o *object.Object) error {
|
|
|
|
return w.headWriter.WriteHeader(ctx, o)
|
2020-12-07 17:49:47 +00:00
|
|
|
}
|
|
|
|
|
2023-03-09 08:02:27 +00:00
|
|
|
func (h *hasherWrapper) WriteChunk(_ context.Context, p []byte) error {
|
2020-12-08 16:18:24 +00:00
|
|
|
_, err := h.hash.Write(p)
|
|
|
|
return err
|
|
|
|
}
|