github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/common/ledger/blkstorage/fsblkstorage/fs_blockstore.go (about)

     1  /*
     2  Copyright IBM Corp. 2016 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 fsblkstorage
    18  
    19  import (
    20  	"github.com/hyperledger/fabric/common/ledger"
    21  	"github.com/hyperledger/fabric/common/ledger/blkstorage"
    22  	"github.com/hyperledger/fabric/common/ledger/util/leveldbhelper"
    23  
    24  	"github.com/hyperledger/fabric/protos/common"
    25  	"github.com/hyperledger/fabric/protos/peer"
    26  )
    27  
    28  // fsBlockStore - filesystem based implementation for `BlockStore`
    29  type fsBlockStore struct {
    30  	id      string
    31  	conf    *Conf
    32  	fileMgr *blockfileMgr
    33  }
    34  
    35  // NewFsBlockStore constructs a `FsBlockStore`
    36  func newFsBlockStore(id string, conf *Conf, indexConfig *blkstorage.IndexConfig,
    37  	dbHandle *leveldbhelper.DBHandle) *fsBlockStore {
    38  	return &fsBlockStore{id, conf, newBlockfileMgr(id, conf, indexConfig, dbHandle)}
    39  }
    40  
    41  // AddBlock adds a new block
    42  func (store *fsBlockStore) AddBlock(block *common.Block) error {
    43  	return store.fileMgr.addBlock(block)
    44  }
    45  
    46  // GetBlockchainInfo returns the current info about blockchain
    47  func (store *fsBlockStore) GetBlockchainInfo() (*common.BlockchainInfo, error) {
    48  	return store.fileMgr.getBlockchainInfo(), nil
    49  }
    50  
    51  // RetrieveBlocks returns an iterator that can be used for iterating over a range of blocks
    52  func (store *fsBlockStore) RetrieveBlocks(startNum uint64) (ledger.ResultsIterator, error) {
    53  	var itr *blocksItr
    54  	var err error
    55  	if itr, err = store.fileMgr.retrieveBlocks(startNum); err != nil {
    56  		return nil, err
    57  	}
    58  	return itr, nil
    59  }
    60  
    61  // RetrieveBlockByHash returns the block for given block-hash
    62  func (store *fsBlockStore) RetrieveBlockByHash(blockHash []byte) (*common.Block, error) {
    63  	return store.fileMgr.retrieveBlockByHash(blockHash)
    64  }
    65  
    66  // RetrieveBlockByNumber returns the block at a given blockchain height
    67  func (store *fsBlockStore) RetrieveBlockByNumber(blockNum uint64) (*common.Block, error) {
    68  	return store.fileMgr.retrieveBlockByNumber(blockNum)
    69  }
    70  
    71  // RetrieveTxByID returns a transaction for given transaction id
    72  func (store *fsBlockStore) RetrieveTxByID(txID string) (*common.Envelope, error) {
    73  	return store.fileMgr.retrieveTransactionByID(txID)
    74  }
    75  
    76  // RetrieveTxByID returns a transaction for given transaction id
    77  func (store *fsBlockStore) RetrieveTxByBlockNumTranNum(blockNum uint64, tranNum uint64) (*common.Envelope, error) {
    78  	return store.fileMgr.retrieveTransactionByBlockNumTranNum(blockNum, tranNum)
    79  }
    80  
    81  func (store *fsBlockStore) RetrieveBlockByTxID(txID string) (*common.Block, error) {
    82  	return store.fileMgr.retrieveBlockByTxID(txID)
    83  }
    84  
    85  func (store *fsBlockStore) RetrieveTxValidationCodeByTxID(txID string) (peer.TxValidationCode, error) {
    86  	return store.fileMgr.retrieveTxValidationCodeByTxID(txID)
    87  }
    88  
    89  // Shutdown shuts down the block store
    90  func (store *fsBlockStore) Shutdown() {
    91  	logger.Debugf("closing fs blockStore:%s", store.id)
    92  	store.fileMgr.close()
    93  }