Skip to content

Commit 2ce8f48

Browse files
committed
Add java test for keyMayExists.
1 parent 76a88c7 commit 2ce8f48

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

db/db_impl/db_impl.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3830,7 +3830,6 @@ bool DBImpl::KeyMayExist(const ReadOptions& read_options,
38303830
// not present in block_cache, the return value will be Status::Incomplete.
38313831
// In this case, key may still exist in the table.
38323832
if (value_found != nullptr && s.IsIncomplete()) {
3833-
// falsify later if key-may-exist but can't fetch value
38343833
*value_found = false;
38353834
}
38363835
return s.ok() || s.IsIncomplete();

java/src/test/java/org/rocksdb/KeyMayExistTest.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ public class KeyMayExistTest {
2424
@Rule
2525
public TemporaryFolder dbFolder = new TemporaryFolder();
2626

27+
@Rule public TemporaryFolder dbFolder2 = new TemporaryFolder();
28+
2729
@Rule public ExpectedException exceptionRule = ExpectedException.none();
2830

2931
List<ColumnFamilyDescriptor> cfDescriptors;
@@ -541,4 +543,40 @@ public void keyMayExistNonUnicodeString() throws RocksDBException {
541543
exists = db.keyMayExist("key".getBytes(UTF_8), null);
542544
assertThat(exists).isTrue();
543545
}
546+
547+
/*
548+
This test was created to verify bug with keyMaye exist and value_found.
549+
It's probabilistic and expect that SST files are not loaded immediately to memory
550+
after database opening.
551+
*/
552+
@Test
553+
public void keyMayExistNoCache() throws RocksDBException {
554+
byte[] key = "key".getBytes(UTF_8);
555+
byte[] value = "value".getBytes(UTF_8);
556+
557+
try (RocksDB db2 = RocksDB.open(dbFolder2.getRoot().getAbsolutePath())) {
558+
db2.put(key, value);
559+
db2.compactRange();
560+
}
561+
562+
try (RocksDB db2 = RocksDB.open(dbFolder2.getRoot().getAbsolutePath())) {
563+
Holder<byte[]> holder = new Holder<>();
564+
565+
boolean exists = db2.keyMayExist("key".getBytes(UTF_8), holder);
566+
567+
assertThat(exists).isTrue();
568+
569+
// keys is not in cache, so returned value must be null;
570+
assertThat(holder.getValue()).isNull();
571+
572+
byte[] returned_value = db2.get(key);
573+
574+
assertThat(returned_value).isEqualTo(value);
575+
576+
exists = db2.keyMayExist("key".getBytes(UTF_8), holder);
577+
assertThat(exists).isTrue();
578+
// key is in cache, it should return value
579+
assertThat(holder.getValue()).isEqualTo(value);
580+
}
581+
}
544582
}

0 commit comments

Comments
 (0)