prysm-pulse/client/database/database.go
Raul Jordan 92af8bc351 Rename Entire Project to Repo, Change Import Paths and Readmes (#298)
Former-commit-id: b7b8bbd10777012ae6f7d30eb6b05c3b1c3ec5d3 [formerly 06e1112fa0e1092a7137186d3a386972daa2effe]
Former-commit-id: ff2bc760c9dafb6250f056606eb2cbf96b6afa5b
2018-07-20 16:31:26 -05:00

75 lines
1.7 KiB
Go

// 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.
package database
import (
"fmt"
"path/filepath"
"github.com/ethereum/go-ethereum/ethdb"
sharedDB "github.com/prysmaticlabs/prysm/shared/database"
log "github.com/sirupsen/logrus"
)
// ShardDB defines a service for the sharding system's persistent storage.
type ShardDB struct {
inmemory bool
dataDir string
name string
cache int
handles int
db ethdb.Database
}
// ShardDBConfig specifies configuration options for the db service.
type ShardDBConfig struct {
DataDir string
Name string
InMemory bool
}
// NewShardDB initializes a shardDB.
func NewShardDB(config *ShardDBConfig) (*ShardDB, error) {
// Uses default cache and handles values.
// TODO: allow these arguments to be set based on cli context.
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
}
return shardDB, nil
}
// Start the shard DB service.
func (s *ShardDB) Start() {
log.Info("Starting shardDB service")
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
}
}
// 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
}