1 /* $Id: kcompare.c,v 1.48 2004-08-04 09:00:00 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
31 #define INT_CODEC_NEW 1
33 #define INT_CODEC_NEW 0
36 #define CODEC_INLINE inline
37 void key_logdump_txt (int logmask, const void *p, const char *txt)
46 memcpy (&key, p, sizeof(key));
47 assert(key.len > 0 && key.len <= IT_KEY_LEVEL_MAX);
49 for (i = 0; i<key.len; i++)
51 sprintf(formstr + strlen(formstr), ZINT_FORMAT, key.mem[i]);
55 logf (logmask, "%s %s", formstr, txt);
58 memcpy (&key, p, sizeof(key));
59 logf (logmask, "%7d:%-4d %s", key.sysno, key.seqno, txt);
63 logf(logmask, " (null) %s",txt);
66 void key_logdump (int logmask, const void *p)
68 key_logdump_txt(logmask, p, "");
71 int key_compare_it (const void *p1, const void *p2)
74 int i, l = ((struct it_key *) p1)->len;
75 if (((struct it_key *) p2)->len > l)
76 l = ((struct it_key *) p2)->len;
77 assert (l <= 4 && l > 0);
78 for (i = 0; i < l; i++)
80 if (((struct it_key *) p1)->mem[i] != ((struct it_key *) p2)->mem[i])
82 if (((struct it_key *) p1)->mem[i] > ((struct it_key *) p2)->mem[i])
89 if (((struct it_key *) p1)->sysno != ((struct it_key *) p2)->sysno)
91 if (((struct it_key *) p1)->sysno > ((struct it_key *) p2)->sysno)
96 if (((struct it_key *) p1)->seqno != ((struct it_key *) p2)->seqno)
98 if (((struct it_key *) p1)->seqno > ((struct it_key *) p2)->seqno)
107 char *key_print_it (const void *p, char *buf)
112 const struct it_key *i = p;
113 sprintf (buf, "%d:%d", i->sysno, i->seqno);
118 int key_compare (const void *p1, const void *p2)
120 struct it_key i1, i2;
124 memcpy (&i1, p1, sizeof(i1));
125 memcpy (&i2, p2, sizeof(i2));
130 assert (l <= 4 && l > 0);
131 for (i = 0; i < l; i++)
133 if (i1.mem[i] != i2.mem[i])
135 if (i1.mem[i] > i2.mem[i])
142 if (i1.sysno != i2.sysno)
144 if (i1.sysno > i2.sysno)
149 if (i1.seqno != i2.seqno)
151 if (i1.seqno > i2.seqno)
160 int key_get_seq(const void *p)
163 memcpy (&k, p, sizeof(k));
165 return k.mem[k.len-1];
171 int key_qsort_compare (const void *p1, const void *p2)
175 char *cp1 = *(char **) p1;
176 char *cp2 = *(char **) p2;
178 if ((r = strcmp (cp1, cp2)))
181 if ((r = key_compare (cp1+l+1, cp2+l+1)))
183 return cp1[l] - cp2[l];
186 struct iscz1_code_info {
190 void *iscz1_start (void)
192 struct iscz1_code_info *p = (struct iscz1_code_info *)
193 xmalloc (sizeof(*p));
199 void key_init(struct it_key *key)
203 for (i = 0; i<IT_KEY_LEVEL_MAX; i++)
208 void iscz1_reset (void *vp)
210 struct iscz1_code_info *p = (struct iscz1_code_info *) vp;
214 for (i = 0; i< IT_KEY_LEVEL_MAX; i++)
222 void iscz1_stop (void *p)
228 /* small encoder that works with unsigneds of any length */
229 static CODEC_INLINE void iscz1_encode_int (zint d, char **dst)
231 unsigned char *bp = (unsigned char*) *dst;
235 *bp++ = 128 | (d & 127);
242 /* small decoder that works with unsigneds of any length */
243 static CODEC_INLINE zint iscz1_decode_int (unsigned char **src)
249 while (((c = *(*src)++) & 128))
251 d += ((zint) (c&127) << r);
254 d += ((zint) c << r);
258 /* ! INT_CODEC_NEW */
260 /* old encoder that works with unsigneds up to 30 bits */
261 static CODEC_INLINE void iscz1_encode_int (unsigned d, char **dst)
263 unsigned char *bp = (unsigned char*) *dst;
272 else if (d <= 4194303)
274 *bp++ = 128 | (d>>16);
275 *bp++ = (d>>8) & 255;
280 *bp++ = 192 | (d>>24);
281 *bp++ = (d>>16) & 255;
282 *bp++ = (d>>8) & 255;
288 /* old decoder that works with unsigneds up to 30 bits */
289 static CODEC_INLINE int iscz1_decode_int (unsigned char **src)
291 unsigned c = *(*src)++;
297 return ((c & 63) << 8) + *(*src)++;
299 c = ((c & 63) << 8) + *(*src)++;
300 c = (c << 8) + *(*src)++;
303 if (c&32) /* expand sign bit to high bits */
304 c = ((c | 63) << 8) + *(*src)++;
306 c = ((c & 63) << 8) + *(*src)++;
307 c = (c << 8) + *(*src)++;
308 c = (c << 8) + *(*src)++;
314 void iscz1_encode (void *vp, char **dst, const char **src)
316 struct iscz1_code_info *p = (struct iscz1_code_info *) vp;
330 if diff is 0, then there is more ...
331 if diff is non-zero, then _may_ be more
333 memcpy (&tkey, *src, sizeof(struct it_key));
335 /* deal with leader + delta encoding .. */
337 assert(tkey.len > 0 && tkey.len <= 4);
338 for (i = 0; i < tkey.len; i++)
340 d = tkey.mem[i] - p->key.mem[i];
341 if (d || i == tkey.len-1)
342 { /* all have been equal until now, now make delta .. */
343 p->key.mem[i] = tkey.mem[i];
346 iscz1_encode_int (i + (tkey.len << 3) + 64, dst);
348 iscz1_encode_int (d, dst);
352 iscz1_encode_int (i + (tkey.len << 3), dst);
357 /* rest uses absolute encoding ... */
358 for (; i < tkey.len; i++)
360 iscz1_encode_int (tkey.mem[i], dst);
361 p->key.mem[i] = tkey.mem[i];
363 (*src) += sizeof(struct it_key);
365 d = tkey.sysno - p->key.sysno;
368 iscz1_encode_int (2*tkey.seqno + 1, dst);
369 iscz1_encode_int (d, dst);
371 p->key.seqno = tkey.seqno;
375 iscz1_encode_int (2*(tkey.seqno - p->key.seqno), dst);
376 p->key.seqno = tkey.seqno;
378 (*src) += sizeof(struct it_key);
382 void iscz1_decode (void *vp, char **dst, const char **src)
384 struct iscz1_code_info *p = (struct iscz1_code_info *) vp;
392 int leader = iscz1_decode_int ((unsigned char **) src);
395 p->key.mem[i] += iscz1_decode_int ((unsigned char **) src);
397 p->key.mem[i] = iscz1_decode_int ((unsigned char **) src);
398 p->key.len = (leader >> 3) & 7;
399 while (++i < p->key.len)
400 p->key.mem[i] = iscz1_decode_int ((unsigned char **) src);
401 memcpy (*dst, &p->key, sizeof(struct it_key));
402 (*dst) += sizeof(struct it_key);
404 d = iscz1_decode_int ((unsigned char **) src);
408 p->key.sysno += iscz1_decode_int ((unsigned char **) src);
411 p->key.seqno += d>>1;
412 memcpy (*dst, &p->key, sizeof(struct it_key));
413 (*dst) += sizeof(struct it_key);
417 ISAMS_M *key_isams_m (Res res, ISAMS_M *me)
419 isams_getmethod (me);
421 me->compare_item = key_compare;
422 me->log_item = key_logdump_txt;
424 me->codec.start = iscz1_start;
425 me->codec.decode = iscz1_decode;
426 me->codec.encode = iscz1_encode;
427 me->codec.stop = iscz1_stop;
428 me->codec.reset = iscz1_reset;
430 me->debug = atoi(res_get_def (res, "isamsDebug", "0"));
435 ISAMC_M *key_isamc_m (Res res, ISAMC_M *me)
439 me->compare_item = key_compare;
440 me->log_item = key_logdump_txt;
442 me->codec.start = iscz1_start;
443 me->codec.decode = iscz1_decode;
444 me->codec.encode = iscz1_encode;
445 me->codec.stop = iscz1_stop;
446 me->codec.reset = iscz1_reset;
448 me->debug = atoi(res_get_def (res, "isamcDebug", "0"));
453 int key_SU_encode (int ch, char *out)
459 out[i] = 65 + (ch & 63);
478 int key_SU_decode (int *ch, const unsigned char *out)
483 for (len = 1; *out >= 65; len++, out++)
485 *ch += (*out - 65) * fact;
488 *ch += (*out - 1) * fact;