github.com/true-sqn/fabric@v2.1.1+incompatible/common/ledger/blockledger/fileledger/factory.go (about)

     1  /*
     2  Copyright IBM Corp. 2017 All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8                   http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package fileledger
    18  
    19  import (
    20  	"sync"
    21  
    22  	"github.com/hyperledger/fabric/common/ledger/blkstorage"
    23  	"github.com/hyperledger/fabric/common/ledger/blkstorage/fsblkstorage"
    24  	"github.com/hyperledger/fabric/common/ledger/blockledger"
    25  	"github.com/hyperledger/fabric/common/metrics"
    26  )
    27  
    28  type fileLedgerFactory struct {
    29  	blkstorageProvider blkstorage.BlockStoreProvider
    30  	ledgers            map[string]blockledger.ReadWriter
    31  	mutex              sync.Mutex
    32  }
    33  
    34  // GetOrCreate gets an existing ledger (if it exists) or creates it if it does not
    35  func (flf *fileLedgerFactory) GetOrCreate(chainID string) (blockledger.ReadWriter, error) {
    36  	flf.mutex.Lock()
    37  	defer flf.mutex.Unlock()
    38  
    39  	key := chainID
    40  	// check cache
    41  	ledger, ok := flf.ledgers[key]
    42  	if ok {
    43  		return ledger, nil
    44  	}
    45  	// open fresh
    46  	blockStore, err := flf.blkstorageProvider.OpenBlockStore(key)
    47  	if err != nil {
    48  		return nil, err
    49  	}
    50  	ledger = NewFileLedger(blockStore)
    51  	flf.ledgers[key] = ledger
    52  	return ledger, nil
    53  }
    54  
    55  // ChannelIDs returns the channel IDs the factory is aware of
    56  func (flf *fileLedgerFactory) ChannelIDs() []string {
    57  	channelIDs, err := flf.blkstorageProvider.List()
    58  	if err != nil {
    59  		logger.Panic(err)
    60  	}
    61  	return channelIDs
    62  }
    63  
    64  // Close releases all resources acquired by the factory
    65  func (flf *fileLedgerFactory) Close() {
    66  	flf.blkstorageProvider.Close()
    67  }
    68  
    69  // New creates a new ledger factory
    70  func New(directory string, metricsProvider metrics.Provider) (blockledger.Factory, error) {
    71  	p, err := fsblkstorage.NewProvider(
    72  		fsblkstorage.NewConf(directory, -1),
    73  		&blkstorage.IndexConfig{
    74  			AttrsToIndex: []blkstorage.IndexableAttr{blkstorage.IndexableAttrBlockNum}},
    75  		metricsProvider,
    76  	)
    77  	if err != nil {
    78  		return nil, err
    79  	}
    80  	return &fileLedgerFactory{
    81  		blkstorageProvider: p,
    82  		ledgers:            make(map[string]blockledger.ReadWriter),
    83  	}, nil
    84  }