]> mj.ucw.cz Git - libucw.git/blob - lib/index.h
75b3b7dc2a73d940047589d30fe6c0c5cc569c4b
[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 "charset/unistream.h"
12
13 /* Words */
14
15 #define MAX_WORD_LEN            64
16 #define MAX_COMPLEX_LEN         10
17
18 /* Word and string types are defined in lib/custom.h */
19
20 /* Global index parameters */
21
22 struct index_params {
23   sh_time_t ref_time;                   /* Reference time (for document ages etc.) */
24 };
25
26 /* Index card attributes */
27
28 struct card_attr {
29   u32 card;                             /* Reference to card description (either oid or filepos) */
30   u32 site_id;
31 #define INT_ATTR(t,i,o,k,g,p) t i;
32   CUSTOM_ATTRS                          /* Include all custom attributes */
33 #undef INT_ATTR
34   byte weight;
35   byte flags;
36   byte age;                             /* Document age in pseudo-logarithmic units wrt. reference time */
37   // byte rfu[1];                       /* If no custom attributes are defined */
38 };
39
40 enum card_flag {
41   CARD_FLAG_EMPTY = 1,                  /* Empty document (redirect, robot file etc.) [scanner] */
42   CARD_FLAG_ACCENTED = 2,               /* Document contains accented characters [scanner] */
43   CARD_FLAG_DUP = 4,                    /* Removed as a duplicate [merger] */
44   CARD_FLAG_MERGED = 8,                 /* Destination of a merge [merger] */
45   CARD_FLAG_IMAGE = 16,                 /* Is an image object [scanner] */
46 };
47
48 #define CARD_POS_SHIFT 5                /* Card positions are shifted this # of bytes to the right */
49
50 /* String fingerprints */
51
52 struct fingerprint {
53   byte hash[12];
54 };
55
56 void fingerprint(byte *string, struct fingerprint *fp);
57
58 static inline u32
59 fp_hash(struct fingerprint *fp)
60 {
61   return (fp->hash[0] << 24) | (fp->hash[1] << 16) | (fp->hash[2] << 8) | fp->hash[3];
62 }
63
64 /* Reading of tagged text (Unicode values, tags mapped to 0x80000000 and higher) */
65
66 #define GET_TAGGED_CHAR(p,u) do {                               \
67   u = *p;                                                       \
68   if (u >= 0xc0)                                                \
69     GET_UTF8_CHAR(p,u);                                         \
70   else if (u >= 0x80)                                           \
71     {                                                           \
72       p++;                                                      \
73       if (u >= 0xb0)                                            \
74         {                                                       \
75           ASSERT(u == 0xb0);                                    \
76           u += 0x80020000;                                      \
77         }                                                       \
78       else if (u >= 0xa0)                                       \
79         {                                                       \
80           ASSERT(*p >= 0x80 && *p <= 0xbf);                     \
81           u = 0x80010000 + ((u & 0x0f) << 6) + (*p++ & 0x3f);   \
82         }                                                       \
83       else                                                      \
84         u += 0x80000000;                                        \
85     }                                                           \
86   else                                                          \
87     p++;                                                        \
88 } while (0)
89
90 static inline uns
91 bget_tagged_char(struct fastbuf *f)
92 {
93   uns u = bgetc(f);
94   if ((int)u < 0x80)
95     ;
96   else if (u < 0xc0)
97     {
98       if (u >= 0xb0)
99         {
100           ASSERT(u == 0xb0);
101           u += 0x80020000;
102         }
103       else if (u >= 0xa0)
104         {
105           uns v = bgetc(f);
106           ASSERT(v >= 0x80 && v <= 0xbf);
107           u = 0x80010000 + ((u & 0x0f) << 6) + (v & 0x3f);
108         }
109       else
110         u += 0x80000000;
111     }
112   else
113     {
114       bungetc(f);
115       u = bget_utf8(f);
116     }
117   return u;
118 }
119
120 /* Conversion of document age from seconds to our internal units */
121
122 static inline int
123 convert_age(sh_time_t lastmod, sh_time_t reftime)
124 {
125   sh_time_t age;
126   if (reftime < lastmod)                /* past times */
127     return -1;
128   age = (reftime - lastmod) / 3600;
129   if (age < 48)                         /* last 2 days: 1 hour resolution */
130     return age;
131   age = (age-48) / 24;
132   if (age < 64)                         /* next 64 days: 1 day resolution */
133     return 48 + age;
134   age = (age-64) / 7;
135   if (age < 135)                        /* next 135 weeks: 1 week resolution */
136     return 112 + age;
137   age = (age-135) / 52;
138   if (age < 8)                          /* next 8 years: 1 year resolution */
139     return 247 + age;
140   return 255;                           /* then just "infinite future" */
141 }
142
143 #endif