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