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