2019-01-23 15:48:46 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package bootstrapdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
|
|
|
"storj.io/storj/bootstrap"
|
|
|
|
"storj.io/storj/pkg/kademlia"
|
|
|
|
"storj.io/storj/storage"
|
|
|
|
"storj.io/storj/storage/boltdb"
|
|
|
|
"storj.io/storj/storage/teststore"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ bootstrap.DB = (*DB)(nil)
|
|
|
|
|
|
|
|
// Config configures storage node database
|
|
|
|
type Config struct {
|
|
|
|
Kademlia string
|
|
|
|
}
|
|
|
|
|
|
|
|
// DB contains access to different database tables
|
|
|
|
type DB struct {
|
2019-07-01 22:20:19 +01:00
|
|
|
kdb, ndb, adb storage.KeyValueStore
|
2019-01-23 15:48:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new master database for storage node
|
|
|
|
func New(config Config) (*DB, error) {
|
2019-07-01 22:20:19 +01:00
|
|
|
dbs, err := boltdb.NewShared(config.Kademlia, kademlia.KademliaBucket, kademlia.NodeBucket, kademlia.AntechamberBucket)
|
2019-01-23 15:48:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &DB{
|
|
|
|
kdb: dbs[0],
|
|
|
|
ndb: dbs[1],
|
2019-07-01 22:20:19 +01:00
|
|
|
adb: dbs[2],
|
2019-01-23 15:48:46 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-07-22 20:10:04 +01:00
|
|
|
// NewInMemory creates new in-memory master database for storage node
|
2019-01-23 15:48:46 +00:00
|
|
|
// TODO: still stores data on disk
|
2019-07-22 20:10:04 +01:00
|
|
|
func NewInMemory() (*DB, error) {
|
2019-01-23 15:48:46 +00:00
|
|
|
return &DB{
|
|
|
|
kdb: teststore.New(),
|
|
|
|
ndb: teststore.New(),
|
2019-07-01 22:20:19 +01:00
|
|
|
adb: teststore.New(),
|
2019-01-23 15:48:46 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-01-24 20:28:06 +00:00
|
|
|
// CreateTables initializes the database
|
|
|
|
func (db *DB) CreateTables() error { return nil }
|
|
|
|
|
2019-01-23 15:48:46 +00:00
|
|
|
// Close closes any resources.
|
|
|
|
func (db *DB) Close() error {
|
|
|
|
return errs.Combine(
|
|
|
|
db.kdb.Close(),
|
|
|
|
db.ndb.Close(),
|
2019-07-01 22:20:19 +01:00
|
|
|
db.adb.Close(),
|
2019-01-23 15:48:46 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RoutingTable returns kademlia routing table
|
2019-07-01 22:20:19 +01:00
|
|
|
func (db *DB) RoutingTable() (kdb, ndb, adb storage.KeyValueStore) {
|
|
|
|
return db.kdb, db.ndb, db.adb
|
2019-01-23 15:48:46 +00:00
|
|
|
}
|