Skip to content

Commit

Permalink
Apply changes from v0.1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
mattverse committed Jul 12, 2023
1 parent f4499a9 commit 34d4565
Show file tree
Hide file tree
Showing 15 changed files with 773 additions and 349 deletions.
6 changes: 3 additions & 3 deletions cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

// Node represents a node eligible for caching.
type Node interface {
GetKey() []byte
GetCacheKey() []byte
}

// Cache is an in-memory structure to persist nodes for quick access.
Expand Down Expand Up @@ -61,7 +61,7 @@ func New(maxElementCount int) Cache {
}

func (c *lruCache) Add(node Node) Node {
keyStr := ibytes.UnsafeBytesToStr(node.GetKey())
keyStr := ibytes.UnsafeBytesToStr(node.GetCacheKey())
if e, exists := c.dict[keyStr]; exists {
c.ll.MoveToFront(e)
old := e.Value
Expand Down Expand Up @@ -105,6 +105,6 @@ func (c *lruCache) Remove(key []byte) Node {

func (c *lruCache) remove(e *list.Element) Node {
removed := c.ll.Remove(e).(Node)
delete(c.dict, ibytes.UnsafeBytesToStr(removed.GetKey()))
delete(c.dict, ibytes.UnsafeBytesToStr(removed.GetCacheKey()))
return removed
}
8 changes: 4 additions & 4 deletions cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type testcase struct {
expectedNodeIndexes []int // contents of the cache once test case completes represent by indexes in testNodes
}

func (tn *testNode) GetKey() []byte {
func (tn *testNode) GetCacheKey() []byte {
return tn.key
}

Expand Down Expand Up @@ -271,7 +271,7 @@ func Test_Cache_Remove(t *testing.T) {

for _, op := range tc.cacheOps {

actualResult := cache.Remove(testNodes[op.testNodexIdx].GetKey())
actualResult := cache.Remove(testNodes[op.testNodexIdx].GetCacheKey())

expectedResult := op.expectedResult

Expand All @@ -296,8 +296,8 @@ func validateCacheContentsAfterTest(t *testing.T, tc testcase, cache cache.Cache
require.Equal(t, len(tc.expectedNodeIndexes), cache.Len())
for _, idx := range tc.expectedNodeIndexes {
expectedNode := testNodes[idx]
require.True(t, cache.Has(expectedNode.GetKey()))
require.Equal(t, expectedNode, cache.Get(expectedNode.GetKey()))
require.True(t, cache.Has(expectedNode.GetCacheKey()))
require.Equal(t, expectedNode, cache.Get(expectedNode.GetCacheKey()))
}
}

Expand Down
8 changes: 4 additions & 4 deletions export.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ func newExporter(tree *ImmutableTree) *Exporter {
func (e *Exporter) export(ctx context.Context) {
e.tree.root.traversePost(e.tree, true, func(node *Node) bool {
exportNode := &ExportNode{
Key: node.key,
Value: node.value,
Version: node.version,
Height: node.height,
Key: node.GetNodeKey(),
Value: node.GetValue(),
Version: node.GetVersion(),
Height: node.GetHeight(),
}

select {
Expand Down
2 changes: 1 addition & 1 deletion fast_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func DeserializeFastNode(key []byte, buf []byte) (*FastNode, error) {
return fastNode, nil
}

func (fn *FastNode) GetKey() []byte {
func (fn *FastNode) GetCacheKey() []byte {
return fn.key
}

Expand Down
22 changes: 14 additions & 8 deletions immutable_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package iavl
import (
"fmt"
"strings"
"sync"

dbm "github.com/tendermint/tm-db"
)
Expand All @@ -18,6 +19,7 @@ type ImmutableTree struct {
ndb *nodeDB
version int64
skipFastStorageUpgrade bool
mtx *sync.Mutex
}

// NewImmutableTree creates both in-memory and persistent instances
Expand All @@ -30,6 +32,7 @@ func NewImmutableTree(db dbm.DB, cacheSize int, skipFastStorageUpgrade bool) *Im
// NodeDB-backed Tree.
ndb: newNodeDB(db, cacheSize, nil),
skipFastStorageUpgrade: skipFastStorageUpgrade,
mtx: &sync.Mutex{},
}
}

Expand All @@ -39,6 +42,7 @@ func NewImmutableTreeWithOpts(db dbm.DB, cacheSize int, opts *Options, skipFastS
// NodeDB-backed Tree.
ndb: newNodeDB(db, cacheSize, opts),
skipFastStorageUpgrade: skipFastStorageUpgrade,
mtx: &sync.Mutex{},
}
}

Expand Down Expand Up @@ -86,12 +90,12 @@ func (t *ImmutableTree) renderNode(node *Node, indent string, depth int, encoder
}
// handle leaf
if node.isLeaf() {
here := fmt.Sprintf("%s%s", prefix, encoder(node.key, depth, true))
here := fmt.Sprintf("%s%s", prefix, encoder(node.GetNodeKey(), depth, true))
return []string{here}, nil
}

// recurse on inner node
here := fmt.Sprintf("%s%s", prefix, encoder(node.hash, depth, false))
here := fmt.Sprintf("%s%s", prefix, encoder(node.GetHash(), depth, false))

rightNode, err := node.getRightNode(t)
if err != nil {
Expand Down Expand Up @@ -123,7 +127,7 @@ func (t *ImmutableTree) Size() int64 {
if t.root == nil {
return 0
}
return t.root.size
return t.root.GetSize()
}

// Version returns the version of the tree.
Expand All @@ -136,7 +140,7 @@ func (t *ImmutableTree) Height() int8 {
if t.root == nil {
return 0
}
return t.root.height
return t.root.GetHeight()
}

// Has returns whether or not a key exists.
Expand Down Expand Up @@ -256,6 +260,7 @@ func (t *ImmutableTree) Iterator(start, end []byte, ascending bool) (dbm.Iterato
return NewFastIterator(start, end, ascending, t.ndb), nil
}
}

return NewIterator(start, end, ascending, t), nil
}

Expand All @@ -267,8 +272,8 @@ func (t *ImmutableTree) IterateRange(start, end []byte, ascending bool, fn func(
return false
}
return t.root.traverseInRange(t, start, end, ascending, false, false, func(node *Node) bool {
if node.height == 0 {
return fn(node.key, node.value)
if node.GetHeight() == 0 {
return fn(node.GetNodeKey(), node.GetValue())
}
return false
})
Expand All @@ -282,8 +287,8 @@ func (t *ImmutableTree) IterateRangeInclusive(start, end []byte, ascending bool,
return false
}
return t.root.traverseInRange(t, start, end, ascending, true, false, func(node *Node) bool {
if node.height == 0 {
return fn(node.key, node.value, node.version)
if node.GetHeight() == 0 {
return fn(node.GetNodeKey(), node.GetValue(), node.GetVersion())
}
return false
})
Expand Down Expand Up @@ -317,6 +322,7 @@ func (t *ImmutableTree) clone() *ImmutableTree {
ndb: t.ndb,
version: t.version,
skipFastStorageUpgrade: t.skipFastStorageUpgrade,
mtx: t.mtx,
}
}

Expand Down
Loading

0 comments on commit 34d4565

Please sign in to comment.