IDZEBRA  2.1.2
reckeys.c
Go to the documentation of this file.
1 /* This file is part of the Zebra server.
2  Copyright (C) Index Data
3 
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
7 version.
8 
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
12 for more details.
13 
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
17 
18 */
19 
20 #if HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <assert.h>
27 #include <ctype.h>
28 
29 #include "reckeys.h"
30 #include <yaz/nmem.h>
31 #include <yaz/xmalloc.h>
32 
34  char *buf;
35  size_t len;
36  struct it_key key;
38 };
39 
41  size_t buf_used;
42  size_t buf_max;
43  size_t fetch_offset;
44  char *buf;
49 
50  NMEM nmem;
51  size_t hash_size;
53 };
54 
55 
57  const char *buf,
58  size_t len,
59  const struct it_key *key)
60 {
61  unsigned h = 0;
62  size_t i;
63  int j;
64 #if 0
65  h = key->mem[key->len-1];
66 #else
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]);
71 #endif
72  return &p->entries[h % (unsigned) p->hash_size];
73 }
74 
76 {
77  p->entries = 0;
78  nmem_reset(p->nmem);
79  if (p->hash_size)
80  {
81  size_t i;
82  p->entries = nmem_malloc(p->nmem, p->hash_size * sizeof(*p->entries));
83  for (i = 0; i<p->hash_size; i++)
84  p->entries[i] = 0;
85  }
86 }
87 
89 {
90  zebra_rec_keys_t p = xmalloc(sizeof(*p));
91  p->buf_used = 0;
92  p->buf_max = 0;
93  p->fetch_offset = 0;
94  p->buf = 0;
95  p->owner_of_buffer = 1;
98 
99  p->custom_record_id = 0;
100  p->nmem = nmem_create();
101  p->hash_size = 32767;
102  p->entries = 0;
103 
104  init_hash(p);
105 
106  return p;
107 }
108 
109 void zebra_rec_keys_set_buf(zebra_rec_keys_t p, char *buf, size_t sz,
110  int copy_buf)
111 {
112  if (p->owner_of_buffer)
113  xfree(p->buf);
114  p->buf_used = sz;
115  p->buf_max = sz;
116  if (!copy_buf)
117  {
118  p->buf = buf;
119  }
120  else
121  {
122  if (!sz)
123  p->buf = 0;
124  else
125  {
126  p->buf = xmalloc(sz);
127  memcpy(p->buf, buf, sz);
128  }
129  }
130  p->owner_of_buffer = copy_buf;
131 }
132 
133 void zebra_rec_keys_get_buf(zebra_rec_keys_t p, char **buf, size_t *sz)
134 {
135  *buf = p->buf;
136  *sz = p->buf_used;
137 
138  p->buf = 0;
139  p->buf_max = 0;
140  p->buf_used = 0;
141 }
142 
144 {
145  if (!p)
146  return;
147 
148  if (p->owner_of_buffer)
149  xfree(p->buf);
150  if (p->encode_handle)
152  if (p->decode_handle)
154  nmem_destroy(p->nmem);
155  xfree(p);
156 }
157 
159  const char *str, size_t slen,
160  const struct it_key *key)
161 {
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;
165  while (*kep)
166  {
167  struct zebra_rec_key_entry *e = *kep;
168  if (slen == e->len && !memcmp(str, e->buf, slen) &&
169  !key_compare(key, &e->key))
170  {
171  *kep = (*kep)->next; /* out of queue */
172  e->next = *kep_first; /* move to front */
173  *kep_first = e;
174 
175  return 0;
176  }
177  kep = &(*kep)->next;
178  }
179  *kep = nmem_malloc(keys->nmem, sizeof(**kep));
180  (*kep)->next = 0;
181  (*kep)->len = slen;
182  memcpy(&(*kep)->key, key, sizeof(*key));
183  (*kep)->buf = nmem_malloc(keys->nmem, slen);
184  memcpy((*kep)->buf, str, slen);
185  return 1;
186 }
187 
189  const char *str, size_t slen,
190  const struct it_key *key)
191 {
192  char *dst;
193  const char *src = (char*) key;
194 
195  assert(keys->owner_of_buffer);
196 
197  if (key->mem[1]) /* record_id custom */
198  {
199  keys->custom_record_id = key->mem[1];
200  }
201 #if 1
202  if (!zebra_rec_keys_add_hash(keys, str, slen, key))
203  {
204 #if 0
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]);
208 #endif
209  return; /* key already there . Omit it */
210  }
211 #endif
212  if (keys->buf_used+1024 > keys->buf_max)
213  {
214  char *b = (char *) xmalloc (keys->buf_max += 128000);
215  if (keys->buf_used > 0)
216  memcpy (b, keys->buf, keys->buf_used);
217  xfree (keys->buf);
218  keys->buf = b;
219  }
220  dst = keys->buf + keys->buf_used;
221 
222  iscz1_encode(keys->encode_handle, &dst, &src);
223 
224  memcpy (dst, str, slen);
225  dst += slen;
226  *dst++ = '\0';
227  keys->buf_used = dst - keys->buf;
228 }
229 
231 {
232  assert(keys);
233  keys->buf_used = 0;
234 
235  iscz1_reset(keys->encode_handle);
236 
237  init_hash(keys);
238 }
239 
241 {
242  assert(keys);
243  iscz1_reset(keys->decode_handle);
244 
245 
246  keys->fetch_offset = 0;
247  if (keys->buf_used == 0)
248  return 0;
249  return 1;
250 }
251 
253 {
254  if (keys->buf_used == 0)
255  return 1;
256  return 0;
257 }
258 
260  const char **str, size_t *slen,
261  struct it_key *key)
262 {
263  assert(keys);
264  if (keys->fetch_offset == keys->buf_used)
265  return 0;
266  else
267  {
268  const char *src = keys->buf + keys->fetch_offset;
269  char *dst = (char*) key;
270 
271  assert (keys->fetch_offset < keys->buf_used);
272 
273  /* store the destination key */
274  iscz1_decode(keys->decode_handle, &dst, &src);
275 
276  /* store pointer to string and length of it */
277  *str = src;
278  *slen = strlen(src);
279  src += *slen + 1;
280 
281  keys->fetch_offset = src - keys->buf;
282  }
283  return 1;
284 }
285 
287 {
288  return keys->custom_record_id;
289 }
290 
291 /*
292  * Local variables:
293  * c-basic-offset: 4
294  * c-file-style: "Stroustrup"
295  * indent-tabs-mode: nil
296  * End:
297  * vim: shiftwidth=4 tabstop=8 expandtab
298  */
299 
int key_compare(const void *p1, const void *p2)
Definition: it_key.c:73
size_t len
Definition: reckeys.c:35
int len
Definition: it_key.h:31
void zebra_rec_keys_write(zebra_rec_keys_t keys, const char *str, size_t slen, const struct it_key *key)
Definition: reckeys.c:188
size_t hash_size
Definition: reckeys.c:51
char owner_of_buffer
Definition: reckeys.c:47
void zebra_rec_keys_get_buf(zebra_rec_keys_t p, char **buf, size_t *sz)
Definition: reckeys.c:133
void * iscz1_start(void)
Definition: it_key.c:129
void iscz1_reset(void *vp)
Definition: it_key.c:145
void * encode_handle
Definition: reckeys.c:45
struct zebra_rec_key_entry * next
Definition: reckeys.c:37
void iscz1_stop(void *p)
Definition: it_key.c:154
static void init_hash(zebra_rec_keys_t p)
Definition: reckeys.c:75
int zebra_rec_keys_add_hash(zebra_rec_keys_t keys, const char *str, size_t slen, const struct it_key *key)
Definition: reckeys.c:158
int zebra_rec_keys_rewind(zebra_rec_keys_t keys)
Definition: reckeys.c:240
zint zebra_rec_keys_get_custom_record_id(zebra_rec_keys_t keys)
Definition: reckeys.c:286
struct it_key key
Definition: reckeys.c:36
void zebra_rec_keys_reset(zebra_rec_keys_t keys)
Definition: reckeys.c:230
struct zebra_rec_key_entry ** zebra_rec_keys_mk_hash(zebra_rec_keys_t p, const char *buf, size_t len, const struct it_key *key)
Definition: reckeys.c:56
void zebra_rec_keys_close(zebra_rec_keys_t p)
Definition: reckeys.c:143
#define CAST_ZINT_TO_INT(x)
Definition: util.h:96
size_t buf_max
Definition: reckeys.c:42
zebra_rec_keys_t zebra_rec_keys_open(void)
Definition: reckeys.c:88
void * decode_handle
Definition: reckeys.c:46
struct zebra_rec_key_entry ** entries
Definition: reckeys.c:52
zint custom_record_id
Definition: reckeys.c:48
zint mem[IT_KEY_LEVEL_MAX]
Definition: it_key.h:32
void zebra_rec_keys_set_buf(zebra_rec_keys_t p, char *buf, size_t sz, int copy_buf)
Definition: reckeys.c:109
void iscz1_encode(void *vp, char **dst, const char **src)
Definition: it_key.c:189
size_t fetch_offset
Definition: reckeys.c:43
size_t buf_used
Definition: reckeys.c:41
long zint
Zebra integer.
Definition: util.h:66
int zebra_rec_keys_read(zebra_rec_keys_t keys, const char **str, size_t *slen, struct it_key *key)
Definition: reckeys.c:259
Definition: reckeys.c:33
Definition: it_key.h:30
char * buf
Definition: reckeys.c:34
int zebra_rec_keys_empty(zebra_rec_keys_t keys)
Definition: reckeys.c:252
void iscz1_decode(void *vp, char **dst, const char **src)
Definition: it_key.c:237
#define ZINT_FORMAT
Definition: util.h:72