I'm using RocksDbSharp library, which is implementation of RocksDb from facebook in C#. I would like to filter my records by part of a key, but unfortunately, I don't see an option how to do that. Here is sample code:
static void Main(string[] args)
{
string directory = @"C:\Path\To\RocksDB\Storage";
DbOptions options = new DbOptions().SetCreateIfMissing(true);
using (RocksDb rocksDb = RocksDb.Open(options, directory))
{
rocksDb.Put("AAAAB", "Test 1");
rocksDb.Put("AAABB", "Test 2");
rocksDb.Put("AABBB", "Test 3");
rocksDb.Put("ABBBB", "Test 4");
rocksDb.Put("BBBBB", "Test 5");
List<string> keys = new List<string>();
using (Iterator iterator = rocksDb.NewIterator())
{
iterator.Seek("AAB");
while (iterator.Valid())
{
keys.Add(iterator.StringKey());
iterator.Next();
}
}
Console.WriteLine(string.Join(", ", keys));
Console.ReadLine();
}
}
The output is:
AABBB, ABBBB, BBBBB
How can I change it, so the output would be only AABBB
? I read Iterator wiki and Prefix Seek API, that I can do that setting prefix_same_as_start
to true
:
If no key of the prefix equals or is larger than lookup_key, or after calling one or more Next(), we finish all keys for the prefix, we might return Valid()=false, or any key that is larger than the previous key, depending on whether ReadOptions.prefix_same_as_start=true.
Also, it is explain in this ticket:
If you only want keys starting with prefix 002, use the read_options.prefix_same_as_start, which only shows keys with the same prefix as that when you did the seek.
How can I set this property to true? If you check the implementation of ReadOptions class, you won't find anything to set this property. Do I have any other options to filter keys?