forked from TrueCloudLab/frostfs-node
73 lines
1.3 KiB
Go
73 lines
1.3 KiB
Go
package getsvc
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
|
)
|
|
|
|
type SimpleObjectWriter struct {
|
|
obj *object.Object
|
|
|
|
pld []byte
|
|
}
|
|
|
|
type partWriter struct {
|
|
ObjectWriter
|
|
|
|
headWriter HeaderWriter
|
|
|
|
chunkWriter ChunkWriter
|
|
}
|
|
|
|
type hasherWrapper struct {
|
|
hash io.Writer
|
|
}
|
|
|
|
func NewSimpleObjectWriter() *SimpleObjectWriter {
|
|
return &SimpleObjectWriter{
|
|
obj: object.New(),
|
|
}
|
|
}
|
|
|
|
func (s *SimpleObjectWriter) WriteHeader(_ context.Context, obj *object.Object) error {
|
|
s.obj = obj
|
|
|
|
s.pld = make([]byte, 0, obj.PayloadSize())
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *SimpleObjectWriter) WriteChunk(_ context.Context, p []byte) error {
|
|
s.pld = append(s.pld, p...)
|
|
return nil
|
|
}
|
|
|
|
func (s *SimpleObjectWriter) Object() *object.Object {
|
|
if len(s.pld) > 0 {
|
|
s.obj.SetPayload(s.pld)
|
|
}
|
|
|
|
return s.obj
|
|
}
|
|
|
|
func (w *partWriter) WriteChunk(ctx context.Context, p []byte) error {
|
|
return w.chunkWriter.WriteChunk(ctx, p)
|
|
}
|
|
|
|
func (w *partWriter) WriteHeader(ctx context.Context, o *object.Object) error {
|
|
return w.headWriter.WriteHeader(ctx, o)
|
|
}
|
|
|
|
func payloadOnlyObject(payload []byte) *object.Object {
|
|
obj := object.New()
|
|
obj.SetPayload(payload)
|
|
|
|
return obj
|
|
}
|
|
|
|
func (h *hasherWrapper) WriteChunk(_ context.Context, p []byte) error {
|
|
_, err := h.hash.Write(p)
|
|
return err
|
|
}
|