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"
|
|
|
|
|
|
|
|
"storj.io/storj/pkg/ranger"
|
|
|
|
pb "storj.io/storj/protos/piecestore"
|
|
|
|
)
|
|
|
|
|
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-08-17 18:40:15 +01:00
|
|
|
c *Client
|
|
|
|
id PieceID
|
|
|
|
size int64
|
|
|
|
stream pb.PieceStoreRoutes_RetrieveClient
|
|
|
|
pba *pb.PayerBandwidthAllocation
|
2018-06-27 19:42:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// PieceRanger PieceRanger returns a RangeCloser from a PieceID.
|
2018-08-17 18:40:15 +01:00
|
|
|
func PieceRanger(ctx context.Context, c *Client, stream pb.PieceStoreRoutes_RetrieveClient, id PieceID, pba *pb.PayerBandwidthAllocation) (ranger.RangeCloser, 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-08-17 18:40:15 +01:00
|
|
|
return &pieceRanger{c: c, id: id, size: piece.Size, stream: stream, pba: pba}, 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-08-17 18:40:15 +01:00
|
|
|
func PieceRangerSize(c *Client, stream pb.PieceStoreRoutes_RetrieveClient, id PieceID, size int64, pba *pb.PayerBandwidthAllocation) ranger.RangeCloser {
|
|
|
|
return &pieceRanger{c: c, id: id, size: size, stream: stream, pba: pba}
|
2018-06-27 19:42:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Size implements Ranger.Size
|
|
|
|
func (r *pieceRanger) Size() int64 {
|
|
|
|
return r.size
|
|
|
|
}
|
|
|
|
|
|
|
|
// Size implements Ranger.Size
|
|
|
|
func (r *pieceRanger) Close() error {
|
2018-08-20 16:11:54 +01:00
|
|
|
return r.c.Close()
|
2018-06-27 19:42:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
if err := r.stream.Send(&pb.PieceRetrieval{PieceData: &pb.PieceRetrieval_PieceData{Id: r.id.String(), Size: length, Offset: offset}}); err != nil {
|
2018-06-27 19:42:54 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-08-17 18:40:15 +01:00
|
|
|
return NewStreamReader(r.c, r.stream, r.pba), nil
|
2018-06-27 19:42:54 +01:00
|
|
|
}
|