C API: How to get keys with matching prefix?

#1

In the FoundationDB C API, how can I get all keys with a matching prefix? Specifically, using fdb_transaction_get_range and a key selector.

For example, if I have a database similar to the following:

'a' is 'value'
'b' is 'value'
'ba' is 'value'
'babc' is 'value'
'bb' is 'value'
'c' is 'value'

How could I get all keys beginning with ba? I’ve found key selectors to be confusing and would greatly appreciate any help!

(Alex Miller) #2

I would just do a range read from ba to bb. Or, more generally, get_range(prefix, prefix[0:len(prefix)-1] + char(prefix[-1]+1).

In some places there’s a strinc (string increment) function available for this.

(A.J. Beamon) #3

Be careful, however, when incrementing a key that ends in the \xff byte. Here is an example implementation of strinc that handles that case as well: