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