summaryrefslogtreecommitdiff
path: root/lib/mu-query-results.hh
blob: 4ebb0c2ae7441c9389b2778dcb2649a963a6939f (plain)
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/*
** Copyright (C) 2022-2024 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_QUERY_RESULTS_HH__
#define MU_QUERY_RESULTS_HH__

#include <algorithm>
#include <limits>
#include <stdexcept>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <limits>
#include <ostream>
#include <cmath>
#include <memory>

#include <unistd.h>
#include <fcntl.h>
#include <glib.h>

#include <mu-xapian-db.hh>
#include <utils/mu-utils.hh>
#include <utils/mu-option.hh>

#include <message/mu-message.hh>

namespace Mu {

/**
 * This implements a QueryResults structure, which capture the results of a
 * Xapian query, and a QueryResultsIterator, which gives C++-compliant iterator
 * to go over the results. and finally QueryThreader (in query-threader.cc) which
 * calculates the threads, using the JWZ algorithm.
 */

/// Flags that influence now matches are presented (or skipped)
enum struct QueryFlags {
	None           = 0,      /**< no flags */
	Descending     = 1 << 0, /**< sort z->a */
	SkipUnreadable = 1 << 1, /**< skip unreadable msgs */
	SkipDuplicates = 1 << 2, /**< skip duplicate msgs */
	IncludeRelated = 1 << 3, /**< include related msgs */
	Threading      = 1 << 4, /**< calculate threading info */
	// internal
	Leader = 1 << 5, /**< This is the leader query (for internal use
			  * only)*/
};
MU_ENABLE_BITOPS(QueryFlags);

/// Stores all the essential information for sorting the results.
struct QueryMatch {
	/// Flags for a match (message) found
	enum struct Flags {
		None       = 0,      /**< No Flags */
		Leader     = 1 << 0, /**< Mark direct matches as leader */
		Related    = 1 << 1, /**< A related message */
		Unreadable = 1 << 2, /**< No readable file */
		Duplicate  = 1 << 3, /**< Message-id seen before */

		Root     = 1 << 10, /**< Is this the thread-root? */
		First    = 1 << 11, /**< Is this the first message in a thread? */
		Last     = 1 << 12, /**< Is this the last message in a thread? */
		Orphan   = 1 << 13, /**< Is this message without a parent? */
		HasChild = 1 << 14, /**< Does this message have a child? */

		ThreadSubject = 1 << 20, /**< Message holds subject for (sub)thread */
	};

	Flags       flags{Flags::None}; /**< Flags */
	std::string date_key;           /**< The date-key (for sorting all sub-root levels) */
	// the thread subject is the subject of the first message in a thread,
	// and any message that has a different subject compared to its predecessor
	// (ignoring prefixes such as Re:)
	//
	// otherwise, it is empty.
	std::string subject;        /**< subject for this message */
	size_t      thread_level{}; /**< The thread level */
	std::string thread_path;    /**< The hex-numerial path in the thread, ie. '00:01:0a' */
	std::string thread_date;    /**< date of newest message in thread */

	bool operator<(const QueryMatch& rhs) const { return date_key < rhs.date_key; }

	bool has_flag(Flags flag) const;
};

MU_ENABLE_BITOPS(QueryMatch::Flags);

inline bool
QueryMatch::has_flag(QueryMatch::Flags flag) const
{
	return any_of(flags & flag);
}

/* LCOV_EXCL_START */
static inline std::ostream&
operator<<(std::ostream& os, QueryMatch::Flags mflags)
{
	if (mflags == QueryMatch::Flags::None) {
		os << "<none>";
		return os;
	}

	if (any_of(mflags & QueryMatch::Flags::Leader))
		os << "leader ";
	if (any_of(mflags & QueryMatch::Flags::Unreadable))
		os << "unreadable ";
	if (any_of(mflags & QueryMatch::Flags::Duplicate))
		os << "dup ";

	if (any_of(mflags & QueryMatch::Flags::Root))
		os << "root ";
	if (any_of(mflags & QueryMatch::Flags::Related))
		os << "related ";
	if (any_of(mflags & QueryMatch::Flags::First))
		os << "first ";
	if (any_of(mflags & QueryMatch::Flags::Last))
		os << "last ";
	if (any_of(mflags & QueryMatch::Flags::Orphan))
		os << "orphan ";
	if (any_of(mflags & QueryMatch::Flags::HasChild))
		os << "has-child ";

	return os;
}

inline std::ostream&
operator<<(std::ostream& os, const QueryMatch& qmatch)
{
	os << "qm:[" << qmatch.thread_path << "]: " // " (" << qmatch.thread_level << "): "
	   << "> date:<" << qmatch.date_key << "> "
	   << "flags:{" << qmatch.flags << "}";

	return os;
}
/* LCOV_EXCL_STOP*/

using QueryMatches = std::unordered_map<Xapian::docid, QueryMatch>;


///
/// This is a view over the Xapian::MSet, which can optionally filter unreadable
/// / duplicate messages.
///
/// Note, we internally skip unreadable/duplicate messages (when asked too); those
/// skipped ones do _not_ count towards the max_size
///
class QueryResultsIterator {
public:
	using iterator_category = std::bidirectional_iterator_tag;
	using value_type        = Message;
	using difference_type   = void;
	using pointer           = void;
	using reference         = void;

	QueryResultsIterator(Xapian::MSetIterator mset_it, QueryMatches& query_matches)
		: mset_it_{mset_it}, query_matches_{query_matches} {
	}

	/**
	 * Increment the iterator
	 *
	 * @return an updated iterator
	 */
	QueryResultsIterator& operator++() {
		++mset_it_;
		mdoc_ = Nothing;
		return *this;
	}

	/**
	 * Increment the iterator (postfix)
	 *
	 * @return the iterator before updating
	 */
	QueryResultsIterator operator++(int) {
		auto old{mset_it_};
		++mset_it_;
		return QueryResultsIterator{old, query_matches_};
	}

	/**
	 * Decrement the iterator
	 *
	 * @return an updated iterator
	 */
	QueryResultsIterator& operator--() {
		--mset_it_;
		mdoc_ = Nothing;
		return *this;
	}

	/**
	 * Decrement the iterator (postfix)
	 *
	 * @return the iterator before updating
	 */
	QueryResultsIterator operator--(int) {
		auto old{mset_it_};
		--mset_it_;
		return QueryResultsIterator{old, query_matches_};
	}

	/**
	 * (Non)Equivalence operators
	 *
	 * @param rhs some other iterator
	 *
	 * @return true or false
	 */
	bool operator==(const QueryResultsIterator& rhs) const { return mset_it_ == rhs.mset_it_; }
	bool operator!=(const QueryResultsIterator& rhs) const { return mset_it_ != rhs.mset_it_; }

	QueryResultsIterator&       operator*() { return *this; }
	const QueryResultsIterator& operator*() const { return *this; }


	/**
	 * Get the Xapian::Document this iterator is pointing at,
	 * or an empty document when looking at end().
	 *
	 * @return a document
	 */
	Option<Xapian::Document> document() const {
		return xapian_try([this]()->Option<Xapian::Document> {
			auto doc{mset_it_.get_document()};
			if (doc.get_docid() == 0)
				return Nothing;
			else
				return Some(std::move(doc));
			}, Nothing);
	}


	/**
	 * get the corresponding Message for this iter, if any
	 *
	 * @return a Message or Nothing
	 */
	Option<Message> message() const {
		if (auto&& xdoc{document()}; !xdoc)
			return Nothing;
		else if (auto&& doc{Message::make_from_document(std::move(xdoc.value()))};
			 !doc)
			return Nothing;
		else
			return Some(std::move(doc.value()));
	}

	/**
	 * Get the doc-id for the document this iterator is pointing at, or 0
	 * when looking at end.
	 *
	 * @return a doc-id.
	 */
	Xapian::docid doc_id() const { return *mset_it_; }

	/**
	 * Get the message-id for the document (message) this iterator is
	 * pointing at, or not when not available
	 *
	 * @return a message-id
	 */
	Option<std::string> message_id() const noexcept {
		return opt_string(Field::Id::MessageId);
	}

	/**
	 * Get the thread-id for the document (message) this iterator is
	 * pointing at, or Nothing.
	 *
	 * @return a message-id
	 */
	Option<std::string> thread_id() const noexcept {
		return opt_string(Field::Id::ThreadId);
	}

	/**
	 * Get the file-system path for the document (message) this iterator is
	 * pointing at, or Nothing.
	 *
	 * @return a filesystem path
	 */
	Option<std::string> path() const noexcept {
		return opt_string(Field::Id::Path);
	}

	/**
	 * Get the a sortable date str for the document (message) the iterator
	 * is pointing at. pointing at, or Nothing. This (encoded) string
	 * has the same sort-order as the corresponding date.
	 *
	 * @return a filesystem path
	 */
	Option<std::string> date_str() const noexcept {
		return opt_string(Field::Id::Date);
	}

	/**
	 * Get the subject for the document (message) this iterator is pointing
	 * at.
	 *
	 * @return the subject
	 */
	Option<std::string> subject() const noexcept {
		return opt_string(Field::Id::Subject);
	}

	/**
	 * Get the references for the document (messages) this is iterator is
	 * pointing at, or empty if pointing at end of if no references are
	 * available.
	 *
	 * @return references
	 */
	std::vector<std::string> references() const noexcept {
		return mu_document().string_vec_value(Field::Id::References);
	}

	/**
	 * Get some value from the document, or Nothing if empty.
	 *
	 * @param id a message field id
	 *
	 * @return the value
	 */
	Option<std::string> opt_string(Field::Id id) const noexcept {
		if (auto&& val{mu_document().string_value(id)}; val.empty())
			return Nothing;
		else
			return Some(std::move(val));
	}

	/**
	 * Get the Query match info for this message.
	 *
	 * @return the match info.
	 */
	QueryMatch& query_match() {
		g_assert(query_matches_.find(doc_id()) != query_matches_.end());
		return query_matches_.find(doc_id())->second;
	}
	const QueryMatch& query_match() const {
		g_assert(query_matches_.find(doc_id()) != query_matches_.end());
		return query_matches_.find(doc_id())->second;
	}

private:
	/**
	 * Get a (cached) reference for the Mu::Document corresponding
	 * to the current iter.
	 *
	 * @return cached mu document,
	 */
	const Mu::Document& mu_document() const {
		if (!mdoc_) {
			if (auto xdoc = document(); !xdoc)
				std::runtime_error("iter without document");
			else
				mdoc_ = Mu::Document{xdoc.value()};
		}
		return mdoc_.value();
	}

	mutable Option<Mu::Document>    mdoc_; // cache.
	Xapian::MSetIterator		mset_it_;
	QueryMatches&			query_matches_;
};


static inline auto
format_as(const QueryResultsIterator& it)
{
	return it.path().value_or("<no path>");
}

constexpr auto MaxQueryResultsSize = std::numeric_limits<size_t>::max();

class QueryResults {
public:
	/// Helper types
	using iterator       = QueryResultsIterator;
	using const_iterator = const iterator;

	/**
	 * Construct a QueryResults object
	 *
	 * @param mset an Xapian::MSet with matches
	 */
	QueryResults(const Xapian::MSet& mset, QueryMatches&& query_matches)
	    : mset_{mset}, query_matches_{std::move(query_matches)} {}
	/**
	 * Is this QueryResults object empty (ie., no matches)?
	 *
	 * @return true are false
	 */
	bool empty() const { return mset_.empty(); }

	/**
	 * Get the number of matches in this QueryResult
	 *
	 * @return number of matches
	 */
	size_t size() const { return mset_.size(); }

	/**
	 * Get the begin iterator to the results.
	 *
	 * @return iterator
	 */
	const_iterator begin() const {
		return QueryResultsIterator(mset_.begin(), query_matches_);
	}

	/**
	 * Get the end iterator to the results.
	 *
	 * @return iterator
	 */
	const_iterator end() const {
		return QueryResultsIterator(mset_.end(), query_matches_);
	}

	/**
	 * Get the query-matches for these QueryResults. The non-const
	 * version can be use to _steal_ the query results, by moving
	 * them.
	 *
	 * @return query-matches
	 */
	const QueryMatches& query_matches() const { return query_matches_; }
	QueryMatches&       query_matches() { return query_matches_; }

private:
	const Xapian::MSet   mset_;
	mutable QueryMatches query_matches_;
};

} // namespace Mu

#endif /* MU_QUERY_RESULTS_HH__ */