Skip to content

Commit

Permalink
intern ExportNode
Browse files Browse the repository at this point in the history
  • Loading branch information
yihuang committed Sep 6, 2023
1 parent d953e1a commit 123aeb2
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 31 deletions.
12 changes: 5 additions & 7 deletions memiavl/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"errors"
"fmt"
"path/filepath"

"github.com/cosmos/iavl"
)

// ErrorExportDone is returned by Exporter.Next() when all items have been exported.
Expand Down Expand Up @@ -103,19 +101,19 @@ func (mte *MultiTreeExporter) Close() error {
return nil
}

type exportWorker func(callback func(*iavl.ExportNode) bool)
type exportWorker func(callback func(*ExportNode) bool)

type Exporter struct {
ch <-chan *iavl.ExportNode
ch <-chan *ExportNode
cancel context.CancelFunc
}

func newExporter(worker exportWorker) *Exporter {
ctx, cancel := context.WithCancel(context.Background())
ch := make(chan *iavl.ExportNode, exportBufferSize)
ch := make(chan *ExportNode, exportBufferSize)
go func() {
defer close(ch)
worker(func(enode *iavl.ExportNode) bool {
worker(func(enode *ExportNode) bool {
select {
case ch <- enode:
case <-ctx.Done():
Expand All @@ -127,7 +125,7 @@ func newExporter(worker exportWorker) *Exporter {
return &Exporter{ch, cancel}
}

func (e *Exporter) Next() (*iavl.ExportNode, error) {
func (e *Exporter) Next() (*ExportNode, error) {
if exportNode, ok := <-e.ch; ok {
return exportNode, nil
}
Expand Down
18 changes: 8 additions & 10 deletions memiavl/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"math"
"os"
"path/filepath"

"github.com/cosmos/iavl"
)

type MultiTreeImporter struct {
Expand Down Expand Up @@ -43,7 +41,7 @@ func (mti *MultiTreeImporter) tmpDir() string {

func (mti *MultiTreeImporter) Add(item interface{}) error {
switch item := item.(type) {
case *iavl.ExportNode:
case *ExportNode:
mti.AddNode(item)
return nil
case string:
Expand All @@ -63,7 +61,7 @@ func (mti *MultiTreeImporter) AddTree(name string) error {
return nil
}

func (mti *MultiTreeImporter) AddNode(node *iavl.ExportNode) {
func (mti *MultiTreeImporter) AddNode(node *ExportNode) {
mti.importer.Add(node)
}

Expand Down Expand Up @@ -97,12 +95,12 @@ func (mti *MultiTreeImporter) Close() error {

// TreeImporter import a single memiavl tree from state-sync snapshot
type TreeImporter struct {
nodesChan chan *iavl.ExportNode
nodesChan chan *ExportNode
quitChan chan error
}

func NewTreeImporter(dir string, version int64) *TreeImporter {
nodesChan := make(chan *iavl.ExportNode)
nodesChan := make(chan *ExportNode)
quitChan := make(chan error)
go func() {
defer close(quitChan)
Expand All @@ -111,7 +109,7 @@ func NewTreeImporter(dir string, version int64) *TreeImporter {
return &TreeImporter{nodesChan, quitChan}
}

func (ai *TreeImporter) Add(node *iavl.ExportNode) {
func (ai *TreeImporter) Add(node *ExportNode) {
ai.nodesChan <- node
}

Expand All @@ -127,8 +125,8 @@ func (ai *TreeImporter) Close() error {
return err
}

// doImport a stream of `iavl.ExportNode`s into a new snapshot.
func doImport(dir string, version int64, nodes <-chan *iavl.ExportNode) (returnErr error) {
// doImport a stream of `ExportNode`s into a new snapshot.
func doImport(dir string, version int64, nodes <-chan *ExportNode) (returnErr error) {
if version > int64(math.MaxUint32) {
return errors.New("version overflows uint32")
}
Expand Down Expand Up @@ -164,7 +162,7 @@ type importer struct {
nodeStack []*MemNode
}

func (i *importer) Add(n *iavl.ExportNode) error {
func (i *importer) Add(n *ExportNode) error {
if n.Version > int64(math.MaxUint32) {
return errors.New("version overflows uint32")
}
Expand Down
10 changes: 4 additions & 6 deletions memiavl/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"io"
"os"
"path/filepath"

"github.com/cosmos/iavl"
)

const (
Expand Down Expand Up @@ -298,14 +296,14 @@ func (snapshot *Snapshot) Export() *Exporter {
return newExporter(snapshot.export)
}

func (snapshot *Snapshot) export(callback func(*iavl.ExportNode) bool) {
func (snapshot *Snapshot) export(callback func(*ExportNode) bool) {
if snapshot.leavesLen() == 0 {
return
}

if snapshot.leavesLen() == 1 {
leaf := snapshot.Leaf(0)
callback(&iavl.ExportNode{
callback(&ExportNode{
Height: 0,
Version: int64(leaf.Version()),
Key: leaf.Key(),
Expand All @@ -323,7 +321,7 @@ func (snapshot *Snapshot) export(callback func(*iavl.ExportNode) bool) {
// add more leaf nodes
leaf := snapshot.leavesLayout.Leaf(j)
key, value := snapshot.KeyValue(leaf.KeyOffset())
enode := &iavl.ExportNode{
enode := &ExportNode{
Height: 0,
Version: int64(leaf.Version()),
Key: key,
Expand All @@ -336,7 +334,7 @@ func (snapshot *Snapshot) export(callback func(*iavl.ExportNode) bool) {
return
}
}
enode := &iavl.ExportNode{
enode := &ExportNode{
Height: int8(node.Height()),
Version: int64(node.Version()),
Key: snapshot.LeafKey(node.KeyLeaf()),
Expand Down
7 changes: 3 additions & 4 deletions memiavl/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package memiavl
import (
"testing"

"github.com/cosmos/iavl"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -48,7 +47,7 @@ func TestSnapshotEncodingRoundTrip(t *testing.T) {
}

func TestSnapshotExport(t *testing.T) {
expNodes := []*iavl.ExportNode{
expNodes := []*ExportNode{
{Key: []byte("hello"), Value: []byte("world1"), Version: 2, Height: 0},
{Key: []byte("hello1"), Value: []byte("world1"), Version: 2, Height: 0},
{Key: []byte("hello1"), Value: nil, Version: 3, Height: 1},
Expand All @@ -72,7 +71,7 @@ func TestSnapshotExport(t *testing.T) {
snapshot, err := OpenSnapshot(snapshotDir)
require.NoError(t, err)

var nodes []*iavl.ExportNode
var nodes []*ExportNode
exporter := snapshot.Export()
for {
node, err := exporter.Next()
Expand Down Expand Up @@ -100,7 +99,7 @@ func TestSnapshotImportExport(t *testing.T) {
snapshot, err := OpenSnapshot(snapshotDir)
require.NoError(t, err)

ch := make(chan *iavl.ExportNode)
ch := make(chan *ExportNode)

go func() {
defer close(ch)
Expand Down
4 changes: 2 additions & 2 deletions memiavl/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,9 @@ func (t *Tree) Export() *Exporter {
}

// do normal post-order traversal export
return newExporter(func(callback func(node *iavl.ExportNode) bool) {
return newExporter(func(callback func(node *ExportNode) bool) {
t.ScanPostOrder(func(node Node) bool {
return callback(&iavl.ExportNode{
return callback(&ExportNode{
Key: node.Key(),
Value: node.Value(),
Version: int64(node.Version()),
Expand Down
8 changes: 8 additions & 0 deletions memiavl/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,11 @@ func NewNopLogger() Logger { return &nopLogger{} }
func (nopLogger) Info(string, ...interface{}) {}
func (nopLogger) Debug(string, ...interface{}) {}
func (nopLogger) Error(string, ...interface{}) {}

// ExportNode contains exported node data.
type ExportNode struct {
Key []byte
Value []byte
Version int64
Height int8
}
3 changes: 1 addition & 2 deletions store/rootmulti/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
snapshottypes "github.com/cosmos/cosmos-sdk/snapshots/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
protoio "github.com/cosmos/gogoproto/io"
"github.com/cosmos/iavl"

"github.com/crypto-org-chain/cronos/memiavl"
)
Expand Down Expand Up @@ -52,7 +51,7 @@ loop:
return snapshottypes.SnapshotItem{}, errors.Wrapf(sdkerrors.ErrLogic, "node height %v cannot exceed %v",
item.IAVL.Height, math.MaxInt8)
}
node := &iavl.ExportNode{
node := &memiavl.ExportNode{
Key: item.IAVL.Key,
Value: item.IAVL.Value,
Height: int8(item.IAVL.Height),
Expand Down

0 comments on commit 123aeb2

Please sign in to comment.