Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(store/db): remove unnecessary code and correct error handling #19763

Merged
merged 1 commit into from
Mar 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 29 additions & 54 deletions store/db/goleveldb.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package db

import (
"bytes"
"errors"
"fmt"
"path/filepath"

Expand All @@ -15,7 +16,7 @@ import (

corestore "cosmossdk.io/core/store"
"cosmossdk.io/store/v2"
"cosmossdk.io/store/v2/errors"
storeerrors "cosmossdk.io/store/v2/errors"
)

// GoLevelDB implements RawDB using github.com/syndtr/goleveldb/leveldb.
Expand Down Expand Up @@ -46,20 +47,17 @@ func NewGoLevelDBWithOpts(name, dir string, o *opt.Options) (*GoLevelDB, error)
if err != nil {
return nil, err
}
database := &GoLevelDB{
db: db,
}
return database, nil
return &GoLevelDB{db: db}, nil
}

// Get implements RawDB.
func (db *GoLevelDB) Get(key []byte) ([]byte, error) {
if len(key) == 0 {
return nil, errors.ErrKeyEmpty
return nil, storeerrors.ErrKeyEmpty
}
res, err := db.db.Get(key, nil)
if err != nil {
if err == dberrors.ErrNotFound {
if errors.Is(err, dberrors.ErrNotFound) {
return nil, nil
}
return nil, err
Expand All @@ -69,62 +67,45 @@ func (db *GoLevelDB) Get(key []byte) ([]byte, error) {

// Has implements RawDB.
func (db *GoLevelDB) Has(key []byte) (bool, error) {
bytes, err := db.Get(key)
if err != nil {
return false, err
}
return bytes != nil, nil
return db.db.Has(key, nil)
}

// Set implements RawDB.
func (db *GoLevelDB) Set(key, value []byte) error {
if len(key) == 0 {
return errors.ErrKeyEmpty
return storeerrors.ErrKeyEmpty
}
if value == nil {
return errors.ErrValueNil
}
if err := db.db.Put(key, value, nil); err != nil {
return err
return storeerrors.ErrValueNil
}
return nil
return db.db.Put(key, value, nil)
}

// SetSync implements RawDB.
func (db *GoLevelDB) SetSync(key, value []byte) error {
if len(key) == 0 {
return errors.ErrKeyEmpty
return storeerrors.ErrKeyEmpty
}
if value == nil {
return errors.ErrValueNil
}
if err := db.db.Put(key, value, &opt.WriteOptions{Sync: true}); err != nil {
return err
return storeerrors.ErrValueNil
}
return nil
return db.db.Put(key, value, &opt.WriteOptions{Sync: true})
}

// Delete implements RawDB.
func (db *GoLevelDB) Delete(key []byte) error {
if len(key) == 0 {
return errors.ErrKeyEmpty
}
if err := db.db.Delete(key, nil); err != nil {
return err
return storeerrors.ErrKeyEmpty
}
return nil
return db.db.Delete(key, nil)
}

// DeleteSync implements RawDB.
func (db *GoLevelDB) DeleteSync(key []byte) error {
if len(key) == 0 {
return errors.ErrKeyEmpty
}
err := db.db.Delete(key, &opt.WriteOptions{Sync: true})
if err != nil {
return err
return storeerrors.ErrKeyEmpty
}
return nil
return db.db.Delete(key, &opt.WriteOptions{Sync: true})
}

func (db *GoLevelDB) RawDB() *leveldb.DB {
Expand All @@ -133,10 +114,7 @@ func (db *GoLevelDB) RawDB() *leveldb.DB {

// Close implements RawDB.
func (db *GoLevelDB) Close() error {
if err := db.db.Close(); err != nil {
return err
}
return nil
return db.db.Close()
}

// Print implements RawDB.
Expand Down Expand Up @@ -171,8 +149,7 @@ func (db *GoLevelDB) Stats() map[string]string {

stats := make(map[string]string)
for _, key := range keys {
str, err := db.db.GetProperty(key)
if err == nil {
if str, err := db.db.GetProperty(key); err == nil {
stats[key] = str
}
}
Expand All @@ -196,7 +173,7 @@ func (db *GoLevelDB) NewBatchWithSize(size int) store.RawBatch {
// Iterator implements RawDB.
func (db *GoLevelDB) Iterator(start, end []byte) (corestore.Iterator, error) {
if (start != nil && len(start) == 0) || (end != nil && len(end) == 0) {
return nil, errors.ErrKeyEmpty
return nil, storeerrors.ErrKeyEmpty
}
itr := db.db.NewIterator(&util.Range{Start: start, Limit: end}, nil)
return newGoLevelDBIterator(itr, start, end, false), nil
Expand All @@ -205,7 +182,7 @@ func (db *GoLevelDB) Iterator(start, end []byte) (corestore.Iterator, error) {
// ReverseIterator implements RawDB.
func (db *GoLevelDB) ReverseIterator(start, end []byte) (corestore.Iterator, error) {
if (start != nil && len(start) == 0) || (end != nil && len(end) == 0) {
return nil, errors.ErrKeyEmpty
return nil, storeerrors.ErrKeyEmpty
}
itr := db.db.NewIterator(&util.Range{Start: start, Limit: end}, nil)
return newGoLevelDBIterator(itr, start, end, true), nil
Expand All @@ -226,8 +203,7 @@ func newGoLevelDBIterator(source iterator.Iterator, start, end []byte, isReverse
if end == nil {
source.Last()
} else {
valid := source.Seek(end)
if valid {
if source.Seek(end) {
eoakey := source.Key() // end or after key
if bytes.Compare(end, eoakey) <= 0 {
source.Prev()
Expand Down Expand Up @@ -364,13 +340,13 @@ func newGoLevelDBBatchWithSize(db *GoLevelDB, size int) *goLevelDBBatch {
// Set implements RawBatch.
func (b *goLevelDBBatch) Set(key, value []byte) error {
if len(key) == 0 {
return errors.ErrKeyEmpty
return storeerrors.ErrKeyEmpty
}
if value == nil {
return errors.ErrValueNil
return storeerrors.ErrValueNil
}
if b.batch == nil {
return errors.ErrBatchClosed
return storeerrors.ErrBatchClosed
}
b.batch.Put(key, value)
return nil
Expand All @@ -379,10 +355,10 @@ func (b *goLevelDBBatch) Set(key, value []byte) error {
// Delete implements RawBatch.
func (b *goLevelDBBatch) Delete(key []byte) error {
if len(key) == 0 {
return errors.ErrKeyEmpty
return storeerrors.ErrKeyEmpty
}
if b.batch == nil {
return errors.ErrBatchClosed
return storeerrors.ErrBatchClosed
}
b.batch.Delete(key)
return nil
Expand All @@ -400,10 +376,9 @@ func (b *goLevelDBBatch) WriteSync() error {

func (b *goLevelDBBatch) write(sync bool) error {
if b.batch == nil {
return errors.ErrBatchClosed
return storeerrors.ErrBatchClosed
}
err := b.db.db.Write(b.batch, &opt.WriteOptions{Sync: sync})
if err != nil {
if err := b.db.db.Write(b.batch, &opt.WriteOptions{Sync: sync}); err != nil {
return err
}
// Make sure batch cannot be used afterwards. Callers should still call Close(), for errors.
Expand All @@ -422,7 +397,7 @@ func (b *goLevelDBBatch) Close() error {
// GetByteSize implements RawBatch
func (b *goLevelDBBatch) GetByteSize() (int, error) {
if b.batch == nil {
return 0, errors.ErrBatchClosed
return 0, storeerrors.ErrBatchClosed
}
return len(b.batch.Dump()), nil
}
Loading