Skip to content

Commit

Permalink
saveOrphans
Browse files Browse the repository at this point in the history
  • Loading branch information
facundomedica committed May 5, 2022
1 parent 0f5d977 commit 2886aa5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
8 changes: 6 additions & 2 deletions mutable_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,9 @@ func (tree *MutableTree) SaveVersion() ([]byte, int64, error) {
// There can still be orphans, for example if the root is the node being
// removed.
logger.Debug("SAVE EMPTY TREE %v\n", version)
tree.ndb.SaveOrphans(version, tree.orphans)
if err := tree.ndb.SaveOrphans(version, tree.orphans); err != nil {
return nil, 0, err
}
if err := tree.ndb.SaveEmptyRoot(version); err != nil {
return nil, 0, err
}
Expand All @@ -802,7 +804,9 @@ func (tree *MutableTree) SaveVersion() ([]byte, int64, error) {
if _, err := tree.ndb.SaveBranch(tree.root); err != nil {
return nil, 0, err
}
tree.ndb.SaveOrphans(version, tree.orphans)
if err := tree.ndb.SaveOrphans(version, tree.orphans); err != nil {
return nil, 0, err
}
if err := tree.ndb.SaveRoot(tree.root, version); err != nil {
return nil, 0, err
}
Expand Down
19 changes: 13 additions & 6 deletions nodedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,9 @@ func (ndb *nodeDB) DeleteVersionsRange(fromVersion, toVersion int64) error {
}
ndb.uncacheNode(hash)
} else {
ndb.saveOrphan(hash, from, predecessor)
if err := ndb.saveOrphan(hash, from, predecessor); err != nil {
return err
}
}
return nil
})
Expand Down Expand Up @@ -612,26 +614,31 @@ func (ndb *nodeDB) deleteNodesFrom(version int64, hash []byte) error {
// Saves orphaned nodes to disk under a special prefix.
// version: the new version being saved.
// orphans: the orphan nodes created since version-1
func (ndb *nodeDB) SaveOrphans(version int64, orphans map[string]int64) {
func (ndb *nodeDB) SaveOrphans(version int64, orphans map[string]int64) error {
ndb.mtx.Lock()
defer ndb.mtx.Unlock()

toVersion := ndb.getPreviousVersion(version)
for hash, fromVersion := range orphans {
logger.Debug("SAVEORPHAN %v-%v %X\n", fromVersion, toVersion, hash)
ndb.saveOrphan([]byte(hash), fromVersion, toVersion)
err := ndb.saveOrphan([]byte(hash), fromVersion, toVersion)
if err != nil {
return err
}
}
return nil
}

// Saves a single orphan to disk.
func (ndb *nodeDB) saveOrphan(hash []byte, fromVersion, toVersion int64) {
func (ndb *nodeDB) saveOrphan(hash []byte, fromVersion, toVersion int64) error {
if fromVersion > toVersion {
panic(fmt.Sprintf("Orphan expires before it comes alive. %d > %d", fromVersion, toVersion))
return fmt.Errorf("orphan expires before it comes alive. %d > %d", fromVersion, toVersion)
}
key := ndb.orphanKey(fromVersion, toVersion, hash)
if err := ndb.batch.Set(key, hash); err != nil {
panic(err)
return err
}
return nil
}

// deleteOrphans deletes orphaned nodes from disk, and the associated orphan
Expand Down

0 comments on commit 2886aa5

Please sign in to comment.