1 /* $Id: it_key.c,v 1.1 2006-11-21 14:32:38 adam Exp $
2 Copyright (C) 1995-2006
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 this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 #include <yaz/xmalloc.h>
32 #define CODEC_INLINE inline
37 void key_logdump_txt(int logmask, const void *p, const char *txt)
47 memcpy (&key, p, sizeof(key));
48 assert(key.len > 0 && key.len <= IT_KEY_LEVEL_MAX);
50 for (i = 0; i<key.len; i++)
54 sprintf(formstr + strlen(formstr), ZINT_FORMAT, key.mem[i]);
56 yaz_log(logmask, "%s %s", formstr, txt);
59 yaz_log(logmask, " (no key) %s",txt);
62 void key_logdump(int logmask, const void *p)
64 key_logdump_txt(logmask, p, "");
67 int key_compare_it (const void *p1, const void *p2)
69 int i, l = ((struct it_key *) p1)->len;
70 if (((struct it_key *) p2)->len > l)
71 l = ((struct it_key *) p2)->len;
72 assert (l <= IT_KEY_LEVEL_MAX && l > 0);
73 for (i = 0; i < l; i++)
75 if (((struct it_key *) p1)->mem[i] != ((struct it_key *) p2)->mem[i])
77 if (((struct it_key *) p1)->mem[i] > ((struct it_key *) p2)->mem[i])
86 char *key_print_it (const void *p, char *buf)
92 int key_compare (const void *p1, const void *p2)
96 memcpy (&i1, p1, sizeof(i1));
97 memcpy (&i2, p2, sizeof(i2));
101 assert (l <= IT_KEY_LEVEL_MAX && l > 0);
102 for (i = 0; i < l; i++)
104 if (i1.mem[i] != i2.mem[i])
106 if (i1.mem[i] > i2.mem[i])
115 zint key_get_seq(const void *p)
118 memcpy (&k, p, sizeof(k));
119 return k.mem[k.len-1];
122 zint key_get_segment(const void *p)
125 memcpy (&k, p, sizeof(k));
126 return k.mem[k.len-2];
129 int key_qsort_compare (const void *p1, const void *p2)
133 char *cp1 = *(char **) p1;
134 char *cp2 = *(char **) p2;
136 if ((r = strcmp (cp1, cp2)))
139 if ((r = key_compare (cp1+l+1, cp2+l+1)))
141 return cp1[l] - cp2[l];
144 struct iscz1_code_info {
148 void *iscz1_start (void)
150 struct iscz1_code_info *p = (struct iscz1_code_info *)
151 xmalloc (sizeof(*p));
156 void key_init(struct it_key *key)
160 for (i = 0; i < IT_KEY_LEVEL_MAX; i++)
164 void iscz1_reset (void *vp)
166 struct iscz1_code_info *p = (struct iscz1_code_info *) vp;
169 for (i = 0; i < IT_KEY_LEVEL_MAX; i++)
173 void iscz1_stop (void *p)
178 /* small encoder that works with unsigneds of any length */
179 static CODEC_INLINE void iscz1_encode_int (zint d, char **dst)
181 unsigned char *bp = (unsigned char*) *dst;
185 *bp++ = (unsigned) (128 | (d & 127));
188 *bp++ = (unsigned) d;
192 /* small decoder that works with unsigneds of any length */
193 static CODEC_INLINE zint iscz1_decode_int (unsigned char **src)
199 while (((c = *(*src)++) & 128))
201 d += ((zint) (c&127) << r);
204 d += ((zint) c << r);
208 void iscz1_encode (void *vp, char **dst, const char **src)
210 struct iscz1_code_info *p = (struct iscz1_code_info *) vp;
220 if diff is 0, then there is more ...
221 if diff is non-zero, then _may_ be more
223 memcpy (&tkey, *src, sizeof(struct it_key));
225 /* deal with leader + delta encoding .. */
227 assert(tkey.len > 0 && tkey.len <= IT_KEY_LEVEL_MAX);
228 for (i = 0; i < tkey.len; i++)
230 d = tkey.mem[i] - p->key.mem[i];
231 if (d || i == tkey.len-1)
232 { /* all have been equal until now, now make delta .. */
233 p->key.mem[i] = tkey.mem[i];
236 iscz1_encode_int (i + (tkey.len << 3) + 64, dst);
238 iscz1_encode_int (d, dst);
242 iscz1_encode_int (i + (tkey.len << 3), dst);
247 /* rest uses absolute encoding ... */
248 for (; i < tkey.len; i++)
250 iscz1_encode_int (tkey.mem[i], dst);
251 p->key.mem[i] = tkey.mem[i];
253 (*src) += sizeof(struct it_key);
256 void iscz1_decode (void *vp, char **dst, const char **src)
258 struct iscz1_code_info *p = (struct iscz1_code_info *) vp;
261 int leader = (int) iscz1_decode_int ((unsigned char **) src);
264 p->key.mem[i] += iscz1_decode_int ((unsigned char **) src);
266 p->key.mem[i] = iscz1_decode_int ((unsigned char **) src);
267 p->key.len = (leader >> 3) & 7;
268 while (++i < p->key.len)
269 p->key.mem[i] = iscz1_decode_int ((unsigned char **) src);
270 memcpy (*dst, &p->key, sizeof(struct it_key));
271 (*dst) += sizeof(struct it_key);
277 * indent-tabs-mode: nil
279 * vim: shiftwidth=4 tabstop=8 expandtab