github.com/ethersphere/bee/v2@v2.2.0/pkg/storer/migration/step_04.go (about)

     1  // Copyright 2023 The Swarm Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package migration
     6  
     7  import (
     8  	"context"
     9  	"os"
    10  
    11  	"github.com/ethersphere/bee/v2/pkg/log"
    12  	"github.com/ethersphere/bee/v2/pkg/sharky"
    13  	"github.com/ethersphere/bee/v2/pkg/storer/internal/chunkstore"
    14  	"github.com/ethersphere/bee/v2/pkg/storer/internal/transaction"
    15  	"github.com/ethersphere/bee/v2/pkg/swarm"
    16  )
    17  
    18  // step_04 is the fourth step of the migration. It forces a sharky recovery to
    19  // be run on the localstore.
    20  func step_04(
    21  	sharkyBasePath string,
    22  	sharkyNoOfShards int,
    23  	st transaction.Storage,
    24  ) func() error {
    25  	return func() error {
    26  		// for in-mem store, skip this step
    27  		if sharkyBasePath == "" {
    28  			return nil
    29  		}
    30  		logger := log.NewLogger("migration-step-04", log.WithSink(os.Stdout))
    31  
    32  		logger.Info("starting sharky recovery")
    33  		sharkyRecover, err := sharky.NewRecovery(sharkyBasePath, sharkyNoOfShards, swarm.SocMaxChunkSize)
    34  		if err != nil {
    35  			return err
    36  		}
    37  
    38  		c := chunkstore.IterateLocations(context.Background(), st.IndexStore())
    39  
    40  		for res := range c {
    41  			if res.Err != nil {
    42  				return res.Err
    43  			}
    44  
    45  			if err := sharkyRecover.Add(res.Location); err != nil {
    46  				return err
    47  			}
    48  		}
    49  
    50  		if err := sharkyRecover.Save(); err != nil {
    51  			return err
    52  		}
    53  		logger.Info("finished sharky recovery")
    54  
    55  		return sharkyRecover.Close()
    56  	}
    57  }