Updated WIN32 code specific sections. Changed header.
[idzebra-moved-to-github.git] / index / kcompare.c
1 /*
2  * Copyright (C) 1994-1999, Index Data
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: kcompare.c,v $
7  * Revision 1.26  1999-02-02 14:50:54  adam
8  * Updated WIN32 code specific sections. Changed header.
9  *
10  * Revision 1.25  1998/06/08 15:26:06  adam
11  * Minor changes.
12  *
13  * Revision 1.24  1998/06/08 14:43:12  adam
14  * Added suport for EXPLAIN Proxy servers - added settings databasePath
15  * and explainDatabase to facilitate this. Increased maximum number
16  * of databases and attributes in one register.
17  *
18  * Revision 1.23  1998/03/05 08:45:12  adam
19  * New result set model and modular ranking system. Moved towards
20  * descent server API. System information stored as "SGML" records.
21  *
22  * Revision 1.22  1997/09/22 12:39:06  adam
23  * Added get_pos method for the ranked result sets.
24  *
25  * Revision 1.21  1997/09/17 12:19:13  adam
26  * Zebra version corresponds to YAZ version 1.4.
27  * Changed Zebra server so that it doesn't depend on global common_resource.
28  *
29  * Revision 1.20  1996/12/23 15:30:44  adam
30  * Work on truncation.
31  * Bug fix: result sets weren't deleted after server shut down.
32  *
33  * Revision 1.19  1996/12/11 12:08:00  adam
34  * Added better compression.
35  *
36  * Revision 1.18  1996/10/29 14:09:44  adam
37  * Use of cisam system - enabled if setting isamc is 1.
38  *
39  * Revision 1.17  1996/06/04 10:18:58  adam
40  * Minor changes - removed include of ctype.h.
41  *
42  * Revision 1.16  1996/05/13  14:23:05  adam
43  * Work on compaction of set/use bytes in dictionary.
44  *
45  * Revision 1.15  1995/11/20  16:59:46  adam
46  * New update method: the 'old' keys are saved for each records.
47  *
48  * Revision 1.14  1995/10/30  15:08:08  adam
49  * Bug fixes.
50  *
51  * Revision 1.13  1995/10/27  14:00:11  adam
52  * Implemented detection of database availability.
53  *
54  * Revision 1.12  1995/10/17  18:02:08  adam
55  * New feature: databases. Implemented as prefix to words in dictionary.
56  *
57  * Revision 1.11  1995/10/06  16:33:37  adam
58  * Use attribute mappings.
59  *
60  * Revision 1.10  1995/09/29  14:01:41  adam
61  * Bug fixes.
62  *
63  * Revision 1.9  1995/09/28  12:10:32  adam
64  * Bug fixes. Field prefix used in queries.
65  *
66  * Revision 1.8  1995/09/28  09:19:42  adam
67  * xfree/xmalloc used everywhere.
68  * Extract/retrieve method seems to work for text records.
69  *
70  * Revision 1.7  1995/09/27  12:22:28  adam
71  * More work on extract in record control.
72  * Field name is not in isam keys but in prefix in dictionary words.
73  *
74  * Revision 1.6  1995/09/14  07:48:23  adam
75  * Record control management.
76  *
77  * Revision 1.5  1995/09/11  13:09:34  adam
78  * More work on relevance feedback.
79  *
80  * Revision 1.4  1995/09/08  14:52:27  adam
81  * Minor changes. Dictionary is lower case now.
82  *
83  * Revision 1.3  1995/09/07  13:58:36  adam
84  * New parameter: result-set file descriptor (RSFD) to support multiple
85  * positions within the same result-set.
86  * Boolean operators: and, or, not implemented.
87  * Result-set references.
88  *
89  * Revision 1.2  1995/09/06  16:11:17  adam
90  * Option: only one word key per file.
91  *
92  * Revision 1.1  1995/09/04  09:10:36  adam
93  * More work on index add/del/update.
94  * Merge sort implemented.
95  * Initial work on z39 server.
96  *
97  */
98
99 #include <stdlib.h>
100 #include <string.h>
101 #include <stdio.h>
102 #include <assert.h>
103
104 #include "index.h"
105
106 void key_logdump (int logmask, const void *p)
107 {
108     struct it_key key;
109
110     memcpy (&key, p, sizeof(key));
111     logf (logmask, "%7d s=%-4d", key.sysno, key.seqno);
112 }
113
114 int key_compare_it (const void *p1, const void *p2)
115 {
116     if (((struct it_key *) p1)->sysno != ((struct it_key *) p2)->sysno)
117     {
118         if (((struct it_key *) p1)->sysno > ((struct it_key *) p2)->sysno)
119             return 2;
120         else
121             return -2;
122     }
123     if (((struct it_key *) p1)->seqno != ((struct it_key *) p2)->seqno)
124     {
125         if (((struct it_key *) p1)->seqno > ((struct it_key *) p2)->seqno)
126             return 1;
127         else
128             return -1;
129     }
130     return 0;
131 }
132
133 int key_compare (const void *p1, const void *p2)
134 {
135     struct it_key i1, i2;
136     memcpy (&i1, p1, sizeof(i1));
137     memcpy (&i2, p2, sizeof(i2));
138     if (i1.sysno != i2.sysno)
139     {
140         if (i1.sysno > i2.sysno)
141             return 2;
142         else
143             return -2;
144     }
145     if (i1.seqno != i2.seqno)
146     {
147         if (i1.seqno > i2.seqno)
148             return 1;
149         else
150             return -1;
151     }
152     return 0;
153 }
154
155 int key_qsort_compare (const void *p1, const void *p2)
156 {
157     int r;
158     size_t l;
159     char *cp1 = *(char **) p1;
160     char *cp2 = *(char **) p2;
161  
162     if ((r = strcmp (cp1, cp2)))
163         return r;
164     l = strlen(cp1)+1;
165     if ((r = key_compare (cp1+l+1, cp2+l+1)))
166         return r;
167     return cp1[l] - cp2[l];
168 }
169
170 int key_get_pos (const void *p)
171 {
172     struct it_key key;
173     memcpy (&key, p, sizeof(key));
174     return key.seqno;
175 }
176
177 struct iscz1_code_info {
178     struct it_key key;
179 };
180
181 static void *iscz1_code_start (int mode)
182 {
183     struct iscz1_code_info *p = xmalloc (sizeof(*p));
184     p->key.sysno = 0;
185     p->key.seqno = 0;
186     return p;
187 }
188
189 static void iscz1_code_stop (int mode, void *p)
190 {
191     xfree (p);
192 }
193
194 void iscz1_encode_int (unsigned d, char **dst)
195 {
196     unsigned char *bp = (unsigned char*) *dst;
197
198     if (d <= 63)
199         *bp++ = d;
200     else if (d <= 16383)
201     {
202         *bp++ = 64 + (d>>8);
203        *bp++ = d & 255;
204     }
205     else if (d <= 4194303)
206     {
207         *bp++ = 128 + (d>>16);
208         *bp++ = (d>>8) & 255;
209         *bp++ = d & 255;
210     }
211     else
212     {
213         *bp++ = 192 + (d>>24);
214         *bp++ = (d>>16) & 255;
215         *bp++ = (d>>8) & 255;
216         *bp++ = d & 255;
217     }
218     *dst = (char *) bp;
219 }
220
221 int iscz1_decode_int (unsigned char **src)
222 {
223     unsigned c = *(*src)++;
224     switch (c & 192)
225     {
226     case 0:
227         return c;
228     case 64:
229         return ((c & 63) << 8) + *(*src)++;
230     case 128:
231         c = ((c & 63) << 8) + *(*src)++;
232         c = (c << 8) + *(*src)++;
233         return c;
234     }
235     c = ((c & 63) << 8) + *(*src)++;
236     c = (c << 8) + *(*src)++;
237     c = (c << 8) + *(*src)++;
238     return c;
239 }
240
241 static void iscz1_code_item (int mode, void *vp, char **dst, char **src)
242 {
243     struct iscz1_code_info *p = vp;
244     struct it_key tkey;
245     int d;
246
247     if (mode == ISAMC_ENCODE)
248     {
249         memcpy (&tkey, *src, sizeof(struct it_key));
250         d = tkey.sysno - p->key.sysno;
251         if (d)
252         {
253             iscz1_encode_int (2*tkey.seqno + 1, dst);
254             iscz1_encode_int (d, dst);
255             p->key.sysno += d;
256             p->key.seqno = tkey.seqno;
257         }
258         else
259         {
260             iscz1_encode_int (2*(tkey.seqno - p->key.seqno), dst);
261             p->key.seqno = tkey.seqno;
262         }
263         (*src) += sizeof(struct it_key);
264     }
265     else
266     {
267         d = iscz1_decode_int ((unsigned char **) src);
268         if (d & 1)
269         {
270             p->key.seqno = d>>1;
271             p->key.sysno += iscz1_decode_int ((unsigned char **) src);
272         }
273         else
274             p->key.seqno += d>>1;
275         memcpy (*dst, &p->key, sizeof(struct it_key));
276         (*dst) += sizeof(struct it_key);
277     }
278 }
279
280 ISAMC_M key_isamc_m (Res res)
281 {
282     static ISAMC_M me = NULL;
283
284     if (me)
285         return me;
286
287     me = isc_getmethod ();
288
289     me->compare_item = key_compare;
290
291     me->code_start = iscz1_code_start;
292     me->code_item = iscz1_code_item;
293     me->code_stop = iscz1_code_stop;
294
295     me->debug = atoi(res_get_def (res, "isamcDebug", "0"));
296
297     return me;
298 }
299
300 int key_SU_code (int ch, char *out)
301 {
302     int i;
303     for (i = 0; ch; i++)
304     {
305         if (ch > 63)
306             out[i] = 128 + (ch & 63);
307         else
308             out[i] = 1 + ch;
309         ch = ch >> 6;
310     }
311     return i;
312 }