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);
}
}
use of com.zimbra.cs.store.StoreManager in project zm-mailbox by Zimbra.
the class MailboxTestUtil method clearData.
/**
* Clears the database and index.
* @param zimbraServerDir the directory that contains the ZimbraServer project
*/
public static void clearData(String zimbraServerDir) throws Exception {
HSQLDB.clearDatabase(zimbraServerDir);
MailboxManager.getInstance().clearCache();
MailboxIndex.shutdown();
File index = new File("build/test/index");
if (index.isDirectory()) {
deleteDirContents(index);
}
StoreManager sm = StoreManager.getInstance();
if (sm instanceof MockStoreManager) {
((MockStoreManager) sm).purge();
} else if (sm instanceof MockHttpStoreManager) {
MockHttpStore.purge();
}
DocumentHandler.resetLocalHost();
EphemeralStore.getFactory().shutdown();
}
use of com.zimbra.cs.store.StoreManager 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);
}
}
use of com.zimbra.cs.store.StoreManager in project zm-mailbox by Zimbra.
the class Mailbox method updateChat.
public Chat updateChat(OperationContext octxt, ParsedMessage pm, int id) throws IOException, ServiceException {
if (pm == null) {
throw ServiceException.INVALID_REQUEST("null ParsedMessage when updating chat " + id + " in 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();
SaveChat redoRecorder = new SaveChat(mId, id, digest, size, -1, 0, null);
boolean success = false;
try {
beginTransaction("saveChat", octxt, redoRecorder);
SaveChat redoPlayer = (SaveChat) currentChange().getRedoPlayer();
redoRecorder.setMessageBodyInfo(new ParsedMessageDataSource(pm), size);
Chat chat = (Chat) getItemById(id, MailItem.Type.CHAT);
if (!chat.isMutable()) {
throw MailServiceException.IMMUTABLE_OBJECT(id);
}
if (!checkItemChangeID(chat)) {
throw MailServiceException.MODIFY_CONFLICT();
}
// content changed, so we're obliged to change the IMAP uid
int imapID = getNextItemId(redoPlayer == null ? ID_AUTO_INCREMENT : redoPlayer.getImapId());
redoRecorder.setImapId(imapID);
// update the content and increment the revision number
chat.setContent(staged, pm);
// NOTE: msg is now uncached (will this cause problems during commit/reindex?)
index.add(chat);
success = true;
return chat;
} finally {
endTransaction(success);
sm.quietDelete(staged);
}
}
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);
}
}
Aggregations