Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add MutableTree.SetInitialVersion() #312

Merged
merged 2 commits into from
Aug 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions mutable_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
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, err := db.NewDB("test", db.MemDBBackend, "")
require.NoError(b, err)
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{}
}