*/
protected MessageMetaData save(Mailbox<UUID> mailbox, Message<UUID> message) throws MailboxException {
HTable messages = null;
HTable mailboxes = null;
BufferedInputStream in = null;
ChunkOutputStream out = null;
try {
//TODO: update the mailbox information about messages
messages = new HTable(conf, MESSAGES_TABLE);
mailboxes = new HTable(conf, MAILBOXES_TABLE);
//save the message metadata
Put put = metadataToPut(message);
messages.put(put);
//save the message content
//TODO: current implementation is crude.
int b;
out = new ChunkOutputStream(conf,
MESSAGES_TABLE, MESSAGE_DATA_BODY_CF, messageRowKey(message), MAX_COLUMN_SIZE);
in = new BufferedInputStream(message.getBodyContent());
while ((b = in.read()) != -1) {
out.write(b);
}
in.close();
out.close();
out = new ChunkOutputStream(conf,
MESSAGES_TABLE, MESSAGE_DATA_HEADERS_CF, messageRowKey(message), MAX_COLUMN_SIZE);
in = new BufferedInputStream(message.getHeaderContent());
while ((b = in.read()) != -1) {
out.write(b);
}
in.close();
out.close();
// increase the message count for the current mailbox
mailboxes.incrementColumnValue(mailboxRowKey(mailbox.getMailboxId()), MAILBOX_CF, MAILBOX_MESSAGE_COUNT, 1);
return new SimpleMessageMetaData(message);
} catch (IOException ex) {
throw new MailboxException("Error setting flags for messages in " + mailbox, ex);
} finally {
if (messages != null) {
try {
messages.close();
} catch (IOException ex) {
throw new MailboxException("Error closing table " + messages, ex);
}
}
if (mailboxes != null) {
try {
mailboxes.close();
} catch (IOException ex) {
throw new MailboxException("Error closing table " + mailboxes, ex);
}
}
if (in != null) {
try {
in.close();
} catch (IOException ex) {
throw new MailboxException("Error closing Inputtream", ex);
}
}
if (out != null) {
try {
out.close();
} catch (IOException ex) {
throw new MailboxException("Error closing OutputStream", ex);
}
}
}