DyK
DyK

Reputation: 249

can Xodus get collection by key prefix?

I need a function like prefixSubMap of MapDB. is there such function in Xodus? i cannot find out the interface.

https://jankotek.gitbooks.io/mapdb/content/btreemap/composite-keys.html

prefixSubMap

Upvotes: 1

Views: 162

Answers (2)

Vyacheslav Lukianov
Vyacheslav Lukianov

Reputation: 2053

There is no such function, but you can do the job using Environments API. Given you have Transaction txn, Store store and ByteIterable keyPrefix, enumerating key/value pairs whose keys start with keyPrefix would look like this:

int prefixLen = keyPrefix.getLength();

try (Cursor cursor = store.openCursor(txn)) {
    if (cursor.getSearchKeyRange(keyPrefix) != null) {
        do {
            ByteIterable key = cursor.getKey();
            // check if the key starts with keyPrefix
            int keyLen = key.getLength();
            if (keyLen < prefixLen ||
                ByteIterableUtil.compare(keyPrefix, key.subIterable(0, prefixLen)) != 0) {
                break;
            }
            // wanted key/value pair is here
            ByteIterable value = cursor.getValue();
            ...

        } while(cursor.getNext());
    }
}

Upvotes: 1

DyK
DyK

Reputation: 249

I found out store.openCursor() and cursor.getSearchKey().

Upvotes: 0

Related Questions