Skip to content

Commit

Permalink
refactor: remove cosmos-db as a dep (#955)
Browse files Browse the repository at this point in the history
  • Loading branch information
tac0turtle authored Jun 11, 2024
1 parent fcc7eb7 commit e063edd
Show file tree
Hide file tree
Showing 38 changed files with 973 additions and 861 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ format:

# look into .golangci.yml for enabling / disabling linters
golangci_lint_cmd=golangci-lint
golangci_version=v1.55.2
golangci_version=v1.59.1

lint:
@echo "--> Running linter"
Expand Down
2 changes: 1 addition & 1 deletion basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ func TestIterateRange(t *testing.T) {
}
// test traversing the whole node works... in order
viewed := []string{}
tree.Iterate(func(key []byte, value []byte) bool {
tree.Iterate(func(key []byte, _ []byte) bool {
viewed = append(viewed, string(key))
return false
})
Expand Down
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
12 changes: 3 additions & 9 deletions batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,13 @@ func makeKey(n uint16) []byte {
}

func TestBatchWithFlusher(t *testing.T) {
testedBackends := []string{
"goleveldb",
}

for _, backend := range testedBackends {
testBatchWithFlusher(t, backend)
}
testBatchWithFlusher(t)
}

func testBatchWithFlusher(t *testing.T, backend string) {
func testBatchWithFlusher(t *testing.T) {
name := fmt.Sprintf("test_%x", randstr(12))
dir := t.TempDir()
db, err := dbm.NewDB(name, backend, dir)
db, err := dbm.NewGoLevelDB(name, dir)
require.NoError(t, err)
defer cleanupDBDir(dir, name)

Expand Down
17 changes: 3 additions & 14 deletions benchmarks/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
mrand "math/rand"
"os"
"runtime"
"strings"
"testing"

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

"github.com/cosmos/iavl"
Expand Down Expand Up @@ -147,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 Expand Up @@ -330,9 +330,6 @@ func runBenchmarks(b *testing.B, benchmarks []benchmark) {

// prepare a dir for the db and cleanup afterwards
dirName := fmt.Sprintf("./%s-db", prefix)
if bb.dbType == "rocksdb" {
_ = os.Mkdir(dirName, 0o755)
}

defer func() {
err := os.RemoveAll(dirName)
Expand All @@ -347,16 +344,8 @@ func runBenchmarks(b *testing.B, benchmarks []benchmark) {
err error
)
if bb.dbType != "nodb" {
d, err = dbm.NewDB("test", bb.dbType, dirName)

d, err = dbm.NewGoLevelDB("test", dirName)
if err != nil {
if strings.Contains(err.Error(), "unknown db_backend") {
// As an exception to run benchmarks: if the error is about cleveldb, or rocksdb,
// it requires a tag "cleveldb" to link the database at runtime, so instead just
// log the error instead of failing.
b.Logf("%+v\n", err)
continue
}
require.NoError(b, err)
}
defer d.Close()
Expand Down
13 changes: 6 additions & 7 deletions benchmarks/cosmos-exim/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"time"

"cosmossdk.io/core/log"
tmdb "github.com/cosmos/cosmos-db"

"github.com/cosmos/iavl"
idbm "github.com/cosmos/iavl/db"
Expand Down Expand Up @@ -89,11 +88,11 @@ func run(dbPath string) error {

// runExport runs an export benchmark and returns a map of store names/export nodes
func runExport(dbPath string) (int64, map[string][]*iavl.ExportNode, error) {
ldb, err := tmdb.NewDB("application", tmdb.GoLevelDBBackend, dbPath)
ldb, err := idbm.NewGoLevelDB("application", dbPath)
if err != nil {
return 0, nil, err
}
tree := iavl.NewMutableTree(idbm.NewWrapper(tmdb.NewPrefixDB(ldb, []byte("s/k:main/"))), 0, false, log.NewNopLogger())
tree := iavl.NewMutableTree(idbm.NewPrefixDB(ldb, []byte("s/k:main/")), 0, false, log.NewNopLogger())
version, err := tree.LoadVersion(0)
if err != nil {
return 0, nil, err
Expand All @@ -104,8 +103,8 @@ func runExport(dbPath string) (int64, map[string][]*iavl.ExportNode, error) {

totalStats := Stats{}
for _, name := range stores {
db := tmdb.NewPrefixDB(ldb, []byte("s/k:"+name+"/"))
tree := iavl.NewMutableTree(idbm.NewWrapper(db), 0, false, log.NewNopLogger())
db := idbm.NewPrefixDB(ldb, []byte("s/k:"+name+"/"))
tree := iavl.NewMutableTree(db, 0, false, log.NewNopLogger())

stats := Stats{}
export := make([]*iavl.ExportNode, 0, 100000)
Expand Down Expand Up @@ -166,11 +165,11 @@ func runImport(version int64, exports map[string][]*iavl.ExportNode) error {
start := time.Now()
stats := Stats{}

newDB, err := tmdb.NewDB(name, tmdb.GoLevelDBBackend, tempdir)
newDB, err := idbm.NewGoLevelDB(name, tempdir)
if err != nil {
return err
}
newTree := iavl.NewMutableTree(idbm.NewWrapper(newDB), 0, false, log.NewNopLogger())
newTree := iavl.NewMutableTree(newDB, 0, false, log.NewNopLogger())
importer, err := newTree.Import(version)
if err != nil {
return err
Expand Down
53 changes: 0 additions & 53 deletions benchmarks/hash_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion cmd/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ module github.com/cosmos/iavl/cmd
go 1.21

require (
cosmossdk.io/core v0.12.1-0.20240514205955-97c9bbb0341b
cosmossdk.io/log v1.3.2-0.20240514205955-97c9bbb0341b
github.com/cosmos/cosmos-db v1.0.2
github.com/cosmos/iavl v1.2.0
)

require (
cosmossdk.io/core v0.12.1-0.20240514205955-97c9bbb0341b // indirect
github.com/DataDog/zstd v1.4.5 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions cmd/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,8 @@ golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 h1:uVc8UZUe6tr40fFVnUP5Oj+veunVezqYl9z7DYw9xzw=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down
8 changes: 4 additions & 4 deletions cmd/iaviewer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,15 @@ diff a-cur.shape b-cur.shape
```

Yup, that is quite some difference. You can also look at the tree as a whole.
So, stretch your terminal nice and wide, and...
So, stretch your terminal nice and wide, and...

```shell
less a-cur.shape
```

It has `-5 ` for an inner node of depth 5, and `*6 ` for a leaf node (data) of depth 6.
Indentation also suggests the shape of the tree.
It has `-5` for an inner node of depth 5, and `*6` for a leaf node (data) of depth 6.
Indentation also suggests the shape of the tree.

Note, if anyone wants to improve the visualization, that would be awesome.
I have no idea how to do this well, but at least text output makes some
sense and is diff-able.
sense and is diff-able.
3 changes: 1 addition & 2 deletions cmd/iaviewer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
dbm "github.com/cosmos/cosmos-db"

"github.com/cosmos/iavl"
idbm "github.com/cosmos/iavl/db"
)

// TODO: make this configurable?
Expand Down Expand Up @@ -123,7 +122,7 @@ func ReadTree(dir string, version int, prefix []byte) (*iavl.MutableTree, error)
db = dbm.NewPrefixDB(db, prefix)
}

tree := iavl.NewMutableTree(idbm.NewWrapper(db), DefaultCacheSize, false, log.NewLogger(os.Stdout))
tree := iavl.NewMutableTree(newWrapper(db), DefaultCacheSize, false, log.NewLogger(os.Stdout))
ver, err := tree.LoadVersion(int64(version))
fmt.Printf("Got version: %d\n", ver)
return tree, err
Expand Down
37 changes: 37 additions & 0 deletions cmd/iaviewer/wrapper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package main

import (
corestore "cosmossdk.io/core/store"
dbm "github.com/cosmos/cosmos-db"
)

// Wrapper wraps a dbm.DB to implement DB.
type Wrapper struct {
dbm.DB
}

// newWrapper returns a new Wrapper.
// Wrapper must be implemented against rocksdb.DB and pebbleDB separately
func newWrapper(db dbm.DB) *Wrapper {
return &Wrapper{DB: db}
}

// Iterator implements DB.
func (db *Wrapper) Iterator(start, end []byte) (corestore.Iterator, error) {
return db.DB.Iterator(start, end)
}

// ReverseIterator implements DB.
func (db *Wrapper) ReverseIterator(start, end []byte) (corestore.Iterator, error) {
return db.DB.ReverseIterator(start, end)
}

// NewBatch implements DB.
func (db *Wrapper) NewBatch() corestore.Batch {
return db.DB.NewBatch()
}

// NewBatchWithSize implements DB.
func (db *Wrapper) NewBatchWithSize(size int) corestore.Batch {
return db.DB.NewBatchWithSize(size)
}
14 changes: 7 additions & 7 deletions db/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ package main

import (
"cosmossdk.io/log"
dbm "github.com/cosmos/cosmos-db"
dbm "github.com/cosmos/cosmos-db"

"github.com/cosmos/iavl"
idbm "github.com/cosmos/iavl/db"
"github.com/cosmos/iavl"
idbm "github.com/cosmos/iavl/db"
)

func main() {
levelDB, err := dbm.NewDB("application", dbm.GoLevelDBBackend, "test")
if err != nil {
panic(err)
}
if err != nil {
panic(err)
}

tree := iavl.NewMutableTree(idbm.NewWrapper(dbm.NewPrefixDB(levelDB, []byte("s/k:main/"))), 0, false, log.NewNopLogger())
tree := iavl.NewMutableTree(idbm.NewWrapper(dbm.NewPrefixDB(levelDB, []byte("s/k:main/"))), 0, false, log.NewNopLogger())
}
```
Loading

0 comments on commit e063edd

Please sign in to comment.