Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions common/bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,24 @@ func TrimRightZeroes(s []byte) []byte {
}
return s[:idx]
}

// UpperBound returns the upper bound for iteration over keys with the given prefix.
// It returns the next key in lexicographic order that is greater than all keys with
// the given prefix. This is useful for setting iteration bounds in databases.
// Returns nil if no such upper bound exists (e.g., if prefix is empty or all 0xff bytes).
func UpperBound(prefix []byte) []byte {
if len(prefix) == 0 {
return nil
}
var limit []byte
for i := len(prefix) - 1; i >= 0; i-- {
c := prefix[i]
if c < 0xff {
limit = make([]byte, i+1)
copy(limit, prefix)
limit[i] = c + 1
break
}
}
return limit
}
17 changes: 1 addition & 16 deletions ethdb/pebble/pebble.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,21 +450,6 @@ func (d *Database) NewBatchWithSize(size int) ethdb.Batch {
}
}

// upperBound returns the upper bound for the given prefix
func upperBound(prefix []byte) (limit []byte) {
for i := len(prefix) - 1; i >= 0; i-- {
c := prefix[i]
if c == 0xff {
continue
}
limit = make([]byte, i+1)
copy(limit, prefix)
limit[i] = c + 1
break
}
return limit
}

// Stat returns the internal metrics of Pebble in a text format. It's a developer
// method to read everything there is to read, independent of Pebble version.
func (d *Database) Stat() (string, error) {
Expand Down Expand Up @@ -731,7 +716,7 @@ type pebbleIterator struct {
func (d *Database) NewIterator(prefix []byte, start []byte) ethdb.Iterator {
iter, _ := d.db.NewIter(&pebble.IterOptions{
LowerBound: append(prefix, start...),
UpperBound: upperBound(prefix),
UpperBound: common.UpperBound(prefix),
})
iter.First()
return &pebbleIterator{iter: iter, moved: true, released: false}
Expand Down
Loading