blocks_itr.go 3.43 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
20
/*
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 (
	"fmt"
manish's avatar
manish committed
21
	"sync"
manish's avatar
manish committed
22

23
	"github.com/hyperledger/fabric/common/ledger"
24

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

manish's avatar
manish committed
28
29
// blockHolder holds block bytes
type blockHolder struct {
manish's avatar
manish committed
30
31
32
33
	blockBytes []byte
}

// GetBlock serializes Block from block bytes
manish's avatar
manish committed
34
func (bh *blockHolder) GetBlock() *common.Block {
35
	block, err := deserializeBlock(bh.blockBytes)
manish's avatar
manish committed
36
37
38
39
40
41
42
	if err != nil {
		panic(fmt.Errorf("Problem in deserialzing block: %s", err))
	}
	return block
}

// GetBlockBytes returns block bytes
manish's avatar
manish committed
43
func (bh *blockHolder) GetBlockBytes() []byte {
manish's avatar
manish committed
44
45
46
	return bh.blockBytes
}

manish's avatar
manish committed
47
48
// blocksItr - an iterator for iterating over a sequence of blocks
type blocksItr struct {
manish's avatar
manish committed
49
50
51
52
53
54
	mgr                  *blockfileMgr
	maxBlockNumAvailable uint64
	blockNumToRetrieve   uint64
	stream               *blockStream
	closeMarker          bool
	closeMarkerLock      *sync.Mutex
manish's avatar
manish committed
55
56
}

manish's avatar
manish committed
57
58
func newBlockItr(mgr *blockfileMgr, startBlockNum uint64) *blocksItr {
	return &blocksItr{mgr, mgr.cpInfo.lastBlockNumber, startBlockNum, nil, false, &sync.Mutex{}}
manish's avatar
manish committed
59
60
}

manish's avatar
manish committed
61
func (itr *blocksItr) waitForBlock(blockNum uint64) uint64 {
manish's avatar
manish committed
62
63
64
65
66
67
68
69
70
71
72
	itr.mgr.cpInfoCond.L.Lock()
	defer itr.mgr.cpInfoCond.L.Unlock()
	for itr.mgr.cpInfo.lastBlockNumber < blockNum && !itr.shouldClose() {
		logger.Debugf("Going to wait for newer blocks. maxAvailaBlockNumber=[%d], waitForBlockNum=[%d]",
			itr.mgr.cpInfo.lastBlockNumber, blockNum)
		itr.mgr.cpInfoCond.Wait()
		logger.Debugf("Came out of wait. maxAvailaBlockNumber=[%d]", itr.mgr.cpInfo.lastBlockNumber)
	}
	return itr.mgr.cpInfo.lastBlockNumber
}

manish's avatar
manish committed
73
func (itr *blocksItr) initStream() error {
manish's avatar
manish committed
74
75
76
77
78
79
80
	var lp *fileLocPointer
	var err error
	if lp, err = itr.mgr.index.getBlockLocByBlockNum(itr.blockNumToRetrieve); err != nil {
		return err
	}
	if itr.stream, err = newBlockStream(itr.mgr.rootDir, lp.fileSuffixNum, int64(lp.offset), -1); err != nil {
		return err
manish's avatar
manish committed
81
	}
manish's avatar
manish committed
82
83
84
	return nil
}

manish's avatar
manish committed
85
func (itr *blocksItr) shouldClose() bool {
manish's avatar
manish committed
86
87
88
	itr.closeMarkerLock.Lock()
	defer itr.closeMarkerLock.Unlock()
	return itr.closeMarker
manish's avatar
manish committed
89
90
}

manish's avatar
manish committed
91
// Next moves the cursor to next block and returns true iff the iterator is not exhausted
manish's avatar
manish committed
92
func (itr *blocksItr) Next() (ledger.QueryResult, error) {
manish's avatar
manish committed
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
	if itr.maxBlockNumAvailable < itr.blockNumToRetrieve {
		itr.maxBlockNumAvailable = itr.waitForBlock(itr.blockNumToRetrieve)
	}
	itr.closeMarkerLock.Lock()
	defer itr.closeMarkerLock.Unlock()
	if itr.closeMarker {
		return nil, nil
	}
	if itr.stream == nil {
		if err := itr.initStream(); err != nil {
			return nil, err
		}
	}
	nextBlockBytes, err := itr.stream.nextBlockBytes()
	if err != nil {
		return nil, err
manish's avatar
manish committed
109
	}
manish's avatar
manish committed
110
	itr.blockNumToRetrieve++
manish's avatar
manish committed
111
	return &blockHolder{nextBlockBytes}, nil
manish's avatar
manish committed
112
113
114
}

// Close releases any resources held by the iterator
manish's avatar
manish committed
115
func (itr *blocksItr) Close() {
manish's avatar
manish committed
116
117
118
119
120
121
	itr.closeMarkerLock.Lock()
	defer itr.closeMarkerLock.Unlock()
	itr.closeMarker = true
	itr.mgr.cpInfoCond.L.Lock()
	defer itr.mgr.cpInfoCond.L.Unlock()
	itr.mgr.cpInfoCond.Broadcast()
manish's avatar
manish committed
122
123
	itr.stream.close()
}