From 5502704908e86eee93bbc2c11afd1d5aa85dddd6 Mon Sep 17 00:00:00 2001 From: grapebaba <281165273@qq.com> Date: Fri, 12 Aug 2016 17:01:23 +0800 Subject: [PATCH] Change blockchainIndexer interface It would be cleaner to have a single interface method createIndexes(...) and let the two implementations (Sync and Async) deal with the inherent differences in implementation than require that the caller choose the correct method. Change-Id: Ia040c5aeee2c5fdd1b05cc6f7729931aa6d7d548 Signed-off-by: grapebaba <281165273@qq.com> --- core/ledger/blockchain.go | 10 ++++++---- core/ledger/blockchain_indexes.go | 9 ++------- core/ledger/blockchain_indexes_async.go | 7 +------ core/ledger/blockchain_indexes_async_test.go | 5 +---- 4 files changed, 10 insertions(+), 21 deletions(-) diff --git a/core/ledger/blockchain.go b/core/ledger/blockchain.go index abc7cbaf044..6515d3dd2f6 100644 --- a/core/ledger/blockchain.go +++ b/core/ledger/blockchain.go @@ -207,7 +207,7 @@ func (blockchain *blockchain) addPersistenceChangesForNewBlock(ctx context.Conte writeBatch.PutCF(db.GetDBHandle().BlockchainCF, encodeBlockNumberDBKey(blockNumber), blockBytes) writeBatch.PutCF(db.GetDBHandle().BlockchainCF, blockCountKey, encodeUint64(blockNumber+1)) if blockchain.indexer.isSynchronous() { - blockchain.indexer.createIndexesSync(block, blockNumber, blockHash, writeBatch) + blockchain.indexer.createIndexes(block, blockNumber, blockHash, writeBatch) } blockchain.lastProcessedBlock = &lastProcessedBlock{block, blockNumber, blockHash} return blockNumber, nil @@ -218,8 +218,10 @@ func (blockchain *blockchain) blockPersistenceStatus(success bool) { blockchain.size++ blockchain.previousBlockHash = blockchain.lastProcessedBlock.blockHash if !blockchain.indexer.isSynchronous() { - blockchain.indexer.createIndexesAsync(blockchain.lastProcessedBlock.block, - blockchain.lastProcessedBlock.blockNumber, blockchain.lastProcessedBlock.blockHash) + writeBatch := gorocksdb.NewWriteBatch() + defer writeBatch.Destroy() + blockchain.indexer.createIndexes(blockchain.lastProcessedBlock.block, + blockchain.lastProcessedBlock.blockNumber, blockchain.lastProcessedBlock.blockHash, writeBatch) } } blockchain.lastProcessedBlock = nil @@ -249,7 +251,7 @@ func (blockchain *blockchain) persistRawBlock(block *protos.Block, blockNumber u } if blockchain.indexer.isSynchronous() { - blockchain.indexer.createIndexesSync(block, blockNumber, blockHash, writeBatch) + blockchain.indexer.createIndexes(block, blockNumber, blockHash, writeBatch) } opt := gorocksdb.NewDefaultWriteOptions() diff --git a/core/ledger/blockchain_indexes.go b/core/ledger/blockchain_indexes.go index 9e17b7469e9..526393993b8 100644 --- a/core/ledger/blockchain_indexes.go +++ b/core/ledger/blockchain_indexes.go @@ -34,8 +34,7 @@ var prefixAddressBlockNumCompositeKey = byte(3) type blockchainIndexer interface { isSynchronous() bool start(blockchain *blockchain) error - createIndexesSync(block *protos.Block, blockNumber uint64, blockHash []byte, writeBatch *gorocksdb.WriteBatch) error - createIndexesAsync(block *protos.Block, blockNumber uint64, blockHash []byte) error + createIndexes(block *protos.Block, blockNumber uint64, blockHash []byte, writeBatch *gorocksdb.WriteBatch) error fetchBlockNumberByBlockHash(blockHash []byte) (uint64, error) fetchTransactionIndexByID(txID string) (uint64, uint64, error) stop() @@ -57,15 +56,11 @@ func (indexer *blockchainIndexerSync) start(blockchain *blockchain) error { return nil } -func (indexer *blockchainIndexerSync) createIndexesSync( +func (indexer *blockchainIndexerSync) createIndexes( block *protos.Block, blockNumber uint64, blockHash []byte, writeBatch *gorocksdb.WriteBatch) error { return addIndexDataForPersistence(block, blockNumber, blockHash, writeBatch) } -func (indexer *blockchainIndexerSync) createIndexesAsync(block *protos.Block, blockNumber uint64, blockHash []byte) error { - return fmt.Errorf("Method not applicable") -} - func (indexer *blockchainIndexerSync) fetchBlockNumberByBlockHash(blockHash []byte) (uint64, error) { return fetchBlockNumberByBlockHashFromDB(blockHash) } diff --git a/core/ledger/blockchain_indexes_async.go b/core/ledger/blockchain_indexes_async.go index d89528b6435..53f1a1b178a 100644 --- a/core/ledger/blockchain_indexes_async.go +++ b/core/ledger/blockchain_indexes_async.go @@ -99,12 +99,7 @@ func (indexer *blockchainIndexerAsync) start(blockchain *blockchain) error { return nil } -func (indexer *blockchainIndexerAsync) createIndexesSync( - block *protos.Block, blockNumber uint64, blockHash []byte, writeBatch *gorocksdb.WriteBatch) error { - return fmt.Errorf("Method not applicable") -} - -func (indexer *blockchainIndexerAsync) createIndexesAsync(block *protos.Block, blockNumber uint64, blockHash []byte) error { +func (indexer *blockchainIndexerAsync) createIndexes(block *protos.Block, blockNumber uint64, blockHash []byte, writeBatch *gorocksdb.WriteBatch) error { indexer.blockChan <- blockWrapper{block, blockNumber, blockHash, false} return nil } diff --git a/core/ledger/blockchain_indexes_async_test.go b/core/ledger/blockchain_indexes_async_test.go index 10d189f9631..a7de4788c2c 100644 --- a/core/ledger/blockchain_indexes_async_test.go +++ b/core/ledger/blockchain_indexes_async_test.go @@ -155,10 +155,7 @@ func (noop *NoopIndexer) isSynchronous() bool { func (noop *NoopIndexer) start(blockchain *blockchain) error { return nil } -func (noop *NoopIndexer) createIndexesSync(block *protos.Block, blockNumber uint64, blockHash []byte, writeBatch *gorocksdb.WriteBatch) error { - return nil -} -func (noop *NoopIndexer) createIndexesAsync(block *protos.Block, blockNumber uint64, blockHash []byte) error { +func (noop *NoopIndexer) createIndexes(block *protos.Block, blockNumber uint64, blockHash []byte, writeBatch *gorocksdb.WriteBatch) error { return nil } func (noop *NoopIndexer) fetchBlockNumberByBlockHash(blockHash []byte) (uint64, error) {