2019-01-10 13:13:27 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package storagenodedb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/zeebo/errs"
|
2019-02-19 09:39:04 +00:00
|
|
|
"go.uber.org/zap"
|
2019-01-10 13:13:27 +00:00
|
|
|
|
2019-01-23 10:39:03 +00:00
|
|
|
"storj.io/storj/pkg/kademlia"
|
2019-01-10 13:13:27 +00:00
|
|
|
"storj.io/storj/storage"
|
2019-01-23 10:39:03 +00:00
|
|
|
"storj.io/storj/storage/boltdb"
|
2019-03-18 10:55:06 +00:00
|
|
|
"storj.io/storj/storage/filestore"
|
2019-01-10 13:13:27 +00:00
|
|
|
"storj.io/storj/storage/teststore"
|
|
|
|
"storj.io/storj/storagenode"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ storagenode.DB = (*DB)(nil)
|
|
|
|
|
2019-01-23 10:39:03 +00:00
|
|
|
// Config configures storage node database
|
|
|
|
type Config struct {
|
|
|
|
// TODO: figure out better names
|
|
|
|
Storage string
|
|
|
|
Info string
|
2019-03-18 10:55:06 +00:00
|
|
|
Info2 string
|
2019-01-23 10:39:03 +00:00
|
|
|
Kademlia string
|
2019-03-18 10:55:06 +00:00
|
|
|
|
|
|
|
Pieces string
|
2019-01-23 10:39:03 +00:00
|
|
|
}
|
|
|
|
|
2019-01-10 13:13:27 +00:00
|
|
|
// DB contains access to different database tables
|
|
|
|
type DB struct {
|
2019-04-26 06:17:18 +01:00
|
|
|
log *zap.Logger
|
2019-03-18 10:55:06 +00:00
|
|
|
|
|
|
|
pieces interface {
|
|
|
|
storage.Blobs
|
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
2019-04-02 08:54:09 +01:00
|
|
|
info *InfoDB
|
2019-03-18 10:55:06 +00:00
|
|
|
|
2019-01-10 13:13:27 +00:00
|
|
|
kdb, ndb storage.KeyValueStore
|
|
|
|
}
|
|
|
|
|
2019-01-23 10:39:03 +00:00
|
|
|
// New creates a new master database for storage node
|
2019-02-19 09:39:04 +00:00
|
|
|
func New(log *zap.Logger, config Config) (*DB, error) {
|
2019-03-18 10:55:06 +00:00
|
|
|
piecesDir, err := filestore.NewDir(config.Pieces)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pieces := filestore.New(piecesDir)
|
|
|
|
|
|
|
|
infodb, err := newInfo(config.Info2)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-01-23 10:39:03 +00:00
|
|
|
dbs, err := boltdb.NewShared(config.Kademlia, kademlia.KademliaBucket, kademlia.NodeBucket)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &DB{
|
2019-04-26 06:17:18 +01:00
|
|
|
log: log,
|
2019-03-18 10:55:06 +00:00
|
|
|
|
|
|
|
pieces: pieces,
|
|
|
|
|
|
|
|
info: infodb,
|
|
|
|
|
|
|
|
kdb: dbs[0],
|
|
|
|
ndb: dbs[1],
|
2019-01-23 10:39:03 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewInMemory creates new inmemory master database for storage node
|
2019-01-10 13:13:27 +00:00
|
|
|
// TODO: still stores data on disk
|
2019-02-19 09:39:04 +00:00
|
|
|
func NewInMemory(log *zap.Logger, storageDir string) (*DB, error) {
|
2019-03-18 10:55:06 +00:00
|
|
|
piecesDir, err := filestore.NewDir(storageDir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pieces := filestore.New(piecesDir)
|
|
|
|
|
2019-04-02 08:54:09 +01:00
|
|
|
infodb, err := NewInfoInMemory()
|
2019-03-18 10:55:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-01-10 13:13:27 +00:00
|
|
|
return &DB{
|
2019-04-26 06:17:18 +01:00
|
|
|
log: log,
|
2019-03-18 10:55:06 +00:00
|
|
|
|
|
|
|
pieces: pieces,
|
2019-04-26 06:17:18 +01:00
|
|
|
info: infodb,
|
2019-03-18 10:55:06 +00:00
|
|
|
|
|
|
|
kdb: teststore.New(),
|
|
|
|
ndb: teststore.New(),
|
2019-01-10 13:13:27 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-01-24 20:28:06 +00:00
|
|
|
// CreateTables creates any necessary tables.
|
|
|
|
func (db *DB) CreateTables() error {
|
2019-04-26 06:17:18 +01:00
|
|
|
return db.info.CreateTables(db.log)
|
2019-01-24 20:28:06 +00:00
|
|
|
}
|
|
|
|
|
2019-01-10 13:13:27 +00:00
|
|
|
// Close closes any resources.
|
|
|
|
func (db *DB) Close() error {
|
|
|
|
return errs.Combine(
|
|
|
|
db.kdb.Close(),
|
|
|
|
db.ndb.Close(),
|
2019-03-18 10:55:06 +00:00
|
|
|
|
|
|
|
db.pieces.Close(),
|
|
|
|
db.info.Close(),
|
2019-01-10 13:13:27 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-03-18 10:55:06 +00:00
|
|
|
// Pieces returns blob storage for pieces
|
|
|
|
func (db *DB) Pieces() storage.Blobs {
|
|
|
|
return db.pieces
|
|
|
|
}
|
|
|
|
|
2019-01-10 13:13:27 +00:00
|
|
|
// RoutingTable returns kademlia routing table
|
|
|
|
func (db *DB) RoutingTable() (kdb, ndb storage.KeyValueStore) {
|
|
|
|
return db.kdb, db.ndb
|
|
|
|
}
|