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 int longcmp(const void *rec1, const void *rec2) {
41 i1 = *(const long *)rec1;
42 i2 = *(const long *)rec2;
44 if (i1 > i2) return(1);
45 if (i1 < i2) return(-1);
50 //if (ftc_msgs[i] != NULL) {
51 //cdb_store(CDB_FULLTEXT, &i, sizeof(int), ftc_msgs[i], (ftc_num_msgs[i] * sizeof(long)));
55 // Index or de-index a message. (op == 1 to index, 0 to de-index)
56 void ft_index_message(long msgnum, int op) {
58 Array *tokens_in_this_message = NULL;
59 struct cdbdata cdb_bucket;
63 struct CtdlMessage *msg = NULL;
65 if (msgnum == 0) return;
67 msg = CtdlFetchMessage(msgnum, 1);
69 syslog(LOG_ERR, "fulltext: ft_index_message() could not load msg %ld", msgnum);
73 if (!CM_IsEmpty(msg, eSuppressIdx)) {
74 syslog(LOG_DEBUG, "fulltext: ft_index_message() excluded msg %ld", msgnum);
79 syslog(LOG_DEBUG, "fulltext: ft_index_message() %s msg %ld", (op ? "adding" : "removing") , msgnum);
81 // Output the message as text before indexing it, so we don't end up indexing a bunch of encoded base64, etc.
82 CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
83 CtdlOutputPreLoadedMsg(msg, MT_CITADEL, HEADERS_ALL, 0, 1, 0);
85 msgtext = CC->redirect_buffer;
86 CC->redirect_buffer = NULL;
87 if (msgtext != NULL) {
88 syslog(LOG_DEBUG, "fulltext: wordbreaking message %ld (%d bytes)", msgnum, StrLength(msgtext));
90 txt = SmashStrBuf(&msgtext);
91 tokens_in_this_message = wordbreaker(txt);
94 syslog(LOG_DEBUG, "fulltext: %sindexing message %ld [%d tokens]",
97 array_len(tokens_in_this_message)
100 if (array_len(tokens_in_this_message) > 0) {
101 for (i=0; i<array_len(tokens_in_this_message); ++i) {
103 // Identify the bucket which we will be modifying
104 memcpy(&tok, array_get_element_at(tokens_in_this_message, i), sizeof(int));
107 // cdb_bucket = cdb_fetch(CDB_FULLTEXT, &tok, sizeof(int));
108 // if op == 1 , add this msgnum to the record.
109 // if op == 0 , remove this msgnum from the record.
111 // FIXME then write it back to disk
114 array_free(tokens_in_this_message);
119 Array *messages_to_be_indexed = NULL;
120 long highest_msg_already_indexed = 0;
121 long highest_msg_to_be_indexed = 0;
124 // Scan a room for messages to index.
125 void ft_index_room(struct ctdlroom *qrbuf, void *data) {
126 if (server_shutting_down) {
129 CtdlGetRoom(&CC->room, qrbuf->QRname);
135 num_msgs = CtdlFetchMsgList(CC->room.QRnumber, &msglist);
136 if (msglist != NULL) {
137 for (i=0; i<num_msgs; ++i) {
140 && (msglist[i] > highest_msg_already_indexed)
141 && (msglist[i] <= highest_msg_to_be_indexed)
143 array_append(messages_to_be_indexed, &msglist[i]);
151 // Begin the fulltext indexing process.
152 void do_fulltext_indexing(void) {
154 static time_t last_progress = 0L;
155 static int is_running = 0;
156 if (is_running) return; // Concurrency check - only one can run
159 // Don't do this if the site doesn't have it enabled.
160 if (!CtdlGetConfigInt("c_enable_fulltext")) {
163 syslog(LOG_DEBUG, "fulltext: indexing started");
165 // If we've switched wordbreaker modules, burn the index and start over.
166 begin_critical_section(S_CONTROL);
167 if (CtdlGetConfigInt("MM_fulltext_wordbreaker") != FT_WORDBREAKER_ID) {
168 syslog(LOG_DEBUG, "fulltext: wb ver on disk = %d, code ver = %d",
169 CtdlGetConfigInt("MM_fulltext_wordbreaker"), FT_WORDBREAKER_ID
171 syslog(LOG_INFO, "fulltext: (re)initializing index");
172 cdb_trunc(CDB_FULLTEXT);
173 CtdlSetConfigLong("MMfulltext", 0);
175 end_critical_section(S_CONTROL);
177 // Silently return if our fulltext index is up to date with new messages.
178 if ((CtdlGetConfigLong("MMfulltext") >= CtdlGetConfigLong("MMhighest"))) {
179 return; // nothing to do!
182 highest_msg_already_indexed = CtdlGetConfigLong("MMfulltext");
183 highest_msg_to_be_indexed = CtdlGetConfigLong("MMhighest");
184 messages_to_be_indexed = array_new(sizeof(long));
186 // Now go through each room and find messages to index.
187 CtdlForEachRoom(ft_index_room, NULL); // load all msg pointers
188 array_sort(messages_to_be_indexed, longcmp); // sort them
190 // Here it is ... do each message!
192 long prev_msgnum = 0;
193 for (i=0; i<array_len(messages_to_be_indexed); ++i) {
194 memcpy(&msgnum, array_get_element_at(messages_to_be_indexed, i), sizeof(long));
196 if (msgnum != prev_msgnum) { // careful to avoid dupes
197 ft_index_message(msgnum, 1);
200 prev_msgnum = msgnum;
203 array_free(messages_to_be_indexed);
207 "fulltext: indexed %d of %d messages (%d%%)", i, ft_num_msgs, ((i*100) / ft_num_msgs));
208 last_progress = time(NULL);
210 // Check to see if we need to quit early
211 if (server_shutting_down) {
212 syslog(LOG_DEBUG, "fulltext: indexer quitting early");
213 ft_newhighest = ft_newmsgs[i];
217 begin_critical_section(S_CONTROL);
218 CtdlSetConfigLong("MMfulltext", ft_newhighest);
219 CtdlSetConfigInt("MM_fulltext_wordbreaker", FT_WORDBREAKER_ID);
220 end_critical_section(S_CONTROL);
223 syslog(LOG_DEBUG, "fulltext: indexing finished");
229 // API call to perform searches.
230 // (This one does the "all of these words" search.)
231 // Caller is responsible for freeing the message list.
232 void ft_search(int *fts_num_msgs, long **fts_msgs, const char *search_string) {
235 struct cdbdata cdb_bucket;
236 int num_all_msgs = 0;
237 long *all_msgs = NULL;
238 int num_ret_msgs = 0;
239 int num_ret_alloc = 0;
240 long *ret_msgs = NULL;
244 t = wordbreaker(search_string);
245 if (array_len(t) > 0) {
246 for (i=0; i<array_len(t); ++i) {
248 // search for tokens[i]
249 memcpy(&tok, array_get_element_at(t, i), sizeof(int));
251 // fetch the bucket, Liza
252 if (ftc_msgs[tok] == NULL) {
253 cdb_bucket = cdb_fetch(CDB_FULLTEXT, &tok, sizeof(int));
254 if (cdb_bucket.ptr != NULL) {
255 ftc_num_msgs[tok] = cdb_bucket.len / sizeof(long);
256 ftc_msgs[tok] = (long *) malloc(cdb_bucket.len);
257 memcpy(ftc_msgs[tok], cdb_bucket.ptr, cdb_bucket.len);
260 ftc_num_msgs[tok] = 0;
261 ftc_msgs[tok] = malloc(sizeof(long));
265 num_all_msgs += ftc_num_msgs[tok];
266 if (num_all_msgs > 0) {
267 all_msgs = realloc(all_msgs, num_all_msgs*sizeof(long) );
268 memcpy(&all_msgs[num_all_msgs-ftc_num_msgs[tok]], ftc_msgs[tok], ftc_num_msgs[tok]*sizeof(long) );
273 if (all_msgs != NULL) {
274 qsort(all_msgs, num_all_msgs, sizeof(long), longcmp);
276 // At this point, if a message appears array_len(t) times in the
277 // list, then it contains all of the search tokens.
278 if (num_all_msgs >= array_len(t))
279 for (j=0; j<(num_all_msgs-array_len(t)+1); ++j) {
280 if (all_msgs[j] == all_msgs[j+array_len(t)-1]) {
282 if (num_ret_msgs > num_ret_alloc) {
284 ret_msgs = realloc(ret_msgs, (num_ret_alloc*sizeof(long)) );
286 ret_msgs[num_ret_msgs - 1] = all_msgs[j];
294 *fts_num_msgs = num_ret_msgs;
295 *fts_msgs = ret_msgs;
300 // This search command is for diagnostic purposes and may be removed or replaced.
301 void cmd_srch(char *argbuf) {
305 char search_string[256];
307 if (CtdlAccessCheck(ac_logged_in)) return;
309 if (!CtdlGetConfigInt("c_enable_fulltext")) {
310 cprintf("%d Full text index is not enabled on this server.\n", ERROR + CMD_NOT_SUPPORTED);
314 extract_token(search_string, argbuf, 0, '|', sizeof search_string);
315 ft_search(&num_msgs, &msgs, search_string);
317 cprintf("%d %d msgs match all search words:\n", LISTING_FOLLOWS, num_msgs);
319 for (i=0; i<num_msgs; ++i) {
320 cprintf("%ld\n", msgs[i]);
323 if (msgs != NULL) free(msgs);
329 void ft_delete_remove(char *room, long msgnum) {
332 // Remove from fulltext index
333 if (CtdlGetConfigInt("c_enable_fulltext")) {
334 ft_index_message(msgnum, 0);
339 // Initialization function, called from modules_init.c
340 char *ctdl_module_init_fulltext(void) {
342 CtdlRegisterProtoHook(cmd_srch, "SRCH", "Full text search");
343 CtdlRegisterDeleteHook(ft_delete_remove);
344 CtdlRegisterSearchFuncHook(ft_search, "fulltext");
345 CtdlRegisterSessionHook(do_fulltext_indexing, EVT_TIMER, PRIO_CLEANUP + 300);
347 // return our module name for the log