prysm-pulse/sharding/database/database.go
Raul Jordan f5e5287082 sharding: Remove Dependency on Geth: Replace Log With Logrus Package (#242)
Former-commit-id: 2ec35880b3aa30d3c217815c9a474e8d81ee1fa8 [formerly c20da02cf9149990f8b7913cfe426b8036992c16]
Former-commit-id: 4851374829557f1523f5994b1d97e08e46979aed
2018-07-09 21:27:23 -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"
log "github.com/sirupsen/logrus"
)
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
}