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"
37 // These can be global variables because only one indexer runs at a time.
38 Array *messages_to_be_indexed = NULL;
39 long highest_msg_already_indexed = 0;
40 long highest_msg_to_be_indexed = 0;
44 int longcmp(const void *rec1, const void *rec2) {
47 i1 = *(const long *)rec1;
48 i2 = *(const long *)rec2;
50 if (i1 > i2) return(1);
51 if (i1 < i2) return(-1);
56 // Index or de-index a message. (op == 1 to index, 0 to de-index)
57 void ft_index_message(long msgnum, int op) {
59 Array *tokens_in_this_message = NULL;
60 struct cdbdata cdb_bucket;
64 struct CtdlMessage *msg = NULL;
66 if (msgnum == 0) return;
68 msg = CtdlFetchMessage(msgnum, 1);
70 // This is not necessarily an error condition; it could simply mean that the message was
71 // deleted before it could be indexed. This happens often when the load tester is running.
72 syslog(LOG_ERR, "fulltext: ft_index_message() could not load msg %ld", msgnum);
76 if (!CM_IsEmpty(msg, eSuppressIdx)) {
77 syslog(LOG_DEBUG, "fulltext: ft_index_message() excluded msg %ld", msgnum);
82 syslog(LOG_DEBUG, "fulltext: ft_index_message() %s msg %ld", (op ? "adding" : "removing") , msgnum);
84 // Output the message as text before indexing it, so we don't end up indexing a bunch of encoded base64, etc.
85 CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
86 CtdlOutputPreLoadedMsg(msg, MT_CITADEL, HEADERS_ALL, 0, 1, 0);
88 msgtext = CC->redirect_buffer;
89 CC->redirect_buffer = NULL;
90 if (msgtext != NULL) {
91 syslog(LOG_DEBUG, "fulltext: wordbreaking message %ld (%d bytes)", msgnum, StrLength(msgtext));
93 txt = SmashStrBuf(&msgtext);
94 tokens_in_this_message = wordbreaker(txt);
97 if (tokens_in_this_message == NULL) {
98 syslog(LOG_DEBUG, "fulltext: skipping empty index for message %ld", msgnum);
102 syslog(LOG_DEBUG, "fulltext: %sindexing message %ld [%d tokens]",
105 array_len(tokens_in_this_message)
108 if (array_len(tokens_in_this_message) > 0) {
109 begin_critical_section(S_INDEXER);
110 cdb_begin_transaction();
111 for (i=0; i<array_len(tokens_in_this_message); ++i) {
113 // Identify the bucket which we will be modifying
114 memcpy(&tok, array_get_element_at(tokens_in_this_message, i), sizeof(int));
117 cdb_bucket = cdb_fetch(CDB_FULLTEXT, &tok, sizeof(int));
118 long *newbucket = malloc(cdb_bucket.len + sizeof(long));
119 int nmsgs = cdb_bucket.len / sizeof(long);
121 if (op == 1) { // indexing, add this message to the bucket
122 memcpy(&newbucket[0], cdb_bucket.ptr, cdb_bucket.len);
123 int already_there = 0;
124 for (j=0; j<nmsgs; ++j) {
125 if (newbucket[j] == msgnum) {
129 if (already_there == 0) {
130 memcpy(&newbucket[nmsgs++], &msgnum, sizeof(long));
134 else if (op == 0) { // deindexing, remove this message from the bucket
135 memcpy(newbucket, cdb_bucket.ptr, cdb_bucket.len);
136 for (j=0; j<nmsgs; ++j) {
137 if ((newbucket[j] == msgnum) || (newbucket[j] == 0)) {
138 memcpy(&newbucket[j], &newbucket[j+1], ((nmsgs-j)*sizeof(long)));
145 // Then write it back to disk
146 cdb_store(CDB_FULLTEXT, &tok, sizeof(int), newbucket, (nmsgs*sizeof(long)));
149 if (server_shutting_down) break;
151 cdb_end_transaction();
152 end_critical_section(S_INDEXER);
153 CtdlSetConfigLong("MMfulltext", msgnum);
155 array_free(tokens_in_this_message);
159 // Scan a room for messages to index.
160 void ft_index_room(struct ctdlroom *qrbuf, void *data) {
161 if (server_shutting_down) {
169 // 2023aug30 ajc - old code did another CtdlGetRoom() here. Not only is that redundant,
170 // but for some reason it also made Berkeley DB deadlock after a while. I don't know why.
172 // qrbuf is already populated. fetch the list of messages in this room.
173 num_msgs = CtdlFetchMsgList(qrbuf->QRnumber, &msglist);
175 // Identify messages which have NOT yet been seen by the indexer.
176 if (msglist != NULL) {
177 for (i=0; i<num_msgs; ++i) {
180 && (msglist[i] > highest_msg_already_indexed)
181 && (msglist[i] <= highest_msg_to_be_indexed)
183 array_append(messages_to_be_indexed, &msglist[i]);
191 // Begin the fulltext indexing process.
192 void do_fulltext_indexing(void) {
194 static time_t last_progress = 0L;
195 static int is_running = 0;
197 if (is_running) return; // Concurrency check - only one can run
200 // Don't do this if the site doesn't have it enabled.
201 if (!CtdlGetConfigInt("c_enable_fulltext")) {
205 // If we've switched wordbreaker modules, burn the index and start over.
206 begin_critical_section(S_CONTROL);
207 if (CtdlGetConfigInt("MM_fulltext_wordbreaker") != FT_WORDBREAKER_ID) {
208 syslog(LOG_DEBUG, "fulltext: wb ver on disk = %d, code ver = %d",
209 CtdlGetConfigInt("MM_fulltext_wordbreaker"), FT_WORDBREAKER_ID
211 syslog(LOG_INFO, "fulltext: (re)initializing index");
212 cdb_trunc(CDB_FULLTEXT);
213 CtdlSetConfigLong("MMfulltext", 0);
215 end_critical_section(S_CONTROL);
217 // Silently return if our fulltext index is up to date with new messages.
218 if ((CtdlGetConfigLong("MMfulltext") >= CtdlGetConfigLong("MMhighest"))) {
220 return; // nothing to do!
223 highest_msg_already_indexed = CtdlGetConfigLong("MMfulltext");
224 highest_msg_to_be_indexed = CtdlGetConfigLong("MMhighest");
225 syslog(LOG_DEBUG, "fulltext: indexing started. msgs %ld--%ld", highest_msg_already_indexed, highest_msg_to_be_indexed);
227 messages_to_be_indexed = array_new(sizeof(long));
229 // Now go through each room and find messages to index.
230 CtdlForEachRoom(ft_index_room, NULL); // load all msg pointers
231 array_sort(messages_to_be_indexed, longcmp); // sort them
233 // Here it is ... do each message!
235 long prev_msgnum = 0;
236 time_t started_indexing_at = time(NULL);
238 for (i=0; ((i<array_len(messages_to_be_indexed)) && (yielded == 0)); ++i) {
239 memcpy(&msgnum, array_get_element_at(messages_to_be_indexed, i), sizeof(long));
241 if (msgnum != prev_msgnum) { // careful to avoid dupes
242 ft_index_message(msgnum, 1);
244 prev_msgnum = msgnum;
246 // If we run too long, yield the thread so the server can do other things. We'll be back.
247 if (time(NULL) - started_indexing_at >= MAXIMUM_INDEXER_RUN_TIME) {
252 array_free(messages_to_be_indexed);
253 CtdlSetConfigInt("MM_fulltext_wordbreaker", FT_WORDBREAKER_ID);
254 syslog(LOG_DEBUG, "fulltext: indexer has run for %ld seconds; yielded=%d", time(NULL) - started_indexing_at, yielded);
256 // This keeps the indexer from starting up over and over if the highest message in the list was deleted
257 // before we had a chance to index it.
259 CtdlSetConfigLong("MMfulltext", highest_msg_to_be_indexed);
267 // API call to perform searches.
268 // (This one does the "all of these words" search.)
269 // Caller is responsible for freeing the message list.
270 Array *CtdlFullTextSearch(const char *search_string) {
272 struct cdbdata cdb_bucket;
273 long msgnum, smsgnum;
276 Array *r = array_new(sizeof(long));
277 if (!r) return(NULL);
278 Array *t = wordbreaker(search_string);
280 if ((t != NULL) && (array_len(t) > 0)) {
281 for (i=0; i<array_len(t); ++i) {
282 memcpy(&tok, array_get_element_at(t, i), sizeof(int));
283 cdb_bucket = cdb_fetch(CDB_FULLTEXT, &tok, sizeof(int));
284 if (cdb_bucket.ptr != NULL) {
285 for (j=0; j<(cdb_bucket.len / sizeof(long)); ++j) {
286 memcpy(&msgnum, cdb_bucket.ptr + (j*sizeof(long)), sizeof(long));
287 array_append(r, &msgnum);
293 // We need to return any message containing ALL of the tokens.
294 // If a message number appears in the array `n` times, where `n` is the number of search words,
295 // that means it matched all search words.
296 array_sort(r, longcmp);
297 for (i=0; i<array_len(r); ++i) {
298 memcpy(&msgnum, array_get_element_at(r, i), sizeof(long));
300 for (j=i+1; j<array_len(r); ++j) {
301 memcpy(&smsgnum, array_get_element_at(r, j), sizeof(long));
302 if (msgnum == smsgnum) {
304 array_delete_element_at(r, j);
308 if (count != array_len(t)) {
309 array_delete_element_at(r, i);
319 // This search command is for diagnostic purposes and may be removed or replaced.
320 void cmd_srch(char *argbuf) {
322 char search_string[SIZ];
323 Array *matches = NULL;
326 if (CtdlAccessCheck(ac_logged_in)) return;
328 if (!CtdlGetConfigInt("c_enable_fulltext")) {
329 cprintf("%d Full text index is not enabled on this server.\n", ERROR + CMD_NOT_SUPPORTED);
333 extract_token(search_string, argbuf, 0, '|', sizeof search_string);
334 matches = CtdlFullTextSearch(search_string);
336 cprintf("%d %d msgs match all search words:\n", LISTING_FOLLOWS, array_len(matches));
337 if ((matches != NULL) && (array_len(matches) > 0)) {
338 for (i=0; i<array_len(matches); ++i) {
339 memcpy(&msgnum, array_get_element_at(matches, i), sizeof(long));
340 cprintf("%ld\n", msgnum);
343 if (matches != NULL) {
350 void ft_delete_remove(char *room, long msgnum) {
353 // Remove from fulltext index
354 if (CtdlGetConfigInt("c_enable_fulltext")) {
355 ft_index_message(msgnum, 0);
360 // Initialization function, called from modules_init.c
361 char *ctdl_module_init_fulltext(void) {
363 CtdlRegisterProtoHook(cmd_srch, "SRCH", "Full text search");
364 CtdlRegisterDeleteHook(ft_delete_remove);
365 CtdlRegisterSessionHook(do_fulltext_indexing, EVT_HOUSE, PRIO_CLEANUP + 300);
367 // return our module name for the log