Skip to content

Commit

Permalink
feat(core/coretesting): make memDB satisfy db.Db interface
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt committed Nov 20, 2024
1 parent 50c5963 commit 77de106
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
33 changes: 32 additions & 1 deletion core/testing/memdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,9 @@ func (mi *memIterator) assertValid() {
}
}

var _ store.KVStoreWithBatch = (*MemDB)(nil)
var (
_ store.KVStoreWithBatch = (*MemDB)(nil)
)

// MemDB is a simple in-memory key-value store with Batch support.
type MemDB struct {
Expand Down Expand Up @@ -291,12 +293,20 @@ func (bt *MemDB) Set(key, value []byte) error {
return bt.kv.Set(key, value)
}

func (bt *MemDB) SetSync(key, value []byte) error {
return bt.Set(key, value)
}

func (bt *MemDB) Delete(key []byte) error {
bt.mtx.Lock()
defer bt.mtx.Unlock()
return bt.kv.Delete(key)
}

func (bt *MemDB) DeleteSync(key []byte) error {
return bt.Delete(key)
}

func (bt *MemDB) Iterator(start, end []byte) (store.Iterator, error) {
return bt.kv.Iterator(start, end)
}
Expand All @@ -305,6 +315,27 @@ func (bt *MemDB) ReverseIterator(start, end []byte) (store.Iterator, error) {
return bt.kv.ReverseIterator(start, end)
}

func (db *MemDB) Print() error {
db.mtx.RLock()
defer db.mtx.RUnlock()

db.kv.tree.Ascend(item{}, func(i item) bool {
fmt.Printf("[%X]:\t[%X]\n", i.key, i.value)
return true
})
return nil
}

func (db *MemDB) Stats() map[string]string {
db.mtx.RLock()
defer db.mtx.RUnlock()

stats := make(map[string]string)
stats["database.type"] = "memDB"
stats["database.size"] = fmt.Sprintf("%d", db.kv.tree.Len())
return stats
}

// Close closes the MemDB, releasing any resources held.
func (db *MemDB) Close() error {
return nil
Expand Down
14 changes: 14 additions & 0 deletions tests/integration/type_check.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package integration

import (
coretesting "cosmossdk.io/core/testing"

db "github.com/cosmos/cosmos-db"
)

// This file contains a list of type checks that are used to ensure that implementations
// matches the interface. We do not do those type checks directly in the components to
// avoid to bring in more dependencies than needed.
var (
_ db.DB = (*coretesting.MemDB)(nil)
)

0 comments on commit 77de106

Please sign in to comment.