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