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-10 02:27:23 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2018-05-22 18:36:55 +00:00
|
|
|
)
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// NewShardDB initializes a shardDB.
|
2018-06-27 18:19:36 +00:00
|
|
|
func NewShardDB(dataDir string, name string, inmemory bool) (*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-06-27 18:19:36 +00:00
|
|
|
if inmemory {
|
|
|
|
return &ShardDB{
|
|
|
|
inmemory: inmemory,
|
|
|
|
dataDir: dataDir,
|
|
|
|
name: name,
|
|
|
|
cache: 16,
|
|
|
|
handles: 16,
|
|
|
|
db: NewShardKV(),
|
|
|
|
}, nil
|
|
|
|
}
|
2018-06-20 03:59:02 +00:00
|
|
|
return &ShardDB{
|
|
|
|
dataDir: dataDir,
|
|
|
|
name: name,
|
|
|
|
cache: 16,
|
|
|
|
handles: 16,
|
|
|
|
db: nil,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|