Skip to content

Commit

Permalink
test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
erikgrinaker committed Jun 24, 2020
1 parent 33415bd commit 42b6ed1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
13 changes: 5 additions & 8 deletions mutable_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,22 +361,19 @@ func (tree *MutableTree) LoadVersion(targetVersion int64) (int64, error) {
}

// LoadVersionForOverwriting attempts to load a tree at a previously committed
// version. Any versions greater than targetVersion will be deleted.
// version, or the latest version below it. Any versions greater than targetVersion will be deleted.
func (tree *MutableTree) LoadVersionForOverwriting(targetVersion int64) (int64, error) {
latestVersion, err := tree.LoadVersion(targetVersion)
if err != nil {
return 0, err
}
if latestVersion != targetVersion {
return 0, errors.Errorf("version %v does not exist, found %v", targetVersion, latestVersion)
return latestVersion, err
}

if err = tree.ndb.DeleteVersionsFrom(targetVersion + 1); err != nil {
return 0, err
return latestVersion, err
}

if err = tree.ndb.Commit(); err != nil {
return 0, err
return latestVersion, err
}

tree.ndb.resetLatestVersion(latestVersion)
Expand All @@ -387,7 +384,7 @@ func (tree *MutableTree) LoadVersionForOverwriting(targetVersion int64) (int64,
}
}

return targetVersion, nil
return latestVersion, nil
}

// GetImmutable loads an ImmutableTree at a given version for querying. The returned tree is
Expand Down
4 changes: 2 additions & 2 deletions nodedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ func (ndb *nodeDB) DeleteVersionsFrom(version int64) error {
if err != nil {
return err
}
if len(root) == 0 {
return errors.New("root for version %v not found")
if root == nil {
return errors.Errorf("root for version %v not found", latest)
}

for v, r := range ndb.versionReaders {
Expand Down
17 changes: 11 additions & 6 deletions tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1438,17 +1438,20 @@ func TestLoadVersionForOverwritingCase2(t *testing.T) {
func TestLoadVersionForOverwritingCase3(t *testing.T) {
require := require.New(t)

tree, _ := NewMutableTreeWithOpts(db.NewMemDB(), 0, nil)
tree, err := NewMutableTreeWithOpts(db.NewMemDB(), 0, nil)
require.NoError(err)

for i := byte(0); i < 20; i++ {
tree.Set([]byte{i}, []byte{i})
}
tree.SaveVersion()
_, _, err = tree.SaveVersion()
require.NoError(err)

for i := byte(0); i < 20; i++ {
tree.Set([]byte{i}, []byte{i + 1})
}
tree.SaveVersion()
_, _, err = tree.SaveVersion()
require.NoError(err)

removedNodes := []*Node{}

Expand All @@ -1461,12 +1464,14 @@ func TestLoadVersionForOverwritingCase3(t *testing.T) {
for i := byte(0); i < 20; i++ {
tree.Remove([]byte{i})
}
tree.SaveVersion()
_, _, err = tree.SaveVersion()
require.NoError(err)

_, err := tree.LoadVersionForOverwriting(1)
_, err = tree.LoadVersionForOverwriting(1)
require.NoError(err)
for _, n := range removedNodes {
has, _ := tree.ndb.Has(n.hash)
has, err := tree.ndb.Has(n.hash)
require.NoError(err)
require.False(has, "LoadVersionForOverwriting should remove useless nodes")
}

Expand Down

0 comments on commit 42b6ed1

Please sign in to comment.