1 /* query.cc - Support for searching a notmuch database
2  *
3  * Copyright © 2009 Carl Worth
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see https://www.gnu.org/licenses/ .
17  *
18  * Author: Carl Worth <cworth@cworth.org>
19  */
20 
21 #include "notmuch-private.h"
22 #include "database-private.h"
23 
24 #include <glib.h> /* GHashTable, GPtrArray */
25 
26 struct _notmuch_query {
27     notmuch_database_t *notmuch;
28     const char *query_string;
29     notmuch_sort_t sort;
30     notmuch_string_list_t *exclude_terms;
31     notmuch_exclude_t omit_excluded;
32     bool parsed;
33     notmuch_query_syntax_t syntax;
34     Xapian::Query xapian_query;
35     std::set<std::string> terms;
36 };
37 
38 typedef struct _notmuch_mset_messages {
39     notmuch_messages_t base;
40     notmuch_database_t *notmuch;
41     Xapian::MSetIterator iterator;
42     Xapian::MSetIterator iterator_end;
43 } notmuch_mset_messages_t;
44 
45 struct _notmuch_doc_id_set {
46     unsigned char *bitmap;
47     unsigned int bound;
48 };
49 
50 #define DOCIDSET_WORD(bit) ((bit) / CHAR_BIT)
51 #define DOCIDSET_BIT(bit) ((bit) % CHAR_BIT)
52 
53 struct _notmuch_threads {
54     notmuch_query_t *query;
55 
56     /* The ordered list of doc ids matched by the query. */
57     GArray *doc_ids;
58     /* Our iterator's current position in doc_ids. */
59     unsigned int doc_id_pos;
60     /* The set of matched docid's that have not been assigned to a
61      * thread. Initially, this contains every docid in doc_ids. */
62     notmuch_doc_id_set_t match_set;
63 };
64 
65 /* We need this in the message functions so forward declare. */
66 static bool
67 _notmuch_doc_id_set_init (void *ctx,
68 			  notmuch_doc_id_set_t *doc_ids,
69 			  GArray *arr);
70 
71 static bool
_debug_query(void)72 _debug_query (void)
73 {
74     char *env = getenv ("NOTMUCH_DEBUG_QUERY");
75 
76     return (env && strcmp (env, "") != 0);
77 }
78 
79 /* Explicit destructor call for placement new */
80 static int
_notmuch_query_destructor(notmuch_query_t * query)81 _notmuch_query_destructor (notmuch_query_t *query)
82 {
83     query->xapian_query.~Query();
84     query->terms.~set<std::string>();
85     return 0;
86 }
87 
88 static notmuch_query_t *
_notmuch_query_constructor(notmuch_database_t * notmuch,const char * query_string)89 _notmuch_query_constructor (notmuch_database_t *notmuch,
90 			    const char *query_string)
91 {
92     notmuch_query_t *query;
93 
94     if (_debug_query ())
95 	fprintf (stderr, "Query string is:\n%s\n", query_string);
96 
97     query = talloc (notmuch, notmuch_query_t);
98     if (unlikely (query == NULL))
99 	return NULL;
100 
101     new (&query->xapian_query) Xapian::Query ();
102     new (&query->terms) std::set<std::string> ();
103     query->parsed = false;
104 
105     talloc_set_destructor (query, _notmuch_query_destructor);
106 
107     query->notmuch = notmuch;
108 
109     if (query_string)
110 	query->query_string = talloc_strdup (query, query_string);
111     else
112 	query->query_string = NULL;
113 
114     query->sort = NOTMUCH_SORT_NEWEST_FIRST;
115 
116     query->exclude_terms = _notmuch_string_list_create (query);
117 
118     query->omit_excluded = NOTMUCH_EXCLUDE_TRUE;
119 
120     return query;
121 }
122 
123 notmuch_query_t *
notmuch_query_create(notmuch_database_t * notmuch,const char * query_string)124 notmuch_query_create (notmuch_database_t *notmuch,
125 		      const char *query_string)
126 {
127 
128     notmuch_query_t *query;
129     notmuch_status_t status;
130 
131     status = notmuch_query_create_with_syntax (notmuch, query_string,
132 					       NOTMUCH_QUERY_SYNTAX_XAPIAN,
133 					       &query);
134     if (status)
135 	return NULL;
136 
137     return query;
138 }
139 
140 notmuch_status_t
notmuch_query_create_with_syntax(notmuch_database_t * notmuch,const char * query_string,notmuch_query_syntax_t syntax,notmuch_query_t ** output)141 notmuch_query_create_with_syntax (notmuch_database_t *notmuch,
142 				  const char *query_string,
143 				  notmuch_query_syntax_t syntax,
144 				  notmuch_query_t **output)
145 {
146 
147     notmuch_query_t *query;
148 
149     if (! output)
150 	return NOTMUCH_STATUS_NULL_POINTER;
151 
152     query = _notmuch_query_constructor (notmuch, query_string);
153     if (! query)
154 	return NOTMUCH_STATUS_OUT_OF_MEMORY;
155 
156     if (syntax == NOTMUCH_QUERY_SYNTAX_SEXP && ! HAVE_SFSEXP) {
157 	_notmuch_database_log (notmuch, "sexp query parser not available");
158 	return NOTMUCH_STATUS_ILLEGAL_ARGUMENT;
159     }
160 
161     query->syntax = syntax;
162 
163     *output = query;
164 
165     return NOTMUCH_STATUS_SUCCESS;
166 }
167 
168 static void
_notmuch_query_cache_terms(notmuch_query_t * query)169 _notmuch_query_cache_terms (notmuch_query_t *query)
170 {
171     /* Xapian doesn't support skip_to on terms from a query since
172      *  they are unordered, so cache a copy of all terms in
173      *  something searchable.
174      */
175 
176     for (Xapian::TermIterator t = query->xapian_query.get_terms_begin ();
177 	 t != query->xapian_query.get_terms_end (); ++t)
178 	query->terms.insert (*t);
179 }
180 
181 notmuch_status_t
_notmuch_query_string_to_xapian_query(notmuch_database_t * notmuch,std::string query_string,Xapian::Query & output,std::string & msg)182 _notmuch_query_string_to_xapian_query (notmuch_database_t *notmuch,
183 				       std::string query_string,
184 				       Xapian::Query &output,
185 				       std::string &msg)
186 {
187     try {
188 	if (query_string == "" || query_string == "*") {
189 	    output = Xapian::Query::MatchAll;
190 	} else {
191 	    output =
192 		notmuch->query_parser->
193 		parse_query (query_string, NOTMUCH_QUERY_PARSER_FLAGS);
194 	}
195     } catch (const Xapian::Error &error) {
196 	if (! notmuch->exception_reported) {
197 	    _notmuch_database_log (notmuch,
198 				   "A Xapian exception occurred parsing query: %s\n",
199 				   error.get_msg ().c_str ());
200 	    _notmuch_database_log_append (notmuch,
201 					  "Query string was: %s\n",
202 					  query_string.c_str ());
203 	    notmuch->exception_reported = true;
204 	}
205 
206 	msg = error.get_msg ();
207 	return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
208     }
209     return NOTMUCH_STATUS_SUCCESS;
210 }
211 
212 static notmuch_status_t
_notmuch_query_ensure_parsed_xapian(notmuch_query_t * query)213 _notmuch_query_ensure_parsed_xapian (notmuch_query_t *query)
214 {
215     notmuch_status_t status;
216     std::string msg; /* ignored */
217 
218     status =  _notmuch_query_string_to_xapian_query (query->notmuch, query->query_string,
219 						     query->xapian_query, msg);
220     if (status)
221 	return status;
222 
223     query->parsed = true;
224 
225     _notmuch_query_cache_terms (query);
226 
227     return NOTMUCH_STATUS_SUCCESS;
228 }
229 
230 static notmuch_status_t
_notmuch_query_ensure_parsed_sexpr(notmuch_query_t * query)231 _notmuch_query_ensure_parsed_sexpr (notmuch_query_t *query)
232 {
233     notmuch_status_t status;
234 
235     if (query->parsed)
236 	return NOTMUCH_STATUS_SUCCESS;
237 
238     status = _notmuch_sexp_string_to_xapian_query (query->notmuch, query->query_string,
239 						   query->xapian_query);
240     if (status)
241 	return status;
242 
243     _notmuch_query_cache_terms (query);
244     return NOTMUCH_STATUS_SUCCESS;
245 }
246 
247 static notmuch_status_t
_notmuch_query_ensure_parsed(notmuch_query_t * query)248 _notmuch_query_ensure_parsed (notmuch_query_t *query)
249 {
250     if (query->parsed)
251 	return NOTMUCH_STATUS_SUCCESS;
252 
253 #if HAVE_SFSEXP
254     if (query->syntax == NOTMUCH_QUERY_SYNTAX_SEXP)
255 	return _notmuch_query_ensure_parsed_sexpr (query);
256 #endif
257 
258     return _notmuch_query_ensure_parsed_xapian (query);
259 }
260 
261 const char *
notmuch_query_get_query_string(const notmuch_query_t * query)262 notmuch_query_get_query_string (const notmuch_query_t *query)
263 {
264     return query->query_string;
265 }
266 
267 void
notmuch_query_set_omit_excluded(notmuch_query_t * query,notmuch_exclude_t omit_excluded)268 notmuch_query_set_omit_excluded (notmuch_query_t *query,
269 				 notmuch_exclude_t omit_excluded)
270 {
271     query->omit_excluded = omit_excluded;
272 }
273 
274 void
notmuch_query_set_sort(notmuch_query_t * query,notmuch_sort_t sort)275 notmuch_query_set_sort (notmuch_query_t *query, notmuch_sort_t sort)
276 {
277     query->sort = sort;
278 }
279 
280 notmuch_sort_t
notmuch_query_get_sort(const notmuch_query_t * query)281 notmuch_query_get_sort (const notmuch_query_t *query)
282 {
283     return query->sort;
284 }
285 
286 notmuch_status_t
notmuch_query_add_tag_exclude(notmuch_query_t * query,const char * tag)287 notmuch_query_add_tag_exclude (notmuch_query_t *query, const char *tag)
288 {
289     notmuch_status_t status;
290     char *term;
291 
292     status = _notmuch_query_ensure_parsed (query);
293     if (status)
294 	return status;
295 
296     term = talloc_asprintf (query, "%s%s", _find_prefix ("tag"), tag);
297     if (query->terms.count (term) != 0)
298 	return NOTMUCH_STATUS_IGNORED;
299 
300     _notmuch_string_list_append (query->exclude_terms, term);
301     return NOTMUCH_STATUS_SUCCESS;
302 }
303 
304 /* We end up having to call the destructors explicitly because we had
305  * to use "placement new" in order to initialize C++ objects within a
306  * block that we allocated with talloc. So C++ is making talloc
307  * slightly less simple to use, (we wouldn't need
308  * talloc_set_destructor at all otherwise).
309  */
310 static int
_notmuch_messages_destructor(notmuch_mset_messages_t * messages)311 _notmuch_messages_destructor (notmuch_mset_messages_t *messages)
312 {
313     messages->iterator.~MSetIterator ();
314     messages->iterator_end.~MSetIterator ();
315 
316     return 0;
317 }
318 
319 /* Return a query that matches messages with the excluded tags
320  * registered with query. The caller of this function has to combine the returned
321  * query appropriately.*/
322 static Xapian::Query
_notmuch_exclude_tags(notmuch_query_t * query)323 _notmuch_exclude_tags (notmuch_query_t *query)
324 {
325     Xapian::Query exclude_query = Xapian::Query::MatchNothing;
326 
327     for (notmuch_string_node_t *term = query->exclude_terms->head; term;
328 	 term = term->next) {
329 	exclude_query = Xapian::Query (Xapian::Query::OP_OR,
330 				       exclude_query, Xapian::Query (term->string));
331     }
332     return exclude_query;
333 }
334 
335 
336 notmuch_status_t
notmuch_query_search_messages_st(notmuch_query_t * query,notmuch_messages_t ** out)337 notmuch_query_search_messages_st (notmuch_query_t *query,
338 				  notmuch_messages_t **out)
339 {
340     return notmuch_query_search_messages (query, out);
341 }
342 
343 notmuch_status_t
notmuch_query_search_messages(notmuch_query_t * query,notmuch_messages_t ** out)344 notmuch_query_search_messages (notmuch_query_t *query,
345 			       notmuch_messages_t **out)
346 {
347     return _notmuch_query_search_documents (query, "mail", out);
348 }
349 
350 notmuch_status_t
_notmuch_query_search_documents(notmuch_query_t * query,const char * type,notmuch_messages_t ** out)351 _notmuch_query_search_documents (notmuch_query_t *query,
352 				 const char *type,
353 				 notmuch_messages_t **out)
354 {
355     notmuch_database_t *notmuch = query->notmuch;
356     notmuch_mset_messages_t *messages;
357     notmuch_status_t status;
358 
359     status = _notmuch_query_ensure_parsed (query);
360     if (status)
361 	return status;
362 
363     messages = talloc (query, notmuch_mset_messages_t);
364     if (unlikely (messages == NULL))
365 	return NOTMUCH_STATUS_OUT_OF_MEMORY;
366 
367     try {
368 
369 	messages->base.is_of_list_type = false;
370 	messages->base.iterator = NULL;
371 	messages->notmuch = notmuch;
372 	new (&messages->iterator) Xapian::MSetIterator ();
373 	new (&messages->iterator_end) Xapian::MSetIterator ();
374 
375 	talloc_set_destructor (messages, _notmuch_messages_destructor);
376 
377 	Xapian::Enquire enquire (*notmuch->xapian_db);
378 	Xapian::Query mail_query (talloc_asprintf (query, "%s%s",
379 						   _find_prefix ("type"),
380 						   type));
381 	Xapian::Query final_query, exclude_query;
382 	Xapian::MSet mset;
383 	Xapian::MSetIterator iterator;
384 
385 	final_query = Xapian::Query (Xapian::Query::OP_AND,
386 				     mail_query, query->xapian_query);
387 
388 	messages->base.excluded_doc_ids = NULL;
389 
390 	if ((query->omit_excluded != NOTMUCH_EXCLUDE_FALSE) && (query->exclude_terms)) {
391 	    exclude_query = _notmuch_exclude_tags (query);
392 
393 	    if (query->omit_excluded == NOTMUCH_EXCLUDE_TRUE ||
394 		query->omit_excluded == NOTMUCH_EXCLUDE_ALL) {
395 		final_query = Xapian::Query (Xapian::Query::OP_AND_NOT,
396 					     final_query, exclude_query);
397 	    } else { /* NOTMUCH_EXCLUDE_FLAG */
398 		exclude_query = Xapian::Query (Xapian::Query::OP_AND,
399 					       exclude_query, final_query);
400 
401 		enquire.set_weighting_scheme (Xapian::BoolWeight ());
402 		enquire.set_query (exclude_query);
403 
404 		mset = enquire.get_mset (0, notmuch->xapian_db->get_doccount ());
405 
406 		GArray *excluded_doc_ids = g_array_new (false, false, sizeof (unsigned int));
407 
408 		for (iterator = mset.begin (); iterator != mset.end (); iterator++) {
409 		    unsigned int doc_id = *iterator;
410 		    g_array_append_val (excluded_doc_ids, doc_id);
411 		}
412 		messages->base.excluded_doc_ids = talloc (messages, _notmuch_doc_id_set);
413 		_notmuch_doc_id_set_init (query, messages->base.excluded_doc_ids,
414 					  excluded_doc_ids);
415 		g_array_unref (excluded_doc_ids);
416 	    }
417 	}
418 
419 
420 	enquire.set_weighting_scheme (Xapian::BoolWeight ());
421 
422 	switch (query->sort) {
423 	case NOTMUCH_SORT_OLDEST_FIRST:
424 	    enquire.set_sort_by_value (NOTMUCH_VALUE_TIMESTAMP, false);
425 	    break;
426 	case NOTMUCH_SORT_NEWEST_FIRST:
427 	    enquire.set_sort_by_value (NOTMUCH_VALUE_TIMESTAMP, true);
428 	    break;
429 	case NOTMUCH_SORT_MESSAGE_ID:
430 	    enquire.set_sort_by_value (NOTMUCH_VALUE_MESSAGE_ID, false);
431 	    break;
432 	case NOTMUCH_SORT_UNSORTED:
433 	    break;
434 	}
435 
436 	if (_debug_query ()) {
437 	    fprintf (stderr, "Exclude query is:\n%s\n",
438 		     exclude_query.get_description ().c_str ());
439 	    fprintf (stderr, "Final query is:\n%s\n",
440 		     final_query.get_description ().c_str ());
441 	}
442 
443 	enquire.set_query (final_query);
444 
445 	mset = enquire.get_mset (0, notmuch->xapian_db->get_doccount ());
446 
447 	messages->iterator = mset.begin ();
448 	messages->iterator_end = mset.end ();
449 
450 	*out = &messages->base;
451 	return NOTMUCH_STATUS_SUCCESS;
452 
453     } catch (const Xapian::Error &error) {
454 	_notmuch_database_log (notmuch,
455 			       "A Xapian exception occurred performing query: %s\n",
456 			       error.get_msg ().c_str ());
457 	_notmuch_database_log_append (notmuch,
458 				      "Query string was: %s\n",
459 				      query->query_string);
460 
461 	notmuch->exception_reported = true;
462 	talloc_free (messages);
463 	return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
464     }
465 }
466 
467 bool
_notmuch_mset_messages_valid(notmuch_messages_t * messages)468 _notmuch_mset_messages_valid (notmuch_messages_t *messages)
469 {
470     notmuch_mset_messages_t *mset_messages;
471 
472     mset_messages = (notmuch_mset_messages_t *) messages;
473 
474     return (mset_messages->iterator != mset_messages->iterator_end);
475 }
476 
477 static Xapian::docid
_notmuch_mset_messages_get_doc_id(notmuch_messages_t * messages)478 _notmuch_mset_messages_get_doc_id (notmuch_messages_t *messages)
479 {
480     notmuch_mset_messages_t *mset_messages;
481 
482     mset_messages = (notmuch_mset_messages_t *) messages;
483 
484     if (! _notmuch_mset_messages_valid (&mset_messages->base))
485 	return 0;
486 
487     return *mset_messages->iterator;
488 }
489 
490 notmuch_message_t *
_notmuch_mset_messages_get(notmuch_messages_t * messages)491 _notmuch_mset_messages_get (notmuch_messages_t *messages)
492 {
493     notmuch_message_t *message;
494     Xapian::docid doc_id;
495     notmuch_private_status_t status;
496     notmuch_mset_messages_t *mset_messages;
497 
498     mset_messages = (notmuch_mset_messages_t *) messages;
499 
500     if (! _notmuch_mset_messages_valid (&mset_messages->base))
501 	return NULL;
502 
503     doc_id = *mset_messages->iterator;
504 
505     message = _notmuch_message_create (mset_messages,
506 				       mset_messages->notmuch, doc_id,
507 				       &status);
508 
509     if (message == NULL &&
510 	status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND) {
511 	INTERNAL_ERROR ("a messages iterator contains a non-existent document ID.\n");
512     }
513 
514     if (messages->excluded_doc_ids &&
515 	_notmuch_doc_id_set_contains (messages->excluded_doc_ids, doc_id))
516 	notmuch_message_set_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED, true);
517 
518     return message;
519 }
520 
521 void
_notmuch_mset_messages_move_to_next(notmuch_messages_t * messages)522 _notmuch_mset_messages_move_to_next (notmuch_messages_t *messages)
523 {
524     notmuch_mset_messages_t *mset_messages;
525 
526     mset_messages = (notmuch_mset_messages_t *) messages;
527 
528     mset_messages->iterator++;
529 }
530 
531 static bool
_notmuch_doc_id_set_init(void * ctx,notmuch_doc_id_set_t * doc_ids,GArray * arr)532 _notmuch_doc_id_set_init (void *ctx,
533 			  notmuch_doc_id_set_t *doc_ids,
534 			  GArray *arr)
535 {
536     unsigned int max = 0;
537     unsigned char *bitmap;
538 
539     for (unsigned int i = 0; i < arr->len; i++)
540 	max = MAX (max, g_array_index (arr, unsigned int, i));
541     bitmap = talloc_zero_array (ctx, unsigned char, DOCIDSET_WORD (max) + 1);
542 
543     if (bitmap == NULL)
544 	return false;
545 
546     doc_ids->bitmap = bitmap;
547     doc_ids->bound = max + 1;
548 
549     for (unsigned int i = 0; i < arr->len; i++) {
550 	unsigned int doc_id = g_array_index (arr, unsigned int, i);
551 	bitmap[DOCIDSET_WORD (doc_id)] |= 1 << DOCIDSET_BIT (doc_id);
552     }
553 
554     return true;
555 }
556 
557 bool
_notmuch_doc_id_set_contains(notmuch_doc_id_set_t * doc_ids,unsigned int doc_id)558 _notmuch_doc_id_set_contains (notmuch_doc_id_set_t *doc_ids,
559 			      unsigned int doc_id)
560 {
561     if (doc_id >= doc_ids->bound)
562 	return false;
563     return doc_ids->bitmap[DOCIDSET_WORD (doc_id)] & (1 << DOCIDSET_BIT (doc_id));
564 }
565 
566 void
_notmuch_doc_id_set_remove(notmuch_doc_id_set_t * doc_ids,unsigned int doc_id)567 _notmuch_doc_id_set_remove (notmuch_doc_id_set_t *doc_ids,
568 			    unsigned int doc_id)
569 {
570     if (doc_id < doc_ids->bound)
571 	doc_ids->bitmap[DOCIDSET_WORD (doc_id)] &= ~(1 << DOCIDSET_BIT (doc_id));
572 }
573 
574 /* Glib objects force use to use a talloc destructor as well, (but not
575  * nearly as ugly as the for messages due to C++ objects). At
576  * this point, I'd really like to have some talloc-friendly
577  * equivalents for the few pieces of glib that I'm using. */
578 static int
_notmuch_threads_destructor(notmuch_threads_t * threads)579 _notmuch_threads_destructor (notmuch_threads_t *threads)
580 {
581     if (threads->doc_ids)
582 	g_array_unref (threads->doc_ids);
583 
584     return 0;
585 }
586 
587 notmuch_status_t
notmuch_query_search_threads_st(notmuch_query_t * query,notmuch_threads_t ** out)588 notmuch_query_search_threads_st (notmuch_query_t *query, notmuch_threads_t **out)
589 {
590     return notmuch_query_search_threads (query, out);
591 }
592 
593 notmuch_status_t
notmuch_query_search_threads(notmuch_query_t * query,notmuch_threads_t ** out)594 notmuch_query_search_threads (notmuch_query_t *query,
595 			      notmuch_threads_t **out)
596 {
597     notmuch_threads_t *threads;
598     notmuch_messages_t *messages;
599     notmuch_status_t status;
600 
601     threads = talloc (query, notmuch_threads_t);
602     if (threads == NULL)
603 	return NOTMUCH_STATUS_OUT_OF_MEMORY;
604     threads->doc_ids = NULL;
605     talloc_set_destructor (threads, _notmuch_threads_destructor);
606 
607     threads->query = query;
608 
609     status = notmuch_query_search_messages (query, &messages);
610     if (status) {
611 	talloc_free (threads);
612 	return status;
613     }
614 
615     threads->doc_ids = g_array_new (false, false, sizeof (unsigned int));
616     while (notmuch_messages_valid (messages)) {
617 	unsigned int doc_id = _notmuch_mset_messages_get_doc_id (messages);
618 	g_array_append_val (threads->doc_ids, doc_id);
619 	notmuch_messages_move_to_next (messages);
620     }
621     threads->doc_id_pos = 0;
622 
623     talloc_free (messages);
624 
625     if (! _notmuch_doc_id_set_init (threads, &threads->match_set,
626 				    threads->doc_ids)) {
627 	talloc_free (threads);
628 	return NOTMUCH_STATUS_OUT_OF_MEMORY;
629     }
630 
631     *out = threads;
632     return NOTMUCH_STATUS_SUCCESS;
633 }
634 
635 void
notmuch_query_destroy(notmuch_query_t * query)636 notmuch_query_destroy (notmuch_query_t *query)
637 {
638     talloc_free (query);
639 }
640 
641 notmuch_bool_t
notmuch_threads_valid(notmuch_threads_t * threads)642 notmuch_threads_valid (notmuch_threads_t *threads)
643 {
644     unsigned int doc_id;
645 
646     if (! threads)
647 	return false;
648 
649     while (threads->doc_id_pos < threads->doc_ids->len) {
650 	doc_id = g_array_index (threads->doc_ids, unsigned int,
651 				threads->doc_id_pos);
652 	if (_notmuch_doc_id_set_contains (&threads->match_set, doc_id))
653 	    break;
654 
655 	threads->doc_id_pos++;
656     }
657 
658     return threads->doc_id_pos < threads->doc_ids->len;
659 }
660 
661 notmuch_thread_t *
notmuch_threads_get(notmuch_threads_t * threads)662 notmuch_threads_get (notmuch_threads_t *threads)
663 {
664     unsigned int doc_id;
665 
666     if (! notmuch_threads_valid (threads))
667 	return NULL;
668 
669     doc_id = g_array_index (threads->doc_ids, unsigned int,
670 			    threads->doc_id_pos);
671     return _notmuch_thread_create (threads->query,
672 				   threads->query->notmuch,
673 				   doc_id,
674 				   &threads->match_set,
675 				   threads->query->exclude_terms,
676 				   threads->query->omit_excluded,
677 				   threads->query->sort);
678 }
679 
680 void
notmuch_threads_move_to_next(notmuch_threads_t * threads)681 notmuch_threads_move_to_next (notmuch_threads_t *threads)
682 {
683     threads->doc_id_pos++;
684 }
685 
686 void
notmuch_threads_destroy(notmuch_threads_t * threads)687 notmuch_threads_destroy (notmuch_threads_t *threads)
688 {
689     talloc_free (threads);
690 }
691 
692 notmuch_status_t
notmuch_query_count_messages_st(notmuch_query_t * query,unsigned * count_out)693 notmuch_query_count_messages_st (notmuch_query_t *query, unsigned *count_out)
694 {
695     return notmuch_query_count_messages (query, count_out);
696 }
697 
698 notmuch_status_t
notmuch_query_count_messages(notmuch_query_t * query,unsigned * count_out)699 notmuch_query_count_messages (notmuch_query_t *query, unsigned *count_out)
700 {
701     return _notmuch_query_count_documents (query, "mail", count_out);
702 }
703 
704 notmuch_status_t
_notmuch_query_count_documents(notmuch_query_t * query,const char * type,unsigned * count_out)705 _notmuch_query_count_documents (notmuch_query_t *query, const char *type, unsigned *count_out)
706 {
707     notmuch_database_t *notmuch = query->notmuch;
708     Xapian::doccount count = 0;
709     notmuch_status_t status;
710 
711     status = _notmuch_query_ensure_parsed (query);
712     if (status)
713 	return status;
714 
715     try {
716 	Xapian::Enquire enquire (*notmuch->xapian_db);
717 	Xapian::Query mail_query (talloc_asprintf (query, "%s%s",
718 						   _find_prefix ("type"),
719 						   type));
720 	Xapian::Query final_query, exclude_query;
721 	Xapian::MSet mset;
722 
723 	final_query = Xapian::Query (Xapian::Query::OP_AND,
724 				     mail_query, query->xapian_query);
725 
726 	exclude_query = _notmuch_exclude_tags (query);
727 
728 	final_query = Xapian::Query (Xapian::Query::OP_AND_NOT,
729 				     final_query, exclude_query);
730 
731 	enquire.set_weighting_scheme (Xapian::BoolWeight ());
732 	enquire.set_docid_order (Xapian::Enquire::ASCENDING);
733 
734 	if (_debug_query ()) {
735 	    fprintf (stderr, "Exclude query is:\n%s\n",
736 		     exclude_query.get_description ().c_str ());
737 	    fprintf (stderr, "Final query is:\n%s\n",
738 		     final_query.get_description ().c_str ());
739 	}
740 
741 	enquire.set_query (final_query);
742 
743 	/*
744 	 * Set the checkatleast parameter to the number of documents
745 	 * in the database to make get_matches_estimated() exact.
746 	 * Set the max parameter to 1 to avoid fetching documents we will discard.
747 	 */
748 	mset = enquire.get_mset (0, 1,
749 				 notmuch->xapian_db->get_doccount ());
750 
751 	count = mset.get_matches_estimated ();
752 
753     } catch (const Xapian::Error &error) {
754 	_notmuch_database_log (notmuch,
755 			       "A Xapian exception occurred performing query: %s\n",
756 			       error.get_msg ().c_str ());
757 	_notmuch_database_log_append (notmuch,
758 				      "Query string was: %s\n",
759 				      query->query_string);
760 	return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
761     }
762 
763     *count_out = count;
764     return NOTMUCH_STATUS_SUCCESS;
765 }
766 
767 notmuch_status_t
notmuch_query_count_threads_st(notmuch_query_t * query,unsigned * count)768 notmuch_query_count_threads_st (notmuch_query_t *query, unsigned *count)
769 {
770     return notmuch_query_count_threads (query, count);
771 }
772 
773 notmuch_status_t
notmuch_query_count_threads(notmuch_query_t * query,unsigned * count)774 notmuch_query_count_threads (notmuch_query_t *query, unsigned *count)
775 {
776     notmuch_messages_t *messages;
777     GHashTable *hash;
778     notmuch_sort_t sort;
779     notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
780 
781     sort = query->sort;
782     query->sort = NOTMUCH_SORT_UNSORTED;
783     ret = notmuch_query_search_messages (query, &messages);
784     if (ret)
785 	return ret;
786     query->sort = sort;
787     if (messages == NULL)
788 	return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
789 
790     hash = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL);
791     if (hash == NULL) {
792 	talloc_free (messages);
793 	return NOTMUCH_STATUS_OUT_OF_MEMORY;
794     }
795 
796     while (notmuch_messages_valid (messages)) {
797 	notmuch_message_t *message = notmuch_messages_get (messages);
798 	const char *thread_id = notmuch_message_get_thread_id (message);
799 	char *thread_id_copy = talloc_strdup (messages, thread_id);
800 	if (unlikely (thread_id_copy == NULL)) {
801 	    notmuch_message_destroy (message);
802 	    ret = NOTMUCH_STATUS_OUT_OF_MEMORY;
803 	    goto DONE;
804 	}
805 	g_hash_table_insert (hash, thread_id_copy, NULL);
806 	notmuch_message_destroy (message);
807 	notmuch_messages_move_to_next (messages);
808     }
809 
810     *count = g_hash_table_size (hash);
811 
812   DONE:
813     g_hash_table_unref (hash);
814     talloc_free (messages);
815 
816     return ret;
817 }
818 
819 notmuch_database_t *
notmuch_query_get_database(const notmuch_query_t * query)820 notmuch_query_get_database (const notmuch_query_t *query)
821 {
822     return query->notmuch;
823 }
824 
825 notmuch_status_t
_notmuch_query_expand(notmuch_database_t * notmuch,const char * field,Xapian::Query subquery,Xapian::Query & output,std::string & msg)826 _notmuch_query_expand (notmuch_database_t *notmuch, const char *field, Xapian::Query subquery,
827 		       Xapian::Query &output, std::string &msg)
828 {
829     std::set<std::string> terms;
830     const std::string term_prefix =  _find_prefix (field);
831 
832     if (_debug_query ()) {
833 	fprintf (stderr, "Expanding subquery:\n%s\n",
834 		 subquery.get_description ().c_str ());
835     }
836 
837     try {
838 	Xapian::Enquire enquire (*notmuch->xapian_db);
839 	Xapian::MSet mset;
840 
841 	enquire.set_weighting_scheme (Xapian::BoolWeight ());
842 	enquire.set_query (subquery);
843 
844 	mset = enquire.get_mset (0, notmuch->xapian_db->get_doccount ());
845 
846 	for (Xapian::MSetIterator iterator = mset.begin (); iterator != mset.end (); iterator++) {
847 	    Xapian::docid doc_id = *iterator;
848 	    Xapian::Document doc = notmuch->xapian_db->get_document (doc_id);
849 	    Xapian::TermIterator i = doc.termlist_begin ();
850 
851 	    for (i.skip_to (term_prefix);
852 		 i != doc.termlist_end () && ((*i).rfind (term_prefix, 0) == 0); i++) {
853 		terms.insert (*i);
854 	    }
855 	}
856 	output = Xapian::Query (Xapian::Query::OP_OR, terms.begin (), terms.end ());
857 	if (_debug_query ()) {
858 	    fprintf (stderr, "Expanded query:\n%s\n",
859 		     subquery.get_description ().c_str ());
860 	}
861 
862     } catch (const Xapian::Error &error) {
863 	_notmuch_database_log (notmuch,
864 			       "A Xapian exception occurred expanding query: %s\n",
865 			       error.get_msg ().c_str ());
866 	msg = error.get_msg ();
867 	return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
868     }
869 
870     return NOTMUCH_STATUS_SUCCESS;
871 }
872