forked from TrueCloudLab/neoneo-go
264dfef370
add close function to storage interface add common defer function call which will close db connection remove context as soon as it's not needed anymore updated unit tests
71 lines
1.8 KiB
Go
71 lines
1.8 KiB
Go
package storage
|
|
|
|
import (
|
|
"github.com/syndtr/goleveldb/leveldb"
|
|
"github.com/syndtr/goleveldb/leveldb/opt"
|
|
"github.com/syndtr/goleveldb/leveldb/util"
|
|
)
|
|
|
|
// LevelDBOptions configuration for LevelDB.
|
|
type LevelDBOptions struct {
|
|
DataDirectoryPath string `yaml:"DataDirectoryPath"`
|
|
}
|
|
|
|
// LevelDBStore is the official storage implementation for storing and retrieving
|
|
// blockchain data.
|
|
type LevelDBStore struct {
|
|
db *leveldb.DB
|
|
path string
|
|
}
|
|
|
|
// NewLevelDBStore return a new LevelDBStore object that will
|
|
// initialize the database found at the given path.
|
|
func NewLevelDBStore(cfg LevelDBOptions) (*LevelDBStore, error) {
|
|
var opts *opt.Options = nil // should be exposed via LevelDBOptions if anything needed
|
|
|
|
db, err := leveldb.OpenFile(cfg.DataDirectoryPath, opts)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &LevelDBStore{
|
|
path: cfg.DataDirectoryPath,
|
|
db: db,
|
|
}, nil
|
|
}
|
|
|
|
// Put implements the Store interface.
|
|
func (s *LevelDBStore) Put(key, value []byte) error {
|
|
return s.db.Put(key, value, nil)
|
|
}
|
|
|
|
// Get implements the Store interface.
|
|
func (s *LevelDBStore) Get(key []byte) ([]byte, error) {
|
|
return s.db.Get(key, nil)
|
|
}
|
|
|
|
// PutBatch implements the Store interface.
|
|
func (s *LevelDBStore) PutBatch(batch Batch) error {
|
|
lvldbBatch := batch.(*leveldb.Batch)
|
|
return s.db.Write(lvldbBatch, nil)
|
|
}
|
|
|
|
// Seek implements the Store interface.
|
|
func (s *LevelDBStore) Seek(key []byte, f func(k, v []byte)) {
|
|
iter := s.db.NewIterator(util.BytesPrefix(key), nil)
|
|
for iter.Next() {
|
|
f(iter.Key(), iter.Value())
|
|
}
|
|
iter.Release()
|
|
}
|
|
|
|
// Batch implements the Batch interface and returns a leveldb
|
|
// compatible Batch.
|
|
func (s *LevelDBStore) Batch() Batch {
|
|
return new(leveldb.Batch)
|
|
}
|
|
|
|
// Close implements the Store interface.
|
|
func (s *LevelDBStore) Close() error {
|
|
return s.db.Close()
|
|
}
|