core, ethdb: define MaximumKey

This commit is contained in:
Gary Rong 2025-06-19 20:10:36 +08:00
parent 9bc6bf62ce
commit ad34d98cf1
3 changed files with 18 additions and 8 deletions

View file

@ -17,8 +17,6 @@
package rawdb package rawdb
import ( import (
"bytes"
"github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethdb"
) )
@ -137,7 +135,7 @@ func (t *table) DeleteRange(start, end []byte) error {
// The nilness will be lost by adding the prefix, explicitly converting it // The nilness will be lost by adding the prefix, explicitly converting it
// to a special flag representing the end of key range. // to a special flag representing the end of key range.
if end == nil { if end == nil {
end = bytes.Repeat([]byte{0xff}, 32) end = ethdb.MaximumKey
} }
return t.db.DeleteRange(append([]byte(t.prefix), start...), append([]byte(t.prefix), end...)) return t.db.DeleteRange(append([]byte(t.prefix), start...), append([]byte(t.prefix), end...))
} }
@ -235,7 +233,7 @@ func (b *tableBatch) DeleteRange(start, end []byte) error {
// The nilness will be lost by adding the prefix, explicitly converting it // The nilness will be lost by adding the prefix, explicitly converting it
// to a special flag representing the end of key range. // to a special flag representing the end of key range.
if end == nil { if end == nil {
end = bytes.Repeat([]byte{0xff}, 32) end = ethdb.MaximumKey
} }
return b.batch.DeleteRange(append([]byte(b.prefix), start...), append([]byte(b.prefix), end...)) return b.batch.DeleteRange(append([]byte(b.prefix), start...), append([]byte(b.prefix), end...))
} }

View file

@ -18,10 +18,23 @@
package ethdb package ethdb
import ( import (
"bytes"
"errors" "errors"
"io" "io"
) )
var (
// MaximumKey is a special marker representing the largest possible key
// in the database.
//
// All prefixed database entries will be smaller than this marker.
// For trie nodes in hash mode, we use a 32-byte slice filled with 0xFF
// because there may be shared prefixes starting with multiple 0xFF bytes.
// Using 32 bytes ensures that only a hash collision could potentially
// match or exceed it.
MaximumKey = bytes.Repeat([]byte{0xff}, 32)
)
// KeyValueReader wraps the Has and Get method of a backing data store. // KeyValueReader wraps the Has and Get method of a backing data store.
type KeyValueReader interface { type KeyValueReader interface {
// Has retrieves if a key is present in the key-value data store. // Has retrieves if a key is present in the key-value data store.

View file

@ -18,7 +18,6 @@
package pebble package pebble
import ( import (
"bytes"
"fmt" "fmt"
"runtime" "runtime"
"strings" "strings"
@ -411,7 +410,7 @@ func (d *Database) DeleteRange(start, end []byte) error {
// in pebble(nil in leveldb). Use an ugly hack to construct a // in pebble(nil in leveldb). Use an ugly hack to construct a
// large key to represent it. // large key to represent it.
if end == nil { if end == nil {
end = bytes.Repeat([]byte{0xff}, 32) end = ethdb.MaximumKey
} }
return d.db.DeleteRange(start, end, d.writeOptions) return d.db.DeleteRange(start, end, d.writeOptions)
} }
@ -471,7 +470,7 @@ func (d *Database) Compact(start []byte, limit []byte) error {
// 0xff-s, so 32 ensures than only a hash collision could touch it. // 0xff-s, so 32 ensures than only a hash collision could touch it.
// https://github.com/cockroachdb/pebble/issues/2359#issuecomment-1443995833 // https://github.com/cockroachdb/pebble/issues/2359#issuecomment-1443995833
if limit == nil { if limit == nil {
limit = bytes.Repeat([]byte{0xff}, 32) limit = ethdb.MaximumKey
} }
return d.db.Compact(start, limit, true) // Parallelization is preferred return d.db.Compact(start, limit, true) // Parallelization is preferred
} }
@ -633,7 +632,7 @@ func (b *batch) DeleteRange(start, end []byte) error {
// in pebble(nil in leveldb). Use an ugly hack to construct a // in pebble(nil in leveldb). Use an ugly hack to construct a
// large key to represent it. // large key to represent it.
if end == nil { if end == nil {
end = bytes.Repeat([]byte{0xff}, 32) end = ethdb.MaximumKey
} }
if err := b.b.DeleteRange(start, end, nil); err != nil { if err := b.b.DeleteRange(start, end, nil); err != nil {
return err return err