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