2 * This module handles fulltext indexing of the message base.
3 * Copyright (c) 2005-2011 by the citadel.org team
5 * This program is open source software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as published
7 * by the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include <sys/types.h>
30 #if TIME_WITH_SYS_TIME
31 # include <sys/time.h>
35 # include <sys/time.h>
44 #include <libcitadel.h>
47 #include "citserver.h"
53 #include "serv_fulltext.h"
54 #include "ft_wordbreaker.h"
58 #include "ctdl_module.h"
60 long ft_newhighest = 0L;
61 long *ft_newmsgs = NULL;
65 int ftc_num_msgs[65536];
66 long *ftc_msgs[65536];
72 int longcmp(const void *rec1, const void *rec2) {
75 i1 = *(const long *)rec1;
76 i2 = *(const long *)rec2;
78 if (i1 > i2) return(1);
79 if (i1 < i2) return(-1);
84 * Flush our index cache out to disk.
86 void ft_flush_cache(void) {
88 time_t last_update = 0;
90 for (i=0; i<65536; ++i) {
91 if ((time(NULL) - last_update) >= 10) {
93 "Flushing index cache to disk (%d%% complete)",
96 last_update = time(NULL);
98 if (ftc_msgs[i] != NULL) {
99 cdb_store(CDB_FULLTEXT, &i, sizeof(int), ftc_msgs[i],
100 (ftc_num_msgs[i] * sizeof(long)));
106 syslog(LOG_INFO, "Flushed index cache to disk (100%% complete)");
111 * Index or de-index a message. (op == 1 to index, 0 to de-index)
113 void ft_index_message(long msgnum, int op) {
117 struct cdbdata *cdb_bucket;
121 struct CtdlMessage *msg = NULL;
123 msg = CtdlFetchMessage(msgnum, 1);
125 syslog(LOG_ERR, "ft_index_message() could not load msg %ld", msgnum);
129 if (msg->cm_fields['1'] != NULL) {
130 syslog(LOG_DEBUG, "ft_index_message() excluded msg %ld", msgnum);
131 CtdlFreeMessage(msg);
135 syslog(LOG_DEBUG, "ft_index_message() %s msg %ld", (op ? "adding" : "removing") , msgnum);
137 /* Output the message as text before indexing it, so we don't end up
138 * indexing a bunch of encoded base64, etc.
140 CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
141 CtdlOutputPreLoadedMsg(msg, MT_CITADEL, HEADERS_ALL, 0, 1, 0);
142 CtdlFreeMessage(msg);
143 msgtext = CC->redirect_buffer;
144 CC->redirect_buffer = NULL;
145 syslog(LOG_DEBUG, "Wordbreaking message %ld...", msgnum);
146 if ((msgtext == NULL) || (StrLength(msgtext) == 0)) {
147 syslog(LOG_ALERT, "This message has a zero length. Probable data corruption.");
149 txt = SmashStrBuf(&msgtext);
150 wordbreaker(txt, &num_tokens, &tokens);
153 syslog(LOG_DEBUG, "Indexing message %ld [%d tokens]", msgnum, num_tokens);
154 if (num_tokens > 0) {
155 for (i=0; i<num_tokens; ++i) {
157 /* Add the message to the relevant token bucket */
159 /* search for tokens[i] */
162 if ( (tok >= 0) && (tok <= 65535) ) {
163 /* fetch the bucket, Liza */
164 if (ftc_msgs[tok] == NULL) {
165 cdb_bucket = cdb_fetch(CDB_FULLTEXT, &tok, sizeof(int));
166 if (cdb_bucket != NULL) {
167 ftc_num_msgs[tok] = cdb_bucket->len / sizeof(long);
168 ftc_msgs[tok] = (long *)cdb_bucket->ptr;
169 cdb_bucket->ptr = NULL;
170 cdb_free(cdb_bucket);
173 ftc_num_msgs[tok] = 0;
174 ftc_msgs[tok] = malloc(sizeof(long));
179 if (op == 1) { /* add to index */
181 ftc_msgs[tok] = realloc(ftc_msgs[tok],
182 ftc_num_msgs[tok]*sizeof(long));
183 ftc_msgs[tok][ftc_num_msgs[tok] - 1] = msgnum;
186 if (op == 0) { /* remove from index */
187 if (ftc_num_msgs[tok] >= 1) {
188 for (j=0; j<ftc_num_msgs[tok]; ++j) {
189 if (ftc_msgs[tok][j] == msgnum) {
190 memmove(&ftc_msgs[tok][j], &ftc_msgs[tok][j+1], ((ftc_num_msgs[tok] - j - 1)*sizeof(long)));
199 syslog(LOG_ALERT, "Invalid token %d !!", tok);
210 * Add a message to the list of those to be indexed.
212 void ft_index_msg(long msgnum, void *userdata) {
214 if ((msgnum > CitControl.MMfulltext) && (msgnum <= ft_newhighest)) {
216 if (ft_num_msgs > ft_num_alloc) {
217 ft_num_alloc += 1024;
218 ft_newmsgs = realloc(ft_newmsgs,
219 (ft_num_alloc * sizeof(long)));
221 ft_newmsgs[ft_num_msgs - 1] = msgnum;
227 * Scan a room for messages to index.
229 void ft_index_room(struct ctdlroom *qrbuf, void *data)
231 if (server_shutting_down)
234 CtdlGetRoom(&CC->room, qrbuf->QRname);
235 CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, ft_index_msg, NULL);
240 * Begin the fulltext indexing process.
242 void do_fulltext_indexing(void) {
244 static time_t last_index = 0L;
245 static time_t last_progress = 0L;
246 time_t run_time = 0L;
247 time_t end_time = 0L;
248 static int is_running = 0;
249 if (is_running) return; /* Concurrency check - only one can run */
253 * Don't do this if the site doesn't have it enabled.
255 if (!config.c_enable_fulltext) {
260 * Make sure we don't run the indexer too frequently.
261 * FIXME move the setting into config
264 if ( (time(NULL) - last_index) < 300L) {
269 * Check to see whether the fulltext index is up to date; if there
270 * are no messages to index, don't waste any more time trying.
272 if ((CitControl.MMfulltext >= CitControl.MMhighest) && (CitControl.fulltext_wordbreaker == FT_WORDBREAKER_ID)) {
273 return; /* nothing to do! */
276 run_time = time(NULL);
277 syslog(LOG_DEBUG, "do_fulltext_indexing() started (%ld)", run_time);
280 * If we've switched wordbreaker modules, burn the index and start
283 begin_critical_section(S_CONTROL);
284 if (CitControl.fulltext_wordbreaker != FT_WORDBREAKER_ID) {
285 syslog(LOG_DEBUG, "wb ver on disk = %d, code ver = %d",
286 CitControl.fulltext_wordbreaker, FT_WORDBREAKER_ID
288 syslog(LOG_INFO, "(re)initializing full text index");
289 cdb_trunc(CDB_FULLTEXT);
290 CitControl.MMfulltext = 0L;
293 end_critical_section(S_CONTROL);
296 * Now go through each room and find messages to index.
298 ft_newhighest = CitControl.MMhighest;
299 CtdlForEachRoom(ft_index_room, NULL); /* load all msg pointers */
301 if (ft_num_msgs > 0) {
302 qsort(ft_newmsgs, ft_num_msgs, sizeof(long), longcmp);
303 for (i=0; i<(ft_num_msgs-1); ++i) { /* purge dups */
304 if (ft_newmsgs[i] == ft_newmsgs[i+1]) {
305 memmove(&ft_newmsgs[i], &ft_newmsgs[i+1],
306 ((ft_num_msgs - i - 1)*sizeof(long)));
312 /* Here it is ... do each message! */
313 for (i=0; i<ft_num_msgs; ++i) {
314 if (time(NULL) != last_progress) {
316 "Indexed %d of %d messages (%d%%)",
318 ((i*100) / ft_num_msgs)
320 last_progress = time(NULL);
322 ft_index_message(ft_newmsgs[i], 1);
324 /* Check to see if we need to quit early */
325 if (server_shutting_down) {
326 syslog(LOG_DEBUG, "Indexer quitting early");
327 ft_newhighest = ft_newmsgs[i];
331 /* Check to see if we have to maybe flush to disk */
332 if (i >= FT_MAX_CACHE) {
333 syslog(LOG_DEBUG, "Time to flush.");
334 ft_newhighest = ft_newmsgs[i];
345 end_time = time(NULL);
347 if (server_shutting_down) {
352 syslog(LOG_DEBUG, "do_fulltext_indexing() duration (%ld)", end_time - run_time);
354 /* Save our place so we don't have to do this again */
356 begin_critical_section(S_CONTROL);
357 CitControl.MMfulltext = ft_newhighest;
358 CitControl.fulltext_wordbreaker = FT_WORDBREAKER_ID;
360 end_critical_section(S_CONTROL);
361 last_index = time(NULL);
363 syslog(LOG_DEBUG, "do_fulltext_indexing() finished");
371 * API call to perform searches.
372 * (This one does the "all of these words" search.)
373 * Caller is responsible for freeing the message list.
375 void ft_search(int *fts_num_msgs, long **fts_msgs, const char *search_string) {
379 struct cdbdata *cdb_bucket;
380 int num_all_msgs = 0;
381 long *all_msgs = NULL;
382 int num_ret_msgs = 0;
383 int num_ret_alloc = 0;
384 long *ret_msgs = NULL;
387 wordbreaker(search_string, &num_tokens, &tokens);
388 if (num_tokens > 0) {
389 for (i=0; i<num_tokens; ++i) {
391 /* search for tokens[i] */
394 /* fetch the bucket, Liza */
395 if (ftc_msgs[tok] == NULL) {
396 cdb_bucket = cdb_fetch(CDB_FULLTEXT, &tok, sizeof(int));
397 if (cdb_bucket != NULL) {
398 ftc_num_msgs[tok] = cdb_bucket->len / sizeof(long);
399 ftc_msgs[tok] = (long *)cdb_bucket->ptr;
400 cdb_bucket->ptr = NULL;
401 cdb_free(cdb_bucket);
404 ftc_num_msgs[tok] = 0;
405 ftc_msgs[tok] = malloc(sizeof(long));
409 num_all_msgs += ftc_num_msgs[tok];
410 if (num_all_msgs > 0) {
411 all_msgs = realloc(all_msgs, num_all_msgs*sizeof(long) );
412 memcpy(&all_msgs[num_all_msgs-ftc_num_msgs[tok]],
413 ftc_msgs[tok], ftc_num_msgs[tok]*sizeof(long) );
418 qsort(all_msgs, num_all_msgs, sizeof(long), longcmp);
421 * At this point, if a message appears num_tokens times in the
422 * list, then it contains all of the search tokens.
424 if (num_all_msgs >= num_tokens)
425 for (j=0; j<(num_all_msgs-num_tokens+1); ++j) {
426 if (all_msgs[j] == all_msgs[j+num_tokens-1]) {
429 if (num_ret_msgs > num_ret_alloc) {
431 ret_msgs = realloc(ret_msgs,
432 (num_ret_alloc*sizeof(long)) );
434 ret_msgs[num_ret_msgs - 1] = all_msgs[j];
442 *fts_num_msgs = num_ret_msgs;
443 *fts_msgs = ret_msgs;
448 * This search command is for diagnostic purposes and may be removed or replaced.
450 void cmd_srch(char *argbuf) {
454 char search_string[256];
456 if (CtdlAccessCheck(ac_logged_in)) return;
458 if (!config.c_enable_fulltext) {
459 cprintf("%d Full text index is not enabled on this server.\n",
460 ERROR + CMD_NOT_SUPPORTED);
464 extract_token(search_string, argbuf, 0, '|', sizeof search_string);
465 ft_search(&num_msgs, &msgs, search_string);
467 cprintf("%d %d msgs match all search words:\n",
468 LISTING_FOLLOWS, num_msgs);
470 for (i=0; i<num_msgs; ++i) {
471 cprintf("%ld\n", msgs[i]);
474 if (msgs != NULL) free(msgs);
479 * Zero out our index cache.
481 void initialize_ft_cache(void) {
482 memset(ftc_num_msgs, 0, (65536 * sizeof(int)));
483 memset(ftc_msgs, 0, (65536 * sizeof(long *)));
487 void ft_delete_remove(char *room, long msgnum)
491 /* Remove from fulltext index */
492 if (config.c_enable_fulltext) {
493 ft_index_message(msgnum, 0);
497 /*****************************************************************************/
499 CTDL_MODULE_INIT(fulltext)
503 initialize_ft_cache();
504 initialize_noise_words();
505 CtdlRegisterProtoHook(cmd_srch, "SRCH", "Full text search");
506 CtdlRegisterDeleteHook(ft_delete_remove);
507 CtdlRegisterSearchFuncHook(ft_search, "fulltext");
508 CtdlRegisterCleanupHook(noise_word_cleanup);
509 CtdlRegisterSessionHook(do_fulltext_indexing, EVT_TIMER);
511 /* return our module name for the log */