Back to index

citadel  8.12
Functions
serv_fulltext.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

void ft_index_message (long msgnum, int op)
void ft_search (int *fts_num_msgs, long **fts_msgs, const char *search_string)
void * indexer_thread (void *arg)

Function Documentation

void ft_index_message ( long  msgnum,
int  op 
)

Definition at line 113 of file serv_fulltext.c.

                                           {
       int num_tokens = 0;
       int *tokens = NULL;
       int i, j;
       struct cdbdata *cdb_bucket;
       StrBuf *msgtext;
       char *txt;
       int tok;
       struct CtdlMessage *msg = NULL;

       msg = CtdlFetchMessage(msgnum, 1);
       if (msg == NULL) {
              syslog(LOG_ERR, "ft_index_message() could not load msg %ld", msgnum);
              return;
       }

       if (msg->cm_fields['1'] != NULL) {
              syslog(LOG_DEBUG, "ft_index_message() excluded msg %ld", msgnum);
              CtdlFreeMessage(msg);
              return;
       }

       syslog(LOG_DEBUG, "ft_index_message() %s msg %ld", (op ? "adding" : "removing") , msgnum);

       /* Output the message as text before indexing it, so we don't end up
        * indexing a bunch of encoded base64, etc.
        */
       CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
       CtdlOutputPreLoadedMsg(msg, MT_CITADEL, HEADERS_ALL, 0, 1, 0);
       CtdlFreeMessage(msg);
       msgtext = CC->redirect_buffer;
       CC->redirect_buffer = NULL;
       syslog(LOG_DEBUG, "Wordbreaking message %ld...", msgnum);
       if ((msgtext == NULL) || (StrLength(msgtext) == 0)) {
              syslog(LOG_ALERT, "This message has a zero length.  Probable data corruption.");
       }
       txt = SmashStrBuf(&msgtext);
       wordbreaker(txt, &num_tokens, &tokens);
       free(txt);

       syslog(LOG_DEBUG, "Indexing message %ld [%d tokens]", msgnum, num_tokens);
       if (num_tokens > 0) {
              for (i=0; i<num_tokens; ++i) {

                     /* Add the message to the relevant token bucket */

                     /* search for tokens[i] */
                     tok = tokens[i];

                     if ( (tok >= 0) && (tok <= 65535) ) {
                            /* fetch the bucket, Liza */
                            if (ftc_msgs[tok] == NULL) {
                                   cdb_bucket = cdb_fetch(CDB_FULLTEXT, &tok, sizeof(int));
                                   if (cdb_bucket != NULL) {
                                          ftc_num_msgs[tok] = cdb_bucket->len / sizeof(long);
                                          ftc_msgs[tok] = (long *)cdb_bucket->ptr;
                                          cdb_bucket->ptr = NULL;
                                          cdb_free(cdb_bucket);
                                   }
                                   else {
                                          ftc_num_msgs[tok] = 0;
                                          ftc_msgs[tok] = malloc(sizeof(long));
                                   }
                            }
       
       
                            if (op == 1) {       /* add to index */
                                   ++ftc_num_msgs[tok];
                                   ftc_msgs[tok] = realloc(ftc_msgs[tok],
                                                        ftc_num_msgs[tok]*sizeof(long));
                                   ftc_msgs[tok][ftc_num_msgs[tok] - 1] = msgnum;
                            }
       
                            if (op == 0) {       /* remove from index */
                                   if (ftc_num_msgs[tok] >= 1) {
                                          for (j=0; j<ftc_num_msgs[tok]; ++j) {
                                                 if (ftc_msgs[tok][j] == msgnum) {
                                                        memmove(&ftc_msgs[tok][j], &ftc_msgs[tok][j+1], ((ftc_num_msgs[tok] - j - 1)*sizeof(long)));
                                                        --ftc_num_msgs[tok];
                                                        --j;
                                                 }
                                          }
                                   }
                            }
                     }
                     else {
                            syslog(LOG_ALERT, "Invalid token %d !!", tok);
                     }
              }

              free(tokens);
       }
}

Here is the call graph for this function:

Here is the caller graph for this function:

void ft_search ( int *  fts_num_msgs,
long **  fts_msgs,
const char *  search_string 
)

Definition at line 375 of file serv_fulltext.c.

                                                                              {
       int num_tokens = 0;
       int *tokens = NULL;
       int i, j;
       struct cdbdata *cdb_bucket;
       int num_all_msgs = 0;
       long *all_msgs = NULL;
       int num_ret_msgs = 0;
       int num_ret_alloc = 0;
       long *ret_msgs = NULL;
       int tok;

       wordbreaker(search_string, &num_tokens, &tokens);
       if (num_tokens > 0) {
              for (i=0; i<num_tokens; ++i) {

                     /* search for tokens[i] */
                     tok = tokens[i];

                     /* fetch the bucket, Liza */
                     if (ftc_msgs[tok] == NULL) {
                            cdb_bucket = cdb_fetch(CDB_FULLTEXT, &tok, sizeof(int));
                            if (cdb_bucket != NULL) {
                                   ftc_num_msgs[tok] = cdb_bucket->len / sizeof(long);
                                   ftc_msgs[tok] = (long *)cdb_bucket->ptr;
                                   cdb_bucket->ptr = NULL;
                                   cdb_free(cdb_bucket);
                            }
                            else {
                                   ftc_num_msgs[tok] = 0;
                                   ftc_msgs[tok] = malloc(sizeof(long));
                            }
                     }

                     num_all_msgs += ftc_num_msgs[tok];
                     if (num_all_msgs > 0) {
                            all_msgs = realloc(all_msgs, num_all_msgs*sizeof(long) );
                            memcpy(&all_msgs[num_all_msgs-ftc_num_msgs[tok]],
                                   ftc_msgs[tok], ftc_num_msgs[tok]*sizeof(long) );
                     }

              }
              free(tokens);
              if (all_msgs != NULL) {
                     qsort(all_msgs, num_all_msgs, sizeof(long), longcmp);

                     /*
                      * At this point, if a message appears num_tokens times in the
                      * list, then it contains all of the search tokens.
                      */
                     if (num_all_msgs >= num_tokens)
                            for (j=0; j<(num_all_msgs-num_tokens+1); ++j) {
                                   if (all_msgs[j] == all_msgs[j+num_tokens-1]) {
                                          
                                          ++num_ret_msgs;
                                          if (num_ret_msgs > num_ret_alloc) {
                                                 num_ret_alloc += 64;
                                                 ret_msgs = realloc(ret_msgs,
                                                                  (num_ret_alloc*sizeof(long)) );
                                          }
                                          ret_msgs[num_ret_msgs - 1] = all_msgs[j];
                                          
                                   }
                            }
                     free(all_msgs);
              }
       }

       *fts_num_msgs = num_ret_msgs;
       *fts_msgs = ret_msgs;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void* indexer_thread ( void *  arg)