1 /* $Id: key_block.c,v 1.2 2006-11-21 14:54:12 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
34 #include "key_block.h"
36 struct zebra_key_block {
45 char **thread_key_buf;
46 size_t thread_ptr_top;
50 pthread_mutex_t mutex;
52 pthread_cond_t work_available;
54 pthread_cond_t cond_sorting;
59 #define ENCODE_BUFLEN 768
63 char buf[ENCODE_BUFLEN];
66 static int log_level = 0;
68 #define USE_SHELLSORT 0
71 static void shellsort(void *ar, int r, size_t s,
72 int (*cmp)(const void *a, const void *b))
77 static const int incs[16] = { 1391376, 463792, 198768, 86961, 33936,
78 13776, 4592, 1968, 861, 336,
79 112, 48, 21, 7, 3, 1 };
80 for ( k = 0; k < 16; k++)
81 for (h = incs[k], i = h; i < r; i++)
85 while (j > h && (*cmp)(a + s*(j-h), v) > 0)
87 memcpy (a + s*j, a + s*(j-h), s);
96 static void encode_key_init(struct encode_info *i)
98 i->encode_handle = iscz1_start();
99 i->decode_handle = iscz1_start();
102 static void encode_key_write (char *k, struct encode_info *i, FILE *outf)
105 char *bp = i->buf, *bp0;
106 const char *src = (char *) &key;
108 /* copy term to output buf */
109 while ((*bp++ = *k++))
111 /* and copy & align key so we can mangle */
112 memcpy (&key, k+1, sizeof(struct it_key)); /* *k is insert/delete */
116 key_logdump_txt(YLOG_LOG, &key, *k ? "i" : "d");
118 assert(key.mem[0] >= 0);
121 iscz1_encode(i->encode_handle, &bp, &src);
123 *bp0 = (*k * 128) + bp - bp0 - 1; /* length and insert/delete combined */
124 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
126 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fwrite");
127 zebra_exit("encode_key_write");
135 const char *src = bp0+1;
136 char *dst = (char*) &key2;
137 iscz1_decode(i->decode_handle, &dst, &src);
139 key_logdump_txt(YLOG_LOG, &key2, *k ? "i" : "d");
146 static void encode_key_flush (struct encode_info *i, FILE *outf)
148 iscz1_stop(i->encode_handle);
149 iscz1_stop(i->decode_handle);
152 void key_block_flush_int(zebra_key_block_t p,
153 char **key_buf, size_t ptr_top, size_t ptr_i);
155 #if YAZ_POSIX_THREADS
156 static void *thread_func(void *vp)
158 zebra_key_block_t p = (zebra_key_block_t) vp;
161 pthread_mutex_lock(&p->mutex);
163 while (!p->is_sorting && !p->exit_flag)
164 pthread_cond_wait(&p->work_available, &p->mutex);
169 pthread_mutex_unlock(&p->mutex);
171 key_block_flush_int(p, p->thread_key_buf,
172 p->thread_ptr_top, p->thread_ptr_i);
174 pthread_mutex_lock(&p->mutex);
176 pthread_cond_signal(&p->cond_sorting);
177 pthread_mutex_unlock(&p->mutex);
179 pthread_mutex_unlock(&p->mutex);
184 zebra_key_block_t key_block_create(int mem, const char *key_tmp_dir)
186 zebra_key_block_t p = xmalloc(sizeof(*p));
188 p->key_buf = (char**) xmalloc (mem);
189 p->ptr_top = mem/sizeof(char*);
192 p->key_tmp_dir = xstrdup(key_tmp_dir);
194 #if YAZ_POSIX_THREADS
195 p->alt_buf = (char**) xmalloc (mem);
198 pthread_mutex_init(&p->mutex, 0);
199 pthread_cond_init(&p->work_available, 0);
200 pthread_cond_init(&p->cond_sorting, 0);
201 pthread_create(&p->thread_id, 0, thread_func, p);
206 void key_block_destroy(zebra_key_block_t *pp)
208 zebra_key_block_t p = *pp;
211 #if YAZ_POSIX_THREADS
212 pthread_mutex_lock(&p->mutex);
214 while (p->is_sorting)
215 pthread_cond_wait(&p->cond_sorting, &p->mutex);
219 pthread_cond_broadcast(&p->work_available);
221 pthread_mutex_unlock(&p->mutex);
222 pthread_join(p->thread_id, 0);
223 pthread_cond_destroy(&p->work_available);
224 pthread_cond_destroy(&p->cond_sorting);
225 pthread_mutex_destroy(&p->mutex);
230 xfree(p->key_tmp_dir);
236 void key_block_write(zebra_key_block_t p, SYSNO sysno, struct it_key *key_in,
237 int cmd, const char *str_buf, size_t str_len,
238 zint staticrank, int static_rank_enable)
242 struct it_key key_out;
244 if (p->key_buf_used + 1024 > (p->ptr_top -p->ptr_i)*sizeof(char*))
245 key_block_flush(p, 0);
247 assert(p->ptr_i > 0);
248 (p->key_buf)[p->ptr_top - p->ptr_i] =
249 (char*)p->key_buf + p->key_buf_used;
251 /* key_in->mem[0] ord/ch */
252 /* key_in->mem[1] filter specified record ID */
254 /* encode the ordinal value (field/use/attribute) .. */
255 ch = CAST_ZINT_TO_INT(key_in->mem[0]);
257 key_SU_encode(ch, (char*)p->key_buf +
260 /* copy the 0-terminated stuff from str to output */
261 memcpy((char*)p->key_buf + p->key_buf_used, str_buf, str_len);
262 p->key_buf_used += str_len;
263 ((char*)p->key_buf)[(p->key_buf_used)++] = '\0';
265 /* the delete/insert indicator */
266 ((char*)p->key_buf)[(p->key_buf_used)++] = cmd;
268 if (static_rank_enable)
269 key_out.mem[j++] = staticrank;
271 if (key_in->mem[1]) /* filter specified record ID */
272 key_out.mem[j++] = key_in->mem[1];
274 key_out.mem[j++] = sysno;
275 for (i = 2; i < key_in->len; i++)
276 key_out.mem[j++] = key_in->mem[i];
279 memcpy((char*)p->key_buf + p->key_buf_used,
280 &key_out, sizeof(key_out));
281 (p->key_buf_used) += sizeof(key_out);
285 void key_block_flush_int(zebra_key_block_t p,
286 char **key_buf, size_t ptr_top, size_t ptr_i)
291 struct encode_info encode_info;
294 yaz_log(YLOG_LOG, "sorting section %d", (p->key_file_no));
295 yaz_log(log_level, " sort_buff at %p n=%d",
296 key_buf + ptr_top - ptr_i,ptr_i);
300 shellsort(key_buf + ptr_top - ptr_i, ptr_i,
301 sizeof(char*), key_qsort_compare);
303 qsort(key_buf + ptr_top - ptr_i, ptr_i,
304 sizeof(char*), key_qsort_compare);
306 sprintf(out_fname, "%s/key%d.tmp", p->key_tmp_dir, p->key_file_no);
308 if (!(outf = fopen (out_fname, "wb")))
310 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fopen %s", out_fname);
311 zebra_exit("key_block_flush");
313 yaz_log(YLOG_LOG, "writing section %d", p->key_file_no);
314 prevcp = cp = (key_buf)[ptr_top - ptr_i];
316 encode_key_init (&encode_info);
317 encode_key_write (cp, &encode_info, outf);
321 cp = (key_buf)[ptr_top - ptr_i];
322 if (strcmp (cp, prevcp))
324 encode_key_flush ( &encode_info, outf);
325 encode_key_init (&encode_info);
326 encode_key_write (cp, &encode_info, outf);
330 encode_key_write (cp + strlen(cp), &encode_info, outf);
332 encode_key_flush ( &encode_info, outf);
335 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fclose %s", out_fname);
336 zebra_exit("key_block_flush");
338 yaz_log(YLOG_LOG, "finished section %d", p->key_file_no);
341 void key_block_flush(zebra_key_block_t p, int is_final)
342 /* optimizing: if final=1, and no files written yet */
343 /* push the keys directly to merge, sidestepping the */
344 /* temp file altogether. Speeds small updates */
346 #if YAZ_POSIX_THREADS
352 #if YAZ_POSIX_THREADS
353 pthread_mutex_lock(&p->mutex);
355 while (p->is_sorting)
356 pthread_cond_wait(&p->cond_sorting, &p->mutex);
360 p->thread_ptr_top = p->ptr_top;
361 p->thread_ptr_i = p->ptr_i;
362 p->thread_key_buf = p->key_buf;
365 p->key_buf = p->alt_buf;
368 pthread_cond_signal(&p->work_available);
372 while (p->is_sorting)
373 pthread_cond_wait(&p->cond_sorting, &p->mutex);
375 pthread_mutex_unlock(&p->mutex);
377 key_block_flush_int(p, p->key_buf, p->ptr_top, p->ptr_i);
383 int key_block_get_no_files(zebra_key_block_t p)
386 return p->key_file_no;
393 * indent-tabs-mode: nil
395 * vim: shiftwidth=4 tabstop=8 expandtab