1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
/** Copyright (C) 2022-2025 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
**
** This program is free software; you can redistribute it and/or modify it
** under the terms of the GNU General Public License as published by the
** Free Software Foundation; either version 3, or (at your option) any
** later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software Foundation,
** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**
*/
#ifndef MU_DOCUMENT_HH__
#define MU_DOCUMENT_HH__
#include <utility>
#include <string>
#include <vector>
#include "mu-xapian-db.hh"
#include "mu-fields.hh"
#include "mu-priority.hh"
#include "mu-flags.hh"
#include "mu-contact.hh"
#include "mu-labels.hh"
#include <utils/mu-option.hh>
#include <utils/mu-sexp.hh>
namespace Mu {
/**
* A Document describes the information about a message that is
* or can be stored in the database.
*
*/
class Document {
public:
enum struct Options {
None = 0,
SupportNgrams = 1 << 0, /**< Support ngrams, as used in
* CJK and other languages. */
};
/**
* Construct a message for a new Xapian Document
*
* @param flags behavioral flags
*/
Document(Options opts = Options::None): options_{opts} {}
/**
* Construct a message document based on an existing Xapian document.
*
* @param doc
* @param flags behavioral flags
*/
Document(const Xapian::Document& doc, Options opts = Options::None):
xdoc_{doc}, options_{opts} {}
/**
* DTOR
*/
~Document() {
xapian_document(); // for side-effect up updating sexp.
}
/**
* Get a reference to the underlying Xapian document.
*
*/
const Xapian::Document& xapian_document() const;
/**
* Get the doc-id for this document
*
* @return the docid
*/
Xapian::docid docid() const { return xdoc_.get_docid(); }
/*
* updating a document with terms & values
*/
/**
* Add a string value to the document
*
* @param field_id field id
* @param val string value
*/
void add(Field::Id field_id, const std::string& val);
/**
* Add a string-vec value to the document, if non-empty
*
* @param field_id field id
* @param val string-vec value
*/
void add(Field::Id field_id, const std::vector<std::string>& vals);
/**
* Add message-contacts to the document, if non-empty
*
* @param field_id field id
* @param contacts message contacts
*/
void add(Field::Id id, const Contacts& contacts);
/**
* Add some extra contacts with the given propname; this is useful for
* ":reply-to" and ":list-post" which don't have a Field::Id and are
* only present in the sexp, not in the terms/values
*
* @param propname property name (e.g.,. ":reply-to")
* @param contacts contacts for this property.
*/
void add_extra_contacts(const std::string& propname,
const Contacts& contacts);
/**
* Add an integer value to the document
*
* @param field_id field id
* @param val integer value
*/
void add(Field::Id field_id, int64_t val);
/**
* Add a message priority to the document
*
* @param prio priority
*/
void add(Priority prio);
/**
* Add message flags to the document
*
* @param flags mesage flags.
*/
void add(Flags flags);
/**
* Remove values and terms for some field.
*
* @param field_id
*/
void remove(Field::Id field_id);
/**
* Get the cached s-expression
*
* @return the cached s-expression
*/
const Sexp& sexp() const { return cached_sexp(); }
/**
* Get the message s-expression as a string
*
* @return message s-expression string
*/
std::string sexp_str() const { return xdoc_.get_data(); }
/**
* Generically adds an optional value, if set, to the document
*
* @param id the field 0d
* @param an optional value
*/
template<typename T> void add(Field::Id id, const Option<T>& val) {
if (val)
add(id, val.value());
}
/*
* Retrieving values
*/
/**
* Get a message-field as a string-value
*
* @param field_id id of the field to get.
*
* @return a string (empty if not found)
*/
std::string string_value(Field::Id field_id) const noexcept {
return xapian_try([&]{
return xdoc_.get_value(field_from_id(field_id).value_no());
}, std::string{});
}
/**
* Get a vec of string values.
*
* @param field_id id of the field to get
*
* @return a string list
*/
std::vector<std::string> string_vec_value(Field::Id field_id) const noexcept;
/**
* Get an integer value
*
* @param field_id id of the field to get
*
* @return an integer or 0 if not found.
*/
int64_t integer_value(Field::Id field_id) const noexcept;
/**
* Get contacts
*
* @param field_id id of the contacts field to get
*
* @return a contacts list
*/
Contacts contacts_value(Field::Id id) const noexcept;
/**
* Get the priority
*
* @return the message priority
*/
Priority priority_value() const noexcept;
/**
* Get the message flags
*
*
* @return flags
*/
Flags flags_value() const noexcept;
private:
template<typename SexpType> void sexp_put_prop(const Field& field, SexpType&& val);
void sexp_remove_prop(const Field& field);
Sexp& cached_sexp() const {
if (cached_sexp_.empty())
if (auto&& s{Sexp::parse(xdoc_.get_data())}; s)
cached_sexp_ = std::move(*s);
return cached_sexp_;
}
mutable Xapian::Document xdoc_;
Options options_;
mutable Sexp cached_sexp_;
mutable bool dirty_sexp_{}; /* xdoc's sexp is outdated */
};
MU_ENABLE_BITOPS(Document::Options);
} // namepace Mu
#endif /* MU_DOCUMENT_HH__ */
|