I am using RocksDB Java JNI and would like to get new entries as they are added to the RocksDB.
Thread t = new Thread(() -> {
for (int i = 0; i < 1000; i++) {
try {
System.out.println("Putting " + i);
rocksDB.put(("key " + i).getBytes(), ("value " + i).getBytes());
Thread.sleep(100);
} catch (InterruptedException | RocksDBException e) {
e.printStackTrace();
}
}
}, "Putting thread");
t.start();
Thread.sleep(1000); // wait for sometime
ReadOptions readOptions = new ReadOptions();
readOptions.setTailing(true);
try (RocksIterator rocksIterator = rocksDB.newIterator(readOptions)) {
for (rocksIterator.seekToFirst(); rocksIterator.isValid(); rocksIterator.next()) {
System.out.println(new String(rocksIterator.key()) + " = " + new String(rocksIterator.value()));
}
}
t.join();
Here, I suppose it is creating a snapshot at that instant of time (i.e. after 1 sec
) and only those elements that are added are getting printed. I expected the tailing iterator should be blocking because because new entries will be added.
Is there any example on how to use tailing iterator in RocksDB?