[badger] Prefix + reverse lookup

I am using prefixes in my keys. And I am able to lookup up data for those prefixes.

However, when I set opts.Reverse = true, I get no results. What is the recommended way of doing a reverse scan of prefixed keys?

My current code looks like this:
opts := badger.DefaultIteratorOptions
//opts.Reverse = true
it := txn.NewIterator(opts)
defer it.Close()
prefix := byte(req.Topic)
for it.Seek(prefix); it.ValidForPrefix(prefix); it.Next() {


You might be going past the search space. See:

Thanks! Is there another way of breaking up the storage space?

Let’s say, I am storing data which has 1000 prefixes with 100 values each. To find 100 values for a given prefix, in the worst case, I am scanning through 100000 values. Instead, it would be nice to break-up the keyspace for prefixes.

I’m not sure I follow the question. By “finding a value”, do you mean “seeking a key”? If so, a Seek would practically do a binary search in the entire space, it won’t scan over all the values.

If by finding a value, you really meant retrieve each value and compare it, even then you can seek to the prefix keyspace, and then do KV iteration.

Never mind. I misunderstood how storage is being done.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.