storj/pkg/bwagreement/database-manager/database-manager.go
Alexander Leitner 5e9377524f
Refactor basystem (#641)
* Signature verification

* Clean up agreement sender to have less errors

* overlay address in captnplanet

* Refactor bandwidth.proto to not use streams

* Make sure the send worked

* Handle connection to satellite

* Save renter public key inside of renter bandwidth allocations

* Default diag to sqlite. Make configurable

* Separate bw server and dbm; regenerate dbx files

* Make sure test uses protobufs

* Demonstrate creating bandwidth allocations
2018-11-15 14:06:09 -05:00

78 lines
1.7 KiB
Go

// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
package dbmanager
import (
"context"
"sync"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
monkit "gopkg.in/spacemonkeygo/monkit.v2"
"storj.io/storj/internal/migrate"
dbx "storj.io/storj/pkg/bwagreement/database-manager/dbx"
"storj.io/storj/pkg/pb"
)
var (
mon = monkit.Package()
)
// DBManager is an implementation of the database access interface
type DBManager struct {
DB *dbx.DB
mu sync.Mutex
}
// NewDBManager creates a new instance of a DatabaseManager
func NewDBManager(driver, source string) (*DBManager, error) {
db, err := dbx.Open(driver, source)
if err != nil {
return nil, err
}
err = migrate.Create("bwagreement", db)
if err != nil {
return nil, err
}
return &DBManager{
DB: db,
}, nil
}
func (dbm *DBManager) locked() func() {
dbm.mu.Lock()
return dbm.mu.Unlock
}
// Create a db entry for the provided storagenode
func (dbm *DBManager) Create(ctx context.Context, createBwAgreement *pb.RenterBandwidthAllocation) (bwagreement *dbx.Bwagreement, err error) {
defer mon.Task()(&ctx)(&err)
defer dbm.locked()()
signature := createBwAgreement.GetSignature()
data := createBwAgreement.GetData()
bwagreement, err = dbm.DB.Create_Bwagreement(
ctx,
dbx.Bwagreement_Signature(signature),
dbx.Bwagreement_Data(data),
)
if err != nil {
return nil, status.Errorf(codes.Internal, err.Error())
}
return bwagreement, nil
}
// GetBandwidthAllocations all bandwidth agreements and sorts by satellite
func (dbm *DBManager) GetBandwidthAllocations(ctx context.Context) (rows []*dbx.Bwagreement, err error) {
defer mon.Task()(&ctx)(&err)
defer dbm.locked()()
rows, err = dbm.DB.All_Bwagreement(ctx)
return rows, err
}