final CollectionTrigger colTrigger = new CollectionTriggers(this, parent);
colTrigger.beforeDeleteCollection(this, transaction, collection);
final long start = System.currentTimeMillis();
final CollectionCache collectionsCache = pool.getCollectionsCache();
synchronized(collectionsCache) {
final XmldbURI uri = collection.getURI();
final String collName = uri.getRawCollectionPath();
// Notify the collection configuration manager
final CollectionConfigurationManager manager = pool.getConfigurationManager();
if(manager != null) {
manager.invalidate(uri, getBrokerPool());
}
if(LOG.isDebugEnabled()) {
LOG.debug("Removing children collections from their parent '" + collName + "'...");
}
for(final Iterator<XmldbURI> i = collection.collectionIterator(this); i.hasNext(); ) {
final XmldbURI childName = i.next();
//TODO : resolve from collection's base URI
//TODO : resolve URIs !!! (uri.resolve(childName))
final Collection childCollection = openCollection(uri.append(childName), Lock.WRITE_LOCK);
try {
removeCollection(transaction, childCollection);
} catch(NullPointerException npe) {
LOG.error("childCollection '" + childName + "' is corrupted. Caught NPE to be able to actually remove the parent.");
} finally {
if(childCollection != null) {
childCollection.getLock().release(Lock.WRITE_LOCK);
} else {
LOG.warn("childCollection is null !");
}
}
}
//Drop all index entries
notifyDropIndex(collection);
// Drop custom indexes
indexController.removeCollection(collection, this, false);
if(!isRoot) {
// remove from parent collection
//TODO : resolve URIs ! (uri.resolve(".."))
final Collection parentCollection = openCollection(collection.getParentURI(), Lock.WRITE_LOCK);
// keep the lock for the transaction
if(transaction != null) {
transaction.registerLock(parentCollection.getLock(), Lock.WRITE_LOCK);
}
if(parentCollection != null) {
try {
LOG.debug("Removing collection '" + collName + "' from its parent...");
//TODO : resolve from collection's base URI
parentCollection.removeCollection(this, uri.lastSegment());
saveCollection(transaction, parentCollection);
} catch(final LockException e) {
LOG.warn("LockException while removing collection '" + collName + "'");
} finally {
if(transaction == null) {
parentCollection.getLock().release(Lock.WRITE_LOCK);
}
}
}
}
//Update current state
final Lock lock = collectionsDb.getLock();
try {
lock.acquire(Lock.WRITE_LOCK);
// remove the metadata of all documents in the collection
final Value docKey = new CollectionStore.DocumentKey(collection.getId());
final IndexQuery query = new IndexQuery(IndexQuery.TRUNC_RIGHT, docKey);
collectionsDb.removeAll(transaction, query);
// if this is not the root collection remove it...
if(!isRoot) {
final Value key = new CollectionStore.CollectionKey(collName);
//... from the disk
collectionsDb.remove(transaction, key);
//... from the cache
collectionsCache.remove(collection);
//and free its id for any further use
collectionsDb.freeCollectionId(collection.getId());
} else {
//Simply save the collection on disk
//It will remain cached