代码拉取完成,页面将自动刷新
/*
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"
"sync"
"github.com/hyperledger/fabric/common/ledger"
"github.com/hyperledger/fabric/protos/common"
)
// blockHolder holds block bytes
type blockHolder struct {
blockBytes []byte
}
// GetBlock serializes Block from block bytes
func (bh *blockHolder) GetBlock() *common.Block {
block, err := deserializeBlock(bh.blockBytes)
if err != nil {
panic(fmt.Errorf("Problem in deserialzing block: %s", err))
}
return block
}
// GetBlockBytes returns block bytes
func (bh *blockHolder) GetBlockBytes() []byte {
return bh.blockBytes
}
// blocksItr - an iterator for iterating over a sequence of blocks
type blocksItr struct {
mgr *blockfileMgr
maxBlockNumAvailable uint64
blockNumToRetrieve uint64
stream *blockStream
closeMarker bool
closeMarkerLock *sync.Mutex
}
func newBlockItr(mgr *blockfileMgr, startBlockNum uint64) *blocksItr {
return &blocksItr{mgr, mgr.cpInfo.lastBlockNumber, startBlockNum, nil, false, &sync.Mutex{}}
}
func (itr *blocksItr) waitForBlock(blockNum uint64) uint64 {
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
}
func (itr *blocksItr) initStream() error {
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
}
return nil
}
func (itr *blocksItr) shouldClose() bool {
itr.closeMarkerLock.Lock()
defer itr.closeMarkerLock.Unlock()
return itr.closeMarker
}
// Next moves the cursor to next block and returns true iff the iterator is not exhausted
func (itr *blocksItr) Next() (ledger.QueryResult, error) {
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
}
itr.blockNumToRetrieve++
return &blockHolder{nextBlockBytes}, nil
}
// Close releases any resources held by the iterator
func (itr *blocksItr) Close() {
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()
itr.stream.close()
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。