github.com/sykesm/fabric@v1.1.0-preview.0.20200129034918-2aa12b1a0181/common/ledger/blkstorage/blockstorage.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package blkstorage
     8  
     9  import (
    10  	"github.com/hyperledger/fabric-protos-go/common"
    11  	"github.com/hyperledger/fabric-protos-go/peer"
    12  	"github.com/hyperledger/fabric/common/ledger"
    13  	l "github.com/hyperledger/fabric/core/ledger"
    14  	"github.com/pkg/errors"
    15  )
    16  
    17  // IndexableAttr represents an indexable attribute
    18  type IndexableAttr string
    19  
    20  // constants for indexable attributes
    21  const (
    22  	IndexableAttrBlockNum        = IndexableAttr("BlockNum")
    23  	IndexableAttrBlockHash       = IndexableAttr("BlockHash")
    24  	IndexableAttrTxID            = IndexableAttr("TxID")
    25  	IndexableAttrBlockNumTranNum = IndexableAttr("BlockNumTranNum")
    26  )
    27  
    28  // IndexConfig - a configuration that includes a list of attributes that should be indexed
    29  type IndexConfig struct {
    30  	AttrsToIndex []IndexableAttr
    31  }
    32  
    33  // Contains returns true iff the supplied parameter is present in the IndexConfig.AttrsToIndex
    34  func (c *IndexConfig) Contains(indexableAttr IndexableAttr) bool {
    35  	for _, a := range c.AttrsToIndex {
    36  		if a == indexableAttr {
    37  			return true
    38  		}
    39  	}
    40  	return false
    41  }
    42  
    43  var (
    44  	// ErrNotFoundInIndex is used to indicate missing entry in the index
    45  	ErrNotFoundInIndex = l.NotFoundInIndexErr("")
    46  
    47  	// ErrAttrNotIndexed is used to indicate that an attribute is not indexed
    48  	ErrAttrNotIndexed = errors.New("attribute not indexed")
    49  )
    50  
    51  // BlockStoreProvider provides an handle to a BlockStore
    52  type BlockStoreProvider interface {
    53  	CreateBlockStore(ledgerid string) (BlockStore, error)
    54  	OpenBlockStore(ledgerid string) (BlockStore, error)
    55  	Exists(ledgerid string) (bool, error)
    56  	List() ([]string, error)
    57  	Close()
    58  }
    59  
    60  // BlockStore - an interface for persisting and retrieving blocks
    61  // An implementation of this interface is expected to take an argument
    62  // of type `IndexConfig` which configures the block store on what items should be indexed
    63  type BlockStore interface {
    64  	AddBlock(block *common.Block) error
    65  	GetBlockchainInfo() (*common.BlockchainInfo, error)
    66  	RetrieveBlocks(startNum uint64) (ledger.ResultsIterator, error)
    67  	RetrieveBlockByHash(blockHash []byte) (*common.Block, error)
    68  	RetrieveBlockByNumber(blockNum uint64) (*common.Block, error) // blockNum of  math.MaxUint64 will return last block
    69  	RetrieveTxByID(txID string) (*common.Envelope, error)
    70  	RetrieveTxByBlockNumTranNum(blockNum uint64, tranNum uint64) (*common.Envelope, error)
    71  	RetrieveBlockByTxID(txID string) (*common.Block, error)
    72  	RetrieveTxValidationCodeByTxID(txID string) (peer.TxValidationCode, error)
    73  	Shutdown()
    74  }