322de8138559418718f9a9bfa7d87ffb11b974ca
[idzebra-moved-to-github.git] / index / key_block.c
1 /* $Id: key_block.c,v 1.6 2006-11-29 10:31:29 adam Exp $
2    Copyright (C) 1995-2006
3    Index Data ApS
4
5 This file is part of the Zebra server.
6
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
10 version.
11
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
15 for more details.
16
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
20
21 */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <assert.h>
27 #include <ctype.h>
28
29 #if YAZ_POSIX_THREADS
30 #include <pthread.h>
31 #endif
32
33 #include "key_block.h"
34 #include <yaz/nmem.h>
35 #include <yaz/xmalloc.h>
36
37 struct zebra_key_block {
38     char **key_buf;
39     size_t ptr_top;
40     size_t ptr_i;
41     size_t key_buf_used;
42     int key_file_no;
43     char *key_tmp_dir;
44     int use_threads;
45     char **alt_buf;
46 #if YAZ_POSIX_THREADS
47     char **thread_key_buf;
48     size_t thread_ptr_top;
49     size_t thread_ptr_i;
50     int exit_flag;
51     pthread_t thread_id;
52     pthread_mutex_t mutex;
53
54     pthread_cond_t work_available;
55
56     pthread_cond_t cond_sorting;
57     int is_sorting;
58 #endif
59 };
60
61 #define ENCODE_BUFLEN 768
62 struct encode_info {
63     void *encode_handle;
64     void *decode_handle;
65     char buf[ENCODE_BUFLEN];
66 };
67
68 static int log_level = 0;
69
70 #define USE_SHELLSORT 0
71
72 #if USE_SHELLSORT
73 static void shellsort(void *ar, int r, size_t s,
74                       int (*cmp)(const void *a, const void *b))
75 {
76     char *a = ar;
77     char v[100];
78     int h, i, j, k;
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++)
84         { 
85             memcpy (v, a+s*i, s);
86             j = i;
87             while (j > h && (*cmp)(a + s*(j-h), v) > 0)
88             {
89                 memcpy (a + s*j, a + s*(j-h), s);
90                 j -= h;
91             }
92             memcpy (a+s*j, v, s);
93         } 
94 }
95 #endif
96
97
98 static void encode_key_init(struct encode_info *i)
99 {
100     i->encode_handle = iscz1_start();
101     i->decode_handle = iscz1_start();
102 }
103
104 static void encode_key_write (char *k, struct encode_info *i, FILE *outf)
105 {
106     struct it_key key;
107     char *bp = i->buf, *bp0;
108     const char *src = (char *) &key;
109
110     /* copy term to output buf */
111     while ((*bp++ = *k++))
112         ;
113     /* and copy & align key so we can mangle */
114     memcpy (&key, k+1, sizeof(struct it_key));  /* *k is insert/delete */
115
116 #if 0
117     /* debugging */
118     key_logdump_txt(YLOG_LOG, &key, *k ? "i" : "d");
119 #endif
120     assert(key.mem[0] >= 0);
121
122     bp0 = bp++;
123     iscz1_encode(i->encode_handle, &bp, &src);
124
125     *bp0 = (*k * 128) + bp - bp0 - 1; /* length and insert/delete combined */
126     if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
127     {
128         yaz_log (YLOG_FATAL|YLOG_ERRNO, "fwrite");
129         zebra_exit("encode_key_write");
130     }
131
132 #if 0
133     /* debugging */
134     if (1)
135     {
136         struct it_key key2;
137         const char *src = bp0+1;
138         char *dst = (char*) &key2;
139         iscz1_decode(i->decode_handle, &dst, &src);
140
141         key_logdump_txt(YLOG_LOG, &key2, *k ? "i" : "d");
142
143         assert(key2.mem[1]);
144     }
145 #endif
146 }
147
148 static void encode_key_flush (struct encode_info *i, FILE *outf)
149
150     iscz1_stop(i->encode_handle);
151     iscz1_stop(i->decode_handle);
152 }
153
154 void key_block_flush_int(zebra_key_block_t p,
155                          char **key_buf, size_t ptr_top, size_t ptr_i);
156
157 #if YAZ_POSIX_THREADS
158 static void *thread_func(void *vp)
159 {
160     zebra_key_block_t p = (zebra_key_block_t) vp;
161     while (1)
162     {
163         pthread_mutex_lock(&p->mutex);
164         
165         while (!p->is_sorting && !p->exit_flag)
166             pthread_cond_wait(&p->work_available, &p->mutex);
167
168         if (p->exit_flag)
169             break;
170             
171         pthread_mutex_unlock(&p->mutex);
172         
173         key_block_flush_int(p, p->thread_key_buf, 
174                             p->thread_ptr_top, p->thread_ptr_i);
175         
176         pthread_mutex_lock(&p->mutex);
177         p->is_sorting = 0;
178         pthread_cond_signal(&p->cond_sorting);
179         pthread_mutex_unlock(&p->mutex);        
180     }
181     pthread_mutex_unlock(&p->mutex);
182     return 0;
183 }
184 #endif
185
186 zebra_key_block_t key_block_create(int mem, const char *key_tmp_dir,
187                                    int use_threads)
188 {
189     zebra_key_block_t p = xmalloc(sizeof(*p));
190
191 #if YAZ_POSIX_THREADS
192     /* we'll be making two memory areas so cut in half */
193     if (use_threads)
194         mem = mem / 2;
195 #endif
196     p->key_buf = (char**) xmalloc (mem);
197     p->ptr_top = mem/sizeof(char*);
198     p->ptr_i = 0;
199     p->key_buf_used = 0;
200     p->key_tmp_dir = xstrdup(key_tmp_dir);
201     p->key_file_no = 0;
202     p->alt_buf = 0;
203     p->use_threads = 0;
204     if (use_threads)
205     {
206 #if YAZ_POSIX_THREADS
207         p->use_threads = use_threads;
208         p->is_sorting = 0;
209         p->exit_flag = 0;
210         pthread_mutex_init(&p->mutex, 0);
211         pthread_cond_init(&p->work_available, 0);
212         pthread_cond_init(&p->cond_sorting, 0);
213         pthread_create(&p->thread_id, 0, thread_func, p);
214         p->alt_buf = (char**) xmalloc (mem);
215 #endif
216     }
217     yaz_log(YLOG_LOG, "key_block_create t=%d", p->use_threads);
218     return p;
219 }
220
221 void key_block_destroy(zebra_key_block_t *pp)
222 {
223     zebra_key_block_t p = *pp;
224     if (p)
225     {
226         if (p->use_threads)
227         {
228 #if YAZ_POSIX_THREADS
229             pthread_mutex_lock(&p->mutex);
230             
231             while (p->is_sorting)
232                 pthread_cond_wait(&p->cond_sorting, &p->mutex);
233             
234             p->exit_flag = 1;
235             
236             pthread_cond_broadcast(&p->work_available);
237             
238             pthread_mutex_unlock(&p->mutex);
239             pthread_join(p->thread_id, 0);
240             pthread_cond_destroy(&p->work_available);
241             pthread_cond_destroy(&p->cond_sorting);
242             pthread_mutex_destroy(&p->mutex);
243             
244 #endif
245             xfree(p->alt_buf);
246         }
247         xfree(p->key_buf);
248         xfree(p->key_tmp_dir);
249         xfree(p);
250         *pp = 0;
251     }
252 }
253
254 void key_block_write(zebra_key_block_t p, zint sysno, struct it_key *key_in,
255                      int cmd, const char *str_buf, size_t str_len,
256                      zint staticrank, int static_rank_enable)
257 {
258     int ch;
259     int i, j = 0;
260     struct it_key key_out;
261
262     if (p->key_buf_used + 1024 > (p->ptr_top -p->ptr_i)*sizeof(char*))
263         key_block_flush(p, 0);
264     ++(p->ptr_i);
265     assert(p->ptr_i > 0);
266     (p->key_buf)[p->ptr_top - p->ptr_i] =
267         (char*)p->key_buf + p->key_buf_used;
268     
269     /* key_in->mem[0] ord/ch */
270     /* key_in->mem[1] filter specified record ID */
271     
272     /* encode the ordinal value (field/use/attribute) .. */
273     ch = CAST_ZINT_TO_INT(key_in->mem[0]);
274     p->key_buf_used +=
275         key_SU_encode(ch, (char*)p->key_buf +
276                       p->key_buf_used);
277     
278     /* copy the 0-terminated stuff from str to output */
279     memcpy((char*)p->key_buf + p->key_buf_used, str_buf, str_len);
280     p->key_buf_used += str_len;
281     ((char*)p->key_buf)[(p->key_buf_used)++] = '\0';
282     
283     /* the delete/insert indicator */
284     ((char*)p->key_buf)[(p->key_buf_used)++] = cmd;
285     
286     if (static_rank_enable)
287     {
288         assert(staticrank >= 0);
289         key_out.mem[j++] = staticrank;
290     }
291     
292     if (key_in->mem[1]) /* filter specified record ID */
293         key_out.mem[j++] = key_in->mem[1];
294     else
295         key_out.mem[j++] = sysno;
296     for (i = 2; i < key_in->len; i++)
297         key_out.mem[j++] = key_in->mem[i];
298     key_out.len = j;
299     
300     memcpy((char*)p->key_buf + p->key_buf_used,
301            &key_out, sizeof(key_out));
302     (p->key_buf_used) += sizeof(key_out);
303 }
304
305
306 void key_block_flush_int(zebra_key_block_t p,
307                          char **key_buf, size_t ptr_top,  size_t ptr_i)
308 {
309     FILE *outf;
310     char out_fname[200];
311     char *prevcp, *cp;
312     struct encode_info encode_info;
313
314     (p->key_file_no)++;
315     yaz_log(YLOG_LOG, "sorting section %d", (p->key_file_no));
316
317 #if USE_SHELLSORT
318     shellsort(key_buf + ptr_top - ptr_i, ptr_i,
319               sizeof(char*), key_qsort_compare);
320 #else
321     qsort(key_buf + ptr_top - ptr_i, ptr_i,
322           sizeof(char*), key_qsort_compare);
323 #endif
324     sprintf(out_fname, "%s/key%d.tmp", p->key_tmp_dir, p->key_file_no);
325
326     if (!(outf = fopen (out_fname, "wb")))
327     {
328         yaz_log (YLOG_FATAL|YLOG_ERRNO, "fopen %s", out_fname);
329         zebra_exit("key_block_flush");
330     }
331     yaz_log(YLOG_LOG, "writing section %d", p->key_file_no);
332     prevcp = cp = (key_buf)[ptr_top - ptr_i];
333     
334     encode_key_init (&encode_info);
335     encode_key_write (cp, &encode_info, outf);
336     
337     while (--ptr_i > 0)
338     {
339         cp = (key_buf)[ptr_top - ptr_i];
340         if (strcmp (cp, prevcp))
341         {
342             encode_key_flush ( &encode_info, outf);
343             encode_key_init (&encode_info);
344             encode_key_write (cp, &encode_info, outf);
345             prevcp = cp;
346         }
347         else
348             encode_key_write (cp + strlen(cp), &encode_info, outf);
349     }
350     encode_key_flush ( &encode_info, outf);
351     if (fclose (outf))
352     {
353         yaz_log (YLOG_FATAL|YLOG_ERRNO, "fclose %s", out_fname);
354         zebra_exit("key_block_flush");
355     }
356     yaz_log(YLOG_LOG, "finished section %d", p->key_file_no);
357 }
358
359 void key_block_flush(zebra_key_block_t p, int is_final)
360 {
361     if (!p)
362         return;
363
364     if (p->use_threads)
365     {
366 #if YAZ_POSIX_THREADS
367         char **tmp;
368     
369         pthread_mutex_lock(&p->mutex);
370         
371         while (p->is_sorting)
372             pthread_cond_wait(&p->cond_sorting, &p->mutex);
373         
374         p->is_sorting = 1;
375         
376         p->thread_ptr_top = p->ptr_top;
377         p->thread_ptr_i = p->ptr_i;
378         p->thread_key_buf = p->key_buf;
379         
380         tmp = p->key_buf;
381         p->key_buf = p->alt_buf;
382         p->alt_buf = tmp;
383         
384         pthread_cond_signal(&p->work_available);
385         
386         if (is_final)
387         {
388             while (p->is_sorting)
389                 pthread_cond_wait(&p->cond_sorting, &p->mutex);
390         }
391         pthread_mutex_unlock(&p->mutex);
392 #endif
393     }
394     else
395         key_block_flush_int(p, p->key_buf, p->ptr_top, p->ptr_i);
396     p->ptr_i = 0;
397     p->key_buf_used = 0;
398 }
399
400 int key_block_get_no_files(zebra_key_block_t p)
401 {
402     if (p)
403         return p->key_file_no;
404     return 0;
405 }
406
407 /*
408  * Local variables:
409  * c-basic-offset: 4
410  * indent-tabs-mode: nil
411  * End:
412  * vim: shiftwidth=4 tabstop=8 expandtab
413  */
414