Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deleterange #88

Open
publicocean0 opened this issue Dec 10, 2017 · 1 comment
Open

Deleterange #88

publicocean0 opened this issue Dec 10, 2017 · 1 comment

Comments

@publicocean0
Copy link

How to delete a range of keys

@pcmind
Copy link
Contributor

pcmind commented Jan 2, 2018

LevelDB does not support delete range; a delete is like an insert, you need to create a delete entry for each existing key.
You should do something like this to delete a key range:

final DB db = ...;
final byte[] firstKeyOrPreffix = ...;
final Predicate<byte[]> inRange = ...;
try(final DBIterator iterator = db.iterator(); final WriteBatch wb = db.createWriteBatch()) {
    iterator.seek(firstKeyOrPreffix);
    while (iterator.hasNext()) {
        final Map.Entry<byte[], byte[]> next = iterator.next();
        if (!inRange.test(next.getKey())) {
            break;
        }
        wb.delete(next.getKey());
    }
    db.write(wb);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants