bef50340148a1015468077c5873dedc3ae4bb1af
[idzebra-moved-to-github.git] / index / recindex.c
1 /* $Id: recindex.c,v 1.47 2006-03-26 14:17:01 adam Exp $
2    Copyright (C) 1995-2005
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 Zebra; see the file LICENSE.zebra.  If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
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 = (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 = (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 = (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 += (int)(p->head.block_size[dst_type]) - sizeof(zint);
263         p->head.block_used[dst_type]++;
264     }
265     assert(block_prev != -1);
266     block_free = 0;
267     memcpy(cptr, &block_free, sizeof(block_free));
268     bf_write(p->data_BFile[dst_type], block_prev, 0,
269               sizeof(block_free) + (p->tmp_buf+size) - cptr, cptr);
270     return ZEBRA_OK;
271 }
272
273 Records rec_open(BFiles bfs, int rw, int compression_method)
274 {
275     Records p;
276     int i, r;
277     int version;
278     ZEBRA_RES ret = ZEBRA_OK;
279
280     p = (Records) xmalloc(sizeof(*p));
281     p->compression_method = compression_method;
282     p->rw = rw;
283     p->tmp_size = 1024;
284     p->index_fname = "reci";
285     p->index_BFile = bf_open(bfs, p->index_fname, RIDX_CHUNK, rw);
286     if (p->index_BFile == NULL)
287     {
288         yaz_log(YLOG_FATAL|YLOG_ERRNO, "open %s", p->index_fname);
289         xfree(p);
290         return 0;
291     }
292     p->tmp_buf = (char *) xmalloc(p->tmp_size);
293     r = bf_read(p->index_BFile, 0, 0, 0, p->tmp_buf);
294     switch (r)
295     {
296     case 0:
297         memcpy(p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic));
298         sprintf(p->head.version, "%3d", REC_VERSION);
299         p->head.index_free = 0;
300         p->head.index_last = 1;
301         p->head.no_records = 0;
302         p->head.total_bytes = 0;
303         for (i = 0; i<REC_BLOCK_TYPES; i++)
304         {
305             p->head.block_free[i] = 0;
306             p->head.block_last[i] = 1;
307             p->head.block_used[i] = 0;
308         }
309         p->head.block_size[0] = 128;
310         p->head.block_move[0] = 0;
311         for (i = 1; i<REC_BLOCK_TYPES; i++)
312         {
313             p->head.block_size[i] = p->head.block_size[i-1] * 4;
314             p->head.block_move[i] = p->head.block_size[i] * 24;
315         }
316         if (rw)
317         {
318             if (rec_write_head(p) != ZEBRA_OK)
319                 ret = ZEBRA_FAIL;
320         }
321         break;
322     case 1:
323         memcpy(&p->head, p->tmp_buf, sizeof(p->head));
324         if (memcmp(p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic)))
325         {
326             yaz_log(YLOG_FATAL, "file %s has bad format", p->index_fname);
327             ret = ZEBRA_FAIL;
328         }
329         version = atoi(p->head.version);
330         if (version != REC_VERSION)
331         {
332             yaz_log(YLOG_FATAL, "file %s is version %d, but version"
333                   " %d is required", p->index_fname, version, REC_VERSION);
334             ret = ZEBRA_FAIL;
335         }
336         break;
337     }
338     for (i = 0; i<REC_BLOCK_TYPES; i++)
339     {
340         char str[80];
341         sprintf(str, "recd%c", i + 'A');
342         p->data_fname[i] = (char *) xmalloc(strlen(str)+1);
343         strcpy(p->data_fname[i], str);
344         p->data_BFile[i] = NULL;
345     }
346     for (i = 0; i<REC_BLOCK_TYPES; i++)
347     {
348         if (!(p->data_BFile[i] = bf_open(bfs, p->data_fname[i],
349                                          (int) (p->head.block_size[i]),
350                                          rw)))
351         {
352             yaz_log(YLOG_FATAL|YLOG_ERRNO, "bf_open %s", p->data_fname[i]);
353             ret = ZEBRA_FAIL;
354         }
355     }
356     p->cache_max = 400;
357     p->cache_cur = 0;
358     p->record_cache = (struct record_cache_entry *)
359         xmalloc(sizeof(*p->record_cache)*p->cache_max);
360     zebra_mutex_init(&p->mutex);
361     if (ret == ZEBRA_FAIL)
362         rec_close(&p);
363     return p;
364 }
365
366 static void rec_encode_unsigned(unsigned n, unsigned char *buf, int *len)
367 {
368     (*len) = 0;
369     while (n > 127)
370     {
371         buf[*len] = 128 + (n & 127);
372         n = n >> 7;
373         (*len)++;
374     }
375     buf[*len] = n;
376     (*len)++;
377 }
378
379 static void rec_decode_unsigned(unsigned *np, unsigned char *buf, int *len)
380 {
381     unsigned n = 0;
382     unsigned w = 1;
383     (*len) = 0;
384
385     while (buf[*len] > 127)
386     {
387         n += w*(buf[*len] & 127);
388         w = w << 7;
389         (*len)++;
390     }
391     n += w * buf[*len];
392     (*len)++;
393     *np = n;
394 }
395
396 static void rec_encode_zint(zint n, unsigned char *buf, int *len)
397 {
398     (*len) = 0;
399     while (n > 127)
400     {
401         buf[*len] = (unsigned) (128 + (n & 127));
402         n = n >> 7;
403         (*len)++;
404     }
405     buf[*len] = (unsigned) n;
406     (*len)++;
407 }
408
409 static void rec_decode_zint(zint *np, unsigned char *buf, int *len)
410 {
411     zint  n = 0;
412     zint w = 1;
413     (*len) = 0;
414
415     while (buf[*len] > 127)
416     {
417         n += w*(buf[*len] & 127);
418         w = w << 7;
419         (*len)++;
420     }
421     n += w * buf[*len];
422     (*len)++;
423     *np = n;
424 }
425
426 static void rec_cache_flush_block1(Records p, Record rec, Record last_rec,
427                                    char **out_buf, int *out_size,
428                                    int *out_offset)
429 {
430     int i;
431     int len;
432
433     for (i = 0; i<REC_NO_INFO; i++)
434     {
435         if (*out_offset + (int) rec->size[i] + 20 > *out_size)
436         {
437             int new_size = *out_offset + rec->size[i] + 65536;
438             char *np = (char *) xmalloc(new_size);
439             if (*out_offset)
440                 memcpy(np, *out_buf, *out_offset);
441             xfree(*out_buf);
442             *out_size = new_size;
443             *out_buf = np;
444         }
445         if (i == 0)
446         {
447             rec_encode_zint(rec_sysno_to_int(rec->sysno), 
448                             (unsigned char *) *out_buf + *out_offset, &len);
449             (*out_offset) += len;
450         }
451         if (rec->size[i] == 0)
452         {
453             rec_encode_unsigned(1, (unsigned char *) *out_buf + *out_offset,
454                                 &len);
455             (*out_offset) += len;
456         }
457         else if (last_rec && rec->size[i] == last_rec->size[i] &&
458                  !memcmp(rec->info[i], last_rec->info[i], rec->size[i]))
459         {
460             rec_encode_unsigned(0, (unsigned char *) *out_buf + *out_offset,
461                                 &len);
462             (*out_offset) += len;
463         }
464         else
465         {
466             rec_encode_unsigned(rec->size[i]+1,
467                                 (unsigned char *) *out_buf + *out_offset,
468                                 &len);
469             (*out_offset) += len;
470             memcpy(*out_buf + *out_offset, rec->info[i], rec->size[i]);
471             (*out_offset) += rec->size[i];
472         }
473     }
474 }
475
476 static ZEBRA_RES rec_write_multiple(Records p, int saveCount)
477 {
478     int i;
479     short ref_count = 0;
480     char compression_method;
481     Record last_rec = 0;
482     int out_size = 1000;
483     int out_offset = 0;
484     char *out_buf = (char *) xmalloc(out_size);
485     SYSNO *sysnos = (SYSNO *) xmalloc(sizeof(*sysnos) * (p->cache_cur + 1));
486     SYSNO *sysnop = sysnos;
487     ZEBRA_RES ret = ZEBRA_OK;
488
489     for (i = 0; i<p->cache_cur - saveCount; i++)
490     {
491         struct record_cache_entry *e = p->record_cache + i;
492         switch (e->flag)
493         {
494         case recordFlagNew:
495             rec_cache_flush_block1(p, e->rec, last_rec, &out_buf,
496                                     &out_size, &out_offset);
497             *sysnop++ = rec_sysno_to_int(e->rec->sysno);
498             ref_count++;
499             e->flag = recordFlagNop;
500             last_rec = e->rec;
501             break;
502         case recordFlagWrite:
503             if (rec_release_blocks(p, rec_sysno_to_int(e->rec->sysno))
504                 != ZEBRA_OK)
505                 ret = ZEBRA_FAIL;
506
507             rec_cache_flush_block1(p, e->rec, last_rec, &out_buf,
508                                     &out_size, &out_offset);
509             *sysnop++ = rec_sysno_to_int(e->rec->sysno);
510             ref_count++;
511             e->flag = recordFlagNop;
512             last_rec = e->rec;
513             break;
514         case recordFlagDelete:
515             if (rec_delete_single(p, e->rec) != ZEBRA_OK)
516                 ret = ZEBRA_FAIL;
517
518             e->flag = recordFlagNop;
519             break;
520         default:
521             break;
522         }
523     }
524
525     *sysnop = -1;
526     if (ref_count)
527     {
528         unsigned int csize = 0;  /* indicate compression "not performed yet" */
529         compression_method = p->compression_method;
530         switch (compression_method)
531         {
532         case REC_COMPRESS_BZIP2:
533 #if HAVE_BZLIB_H        
534             csize = out_offset + (out_offset >> 6) + 620;
535             rec_tmp_expand(p, csize);
536 #ifdef BZ_CONFIG_ERROR
537             i = BZ2_bzBuffToBuffCompress 
538 #else
539             i = bzBuffToBuffCompress 
540 #endif
541                                     (p->tmp_buf+sizeof(zint)+sizeof(short)+
542                                       sizeof(char),
543                                       &csize, out_buf, out_offset, 1, 0, 30);
544             if (i != BZ_OK)
545             {
546                 yaz_log(YLOG_WARN, "bzBuffToBuffCompress error code=%d", i);
547                 csize = 0;
548             }
549             yaz_log(YLOG_LOG, "compress %4d %5d %5d", ref_count, out_offset,
550                   csize);
551 #endif
552             break;
553         case REC_COMPRESS_NONE:
554             break;
555         }
556         if (!csize)  
557         {
558             /* either no compression or compression not supported ... */
559             csize = out_offset;
560             rec_tmp_expand(p, csize);
561             memcpy(p->tmp_buf + sizeof(zint) + sizeof(short) + sizeof(char),
562                     out_buf, out_offset);
563             csize = out_offset;
564             compression_method = REC_COMPRESS_NONE;
565         }
566         memcpy(p->tmp_buf + sizeof(zint), &ref_count, sizeof(ref_count));
567         memcpy(p->tmp_buf + sizeof(zint)+sizeof(short),
568                 &compression_method, sizeof(compression_method));
569                 
570         /* -------- compression */
571         if (rec_write_tmp_buf(p, csize + sizeof(short) + sizeof(char), sysnos)
572             != ZEBRA_OK)
573             ret = ZEBRA_FAIL;
574     }
575     xfree(out_buf);
576     xfree(sysnos);
577     return ret;
578 }
579
580 static ZEBRA_RES rec_cache_flush(Records p, int saveCount)
581 {
582     int i, j;
583     ZEBRA_RES ret;
584
585     if (saveCount >= p->cache_cur)
586         saveCount = 0;
587
588     ret = rec_write_multiple(p, saveCount);
589
590     for (i = 0; i<p->cache_cur - saveCount; i++)
591     {
592         struct record_cache_entry *e = p->record_cache + i;
593         rec_rm(&e->rec);
594     } 
595     /* i still being used ... */
596     for (j = 0; j<saveCount; j++, i++)
597         memcpy(p->record_cache+j, p->record_cache+i,
598                 sizeof(*p->record_cache));
599     p->cache_cur = saveCount;
600     return ret;
601 }
602
603 static Record *rec_cache_lookup(Records p, SYSNO sysno,
604                                 enum recordCacheFlag flag)
605 {
606     int i;
607     for (i = 0; i<p->cache_cur; i++)
608     {
609         struct record_cache_entry *e = p->record_cache + i;
610         if (e->rec->sysno == sysno)
611         {
612             if (flag != recordFlagNop && e->flag == recordFlagNop)
613                 e->flag = flag;
614             return &e->rec;
615         }
616     }
617     return NULL;
618 }
619
620 static ZEBRA_RES rec_cache_insert(Records p, Record rec, enum recordCacheFlag flag)
621 {
622     struct record_cache_entry *e;
623     ZEBRA_RES ret = ZEBRA_OK;
624
625     if (p->cache_cur == p->cache_max)
626         ret = rec_cache_flush(p, 1);
627     else if (p->cache_cur > 0)
628     {
629         int i, j;
630         int used = 0;
631         for (i = 0; i<p->cache_cur; i++)
632         {
633             Record r = (p->record_cache + i)->rec;
634             for (j = 0; j<REC_NO_INFO; j++)
635                 used += r->size[j];
636         }
637         if (used > 90000)
638             ret = rec_cache_flush(p, 1);
639     }
640     assert(p->cache_cur < p->cache_max);
641
642     e = p->record_cache + (p->cache_cur)++;
643     e->flag = flag;
644     e->rec = rec_cp(rec);
645     return ret;
646 }
647
648 ZEBRA_RES rec_close(Records *pp)
649 {
650     Records p = *pp;
651     int i;
652     ZEBRA_RES ret = ZEBRA_OK;
653
654     if (!p)
655         return ret;
656
657     zebra_mutex_destroy(&p->mutex);
658     if (rec_cache_flush(p, 0) != ZEBRA_OK)
659         ret = ZEBRA_FAIL;
660
661     xfree(p->record_cache);
662
663     if (p->rw)
664     {
665         if (rec_write_head(p) != ZEBRA_OK)
666             ret = ZEBRA_FAIL;
667     }
668
669     if (p->index_BFile)
670         bf_close(p->index_BFile);
671
672     for (i = 0; i<REC_BLOCK_TYPES; i++)
673     {
674         if (p->data_BFile[i])
675             bf_close(p->data_BFile[i]);
676         xfree(p->data_fname[i]);
677     }
678     xfree(p->tmp_buf);
679     xfree(p);
680     *pp = NULL;
681     return ret;
682 }
683
684 static Record rec_get_int(Records p, SYSNO sysno)
685 {
686     int i, in_size, r;
687     Record rec, *recp;
688     struct record_index_entry entry;
689     zint freeblock;
690     int dst_type;
691     char *nptr, *cptr;
692     char *in_buf = 0;
693     char *bz_buf = 0;
694 #if HAVE_BZLIB_H
695     unsigned int bz_size;
696 #endif
697     char compression_method;
698
699     assert(sysno > 0);
700     assert(p);
701
702     if ((recp = rec_cache_lookup(p, sysno, recordFlagNop)))
703         return rec_cp(*recp);
704
705     if (read_indx(p, rec_sysno_to_int(sysno), &entry, sizeof(entry), 1) < 1)
706         return NULL;       /* record is not there! */
707
708     if (!entry.size)
709         return NULL;       /* record is deleted */
710
711     dst_type = (int) (entry.next & 7);
712     assert(dst_type < REC_BLOCK_TYPES);
713     freeblock = entry.next / 8;
714
715     assert(freeblock > 0);
716     
717     rec_tmp_expand(p, entry.size);
718
719     cptr = p->tmp_buf;
720     r = bf_read(p->data_BFile[dst_type], freeblock, 0, 0, cptr);
721     if (r < 0)
722         return 0;
723     memcpy(&freeblock, cptr, sizeof(freeblock));
724
725     while (freeblock)
726     {
727         zint tmp;
728
729         cptr += p->head.block_size[dst_type] - sizeof(freeblock);
730         
731         memcpy(&tmp, cptr, sizeof(tmp));
732         r = bf_read(p->data_BFile[dst_type], freeblock, 0, 0, cptr);
733         if (r < 0)
734             return 0;
735         memcpy(&freeblock, cptr, sizeof(freeblock));
736         memcpy(cptr, &tmp, sizeof(tmp));
737     }
738
739     rec = (Record) xmalloc(sizeof(*rec));
740     rec->sysno = sysno;
741     memcpy(&compression_method, p->tmp_buf + sizeof(zint) + sizeof(short),
742             sizeof(compression_method));
743     in_buf = p->tmp_buf + sizeof(zint) + sizeof(short) + sizeof(char);
744     in_size = entry.size - sizeof(short) - sizeof(char);
745     switch (compression_method)
746     {
747     case REC_COMPRESS_BZIP2:
748 #if HAVE_BZLIB_H
749         bz_size = entry.size * 20 + 100;
750         while (1)
751         {
752             bz_buf = (char *) xmalloc(bz_size);
753 #ifdef BZ_CONFIG_ERROR
754             i = BZ2_bzBuffToBuffDecompress
755 #else
756             i = bzBuffToBuffDecompress
757 #endif
758                 (bz_buf, &bz_size, in_buf, in_size, 0, 0);
759             yaz_log(YLOG_LOG, "decompress %5d %5d", in_size, bz_size);
760             if (i == BZ_OK)
761                 break;
762             yaz_log(YLOG_LOG, "failed");
763             xfree(bz_buf);
764             bz_size *= 2;
765         }
766         in_buf = bz_buf;
767         in_size = bz_size;
768 #else
769         yaz_log(YLOG_FATAL, "cannot decompress record(s) in BZIP2 format");
770         return 0;
771 #endif
772         break;
773     case REC_COMPRESS_NONE:
774         break;
775     }
776     for (i = 0; i<REC_NO_INFO; i++)
777         rec->info[i] = 0;
778
779     nptr = in_buf;                /* skip ref count */
780     while (nptr < in_buf + in_size)
781     {
782         zint this_sysno;
783         int len;
784         rec_decode_zint(&this_sysno, (unsigned char *) nptr, &len);
785         nptr += len;
786
787         for (i = 0; i < REC_NO_INFO; i++)
788         {
789             unsigned int this_size;
790             rec_decode_unsigned(&this_size, (unsigned char *) nptr, &len);
791             nptr += len;
792
793             if (this_size == 0)
794                 continue;
795             rec->size[i] = this_size-1;
796
797             if (rec->size[i])
798             {
799                 rec->info[i] = nptr;
800                 nptr += rec->size[i];
801             }
802             else
803                 rec->info[i] = NULL;
804         }
805         if (this_sysno == rec_sysno_to_int(sysno))
806             break;
807     }
808     for (i = 0; i<REC_NO_INFO; i++)
809     {
810         if (rec->info[i] && rec->size[i])
811         {
812             char *np = xmalloc(rec->size[i]+1);
813             memcpy(np, rec->info[i], rec->size[i]);
814             np[rec->size[i]] = '\0';
815             rec->info[i] = np;
816         }
817         else
818         {
819             assert(rec->info[i] == 0);
820             assert(rec->size[i] == 0);
821         }
822     }
823     xfree(bz_buf);
824     if (rec_cache_insert(p, rec, recordFlagNop) != ZEBRA_OK)
825         return 0;
826     return rec;
827 }
828
829 Record rec_get(Records p, SYSNO sysno)
830 {
831     Record rec;
832     zebra_mutex_lock(&p->mutex);
833
834     rec = rec_get_int(p, sysno);
835     zebra_mutex_unlock(&p->mutex);
836     return rec;
837 }
838
839 Record rec_get_root(Records p)
840 {
841     return rec_get(p, rec_sysno_to_ext(1));
842 }
843
844 static Record rec_new_int(Records p)
845 {
846     int i;
847     SYSNO sysno;
848     Record rec;
849
850     assert(p);
851     rec = (Record) xmalloc(sizeof(*rec));
852     if (1 || p->head.index_free == 0)
853         sysno = (p->head.index_last)++;
854     else
855     {
856         struct record_index_entry entry;
857
858         if (read_indx(p, p->head.index_free, &entry, sizeof(entry), 0) < 1)
859         {
860             xfree(rec);
861             return 0;
862         }
863         sysno = p->head.index_free;
864         p->head.index_free = entry.next;
865     }
866     (p->head.no_records)++;
867     rec->sysno = rec_sysno_to_ext(sysno);
868     for (i = 0; i < REC_NO_INFO; i++)
869     {
870         rec->info[i] = NULL;
871         rec->size[i] = 0;
872     }
873     rec_cache_insert(p, rec, recordFlagNew);
874     return rec;
875 }
876
877 Record rec_new(Records p)
878 {
879     Record rec;
880     zebra_mutex_lock(&p->mutex);
881
882     rec = rec_new_int(p);
883     zebra_mutex_unlock(&p->mutex);
884     return rec;
885 }
886
887 ZEBRA_RES rec_del(Records p, Record *recpp)
888 {
889     Record *recp;
890     ZEBRA_RES ret = ZEBRA_OK;
891
892     zebra_mutex_lock(&p->mutex);
893     (p->head.no_records)--;
894     if ((recp = rec_cache_lookup(p, (*recpp)->sysno, recordFlagDelete)))
895     {
896         rec_rm(recp);
897         *recp = *recpp;
898     }
899     else
900     {
901         ret = rec_cache_insert(p, *recpp, recordFlagDelete);
902         rec_rm(recpp);
903     }
904     zebra_mutex_unlock(&p->mutex);
905     *recpp = NULL;
906     return ret;
907 }
908
909 ZEBRA_RES rec_put(Records p, Record *recpp)
910 {
911     Record *recp;
912     ZEBRA_RES ret = ZEBRA_OK;
913
914     zebra_mutex_lock(&p->mutex);
915     if ((recp = rec_cache_lookup(p, (*recpp)->sysno, recordFlagWrite)))
916     {
917         rec_rm(recp);
918         *recp = *recpp;
919     }
920     else
921     {
922         ret = rec_cache_insert(p, *recpp, recordFlagWrite);
923         rec_rm(recpp);
924     }
925     zebra_mutex_unlock(&p->mutex);
926     *recpp = NULL;
927     return ret;
928 }
929
930 void rec_rm(Record *recpp)
931 {
932     int i;
933
934     if (!*recpp)
935         return ;
936     for (i = 0; i < REC_NO_INFO; i++)
937         xfree((*recpp)->info[i]);
938     xfree(*recpp);
939     *recpp = NULL;
940 }
941
942 Record rec_cp(Record rec)
943 {
944     Record n;
945     int i;
946
947     n = (Record) xmalloc(sizeof(*n));
948     n->sysno = rec->sysno;
949     for (i = 0; i < REC_NO_INFO; i++)
950         if (!rec->info[i])
951         {
952             n->info[i] = NULL;
953             n->size[i] = 0;
954         }
955         else
956         {
957             n->size[i] = rec->size[i];
958             n->info[i] = (char *) xmalloc(rec->size[i]);
959             memcpy(n->info[i], rec->info[i], rec->size[i]);
960         }
961     return n;
962 }
963
964
965 char *rec_strdup(const char *s, size_t *len)
966 {
967     char *p;
968
969     if (!s)
970     {
971         *len = 0;
972         return NULL;
973     }
974     *len = strlen(s)+1;
975     p = (char *) xmalloc(*len);
976     strcpy(p, s);
977     return p;
978 }
979