1 /* $Id: kcompare.c,v 1.47 2004-08-04 08:35:23 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;
121 memcpy (&i1, p1, sizeof(i1));
122 memcpy (&i2, p2, sizeof(i2));
127 assert (l <= 4 && l > 0);
128 for (i = 0; i < l; i++)
130 if (i1.mem[i] != i2.mem[i])
132 if (i1.mem[i] > i2.mem[i])
139 if (i1.sysno != i2.sysno)
141 if (i1.sysno > i2.sysno)
146 if (i1.seqno != i2.seqno)
148 if (i1.seqno > i2.seqno)
157 int key_get_seq(const void *p)
160 memcpy (&k, p, sizeof(k));
162 return k.mem[k.len-1];
168 int key_qsort_compare (const void *p1, const void *p2)
172 char *cp1 = *(char **) p1;
173 char *cp2 = *(char **) p2;
175 if ((r = strcmp (cp1, cp2)))
178 if ((r = key_compare (cp1+l+1, cp2+l+1)))
180 return cp1[l] - cp2[l];
183 struct iscz1_code_info {
187 void *iscz1_start (void)
189 struct iscz1_code_info *p = (struct iscz1_code_info *)
190 xmalloc (sizeof(*p));
196 void key_init(struct it_key *key)
200 for (i = 0; i<IT_KEY_LEVEL_MAX; i++)
205 void iscz1_reset (void *vp)
207 struct iscz1_code_info *p = (struct iscz1_code_info *) vp;
211 for (i = 0; i< IT_KEY_LEVEL_MAX; i++)
219 void iscz1_stop (void *p)
225 /* small encoder that works with unsigneds of any length */
226 static CODEC_INLINE void iscz1_encode_int (zint d, char **dst)
228 unsigned char *bp = (unsigned char*) *dst;
232 *bp++ = 128 | (d & 127);
239 /* small decoder that works with unsigneds of any length */
240 static CODEC_INLINE zint iscz1_decode_int (unsigned char **src)
246 while (((c = *(*src)++) & 128))
248 d += ((zint) (c&127) << r);
251 d += ((zint) c << r);
255 /* ! INT_CODEC_NEW */
257 /* old encoder that works with unsigneds up to 30 bits */
258 static CODEC_INLINE void iscz1_encode_int (unsigned d, char **dst)
260 unsigned char *bp = (unsigned char*) *dst;
269 else if (d <= 4194303)
271 *bp++ = 128 | (d>>16);
272 *bp++ = (d>>8) & 255;
277 *bp++ = 192 | (d>>24);
278 *bp++ = (d>>16) & 255;
279 *bp++ = (d>>8) & 255;
285 /* old decoder that works with unsigneds up to 30 bits */
286 static CODEC_INLINE int iscz1_decode_int (unsigned char **src)
288 unsigned c = *(*src)++;
294 return ((c & 63) << 8) + *(*src)++;
296 c = ((c & 63) << 8) + *(*src)++;
297 c = (c << 8) + *(*src)++;
300 if (c&32) /* expand sign bit to high bits */
301 c = ((c | 63) << 8) + *(*src)++;
303 c = ((c & 63) << 8) + *(*src)++;
304 c = (c << 8) + *(*src)++;
305 c = (c << 8) + *(*src)++;
311 void iscz1_encode (void *vp, char **dst, const char **src)
313 struct iscz1_code_info *p = (struct iscz1_code_info *) vp;
327 if diff is 0, then there is more ...
328 if diff is non-zero, then _may_ be more
330 memcpy (&tkey, *src, sizeof(struct it_key));
332 /* deal with leader + delta encoding .. */
334 assert(tkey.len > 0 && tkey.len <= 4);
335 for (i = 0; i < tkey.len; i++)
337 d = tkey.mem[i] - p->key.mem[i];
338 if (d || i == tkey.len-1)
339 { /* all have been equal until now, now make delta .. */
340 p->key.mem[i] = tkey.mem[i];
343 iscz1_encode_int (i + (tkey.len << 3) + 64, dst);
345 iscz1_encode_int (d, dst);
349 iscz1_encode_int (i + (tkey.len << 3), dst);
354 /* rest uses absolute encoding ... */
355 for (; i < tkey.len; i++)
357 iscz1_encode_int (tkey.mem[i], dst);
358 p->key.mem[i] = tkey.mem[i];
360 (*src) += sizeof(struct it_key);
362 d = tkey.sysno - p->key.sysno;
365 iscz1_encode_int (2*tkey.seqno + 1, dst);
366 iscz1_encode_int (d, dst);
368 p->key.seqno = tkey.seqno;
372 iscz1_encode_int (2*(tkey.seqno - p->key.seqno), dst);
373 p->key.seqno = tkey.seqno;
375 (*src) += sizeof(struct it_key);
379 void iscz1_decode (void *vp, char **dst, const char **src)
381 struct iscz1_code_info *p = (struct iscz1_code_info *) vp;
389 int leader = iscz1_decode_int ((unsigned char **) src);
392 p->key.mem[i] += iscz1_decode_int ((unsigned char **) src);
394 p->key.mem[i] = iscz1_decode_int ((unsigned char **) src);
395 p->key.len = (leader >> 3) & 7;
396 while (++i < p->key.len)
397 p->key.mem[i] = iscz1_decode_int ((unsigned char **) src);
398 memcpy (*dst, &p->key, sizeof(struct it_key));
399 (*dst) += sizeof(struct it_key);
401 d = iscz1_decode_int ((unsigned char **) src);
405 p->key.sysno += iscz1_decode_int ((unsigned char **) src);
408 p->key.seqno += d>>1;
409 memcpy (*dst, &p->key, sizeof(struct it_key));
410 (*dst) += sizeof(struct it_key);
414 ISAMS_M *key_isams_m (Res res, ISAMS_M *me)
416 isams_getmethod (me);
418 me->compare_item = key_compare;
419 me->log_item = key_logdump_txt;
421 me->codec.start = iscz1_start;
422 me->codec.decode = iscz1_decode;
423 me->codec.encode = iscz1_encode;
424 me->codec.stop = iscz1_stop;
425 me->codec.reset = iscz1_reset;
427 me->debug = atoi(res_get_def (res, "isamsDebug", "0"));
432 ISAMC_M *key_isamc_m (Res res, ISAMC_M *me)
436 me->compare_item = key_compare;
437 me->log_item = key_logdump_txt;
439 me->codec.start = iscz1_start;
440 me->codec.decode = iscz1_decode;
441 me->codec.encode = iscz1_encode;
442 me->codec.stop = iscz1_stop;
443 me->codec.reset = iscz1_reset;
445 me->debug = atoi(res_get_def (res, "isamcDebug", "0"));
450 int key_SU_encode (int ch, char *out)
456 out[i] = 65 + (ch & 63);
475 int key_SU_decode (int *ch, const unsigned char *out)
480 for (len = 1; *out >= 65; len++, out++)
482 *ch += (*out - 65) * fact;
485 *ch += (*out - 1) * fact;