]> git.notmuchmail.org Git - notmuch/blob - lib/database-private.h
lib: Database version 3: Introduce fine-grained "features"
[notmuch] / lib / database-private.h
1 /* database-private.h - For peeking into the internals of notmuch_database_t
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 http://www.gnu.org/licenses/ .
17  *
18  * Author: Carl Worth <cworth@cworth.org>
19  */
20
21 #ifndef NOTMUCH_DATABASE_PRIVATE_H
22 #define NOTMUCH_DATABASE_PRIVATE_H
23
24 /* According to WG14/N1124, a C++ implementation won't provide us a
25  * macro like PRIx64 (which gives a printf format string for
26  * formatting a uint64_t as hexadecimal) unless we define
27  * __STDC_FORMAT_MACROS before including inttypes.h. That's annoying,
28  * but there it is.
29  */
30 #define __STDC_FORMAT_MACROS
31 #include <inttypes.h>
32
33 #include "notmuch-private.h"
34
35 #include <xapian.h>
36
37 #pragma GCC visibility push(hidden)
38
39 /* Bit masks for _notmuch_database::features.  Features are named,
40  * independent aspects of the database schema.
41  *
42  * A database stores the set of features that it "uses" (implicitly
43  * before database version 3 and explicitly as of version 3).
44  *
45  * A given library version will "recognize" a particular set of
46  * features; if a database uses a feature that the library does not
47  * recognize, the library will refuse to open it.  It is assumed the
48  * set of recognized features grows monotonically over time.  A
49  * library version will "implement" some subset of the recognized
50  * features: some operations may require that the database use (or not
51  * use) some feature, while other operations may support both
52  * databases that use and that don't use some feature.
53  *
54  * On disk, the database stores string names for these features (see
55  * the feature_names array).  These enum bit values are never
56  * persisted to disk and may change freely.
57  */
58 enum _notmuch_features {
59     /* If set, file names are stored in "file-direntry" terms.  If
60      * unset, file names are stored in document data.
61      *
62      * Introduced: version 1. */
63     NOTMUCH_FEATURE_FILE_TERMS = 1 << 0,
64
65     /* If set, directory timestamps are stored in documents with
66      * XDIRECTORY terms and relative paths.  If unset, directory
67      * timestamps are stored in documents with XTIMESTAMP terms and
68      * absolute paths.
69      *
70      * Introduced: version 1. */
71     NOTMUCH_FEATURE_DIRECTORY_DOCS = 1 << 1,
72
73     /* If set, the from, subject, and message-id headers are stored in
74      * message document values.  If unset, message documents *may*
75      * have these values, but if the value is empty, it must be
76      * retrieved from the message file.
77      *
78      * Introduced: optional in version 1, required as of version 3.
79      */
80     NOTMUCH_FEATURE_FROM_SUBJECT_ID_VALUES = 1 << 2,
81
82     /* If set, folder terms are boolean and path terms exist.  If
83      * unset, folder terms are probabilistic and stemmed and path
84      * terms do not exist.
85      *
86      * Introduced: version 2. */
87     NOTMUCH_FEATURE_BOOL_FOLDER = 1 << 3,
88 };
89
90 /* In C++, a named enum is its own type, so define bitwise operators
91  * on _notmuch_features. */
92 inline _notmuch_features
93 operator|(_notmuch_features a, _notmuch_features b)
94 {
95     return static_cast<_notmuch_features>(
96         static_cast<unsigned>(a) | static_cast<unsigned>(b));
97 }
98
99 inline _notmuch_features
100 operator&(_notmuch_features a, _notmuch_features b)
101 {
102     return static_cast<_notmuch_features>(
103         static_cast<unsigned>(a) & static_cast<unsigned>(b));
104 }
105
106 inline _notmuch_features
107 operator~(_notmuch_features a)
108 {
109     return static_cast<_notmuch_features>(~static_cast<unsigned>(a));
110 }
111
112 inline _notmuch_features&
113 operator|=(_notmuch_features &a, _notmuch_features b)
114 {
115     a = a | b;
116     return a;
117 }
118
119 inline _notmuch_features&
120 operator&=(_notmuch_features &a, _notmuch_features b)
121 {
122     a = a & b;
123     return a;
124 }
125
126 struct _notmuch_database {
127     notmuch_bool_t exception_reported;
128
129     char *path;
130
131     notmuch_database_mode_t mode;
132     int atomic_nesting;
133     Xapian::Database *xapian_db;
134
135     /* Bit mask of features used by this database.  This is a
136      * bitwise-OR of NOTMUCH_FEATURE_* values (above). */
137     enum _notmuch_features features;
138
139     unsigned int last_doc_id;
140     uint64_t last_thread_id;
141
142     Xapian::QueryParser *query_parser;
143     Xapian::TermGenerator *term_gen;
144     Xapian::ValueRangeProcessor *value_range_processor;
145     Xapian::ValueRangeProcessor *date_range_processor;
146 };
147
148 /* Prior to database version 3, features were implied by the database
149  * version number, so hard-code them for earlier versions. */
150 #define NOTMUCH_FEATURES_V0 ((enum _notmuch_features)0)
151 #define NOTMUCH_FEATURES_V1 (NOTMUCH_FEATURES_V0 | NOTMUCH_FEATURE_FILE_TERMS | \
152                              NOTMUCH_FEATURE_DIRECTORY_DOCS)
153 #define NOTMUCH_FEATURES_V2 (NOTMUCH_FEATURES_V1 | NOTMUCH_FEATURE_BOOL_FOLDER)
154
155 /* Current database features.  If any of these are missing from a
156  * database, request an upgrade.
157  * NOTMUCH_FEATURE_FROM_SUBJECT_ID_VALUES is not included because
158  * upgrade doesn't currently introduce the feature (though brand new
159  * databases will have it). */
160 #define NOTMUCH_FEATURES_CURRENT \
161     (NOTMUCH_FEATURE_FILE_TERMS | NOTMUCH_FEATURE_DIRECTORY_DOCS | \
162      NOTMUCH_FEATURE_BOOL_FOLDER)
163
164 /* Return the list of terms from the given iterator matching a prefix.
165  * The prefix will be stripped from the strings in the returned list.
166  * The list will be allocated using ctx as the talloc context.
167  *
168  * The function returns NULL on failure.
169  */
170 notmuch_string_list_t *
171 _notmuch_database_get_terms_with_prefix (void *ctx, Xapian::TermIterator &i,
172                                          Xapian::TermIterator &end,
173                                          const char *prefix);
174
175 #pragma GCC visibility pop
176
177 #endif