1 // This module handles fulltext indexing of the message base.
3 // Copyright (c) 2005-2023 by the citadel.org team
5 // This program is open source software. Use, duplication, or disclosure
6 // is subject to the terms of the GNU General Public License, version 3.
8 #include "../../sysdep.h"
16 #include <sys/types.h>
21 #include <libcitadel.h>
22 #include "../../citadel_defs.h"
23 #include "../../server.h"
24 #include "../../citserver.h"
25 #include "../../support.h"
26 #include "../../config.h"
27 #include "../../room_ops.h"
28 #include "../../database.h"
29 #include "../../msgbase.h"
30 #include "../../control.h"
31 #include "serv_fulltext.h"
32 #include "ft_wordbreaker.h"
33 #include "../../threads.h"
34 #include "../../context.h"
35 #include "../../ctdl_module.h"
38 // These can be global variables because only one indexer runs at a time.
39 Array *messages_to_be_indexed = NULL;
40 long highest_msg_already_indexed = 0;
41 long highest_msg_to_be_indexed = 0;
45 int longcmp(const void *rec1, const void *rec2) {
48 i1 = *(const long *)rec1;
49 i2 = *(const long *)rec2;
51 if (i1 > i2) return(1);
52 if (i1 < i2) return(-1);
57 // Index or de-index a message. (op == 1 to index, 0 to de-index)
58 void ft_index_message(long msgnum, int op) {
60 Array *tokens_in_this_message = NULL;
61 struct cdbdata cdb_bucket;
65 struct CtdlMessage *msg = NULL;
67 if (msgnum == 0) return;
69 msg = CtdlFetchMessage(msgnum, 1);
71 // This is not necessarily an error condition; it could simply mean that the message was
72 // deleted before it could be indexed. This happens often when the load tester is running.
73 syslog(LOG_ERR, "fulltext: ft_index_message() could not load msg %ld", msgnum);
77 if (!CM_IsEmpty(msg, eSuppressIdx)) {
78 syslog(LOG_DEBUG, "fulltext: ft_index_message() excluded msg %ld", msgnum);
83 syslog(LOG_DEBUG, "fulltext: ft_index_message() %s msg %ld", (op ? "adding" : "removing") , msgnum);
85 // Output the message as text before indexing it, so we don't end up indexing a bunch of encoded base64, etc.
86 CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
87 CtdlOutputPreLoadedMsg(msg, MT_CITADEL, HEADERS_ALL, 0, 1, 0);
89 msgtext = CC->redirect_buffer;
90 CC->redirect_buffer = NULL;
91 if (msgtext != NULL) {
92 syslog(LOG_DEBUG, "fulltext: wordbreaking message %ld (%d bytes)", msgnum, StrLength(msgtext));
94 txt = SmashStrBuf(&msgtext);
95 tokens_in_this_message = wordbreaker(txt);
98 syslog(LOG_DEBUG, "fulltext: %sindexing message %ld [%d tokens]",
101 array_len(tokens_in_this_message)
104 if (array_len(tokens_in_this_message) > 0) {
105 begin_critical_section(S_INDEXER);
106 cdb_begin_transaction();
107 for (i=0; i<array_len(tokens_in_this_message); ++i) {
109 // Identify the bucket which we will be modifying
110 memcpy(&tok, array_get_element_at(tokens_in_this_message, i), sizeof(int));
113 cdb_bucket = cdb_fetch(CDB_FULLTEXT, &tok, sizeof(int));
114 long *newbucket = malloc(cdb_bucket.len + sizeof(long));
115 int nmsgs = cdb_bucket.len / sizeof(long);
117 if (op == 1) { // indexing, add this message to the bucket
118 memcpy(&newbucket[0], cdb_bucket.ptr, cdb_bucket.len);
119 memcpy(&newbucket[nmsgs++], &msgnum, sizeof(long));
122 else if (op == 0) { // deindexing, remove this message from the bucket
123 memcpy(newbucket, cdb_bucket.ptr, cdb_bucket.len);
124 for (j=0; j<nmsgs; ++j) {
125 if ((newbucket[j] == msgnum) || (newbucket[j] == 0)) {
126 memcpy(&newbucket[j], &newbucket[j+1], ((nmsgs-j)*sizeof(long)));
133 // Then write it back to disk
134 cdb_store(CDB_FULLTEXT, &tok, sizeof(int), newbucket, (nmsgs*sizeof(long)));
137 if (server_shutting_down) break;
139 cdb_end_transaction();
140 end_critical_section(S_INDEXER);
141 CtdlSetConfigLong("MMfulltext", msgnum);
143 array_free(tokens_in_this_message);
147 // Scan a room for messages to index.
148 void ft_index_room(struct ctdlroom *qrbuf, void *data) {
149 if (server_shutting_down) {
157 // 2023aug30 ajc - old code did another CtdlGetRoom() here. Not only is that redundant,
158 // but for some reason it also made Berkeley DB deadlock after a while. I don't know why.
160 // qrbuf is already populated. fetch the list of messages in this room.
161 num_msgs = CtdlFetchMsgList(qrbuf->QRnumber, &msglist);
163 // Identify messages which have NOT yet been seen by the indexer.
164 if (msglist != NULL) {
165 for (i=0; i<num_msgs; ++i) {
168 && (msglist[i] > highest_msg_already_indexed)
169 && (msglist[i] <= highest_msg_to_be_indexed)
171 array_append(messages_to_be_indexed, &msglist[i]);
179 // Begin the fulltext indexing process.
180 void do_fulltext_indexing(void) {
182 static time_t last_progress = 0L;
183 static int is_running = 0;
184 if (is_running) return; // Concurrency check - only one can run
187 // Don't do this if the site doesn't have it enabled.
188 if (!CtdlGetConfigInt("c_enable_fulltext")) {
191 syslog(LOG_DEBUG, "fulltext: indexing started");
193 // If we've switched wordbreaker modules, burn the index and start over.
194 begin_critical_section(S_CONTROL);
195 if (CtdlGetConfigInt("MM_fulltext_wordbreaker") != FT_WORDBREAKER_ID) {
196 syslog(LOG_DEBUG, "fulltext: wb ver on disk = %d, code ver = %d",
197 CtdlGetConfigInt("MM_fulltext_wordbreaker"), FT_WORDBREAKER_ID
199 syslog(LOG_INFO, "fulltext: (re)initializing index");
200 cdb_trunc(CDB_FULLTEXT);
201 CtdlSetConfigLong("MMfulltext", 0);
203 end_critical_section(S_CONTROL);
205 // Silently return if our fulltext index is up to date with new messages.
206 if ((CtdlGetConfigLong("MMfulltext") >= CtdlGetConfigLong("MMhighest"))) {
207 return; // nothing to do!
210 highest_msg_already_indexed = CtdlGetConfigLong("MMfulltext");
211 highest_msg_to_be_indexed = CtdlGetConfigLong("MMhighest");
212 messages_to_be_indexed = array_new(sizeof(long));
214 // Now go through each room and find messages to index.
215 CtdlForEachRoom(ft_index_room, NULL); // load all msg pointers
216 array_sort(messages_to_be_indexed, longcmp); // sort them
218 // Here it is ... do each message!
220 long prev_msgnum = 0;
221 for (i=0; i<array_len(messages_to_be_indexed); ++i) {
222 memcpy(&msgnum, array_get_element_at(messages_to_be_indexed, i), sizeof(long));
224 if (msgnum != prev_msgnum) { // careful to avoid dupes
225 ft_index_message(msgnum, 1);
228 prev_msgnum = msgnum;
231 array_free(messages_to_be_indexed);
233 CtdlSetConfigInt("MM_fulltext_wordbreaker", FT_WORDBREAKER_ID);
235 syslog(LOG_DEBUG, "fulltext: indexing finished");
241 // API call to perform searches.
242 // (This one does the "all of these words" search.)
243 // Caller is responsible for freeing the message list.
244 void ft_search(int *fts_num_msgs, long **fts_msgs, const char *search_string) {
247 struct cdbdata cdb_bucket;
248 int num_all_msgs = 0;
249 long *all_msgs = NULL;
250 int num_ret_msgs = 0;
251 int num_ret_alloc = 0;
252 long *ret_msgs = NULL;
256 t = wordbreaker(search_string);
257 if (array_len(t) > 0) {
258 for (i=0; i<array_len(t); ++i) {
260 // search for tokens[i]
261 memcpy(&tok, array_get_element_at(t, i), sizeof(int));
263 // fetch the bucket, Liza
264 if (ftc_msgs[tok] == NULL) {
265 cdb_bucket = cdb_fetch(CDB_FULLTEXT, &tok, sizeof(int));
266 if (cdb_bucket.ptr != NULL) {
267 ftc_num_msgs[tok] = cdb_bucket.len / sizeof(long);
268 ftc_msgs[tok] = (long *) malloc(cdb_bucket.len);
269 memcpy(ftc_msgs[tok], cdb_bucket.ptr, cdb_bucket.len);
272 ftc_num_msgs[tok] = 0;
273 ftc_msgs[tok] = malloc(sizeof(long));
277 num_all_msgs += ftc_num_msgs[tok];
278 if (num_all_msgs > 0) {
279 all_msgs = realloc(all_msgs, num_all_msgs*sizeof(long) );
280 memcpy(&all_msgs[num_all_msgs-ftc_num_msgs[tok]], ftc_msgs[tok], ftc_num_msgs[tok]*sizeof(long) );
285 if (all_msgs != NULL) {
286 qsort(all_msgs, num_all_msgs, sizeof(long), longcmp);
288 // At this point, if a message appears array_len(t) times in the
289 // list, then it contains all of the search tokens.
290 if (num_all_msgs >= array_len(t))
291 for (j=0; j<(num_all_msgs-array_len(t)+1); ++j) {
292 if (all_msgs[j] == all_msgs[j+array_len(t)-1]) {
294 if (num_ret_msgs > num_ret_alloc) {
296 ret_msgs = realloc(ret_msgs, (num_ret_alloc*sizeof(long)) );
298 ret_msgs[num_ret_msgs - 1] = all_msgs[j];
306 *fts_num_msgs = num_ret_msgs;
307 *fts_msgs = ret_msgs;
312 // This search command is for diagnostic purposes and may be removed or replaced.
313 void cmd_srch(char *argbuf) {
317 char search_string[256];
319 if (CtdlAccessCheck(ac_logged_in)) return;
321 if (!CtdlGetConfigInt("c_enable_fulltext")) {
322 cprintf("%d Full text index is not enabled on this server.\n", ERROR + CMD_NOT_SUPPORTED);
326 extract_token(search_string, argbuf, 0, '|', sizeof search_string);
327 ft_search(&num_msgs, &msgs, search_string);
329 cprintf("%d %d msgs match all search words:\n", LISTING_FOLLOWS, num_msgs);
331 for (i=0; i<num_msgs; ++i) {
332 cprintf("%ld\n", msgs[i]);
335 if (msgs != NULL) free(msgs);
340 void ft_delete_remove(char *room, long msgnum) {
343 // Remove from fulltext index
344 if (CtdlGetConfigInt("c_enable_fulltext")) {
345 ft_index_message(msgnum, 0);
350 // Initialization function, called from modules_init.c
351 char *ctdl_module_init_fulltext(void) {
353 CtdlRegisterProtoHook(cmd_srch, "SRCH", "Full text search");
354 CtdlRegisterDeleteHook(ft_delete_remove);
355 CtdlRegisterSearchFuncHook(ft_search, "fulltext");
356 CtdlRegisterSessionHook(do_fulltext_indexing, EVT_TIMER, PRIO_CLEANUP + 300);
358 // return our module name for the log