]> git.notmuchmail.org Git - notmuch/blob - lib/notmuch-private.h
lib: optimize counting documents
[notmuch] / lib / notmuch-private.h
1 /* notmuch-private.h - Internal interfaces for notmuch.
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 #ifndef NOTMUCH_PRIVATE_H
22 #define NOTMUCH_PRIVATE_H
23
24 #ifndef _GNU_SOURCE
25 #define _GNU_SOURCE /* For getline and asprintf */
26 #endif
27 #include <stdio.h>
28
29 #include "compat.h"
30
31 #include "notmuch.h"
32
33 NOTMUCH_BEGIN_DECLS
34
35 #include <stdlib.h>
36 #include <stdarg.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <sys/mman.h>
40 #include <string.h>
41 #include <errno.h>
42 #include <fcntl.h>
43 #include <unistd.h>
44 #include <ctype.h>
45 #include <assert.h>
46
47 #include <talloc.h>
48
49 #include <gmime/gmime.h>
50
51 #include "xutil.h"
52 #include "error_util.h"
53 #include "string-util.h"
54
55 #pragma GCC visibility push(hidden)
56
57 #ifdef DEBUG
58 # define DEBUG_DATABASE_SANITY 1
59 # define DEBUG_QUERY 1
60 #endif
61
62 #define COMPILE_TIME_ASSERT(pred) ((void)sizeof(char[1 - 2*!(pred)]))
63
64 #define STRNCMP_LITERAL(var, literal) \
65     strncmp ((var), (literal), sizeof (literal) - 1)
66
67 /* Robust bit test/set/reset macros */
68 #define _NOTMUCH_VALID_BIT(bit) \
69     ((bit) >= 0 && ((unsigned long) bit) < CHAR_BIT * sizeof (unsigned long long))
70 #define NOTMUCH_TEST_BIT(val, bit) \
71     (_NOTMUCH_VALID_BIT(bit) ? !!((val) & (1ull << (bit))) : 0)
72 #define NOTMUCH_SET_BIT(valp, bit) \
73     (_NOTMUCH_VALID_BIT(bit) ? (*(valp) |= (1ull << (bit))) : *(valp))
74 #define NOTMUCH_CLEAR_BIT(valp,  bit) \
75     (_NOTMUCH_VALID_BIT(bit) ? (*(valp) &= ~(1ull << (bit))) : *(valp))
76
77 #define unused(x) x __attribute__ ((unused))
78
79 #ifdef __cplusplus
80 # define visible __attribute__((visibility("default")))
81 #else
82 # define visible
83 #endif
84
85 /* Thanks to Andrew Tridgell's (SAMBA's) talloc for this definition of
86  * unlikely. The talloc source code comes to us via the GNU LGPL v. 3.
87  */
88 /* these macros gain us a few percent of speed on gcc */
89 #if (__GNUC__ >= 3)
90 /* the strange !! is to ensure that __builtin_expect() takes either 0 or 1
91    as its first argument */
92 #ifndef likely
93 #define likely(x)   __builtin_expect(!!(x), 1)
94 #endif
95 #ifndef unlikely
96 #define unlikely(x) __builtin_expect(!!(x), 0)
97 #endif
98 #else
99 #ifndef likely
100 #define likely(x) (x)
101 #endif
102 #ifndef unlikely
103 #define unlikely(x) (x)
104 #endif
105 #endif
106
107 typedef enum {
108     NOTMUCH_VALUE_TIMESTAMP = 0,
109     NOTMUCH_VALUE_MESSAGE_ID,
110     NOTMUCH_VALUE_FROM,
111     NOTMUCH_VALUE_SUBJECT,
112     NOTMUCH_VALUE_LAST_MOD,
113 } notmuch_value_t;
114
115 /* Xapian (with flint backend) complains if we provide a term longer
116  * than this, but I haven't yet found a way to query the limit
117  * programmatically. */
118 #define NOTMUCH_TERM_MAX 245
119
120 #define NOTMUCH_METADATA_THREAD_ID_PREFIX "thread_id_"
121
122 /* For message IDs we have to be even more restrictive. Beyond fitting
123  * into the term limit, we also use message IDs to construct
124  * metadata-key values. And the documentation says that these should
125  * be restricted to about 200 characters. (The actual limit for the
126  * chert backend at least is 252.)
127  */
128 #define NOTMUCH_MESSAGE_ID_MAX (200 - sizeof (NOTMUCH_METADATA_THREAD_ID_PREFIX))
129
130 typedef enum _notmuch_private_status {
131     /* First, copy all the public status values. */
132     NOTMUCH_PRIVATE_STATUS_SUCCESS = NOTMUCH_STATUS_SUCCESS,
133     NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY = NOTMUCH_STATUS_OUT_OF_MEMORY,
134     NOTMUCH_PRIVATE_STATUS_READ_ONLY_DATABASE = NOTMUCH_STATUS_READ_ONLY_DATABASE,
135     NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION = NOTMUCH_STATUS_XAPIAN_EXCEPTION,
136     NOTMUCH_PRIVATE_STATUS_FILE_NOT_EMAIL = NOTMUCH_STATUS_FILE_NOT_EMAIL,
137     NOTMUCH_PRIVATE_STATUS_NULL_POINTER = NOTMUCH_STATUS_NULL_POINTER,
138     NOTMUCH_PRIVATE_STATUS_TAG_TOO_LONG = NOTMUCH_STATUS_TAG_TOO_LONG,
139     NOTMUCH_PRIVATE_STATUS_UNBALANCED_FREEZE_THAW = NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW,
140
141     /* Then add our own private values. */
142     NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG = NOTMUCH_STATUS_LAST_STATUS,
143     NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND,
144
145     NOTMUCH_PRIVATE_STATUS_LAST_STATUS
146 } notmuch_private_status_t;
147
148 /* Coerce a notmuch_private_status_t value to a notmuch_status_t
149  * value, generating an internal error if the private value is equal
150  * to or greater than NOTMUCH_STATUS_LAST_STATUS. (The idea here is
151  * that the caller has previously handled any expected
152  * notmuch_private_status_t values.)
153  *
154  * Note that the function _internal_error does not return. Evaluating
155  * to NOTMUCH_STATUS_SUCCESS is done purely to appease the compiler.
156  */
157 #define COERCE_STATUS(private_status, format, ...)                      \
158     ((private_status >= (notmuch_private_status_t) NOTMUCH_STATUS_LAST_STATUS)\
159      ?                                                                  \
160      _internal_error (format " (%s).\n",                                \
161                       ##__VA_ARGS__,                                    \
162                       __location__),                                    \
163      (notmuch_status_t) NOTMUCH_PRIVATE_STATUS_SUCCESS                  \
164      :                                                                  \
165      (notmuch_status_t) private_status)
166
167 /* Flags shared by various lookup functions. */
168 typedef enum _notmuch_find_flags {
169     /* Lookup without creating any documents.  This is the default
170      * behavior. */
171     NOTMUCH_FIND_LOOKUP = 0,
172     /* If set, create the necessary document (or documents) if they
173      * are missing.  Requires a read/write database. */
174     NOTMUCH_FIND_CREATE = 1<<0,
175 } notmuch_find_flags_t;
176
177 typedef struct _notmuch_doc_id_set notmuch_doc_id_set_t;
178
179 /* database.cc */
180
181 /* Lookup a prefix value by name.
182  *
183  * XXX: This should really be static inside of message.cc, and we can
184  * do that once we convert database.cc to use the
185  * _notmuch_message_add/remove_term functions. */
186 const char *
187 _find_prefix (const char *name);
188
189 char *
190 _notmuch_message_id_compressed (void *ctx, const char *message_id);
191
192 notmuch_status_t
193 _notmuch_database_ensure_writable (notmuch_database_t *notmuch);
194
195 void
196 _notmuch_database_log (notmuch_database_t *notmuch,
197                        const char *format, ...);
198
199 void
200 _notmuch_database_log_append (notmuch_database_t *notmuch,
201                               const char *format, ...);
202
203 unsigned long
204 _notmuch_database_new_revision (notmuch_database_t *notmuch);
205
206 const char *
207 _notmuch_database_relative_path (notmuch_database_t *notmuch,
208                                  const char *path);
209
210 notmuch_status_t
211 _notmuch_database_split_path (void *ctx,
212                               const char *path,
213                               const char **directory,
214                               const char **basename);
215
216 const char *
217 _notmuch_database_get_directory_db_path (const char *path);
218
219 unsigned int
220 _notmuch_database_generate_doc_id (notmuch_database_t *notmuch);
221
222 notmuch_private_status_t
223 _notmuch_database_find_unique_doc_id (notmuch_database_t *notmuch,
224                                       const char *prefix_name,
225                                       const char *value,
226                                       unsigned int *doc_id);
227
228 notmuch_status_t
229 _notmuch_database_find_directory_id (notmuch_database_t *database,
230                                      const char *path,
231                                      notmuch_find_flags_t flags,
232                                      unsigned int *directory_id);
233
234 const char *
235 _notmuch_database_get_directory_path (void *ctx,
236                                       notmuch_database_t *notmuch,
237                                       unsigned int doc_id);
238
239 notmuch_status_t
240 _notmuch_database_filename_to_direntry (void *ctx,
241                                         notmuch_database_t *notmuch,
242                                         const char *filename,
243                                         notmuch_find_flags_t flags,
244                                         char **direntry);
245
246 /* directory.cc */
247
248 notmuch_directory_t *
249 _notmuch_directory_create (notmuch_database_t *notmuch,
250                            const char *path,
251                            notmuch_find_flags_t flags,
252                            notmuch_status_t *status_ret);
253
254 unsigned int
255 _notmuch_directory_get_document_id (notmuch_directory_t *directory);
256
257 /* message.cc */
258
259 notmuch_message_t *
260 _notmuch_message_create (const void *talloc_owner,
261                          notmuch_database_t *notmuch,
262                          unsigned int doc_id,
263                          notmuch_private_status_t *status);
264
265 notmuch_message_t *
266 _notmuch_message_create_for_message_id (notmuch_database_t *notmuch,
267                                         const char *message_id,
268                                         notmuch_private_status_t *status);
269
270 unsigned int
271 _notmuch_message_get_doc_id (notmuch_message_t *message);
272
273 const char *
274 _notmuch_message_get_in_reply_to (notmuch_message_t *message);
275
276 notmuch_private_status_t
277 _notmuch_message_add_term (notmuch_message_t *message,
278                            const char *prefix_name,
279                            const char *value);
280
281 notmuch_private_status_t
282 _notmuch_message_remove_term (notmuch_message_t *message,
283                               const char *prefix_name,
284                               const char *value);
285
286 notmuch_private_status_t
287 _notmuch_message_has_term (notmuch_message_t *message,
288                            const char *prefix_name,
289                            const char *value,
290                            notmuch_bool_t *result);
291
292 notmuch_private_status_t
293 _notmuch_message_gen_terms (notmuch_message_t *message,
294                             const char *prefix_name,
295                             const char *text);
296
297 void
298 _notmuch_message_upgrade_filename_storage (notmuch_message_t *message);
299
300 void
301 _notmuch_message_upgrade_folder (notmuch_message_t *message);
302
303 notmuch_status_t
304 _notmuch_message_add_filename (notmuch_message_t *message,
305                                const char *filename);
306
307 notmuch_status_t
308 _notmuch_message_remove_filename (notmuch_message_t *message,
309                                   const char *filename);
310
311 notmuch_status_t
312 _notmuch_message_rename (notmuch_message_t *message,
313                          const char *new_filename);
314
315 void
316 _notmuch_message_ensure_thread_id (notmuch_message_t *message);
317
318 void
319 _notmuch_message_set_header_values (notmuch_message_t *message,
320                                     const char *date,
321                                     const char *from,
322                                     const char *subject);
323
324 void
325 _notmuch_message_upgrade_last_mod (notmuch_message_t *message);
326
327 void
328 _notmuch_message_sync (notmuch_message_t *message);
329
330 notmuch_status_t
331 _notmuch_message_delete (notmuch_message_t *message);
332
333 notmuch_private_status_t
334 _notmuch_message_initialize_ghost (notmuch_message_t *message,
335                                    const char *thread_id);
336
337 void
338 _notmuch_message_close (notmuch_message_t *message);
339
340 /* Get a copy of the data in this message document.
341  *
342  * Caller should talloc_free the result when done.
343  *
344  * This function is intended to support database upgrade and really
345  * shouldn't be used otherwise. */
346 char *
347 _notmuch_message_talloc_copy_data (notmuch_message_t *message);
348
349 /* Clear the data in this message document.
350  *
351  * This function is intended to support database upgrade and really
352  * shouldn't be used otherwise. */
353 void
354 _notmuch_message_clear_data (notmuch_message_t *message);
355
356 /* Set the author member of 'message' - this is the representation used
357  * when displaying the message */
358 void
359 _notmuch_message_set_author (notmuch_message_t *message, const char *author);
360
361 /* Get the author member of 'message' */
362 const char *
363 _notmuch_message_get_author (notmuch_message_t *message);
364
365 /* message-file.c */
366
367 /* XXX: I haven't decided yet whether these will actually get exported
368  * into the public interface in notmuch.h
369  */
370
371 typedef struct _notmuch_message_file notmuch_message_file_t;
372
373 /* Open a file containing a single email message.
374  *
375  * The caller should call notmuch_message_close when done with this.
376  *
377  * Returns NULL if any error occurs.
378  */
379 notmuch_message_file_t *
380 _notmuch_message_file_open (notmuch_database_t *notmuch, const char *filename);
381
382 /* Like notmuch_message_file_open but with 'ctx' as the talloc owner. */
383 notmuch_message_file_t *
384 _notmuch_message_file_open_ctx (notmuch_database_t *notmuch,
385                                 void *ctx, const char *filename);
386
387 /* Close a notmuch message previously opened with notmuch_message_open. */
388 void
389 _notmuch_message_file_close (notmuch_message_file_t *message);
390
391 /* Parse the message.
392  *
393  * This will be done automatically as necessary on other calls
394  * depending on it, but an explicit call allows for better error
395  * status reporting.
396  */
397 notmuch_status_t
398 _notmuch_message_file_parse (notmuch_message_file_t *message);
399
400 /* Get the gmime message of a message file.
401  *
402  * The message file is parsed as necessary.
403  *
404  * The GMimeMessage* is set to *mime_message on success (which the
405  * caller must not unref).
406  *
407  * XXX: Would be nice to not have to expose GMimeMessage here.
408  */
409 notmuch_status_t
410 _notmuch_message_file_get_mime_message (notmuch_message_file_t *message,
411                                         GMimeMessage **mime_message);
412
413 /* Get the value of the specified header from the message as a UTF-8 string.
414  *
415  * The message file is parsed as necessary.
416  *
417  * The header name is case insensitive.
418  *
419  * The Received: header is special - for it all Received: headers in
420  * the message are concatenated
421  *
422  * The returned value is owned by the notmuch message and is valid
423  * only until the message is closed. The caller should copy it if
424  * needing to modify the value or to hold onto it for longer.
425  *
426  * Returns NULL on errors, empty string if the message does not
427  * contain a header line matching 'header'.
428  */
429 const char *
430 _notmuch_message_file_get_header (notmuch_message_file_t *message,
431                                  const char *header);
432
433 /* index.cc */
434
435 notmuch_status_t
436 _notmuch_message_index_file (notmuch_message_t *message,
437                              notmuch_message_file_t *message_file);
438
439 /* messages.c */
440
441 typedef struct _notmuch_message_node {
442     notmuch_message_t *message;
443     struct _notmuch_message_node *next;
444 } notmuch_message_node_t;
445
446 typedef struct _notmuch_message_list {
447     notmuch_message_node_t *head;
448     notmuch_message_node_t **tail;
449 } notmuch_message_list_t;
450
451 /* There's a rumor that there's an alternate struct _notmuch_messages
452  * somewhere with some nasty C++ objects in it. We'll try to maintain
453  * ignorance of that here. (See notmuch_mset_messages_t in query.cc)
454  */
455 struct visible _notmuch_messages {
456     notmuch_bool_t is_of_list_type;
457     notmuch_doc_id_set_t *excluded_doc_ids;
458     notmuch_message_node_t *iterator;
459 };
460
461 notmuch_message_list_t *
462 _notmuch_message_list_create (const void *ctx);
463
464 void
465 _notmuch_message_list_add_message (notmuch_message_list_t *list,
466                                    notmuch_message_t *message);
467
468 notmuch_messages_t *
469 _notmuch_messages_create (notmuch_message_list_t *list);
470
471 /* query.cc */
472
473 notmuch_bool_t
474 _notmuch_mset_messages_valid (notmuch_messages_t *messages);
475
476 notmuch_message_t *
477 _notmuch_mset_messages_get (notmuch_messages_t *messages);
478
479 void
480 _notmuch_mset_messages_move_to_next (notmuch_messages_t *messages);
481
482 notmuch_bool_t
483 _notmuch_doc_id_set_contains (notmuch_doc_id_set_t *doc_ids,
484                               unsigned int doc_id);
485
486 void
487 _notmuch_doc_id_set_remove (notmuch_doc_id_set_t *doc_ids,
488                             unsigned int doc_id);
489
490 /* querying xapian documents by type (e.g. "mail" or "ghost"): */
491 notmuch_status_t
492 _notmuch_query_search_documents (notmuch_query_t *query,
493                                  const char *type,
494                                  notmuch_messages_t **out);
495
496 notmuch_status_t
497 _notmuch_query_count_documents (notmuch_query_t *query,
498                                 const char *type,
499                                 unsigned *count_out);
500
501 /* message.cc */
502
503 void
504 _notmuch_message_add_reply (notmuch_message_t *message,
505                             notmuch_message_t *reply);
506 notmuch_database_t *
507 _notmuch_message_database (notmuch_message_t *message);
508
509 /* sha1.c */
510
511 char *
512 _notmuch_sha1_of_string (const char *str);
513
514 char *
515 _notmuch_sha1_of_file (const char *filename);
516
517 /* string-list.c */
518
519 typedef struct _notmuch_string_node {
520     char *string;
521     struct _notmuch_string_node *next;
522 } notmuch_string_node_t;
523
524 typedef struct visible _notmuch_string_list {
525     int length;
526     notmuch_string_node_t *head;
527     notmuch_string_node_t **tail;
528 } notmuch_string_list_t;
529
530 notmuch_string_list_t *
531 _notmuch_string_list_create (const void *ctx);
532
533 /* Add 'string' to 'list'.
534  *
535  * The list will create its own talloced copy of 'string'.
536  */
537 void
538 _notmuch_string_list_append (notmuch_string_list_t *list,
539                              const char *string);
540
541 void
542 _notmuch_string_list_sort (notmuch_string_list_t *list);
543
544 /* string-map.c */
545 typedef struct _notmuch_string_map  notmuch_string_map_t;
546 typedef struct _notmuch_string_map_iterator notmuch_string_map_iterator_t;
547 notmuch_string_map_t *
548 _notmuch_string_map_create (const void *ctx);
549
550 void
551 _notmuch_string_map_append (notmuch_string_map_t *map,
552                             const char *key,
553                             const char *value);
554
555 const char *
556 _notmuch_string_map_get (notmuch_string_map_t *map, const char *key);
557
558 notmuch_string_map_iterator_t *
559 _notmuch_string_map_iterator_create (notmuch_string_map_t *map, const char *key,
560                                      notmuch_bool_t exact);
561
562 notmuch_bool_t
563 _notmuch_string_map_iterator_valid (notmuch_string_map_iterator_t *iter);
564
565 void
566 _notmuch_string_map_iterator_move_to_next (notmuch_string_map_iterator_t *iter);
567
568 const char *
569 _notmuch_string_map_iterator_key (notmuch_string_map_iterator_t *iterator);
570
571 const char *
572 _notmuch_string_map_iterator_value (notmuch_string_map_iterator_t *iterator);
573
574 void
575 _notmuch_string_map_iterator_destroy (notmuch_string_map_iterator_t *iterator);
576
577 /* tags.c */
578
579 notmuch_tags_t *
580 _notmuch_tags_create (const void *ctx, notmuch_string_list_t *list);
581
582 /* filenames.c */
583
584 /* The notmuch_filenames_t iterates over a notmuch_string_list_t of
585  * file names */
586 notmuch_filenames_t *
587 _notmuch_filenames_create (const void *ctx,
588                            notmuch_string_list_t *list);
589
590 /* thread.cc */
591
592 notmuch_thread_t *
593 _notmuch_thread_create (void *ctx,
594                         notmuch_database_t *notmuch,
595                         unsigned int seed_doc_id,
596                         notmuch_doc_id_set_t *match_set,
597                         notmuch_string_list_t *excluded_terms,
598                         notmuch_exclude_t omit_exclude,
599                         notmuch_sort_t sort);
600
601 NOTMUCH_END_DECLS
602
603 #ifdef __cplusplus
604 /* Implicit typecast from 'void *' to 'T *' is okay in C, but not in
605  * C++. In talloc_steal, an explicit cast is provided for type safety
606  * in some GCC versions. Otherwise, a cast is required. Provide a
607  * template function for this to maintain type safety, and redefine
608  * talloc_steal to use it.
609  */
610 #if !(__GNUC__ >= 3)
611 template <class T> T *
612 _notmuch_talloc_steal (const void *new_ctx, const T *ptr)
613 {
614     return static_cast<T *> (talloc_steal (new_ctx, ptr));
615 }
616 #undef talloc_steal
617 #define talloc_steal _notmuch_talloc_steal
618 #endif
619 #endif
620
621 #pragma GCC visibility pop
622
623 #endif