if (readOnly) throw new PermissionDeniedException(DATABASE_IS_READ_ONLY); try { if (!name.startsWith(ROOT_COLLECTION)) name = ROOT_COLLECTION + name;  Collection collection = getCollection(name); if (collection == null) { LOG.debug("collection " + name + " not found!"); return false; } if (!collection.getPermissions().validate(user, Permission.WRITE)) throw new PermissionDeniedException("not allowed to remove collection");  String childCollection; LOG.debug("removing sub-collections"); for (Iterator i = collection.collectionIterator(); i.hasNext();) { childCollection = (String) i.next(); removeCollection( (name.equals("/") ? "/" + childCollection : name + "/" + childCollection)); } Lock lock = collectionsDb.getLock(); try { lock.acquire(Lock.WRITE_LOCK);   if (name.equals(ROOT_COLLECTION)) saveCollection(collection); else { Value key; try { key = new Value(name.getBytes("UTF-8")); } catch (UnsupportedEncodingException uee) { key = new Value(name.getBytes()); } collectionsDb.remove(key); } if (!name.equals(ROOT_COLLECTION)) collectionsDb.getCollectionCache().remove(collection); Collection parent = collection.getParent(this); if (parent != null) { parent.removeCollection(name.substring(name.lastIndexOf("/") + 1)); saveCollection(parent); } } catch (LockException e) { LOG.warn("Failed to acquire lock on collections.dbx"); } finally { lock.release(); } freeCollection(collection.getId());  textEngine.dropIndex(collection);  LOG.debug("removing elements ..."); short collectionId = collection.getId();  Value ref = new ElementValue(collectionId); IndexQuery query = new IndexQuery(IndexQuery.TRUNC_RIGHT, ref); lock = elementsDb.getLock(); try { lock.acquire(Lock.WRITE_LOCK); elementsDb.removeAll(query); } catch (LockException e) { LOG.error("could not acquire lock on elements index", e); } finally { lock.release(); }  LOG.debug("removing dom nodes ..."); for (Iterator i = collection.iterator(); i.hasNext();) { final DocumentImpl doc = (DocumentImpl) i.next(); LOG.debug("removing document " + doc.getFileName()); new DOMTransaction(this, domDb, Lock.WRITE_LOCK) { public Object start() { if(doc.getResourceType() == DocumentImpl.BINARY_FILE) { domDb.remove(doc.getAddress()); domDb.removeOverflowValue(((BinaryDocument)doc).getPage()); } else { NodeImpl node = (NodeImpl)doc.getFirstChild(); domDb.removeAll(node.getInternalAddress()); } return null; } } .run(); new DOMTransaction(this, domDb, Lock.WRITE_LOCK) { public Object start() { try { Value ref = new NodeRef(doc.getDocId()); IndexQuery query = new IndexQuery(IndexQuery.TRUNC_RIGHT, ref); domDb.remove(query, null); domDb.flush(); } catch (BTreeException e) { LOG.warn("btree error while removing document", e); } catch (DBException e) { LOG.warn("db error while removing document", e); } catch (IOException e) { LOG.warn("io error while removing document", e); } catch (TerminatedException e) { LOG.warn("method terminated", e); } return null; } } .run(); freeDocument(doc.getDocId()); } return true; } catch (IOException ioe) { LOG.warn(ioe); } catch (BTreeException bte) { LOG.warn(bte); } catch (ReadOnlyException e) { LOG.warn(DATABASE_IS_READ_ONLY); } return false;
if (readOnly) throw new PermissionDeniedException(DATABASE_IS_READ_ONLY); if (!name.startsWith(ROOT_COLLECTION)) name = ROOT_COLLECTION + name;  Collection collection = getCollection(name); if (collection == null) { LOG.debug("collection " + name + " not found!"); return false; } if (!collection.getPermissions().validate(user, Permission.WRITE)) throw new PermissionDeniedException("not allowed to remove collection");  String childCollection; LOG.debug("removing sub-collections"); for (Iterator i = collection.collectionIterator(); i.hasNext();) { childCollection = (String) i.next(); removeCollection( (name.equals("/") ? "/" + childCollection : name + "/" + childCollection)); } Lock lock = collectionsDb.getLock(); try { lock.acquire(Lock.WRITE_LOCK);   if (name.equals(ROOT_COLLECTION)) saveCollection(collection); else { Value key; try { key = new Value(name.getBytes("UTF-8")); } catch (UnsupportedEncodingException uee) { key = new Value(name.getBytes()); } collectionsDb.remove(key); } if (!name.equals(ROOT_COLLECTION)) collectionsDb.getCollectionCache().remove(collection); Collection parent = collection.getParent(this); if (parent != null) { parent.removeCollection(name.substring(name.lastIndexOf("/") + 1)); saveCollection(parent); } } catch (LockException e) { LOG.warn("Failed to acquire lock on collections.dbx"); } catch (ReadOnlyException e) { throw new PermissionDeniedException(DATABASE_IS_READ_ONLY); } finally { lock.release(); } freeCollection(collection.getId());  textEngine.dropIndex(collection); elementIndex.dropIndex(collection);  LOG.debug("removing dom nodes ..."); for (Iterator i = collection.iterator(); i.hasNext();) { final DocumentImpl doc = (DocumentImpl) i.next(); LOG.debug("removing document " + doc.getFileName()); new DOMTransaction(this, domDb, Lock.WRITE_LOCK) { public Object start() { if(doc.getResourceType() == DocumentImpl.BINARY_FILE) { domDb.remove(doc.getAddress()); domDb.removeOverflowValue(((BinaryDocument)doc).getPage()); } else { NodeImpl node = (NodeImpl)doc.getFirstChild(); domDb.removeAll(node.getInternalAddress()); } return null; } } .run(); new DOMTransaction(this, domDb, Lock.WRITE_LOCK) { public Object start() { try { Value ref = new NodeRef(doc.getDocId()); IndexQuery query = new IndexQuery(IndexQuery.TRUNC_RIGHT, ref); domDb.remove(query, null); domDb.flush(); } catch (BTreeException e) { LOG.warn("btree error while removing document", e); } catch (DBException e) { LOG.warn("db error while removing document", e); } catch (IOException e) { LOG.warn("io error while removing document", e); } catch (TerminatedException e) { LOG.warn("method terminated", e); } return null; } } .run(); freeDocument(doc.getDocId()); } return true;
	public boolean removeCollection(String name) throws PermissionDeniedException {		if (readOnly)			throw new PermissionDeniedException(DATABASE_IS_READ_ONLY);		try {			if (!name.startsWith(ROOT_COLLECTION))				name = ROOT_COLLECTION + name;			Collection collection = getCollection(name);			if (collection == null) {				LOG.debug("collection " + name + " not found!");				return false;			}			if (!collection.getPermissions().validate(user, Permission.WRITE))				throw new PermissionDeniedException("not allowed to remove collection");			String childCollection;			LOG.debug("removing sub-collections");			for (Iterator i = collection.collectionIterator(); i.hasNext();) {				childCollection = (String) i.next();				removeCollection(					(name.equals("/")						? "/" + childCollection						: name + "/" + childCollection));			}			Lock lock = collectionsDb.getLock();			try {				lock.acquire(Lock.WRITE_LOCK);								// if this is not the root collection remove it completely				if (name.equals(ROOT_COLLECTION))					saveCollection(collection);				else {					Value key;					try {						key = new Value(name.getBytes("UTF-8"));					} catch (UnsupportedEncodingException uee) {						key = new Value(name.getBytes());					}						collectionsDb.remove(key);				}				if (!name.equals(ROOT_COLLECTION))					collectionsDb.getCollectionCache().remove(collection);				Collection parent = collection.getParent(this);				if (parent != null) {					parent.removeCollection(name.substring(name.lastIndexOf("/") + 1));					saveCollection(parent);				}			} catch (LockException e) {				LOG.warn("Failed to acquire lock on collections.dbx");			} finally {				lock.release();			}			freeCollection(collection.getId());						textEngine.dropIndex(collection);			LOG.debug("removing elements ...");			short collectionId = collection.getId();			Value ref = new ElementValue(collectionId);			IndexQuery query = new IndexQuery(IndexQuery.TRUNC_RIGHT, ref);			lock = elementsDb.getLock();			try {				lock.acquire(Lock.WRITE_LOCK);				elementsDb.removeAll(query);			} catch (LockException e) {				LOG.error("could not acquire lock on elements index", e);			} finally {				lock.release();			}			LOG.debug("removing dom nodes ...");			for (Iterator i = collection.iterator(); i.hasNext();) {				final DocumentImpl doc = (DocumentImpl) i.next();				LOG.debug("removing document " + doc.getFileName());				new DOMTransaction(this, domDb, Lock.WRITE_LOCK) {					public Object start() {						if(doc.getResourceType() == DocumentImpl.BINARY_FILE) {							domDb.remove(doc.getAddress());							domDb.removeOverflowValue(((BinaryDocument)doc).getPage());						} else {							NodeImpl node = (NodeImpl)doc.getFirstChild();							domDb.removeAll(node.getInternalAddress());						}						return null;					}				}				.run();				new DOMTransaction(this, domDb, Lock.WRITE_LOCK) {					public Object start() {						try {							Value ref = new NodeRef(doc.getDocId());							IndexQuery query =								new IndexQuery(IndexQuery.TRUNC_RIGHT, ref);							domDb.remove(query, null);							domDb.flush();						} catch (BTreeException e) {							LOG.warn("btree error while removing document", e);						} catch (DBException e) {							LOG.warn("db error while removing document", e);						} catch (IOException e) {							LOG.warn("io error while removing document", e);						} catch (TerminatedException e) {                            LOG.warn("method terminated", e);                        }						return null;					}				}				.run();				freeDocument(doc.getDocId());			}			return true;		} catch (IOException ioe) {			LOG.warn(ioe);		} catch (BTreeException bte) {			LOG.warn(bte);		} catch (ReadOnlyException e) {			LOG.warn(DATABASE_IS_READ_ONLY);		}		return false;	}