mu: use updated Query API

This commit is contained in:
Dirk-Jan C. Binnema 2020-11-03 10:01:18 +02:00
parent ed4a640c39
commit 2804087b3a
7 changed files with 133 additions and 258 deletions

View File

@ -30,7 +30,7 @@
#include "mu-msg.h"
#include "mu-maildir.h"
#include "mu-query.h"
#include "mu-query.hh"
#include "mu-msg-iter.h"
#include "mu-bookmarks.h"
#include "mu-runtime.h"
@ -48,22 +48,11 @@ typedef gboolean (OutputFunc) (MuMsg *msg, MuMsgIter *iter,
const MuConfig *opts, GError **err);
static gboolean
print_internal (MuQuery *query, const gchar *expr, gboolean xapian,
print_internal (const Query& query, const gchar *expr, gboolean xapian,
gboolean warn, GError **err)
{
char *str;
if (xapian)
str = mu_query_internal_xapian (query, expr, err);
else
str = mu_query_internal (query, expr, warn, err);
if (str) {
g_print ("%s\n", str);
g_free (str);
}
return str != NULL;
std::cout << query.parse(expr, xapian) << "\n";
return TRUE;
}
@ -112,11 +101,10 @@ get_message (MuMsgIter *iter, time_t after)
}
static MuMsgIter*
run_query (MuQuery *xapian, const gchar *query, const MuConfig *opts, GError **err)
run_query (const Query& q, const std::string& expr, const MuConfig *opts, GError **err)
{
MuMsgIter *iter;
MuMsgFieldId sortid;
int qflags;
sortid = MU_MSG_FIELD_ID_NONE;
if (opts->sortfield) {
@ -125,19 +113,17 @@ run_query (MuQuery *xapian, const gchar *query, const MuConfig *opts, GError **
return FALSE;
}
qflags = MU_QUERY_FLAG_NONE;
Mu::Query::Flags qflags{Query::Flags::None};
if (opts->reverse)
qflags |= MU_QUERY_FLAG_DESCENDING;
qflags |= Query::Flags::Descending;
if (opts->skip_dups)
qflags |= MU_QUERY_FLAG_SKIP_DUPS;
qflags |= Query::Flags::SkipDups;
if (opts->include_related)
qflags |= MU_QUERY_FLAG_INCLUDE_RELATED;
qflags |= Query::Flags::IncludeRelated;
if (opts->threads)
qflags |= MU_QUERY_FLAG_THREADS;
qflags |= Query::Flags::Threading;
iter = mu_query_run (xapian, query, sortid, opts->maxnum,
(MuQueryFlags)qflags, err);
return iter;
return q.run(expr, sortid, qflags, opts->maxnum, err);
}
static gboolean
@ -216,28 +202,18 @@ get_query (const MuConfig *opts, GError **err)
return query;
}
static MuQuery*
get_query_obj (MuStore *store, GError **err)
static Mu::Query
get_query_obj (const Store& store, GError **err)
{
MuQuery *mquery;
unsigned count;
count = mu_store_count (store, err);
const auto count{store.size()};
if (count == (unsigned)-1)
return NULL;
throw Mu::Error(Error::Code::Store, "invalid store");
if (count == 0) {
g_set_error (err, MU_ERROR_DOMAIN, MU_ERROR_XAPIAN_NEEDS_REINDEX,
"the database is empty");
return NULL;
}
if (count == 0)
throw Mu::Error(Error::Code::Store, "store is empty");
mquery = mu_query_new (store, err);
if (!mquery)
return NULL;
return mquery;
return Mu::Query{store};
}
static gboolean
@ -671,12 +647,11 @@ output_query_results (MuMsgIter *iter, const MuConfig *opts, GError **err)
}
static gboolean
process_query (MuQuery *xapian, const gchar *query, const MuConfig *opts, GError **err)
process_query (const Query& q, const gchar *expr, const MuConfig *opts, GError **err)
{
MuMsgIter *iter;
gboolean rv;
iter = run_query (xapian, query, opts, err);
auto iter = run_query (q, expr, opts, err);
if (!iter)
return FALSE;
@ -687,34 +662,21 @@ process_query (MuQuery *xapian, const gchar *query, const MuConfig *opts, GError
}
static gboolean
execute_find (MuStore *store, const MuConfig *opts, GError **err)
execute_find (const Store& store, const MuConfig *opts, GError **err)
{
char *query_str;
MuQuery *oracle;
gboolean rv;
auto q{get_query_obj (store, err)};
oracle = get_query_obj (store, err);
if (!oracle)
auto expr{get_query (opts, err)};
if (!expr)
return FALSE;
query_str = get_query (opts, err);
if (!query_str) {
mu_query_destroy (oracle);
return FALSE;
}
if (opts->format == MU_CONFIG_FORMAT_XQUERY)
rv = print_internal (oracle, query_str, TRUE, FALSE, err);
return print_internal (q, expr, TRUE, FALSE, err);
else if (opts->format == MU_CONFIG_FORMAT_MQUERY)
rv = print_internal (oracle, query_str, FALSE,
return print_internal (q, expr, FALSE,
opts->verbose, err);
else
rv = process_query (oracle, query_str, opts, err);
mu_query_destroy (oracle);
g_free (query_str);
return rv;
return process_query (q, expr, opts, err);
}
static gboolean
@ -780,7 +742,7 @@ query_params_valid (const MuConfig *opts, GError **err)
}
MuError
mu_cmd_find (MuStore *store, const MuConfig *opts, GError **err)
mu_cmd_find (const Store& store, const MuConfig *opts, GError **err)
{
g_return_val_if_fail (opts, MU_ERROR_INTERNAL);
g_return_val_if_fail (opts->cmd == MU_CONFIG_CMD_FIND,

View File

@ -643,7 +643,7 @@ cmd_find (const MuConfig *opts, GError **err)
{
Mu::Store store{mu_runtime_path(MU_RUNTIME_PATH_XAPIANDB), true/*readonly*/};
return mu_cmd_find(reinterpret_cast<MuStore*>(&store), opts, err);
return mu_cmd_find(store, opts, err);
}
static void

View File

@ -37,7 +37,7 @@ G_BEGIN_DECLS
* >MU_OK (0) results, MU_EXITCODE_NO_MATCHES if the command
* succeeds but there no matches, some error code for all other errors
*/
MuError mu_cmd_find (MuStore* store, const MuConfig *opts,
MuError mu_cmd_find (const Mu::Store& store, const MuConfig *opts,
GError **err);
/**

View File

@ -1,4 +1,4 @@
/* -*- mode: c; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
/*
**
** Copyright (C) 2008-2020 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
**
@ -27,9 +27,9 @@
#include <unistd.h>
#include <string.h>
#include "test-mu-common.h"
#include "test-mu-common.hh"
#include "mu-store.hh"
#include "mu-query.h"
#include "mu-query.hh"
static gchar *CONTACTS_CACHE = NULL;

View File

@ -30,9 +30,9 @@
#include <unistd.h>
#include <string.h>
#include "test-mu-common.h"
#include "test-mu-common.hh"
#include "mu-store.hh"
#include "mu-query.h"
#include "mu-query.hh"
/* tests for the command line interface, uses testdir2 */

View File

@ -1,7 +1,5 @@
/* -*-mode: c; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-*/
/*
** Copyright (C) 2008-2017 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
** Copyright (C) 2008-2020 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
**
** 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
@ -31,40 +29,41 @@
#include <string.h>
#include <locale.h>
#include "test-mu-common.h"
#include "mu-query.h"
#include "test-mu-common.hh"
#include "mu-query.hh"
#include "utils/mu-str.h"
#include "utils/mu-utils.hh"
#include "mu-store.hh"
static char* DB_PATH1 = NULL;
static char* DB_PATH2 = NULL;
using namespace Mu;
static gchar*
fill_database (const char *testdir)
static std::string DB_PATH1;
static std::string DB_PATH2;
static std::string
make_database (const std::string& testdir)
{
gchar *cmdline, *tmpdir, *xpath;
tmpdir = test_mu_common_get_random_tmpdir();
cmdline = g_strdup_printf (
"/bin/sh -c '"
"%s init --muhome=%s --maildir=%s --quiet ; "
"%s index --muhome=%s --quiet'",
MU_PROGRAM, tmpdir, testdir,
MU_PROGRAM, tmpdir);
char *tmpdir{test_mu_common_get_random_tmpdir()};
const auto cmdline{
format("/bin/sh -c '"
"%s init --muhome=%s --maildir=%s --quiet ; "
"%s index --muhome=%s --quiet'",
MU_PROGRAM, tmpdir, testdir.c_str(),
MU_PROGRAM, tmpdir)};
if (g_test_verbose())
g_printerr ("\n%s\n", cmdline);
g_printerr ("\n%s\n", cmdline.c_str());
g_assert (g_spawn_command_line_sync (cmdline, NULL, NULL,
g_assert (g_spawn_command_line_sync (cmdline.c_str(), NULL, NULL,
NULL, NULL));
g_free (cmdline);
xpath= g_strdup_printf ("%s%c%s", tmpdir,
G_DIR_SEPARATOR, "xapian");
auto xpath= g_strdup_printf ("%s%c%s", tmpdir, G_DIR_SEPARATOR, "xapian");
g_free (tmpdir);
return xpath;
std::string dbpath{xpath};
g_free(xpath);
return dbpath;
}
@ -94,51 +93,24 @@ assert_no_dups (MuMsgIter *iter)
/* note: this also *moves the iter* */
static guint
run_and_count_matches_with_query_flags (const char *xpath, const char *query,
MuQueryFlags flags)
run_and_count_matches (const std::string& xpath, const std::string& expr,
Mu::Query::Flags flags = Mu::Query::Flags::None)
{
MuQuery *mquery;
MuMsgIter *iter;
MuStore *store;
guint count1, count2;
GError *err;
err = NULL;
store = mu_store_new_readable (xpath, &err);
if (err) {
g_printerr ("error: %s\n", err->message);
g_clear_error (&err);
err = NULL;
}
g_assert (store);
mquery = mu_query_new (store, &err);
if (err) {
g_printerr ("error: %s\n", err->message);
g_clear_error (&err);
err = NULL;
}
g_assert (mquery);
mu_store_unref (store);
Mu::Store store{xpath};
Mu::Query query{store};
if (g_test_verbose()) {
char *x;
g_print ("\n==> query: %s\n", query);
x = mu_query_internal (mquery, query, FALSE, NULL);
g_print ("==> mquery: '%s'\n", x);
g_free (x);
x = mu_query_internal_xapian (mquery, query, NULL);
g_print ("==> xquery: '%s'\n", x);
g_free (x);
std::cout << "==> mquery: " << query.parse (expr, false) << "\n";
std::cout << "==> xquery: " << query.parse (expr, true) << "\n";
}
iter = mu_query_run (mquery, query, MU_MSG_FIELD_ID_NONE, -1,
flags, NULL);
mu_query_destroy (mquery);
g_assert (iter);
Mu::allow_warnings();
iter = query.run (expr, MU_MSG_FIELD_ID_NONE, flags);
g_assert (iter);
assert_no_dups (iter);
/* run query twice, to test mu_msg_iter_reset */
@ -159,13 +131,6 @@ run_and_count_matches_with_query_flags (const char *xpath, const char *query,
return count1;
}
static guint
run_and_count_matches (const char *xpath, const char *query)
{
return run_and_count_matches_with_query_flags (
xpath, query, MU_QUERY_FLAG_NONE);
}
typedef struct {
const char *query;
size_t count; /* expected number of matches */
@ -302,21 +267,15 @@ test_mu_query_logic (void)
static void
test_mu_query_accented_chars_01 (void)
{
MuQuery *query;
MuMsgIter *iter;
MuMsg *msg;
MuStore *store;
GError *err;
gchar *summ;
store = mu_store_new_readable (DB_PATH1, NULL);
g_assert (store);
Store store{DB_PATH1};
Query q{store};
query = mu_query_new (store, NULL);
mu_store_unref (store);
iter = mu_query_run (query, "fünkÿ", MU_MSG_FIELD_ID_NONE,
-1, MU_QUERY_FLAG_NONE, NULL);
iter = q.run("fünkÿ");
err = NULL;
msg = mu_msg_iter_get_msg_floating (iter); /* don't unref */
if (!msg) {
@ -336,7 +295,6 @@ test_mu_query_accented_chars_01 (void)
g_free (summ);
mu_msg_iter_destroy (iter);
mu_query_destroy (query);
}
static void
@ -410,7 +368,6 @@ test_mu_query_wildcards (void)
static void
test_mu_query_dates_helsinki (void)
{
gchar *xpath;
int i;
const char *old_tz;
@ -424,15 +381,14 @@ test_mu_query_dates_helsinki (void)
old_tz = set_tz ("Europe/Helsinki");
xpath = fill_database (MU_TESTMAILDIR);
g_assert (xpath != NULL);
const auto xpath{make_database (MU_TESTMAILDIR)};
g_assert_false (xpath.empty());
for (i = 0; i != G_N_ELEMENTS(queries); ++i)
g_assert_cmpuint (run_and_count_matches
(xpath, queries[i].query),
==, queries[i].count);
g_free (xpath);
set_tz (old_tz);
}
@ -440,10 +396,8 @@ test_mu_query_dates_helsinki (void)
static void
test_mu_query_dates_sydney (void)
{
gchar *xpath;
int i;
const char *old_tz;
QResults queries[] = {
{ "date:20080731..20080804", 5},
{ "date:20080731..20080804 s:gcc", 1},
@ -454,23 +408,19 @@ test_mu_query_dates_sydney (void)
old_tz = set_tz ("Australia/Sydney");
xpath = fill_database (MU_TESTMAILDIR);
g_assert (xpath != NULL);
const auto xpath{make_database(MU_TESTMAILDIR)};
g_assert_false (xpath.empty());
for (i = 0; i != G_N_ELEMENTS(queries); ++i)
g_assert_cmpuint (run_and_count_matches
(xpath, queries[i].query),
==, queries[i].count);
g_free (xpath);
set_tz (old_tz);
}
static void
test_mu_query_dates_la (void)
{
gchar *xpath;
int i;
const char *old_tz;
@ -486,8 +436,8 @@ test_mu_query_dates_la (void)
old_tz = set_tz ("America/Los_Angeles");
xpath = fill_database (MU_TESTMAILDIR);
g_assert (xpath != NULL);
const auto xpath = make_database (MU_TESTMAILDIR);
g_assert_false (xpath.empty());
for (i = 0; i != G_N_ELEMENTS(queries); ++i) {
/* g_print ("%s\n", queries[i].query); */
@ -496,7 +446,6 @@ test_mu_query_dates_la (void)
==, queries[i].count);
}
g_free (xpath);
set_tz (old_tz);
}
@ -671,26 +620,19 @@ test_mu_query_tags_02 (void)
static void
test_mu_query_threads_compilation_error (void)
{
gchar *xpath;
const auto xpath = make_database (MU_TESTMAILDIR);
g_assert_false (xpath.empty());
xpath = fill_database (MU_TESTMAILDIR);
g_assert (xpath != NULL);
g_assert_cmpuint (run_and_count_matches_with_query_flags
(xpath, "msgid:uwsireh25.fsf@one.dot.net",
MU_QUERY_FLAG_NONE),
g_assert_cmpuint (run_and_count_matches
(xpath, "msgid:uwsireh25.fsf@one.dot.net"),
==, 1);
g_assert_cmpuint (run_and_count_matches_with_query_flags
g_assert_cmpuint (run_and_count_matches
(xpath, "msgid:uwsireh25.fsf@one.dot.net",
MU_QUERY_FLAG_INCLUDE_RELATED),
Query::Flags::IncludeRelated),
==, 3);
g_free (xpath);
}
int
main (int argc, char *argv[])
{
@ -700,11 +642,11 @@ main (int argc, char *argv[])
g_test_init (&argc, &argv, NULL);
DB_PATH1 = fill_database (MU_TESTMAILDIR);
g_assert (DB_PATH1);
DB_PATH1 = make_database (MU_TESTMAILDIR);
g_assert_false (DB_PATH1.empty());
DB_PATH2 = fill_database (MU_TESTMAILDIR2);
g_assert (DB_PATH2);
DB_PATH2 = make_database (MU_TESTMAILDIR2);
g_assert_false (DB_PATH2.empty());
g_test_add_func ("/mu-query/test-mu-query-01", test_mu_query_01);
g_test_add_func ("/mu-query/test-mu-query-02", test_mu_query_02);
@ -753,15 +695,11 @@ main (int argc, char *argv[])
test_mu_query_threads_compilation_error);
if (!g_test_verbose())
g_log_set_handler (NULL,
(GLogLevelFlags)(G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL|
G_LOG_FLAG_RECURSION),
(GLogFunc)black_hole, NULL);
g_log_set_handler (NULL,
(GLogLevelFlags)(G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL|
G_LOG_LEVEL_WARNING|G_LOG_FLAG_RECURSION),
(GLogFunc)black_hole, NULL);
rv = g_test_run ();
g_free (DB_PATH1);
g_free (DB_PATH2);
return rv;
}

View File

@ -1,7 +1,5 @@
/* -*-mode: c; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-*/
/*
** Copyright (C) 2008-2013 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
** Copyright (C) 2008-2020 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
**
** 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
@ -30,10 +28,12 @@
#include <unistd.h>
#include <string.h>
#include "test-mu-common.h"
#include "mu-query.h"
#include "test-mu-common.hh"
#include "mu-query.hh"
#include "utils/mu-str.h"
using namespace Mu;
struct _tinfo {
const char *threadpath;
const char *msgid;
@ -93,72 +93,59 @@ foreach_assert_tinfo_equal (MuMsgIter *iter, const tinfo items[], guint n_items)
g_assert (u == n_items);
}
static gchar*
fill_database (const char *testdir)
static std::string
make_database (const std::string& testdir)
{
gchar *cmdline, *tmpdir, *xpath;
char *tmpdir{test_mu_common_get_random_tmpdir()};
const auto cmdline{
format("/bin/sh -c '"
"%s init --muhome=%s --maildir=%s --quiet ; "
"%s index --muhome=%s --quiet'",
MU_PROGRAM, tmpdir, testdir.c_str(),
MU_PROGRAM, tmpdir)};
tmpdir = test_mu_common_get_random_tmpdir();
cmdline = g_strdup_printf (
"/bin/sh -c '"
"%s init --muhome=%s --maildir=%s --quiet ; "
"%s index --muhome=%s --quiet'",
MU_PROGRAM, tmpdir, testdir,
MU_PROGRAM, tmpdir);
if (g_test_verbose())
g_print ("%s\n", cmdline);
g_printerr ("\n%s\n", cmdline.c_str());
g_assert (g_spawn_command_line_sync (cmdline, NULL, NULL,
g_assert (g_spawn_command_line_sync (cmdline.c_str(), NULL, NULL,
NULL, NULL));
g_free (cmdline);
xpath= g_strdup_printf ("%s%c%s", tmpdir,
G_DIR_SEPARATOR, "xapian");
auto xpath= g_strdup_printf ("%s%c%s", tmpdir, G_DIR_SEPARATOR, "xapian");
g_free (tmpdir);
return xpath;
std::string dbpath{xpath};
g_free(xpath);
return dbpath;
}
/* note: this also *moves the iter* */
static MuMsgIter*
run_and_get_iter_full (const char *xpath, const char *query,
MuMsgFieldId sort_field, MuQueryFlags flags)
run_and_get_iter_full (const std::string& xpath, const std::string& expr,
MuMsgFieldId sort_field,
Mu::Query::Flags flags=Mu::Query::Flags::None)
{
MuQuery *mquery;
MuStore *store;
MuMsgIter *iter;
int myflags;
Mu::Store store{xpath};
Mu::Query q{store};
store = mu_store_new_readable (xpath, NULL);
g_assert (store);
mquery = mu_query_new (store, NULL);
mu_store_unref (store);
g_assert (query);
myflags = flags;
myflags |= MU_QUERY_FLAG_THREADS;
iter = mu_query_run (mquery, query, sort_field, -1,
(MuQueryFlags)myflags, NULL);
mu_query_destroy (mquery);
const auto myflags{flags | Mu::Query::Flags::Threading};
auto iter = q.run (expr, sort_field, myflags);
g_assert (iter);
return iter;
}
static MuMsgIter*
run_and_get_iter (const char *xpath, const char *query)
run_and_get_iter (const std::string& xpath, const char *query)
{
return run_and_get_iter_full (xpath, query, MU_MSG_FIELD_ID_DATE,
MU_QUERY_FLAG_NONE);
return run_and_get_iter_full (xpath, query, MU_MSG_FIELD_ID_DATE);
}
static void
test_mu_threads_01 (void)
{
gchar *xpath;
MuMsgIter *iter;
const tinfo items [] = {
{"0", "root0@msg.id", "root0"},
{"0:0", "child0.0@msg.id", "Re: child 0.0"},
@ -176,23 +163,20 @@ test_mu_threads_01 (void)
{"4:1", "child4.1@msg.id", "Re: child 4.1"}
};
xpath = fill_database (MU_TESTMAILDIR3);
g_assert (xpath != NULL);
const auto xpath{make_database(MU_TESTMAILDIR3)};
g_assert (!xpath.empty());
iter = run_and_get_iter (xpath, "abc");
auto iter = run_and_get_iter (xpath, "abc");
g_assert (iter);
g_assert (!mu_msg_iter_is_done(iter));
foreach_assert_tinfo_equal (iter, items, G_N_ELEMENTS (items));
g_free (xpath);
mu_msg_iter_destroy (iter);
}
static void
test_mu_threads_rogue (void)
{
gchar *xpath;
MuMsgIter *iter;
tinfo *items;
@ -210,8 +194,8 @@ test_mu_threads_rogue (void)
{"0:1", "cycle0.0.0@msg.id", "cycle0.0.0"}
};
xpath = fill_database (MU_TESTMAILDIR3);
g_assert (xpath != NULL);
const auto xpath{make_database (MU_TESTMAILDIR3)};
g_assert_false (xpath.empty());
iter = run_and_get_iter (xpath, "def");
g_assert (iter);
@ -226,30 +210,20 @@ test_mu_threads_rogue (void)
items = items2;
foreach_assert_tinfo_equal (iter, items, G_N_ELEMENTS (items1));
g_free (xpath);
mu_msg_iter_destroy (iter);
}
static MuMsgIter*
query_testdir (const char *query, MuMsgFieldId sort_field, gboolean descending)
{
MuMsgIter *iter;
gchar *xpath;
int flags;
const auto flags{descending ? Query::Flags::Descending : Query::Flags::None};
const auto xpath{make_database(MU_TESTMAILDIR3)};
g_assert_false (xpath.empty());
flags = MU_QUERY_FLAG_NONE;
if (descending)
flags |= MU_QUERY_FLAG_DESCENDING;
xpath = fill_database (MU_TESTMAILDIR3);
g_assert (xpath != NULL);
iter = run_and_get_iter_full (xpath, query, sort_field, (MuQueryFlags)flags);
auto iter = run_and_get_iter_full (xpath, query, sort_field, flags);
g_assert (iter != NULL);
g_assert (!mu_msg_iter_is_done (iter));
g_free (xpath);
return iter;
}
@ -442,6 +416,7 @@ main (int argc, char *argv[])
g_test_add_func ("/mu-query/test-mu-threads-01", test_mu_threads_01);
g_test_add_func ("/mu-query/test-mu-threads-rogue", test_mu_threads_rogue);
g_test_add_func ("/mu-query/test-mu-threads-sort-1st-child-promotes-thread",
test_mu_threads_sort_1st_child_promotes_thread);
g_test_add_func ("/mu-query/test-mu-threads-sort-2nd-child-promotes-thread",