-
Notifications
You must be signed in to change notification settings - Fork 302
feat: allow saving version 0 #1002
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
Changes from 3 commits
b7bfb3a
35f6e94
81b4a52
507b04b
f37eaef
f1d176e
cfce4c6
ec511e4
fbd5df5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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 | ||||||||||||||||
} | ||||||||||||||||
|
@@ -62,6 +63,7 @@ func NewMutableTree(db corestore.KVStoreWithBatch, cacheSize int, skipFastStorag | |||||||||||||||
unsavedFastNodeRemovals: &sync.Map{}, | ||||||||||||||||
ndb: ndb, | ||||||||||||||||
skipFastStorageUpgrade: skipFastStorageUpgrade, | ||||||||||||||||
initialVersionSet: opts.initialVersionSet, | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
|
@@ -73,7 +75,8 @@ func (tree *MutableTree) IsEmpty() bool { | |||||||||||||||
|
||||||||||||||||
// GetLatestVersion returns the latest version of the tree. | ||||||||||||||||
func (tree *MutableTree) GetLatestVersion() (int64, error) { | ||||||||||||||||
return tree.ndb.getLatestVersion() | ||||||||||||||||
_, v, err := tree.ndb.getLatestVersion() | ||||||||||||||||
return v, err | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
// VersionExists returns whether or not a version exists. | ||||||||||||||||
|
@@ -90,10 +93,13 @@ func (tree *MutableTree) VersionExists(version int64) bool { | |||||||||||||||
if err != nil { | ||||||||||||||||
return false | ||||||||||||||||
} | ||||||||||||||||
latestVersion, err := tree.ndb.getLatestVersion() | ||||||||||||||||
found, latestVersion, err := tree.ndb.getLatestVersion() | ||||||||||||||||
if err != nil { | ||||||||||||||||
return false | ||||||||||||||||
} | ||||||||||||||||
if !found { | ||||||||||||||||
return false | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
return firstVersion <= version && version <= latestVersion | ||||||||||||||||
} | ||||||||||||||||
|
@@ -104,7 +110,7 @@ func (tree *MutableTree) AvailableVersions() []int { | |||||||||||||||
if err != nil { | ||||||||||||||||
return nil | ||||||||||||||||
} | ||||||||||||||||
latestVersion, err := tree.ndb.getLatestVersion() | ||||||||||||||||
_, latestVersion, err := tree.ndb.getLatestVersion() | ||||||||||||||||
if err != nil { | ||||||||||||||||
return nil | ||||||||||||||||
} | ||||||||||||||||
|
@@ -146,8 +152,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 | ||||||||||||||||
kocubinski marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||
} | ||||||||||||||||
|
@@ -449,7 +456,7 @@ func (tree *MutableTree) LoadVersion(targetVersion int64) (int64, error) { | |||||||||||||||
tree.ndb.opts.InitialVersion, firstVersion) | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
latestVersion, err := tree.ndb.getLatestVersion() | ||||||||||||||||
_, latestVersion, err := tree.ndb.getLatestVersion() | ||||||||||||||||
if err != nil { | ||||||||||||||||
return 0, err | ||||||||||||||||
} | ||||||||||||||||
|
@@ -459,11 +466,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() | ||||||||||||||||
|
@@ -549,7 +556,8 @@ func (tree *MutableTree) IsUpgradeable() (bool, error) { | |||||||||||||||
if err != nil { | ||||||||||||||||
return false, err | ||||||||||||||||
} | ||||||||||||||||
return !tree.skipFastStorageUpgrade && (!tree.ndb.hasUpgradedToFastStorage() || shouldForce), nil | ||||||||||||||||
hasUpgradedToFastStorage := tree.ndb.hasUpgradedToFastStorage() | ||||||||||||||||
return !tree.skipFastStorageUpgrade && (!hasUpgradedToFastStorage || shouldForce), nil | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
// enableFastStorageAndCommitIfNotEnabled if nodeDB doesn't mark fast storage as enabled, enable it, and commit the update. | ||||||||||||||||
|
@@ -604,7 +612,7 @@ func (tree *MutableTree) enableFastStorageAndCommit() error { | |||||||||||||||
return err | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
latestVersion, err := tree.ndb.getLatestVersion() | ||||||||||||||||
_, latestVersion, err := tree.ndb.getLatestVersion() | ||||||||||||||||
if err != nil { | ||||||||||||||||
return err | ||||||||||||||||
} | ||||||||||||||||
|
@@ -871,6 +879,7 @@ func (tree *MutableTree) saveFastNodeRemovals() error { | |||||||||||||||
// and is otherwise ignored. | ||||||||||||||||
func (tree *MutableTree) SetInitialVersion(version uint64) { | ||||||||||||||||
tree.ndb.opts.InitialVersion = version | ||||||||||||||||
tree.initialVersionSet = true | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider adding mutex protection for thread safety The Apply this diff to add mutex protection: func (tree *MutableTree) SetInitialVersion(version uint64) {
+ tree.mtx.Lock()
+ defer tree.mtx.Unlock()
tree.ndb.opts.InitialVersion = version
tree.initialVersionSet = true
} 📝 Committable suggestion
Suggested change
|
||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
// DeleteVersionsTo removes versions upto the given version from the MutableTree. | ||||||||||||||||
|
Uh oh!
There was an error while loading. Please reload this page.