]> mj.ucw.cz Git - libucw.git/blob - lib/index.h
b17e9a62626440c342d1632086ffa94aa8bc7e1b
[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   byte age;                             /* Document age in pseudo-logarithmic units wrt. reference time */
50   // byte rfu[1];                       /* If no custom attributes are defined */
51 };
52
53 enum card_flag {
54   CARD_FLAG_EMPTY = 1,                  /* Empty document (redirect, robot file etc.) [scanner] */
55   CARD_FLAG_ACCENTED = 2,               /* Document contains accented characters [scanner] */
56   CARD_FLAG_DUP = 4,                    /* Removed as a duplicate [merger] */
57   CARD_FLAG_MERGED = 8,                 /* Destination of a merge [merger] */
58   CARD_FLAG_IMAGE = 16,                 /* Is an image object [scanner] */
59 };
60
61 #define CARD_POS_SHIFT 5                /* Card positions are shifted this # of bytes to the right */
62
63 /* String fingerprints */
64
65 struct fingerprint {
66   byte hash[12];
67 };
68
69 void fingerprint(byte *string, struct fingerprint *fp);
70
71 static inline u32
72 fp_hash(struct fingerprint *fp)
73 {
74   return (fp->hash[0] << 24) | (fp->hash[1] << 16) | (fp->hash[2] << 8) | fp->hash[3];
75 }
76
77 /* Reading of tagged text (Unicode values, tags mapped to 0x80000000 and higher) */
78
79 #define GET_TAGGED_CHAR(p,u) do {                               \
80   u = *p;                                                       \
81   if (u >= 0xc0)                                                \
82     GET_UTF8_CHAR(p,u);                                         \
83   else if (u >= 0x80)                                           \
84     {                                                           \
85       p++;                                                      \
86       if (u >= 0xb0)                                            \
87         {                                                       \
88           ASSERT(u == 0xb0);                                    \
89           u += 0x80020000;                                      \
90         }                                                       \
91       else if (u >= 0xa0)                                       \
92         {                                                       \
93           ASSERT(*p >= 0x80 && *p <= 0xbf);                     \
94           u = 0x80010000 + ((u & 0x0f) << 6) + (*p++ & 0x3f);   \
95         }                                                       \
96       else                                                      \
97         u += 0x80000000;                                        \
98     }                                                           \
99   else                                                          \
100     p++;                                                        \
101 } while (0)
102
103 #define SKIP_TAGGED_CHAR(p) do {                                \
104   if (*p >= 0x80 && *p < 0xc0)                                  \
105     {                                                           \
106       uns u = *p++;                                             \
107       if (u >= 0xa0 && u < 0xb0 && *p >= 0x80 && *p < 0xc0)     \
108         p++;                                                    \
109     }                                                           \
110   else                                                          \
111     UTF8_SKIP(p);                                               \
112 } while (0)
113
114 static inline uns
115 bget_tagged_char(struct fastbuf *f)
116 {
117   uns u = bgetc(f);
118   if ((int)u < 0x80)
119     ;
120   else if (u < 0xc0)
121     {
122       if (u >= 0xb0)
123         {
124           ASSERT(u == 0xb0);
125           u += 0x80020000;
126         }
127       else if (u >= 0xa0)
128         {
129           uns v = bgetc(f);
130           ASSERT(v >= 0x80 && v <= 0xbf);
131           u = 0x80010000 + ((u & 0x0f) << 6) + (v & 0x3f);
132         }
133       else
134         u += 0x80000000;
135     }
136   else
137     {
138       bungetc(f);
139       u = bget_utf8(f);
140     }
141   return u;
142 }
143
144 /* Conversion of document age from seconds to our internal units */
145
146 static inline int
147 convert_age(sh_time_t lastmod, sh_time_t reftime)
148 {
149   sh_time_t age;
150   if (reftime < lastmod)                /* past times */
151     return -1;
152   age = (reftime - lastmod) / 3600;
153   if (age < 48)                         /* last 2 days: 1 hour resolution */
154     return age;
155   age = (age-48) / 24;
156   if (age < 64)                         /* next 64 days: 1 day resolution */
157     return 48 + age;
158   age = (age-64) / 7;
159   if (age < 135)                        /* next 135 weeks: 1 week resolution */
160     return 112 + age;
161   age = (age-135) / 52;
162   if (age < 8)                          /* next 8 years: 1 year resolution */
163     return 247 + age;
164   return 255;                           /* then just "infinite future" */
165 }
166
167 #endif