storj/pkg/netstate/netstate.go
Natalie Villasana 1a06c2b0c7 adding grpc interface to network state (#33)
* adds proto files for netstate crud

* moves netstate grpc client lib into pkg/netstate where grpc netstate service is defined

* starts adding grpc client and server tests

* moves creation of grpc server into cmd/netstate/main.go, removes pkg/netstate/service.go, adds more client testing

* changed all 'Path' and 'Value' fields from strings to bytes, updated tests

* changes Get and Delete in proto file to receive 'requests' instead of 'file paths', adds tests for Get, List, and Delete

* changes netstate-routes to get 'fileValue' bytes not 'fileInfo'

* adds example rpc client in 'examples' and adds more specific debug logs

* adds readmes for netstate rpc services and updates netstate-routes
2018-05-14 18:31:26 -06:00

105 lines
2.5 KiB
Go

// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
package netstate
import (
"context"
"go.uber.org/zap"
proto "storj.io/storj/protos/netstate"
"storj.io/storj/storage/boltdb"
)
// Server implements the network state RPC service
type Server struct {
DB DB
logger *zap.Logger
}
// NewServer creates instance of Server
func NewServer(db DB, logger *zap.Logger) *Server {
return &Server{
DB: db,
logger: logger,
}
}
// DB interface allows more modular unit testing
// and makes it easier in the future to substitute
// db clients other than bolt
type DB interface {
Put(boltdb.File) error
Get([]byte) ([]byte, error)
List() ([][]byte, error)
Delete([]byte) error
}
// Put formats and hands off a file path to be saved to boltdb
func (s *Server) Put(ctx context.Context, filepath *proto.FilePath) (*proto.PutResponse, error) {
s.logger.Debug("entering netstate put")
file := boltdb.File{
Path: []byte(filepath.Path),
Value: []byte(filepath.SmallValue),
}
if err := s.DB.Put(file); err != nil {
s.logger.Error("err putting file", zap.Error(err))
return nil, err
}
s.logger.Debug("put to the db: " + string(file.Path))
return &proto.PutResponse{
Confirmation: "success",
}, nil
}
// Get formats and hands off a file path to get from boltdb
func (s *Server) Get(ctx context.Context, req *proto.GetRequest) (*proto.GetResponse, error) {
s.logger.Debug("entering netstate get")
fileValue, err := s.DB.Get(req.Path)
if err != nil {
s.logger.Error("err getting file", zap.Error(err))
return nil, err
}
return &proto.GetResponse{
SmallValue: fileValue,
}, nil
}
// List calls the bolt client's List function and returns all file paths
func (s *Server) List(ctx context.Context, req *proto.ListRequest) (*proto.ListResponse, error) {
s.logger.Debug("entering netstate list")
filePaths, err := s.DB.List()
if err != nil {
s.logger.Error("err listing file paths", zap.Error(err))
return nil, err
}
s.logger.Debug("file paths retrieved")
return &proto.ListResponse{
// filePaths is an array of byte arrays
Filepaths: filePaths,
}, nil
}
// Delete formats and hands off a file path to delete from boltdb
func (s *Server) Delete(ctx context.Context, req *proto.DeleteRequest) (*proto.DeleteResponse, error) {
s.logger.Debug("entering netstate delete")
err := s.DB.Delete(req.Path)
if err != nil {
s.logger.Error("err deleting file", zap.Error(err))
return nil, err
}
s.logger.Debug("deleted: " + string(req.Path))
return &proto.DeleteResponse{
Confirmation: "success",
}, nil
}