diff --git a/CHANGELOG.md b/CHANGELOG.md index 13956f6a5..d220fe637 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ - [\#296](https://github.com/cosmos/iavl/pull/296) Add `iavlserver`, a gRPC/REST API server. +- [\#312](https://github.com/cosmos/iavl/pull/312) Add `MutableTree.SetInitialVersion()` to set the + initial version after tree initialization. + ### Bug Fixes - [\#309](https://github.com/cosmos/iavl/pull/309) Fix `SaveVersion()` for old, empty versions as diff --git a/mutable_tree.go b/mutable_tree.go index c9f0db934..bc69cb150 100644 --- a/mutable_tree.go +++ b/mutable_tree.go @@ -527,6 +527,13 @@ func (tree *MutableTree) deleteVersion(version int64) error { return nil } +// SetInitialVersion sets the initial version of the tree, replacing Options.InitialVersion. +// It is only used during the initial SaveVersion() call for a tree with no other versions, +// and is otherwise ignored. +func (tree *MutableTree) SetInitialVersion(version uint64) { + tree.ndb.opts.InitialVersion = version +} + // DeleteVersions deletes a series of versions from the MutableTree. An error // is returned if any single version is invalid or the delete fails. All writes // happen in a single batch with a single commit. diff --git a/mutable_tree_test.go b/mutable_tree_test.go index 1c2c4db8b..d3214d874 100644 --- a/mutable_tree_test.go +++ b/mutable_tree_test.go @@ -147,6 +147,18 @@ func TestMutableTree_InitialVersion(t *testing.T) { assert.EqualValues(t, 11, version) } +func TestMutableTree_SetInitialVersion(t *testing.T) { + memDB := db.NewMemDB() + tree, err := NewMutableTree(memDB, 0) + require.NoError(t, err) + tree.SetInitialVersion(9) + + tree.Set([]byte("a"), []byte{0x01}) + _, version, err := tree.SaveVersion() + require.NoError(t, err) + assert.EqualValues(t, 9, version) +} + func BenchmarkMutableTree_Set(b *testing.B) { db, err := db.NewDB("test", db.MemDBBackend, "") require.NoError(b, err) diff --git a/nodedb.go b/nodedb.go index 621d1f515..720ee7afe 100644 --- a/nodedb.go +++ b/nodedb.go @@ -38,7 +38,7 @@ type nodeDB struct { mtx sync.Mutex // Read/write lock. db dbm.DB // Persistent node storage. batch dbm.Batch // Batched writing buffer. - opts *Options // Options to customize for pruning/writing + opts Options // Options to customize for pruning/writing versionReaders map[int64]uint32 // Number of active version readers latestVersion int64 @@ -49,12 +49,13 @@ type nodeDB struct { func newNodeDB(db dbm.DB, cacheSize int, opts *Options) *nodeDB { if opts == nil { - opts = DefaultOptions() + o := DefaultOptions() + opts = &o } return &nodeDB{ db: db, batch: db.NewBatch(), - opts: opts, + opts: *opts, latestVersion: 0, // initially invalid nodeCache: make(map[string]*list.Element), nodeCacheSize: cacheSize, diff --git a/options.go b/options.go index 61c54740d..ba9410213 100644 --- a/options.go +++ b/options.go @@ -13,6 +13,6 @@ type Options struct { } // DefaultOptions returns the default options for IAVL. -func DefaultOptions() *Options { - return &Options{} +func DefaultOptions() Options { + return Options{} }