Skip to content

Commit 266c66c

Browse files
authored
Merge pull request #2671 from CortexFoundation/dev
batch db closed
2 parents e11db5a + 8a040bd commit 266c66c

9 files changed

Lines changed: 29 additions & 0 deletions

File tree

core/blockchain.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -978,6 +978,8 @@ func (bc *BlockChain) ExportN(w io.Writer, first uint64, last uint64) error {
978978
func (bc *BlockChain) writeHeadBlock(block *types.Block) {
979979
// Add the block to the canonical chain number scheme and mark as the head
980980
batch := bc.db.NewBatch()
981+
defer batch.Close()
982+
981983
rawdb.WriteHeadHeaderHash(batch, block.Hash())
982984
rawdb.WriteHeadFastBlockHash(batch, block.Hash())
983985
rawdb.WriteCanonicalHash(batch, block.Hash(), block.NumberU64())
@@ -1437,6 +1439,7 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
14371439
// Note all the components of block(td, hash->number map, header, body, receipts)
14381440
// should be written atomically. BlockBatch is used for containing all components.
14391441
blockBatch := bc.db.NewBatch()
1442+
defer blockBatch.Close()
14401443
rawdb.WriteTd(blockBatch, block.Hash(), block.NumberU64(), externTd)
14411444
rawdb.WriteBlock(blockBatch, block)
14421445
rawdb.WriteReceipts(blockBatch, block.Hash(), block.NumberU64(), receipts)
@@ -2286,6 +2289,8 @@ func (bc *BlockChain) reorg(oldBlock, newBlock *types.Header) error {
22862289
// Delete useless indexes right now which includes the non-canonical
22872290
// transaction indexes, canonical chain indexes which above the head.
22882291
batch := bc.db.NewBatch()
2292+
defer batch.Close()
2293+
22892294
for _, tx := range types.HashDifference(deletedTxs, rebirthTxs) {
22902295
rawdb.DeleteTxLookupEntry(batch, tx)
22912296
}

core/rawdb/table.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,11 @@ func (b *tableBatch) Reset() {
260260
b.batch.Reset()
261261
}
262262

263+
// Close closes the batch and releases all associated resources.
264+
func (b *tableBatch) Close() {
265+
b.batch.Close()
266+
}
267+
263268
// tableReplayer is a wrapper around a batch replayer which truncates
264269
// the added prefix.
265270
type tableReplayer struct {

core/state/statedb.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,6 +1095,8 @@ func (s *StateDB) Commit(block uint64, deleteEmptyObjects bool) (common.Hash, er
10951095
return common.Hash{}, err
10961096
}
10971097

1098+
code.Close()
1099+
10981100
// Write the account trie changes, measuing the amount of wasted time
10991101
// The onleaf func is called _serially_, so we can reuse the same account
11001102
// for unmarshalling every time.

ctxcdb/batch.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ type Batch interface {
3737

3838
// Replay replays the batch contents.
3939
Replay(w KeyValueWriter) error
40+
41+
// Close closes the batch and releases all associated resources.
42+
Close()
4043
}
4144

4245
// Batcher wraps the NewBatch method of a backing data store.

ctxcdb/leveldb/leveldb.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,9 @@ func (b *batch) Replay(w ctxcdb.KeyValueWriter) error {
491491
return b.b.Replay(&replayer{writer: w})
492492
}
493493

494+
// Close closes the batch and releases all associated resources.
495+
func (b *batch) Close() {}
496+
494497
// replayer is a small wrapper to implement the correct replay methods.
495498
type replayer struct {
496499
writer ctxcdb.KeyValueWriter

ctxcdb/memorydb/memorydb.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,9 @@ func (b *batch) Replay(w ctxcdb.KeyValueWriter) error {
350350
return nil
351351
}
352352

353+
// Close closes the batch and releases all associated resources.
354+
func (b *batch) Close() {}
355+
353356
// iterator can walk over the (potentially partial) keyspace of a memory key
354357
// value store. Internally it is a deep copy of the entire iterated state,
355358
// sorted by keys.

ctxcdb/pebble/pebble.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,12 @@ func (b *batch) Replay(w ctxcdb.KeyValueWriter) error {
719719
}
720720
}
721721

722+
// Close closes the batch and releases all associated resources. After it is
723+
// closed, any subsequent operations on this batch are undefined.
724+
func (b *batch) Close() {
725+
b.b.Close()
726+
}
727+
722728
// pebbleIterator is a wrapper of underlying iterator in storage engine.
723729
// The purpose of this structure is to implement the missing APIs.
724730
//

trie/database.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,7 @@ func (db *Database) Cap(limit common.StorageSize) error {
611611
log.Error("Failed to write flush list to disk", "err", err)
612612
return err
613613
}
614+
batch.Close()
614615
// Write successful, clear out the flushed data
615616

616617
for db.oldest != oldest {

trie/trie_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,7 @@ func (b *spongeBatch) ValueSize() int { return 100 }
776776
func (b *spongeBatch) Write() error { return nil }
777777
func (b *spongeBatch) Reset() {}
778778
func (b *spongeBatch) Replay(w ctxcdb.KeyValueWriter) error { return nil }
779+
func (b *spongeBatch) Close() {}
779780

780781
// TestCommitSequence tests that the trie.Commit operation writes the elements
781782
// of the trie in the expected order.

0 commit comments

Comments
 (0)