From ea3466400a54d3ec6b33a46dfe1a324a40f4134d Mon Sep 17 00:00:00 2001 From: yzang2019 Date: Wed, 11 Sep 2024 16:50:41 -0700 Subject: [PATCH] Add log --- mutable_tree.go | 65 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) diff --git a/mutable_tree.go b/mutable_tree.go index a33cd0b..801bd8f 100644 --- a/mutable_tree.go +++ b/mutable_tree.go @@ -633,9 +633,10 @@ func (tree *MutableTree) LoadVersion(targetVersion int64) (toReturn int64, toErr if rootNodeKey != nil { t.root, err = tree.ndb.GetNode(rootNodeKey) if err != nil { - return 0, err + return tree.LegacyLoadVersion(targetVersion) } } + tree.orphans = map[string]int64{} tree.ITree = t // Mtx is already held tree.lastSaved = t.clone() @@ -643,6 +644,68 @@ func (tree *MutableTree) LoadVersion(targetVersion int64) (toReturn int64, toErr return latestVersion, nil } +// Returns the version number of the latest version found +func (tree *MutableTree) LegacyLoadVersion(targetVersion int64) (toReturn int64, toErr error) { + roots, err := tree.ndb.getRoots() + fmt.Printf("[Debug] Found %d roots]\n", len(roots)) + if err != nil { + return 0, err + } + + if len(roots) == 0 { + if targetVersion <= 0 { + if !tree.skipFastStorageUpgrade { + _, err := tree.enableFastStorageAndCommitIfNotEnabled() + return 0, err + } + return 0, nil + } + return 0, fmt.Errorf("no versions found while trying to load %v", targetVersion) + } + + firstVersion := int64(0) + latestVersion := int64(0) + + var latestRoot []byte + for version, r := range roots { + if version > latestVersion && (targetVersion == 0 || version <= targetVersion) { + latestVersion = version + latestRoot = r + } + if firstVersion == 0 || version < firstVersion { + firstVersion = version + } + } + + if !(targetVersion == 0 || latestVersion == targetVersion) { + return latestVersion, fmt.Errorf("wanted to load target %v but only found up to %v", + targetVersion, latestVersion) + } + + if firstVersion > 0 && firstVersion < int64(tree.ndb.opts.InitialVersion) { + return latestVersion, fmt.Errorf("initial version set to %v, but found earlier version %v", + tree.ndb.opts.InitialVersion, firstVersion) + } + + t := &ImmutableTree{ + ndb: tree.ndb, + version: latestVersion, + skipFastStorageUpgrade: tree.skipFastStorageUpgrade, + } + + if len(latestRoot) != 0 { + t.root, err = tree.ndb.GetNode(latestRoot) + if err != nil { + return 0, err + } + } + + tree.orphans = map[string]int64{} + tree.ITree = t // Mtx is already held + tree.lastSaved = t.clone() + return latestVersion, nil +} + // LoadVersionForOverwriting attempts to load a tree at a previously committed // version, or the latest version below it. Any versions greater than targetVersion will be deleted. func (tree *MutableTree) LoadVersionForOverwriting(targetVersion int64) (int64, error) {