prysm-pulse/sharding/database/database.go
Raul Jordan f8d4cdda84 Simplify Goroutines for Better Testing (#216)
sharding: goroutine for better testing (#216)
Former-commit-id: 2c70ee0892b1e36d5b4473f1e5bba5f151ee449c [formerly 3d91ae5c4288ab27fbf09347d5b12164802726bc]
Former-commit-id: 24085acd2b045f549a3356ef0da219cb91149650
2018-06-27 13:19:36 -05:00

70 lines
1.5 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"
"github.com/ethereum/go-ethereum/log"
)
type ShardDB struct {
inmemory bool
dataDir string
name string
cache int
handles int
db ethdb.Database
}
// NewShardDB initializes a shardDB.
func NewShardDB(dataDir string, name string, inmemory bool) (*ShardDB, error) {
// Uses default cache and handles values.
// TODO: allow these arguments to be set based on cli context.
if inmemory {
return &ShardDB{
inmemory: inmemory,
dataDir: dataDir,
name: name,
cache: 16,
handles: 16,
db: NewShardKV(),
}, nil
}
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")
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
}