public synchronized void truncateClass(Transaction txn, Class entityClass) throws DatabaseException { checkOpen(); /* Close primary and secondary databases. */ closeClass(entityClass); String clsName = entityClass.getName(); EntityMetadata entityMeta = checkEntityClass(clsName); /* * Truncate the primary first and let any exceptions propogate * upwards. Then truncate each secondary, only throwing the first * exception. */ String dbName = storePrefix + clsName; boolean primaryExists = true; try { env.truncateDatabase(txn, dbName, false); } catch (DatabaseNotFoundException ignored) { primaryExists = false; } if (primaryExists) { DatabaseException firstException = null; for (SecondaryKeyMetadata keyMeta : entityMeta.getSecondaryKeys().values()) { try { env.truncateDatabase( txn, storePrefix + makeSecName(clsName, keyMeta.getKeyName()), false); } catch (DatabaseNotFoundException ignored) { /* Ignore secondaries that do not exist. */ } catch (DatabaseException e) { if (firstException == null) { firstException = e; } } } if (firstException != null) { throw firstException; } } }
@Test public void testCleanAfterDbTruncate() { open(true /*runCleaner*/); writeFiles(5 /*nActive*/, 0 /*nObsolete*/); expectNothingToClean(); db.close(); db = null; env.truncateDatabase(null, DB_NAME, false); expectBackgroundCleaning(); close(); }