mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
core/rawdb: reinit from freezer in sequential order
This commit is contained in:
parent
1afcbae55e
commit
0e5226cc62
3 changed files with 66 additions and 20 deletions
|
|
@ -1,5 +1,20 @@
|
||||||
|
// CookieJar - A contestant's algorithm toolbox
|
||||||
|
// Copyright (c) 2013 Peter Szilagyi. All rights reserved.
|
||||||
|
//
|
||||||
|
// CookieJar is dual licensed: use of this source code is governed by a BSD
|
||||||
|
// license that can be found in the LICENSE file. Alternatively, the CookieJar
|
||||||
|
// toolbox may be used in accordance with the terms and conditions contained
|
||||||
|
// in a signed written agreement between you and the author(s).
|
||||||
|
|
||||||
// This is a duplicated and slightly modified version of "gopkg.in/karalabe/cookiejar.v2/collections/prque".
|
// This is a duplicated and slightly modified version of "gopkg.in/karalabe/cookiejar.v2/collections/prque".
|
||||||
|
|
||||||
|
// Package prque implements a priority queue data structure supporting arbitrary
|
||||||
|
// value types and int64 priorities.
|
||||||
|
//
|
||||||
|
// If you would like to use a min-priority queue, simply negate the priorities.
|
||||||
|
//
|
||||||
|
// Internally the queue is based on the standard heap package working on a
|
||||||
|
// sortable version of the block based stack.
|
||||||
package prque
|
package prque
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|
@ -11,8 +26,8 @@ type Prque struct {
|
||||||
cont *sstack
|
cont *sstack
|
||||||
}
|
}
|
||||||
|
|
||||||
// Creates a new priority queue.
|
// New creates a new priority queue.
|
||||||
func New(setIndex setIndexCallback) *Prque {
|
func New(setIndex SetIndexCallback) *Prque {
|
||||||
return &Prque{newSstack(setIndex)}
|
return &Prque{newSstack(setIndex)}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -21,6 +36,12 @@ func (p *Prque) Push(data interface{}, priority int64) {
|
||||||
heap.Push(p.cont, &item{data, priority})
|
heap.Push(p.cont, &item{data, priority})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Peek returns the value with the greates priority but does not pop it off.
|
||||||
|
func (p *Prque) Peek() (interface{}, int64) {
|
||||||
|
item := p.cont.blocks[0][0]
|
||||||
|
return item.value, item.priority
|
||||||
|
}
|
||||||
|
|
||||||
// Pops the value with the greates priority off the stack and returns it.
|
// Pops the value with the greates priority off the stack and returns it.
|
||||||
// Currently no shrinking is done.
|
// Currently no shrinking is done.
|
||||||
func (p *Prque) Pop() (interface{}, int64) {
|
func (p *Prque) Pop() (interface{}, int64) {
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,11 @@
|
||||||
|
// CookieJar - A contestant's algorithm toolbox
|
||||||
|
// Copyright (c) 2013 Peter Szilagyi. All rights reserved.
|
||||||
|
//
|
||||||
|
// CookieJar is dual licensed: use of this source code is governed by a BSD
|
||||||
|
// license that can be found in the LICENSE file. Alternatively, the CookieJar
|
||||||
|
// toolbox may be used in accordance with the terms and conditions contained
|
||||||
|
// in a signed written agreement between you and the author(s).
|
||||||
|
|
||||||
// This is a duplicated and slightly modified version of "gopkg.in/karalabe/cookiejar.v2/collections/prque".
|
// This is a duplicated and slightly modified version of "gopkg.in/karalabe/cookiejar.v2/collections/prque".
|
||||||
|
|
||||||
package prque
|
package prque
|
||||||
|
|
@ -14,16 +22,16 @@ type item struct {
|
||||||
priority int64
|
priority int64
|
||||||
}
|
}
|
||||||
|
|
||||||
// setIndexCallback is called when the element is moved to a new index.
|
// SetIndexCallback is called when the element is moved to a new index.
|
||||||
// Providing setIndexCallback is optional, it is needed only if the application needs
|
// Providing SetIndexCallback is optional, it is needed only if the application needs
|
||||||
// to delete elements other than the top one.
|
// to delete elements other than the top one.
|
||||||
type setIndexCallback func(a interface{}, i int)
|
type SetIndexCallback func(data interface{}, index int)
|
||||||
|
|
||||||
// Internal sortable stack data structure. Implements the Push and Pop ops for
|
// Internal sortable stack data structure. Implements the Push and Pop ops for
|
||||||
// the stack (heap) functionality and the Len, Less and Swap methods for the
|
// the stack (heap) functionality and the Len, Less and Swap methods for the
|
||||||
// sortability requirements of the heaps.
|
// sortability requirements of the heaps.
|
||||||
type sstack struct {
|
type sstack struct {
|
||||||
setIndex setIndexCallback
|
setIndex SetIndexCallback
|
||||||
size int
|
size int
|
||||||
capacity int
|
capacity int
|
||||||
offset int
|
offset int
|
||||||
|
|
@ -33,7 +41,7 @@ type sstack struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Creates a new, empty stack.
|
// Creates a new, empty stack.
|
||||||
func newSstack(setIndex setIndexCallback) *sstack {
|
func newSstack(setIndex SetIndexCallback) *sstack {
|
||||||
result := new(sstack)
|
result := new(sstack)
|
||||||
result.setIndex = setIndex
|
result.setIndex = setIndex
|
||||||
result.active = make([]*item, blockSize)
|
result.active = make([]*item, blockSize)
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
|
"github.com/ethereum/go-ethereum/common/prque"
|
||||||
"github.com/ethereum/go-ethereum/core/types"
|
"github.com/ethereum/go-ethereum/core/types"
|
||||||
"github.com/ethereum/go-ethereum/ethdb"
|
"github.com/ethereum/go-ethereum/ethdb"
|
||||||
"github.com/ethereum/go-ethereum/log"
|
"github.com/ethereum/go-ethereum/log"
|
||||||
|
|
@ -75,6 +76,9 @@ func InitDatabaseFromFreezer(db ethdb.Database) error {
|
||||||
}
|
}
|
||||||
// Reassemble the blocks into a contiguous stream and push them out to disk
|
// Reassemble the blocks into a contiguous stream and push them out to disk
|
||||||
var (
|
var (
|
||||||
|
queue = prque.New(nil)
|
||||||
|
next = int64(0)
|
||||||
|
|
||||||
batch = db.NewBatch()
|
batch = db.NewBatch()
|
||||||
start = time.Now()
|
start = time.Now()
|
||||||
logged time.Time
|
logged time.Time
|
||||||
|
|
@ -85,11 +89,23 @@ func InitDatabaseFromFreezer(db ethdb.Database) error {
|
||||||
if block == nil {
|
if block == nil {
|
||||||
return errors.New("broken ancient database")
|
return errors.New("broken ancient database")
|
||||||
}
|
}
|
||||||
|
// Push the block into the import queue and process contiguous ranges
|
||||||
|
queue.Push(block, -int64(block.NumberU64()))
|
||||||
|
for !queue.Empty() {
|
||||||
|
// If the next available item is gapped, return
|
||||||
|
if _, priority := queue.Peek(); -priority != next {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
// Next block available, pop it off and index it
|
||||||
|
block = queue.PopItem().(*types.Block)
|
||||||
|
next++
|
||||||
|
|
||||||
// Inject hash<->number mapping and txlookup indexes
|
// Inject hash<->number mapping and txlookup indexes
|
||||||
WriteHeaderNumber(batch, block.Hash(), block.NumberU64())
|
WriteHeaderNumber(batch, block.Hash(), block.NumberU64())
|
||||||
WriteTxLookupEntries(batch, block)
|
WriteTxLookupEntries(batch, block)
|
||||||
|
|
||||||
if batch.ValueSize() > ethdb.IdealBatchSize || i == frozen-1 {
|
// If enough data was accumulated in memory or we're at the last block, dump to disk
|
||||||
|
if batch.ValueSize() > ethdb.IdealBatchSize || uint64(next) == frozen {
|
||||||
if err := batch.Write(); err != nil {
|
if err := batch.Write(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -101,6 +117,7 @@ func InitDatabaseFromFreezer(db ethdb.Database) error {
|
||||||
logged = time.Now()
|
logged = time.Now()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
hash := ReadCanonicalHash(db, frozen-1)
|
hash := ReadCanonicalHash(db, frozen-1)
|
||||||
WriteHeadHeaderHash(db, hash)
|
WriteHeadHeaderHash(db, hash)
|
||||||
WriteHeadFastBlockHash(db, hash)
|
WriteHeadFastBlockHash(db, hash)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue