Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: change key format for node #568

Closed
wants to merge 16 commits into from
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ jobs:
- name: test & coverage report creation
run: |
go test ./... -mod=readonly -timeout 5m -short -race -coverprofile=coverage.txt -covermode=atomic
go test ./... -mod=readonly -timeout 8m
GOARCH=386 go test ./... -mod=readonly -timeout 8m
go test ./... -mod=readonly -timeout 5m
GOARCH=386 go test ./... -mod=readonly -timeout 5m
5 changes: 3 additions & 2 deletions basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ func TestPersistence(t *testing.T) {

// Create some random key value pairs
records := make(map[string]string)
for i := 0; i < 10000; i++ {
for i := 0; i < 8; i++ {
records[randstr(20)] = randstr(20)
}

Expand All @@ -480,7 +480,8 @@ func TestPersistence(t *testing.T) {
// Load a tree
t2, err := NewMutableTree(db, 0, false)
require.NoError(t, err)
t2.Load()
_, err = t2.Load()
require.NoError(t, err)
for key, value := range records {
t2value, err := t2.Get([]byte(key))
require.NoError(t, err)
Expand Down
4 changes: 2 additions & 2 deletions docs/tree/mutable_tree.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,9 @@ Before `RotateRight(node8)`:
After `RotateRight(node8)`:
```
|---9
|---8
|---8 call SaveNode(*node)
| | |---7
| |---6
| |---6(P)
| |---5
4'---|
| |---1
Expand Down
25 changes: 25 additions & 0 deletions export_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package iavl

import (
"fmt"
"math"
"math/rand"
"testing"
Expand All @@ -11,6 +12,25 @@ import (
db "github.com/cosmos/cosmos-db"
)

func printNodes(t *MutableTree) {
actual := []ExportNode{}
exporter := t.Export()
defer exporter.Close()
for {
node, err := exporter.Next()
if err == ExportDone {
break
}
actual = append(actual, *node)
}

for _, i := range actual {
fmt.Printf("Key: %s, Value: %d, Version: %d, Height: %d \n", i.Key, i.Value, i.Version, i.Height)
}

fmt.Println("==================")
}

// setupExportTreeBasic sets up a basic tree with a handful of
// create/update/delete operations over a few versions.
func setupExportTreeBasic(t require.TestingT) *ImmutableTree {
Expand Down Expand Up @@ -172,6 +192,11 @@ func TestExporter(t *testing.T) {
}

assert.Equal(t, expect, actual)
// fmt.Println(tree.RenderShape(" ", defaultNodeEncoder))

// for _, i := range actual {
// fmt.Printf("Key: %s, Value: %d, Version: %d, Height: %d \n", i.Key, i.Value, i.Version, i.Height)
// }
}

func TestExporter_Import(t *testing.T) {
Expand Down
18 changes: 9 additions & 9 deletions immutable_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,15 @@ func (t *ImmutableTree) renderNode(node *Node, indent string, depth int, encoder
// recurse on inner node
here := fmt.Sprintf("%s%s", prefix, encoder(node.hash, depth, false))

rightNode, err := node.getRightNode(t)
if err != nil {
return nil, err
}

leftNode, err := node.getLeftNode(t)
if err != nil {
return nil, err
}
rightNode := node.rightNode
// if err != nil {
// return nil, err
// }

leftNode := node.leftNode
// if err != nil {
// return nil, err
// }

right, err := t.renderNode(rightNode, indent, depth+1, encoder)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions import.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (i *Importer) Add(exportNode *ExportNode) error {
bytesCopy := make([]byte, buf.Len())
copy(bytesCopy, buf.Bytes())

if err = i.batch.Set(i.tree.ndb.nodeKey(node.hash), bytesCopy); err != nil {
if err = i.batch.Set(node.GetKey(), bytesCopy); err != nil {
return err
}

Expand Down Expand Up @@ -175,7 +175,7 @@ func (i *Importer) Commit() error {
return err
}
case 1:
if err := i.batch.Set(i.tree.ndb.rootKey(i.version), i.stack[0].hash); err != nil {
if err := i.batch.Set(i.tree.ndb.rootKey(i.version), RootRecord(i.stack[0].hash, i.stack[0].nodeKey)); err != nil {
return err
}
default:
Expand Down
6 changes: 6 additions & 0 deletions keyformat/key_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,19 @@ func format(a interface{}) []byte {
return formatUint64(uint64(v))
case int:
return formatUint64(uint64(v))
case uint8:
return formatUint8(v)
case []byte:
return v
default:
panic(fmt.Errorf("keyFormat format() does not support formatting value of type %T: %v", a, a))
}
}

func formatUint8(v uint8) []byte {
return []byte{v}
}

func formatUint64(v uint64) []byte {
bs := make([]byte, 8)
binary.BigEndian.PutUint64(bs, v)
Expand Down
Loading