Skip to content

Commit

Permalink
improve readability of HandleChaincodeDeploy (hyperledger#1080)
Browse files Browse the repository at this point in the history
Signed-off-by: senthil <cendhu@gmail.com>
  • Loading branch information
cendhu authored Apr 18, 2020
1 parent b17b6dd commit 89eb4a3
Showing 1 changed file with 40 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ const (
pvtDataPrefix = "p"
hashDataPrefix = "h"
couchDB = "CouchDB"
// Example for chaincode indexes:
// "META-INF/statedb/couchdb/indexes/indexColorSortName.json"
chaincodeIndexDirDepth = 3
// Example for collection scoped indexes:
// "META-INF/statedb/couchdb/collections/collectionMarbles/indexes/indexCollMarbles.json"
collectionDirDepth = 3
collectionNameDepth = 4
collectionIndexDirDepth = 5
)

// StateDBConfig encapsulates the configuration for stateDB on the ledger.
Expand Down Expand Up @@ -306,32 +314,28 @@ func (s *CommonStorageDB) HandleChaincodeDeploy(chaincodeDefinition *cceventmgmt
}

for directoryPath, indexFiles := range dbArtifacts {
// split the directory name
directoryPathArray := strings.Split(directoryPath, "/")
// process the indexes for the chain
indexFilesData := make(map[string][]byte)
for _, f := range indexFiles {
indexFilesData[f.FileHeader.Name] = f.FileContent
}
if directoryPathArray[3] == "indexes" {

indexInfo := getIndexInfo(directoryPath)
switch {
case indexInfo.hasIndexForChaincode:
err := indexCapable.ProcessIndexesForChaincodeDeploy(chaincodeDefinition.Name, indexFilesData)
if err != nil {
logger.Errorf("Error processing index for chaincode [%s]: %s", chaincodeDefinition.Name, err)
}
continue
}
// check for the indexes directory for the collection
if directoryPathArray[3] == "collections" && directoryPathArray[5] == "indexes" {
collectionName := directoryPathArray[4]
_, ok := collectionConfigMap[collectionName]
case indexInfo.hasIndexForCollection:
_, ok := collectionConfigMap[indexInfo.collectionName]
if !ok {
logger.Errorf("Error processing index for chaincode [%s]: cannot create an index for an undefined collection=[%s]", chaincodeDefinition.Name, collectionName)
} else {
err := indexCapable.ProcessIndexesForChaincodeDeploy(derivePvtDataNs(chaincodeDefinition.Name, collectionName),
indexFilesData)
if err != nil {
logger.Errorf("Error processing collection index for chaincode [%s]: %s", chaincodeDefinition.Name, err)
}
logger.Errorf("Error processing index for chaincode [%s]: cannot create an index for an undefined collection=[%s]",
chaincodeDefinition.Name, indexInfo.collectionName)
continue
}
err := indexCapable.ProcessIndexesForChaincodeDeploy(derivePvtDataNs(chaincodeDefinition.Name, indexInfo.collectionName), indexFilesData)
if err != nil {
logger.Errorf("Error processing collection index for chaincode [%s]: %s", chaincodeDefinition.Name, err)
}
}
}
Expand Down Expand Up @@ -388,3 +392,22 @@ func extractCollectionNames(chaincodeDefinition *cceventmgmt.ChaincodeDefinition
}
return collectionConfigsMap, nil
}

type indexInfo struct {
hasIndexForChaincode bool
hasIndexForCollection bool
collectionName string
}

func getIndexInfo(indexPath string) *indexInfo {
indexInfo := &indexInfo{}
dirsDepth := strings.Split(indexPath, "/")
switch {
case dirsDepth[chaincodeIndexDirDepth] == "indexes":
indexInfo.hasIndexForChaincode = true
case dirsDepth[collectionDirDepth] == "collections" && dirsDepth[collectionIndexDirDepth] == "indexes":
indexInfo.hasIndexForCollection = true
indexInfo.collectionName = dirsDepth[collectionNameDepth]
}
return indexInfo
}

0 comments on commit 89eb4a3

Please sign in to comment.