900f67e3d0
* Implement psclient interface * Add string method to pieceID type * try to fix linter errors * Whoops missed an error * More linter errors * Typo * Lol double typo * Get everything working, begin adding tests for psclient rpc * goimports * Forgot to change the piecestore cli when changed the piecestore code * Fix CLI * remove ID length, added validator to pieceID * Move grpc ranger to client Change client PUT api to take a reader rather than return a writer * GRPCRanger -> PieceRanger; Make PieceRanger a RangeCloser * Forgot to remove offset * Added message upon successful store * Do that thing dennis and kaloyan wanted * goimports * Make closeConn a part of the interface for psclient * Use interface * Removed uneccessary new lines * goimport * Whoops * Actually we don't want to use the interface in Piece Ranger * Renamed piecestore in examples to piecestore-client; moved piecestore-cli to examples * Make comments look nicer
73 lines
1.7 KiB
Go
73 lines
1.7 KiB
Go
// 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"
|
|
)
|
|
|
|
var Error = errs.Class("pieceRanger error")
|
|
|
|
type pieceRanger struct {
|
|
c *Client
|
|
id PieceID
|
|
size int64
|
|
}
|
|
|
|
// PieceRanger PieceRanger returns a RangeCloser from a PieceID.
|
|
func PieceRanger(ctx context.Context, c *Client, id PieceID) (ranger.RangeCloser, error) {
|
|
piece, err := c.Meta(ctx, PieceID(id))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &pieceRanger{c: c, id: id, size: piece.Size}, nil
|
|
}
|
|
|
|
// 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.
|
|
func PieceRangerSize(c *Client, id PieceID, size int64) ranger.RangeCloser {
|
|
return &pieceRanger{c: c, id: id, size: size}
|
|
}
|
|
|
|
// Size implements Ranger.Size
|
|
func (r *pieceRanger) Size() int64 {
|
|
return r.size
|
|
}
|
|
|
|
// Size implements Ranger.Size
|
|
func (r *pieceRanger) Close() error {
|
|
return r.c.CloseConn()
|
|
}
|
|
|
|
// 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
|
|
}
|
|
stream, err := r.c.route.Retrieve(ctx, &pb.PieceRetrieval{Id: r.id.String(), Size: length, Offset: offset})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return NewStreamReader(stream), nil
|
|
}
|