Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,11 @@ public boolean isEmpty() throws IOException {
@Override
public boolean isExist(byte[] key) throws IOException {
try {
return db.get(handle, key) != null;
// RocksDB#keyMayExist
// If the key definitely does not exist in the database, then this
// method returns false, else true.
return db.keyMayExist(handle, key, new StringBuilder())
&& db.get(handle, key) != null;
} catch (RocksDBException e) {
throw toIOException(
"Error in accessing DB. ", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.Map;
import java.util.Set;

import org.apache.commons.codec.binary.StringUtils;
import org.apache.hadoop.hdfs.DFSUtil;

import org.apache.commons.lang3.RandomStringUtils;
Expand Down Expand Up @@ -289,4 +290,41 @@ public void testRocksDBCheckpointCleanup() throws Exception {
checkpoint.getCheckpointLocation()));
}
}

/**
* Not strictly a unit test. Just a confirmation of the expected behavior
* of RocksDB keyMayExist API.
* @throws Exception if unable to read from RocksDB.
*/
@Test
public void testRocksDBKeyMayExistApi() throws Exception {
try (RDBStore newStore =
new RDBStore(folder.newFolder(), options, configSet)) {
RocksDB db = newStore.getDb();

for (int i = 0; i < 100; i++) {
db.put(StringUtils.getBytesUtf16("key" + i),
StringUtils.getBytesUtf16("Value" + i));
}

long start = System.nanoTime();
for (int i = 0; i < 50; i++) {
Assert.assertTrue(db.get(
StringUtils.getBytesUtf16("key" + i))!= null);
}
long end = System.nanoTime();
long keyGetLatency = end - start;

start = System.nanoTime();
for (int i = 50; i < 100; i++) {
Assert.assertTrue(db.get(
StringUtils.getBytesUtf16("key" + i))!= null);
}
end = System.nanoTime();
long keyMayExistLatency = end - start;

Assert.assertTrue(keyMayExistLatency < keyGetLatency);
}
}

}