77import static java .nio .charset .StandardCharsets .UTF_8 ;
88import static org .assertj .core .api .Assertions .assertThat ;
99
10+ import java .io .IOException ;
1011import java .nio .ByteBuffer ;
11- import java .util .ArrayList ;
12- import java .util .Arrays ;
13- import java .util .List ;
12+ import java .nio .charset .StandardCharsets ;
13+ import java .nio .file .Files ;
14+ import java .nio .file .Path ;
15+ import java .nio .file .Paths ;
16+ import java .util .*;
17+
1418import org .junit .*;
1519import org .junit .rules .ExpectedException ;
1620import org .junit .rules .TemporaryFolder ;
@@ -54,8 +58,6 @@ public void keyExistBug() throws RocksDBException{
5458 assertThat (db2 .keyExists ("key" .getBytes (UTF_8 ))).isTrue ();
5559 assertThat (db2 .keyExists ("key2" .getBytes (UTF_8 ))).isFalse ();
5660 assertThat (db2 .keyMayExist ("key" .getBytes (UTF_8 ), null )).isTrue ();
57- // db2.flushWal(true);
58- // db2.compactRange();
5961 }
6062 try (RocksDB db2 = RocksDB .open (dbFolder2 .getRoot ().getAbsolutePath ())) {
6163 assertThat (db2 .keyMayExist ("key" .getBytes (UTF_8 ), null )).isTrue ();
@@ -73,6 +75,102 @@ public void keyExistBug() throws RocksDBException{
7375 }
7476 }
7577
78+ @ Test
79+ public void keyExistGenerator () throws RocksDBException , IOException {
80+
81+ final byte [] KNOWN_KEY = "random_key_value" .getBytes (UTF_8 );
82+ final long PSEUDO_RANDOM_SEED = 15875551233124l ;
83+
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+ ) {
96+ writeOptions .setDisableWAL (true );
97+ db2 .put (KNOWN_KEY , "value" .getBytes (UTF_8 ));
98+
99+ ByteBuffer key = ByteBuffer .allocateDirect (16 );
100+ ByteBuffer value = ByteBuffer .allocateDirect (16 );
101+ Random r = new Random (PSEUDO_RANDOM_SEED );
102+ for (int i = 0 ; i < 1_000_000 ; i ++){
103+ key .clear ();
104+ key .putLong (r .nextLong ());
105+ key .putLong (r .nextLong ());
106+ key .flip ();
107+
108+ value .clear ();
109+ value .putLong (r .nextLong ());
110+ value .putLong (r .nextLong ());
111+ value .flip ();
112+
113+ db2 .put (writeOptions , key , value );
114+ if (i % 50000 == 0 ) {
115+ System .out .println (i );
116+ }
117+ }
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 );
152+ }
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 ();
169+ }
170+ }
171+
172+
173+
76174 @ Test
77175 public void keyExists () throws RocksDBException {
78176 db .put ("key" .getBytes (UTF_8 ), "value" .getBytes (UTF_8 ));
0 commit comments