lib: Update for new querying machinery

Port to c++ and use new APIs.
This commit is contained in:
Dirk-Jan C. Binnema
2020-11-28 10:15:49 +02:00
parent 95dffb98a6
commit e282d80bc0
24 changed files with 433 additions and 513 deletions

View File

@ -34,13 +34,12 @@
#include <glib.h>
#include <glib/gprintf.h>
#include "mu-msg.h"
#include "mu-runtime.hh"
#include "mu-maildir.h"
#include "mu-maildir.hh"
#include "mu-query.hh"
#include "index/mu-indexer.hh"
#include "mu-store.hh"
#include "mu-msg-part.h"
#include "mu-msg-part.hh"
#include "mu-contacts.hh"
#include "utils/mu-str.h"
@ -90,7 +89,7 @@ struct Server::Private {
void output_sexp(Sexp::List&& lst) const {
output_sexp(Sexp::make_list(std::move(lst)));
}
size_t output_sexp (MuMsgIter *iter, unsigned maxnum);
size_t output_sexp (const QueryResults& qres, unsigned maxnum);
//
// handlers for various commands.
@ -359,8 +358,7 @@ Server::Private::add_handler (const Parameters& params)
path.c_str(), docid);
Sexp::List update;
update.add_prop(":update", Mu::msg_to_sexp(msg, docid, NULL,
MU_MSG_OPTION_VERIFY));
update.add_prop(":update", Mu::msg_to_sexp(msg, docid, {}, MU_MSG_OPTION_VERIFY));
output_sexp(Sexp::make_list(std::move(update)));
mu_msg_unref(msg);
}
@ -617,18 +615,17 @@ docids_for_msgid (const Query& q, const std::string& msgid, size_t max=100)
g_free(tmp);
GError *gerr{};
auto iter{q.run(expr , MU_MSG_FIELD_ID_NONE, Query::Flags::None, max)};
const auto res{q.run(expr, MU_MSG_FIELD_ID_NONE, QueryFlags::None, max)};
g_free (expr);
if (!iter)
if (!res)
throw Error(Error::Code::Store, &gerr, "failed to run msgid-query");
if (mu_msg_iter_is_done (iter))
else if (res->empty())
throw Error(Error::Code::NotFound,
"could not find message(s) for msgid %s", msgid.c_str());
std::vector<DocId> docids;
do {
docids.emplace_back(mu_msg_iter_get_docid (iter));
} while (mu_msg_iter_next (iter));
mu_msg_iter_destroy (iter);
"could not find message(s) for msgid %s", msgid.c_str());
std::vector<DocId> docids{};
for(auto&& mi: *res)
docids.emplace_back(mi.doc_id());
return docids;
}
@ -676,24 +673,23 @@ determine_docids (const Query& q, const Parameters& params)
size_t
Server::Private::output_sexp (MuMsgIter *iter, unsigned maxnum)
Server::Private::output_sexp (const QueryResults& qres, unsigned maxnum)
{
size_t n{};
while (!mu_msg_iter_is_done (iter) && n < maxnum) {
for (auto&& mi: qres) {
MuMsg *msg;
msg = mu_msg_iter_get_msg_floating (iter);
if (n >= maxnum)
break;
++n;
auto msg{mi.floating_msg()};
if (!msg)
continue;
if (mu_msg_is_readable (msg)) {
const MuMsgIterThreadInfo* ti;
ti = mu_msg_iter_get_thread_info (iter);
output_sexp(Mu::msg_to_sexp(msg,
mu_msg_iter_get_docid (iter),
ti, MU_MSG_OPTION_HEADERS_ONLY));
++n;
}
mu_msg_iter_next (iter);
auto qm{mi.query_match()};
output_sexp(Mu::msg_to_sexp(msg, mi.doc_id(),
qm, MU_MSG_OPTION_HEADERS_ONLY));
}
return n;
}
@ -718,20 +714,19 @@ Server::Private::find_handler (const Parameters& params)
sortfieldstr.c_str()};
}
auto qflags{Query::Flags::None};
auto qflags{QueryFlags::None};
if (descending)
qflags |= Query::Flags::Descending;
qflags |= QueryFlags::Descending;
if (skip_dups)
qflags |= Query::Flags::SkipDups;
qflags |= QueryFlags::SkipDuplicates;
if (include_related)
qflags |= Query::Flags::IncludeRelated;
qflags |= QueryFlags::IncludeRelated;
if (threads)
qflags |= Query::Flags::Threading;
qflags |= QueryFlags::Threading;
GError *gerr{};
auto miter{query().run(q, sort_field, qflags, maxnum, &gerr)};
if (!miter)
throw Error(Error::Code::Query, &gerr, "failed to run query");
auto qres{query().run(q, sort_field, qflags, maxnum)};
if (!qres)
throw Error(Error::Code::Query, "failed to run query");
/* before sending new results, send an 'erase' message, so the frontend
* knows it should erase the headers buffer. this will ensure that the
@ -743,16 +738,13 @@ Server::Private::find_handler (const Parameters& params)
}
{
const auto foundnum{output_sexp (miter, maxnum)};
const auto foundnum{output_sexp (*qres, maxnum)};
Sexp::List lst;
lst.add_prop(":found", Sexp::make_number(foundnum));
output_sexp(std::move(lst));
}
//output_sexp ("(:found %u)", foundnum);
mu_msg_iter_destroy (miter);
}
void
Server::Private::help_handler (const Parameters& params)
{
@ -886,7 +878,7 @@ perform_move (Store& store, DocId docid, MuMsg *msg, const std::string& maildira
throw Error{Error::Code::Store, "failed to store updated message"};
Sexp::List seq;
seq.add_prop(":update", msg_to_sexp (msg, docid, NULL, MU_MSG_OPTION_VERIFY));
seq.add_prop(":update", msg_to_sexp (msg, docid, {}, MU_MSG_OPTION_VERIFY));
/* note, the :move t thing is a hint to the frontend that it
* could remove the particular header */
if (different_mdir)