github.com/osdi23p228/fabric@v0.0.0-20221218062954-77808885f5db/core/ledger/kvledger/rebuild_dbs.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package kvledger
     8  
     9  import (
    10  	"github.com/osdi23p228/fabric/common/ledger/blkstorage"
    11  	"github.com/osdi23p228/fabric/common/ledger/util/leveldbhelper"
    12  	"github.com/osdi23p228/fabric/core/ledger"
    13  	"github.com/osdi23p228/fabric/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  	if config.StateDBConfig.StateDatabase == "CouchDB" {
    30  		if err := statecouchdb.DropApplicationDBs(config.StateDBConfig.CouchDB); err != nil {
    31  			return err
    32  		}
    33  	}
    34  	if err := dropDBs(rootFSPath); err != nil {
    35  		return err
    36  	}
    37  
    38  	blockstorePath := BlockStorePath(rootFSPath)
    39  	return blkstorage.DeleteBlockStoreIndex(blockstorePath)
    40  }