Search in sources :

Example 1 with DeleteMailbox

use of com.zimbra.cs.redolog.op.DeleteMailbox in project zm-mailbox by Zimbra.

the class Mailbox method deleteMailbox.

public void deleteMailbox(DeleteBlobs deleteBlobs) throws ServiceException {
    StoreManager sm = StoreManager.getInstance();
    boolean deleteStore = deleteBlobs == DeleteBlobs.ALWAYS || (deleteBlobs == DeleteBlobs.UNLESS_CENTRALIZED && !sm.supports(StoreFeature.CENTRALIZED));
    SpoolingCache<MailboxBlob.MailboxBlobInfo> blobs = null;
    lock.lock();
    try {
        // first, throw the mailbox into maintenance mode
        // (so anyone else with a cached reference to the Mailbox can't use it)
        MailboxMaintenance maint = null;
        try {
            maint = MailboxManager.getInstance().beginMaintenance(mData.accountId, mId);
        } catch (MailServiceException e) {
            // there may be other files that still need to be cleaned up.
            if (!MailServiceException.WRONG_MAILBOX.equals(e.getCode())) {
                throw e;
            }
        }
        DeleteMailbox redoRecorder = new DeleteMailbox(mId);
        boolean needRedo = needRedo(null, redoRecorder);
        boolean success = false;
        try {
            beginTransaction("deleteMailbox", null, redoRecorder);
            if (needRedo) {
                redoRecorder.log();
            }
            if (deleteStore && !sm.supports(StoreManager.StoreFeature.BULK_DELETE)) {
                blobs = DbMailItem.getAllBlobs(this);
            }
            try {
                // Remove the mime messages from MessageCache
                if (deleteStore) {
                    DbMailItem.visitAllBlobDigests(this, new MessageCachePurgeCallback());
                }
                // remove all the relevant entries from the database
                DbConnection conn = getOperationConnection();
                DbMailbox.clearMailboxContent(this);
                DbMailbox.deleteMailbox(conn, this);
                DbVolumeBlobs.deleteBlobRef(conn, this);
                // Remove all data related to this mailbox from memcached, so the data doesn't
                // get used by another user later by mistake if/when mailbox id gets reused.
                MemcachedCacheManager.purgeMailbox(this);
                success = true;
            } finally {
                // commit the DB transaction before touching the store!  (also ends the operation)
                endTransaction(success);
            }
            if (success) {
                // remove all traces of the mailbox from the Mailbox cache
                // (so anyone asking for the Mailbox gets NO_SUCH_MBOX or creates a fresh new empty one with a different id)
                MailboxManager.getInstance().markMailboxDeleted(this);
                // attempt to nuke the store and index
                try {
                    index.deleteIndex();
                } catch (IOException iox) {
                    ZimbraLog.store.warn("Unable to delete index data", iox);
                }
                if (deleteStore) {
                    try {
                        sm.deleteStore(this, blobs);
                    } catch (IOException iox) {
                        ZimbraLog.store.warn("Unable to delete message data", iox);
                    }
                }
            }
        } finally {
            if (needRedo) {
                if (success) {
                    redoRecorder.commit();
                } else {
                    redoRecorder.abort();
                }
            }
            if (maint != null) {
                if (success) {
                    // twiddle the mailbox lock [must be the last command of this function!]
                    // (so even *we* can't access this Mailbox going forward)
                    maint.markUnavailable();
                } else {
                    // end the maintenance if the delete is not successful.
                    MailboxManager.getInstance().endMaintenance(maint, success, true);
                }
            }
            if (blobs != null) {
                blobs.cleanup();
            }
        }
    } finally {
        lock.release();
    }
}
Also used : DeleteMailbox(com.zimbra.cs.redolog.op.DeleteMailbox) IOException(java.io.IOException) DbConnection(com.zimbra.cs.db.DbPool.DbConnection) StoreManager(com.zimbra.cs.store.StoreManager)

Aggregations

DbConnection (com.zimbra.cs.db.DbPool.DbConnection)1 DeleteMailbox (com.zimbra.cs.redolog.op.DeleteMailbox)1 StoreManager (com.zimbra.cs.store.StoreManager)1 IOException (java.io.IOException)1