Skip to content

Commit 38fd6f2

Browse files
committed
decouple cosmos-db
1 parent 0710513 commit 38fd6f2

26 files changed

+844
-230
lines changed

basic_test.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ import (
88
"testing"
99

1010
"cosmossdk.io/log"
11-
db "github.com/cosmos/cosmos-db"
12-
iavlrand "github.com/cosmos/iavl/internal/rand"
1311
"github.com/stretchr/testify/assert"
1412
"github.com/stretchr/testify/require"
13+
14+
dbm "github.com/cosmos/iavl/db"
15+
iavlrand "github.com/cosmos/iavl/internal/rand"
1516
)
1617

1718
func TestBasic(t *testing.T) {
@@ -432,7 +433,7 @@ func TestIterateRange(t *testing.T) {
432433
}
433434

434435
func TestPersistence(t *testing.T) {
435-
db := db.NewMemDB()
436+
db := dbm.NewMemDB()
436437

437438
// Create some random key value pairs
438439
records := make(map[string]string)
@@ -495,7 +496,7 @@ func TestProof(t *testing.T) {
495496
}
496497

497498
func TestTreeProof(t *testing.T) {
498-
db := db.NewMemDB()
499+
db := dbm.NewMemDB()
499500
tree := NewMutableTree(db, 100, false, log.NewNopLogger())
500501
hash := tree.Hash()
501502
assert.Equal(t, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", hex.EncodeToString(hash))

batch.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package iavl
22

33
import (
4-
dbm "github.com/cosmos/cosmos-db"
4+
dbm "github.com/cosmos/iavl/db"
55
)
66

77
// BatchWithFlusher is a wrapper

batch_test.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ import (
77
"path/filepath"
88
"testing"
99

10-
dbm "github.com/cosmos/cosmos-db"
1110
"github.com/stretchr/testify/require"
11+
12+
dbm "github.com/cosmos/iavl/db"
1213
)
1314

1415
func cleanupDBDir(dir, name string) {
@@ -27,16 +28,16 @@ func makeKey(n uint16) []byte {
2728
}
2829

2930
func TestBatchWithFlusher(t *testing.T) {
30-
testedBackends := []dbm.BackendType{
31-
dbm.GoLevelDBBackend,
31+
testedBackends := []string{
32+
"goleveldb",
3233
}
3334

3435
for _, backend := range testedBackends {
3536
testBatchWithFlusher(t, backend)
3637
}
3738
}
3839

39-
func testBatchWithFlusher(t *testing.T, backend dbm.BackendType) {
40+
func testBatchWithFlusher(t *testing.T, backend string) {
4041
name := fmt.Sprintf("test_%x", randstr(12))
4142
dir := t.TempDir()
4243
db, err := dbm.NewDB(name, backend, dir)

benchmarks/bench_test.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import (
1212
"cosmossdk.io/log"
1313
"github.com/stretchr/testify/require"
1414

15-
db "github.com/cosmos/cosmos-db"
1615
"github.com/cosmos/iavl"
16+
dbm "github.com/cosmos/iavl/db"
1717
)
1818

1919
const historySize = 20
@@ -26,7 +26,7 @@ func randBytes(length int) []byte {
2626
return key
2727
}
2828

29-
func prepareTree(b *testing.B, db db.DB, size, keyLen, dataLen int) (*iavl.MutableTree, [][]byte) {
29+
func prepareTree(b *testing.B, db dbm.DB, size, keyLen, dataLen int) (*iavl.MutableTree, [][]byte) {
3030
t := iavl.NewMutableTree(db, size, false, log.NewNopLogger())
3131
keys := make([][]byte, size)
3232

@@ -147,7 +147,7 @@ func runIterationSlow(b *testing.B, t *iavl.MutableTree, expectedSize int) {
147147
}
148148
}
149149

150-
func iterate(b *testing.B, itr db.Iterator, expectedSize int) {
150+
func iterate(b *testing.B, itr dbm.Iterator, expectedSize int) {
151151
b.StartTimer()
152152
keyValuePairs := make([][][]byte, 0, expectedSize)
153153
for i := 0; i < expectedSize && itr.Valid(); i++ {
@@ -259,7 +259,7 @@ func BenchmarkRandomBytes(b *testing.B) {
259259
}
260260

261261
type benchmark struct {
262-
dbType db.BackendType
262+
dbType string
263263
initSize, blockSize int
264264
keyLen, dataLen int
265265
}
@@ -330,7 +330,7 @@ func runBenchmarks(b *testing.B, benchmarks []benchmark) {
330330

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

@@ -343,11 +343,11 @@ func runBenchmarks(b *testing.B, benchmarks []benchmark) {
343343

344344
// note that "" leads to nil backing db!
345345
var (
346-
d db.DB
346+
d dbm.DB
347347
err error
348348
)
349349
if bb.dbType != "nodb" {
350-
d, err = db.NewDB("test", bb.dbType, dirName)
350+
d, err = dbm.NewDB("test", bb.dbType, dirName)
351351

352352
if err != nil {
353353
if strings.Contains(err.Error(), "unknown db_backend") {
@@ -376,7 +376,7 @@ func memUseMB() float64 {
376376
return mb
377377
}
378378

379-
func runSuite(b *testing.B, d db.DB, initSize, blockSize, keyLen, dataLen int) {
379+
func runSuite(b *testing.B, d dbm.DB, initSize, blockSize, keyLen, dataLen int) {
380380
// measure mem usage
381381
runtime.GC()
382382
init := memUseMB()

benchmarks/cosmos-exim/main.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import (
77

88
"cosmossdk.io/log"
99
tmdb "github.com/cosmos/cosmos-db"
10+
1011
"github.com/cosmos/iavl"
12+
idbm "github.com/cosmos/iavl/db"
1113
)
1214

1315
// stores is the list of stores in the CosmosHub database
@@ -91,7 +93,7 @@ func runExport(dbPath string) (int64, map[string][]*iavl.ExportNode, error) {
9193
if err != nil {
9294
return 0, nil, err
9395
}
94-
tree := iavl.NewMutableTree(tmdb.NewPrefixDB(ldb, []byte("s/k:main/")), 0, false, log.NewNopLogger())
96+
tree := iavl.NewMutableTree(idbm.NewWrapper(tmdb.NewPrefixDB(ldb, []byte("s/k:main/"))), 0, false, log.NewNopLogger())
9597
version, err := tree.LoadVersion(0)
9698
if err != nil {
9799
return 0, nil, err
@@ -103,7 +105,7 @@ func runExport(dbPath string) (int64, map[string][]*iavl.ExportNode, error) {
103105
totalStats := Stats{}
104106
for _, name := range stores {
105107
db := tmdb.NewPrefixDB(ldb, []byte("s/k:"+name+"/"))
106-
tree := iavl.NewMutableTree(db, 0, false, log.NewNopLogger())
108+
tree := iavl.NewMutableTree(idbm.NewWrapper(db), 0, false, log.NewNopLogger())
107109

108110
stats := Stats{}
109111
export := make([]*iavl.ExportNode, 0, 100000)
@@ -168,7 +170,7 @@ func runImport(version int64, exports map[string][]*iavl.ExportNode) error {
168170
if err != nil {
169171
return err
170172
}
171-
newTree := iavl.NewMutableTree(newDB, 0, false, log.NewNopLogger())
173+
newTree := iavl.NewMutableTree(idbm.NewWrapper(newDB), 0, false, log.NewNopLogger())
172174
importer, err := newTree.Import(version)
173175
if err != nil {
174176
return err

cmd/iaviewer/main.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
dbm "github.com/cosmos/cosmos-db"
1515

1616
"github.com/cosmos/iavl"
17+
idbm "github.com/cosmos/iavl/db"
1718
)
1819

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

125-
tree := iavl.NewMutableTree(db, DefaultCacheSize, false, log.NewLogger(os.Stdout))
126+
tree := iavl.NewMutableTree(idbm.NewWrapper(db), DefaultCacheSize, false, log.NewLogger(os.Stdout))
126127
ver, err := tree.LoadVersion(int64(version))
127128
fmt.Printf("Got version: %d\n", ver)
128129
return tree, err

0 commit comments

Comments
 (0)