1 /* $Id: kcompare.c,v 1.58 2006-05-10 08:13:21 adam Exp $
2 Copyright (C) 1995-2005
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 CODEC_INLINE inline
36 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++)
53 sprintf(formstr + strlen(formstr), ZINT_FORMAT, key.mem[i]);
55 yaz_log(logmask, "%s %s", formstr, txt);
58 yaz_log(logmask, " (no key) %s",txt);
61 void key_logdump(int logmask, const void *p)
63 key_logdump_txt(logmask, p, "");
66 int key_compare_it (const void *p1, const void *p2)
68 int i, l = ((struct it_key *) p1)->len;
69 if (((struct it_key *) p2)->len > l)
70 l = ((struct it_key *) p2)->len;
71 assert (l <= 4 && l > 0);
72 for (i = 0; i < l; i++)
74 if (((struct it_key *) p1)->mem[i] != ((struct it_key *) p2)->mem[i])
76 if (((struct it_key *) p1)->mem[i] > ((struct it_key *) p2)->mem[i])
85 char *key_print_it (const void *p, char *buf)
91 int key_compare (const void *p1, const void *p2)
95 memcpy (&i1, p1, sizeof(i1));
96 memcpy (&i2, p2, sizeof(i2));
100 assert (l <= 4 && l > 0);
101 for (i = 0; i < l; i++)
103 if (i1.mem[i] != i2.mem[i])
105 if (i1.mem[i] > i2.mem[i])
114 zint key_get_seq(const void *p)
117 memcpy (&k, p, sizeof(k));
118 return k.mem[k.len-1];
121 int key_qsort_compare (const void *p1, const void *p2)
125 char *cp1 = *(char **) p1;
126 char *cp2 = *(char **) p2;
128 if ((r = strcmp (cp1, cp2)))
131 if ((r = key_compare (cp1+l+1, cp2+l+1)))
133 return cp1[l] - cp2[l];
136 struct iscz1_code_info {
140 void *iscz1_start (void)
142 struct iscz1_code_info *p = (struct iscz1_code_info *)
143 xmalloc (sizeof(*p));
148 void key_init(struct it_key *key)
152 for (i = 0; i<IT_KEY_LEVEL_MAX; i++)
156 void iscz1_reset (void *vp)
158 struct iscz1_code_info *p = (struct iscz1_code_info *) vp;
161 for (i = 0; i< IT_KEY_LEVEL_MAX; i++)
165 void iscz1_stop (void *p)
170 /* small encoder that works with unsigneds of any length */
171 static CODEC_INLINE void iscz1_encode_int (zint d, char **dst)
173 unsigned char *bp = (unsigned char*) *dst;
177 *bp++ = (unsigned) (128 | (d & 127));
180 *bp++ = (unsigned) d;
184 /* small decoder that works with unsigneds of any length */
185 static CODEC_INLINE zint iscz1_decode_int (unsigned char **src)
191 while (((c = *(*src)++) & 128))
193 d += ((zint) (c&127) << r);
196 d += ((zint) c << r);
200 void iscz1_encode (void *vp, char **dst, const char **src)
202 struct iscz1_code_info *p = (struct iscz1_code_info *) vp;
212 if diff is 0, then there is more ...
213 if diff is non-zero, then _may_ be more
215 memcpy (&tkey, *src, sizeof(struct it_key));
217 /* deal with leader + delta encoding .. */
219 assert(tkey.len > 0 && tkey.len <= 4);
220 for (i = 0; i < tkey.len; i++)
222 d = tkey.mem[i] - p->key.mem[i];
223 if (d || i == tkey.len-1)
224 { /* all have been equal until now, now make delta .. */
225 p->key.mem[i] = tkey.mem[i];
228 iscz1_encode_int (i + (tkey.len << 3) + 64, dst);
230 iscz1_encode_int (d, dst);
234 iscz1_encode_int (i + (tkey.len << 3), dst);
239 /* rest uses absolute encoding ... */
240 for (; i < tkey.len; i++)
242 iscz1_encode_int (tkey.mem[i], dst);
243 p->key.mem[i] = tkey.mem[i];
245 (*src) += sizeof(struct it_key);
248 void iscz1_decode (void *vp, char **dst, const char **src)
250 struct iscz1_code_info *p = (struct iscz1_code_info *) vp;
253 int leader = (int) iscz1_decode_int ((unsigned char **) src);
256 p->key.mem[i] += iscz1_decode_int ((unsigned char **) src);
258 p->key.mem[i] = iscz1_decode_int ((unsigned char **) src);
259 p->key.len = (leader >> 3) & 7;
260 while (++i < p->key.len)
261 p->key.mem[i] = iscz1_decode_int ((unsigned char **) src);
262 memcpy (*dst, &p->key, sizeof(struct it_key));
263 (*dst) += sizeof(struct it_key);
266 ISAMS_M *key_isams_m (Res res, ISAMS_M *me)
268 isams_getmethod (me);
270 me->compare_item = key_compare;
271 me->log_item = key_logdump_txt;
273 me->codec.start = iscz1_start;
274 me->codec.decode = iscz1_decode;
275 me->codec.encode = iscz1_encode;
276 me->codec.stop = iscz1_stop;
277 me->codec.reset = iscz1_reset;
279 me->debug = atoi(res_get_def (res, "isamsDebug", "0"));
284 ISAMC_M *key_isamc_m (Res res, ISAMC_M *me)
286 isamc_getmethod (me);
288 me->compare_item = key_compare;
289 me->log_item = key_logdump_txt;
291 me->codec.start = iscz1_start;
292 me->codec.decode = iscz1_decode;
293 me->codec.encode = iscz1_encode;
294 me->codec.stop = iscz1_stop;
295 me->codec.reset = iscz1_reset;
297 me->debug = atoi(res_get_def (res, "isamcDebug", "0"));
302 int key_SU_encode (int ch, char *out)
308 out[i] = 65 + (ch & 63);
327 int key_SU_decode (int *ch, const unsigned char *out)
332 for (len = 1; *out >= 65; len++, out++)
334 *ch += (*out - 65) * fact;
337 *ch += (*out - 1) * fact;
344 * indent-tabs-mode: nil
346 * vim: shiftwidth=4 tabstop=8 expandtab