1 /* This file is part of the Zebra server.
2 Copyright (C) 1994-2009 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
28 #include <yaz/xmalloc.h>
30 struct zebra_rec_key_entry {
34 struct zebra_rec_key_entry *next;
37 struct zebra_rec_keys_t_ {
45 zint custom_record_id;
49 struct zebra_rec_key_entry **entries;
53 struct zebra_rec_key_entry **zebra_rec_keys_mk_hash(zebra_rec_keys_t p,
56 const struct it_key *key)
62 h = key->mem[key->len-1];
64 for (i = 0; i<len; i++)
65 h = h * 65509 + buf[i];
66 for (j = 0; j<key->len; j++)
67 h = h * 65509 + CAST_ZINT_TO_INT(key->mem[j]);
69 return &p->entries[h % (unsigned) p->hash_size];
72 static void init_hash(zebra_rec_keys_t p)
79 p->entries = nmem_malloc(p->nmem, p->hash_size * sizeof(*p->entries));
80 for (i = 0; i<p->hash_size; i++)
85 zebra_rec_keys_t zebra_rec_keys_open(void)
87 zebra_rec_keys_t p = xmalloc(sizeof(*p));
92 p->owner_of_buffer = 1;
93 p->encode_handle = iscz1_start();
94 p->decode_handle = iscz1_start();
96 p->custom_record_id = 0;
97 p->nmem = nmem_create();
106 void zebra_rec_keys_set_buf(zebra_rec_keys_t p, char *buf, size_t sz,
109 if (p->owner_of_buffer)
123 p->buf = xmalloc(sz);
124 memcpy(p->buf, buf, sz);
127 p->owner_of_buffer = copy_buf;
130 void zebra_rec_keys_get_buf(zebra_rec_keys_t p, char **buf, size_t *sz)
140 void zebra_rec_keys_close(zebra_rec_keys_t p)
145 if (p->owner_of_buffer)
147 if (p->encode_handle)
148 iscz1_stop(p->encode_handle);
149 if (p->decode_handle)
150 iscz1_stop(p->decode_handle);
151 nmem_destroy(p->nmem);
155 int zebra_rec_keys_add_hash(zebra_rec_keys_t keys,
156 const char *str, size_t slen,
157 const struct it_key *key)
159 struct zebra_rec_key_entry **kep_first
160 = zebra_rec_keys_mk_hash(keys, str, slen, key);
161 struct zebra_rec_key_entry **kep = kep_first;
164 struct zebra_rec_key_entry *e = *kep;
165 if (slen == e->len && !memcmp(str, e->buf, slen) &&
166 !key_compare(key, &e->key))
168 *kep = (*kep)->next; /* out of queue */
169 e->next = *kep_first; /* move to front */
176 *kep = nmem_malloc(keys->nmem, sizeof(**kep));
179 memcpy(&(*kep)->key, key, sizeof(*key));
180 (*kep)->buf = nmem_malloc(keys->nmem, slen);
181 memcpy((*kep)->buf, str, slen);
185 void zebra_rec_keys_write(zebra_rec_keys_t keys,
186 const char *str, size_t slen,
187 const struct it_key *key)
190 const char *src = (char*) key;
192 assert(keys->owner_of_buffer);
194 if (key->mem[1]) /* record_id custom */
196 keys->custom_record_id = key->mem[1];
199 if (!zebra_rec_keys_add_hash(keys, str, slen, key))
202 yaz_log(YLOG_LOG, "dup key slen=%d %.*s "
203 "ord=" ZINT_FORMAT " seq=" ZINT_FORMAT,
204 slen, slen, str, key->mem[0], key->mem[key->len-1]);
206 return; /* key already there . Omit it */
209 if (keys->buf_used+1024 > keys->buf_max)
211 char *b = (char *) xmalloc (keys->buf_max += 128000);
212 if (keys->buf_used > 0)
213 memcpy (b, keys->buf, keys->buf_used);
217 dst = keys->buf + keys->buf_used;
219 iscz1_encode(keys->encode_handle, &dst, &src);
221 memcpy (dst, str, slen);
224 keys->buf_used = dst - keys->buf;
227 void zebra_rec_keys_reset(zebra_rec_keys_t keys)
232 iscz1_reset(keys->encode_handle);
237 int zebra_rec_keys_rewind(zebra_rec_keys_t keys)
240 iscz1_reset(keys->decode_handle);
243 keys->fetch_offset = 0;
244 if (keys->buf_used == 0)
249 int zebra_rec_keys_empty(zebra_rec_keys_t keys)
251 if (keys->buf_used == 0)
256 int zebra_rec_keys_read(zebra_rec_keys_t keys,
257 const char **str, size_t *slen,
261 if (keys->fetch_offset == keys->buf_used)
265 const char *src = keys->buf + keys->fetch_offset;
266 char *dst = (char*) key;
268 assert (keys->fetch_offset < keys->buf_used);
270 /* store the destination key */
271 iscz1_decode(keys->decode_handle, &dst, &src);
273 /* store pointer to string and length of it */
278 keys->fetch_offset = src - keys->buf;
283 zint zebra_rec_keys_get_custom_record_id(zebra_rec_keys_t keys)
285 return keys->custom_record_id;
291 * indent-tabs-mode: nil
293 * vim: shiftwidth=4 tabstop=8 expandtab