1 /* $Id: key_block.c,v 1.4 2006-11-21 22:17:49 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
33 #include "key_block.h"
35 #include <yaz/xmalloc.h>
37 struct zebra_key_block {
47 char **thread_key_buf;
48 size_t thread_ptr_top;
52 pthread_mutex_t mutex;
54 pthread_cond_t work_available;
56 pthread_cond_t cond_sorting;
61 #define ENCODE_BUFLEN 768
65 char buf[ENCODE_BUFLEN];
68 static int log_level = 0;
70 #define USE_SHELLSORT 0
73 static void shellsort(void *ar, int r, size_t s,
74 int (*cmp)(const void *a, const void *b))
79 static const int incs[16] = { 1391376, 463792, 198768, 86961, 33936,
80 13776, 4592, 1968, 861, 336,
81 112, 48, 21, 7, 3, 1 };
82 for ( k = 0; k < 16; k++)
83 for (h = incs[k], i = h; i < r; i++)
87 while (j > h && (*cmp)(a + s*(j-h), v) > 0)
89 memcpy (a + s*j, a + s*(j-h), s);
98 static void encode_key_init(struct encode_info *i)
100 i->encode_handle = iscz1_start();
101 i->decode_handle = iscz1_start();
104 static void encode_key_write (char *k, struct encode_info *i, FILE *outf)
107 char *bp = i->buf, *bp0;
108 const char *src = (char *) &key;
110 /* copy term to output buf */
111 while ((*bp++ = *k++))
113 /* and copy & align key so we can mangle */
114 memcpy (&key, k+1, sizeof(struct it_key)); /* *k is insert/delete */
118 key_logdump_txt(YLOG_LOG, &key, *k ? "i" : "d");
120 assert(key.mem[0] >= 0);
123 iscz1_encode(i->encode_handle, &bp, &src);
125 *bp0 = (*k * 128) + bp - bp0 - 1; /* length and insert/delete combined */
126 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
128 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fwrite");
129 zebra_exit("encode_key_write");
137 const char *src = bp0+1;
138 char *dst = (char*) &key2;
139 iscz1_decode(i->decode_handle, &dst, &src);
141 key_logdump_txt(YLOG_LOG, &key2, *k ? "i" : "d");
148 static void encode_key_flush (struct encode_info *i, FILE *outf)
150 iscz1_stop(i->encode_handle);
151 iscz1_stop(i->decode_handle);
154 void key_block_flush_int(zebra_key_block_t p,
155 char **key_buf, size_t ptr_top, size_t ptr_i);
157 #if YAZ_POSIX_THREADS
158 static void *thread_func(void *vp)
160 zebra_key_block_t p = (zebra_key_block_t) vp;
163 pthread_mutex_lock(&p->mutex);
165 while (!p->is_sorting && !p->exit_flag)
166 pthread_cond_wait(&p->work_available, &p->mutex);
171 pthread_mutex_unlock(&p->mutex);
173 key_block_flush_int(p, p->thread_key_buf,
174 p->thread_ptr_top, p->thread_ptr_i);
176 pthread_mutex_lock(&p->mutex);
178 pthread_cond_signal(&p->cond_sorting);
179 pthread_mutex_unlock(&p->mutex);
181 pthread_mutex_unlock(&p->mutex);
186 zebra_key_block_t key_block_create(int mem, const char *key_tmp_dir,
189 zebra_key_block_t p = xmalloc(sizeof(*p));
191 p->key_buf = (char**) xmalloc (mem);
192 p->ptr_top = mem/sizeof(char*);
195 p->key_tmp_dir = xstrdup(key_tmp_dir);
201 #if YAZ_POSIX_THREADS
202 p->use_threads = use_threads;
205 pthread_mutex_init(&p->mutex, 0);
206 pthread_cond_init(&p->work_available, 0);
207 pthread_cond_init(&p->cond_sorting, 0);
208 pthread_create(&p->thread_id, 0, thread_func, p);
209 p->alt_buf = (char**) xmalloc (mem);
212 yaz_log(YLOG_LOG, "key_block_create t=%d", p->use_threads);
216 void key_block_destroy(zebra_key_block_t *pp)
218 zebra_key_block_t p = *pp;
223 #if YAZ_POSIX_THREADS
224 pthread_mutex_lock(&p->mutex);
226 while (p->is_sorting)
227 pthread_cond_wait(&p->cond_sorting, &p->mutex);
231 pthread_cond_broadcast(&p->work_available);
233 pthread_mutex_unlock(&p->mutex);
234 pthread_join(p->thread_id, 0);
235 pthread_cond_destroy(&p->work_available);
236 pthread_cond_destroy(&p->cond_sorting);
237 pthread_mutex_destroy(&p->mutex);
243 xfree(p->key_tmp_dir);
249 void key_block_write(zebra_key_block_t p, zint sysno, struct it_key *key_in,
250 int cmd, const char *str_buf, size_t str_len,
251 zint staticrank, int static_rank_enable)
255 struct it_key key_out;
257 if (p->key_buf_used + 1024 > (p->ptr_top -p->ptr_i)*sizeof(char*))
258 key_block_flush(p, 0);
260 assert(p->ptr_i > 0);
261 (p->key_buf)[p->ptr_top - p->ptr_i] =
262 (char*)p->key_buf + p->key_buf_used;
264 /* key_in->mem[0] ord/ch */
265 /* key_in->mem[1] filter specified record ID */
267 /* encode the ordinal value (field/use/attribute) .. */
268 ch = CAST_ZINT_TO_INT(key_in->mem[0]);
270 key_SU_encode(ch, (char*)p->key_buf +
273 /* copy the 0-terminated stuff from str to output */
274 memcpy((char*)p->key_buf + p->key_buf_used, str_buf, str_len);
275 p->key_buf_used += str_len;
276 ((char*)p->key_buf)[(p->key_buf_used)++] = '\0';
278 /* the delete/insert indicator */
279 ((char*)p->key_buf)[(p->key_buf_used)++] = cmd;
281 if (static_rank_enable)
282 key_out.mem[j++] = staticrank;
284 if (key_in->mem[1]) /* filter specified record ID */
285 key_out.mem[j++] = key_in->mem[1];
287 key_out.mem[j++] = sysno;
288 for (i = 2; i < key_in->len; i++)
289 key_out.mem[j++] = key_in->mem[i];
292 memcpy((char*)p->key_buf + p->key_buf_used,
293 &key_out, sizeof(key_out));
294 (p->key_buf_used) += sizeof(key_out);
298 void key_block_flush_int(zebra_key_block_t p,
299 char **key_buf, size_t ptr_top, size_t ptr_i)
304 struct encode_info encode_info;
307 yaz_log(YLOG_LOG, "sorting section %d", (p->key_file_no));
308 yaz_log(log_level, " sort_buff at %p n=%d",
309 key_buf + ptr_top - ptr_i,ptr_i);
313 shellsort(key_buf + ptr_top - ptr_i, ptr_i,
314 sizeof(char*), key_qsort_compare);
316 qsort(key_buf + ptr_top - ptr_i, ptr_i,
317 sizeof(char*), key_qsort_compare);
319 sprintf(out_fname, "%s/key%d.tmp", p->key_tmp_dir, p->key_file_no);
321 if (!(outf = fopen (out_fname, "wb")))
323 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fopen %s", out_fname);
324 zebra_exit("key_block_flush");
326 yaz_log(YLOG_LOG, "writing section %d", p->key_file_no);
327 prevcp = cp = (key_buf)[ptr_top - ptr_i];
329 encode_key_init (&encode_info);
330 encode_key_write (cp, &encode_info, outf);
334 cp = (key_buf)[ptr_top - ptr_i];
335 if (strcmp (cp, prevcp))
337 encode_key_flush ( &encode_info, outf);
338 encode_key_init (&encode_info);
339 encode_key_write (cp, &encode_info, outf);
343 encode_key_write (cp + strlen(cp), &encode_info, outf);
345 encode_key_flush ( &encode_info, outf);
348 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fclose %s", out_fname);
349 zebra_exit("key_block_flush");
351 yaz_log(YLOG_LOG, "finished section %d", p->key_file_no);
354 void key_block_flush(zebra_key_block_t p, int is_final)
361 #if YAZ_POSIX_THREADS
364 pthread_mutex_lock(&p->mutex);
366 while (p->is_sorting)
367 pthread_cond_wait(&p->cond_sorting, &p->mutex);
371 p->thread_ptr_top = p->ptr_top;
372 p->thread_ptr_i = p->ptr_i;
373 p->thread_key_buf = p->key_buf;
376 p->key_buf = p->alt_buf;
379 pthread_cond_signal(&p->work_available);
383 while (p->is_sorting)
384 pthread_cond_wait(&p->cond_sorting, &p->mutex);
386 pthread_mutex_unlock(&p->mutex);
390 key_block_flush_int(p, p->key_buf, p->ptr_top, p->ptr_i);
395 int key_block_get_no_files(zebra_key_block_t p)
398 return p->key_file_no;
405 * indent-tabs-mode: nil
407 * vim: shiftwidth=4 tabstop=8 expandtab