Skip to content

Commit

Permalink
Improve Iterator Performance of Seeking with Prefix dgraph-io#1719
Browse files Browse the repository at this point in the history
Signed-off-by: thomassong <thomassong2012@gmail.com>
  • Loading branch information
mYmNeo committed Feb 13, 2023
1 parent 5ceeb30 commit cae7356
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ func (it *Iterator) Next() {
// Set next item to current
it.item = it.data.pop()

for it.iitr.Valid() {
for it.iitr.Valid() && hasPrefix(it.iitr, it.opt.Prefix) {
if it.parseItem() {
// parseItem calls one extra next.
// This is used to deal with the complexity of reverse iteration.
Expand Down Expand Up @@ -666,7 +666,7 @@ func (it *Iterator) prefetch() {
i := it.iitr
var count int
it.item = nil
for i.Valid() {
for i.Valid() && hasPrefix(i, it.opt.Prefix) {
if !it.parseItem() {
continue
}
Expand All @@ -677,6 +677,14 @@ func (it *Iterator) prefetch() {
}
}

// hasPrefix would check if current key in the internal iterator has the prefix
func hasPrefix(it y.Iterator, prefix []byte) bool {
if len(prefix) > 0 {
return bytes.HasPrefix(y.ParseKey(it.Key()), prefix)
}
return true
}

// Seek would seek to the provided key if present. If absent, it would seek to the next
// smallest key greater than the provided key if iterating in the forward direction.
// Behavior would be reversed if iterating backwards.
Expand Down

0 comments on commit cae7356

Please sign in to comment.