* mu-msg-xapian.cc: plug some exception leaks; bit of cleanup

This commit is contained in:
Dirk-Jan C. Binnema 2010-01-08 20:51:25 +02:00
parent 3252ba6d5d
commit d95193d718
1 changed files with 47 additions and 63 deletions

View File

@ -23,6 +23,8 @@
#include <errno.h> #include <errno.h>
#include "xapian.h" #include "xapian.h"
#include "mu-util.h"
#include "mu-msg-xapian.h" #include "mu-msg-xapian.h"
struct _MuMsgXapian { struct _MuMsgXapian {
@ -56,15 +58,7 @@ mu_msg_xapian_new (const Xapian::Enquire& enq, size_t batchsize)
return msg; return msg;
} catch (const Xapian::Error &err) { } MU_XAPIAN_CATCH_BLOCK_RETURN(NULL);
g_warning ("%s: caught xapian exception '%s' (%s)",
__FUNCTION__, err.get_msg().c_str(),
err.get_error_string());
} catch (...) {
g_warning ("%s: caught exception", __FUNCTION__);
}
return msg;
} }
void void
@ -74,8 +68,11 @@ mu_msg_xapian_destroy (MuMsgXapian *msg)
for (int i = 0; i != MU_MSG_FIELD_ID_NUM; ++i) for (int i = 0; i != MU_MSG_FIELD_ID_NUM; ++i)
g_free (msg->_str[i]); g_free (msg->_str[i]);
delete msg->_enq; try {
delete msg; delete msg->_enq;
delete msg;
} MU_XAPIAN_CATCH_BLOCK;
} }
} }
@ -106,7 +103,7 @@ mu_msg_xapian_next (MuMsgXapian *msg)
if (++msg->_cursor == msg->_matches.end()) if (++msg->_cursor == msg->_matches.end())
return FALSE; /* no more matches */ return FALSE; /* no more matches */
/* the message may not be readable / existant, eg., because /* the message may not be readable / existing, e.g., because
* of the database not being fully up to date. in that case, * of the database not being fully up to date. in that case,
* we ignore the message. it might be nice to auto-delete * we ignore the message. it might be nice to auto-delete
* these messages from the db, but that would might screw * these messages from the db, but that would might screw
@ -120,29 +117,23 @@ mu_msg_xapian_next (MuMsgXapian *msg)
msg->_str[i] = NULL; msg->_str[i] = NULL;
} }
} catch (const Xapian::Error &err) { return TRUE;
g_warning ("%s: caught xapian exception '%s' (%s)", } MU_XAPIAN_CATCH_BLOCK_RETURN(FALSE);
__FUNCTION__, err.get_msg().c_str(),
err.get_error_string());
return FALSE;
} catch (...) {
g_warning ("%s: caught exception", __FUNCTION__);
return FALSE;
}
return TRUE;
} }
gboolean gboolean
mu_msg_xapian_is_done (MuMsgXapian *msg) mu_msg_xapian_is_done (MuMsgXapian *msg)
{ {
g_return_val_if_fail (msg, TRUE);
if (msg->_matches.empty()) if (msg->_matches.empty())
return TRUE; return TRUE;
if (msg->_cursor == msg->_matches.end()) if (msg->_cursor == msg->_matches.end())
return TRUE; return TRUE;
return FALSE; return FALSE;
} }
@ -165,114 +156,107 @@ mu_msg_xapian_get_field (MuMsgXapian *row, const MuMsgField *field)
return row->_str[id]; return row->_str[id];
} catch (const Xapian::Error &err) { } MU_XAPIAN_CATCH_BLOCK_RETURN(NULL);
g_warning ("%s: caught xapian exception '%s' (%s)",
__FUNCTION__, err.get_msg().c_str(),
err.get_error_string());
} catch (...) {
g_warning ("%s: caught exception", __FUNCTION__);
}
return NULL;
} }
gint64 gint64
mu_msg_xapian_get_field_numeric (MuMsgXapian *row, const MuMsgField *field) mu_msg_xapian_get_field_numeric (MuMsgXapian *row, const MuMsgField *field)
{ {
g_return_val_if_fail (mu_msg_field_is_numeric(field), -1); g_return_val_if_fail (mu_msg_field_is_numeric(field), -1);
return Xapian::sortable_unserialise( try {
mu_msg_xapian_get_field(row, field)); return Xapian::sortable_unserialise(
mu_msg_xapian_get_field(row, field));
} MU_XAPIAN_CATCH_BLOCK_RETURN(-1);
} }
static const gchar* static const gchar*
get_field (MuMsgXapian *row, MuMsgFieldId id) _get_field (MuMsgXapian *row, MuMsgFieldId id)
{ {
return mu_msg_xapian_get_field(row, mu_msg_field_from_id (id)); return mu_msg_xapian_get_field(row, mu_msg_field_from_id (id));
} }
static long
_get_field_number (MuMsgXapian *row, MuMsgFieldId id)
unsigned int
mu_msg_xapian_get_id (MuMsgXapian *row)
{ {
g_return_val_if_fail (row, NULL); const char* str = _get_field (row, id);
return str ? atol (str) : 0;
}
/* hmmm.... is it impossible to get a 0 docid, or just very improbable? */
unsigned int
mu_msg_xapian_get_docid (MuMsgXapian *row)
{
g_return_val_if_fail (row, 0);
try { try {
return row->_cursor.get_document().get_docid(); return row->_cursor.get_document().get_docid();
} catch (const Xapian::Error &err) { } MU_XAPIAN_CATCH_BLOCK_RETURN (0);
g_warning ("%s: caught xapian exception '%s' (%s)",
__FUNCTION__, err.get_msg().c_str(),
err.get_error_string());
} catch (...) {
g_warning ("%s: caught exception", __FUNCTION__);
}
return 0;
} }
const char* const char*
mu_msg_xapian_get_path (MuMsgXapian *row) mu_msg_xapian_get_path (MuMsgXapian *row)
{ {
return get_field (row, MU_MSG_FIELD_ID_PATH); return _get_field (row, MU_MSG_FIELD_ID_PATH);
} }
const char* const char*
mu_msg_xapian_get_from (MuMsgXapian *row) mu_msg_xapian_get_from (MuMsgXapian *row)
{ {
return get_field (row, MU_MSG_FIELD_ID_FROM); return _get_field (row, MU_MSG_FIELD_ID_FROM);
} }
const char* const char*
mu_msg_xapian_get_to (MuMsgXapian *row) mu_msg_xapian_get_to (MuMsgXapian *row)
{ {
return get_field (row, MU_MSG_FIELD_ID_TO); return _get_field (row, MU_MSG_FIELD_ID_TO);
} }
const char* const char*
mu_msg_xapian_get_cc (MuMsgXapian *row) mu_msg_xapian_get_cc (MuMsgXapian *row)
{ {
return get_field (row, MU_MSG_FIELD_ID_CC); return _get_field (row, MU_MSG_FIELD_ID_CC);
} }
const char* const char*
mu_msg_xapian_get_subject (MuMsgXapian *row) mu_msg_xapian_get_subject (MuMsgXapian *row)
{ {
return get_field (row, MU_MSG_FIELD_ID_SUBJECT); return _get_field (row, MU_MSG_FIELD_ID_SUBJECT);
} }
size_t size_t
mu_msg_xapian_get_size (MuMsgXapian *row) mu_msg_xapian_get_size (MuMsgXapian *row)
{ {
return atoi(get_field (row, MU_MSG_FIELD_ID_SIZE)); return (size_t) _get_field_number (row, MU_MSG_FIELD_ID_SIZE);
} }
time_t time_t
mu_msg_xapian_get_date (MuMsgXapian *row) mu_msg_xapian_get_date (MuMsgXapian *row)
{ {
return atoi(get_field (row, MU_MSG_FIELD_ID_DATE)); return (size_t) _get_field_number (row, MU_MSG_FIELD_ID_DATE);
} }
MuMsgFlags MuMsgFlags
mu_msg_xapian_get_flags (MuMsgXapian *row) mu_msg_xapian_get_flags (MuMsgXapian *row)
{ {
return (MuMsgFlags)atoi(get_field return (MuMsgFlags) _get_field_number (row, MU_MSG_FIELD_ID_FLAGS);
(row, MU_MSG_FIELD_ID_FLAGS));
} }
MuMsgPriority MuMsgPriority
mu_msg_xapian_get_priority (MuMsgXapian *row) mu_msg_xapian_get_priority (MuMsgXapian *row)
{ {
return (MuMsgPriority)atoi(get_field return (MuMsgPriority) _get_field_number (row, MU_MSG_FIELD_ID_PRIORITY);
(row, MU_MSG_FIELD_ID_PRIORITY));
} }