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