fs_blockstore.go 2.63 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 fsblkstorage

import (
20
21
22
	"github.com/hyperledger/fabric/common/ledger"
	"github.com/hyperledger/fabric/common/ledger/blkstorage"
	"github.com/hyperledger/fabric/common/ledger/util/leveldbhelper"
23

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

manish's avatar
manish committed
27
28
29
30
// fsBlockStore - filesystem based implementation for `BlockStore`
type fsBlockStore struct {
	id      string
	conf    *Conf
manish's avatar
manish committed
31
32
33
34
	fileMgr *blockfileMgr
}

// NewFsBlockStore constructs a `FsBlockStore`
manish's avatar
manish committed
35
36
37
func newFsBlockStore(id string, conf *Conf, indexConfig *blkstorage.IndexConfig,
	dbHandle *leveldbhelper.DBHandle) *fsBlockStore {
	return &fsBlockStore{id, conf, newBlockfileMgr(id, conf, indexConfig, dbHandle)}
manish's avatar
manish committed
38
39
40
}

// AddBlock adds a new block
manish's avatar
manish committed
41
func (store *fsBlockStore) AddBlock(block *common.Block) error {
manish's avatar
manish committed
42
43
44
45
	return store.fileMgr.addBlock(block)
}

// GetBlockchainInfo returns the current info about blockchain
46
func (store *fsBlockStore) GetBlockchainInfo() (*common.BlockchainInfo, error) {
manish's avatar
manish committed
47
48
49
50
	return store.fileMgr.getBlockchainInfo(), nil
}

// RetrieveBlocks returns an iterator that can be used for iterating over a range of blocks
manish's avatar
manish committed
51
52
func (store *fsBlockStore) RetrieveBlocks(startNum uint64) (ledger.ResultsIterator, error) {
	var itr *blocksItr
manish's avatar
manish committed
53
	var err error
manish's avatar
manish committed
54
	if itr, err = store.fileMgr.retrieveBlocks(startNum); err != nil {
manish's avatar
manish committed
55
56
57
58
59
60
		return nil, err
	}
	return itr, nil
}

// RetrieveBlockByHash returns the block for given block-hash
manish's avatar
manish committed
61
func (store *fsBlockStore) RetrieveBlockByHash(blockHash []byte) (*common.Block, error) {
manish's avatar
manish committed
62
63
64
65
	return store.fileMgr.retrieveBlockByHash(blockHash)
}

// RetrieveBlockByNumber returns the block at a given blockchain height
manish's avatar
manish committed
66
func (store *fsBlockStore) RetrieveBlockByNumber(blockNum uint64) (*common.Block, error) {
manish's avatar
manish committed
67
68
69
70
	return store.fileMgr.retrieveBlockByNumber(blockNum)
}

// RetrieveTxByID returns a transaction for given transaction id
71
func (store *fsBlockStore) RetrieveTxByID(txID string) (*common.Envelope, error) {
manish's avatar
manish committed
72
73
74
75
	return store.fileMgr.retrieveTransactionByID(txID)
}

// Shutdown shuts down the block store
manish's avatar
manish committed
76
77
func (store *fsBlockStore) Shutdown() {
	logger.Debugf("closing fs blockStore:%s", store.id)
manish's avatar
manish committed
78
79
	store.fileMgr.close()
}