/** Should only be called by one thread, and only after all accesses to the DB happened. */ @Override public void dispose() { super.dispose(); // Acquire the lock, so that no ongoing snapshots access the db during cleanup synchronized (asyncSnapshotLock) { // IMPORTANT: null reference to signal potential async checkpoint workers that the db was // disposed, as // working on the disposed object results in SEGFAULTS. Other code has to check field #db for // null // and access it in a synchronized block that locks on #dbDisposeLock. if (db != null) { for (Tuple2<ColumnFamilyHandle, StateDescriptor<?, ?>> column : kvStateInformation.values()) { column.f0.close(); } kvStateInformation.clear(); db.close(); db = null; } } try { FileUtils.deleteDirectory(instanceBasePath); } catch (IOException ioex) { LOG.info("Could not delete instace base path for RocksDB: " + instanceBasePath); } }
@Test public void snapshots() throws RocksDBException { RocksDB db = null; Options options = null; ReadOptions readOptions = null; try { options = new Options(); options.setCreateIfMissing(true); db = RocksDB.open(options, dbFolder.getRoot().getAbsolutePath()); db.put("key".getBytes(), "value".getBytes()); // Get new Snapshot of database Snapshot snapshot = db.getSnapshot(); readOptions = new ReadOptions(); // set snapshot in ReadOptions readOptions.setSnapshot(snapshot); // retrieve key value pair assertThat(new String(db.get("key".getBytes()))).isEqualTo("value"); // retrieve key value pair created before // the snapshot was made assertThat(new String(db.get(readOptions, "key".getBytes()))).isEqualTo("value"); // add new key/value pair db.put("newkey".getBytes(), "newvalue".getBytes()); // using no snapshot the latest db entries // will be taken into account assertThat(new String(db.get("newkey".getBytes()))).isEqualTo("newvalue"); // snapshopot was created before newkey assertThat(db.get(readOptions, "newkey".getBytes())).isNull(); // Retrieve snapshot from read options Snapshot sameSnapshot = readOptions.snapshot(); readOptions.setSnapshot(sameSnapshot); // results must be the same with new Snapshot // instance using the same native pointer assertThat(new String(db.get(readOptions, "key".getBytes()))).isEqualTo("value"); // update key value pair to newvalue db.put("key".getBytes(), "newvalue".getBytes()); // read with previously created snapshot will // read previous version of key value pair assertThat(new String(db.get(readOptions, "key".getBytes()))).isEqualTo("value"); // read for newkey using the snapshot must be // null assertThat(db.get(readOptions, "newkey".getBytes())).isNull(); // setting null to snapshot in ReadOptions leads // to no Snapshot being used. readOptions.setSnapshot(null); assertThat(new String(db.get(readOptions, "newkey".getBytes()))).isEqualTo("newvalue"); // release Snapshot db.releaseSnapshot(snapshot); } finally { if (db != null) { db.close(); } if (options != null) { options.dispose(); } if (readOptions != null) { readOptions.dispose(); } } }