storj/pkg/ranger/grpc.go
Kaloyan Raev 916e0b0ee0 gRPC Ranger (#44)
* Added piecestore

* gofmt

* Added requested changes

* Added cli

* Removed ranger because I wanted something that can stand alone

* Add example of http server using piece store

* Changed piecestore code to make it more optial for error handelling

* Merged with piecestore

* Added missing package

* Forgot io import

* gofmt

* gofmt

* Forgot io

* Make path by hash exported

* updated to simplify again whoops

* Updated server to work real good

* Forgot ampersand

* Updated to match FilePiece

* Merged in cam's delete code

* Remove unused io

* Added RPC code

* Give the download request a reader

* Removed http server stuff; changed receive stream to say io.reader

* Added expiration date to shardInfo

* gRPC Ranger

* Change all instances of Shard to Piece; change protobuf name; moved client insance to outside functions

* Adapt to latest changes in piece store rpc api

* added ttl info request

* Initialize grpcRanger type with named fields

* Move scripts to http server pr; added close method for Retrieve api

* added rpc server tests for getting piece meta data and retrieval routes

* Adapt to PieceStreamReader now being a ReadCloser

* Resolved linter errors, moved to prc server to pkg, updated go.mod to use latest protobuf

* Imported cams test

* Bump gometalinter deadline

* Adapt to package name changes

* Remove Garbage

* Adapt to latest changes in piece store rpc api

* NewCustomRoute constructor to allow mocking the gRPC client

* Name struct values in constructor.
2018-06-02 22:20:17 -06:00

62 lines
1.5 KiB
Go

// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
package ranger
import (
"bytes"
"io"
"io/ioutil"
"storj.io/storj/internal/pkg/readcloser"
"storj.io/storj/pkg/piecestore/rpc/client"
)
type grpcRanger struct {
c *client.Client
id string
size int64
}
// GRPCRanger turns a gRPC connection to piece store into a Ranger
func GRPCRanger(c *client.Client, id string) (Ranger, error) {
piece, err := c.PieceMetaRequest(id)
if err != nil {
return nil, err
}
return &grpcRanger{c: c, id: id, size: piece.Size}, nil
}
// GRPCRangerSize creates a GRPCRanger 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 GRPCRangerSize(c *client.Client, id string, size int64) Ranger {
return &grpcRanger{c: c, id: id, size: size}
}
// Size implements Ranger.Size
func (r *grpcRanger) Size() int64 {
return r.size
}
// Range implements Ranger.Range
func (r *grpcRanger) Range(offset, length int64) io.ReadCloser {
if offset < 0 {
return readcloser.FatalReadCloser(Error.New("negative offset"))
}
if length < 0 {
return readcloser.FatalReadCloser(Error.New("negative length"))
}
if offset+length > r.size {
return readcloser.FatalReadCloser(Error.New("range beyond end"))
}
if length == 0 {
return ioutil.NopCloser(bytes.NewReader([]byte{}))
}
reader, err := r.c.RetrievePieceRequest(r.id, length, offset)
if err != nil {
return readcloser.FatalReadCloser(Error.Wrap(err))
}
return reader
}