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 "../../database.h"
28 #include "../../msgbase.h"
29 #include "../../control.h"
30 #include "serv_fulltext.h"
31 #include "ft_wordbreaker.h"
32 #include "../../threads.h"
33 #include "../../context.h"
34 #include "../../ctdl_module.h"
36 long ft_newhighest = 0L;
37 long *ft_newmsgs = NULL;
41 int ftc_num_msgs[65536];
42 long *ftc_msgs[65536];
46 int longcmp(const void *rec1, const void *rec2) {
49 i1 = *(const long *)rec1;
50 i2 = *(const long *)rec2;
52 if (i1 > i2) return(1);
53 if (i1 < i2) return(-1);
58 // Flush our index cache out to disk.
59 void ft_flush_cache(void) {
61 time_t last_update = 0;
63 cdb_begin_transaction();
64 for (i=0; i<65536; ++i) {
65 if ((time(NULL) - last_update) >= 10) {
66 syslog(LOG_INFO, "fulltext: flushing index cache to disk (%d%% complete)", (i * 100 / 65536));
67 last_update = time(NULL);
69 if (ftc_msgs[i] != NULL) {
70 cdb_store(CDB_FULLTEXT, &i, sizeof(int), ftc_msgs[i], (ftc_num_msgs[i] * sizeof(long)));
76 cdb_end_transaction();
77 syslog(LOG_INFO, "fulltext: flushed index cache to disk (100%% complete)");
81 // Index or de-index a message. (op == 1 to index, 0 to de-index)
82 void ft_index_message(long msgnum, int op) {
86 struct cdbdata cdb_bucket;
90 struct CtdlMessage *msg = NULL;
92 msg = CtdlFetchMessage(msgnum, 1);
94 syslog(LOG_ERR, "fulltext: ft_index_message() could not load msg %ld", msgnum);
98 if (!CM_IsEmpty(msg, eSuppressIdx)) {
99 syslog(LOG_DEBUG, "fulltext: ft_index_message() excluded msg %ld", msgnum);
104 syslog(LOG_DEBUG, "fulltext: ft_index_message() %s msg %ld", (op ? "adding" : "removing") , msgnum);
106 // Output the message as text before indexing it, so we don't end up
107 // indexing a bunch of encoded base64, etc.
108 CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
109 CtdlOutputPreLoadedMsg(msg, MT_CITADEL, HEADERS_ALL, 0, 1, 0);
111 msgtext = CC->redirect_buffer;
112 CC->redirect_buffer = NULL;
113 if (msgtext != NULL) {
114 syslog(LOG_DEBUG, "fulltext: wordbreaking message %ld (%d bytes)", msgnum, StrLength(msgtext));
116 txt = SmashStrBuf(&msgtext);
117 wordbreaker(txt, &num_tokens, &tokens);
120 syslog(LOG_DEBUG, "fulltext: %sindexing message %ld [%d tokens]", (op ? "" : "de"), msgnum, num_tokens);
121 if (num_tokens > 0) {
122 for (i=0; i<num_tokens; ++i) {
124 // Add the message to the relevant token bucket
126 // search for tokens[i]
129 if ( (tok >= 0) && (tok <= 65535) ) {
130 // fetch the bucket, Liza
131 if (ftc_msgs[tok] == NULL) {
132 cdb_bucket = cdb_fetch(CDB_FULLTEXT, &tok, sizeof(int));
133 if (cdb_bucket.ptr != NULL) {
134 ftc_num_msgs[tok] = cdb_bucket.len / sizeof(long);
135 ftc_msgs[tok] = malloc(cdb_bucket.len);
136 memcpy(ftc_msgs[tok], cdb_bucket.ptr, cdb_bucket.len);
139 ftc_num_msgs[tok] = 0;
140 ftc_msgs[tok] = malloc(sizeof(long));
144 if (op == 1) { // add to index
146 ftc_msgs[tok] = realloc(ftc_msgs[tok], ftc_num_msgs[tok]*sizeof(long));
147 ftc_msgs[tok][ftc_num_msgs[tok] - 1] = msgnum;
150 if (op == 0) { // remove from index
151 if (ftc_num_msgs[tok] >= 1) {
152 for (j=0; j<ftc_num_msgs[tok]; ++j) {
153 if (ftc_msgs[tok][j] == msgnum) {
154 memmove(&ftc_msgs[tok][j], &ftc_msgs[tok][j+1], ((ftc_num_msgs[tok] - j - 1)*sizeof(long)));
163 syslog(LOG_ALERT, "fulltext: invalid token %d !!", tok);
172 // Add a message to the list of those to be indexed.
173 void ft_index_msg(long msgnum, void *userdata) {
175 if ((msgnum > CtdlGetConfigLong("MMfulltext")) && (msgnum <= ft_newhighest)) {
177 if (ft_num_msgs > ft_num_alloc) {
178 ft_num_alloc += 1024;
179 ft_newmsgs = realloc(ft_newmsgs, (ft_num_alloc * sizeof(long)));
181 ft_newmsgs[ft_num_msgs - 1] = msgnum;
187 // Scan a room for messages to index.
188 void ft_index_room(struct ctdlroom *qrbuf, void *data) {
189 if (server_shutting_down)
192 CtdlGetRoom(&CC->room, qrbuf->QRname);
193 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, ft_index_msg, NULL);
197 // Begin the fulltext indexing process.
198 void do_fulltext_indexing(void) {
200 static time_t last_progress = 0L;
201 static int is_running = 0;
202 if (is_running) return; // Concurrency check - only one can run
205 // Don't do this if the site doesn't have it enabled.
206 if (!CtdlGetConfigInt("c_enable_fulltext")) {
210 // If we've switched wordbreaker modules, burn the index and start over.
211 begin_critical_section(S_CONTROL);
212 if (CtdlGetConfigInt("MM_fulltext_wordbreaker") != FT_WORDBREAKER_ID) {
213 syslog(LOG_DEBUG, "fulltext: wb ver on disk = %d, code ver = %d",
214 CtdlGetConfigInt("MM_fulltext_wordbreaker"), FT_WORDBREAKER_ID
216 syslog(LOG_INFO, "fulltext: (re)initializing index");
217 cdb_trunc(CDB_FULLTEXT);
218 CtdlSetConfigLong("MMfulltext", 0);
220 end_critical_section(S_CONTROL);
222 // Silently return if our fulltext index is up to date with new messages.
223 if ((CtdlGetConfigLong("MMfulltext") >= CtdlGetConfigLong("MMhighest"))) {
224 return; // nothing to do!
227 // Now go through each room and find messages to index.
228 ft_newhighest = CtdlGetConfigLong("MMhighest");
229 CtdlForEachRoom(ft_index_room, NULL); // load all msg pointers
231 if (ft_num_msgs > 0) {
232 qsort(ft_newmsgs, ft_num_msgs, sizeof(long), longcmp);
233 for (i=0; i<(ft_num_msgs-1); ++i) { // purge dups
234 if (ft_newmsgs[i] == ft_newmsgs[i+1]) {
235 memmove(&ft_newmsgs[i], &ft_newmsgs[i+1], ((ft_num_msgs - i - 1)*sizeof(long)));
241 // Here it is ... do each message!
242 for (i=0; i<ft_num_msgs; ++i) {
243 if (time(NULL) != last_progress) {
245 "fulltext: indexed %d of %d messages (%d%%)", i, ft_num_msgs, ((i*100) / ft_num_msgs));
246 last_progress = time(NULL);
248 ft_index_message(ft_newmsgs[i], 1);
250 // Check to see if we need to quit early
251 if (server_shutting_down) {
252 syslog(LOG_DEBUG, "fulltext: indexer quitting early");
253 ft_newhighest = ft_newmsgs[i];
257 // Check to see if we have to maybe flush to disk
258 if (i >= FT_MAX_CACHE) {
259 syslog(LOG_DEBUG, "fulltext: time to flush.");
260 ft_newhighest = ft_newmsgs[i];
272 if (server_shutting_down) {
277 // Save our place so we don't have to do this again
279 begin_critical_section(S_CONTROL);
280 CtdlSetConfigLong("MMfulltext", ft_newhighest);
281 CtdlSetConfigInt("MM_fulltext_wordbreaker", FT_WORDBREAKER_ID);
282 end_critical_section(S_CONTROL);
284 syslog(LOG_DEBUG, "fulltext: indexing finished");
290 // API call to perform searches.
291 // (This one does the "all of these words" search.)
292 // Caller is responsible for freeing the message list.
293 void ft_search(int *fts_num_msgs, long **fts_msgs, const char *search_string) {
297 struct cdbdata cdb_bucket;
298 int num_all_msgs = 0;
299 long *all_msgs = NULL;
300 int num_ret_msgs = 0;
301 int num_ret_alloc = 0;
302 long *ret_msgs = NULL;
305 wordbreaker(search_string, &num_tokens, &tokens);
306 if (num_tokens > 0) {
307 for (i=0; i<num_tokens; ++i) {
309 // search for tokens[i]
312 // fetch the bucket, Liza
313 if (ftc_msgs[tok] == NULL) {
314 cdb_bucket = cdb_fetch(CDB_FULLTEXT, &tok, sizeof(int));
315 if (cdb_bucket.ptr != NULL) {
316 ftc_num_msgs[tok] = cdb_bucket.len / sizeof(long);
317 ftc_msgs[tok] = (long *) malloc(cdb_bucket.len);
318 memcpy(ftc_msgs[tok], cdb_bucket.ptr, cdb_bucket.len);
321 ftc_num_msgs[tok] = 0;
322 ftc_msgs[tok] = malloc(sizeof(long));
326 num_all_msgs += ftc_num_msgs[tok];
327 if (num_all_msgs > 0) {
328 all_msgs = realloc(all_msgs, num_all_msgs*sizeof(long) );
329 memcpy(&all_msgs[num_all_msgs-ftc_num_msgs[tok]], ftc_msgs[tok], ftc_num_msgs[tok]*sizeof(long) );
334 if (all_msgs != NULL) {
335 qsort(all_msgs, num_all_msgs, sizeof(long), longcmp);
337 // At this point, if a message appears num_tokens times in the
338 // list, then it contains all of the search tokens.
339 if (num_all_msgs >= num_tokens)
340 for (j=0; j<(num_all_msgs-num_tokens+1); ++j) {
341 if (all_msgs[j] == all_msgs[j+num_tokens-1]) {
343 if (num_ret_msgs > num_ret_alloc) {
345 ret_msgs = realloc(ret_msgs, (num_ret_alloc*sizeof(long)) );
347 ret_msgs[num_ret_msgs - 1] = all_msgs[j];
355 *fts_num_msgs = num_ret_msgs;
356 *fts_msgs = ret_msgs;
360 // This search command is for diagnostic purposes and may be removed or replaced.
361 void cmd_srch(char *argbuf) {
365 char search_string[256];
367 if (CtdlAccessCheck(ac_logged_in)) return;
369 if (!CtdlGetConfigInt("c_enable_fulltext")) {
370 cprintf("%d Full text index is not enabled on this server.\n", ERROR + CMD_NOT_SUPPORTED);
374 extract_token(search_string, argbuf, 0, '|', sizeof search_string);
375 ft_search(&num_msgs, &msgs, search_string);
377 cprintf("%d %d msgs match all search words:\n",
378 LISTING_FOLLOWS, num_msgs);
380 for (i=0; i<num_msgs; ++i) {
381 cprintf("%ld\n", msgs[i]);
384 if (msgs != NULL) free(msgs);
389 // Zero out our index cache.
390 void initialize_ft_cache(void) {
391 memset(ftc_num_msgs, 0, (65536 * sizeof(int)));
392 memset(ftc_msgs, 0, (65536 * sizeof(long *)));
396 void ft_delete_remove(char *room, long msgnum) {
399 // Remove from fulltext index
400 if (CtdlGetConfigInt("c_enable_fulltext")) {
401 ft_index_message(msgnum, 0);
406 // Initialization function, called from modules_init.c
407 char *ctdl_module_init_fulltext(void) {
409 initialize_ft_cache();
410 CtdlRegisterProtoHook(cmd_srch, "SRCH", "Full text search");
411 CtdlRegisterDeleteHook(ft_delete_remove);
412 CtdlRegisterSearchFuncHook(ft_search, "fulltext");
413 CtdlRegisterSessionHook(do_fulltext_indexing, EVT_TIMER, PRIO_CLEANUP + 300);
415 // return our module name for the log