cockroachdb / pebble

RocksDB/LevelDB inspired key-value database in Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Prefix Scan not working

nilansaha opened this issue · comments

Hi, I am unable to make the prefix scan work. I did try looking into examples but couldn't find any good ones. The one example from an issue I found doesn't really work.

I am basically trying to get all keys with a prefix special__ so keys like special__1, special__2 are a match. But special_1 shouldn't be a match

Code

package main

import (
    "fmt"
    "github.com/cockroachdb/pebble"
    "log"
    "strings"
    "time"
)

func main() {
    // Open the Pebble database
    db, err := pebble.Open("../data.db", &pebble.Options{})
    if err != nil {
        log.Fatalf("Error opening database: %v", err)
    }
    defer db.Close()

    fmt.Println("Database opened")

    start := time.Now()

    iter, _ := db.NewIter(&pebble.IterOptions{LowerBound: []byte("special")})
        for succ := iter.First(); succ == true; succ = iter.Next() {
                key := string(iter.Key())
        
        if !strings.HasPrefix(key,"special__") {
                break
        }

        fmt.Println(key)
        }

    elapsed := time.Since(start)
    fmt.Printf("Took: %s\n", elapsed)

This returns empty but I know for a fact those keys exists. Any help will be appreciated.

The way you've written your code, special__ would sort after special_1 so you'd be breaking out of the loop before you get to the keys you're interested in.