2018-06-05 21:28:57 +00:00
|
|
|
// Package database provides several constructs including a simple in-memory database.
|
|
|
|
// This should not be used for production, but would be a helpful interim
|
|
|
|
// solution for development.
|
2018-05-22 18:36:55 +00:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
2018-06-20 03:59:02 +00:00
|
|
|
"fmt"
|
2018-05-22 18:36:55 +00:00
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
2018-07-20 21:31:26 +00:00
|
|
|
sharedDB "github.com/prysmaticlabs/prysm/shared/database"
|
2018-07-21 17:51:18 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2018-05-22 18:36:55 +00:00
|
|
|
)
|
|
|
|
|
2018-07-21 17:51:18 +00:00
|
|
|
var log = logrus.WithField("prefix", "db")
|
|
|
|
|
2018-07-19 16:31:50 +00:00
|
|
|
// ShardDB defines a service for the sharding system's persistent storage.
|
2018-06-20 03:59:02 +00:00
|
|
|
type ShardDB struct {
|
2018-06-27 18:19:36 +00:00
|
|
|
inmemory bool
|
|
|
|
dataDir string
|
|
|
|
name string
|
|
|
|
cache int
|
|
|
|
handles int
|
|
|
|
db ethdb.Database
|
2018-06-20 03:59:02 +00:00
|
|
|
}
|
|
|
|
|
2018-07-19 16:31:50 +00:00
|
|
|
// ShardDBConfig specifies configuration options for the db service.
|
|
|
|
type ShardDBConfig struct {
|
|
|
|
DataDir string
|
|
|
|
Name string
|
|
|
|
InMemory bool
|
|
|
|
}
|
|
|
|
|
2018-06-20 03:59:02 +00:00
|
|
|
// NewShardDB initializes a shardDB.
|
2018-07-19 16:31:50 +00:00
|
|
|
func NewShardDB(config *ShardDBConfig) (*ShardDB, error) {
|
2018-05-22 18:36:55 +00:00
|
|
|
// Uses default cache and handles values.
|
2018-05-25 15:06:39 +00:00
|
|
|
// TODO: allow these arguments to be set based on cli context.
|
2018-07-19 16:31:50 +00:00
|
|
|
shardDB := &ShardDB{
|
|
|
|
name: config.Name,
|
|
|
|
dataDir: config.DataDir,
|
|
|
|
}
|
|
|
|
if config.InMemory {
|
|
|
|
shardDB.inmemory = true
|
|
|
|
shardDB.db = sharedDB.NewKVStore()
|
|
|
|
} else {
|
|
|
|
shardDB.inmemory = false
|
|
|
|
shardDB.cache = 16
|
|
|
|
shardDB.handles = 16
|
2018-06-27 18:19:36 +00:00
|
|
|
}
|
2018-07-19 16:31:50 +00:00
|
|
|
return shardDB, nil
|
2018-06-20 03:59:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start the shard DB service.
|
|
|
|
func (s *ShardDB) Start() {
|
|
|
|
log.Info("Starting shardDB service")
|
2018-06-27 18:19:36 +00:00
|
|
|
if !s.inmemory {
|
|
|
|
db, err := ethdb.NewLDBDatabase(filepath.Join(s.dataDir, s.name), s.cache, s.handles)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(fmt.Sprintf("Could not start shard DB: %v", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
s.db = db
|
2018-06-20 03:59:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop the shard DB service gracefully.
|
|
|
|
func (s *ShardDB) Stop() error {
|
|
|
|
log.Info("Stopping shardDB service")
|
|
|
|
s.db.Close()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DB returns the attached ethdb instance.
|
|
|
|
func (s *ShardDB) DB() ethdb.Database {
|
|
|
|
return s.db
|
2018-05-22 18:36:55 +00:00
|
|
|
}
|