2011-08-29 22:38:55 +02:00
|
|
|
/*
|
2011-01-04 22:44:18 +01:00
|
|
|
** Copyright (C) 2008-2011 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>
|
2009-11-25 21:55:06 +01:00
|
|
|
#include <stdlib.h>
|
2010-11-22 23:33:15 +01:00
|
|
|
|
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
|
|
|
|
2010-11-22 23:33:15 +01:00
|
|
|
/*
|
2010-11-23 21:06:57 +01:00
|
|
|
* custom parser for date ranges
|
|
|
|
*/
|
2011-07-20 23:38:00 +02:00
|
|
|
class MuDateRangeProcessor : public Xapian::StringValueRangeProcessor {
|
2010-11-22 23:33:15 +01:00
|
|
|
public:
|
2011-07-20 23:38:00 +02:00
|
|
|
MuDateRangeProcessor():
|
|
|
|
Xapian::StringValueRangeProcessor(
|
|
|
|
(Xapian::valueno)MU_MSG_FIELD_ID_DATE) {}
|
2010-11-22 23:33:15 +01:00
|
|
|
|
|
|
|
Xapian::valueno operator()(std::string &begin, std::string &end) {
|
|
|
|
|
|
|
|
if (!clear_prefix (begin))
|
|
|
|
return Xapian::BAD_VALUENO;
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2011-07-20 23:38:00 +02:00
|
|
|
begin = to_sortable (begin, true);
|
|
|
|
end = to_sortable (end, false);
|
2011-08-29 22:38:55 +02:00
|
|
|
|
|
|
|
if (begin > end)
|
2011-07-21 20:58:27 +02:00
|
|
|
throw Xapian::QueryParserError
|
|
|
|
("end time is before begin");
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2011-07-20 23:38:00 +02:00
|
|
|
return (Xapian::valueno)MU_MSG_FIELD_ID_DATE;
|
2010-11-22 23:33:15 +01:00
|
|
|
}
|
|
|
|
private:
|
2011-07-20 23:38:00 +02:00
|
|
|
std::string to_sortable (std::string& s, bool is_begin) {
|
|
|
|
|
|
|
|
const char* str;
|
|
|
|
time_t t;
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2011-07-20 23:38:00 +02:00
|
|
|
str = mu_date_interpret_s (s.c_str(), is_begin ? TRUE: FALSE);
|
|
|
|
str = mu_date_complete_s (str, is_begin ? TRUE: FALSE);
|
2011-07-21 20:58:27 +02:00
|
|
|
t = mu_date_str_to_time_t (str, TRUE /*local*/);
|
2011-07-20 23:38:00 +02:00
|
|
|
str = mu_date_time_t_to_str (t, FALSE /*UTC*/);
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2011-07-20 23:38:00 +02:00
|
|
|
return s = std::string(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-22 23:33:15 +01:00
|
|
|
bool clear_prefix (std::string& begin) {
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2010-11-22 23:33:15 +01:00
|
|
|
const std::string colon (":");
|
2010-11-24 22:30:41 +01:00
|
|
|
const std::string name (mu_msg_field_name
|
|
|
|
(MU_MSG_FIELD_ID_DATE) + colon);
|
2010-11-22 23:33:15 +01:00
|
|
|
const std::string shortcut (
|
|
|
|
std::string(1, mu_msg_field_shortcut
|
|
|
|
(MU_MSG_FIELD_ID_DATE)) + colon);
|
|
|
|
|
|
|
|
if (begin.find (name) == 0) {
|
|
|
|
begin.erase (0, name.length());
|
|
|
|
return true;
|
|
|
|
} else if (begin.find (shortcut) == 0) {
|
|
|
|
begin.erase (0, shortcut.length());
|
|
|
|
return true;
|
|
|
|
} else
|
2011-08-29 22:38:55 +02:00
|
|
|
return false;
|
2010-11-22 23:33:15 +01:00
|
|
|
}
|
2011-07-21 20:58:27 +02:00
|
|
|
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2010-11-21 17:12:01 +01:00
|
|
|
};
|
|
|
|
|
2010-11-22 23:33:15 +01:00
|
|
|
|
2011-01-06 15:21:09 +01:00
|
|
|
class MuSizeRangeProcessor : public Xapian::NumberValueRangeProcessor {
|
|
|
|
public:
|
2011-03-22 22:37:01 +01:00
|
|
|
MuSizeRangeProcessor():
|
|
|
|
Xapian::NumberValueRangeProcessor(MU_MSG_FIELD_ID_SIZE) {
|
2011-01-06 15:21:09 +01:00
|
|
|
}
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2011-01-06 15:21:09 +01:00
|
|
|
Xapian::valueno operator()(std::string &begin, std::string &end) {
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2011-01-06 15:21:09 +01:00
|
|
|
if (!clear_prefix (begin))
|
|
|
|
return Xapian::BAD_VALUENO;
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2011-01-06 15:21:09 +01:00
|
|
|
if (!substitute_size (begin) || !substitute_size (end))
|
|
|
|
return Xapian::BAD_VALUENO;
|
|
|
|
|
2011-05-21 08:14:02 +02:00
|
|
|
/* swap if b > e */
|
|
|
|
if (begin > end)
|
|
|
|
std::swap (begin, end);
|
|
|
|
|
|
|
|
begin = Xapian::sortable_serialise (atol(begin.c_str()));
|
|
|
|
end = Xapian::sortable_serialise (atol(end.c_str()));
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2011-01-06 15:21:09 +01:00
|
|
|
return (Xapian::valueno)MU_MSG_FIELD_ID_SIZE;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
bool clear_prefix (std::string& begin) {
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2011-01-06 15:21:09 +01:00
|
|
|
const std::string colon (":");
|
|
|
|
const std::string name (mu_msg_field_name
|
|
|
|
(MU_MSG_FIELD_ID_SIZE) + colon);
|
|
|
|
const std::string shortcut (
|
|
|
|
std::string(1, mu_msg_field_shortcut
|
|
|
|
(MU_MSG_FIELD_ID_SIZE)) + colon);
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2011-01-06 15:21:09 +01:00
|
|
|
if (begin.find (name) == 0) {
|
|
|
|
begin.erase (0, name.length());
|
|
|
|
return true;
|
|
|
|
} else if (begin.find (shortcut) == 0) {
|
|
|
|
begin.erase (0, shortcut.length());
|
|
|
|
return true;
|
|
|
|
} else
|
2011-08-29 22:38:55 +02:00
|
|
|
return false;
|
2011-01-06 15:21:09 +01:00
|
|
|
}
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2011-01-06 15:21:09 +01:00
|
|
|
bool substitute_size (std::string& size) {
|
|
|
|
gchar str[16];
|
2011-07-16 11:50:17 +02:00
|
|
|
gint64 num = mu_str_size_parse_bkm(size.c_str());
|
|
|
|
if (num < 0)
|
|
|
|
throw Xapian::QueryParserError ("invalid size");
|
2011-01-06 15:21:09 +01:00
|
|
|
snprintf (str, sizeof(str), "%" G_GUINT64_FORMAT, num);
|
|
|
|
size = str;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-11-20 13:43:04 +01:00
|
|
|
static void add_prefix (MuMsgFieldId field, Xapian::QueryParser* qparser);
|
2009-12-05 19:10:58 +01:00
|
|
|
|
2010-08-25 20:46:16 +02:00
|
|
|
struct _MuQuery {
|
2011-08-30 21:02:08 +02:00
|
|
|
public:
|
2011-09-03 09:49:51 +02:00
|
|
|
_MuQuery (MuStore *store): _store(mu_store_ref(store)) {
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2011-08-30 21:02:08 +02:00
|
|
|
_qparser.set_database (db());
|
2011-03-22 22:37:01 +01:00
|
|
|
_qparser.set_default_op (Xapian::Query::OP_AND);
|
|
|
|
|
|
|
|
_qparser.add_valuerangeprocessor (&_date_range_processor);
|
|
|
|
_qparser.add_valuerangeprocessor (&_size_range_processor);
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2009-12-05 19:10:58 +01:00
|
|
|
mu_msg_field_foreach ((MuMsgFieldForEachFunc)add_prefix,
|
2011-03-22 22:37:01 +01:00
|
|
|
&_qparser);
|
|
|
|
}
|
2011-08-29 22:38:55 +02:00
|
|
|
|
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 {
|
|
|
|
Xapian::Database* db;
|
|
|
|
db = reinterpret_cast<Xapian::Database*>
|
|
|
|
(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
|
|
|
Xapian::QueryParser& query_parser () { return _qparser; }
|
|
|
|
|
|
|
|
private:
|
2011-03-22 22:37:01 +01:00
|
|
|
Xapian::QueryParser _qparser;
|
|
|
|
MuDateRangeProcessor _date_range_processor;
|
|
|
|
MuSizeRangeProcessor _size_range_processor;
|
2011-08-30 21:02:08 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
Xapian::Query query;
|
2011-08-29 22:38:55 +02:00
|
|
|
char *preprocessed;
|
|
|
|
|
2011-07-13 07:07:52 +02:00
|
|
|
preprocessed = mu_query_preprocess (searchexpr);
|
|
|
|
|
2009-12-08 23:01:49 +01:00
|
|
|
try {
|
2011-08-30 21:02:08 +02:00
|
|
|
query = mqx->query_parser().parse_query
|
2011-07-13 07:07:52 +02:00
|
|
|
(preprocessed,
|
2010-11-21 14:42:53 +01:00
|
|
|
Xapian::QueryParser::FLAG_BOOLEAN |
|
|
|
|
Xapian::QueryParser::FLAG_PURE_NOT |
|
2011-05-09 08:33:33 +02:00
|
|
|
Xapian::QueryParser::FLAG_WILDCARD |
|
2010-11-20 13:43:04 +01:00
|
|
|
Xapian::QueryParser::FLAG_AUTO_SYNONYMS |
|
2010-09-12 15:30:29 +02:00
|
|
|
Xapian::QueryParser::FLAG_BOOLEAN_ANY_CASE);
|
2011-07-13 07:07:52 +02:00
|
|
|
g_free (preprocessed);
|
|
|
|
return query;
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2011-07-13 07:07:52 +02:00
|
|
|
} catch (...) {
|
|
|
|
/* some error occured */
|
2011-08-11 19:20:22 +02:00
|
|
|
g_set_error (err, 0, MU_ERROR_XAPIAN_QUERY,
|
2011-07-16 11:50:17 +02:00
|
|
|
"parse error in query");
|
2011-07-13 07:07:52 +02:00
|
|
|
g_free (preprocessed);
|
|
|
|
throw;
|
|
|
|
}
|
2009-12-08 23:01:49 +01:00
|
|
|
}
|
2009-11-25 21:55:06 +01:00
|
|
|
|
2011-07-13 07:07:52 +02:00
|
|
|
|
|
|
|
|
2009-11-25 21:55:06 +01:00
|
|
|
static void
|
2010-11-20 13:43:04 +01:00
|
|
|
add_prefix (MuMsgFieldId mfid, Xapian::QueryParser* qparser)
|
2009-11-25 21:55:06 +01:00
|
|
|
{
|
2010-11-20 13:43:04 +01:00
|
|
|
if (!mu_msg_field_xapian_index(mfid) &&
|
|
|
|
!mu_msg_field_xapian_term(mfid) &&
|
|
|
|
!mu_msg_field_xapian_contact(mfid))
|
2009-11-25 21:55:06 +01:00
|
|
|
return;
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2010-11-20 13:43:04 +01:00
|
|
|
try {
|
2010-11-20 15:37:23 +01:00
|
|
|
const std::string pfx
|
|
|
|
(1, mu_msg_field_xapian_prefix (mfid));
|
|
|
|
const std::string shortcut
|
|
|
|
(1, mu_msg_field_shortcut (mfid));
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2011-05-20 20:30:04 +02:00
|
|
|
if (mu_msg_field_uses_boolean_prefix (mfid)) {
|
|
|
|
qparser->add_boolean_prefix
|
|
|
|
(mu_msg_field_name(mfid), pfx);
|
|
|
|
qparser->add_boolean_prefix (shortcut, pfx);
|
2010-11-20 13:43:04 +01:00
|
|
|
} else {
|
2011-05-20 20:30:04 +02:00
|
|
|
qparser->add_prefix
|
2010-11-20 13:43:04 +01:00
|
|
|
(mu_msg_field_name(mfid), pfx);
|
2011-05-20 20:30:04 +02:00
|
|
|
qparser->add_prefix (shortcut, pfx);
|
2010-11-20 13:43:04 +01:00
|
|
|
}
|
2011-08-29 22:38:55 +02:00
|
|
|
|
|
|
|
if (!mu_msg_field_needs_prefix(mfid))
|
2011-05-20 20:30:04 +02:00
|
|
|
qparser->add_prefix ("", pfx);
|
|
|
|
|
2010-11-20 13:43:04 +01:00
|
|
|
} MU_XAPIAN_CATCH_BLOCK;
|
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) {
|
|
|
|
g_set_error (err, 0, MU_ERROR_XAPIAN_IS_EMPTY,
|
|
|
|
"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);
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2011-09-03 09:49:51 +02:00
|
|
|
} MU_XAPIAN_CATCH_BLOCK_G_ERROR_RETURN (err,
|
|
|
|
MU_ERROR_XAPIAN, NULL);
|
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
|
|
|
|
2010-09-09 07:21:01 +02:00
|
|
|
/* preprocess a query to make them a bit more permissive */
|
2010-09-12 15:30:29 +02:00
|
|
|
char*
|
|
|
|
mu_query_preprocess (const char *query)
|
2010-09-09 07:21:01 +02:00
|
|
|
{
|
|
|
|
gchar *my_query;
|
2010-09-12 15:30:29 +02:00
|
|
|
|
|
|
|
g_return_val_if_fail (query, NULL);
|
2010-11-29 20:29:43 +01:00
|
|
|
my_query = g_strdup (query);
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2010-11-29 20:29:43 +01:00
|
|
|
/* remove accents and turn to lower-case */
|
|
|
|
mu_str_normalize_in_place (my_query, TRUE);
|
|
|
|
/* escape '@', single '_' and ':' if it's not following a
|
|
|
|
* xapian-pfx with '_' */
|
|
|
|
mu_str_ascii_xapian_escape_in_place (my_query);
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2010-09-09 07:21:01 +02:00
|
|
|
return my_query;
|
|
|
|
}
|
|
|
|
|
2009-11-25 21:55:06 +01:00
|
|
|
|
2010-08-25 20:29:53 +02:00
|
|
|
MuMsgIter*
|
2011-06-19 20:03:33 +02:00
|
|
|
mu_query_run (MuQuery *self, const char* searchexpr, gboolean threads,
|
2010-11-20 13:43:04 +01:00
|
|
|
MuMsgFieldId sortfieldid, gboolean ascending,
|
2011-08-29 22:38:55 +02:00
|
|
|
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);
|
2010-11-20 13:43:04 +01:00
|
|
|
g_return_val_if_fail (mu_msg_field_id_is_valid (sortfieldid) ||
|
2010-11-23 22:52:37 +01:00
|
|
|
sortfieldid == MU_MSG_FIELD_ID_NONE,
|
|
|
|
NULL);
|
2010-01-23 23:51:17 +01:00
|
|
|
try {
|
2011-08-30 21:02:08 +02:00
|
|
|
Xapian::Enquire enq (self->db());
|
2010-01-24 12:15:04 +01:00
|
|
|
|
2011-07-02 10:27:08 +02:00
|
|
|
/* note, when our result will be *threaded*, we sort
|
|
|
|
* there, and don't let Xapian do any sorting */
|
|
|
|
if (!threads && sortfieldid != MU_MSG_FIELD_ID_NONE)
|
2010-11-23 22:52:37 +01:00
|
|
|
enq.set_sort_by_value ((Xapian::valueno)sortfieldid,
|
|
|
|
ascending ? true : false);
|
2011-07-13 07:18:42 +02:00
|
|
|
|
|
|
|
if (!mu_str_is_empty(searchexpr)) /* NULL or "" */
|
|
|
|
enq.set_query(get_query (self, searchexpr, err));
|
|
|
|
else
|
|
|
|
enq.set_query(Xapian::Query::MatchAll);
|
|
|
|
|
2010-01-23 23:51:17 +01:00
|
|
|
enq.set_cutoff(0,0);
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2011-07-02 10:27:08 +02:00
|
|
|
return mu_msg_iter_new (
|
|
|
|
(XapianEnquire*)&enq,
|
2011-08-30 21:02:08 +02:00
|
|
|
self->db().get_doccount(), threads,
|
2011-07-02 10:27:08 +02:00
|
|
|
threads ? sortfieldid : MU_MSG_FIELD_ID_NONE);
|
2011-08-29 22:38:55 +02:00
|
|
|
|
2010-01-08 19:49:55 +01:00
|
|
|
} MU_XAPIAN_CATCH_BLOCK_RETURN(NULL);
|
2009-11-25 21:55:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
char*
|
2011-08-29 22:38:55 +02:00
|
|
|
mu_query_as_string (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
|
|
|
}
|
2010-01-03 22:53:49 +01:00
|
|
|
|
|
|
|
|