Skip to content

Commit

Permalink
feat: allow saving version 0
Browse files Browse the repository at this point in the history
  • Loading branch information
kocubinski committed Oct 31, 2024
1 parent e99f035 commit b7bfb3a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
9 changes: 6 additions & 3 deletions mutable_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type MutableTree struct {
unsavedFastNodeRemovals *sync.Map // map[string]interface{} FastNodes that have not yet been removed from disk
ndb *nodeDB
skipFastStorageUpgrade bool // If true, the tree will work like no fast storage and always not upgrade fast storage
initialVersionSet bool

mtx sync.Mutex
}
Expand Down Expand Up @@ -146,8 +147,9 @@ func (tree *MutableTree) WorkingHash() []byte {

func (tree *MutableTree) WorkingVersion() int64 {
version := tree.version + 1
if version == 1 && tree.ndb.opts.InitialVersion > 0 {
if version == 1 && tree.initialVersionSet {
version = int64(tree.ndb.opts.InitialVersion)
tree.initialVersionSet = false
}
return version
}
Expand Down Expand Up @@ -459,11 +461,11 @@ func (tree *MutableTree) LoadVersion(targetVersion int64) (int64, error) {
tree.ndb.opts.InitialVersion, firstVersion)
}

if latestVersion < targetVersion {
if latestVersion >= 0 && latestVersion < targetVersion {
return latestVersion, fmt.Errorf("wanted to load target %d but only found up to %d", targetVersion, latestVersion)
}

if firstVersion == 0 {
if firstVersion <= 0 {
if targetVersion <= 0 {
if !tree.skipFastStorageUpgrade {
tree.mtx.Lock()
Expand Down Expand Up @@ -871,6 +873,7 @@ func (tree *MutableTree) saveFastNodeRemovals() error {
// and is otherwise ignored.
func (tree *MutableTree) SetInitialVersion(version uint64) {
tree.ndb.opts.InitialVersion = version
tree.initialVersionSet = true
}

// DeleteVersionsTo removes versions upto the given version from the MutableTree.
Expand Down
2 changes: 1 addition & 1 deletion nodedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,7 @@ func (ndb *nodeDB) getLatestVersion() (int64, error) {
return latestVersion, nil
}

return 0, nil
return -1, nil
}

func (ndb *nodeDB) resetLatestVersion(version int64) {
Expand Down

0 comments on commit b7bfb3a

Please sign in to comment.