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 #905

Merged
merged 2 commits 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 @@ -1103,3 +1103,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())
Comment on lines +1449 to +1459
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test TestMutableTreeClose correctly tests the basic functionality of the Close method by setting a key-value pair, saving the version, and then closing the tree. However, it could be enhanced by adding assertions to verify the state of the tree after closing. For instance, attempting further operations on the closed tree (like setting a new key-value pair or saving another version) should result in an error. This would ensure that the Close method not only completes without errors but also effectively prevents further operations on the tree, which is crucial for resource management.

Consider adding the following checks:

  • Verify that the internal state of the tree is as expected after closing (e.g., internal fields are set to nil).
  • Attempt to perform operations on the tree after closing it and assert that these operations result in an error.

This would provide a more comprehensive test coverage for the Close method, ensuring it behaves as expected in various scenarios.

}
16 changes: 16 additions & 0 deletions nodedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,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