notmuch dump: Fix the sorting of results.

To properly support sorting in notmuch_query we know use an
Enquire object. We also throw in a QueryParser too, so we're
really close to being able to support arbitrary full-text
searches.

I took a look at the supported QueryParser syntax and chose
a set of flags for everything I like, (such as supporting
Boolean operators in either case ("AND" or "and"), supporting
phrase searching, supporting + and - to include/preclude terms,
and supporting a trailing * on any term as a wildcard).
This commit is contained in:
Carl Worth 2009-10-21 00:35:56 -07:00
parent 6a3b68edef
commit 65baa4f4e7
4 changed files with 59 additions and 22 deletions

View file

@ -28,7 +28,7 @@
struct _notmuch_database { struct _notmuch_database {
char *path; char *path;
Xapian::WritableDatabase *xapian_db; Xapian::WritableDatabase *xapian_db;
Xapian::TermGenerator *term_gen; Xapian::QueryParser *query_parser;
}; };
#endif #endif

View file

@ -67,15 +67,6 @@ prefix_t BOOLEAN_PREFIX[] = {
{ "ref", "R" } { "ref", "R" }
}; };
/* Similarly, these value numbers are also chosen to be sup
* compatible. */
typedef enum {
NOTMUCH_VALUE_MESSAGE_ID = 0,
NOTMUCH_VALUE_THREAD = 1,
NOTMUCH_VALUE_DATE = 2
} notmuch_value_t;
static const char * static const char *
find_prefix (const char *name) find_prefix (const char *name)
{ {
@ -459,6 +450,9 @@ notmuch_database_open (const char *path)
try { try {
notmuch->xapian_db = new Xapian::WritableDatabase (xapian_path, notmuch->xapian_db = new Xapian::WritableDatabase (xapian_path,
Xapian::DB_CREATE_OR_OPEN); Xapian::DB_CREATE_OR_OPEN);
notmuch->query_parser = new Xapian::QueryParser;
notmuch->query_parser->set_default_op (Xapian::Query::OP_AND);
notmuch->query_parser->set_database (*notmuch->xapian_db);
} catch (const Xapian::Error &error) { } catch (const Xapian::Error &error) {
fprintf (stderr, "A Xapian exception occurred: %s\n", fprintf (stderr, "A Xapian exception occurred: %s\n",
error.get_msg().c_str()); error.get_msg().c_str());
@ -478,6 +472,7 @@ notmuch_database_open (const char *path)
void void
notmuch_database_close (notmuch_database_t *notmuch) notmuch_database_close (notmuch_database_t *notmuch)
{ {
delete notmuch->query_parser;
delete notmuch->xapian_db; delete notmuch->xapian_db;
free (notmuch->path); free (notmuch->path);
free (notmuch); free (notmuch);

View file

@ -66,6 +66,15 @@ NOTMUCH_BEGIN_DECLS
#endif #endif
#endif #endif
/* These value numbers are chosen to be sup compatible (for now at
* least). */
typedef enum {
NOTMUCH_VALUE_MESSAGE_ID = 0,
NOTMUCH_VALUE_THREAD = 1,
NOTMUCH_VALUE_DATE = 2
} notmuch_value_t;
/* xutil.c */ /* xutil.c */
void * void *
xcalloc (size_t nmemb, size_t size); xcalloc (size_t nmemb, size_t size);

View file

@ -31,8 +31,8 @@ struct _notmuch_query {
struct _notmuch_results { struct _notmuch_results {
notmuch_database_t *notmuch; notmuch_database_t *notmuch;
Xapian::PostingIterator iterator; Xapian::MSetIterator iterator;
Xapian::PostingIterator iterator_end; Xapian::MSetIterator iterator_end;
}; };
notmuch_query_t * notmuch_query_t *
@ -69,8 +69,8 @@ notmuch_query_set_sort (notmuch_query_t *query, notmuch_sort_t sort)
static int static int
_notmuch_results_destructor (notmuch_results_t *results) _notmuch_results_destructor (notmuch_results_t *results)
{ {
results->iterator.~PostingIterator (); results->iterator.~MSetIterator ();
results->iterator_end.~PostingIterator (); results->iterator_end.~MSetIterator ();
return 0; return 0;
} }
@ -78,6 +78,8 @@ _notmuch_results_destructor (notmuch_results_t *results)
notmuch_results_t * notmuch_results_t *
notmuch_query_search (notmuch_query_t *query) notmuch_query_search (notmuch_query_t *query)
{ {
notmuch_database_t *notmuch = query->notmuch;
const char *query_string = query->query_string;
notmuch_results_t *results; notmuch_results_t *results;
results = talloc (query, notmuch_results_t); results = talloc (query, notmuch_results_t);
@ -85,19 +87,50 @@ notmuch_query_search (notmuch_query_t *query)
return NULL; return NULL;
try { try {
if (strlen (query->query_string)) { Xapian::Enquire enquire (*notmuch->xapian_db);
fprintf (stderr, "Error: Arbitrary search strings are not supported yet. Come back soon!\n"); Xapian::Query mail_query ("Kmail");
exit (1); Xapian::Query string_query, final_query;
Xapian::MSet mset;
unsigned int flags = (Xapian::QueryParser::FLAG_BOOLEAN &
Xapian::QueryParser::FLAG_PHRASE &
Xapian::QueryParser::FLAG_LOVEHATE &
Xapian::QueryParser::FLAG_BOOLEAN_ANY_CASE &
Xapian::QueryParser::FLAG_WILDCARD);
if (strcmp (query_string, "") == 0) {
final_query = mail_query;
} else {
string_query = notmuch->query_parser->
parse_query (query_string, flags);
final_query = Xapian::Query (Xapian::Query::OP_AND,
mail_query, string_query);
} }
results->notmuch = query->notmuch; switch (query->sort) {
new (&results->iterator) Xapian::PostingIterator (); case NOTMUCH_SORT_DATE_OLDEST_FIRST:
new (&results->iterator_end) Xapian::PostingIterator (); enquire.set_sort_by_value (NOTMUCH_VALUE_DATE, FALSE);
break;
case NOTMUCH_SORT_DATE_NEWEST_FIRST:
enquire.set_sort_by_value (NOTMUCH_VALUE_DATE, TRUE);
break;
case NOTMUCH_SORT_MESSAGE_ID:
enquire.set_sort_by_value (NOTMUCH_VALUE_MESSAGE_ID, FALSE);
break;
}
enquire.set_query (final_query);
mset = enquire.get_mset (0, notmuch->xapian_db->get_doccount ());
results->notmuch = notmuch;
new (&results->iterator) Xapian::MSetIterator ();
new (&results->iterator_end) Xapian::MSetIterator ();
talloc_set_destructor (results, _notmuch_results_destructor); talloc_set_destructor (results, _notmuch_results_destructor);
results->iterator = query->notmuch->xapian_db->postlist_begin (""); results->iterator = mset.begin ();
results->iterator_end = query->notmuch->xapian_db->postlist_end (""); results->iterator_end = mset.end ();
} catch (const Xapian::Error &error) { } catch (const Xapian::Error &error) {
fprintf (stderr, "A Xapian exception occurred: %s\n", fprintf (stderr, "A Xapian exception occurred: %s\n",