Search in sources :

Example 6 with StoreManager

use of com.zimbra.cs.store.StoreManager in project zm-mailbox by Zimbra.

the class Mailbox method modifyContact.

public void modifyContact(OperationContext octxt, int contactId, ParsedContact pc) throws ServiceException {
    StoreManager sm = StoreManager.getInstance();
    StagedBlob staged = null;
    if (pc.hasAttachment()) {
        // write the contact content directly to the mailbox's blob staging area
        InputStream is = null;
        try {
            staged = sm.stage(is = pc.getContentStream(), pc.getSize(), this);
        } catch (IOException ioe) {
            throw ServiceException.FAILURE("could not save contact blob", ioe);
        } finally {
            ByteUtil.closeStream(is);
        }
    }
    ModifyContact redoRecorder = new ModifyContact(mId, contactId, pc);
    boolean success = false;
    try {
        beginTransaction("modifyContact", octxt, redoRecorder);
        Contact con = getContactById(contactId);
        if (!checkItemChangeID(con)) {
            throw MailServiceException.MODIFY_CONFLICT();
        }
        try {
            // setContent() calls reanalyze(), which also updates the contact fields even when there is no blob
            con.setContent(staged, pc);
        } catch (IOException ioe) {
            throw ServiceException.FAILURE("could not save contact blob", ioe);
        }
        index.add(con);
        success = true;
    } finally {
        endTransaction(success);
        sm.quietDelete(staged);
    }
}
Also used : ModifyContact(com.zimbra.cs.redolog.op.ModifyContact) StagedBlob(com.zimbra.cs.store.StagedBlob) Rfc822ValidationInputStream(com.zimbra.common.mime.Rfc822ValidationInputStream) CopyInputStream(com.zimbra.common.util.CopyInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) StoreManager(com.zimbra.cs.store.StoreManager) ModifyContact(com.zimbra.cs.redolog.op.ModifyContact) ParsedContact(com.zimbra.cs.mime.ParsedContact) CreateContact(com.zimbra.cs.redolog.op.CreateContact)

Example 7 with StoreManager

use of com.zimbra.cs.store.StoreManager in project zm-mailbox by Zimbra.

the class VerifyStoreManager method testStore.

private void testStore() throws Exception {
    ParsedMessage pm = getMessage(1024);
    byte[] mimeBytes = readInputStream(pm.getRawInputStream());
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(AccountTestUtil.getAccount(USER_NAME));
    StoreManager sm = StoreManager.getInstance();
    Blob blob = sm.storeIncoming(pm.getRawInputStream());
    assertEquals("blob size = message size", pm.getRawData().length, blob.getRawSize());
    assertTrue("blob content = mime content", bytesEqual(mimeBytes, readInputStream(blob.getInputStream())));
    StagedBlob staged = sm.stage(blob, mbox);
    assertEquals("staged size = blob size", blob.getRawSize(), staged.getSize());
    MailboxBlob mblob = sm.link(staged, mbox, 0, 0);
    assertEquals("link size = staged size", staged.getSize(), mblob.getSize());
    assertTrue("link content = mime content", bytesEqual(mimeBytes, readInputStream(mblob.getLocalBlob().getInputStream())));
    mblob = sm.getMailboxBlob(mbox, 0, 0, staged.getLocator());
    assertEquals("mblob size = staged size", staged.getSize(), mblob.getSize());
    assertTrue("mailboxblob content = mime content", bytesEqual(mimeBytes, readInputStream(mblob.getLocalBlob().getInputStream())));
    sm.delete(mblob);
}
Also used : Blob(com.zimbra.cs.store.Blob) MailboxBlob(com.zimbra.cs.store.MailboxBlob) StagedBlob(com.zimbra.cs.store.StagedBlob) StagedBlob(com.zimbra.cs.store.StagedBlob) Mailbox(com.zimbra.cs.mailbox.Mailbox) MailboxBlob(com.zimbra.cs.store.MailboxBlob) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) StoreManager(com.zimbra.cs.store.StoreManager)

Example 8 with StoreManager

use of com.zimbra.cs.store.StoreManager in project zm-mailbox by Zimbra.

the class BlobConsistencyChecker method check.

public Results check(Collection<Short> volumeIds, int mboxId, boolean checkSize, boolean reportUsedBlobs) throws ServiceException {
    StoreManager sm = StoreManager.getInstance();
    if (!(sm instanceof FileBlobStore)) {
        throw ServiceException.INVALID_REQUEST(sm.getClass().getSimpleName() + " is not supported", null);
    }
    mailboxId = mboxId;
    this.checkSize = checkSize;
    this.reportUsedBlobs = reportUsedBlobs;
    results = new Results();
    Mailbox mbox = MailboxManager.getInstance().getMailboxById(mailboxId);
    DbConnection conn = null;
    try {
        conn = DbPool.getConnection();
        for (short volumeId : volumeIds) {
            Volume vol = VolumeManager.getInstance().getVolume(volumeId);
            if (vol.getType() == Volume.TYPE_INDEX) {
                log.warn("Skipping index volume %d.  Only message volumes are supported.", vol.getId());
                continue;
            }
            int numGroups = 1 << vol.getFileGroupBits();
            int filesPerGroup = 1 << vol.getFileBits();
            // Maximum id for the entire mailbox
            int mailboxMaxId = DbBlobConsistency.getMaxId(conn, mbox);
            // Iterate group directories one at a time, looking up all item id's
            // that have blobs in each directory.  Each group can have multiple blocks
            // of id's if we wrap from group 255 back to group 0.
            // Minimum id for the current block
            int minId = 0;
            // Current group number
            int group = 0;
            int maxId = 0;
            while (minId <= mailboxMaxId && group < numGroups) {
                // We used Multimap to make sure we store multiple BlobInfo objects for the same itemId
                // multiple BlobInfo objects are created when there are multiple revisions of the same file
                Multimap<Integer, BlobInfo> blobsById = HashMultimap.create();
                String blobDir = vol.getBlobDir(mbox.getId(), minId);
                while (minId <= mailboxMaxId) {
                    // Maximum id for the current block
                    maxId = minId + filesPerGroup - 1;
                    for (BlobInfo blob : DbBlobConsistency.getBlobInfo(conn, mbox, minId, maxId, volumeId)) {
                        blobsById.put(blob.itemId, blob);
                    }
                    minId += (numGroups * filesPerGroup);
                }
                try {
                    check(volumeId, blobDir, blobsById);
                } catch (IOException e) {
                    throw ServiceException.FAILURE("Unable to check " + blobDir, e);
                }
                group++;
                // Set minId to the smallest id in the next group
                minId = group * filesPerGroup;
            }
        }
    } finally {
        DbPool.quietClose(conn);
    }
    return results;
}
Also used : IOException(java.io.IOException) DbConnection(com.zimbra.cs.db.DbPool.DbConnection) StoreManager(com.zimbra.cs.store.StoreManager) Mailbox(com.zimbra.cs.mailbox.Mailbox) Volume(com.zimbra.cs.volume.Volume)

Example 9 with StoreManager

use of com.zimbra.cs.store.StoreManager in project zm-mailbox by Zimbra.

the class Mailbox method createDocument.

public Document createDocument(OperationContext octxt, int folderId, ParsedDocument pd, MailItem.Type type, int flags, MailItem parent, CustomMetadata custom, boolean indexing) throws IOException, ServiceException {
    StoreManager sm = StoreManager.getInstance();
    StagedBlob staged = sm.stage(pd.getBlob(), this);
    SaveDocument redoRecorder = new SaveDocument(mId, pd.getDigest(), pd.getSize(), folderId, flags);
    boolean success = false;
    try {
        long start = System.currentTimeMillis();
        beginTransaction("createDoc", octxt, redoRecorder);
        SaveDocument redoPlayer = (octxt == null ? null : (SaveDocument) octxt.getPlayer());
        int itemId = getNextItemId(redoPlayer == null ? ID_AUTO_INCREMENT : redoPlayer.getMessageId());
        String uuid = redoPlayer == null ? UUIDUtil.generateUUID() : redoPlayer.getUuid();
        Document doc;
        switch(type) {
            case DOCUMENT:
                doc = Document.create(itemId, uuid, getFolderById(folderId), pd.getFilename(), pd.getContentType(), pd, custom, flags, parent);
                break;
            case WIKI:
                doc = WikiItem.create(itemId, uuid, getFolderById(folderId), pd.getFilename(), pd, null);
                break;
            default:
                throw MailServiceException.INVALID_TYPE(type.toString());
        }
        redoRecorder.setMessageId(itemId);
        redoRecorder.setUuid(doc.getUuid());
        redoRecorder.setDocument(pd);
        redoRecorder.setItemType(type);
        redoRecorder.setDescription(pd.getDescription());
        redoRecorder.setFlags(doc.getFlagBitmask());
        // Get the redolog data from the mailbox blob.  This is less than ideal in the
        // HTTP store case because it will result in network access, and possibly an
        // extra write to local disk.  If this becomes a problem, we should update the
        // ParsedDocument constructor to take a DataSource instead of an InputStream.
        MailboxBlob mailboxBlob = doc.setContent(staged, pd);
        redoRecorder.setMessageBodyInfo(new MailboxBlobDataSource(mailboxBlob), mailboxBlob.getSize());
        if (indexing) {
            index.add(doc);
        }
        success = true;
        long elapsed = System.currentTimeMillis() - start;
        ZimbraLog.mailbox.debug("createDocument elapsed=" + elapsed);
        return doc;
    } catch (IOException ioe) {
        throw ServiceException.FAILURE("error writing document blob", ioe);
    } finally {
        endTransaction(success);
        sm.quietDelete(staged);
    }
}
Also used : StagedBlob(com.zimbra.cs.store.StagedBlob) MailboxBlob(com.zimbra.cs.store.MailboxBlob) MailboxBlobDataSource(com.zimbra.cs.store.MailboxBlobDataSource) SaveDocument(com.zimbra.cs.redolog.op.SaveDocument) IOException(java.io.IOException) ParsedDocument(com.zimbra.cs.mime.ParsedDocument) IndexDocument(com.zimbra.cs.index.IndexDocument) SaveDocument(com.zimbra.cs.redolog.op.SaveDocument) RefreshMountpoint(com.zimbra.cs.redolog.op.RefreshMountpoint) TargetConstraint(com.zimbra.cs.mailbox.MailItem.TargetConstraint) CreateMountpoint(com.zimbra.cs.redolog.op.CreateMountpoint) StoreManager(com.zimbra.cs.store.StoreManager)

Example 10 with StoreManager

use of com.zimbra.cs.store.StoreManager in project zm-mailbox by Zimbra.

the class Mailbox method createContact.

public Contact createContact(OperationContext octxt, ParsedContact pc, int folderId, String[] tags) throws ServiceException {
    StoreManager sm = StoreManager.getInstance();
    StagedBlob staged = null;
    if (pc.hasAttachment()) {
        // write the contact content directly to the mailbox's blob staging area
        InputStream is = null;
        try {
            staged = sm.stage(is = pc.getContentStream(), (int) pc.getSize(), this);
        } catch (IOException ioe) {
            throw ServiceException.FAILURE("could not save contact blob", ioe);
        } finally {
            ByteUtil.closeStream(is);
        }
    }
    CreateContact redoRecorder = new CreateContact(mId, folderId, pc, tags);
    boolean success = false;
    try {
        beginTransaction("createContact", octxt, redoRecorder);
        CreateContact redoPlayer = (CreateContact) currentChange().getRedoPlayer();
        boolean isRedo = redoPlayer != null;
        Tag.NormalizedTags ntags = new Tag.NormalizedTags(this, tags);
        int contactId = getNextItemId(isRedo ? redoPlayer.getContactId() : ID_AUTO_INCREMENT);
        MailboxBlob mblob = null;
        if (pc.hasAttachment()) {
            try {
                mblob = sm.renameTo(staged, this, contactId, getOperationChangeID());
                markOtherItemDirty(mblob);
            } catch (IOException ioe) {
                throw ServiceException.FAILURE("could not save contact blob", ioe);
            }
        }
        int flags = 0;
        Contact con = Contact.create(contactId, getFolderById(folderId), mblob, pc, flags, ntags, null);
        redoRecorder.setContactId(contactId);
        index.add(con);
        success = true;
        return con;
    } finally {
        endTransaction(success);
        sm.quietDelete(staged);
    }
}
Also used : StagedBlob(com.zimbra.cs.store.StagedBlob) NormalizedTags(com.zimbra.cs.mailbox.Tag.NormalizedTags) MailboxBlob(com.zimbra.cs.store.MailboxBlob) Rfc822ValidationInputStream(com.zimbra.common.mime.Rfc822ValidationInputStream) CopyInputStream(com.zimbra.common.util.CopyInputStream) InputStream(java.io.InputStream) NormalizedTags(com.zimbra.cs.mailbox.Tag.NormalizedTags) CreateContact(com.zimbra.cs.redolog.op.CreateContact) IOException(java.io.IOException) AlterItemTag(com.zimbra.cs.redolog.op.AlterItemTag) CreateTag(com.zimbra.cs.redolog.op.CreateTag) DbTag(com.zimbra.cs.db.DbTag) RefreshMountpoint(com.zimbra.cs.redolog.op.RefreshMountpoint) TargetConstraint(com.zimbra.cs.mailbox.MailItem.TargetConstraint) CreateMountpoint(com.zimbra.cs.redolog.op.CreateMountpoint) StoreManager(com.zimbra.cs.store.StoreManager) ModifyContact(com.zimbra.cs.redolog.op.ModifyContact) ParsedContact(com.zimbra.cs.mime.ParsedContact) CreateContact(com.zimbra.cs.redolog.op.CreateContact)

Aggregations

StoreManager (com.zimbra.cs.store.StoreManager)29 MailboxBlob (com.zimbra.cs.store.MailboxBlob)17 StagedBlob (com.zimbra.cs.store.StagedBlob)16 IOException (java.io.IOException)14 InputStream (java.io.InputStream)11 Blob (com.zimbra.cs.store.Blob)10 Mailbox (com.zimbra.cs.mailbox.Mailbox)9 ParsedMessage (com.zimbra.cs.mime.ParsedMessage)9 Rfc822ValidationInputStream (com.zimbra.common.mime.Rfc822ValidationInputStream)6 ServiceException (com.zimbra.common.service.ServiceException)6 CopyInputStream (com.zimbra.common.util.CopyInputStream)6 TargetConstraint (com.zimbra.cs.mailbox.MailItem.TargetConstraint)5 CreateMountpoint (com.zimbra.cs.redolog.op.CreateMountpoint)5 Test (org.junit.Test)5 DbConnection (com.zimbra.cs.db.DbPool.DbConnection)4 RefreshMountpoint (com.zimbra.cs.redolog.op.RefreshMountpoint)4 ArrayList (java.util.ArrayList)4 AccountServiceException (com.zimbra.cs.account.AccountServiceException)3 DbMailItem (com.zimbra.cs.db.DbMailItem)3 ParsedMessageDataSource (com.zimbra.cs.mime.ParsedMessageDataSource)3