]> mj.ucw.cz Git - libucw.git/blob - lib/index.h
70da6b97caf6d677da9d35cbc7f7d98a5256782a
[libucw.git] / lib / index.h
1 /*
2  *      Sherlock: Data structures used in indices
3  *
4  *      (c) 2001--2002 Martin Mares <mj@ucw.cz>
5  */
6
7 #ifndef _SHERLOCK_INDEX_H
8 #define _SHERLOCK_INDEX_H
9
10 #include "lib/fastbuf.h"
11 #include SHERLOCK_CUSTOM
12 #include "charset/unistream.h"
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 /* Global index parameters */
34
35 struct index_params {
36   sh_time_t ref_time;                   /* Reference time (for document ages etc.) */
37 };
38
39 /* Index card attributes */
40
41 struct card_attr {
42   u32 card;                             /* Reference to card description (either oid or filepos) */
43 #ifdef CONFIG_SITES
44   u32 site_id;
45 #endif
46   CUSTOM_CARD_ATTRS                     /* Include all custom attributes */
47   byte weight;
48   byte flags;
49 #ifdef CONFIG_LASTMOD
50   byte age;                             /* Document age in pseudo-logarithmic units wrt. reference time */
51 #endif
52 #ifdef CONFIG_FILETYPE
53   byte type_flags;                      /* File type flags (see below) */
54 #endif
55 };
56
57 enum card_flag {
58   CARD_FLAG_EMPTY = 1,                  /* Empty document (redirect, robot file etc.) [scanner] */
59   CARD_FLAG_ACCENTED = 2,               /* Document contains accented characters [scanner] */
60   CARD_FLAG_DUP = 4,                    /* Removed as a duplicate [merger] */
61   CARD_FLAG_MERGED = 8,                 /* Destination of a merge [merger] */
62   CARD_FLAG_IMAGE = 16,                 /* Is an image object [scanner] */
63 };
64
65 #define CARD_POS_SHIFT 5                /* Card positions are shifted this # of bits to the right */
66
67 /*
68  *  We store document type and several other properties in card_attr->type_flags.
69  *  Here we define only the basic structure, the details and also how to match the
70  *  types are defined in custom.h.
71  *
72  *  bits 7--5   file type: (0-3: text types, 4-7: other types, defined by custom.h)
73  *  bits 4--0   type-dependent information, for text types it's document language code
74  */
75
76 #define CA_GET_FILE_TYPE(a) ((a)->type_flags >> 5)
77 #define CA_GET_FILE_INFO(a) ((a)->type_flags & 0x1f)
78 #define CA_GET_FILE_LANG(a) ((a)->type_flags & 0x80 ? 0 : CA_GET_FILE_INFO(a))
79
80 /* String fingerprints */
81
82 struct fingerprint {
83   byte hash[12];
84 };
85
86 void fingerprint(byte *string, struct fingerprint *fp);
87
88 static inline u32
89 fp_hash(struct fingerprint *fp)
90 {
91   return (fp->hash[0] << 24) | (fp->hash[1] << 16) | (fp->hash[2] << 8) | fp->hash[3];
92 }
93
94 /* Reading of tagged text (Unicode values, tags mapped to 0x80000000 and higher) */
95
96 #define GET_TAGGED_CHAR(p,u) do {                               \
97   u = *p;                                                       \
98   if (u >= 0xc0)                                                \
99     GET_UTF8_CHAR(p,u);                                         \
100   else if (u >= 0x80)                                           \
101     {                                                           \
102       p++;                                                      \
103       if (u >= 0xb0)                                            \
104         {                                                       \
105           ASSERT(u == 0xb0);                                    \
106           u += 0x80020000;                                      \
107         }                                                       \
108       else if (u >= 0xa0)                                       \
109         {                                                       \
110           ASSERT(*p >= 0x80 && *p <= 0xbf);                     \
111           u = 0x80010000 + ((u & 0x0f) << 6) + (*p++ & 0x3f);   \
112         }                                                       \
113       else                                                      \
114         u += 0x80000000;                                        \
115     }                                                           \
116   else                                                          \
117     p++;                                                        \
118 } while (0)
119
120 #define SKIP_TAGGED_CHAR(p) do {                                \
121   if (*p >= 0x80 && *p < 0xc0)                                  \
122     {                                                           \
123       uns u = *p++;                                             \
124       if (u >= 0xa0 && u < 0xb0 && *p >= 0x80 && *p < 0xc0)     \
125         p++;                                                    \
126     }                                                           \
127   else                                                          \
128     UTF8_SKIP(p);                                               \
129 } while (0)
130
131 static inline uns
132 bget_tagged_char(struct fastbuf *f)
133 {
134   uns u = bgetc(f);
135   if ((int)u < 0x80)
136     ;
137   else if (u < 0xc0)
138     {
139       if (u >= 0xb0)
140         {
141           ASSERT(u == 0xb0);
142           u += 0x80020000;
143         }
144       else if (u >= 0xa0)
145         {
146           uns v = bgetc(f);
147           ASSERT(v >= 0x80 && v <= 0xbf);
148           u = 0x80010000 + ((u & 0x0f) << 6) + (v & 0x3f);
149         }
150       else
151         u += 0x80000000;
152     }
153   else
154     {
155       bungetc(f);
156       u = bget_utf8(f);
157     }
158   return u;
159 }
160
161 /* Conversion of document age from seconds to our internal units */
162
163 static inline int
164 convert_age(sh_time_t lastmod, sh_time_t reftime)
165 {
166   sh_time_t age;
167   if (reftime < lastmod)                /* past times */
168     return -1;
169   age = (reftime - lastmod) / 3600;
170   if (age < 48)                         /* last 2 days: 1 hour resolution */
171     return age;
172   age = (age-48) / 24;
173   if (age < 64)                         /* next 64 days: 1 day resolution */
174     return 48 + age;
175   age = (age-64) / 7;
176   if (age < 135)                        /* next 135 weeks: 1 week resolution */
177     return 112 + age;
178   age = (age-135) / 52;
179   if (age < 8)                          /* next 8 years: 1 year resolution */
180     return 247 + age;
181   return 255;                           /* then just "infinite future" */
182 }
183
184 #endif