1 #ifndef INDEX_THREAD_PRIVATE_H
2 #define INDEX_THREAD_PRIVATE_H
3 
4 #include "crc32.h"
5 #include "mail-thread.h"
6 #include "mail-index-strmap.h"
7 
8 #define MAIL_THREAD_INDEX_SUFFIX ".thread"
9 
10 /* After initially building the index, assign first_invalid_msgid_idx to
11    the next unused index + SKIP_COUNT. When more messages are added and
12    the next valid msgid conflicts with the first invalid msgid, the invalid
13    msgids will be moved forward again this many indexes. */
14 #define THREAD_INVALID_MSGID_STR_IDX_SKIP_COUNT \
15 	(4096 / sizeof(struct mail_thread_node))
16 
17 #define HDR_MESSAGE_ID "message-id"
18 #define HDR_IN_REPLY_TO "in-reply-to"
19 #define HDR_REFERENCES "references"
20 #define HDR_SUBJECT "subject"
21 
22 #define MAIL_THREAD_NODE_REF_MSGID 0
23 #define MAIL_THREAD_NODE_REF_INREPLYTO 1
24 #define MAIL_THREAD_NODE_REF_REFERENCES1 2
25 
26 struct mail_thread_node {
27 	/* UID of the message, or 0 for dummy nodes */
28 	uint32_t uid;
29 	/* Index for this node's parent node, 0 = this is root */
30 	uint32_t parent_idx;
31 	/* Number of messages containing "this message" -> "parent message"
32 	   link, i.e. "number of links to parent node". However since parents
33 	   can change, not all of these references might be from our current
34 	   child nodes. When this refcount reaches 0, it means we must detach
35 	   from our parent. */
36 	unsigned int parent_link_refcount:30;
37 	/* If uid is expunged, rebuild the thread tree. */
38 	bool expunge_rebuilds:1;
39 	/* If a link between this node and its child gets unreferenced,
40 	   rebuild the thread tree. */
41 	bool child_unref_rebuilds:1;
42 };
43 ARRAY_DEFINE_TYPE(mail_thread_node, struct mail_thread_node);
44 #define MAIL_THREAD_NODE_EXISTS(node) \
45 	((node)->uid != 0)
46 
47 struct mail_thread_cache {
48 	uint32_t last_uid;
49 	/* indexes used for invalid Message-IDs. that means no other messages
50 	   point to them and they can safely be moved around whenever
51 	   necessary. */
52 	uint32_t first_invalid_msgid_str_idx;
53 	uint32_t next_invalid_msgid_str_idx;
54 
55 	struct mail_search_result *search_result;
56 
57 	/* indexed by mail_index_strmap_rec.str_idx */
58 	ARRAY_TYPE(mail_thread_node) thread_nodes;
59 };
60 
crc32_str_nonzero(const char * str)61 static inline uint32_t crc32_str_nonzero(const char *str)
62 {
63 	uint32_t value = crc32_str(str);
64 	return value == 0 ? 1 : value;
65 }
66 
67 void mail_thread_add(struct mail_thread_cache *cache,
68 		     const struct mail_index_strmap_rec *msgid_map,
69 		     unsigned int *msgid_map_idx);
70 bool mail_thread_remove(struct mail_thread_cache *cache,
71 			const struct mail_index_strmap_rec *msgid_map,
72 			unsigned int *msgid_map_idx);
73 
74 struct mail_thread_iterate_context *
75 mail_thread_iterate_init_full(struct mail_thread_cache *cache,
76 			      struct mail *tmp_mail,
77 			      enum mail_thread_type thread_type,
78 			      bool return_seqs);
79 
80 void index_thread_mailbox_opened(struct mailbox *box);
81 
82 #endif
83