]> code.citadel.org Git - citadel.git/blob - citadel/server/modules/fulltext/serv_fulltext.c
serv_fulltext: wordbreaker now returns a libcitadel Array
[citadel.git] / citadel / server / modules / fulltext / serv_fulltext.c
1 // This module handles fulltext indexing of the message base.
2 //
3 // Copyright (c) 2005-2023 by the citadel.org team
4 //
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.
7
8 #include "../../sysdep.h"
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <fcntl.h>
13 #include <signal.h>
14 #include <pwd.h>
15 #include <errno.h>
16 #include <sys/types.h>
17 #include <time.h>
18 #include <sys/wait.h>
19 #include <string.h>
20 #include <limits.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"
35
36 long ft_newhighest = 0L;
37 long *ft_newmsgs = NULL;
38 int ft_num_msgs = 0;
39 int ft_num_alloc = 0;
40
41 int ftc_num_msgs[65536];
42 long *ftc_msgs[65536];
43
44
45 // Compare function
46 int longcmp(const void *rec1, const void *rec2) {
47         long i1, i2;
48
49         i1 = *(const long *)rec1;
50         i2 = *(const long *)rec2;
51
52         if (i1 > i2) return(1);
53         if (i1 < i2) return(-1);
54         return(0);
55 }
56
57
58 // Flush our index cache out to disk.
59 void ft_flush_cache(void) {
60         int i;
61         time_t last_update = 0;
62
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);
68                 }
69                 if (ftc_msgs[i] != NULL) {
70                         cdb_store(CDB_FULLTEXT, &i, sizeof(int), ftc_msgs[i], (ftc_num_msgs[i] * sizeof(long)));
71                         ftc_num_msgs[i] = 0;
72                         free(ftc_msgs[i]);
73                         ftc_msgs[i] = NULL;
74                 }
75         }
76         cdb_end_transaction();
77         syslog(LOG_INFO, "fulltext: flushed index cache to disk (100%% complete)");
78 }
79
80
81 // Index or de-index a message.  (op == 1 to index, 0 to de-index)
82 void ft_index_message(long msgnum, int op) {
83         int i, j;
84         Array *t = NULL;
85         struct cdbdata cdb_bucket;
86         StrBuf *msgtext;
87         char *txt;
88         int tok;
89         struct CtdlMessage *msg = NULL;
90
91         msg = CtdlFetchMessage(msgnum, 1);
92         if (msg == NULL) {
93                 syslog(LOG_ERR, "fulltext: ft_index_message() could not load msg %ld", msgnum);
94                 return;
95         }
96
97         if (!CM_IsEmpty(msg, eSuppressIdx)) {
98                 syslog(LOG_DEBUG, "fulltext: ft_index_message() excluded msg %ld", msgnum);
99                 CM_Free(msg);
100                 return;
101         }
102
103         syslog(LOG_DEBUG, "fulltext: ft_index_message() %s msg %ld", (op ? "adding" : "removing") , msgnum);
104
105         // Output the message as text before indexing it, so we don't end up
106         // indexing a bunch of encoded base64, etc.
107         CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
108         CtdlOutputPreLoadedMsg(msg, MT_CITADEL, HEADERS_ALL, 0, 1, 0);
109         CM_Free(msg);
110         msgtext = CC->redirect_buffer;
111         CC->redirect_buffer = NULL;
112         if (msgtext != NULL) {
113                 syslog(LOG_DEBUG, "fulltext: wordbreaking message %ld (%d bytes)", msgnum, StrLength(msgtext));
114         }
115         txt = SmashStrBuf(&msgtext);
116         t = wordbreaker(txt);
117         free(txt);
118
119         syslog(LOG_DEBUG, "fulltext: %sindexing message %ld [%d tokens]", (op ? "" : "de"), msgnum, array_len(t));
120         if (array_len(t) > 0) {
121                 for (i=0; i<array_len(t); ++i) {
122
123                         // Add the message to the relevant token bucket
124
125                         // search for tokens[i]
126                         memcpy(&tok, array_get_element_at(t, i), sizeof(int));
127
128                         if ( (tok >= 0) && (tok <= 65535) ) {
129                                 // fetch the bucket, Liza
130                                 if (ftc_msgs[tok] == NULL) {
131                                         cdb_bucket = cdb_fetch(CDB_FULLTEXT, &tok, sizeof(int));
132                                         if (cdb_bucket.ptr != NULL) {
133                                                 ftc_num_msgs[tok] = cdb_bucket.len / sizeof(long);
134                                                 ftc_msgs[tok] = malloc(cdb_bucket.len);
135                                                 memcpy(ftc_msgs[tok], cdb_bucket.ptr, cdb_bucket.len);
136                                         }
137                                         else {
138                                                 ftc_num_msgs[tok] = 0;
139                                                 ftc_msgs[tok] = malloc(sizeof(long));
140                                         }
141                                 }
142         
143                                 if (op == 1) {  // add to index
144                                         ++ftc_num_msgs[tok];
145                                         ftc_msgs[tok] = realloc(ftc_msgs[tok], ftc_num_msgs[tok]*sizeof(long));
146                                         ftc_msgs[tok][ftc_num_msgs[tok] - 1] = msgnum;
147                                 }
148         
149                                 if (op == 0) {  // remove from index
150                                         if (ftc_num_msgs[tok] >= 1) {
151                                                 for (j=0; j<ftc_num_msgs[tok]; ++j) {
152                                                         if (ftc_msgs[tok][j] == msgnum) {
153                                                                 memmove(&ftc_msgs[tok][j], &ftc_msgs[tok][j+1], ((ftc_num_msgs[tok] - j - 1)*sizeof(long)));
154                                                                 --ftc_num_msgs[tok];
155                                                                 --j;
156                                                         }
157                                                 }
158                                         }
159                                 }
160                         }
161                         else {
162                                 syslog(LOG_ALERT, "fulltext: invalid token %d !!", tok);
163                         }
164                 }
165
166                 array_free(t);
167         }
168 }
169
170
171 // Add a message to the list of those to be indexed.
172 void ft_index_msg(long msgnum, void *userdata) {
173
174         if ((msgnum > CtdlGetConfigLong("MMfulltext")) && (msgnum <= ft_newhighest)) {
175                 ++ft_num_msgs;
176                 if (ft_num_msgs > ft_num_alloc) {
177                         ft_num_alloc += 1024;
178                         ft_newmsgs = realloc(ft_newmsgs, (ft_num_alloc * sizeof(long)));
179                 }
180                 ft_newmsgs[ft_num_msgs - 1] = msgnum;
181         }
182
183 }
184
185
186 // Scan a room for messages to index.
187 void ft_index_room(struct ctdlroom *qrbuf, void *data) {
188         if (server_shutting_down)
189                 return;
190                 
191         CtdlGetRoom(&CC->room, qrbuf->QRname);
192         CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL, ft_index_msg, NULL);
193 }
194
195
196 // Begin the fulltext indexing process.
197 void do_fulltext_indexing(void) {
198         int i;
199         static time_t last_progress = 0L;
200         static int is_running = 0;
201         if (is_running) return;         // Concurrency check - only one can run 
202         is_running = 1;
203
204         // Don't do this if the site doesn't have it enabled.
205         if (!CtdlGetConfigInt("c_enable_fulltext")) {
206                 return;
207         }
208
209         // If we've switched wordbreaker modules, burn the index and start over.
210         begin_critical_section(S_CONTROL);
211         if (CtdlGetConfigInt("MM_fulltext_wordbreaker") != FT_WORDBREAKER_ID) {
212                 syslog(LOG_DEBUG, "fulltext: wb ver on disk = %d, code ver = %d",
213                         CtdlGetConfigInt("MM_fulltext_wordbreaker"), FT_WORDBREAKER_ID
214                 );
215                 syslog(LOG_INFO, "fulltext: (re)initializing index");
216                 cdb_trunc(CDB_FULLTEXT);
217                 CtdlSetConfigLong("MMfulltext", 0);
218         }
219         end_critical_section(S_CONTROL);
220
221         // Silently return if our fulltext index is up to date with new messages.
222         if ((CtdlGetConfigLong("MMfulltext") >= CtdlGetConfigLong("MMhighest"))) {
223                 return;         // nothing to do!
224         }
225
226         // Now go through each room and find messages to index.
227         ft_newhighest = CtdlGetConfigLong("MMhighest");
228         CtdlForEachRoom(ft_index_room, NULL);   // load all msg pointers
229
230         if (ft_num_msgs > 0) {
231                 qsort(ft_newmsgs, ft_num_msgs, sizeof(long), longcmp);
232                 for (i=0; i<(ft_num_msgs-1); ++i) { // purge dups
233                         if (ft_newmsgs[i] == ft_newmsgs[i+1]) {
234                                 memmove(&ft_newmsgs[i], &ft_newmsgs[i+1], ((ft_num_msgs - i - 1)*sizeof(long)));
235                                 --ft_num_msgs;
236                                 --i;
237                         }
238                 }
239
240                 // Here it is ... do each message!
241                 for (i=0; i<ft_num_msgs; ++i) {
242                         if (time(NULL) != last_progress) {
243                                 syslog(LOG_DEBUG,
244                                         "fulltext: indexed %d of %d messages (%d%%)", i, ft_num_msgs, ((i*100) / ft_num_msgs));
245                                 last_progress = time(NULL);
246                         }
247                         ft_index_message(ft_newmsgs[i], 1);
248
249                         // Check to see if we need to quit early
250                         if (server_shutting_down) {
251                                 syslog(LOG_DEBUG, "fulltext: indexer quitting early");
252                                 ft_newhighest = ft_newmsgs[i];
253                                 break;
254                         }
255
256                         // Check to see if we have to maybe flush to disk
257                         if (i >= FT_MAX_CACHE) {
258                                 syslog(LOG_DEBUG, "fulltext: time to flush.");
259                                 ft_newhighest = ft_newmsgs[i];
260                                 break;
261                         }
262
263                 }
264
265                 free(ft_newmsgs);
266                 ft_num_msgs = 0;
267                 ft_num_alloc = 0;
268                 ft_newmsgs = NULL;
269         }
270
271         if (server_shutting_down) {
272                 is_running = 0;
273                 return;
274         }
275         
276         // Save our place so we don't have to do this again
277         ft_flush_cache();
278         begin_critical_section(S_CONTROL);
279         CtdlSetConfigLong("MMfulltext", ft_newhighest);
280         CtdlSetConfigInt("MM_fulltext_wordbreaker", FT_WORDBREAKER_ID);
281         end_critical_section(S_CONTROL);
282
283         syslog(LOG_DEBUG, "fulltext: indexing finished");
284         is_running = 0;
285         return;
286 }
287
288
289 // API call to perform searches.
290 // (This one does the "all of these words" search.)
291 // Caller is responsible for freeing the message list.
292 void ft_search(int *fts_num_msgs, long **fts_msgs, const char *search_string) {
293         Array *t = NULL;
294         int i, j;
295         struct cdbdata cdb_bucket;
296         int num_all_msgs = 0;
297         long *all_msgs = NULL;
298         int num_ret_msgs = 0;
299         int num_ret_alloc = 0;
300         long *ret_msgs = NULL;
301         int tok;
302
303         t = wordbreaker(search_string);
304         if (array_len(t) > 0) {
305                 for (i=0; i<array_len(t); ++i) {
306
307                         // search for tokens[i]
308                         memcpy(&tok, array_get_element_at(t, i), sizeof(int));
309
310                         // fetch the bucket, Liza
311                         if (ftc_msgs[tok] == NULL) {
312                                 cdb_bucket = cdb_fetch(CDB_FULLTEXT, &tok, sizeof(int));
313                                 if (cdb_bucket.ptr != NULL) {
314                                         ftc_num_msgs[tok] = cdb_bucket.len / sizeof(long);
315                                         ftc_msgs[tok] = (long *) malloc(cdb_bucket.len);
316                                         memcpy(ftc_msgs[tok], cdb_bucket.ptr, cdb_bucket.len);
317                                 }
318                                 else {
319                                         ftc_num_msgs[tok] = 0;
320                                         ftc_msgs[tok] = malloc(sizeof(long));
321                                 }
322                         }
323
324                         num_all_msgs += ftc_num_msgs[tok];
325                         if (num_all_msgs > 0) {
326                                 all_msgs = realloc(all_msgs, num_all_msgs*sizeof(long) );
327                                 memcpy(&all_msgs[num_all_msgs-ftc_num_msgs[tok]], ftc_msgs[tok], ftc_num_msgs[tok]*sizeof(long) );
328                         }
329
330                 }
331                 array_free(t);
332                 if (all_msgs != NULL) {
333                         qsort(all_msgs, num_all_msgs, sizeof(long), longcmp);
334
335                         // At this point, if a message appears array_len(t) times in the
336                         // list, then it contains all of the search tokens.
337                         if (num_all_msgs >= array_len(t))
338                                 for (j=0; j<(num_all_msgs-array_len(t)+1); ++j) {
339                                         if (all_msgs[j] == all_msgs[j+array_len(t)-1]) {
340                                                 ++num_ret_msgs;
341                                                 if (num_ret_msgs > num_ret_alloc) {
342                                                         num_ret_alloc += 64;
343                                                         ret_msgs = realloc(ret_msgs, (num_ret_alloc*sizeof(long)) );
344                                                 }
345                                                 ret_msgs[num_ret_msgs - 1] = all_msgs[j];
346                                                 
347                                         }
348                                 }
349                         free(all_msgs);
350                 }
351         }
352
353         *fts_num_msgs = num_ret_msgs;
354         *fts_msgs = ret_msgs;
355 }
356
357
358 // This search command is for diagnostic purposes and may be removed or replaced.
359 void cmd_srch(char *argbuf) {
360         int num_msgs = 0;
361         long *msgs = NULL;
362         int i;
363         char search_string[256];
364
365         if (CtdlAccessCheck(ac_logged_in)) return;
366
367         if (!CtdlGetConfigInt("c_enable_fulltext")) {
368                 cprintf("%d Full text index is not enabled on this server.\n", ERROR + CMD_NOT_SUPPORTED);
369                 return;
370         }
371
372         extract_token(search_string, argbuf, 0, '|', sizeof search_string);
373         ft_search(&num_msgs, &msgs, search_string);
374
375         cprintf("%d %d msgs match all search words:\n",
376                 LISTING_FOLLOWS, num_msgs);
377         if (num_msgs > 0) {
378                 for (i=0; i<num_msgs; ++i) {
379                         cprintf("%ld\n", msgs[i]);
380                 }
381         }
382         if (msgs != NULL) free(msgs);
383         cprintf("000\n");
384 }
385
386
387 // Zero out our index cache.
388 void initialize_ft_cache(void) {
389         memset(ftc_num_msgs, 0, (65536 * sizeof(int)));
390         memset(ftc_msgs, 0, (65536 * sizeof(long *)));
391 }
392
393
394 void ft_delete_remove(char *room, long msgnum) {
395         if (room) return;
396         
397         // Remove from fulltext index
398         if (CtdlGetConfigInt("c_enable_fulltext")) {
399                 ft_index_message(msgnum, 0);
400         }
401 }
402
403
404 // Initialization function, called from modules_init.c
405 char *ctdl_module_init_fulltext(void) {
406         if (!threading) {
407                 initialize_ft_cache();
408                 CtdlRegisterProtoHook(cmd_srch, "SRCH", "Full text search");
409                 CtdlRegisterDeleteHook(ft_delete_remove);
410                 CtdlRegisterSearchFuncHook(ft_search, "fulltext");
411                 CtdlRegisterSessionHook(do_fulltext_indexing, EVT_TIMER, PRIO_CLEANUP + 300);
412         }
413         // return our module name for the log
414         return "fulltext";
415 }