1 /* This file is part of the Zebra server.
2 Copyright (C) Index Data
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31 #include <yaz/xmalloc.h>
33 struct zebra_rec_key_entry {
37 struct zebra_rec_key_entry *next;
40 struct zebra_rec_keys_t_ {
48 zint custom_record_id;
52 struct zebra_rec_key_entry **entries;
56 struct zebra_rec_key_entry **zebra_rec_keys_mk_hash(zebra_rec_keys_t p,
59 const struct it_key *key)
65 h = key->mem[key->len-1];
67 for (i = 0; i<len; i++)
68 h = h * 65509 + buf[i];
69 for (j = 0; j<key->len; j++)
70 h = h * 65509 + CAST_ZINT_TO_INT(key->mem[j]);
72 return &p->entries[h % (unsigned) p->hash_size];
75 static void init_hash(zebra_rec_keys_t p)
82 p->entries = nmem_malloc(p->nmem, p->hash_size * sizeof(*p->entries));
83 for (i = 0; i<p->hash_size; i++)
88 zebra_rec_keys_t zebra_rec_keys_open(void)
90 zebra_rec_keys_t p = xmalloc(sizeof(*p));
95 p->owner_of_buffer = 1;
96 p->encode_handle = iscz1_start();
97 p->decode_handle = iscz1_start();
99 p->custom_record_id = 0;
100 p->nmem = nmem_create();
101 p->hash_size = 32767;
109 void zebra_rec_keys_set_buf(zebra_rec_keys_t p, char *buf, size_t sz,
112 if (p->owner_of_buffer)
126 p->buf = xmalloc(sz);
127 memcpy(p->buf, buf, sz);
130 p->owner_of_buffer = copy_buf;
133 void zebra_rec_keys_get_buf(zebra_rec_keys_t p, char **buf, size_t *sz)
143 void zebra_rec_keys_close(zebra_rec_keys_t p)
148 if (p->owner_of_buffer)
150 if (p->encode_handle)
151 iscz1_stop(p->encode_handle);
152 if (p->decode_handle)
153 iscz1_stop(p->decode_handle);
154 nmem_destroy(p->nmem);
158 int zebra_rec_keys_add_hash(zebra_rec_keys_t keys,
159 const char *str, size_t slen,
160 const struct it_key *key)
162 struct zebra_rec_key_entry **kep_first
163 = zebra_rec_keys_mk_hash(keys, str, slen, key);
164 struct zebra_rec_key_entry **kep = kep_first;
167 struct zebra_rec_key_entry *e = *kep;
168 if (slen == e->len && !memcmp(str, e->buf, slen) &&
169 !key_compare(key, &e->key))
171 *kep = (*kep)->next; /* out of queue */
172 e->next = *kep_first; /* move to front */
179 *kep = nmem_malloc(keys->nmem, sizeof(**kep));
182 memcpy(&(*kep)->key, key, sizeof(*key));
183 (*kep)->buf = nmem_malloc(keys->nmem, slen);
184 memcpy((*kep)->buf, str, slen);
188 void zebra_rec_keys_write(zebra_rec_keys_t keys,
189 const char *str, size_t slen,
190 const struct it_key *key)
193 const char *src = (char*) key;
195 assert(keys->owner_of_buffer);
197 if (key->mem[1]) /* record_id custom */
199 keys->custom_record_id = key->mem[1];
202 if (!zebra_rec_keys_add_hash(keys, str, slen, key))
205 yaz_log(YLOG_LOG, "dup key slen=%d %.*s "
206 "ord=" ZINT_FORMAT " seq=" ZINT_FORMAT,
207 slen, slen, str, key->mem[0], key->mem[key->len-1]);
209 return; /* key already there . Omit it */
212 if (keys->buf_used+1024 > keys->buf_max)
214 char *b = (char *) xmalloc (keys->buf_max += 128000);
215 if (keys->buf_used > 0)
216 memcpy (b, keys->buf, keys->buf_used);
220 dst = keys->buf + keys->buf_used;
222 iscz1_encode(keys->encode_handle, &dst, &src);
224 memcpy (dst, str, slen);
227 keys->buf_used = dst - keys->buf;
230 void zebra_rec_keys_reset(zebra_rec_keys_t keys)
235 iscz1_reset(keys->encode_handle);
240 int zebra_rec_keys_rewind(zebra_rec_keys_t keys)
243 iscz1_reset(keys->decode_handle);
246 keys->fetch_offset = 0;
247 if (keys->buf_used == 0)
252 int zebra_rec_keys_empty(zebra_rec_keys_t keys)
254 if (keys->buf_used == 0)
259 int zebra_rec_keys_read(zebra_rec_keys_t keys,
260 const char **str, size_t *slen,
264 if (keys->fetch_offset == keys->buf_used)
268 const char *src = keys->buf + keys->fetch_offset;
269 char *dst = (char*) key;
271 assert (keys->fetch_offset < keys->buf_used);
273 /* store the destination key */
274 iscz1_decode(keys->decode_handle, &dst, &src);
276 /* store pointer to string and length of it */
281 keys->fetch_offset = src - keys->buf;
286 zint zebra_rec_keys_get_custom_record_id(zebra_rec_keys_t keys)
288 return keys->custom_record_id;
294 * c-file-style: "Stroustrup"
295 * indent-tabs-mode: nil
297 * vim: shiftwidth=4 tabstop=8 expandtab