Skip to content

Commit

Permalink
add MutableTree.SetInitialVersion() (#312)
Browse files Browse the repository at this point in the history
Requested in cosmos/cosmos-sdk#7089 (comment).

I didn't add a `GetInitialVersion()`, since it's not clear whether this should return only the option or the _actual_ initial version that exists in a tree.

It might be cleaner to add `GetOptions()`, `SetOptions()`, and `UpdateOptions(func (o Options) Options)`, but we may not want all options to be mutable after construction.

CC @amaurymartiny
  • Loading branch information
erikgrinaker committed Sep 25, 2020
1 parent fe4c390 commit b0666d6
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 6 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

### Improvements

- Added `Options.InitialVersion` to specify the initial version to start new IAVL trees from.
- [\#299](https://github.com/cosmos/iavl/pull/299) Added `Options.InitialVersion` to specify the
initial version for new IAVL trees.

- [\#312](https://github.com/cosmos/iavl/pull/312) Added `MutableTree.SetInitialVersion()` to
set the initial version after tree initialization.

## 0.14.0 (July 2, 2020)

Expand Down
7 changes: 7 additions & 0 deletions mutable_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,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.
Expand Down
12 changes: 12 additions & 0 deletions mutable_tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 := db.NewDB("test", db.MemDBBackend, "")
t, err := NewMutableTree(db, 100000)
Expand Down
7 changes: 4 additions & 3 deletions nodedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ type Options struct {
}

// DefaultOptions returns the default options for IAVL.
func DefaultOptions() *Options {
return &Options{}
func DefaultOptions() Options {
return Options{}
}

0 comments on commit b0666d6

Please sign in to comment.