Slightly optimize GenerateInitialIndex.

Rather than fetching the hash of each block individually 2k+ times, make
use of the FetchHeightRange function so all of the most recent hashes can
be fetched at once.
This commit is contained in:
Dave Collins 2013-10-13 16:07:06 -05:00
parent d7e057a020
commit c88338d227

View file

@ -347,19 +347,42 @@ func (b *BlockChain) GenerateInitialIndex() error {
// Loop forwards through each block loading the node into the index for // Loop forwards through each block loading the node into the index for
// the block. // the block.
for i := startHeight; i <= endHeight; i++ { //
hash, err := b.db.FetchBlockShaByHeight(i) // Due to a bug in the SQLite btcdb driver, the FetchBlockBySha call is
// limited to a maximum number of hashes per invocation. Since SQLite
// is going to be nuked eventually, the bug isn't being fixed in the
// driver. In the mean time, work around the issue by calling
// FetchBlockBySha multiple times with the appropriate indices as needed.
for start := startHeight; start < endHeight; {
hashList, err := b.db.FetchHeightRange(start, endHeight+1)
if err != nil { if err != nil {
return err return err
} }
node, err := b.loadBlockNode(hash) // The database did not return any further hashes. Break out of
if err != nil { // the loop now.
return err if len(hashList) == 0 {
break
} }
// This node is now the end of the best chain. // Loop forwards through each block loading the node into the
b.bestChain = node // index for the block.
for _, hash := range hashList {
// Make a copy of the hash to make sure there are no references
// into the list so it can be freed.
hashCopy := hash
node, err := b.loadBlockNode(&hashCopy)
if err != nil {
return err
}
// This node is now the end of the best chain.
b.bestChain = node
}
// Start at the next block after the latest one on the next loop
// iteration.
start += int64(len(hashList))
} }
return nil return nil