public synchronized void deleteCollection(String name) { try { long nameDirectory_recid = getRoot(NAME_DIRECTORY_ROOT); if (nameDirectory_recid == 0) throw new IOException("Collection not found"); HTree<String, Long> dir = fetch(nameDirectory_recid); Long recid = dir.get(name); if (recid == null) throw new IOException("Collection not found"); Object o = fetch(recid); // we can not use O instance since it is not correctly initialized if (o instanceof LinkedList2) { LinkedList2 l = (LinkedList2) o; l.clear(); delete(l.rootRecid); } else if (o instanceof BTree) { ((BTree) o).clear(); } else if (o instanceof HTree) { HTree t = (HTree) o; t.clear(); HTreeDirectory n = (HTreeDirectory) fetch(t.rootRecid, t.SERIALIZER); n.deleteAllChildren(); delete(t.rootRecid); } else { throw new InternalError("unknown collection type: " + (o == null ? null : o.getClass())); } delete(recid); collections.remove(name); dir.remove(name); } catch (IOException e) { throw new IOError(e); } }
/* test this issue http://code.google.com/p/jdbm2/issues/detail?id=2 */ public void testHTreeClear() throws IOException { final DBAbstract db = newDBCache(); final HTree<String, String> tree = (HTree) db.createHashMap("name"); for (int i = 0; i < 1001; i++) { tree.put(String.valueOf(i), String.valueOf(i)); } db.commit(); System.out.println("finished adding"); tree.clear(); db.commit(); System.out.println("finished clearing"); assertTrue(tree.isEmpty()); }