mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-05-31 20:18:37 +00:00
core/rawdb, triedb: polish code
This commit is contained in:
parent
133ee263c8
commit
40a23eafb8
3 changed files with 29 additions and 36 deletions
|
|
@ -329,6 +329,9 @@ func (f *Freezer) TruncateTail(group string, tail uint64) (uint64, error) {
|
||||||
defer f.writeLock.Unlock()
|
defer f.writeLock.Unlock()
|
||||||
|
|
||||||
prev := cached.Load()
|
prev := cached.Load()
|
||||||
|
if prev >= tail {
|
||||||
|
return prev, nil
|
||||||
|
}
|
||||||
for _, table := range f.tables {
|
for _, table := range f.tables {
|
||||||
if table.config.tailGroup != group {
|
if table.config.tailGroup != group {
|
||||||
continue
|
continue
|
||||||
|
|
@ -337,9 +340,8 @@ func (f *Freezer) TruncateTail(group string, tail uint64) (uint64, error) {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if tail > prev {
|
cached.Store(tail)
|
||||||
cached.Store(tail)
|
|
||||||
}
|
|
||||||
// Update the head if the requested tail exceeds the current head.
|
// Update the head if the requested tail exceeds the current head.
|
||||||
if f.head.Load() < tail {
|
if f.head.Load() < tail {
|
||||||
f.head.Store(tail)
|
f.head.Store(tail)
|
||||||
|
|
|
||||||
|
|
@ -228,6 +228,7 @@ func (b *memoryBatch) commit(freezer *MemoryFreezer) (items uint64, writeSize in
|
||||||
// interface and can be used along with ephemeral key-value store.
|
// interface and can be used along with ephemeral key-value store.
|
||||||
type MemoryFreezer struct {
|
type MemoryFreezer struct {
|
||||||
items uint64 // Number of items stored
|
items uint64 // Number of items stored
|
||||||
|
tails map[string]uint64 // Per-group tail cache; access serialized by lock
|
||||||
readonly bool // Flag if the freezer is only for reading
|
readonly bool // Flag if the freezer is only for reading
|
||||||
lock sync.RWMutex // Lock to protect fields
|
lock sync.RWMutex // Lock to protect fields
|
||||||
tables map[string]*memoryTable // Tables for storing everything
|
tables map[string]*memoryTable // Tables for storing everything
|
||||||
|
|
@ -236,14 +237,21 @@ type MemoryFreezer struct {
|
||||||
|
|
||||||
// NewMemoryFreezer initializes an in-memory freezer instance.
|
// NewMemoryFreezer initializes an in-memory freezer instance.
|
||||||
func NewMemoryFreezer(readonly bool, tableName map[string]freezerTableConfig) *MemoryFreezer {
|
func NewMemoryFreezer(readonly bool, tableName map[string]freezerTableConfig) *MemoryFreezer {
|
||||||
tables := make(map[string]*memoryTable)
|
var (
|
||||||
|
tables = make(map[string]*memoryTable)
|
||||||
|
tails = make(map[string]uint64)
|
||||||
|
)
|
||||||
for name, cfg := range tableName {
|
for name, cfg := range tableName {
|
||||||
tables[name] = newMemoryTable(name, cfg)
|
tables[name] = newMemoryTable(name, cfg)
|
||||||
|
if cfg.tailGroup != "" {
|
||||||
|
tails[cfg.tailGroup] = 0
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return &MemoryFreezer{
|
return &MemoryFreezer{
|
||||||
writeBatch: newMemoryBatch(),
|
writeBatch: newMemoryBatch(),
|
||||||
readonly: readonly,
|
readonly: readonly,
|
||||||
tables: tables,
|
tables: tables,
|
||||||
|
tails: tails,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -289,7 +297,7 @@ func (f *MemoryFreezer) Ancients() (uint64, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tail returns the lowest accessible item index for the given tail group.
|
// Tail returns the lowest accessible item index for the given tail group.
|
||||||
// All tables sharing the group must agree on the tail; an empty group name
|
// All tables sharing the group agree on the tail; an empty group name
|
||||||
// refers to non-prunable tables and always returns 0.
|
// refers to non-prunable tables and always returns 0.
|
||||||
func (f *MemoryFreezer) Tail(group string) (uint64, error) {
|
func (f *MemoryFreezer) Tail(group string) (uint64, error) {
|
||||||
f.lock.RLock()
|
f.lock.RLock()
|
||||||
|
|
@ -298,22 +306,8 @@ func (f *MemoryFreezer) Tail(group string) (uint64, error) {
|
||||||
if group == "" {
|
if group == "" {
|
||||||
return 0, nil
|
return 0, nil
|
||||||
}
|
}
|
||||||
var (
|
tail, ok := f.tails[group]
|
||||||
tail uint64
|
if !ok {
|
||||||
found bool
|
|
||||||
)
|
|
||||||
for _, table := range f.tables {
|
|
||||||
if table.config.tailGroup != group {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if !found {
|
|
||||||
tail = table.offset
|
|
||||||
found = true
|
|
||||||
} else if table.offset != tail {
|
|
||||||
return 0, fmt.Errorf("inconsistent tail in group %q: %d vs %d", group, table.offset, tail)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !found {
|
|
||||||
return 0, fmt.Errorf("unknown tail group: %q", group)
|
return 0, fmt.Errorf("unknown tail group: %q", group)
|
||||||
}
|
}
|
||||||
return tail, nil
|
return tail, nil
|
||||||
|
|
@ -409,22 +403,13 @@ func (f *MemoryFreezer) TruncateTail(group string, tail uint64) (uint64, error)
|
||||||
if group == "" {
|
if group == "" {
|
||||||
return 0, errors.New("empty tail group")
|
return 0, errors.New("empty tail group")
|
||||||
}
|
}
|
||||||
var (
|
prev, ok := f.tails[group]
|
||||||
prev uint64
|
if !ok {
|
||||||
found bool
|
|
||||||
)
|
|
||||||
for _, table := range f.tables {
|
|
||||||
if table.config.tailGroup != group {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if !found {
|
|
||||||
prev = table.offset
|
|
||||||
found = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !found {
|
|
||||||
return 0, fmt.Errorf("unknown tail group: %q", group)
|
return 0, fmt.Errorf("unknown tail group: %q", group)
|
||||||
}
|
}
|
||||||
|
if prev >= tail {
|
||||||
|
return prev, nil
|
||||||
|
}
|
||||||
for _, table := range f.tables {
|
for _, table := range f.tables {
|
||||||
if table.config.tailGroup != group {
|
if table.config.tailGroup != group {
|
||||||
continue
|
continue
|
||||||
|
|
@ -433,6 +418,7 @@ func (f *MemoryFreezer) TruncateTail(group string, tail uint64) (uint64, error)
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
f.tails[group] = tail
|
||||||
if f.items < tail {
|
if f.items < tail {
|
||||||
f.items = tail
|
f.items = tail
|
||||||
}
|
}
|
||||||
|
|
@ -462,10 +448,15 @@ func (f *MemoryFreezer) Reset() error {
|
||||||
defer f.lock.Unlock()
|
defer f.lock.Unlock()
|
||||||
|
|
||||||
tables := make(map[string]*memoryTable)
|
tables := make(map[string]*memoryTable)
|
||||||
|
tails := make(map[string]uint64)
|
||||||
for name, table := range f.tables {
|
for name, table := range f.tables {
|
||||||
tables[name] = newMemoryTable(name, table.config)
|
tables[name] = newMemoryTable(name, table.config)
|
||||||
|
if table.config.tailGroup != "" {
|
||||||
|
tails[table.config.tailGroup] = 0
|
||||||
|
}
|
||||||
}
|
}
|
||||||
f.tables = tables
|
f.tables = tables
|
||||||
|
f.tails = tails
|
||||||
f.items = 0
|
f.items = 0
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -237,7 +237,7 @@ func TestTruncateOutOfRange(t *testing.T) {
|
||||||
|
|
||||||
// Ensure of-out-range truncations are rejected correctly.
|
// Ensure of-out-range truncations are rejected correctly.
|
||||||
head, _ := freezer.Ancients()
|
head, _ := freezer.Ancients()
|
||||||
tail, _ := freezer.Tail(rawdb.StateHistoryTailGroup)
|
tail, _ := freezer.Tail(rawdb.DefaultHistoryGroup)
|
||||||
|
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
mode int
|
mode int
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue