github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/core/ledger/kvledger/rebuild_dbs.go (about)

     1  /*
     2  Copyright hechain. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package kvledger
     8  
     9  import (
    10  	"github.com/hechain20/hechain/common/ledger/blkstorage"
    11  	"github.com/hechain20/hechain/common/ledger/util/leveldbhelper"
    12  	"github.com/hechain20/hechain/core/ledger"
    13  	"github.com/hechain20/hechain/core/ledger/kvledger/txmgmt/statedb/statecouchdb"
    14  	"github.com/pkg/errors"
    15  )
    16  
    17  // RebuildDBs drops existing ledger databases.
    18  // Dropped database will be rebuilt upon server restart
    19  func RebuildDBs(config *ledger.Config) error {
    20  	rootFSPath := config.RootFSPath
    21  	fileLockPath := fileLockPath(rootFSPath)
    22  	fileLock := leveldbhelper.NewFileLock(fileLockPath)
    23  	if err := fileLock.Lock(); err != nil {
    24  		return errors.Wrap(err, "as another peer node command is executing,"+
    25  			" wait for that command to complete its execution or terminate it before retrying")
    26  	}
    27  	defer fileLock.Unlock()
    28  
    29  	blockstorePath := BlockStorePath(rootFSPath)
    30  	ledgerIDs, err := blkstorage.GetLedgersBootstrappedFromSnapshot(blockstorePath)
    31  	if err != nil {
    32  		return errors.WithMessage(err, "error while checking if any ledger has been bootstrapped from snapshot")
    33  	}
    34  	if len(ledgerIDs) > 0 {
    35  		return errors.Errorf("cannot rebuild databases because the peer contains channel(s) %s that were bootstrapped from snapshot", ledgerIDs)
    36  	}
    37  
    38  	if config.StateDBConfig.StateDatabase == ledger.CouchDB {
    39  		if err := statecouchdb.DropApplicationDBs(config.StateDBConfig.CouchDB); err != nil {
    40  			return err
    41  		}
    42  	}
    43  	if err := dropDBs(rootFSPath); err != nil {
    44  		return err
    45  	}
    46  	return blkstorage.DeleteBlockStoreIndex(blockstorePath)
    47  }