Skip to content

Commit

Permalink
remove extra interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
tac0turtle committed Jun 11, 2024
1 parent 2eedc5d commit f7e619b
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 109 deletions.
7 changes: 4 additions & 3 deletions batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package iavl
import (
"sync"

corestore "cosmossdk.io/core/store"
dbm "github.com/cosmos/iavl/db"
)

Expand All @@ -11,13 +12,13 @@ import (
// as soon as the configurable limit is reached.
type BatchWithFlusher struct {
mtx sync.Mutex
db dbm.DB // This is only used to create new batch
batch dbm.Batch // Batched writing buffer.
db dbm.DB // This is only used to create new batch
batch corestore.Batch // Batched writing buffer.

flushThreshold int // The threshold to flush the batch to disk.
}

var _ dbm.Batch = (*BatchWithFlusher)(nil)
var _ corestore.Batch = (*BatchWithFlusher)(nil)

// NewBatchWithFlusher returns new BatchWithFlusher wrapping the passed in batch
func NewBatchWithFlusher(db dbm.DB, flushThreshold int) *BatchWithFlusher {
Expand Down
3 changes: 2 additions & 1 deletion benchmarks/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"testing"

"cosmossdk.io/core/log"
corestore "cosmossdk.io/core/store"
"github.com/stretchr/testify/require"

"github.com/cosmos/iavl"
Expand Down Expand Up @@ -146,7 +147,7 @@ func runIterationSlow(b *testing.B, t *iavl.MutableTree, expectedSize int) {
}
}

func iterate(b *testing.B, itr dbm.Iterator, expectedSize int) {
func iterate(b *testing.B, itr corestore.Iterator, expectedSize int) {
b.StartTimer()
keyValuePairs := make([][][]byte, 0, expectedSize)
for i := 0; i < expectedSize && itr.Valid(); i++ {
Expand Down
4 changes: 2 additions & 2 deletions db/goleveldb.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ type goLevelDBIterator struct {
isInvalid bool
}

var _ Iterator = (*goLevelDBIterator)(nil)
var _ corestore.Iterator = (*goLevelDBIterator)(nil)

func newGoLevelDBIterator(source iterator.Iterator, start, end []byte, isReverse bool) *goLevelDBIterator {
if isReverse {
Expand Down Expand Up @@ -312,7 +312,7 @@ type goLevelDBBatch struct {
batch *leveldb.Batch
}

var _ Batch = (*goLevelDBBatch)(nil)
var _ corestore.Batch = (*goLevelDBBatch)(nil)

func newGoLevelDBBatch(db *GoLevelDB) *goLevelDBBatch {
return &goLevelDBBatch{
Expand Down
2 changes: 1 addition & 1 deletion db/memdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ type memDBIterator struct {
useMtx bool
}

var _ Iterator = (*memDBIterator)(nil)
var _ corestore.Iterator = (*memDBIterator)(nil)

// newMemDBIterator creates a new memDBIterator.
func newMemDBIterator(db *MemDB, start []byte, end []byte, reverse bool) *memDBIterator {
Expand Down
14 changes: 7 additions & 7 deletions db/prefixdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func (pdb *PrefixDB) prefixed(key []byte) []byte {

// IteratePrefix is a convenience function for iterating over a key domain
// restricted by prefix.
func IteratePrefix(db DB, prefix []byte) (Iterator, error) {
func IteratePrefix(db DB, prefix []byte) (corestore.Iterator, error) {
var start, end []byte
if len(prefix) == 0 {
start = nil
Expand All @@ -175,14 +175,14 @@ type prefixDBIterator struct {
prefix []byte
start []byte
end []byte
source Iterator
source corestore.Iterator
valid bool
err error
}

var _ Iterator = (*prefixDBIterator)(nil)
var _ corestore.Iterator = (*prefixDBIterator)(nil)

func newPrefixIterator(prefix, start, end []byte, source Iterator) *prefixDBIterator {
func newPrefixIterator(prefix, start, end []byte, source corestore.Iterator) *prefixDBIterator {
pitrInvalid := &prefixDBIterator{
prefix: prefix,
start: start,
Expand Down Expand Up @@ -279,12 +279,12 @@ func (itr *prefixDBIterator) assertIsValid() {

type prefixDBBatch struct {
prefix []byte
source Batch
source corestore.Batch
}

var _ Batch = (*prefixDBBatch)(nil)
var _ corestore.Batch = (*prefixDBBatch)(nil)

func newPrefixBatch(prefix []byte, source Batch) prefixDBBatch {
func newPrefixBatch(prefix []byte, source corestore.Batch) prefixDBBatch {
return prefixDBBatch{
prefix: prefix,
source: source,
Expand Down
82 changes: 0 additions & 82 deletions db/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,85 +57,3 @@ type DB interface {
// This will does the same thing as NewBatch if the batch implementation doesn't support pre-allocation.
NewBatchWithSize(int) corestore.Batch
}

// Iterator represents an iterator over a domain of keys. Callers must call Close when done.
// No writes can happen to a domain while there exists an iterator over it, some backends may take
// out database locks to ensure this will not happen.
//
// Callers must make sure the iterator is valid before calling any methods on it, otherwise
// these methods will panic. This is in part caused by most backend databases using this convention.
//
// As with DB, keys and values should be considered read-only, and must be copied before they are
// modified.
//
// Typical usage:
//
// var itr Iterator = ...
// defer itr.Close()
//
// for ; itr.Valid(); itr.Next() {
// k, v := itr.Key(); itr.Value()
// ...
// }
//
// if err := itr.Error(); err != nil {
// ...
// }
type Iterator interface {
// Domain returns the start (inclusive) and end (exclusive) limits of the iterator.
// CONTRACT: start, end readonly []byte
Domain() (start []byte, end []byte)

// Valid returns whether the current iterator is valid. Once invalid, the Iterator remains
// invalid forever.
Valid() bool

// Next moves the iterator to the next key in the database, as defined by order of iteration.
// If Valid returns false, this method will panic.
Next()

// Key returns the key at the current position. Panics if the iterator is invalid.
// CONTRACT: key readonly []byte
Key() (key []byte)

// Value returns the value at the current position. Panics if the iterator is invalid.
// CONTRACT: value readonly []byte
Value() (value []byte)

// Error returns the last error encountered by the iterator, if any.
Error() error

// Close closes the iterator, relasing any allocated resources.
Close() error
}

// Batch represents a group of writes. They may or may not be written atomically depending on the
// backend. Callers must call Close on the batch when done.
//
// As with DB, given keys and values should be considered read-only, and must not be modified after
// passing them to the batch.
type Batch interface {
// Set sets a key/value pair.
// CONTRACT: key, value readonly []byte
Set(key, value []byte) error

// Delete deletes a key/value pair.
// CONTRACT: key readonly []byte
Delete(key []byte) error

// Write writes the batch, possibly without flushing to disk. Only Close() can be called after,
// other methods will error.
Write() error

// WriteSync writes the batch and flushes it to disk. Only Close() can be called after, other
// methods will error.
WriteSync() error

// Close closes the batch. It is idempotent, but calls to other methods afterwards will error.
Close() error

// GetByteSize that returns the current size of the batch in bytes. Depending on the implementation,
// this may return the size of the underlying LSM batch, including the size of additional metadata
// on top of the expected key and value total byte count.
GetByteSize() (int, error)
}
3 changes: 2 additions & 1 deletion immutable_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"

"cosmossdk.io/core/log"
corestore "cosmossdk.io/core/store"

dbm "github.com/cosmos/iavl/db"
)
Expand Down Expand Up @@ -246,7 +247,7 @@ func (t *ImmutableTree) Iterate(fn func(key []byte, value []byte) bool) (bool, e
}

// Iterator returns an iterator over the immutable tree.
func (t *ImmutableTree) Iterator(start, end []byte, ascending bool) (dbm.Iterator, error) {
func (t *ImmutableTree) Iterator(start, end []byte, ascending bool) (corestore.Iterator, error) {
if !t.skipFastStorageUpgrade {
isFastCacheEnabled, err := t.IsFastCacheEnabled()
if err != nil {
Expand Down
15 changes: 8 additions & 7 deletions iterator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"

log "cosmossdk.io/core/log"
corestore "cosmossdk.io/core/store"
"github.com/stretchr/testify/require"

dbm "github.com/cosmos/iavl/db"
Expand All @@ -16,7 +17,7 @@ func TestIterator_NewIterator_NilTree_Failure(t *testing.T) {
start, end := []byte{'a'}, []byte{'c'}
ascending := true

performTest := func(t *testing.T, itr dbm.Iterator) {
performTest := func(t *testing.T, itr corestore.Iterator) {
require.NotNil(t, itr)
require.False(t, itr.Valid())
actualsStart, actualEnd := itr.Domain()
Expand Down Expand Up @@ -48,7 +49,7 @@ func TestUnsavedFastIterator_NewIterator_NilAdditions_Failure(t *testing.T) {
start, end := []byte{'a'}, []byte{'c'}
ascending := true

performTest := func(t *testing.T, itr dbm.Iterator) {
performTest := func(t *testing.T, itr corestore.Iterator) {
require.NotNil(t, itr)
require.False(t, itr.Valid())
actualsStart, actualEnd := itr.Domain()
Expand Down Expand Up @@ -94,7 +95,7 @@ func TestIterator_Empty_Invalid(t *testing.T) {
ascending: true,
}

performTest := func(t *testing.T, itr dbm.Iterator, mirror [][]string) {
performTest := func(t *testing.T, itr corestore.Iterator, mirror [][]string) {
require.Equal(t, 0, len(mirror))
require.False(t, itr.Valid())
}
Expand Down Expand Up @@ -217,7 +218,7 @@ func TestIterator_WithDelete_Full_Ascending_Success(t *testing.T) {
}

func iteratorSuccessTest(t *testing.T, config *iteratorTestConfig) {
performTest := func(t *testing.T, itr dbm.Iterator, mirror [][]string) {
performTest := func(t *testing.T, itr corestore.Iterator, mirror [][]string) {
actualStart, actualEnd := itr.Domain()
require.Equal(t, config.startIterate, actualStart)
require.Equal(t, config.endIterate, actualEnd)
Expand Down Expand Up @@ -246,7 +247,7 @@ func iteratorSuccessTest(t *testing.T, config *iteratorTestConfig) {
})
}

func setupIteratorAndMirror(t *testing.T, config *iteratorTestConfig) (dbm.Iterator, [][]string) {
func setupIteratorAndMirror(t *testing.T, config *iteratorTestConfig) (corestore.Iterator, [][]string) {
tree := NewMutableTree(dbm.NewMemDB(), 0, false, log.NewNopLogger())

mirror := setupMirrorForIterator(t, config, tree)
Expand All @@ -262,7 +263,7 @@ func setupIteratorAndMirror(t *testing.T, config *iteratorTestConfig) (dbm.Itera
return itr, mirror
}

func setupFastIteratorAndMirror(t *testing.T, config *iteratorTestConfig) (dbm.Iterator, [][]string) {
func setupFastIteratorAndMirror(t *testing.T, config *iteratorTestConfig) (corestore.Iterator, [][]string) {
tree := NewMutableTree(dbm.NewMemDB(), 0, false, log.NewNopLogger())

mirror := setupMirrorForIterator(t, config, tree)
Expand All @@ -273,7 +274,7 @@ func setupFastIteratorAndMirror(t *testing.T, config *iteratorTestConfig) (dbm.I
return itr, mirror
}

func setupUnsavedFastIterator(t *testing.T, config *iteratorTestConfig) (dbm.Iterator, [][]string) {
func setupUnsavedFastIterator(t *testing.T, config *iteratorTestConfig) (corestore.Iterator, [][]string) {
tree := NewMutableTree(dbm.NewMemDB(), 0, false, log.NewNopLogger())

// For unsaved fast iterator, we would like to test the state where
Expand Down
3 changes: 2 additions & 1 deletion mutable_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"sync"

log "cosmossdk.io/core/log"
corestore "cosmossdk.io/core/store"

dbm "github.com/cosmos/iavl/db"
"github.com/cosmos/iavl/fastnode"
Expand Down Expand Up @@ -228,7 +229,7 @@ func (tree *MutableTree) Iterate(fn func(key []byte, value []byte) bool) (stoppe

// Iterator returns an iterator over the mutable tree.
// CONTRACT: no updates are made to the tree while an iterator is active.
func (tree *MutableTree) Iterator(start, end []byte, ascending bool) (dbm.Iterator, error) {
func (tree *MutableTree) Iterator(start, end []byte, ascending bool) (corestore.Iterator, error) {
if !tree.skipFastStorageUpgrade {
isFastCacheEnabled, err := tree.IsFastCacheEnabled()
if err != nil {
Expand Down
7 changes: 4 additions & 3 deletions nodedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"time"

"cosmossdk.io/core/log"
corestore "cosmossdk.io/core/store"

"github.com/cosmos/iavl/cache"
dbm "github.com/cosmos/iavl/db"
Expand Down Expand Up @@ -80,7 +81,7 @@ type nodeDB struct {

mtx sync.Mutex // Read/write lock.
db dbm.DB // Persistent node storage.
batch dbm.Batch // Batched writing buffer.
batch corestore.Batch // Batched writing buffer.
opts Options // Options to customize for pruning/writing
versionReaders map[int64]uint32 // Number of active version readers
storageVersion string // Storage version
Expand Down Expand Up @@ -873,7 +874,7 @@ func (ndb *nodeDB) traversePrefix(prefix []byte, fn func(k, v []byte) error) err
}

// Get the iterator for a given prefix.
func (ndb *nodeDB) getPrefixIterator(prefix []byte) (dbm.Iterator, error) {
func (ndb *nodeDB) getPrefixIterator(prefix []byte) (corestore.Iterator, error) {
var start, end []byte
if len(prefix) == 0 {
start = nil
Expand All @@ -887,7 +888,7 @@ func (ndb *nodeDB) getPrefixIterator(prefix []byte) (dbm.Iterator, error) {
}

// Get iterator for fast prefix and error, if any
func (ndb *nodeDB) getFastIterator(start, end []byte, ascending bool) (dbm.Iterator, error) {
func (ndb *nodeDB) getFastIterator(start, end []byte, ascending bool) (corestore.Iterator, error) {
var startFormatted, endFormatted []byte

if start != nil {
Expand Down
3 changes: 2 additions & 1 deletion testutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"testing"

log "cosmossdk.io/core/log"
corestore "cosmossdk.io/core/store"
"github.com/stretchr/testify/require"

dbm "github.com/cosmos/iavl/db"
Expand Down Expand Up @@ -285,7 +286,7 @@ func setupMirrorForIterator(t *testing.T, config *iteratorTestConfig, tree *Muta

// assertIterator confirms that the iterator returns the expected values desribed by mirror in the same order.
// mirror is a slice containing slices of the form [key, value]. In other words, key at index 0 and value at index 1.
func assertIterator(t *testing.T, itr dbm.Iterator, mirror [][]string, ascending bool) {
func assertIterator(t *testing.T, itr corestore.Iterator, mirror [][]string, ascending bool) {
startIdx, endIdx := 0, len(mirror)-1
increment := 1
mirrorIdx := startIdx
Expand Down

0 comments on commit f7e619b

Please sign in to comment.