mirror of
https://github.com/djcb/mu.git
synced 2024-06-29 07:51:04 +02:00
mu: use updated Query API
This commit is contained in:
parent
ed4a640c39
commit
2804087b3a
|
@ -30,7 +30,7 @@
|
||||||
|
|
||||||
#include "mu-msg.h"
|
#include "mu-msg.h"
|
||||||
#include "mu-maildir.h"
|
#include "mu-maildir.h"
|
||||||
#include "mu-query.h"
|
#include "mu-query.hh"
|
||||||
#include "mu-msg-iter.h"
|
#include "mu-msg-iter.h"
|
||||||
#include "mu-bookmarks.h"
|
#include "mu-bookmarks.h"
|
||||||
#include "mu-runtime.h"
|
#include "mu-runtime.h"
|
||||||
|
@ -48,22 +48,11 @@ typedef gboolean (OutputFunc) (MuMsg *msg, MuMsgIter *iter,
|
||||||
const MuConfig *opts, GError **err);
|
const MuConfig *opts, GError **err);
|
||||||
|
|
||||||
static gboolean
|
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)
|
gboolean warn, GError **err)
|
||||||
{
|
{
|
||||||
char *str;
|
std::cout << query.parse(expr, xapian) << "\n";
|
||||||
|
return TRUE;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -112,11 +101,10 @@ get_message (MuMsgIter *iter, time_t after)
|
||||||
}
|
}
|
||||||
|
|
||||||
static MuMsgIter*
|
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;
|
MuMsgIter *iter;
|
||||||
MuMsgFieldId sortid;
|
MuMsgFieldId sortid;
|
||||||
int qflags;
|
|
||||||
|
|
||||||
sortid = MU_MSG_FIELD_ID_NONE;
|
sortid = MU_MSG_FIELD_ID_NONE;
|
||||||
if (opts->sortfield) {
|
if (opts->sortfield) {
|
||||||
|
@ -125,19 +113,17 @@ run_query (MuQuery *xapian, const gchar *query, const MuConfig *opts, GError **
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
qflags = MU_QUERY_FLAG_NONE;
|
Mu::Query::Flags qflags{Query::Flags::None};
|
||||||
if (opts->reverse)
|
if (opts->reverse)
|
||||||
qflags |= MU_QUERY_FLAG_DESCENDING;
|
qflags |= Query::Flags::Descending;
|
||||||
if (opts->skip_dups)
|
if (opts->skip_dups)
|
||||||
qflags |= MU_QUERY_FLAG_SKIP_DUPS;
|
qflags |= Query::Flags::SkipDups;
|
||||||
if (opts->include_related)
|
if (opts->include_related)
|
||||||
qflags |= MU_QUERY_FLAG_INCLUDE_RELATED;
|
qflags |= Query::Flags::IncludeRelated;
|
||||||
if (opts->threads)
|
if (opts->threads)
|
||||||
qflags |= MU_QUERY_FLAG_THREADS;
|
qflags |= Query::Flags::Threading;
|
||||||
|
|
||||||
iter = mu_query_run (xapian, query, sortid, opts->maxnum,
|
return q.run(expr, sortid, qflags, opts->maxnum, err);
|
||||||
(MuQueryFlags)qflags, err);
|
|
||||||
return iter;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static gboolean
|
static gboolean
|
||||||
|
@ -216,28 +202,18 @@ get_query (const MuConfig *opts, GError **err)
|
||||||
return query;
|
return query;
|
||||||
}
|
}
|
||||||
|
|
||||||
static MuQuery*
|
static Mu::Query
|
||||||
get_query_obj (MuStore *store, GError **err)
|
get_query_obj (const Store& store, GError **err)
|
||||||
{
|
{
|
||||||
MuQuery *mquery;
|
const auto count{store.size()};
|
||||||
unsigned count;
|
|
||||||
|
|
||||||
count = mu_store_count (store, err);
|
|
||||||
|
|
||||||
if (count == (unsigned)-1)
|
if (count == (unsigned)-1)
|
||||||
return NULL;
|
throw Mu::Error(Error::Code::Store, "invalid store");
|
||||||
|
|
||||||
if (count == 0) {
|
if (count == 0)
|
||||||
g_set_error (err, MU_ERROR_DOMAIN, MU_ERROR_XAPIAN_NEEDS_REINDEX,
|
throw Mu::Error(Error::Code::Store, "store is empty");
|
||||||
"the database is empty");
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
mquery = mu_query_new (store, err);
|
return Mu::Query{store};
|
||||||
if (!mquery)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
return mquery;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static gboolean
|
static gboolean
|
||||||
|
@ -671,12 +647,11 @@ output_query_results (MuMsgIter *iter, const MuConfig *opts, GError **err)
|
||||||
}
|
}
|
||||||
|
|
||||||
static gboolean
|
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;
|
gboolean rv;
|
||||||
|
|
||||||
iter = run_query (xapian, query, opts, err);
|
auto iter = run_query (q, expr, opts, err);
|
||||||
if (!iter)
|
if (!iter)
|
||||||
return FALSE;
|
return FALSE;
|
||||||
|
|
||||||
|
@ -687,34 +662,21 @@ process_query (MuQuery *xapian, const gchar *query, const MuConfig *opts, GError
|
||||||
}
|
}
|
||||||
|
|
||||||
static gboolean
|
static gboolean
|
||||||
execute_find (MuStore *store, const MuConfig *opts, GError **err)
|
execute_find (const Store& store, const MuConfig *opts, GError **err)
|
||||||
{
|
{
|
||||||
char *query_str;
|
auto q{get_query_obj (store, err)};
|
||||||
MuQuery *oracle;
|
|
||||||
gboolean rv;
|
|
||||||
|
|
||||||
oracle = get_query_obj (store, err);
|
auto expr{get_query (opts, err)};
|
||||||
if (!oracle)
|
if (!expr)
|
||||||
return FALSE;
|
return FALSE;
|
||||||
|
|
||||||
query_str = get_query (opts, err);
|
|
||||||
if (!query_str) {
|
|
||||||
mu_query_destroy (oracle);
|
|
||||||
return FALSE;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (opts->format == MU_CONFIG_FORMAT_XQUERY)
|
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)
|
else if (opts->format == MU_CONFIG_FORMAT_MQUERY)
|
||||||
rv = print_internal (oracle, query_str, FALSE,
|
return print_internal (q, expr, FALSE,
|
||||||
opts->verbose, err);
|
opts->verbose, err);
|
||||||
else
|
else
|
||||||
rv = process_query (oracle, query_str, opts, err);
|
return process_query (q, expr, opts, err);
|
||||||
|
|
||||||
mu_query_destroy (oracle);
|
|
||||||
g_free (query_str);
|
|
||||||
|
|
||||||
return rv;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static gboolean
|
static gboolean
|
||||||
|
@ -780,7 +742,7 @@ query_params_valid (const MuConfig *opts, GError **err)
|
||||||
}
|
}
|
||||||
|
|
||||||
MuError
|
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, MU_ERROR_INTERNAL);
|
||||||
g_return_val_if_fail (opts->cmd == MU_CONFIG_CMD_FIND,
|
g_return_val_if_fail (opts->cmd == MU_CONFIG_CMD_FIND,
|
||||||
|
|
|
@ -643,7 +643,7 @@ cmd_find (const MuConfig *opts, GError **err)
|
||||||
{
|
{
|
||||||
Mu::Store store{mu_runtime_path(MU_RUNTIME_PATH_XAPIANDB), true/*readonly*/};
|
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
|
static void
|
||||||
|
|
|
@ -37,7 +37,7 @@ G_BEGIN_DECLS
|
||||||
* >MU_OK (0) results, MU_EXITCODE_NO_MATCHES if the command
|
* >MU_OK (0) results, MU_EXITCODE_NO_MATCHES if the command
|
||||||
* succeeds but there no matches, some error code for all other errors
|
* 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);
|
GError **err);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -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>
|
** Copyright (C) 2008-2020 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
||||||
**
|
**
|
||||||
|
@ -27,9 +27,9 @@
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "test-mu-common.h"
|
#include "test-mu-common.hh"
|
||||||
#include "mu-store.hh"
|
#include "mu-store.hh"
|
||||||
#include "mu-query.h"
|
#include "mu-query.hh"
|
||||||
|
|
||||||
static gchar *CONTACTS_CACHE = NULL;
|
static gchar *CONTACTS_CACHE = NULL;
|
||||||
|
|
||||||
|
|
|
@ -30,9 +30,9 @@
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "test-mu-common.h"
|
#include "test-mu-common.hh"
|
||||||
#include "mu-store.hh"
|
#include "mu-store.hh"
|
||||||
#include "mu-query.h"
|
#include "mu-query.hh"
|
||||||
|
|
||||||
/* tests for the command line interface, uses testdir2 */
|
/* tests for the command line interface, uses testdir2 */
|
||||||
|
|
||||||
|
|
|
@ -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
|
** 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
|
** under the terms of the GNU General Public License as published by the
|
||||||
|
@ -31,40 +29,41 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <locale.h>
|
#include <locale.h>
|
||||||
|
|
||||||
#include "test-mu-common.h"
|
#include "test-mu-common.hh"
|
||||||
#include "mu-query.h"
|
#include "mu-query.hh"
|
||||||
#include "utils/mu-str.h"
|
#include "utils/mu-str.h"
|
||||||
|
#include "utils/mu-utils.hh"
|
||||||
#include "mu-store.hh"
|
#include "mu-store.hh"
|
||||||
|
|
||||||
static char* DB_PATH1 = NULL;
|
using namespace Mu;
|
||||||
static char* DB_PATH2 = NULL;
|
|
||||||
|
|
||||||
static gchar*
|
static std::string DB_PATH1;
|
||||||
fill_database (const char *testdir)
|
static std::string DB_PATH2;
|
||||||
|
|
||||||
|
static std::string
|
||||||
|
make_database (const std::string& testdir)
|
||||||
{
|
{
|
||||||
gchar *cmdline, *tmpdir, *xpath;
|
char *tmpdir{test_mu_common_get_random_tmpdir()};
|
||||||
|
const auto cmdline{
|
||||||
tmpdir = test_mu_common_get_random_tmpdir();
|
format("/bin/sh -c '"
|
||||||
cmdline = g_strdup_printf (
|
"%s init --muhome=%s --maildir=%s --quiet ; "
|
||||||
"/bin/sh -c '"
|
"%s index --muhome=%s --quiet'",
|
||||||
"%s init --muhome=%s --maildir=%s --quiet ; "
|
MU_PROGRAM, tmpdir, testdir.c_str(),
|
||||||
"%s index --muhome=%s --quiet'",
|
MU_PROGRAM, tmpdir)};
|
||||||
MU_PROGRAM, tmpdir, testdir,
|
|
||||||
MU_PROGRAM, tmpdir);
|
|
||||||
|
|
||||||
|
|
||||||
if (g_test_verbose())
|
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));
|
NULL, NULL));
|
||||||
g_free (cmdline);
|
auto xpath= g_strdup_printf ("%s%c%s", tmpdir, G_DIR_SEPARATOR, "xapian");
|
||||||
|
|
||||||
xpath= g_strdup_printf ("%s%c%s", tmpdir,
|
|
||||||
G_DIR_SEPARATOR, "xapian");
|
|
||||||
g_free (tmpdir);
|
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* */
|
/* note: this also *moves the iter* */
|
||||||
static guint
|
static guint
|
||||||
run_and_count_matches_with_query_flags (const char *xpath, const char *query,
|
run_and_count_matches (const std::string& xpath, const std::string& expr,
|
||||||
MuQueryFlags flags)
|
Mu::Query::Flags flags = Mu::Query::Flags::None)
|
||||||
{
|
{
|
||||||
MuQuery *mquery;
|
|
||||||
MuMsgIter *iter;
|
MuMsgIter *iter;
|
||||||
MuStore *store;
|
|
||||||
guint count1, count2;
|
guint count1, count2;
|
||||||
GError *err;
|
|
||||||
|
|
||||||
err = NULL;
|
Mu::Store store{xpath};
|
||||||
store = mu_store_new_readable (xpath, &err);
|
Mu::Query query{store};
|
||||||
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);
|
|
||||||
|
|
||||||
if (g_test_verbose()) {
|
if (g_test_verbose()) {
|
||||||
char *x;
|
std::cout << "==> mquery: " << query.parse (expr, false) << "\n";
|
||||||
g_print ("\n==> query: %s\n", query);
|
std::cout << "==> xquery: " << query.parse (expr, true) << "\n";
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
iter = mu_query_run (mquery, query, MU_MSG_FIELD_ID_NONE, -1,
|
Mu::allow_warnings();
|
||||||
flags, NULL);
|
|
||||||
mu_query_destroy (mquery);
|
|
||||||
g_assert (iter);
|
|
||||||
|
|
||||||
|
iter = query.run (expr, MU_MSG_FIELD_ID_NONE, flags);
|
||||||
|
g_assert (iter);
|
||||||
assert_no_dups (iter);
|
assert_no_dups (iter);
|
||||||
|
|
||||||
/* run query twice, to test mu_msg_iter_reset */
|
/* 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;
|
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 {
|
typedef struct {
|
||||||
const char *query;
|
const char *query;
|
||||||
size_t count; /* expected number of matches */
|
size_t count; /* expected number of matches */
|
||||||
|
@ -302,21 +267,15 @@ test_mu_query_logic (void)
|
||||||
static void
|
static void
|
||||||
test_mu_query_accented_chars_01 (void)
|
test_mu_query_accented_chars_01 (void)
|
||||||
{
|
{
|
||||||
MuQuery *query;
|
|
||||||
MuMsgIter *iter;
|
MuMsgIter *iter;
|
||||||
MuMsg *msg;
|
MuMsg *msg;
|
||||||
MuStore *store;
|
|
||||||
GError *err;
|
GError *err;
|
||||||
gchar *summ;
|
gchar *summ;
|
||||||
|
|
||||||
store = mu_store_new_readable (DB_PATH1, NULL);
|
Store store{DB_PATH1};
|
||||||
g_assert (store);
|
Query q{store};
|
||||||
|
|
||||||
query = mu_query_new (store, NULL);
|
iter = q.run("fünkÿ");
|
||||||
mu_store_unref (store);
|
|
||||||
|
|
||||||
iter = mu_query_run (query, "fünkÿ", MU_MSG_FIELD_ID_NONE,
|
|
||||||
-1, MU_QUERY_FLAG_NONE, NULL);
|
|
||||||
err = NULL;
|
err = NULL;
|
||||||
msg = mu_msg_iter_get_msg_floating (iter); /* don't unref */
|
msg = mu_msg_iter_get_msg_floating (iter); /* don't unref */
|
||||||
if (!msg) {
|
if (!msg) {
|
||||||
|
@ -336,7 +295,6 @@ test_mu_query_accented_chars_01 (void)
|
||||||
g_free (summ);
|
g_free (summ);
|
||||||
|
|
||||||
mu_msg_iter_destroy (iter);
|
mu_msg_iter_destroy (iter);
|
||||||
mu_query_destroy (query);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
@ -410,7 +368,6 @@ test_mu_query_wildcards (void)
|
||||||
static void
|
static void
|
||||||
test_mu_query_dates_helsinki (void)
|
test_mu_query_dates_helsinki (void)
|
||||||
{
|
{
|
||||||
gchar *xpath;
|
|
||||||
int i;
|
int i;
|
||||||
const char *old_tz;
|
const char *old_tz;
|
||||||
|
|
||||||
|
@ -424,15 +381,14 @@ test_mu_query_dates_helsinki (void)
|
||||||
|
|
||||||
old_tz = set_tz ("Europe/Helsinki");
|
old_tz = set_tz ("Europe/Helsinki");
|
||||||
|
|
||||||
xpath = fill_database (MU_TESTMAILDIR);
|
const auto xpath{make_database (MU_TESTMAILDIR)};
|
||||||
g_assert (xpath != NULL);
|
g_assert_false (xpath.empty());
|
||||||
|
|
||||||
for (i = 0; i != G_N_ELEMENTS(queries); ++i)
|
for (i = 0; i != G_N_ELEMENTS(queries); ++i)
|
||||||
g_assert_cmpuint (run_and_count_matches
|
g_assert_cmpuint (run_and_count_matches
|
||||||
(xpath, queries[i].query),
|
(xpath, queries[i].query),
|
||||||
==, queries[i].count);
|
==, queries[i].count);
|
||||||
|
|
||||||
g_free (xpath);
|
|
||||||
set_tz (old_tz);
|
set_tz (old_tz);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -440,10 +396,8 @@ test_mu_query_dates_helsinki (void)
|
||||||
static void
|
static void
|
||||||
test_mu_query_dates_sydney (void)
|
test_mu_query_dates_sydney (void)
|
||||||
{
|
{
|
||||||
gchar *xpath;
|
|
||||||
int i;
|
int i;
|
||||||
const char *old_tz;
|
const char *old_tz;
|
||||||
|
|
||||||
QResults queries[] = {
|
QResults queries[] = {
|
||||||
{ "date:20080731..20080804", 5},
|
{ "date:20080731..20080804", 5},
|
||||||
{ "date:20080731..20080804 s:gcc", 1},
|
{ "date:20080731..20080804 s:gcc", 1},
|
||||||
|
@ -454,23 +408,19 @@ test_mu_query_dates_sydney (void)
|
||||||
|
|
||||||
old_tz = set_tz ("Australia/Sydney");
|
old_tz = set_tz ("Australia/Sydney");
|
||||||
|
|
||||||
xpath = fill_database (MU_TESTMAILDIR);
|
const auto xpath{make_database(MU_TESTMAILDIR)};
|
||||||
g_assert (xpath != NULL);
|
g_assert_false (xpath.empty());
|
||||||
|
|
||||||
for (i = 0; i != G_N_ELEMENTS(queries); ++i)
|
for (i = 0; i != G_N_ELEMENTS(queries); ++i)
|
||||||
g_assert_cmpuint (run_and_count_matches
|
g_assert_cmpuint (run_and_count_matches
|
||||||
(xpath, queries[i].query),
|
(xpath, queries[i].query),
|
||||||
==, queries[i].count);
|
==, queries[i].count);
|
||||||
|
|
||||||
g_free (xpath);
|
|
||||||
set_tz (old_tz);
|
set_tz (old_tz);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
test_mu_query_dates_la (void)
|
test_mu_query_dates_la (void)
|
||||||
{
|
{
|
||||||
gchar *xpath;
|
|
||||||
int i;
|
int i;
|
||||||
const char *old_tz;
|
const char *old_tz;
|
||||||
|
|
||||||
|
@ -486,8 +436,8 @@ test_mu_query_dates_la (void)
|
||||||
|
|
||||||
old_tz = set_tz ("America/Los_Angeles");
|
old_tz = set_tz ("America/Los_Angeles");
|
||||||
|
|
||||||
xpath = fill_database (MU_TESTMAILDIR);
|
const auto xpath = make_database (MU_TESTMAILDIR);
|
||||||
g_assert (xpath != NULL);
|
g_assert_false (xpath.empty());
|
||||||
|
|
||||||
for (i = 0; i != G_N_ELEMENTS(queries); ++i) {
|
for (i = 0; i != G_N_ELEMENTS(queries); ++i) {
|
||||||
/* g_print ("%s\n", queries[i].query); */
|
/* g_print ("%s\n", queries[i].query); */
|
||||||
|
@ -496,7 +446,6 @@ test_mu_query_dates_la (void)
|
||||||
==, queries[i].count);
|
==, queries[i].count);
|
||||||
}
|
}
|
||||||
|
|
||||||
g_free (xpath);
|
|
||||||
set_tz (old_tz);
|
set_tz (old_tz);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -671,26 +620,19 @@ test_mu_query_tags_02 (void)
|
||||||
static void
|
static void
|
||||||
test_mu_query_threads_compilation_error (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_cmpuint (run_and_count_matches
|
||||||
g_assert (xpath != NULL);
|
(xpath, "msgid:uwsireh25.fsf@one.dot.net"),
|
||||||
|
|
||||||
g_assert_cmpuint (run_and_count_matches_with_query_flags
|
|
||||||
(xpath, "msgid:uwsireh25.fsf@one.dot.net",
|
|
||||||
MU_QUERY_FLAG_NONE),
|
|
||||||
==, 1);
|
==, 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",
|
(xpath, "msgid:uwsireh25.fsf@one.dot.net",
|
||||||
MU_QUERY_FLAG_INCLUDE_RELATED),
|
Query::Flags::IncludeRelated),
|
||||||
==, 3);
|
==, 3);
|
||||||
|
|
||||||
g_free (xpath);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int
|
int
|
||||||
main (int argc, char *argv[])
|
main (int argc, char *argv[])
|
||||||
{
|
{
|
||||||
|
@ -700,11 +642,11 @@ main (int argc, char *argv[])
|
||||||
|
|
||||||
g_test_init (&argc, &argv, NULL);
|
g_test_init (&argc, &argv, NULL);
|
||||||
|
|
||||||
DB_PATH1 = fill_database (MU_TESTMAILDIR);
|
DB_PATH1 = make_database (MU_TESTMAILDIR);
|
||||||
g_assert (DB_PATH1);
|
g_assert_false (DB_PATH1.empty());
|
||||||
|
|
||||||
DB_PATH2 = fill_database (MU_TESTMAILDIR2);
|
DB_PATH2 = make_database (MU_TESTMAILDIR2);
|
||||||
g_assert (DB_PATH2);
|
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-01", test_mu_query_01);
|
||||||
g_test_add_func ("/mu-query/test-mu-query-02", test_mu_query_02);
|
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);
|
test_mu_query_threads_compilation_error);
|
||||||
|
|
||||||
if (!g_test_verbose())
|
if (!g_test_verbose())
|
||||||
g_log_set_handler (NULL,
|
g_log_set_handler (NULL,
|
||||||
(GLogLevelFlags)(G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL|
|
(GLogLevelFlags)(G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL|
|
||||||
G_LOG_FLAG_RECURSION),
|
G_LOG_LEVEL_WARNING|G_LOG_FLAG_RECURSION),
|
||||||
(GLogFunc)black_hole, NULL);
|
(GLogFunc)black_hole, NULL);
|
||||||
|
|
||||||
rv = g_test_run ();
|
rv = g_test_run ();
|
||||||
|
|
||||||
g_free (DB_PATH1);
|
|
||||||
g_free (DB_PATH2);
|
|
||||||
|
|
||||||
return rv;
|
return rv;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
** 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
|
** under the terms of the GNU General Public License as published by the
|
||||||
|
@ -30,10 +28,12 @@
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "test-mu-common.h"
|
#include "test-mu-common.hh"
|
||||||
#include "mu-query.h"
|
#include "mu-query.hh"
|
||||||
#include "utils/mu-str.h"
|
#include "utils/mu-str.h"
|
||||||
|
|
||||||
|
using namespace Mu;
|
||||||
|
|
||||||
struct _tinfo {
|
struct _tinfo {
|
||||||
const char *threadpath;
|
const char *threadpath;
|
||||||
const char *msgid;
|
const char *msgid;
|
||||||
|
@ -93,72 +93,59 @@ foreach_assert_tinfo_equal (MuMsgIter *iter, const tinfo items[], guint n_items)
|
||||||
g_assert (u == n_items);
|
g_assert (u == n_items);
|
||||||
}
|
}
|
||||||
|
|
||||||
static gchar*
|
static std::string
|
||||||
fill_database (const char *testdir)
|
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())
|
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));
|
NULL, NULL));
|
||||||
g_free (cmdline);
|
auto xpath= g_strdup_printf ("%s%c%s", tmpdir, G_DIR_SEPARATOR, "xapian");
|
||||||
xpath= g_strdup_printf ("%s%c%s", tmpdir,
|
|
||||||
G_DIR_SEPARATOR, "xapian");
|
|
||||||
g_free (tmpdir);
|
g_free (tmpdir);
|
||||||
|
|
||||||
return xpath;
|
std::string dbpath{xpath};
|
||||||
|
g_free(xpath);
|
||||||
|
|
||||||
|
return dbpath;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* note: this also *moves the iter* */
|
/* note: this also *moves the iter* */
|
||||||
static MuMsgIter*
|
static MuMsgIter*
|
||||||
run_and_get_iter_full (const char *xpath, const char *query,
|
run_and_get_iter_full (const std::string& xpath, const std::string& expr,
|
||||||
MuMsgFieldId sort_field, MuQueryFlags flags)
|
MuMsgFieldId sort_field,
|
||||||
|
Mu::Query::Flags flags=Mu::Query::Flags::None)
|
||||||
{
|
{
|
||||||
MuQuery *mquery;
|
Mu::Store store{xpath};
|
||||||
MuStore *store;
|
Mu::Query q{store};
|
||||||
MuMsgIter *iter;
|
|
||||||
int myflags;
|
|
||||||
|
|
||||||
store = mu_store_new_readable (xpath, NULL);
|
const auto myflags{flags | Mu::Query::Flags::Threading};
|
||||||
g_assert (store);
|
auto iter = q.run (expr, sort_field, myflags);
|
||||||
|
|
||||||
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);
|
|
||||||
g_assert (iter);
|
g_assert (iter);
|
||||||
|
|
||||||
return iter;
|
return iter;
|
||||||
}
|
}
|
||||||
|
|
||||||
static MuMsgIter*
|
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,
|
return run_and_get_iter_full (xpath, query, MU_MSG_FIELD_ID_DATE);
|
||||||
MU_QUERY_FLAG_NONE);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
test_mu_threads_01 (void)
|
test_mu_threads_01 (void)
|
||||||
{
|
{
|
||||||
gchar *xpath;
|
|
||||||
MuMsgIter *iter;
|
|
||||||
|
|
||||||
const tinfo items [] = {
|
const tinfo items [] = {
|
||||||
{"0", "root0@msg.id", "root0"},
|
{"0", "root0@msg.id", "root0"},
|
||||||
{"0:0", "child0.0@msg.id", "Re: child 0.0"},
|
{"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"}
|
{"4:1", "child4.1@msg.id", "Re: child 4.1"}
|
||||||
};
|
};
|
||||||
|
|
||||||
xpath = fill_database (MU_TESTMAILDIR3);
|
const auto xpath{make_database(MU_TESTMAILDIR3)};
|
||||||
g_assert (xpath != NULL);
|
g_assert (!xpath.empty());
|
||||||
|
|
||||||
iter = run_and_get_iter (xpath, "abc");
|
auto iter = run_and_get_iter (xpath, "abc");
|
||||||
g_assert (iter);
|
g_assert (iter);
|
||||||
g_assert (!mu_msg_iter_is_done(iter));
|
g_assert (!mu_msg_iter_is_done(iter));
|
||||||
|
|
||||||
foreach_assert_tinfo_equal (iter, items, G_N_ELEMENTS (items));
|
foreach_assert_tinfo_equal (iter, items, G_N_ELEMENTS (items));
|
||||||
|
|
||||||
g_free (xpath);
|
|
||||||
mu_msg_iter_destroy (iter);
|
mu_msg_iter_destroy (iter);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
test_mu_threads_rogue (void)
|
test_mu_threads_rogue (void)
|
||||||
{
|
{
|
||||||
gchar *xpath;
|
|
||||||
MuMsgIter *iter;
|
MuMsgIter *iter;
|
||||||
tinfo *items;
|
tinfo *items;
|
||||||
|
|
||||||
|
@ -210,8 +194,8 @@ test_mu_threads_rogue (void)
|
||||||
{"0:1", "cycle0.0.0@msg.id", "cycle0.0.0"}
|
{"0:1", "cycle0.0.0@msg.id", "cycle0.0.0"}
|
||||||
};
|
};
|
||||||
|
|
||||||
xpath = fill_database (MU_TESTMAILDIR3);
|
const auto xpath{make_database (MU_TESTMAILDIR3)};
|
||||||
g_assert (xpath != NULL);
|
g_assert_false (xpath.empty());
|
||||||
|
|
||||||
iter = run_and_get_iter (xpath, "def");
|
iter = run_and_get_iter (xpath, "def");
|
||||||
g_assert (iter);
|
g_assert (iter);
|
||||||
|
@ -226,30 +210,20 @@ test_mu_threads_rogue (void)
|
||||||
items = items2;
|
items = items2;
|
||||||
|
|
||||||
foreach_assert_tinfo_equal (iter, items, G_N_ELEMENTS (items1));
|
foreach_assert_tinfo_equal (iter, items, G_N_ELEMENTS (items1));
|
||||||
|
|
||||||
g_free (xpath);
|
|
||||||
mu_msg_iter_destroy (iter);
|
mu_msg_iter_destroy (iter);
|
||||||
}
|
}
|
||||||
|
|
||||||
static MuMsgIter*
|
static MuMsgIter*
|
||||||
query_testdir (const char *query, MuMsgFieldId sort_field, gboolean descending)
|
query_testdir (const char *query, MuMsgFieldId sort_field, gboolean descending)
|
||||||
{
|
{
|
||||||
MuMsgIter *iter;
|
const auto flags{descending ? Query::Flags::Descending : Query::Flags::None};
|
||||||
gchar *xpath;
|
const auto xpath{make_database(MU_TESTMAILDIR3)};
|
||||||
int flags;
|
g_assert_false (xpath.empty());
|
||||||
|
|
||||||
flags = MU_QUERY_FLAG_NONE;
|
auto iter = run_and_get_iter_full (xpath, query, sort_field, flags);
|
||||||
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);
|
|
||||||
g_assert (iter != NULL);
|
g_assert (iter != NULL);
|
||||||
g_assert (!mu_msg_iter_is_done (iter));
|
g_assert (!mu_msg_iter_is_done (iter));
|
||||||
|
|
||||||
g_free (xpath);
|
|
||||||
return iter;
|
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-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-rogue", test_mu_threads_rogue);
|
||||||
|
|
||||||
g_test_add_func ("/mu-query/test-mu-threads-sort-1st-child-promotes-thread",
|
g_test_add_func ("/mu-query/test-mu-threads-sort-1st-child-promotes-thread",
|
||||||
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",
|
g_test_add_func ("/mu-query/test-mu-threads-sort-2nd-child-promotes-thread",
|
||||||
|
|
Loading…
Reference in New Issue
Block a user