Skip to content

Commit 5502704

Browse files
committed
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>
1 parent 8ea25a9 commit 5502704

File tree

4 files changed

+10
-21
lines changed

4 files changed

+10
-21
lines changed

core/ledger/blockchain.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ func (blockchain *blockchain) addPersistenceChangesForNewBlock(ctx context.Conte
207207
writeBatch.PutCF(db.GetDBHandle().BlockchainCF, encodeBlockNumberDBKey(blockNumber), blockBytes)
208208
writeBatch.PutCF(db.GetDBHandle().BlockchainCF, blockCountKey, encodeUint64(blockNumber+1))
209209
if blockchain.indexer.isSynchronous() {
210-
blockchain.indexer.createIndexesSync(block, blockNumber, blockHash, writeBatch)
210+
blockchain.indexer.createIndexes(block, blockNumber, blockHash, writeBatch)
211211
}
212212
blockchain.lastProcessedBlock = &lastProcessedBlock{block, blockNumber, blockHash}
213213
return blockNumber, nil
@@ -218,8 +218,10 @@ func (blockchain *blockchain) blockPersistenceStatus(success bool) {
218218
blockchain.size++
219219
blockchain.previousBlockHash = blockchain.lastProcessedBlock.blockHash
220220
if !blockchain.indexer.isSynchronous() {
221-
blockchain.indexer.createIndexesAsync(blockchain.lastProcessedBlock.block,
222-
blockchain.lastProcessedBlock.blockNumber, blockchain.lastProcessedBlock.blockHash)
221+
writeBatch := gorocksdb.NewWriteBatch()
222+
defer writeBatch.Destroy()
223+
blockchain.indexer.createIndexes(blockchain.lastProcessedBlock.block,
224+
blockchain.lastProcessedBlock.blockNumber, blockchain.lastProcessedBlock.blockHash, writeBatch)
223225
}
224226
}
225227
blockchain.lastProcessedBlock = nil
@@ -249,7 +251,7 @@ func (blockchain *blockchain) persistRawBlock(block *protos.Block, blockNumber u
249251
}
250252

251253
if blockchain.indexer.isSynchronous() {
252-
blockchain.indexer.createIndexesSync(block, blockNumber, blockHash, writeBatch)
254+
blockchain.indexer.createIndexes(block, blockNumber, blockHash, writeBatch)
253255
}
254256

255257
opt := gorocksdb.NewDefaultWriteOptions()

core/ledger/blockchain_indexes.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ var prefixAddressBlockNumCompositeKey = byte(3)
3434
type blockchainIndexer interface {
3535
isSynchronous() bool
3636
start(blockchain *blockchain) error
37-
createIndexesSync(block *protos.Block, blockNumber uint64, blockHash []byte, writeBatch *gorocksdb.WriteBatch) error
38-
createIndexesAsync(block *protos.Block, blockNumber uint64, blockHash []byte) error
37+
createIndexes(block *protos.Block, blockNumber uint64, blockHash []byte, writeBatch *gorocksdb.WriteBatch) error
3938
fetchBlockNumberByBlockHash(blockHash []byte) (uint64, error)
4039
fetchTransactionIndexByID(txID string) (uint64, uint64, error)
4140
stop()
@@ -57,15 +56,11 @@ func (indexer *blockchainIndexerSync) start(blockchain *blockchain) error {
5756
return nil
5857
}
5958

60-
func (indexer *blockchainIndexerSync) createIndexesSync(
59+
func (indexer *blockchainIndexerSync) createIndexes(
6160
block *protos.Block, blockNumber uint64, blockHash []byte, writeBatch *gorocksdb.WriteBatch) error {
6261
return addIndexDataForPersistence(block, blockNumber, blockHash, writeBatch)
6362
}
6463

65-
func (indexer *blockchainIndexerSync) createIndexesAsync(block *protos.Block, blockNumber uint64, blockHash []byte) error {
66-
return fmt.Errorf("Method not applicable")
67-
}
68-
6964
func (indexer *blockchainIndexerSync) fetchBlockNumberByBlockHash(blockHash []byte) (uint64, error) {
7065
return fetchBlockNumberByBlockHashFromDB(blockHash)
7166
}

core/ledger/blockchain_indexes_async.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,7 @@ func (indexer *blockchainIndexerAsync) start(blockchain *blockchain) error {
9999
return nil
100100
}
101101

102-
func (indexer *blockchainIndexerAsync) createIndexesSync(
103-
block *protos.Block, blockNumber uint64, blockHash []byte, writeBatch *gorocksdb.WriteBatch) error {
104-
return fmt.Errorf("Method not applicable")
105-
}
106-
107-
func (indexer *blockchainIndexerAsync) createIndexesAsync(block *protos.Block, blockNumber uint64, blockHash []byte) error {
102+
func (indexer *blockchainIndexerAsync) createIndexes(block *protos.Block, blockNumber uint64, blockHash []byte, writeBatch *gorocksdb.WriteBatch) error {
108103
indexer.blockChan <- blockWrapper{block, blockNumber, blockHash, false}
109104
return nil
110105
}

core/ledger/blockchain_indexes_async_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,7 @@ func (noop *NoopIndexer) isSynchronous() bool {
155155
func (noop *NoopIndexer) start(blockchain *blockchain) error {
156156
return nil
157157
}
158-
func (noop *NoopIndexer) createIndexesSync(block *protos.Block, blockNumber uint64, blockHash []byte, writeBatch *gorocksdb.WriteBatch) error {
159-
return nil
160-
}
161-
func (noop *NoopIndexer) createIndexesAsync(block *protos.Block, blockNumber uint64, blockHash []byte) error {
158+
func (noop *NoopIndexer) createIndexes(block *protos.Block, blockNumber uint64, blockHash []byte, writeBatch *gorocksdb.WriteBatch) error {
162159
return nil
163160
}
164161
func (noop *NoopIndexer) fetchBlockNumberByBlockHash(blockHash []byte) (uint64, error) {

0 commit comments

Comments
 (0)