@@ -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