2011-06-02 16:36:15 +02:00
|
|
|
/* -*- mode: c++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
|
2011-05-19 06:32:09 +02:00
|
|
|
**
|
2011-03-22 22:37:01 +01:00
|
|
|
** Copyright (C) 2008-2011 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
2010-08-25 20:40:07 +02:00
|
|
|
**
|
|
|
|
** This program is free software; you can redistribute it and/or modify
|
|
|
|
** it under the terms of the GNU General Public License as published by
|
|
|
|
** the Free Software Foundation; either version 3 of the License, or
|
|
|
|
** (at your option) any later version.
|
|
|
|
**
|
|
|
|
** This program is distributed in the hope that it will be useful,
|
|
|
|
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
** GNU General Public License for more details.
|
|
|
|
**
|
|
|
|
** You should have received a copy of the GNU General Public License
|
|
|
|
** along with this program; if not, write to the Free Software Foundation,
|
|
|
|
** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <iostream>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2010-09-05 11:37:12 +02:00
|
|
|
#include <xapian.h>
|
2010-08-25 20:40:07 +02:00
|
|
|
|
|
|
|
#include "mu-util.h"
|
2011-05-19 06:32:09 +02:00
|
|
|
#include "mu-msg.h"
|
2010-08-25 20:40:07 +02:00
|
|
|
#include "mu-msg-iter.h"
|
2011-06-13 22:04:20 +02:00
|
|
|
#include "mu-msg-threader.h"
|
|
|
|
|
2011-05-18 22:33:56 +02:00
|
|
|
|
2011-06-03 07:22:56 +02:00
|
|
|
/* just a guess... */
|
|
|
|
#define MAX_FETCH_SIZE 10000
|
|
|
|
|
|
|
|
|
2011-05-18 22:33:56 +02:00
|
|
|
static gboolean update_msg (MuMsgIter *iter);
|
|
|
|
|
2010-08-25 20:40:07 +02:00
|
|
|
struct _MuMsgIter {
|
2011-06-02 16:59:49 +02:00
|
|
|
_MuMsgIter (const Xapian::Enquire &enq, size_t maxnum):
|
2011-06-13 22:04:20 +02:00
|
|
|
_enq(enq), _msg(0), _threader(0) {
|
2011-06-02 16:59:49 +02:00
|
|
|
|
|
|
|
_matches = _enq.get_mset (0, maxnum);
|
|
|
|
_cursor = _matches.begin();
|
2011-06-03 07:22:56 +02:00
|
|
|
|
|
|
|
/* this seems to make search slightly faster, some
|
|
|
|
* non-scientific testing suggests. 5-10% or so */
|
|
|
|
if (_matches.size() <= MAX_FETCH_SIZE)
|
|
|
|
_matches.fetch ();
|
2011-06-02 16:59:49 +02:00
|
|
|
|
2011-06-13 22:04:20 +02:00
|
|
|
if (!_matches.empty()) {
|
2011-06-02 16:59:49 +02:00
|
|
|
update_msg (this);
|
2011-06-13 22:04:20 +02:00
|
|
|
_threader = mu_msg_threader_new ();
|
|
|
|
mu_msg_threader_calculate (_threader, this);
|
|
|
|
}
|
2011-06-02 16:59:49 +02:00
|
|
|
}
|
2011-03-22 22:37:01 +01:00
|
|
|
|
2011-06-02 16:59:49 +02:00
|
|
|
~_MuMsgIter () {
|
|
|
|
if (_msg)
|
|
|
|
mu_msg_unref (_msg);
|
2011-06-13 22:04:20 +02:00
|
|
|
|
|
|
|
mu_msg_threader_destroy (_threader);
|
2011-06-02 16:59:49 +02:00
|
|
|
}
|
|
|
|
|
2011-06-13 22:04:20 +02:00
|
|
|
const Xapian::Enquire _enq;
|
|
|
|
Xapian::MSet _matches;
|
|
|
|
Xapian::MSet::const_iterator _cursor;
|
|
|
|
Xapian::Document _doc;
|
2011-06-02 16:59:49 +02:00
|
|
|
|
2011-06-13 22:04:20 +02:00
|
|
|
MuMsg *_msg;
|
|
|
|
MuMsgThreader *_threader;
|
2010-08-25 20:40:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MuMsgIter*
|
2011-06-02 16:36:15 +02:00
|
|
|
mu_msg_iter_new (XapianEnquire *enq, size_t maxnum)
|
2010-08-25 20:40:07 +02:00
|
|
|
{
|
2011-05-18 22:51:52 +02:00
|
|
|
g_return_val_if_fail (enq, NULL);
|
|
|
|
|
2010-08-25 20:40:07 +02:00
|
|
|
try {
|
2011-06-02 16:36:15 +02:00
|
|
|
return new MuMsgIter ((const Xapian::Enquire&)*enq, maxnum);
|
2010-08-25 20:40:07 +02:00
|
|
|
|
|
|
|
} MU_XAPIAN_CATCH_BLOCK_RETURN(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
mu_msg_iter_destroy (MuMsgIter *iter)
|
|
|
|
{
|
2011-03-22 22:37:01 +01:00
|
|
|
try { delete iter; } MU_XAPIAN_CATCH_BLOCK;
|
2010-08-25 20:40:07 +02:00
|
|
|
}
|
2011-06-13 22:04:20 +02:00
|
|
|
|
2010-08-25 20:40:07 +02:00
|
|
|
MuMsg*
|
2010-11-25 20:49:25 +01:00
|
|
|
mu_msg_iter_get_msg (MuMsgIter *iter, GError **err)
|
|
|
|
{
|
2010-08-25 20:40:07 +02:00
|
|
|
g_return_val_if_fail (iter, NULL);
|
2010-09-08 23:09:00 +02:00
|
|
|
g_return_val_if_fail (!mu_msg_iter_is_done(iter), NULL);
|
2011-05-18 22:33:56 +02:00
|
|
|
g_return_val_if_fail (iter->_msg, NULL);
|
2010-08-25 20:40:07 +02:00
|
|
|
|
2011-05-18 23:26:14 +02:00
|
|
|
return iter->_msg;
|
2010-08-25 20:40:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
message_is_readable (MuMsgIter *iter)
|
|
|
|
{
|
|
|
|
Xapian::Document doc (iter->_cursor.get_document());
|
|
|
|
const std::string path(doc.get_value(MU_MSG_FIELD_ID_PATH));
|
|
|
|
|
|
|
|
if (access (path.c_str(), R_OK) != 0) {
|
|
|
|
g_debug ("cannot read %s: %s", path.c_str(),
|
|
|
|
strerror(errno));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
2011-05-18 22:33:56 +02:00
|
|
|
|
|
|
|
static gboolean
|
|
|
|
update_msg (MuMsgIter *iter)
|
|
|
|
{
|
|
|
|
GError *err;
|
|
|
|
|
|
|
|
/* get a new MuMsg based on the current doc */
|
|
|
|
if (iter->_msg)
|
|
|
|
mu_msg_unref (iter->_msg);
|
|
|
|
|
|
|
|
iter->_doc = iter->_cursor.get_document();
|
|
|
|
err = NULL;
|
|
|
|
iter->_msg = mu_msg_new_from_doc ((XapianDocument*)&iter->_doc, &err);
|
|
|
|
if (!iter->_msg) {
|
|
|
|
g_warning ("%s: failed to create MuMsg: %s",
|
|
|
|
__FUNCTION__, err->message ? err->message : "?");
|
|
|
|
g_error_free (err);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-02 17:13:17 +02:00
|
|
|
gboolean
|
|
|
|
mu_msg_iter_reset (MuMsgIter *iter)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (iter, FALSE);
|
|
|
|
|
|
|
|
try {
|
|
|
|
iter->_cursor = iter->_matches.begin();
|
|
|
|
|
|
|
|
} MU_XAPIAN_CATCH_BLOCK_RETURN (FALSE);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-08-25 20:40:07 +02:00
|
|
|
gboolean
|
|
|
|
mu_msg_iter_next (MuMsgIter *iter)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (iter, FALSE);
|
2011-05-19 06:32:09 +02:00
|
|
|
|
2010-09-08 23:09:00 +02:00
|
|
|
if (mu_msg_iter_is_done(iter))
|
|
|
|
return FALSE;
|
2010-09-05 11:37:12 +02:00
|
|
|
|
2010-08-25 20:40:07 +02:00
|
|
|
try {
|
2011-06-02 16:36:15 +02:00
|
|
|
++iter->_cursor;
|
|
|
|
|
|
|
|
if (mu_msg_iter_is_done(iter))
|
2010-08-25 20:40:07 +02:00
|
|
|
return FALSE; /* no more matches */
|
2011-03-21 00:55:11 +01:00
|
|
|
|
2010-08-25 20:40:07 +02:00
|
|
|
/* the message may not be readable / existing, e.g.,
|
2011-05-03 07:18:08 +02:00
|
|
|
* because of the database not being fully up to
|
|
|
|
* date. in that case, we ignore the message. it
|
|
|
|
* might be nice to auto-delete these messages from
|
|
|
|
* the db, but that might screw up the search;
|
|
|
|
* also, we only have read-only access to the db
|
|
|
|
* here */
|
|
|
|
/* TODO: only mark it as such, let clients handle
|
|
|
|
* it */
|
2010-08-25 20:40:07 +02:00
|
|
|
if (!message_is_readable (iter))
|
|
|
|
return mu_msg_iter_next (iter);
|
2011-05-19 06:32:09 +02:00
|
|
|
|
2011-05-18 22:33:56 +02:00
|
|
|
/* try to get a new MuMsg based on the current doc */
|
|
|
|
return update_msg (iter);
|
2010-08-25 20:40:07 +02:00
|
|
|
|
|
|
|
} MU_XAPIAN_CATCH_BLOCK_RETURN(FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
gboolean
|
2010-09-08 23:09:00 +02:00
|
|
|
mu_msg_iter_is_done (MuMsgIter *iter)
|
2010-08-25 20:40:07 +02:00
|
|
|
{
|
2011-06-02 16:36:15 +02:00
|
|
|
g_return_val_if_fail (iter, TRUE);
|
|
|
|
|
|
|
|
try {
|
|
|
|
return iter->_cursor == iter->_matches.end() ? TRUE : FALSE;
|
|
|
|
|
|
|
|
} MU_XAPIAN_CATCH_BLOCK_RETURN (TRUE);
|
2010-08-25 20:40:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-21 00:55:11 +01:00
|
|
|
static const gchar*
|
|
|
|
get_field (MuMsgIter *iter, MuMsgFieldId mfid)
|
|
|
|
{
|
2011-05-18 22:33:56 +02:00
|
|
|
return mu_msg_get_field_string (iter->_msg, mfid);
|
2011-03-21 00:55:11 +01:00
|
|
|
}
|
|
|
|
|
2010-08-25 20:40:07 +02:00
|
|
|
const gchar*
|
2010-11-20 13:43:04 +01:00
|
|
|
mu_msg_iter_get_field (MuMsgIter *iter, MuMsgFieldId mfid)
|
2010-08-25 20:40:07 +02:00
|
|
|
{
|
2010-09-08 23:09:00 +02:00
|
|
|
g_return_val_if_fail (!mu_msg_iter_is_done(iter), NULL);
|
2010-11-20 13:43:04 +01:00
|
|
|
g_return_val_if_fail (mu_msg_field_id_is_valid(mfid), NULL);
|
2011-05-19 06:32:09 +02:00
|
|
|
|
2011-03-21 00:55:11 +01:00
|
|
|
return get_field (iter, mfid);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gint64
|
|
|
|
get_field_numeric (MuMsgIter *iter, MuMsgFieldId mfid)
|
|
|
|
{
|
2011-05-18 22:33:56 +02:00
|
|
|
return mu_msg_get_field_numeric (iter->_msg, mfid);
|
2010-08-25 20:40:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
gint64
|
2010-11-20 13:43:04 +01:00
|
|
|
mu_msg_iter_get_field_numeric (MuMsgIter *iter, MuMsgFieldId mfid)
|
2010-08-25 20:40:07 +02:00
|
|
|
{
|
2010-11-20 16:51:24 +01:00
|
|
|
g_return_val_if_fail (!mu_msg_iter_is_done(iter), -1);
|
2010-11-20 13:43:04 +01:00
|
|
|
g_return_val_if_fail (mu_msg_field_is_numeric(mfid), -1);
|
2010-09-05 11:37:12 +02:00
|
|
|
|
2011-03-21 00:55:11 +01:00
|
|
|
return get_field_numeric (iter, mfid);
|
2010-08-25 20:40:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* hmmm.... is it impossible to get a 0 docid, or just very improbable? */
|
|
|
|
unsigned int
|
|
|
|
mu_msg_iter_get_docid (MuMsgIter *iter)
|
|
|
|
{
|
2010-12-18 09:44:27 +01:00
|
|
|
g_return_val_if_fail (!mu_msg_iter_is_done(iter),
|
|
|
|
(unsigned int)-1);
|
2010-08-25 20:40:07 +02:00
|
|
|
try {
|
|
|
|
return iter->_cursor.get_document().get_docid();
|
|
|
|
|
|
|
|
} MU_XAPIAN_CATCH_BLOCK_RETURN (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|