]> mj.ucw.cz Git - libucw.git/blob - lib/index.h
18066ea884061187cf7e98c530abb9d9b2f8fbe2
[libucw.git] / lib / index.h
1 /*
2  *      Sherlock: Data structures used in indices
3  *
4  *      (c) 2001--2003 Martin Mares <mj@ucw.cz>
5  */
6
7 #ifndef _SHERLOCK_INDEX_H
8 #define _SHERLOCK_INDEX_H
9
10 #include SHERLOCK_CUSTOM
11
12 #define INDEX_VERSION (0x32240100+sizeof(struct card_attr))     /* Increase with each incompatible change in index format */
13
14 /*
15  *  Words and word complexes
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  *  MAX_COMPLEX_LEN is the upper bound on number of words in any word complex.
26  */
27
28 #define MAX_WORD_LEN            64      /* a multiple of 4 */
29 #define MAX_COMPLEX_LEN         10
30
31 /* Word and string types are defined in lib/custom.h */
32
33 /* Index card attributes */
34
35 struct card_attr {
36   u32 card;                             /* Reference to card description (either oid or filepos) */
37 #ifdef CONFIG_SITES
38   u32 site_id;
39 #endif
40   CUSTOM_CARD_ATTRS                     /* Include all custom attributes */
41   byte weight;
42   byte flags;
43 #ifdef CONFIG_LASTMOD
44   byte age;                             /* Document age in pseudo-logarithmic units wrt. reference time */
45 #endif
46 #ifdef CONFIG_FILETYPE
47   byte type_flags;                      /* File type flags (see below) */
48 #endif
49 };
50
51 enum card_flag {
52   CARD_FLAG_EMPTY = 1,                  /* Empty document (redirect, robot file etc.) [scanner] */
53   CARD_FLAG_ACCENTED = 2,               /* Document contains accented characters [scanner] */
54   CARD_FLAG_DUP = 4,                    /* Removed as a duplicate [merger] */
55   CARD_FLAG_MERGED = 8,                 /* Destination of a merge [merger] */
56   CARD_FLAG_IMAGE = 16,                 /* Is an image object [scanner] */
57   CARD_FLAG_FRAMESET = 32,              /* Contains a frameset to be ignored [scanner] */
58   CARD_FLAG_GIANT_CLASS = 64,           /* Belongs to a very large class, subject to penalties [merger] */
59 };
60
61 #define CARD_POS_SHIFT 5                /* Card positions are shifted this # of bits to the right */
62
63 /*
64  *  We store document type and several other properties in card_attr->type_flags.
65  *  Here we define only the basic structure, the details are defined in custom.h
66  *  (the list of type names custom_file_type_names[] and also setting of the file
67  *  types in custom_create_attrs()).
68  *
69  *  bits 7--5   file type: (0-3: text types, 4-7: other types, defined by custom.h)
70  *  bits 4--0   type-dependent information, for text types it's document language code
71  */
72
73 #ifdef CONFIG_FILETYPE
74 #define CA_GET_FILE_TYPE(a) ((a)->type_flags >> 5)
75 #define CA_GET_FILE_INFO(a) ((a)->type_flags & 0x1f)
76 #define CA_GET_FILE_LANG(a) ((a)->type_flags & 0x80 ? 0 : CA_GET_FILE_INFO(a))
77 #define FILETYPE_ATTRS SMALL_SET_ATTR(ftype, FILETYPE, CA_GET_FILE_TYPE, ext_ft_parse)
78 #define MAX_FILE_TYPES 8
79 byte *ext_ft_parse(u32 *dest, byte *value, uns intval);
80 extern byte *custom_file_type_names[MAX_FILE_TYPES];
81 #else
82 #define FILETYPE_ATTRS
83 #endif
84
85 #ifdef CONFIG_LANG
86 /* You can use language matching without CONFIG_FILETYPE, but you have to define CA_GET_FILE_LANG yourself. */
87 #define LANG_ATTRS SMALL_SET_ATTR(lang, LANG, CA_GET_FILE_LANG, ext_lang_parse)
88 byte *ext_lang_parse(u32 *dest, byte *value, uns intval);
89 #else
90 #define LANG_ATTRS
91 #endif
92
93 #define EXTENDED_ATTRS CUSTOM_ATTRS LANG_ATTRS          /* Beware, FILETYPE_ATTRS are handled separately */
94
95 /* String fingerprints */
96
97 struct fingerprint {
98   byte hash[12];
99 };
100
101 void fingerprint(byte *string, struct fingerprint *fp);
102
103 static inline u32
104 fp_hash(struct fingerprint *fp)
105 {
106   return fp->hash[0] ^ fp->hash[1] ^ fp->hash[2] ^ fp->hash[3];
107 }
108
109 /* URL keys */
110
111 #define URL_KEY_BUF_SIZE (3*MAX_URL_SIZE)
112 byte *url_key(byte *url, byte *buf);
113 void url_fingerprint(byte *url, struct fingerprint *fp);
114 void url_key_init(void);
115
116 /* Conversion of document age from seconds to our internal units */
117
118 static inline int
119 convert_age(sh_time_t lastmod, sh_time_t reftime)
120 {
121   sh_time_t age;
122   if (reftime < lastmod)                /* past times */
123     return -1;
124   age = (reftime - lastmod) / 3600;
125   if (age < 48)                         /* last 2 days: 1 hour resolution */
126     return age;
127   age = (age-48) / 24;
128   if (age < 64)                         /* next 64 days: 1 day resolution */
129     return 48 + age;
130   age = (age-64) / 7;
131   if (age < 135)                        /* next 135 weeks: 1 week resolution */
132     return 112 + age;
133   age = (age-135) / 52;
134   if (age < 8)                          /* next 8 years: 1 year resolution */
135     return 247 + age;
136   return 255;                           /* then just "infinite future" */
137 }
138
139 #endif