Skip to content

Commit

Permalink
Dispose of latestVersion on *VersionedTree
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudhead committed Dec 8, 2017
1 parent 7358fc6 commit 4e42312
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 17 deletions.
2 changes: 0 additions & 2 deletions orphaning_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ func (tree *orphaningTree) unorphan(hash []byte) {
// SaveAs saves the underlying Tree and assigns it a new version.
// Saves orphans too.
func (tree *orphaningTree) SaveAs(version int64) {
tree.version = version

if tree.root == nil {
// There can still be orphans, for example if the root is the node being
// removed.
Expand Down
1 change: 0 additions & 1 deletion tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,6 @@ func (tree *Tree) load(root []byte) {
return
}
tree.root = tree.ndb.GetNode(root)
tree.version = tree.root.version
}

// nodeSize is like Size, but includes inner nodes too.
Expand Down
4 changes: 3 additions & 1 deletion tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ func TestVersionedTree(t *testing.T) {
require.NoError(tree.Load())

require.Len(tree.versions, 2, "wrong number of versions")
require.EqualValues(v2, tree.LatestVersion())

// -----1-----
// key1 = val0 <orphaned>
Expand All @@ -321,7 +322,8 @@ func TestVersionedTree(t *testing.T) {
tree.Remove([]byte("key1"))
tree.Set([]byte("key2"), []byte("val2"))

hash3, _, _ := tree.SaveVersion()
hash3, v3, _ := tree.SaveVersion()
require.EqualValues(3, v3)

// -----1-----
// key1 = val0 <orphaned> (replaced)
Expand Down
28 changes: 15 additions & 13 deletions versioned_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ var ErrVersionDoesNotExist = fmt.Errorf("version does not exist")
type VersionedTree struct {
*orphaningTree // The current, working tree.
versions map[int64]*Tree // The previous, saved versions of the tree.
latestVersion int64 // The latest saved version.
ndb *nodeDB
}

Expand All @@ -31,7 +30,7 @@ func NewVersionedTree(cacheSize int, db dbm.DB) *VersionedTree {

// LatestVersion returns the latest saved version of the tree.
func (tree *VersionedTree) LatestVersion() int64 {
return tree.latestVersion
return tree.orphaningTree.version

This comment has been minimized.

Copy link
@jaekwon

jaekwon Dec 8, 2017

Contributor

i don't think this is right, the currently loaded orphaningTree isn't always the latest saved version.

This comment has been minimized.

Copy link
@jaekwon

jaekwon Dec 8, 2017

Contributor

Or the comments + name of this function needs to change.

}

// IsEmpty returns whether or not the tree has any keys. Only trees that are
Expand All @@ -54,8 +53,8 @@ func (tree *VersionedTree) Tree() *Tree {
// Hash returns the hash of the latest saved version of the tree, as returned
// by SaveVersion. If no versions have been saved, Hash returns nil.
func (tree *VersionedTree) Hash() []byte {
if tree.latestVersion > 0 {
return tree.versions[tree.latestVersion].Hash()
if tree.version > 0 {

This comment has been minimized.

Copy link
@jaekwon

jaekwon Dec 8, 2017

Contributor

ditto

return tree.versions[tree.version].Hash()
}
return nil
}
Expand Down Expand Up @@ -98,7 +97,7 @@ func (tree *VersionedTree) LoadVersion(version int64) error {

tree.versions[v] = t
}
tree.latestVersion = version
tree.version = version
tree.ResetToLatest()

return nil
Expand All @@ -115,28 +114,31 @@ func (tree *VersionedTree) Load() error {
}

// Load all roots from the database.
latestVersion := int64(0)
for version, root := range roots {
t := &Tree{ndb: tree.ndb, version: version}
t.load(root)

tree.versions[version] = t

if version > tree.latestVersion {
tree.latestVersion = version
if version > latestVersion {
latestVersion = version
}
}
// Set the working tree to a copy of the latest.
tree.ResetToLatest()
tree.orphaningTree = newOrphaningTree(
tree.versions[latestVersion].clone(),
)

return nil
}

// ResetToLatest resets the working tree to the latest saved version, discarding
// any unsaved modifications.
func (tree *VersionedTree) ResetToLatest() {
if tree.latestVersion > 0 {
if tree.version > 0 {
tree.orphaningTree = newOrphaningTree(
tree.versions[tree.latestVersion].clone(),
tree.versions[tree.version].clone(),
)
} else {
tree.orphaningTree = newOrphaningTree(&Tree{ndb: tree.ndb, version: 0})
Expand All @@ -156,13 +158,13 @@ func (tree *VersionedTree) GetVersioned(key []byte, version int64) (
// SaveVersion saves a new tree version to disk, based on the current state of
// the tree. Returns the hash and new version number.
func (tree *VersionedTree) SaveVersion() ([]byte, int64, error) {
version := tree.latestVersion + 1
version := tree.version + 1

if _, ok := tree.versions[version]; ok {
return nil, version, errors.Errorf("version %d was already saved", version)
}

tree.latestVersion = version
tree.version = version
tree.versions[version] = tree.orphaningTree.Tree

tree.orphaningTree.SaveAs(version)
Expand All @@ -182,7 +184,7 @@ func (tree *VersionedTree) DeleteVersion(version int64) error {
if version == 0 {
return errors.New("version must be greater than 0")
}
if version == tree.latestVersion {
if version == tree.version {
return errors.Errorf("cannot delete latest saved version (%d)", version)
}
if _, ok := tree.versions[version]; !ok {
Expand Down

0 comments on commit 4e42312

Please sign in to comment.