Main Page | Modules | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

flt_tid_index.c

Go to the documentation of this file.
00001 
00008 /* {{{ Initial comments */
00009 /*
00010  * $LastChangedDate$
00011  * $LastChangedRevision$
00012  * $LastChangedBy$
00013  *
00014  */
00015 /* }}} */
00016 
00017 /* {{{ Includes */
00018 #include "config.h"
00019 #include "defines.h"
00020 
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023 #include <string.h>
00024 #include <ctype.h>
00025 #include <time.h>
00026 
00027 #include <sys/stat.h>
00028 #include <sys/types.h>
00029 
00030 #include <pthread.h>
00031 
00032 #include "cf_pthread.h"
00033 
00034 #include "hashlib.h"
00035 #include "utils.h"
00036 #include "configparser.h"
00037 #include "readline.h"
00038 #include "fo_server.h"
00039 #include "serverlib.h"
00040 #include "fo_tid_index.h"
00041 /* }}} */
00042 
00043 int cmp(const void *t,const void *elem) {
00044   const struct tm *tm = (const struct tm *)t;
00045   const t_tid_index *idx = (const t_tid_index *)elem;
00046 
00047   if(tm->tm_year + 1900 > idx->year) return 1;
00048   if(tm->tm_year + 1900 < idx->year) return -1;
00049   if(tm->tm_mon + 1 > idx->month) return 1;
00050   if(tm->tm_mon + 1 < idx->month) return -1;
00051 
00052   return 0;
00053 }
00054 
00055 int flt_tidx_module(t_thread *thr) {
00056   t_name_value *v = cfg_get_value(&fo_default_conf,"ThreadIndexFile");
00057   t_array index;
00058   FILE *fd;
00059   struct stat st;
00060   t_tid_index *idx;
00061   struct tm t;
00062 
00063   array_init(&index,sizeof(t_tid_index),NULL);
00064 
00065   if(!v) return FLT_DECLINE;
00066   if(stat(v->values[0],&st) == -1) return FLT_DECLINE;
00067 
00068   index.array    = fo_alloc(NULL,1,st.st_size,FO_ALLOC_MALLOC);
00069   index.reserved = st.st_size;
00070   index.elements = st.st_size / sizeof(t_tid_index);
00071 
00072   if((fd = fopen(v->values[0],"r")) == NULL) {
00073     free(index.array);
00074     return FLT_DECLINE;
00075   }
00076   fread(index.array,sizeof(t_tid_index),st.st_size/sizeof(t_tid_index),fd);
00077   fclose(fd);
00078 
00079   if(localtime_r(&thr->postings->date,&t) == NULL) {
00080     free(index.array);
00081     return FLT_DECLINE;
00082   }
00083 
00084   if((idx = array_bsearch(&index,&t,cmp)) == NULL) {
00085     t_tid_index id;
00086 
00087     id.year  = t.tm_year + 1900;
00088     id.month = t.tm_mon  + 1;
00089 
00090     id.start = thr->tid;
00091     id.end   = thr->tid;
00092 
00093     array_push(&index,&id);
00094   }
00095   else {
00096     if(thr->tid < idx->start)    idx->start = thr->tid;
00097     else if(thr->tid > idx->end) idx->end   = thr->tid;
00098   }
00099 
00100   if((fd = fopen(v->values[0],"w")) != 0) {
00101     free(index.array);
00102     return FLT_DECLINE;
00103   }
00104   fwrite(index.array,sizeof(t_tid_index),index.elements,fd);
00105   fclose(fd);
00106 
00107   return FLT_OK;
00108 }
00109 
00110 
00111 t_conf_opt flt_tid_index_config[] = {
00112   { NULL, NULL, NULL }
00113 };
00114 
00115 t_handler_config flt_tid_index_handlers[] = {
00116   { ARCHIVE_HANDLER,            flt_tidx_module   },
00117   { 0, NULL }
00118 };
00119 
00120 t_module_config flt_tid_index = {
00121   flt_tid_index_config,
00122   flt_tid_index_handlers,
00123   NULL,
00124   NULL,
00125   NULL,
00126   NULL,
00127 };
00128 
00129 /* eof */
00130 

Generated on Sun Apr 25 16:37:39 2004 for Classic Forum by doxygen 1.3.5