4 * This module handles fulltext indexing of the message base.
7 * Copyright (c) 2005-2009 by the citadel.org team
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33 #include <sys/types.h>
35 #if TIME_WITH_SYS_TIME
36 # include <sys/time.h>
40 # include <sys/time.h>
49 #include <libcitadel.h>
52 #include "citserver.h"
58 #include "serv_fulltext.h"
59 #include "ft_wordbreaker.h"
63 #include "ctdl_module.h"
67 long ft_newhighest = 0L;
68 long *ft_newmsgs = NULL;
73 int ftc_num_msgs[65536];
74 long *ftc_msgs[65536];
80 int longcmp(const void *rec1, const void *rec2) {
83 i1 = *(const long *)rec1;
84 i2 = *(const long *)rec2;
86 if (i1 > i2) return(1);
87 if (i1 < i2) return(-1);
92 * Flush our index cache out to disk.
94 void ft_flush_cache(void) {
96 time_t last_update = 0;
98 for (i=0; i<65536; ++i) {
99 if ((time(NULL) - last_update) >= 10) {
100 CtdlLogPrintf(CTDL_INFO,
101 "Flushing index cache to disk (%d%% complete)\n",
104 last_update = time(NULL);
106 if (ftc_msgs[i] != NULL) {
107 cdb_store(CDB_FULLTEXT, &i, sizeof(int), ftc_msgs[i],
108 (ftc_num_msgs[i] * sizeof(long)));
114 CtdlLogPrintf(CTDL_INFO, "Flushed index cache to disk (100%% complete)\n");
119 * Index or de-index a message. (op == 1 to index, 0 to de-index)
121 void ft_index_message(long msgnum, int op) {
125 struct cdbdata *cdb_bucket;
129 struct CtdlMessage *msg = NULL;
131 msg = CtdlFetchMessage(msgnum, 1);
133 CtdlLogPrintf(CTDL_ERR, "ft_index_message() could not load msg %ld\n", msgnum);
137 if (msg->cm_fields['1'] != NULL) {
138 CtdlLogPrintf(CTDL_DEBUG, "ft_index_message() excluded msg %ld\n", msgnum);
139 CtdlFreeMessage(msg);
143 CtdlLogPrintf(CTDL_DEBUG, "ft_index_message() %s msg %ld\n",
144 (op ? "adding" : "removing") , msgnum
147 /* Output the message as text before indexing it, so we don't end up
148 * indexing a bunch of encoded base64, etc.
150 CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
151 CtdlOutputPreLoadedMsg(msg, MT_CITADEL, HEADERS_ALL, 0, 1, 0);
152 CtdlFreeMessage(msg);
153 msgtext = CC->redirect_buffer;
154 CC->redirect_buffer = NULL;
155 CtdlLogPrintf(CTDL_DEBUG, "Wordbreaking message %ld...\n", msgnum);
156 txt = SmashStrBuf(&msgtext);
157 wordbreaker(txt, &num_tokens, &tokens);
160 CtdlLogPrintf(CTDL_DEBUG, "Indexing message %ld [%d tokens]\n", msgnum, num_tokens);
161 if (num_tokens > 0) {
162 for (i=0; i<num_tokens; ++i) {
164 /* Add the message to the relevant token bucket */
166 /* search for tokens[i] */
169 if ( (tok >= 0) && (tok <= 65535) ) {
170 /* fetch the bucket, Liza */
171 if (ftc_msgs[tok] == NULL) {
172 cdb_bucket = cdb_fetch(CDB_FULLTEXT, &tok, sizeof(int));
173 if (cdb_bucket != NULL) {
174 ftc_num_msgs[tok] = cdb_bucket->len / sizeof(long);
175 ftc_msgs[tok] = (long *)cdb_bucket->ptr;
176 cdb_bucket->ptr = NULL;
177 cdb_free(cdb_bucket);
180 ftc_num_msgs[tok] = 0;
181 ftc_msgs[tok] = malloc(sizeof(long));
186 if (op == 1) { /* add to index */
188 ftc_msgs[tok] = realloc(ftc_msgs[tok],
189 ftc_num_msgs[tok]*sizeof(long));
190 ftc_msgs[tok][ftc_num_msgs[tok] - 1] = msgnum;
193 if (op == 0) { /* remove from index */
194 if (ftc_num_msgs[tok] >= 1) {
195 for (j=0; j<ftc_num_msgs[tok]; ++j) {
196 if (ftc_msgs[tok][j] == msgnum) {
197 memmove(&ftc_msgs[tok][j], &ftc_msgs[tok][j+1], ((ftc_num_msgs[tok] - j - 1)*sizeof(long)));
206 CtdlLogPrintf(CTDL_ALERT, "Invalid token %d !!\n", tok);
217 * Add a message to the list of those to be indexed.
219 void ft_index_msg(long msgnum, void *userdata) {
221 if ((msgnum > CitControl.MMfulltext) && (msgnum <= ft_newhighest)) {
223 if (ft_num_msgs > ft_num_alloc) {
224 ft_num_alloc += 1024;
225 ft_newmsgs = realloc(ft_newmsgs,
226 (ft_num_alloc * sizeof(long)));
228 ft_newmsgs[ft_num_msgs - 1] = msgnum;
234 * Scan a room for messages to index.
236 void ft_index_room(struct ctdlroom *qrbuf, void *data)
238 if (CtdlThreadCheckStop())
241 CtdlGetRoom(&CC->room, qrbuf->QRname);
242 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, ft_index_msg, NULL);
247 * Begin the fulltext indexing process.
249 void do_fulltext_indexing(void) {
251 static time_t last_index = 0L;
252 static time_t last_progress = 0L;
253 time_t run_time = 0L;
254 time_t end_time = 0L;
257 * Don't do this if the site doesn't have it enabled.
259 if (!config.c_enable_fulltext) {
264 * Make sure we don't run the indexer too frequently.
265 * FIXME move the setting into config
268 * The thread sleeps for 300 seconds so we don't need this here any more
270 if ( (time(NULL) - last_index) < 300L) {
276 * Check to see whether the fulltext index is up to date; if there
277 * are no messages to index, don't waste any more time trying.
279 if ((CitControl.MMfulltext >= CitControl.MMhighest) && (CitControl.fulltext_wordbreaker == FT_WORDBREAKER_ID)) {
280 return; /* nothing to do! */
283 run_time = time(NULL);
284 CtdlLogPrintf(CTDL_DEBUG, "do_fulltext_indexing() started (%ld)\n", run_time);
287 * If we've switched wordbreaker modules, burn the index and start
290 begin_critical_section(S_CONTROL);
291 if (CitControl.fulltext_wordbreaker != FT_WORDBREAKER_ID) {
292 CtdlLogPrintf(CTDL_DEBUG, "wb ver on disk = %d, code ver = %d\n",
293 CitControl.fulltext_wordbreaker, FT_WORDBREAKER_ID);
294 CtdlLogPrintf(CTDL_INFO, "(re)initializing full text index\n");
295 cdb_trunc(CDB_FULLTEXT);
296 CitControl.MMfulltext = 0L;
299 end_critical_section(S_CONTROL);
302 * Now go through each room and find messages to index.
304 ft_newhighest = CitControl.MMhighest;
305 CtdlForEachRoom(ft_index_room, NULL); /* load all msg pointers */
307 if (ft_num_msgs > 0) {
308 qsort(ft_newmsgs, ft_num_msgs, sizeof(long), longcmp);
309 for (i=0; i<(ft_num_msgs-1); ++i) { /* purge dups */
310 if (ft_newmsgs[i] == ft_newmsgs[i+1]) {
311 memmove(&ft_newmsgs[i], &ft_newmsgs[i+1],
312 ((ft_num_msgs - i - 1)*sizeof(long)));
318 /* Here it is ... do each message! */
319 for (i=0; i<ft_num_msgs; ++i) {
320 if (time(NULL) != last_progress) {
321 CtdlLogPrintf(CTDL_DEBUG,
322 "Indexed %d of %d messages (%d%%)\n",
324 ((i*100) / ft_num_msgs)
326 last_progress = time(NULL);
328 ft_index_message(ft_newmsgs[i], 1);
330 /* Check to see if we need to quit early */
331 if (CtdlThreadCheckStop()) {
332 CtdlLogPrintf(CTDL_DEBUG, "Indexer quitting early\n");
333 ft_newhighest = ft_newmsgs[i];
337 /* Check to see if we have to maybe flush to disk */
338 if (i >= FT_MAX_CACHE) {
339 CtdlLogPrintf(CTDL_DEBUG, "Time to flush.\n");
340 ft_newhighest = ft_newmsgs[i];
351 end_time = time(NULL);
353 if (CtdlThreadCheckStop())
356 CtdlLogPrintf(CTDL_DEBUG, "do_fulltext_indexing() duration (%ld)\n", end_time - run_time);
358 /* Save our place so we don't have to do this again */
360 begin_critical_section(S_CONTROL);
361 CitControl.MMfulltext = ft_newhighest;
362 CitControl.fulltext_wordbreaker = FT_WORDBREAKER_ID;
364 end_critical_section(S_CONTROL);
365 last_index = time(NULL);
367 CtdlLogPrintf(CTDL_DEBUG, "do_fulltext_indexing() finished\n");
372 * Main loop for the indexer thread.
374 void *indexer_thread(void *arg) {
375 struct CitContext indexerCC;
377 CtdlLogPrintf(CTDL_DEBUG, "indexer_thread() initializing\n");
379 CtdlFillSystemContext(&indexerCC, "indexer");
380 citthread_setspecific(MyConKey, (void *)&indexerCC );
382 while (!CtdlThreadCheckStop()) {
383 do_fulltext_indexing();
384 CtdlThreadSleep(300);
387 CtdlLogPrintf(CTDL_DEBUG, "indexer_thread() exiting\n");
394 * API call to perform searches.
395 * (This one does the "all of these words" search.)
396 * Caller is responsible for freeing the message list.
398 void ft_search(int *fts_num_msgs, long **fts_msgs, const char *search_string) {
402 struct cdbdata *cdb_bucket;
403 int num_all_msgs = 0;
404 long *all_msgs = NULL;
405 int num_ret_msgs = 0;
406 int num_ret_alloc = 0;
407 long *ret_msgs = NULL;
410 wordbreaker(search_string, &num_tokens, &tokens);
411 if (num_tokens > 0) {
412 for (i=0; i<num_tokens; ++i) {
414 /* search for tokens[i] */
417 /* fetch the bucket, Liza */
418 if (ftc_msgs[tok] == NULL) {
419 cdb_bucket = cdb_fetch(CDB_FULLTEXT, &tok, sizeof(int));
420 if (cdb_bucket != NULL) {
421 ftc_num_msgs[tok] = cdb_bucket->len / sizeof(long);
422 ftc_msgs[tok] = (long *)cdb_bucket->ptr;
423 cdb_bucket->ptr = NULL;
424 cdb_free(cdb_bucket);
427 ftc_num_msgs[tok] = 0;
428 ftc_msgs[tok] = malloc(sizeof(long));
432 num_all_msgs += ftc_num_msgs[tok];
433 if (num_all_msgs > 0) {
434 all_msgs = realloc(all_msgs, num_all_msgs*sizeof(long) );
435 memcpy(&all_msgs[num_all_msgs-ftc_num_msgs[tok]],
436 ftc_msgs[tok], ftc_num_msgs[tok]*sizeof(long) );
441 qsort(all_msgs, num_all_msgs, sizeof(long), longcmp);
444 * At this point, if a message appears num_tokens times in the
445 * list, then it contains all of the search tokens.
447 if (num_all_msgs >= num_tokens)
448 for (j=0; j<(num_all_msgs-num_tokens+1); ++j) {
449 if (all_msgs[j] == all_msgs[j+num_tokens-1]) {
452 if (num_ret_msgs > num_ret_alloc) {
454 ret_msgs = realloc(ret_msgs,
455 (num_ret_alloc*sizeof(long)) );
457 ret_msgs[num_ret_msgs - 1] = all_msgs[j];
465 *fts_num_msgs = num_ret_msgs;
466 *fts_msgs = ret_msgs;
471 * This search command is for diagnostic purposes and may be removed or replaced.
473 void cmd_srch(char *argbuf) {
477 char search_string[256];
479 if (CtdlAccessCheck(ac_logged_in)) return;
481 if (!config.c_enable_fulltext) {
482 cprintf("%d Full text index is not enabled on this server.\n",
483 ERROR + CMD_NOT_SUPPORTED);
487 extract_token(search_string, argbuf, 0, '|', sizeof search_string);
488 ft_search(&num_msgs, &msgs, search_string);
490 cprintf("%d %d msgs match all search words:\n",
491 LISTING_FOLLOWS, num_msgs);
493 for (i=0; i<num_msgs; ++i) {
494 cprintf("%ld\n", msgs[i]);
497 if (msgs != NULL) free(msgs);
502 * Zero out our index cache.
504 void initialize_ft_cache(void) {
505 memset(ftc_num_msgs, 0, (65536 * sizeof(int)));
506 memset(ftc_msgs, 0, (65536 * sizeof(long *)));
510 void ft_delete_remove(char *room, long msgnum)
514 /* Remove from fulltext index */
515 if (config.c_enable_fulltext) {
516 ft_index_message(msgnum, 0);
520 /*****************************************************************************/
522 CTDL_MODULE_INIT(fulltext)
526 initialize_ft_cache();
527 initialize_noise_words();
528 CtdlRegisterProtoHook(cmd_srch, "SRCH", "Full text search");
529 CtdlRegisterDeleteHook(ft_delete_remove);
530 CtdlRegisterSearchFuncHook(ft_search, "fulltext");
531 CtdlRegisterCleanupHook(noise_word_cleanup);
535 CtdlThreadCreate("Indexer", CTDLTHREAD_BIGSTACK, indexer_thread, NULL);
537 /* return our Subversion id for the Log */