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