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

Introduce partial prefix searching #3

Closed
wants to merge 1 commit into from
Closed
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
14 changes: 12 additions & 2 deletions iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@ type Iterator struct {
stack []edges
}

// SeekPrefix is used to seek the iterator to a given prefix
// SeekPrefix is used to seek the iterator to a given prefix.
func (i *Iterator) SeekPrefix(prefix []byte) {
i.SeekPartialPrefix(prefix, true)
}

// SeekCommonPrefix is used to seek the iterator to a given partial prefix.
func (i *Iterator) SeekPartialPrefix(prefix []byte, exactMatch bool) {
// Wipe the stack
i.stack = nil
n := i.node
search := prefix
search := bytes.Trim(prefix, "\x00")
for {
// Check for key exhaution
if len(search) == 0 {
Expand All @@ -38,6 +43,11 @@ func (i *Iterator) SeekPrefix(prefix []byte) {
return
} else {
i.node = nil
// Return the current node in case we are not looking for an
// exact match.
if !exactMatch && bytes.HasPrefix(n.prefix, search) {
i.node = n
}
return
}
}
Expand Down