blockstorage.go 2.38 KB
Newer Older
manish's avatar
manish committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*
Copyright IBM Corp. 2016 All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

		 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package blkstorage

import (
manish's avatar
manish committed
20
21
	"errors"

22
	"github.com/hyperledger/fabric/common/ledger"
23
	"github.com/hyperledger/fabric/protos/common"
manish's avatar
manish committed
24
25
)

manish's avatar
manish committed
26
27
28
29
30
// IndexableAttr represents an indexable attribute
type IndexableAttr string

// constants for indexable attributes
const (
31
32
33
34
	IndexableAttrBlockNum        = IndexableAttr("BlockNum")
	IndexableAttrBlockHash       = IndexableAttr("BlockHash")
	IndexableAttrTxID            = IndexableAttr("TxID")
	IndexableAttrBlockNumTranNum = IndexableAttr("BlockNumTranNum")
manish's avatar
manish committed
35
36
37
38
39
40
41
42
43
44
45
46
47
48
)

// IndexConfig - a configuration that includes a list of attributes that should be indexed
type IndexConfig struct {
	AttrsToIndex []IndexableAttr
}

var (
	// ErrNotFoundInIndex is used to indicate missing entry in the index
	ErrNotFoundInIndex = errors.New("Entry not found in index")
	// ErrAttrNotIndexed is used to indicate that an attribute is not indexed
	ErrAttrNotIndexed = errors.New("Attribute not indexed")
)

manish's avatar
manish committed
49
50
51
52
53
54
55
56
57
// BlockStoreProvider provides an handle to a BlockStore
type BlockStoreProvider interface {
	CreateBlockStore(ledgerid string) (BlockStore, error)
	OpenBlockStore(ledgerid string) (BlockStore, error)
	Exists(ledgerid string) (bool, error)
	List() ([]string, error)
	Close()
}

manish's avatar
manish committed
58
// BlockStore - an interface for persisting and retrieving blocks
manish's avatar
manish committed
59
60
// An implementation of this interface is expected to take an argument
// of type `IndexConfig` which configures the block store on what items should be indexed
manish's avatar
manish committed
61
type BlockStore interface {
62
	AddBlock(block *common.Block) error
63
	GetBlockchainInfo() (*common.BlockchainInfo, error)
manish's avatar
manish committed
64
	RetrieveBlocks(startNum uint64) (ledger.ResultsIterator, error)
65
	RetrieveBlockByHash(blockHash []byte) (*common.Block, error)
denyeart's avatar
denyeart committed
66
	RetrieveBlockByNumber(blockNum uint64) (*common.Block, error) // blockNum of  math.MaxUint64 will return last block
67
	RetrieveTxByID(txID string) (*common.Envelope, error)
manish's avatar
manish committed
68
69
	Shutdown()
}