Skip to content

Commit

Permalink
if always false we don't need the param
Browse files Browse the repository at this point in the history
  • Loading branch information
faddat committed Jan 7, 2024
1 parent 90d9aff commit 019ce53
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 45 deletions.
2 changes: 1 addition & 1 deletion backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func init() {
mdb.Set([]byte("u"), []byte{21})
mdb.Set([]byte("z"), []byte{26})
return NewPrefixDB(mdb, []byte("test/")), nil
}, false)
})
}

func cleanupDBDir(dir, name string) {
Expand Down
4 changes: 2 additions & 2 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ type dbCreator func(name string, dir string) (DB, error)

var backends = map[BackendType]dbCreator{}

func registerDBCreator(backend BackendType, creator dbCreator, force bool) {
func registerDBCreator(backend BackendType, creator dbCreator) {
_, ok := backends[backend]
if !force && ok {
if ok {
return
}
backends[backend] = creator
Expand Down
4 changes: 2 additions & 2 deletions goleveldb.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func init() {
dbCreator := func(name string, dir string) (DB, error) {
return NewGoLevelDB(name, dir)
}
registerDBCreator(GoLevelDBBackend, dbCreator, false)
registerDBCreator(GoLevelDBBackend, dbCreator)
}

type GoLevelDB struct {
Expand All @@ -28,7 +28,7 @@ func NewGoLevelDB(name string, dir string) (*GoLevelDB, error) {
}

func NewGoLevelDBWithOpts(name string, dir string, o *opt.Options) (*GoLevelDB, error) {
dbPath := filepath.Join(dir, name+".db")
dbPath := filepath.Join(dir, name+".db") //nolint:goconst
db, err := leveldb.OpenFile(dbPath, o)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion memdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const (
func init() {
registerDBCreator(MemDBBackend, func(name, dir string) (DB, error) {
return NewMemDB(), nil
}, false)
})
}

// item is a btree.Item with byte slices as keys and values
Expand Down
47 changes: 12 additions & 35 deletions pebble.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
//go:build pebbledb
// +build pebbledb

package db

import (
Expand Down Expand Up @@ -49,14 +46,16 @@ go install -tags pebbledb -ldflags "-w -s -X github.com/cosmos/cosmos-sdk/types.
$HOME/go/bin/sifnoded start --db_backend=pebbledb
*/
var ForceSync = "0"
var isForceSync = false
var (
ForceSync = "0"
isForceSync = false
)

func init() {
dbCreator := func(name string, dir string) (DB, error) {
return NewPebbleDB(name, dir)
}
registerDBCreator(PebbleDBBackend, dbCreator, false)
registerDBCreator(PebbleDBBackend, dbCreator)

if ForceSync == "1" {
isForceSync = true
Expand Down Expand Up @@ -90,13 +89,11 @@ func NewPebbleDBWithOpts(name string, dir string, opts *pebble.Options) (*Pebble

// Get implements DB.
func (db *PebbleDB) Get(key []byte) ([]byte, error) {
//fmt.Println("PebbleDB.Get")
if len(key) == 0 {
return nil, errKeyEmpty
}

res, closer, err := db.db.Get(key)

if err != nil {
if err == pebble.ErrNotFound {
return nil, nil
Expand All @@ -110,20 +107,18 @@ func (db *PebbleDB) Get(key []byte) ([]byte, error) {

// Has implements DB.
func (db *PebbleDB) Has(key []byte) (bool, error) {
//fmt.Println("PebbleDB.Has")
if len(key) == 0 {
return false, errKeyEmpty
}
bytes, err := db.Get(key)
bytesPeb, err := db.Get(key)
if err != nil {
return false, err
}
return bytes != nil, nil
return bytesPeb != nil, nil
}

// Set implements DB.
func (db *PebbleDB) Set(key []byte, value []byte) error {
//fmt.Println("PebbleDB.Set")
if len(key) == 0 {
return errKeyEmpty
}
Expand All @@ -145,7 +140,6 @@ func (db *PebbleDB) Set(key []byte, value []byte) error {

// SetSync implements DB.
func (db *PebbleDB) SetSync(key []byte, value []byte) error {
//fmt.Println("PebbleDB.SetSync")
if len(key) == 0 {
return errKeyEmpty
}
Expand All @@ -161,7 +155,6 @@ func (db *PebbleDB) SetSync(key []byte, value []byte) error {

// Delete implements DB.
func (db *PebbleDB) Delete(key []byte) error {
//fmt.Println("PebbleDB.Delete")
if len(key) == 0 {
return errKeyEmpty
}
Expand All @@ -179,7 +172,6 @@ func (db *PebbleDB) Delete(key []byte) error {

// DeleteSync implements DB.
func (db PebbleDB) DeleteSync(key []byte) error {
//fmt.Println("PebbleDB.DeleteSync")
if len(key) == 0 {
return errKeyEmpty
}
Expand All @@ -196,7 +188,6 @@ func (db *PebbleDB) DB() *pebble.DB {

// Close implements DB.
func (db PebbleDB) Close() error {
//fmt.Println("PebbleDB.Close")
db.db.Close()
return nil
}
Expand All @@ -217,7 +208,7 @@ func (db *PebbleDB) Print() error {
}

// Stats implements DB.
func (db *PebbleDB) Stats() map[string]string {
func (*PebbleDB) Stats() map[string]string {
/*
keys := []string{"rocksdb.stats"}
stats := make(map[string]string, len(keys))
Expand All @@ -235,7 +226,6 @@ func (db *PebbleDB) NewBatch() Batch {

// Iterator implements DB.
func (db *PebbleDB) Iterator(start, end []byte) (Iterator, error) {
//fmt.Println("PebbleDB.Iterator")
if (start != nil && len(start) == 0) || (end != nil && len(end) == 0) {
return nil, errKeyEmpty
}
Expand All @@ -251,7 +241,6 @@ func (db *PebbleDB) Iterator(start, end []byte) (Iterator, error) {

// ReverseIterator implements DB.
func (db *PebbleDB) ReverseIterator(start, end []byte) (Iterator, error) {
//fmt.Println("PebbleDB.ReverseIterator")
if (start != nil && len(start) == 0) || (end != nil && len(end) == 0) {
return nil, errKeyEmpty
}
Expand All @@ -267,7 +256,6 @@ func (db *PebbleDB) ReverseIterator(start, end []byte) (Iterator, error) {
var _ Batch = (*pebbleDBBatch)(nil)

type pebbleDBBatch struct {
db *PebbleDB
batch *pebble.Batch
}

Expand All @@ -281,7 +269,6 @@ func newPebbleDBBatch(db *PebbleDB) *pebbleDBBatch {

// Set implements Batch.
func (b *pebbleDBBatch) Set(key, value []byte) error {
//fmt.Println("pebbleDBBatch.Set")
if len(key) == 0 {
return errKeyEmpty
}
Expand All @@ -291,26 +278,24 @@ func (b *pebbleDBBatch) Set(key, value []byte) error {
if b.batch == nil {
return errBatchClosed
}
b.batch.Set(key, value, nil)
return nil

return b.batch.Set(key, value, nil)
}

// Delete implements Batch.
func (b *pebbleDBBatch) Delete(key []byte) error {
//fmt.Println("pebbleDBBatch.Delete")
if len(key) == 0 {
return errKeyEmpty
}
if b.batch == nil {
return errBatchClosed
}
b.batch.Delete(key, nil)
return nil

return b.batch.Delete(key, nil)
}

// Write implements Batch.
func (b *pebbleDBBatch) Write() error {
//fmt.Println("pebbleDBBatch.Write")
if b.batch == nil {
return errBatchClosed
}
Expand All @@ -330,7 +315,6 @@ func (b *pebbleDBBatch) Write() error {

// WriteSync implements Batch.
func (b *pebbleDBBatch) WriteSync() error {
//fmt.Println("pebbleDBBatch.WriteSync")
if b.batch == nil {
return errBatchClosed
}
Expand All @@ -344,7 +328,6 @@ func (b *pebbleDBBatch) WriteSync() error {

// Close implements Batch.
func (b *pebbleDBBatch) Close() error {
//fmt.Println("pebbleDBBatch.Close")
if b.batch != nil {
err := b.batch.Close()
if err != nil {
Expand Down Expand Up @@ -386,13 +369,11 @@ func newPebbleDBIterator(source *pebble.Iterator, start, end []byte, isReverse b

// Domain implements Iterator.
func (itr *pebbleDBIterator) Domain() ([]byte, []byte) {
//fmt.Println("pebbleDBIterator.Domain")
return itr.start, itr.end
}

// Valid implements Iterator.
func (itr *pebbleDBIterator) Valid() bool {
//fmt.Println("pebbleDBIterator.Valid")
// Once invalid, forever invalid.
if itr.isInvalid {
return false
Expand Down Expand Up @@ -436,21 +417,18 @@ func (itr *pebbleDBIterator) Valid() bool {

// Key implements Iterator.
func (itr *pebbleDBIterator) Key() []byte {
//fmt.Println("pebbleDBIterator.Key")
itr.assertIsValid()
return cp(itr.source.Key())
}

// Value implements Iterator.
func (itr *pebbleDBIterator) Value() []byte {
//fmt.Println("pebbleDBIterator.Value")
itr.assertIsValid()
return cp(itr.source.Value())
}

// Next implements Iterator.
func (itr pebbleDBIterator) Next() {
//fmt.Println("pebbleDBIterator.Next")
itr.assertIsValid()
if itr.isReverse {
itr.source.Prev()
Expand All @@ -466,7 +444,6 @@ func (itr *pebbleDBIterator) Error() error {

// Close implements Iterator.
func (itr *pebbleDBIterator) Close() error {
//fmt.Println("pebbleDBIterator.Close")
err := itr.source.Close()
if err != nil {
return err
Expand Down
6 changes: 2 additions & 4 deletions pebble_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
//go:build pebbledb
// +build pebbledb

package db

import (
Expand Down Expand Up @@ -41,7 +38,8 @@ func BenchmarkPebbleDBRandomReadsWrites(b *testing.B) {
b.Fatal(err)
}
defer func() {
db.Close()
err = db.Close()
require.NoError(b, err)
cleanupDBDir("", name)
}()

Expand Down

0 comments on commit 019ce53

Please sign in to comment.