Search in sources :

Example 1 with StagedBlob

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

the class StoreManagerNegativeTest method nullLocator.

@Test
public void nullLocator() throws Exception {
    Random rand = new Random();
    byte[] bytes = new byte[10000];
    rand.nextBytes(bytes);
    StoreManager sm = StoreManager.getInstance();
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
    IncomingBlob incoming = sm.newIncomingBlob("foo", null);
    OutputStream out = incoming.getAppendingOutputStream();
    out.write(bytes);
    Blob blob = incoming.getBlob();
    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());
    try {
        mblob.getLocalBlob().getInputStream();
        Assert.fail("Expected IOException since locator is not handled correctly");
    } catch (IOException io) {
    //expected
    } finally {
        sm.delete(mblob);
    }
}
Also used : Blob(com.zimbra.cs.store.Blob) IncomingBlob(com.zimbra.cs.store.IncomingBlob) MailboxBlob(com.zimbra.cs.store.MailboxBlob) StagedBlob(com.zimbra.cs.store.StagedBlob) StagedBlob(com.zimbra.cs.store.StagedBlob) Random(java.util.Random) Mailbox(com.zimbra.cs.mailbox.Mailbox) MailboxBlob(com.zimbra.cs.store.MailboxBlob) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) IncomingBlob(com.zimbra.cs.store.IncomingBlob) StoreManager(com.zimbra.cs.store.StoreManager) Test(org.junit.Test)

Example 2 with StagedBlob

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

the class AbstractExternalStoreManagerTest method testUncachedSubstream.

@Test
public void testUncachedSubstream() throws Exception {
    ParsedMessage pm = ThreaderTest.getRootMessage();
    byte[] mimeBytes = TestUtil.readInputStream(pm.getRawInputStream());
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
    StoreManager sm = StoreManager.getInstance();
    Blob blob = sm.storeIncoming(pm.getRawInputStream());
    StagedBlob staged = sm.stage(blob, mbox);
    MailboxBlob mblob = sm.link(staged, mbox, 0, 0);
    mblob = sm.getMailboxBlob(mbox, 0, 0, staged.getLocator());
    Blob localBlob = mblob.getLocalBlob();
    InputStream stream = sm.getContent(localBlob);
    Assert.assertTrue("input stream external", stream instanceof BlobInputStream);
    if (sm instanceof ExternalStoreManager) {
        ((ExternalStoreManager) sm).clearCache();
    }
    blob.getFile().delete();
    Assert.assertFalse(blob.getFile().exists());
    //create new stream spanning the whole blob
    InputStream newStream = ((BlobInputStream) stream).newStream(0, -1);
    Assert.assertNotNull(newStream);
    Assert.assertTrue("stream content = mime content", TestUtil.bytesEqual(mimeBytes, newStream));
}
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) BlobInputStream(com.zimbra.cs.store.BlobInputStream) InputStream(java.io.InputStream) BlobInputStream(com.zimbra.cs.store.BlobInputStream) StoreManager(com.zimbra.cs.store.StoreManager) Test(org.junit.Test) AbstractStoreManagerTest(com.zimbra.cs.store.AbstractStoreManagerTest) ThreaderTest(com.zimbra.cs.mailbox.ThreaderTest)

Example 3 with StagedBlob

use of com.zimbra.cs.store.StagedBlob 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 4 with StagedBlob

use of com.zimbra.cs.store.StagedBlob 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 5 with StagedBlob

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

Aggregations

StagedBlob (com.zimbra.cs.store.StagedBlob)21 MailboxBlob (com.zimbra.cs.store.MailboxBlob)15 StoreManager (com.zimbra.cs.store.StoreManager)15 Blob (com.zimbra.cs.store.Blob)11 InputStream (java.io.InputStream)11 Mailbox (com.zimbra.cs.mailbox.Mailbox)8 ParsedMessage (com.zimbra.cs.mime.ParsedMessage)8 IOException (java.io.IOException)8 Rfc822ValidationInputStream (com.zimbra.common.mime.Rfc822ValidationInputStream)6 CopyInputStream (com.zimbra.common.util.CopyInputStream)6 TargetConstraint (com.zimbra.cs.mailbox.MailItem.TargetConstraint)6 CreateMountpoint (com.zimbra.cs.redolog.op.CreateMountpoint)6 RefreshMountpoint (com.zimbra.cs.redolog.op.RefreshMountpoint)6 Test (org.junit.Test)6 ServiceException (com.zimbra.common.service.ServiceException)3 AccountServiceException (com.zimbra.cs.account.AccountServiceException)3 DbTag (com.zimbra.cs.db.DbTag)3 NormalizedTags (com.zimbra.cs.mailbox.Tag.NormalizedTags)3 ParsedMessageDataSource (com.zimbra.cs.mime.ParsedMessageDataSource)3 AlterItemTag (com.zimbra.cs.redolog.op.AlterItemTag)3