Skip to content

Commit

Permalink
KeyValueStorageRocksDB bounded key iterator: use RocksDB upper bound
Browse files Browse the repository at this point in the history
Use RocksDB upper bound option to move key comparisons into native code
and avoid copying a (key, value) pair that will be discarded.

This will also address a minor (irrelevant?) error where the error check
in next() will falsely succeed after hasNext() returns false on the
bound.
  • Loading branch information
mauricebarnum committed Apr 16, 2021
1 parent 0c5ef8f commit 0be68a1
Showing 1 changed file with 6 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,11 @@

import static com.google.common.base.Preconditions.checkState;

import com.google.common.primitives.UnsignedBytes;

//CHECKSTYLE.OFF: IllegalImport
import io.netty.util.internal.PlatformDependent;
//CHECKSTYLE.ON: IllegalImport

import java.io.IOException;
import java.util.Comparator;
import java.util.Map.Entry;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -311,13 +308,15 @@ public void close() {

@Override
public CloseableIterator<byte[]> keys(byte[] firstKey, byte[] lastKey) {
final RocksIterator iterator = db.newIterator(optionCache);
final Slice upperBound = new Slice(lastKey);
final ReadOptions option = new ReadOptions(optionCache).setIterateUpperBound(upperBound);
final RocksIterator iterator = db.newIterator(option);
iterator.seek(firstKey);

return new CloseableIterator<byte[]>() {
@Override
public boolean hasNext() {
return iterator.isValid() && ByteComparator.compare(iterator.key(), lastKey) < 0;
return iterator.isValid();
}

@Override
Expand All @@ -331,6 +330,8 @@ public byte[] next() {
@Override
public void close() {
iterator.close();
option.close();
upperBound.close();
}
};
}
Expand Down Expand Up @@ -459,7 +460,5 @@ public byte[] getKey() {
}
}

private static final Comparator<byte[]> ByteComparator = UnsignedBytes.lexicographicalComparator();

private static final Logger log = LoggerFactory.getLogger(KeyValueStorageRocksDB.class);
}

0 comments on commit 0be68a1

Please sign in to comment.