Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Java API of Rocks DB support prefix scan?

Tags:

java

rocksdb

I have huge data set(key-value) in Rocks DB and I have to search for key based on prefix of key in hand. I do not want to scan whole data set to filter out key based on key-prefix. is there any way to do that?

like image 784
user2463894 Avatar asked Oct 19 '25 05:10

user2463894


1 Answers

You can use something like this. Using RocksIterator there is a api exposed where you can seek to the key substring and if your key starts with the prefix then consider that key.

Please find the sample code.

List<String> result = new ArrayList<String>();
RocksIterator iterator = db.newIterator();
for (iterator.seek(prefix.getBytes()); iterator.isValid(); iterator
                .next()) {
           String key = new String(iterator.key());
            if (!key.startsWith(prefix))
            break;
      result.add(String.format("%s", new String(iterator.key())));
}

Hope it will help you.

like image 159
Pramatha V Avatar answered Oct 21 '25 21:10

Pramatha V