Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Iterator Performance of Seeking with Prefix #1719

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 12 additions & 2 deletions iterator.go
Expand Up @@ -591,7 +591,8 @@ func (it *Iterator) Next() {

// Set next item to current
it.item = it.data.pop()
for it.iitr.Valid() {
// escape when current key doesn't have prefix
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 @@ -739,7 +740,8 @@ func (it *Iterator) prefetch() {
i := it.iitr
var count int
it.item = nil
for i.Valid() {
// escape when current key doesn't have prefix
for i.Valid() && hasPrefix(i, it.opt.Prefix) {
if !it.parseItem() {
continue
}
Expand All @@ -750,6 +752,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