Work on bug #550: Avoid exit. In particular the mfile/cfile/bfile has
[idzebra-moved-to-github.git] / index / recindex.c
1 /* $Id: recindex.c,v 1.53 2006-11-14 08:12:08 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 #define RIDX_CHUNK 128
24
25 /*
26  *  Format of first block
27  *      next       (8 bytes)
28  *      ref_count  (2 bytes)
29  *      block      (500 bytes)
30  *
31  *  Format of subsequent blocks 
32  *      next  (8 bytes)
33  *      block (502 bytes)
34  *
35  *  Format of each record
36  *      sysno
37  *      (length, data) - pairs
38  *      length = 0 if same as previous
39  */
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <assert.h>
43 #include <string.h>
44
45 #include <yaz/yaz-util.h>
46 #include "recindxp.h"
47
48 #if HAVE_BZLIB_H
49 #include <bzlib.h>
50 #endif
51
52 /* Modify argument to if below: 1=normal, 0=sysno testing */
53 #if 1
54 /* If this is used sysno are not converted (no testing) */
55 #define FAKE_OFFSET 0
56 #define USUAL_RANGE 6000000000LL
57
58 #else
59 /* Use a fake > 2^32 offset so we can test for proper 64-bit handling */
60 #define FAKE_OFFSET 6000000000LL
61 #define USUAL_RANGE 2000000000LL
62 #endif
63
64 static SYSNO rec_sysno_to_ext(SYSNO sysno)
65 {
66     assert(sysno >= 0 && sysno <= USUAL_RANGE);
67     return sysno + FAKE_OFFSET;
68 }
69
70 SYSNO rec_sysno_to_int(SYSNO sysno)
71 {
72     assert(sysno >= FAKE_OFFSET && sysno <= FAKE_OFFSET + USUAL_RANGE);
73     return sysno - FAKE_OFFSET;
74 }
75
76 static ZEBRA_RES rec_write_head(Records p)
77 {
78     int r;
79
80     assert(p);
81     assert(p->index_BFile);
82
83     r = bf_write(p->index_BFile, 0, 0, sizeof(p->head), &p->head);    
84     if (r)
85     {
86         yaz_log(YLOG_FATAL|YLOG_ERRNO, "write head of %s", p->index_fname);
87         return ZEBRA_FAIL;
88     }
89     return ZEBRA_OK;
90 }
91
92 static void rec_tmp_expand(Records p, int size)
93 {
94     if (p->tmp_size < size + 2048 ||
95         p->tmp_size < p->head.block_size[REC_BLOCK_TYPES-1]*2)
96     {
97         xfree(p->tmp_buf);
98         p->tmp_size = size + (int)
99                         (p->head.block_size[REC_BLOCK_TYPES-1])*2 + 2048;
100         p->tmp_buf = (char *) xmalloc(p->tmp_size);
101     }
102 }
103
104 static int read_indx(Records p, SYSNO sysno, void *buf, int itemsize, 
105                       int ignoreError)
106 {
107     int r;
108     zint pos = (sysno-1)*itemsize;
109     int off = CAST_ZINT_TO_INT(pos%RIDX_CHUNK);
110     int sz1 = RIDX_CHUNK - off;    /* sz1 is size of buffer to read.. */
111
112     if (sz1 > itemsize)
113         sz1 = itemsize;  /* no more than itemsize bytes */
114
115     r = bf_read(p->index_BFile, 1+pos/RIDX_CHUNK, off, sz1, buf);
116     if (r == 1 && sz1 < itemsize) /* boundary? - must read second part */
117         r = bf_read(p->index_BFile, 2+pos/RIDX_CHUNK, 0, itemsize - sz1,
118                         (char*) buf + sz1);
119     if (r != 1 && !ignoreError)
120     {
121         yaz_log(YLOG_FATAL|YLOG_ERRNO, "read in %s at pos %ld",
122                 p->index_fname, (long) pos);
123     }
124     return r;
125 }
126
127 static void write_indx(Records p, SYSNO sysno, void *buf, int itemsize)
128 {
129     zint pos = (sysno-1)*itemsize;
130     int off = CAST_ZINT_TO_INT(pos%RIDX_CHUNK);
131     int sz1 = RIDX_CHUNK - off;    /* sz1 is size of buffer to read.. */
132
133     if (sz1 > itemsize)
134         sz1 = itemsize;  /* no more than itemsize bytes */
135
136     bf_write(p->index_BFile, 1+pos/RIDX_CHUNK, off, sz1, buf);
137     if (sz1 < itemsize)   /* boundary? must write second part */
138         bf_write(p->index_BFile, 2+pos/RIDX_CHUNK, 0, itemsize - sz1,
139                 (char*) buf + sz1);
140 }
141
142 static ZEBRA_RES rec_release_blocks(Records p, SYSNO sysno)
143 {
144     struct record_index_entry entry;
145     zint freeblock;
146     char block_and_ref[sizeof(zint) + sizeof(short)];
147     int dst_type;
148     int first = 1;
149
150     if (read_indx(p, sysno, &entry, sizeof(entry), 1) != 1)
151         return ZEBRA_FAIL;
152
153     freeblock = entry.next;
154     assert(freeblock > 0);
155     dst_type = CAST_ZINT_TO_INT(freeblock & 7);
156     assert(dst_type < REC_BLOCK_TYPES);
157     freeblock = freeblock / 8;
158     while (freeblock)
159     {
160         if (bf_read(p->data_BFile[dst_type], freeblock, 0,
161                      first ? sizeof(block_and_ref) : sizeof(zint),
162                      block_and_ref) != 1)
163         {
164             yaz_log(YLOG_FATAL|YLOG_ERRNO, "read in rec_del_single");
165             return ZEBRA_FAIL;
166         }
167         if (first)
168         {
169             short ref;
170             memcpy(&ref, block_and_ref + sizeof(freeblock), sizeof(ref));
171             --ref;
172             memcpy(block_and_ref + sizeof(freeblock), &ref, sizeof(ref));
173             if (ref)
174             {
175                 if (bf_write(p->data_BFile[dst_type], freeblock, 0,
176                               sizeof(block_and_ref), block_and_ref))
177                 {
178                     yaz_log(YLOG_FATAL|YLOG_ERRNO, "write in rec_del_single");
179                     return ZEBRA_FAIL;
180                 }
181                 return ZEBRA_OK;
182             }
183             first = 0;
184         }
185         
186         if (bf_write(p->data_BFile[dst_type], freeblock, 0, sizeof(freeblock),
187                       &p->head.block_free[dst_type]))
188         {
189             yaz_log(YLOG_FATAL|YLOG_ERRNO, "write in rec_del_single");
190             return ZEBRA_FAIL;
191         }
192         p->head.block_free[dst_type] = freeblock;
193         memcpy(&freeblock, block_and_ref, sizeof(freeblock));
194
195         p->head.block_used[dst_type]--;
196     }
197     p->head.total_bytes -= entry.size;
198     return ZEBRA_OK;
199 }
200
201 static ZEBRA_RES rec_delete_single(Records p, Record rec)
202 {
203     struct record_index_entry entry;
204
205     if (rec_release_blocks(p, rec_sysno_to_int(rec->sysno)) != ZEBRA_OK)
206         return ZEBRA_FAIL;
207
208     entry.next = p->head.index_free;
209     entry.size = 0;
210     p->head.index_free = rec_sysno_to_int(rec->sysno);
211     write_indx(p, rec_sysno_to_int(rec->sysno), &entry, sizeof(entry));
212     return ZEBRA_OK;
213 }
214
215 static ZEBRA_RES rec_write_tmp_buf(Records p, int size, SYSNO *sysnos)
216 {
217     struct record_index_entry entry;
218     int no_written = 0;
219     char *cptr = p->tmp_buf;
220     zint block_prev = -1, block_free;
221     int dst_type = 0;
222     int i;
223
224     for (i = 1; i<REC_BLOCK_TYPES; i++)
225         if (size >= p->head.block_move[i])
226             dst_type = i;
227     while (no_written < size)
228     {
229         block_free = p->head.block_free[dst_type];
230         if (block_free)
231         {
232             if (bf_read(p->data_BFile[dst_type],
233                          block_free, 0, sizeof(*p->head.block_free),
234                          &p->head.block_free[dst_type]) != 1)
235             {
236                 yaz_log(YLOG_FATAL|YLOG_ERRNO, "read in %s at free block "
237                          ZINT_FORMAT,
238                          p->data_fname[dst_type], block_free);
239                 return ZEBRA_FAIL;
240             }
241         }
242         else
243             block_free = p->head.block_last[dst_type]++;
244         if (block_prev == -1)
245         {
246             entry.next = block_free*8 + dst_type;
247             entry.size = size;
248             p->head.total_bytes += size;
249             while (*sysnos > 0)
250             {
251                 write_indx(p, *sysnos, &entry, sizeof(entry));
252                 sysnos++;
253             }
254         }
255         else
256         {
257             memcpy(cptr, &block_free, sizeof(block_free));
258             bf_write(p->data_BFile[dst_type], block_prev, 0, 0, cptr);
259             cptr = p->tmp_buf + no_written;
260         }
261         block_prev = block_free;
262         no_written += CAST_ZINT_TO_INT(p->head.block_size[dst_type]) 
263             - sizeof(zint);
264         p->head.block_used[dst_type]++;
265     }
266     assert(block_prev != -1);
267     block_free = 0;
268     memcpy(cptr, &block_free, sizeof(block_free));
269     bf_write(p->data_BFile[dst_type], block_prev, 0,
270               sizeof(block_free) + (p->tmp_buf+size) - cptr, cptr);
271     return ZEBRA_OK;
272 }
273
274 Records rec_open(BFiles bfs, int rw, int compression_method)
275 {
276     Records p;
277     int i, r;
278     int version;
279     ZEBRA_RES ret = ZEBRA_OK;
280
281     p = (Records) xmalloc(sizeof(*p));
282     p->compression_method = compression_method;
283     p->rw = rw;
284     p->tmp_size = 1024;
285     p->index_fname = "reci";
286     p->index_BFile = bf_open(bfs, p->index_fname, RIDX_CHUNK, rw);
287     if (p->index_BFile == NULL)
288     {
289         yaz_log(YLOG_FATAL|YLOG_ERRNO, "open %s", p->index_fname);
290         xfree(p);
291         return 0;
292     }
293     p->tmp_buf = (char *) xmalloc(p->tmp_size);
294     r = bf_read(p->index_BFile, 0, 0, 0, p->tmp_buf);
295     switch (r)
296     {
297     case 0:
298         memcpy(p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic));
299         sprintf(p->head.version, "%3d", REC_VERSION);
300         p->head.index_free = 0;
301         p->head.index_last = 1;
302         p->head.no_records = 0;
303         p->head.total_bytes = 0;
304         for (i = 0; i<REC_BLOCK_TYPES; i++)
305         {
306             p->head.block_free[i] = 0;
307             p->head.block_last[i] = 1;
308             p->head.block_used[i] = 0;
309         }
310         p->head.block_size[0] = 128;
311         p->head.block_move[0] = 0;
312         for (i = 1; i<REC_BLOCK_TYPES; i++)
313         {
314             p->head.block_size[i] = p->head.block_size[i-1] * 4;
315             p->head.block_move[i] = p->head.block_size[i] * 24;
316         }
317         if (rw)
318         {
319             if (rec_write_head(p) != ZEBRA_OK)
320                 ret = ZEBRA_FAIL;
321         }
322         break;
323     case 1:
324         memcpy(&p->head, p->tmp_buf, sizeof(p->head));
325         if (memcmp(p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic)))
326         {
327             yaz_log(YLOG_FATAL, "file %s has bad format", p->index_fname);
328             ret = ZEBRA_FAIL;
329         }
330         version = atoi(p->head.version);
331         if (version != REC_VERSION)
332         {
333             yaz_log(YLOG_FATAL, "file %s is version %d, but version"
334                   " %d is required", p->index_fname, version, REC_VERSION);
335             ret = ZEBRA_FAIL;
336         }
337         break;
338     }
339     for (i = 0; i<REC_BLOCK_TYPES; i++)
340     {
341         char str[80];
342         sprintf(str, "recd%c", i + 'A');
343         p->data_fname[i] = (char *) xmalloc(strlen(str)+1);
344         strcpy(p->data_fname[i], str);
345         p->data_BFile[i] = NULL;
346     }
347     for (i = 0; i<REC_BLOCK_TYPES; i++)
348     {
349         if (!(p->data_BFile[i] =
350               bf_open(bfs, p->data_fname[i],
351                       CAST_ZINT_TO_INT(p->head.block_size[i]), rw)))
352         {
353             yaz_log(YLOG_FATAL|YLOG_ERRNO, "bf_open %s", p->data_fname[i]);
354             ret = ZEBRA_FAIL;
355             break;
356         }
357     }
358     p->cache_max = 400;
359     p->cache_cur = 0;
360     p->record_cache = (struct record_cache_entry *)
361         xmalloc(sizeof(*p->record_cache)*p->cache_max);
362     zebra_mutex_init(&p->mutex);
363     if (ret == ZEBRA_FAIL)
364         rec_close(&p);
365     return p;
366 }
367
368 static void rec_encode_unsigned(unsigned n, unsigned char *buf, int *len)
369 {
370     (*len) = 0;
371     while (n > 127)
372     {
373         buf[*len] = 128 + (n & 127);
374         n = n >> 7;
375         (*len)++;
376     }
377     buf[*len] = n;
378     (*len)++;
379 }
380
381 static void rec_decode_unsigned(unsigned *np, unsigned char *buf, int *len)
382 {
383     unsigned n = 0;
384     unsigned w = 1;
385     (*len) = 0;
386
387     while (buf[*len] > 127)
388     {
389         n += w*(buf[*len] & 127);
390         w = w << 7;
391         (*len)++;
392     }
393     n += w * buf[*len];
394     (*len)++;
395     *np = n;
396 }
397
398 static void rec_encode_zint(zint n, unsigned char *buf, int *len)
399 {
400     (*len) = 0;
401     while (n > 127)
402     {
403         buf[*len] = (unsigned) (128 + (n & 127));
404         n = n >> 7;
405         (*len)++;
406     }
407     buf[*len] = (unsigned) n;
408     (*len)++;
409 }
410
411 static void rec_decode_zint(zint *np, unsigned char *buf, int *len)
412 {
413     zint  n = 0;
414     zint w = 1;
415     (*len) = 0;
416
417     while (buf[*len] > 127)
418     {
419         n += w*(buf[*len] & 127);
420         w = w << 7;
421         (*len)++;
422     }
423     n += w * buf[*len];
424     (*len)++;
425     *np = n;
426 }
427
428 static void rec_cache_flush_block1(Records p, Record rec, Record last_rec,
429                                    char **out_buf, int *out_size,
430                                    int *out_offset)
431 {
432     int i;
433     int len;
434
435     for (i = 0; i<REC_NO_INFO; i++)
436     {
437         if (*out_offset + CAST_ZINT_TO_INT(rec->size[i]) + 20 > *out_size)
438         {
439             int new_size = *out_offset + rec->size[i] + 65536;
440             char *np = (char *) xmalloc(new_size);
441             if (*out_offset)
442                 memcpy(np, *out_buf, *out_offset);
443             xfree(*out_buf);
444             *out_size = new_size;
445             *out_buf = np;
446         }
447         if (i == 0)
448         {
449             rec_encode_zint(rec_sysno_to_int(rec->sysno), 
450                             (unsigned char *) *out_buf + *out_offset, &len);
451             (*out_offset) += len;
452         }
453         if (rec->size[i] == 0)
454         {
455             rec_encode_unsigned(1, (unsigned char *) *out_buf + *out_offset,
456                                 &len);
457             (*out_offset) += len;
458         }
459         else if (last_rec && rec->size[i] == last_rec->size[i] &&
460                  !memcmp(rec->info[i], last_rec->info[i], rec->size[i]))
461         {
462             rec_encode_unsigned(0, (unsigned char *) *out_buf + *out_offset,
463                                 &len);
464             (*out_offset) += len;
465         }
466         else
467         {
468             rec_encode_unsigned(rec->size[i]+1,
469                                 (unsigned char *) *out_buf + *out_offset,
470                                 &len);
471             (*out_offset) += len;
472             memcpy(*out_buf + *out_offset, rec->info[i], rec->size[i]);
473             (*out_offset) += rec->size[i];
474         }
475     }
476 }
477
478 static ZEBRA_RES rec_write_multiple(Records p, int saveCount)
479 {
480     int i;
481     short ref_count = 0;
482     char compression_method;
483     Record last_rec = 0;
484     int out_size = 1000;
485     int out_offset = 0;
486     char *out_buf = (char *) xmalloc(out_size);
487     SYSNO *sysnos = (SYSNO *) xmalloc(sizeof(*sysnos) * (p->cache_cur + 1));
488     SYSNO *sysnop = sysnos;
489     ZEBRA_RES ret = ZEBRA_OK;
490
491     for (i = 0; i<p->cache_cur - saveCount; i++)
492     {
493         struct record_cache_entry *e = p->record_cache + i;
494         switch (e->flag)
495         {
496         case recordFlagNew:
497             rec_cache_flush_block1(p, e->rec, last_rec, &out_buf,
498                                     &out_size, &out_offset);
499             *sysnop++ = rec_sysno_to_int(e->rec->sysno);
500             ref_count++;
501             e->flag = recordFlagNop;
502             last_rec = e->rec;
503             break;
504         case recordFlagWrite:
505             if (rec_release_blocks(p, rec_sysno_to_int(e->rec->sysno))
506                 != ZEBRA_OK)
507                 ret = ZEBRA_FAIL;
508
509             rec_cache_flush_block1(p, e->rec, last_rec, &out_buf,
510                                     &out_size, &out_offset);
511             *sysnop++ = rec_sysno_to_int(e->rec->sysno);
512             ref_count++;
513             e->flag = recordFlagNop;
514             last_rec = e->rec;
515             break;
516         case recordFlagDelete:
517             if (rec_delete_single(p, e->rec) != ZEBRA_OK)
518                 ret = ZEBRA_FAIL;
519
520             e->flag = recordFlagNop;
521             break;
522         default:
523             break;
524         }
525     }
526
527     *sysnop = -1;
528     if (ref_count)
529     {
530         unsigned int csize = 0;  /* indicate compression "not performed yet" */
531         compression_method = p->compression_method;
532         switch (compression_method)
533         {
534         case REC_COMPRESS_BZIP2:
535 #if HAVE_BZLIB_H        
536             csize = out_offset + (out_offset >> 6) + 620;
537             rec_tmp_expand(p, csize);
538 #ifdef BZ_CONFIG_ERROR
539             i = BZ2_bzBuffToBuffCompress 
540 #else
541             i = bzBuffToBuffCompress 
542 #endif
543                                     (p->tmp_buf+sizeof(zint)+sizeof(short)+
544                                       sizeof(char),
545                                       &csize, out_buf, out_offset, 1, 0, 30);
546             if (i != BZ_OK)
547             {
548                 yaz_log(YLOG_WARN, "bzBuffToBuffCompress error code=%d", i);
549                 csize = 0;
550             }
551             yaz_log(YLOG_LOG, "compress %4d %5d %5d", ref_count, out_offset,
552                   csize);
553 #endif
554             break;
555         case REC_COMPRESS_NONE:
556             break;
557         }
558         if (!csize)  
559         {
560             /* either no compression or compression not supported ... */
561             csize = out_offset;
562             rec_tmp_expand(p, csize);
563             memcpy(p->tmp_buf + sizeof(zint) + sizeof(short) + sizeof(char),
564                     out_buf, out_offset);
565             csize = out_offset;
566             compression_method = REC_COMPRESS_NONE;
567         }
568         memcpy(p->tmp_buf + sizeof(zint), &ref_count, sizeof(ref_count));
569         memcpy(p->tmp_buf + sizeof(zint)+sizeof(short),
570                 &compression_method, sizeof(compression_method));
571                 
572         /* -------- compression */
573         if (rec_write_tmp_buf(p, csize + sizeof(short) + sizeof(char), sysnos)
574             != ZEBRA_OK)
575             ret = ZEBRA_FAIL;
576     }
577     xfree(out_buf);
578     xfree(sysnos);
579     return ret;
580 }
581
582 static ZEBRA_RES rec_cache_flush(Records p, int saveCount)
583 {
584     int i, j;
585     ZEBRA_RES ret;
586
587     if (saveCount >= p->cache_cur)
588         saveCount = 0;
589
590     ret = rec_write_multiple(p, saveCount);
591
592     for (i = 0; i<p->cache_cur - saveCount; i++)
593     {
594         struct record_cache_entry *e = p->record_cache + i;
595         rec_free(&e->rec);
596     } 
597     /* i still being used ... */
598     for (j = 0; j<saveCount; j++, i++)
599         memcpy(p->record_cache+j, p->record_cache+i,
600                 sizeof(*p->record_cache));
601     p->cache_cur = saveCount;
602     return ret;
603 }
604
605 static Record *rec_cache_lookup(Records p, SYSNO sysno,
606                                 enum recordCacheFlag flag)
607 {
608     int i;
609     for (i = 0; i<p->cache_cur; i++)
610     {
611         struct record_cache_entry *e = p->record_cache + i;
612         if (e->rec->sysno == sysno)
613         {
614             if (flag != recordFlagNop && e->flag == recordFlagNop)
615                 e->flag = flag;
616             return &e->rec;
617         }
618     }
619     return NULL;
620 }
621
622 static ZEBRA_RES rec_cache_insert(Records p, Record rec, enum recordCacheFlag flag)
623 {
624     struct record_cache_entry *e;
625     ZEBRA_RES ret = ZEBRA_OK;
626
627     if (p->cache_cur == p->cache_max)
628         ret = rec_cache_flush(p, 1);
629     else if (p->cache_cur > 0)
630     {
631         int i, j;
632         int used = 0;
633         for (i = 0; i<p->cache_cur; i++)
634         {
635             Record r = (p->record_cache + i)->rec;
636             for (j = 0; j<REC_NO_INFO; j++)
637                 used += r->size[j];
638         }
639         if (used > 90000)
640             ret = rec_cache_flush(p, 1);
641     }
642     assert(p->cache_cur < p->cache_max);
643
644     e = p->record_cache + (p->cache_cur)++;
645     e->flag = flag;
646     e->rec = rec_cp(rec);
647     return ret;
648 }
649
650 ZEBRA_RES rec_close(Records *pp)
651 {
652     Records p = *pp;
653     int i;
654     ZEBRA_RES ret = ZEBRA_OK;
655
656     if (!p)
657         return ret;
658
659     zebra_mutex_destroy(&p->mutex);
660     if (rec_cache_flush(p, 0) != ZEBRA_OK)
661         ret = ZEBRA_FAIL;
662
663     xfree(p->record_cache);
664
665     if (p->rw)
666     {
667         if (rec_write_head(p) != ZEBRA_OK)
668             ret = ZEBRA_FAIL;
669     }
670
671     if (p->index_BFile)
672         bf_close(p->index_BFile);
673
674     for (i = 0; i<REC_BLOCK_TYPES; i++)
675     {
676         if (p->data_BFile[i])
677             bf_close(p->data_BFile[i]);
678         xfree(p->data_fname[i]);
679     }
680     xfree(p->tmp_buf);
681     xfree(p);
682     *pp = NULL;
683     return ret;
684 }
685
686 static Record rec_get_int(Records p, SYSNO sysno)
687 {
688     int i, in_size, r;
689     Record rec, *recp;
690     struct record_index_entry entry;
691     zint freeblock;
692     int dst_type;
693     char *nptr, *cptr;
694     char *in_buf = 0;
695     char *bz_buf = 0;
696 #if HAVE_BZLIB_H
697     unsigned int bz_size;
698 #endif
699     char compression_method;
700
701     assert(sysno > 0);
702     assert(p);
703
704     if ((recp = rec_cache_lookup(p, sysno, recordFlagNop)))
705         return rec_cp(*recp);
706
707     if (read_indx(p, rec_sysno_to_int(sysno), &entry, sizeof(entry), 1) < 1)
708         return NULL;       /* record is not there! */
709
710     if (!entry.size)
711         return NULL;       /* record is deleted */
712
713     dst_type = (int) (entry.next & 7);
714     assert(dst_type < REC_BLOCK_TYPES);
715     freeblock = entry.next / 8;
716
717     assert(freeblock > 0);
718     
719     rec_tmp_expand(p, entry.size);
720
721     cptr = p->tmp_buf;
722     r = bf_read(p->data_BFile[dst_type], freeblock, 0, 0, cptr);
723     if (r < 0)
724         return 0;
725     memcpy(&freeblock, cptr, sizeof(freeblock));
726
727     while (freeblock)
728     {
729         zint tmp;
730
731         cptr += p->head.block_size[dst_type] - sizeof(freeblock);
732         
733         memcpy(&tmp, cptr, sizeof(tmp));
734         r = bf_read(p->data_BFile[dst_type], freeblock, 0, 0, cptr);
735         if (r < 0)
736             return 0;
737         memcpy(&freeblock, cptr, sizeof(freeblock));
738         memcpy(cptr, &tmp, sizeof(tmp));
739     }
740
741     rec = (Record) xmalloc(sizeof(*rec));
742     rec->sysno = sysno;
743     memcpy(&compression_method, p->tmp_buf + sizeof(zint) + sizeof(short),
744             sizeof(compression_method));
745     in_buf = p->tmp_buf + sizeof(zint) + sizeof(short) + sizeof(char);
746     in_size = entry.size - sizeof(short) - sizeof(char);
747     switch (compression_method)
748     {
749     case REC_COMPRESS_BZIP2:
750 #if HAVE_BZLIB_H
751         bz_size = entry.size * 20 + 100;
752         while (1)
753         {
754             bz_buf = (char *) xmalloc(bz_size);
755 #ifdef BZ_CONFIG_ERROR
756             i = BZ2_bzBuffToBuffDecompress
757 #else
758             i = bzBuffToBuffDecompress
759 #endif
760                 (bz_buf, &bz_size, in_buf, in_size, 0, 0);
761             yaz_log(YLOG_LOG, "decompress %5d %5d", in_size, bz_size);
762             if (i == BZ_OK)
763                 break;
764             yaz_log(YLOG_LOG, "failed");
765             xfree(bz_buf);
766             bz_size *= 2;
767         }
768         in_buf = bz_buf;
769         in_size = bz_size;
770 #else
771         yaz_log(YLOG_FATAL, "cannot decompress record(s) in BZIP2 format");
772         return 0;
773 #endif
774         break;
775     case REC_COMPRESS_NONE:
776         break;
777     }
778     for (i = 0; i<REC_NO_INFO; i++)
779         rec->info[i] = 0;
780
781     nptr = in_buf;                /* skip ref count */
782     while (nptr < in_buf + in_size)
783     {
784         zint this_sysno;
785         int len;
786         rec_decode_zint(&this_sysno, (unsigned char *) nptr, &len);
787         nptr += len;
788
789         for (i = 0; i < REC_NO_INFO; i++)
790         {
791             unsigned int this_size;
792             rec_decode_unsigned(&this_size, (unsigned char *) nptr, &len);
793             nptr += len;
794
795             if (this_size == 0)
796                 continue;
797             rec->size[i] = this_size-1;
798
799             if (rec->size[i])
800             {
801                 rec->info[i] = nptr;
802                 nptr += rec->size[i];
803             }
804             else
805                 rec->info[i] = NULL;
806         }
807         if (this_sysno == rec_sysno_to_int(sysno))
808             break;
809     }
810     for (i = 0; i<REC_NO_INFO; i++)
811     {
812         if (rec->info[i] && rec->size[i])
813         {
814             char *np = xmalloc(rec->size[i]+1);
815             memcpy(np, rec->info[i], rec->size[i]);
816             np[rec->size[i]] = '\0';
817             rec->info[i] = np;
818         }
819         else
820         {
821             assert(rec->info[i] == 0);
822             assert(rec->size[i] == 0);
823         }
824     }
825     xfree(bz_buf);
826     if (rec_cache_insert(p, rec, recordFlagNop) != ZEBRA_OK)
827         return 0;
828     return rec;
829 }
830
831 Record rec_get(Records p, SYSNO sysno)
832 {
833     Record rec;
834     zebra_mutex_lock(&p->mutex);
835
836     rec = rec_get_int(p, sysno);
837     zebra_mutex_unlock(&p->mutex);
838     return rec;
839 }
840
841 Record rec_get_root(Records p)
842 {
843     return rec_get(p, rec_sysno_to_ext(1));
844 }
845
846 static Record rec_new_int(Records p)
847 {
848     int i;
849     SYSNO sysno;
850     Record rec;
851
852     assert(p);
853     rec = (Record) xmalloc(sizeof(*rec));
854     if (1 || p->head.index_free == 0)
855         sysno = (p->head.index_last)++;
856     else
857     {
858         struct record_index_entry entry;
859
860         if (read_indx(p, p->head.index_free, &entry, sizeof(entry), 0) < 1)
861         {
862             xfree(rec);
863             return 0;
864         }
865         sysno = p->head.index_free;
866         p->head.index_free = entry.next;
867     }
868     (p->head.no_records)++;
869     rec->sysno = rec_sysno_to_ext(sysno);
870     for (i = 0; i < REC_NO_INFO; i++)
871     {
872         rec->info[i] = NULL;
873         rec->size[i] = 0;
874     }
875     rec_cache_insert(p, rec, recordFlagNew);
876     return rec;
877 }
878
879 Record rec_new(Records p)
880 {
881     Record rec;
882     zebra_mutex_lock(&p->mutex);
883
884     rec = rec_new_int(p);
885     zebra_mutex_unlock(&p->mutex);
886     return rec;
887 }
888
889 ZEBRA_RES rec_del(Records p, Record *recpp)
890 {
891     Record *recp;
892     ZEBRA_RES ret = ZEBRA_OK;
893
894     zebra_mutex_lock(&p->mutex);
895     (p->head.no_records)--;
896     if ((recp = rec_cache_lookup(p, (*recpp)->sysno, recordFlagDelete)))
897     {
898         rec_free(recp);
899         *recp = *recpp;
900     }
901     else
902     {
903         ret = rec_cache_insert(p, *recpp, recordFlagDelete);
904         rec_free(recpp);
905     }
906     zebra_mutex_unlock(&p->mutex);
907     *recpp = NULL;
908     return ret;
909 }
910
911 ZEBRA_RES rec_put(Records p, Record *recpp)
912 {
913     Record *recp;
914     ZEBRA_RES ret = ZEBRA_OK;
915
916     zebra_mutex_lock(&p->mutex);
917     if ((recp = rec_cache_lookup(p, (*recpp)->sysno, recordFlagWrite)))
918     {
919         rec_free(recp);
920         *recp = *recpp;
921     }
922     else
923     {
924         ret = rec_cache_insert(p, *recpp, recordFlagWrite);
925         rec_free(recpp);
926     }
927     zebra_mutex_unlock(&p->mutex);
928     *recpp = NULL;
929     return ret;
930 }
931
932 void rec_free(Record *recpp)
933 {
934     int i;
935
936     if (!*recpp)
937         return ;
938     for (i = 0; i < REC_NO_INFO; i++)
939         xfree((*recpp)->info[i]);
940     xfree(*recpp);
941     *recpp = NULL;
942 }
943
944 Record rec_cp(Record rec)
945 {
946     Record n;
947     int i;
948
949     n = (Record) xmalloc(sizeof(*n));
950     n->sysno = rec->sysno;
951     for (i = 0; i < REC_NO_INFO; i++)
952         if (!rec->info[i])
953         {
954             n->info[i] = NULL;
955             n->size[i] = 0;
956         }
957         else
958         {
959             n->size[i] = rec->size[i];
960             n->info[i] = (char *) xmalloc(rec->size[i]+1);
961             memcpy(n->info[i], rec->info[i], rec->size[i]);
962             n->info[i][rec->size[i]] = '\0';
963         }
964     return n;
965 }
966
967
968 char *rec_strdup(const char *s, size_t *len)
969 {
970     char *p;
971
972     if (!s)
973     {
974         *len = 0;
975         return NULL;
976     }
977     *len = strlen(s)+1;
978     p = (char *) xmalloc(*len);
979     strcpy(p, s);
980     return p;
981 }
982
983 /*
984  * Local variables:
985  * c-basic-offset: 4
986  * indent-tabs-mode: nil
987  * End:
988  * vim: shiftwidth=4 tabstop=8 expandtab
989  */
990