@@ -442,31 +442,37 @@ func (rkc *rootkeyCache) setRootKey(version int64, rootKey []byte) {
442442// deletes orphans
443443func (ndb * nodeDB ) deleteVersion (version int64 , cache * rootkeyCache ) error {
444444 rootKey , err := cache .getRootKey (ndb , version )
445- if err != nil {
445+ if err != nil && err != ErrVersionDoesNotExist {
446446 return err
447447 }
448448
449- if err := ndb .traverseOrphans (version , version + 1 , func (orphan * Node ) error {
450- if orphan .nodeKey .nonce == 0 && ! orphan .isLegacy {
451- // if the orphan is a reformatted root, it can be a legacy root
452- // so it should be removed from the pruning process.
453- if err := ndb .deleteFromPruning (ndb .legacyNodeKey (orphan .hash )); err != nil {
454- return err
449+ if err == ErrVersionDoesNotExist {
450+ ndb .logger .Error ("Error while pruning, moving on the the next version in the store" , "version missing" , version , "next version" , version + 1 , "err" , err )
451+ }
452+
453+ if rootKey != nil {
454+ if err := ndb .traverseOrphans (version , version + 1 , func (orphan * Node ) error {
455+ if orphan .nodeKey .nonce == 0 && ! orphan .isLegacy {
456+ // if the orphan is a reformatted root, it can be a legacy root
457+ // so it should be removed from the pruning process.
458+ if err := ndb .deleteFromPruning (ndb .legacyNodeKey (orphan .hash )); err != nil {
459+ return err
460+ }
455461 }
462+ if orphan .nodeKey .nonce == 1 && orphan .nodeKey .version < version {
463+ // if the orphan is referred to the previous root, it should be reformatted
464+ // to (version, 0), because the root (version, 1) should be removed but not
465+ // applied now due to the batch writing.
466+ orphan .nodeKey .nonce = 0
467+ }
468+ nk := orphan .GetKey ()
469+ if orphan .isLegacy {
470+ return ndb .deleteFromPruning (ndb .legacyNodeKey (nk ))
471+ }
472+ return ndb .deleteFromPruning (ndb .nodeKey (nk ))
473+ }); err != nil && err != ErrVersionDoesNotExist {
474+ return err
456475 }
457- if orphan .nodeKey .nonce == 1 && orphan .nodeKey .version < version {
458- // if the orphan is referred to the previous root, it should be reformatted
459- // to (version, 0), because the root (version, 1) should be removed but not
460- // applied now due to the batch writing.
461- orphan .nodeKey .nonce = 0
462- }
463- nk := orphan .GetKey ()
464- if orphan .isLegacy {
465- return ndb .deleteFromPruning (ndb .legacyNodeKey (nk ))
466- }
467- return ndb .deleteFromPruning (ndb .nodeKey (nk ))
468- }); err != nil {
469- return err
470476 }
471477
472478 literalRootKey := GetRootKey (version )
@@ -480,7 +486,7 @@ func (ndb *nodeDB) deleteVersion(version int64, cache *rootkeyCache) error {
480486
481487 // check if the version is referred by the next version
482488 nextRootKey , err := cache .getRootKey (ndb , version + 1 )
483- if err != nil {
489+ if err != nil && err != ErrVersionDoesNotExist {
484490 return err
485491 }
486492 if bytes .Equal (literalRootKey , nextRootKey ) {
@@ -704,6 +710,7 @@ func (ndb *nodeDB) deleteVersionsTo(toVersion int64) error {
704710 ndb .logger .Error ("Error deleting legacy versions" , "err" , err )
705711 }
706712 first = legacyLatestVersion + 1
713+
707714 // reset the legacy latest version forcibly to avoid multiple calls
708715 ndb .resetLegacyLatestVersion (- 1 )
709716 }
0 commit comments