168 lines
3.4 KiB
Go
168 lines
3.4 KiB
Go
package frostfs
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/internal/data"
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
type GetFrostFSParams struct {
|
|
// payload range
|
|
Off, Ln uint64
|
|
|
|
Oid oid.ID
|
|
BktInfo *data.BucketInfo
|
|
}
|
|
|
|
type PartObj struct {
|
|
OID oid.ID
|
|
Size uint64
|
|
}
|
|
|
|
type readerInitiator interface {
|
|
InitFrostFSObjectPayloadReader(ctx context.Context, p GetFrostFSParams) (io.Reader, error)
|
|
}
|
|
|
|
// MultiObjectReader implements io.Reader of payloads of the object list stored in the FrostFS network.
|
|
type MultiObjectReader struct {
|
|
ctx context.Context
|
|
log *zap.Logger
|
|
|
|
layer readerInitiator
|
|
|
|
startPartOffset uint64
|
|
endPartLength uint64
|
|
|
|
prm GetFrostFSParams
|
|
|
|
curIndex int
|
|
curReader io.Reader
|
|
|
|
parts []PartObj
|
|
}
|
|
|
|
type MultiObjectReaderConfig struct {
|
|
Handler readerInitiator
|
|
Log *zap.Logger
|
|
|
|
// the offset of complete object and total size to read
|
|
Off, Ln uint64
|
|
|
|
BktInfo *data.BucketInfo
|
|
Parts []PartObj
|
|
}
|
|
|
|
var (
|
|
errOffsetIsOutOfRange = errors.New("offset is out of payload range")
|
|
errLengthIsOutOfRange = errors.New("length is out of payload range")
|
|
errEmptyPartsList = errors.New("empty parts list")
|
|
errorZeroRangeLength = errors.New("zero range length")
|
|
)
|
|
|
|
func NewMultiObjectReader(ctx context.Context, cfg MultiObjectReaderConfig) (*MultiObjectReader, error) {
|
|
if len(cfg.Parts) == 0 {
|
|
return nil, errEmptyPartsList
|
|
}
|
|
|
|
r := &MultiObjectReader{
|
|
ctx: ctx,
|
|
layer: cfg.Handler,
|
|
prm: GetFrostFSParams{
|
|
BktInfo: cfg.BktInfo,
|
|
},
|
|
parts: cfg.Parts,
|
|
log: cfg.Log,
|
|
}
|
|
|
|
if cfg.Off+cfg.Ln == 0 {
|
|
return r, nil
|
|
}
|
|
|
|
if cfg.Off > 0 && cfg.Ln == 0 {
|
|
return nil, errorZeroRangeLength
|
|
}
|
|
|
|
startPartIndex, startPartOffset := findStartPart(cfg)
|
|
if startPartIndex == -1 {
|
|
return nil, errOffsetIsOutOfRange
|
|
}
|
|
r.startPartOffset = startPartOffset
|
|
|
|
endPartIndex, endPartLength := findEndPart(cfg)
|
|
if endPartIndex == -1 {
|
|
return nil, errLengthIsOutOfRange
|
|
}
|
|
r.endPartLength = endPartLength
|
|
|
|
r.parts = cfg.Parts[startPartIndex : endPartIndex+1]
|
|
|
|
return r, nil
|
|
}
|
|
|
|
func findStartPart(cfg MultiObjectReaderConfig) (index int, offset uint64) {
|
|
position := cfg.Off
|
|
for i, part := range cfg.Parts {
|
|
// Strict inequality when searching for start position to avoid reading zero length part.
|
|
if position < part.Size {
|
|
return i, position
|
|
}
|
|
position -= part.Size
|
|
}
|
|
|
|
return -1, 0
|
|
}
|
|
|
|
func findEndPart(cfg MultiObjectReaderConfig) (index int, length uint64) {
|
|
position := cfg.Off + cfg.Ln
|
|
for i, part := range cfg.Parts {
|
|
// Non-strict inequality when searching for end position to avoid out of payload range error.
|
|
if position <= part.Size {
|
|
return i, position
|
|
}
|
|
position -= part.Size
|
|
}
|
|
|
|
return -1, 0
|
|
}
|
|
|
|
func (x *MultiObjectReader) Read(p []byte) (n int, err error) {
|
|
if x.curReader != nil {
|
|
n, err = x.curReader.Read(p)
|
|
if !errors.Is(err, io.EOF) {
|
|
return n, err
|
|
}
|
|
x.curIndex++
|
|
}
|
|
|
|
if x.curIndex == len(x.parts) {
|
|
return n, io.EOF
|
|
}
|
|
|
|
x.prm.Oid = x.parts[x.curIndex].OID
|
|
|
|
if x.curIndex == 0 {
|
|
x.prm.Off = x.startPartOffset
|
|
x.prm.Ln = x.parts[x.curIndex].Size - x.startPartOffset
|
|
}
|
|
|
|
if x.curIndex == len(x.parts)-1 {
|
|
x.prm.Ln = x.endPartLength - x.prm.Off
|
|
}
|
|
|
|
x.curReader, err = x.layer.InitFrostFSObjectPayloadReader(x.ctx, x.prm)
|
|
if err != nil {
|
|
return n, fmt.Errorf("init payload reader for the next part: %w", err)
|
|
}
|
|
|
|
x.prm.Off = 0
|
|
x.prm.Ln = 0
|
|
|
|
next, err := x.Read(p[n:])
|
|
|
|
return n + next, err
|
|
}
|