2011-08-29 22:38:55 +02:00
|
|
|
/*
|
2017-10-24 21:57:57 +02:00
|
|
|
** Copyright (C) 2008-2017 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
2009-11-25 21:55:06 +01: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.
|
2011-08-29 22:38:55 +02:00
|
|
|
**
|
2009-11-25 21:55:06 +01:00
|
|
|
** 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.
|
2011-08-29 22:38:55 +02:00
|
|
|
**
|
2009-11-25 21:55:06 +01:00
|
|
|
** You should have received a copy of the GNU General Public License
|
|
|
|
** along with this program; if not, write to the Free Software Foundation,
|
2011-08-29 22:38:55 +02:00
|
|
|
** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
**
|
2009-11-25 21:55:06 +01:00
|
|
|
*/
|
|
|
|
|
2010-11-22 23:33:15 +01:00
|
|
|
#include <stdexcept>
|
|
|
|
#include <string>
|
|
|
|
#include <cctype>
|
|
|
|
#include <cstring>
|
2017-10-24 21:57:57 +02:00
|
|
|
#include <sstream>
|
2010-11-22 23:33:15 +01:00
|
|
|
|
2017-10-24 21:57:57 +02:00
|
|
|
#include <stdlib.h>
|
2009-11-25 21:55:06 +01:00
|
|
|
#include <xapian.h>
|
|
|
|
#include <glib/gstdio.h>
|
|
|
|
|
2010-08-25 20:46:16 +02:00
|
|
|
#include "mu-query.h"
|
2010-11-22 23:33:15 +01:00
|
|
|
#include "mu-msg-fields.h"
|
2009-11-25 21:55:06 +01:00
|
|
|
|
2010-08-25 20:29:53 +02:00
|
|
|
#include "mu-msg-iter.h"
|
2009-11-25 21:55:06 +01:00
|
|
|
|
2010-01-05 07:32:23 +01:00
|
|
|
#include "mu-util.h"
|
2010-11-22 23:44:18 +01:00
|
|
|
#include "mu-str.h"
|
2011-07-20 23:38:00 +02:00
|
|
|
#include "mu-date.h"
|
2010-01-05 07:32:23 +01:00
|
|
|
|
2017-10-24 21:57:57 +02:00
|
|
|
#include <parser/proc-iface.hh>
|
|
|
|
#include <parser/utils.hh>
|
|
|
|
#include <parser/xapian.hh>
|
2010-11-22 23:33:15 +01:00
|
|
|
|
2017-10-24 21:57:57 +02:00
|
|
|
struct MuProc: public Mux::ProcIface {
|
2010-11-22 23:33:15 +01:00
|
|
|
|
2017-10-24 21:57:57 +02:00
|
|
|
MuProc (const Xapian::Database& db): db_{db} {}
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2017-10-24 21:57:57 +02:00
|
|
|
static MuMsgFieldId field_id (const std::string& field) {
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2017-10-24 21:57:57 +02:00
|
|
|
if (field.empty())
|
|
|
|
return MU_MSG_FIELD_ID_NONE;
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2017-10-24 21:57:57 +02:00
|
|
|
MuMsgFieldId id = mu_msg_field_id_from_name (field.c_str(), FALSE);
|
|
|
|
if (id != MU_MSG_FIELD_ID_NONE)
|
|
|
|
return id;
|
|
|
|
else
|
|
|
|
return mu_msg_field_id_from_shortcut (field[0], FALSE);
|
2010-11-22 23:33:15 +01:00
|
|
|
}
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2017-10-24 21:57:57 +02:00
|
|
|
std::string
|
|
|
|
process_value (const std::string& field,
|
|
|
|
const std::string& value) const override {
|
|
|
|
const auto id = field_id (field);
|
|
|
|
if (id == MU_MSG_FIELD_ID_NONE)
|
|
|
|
return value;
|
|
|
|
switch(id) {
|
|
|
|
case MU_MSG_FIELD_ID_PRIO: {
|
|
|
|
if (!value.empty())
|
|
|
|
return std::string(1, value[0]);
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case MU_MSG_FIELD_ID_FLAGS: {
|
|
|
|
const auto flag = mu_flag_char_from_name (value.c_str());
|
|
|
|
if (flag)
|
|
|
|
return std::string(1, tolower(flag));
|
|
|
|
} break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2013-06-03 21:29:50 +02:00
|
|
|
}
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2017-10-24 21:57:57 +02:00
|
|
|
return value; // XXX prio/flags, etc. alias
|
2011-07-20 23:38:00 +02:00
|
|
|
}
|
|
|
|
|
2017-10-24 21:57:57 +02:00
|
|
|
void add_field (std::vector<FieldInfo>& fields, MuMsgFieldId id) const {
|
2011-07-20 23:38:00 +02:00
|
|
|
|
2017-10-24 21:57:57 +02:00
|
|
|
const auto shortcut = mu_msg_field_shortcut(id);
|
|
|
|
if (!shortcut)
|
|
|
|
return; // can't be searched
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2017-10-24 21:57:57 +02:00
|
|
|
const auto name = mu_msg_field_name (id);
|
|
|
|
const auto pfx = mu_msg_field_xapian_prefix (id);
|
2010-11-21 17:12:01 +01:00
|
|
|
|
2017-10-24 21:57:57 +02:00
|
|
|
if (!name || !pfx)
|
|
|
|
return;
|
2010-11-22 23:33:15 +01:00
|
|
|
|
2017-10-27 17:43:33 +02:00
|
|
|
fields.push_back ({{name}, {pfx},
|
2017-10-30 07:23:51 +01:00
|
|
|
(bool)mu_msg_field_xapian_index(id),
|
2017-10-27 17:43:33 +02:00
|
|
|
id});
|
2011-01-06 15:21:09 +01:00
|
|
|
}
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2017-10-24 21:57:57 +02:00
|
|
|
std::vector<FieldInfo>
|
|
|
|
process_field (const std::string& field) const override {
|
|
|
|
|
|
|
|
std::vector<FieldInfo> fields;
|
|
|
|
|
|
|
|
if (field == "contact" || field == "recip") { // multi fields
|
|
|
|
add_field (fields, MU_MSG_FIELD_ID_TO);
|
|
|
|
add_field (fields, MU_MSG_FIELD_ID_CC);
|
|
|
|
add_field (fields, MU_MSG_FIELD_ID_BCC);
|
|
|
|
if (field == "contact")
|
|
|
|
add_field (fields, MU_MSG_FIELD_ID_FROM);
|
|
|
|
} else if (field == "") {
|
|
|
|
add_field (fields, MU_MSG_FIELD_ID_TO);
|
|
|
|
add_field (fields, MU_MSG_FIELD_ID_CC);
|
|
|
|
add_field (fields, MU_MSG_FIELD_ID_BCC);
|
|
|
|
add_field (fields, MU_MSG_FIELD_ID_FROM);
|
|
|
|
add_field (fields, MU_MSG_FIELD_ID_SUBJECT);
|
|
|
|
add_field (fields, MU_MSG_FIELD_ID_BODY_TEXT);
|
|
|
|
} else {
|
|
|
|
const auto id = field_id (field.c_str());
|
|
|
|
if (id != MU_MSG_FIELD_ID_NONE)
|
|
|
|
add_field (fields, id);
|
|
|
|
}
|
2011-05-21 08:14:02 +02:00
|
|
|
|
2017-10-24 21:57:57 +02:00
|
|
|
return fields;
|
2011-01-06 15:21:09 +01:00
|
|
|
}
|
2017-10-24 21:57:57 +02:00
|
|
|
|
|
|
|
bool is_range_field (const std::string& field) const override {
|
|
|
|
const auto id = field_id (field.c_str());
|
|
|
|
if (id == MU_MSG_FIELD_ID_NONE)
|
2011-08-29 22:38:55 +02:00
|
|
|
return false;
|
2017-10-24 21:57:57 +02:00
|
|
|
else
|
|
|
|
return mu_msg_field_is_range_field (id);
|
2011-01-06 15:21:09 +01:00
|
|
|
}
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2017-10-24 21:57:57 +02:00
|
|
|
Range process_range (const std::string& field, const std::string& lower,
|
|
|
|
const std::string& upper) const override {
|
2011-01-06 15:21:09 +01:00
|
|
|
|
2017-10-24 21:57:57 +02:00
|
|
|
const auto id = field_id (field.c_str());
|
|
|
|
if (id == MU_MSG_FIELD_ID_NONE)
|
|
|
|
return { lower, upper };
|
2011-01-06 15:21:09 +01:00
|
|
|
|
2017-10-24 21:57:57 +02:00
|
|
|
std::string l2 = lower;
|
|
|
|
std::string u2 = upper;
|
2011-01-06 15:21:09 +01:00
|
|
|
|
2017-10-24 21:57:57 +02:00
|
|
|
if (id == MU_MSG_FIELD_ID_DATE) {
|
|
|
|
l2 = Mux::date_to_time_t_string (lower, true);
|
|
|
|
u2 = Mux::date_to_time_t_string (upper, false);
|
|
|
|
} else if (id == MU_MSG_FIELD_ID_SIZE) {
|
|
|
|
l2 = Mux::size_to_string (lower, true);
|
|
|
|
u2 = Mux::size_to_string (upper, false);
|
|
|
|
}
|
2009-12-05 19:10:58 +01:00
|
|
|
|
2017-10-24 21:57:57 +02:00
|
|
|
return { l2, u2 };
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string>
|
|
|
|
process_regex (const std::string& field, const std::regex& rx) const override {
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2017-10-24 21:57:57 +02:00
|
|
|
const auto id = field_id (field.c_str());
|
|
|
|
if (id == MU_MSG_FIELD_ID_NONE)
|
|
|
|
return {};
|
2011-03-22 22:37:01 +01:00
|
|
|
|
2017-10-24 21:57:57 +02:00
|
|
|
char pfx[] = { mu_msg_field_xapian_prefix(id), '\0' };
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2017-10-24 21:57:57 +02:00
|
|
|
std::vector<std::string> terms;
|
|
|
|
for (auto it = db_.allterms_begin(pfx); it != db_.allterms_end(pfx); ++it) {
|
|
|
|
if (std::regex_search((*it).c_str() + 1, rx)) // avoid copy
|
|
|
|
terms.push_back(*it);
|
|
|
|
}
|
2012-10-17 17:35:23 +02:00
|
|
|
|
2017-10-24 21:57:57 +02:00
|
|
|
return terms;
|
2011-03-22 22:37:01 +01:00
|
|
|
}
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2017-10-24 21:57:57 +02:00
|
|
|
const Xapian::Database& db_;
|
|
|
|
};
|
|
|
|
|
2017-10-24 21:58:32 +02:00
|
|
|
struct _MuQuery {
|
2017-10-24 21:57:57 +02:00
|
|
|
public:
|
|
|
|
_MuQuery (MuStore *store): _store(mu_store_ref(store)) {}
|
2011-08-30 21:02:08 +02:00
|
|
|
~_MuQuery () { mu_store_unref (_store); }
|
|
|
|
|
2011-09-03 09:49:51 +02:00
|
|
|
Xapian::Database& db() const {
|
2017-10-24 21:57:57 +02:00
|
|
|
const auto db = reinterpret_cast<Xapian::Database*>
|
2011-09-03 09:49:51 +02:00
|
|
|
(mu_store_get_read_only_database (_store));
|
|
|
|
if (!db)
|
|
|
|
throw std::runtime_error ("no database");
|
|
|
|
return *db;
|
|
|
|
}
|
2011-08-30 21:02:08 +02:00
|
|
|
private:
|
|
|
|
MuStore *_store;
|
2011-03-22 22:37:01 +01:00
|
|
|
};
|
2009-12-08 23:01:49 +01:00
|
|
|
|
2011-07-13 07:07:52 +02:00
|
|
|
static const Xapian::Query
|
|
|
|
get_query (MuQuery *mqx, const char* searchexpr, GError **err)
|
|
|
|
{
|
2009-12-08 23:01:49 +01:00
|
|
|
try {
|
2017-10-24 21:57:57 +02:00
|
|
|
Mux::WarningVec warns;
|
|
|
|
const auto tree = Mux::parse (searchexpr, warns,
|
|
|
|
std::make_unique<MuProc>(mqx->db()));
|
|
|
|
for (const auto w: warns)
|
|
|
|
std::cerr << w << std::endl;
|
|
|
|
|
|
|
|
return Mux::xapian_query (tree);
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2011-07-13 07:07:52 +02:00
|
|
|
} catch (...) {
|
2012-04-13 21:03:23 +02:00
|
|
|
mu_util_g_set_error (err,MU_ERROR_XAPIAN_QUERY,
|
|
|
|
"parse error in query");
|
2011-07-13 07:07:52 +02:00
|
|
|
throw;
|
|
|
|
}
|
2009-12-08 23:01:49 +01:00
|
|
|
}
|
2009-11-25 21:55:06 +01:00
|
|
|
|
2010-08-25 20:46:16 +02:00
|
|
|
MuQuery*
|
2011-08-30 21:02:08 +02:00
|
|
|
mu_query_new (MuStore *store, GError **err)
|
2009-11-25 21:55:06 +01:00
|
|
|
{
|
2011-08-30 21:02:08 +02:00
|
|
|
g_return_val_if_fail (store, NULL);
|
2011-03-22 22:37:01 +01:00
|
|
|
|
2011-09-03 09:49:51 +02:00
|
|
|
if (mu_store_count (store, err) == 0) {
|
2012-04-01 12:08:02 +02:00
|
|
|
g_set_error (err, MU_ERROR_DOMAIN, MU_ERROR_XAPIAN_IS_EMPTY,
|
2011-09-03 09:49:51 +02:00
|
|
|
"database is empty");
|
|
|
|
return 0;
|
|
|
|
}
|
2011-08-29 22:38:55 +02:00
|
|
|
|
|
|
|
try {
|
2011-08-30 21:02:08 +02:00
|
|
|
return new MuQuery (store);
|
2012-06-12 10:55:59 +02:00
|
|
|
} MU_XAPIAN_CATCH_BLOCK_G_ERROR_RETURN (err, MU_ERROR_XAPIAN, 0);
|
2012-10-23 19:29:24 +02:00
|
|
|
|
2011-08-29 22:38:55 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-11-25 21:55:06 +01:00
|
|
|
void
|
2010-08-25 20:46:16 +02:00
|
|
|
mu_query_destroy (MuQuery *self)
|
2009-11-25 21:55:06 +01:00
|
|
|
{
|
2011-03-22 22:37:01 +01:00
|
|
|
try { delete self; } MU_XAPIAN_CATCH_BLOCK;
|
2010-09-12 15:30:29 +02:00
|
|
|
}
|
2009-12-11 18:44:05 +01:00
|
|
|
|
2010-09-12 15:30:29 +02:00
|
|
|
|
2012-08-19 08:57:49 +02:00
|
|
|
/* this function is for handling the case where a DatabaseModified
|
|
|
|
* exception is raised. We try to reopen the database, and run the
|
|
|
|
* query again. */
|
|
|
|
static MuMsgIter *
|
2012-12-16 14:08:34 +01:00
|
|
|
try_requery (MuQuery *self, const char* searchexpr, MuMsgFieldId sortfieldid,
|
|
|
|
int maxnum, MuQueryFlags flags, GError **err)
|
2012-08-19 08:57:49 +02:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
/* let's assume that infinite regression is
|
|
|
|
* impossible */
|
|
|
|
self->db().reopen();
|
|
|
|
MU_WRITE_LOG ("reopening db after modification");
|
2012-12-16 14:08:34 +01:00
|
|
|
return mu_query_run (self, searchexpr, sortfieldid,
|
|
|
|
maxnum, flags, err);
|
2012-08-19 08:57:49 +02:00
|
|
|
|
|
|
|
} MU_XAPIAN_CATCH_BLOCK_G_ERROR_RETURN (err, MU_ERROR_XAPIAN, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-25 16:34:24 +01:00
|
|
|
static MuMsgIterFlags
|
|
|
|
msg_iter_flags (MuQueryFlags flags)
|
|
|
|
{
|
|
|
|
MuMsgIterFlags iflags;
|
|
|
|
|
|
|
|
iflags = MU_MSG_ITER_FLAG_NONE;
|
|
|
|
|
|
|
|
if (flags & MU_QUERY_FLAG_DESCENDING)
|
|
|
|
iflags |= MU_MSG_ITER_FLAG_DESCENDING;
|
|
|
|
if (flags & MU_QUERY_FLAG_SKIP_UNREADABLE)
|
|
|
|
iflags |= MU_MSG_ITER_FLAG_SKIP_UNREADABLE;
|
|
|
|
if (flags & MU_QUERY_FLAG_SKIP_DUPS)
|
|
|
|
iflags |= MU_MSG_ITER_FLAG_SKIP_DUPS;
|
2012-12-27 10:09:05 +01:00
|
|
|
if (flags & MU_QUERY_FLAG_THREADS)
|
|
|
|
iflags |= MU_MSG_ITER_FLAG_THREADS;
|
2012-12-25 16:34:24 +01:00
|
|
|
|
|
|
|
return iflags;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-30 23:22:18 +02:00
|
|
|
static Xapian::Enquire
|
2012-12-16 14:08:34 +01:00
|
|
|
get_enquire (MuQuery *self, const char *searchexpr, MuMsgFieldId sortfieldid,
|
2012-12-25 16:34:24 +01:00
|
|
|
bool descending, GError **err)
|
2012-08-30 23:22:18 +02:00
|
|
|
{
|
|
|
|
Xapian::Enquire enq (self->db());
|
|
|
|
|
2013-09-07 08:43:33 +02:00
|
|
|
try {
|
|
|
|
/* empty or "" means "matchall" */
|
|
|
|
if (!mu_str_is_empty(searchexpr) &&
|
|
|
|
g_strcmp0 (searchexpr, "\"\"") != 0) /* NULL or "" or """" */
|
|
|
|
enq.set_query(get_query (self, searchexpr, err));
|
|
|
|
else
|
|
|
|
enq.set_query(Xapian::Query::MatchAll);
|
|
|
|
} catch (...) {
|
|
|
|
mu_util_g_set_error (err, MU_ERROR_XAPIAN_QUERY,
|
|
|
|
"parse error in query");
|
|
|
|
throw;
|
|
|
|
}
|
2012-08-30 23:22:18 +02:00
|
|
|
|
|
|
|
enq.set_cutoff(0,0);
|
|
|
|
return enq;
|
|
|
|
}
|
|
|
|
|
2012-12-25 16:34:24 +01:00
|
|
|
/*
|
2012-12-28 13:48:00 +01:00
|
|
|
* record all threadids for the messages; also 'orig_set' receives all
|
|
|
|
* original matches (a map msgid-->docid), so we can make sure the
|
|
|
|
* originals are not seen as 'duplicates' later (when skipping
|
|
|
|
* duplicates). We want to favor the originals over the related
|
|
|
|
* messages, when skipping duplicates.
|
2012-12-25 16:34:24 +01:00
|
|
|
*/
|
|
|
|
static GHashTable*
|
2012-12-28 13:48:00 +01:00
|
|
|
get_thread_ids (MuMsgIter *iter, GHashTable **orig_set)
|
2012-12-25 16:34:24 +01:00
|
|
|
{
|
|
|
|
GHashTable *ids;
|
2016-12-11 17:33:31 +01:00
|
|
|
|
2012-12-28 13:48:00 +01:00
|
|
|
ids = g_hash_table_new_full (g_str_hash, g_str_equal,
|
|
|
|
(GDestroyNotify)g_free, NULL);
|
|
|
|
*orig_set = g_hash_table_new_full (g_str_hash, g_str_equal,
|
|
|
|
(GDestroyNotify)g_free, NULL);
|
2012-12-25 16:34:24 +01:00
|
|
|
|
|
|
|
while (!mu_msg_iter_is_done (iter)) {
|
2015-03-05 23:12:34 +01:00
|
|
|
char *thread_id, *msgid;
|
|
|
|
unsigned docid;
|
2012-12-28 13:48:00 +01:00
|
|
|
/* record the thread id for the message */
|
2012-12-25 16:34:24 +01:00
|
|
|
if ((thread_id = mu_msg_iter_get_thread_id (iter)))
|
2015-03-05 23:12:34 +01:00
|
|
|
g_hash_table_insert (ids, thread_id,
|
2012-12-25 16:34:24 +01:00
|
|
|
GSIZE_TO_POINTER(TRUE));
|
2012-12-28 13:48:00 +01:00
|
|
|
/* record the original set */
|
|
|
|
docid = mu_msg_iter_get_docid(iter);
|
|
|
|
if (docid != 0 && (msgid = mu_msg_iter_get_msgid (iter)))
|
2015-03-05 23:12:34 +01:00
|
|
|
g_hash_table_insert (*orig_set, msgid,
|
2012-12-28 13:48:00 +01:00
|
|
|
GSIZE_TO_POINTER(docid));
|
|
|
|
|
2012-12-25 16:34:24 +01:00
|
|
|
if (!mu_msg_iter_next (iter))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ids;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Xapian::Query
|
2012-12-28 13:48:00 +01:00
|
|
|
get_related_query (MuMsgIter *iter, GHashTable **orig_set)
|
2012-12-16 14:08:34 +01:00
|
|
|
{
|
2012-12-25 16:34:24 +01:00
|
|
|
GHashTable *hash;
|
|
|
|
GList *id_list, *cur;
|
2012-12-26 09:12:25 +01:00
|
|
|
std::vector<Xapian::Query> qvec;
|
2012-12-25 16:34:24 +01:00
|
|
|
static std::string pfx (1, mu_msg_field_xapian_prefix
|
|
|
|
(MU_MSG_FIELD_ID_THREAD_ID));
|
|
|
|
|
2012-12-28 13:48:00 +01:00
|
|
|
/* orig_set receives the hash msgid->docid of the set of
|
|
|
|
* original matches */
|
|
|
|
hash = get_thread_ids (iter, orig_set);
|
2012-12-25 16:34:24 +01:00
|
|
|
/* id_list now gets a list of all thread-ids seen in the query
|
|
|
|
* results; either in the Message-Id field or in
|
|
|
|
* References. */
|
|
|
|
id_list = g_hash_table_get_keys (hash);
|
|
|
|
|
2012-12-26 09:12:25 +01:00
|
|
|
// now, we create a vector with queries for each of the
|
|
|
|
// thread-ids, which we combine below. This is /much/ faster
|
|
|
|
// than creating the query as 'query = Query (OR, query)'...
|
|
|
|
for (cur = id_list; cur; cur = g_list_next(cur))
|
2013-07-03 20:56:42 +02:00
|
|
|
qvec.push_back (Xapian::Query((std::string
|
|
|
|
(pfx + (char*)cur->data))));
|
2012-12-16 14:08:34 +01:00
|
|
|
|
2012-12-25 16:34:24 +01:00
|
|
|
g_hash_table_destroy (hash);
|
|
|
|
g_list_free (id_list);
|
2012-12-16 14:08:34 +01:00
|
|
|
|
2012-12-26 09:12:25 +01:00
|
|
|
return Xapian::Query (Xapian::Query::OP_OR, qvec.begin(), qvec.end());
|
2012-12-25 16:34:24 +01:00
|
|
|
}
|
2012-12-16 14:08:34 +01:00
|
|
|
|
2012-12-25 16:34:24 +01:00
|
|
|
|
|
|
|
static void
|
|
|
|
include_related (MuQuery *self, MuMsgIter **iter, int maxnum,
|
|
|
|
MuMsgFieldId sortfieldid, MuQueryFlags flags)
|
|
|
|
{
|
2012-12-28 13:48:00 +01:00
|
|
|
GHashTable *orig_set;
|
2012-12-25 16:34:24 +01:00
|
|
|
Xapian::Enquire enq (self->db());
|
|
|
|
MuMsgIter *rel_iter;
|
|
|
|
|
2012-12-28 13:48:00 +01:00
|
|
|
orig_set = NULL;
|
|
|
|
enq.set_query(get_related_query (*iter, &orig_set));
|
2012-12-25 16:34:24 +01:00
|
|
|
enq.set_cutoff(0,0);
|
|
|
|
|
|
|
|
rel_iter= mu_msg_iter_new (
|
|
|
|
reinterpret_cast<XapianEnquire*>(&enq),
|
|
|
|
maxnum,
|
|
|
|
sortfieldid,
|
|
|
|
msg_iter_flags (flags),
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
mu_msg_iter_destroy (*iter);
|
2012-12-28 13:48:00 +01:00
|
|
|
|
2013-05-04 13:14:58 +02:00
|
|
|
// set the preferred set for the iterator (ie., the set of
|
|
|
|
// messages not considered to be duplicates) to be the
|
|
|
|
// original matches -- the matches without considering
|
|
|
|
// 'related'
|
2012-12-28 13:48:00 +01:00
|
|
|
mu_msg_iter_set_preferred (rel_iter, orig_set);
|
|
|
|
g_hash_table_destroy (orig_set);
|
|
|
|
|
2012-12-25 16:34:24 +01:00
|
|
|
*iter = rel_iter;
|
2012-12-16 14:08:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-25 20:29:53 +02:00
|
|
|
MuMsgIter*
|
2012-12-25 16:34:24 +01:00
|
|
|
mu_query_run (MuQuery *self, const char *searchexpr, MuMsgFieldId sortfieldid,
|
2012-12-16 14:08:34 +01:00
|
|
|
int maxnum, MuQueryFlags flags, GError **err)
|
2009-11-25 21:55:06 +01:00
|
|
|
{
|
|
|
|
g_return_val_if_fail (self, NULL);
|
2011-08-29 22:38:55 +02:00
|
|
|
g_return_val_if_fail (searchexpr, NULL);
|
2013-09-07 08:43:33 +02:00
|
|
|
g_return_val_if_fail (mu_msg_field_id_is_valid (sortfieldid) ||
|
|
|
|
sortfieldid == MU_MSG_FIELD_ID_NONE,
|
2010-11-23 22:52:37 +01:00
|
|
|
NULL);
|
2010-01-23 23:51:17 +01:00
|
|
|
try {
|
2013-09-07 08:43:33 +02:00
|
|
|
MuMsgIter *iter;
|
|
|
|
MuQueryFlags first_flags;
|
|
|
|
bool inc_related = flags & MU_QUERY_FLAG_INCLUDE_RELATED;
|
|
|
|
bool descending = flags & MU_QUERY_FLAG_DESCENDING;
|
2012-12-20 22:37:04 +01:00
|
|
|
Xapian::Enquire enq (get_enquire(self, searchexpr, sortfieldid,
|
2012-12-25 16:34:24 +01:00
|
|
|
descending, err));
|
2012-10-17 16:59:33 +02:00
|
|
|
|
2012-12-27 10:09:05 +01:00
|
|
|
/* when we're doing a 'include-related query', we're
|
|
|
|
* actually doing /two/ queries; one to get the
|
|
|
|
* initial matches, and based on that one to get all
|
|
|
|
* messages in threads in those matches.
|
|
|
|
*/
|
|
|
|
|
2012-10-17 16:59:33 +02:00
|
|
|
/* get the 'real' maxnum if it was specified as < 0 */
|
2012-12-27 10:09:05 +01:00
|
|
|
maxnum = maxnum < 0 ? self->db().get_doccount() : maxnum;
|
|
|
|
/* if we do a include-related query, it's wasted
|
|
|
|
* effort to calculate threads already in the first
|
|
|
|
* query since we can do it in the second one
|
|
|
|
*/
|
2017-10-24 21:57:57 +02:00
|
|
|
if (inc_related)
|
|
|
|
first_flags = (MuQueryFlags)(flags & ~MU_QUERY_FLAG_THREADS);
|
|
|
|
else
|
|
|
|
first_flags = flags;
|
|
|
|
|
2012-12-25 16:34:24 +01:00
|
|
|
iter = mu_msg_iter_new (
|
2012-03-13 23:07:29 +01:00
|
|
|
reinterpret_cast<XapianEnquire*>(&enq),
|
2012-12-16 13:18:22 +01:00
|
|
|
maxnum,
|
2012-12-27 15:00:13 +01:00
|
|
|
/* with inc_related, we do the sorting in the
|
|
|
|
* second query
|
|
|
|
*/
|
|
|
|
inc_related ? MU_MSG_FIELD_ID_NONE : sortfieldid,
|
2012-12-27 10:09:05 +01:00
|
|
|
msg_iter_flags (first_flags),
|
2012-12-16 13:18:22 +01:00
|
|
|
err);
|
2012-12-25 16:34:24 +01:00
|
|
|
/*
|
|
|
|
* if we want related messages, do a second query,
|
|
|
|
* based on the message ids / refs of the first one
|
|
|
|
* */
|
2012-12-27 10:09:05 +01:00
|
|
|
if (inc_related)
|
2013-07-03 20:56:42 +02:00
|
|
|
include_related (self, &iter, maxnum, sortfieldid,
|
|
|
|
flags);
|
2012-08-19 08:57:49 +02:00
|
|
|
|
|
|
|
if (err && *err && (*err)->code == MU_ERROR_XAPIAN_MODIFIED) {
|
|
|
|
g_clear_error (err);
|
2012-12-16 14:08:34 +01:00
|
|
|
return try_requery (self, searchexpr, sortfieldid,
|
|
|
|
maxnum, flags, err);
|
2012-08-19 08:57:49 +02:00
|
|
|
} else
|
|
|
|
return iter;
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2011-09-12 19:48:30 +02:00
|
|
|
} MU_XAPIAN_CATCH_BLOCK_G_ERROR_RETURN (err, MU_ERROR_XAPIAN, 0);
|
2009-11-25 21:55:06 +01:00
|
|
|
}
|
|
|
|
|
2011-09-12 19:48:30 +02:00
|
|
|
|
2009-11-25 21:55:06 +01:00
|
|
|
char*
|
2017-10-24 21:57:57 +02:00
|
|
|
mu_query_internal_xapian (MuQuery *self, const char *searchexpr, GError **err)
|
2009-11-25 21:55:06 +01:00
|
|
|
{
|
|
|
|
g_return_val_if_fail (self, NULL);
|
|
|
|
g_return_val_if_fail (searchexpr, NULL);
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2009-11-25 21:55:06 +01:00
|
|
|
try {
|
2011-08-29 22:38:55 +02:00
|
|
|
Xapian::Query query (get_query(self, searchexpr, err));
|
2010-11-23 23:47:36 +01:00
|
|
|
return g_strdup(query.get_description().c_str());
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2010-02-03 20:06:31 +01:00
|
|
|
} MU_XAPIAN_CATCH_BLOCK_RETURN(NULL);
|
2009-12-05 19:10:58 +01:00
|
|
|
}
|
2017-10-24 21:57:57 +02:00
|
|
|
|
|
|
|
|
|
|
|
char*
|
|
|
|
mu_query_internal (MuQuery *self, const char *searchexpr,
|
|
|
|
gboolean warn, GError **err)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (self, NULL);
|
|
|
|
g_return_val_if_fail (searchexpr, NULL);
|
|
|
|
|
|
|
|
try {
|
|
|
|
Mux::WarningVec warns;
|
|
|
|
const auto tree = Mux::parse (searchexpr, warns,
|
|
|
|
std::make_unique<MuProc>(self->db()));
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << tree;
|
|
|
|
|
|
|
|
if (warn) {
|
|
|
|
for (const auto w: warns)
|
|
|
|
std::cerr << w << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
return g_strdup(ss.str().c_str());
|
|
|
|
|
|
|
|
} MU_XAPIAN_CATCH_BLOCK_RETURN(NULL);
|
|
|
|
}
|