]> mj.ucw.cz Git - libucw.git/blob - lib/index.h
34eaf7ced82576c3b2897d57d6e8019c83d20063
[libucw.git] / lib / index.h
1 /*
2  *      Sherlock: Data structures used in indices
3  *
4  *      (c) 2001--2004 Martin Mares <mj@ucw.cz>
5  */
6
7 #ifndef _SHERLOCK_INDEX_H
8 #define _SHERLOCK_INDEX_H
9
10 #include "custom/lib/custom.h"
11
12 #define INDEX_VERSION (0x32330100+sizeof(struct card_attr))     /* Increase with each incompatible change in index format */
13
14 /*
15  *  Words
16  *
17  *  MAX_WORD_LEN is the maximum length (measured in UTF-8 characters, excluding
18  *  the terminating zero byte if there's any) of any word which may appear in the
19  *  indices or in the bucket file. Naturally, the same constant also bounds
20  *  the number of UCS-2 characters in a word.
21  *
22  *  Caveat: If you are upcasing/downcasing the word, the UTF-8 encoding can
23  *  expand, although at most twice, so you need to reserve 2*MAX_WORD_LEN bytes.
24  */
25
26 #define MAX_WORD_LEN            64      /* a multiple of 4 */
27
28 /* Word and string types are defined in custom/lib/custom.h */
29
30 /* Types used for storing contexts */
31
32 #ifdef CONFIG_CONTEXTS
33 #if CONFIG_MAX_CONTEXTS == 32768
34 typedef u16 context_t;
35 #define bget_context bgetw
36 #define bput_context bputw
37 #define GET_CONTEXT GET_U16
38 #define PUT_CONTEXT PUT_U16
39 #elif CONFIG_MAX_CONTEXTS == 256
40 typedef byte context_t;
41 #define bget_context bgetc
42 #define bput_context bputc
43 #define GET_CONTEXT GET_U8
44 #define PUT_CONTEXT PUT_U8
45 #else
46 #error CONFIG_MAX_CONTEXTS set to an invalid value.
47 #endif
48 #else
49 struct fastbuf;
50 typedef struct { } context_t;
51 static inline uns bget_context(struct fastbuf *b UNUSED) { return 0; }
52 static inline void bput_context(struct fastbuf *b UNUSED, uns context UNUSED) { }
53 #define GET_CONTEXT(p) 0
54 #define PUT_CONTEXT(p,x) do {} while(0)
55 #endif
56
57 /* Index card attributes */
58
59 struct card_attr {
60   u32 card;                             /* Reference to card description (either oid or filepos) */
61 #ifdef CONFIG_SITES
62   u32 site_id;
63 #endif
64   area_t area;
65   CUSTOM_CARD_ATTRS                     /* Include all custom attributes */
66   byte weight;
67   byte flags;
68 #ifdef CONFIG_LASTMOD
69   byte age;                             /* Document age in pseudo-logarithmic units wrt. reference time */
70 #endif
71 #ifdef CONFIG_FILETYPE
72   byte type_flags;                      /* File type flags (see below) */
73 #endif
74 };
75
76 enum card_flag {
77   CARD_FLAG_EMPTY = 1,                  /* Empty document (redirect, robot file etc.) [scanner] */
78   CARD_FLAG_ACCENTED = 2,               /* Document contains accented characters [scanner] */
79   CARD_FLAG_DUP = 4,                    /* Removed as a duplicate [merger] */
80   CARD_FLAG_MERGED = 8,                 /* Destination of a merge [merger] */
81   CARD_FLAG_IMAGE = 16,                 /* Is an image object [scanner] */
82   CARD_FLAG_FRAMESET = 32,              /* Contains a frameset to be ignored [scanner] */
83   CARD_FLAG_OVERRIDEN = 64,             /* Overriden by another index [sherlockd] */
84 };
85
86 #define CARD_POS_SHIFT 5                /* Card positions are shifted this # of bits to the right */
87
88 /*
89  *  We store document type and several other properties in card_attr->type_flags.
90  *  Here we define only the basic structure, the details are defined in custom.h
91  *  (the list of type names custom_file_type_names[] and also setting of the file
92  *  types in custom_create_attrs()).
93  *
94  *  bits 7--5   file type: (0-3: text types, 4-7: other types, defined by custom.h)
95  *  bits 4--0   type-dependent information, for text types it's document language code
96  */
97
98 #ifdef CONFIG_FILETYPE
99 #define CA_GET_FILE_TYPE(a) ((a)->type_flags >> 5)
100 #define CA_GET_FILE_INFO(a) ((a)->type_flags & 0x1f)
101 #define CA_GET_FILE_LANG(a) ((a)->type_flags & 0x80 ? 0 : CA_GET_FILE_INFO(a))
102 #define MAX_FILE_TYPES 8
103 #define FILETYPE_IS_TEXT(f) ((f) < 4)
104 byte *ext_ft_parse(u32 *dest, byte *value, uns intval);
105 extern byte *custom_file_type_names[MAX_FILE_TYPES];
106 #define FILETYPE_STAT_VARS uns matching_per_type[MAX_FILE_TYPES];
107 #define FILETYPE_SHOW_STATS(q,f) ext_ft_show(q,f)
108 #define FILETYPE_INIT_STATS(q) bzero(q->matching_per_type, sizeof(q->matching_per_type))
109 #ifdef CONFIG_COUNT_ALL_FILETYPES
110 #define FILETYPE_ATTRS LATE_SMALL_SET_ATTR(ftype, FILETYPE, CA_GET_FILE_TYPE, ext_ft_parse)
111 #define FILETYPE_EARLY_STATS(q,a) q->matching_per_type[CA_GET_FILE_TYPE(a)]++
112 #define FILETYPE_LATE_STATS(q,a)
113 #else
114 #define FILETYPE_ATTRS SMALL_SET_ATTR(ftype, FILETYPE, CA_GET_FILE_TYPE, ext_ft_parse)
115 #define FILETYPE_EARLY_STATS(q,a)
116 #define FILETYPE_LATE_STATS(q,a) q->matching_per_type[CA_GET_FILE_TYPE(a)]++
117 #endif
118 #else
119 #define FILETYPE_ATTRS
120 #define FILETYPE_STAT_VARS
121 #define FILETYPE_INIT_STATS(q)
122 #define FILETYPE_EARLY_STATS(q,a)
123 #define FILETYPE_LATE_STATS(q,a)
124 #define FILETYPE_SHOW_STATS(q,f)
125 #endif
126
127 #ifdef CONFIG_LANG
128 /* You can use language matching without CONFIG_FILETYPE, but you have to define CA_GET_FILE_LANG yourself. */
129 #define LANG_ATTRS SMALL_SET_ATTR(lang, LANG, CA_GET_FILE_LANG, ext_lang_parse)
130 byte *ext_lang_parse(u32 *dest, byte *value, uns intval);
131 #else
132 #define LANG_ATTRS
133 #endif
134
135 #ifdef CONFIG_AREAS
136 #define CA_GET_AREA(a) ((a)->area)
137 #define SPLIT_ATTRS INT_ATTR(area, AREA, CA_GET_AREA, ext_area_parse)
138 byte *ext_area_parse(u32 *dest, byte *value, uns intval);
139 #else
140 #define SPLIT_ATTRS
141 #endif
142
143 /*
144  * A list of all extended attributes: custom attributes and also some
145  * built-in attributes treated in the same way.
146  */
147
148 #define EXTENDED_ATTRS CUSTOM_ATTRS FILETYPE_ATTRS LANG_ATTRS SPLIT_ATTRS
149
150 /*
151  * A list of all statistics collectors, also composed of custom parts
152  * and built-in parts.
153  */
154
155 #ifndef CUSTOM_STAT_VARS
156 #define CUSTOM_STAT_VARS
157 #define CUSTOM_INIT_STATS(q)
158 #define CUSTOM_EARLY_STATS(q,a)
159 #define CUSTOM_LATE_STATS(q,a)
160 #define CUSTOM_SHOW_STATS(q,f)
161 #endif
162
163 #define EXTENDED_STAT_VARS CUSTOM_STAT_VARS FILETYPE_STAT_VARS
164 #define EXTENDED_INIT_STATS(q) CUSTOM_INIT_STATS(q) FILETYPE_INIT_STATS(q)
165 #define EXTENDED_EARLY_STATS(q,a) CUSTOM_EARLY_STATS(q,a) FILETYPE_EARLY_STATS(q,a)
166 #define EXTENDED_LATE_STATS(q,a) CUSTOM_LATE_STATS(q,a) FILETYPE_LATE_STATS(q,a)
167 #define EXTENDED_SHOW_STATS(q,f) CUSTOM_SHOW_STATS(q,f) FILETYPE_SHOW_STATS(q,f)
168
169 /* String fingerprints */
170
171 struct fingerprint {
172   byte hash[12];
173 };
174
175 void fingerprint(byte *string, struct fingerprint *fp);
176
177 static inline u32
178 fp_hash(struct fingerprint *fp)
179 {
180   return (fp->hash[0] << 24) | (fp->hash[1] << 16) | (fp->hash[2] << 8) | fp->hash[3];
181 }
182
183 /* The card fingerprints */
184
185 struct card_print {
186   struct fingerprint fp;
187   u32 cardid;
188 };
189
190 /* URL keys */
191
192 #define URL_KEY_BUF_SIZE (3*MAX_URL_SIZE)
193 byte *url_key(byte *url, byte *buf);
194 void url_fingerprint(byte *url, struct fingerprint *fp);
195 void url_key_init(void);
196
197 /* Conversion of document age from seconds to our internal units */
198
199 static inline int
200 convert_age(sh_time_t lastmod, sh_time_t reftime)
201 {
202   sh_time_t age;
203   if (reftime < lastmod)                /* past times */
204     return -1;
205   age = (reftime - lastmod) / 3600;
206   if (age < 48)                         /* last 2 days: 1 hour resolution */
207     return age;
208   age = (age-48) / 24;
209   if (age < 64)                         /* next 64 days: 1 day resolution */
210     return 48 + age;
211   age = (age-64) / 7;
212   if (age < 135)                        /* next 135 weeks: 1 week resolution */
213     return 112 + age;
214   age = (age-135) / 52;
215   if (age < 8)                          /* next 8 years: 1 year resolution */
216     return 247 + age;
217   return 255;                           /* then just "infinite future" */
218 }
219
220 #endif