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

feat: add the tree Close api (backport #905) #912

Merged
merged 1 commit into from
Mar 13, 2024
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
10 changes: 10 additions & 0 deletions mutable_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -1105,3 +1105,13 @@ func (tree *MutableTree) SaveChangeSet(cs *ChangeSet) (int64, error) {
_, version, err := tree.SaveVersion()
return version, err
}

// Close closes the tree.
func (tree *MutableTree) Close() error {
tree.mtx.Lock()
defer tree.mtx.Unlock()

tree.ImmutableTree = nil
tree.lastSaved = nil
return tree.ndb.Close()
}
13 changes: 13 additions & 0 deletions mutable_tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1445,3 +1445,16 @@ func TestMutableTree_InitialVersion_FirstVersion(t *testing.T) {
require.NoError(t, err)
require.Equal(t, initialVersion+1, node.nodeKey.version)
}

func TestMutableTreeClose(t *testing.T) {
db := dbm.NewMemDB()
tree := NewMutableTree(db, 0, true, log.NewNopLogger())

_, err := tree.Set([]byte("hello"), []byte("world"))
require.NoError(t, err)

_, _, err = tree.SaveVersion()
require.NoError(t, err)

require.NoError(t, tree.Close())
}
16 changes: 16 additions & 0 deletions nodedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,22 @@ func (ndb *nodeDB) traverseOrphans(prevVersion, curVersion int64, fn func(*Node)
return nil
}

// Close the nodeDB.
func (ndb *nodeDB) Close() error {
ndb.mtx.Lock()
defer ndb.mtx.Unlock()

if ndb.batch != nil {
if err := ndb.batch.Close(); err != nil {
return err
}
ndb.batch = nil
}

// skip the db.Close() since it can be used by other trees
return nil
}

// Utility and test functions

func (ndb *nodeDB) leafNodes() ([]*Node, error) {
Expand Down
Loading