From 123aeb2bb09599296edabebaf3e039d8b9ba6bb5 Mon Sep 17 00:00:00 2001 From: HuangYi Date: Wed, 6 Sep 2023 19:17:37 +0800 Subject: [PATCH] intern ExportNode --- memiavl/export.go | 12 +++++------- memiavl/import.go | 18 ++++++++---------- memiavl/snapshot.go | 10 ++++------ memiavl/snapshot_test.go | 7 +++---- memiavl/tree.go | 4 ++-- memiavl/types.go | 8 ++++++++ store/rootmulti/import.go | 3 +-- 7 files changed, 31 insertions(+), 31 deletions(-) diff --git a/memiavl/export.go b/memiavl/export.go index 3bfba318e1..3f088289d6 100644 --- a/memiavl/export.go +++ b/memiavl/export.go @@ -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. @@ -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(): @@ -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 } diff --git a/memiavl/import.go b/memiavl/import.go index f60c2cac46..c3e08b2f05 100644 --- a/memiavl/import.go +++ b/memiavl/import.go @@ -6,8 +6,6 @@ import ( "math" "os" "path/filepath" - - "github.com/cosmos/iavl" ) type MultiTreeImporter struct { @@ -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: @@ -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) } @@ -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) @@ -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 } @@ -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") } @@ -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") } diff --git a/memiavl/snapshot.go b/memiavl/snapshot.go index 6abf3abf96..12ca196496 100644 --- a/memiavl/snapshot.go +++ b/memiavl/snapshot.go @@ -8,8 +8,6 @@ import ( "io" "os" "path/filepath" - - "github.com/cosmos/iavl" ) const ( @@ -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(), @@ -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, @@ -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()), diff --git a/memiavl/snapshot_test.go b/memiavl/snapshot_test.go index a3629ba51e..6c1e4896af 100644 --- a/memiavl/snapshot_test.go +++ b/memiavl/snapshot_test.go @@ -3,7 +3,6 @@ package memiavl import ( "testing" - "github.com/cosmos/iavl" "github.com/stretchr/testify/require" ) @@ -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}, @@ -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() @@ -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) diff --git a/memiavl/tree.go b/memiavl/tree.go index 362223897c..c9123c5efe 100644 --- a/memiavl/tree.go +++ b/memiavl/tree.go @@ -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()), diff --git a/memiavl/types.go b/memiavl/types.go index 91806c3a18..aba20119df 100644 --- a/memiavl/types.go +++ b/memiavl/types.go @@ -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 +} diff --git a/store/rootmulti/import.go b/store/rootmulti/import.go index 8159a02769..e2c64ae3db 100644 --- a/store/rootmulti/import.go +++ b/store/rootmulti/import.go @@ -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" ) @@ -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),