Search in sources :

Example 1 with MailboxBlob

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

the class TritonBlobStoreManagerTest method sisEmpty.

@Test
public void sisEmpty() throws Exception {
    TritonBlobStoreManager sm = (TritonBlobStoreManager) StoreManager.getInstance();
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
    Assert.assertTrue("StoreManager is content addressable", sm instanceof ContentAddressableStoreManager);
    Assert.assertTrue("StoreManager supports SIS check", sm.supports(StoreFeature.SINGLE_INSTANCE_SERVER_CREATE));
    byte[] bytes = new byte[0];
    Blob blob = sm.storeIncoming(new ByteArrayInputStream(bytes));
    // blob has not yet been finalized, so it shouldn't exist in remote system yet
    byte[] hash = sm.getHash(blob);
    Assert.assertNotNull("empty blob always exists", sm.getSisBlob(hash));
    Assert.assertEquals("blob size = incoming written", bytes.length, blob.getRawSize());
    Assert.assertTrue("blob content = mime content", TestUtil.bytesEqual(bytes, blob.getInputStream()));
    StagedBlob staged = sm.stage(blob, mbox);
    Assert.assertEquals("staged size = blob size", blob.getRawSize(), staged.getSize());
    MailboxBlob mblob = sm.link(staged, mbox, 0, 0);
    Assert.assertEquals("link size = staged size", staged.getSize(), mblob.getSize());
    Assert.assertTrue("link content = mime content", TestUtil.bytesEqual(bytes, mblob.getLocalBlob().getInputStream()));
    // blob uploaded, now sis should return true and increment ref count
    Blob sisBlob = sm.getSisBlob(hash);
    Assert.assertNotNull("object created", sisBlob);
    Assert.assertEquals("blob size = incoming written", bytes.length, sisBlob.getRawSize());
    Assert.assertTrue("blob content = mime content", TestUtil.bytesEqual(bytes, sisBlob.getInputStream()));
}
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) ContentAddressableStoreManager(com.zimbra.cs.store.external.ContentAddressableStoreManager) MailboxBlob(com.zimbra.cs.store.MailboxBlob) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.Test) AbstractStoreManagerTest(com.zimbra.cs.store.AbstractStoreManagerTest)

Example 2 with MailboxBlob

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

the class TritonBlobStoreManagerTest method sis.

@Test
public void sis() throws Exception {
    TritonBlobStoreManager sm = (TritonBlobStoreManager) StoreManager.getInstance();
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
    Assert.assertTrue("StoreManager is content addressable", sm instanceof ContentAddressableStoreManager);
    Assert.assertTrue("StoreManager supports SIS check", sm.supports(StoreFeature.SINGLE_INSTANCE_SERVER_CREATE));
    Random rand = new Random();
    byte[] bytes = new byte[10000];
    rand.nextBytes(bytes);
    Blob blob = sm.storeIncoming(new ByteArrayInputStream(bytes));
    // blob has not yet been finalized, so it shouldn't exist in remote system yet
    byte[] hash = sm.getHash(blob);
    Assert.assertNull("object not yet created", sm.getSisBlob(hash));
    Assert.assertEquals("blob size = incoming written", bytes.length, blob.getRawSize());
    Assert.assertTrue("blob content = mime content", TestUtil.bytesEqual(bytes, blob.getInputStream()));
    StagedBlob staged = sm.stage(blob, mbox);
    Assert.assertEquals("staged size = blob size", blob.getRawSize(), staged.getSize());
    MailboxBlob mblob = sm.link(staged, mbox, 0, 0);
    Assert.assertEquals("link size = staged size", staged.getSize(), mblob.getSize());
    Assert.assertTrue("link content = mime content", TestUtil.bytesEqual(bytes, mblob.getLocalBlob().getInputStream()));
    // blob uploaded, now sis should return true and increment ref count
    Blob sisBlob = sm.getSisBlob(hash);
    Assert.assertNotNull("object created", sisBlob);
    Assert.assertEquals("blob size = incoming written", bytes.length, sisBlob.getRawSize());
    Assert.assertTrue("blob content = mime content", TestUtil.bytesEqual(bytes, sisBlob.getInputStream()));
    // delete once, should still exist;
    sm.delete(mblob);
    Assert.assertNotNull("object still ref'd", sm.getSisBlob(hash));
    // delete twice (once for original, once since we just did a sisCheck above)
    sm.delete(mblob);
    sm.delete(mblob);
    Assert.assertNull("object deleted", sm.getSisBlob(hash));
}
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) ContentAddressableStoreManager(com.zimbra.cs.store.external.ContentAddressableStoreManager) Random(java.util.Random) MailboxBlob(com.zimbra.cs.store.MailboxBlob) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.Test) AbstractStoreManagerTest(com.zimbra.cs.store.AbstractStoreManagerTest)

Example 3 with MailboxBlob

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

the class DbMailItem method accumulateLeafRevisions.

static void accumulateLeafRevisions(PendingDelete info, Mailbox mbox, List<Integer> versioned) throws ServiceException {
    if (versioned == null || versioned.size() == 0) {
        return;
    }
    boolean dumpsterEnabled = mbox.dumpsterEnabled();
    boolean useDumpsterForSpam = mbox.useDumpsterForSpam();
    DbConnection conn = mbox.getOperationConnection();
    StoreManager sm = StoreManager.getInstance();
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try {
        stmt = conn.prepareStatement("SELECT mi.id, mi.folder_id, rev.size, rev.mod_content, rev.locator, rev.blob_digest " + " FROM " + getMailItemTableName(mbox, "mi") + ", " + getRevisionTableName(mbox, "rev") + " WHERE mi.id = rev.item_id AND " + DbUtil.whereIn("mi.id", versioned.size()) + (DebugConfig.disableMailboxGroups ? "" : " AND mi.mailbox_id = ? AND mi.mailbox_id = rev.mailbox_id"));
        int pos = 1;
        for (int vid : versioned) {
            stmt.setInt(pos++, vid);
        }
        pos = setMailboxId(stmt, mbox, pos);
        rs = stmt.executeQuery();
        while (rs.next()) {
            Integer folderId = rs.getInt(2);
            LocationCount count = info.folderCounts.get(folderId);
            if (count == null) {
                info.folderCounts.put(folderId, new LocationCount(0, 0, rs.getLong(3)));
            } else {
                count.increment(0, 0, rs.getLong(3));
            }
            int fid = folderId != null ? folderId.intValue() : -1;
            if (!dumpsterEnabled || fid == Mailbox.ID_FOLDER_DRAFTS || (fid == Mailbox.ID_FOLDER_SPAM && !useDumpsterForSpam)) {
                String blobDigest = rs.getString(6);
                if (blobDigest != null) {
                    info.blobDigests.add(blobDigest);
                    try {
                        MailboxBlob mblob = sm.getMailboxBlob(mbox, rs.getInt(1), rs.getInt(4), rs.getString(5), false);
                        if (mblob == null) {
                            ZimbraLog.mailbox.error("missing blob for id: %d, change: %s", rs.getInt(1), rs.getInt(4));
                        } else {
                            info.blobs.add(mblob);
                        }
                    } catch (Exception e1) {
                    }
                }
            }
            long revisionSize = rs.getLong(3);
            info.size += revisionSize;
        }
    } catch (SQLException e) {
        throw ServiceException.FAILURE("getting version deletion info for items: " + versioned, e);
    } finally {
        DbPool.closeResults(rs);
        DbPool.closeStatement(stmt);
    }
}
Also used : MailboxBlob(com.zimbra.cs.store.MailboxBlob) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) DbConnection(com.zimbra.cs.db.DbPool.DbConnection) EncoderException(org.apache.commons.codec.EncoderException) DecoderException(org.apache.commons.codec.DecoderException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ServiceException(com.zimbra.common.service.ServiceException) SQLException(java.sql.SQLException) IOException(java.io.IOException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) StoreManager(com.zimbra.cs.store.StoreManager) ResultSet(java.sql.ResultSet)

Example 4 with MailboxBlob

use of com.zimbra.cs.store.MailboxBlob 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)

Example 5 with MailboxBlob

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

the class Mailbox method createChat.

public Chat createChat(OperationContext octxt, ParsedMessage pm, int folderId, int flags, String[] tags) throws IOException, ServiceException {
    if (pm == null) {
        throw ServiceException.INVALID_REQUEST("null ParsedMessage when adding chat to mailbox " + mId, null);
    }
    // write the chat content directly to the mailbox's blob staging area
    StoreManager sm = StoreManager.getInstance();
    StagedBlob staged;
    InputStream is = pm.getRawInputStream();
    try {
        staged = sm.stage(is, this);
    } finally {
        ByteUtil.closeStream(is);
    }
    String digest = staged.getDigest();
    int size = (int) staged.getSize();
    CreateChat redoRecorder = new CreateChat(mId, digest, size, folderId, flags, tags);
    boolean success = false;
    try {
        beginTransaction("createChat", octxt, redoRecorder);
        Tag.NormalizedTags ntags = new Tag.NormalizedTags(this, tags);
        CreateChat redoPlayer = (octxt == null ? null : (CreateChat) octxt.getPlayer());
        redoRecorder.setMessageBodyInfo(new ParsedMessageDataSource(pm), size);
        int itemId = getNextItemId(redoPlayer == null ? ID_AUTO_INCREMENT : redoPlayer.getMessageId());
        Chat chat = Chat.create(itemId, getFolderById(folderId), pm, staged, false, flags, ntags);
        redoRecorder.setMessageId(chat.getId());
        MailboxBlob mblob = sm.link(staged, this, itemId, getOperationChangeID());
        markOtherItemDirty(mblob);
        // when we created the Chat, we used the staged locator/size/digest;
        // make sure that data actually matches the final blob in the store
        chat.updateBlobData(mblob);
        index.add(chat);
        success = true;
        return chat;
    } 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) 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) ParsedMessageDataSource(com.zimbra.cs.mime.ParsedMessageDataSource) CreateChat(com.zimbra.cs.redolog.op.CreateChat) NormalizedTags(com.zimbra.cs.mailbox.Tag.NormalizedTags) SaveChat(com.zimbra.cs.redolog.op.SaveChat) CreateChat(com.zimbra.cs.redolog.op.CreateChat) AlterItemTag(com.zimbra.cs.redolog.op.AlterItemTag) CreateTag(com.zimbra.cs.redolog.op.CreateTag) DbTag(com.zimbra.cs.db.DbTag)

Aggregations

MailboxBlob (com.zimbra.cs.store.MailboxBlob)27 StoreManager (com.zimbra.cs.store.StoreManager)16 StagedBlob (com.zimbra.cs.store.StagedBlob)14 Mailbox (com.zimbra.cs.mailbox.Mailbox)12 Blob (com.zimbra.cs.store.Blob)12 IOException (java.io.IOException)11 ServiceException (com.zimbra.common.service.ServiceException)8 ParsedMessage (com.zimbra.cs.mime.ParsedMessage)7 Test (org.junit.Test)7 ArrayList (java.util.ArrayList)6 InputStream (java.io.InputStream)5 TargetConstraint (com.zimbra.cs.mailbox.MailItem.TargetConstraint)4 CreateMountpoint (com.zimbra.cs.redolog.op.CreateMountpoint)4 ZimbraMailItem (com.zimbra.common.mailbox.ZimbraMailItem)3 AccountServiceException (com.zimbra.cs.account.AccountServiceException)3 DbMailItem (com.zimbra.cs.db.DbMailItem)3 DbTag (com.zimbra.cs.db.DbTag)3 IndexDocument (com.zimbra.cs.index.IndexDocument)3 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)3 Message (com.zimbra.cs.mailbox.Message)3