* remove mu_query_xapian_combine; let Xapian handle it. add

mu_util_str_from_strv for combining strings
This commit is contained in:
Dirk-Jan C. Binnema 2010-02-03 21:06:31 +02:00
parent a2d1692dda
commit fa08d66380
5 changed files with 59 additions and 83 deletions

View File

@ -247,15 +247,14 @@ run_query (MuQueryXapian *xapian, const gchar *query, MuConfigOptions *opts)
}
static gboolean
do_output (MuQueryXapian *xapian, MuConfigOptions* opts,
const gchar **params)
const gchar **params)
{
gchar *query;
gboolean retval = TRUE;
query = mu_query_xapian_combine (params, FALSE);
query = mu_util_str_from_strv (params);
/* if xquery is set, we print the xapian query instead of the
* output; this is for debugging purposes */
@ -265,13 +264,11 @@ do_output (MuQueryXapian *xapian, MuConfigOptions* opts,
retval = run_query (xapian, query, opts);
g_free (query);
return retval;
}
static gboolean
query_params_valid (MuConfigOptions *opts)
{

View File

@ -52,7 +52,7 @@ init_mu_query_xapian (MuQueryXapian *mqx, const char* dbpath)
mqx->_qparser = new Xapian::QueryParser;
mqx->_qparser->set_database(*mqx->_db);
mqx->_qparser->set_default_op(Xapian::Query::OP_OR);
mqx->_qparser->set_default_op(Xapian::Query::OP_AND);
mqx->_qparser->set_stemming_strategy
(Xapian::QueryParser::STEM_SOME);
@ -94,8 +94,7 @@ get_query (MuQueryXapian * mqx, const char* searchexpr, int *err = 0) {
(searchexpr,
Xapian::QueryParser::FLAG_BOOLEAN |
Xapian::QueryParser::FLAG_PHRASE |
// Xapian::QueryParser::FLAG_LOVEHATE |
// Xapian::QueryParser::FLAG_BOOLEAN_ANY_CASE |
Xapian::QueryParser::FLAG_BOOLEAN_ANY_CASE |
Xapian::QueryParser::FLAG_WILDCARD |
Xapian::QueryParser::FLAG_PURE_NOT |
Xapian::QueryParser::FLAG_PARTIAL);
@ -105,8 +104,6 @@ get_query (MuQueryXapian * mqx, const char* searchexpr, int *err = 0) {
if (err)
*err = 1;
return Xapian::Query();
}
@ -183,7 +180,14 @@ mu_query_xapian_run (MuQueryXapian *self, const char* searchexpr,
g_return_val_if_fail (searchexpr, NULL);
try {
Xapian::Query q(get_query(self, searchexpr));
int err (0);
Xapian::Query q(get_query(self, searchexpr, &err));
if (err) {
g_warning ("Error in query '%s'", searchexpr);
return NULL;
}
Xapian::Enquire enq (*self->_db);
if (batchsize == 0)
@ -209,58 +213,17 @@ mu_query_xapian_as_string (MuQueryXapian *self, const char* searchexpr)
g_return_val_if_fail (searchexpr, NULL);
try {
Xapian::Query q(get_query(self, searchexpr));
int err (0);
Xapian::Query q(get_query(self, searchexpr, &err));
if (err) {
g_warning ("Error in query '%s'", searchexpr);
return NULL;
}
return g_strdup(q.get_description().c_str());
} MU_XAPIAN_CATCH_BLOCK_RETURN(NULL);
}
static gboolean
needs_quotes (const char* str)
{
int i;
const char *keywords[] = { "AND", "OR", "NOT", "NEAR", "ADJ" };
for (i = 0; i != G_N_ELEMENTS(keywords); ++i)
if (g_strcasecmp (str, keywords[i]) == 0)
return TRUE;
return FALSE;
}
char*
mu_query_xapian_combine (const gchar **params, gboolean connect_or)
{
GString *str;
int i;
g_return_val_if_fail (params && params[0], NULL);
str = g_string_sized_new (64); /* just a guess */
for (i = 0; params && params[i]; ++i) {
const char* elm;
const char* cnx = "";
gboolean do_quote;
elm = (const gchar*)params[i];
if (!elm) /* shouldn't happen */
break;
if (params[i + 1])
cnx = connect_or ? " OR " : " AND ";
do_quote = needs_quotes (elm);
g_string_append_printf (str, "%s%s%s%s",
do_quote ? "\"" : "",
elm,
do_quote ? "\"" : "",
cnx);
}
return g_string_free (str, FALSE);
}

View File

@ -58,8 +58,7 @@ void mu_query_xapian_destroy (MuQueryXapian *self);
*
* @return the version string (free with g_free), or NULL in case of error
*/
char* mu_query_xapian_version (MuQueryXapian *store);
char* mu_query_xapian_version (MuQueryXapian *store) G_GNUC_WARN_UNUSED_RESULT;
/**
* run a Xapian query; for the syntax, please refer to the mu-find
@ -85,20 +84,6 @@ MuMsgIterXapian* mu_query_xapian_run (MuQueryXapian *self,
size_t batchsize)
G_GNUC_WARN_UNUSED_RESULT;
/**
* create a xapian query from list of expressions; for the syntax,
* please refer to the mu-find manpage, or
* http://xapian.org/docs/queryparser.html
*
* @param string array of search expressions
* @param connect_or if TRUE, combine the expressions with OR, otherwise use AND
*
* @return a string with the combined xapian expression or NULL in
* case of error; free with g_free when it's no longer needed
*/
char* mu_query_xapian_combine (const gchar **params,
gboolean connect_or)
G_GNUC_WARN_UNUSED_RESULT;
/**
* get a string representation of the Xapian search query

View File

@ -53,8 +53,7 @@ mu_util_dir_expand (const char *path)
}
gboolean
mu_util_check_dir (const gchar* path, gboolean readable,
gboolean writeable)
mu_util_check_dir (const gchar* path, gboolean readable, gboolean writeable)
{
mode_t mode;
struct stat statbuf;
@ -127,3 +126,26 @@ mu_util_create_dir_maybe (const gchar *path)
return TRUE;
}
gchar*
mu_util_str_from_strv (const gchar **params)
{
GString *str;
int i;
g_return_val_if_fail (params, NULL);
if (!params[0])
return g_strdup ("");
str = g_string_sized_new (64); /* just a guess */
for (i = 0; params[i]; ++i) {
if (i>0)
g_string_append_c (str, ' ');
g_string_append (str, params[i]);
}
return g_string_free (str, FALSE);
}

View File

@ -32,7 +32,7 @@ G_BEGIN_DECLS
* @return the expanded path as a newly allocated string, or NULL in
* case of error
*/
char* mu_util_dir_expand (const char* path);
char* mu_util_dir_expand (const char* path) G_GNUC_WARN_UNUSED_RESULT;
/**
* guess the maildir; first try $MAILDIR; if it is unset or
@ -40,7 +40,7 @@ char* mu_util_dir_expand (const char* path);
*
* @return full path of the guessed Maildir, or NULL; must be freed (gfree)
*/
char* mu_util_guess_maildir (void);
char* mu_util_guess_maildir (void) G_GNUC_WARN_UNUSED_RESULT;
@ -53,7 +53,7 @@ char* mu_util_guess_maildir (void);
* @return TRUE if a read/writeable directory `path' exists after
* leaving this function, FALSE otherwise
*/
gboolean mu_util_create_dir_maybe (const gchar *path);
gboolean mu_util_create_dir_maybe (const gchar *path) G_GNUC_WARN_UNUSED_RESULT;
/**
@ -67,9 +67,18 @@ gboolean mu_util_create_dir_maybe (const gchar *path);
* @return TRUE if dir exist and has the specified properties
*/
gboolean mu_util_check_dir (const gchar* path, gboolean readable,
gboolean writeable);
gboolean writeable) G_GNUC_WARN_UNUSED_RESULT;
/**
* convert a string array in to a string, with the elements separated
* by ' '
*
* @param params a non-NULL, NULL-terminated string array
*
* @return a newly allocated string
*/
gchar* mu_util_str_from_strv (const gchar **params) G_GNUC_WARN_UNUSED_RESULT;
/**
*