1414import java .nio .file .Path ;
1515import java .nio .file .Paths ;
1616import java .util .*;
17-
1817import org .junit .*;
1918import org .junit .rules .ExpectedException ;
2019import org .junit .rules .TemporaryFolder ;
@@ -52,54 +51,44 @@ public void after() {
5251 }
5352
5453 @ Test
55- public void keyExistBug () throws RocksDBException {
56- try (RocksDB db2 = RocksDB .open (dbFolder2 .getRoot ().getAbsolutePath ())) {
54+ public void keyExistAfterDbOpen () throws RocksDBException {
55+ try (RocksDB db2 = RocksDB .open (dbFolder2 .getRoot ().getAbsolutePath ())) {
5756 db2 .put ("key" .getBytes (UTF_8 ), "value" .getBytes (UTF_8 ));
5857 assertThat (db2 .keyExists ("key" .getBytes (UTF_8 ))).isTrue ();
5958 assertThat (db2 .keyExists ("key2" .getBytes (UTF_8 ))).isFalse ();
6059 assertThat (db2 .keyMayExist ("key" .getBytes (UTF_8 ), null )).isTrue ();
6160 }
62- try (RocksDB db2 = RocksDB .open (dbFolder2 .getRoot ().getAbsolutePath ())) {
61+ try (RocksDB db2 = RocksDB .open (dbFolder2 .getRoot ().getAbsolutePath ())) {
6362 assertThat (db2 .keyMayExist ("key" .getBytes (UTF_8 ), null )).isTrue ();
6463 assertThat (db2 .keyExists ("key" .getBytes (UTF_8 ))).isTrue ();
6564 assertThat (db2 .keyExists ("key2" .getBytes (UTF_8 ))).isFalse ();
6665 }
67- try (RocksDB db2 = RocksDB .open (dbFolder2 .getRoot ().getAbsolutePath ())) {
66+ try (RocksDB db2 = RocksDB .open (dbFolder2 .getRoot ().getAbsolutePath ())) {
6867 assertThat (db2 .keyMayExist ("key" .getBytes (UTF_8 ), null )).isTrue ();
6968 }
70- try (RocksDB db2 = RocksDB .open (dbFolder2 .getRoot ().getAbsolutePath ())) {
69+ try (RocksDB db2 = RocksDB .open (dbFolder2 .getRoot ().getAbsolutePath ())) {
7170 assertThat (db2 .keyExists ("key" .getBytes (UTF_8 ))).isTrue ();
7271 }
73- try (RocksDB db2 = RocksDB .open (dbFolder2 .getRoot ().getAbsolutePath ())) {
72+ try (RocksDB db2 = RocksDB .open (dbFolder2 .getRoot ().getAbsolutePath ())) {
7473 assertThat (db2 .keyExists ("key2" .getBytes (UTF_8 ))).isFalse ();
7574 }
7675 }
7776
7877 @ Test
79- public void keyExistGenerator () throws RocksDBException , IOException {
80-
81- final byte [] KNOWN_KEY = "random_key_value" .getBytes (UTF_8 );
78+ public void keyExistInTtlDb () throws RocksDBException , IOException {
79+ final byte [] KNOWN_KEY = "random_key" .getBytes (UTF_8 );
8280 final long PSEUDO_RANDOM_SEED = 15875551233124l ;
8381
84- Path rocksDbPath = Paths .get ("/tmp/rocks-test" );
85- if (Files .exists (rocksDbPath )) {
86- Files .walk (rocksDbPath )
87- .sorted (Comparator .reverseOrder ())
88- .map (Path ::toFile )
89- .forEach (x -> x .delete ());
90- }
91-
92- try (Options options = new Options ().setCreateIfMissing (true );
93- TtlDB db2 = TtlDB .open (options , "/tmp/rocks-test" , 86400 , false );
94- WriteOptions writeOptions = new WriteOptions ()
95- ) {
82+ try (Options options = new Options ().setCreateIfMissing (true );
83+ TtlDB db2 = TtlDB .open (options , dbFolder2 .getRoot ().getAbsolutePath (), 86400 , false );
84+ WriteOptions writeOptions = new WriteOptions ()) {
9685 writeOptions .setDisableWAL (true );
9786 db2 .put (KNOWN_KEY , "value" .getBytes (UTF_8 ));
9887
9988 ByteBuffer key = ByteBuffer .allocateDirect (16 );
10089 ByteBuffer value = ByteBuffer .allocateDirect (16 );
10190 Random r = new Random (PSEUDO_RANDOM_SEED );
102- for (int i = 0 ; i < 1_000_000 ; i ++){
91+ for (int i = 0 ; i < 1000 ; i ++) {
10392 key .clear ();
10493 key .putLong (r .nextLong ());
10594 key .putLong (r .nextLong ());
@@ -109,68 +98,16 @@ public void keyExistGenerator() throws RocksDBException, IOException {
10998 value .putLong (r .nextLong ());
11099 value .putLong (r .nextLong ());
111100 value .flip ();
112-
113101 db2 .put (writeOptions , key , value );
114- if (i % 50000 == 0 ) {
115- System .out .println (i );
116- }
117102 }
118- // System.out.print("Compacting ...");
119- // db.compactRange();
120- // System.out.println(" [OK]");
121- }
122- try (Options options = new Options ().setCreateIfMissing (true );
123-
124- //RocksDB db2 = RocksDB.open(options, "/tmp/rocks-test")
125- //RocksDB db2 = RocksDB.openReadOnly(options, "/tmp/rocks-test")
126- TtlDB db2 = TtlDB .open (options , "/tmp/rocks-test" , 86400 , true );
127-
128- ) {
129-
130- // assertThat(db2.get("key".getBytes())).isEqualTo("value".getBytes(UTF_8));
131- Random r = new Random (PSEUDO_RANDOM_SEED );
132- ByteBuffer key = ByteBuffer .allocateDirect (16 );
133- // for (int i = 0; i < 100; i++) {
134- // key.clear();
135- // key.putLong(r.nextLong());
136- // key.putLong(r.nextLong());
137- // key.flip();
138- //
139- // r.nextLong();
140- // r.nextLong();
141- //
142- // System.out.println(" key : " + i + " exist : " + db2.keyExists(key));
143- //
144- // }
145- System .out .println ("Key exist 1 : " );
146- System .out .flush ();
147- db2 .keyExists (KNOWN_KEY );
148- db2 .get (KNOWN_KEY );
149- System .out .println ("Key exists 2 : " );
150- System .out .flush ();
151- db2 .keyExists (KNOWN_KEY );
103+ db2 .compactRange ();
152104 }
153-
154- }
155-
156- @ Test
157- public void verify () throws RocksDBException {
158- try (Options options = new Options ().setCreateIfMissing (true );
159-
160- //RocksDB db2 = RocksDB.open(options, "/tmp/rocks-test")
161- //RocksDB db2 = RocksDB.openReadOnly(options, "/tmp/rocks-test")
162- TtlDB db2 = TtlDB .open (options , "/tmp/rocks-test" , 86400 , true );
163-
164- ) {
165-
166- // assertThat(db2.get("key".getBytes())).isEqualTo("value".getBytes(UTF_8));
167-
168- assertThat (db2 .keyExists ("key" .getBytes (UTF_8 ))).isTrue ();
105+ try (Options options = new Options ();
106+ TtlDB db2 = TtlDB .open (options , dbFolder2 .getRoot ().getAbsolutePath (), 86400 , true );) {
107+ assertThat (db2 .keyExists (KNOWN_KEY )).isTrue ();
169108 }
170109 }
171110
172-
173-
174111 @ Test
175112 public void keyExists () throws RocksDBException {
176113 db .put ("key" .getBytes (UTF_8 ), "value" .getBytes (UTF_8 ));
0 commit comments