2018-06-27 19:42:54 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
2018-09-18 05:39:06 +01:00
|
|
|
"storj.io/storj/pkg/pb"
|
2018-06-27 19:42:54 +01:00
|
|
|
"storj.io/storj/pkg/ranger"
|
|
|
|
)
|
|
|
|
|
2018-07-16 20:22:34 +01:00
|
|
|
// Error is the error class for pieceRanger
|
2018-06-27 19:42:54 +01:00
|
|
|
var Error = errs.Class("pieceRanger error")
|
|
|
|
|
|
|
|
type pieceRanger struct {
|
2018-10-17 12:40:11 +01:00
|
|
|
c *Client
|
|
|
|
id PieceID
|
|
|
|
size int64
|
|
|
|
stream pb.PieceStoreRoutes_RetrieveClient
|
|
|
|
pba *pb.PayerBandwidthAllocation
|
|
|
|
authorization *pb.SignedMessage
|
2018-06-27 19:42:54 +01:00
|
|
|
}
|
|
|
|
|
2018-09-14 15:10:43 +01:00
|
|
|
// PieceRanger PieceRanger returns a Ranger from a PieceID.
|
2018-10-17 12:40:11 +01:00
|
|
|
func PieceRanger(ctx context.Context, c *Client, stream pb.PieceStoreRoutes_RetrieveClient, id PieceID, pba *pb.PayerBandwidthAllocation, authorization *pb.SignedMessage) (ranger.Ranger, error) {
|
2018-08-27 18:28:16 +01:00
|
|
|
piece, err := c.Meta(ctx, id)
|
2018-06-27 19:42:54 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-10-17 12:40:11 +01:00
|
|
|
return &pieceRanger{c: c, id: id, size: piece.Size, stream: stream, pba: pba, authorization: authorization}, nil
|
2018-06-27 19:42:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// PieceRangerSize creates a PieceRanger with known size.
|
|
|
|
// Use it if you know the piece size. This will safe the extra request for
|
|
|
|
// retrieving the piece size from the piece storage.
|
2018-10-17 12:40:11 +01:00
|
|
|
func PieceRangerSize(c *Client, stream pb.PieceStoreRoutes_RetrieveClient, id PieceID, size int64, pba *pb.PayerBandwidthAllocation, authorization *pb.SignedMessage) ranger.Ranger {
|
|
|
|
return &pieceRanger{c: c, id: id, size: size, stream: stream, pba: pba, authorization: authorization}
|
2018-06-27 19:42:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Size implements Ranger.Size
|
|
|
|
func (r *pieceRanger) Size() int64 {
|
|
|
|
return r.size
|
|
|
|
}
|
|
|
|
|
|
|
|
// Range implements Ranger.Range
|
|
|
|
func (r *pieceRanger) Range(ctx context.Context, offset, length int64) (io.ReadCloser, error) {
|
|
|
|
if offset < 0 {
|
|
|
|
return nil, Error.New("negative offset")
|
|
|
|
}
|
|
|
|
if length < 0 {
|
|
|
|
return nil, Error.New("negative length")
|
|
|
|
}
|
|
|
|
if offset+length > r.size {
|
|
|
|
return nil, Error.New("range beyond end")
|
|
|
|
}
|
|
|
|
if length == 0 {
|
|
|
|
return ioutil.NopCloser(bytes.NewReader([]byte{})), nil
|
|
|
|
}
|
2018-08-17 18:40:15 +01:00
|
|
|
|
|
|
|
// send piece data
|
2018-10-17 12:40:11 +01:00
|
|
|
if err := r.stream.Send(&pb.PieceRetrieval{PieceData: &pb.PieceRetrieval_PieceData{Id: r.id.String(), Size: length, Offset: offset}, Authorization: r.authorization}); err != nil {
|
2018-06-27 19:42:54 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-09-10 10:18:42 +01:00
|
|
|
return NewStreamReader(r.c, r.stream, r.pba, r.size), nil
|
2018-06-27 19:42:54 +01:00
|
|
|
}
|