diff --git a/CHANGELOG.md b/CHANGELOG.md index 45fb4c725..2a375bae8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ ### Improvements +- [#695](https://github.com/cosmos/iavl/pull/695) Add API `SaveChangeSet` to save the changeset as a new version. - [#703](https://github.com/cosmos/iavl/pull/703) New APIs `NewCompressExporter`/`NewCompressImporter` to support more compact snapshot format. - [#729](https://github.com/cosmos/iavl/pull/729) Speedup Genesis writes for IAVL, by writing in small batches. - [#726](https://github.com/cosmos/iavl/pull/726) Make `KVPair` and `ChangeSet` serializable with protobuf. @@ -19,12 +20,17 @@ - [#735](https://github.com/cosmos/iavl/pull/735) Pass logger to `NodeDB`, `MutableTree` and `ImmutableTree` - [#646](https://github.com/cosmos/iavl/pull/646) Remove the `orphans` from the storage +<<<<<<< HEAD - [#777](https://github.com/cosmos/iavl/pull/777) Don't return errors from ImmutableTree.Hash, NewImmutableTree, NewImmutableTreeWIthOpts ### API Changes +======= +- [#777](https://github.com/cosmos/iavl/pull/777) Don't return errors from ImmutableTree.Hash, NewImmutableTree +- [#815](https://github.com/cosmos/iavl/pull/815) `NewMutableTreeWithOpts` was removed in favour of accepting options via a variadic in `NewMutableTree` +- [#815](https://github.com/cosmos/iavl/pull/815) `NewImmutableTreeWithOpts` is removed in favour of accepting options via a variadic in `NewImmutableTree` +>>>>>>> 0420895 (refactor: make batcher configurable (#815)) - [#646](https://github.com/cosmos/iavl/pull/646) Remove the `DeleteVersion`, `DeleteVersions`, `DeleteVersionsRange` and introduce a new endpoint of `DeleteVersionsTo` instead -- [#695](https://github.com/cosmos/iavl/pull/695) Add API `SaveChangeSet` to save the changeset as a new version. ## 0.20.0 (March 14, 2023) diff --git a/batch.go b/batch.go index f91b36f83..a634c1f0b 100644 --- a/batch.go +++ b/batch.go @@ -10,18 +10,18 @@ import ( type BatchWithFlusher struct { db dbm.DB // This is only used to create new batch batch dbm.Batch // Batched writing buffer. + + flushThreshold int // The threshold to flush the batch to disk. } var _ dbm.Batch = &BatchWithFlusher{} -// Ethereum has found that commit of 100KB is optimal, ref ethereum/go-ethereum#15115 -var flushThreshold = 100000 - // NewBatchWithFlusher returns new BatchWithFlusher wrapping the passed in batch -func NewBatchWithFlusher(db dbm.DB) *BatchWithFlusher { +func NewBatchWithFlusher(db dbm.DB, flushThreshold int) *BatchWithFlusher { return &BatchWithFlusher{ - db: db, - batch: db.NewBatchWithSize(flushThreshold), + db: db, + batch: db.NewBatchWithSize(flushThreshold), + flushThreshold: flushThreshold, } } @@ -50,7 +50,7 @@ func (b *BatchWithFlusher) Set(key []byte, value []byte) error { if err != nil { return err } - if batchSizeAfter > flushThreshold { + if batchSizeAfter > b.flushThreshold { err = b.batch.Write() if err != nil { return err @@ -59,7 +59,7 @@ func (b *BatchWithFlusher) Set(key []byte, value []byte) error { if err != nil { return err } - b.batch = b.db.NewBatchWithSize(flushThreshold) + b.batch = b.db.NewBatchWithSize(b.flushThreshold) } err = b.batch.Set(key, value) if err != nil { @@ -77,7 +77,7 @@ func (b *BatchWithFlusher) Delete(key []byte) error { if err != nil { return err } - if batchSizeAfter > flushThreshold { + if batchSizeAfter > b.flushThreshold { err = b.batch.Write() if err != nil { return err @@ -86,7 +86,7 @@ func (b *BatchWithFlusher) Delete(key []byte) error { if err != nil { return err } - b.batch = b.db.NewBatchWithSize(flushThreshold) + b.batch = b.db.NewBatchWithSize(b.flushThreshold) } err = b.batch.Delete(key) if err != nil { diff --git a/batch_test.go b/batch_test.go index 507c1055e..54ed0f2a2 100644 --- a/batch_test.go +++ b/batch_test.go @@ -43,7 +43,7 @@ func testBatchWithFlusher(t *testing.T, backend dbm.BackendType) { require.NoError(t, err) defer cleanupDBDir(dir, name) - batchWithFlusher := NewBatchWithFlusher(db) + batchWithFlusher := NewBatchWithFlusher(db, DefaultOptions().FlushThreshold) // we'll try to to commit 10MBs (1000 * 10KBs each entries) of data into the db for keyNonce := uint16(0); keyNonce < 1000; keyNonce++ { diff --git a/benchmarks/bench_test.go b/benchmarks/bench_test.go index df0557ece..595da1c4f 100644 --- a/benchmarks/bench_test.go +++ b/benchmarks/bench_test.go @@ -27,7 +27,7 @@ func randBytes(length int) []byte { } func prepareTree(b *testing.B, db db.DB, size, keyLen, dataLen int) (*iavl.MutableTree, [][]byte) { - t := iavl.NewMutableTreeWithOpts(db, size, nil, false, log.NewNopLogger()) + t := iavl.NewMutableTree(db, size, false, log.NewNopLogger()) keys := make([][]byte, size) for i := 0; i < size; i++ { diff --git a/immutable_tree.go b/immutable_tree.go index 930e7ea9d..dc8f4a508 100644 --- a/immutable_tree.go +++ b/immutable_tree.go @@ -24,17 +24,17 @@ type ImmutableTree struct { } // NewImmutableTree creates both in-memory and persistent instances -func NewImmutableTree(db dbm.DB, cacheSize int, skipFastStorageUpgrade bool, lg log.Logger) *ImmutableTree { +func NewImmutableTree(db dbm.DB, cacheSize int, skipFastStorageUpgrade bool, lg log.Logger, options ...Option) *ImmutableTree { + opts := DefaultOptions() + for _, opt := range options { + opt(&opts) + } + if db == nil { // In-memory Tree. return &ImmutableTree{} } - return NewImmutableTreeWithOpts(db, cacheSize, nil, skipFastStorageUpgrade, lg) -} - -// NewImmutableTreeWithOpts creates an ImmutableTree with the given options. -func NewImmutableTreeWithOpts(db dbm.DB, cacheSize int, opts *Options, skipFastStorageUpgrade bool, lg log.Logger) *ImmutableTree { return &ImmutableTree{ logger: lg, // NodeDB-backed Tree. diff --git a/iterator_test.go b/iterator_test.go index f7a7ed0f4..9fa14327b 100644 --- a/iterator_test.go +++ b/iterator_test.go @@ -363,11 +363,11 @@ func TestNodeIterator_Success(t *testing.T) { } func TestNodeIterator_WithEmptyRoot(t *testing.T) { - itr, err := NewNodeIterator(nil, newNodeDB(dbm.NewMemDB(), 0, nil, log.NewNopLogger())) + itr, err := NewNodeIterator(nil, newNodeDB(dbm.NewMemDB(), 0, DefaultOptions(), log.NewNopLogger())) require.NoError(t, err) require.False(t, itr.Valid()) - itr, err = NewNodeIterator([]byte{}, newNodeDB(dbm.NewMemDB(), 0, nil, log.NewNopLogger())) + itr, err = NewNodeIterator([]byte{}, newNodeDB(dbm.NewMemDB(), 0, DefaultOptions(), log.NewNopLogger())) require.NoError(t, err) require.False(t, itr.Valid()) } diff --git a/mutable_tree.go b/mutable_tree.go index 49f81070c..f3a88deec 100644 --- a/mutable_tree.go +++ b/mutable_tree.go @@ -14,14 +14,18 @@ import ( ibytes "github.com/cosmos/iavl/internal/bytes" ) -// commitGap after upgrade/delete commitGap FastNodes when commit the batch -var commitGap uint64 = 5000000 +var ( + // commitGap after upgrade/delete commitGap FastNodes when commit the batch + commitGap uint64 = 5000000 -// ErrVersionDoesNotExist is returned if a requested version does not exist. -var ErrVersionDoesNotExist = errors.New("version does not exist") + // ErrVersionDoesNotExist is returned if a requested version does not exist. + ErrVersionDoesNotExist = errors.New("version does not exist") -// ErrKeyDoesNotExist is returned if a key does not exist. -var ErrKeyDoesNotExist = errors.New("key does not exist") + // ErrKeyDoesNotExist is returned if a key does not exist. + ErrKeyDoesNotExist = errors.New("key does not exist") +) + +type Option func(*Options) // MutableTree is a persistent tree which keeps track of versions. It is not safe for concurrent // use, and should be guarded by a Mutex or RWLock as appropriate. An immutable tree at a given @@ -44,13 +48,13 @@ type MutableTree struct { mtx sync.Mutex } -// NewMutableTree returns a new tree with the specified cache size and datastore. -func NewMutableTree(db dbm.DB, cacheSize int, skipFastStorageUpgrade bool, lg log.Logger) *MutableTree { - return NewMutableTreeWithOpts(db, cacheSize, nil, skipFastStorageUpgrade, lg) -} +// NewMutableTree returns a new tree with the specified optional options. +func NewMutableTree(db dbm.DB, cacheSize int, skipFastStorageUpgrade bool, lg log.Logger, options ...Option) *MutableTree { + opts := DefaultOptions() + for _, opt := range options { + opt(&opts) + } -// NewMutableTreeWithOpts returns a new tree with the specified options. -func NewMutableTreeWithOpts(db dbm.DB, cacheSize int, opts *Options, skipFastStorageUpgrade bool, lg log.Logger) *MutableTree { ndb := newNodeDB(db, cacheSize, opts, lg) head := &ImmutableTree{ndb: ndb, skipFastStorageUpgrade: skipFastStorageUpgrade} diff --git a/mutable_tree_test.go b/mutable_tree_test.go index 184979adc..824c2985b 100644 --- a/mutable_tree_test.go +++ b/mutable_tree_test.go @@ -84,6 +84,7 @@ func TestIteratorConcurrency(t *testing.T) { } itr, _ := tree.Iterator(nil, nil, true) for ; itr.Valid(); itr.Next() { + // do nothing } } wg.Wait() @@ -107,6 +108,7 @@ func TestNewIteratorConcurrency(t *testing.T) { }(i, j) } for ; it.Valid(); it.Next() { + // do nothing } wg.Wait() } @@ -257,7 +259,7 @@ func TestMutableTree_LoadVersion_Empty(t *testing.T) { func TestMutableTree_InitialVersion(t *testing.T) { memDB := db.NewMemDB() - tree := NewMutableTreeWithOpts(memDB, 0, &Options{InitialVersion: 9}, false, log.NewNopLogger()) + tree := NewMutableTree(memDB, 0, false, log.NewNopLogger(), InitialVersionOption(9)) _, err := tree.Set([]byte("a"), []byte{0x01}) require.NoError(t, err) @@ -272,18 +274,18 @@ func TestMutableTree_InitialVersion(t *testing.T) { assert.EqualValues(t, 10, version) // Reloading the tree with the same initial version is fine - tree = NewMutableTreeWithOpts(memDB, 0, &Options{InitialVersion: 9}, false, log.NewNopLogger()) + tree = NewMutableTree(memDB, 0, false, log.NewNopLogger(), InitialVersionOption(9)) version, err = tree.Load() require.NoError(t, err) assert.EqualValues(t, 10, version) // Reloading the tree with an initial version beyond the lowest should error - tree = NewMutableTreeWithOpts(memDB, 0, &Options{InitialVersion: 10}, false, log.NewNopLogger()) + tree = NewMutableTree(memDB, 0, false, log.NewNopLogger(), InitialVersionOption(10)) _, err = tree.Load() require.Error(t, err) // Reloading the tree with a lower initial version is fine, and new versions can be produced - tree = NewMutableTreeWithOpts(memDB, 0, &Options{InitialVersion: 3}, false, log.NewNopLogger()) + tree = NewMutableTree(memDB, 0, false, log.NewNopLogger(), InitialVersionOption(3)) version, err = tree.Load() require.NoError(t, err) assert.EqualValues(t, 10, version) @@ -1418,9 +1420,7 @@ func TestMutableTree_InitialVersion_FirstVersion(t *testing.T) { db := db.NewMemDB() initialVersion := int64(1000) - tree := NewMutableTreeWithOpts(db, 0, &Options{ - InitialVersion: uint64(initialVersion), - }, true, log.NewNopLogger()) + tree := NewMutableTree(db, 0, true, log.NewNopLogger(), InitialVersionOption(uint64(initialVersion))) _, err := tree.Set([]byte("hello"), []byte("world")) require.NoError(t, err) diff --git a/nodedb.go b/nodedb.go index b57da76d3..cf074db1a 100644 --- a/nodedb.go +++ b/nodedb.go @@ -85,12 +85,7 @@ type nodeDB struct { fastNodeCache cache.Cache // Cache for nodes in the fast index that represents only key-value pairs at the latest version. } -func newNodeDB(db dbm.DB, cacheSize int, opts *Options, lg log.Logger) *nodeDB { - if opts == nil { - o := DefaultOptions() - opts = &o - } - +func newNodeDB(db dbm.DB, cacheSize int, opts Options, lg log.Logger) *nodeDB { storeVersion, err := db.Get(metadataKeyFormat.Key([]byte(storageVersionKey))) if err != nil || storeVersion == nil { @@ -100,8 +95,8 @@ func newNodeDB(db dbm.DB, cacheSize int, opts *Options, lg log.Logger) *nodeDB { return &nodeDB{ logger: lg, db: db, - batch: NewBatchWithFlusher(db), - opts: *opts, + batch: NewBatchWithFlusher(db, opts.FlushThreshold), + opts: opts, firstVersion: 0, latestVersion: 0, // initially invalid legacyLatestVersion: 0, @@ -382,7 +377,7 @@ func (ndb *nodeDB) writeBatch() error { return err } - ndb.batch = NewBatchWithFlusher(ndb.db) + ndb.batch = NewBatchWithFlusher(ndb.db, ndb.opts.FlushThreshold) return nil } @@ -877,7 +872,7 @@ func (ndb *nodeDB) Commit() error { } ndb.batch.Close() - ndb.batch = NewBatchWithFlusher(ndb.db) + ndb.batch = NewBatchWithFlusher(ndb.db, ndb.opts.FlushThreshold) return nil } diff --git a/nodedb_test.go b/nodedb_test.go index b22471eb1..bc36ce3ec 100644 --- a/nodedb_test.go +++ b/nodedb_test.go @@ -50,7 +50,7 @@ func TestNewNoDbStorage_StorageVersionInDb_Success(t *testing.T) { dbMock.EXPECT().Get(gomock.Any()).Return([]byte(expectedVersion), nil).Times(1) dbMock.EXPECT().NewBatchWithSize(gomock.Any()).Return(nil).Times(1) - ndb := newNodeDB(dbMock, 0, nil, log.NewNopLogger()) + ndb := newNodeDB(dbMock, 0, DefaultOptions(), log.NewNopLogger()) require.Equal(t, expectedVersion, ndb.storageVersion) } @@ -62,7 +62,7 @@ func TestNewNoDbStorage_ErrorInConstructor_DefaultSet(t *testing.T) { dbMock.EXPECT().Get(gomock.Any()).Return(nil, errors.New("some db error")).Times(1) dbMock.EXPECT().NewBatchWithSize(gomock.Any()).Return(nil).Times(1) - ndb := newNodeDB(dbMock, 0, nil, log.NewNopLogger()) + ndb := newNodeDB(dbMock, 0, DefaultOptions(), log.NewNopLogger()) require.Equal(t, expectedVersion, ndb.getStorageVersion()) } @@ -75,7 +75,7 @@ func TestNewNoDbStorage_DoesNotExist_DefaultSet(t *testing.T) { dbMock.EXPECT().Get(gomock.Any()).Return(nil, nil).Times(1) dbMock.EXPECT().NewBatchWithSize(gomock.Any()).Return(nil).Times(1) - ndb := newNodeDB(dbMock, 0, nil, log.NewNopLogger()) + ndb := newNodeDB(dbMock, 0, DefaultOptions(), log.NewNopLogger()) require.Equal(t, expectedVersion, ndb.getStorageVersion()) } @@ -84,7 +84,7 @@ func TestSetStorageVersion_Success(t *testing.T) { db := db.NewMemDB() - ndb := newNodeDB(db, 0, nil, log.NewNopLogger()) + ndb := newNodeDB(db, 0, DefaultOptions(), log.NewNopLogger()) require.Equal(t, defaultStorageVersionValue, ndb.getStorageVersion()) err := ndb.setFastStorageVersionToBatch() @@ -118,7 +118,7 @@ func TestSetStorageVersion_DBFailure_OldKept(t *testing.T) { batchMock.EXPECT().GetByteSize().Return(100, nil).Times(1) batchMock.EXPECT().Set(metadataKeyFormat.Key([]byte(storageVersionKey)), []byte(fastStorageVersionValue+fastStorageVersionDelimiter+strconv.Itoa(expectedFastCacheVersion))).Return(errors.New(expectedErrorMsg)).Times(1) - ndb := newNodeDB(dbMock, 0, nil, log.NewNopLogger()) + ndb := newNodeDB(dbMock, 0, DefaultOptions(), log.NewNopLogger()) require.Equal(t, defaultStorageVersionValue, ndb.getStorageVersion()) err := ndb.setFastStorageVersionToBatch() @@ -139,7 +139,7 @@ func TestSetStorageVersion_InvalidVersionFailure_OldKept(t *testing.T) { dbMock.EXPECT().Get(gomock.Any()).Return([]byte(invalidStorageVersion), nil).Times(1) dbMock.EXPECT().NewBatchWithSize(gomock.Any()).Return(batchMock).Times(1) - ndb := newNodeDB(dbMock, 0, nil, log.NewNopLogger()) + ndb := newNodeDB(dbMock, 0, DefaultOptions(), log.NewNopLogger()) require.Equal(t, invalidStorageVersion, ndb.getStorageVersion()) err := ndb.setFastStorageVersionToBatch() @@ -150,7 +150,7 @@ func TestSetStorageVersion_InvalidVersionFailure_OldKept(t *testing.T) { func TestSetStorageVersion_FastVersionFirst_VersionAppended(t *testing.T) { db := db.NewMemDB() - ndb := newNodeDB(db, 0, nil, log.NewNopLogger()) + ndb := newNodeDB(db, 0, DefaultOptions(), log.NewNopLogger()) ndb.storageVersion = fastStorageVersionValue ndb.latestVersion = 100 @@ -161,7 +161,7 @@ func TestSetStorageVersion_FastVersionFirst_VersionAppended(t *testing.T) { func TestSetStorageVersion_FastVersionSecond_VersionAppended(t *testing.T) { db := db.NewMemDB() - ndb := newNodeDB(db, 0, nil, log.NewNopLogger()) + ndb := newNodeDB(db, 0, DefaultOptions(), log.NewNopLogger()) ndb.latestVersion = 100 storageVersionBytes := []byte(fastStorageVersionValue) @@ -175,7 +175,7 @@ func TestSetStorageVersion_FastVersionSecond_VersionAppended(t *testing.T) { func TestSetStorageVersion_SameVersionTwice(t *testing.T) { db := db.NewMemDB() - ndb := newNodeDB(db, 0, nil, log.NewNopLogger()) + ndb := newNodeDB(db, 0, DefaultOptions(), log.NewNopLogger()) ndb.latestVersion = 100 storageVersionBytes := []byte(fastStorageVersionValue) @@ -195,7 +195,7 @@ func TestSetStorageVersion_SameVersionTwice(t *testing.T) { // Test case where version is incorrect and has some extra garbage at the end func TestShouldForceFastStorageUpdate_DefaultVersion_True(t *testing.T) { db := db.NewMemDB() - ndb := newNodeDB(db, 0, nil, log.NewNopLogger()) + ndb := newNodeDB(db, 0, DefaultOptions(), log.NewNopLogger()) ndb.storageVersion = defaultStorageVersionValue ndb.latestVersion = 100 @@ -206,7 +206,7 @@ func TestShouldForceFastStorageUpdate_DefaultVersion_True(t *testing.T) { func TestShouldForceFastStorageUpdate_FastVersion_Greater_True(t *testing.T) { db := db.NewMemDB() - ndb := newNodeDB(db, 0, nil, log.NewNopLogger()) + ndb := newNodeDB(db, 0, DefaultOptions(), log.NewNopLogger()) ndb.latestVersion = 100 ndb.storageVersion = fastStorageVersionValue + fastStorageVersionDelimiter + strconv.Itoa(int(ndb.latestVersion+1)) @@ -217,7 +217,7 @@ func TestShouldForceFastStorageUpdate_FastVersion_Greater_True(t *testing.T) { func TestShouldForceFastStorageUpdate_FastVersion_Smaller_True(t *testing.T) { db := db.NewMemDB() - ndb := newNodeDB(db, 0, nil, log.NewNopLogger()) + ndb := newNodeDB(db, 0, DefaultOptions(), log.NewNopLogger()) ndb.latestVersion = 100 ndb.storageVersion = fastStorageVersionValue + fastStorageVersionDelimiter + strconv.Itoa(int(ndb.latestVersion-1)) @@ -228,7 +228,7 @@ func TestShouldForceFastStorageUpdate_FastVersion_Smaller_True(t *testing.T) { func TestShouldForceFastStorageUpdate_FastVersion_Match_False(t *testing.T) { db := db.NewMemDB() - ndb := newNodeDB(db, 0, nil, log.NewNopLogger()) + ndb := newNodeDB(db, 0, DefaultOptions(), log.NewNopLogger()) ndb.latestVersion = 100 ndb.storageVersion = fastStorageVersionValue + fastStorageVersionDelimiter + strconv.Itoa(int(ndb.latestVersion)) @@ -239,7 +239,7 @@ func TestShouldForceFastStorageUpdate_FastVersion_Match_False(t *testing.T) { func TestIsFastStorageEnabled_True(t *testing.T) { db := db.NewMemDB() - ndb := newNodeDB(db, 0, nil, log.NewNopLogger()) + ndb := newNodeDB(db, 0, DefaultOptions(), log.NewNopLogger()) ndb.latestVersion = 100 ndb.storageVersion = fastStorageVersionValue + fastStorageVersionDelimiter + strconv.Itoa(int(ndb.latestVersion)) @@ -248,7 +248,7 @@ func TestIsFastStorageEnabled_True(t *testing.T) { func TestIsFastStorageEnabled_False(t *testing.T) { db := db.NewMemDB() - ndb := newNodeDB(db, 0, nil, log.NewNopLogger()) + ndb := newNodeDB(db, 0, DefaultOptions(), log.NewNopLogger()) ndb.latestVersion = 100 ndb.storageVersion = defaultStorageVersionValue @@ -385,7 +385,7 @@ func TestNodeDB_traverseOrphans(t *testing.T) { func makeAndPopulateMutableTree(tb testing.TB) *MutableTree { memDB := db.NewMemDB() - tree := NewMutableTreeWithOpts(memDB, 0, &Options{InitialVersion: 9}, false, log.NewNopLogger()) + tree := NewMutableTree(memDB, 0, false, log.NewNopLogger(), InitialVersionOption(9)) for i := 0; i < 1e4; i++ { buf := make([]byte, 0, (i/255)+1) diff --git a/options.go b/options.go index 7d5d24653..c679cfca1 100644 --- a/options.go +++ b/options.go @@ -81,9 +81,40 @@ type Options struct { // When Stat is not nil, statistical logic needs to be executed Stat *Statistics + + // Ethereum has found that commit of 100KB is optimal, ref ethereum/go-ethereum#15115 + FlushThreshold int } // DefaultOptions returns the default options for IAVL. func DefaultOptions() Options { - return Options{} + return Options{FlushThreshold: 100000} +} + +// SyncOption sets the Sync option. +func SyncOption(sync bool) Option { + return func(opts *Options) { + opts.Sync = sync + } +} + +// InitialVersionOption sets the initial version for the tree. +func InitialVersionOption(iv uint64) Option { + return func(opts *Options) { + opts.InitialVersion = iv + } +} + +// StatOption sets the Statistics for the tree. +func StatOption(stats *Statistics) Option { + return func(opts *Options) { + opts.Stat = stats + } +} + +// FlushThresholdOption sets the FlushThreshold for the batcher. +func FlushThresholdOption(ft int) Option { + return func(opts *Options) { + opts.FlushThreshold = ft + } } diff --git a/proof_iavl_test.go b/proof_iavl_test.go index 722603c83..9327e18fe 100644 --- a/proof_iavl_test.go +++ b/proof_iavl_test.go @@ -10,7 +10,7 @@ import ( ) func TestProofOp(t *testing.T) { - tree := NewMutableTreeWithOpts(db.NewMemDB(), 0, nil, false, log.NewNopLogger()) + tree := NewMutableTree(db.NewMemDB(), 0, false, log.NewNopLogger()) keys := []byte{0x0a, 0x11, 0x2e, 0x32, 0x50, 0x72, 0x99, 0xa1, 0xe4, 0xf7} // 10 total. for _, ikey := range keys { key := []byte{ikey} diff --git a/testutils_test.go b/testutils_test.go index 3673ab0c8..19d31e513 100644 --- a/testutils_test.go +++ b/testutils_test.go @@ -43,7 +43,7 @@ func b2i(bz []byte) int { // Construct a MutableTree func getTestTree(cacheSize int) *MutableTree { - return NewMutableTreeWithOpts(db.NewMemDB(), cacheSize, nil, false, log.NewNopLogger()) + return NewMutableTree(db.NewMemDB(), cacheSize, false, log.NewNopLogger()) } // Convenience for a new node diff --git a/tree_random_test.go b/tree_random_test.go index 09c0e4278..3ff35c7f1 100644 --- a/tree_random_test.go +++ b/tree_random_test.go @@ -68,11 +68,11 @@ func testRandomOperations(t *testing.T, randSeed int64) { r := rand.New(rand.NewSource(randSeed)) // loadTree loads the last persisted version of a tree with random pruning settings. - loadTree := func(levelDB db.DB) (tree *MutableTree, version int64, options *Options) { //nolint:unparam + loadTree := func(levelDB db.DB) (tree *MutableTree, version int64, _ *Options) { //nolint:unparam var err error - options = &Options{ - Sync: r.Float64() < syncChance, - } + + sync := r.Float64() < syncChance + // set the cache size regardless of whether caching is enabled. This ensures we always // call the RNG the same number of times, such that changing settings does not affect // the RNG sequence. @@ -80,10 +80,10 @@ func testRandomOperations(t *testing.T, randSeed int64) { if !(r.Float64() < cacheChance) { cacheSize = 0 } - tree = NewMutableTreeWithOpts(levelDB, cacheSize, options, false, log.NewNopLogger()) + tree = NewMutableTree(levelDB, cacheSize, false, log.NewNopLogger(), SyncOption(sync)) version, err = tree.Load() require.NoError(t, err) - t.Logf("Loaded version %v (sync=%v cache=%v)", version, options.Sync, cacheSize) + t.Logf("Loaded version %v (sync=%v cache=%v)", version, sync, cacheSize) return } diff --git a/tree_test.go b/tree_test.go index 4c0b3ca7a..25771b321 100644 --- a/tree_test.go +++ b/tree_test.go @@ -1481,7 +1481,7 @@ func BenchmarkTreeLoadAndDelete(b *testing.B) { func TestLoadVersionForOverwritingCase2(t *testing.T) { require := require.New(t) - tree := NewMutableTreeWithOpts(db.NewMemDB(), 0, nil, false, log.NewNopLogger()) + tree := NewMutableTree(db.NewMemDB(), 0, false, log.NewNopLogger()) for i := byte(0); i < 20; i++ { tree.Set([]byte{i}, []byte{i}) @@ -1543,7 +1543,7 @@ func TestLoadVersionForOverwritingCase2(t *testing.T) { func TestLoadVersionForOverwritingCase3(t *testing.T) { require := require.New(t) - tree := NewMutableTreeWithOpts(db.NewMemDB(), 0, nil, false, log.NewNopLogger()) + tree := NewMutableTree(db.NewMemDB(), 0, false, log.NewNopLogger()) for i := byte(0); i < 20; i++ { tree.Set([]byte{i}, []byte{i}) @@ -1796,10 +1796,9 @@ func TestNodeCacheStatisic(t *testing.T) { tc := tc t.Run(name, func(sub *testing.T) { stat := &Statistics{} - opts := &Options{Stat: stat} db, err := db.NewDB("test", db.MemDBBackend, "") require.NoError(t, err) - mt := NewMutableTreeWithOpts(db, tc.cacheSize, opts, false, log.NewNopLogger()) + mt := NewMutableTree(db, tc.cacheSize, false, log.NewNopLogger(), StatOption(stat)) for i := 0; i < numKeyVals; i++ { key := []byte(strconv.Itoa(i)) @@ -1817,10 +1816,10 @@ func TestNodeCacheStatisic(t *testing.T) { require.NotNil(t, val) require.NotEmpty(t, val) } - require.Equal(t, tc.expectFastCacheHitCnt, int(opts.Stat.GetFastCacheHitCnt())) - require.Equal(t, tc.expectFastCacheMissCnt, int(opts.Stat.GetFastCacheMissCnt())) - require.Equal(t, tc.expectCacheHitCnt, int(opts.Stat.GetCacheHitCnt())) - require.Equal(t, tc.expectCacheMissCnt, int(opts.Stat.GetCacheMissCnt())) + require.Equal(t, tc.expectFastCacheHitCnt, int(stat.GetFastCacheHitCnt())) + require.Equal(t, tc.expectFastCacheMissCnt, int(stat.GetFastCacheMissCnt())) + require.Equal(t, tc.expectCacheHitCnt, int(stat.GetCacheHitCnt())) + require.Equal(t, tc.expectCacheMissCnt, int(stat.GetCacheMissCnt())) }) } }