1 /* $Id: recindex.c,v 1.46 2005-08-22 08:18:43 adam Exp $
2 Copyright (C) 1995-2005
5 This file is part of the Zebra server.
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
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
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
23 #define RIDX_CHUNK 128
26 * Format of first block
31 * Format of subsequent blocks
35 * Format of each record
37 * (length, data) - pairs
38 * length = 0 if same as previous
45 #include <yaz/yaz-util.h>
52 /* Modify argument to if below: 1=normal, 0=sysno testing */
54 /* If this is used sysno are not converted (no testing) */
56 #define USUAL_RANGE 6000000000LL
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
64 static SYSNO rec_sysno_to_ext(SYSNO sysno)
66 assert(sysno >= 0 && sysno <= USUAL_RANGE);
67 return sysno + FAKE_OFFSET;
70 SYSNO rec_sysno_to_int(SYSNO sysno)
72 assert(sysno >= FAKE_OFFSET && sysno <= FAKE_OFFSET + USUAL_RANGE);
73 return sysno - FAKE_OFFSET;
76 static void rec_write_head(Records p)
81 assert(p->index_BFile);
83 r = bf_write(p->index_BFile, 0, 0, sizeof(p->head), &p->head);
86 yaz_log(YLOG_FATAL|YLOG_ERRNO, "write head of %s", p->index_fname);
91 static void rec_tmp_expand(Records p, int size)
93 if (p->tmp_size < size + 2048 ||
94 p->tmp_size < p->head.block_size[REC_BLOCK_TYPES-1]*2)
97 p->tmp_size = size + (int)
98 (p->head.block_size[REC_BLOCK_TYPES-1])*2 + 2048;
99 p->tmp_buf = (char *) xmalloc(p->tmp_size);
103 static int read_indx(Records p, SYSNO sysno, void *buf, int itemsize,
107 zint pos = (sysno-1)*itemsize;
108 int off = (int) (pos%RIDX_CHUNK);
109 int sz1 = RIDX_CHUNK - off; /* sz1 is size of buffer to read.. */
112 sz1 = itemsize; /* no more than itemsize bytes */
114 r = bf_read(p->index_BFile, 1+pos/RIDX_CHUNK, off, sz1, buf);
115 if (r == 1 && sz1 < itemsize) /* boundary? - must read second part */
116 r = bf_read(p->index_BFile, 2+pos/RIDX_CHUNK, 0, itemsize - sz1,
118 if (r != 1 && !ignoreError)
120 yaz_log(YLOG_FATAL|YLOG_ERRNO, "read in %s at pos %ld",
121 p->index_fname, (long) pos);
127 static void write_indx(Records p, SYSNO sysno, void *buf, int itemsize)
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.. */
134 sz1 = itemsize; /* no more than itemsize bytes */
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,
142 static void rec_release_blocks(Records p, SYSNO sysno)
144 struct record_index_entry entry;
146 char block_and_ref[sizeof(zint) + sizeof(short)];
150 if (read_indx(p, sysno, &entry, sizeof(entry), 1) != 1)
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;
160 if (bf_read(p->data_BFile[dst_type], freeblock, 0,
161 first ? sizeof(block_and_ref) : sizeof(zint),
164 yaz_log(YLOG_FATAL|YLOG_ERRNO, "read in rec_del_single");
170 memcpy(&ref, block_and_ref + sizeof(freeblock), sizeof(ref));
172 memcpy(block_and_ref + sizeof(freeblock), &ref, sizeof(ref));
175 if (bf_write(p->data_BFile[dst_type], freeblock, 0,
176 sizeof(block_and_ref), block_and_ref))
178 yaz_log(YLOG_FATAL|YLOG_ERRNO, "write in rec_del_single");
186 if (bf_write(p->data_BFile[dst_type], freeblock, 0, sizeof(freeblock),
187 &p->head.block_free[dst_type]))
189 yaz_log(YLOG_FATAL|YLOG_ERRNO, "write in rec_del_single");
192 p->head.block_free[dst_type] = freeblock;
193 memcpy(&freeblock, block_and_ref, sizeof(freeblock));
195 p->head.block_used[dst_type]--;
197 p->head.total_bytes -= entry.size;
200 static void rec_delete_single(Records p, Record rec)
202 struct record_index_entry entry;
204 rec_release_blocks(p, rec_sysno_to_int(rec->sysno));
206 entry.next = p->head.index_free;
208 p->head.index_free = rec_sysno_to_int(rec->sysno);
209 write_indx(p, rec_sysno_to_int(rec->sysno), &entry, sizeof(entry));
212 static void rec_write_tmp_buf(Records p, int size, SYSNO *sysnos)
214 struct record_index_entry entry;
216 char *cptr = p->tmp_buf;
217 zint block_prev = -1, block_free;
221 for (i = 1; i<REC_BLOCK_TYPES; i++)
222 if (size >= p->head.block_move[i])
224 while (no_written < size)
226 block_free = p->head.block_free[dst_type];
229 if (bf_read(p->data_BFile[dst_type],
230 block_free, 0, sizeof(*p->head.block_free),
231 &p->head.block_free[dst_type]) != 1)
233 yaz_log(YLOG_FATAL|YLOG_ERRNO, "read in %s at free block "
235 p->data_fname[dst_type], block_free);
240 block_free = p->head.block_last[dst_type]++;
241 if (block_prev == -1)
243 entry.next = block_free*8 + dst_type;
245 p->head.total_bytes += size;
248 write_indx(p, *sysnos, &entry, sizeof(entry));
254 memcpy(cptr, &block_free, sizeof(block_free));
255 bf_write(p->data_BFile[dst_type], block_prev, 0, 0, cptr);
256 cptr = p->tmp_buf + no_written;
258 block_prev = block_free;
259 no_written += (int)(p->head.block_size[dst_type]) - sizeof(zint);
260 p->head.block_used[dst_type]++;
262 assert(block_prev != -1);
264 memcpy(cptr, &block_free, sizeof(block_free));
265 bf_write(p->data_BFile[dst_type], block_prev, 0,
266 sizeof(block_free) + (p->tmp_buf+size) - cptr, cptr);
269 Records rec_open(BFiles bfs, int rw, int compression_method)
275 p = (Records) xmalloc(sizeof(*p));
276 p->compression_method = compression_method;
279 p->tmp_buf = (char *) xmalloc(p->tmp_size);
280 p->index_fname = "reci";
281 p->index_BFile = bf_open(bfs, p->index_fname, RIDX_CHUNK, rw);
282 if (p->index_BFile == NULL)
284 yaz_log(YLOG_FATAL|YLOG_ERRNO, "open %s", p->index_fname);
287 r = bf_read(p->index_BFile, 0, 0, 0, p->tmp_buf);
291 memcpy(p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic));
292 sprintf(p->head.version, "%3d", REC_VERSION);
293 p->head.index_free = 0;
294 p->head.index_last = 1;
295 p->head.no_records = 0;
296 p->head.total_bytes = 0;
297 for (i = 0; i<REC_BLOCK_TYPES; i++)
299 p->head.block_free[i] = 0;
300 p->head.block_last[i] = 1;
301 p->head.block_used[i] = 0;
303 p->head.block_size[0] = 128;
304 p->head.block_move[0] = 0;
305 for (i = 1; i<REC_BLOCK_TYPES; i++)
307 p->head.block_size[i] = p->head.block_size[i-1] * 4;
308 p->head.block_move[i] = p->head.block_size[i] * 24;
314 memcpy(&p->head, p->tmp_buf, sizeof(p->head));
315 if (memcmp(p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic)))
317 yaz_log(YLOG_FATAL, "file %s has bad format", p->index_fname);
320 version = atoi(p->head.version);
321 if (version != REC_VERSION)
323 yaz_log(YLOG_FATAL, "file %s is version %d, but version"
324 " %d is required", p->index_fname, version, REC_VERSION);
329 for (i = 0; i<REC_BLOCK_TYPES; i++)
332 sprintf(str, "recd%c", i + 'A');
333 p->data_fname[i] = (char *) xmalloc(strlen(str)+1);
334 strcpy(p->data_fname[i], str);
335 p->data_BFile[i] = NULL;
337 for (i = 0; i<REC_BLOCK_TYPES; i++)
339 if (!(p->data_BFile[i] = bf_open(bfs, p->data_fname[i],
340 (int) (p->head.block_size[i]),
343 yaz_log(YLOG_FATAL|YLOG_ERRNO, "bf_open %s", p->data_fname[i]);
349 p->record_cache = (struct record_cache_entry *)
350 xmalloc(sizeof(*p->record_cache)*p->cache_max);
351 zebra_mutex_init(&p->mutex);
355 static void rec_encode_unsigned(unsigned n, unsigned char *buf, int *len)
360 buf[*len] = 128 + (n & 127);
368 static void rec_decode_unsigned(unsigned *np, unsigned char *buf, int *len)
374 while (buf[*len] > 127)
376 n += w*(buf[*len] & 127);
385 static void rec_encode_zint(zint n, unsigned char *buf, int *len)
390 buf[*len] = (unsigned) (128 + (n & 127));
394 buf[*len] = (unsigned) n;
398 static void rec_decode_zint(zint *np, unsigned char *buf, int *len)
404 while (buf[*len] > 127)
406 n += w*(buf[*len] & 127);
415 static void rec_cache_flush_block1(Records p, Record rec, Record last_rec,
416 char **out_buf, int *out_size,
422 for (i = 0; i<REC_NO_INFO; i++)
424 if (*out_offset + (int) rec->size[i] + 20 > *out_size)
426 int new_size = *out_offset + rec->size[i] + 65536;
427 char *np = (char *) xmalloc(new_size);
429 memcpy(np, *out_buf, *out_offset);
431 *out_size = new_size;
436 rec_encode_zint(rec_sysno_to_int(rec->sysno),
437 (unsigned char *) *out_buf + *out_offset, &len);
438 (*out_offset) += len;
440 if (rec->size[i] == 0)
442 rec_encode_unsigned(1, (unsigned char *) *out_buf + *out_offset,
444 (*out_offset) += len;
446 else if (last_rec && rec->size[i] == last_rec->size[i] &&
447 !memcmp(rec->info[i], last_rec->info[i], rec->size[i]))
449 rec_encode_unsigned(0, (unsigned char *) *out_buf + *out_offset,
451 (*out_offset) += len;
455 rec_encode_unsigned(rec->size[i]+1,
456 (unsigned char *) *out_buf + *out_offset,
458 (*out_offset) += len;
459 memcpy(*out_buf + *out_offset, rec->info[i], rec->size[i]);
460 (*out_offset) += rec->size[i];
465 static void rec_write_multiple(Records p, int saveCount)
469 char compression_method;
473 char *out_buf = (char *) xmalloc(out_size);
474 SYSNO *sysnos = (SYSNO *) xmalloc(sizeof(*sysnos) * (p->cache_cur + 1));
475 SYSNO *sysnop = sysnos;
477 for (i = 0; i<p->cache_cur - saveCount; i++)
479 struct record_cache_entry *e = p->record_cache + i;
483 rec_cache_flush_block1(p, e->rec, last_rec, &out_buf,
484 &out_size, &out_offset);
485 *sysnop++ = rec_sysno_to_int(e->rec->sysno);
487 e->flag = recordFlagNop;
490 case recordFlagWrite:
491 rec_release_blocks(p, rec_sysno_to_int(e->rec->sysno));
492 rec_cache_flush_block1(p, e->rec, last_rec, &out_buf,
493 &out_size, &out_offset);
494 *sysnop++ = rec_sysno_to_int(e->rec->sysno);
496 e->flag = recordFlagNop;
499 case recordFlagDelete:
500 rec_delete_single(p, e->rec);
501 e->flag = recordFlagNop;
511 unsigned int csize = 0; /* indicate compression "not performed yet" */
512 compression_method = p->compression_method;
513 switch (compression_method)
515 case REC_COMPRESS_BZIP2:
517 csize = out_offset + (out_offset >> 6) + 620;
518 rec_tmp_expand(p, csize);
519 #ifdef BZ_CONFIG_ERROR
520 i = BZ2_bzBuffToBuffCompress
522 i = bzBuffToBuffCompress
524 (p->tmp_buf+sizeof(zint)+sizeof(short)+
526 &csize, out_buf, out_offset, 1, 0, 30);
529 yaz_log(YLOG_WARN, "bzBuffToBuffCompress error code=%d", i);
532 yaz_log(YLOG_LOG, "compress %4d %5d %5d", ref_count, out_offset,
536 case REC_COMPRESS_NONE:
541 /* either no compression or compression not supported ... */
543 rec_tmp_expand(p, csize);
544 memcpy(p->tmp_buf + sizeof(zint) + sizeof(short) + sizeof(char),
545 out_buf, out_offset);
547 compression_method = REC_COMPRESS_NONE;
549 memcpy(p->tmp_buf + sizeof(zint), &ref_count, sizeof(ref_count));
550 memcpy(p->tmp_buf + sizeof(zint)+sizeof(short),
551 &compression_method, sizeof(compression_method));
553 /* -------- compression */
554 rec_write_tmp_buf(p, csize + sizeof(short) + sizeof(char), sysnos);
560 static void rec_cache_flush(Records p, int saveCount)
564 if (saveCount >= p->cache_cur)
567 rec_write_multiple(p, saveCount);
569 for (i = 0; i<p->cache_cur - saveCount; i++)
571 struct record_cache_entry *e = p->record_cache + i;
574 /* i still being used ... */
575 for (j = 0; j<saveCount; j++, i++)
576 memcpy(p->record_cache+j, p->record_cache+i,
577 sizeof(*p->record_cache));
578 p->cache_cur = saveCount;
581 static Record *rec_cache_lookup(Records p, SYSNO sysno,
582 enum recordCacheFlag flag)
585 for (i = 0; i<p->cache_cur; i++)
587 struct record_cache_entry *e = p->record_cache + i;
588 if (e->rec->sysno == sysno)
590 if (flag != recordFlagNop && e->flag == recordFlagNop)
598 static void rec_cache_insert(Records p, Record rec, enum recordCacheFlag flag)
600 struct record_cache_entry *e;
602 if (p->cache_cur == p->cache_max)
603 rec_cache_flush(p, 1);
604 else if (p->cache_cur > 0)
608 for (i = 0; i<p->cache_cur; i++)
610 Record r = (p->record_cache + i)->rec;
611 for (j = 0; j<REC_NO_INFO; j++)
615 rec_cache_flush(p, 1);
617 assert(p->cache_cur < p->cache_max);
619 e = p->record_cache + (p->cache_cur)++;
621 e->rec = rec_cp(rec);
624 void rec_close(Records *pp)
631 zebra_mutex_destroy(&p->mutex);
632 rec_cache_flush(p, 0);
633 xfree(p->record_cache);
639 bf_close(p->index_BFile);
641 for (i = 0; i<REC_BLOCK_TYPES; i++)
643 if (p->data_BFile[i])
644 bf_close(p->data_BFile[i]);
645 xfree(p->data_fname[i]);
652 static Record rec_get_int(Records p, SYSNO sysno)
656 struct record_index_entry entry;
663 unsigned int bz_size;
665 char compression_method;
670 if ((recp = rec_cache_lookup(p, sysno, recordFlagNop)))
671 return rec_cp(*recp);
673 if (read_indx(p, rec_sysno_to_int(sysno), &entry, sizeof(entry), 1) < 1)
674 return NULL; /* record is not there! */
677 return NULL; /* record is deleted */
679 dst_type = (int) (entry.next & 7);
680 assert(dst_type < REC_BLOCK_TYPES);
681 freeblock = entry.next / 8;
683 assert(freeblock > 0);
685 rec_tmp_expand(p, entry.size);
688 r = bf_read(p->data_BFile[dst_type], freeblock, 0, 0, cptr);
691 memcpy(&freeblock, cptr, sizeof(freeblock));
697 cptr += p->head.block_size[dst_type] - sizeof(freeblock);
699 memcpy(&tmp, cptr, sizeof(tmp));
700 r = bf_read(p->data_BFile[dst_type], freeblock, 0, 0, cptr);
703 memcpy(&freeblock, cptr, sizeof(freeblock));
704 memcpy(cptr, &tmp, sizeof(tmp));
707 rec = (Record) xmalloc(sizeof(*rec));
709 memcpy(&compression_method, p->tmp_buf + sizeof(zint) + sizeof(short),
710 sizeof(compression_method));
711 in_buf = p->tmp_buf + sizeof(zint) + sizeof(short) + sizeof(char);
712 in_size = entry.size - sizeof(short) - sizeof(char);
713 switch (compression_method)
715 case REC_COMPRESS_BZIP2:
717 bz_size = entry.size * 20 + 100;
720 bz_buf = (char *) xmalloc(bz_size);
721 #ifdef BZ_CONFIG_ERROR
722 i = BZ2_bzBuffToBuffDecompress
724 i = bzBuffToBuffDecompress
726 (bz_buf, &bz_size, in_buf, in_size, 0, 0);
727 yaz_log(YLOG_LOG, "decompress %5d %5d", in_size, bz_size);
730 yaz_log(YLOG_LOG, "failed");
737 yaz_log(YLOG_FATAL, "cannot decompress record(s) in BZIP2 format");
741 case REC_COMPRESS_NONE:
744 for (i = 0; i<REC_NO_INFO; i++)
747 nptr = in_buf; /* skip ref count */
748 while (nptr < in_buf + in_size)
752 rec_decode_zint(&this_sysno, (unsigned char *) nptr, &len);
755 for (i = 0; i < REC_NO_INFO; i++)
757 unsigned int this_size;
758 rec_decode_unsigned(&this_size, (unsigned char *) nptr, &len);
763 rec->size[i] = this_size-1;
768 nptr += rec->size[i];
773 if (this_sysno == rec_sysno_to_int(sysno))
776 for (i = 0; i<REC_NO_INFO; i++)
778 if (rec->info[i] && rec->size[i])
780 char *np = xmalloc(rec->size[i]+1);
781 memcpy(np, rec->info[i], rec->size[i]);
782 np[rec->size[i]] = '\0';
787 assert(rec->info[i] == 0);
788 assert(rec->size[i] == 0);
792 rec_cache_insert(p, rec, recordFlagNop);
796 Record rec_get(Records p, SYSNO sysno)
799 zebra_mutex_lock(&p->mutex);
801 rec = rec_get_int(p, sysno);
802 zebra_mutex_unlock(&p->mutex);
806 Record rec_get_root(Records p)
808 return rec_get(p, rec_sysno_to_ext(1));
811 static Record rec_new_int(Records p)
818 rec = (Record) xmalloc(sizeof(*rec));
819 if (1 || p->head.index_free == 0)
820 sysno = (p->head.index_last)++;
823 struct record_index_entry entry;
825 read_indx(p, p->head.index_free, &entry, sizeof(entry), 0);
826 sysno = p->head.index_free;
827 p->head.index_free = entry.next;
829 (p->head.no_records)++;
830 rec->sysno = rec_sysno_to_ext(sysno);
831 for (i = 0; i < REC_NO_INFO; i++)
836 rec_cache_insert(p, rec, recordFlagNew);
840 Record rec_new(Records p)
843 zebra_mutex_lock(&p->mutex);
845 rec = rec_new_int(p);
846 zebra_mutex_unlock(&p->mutex);
850 void rec_del(Records p, Record *recpp)
854 zebra_mutex_lock(&p->mutex);
855 (p->head.no_records)--;
856 if ((recp = rec_cache_lookup(p, (*recpp)->sysno, recordFlagDelete)))
863 rec_cache_insert(p, *recpp, recordFlagDelete);
866 zebra_mutex_unlock(&p->mutex);
870 void rec_put(Records p, Record *recpp)
874 zebra_mutex_lock(&p->mutex);
875 if ((recp = rec_cache_lookup(p, (*recpp)->sysno, recordFlagWrite)))
882 rec_cache_insert(p, *recpp, recordFlagWrite);
885 zebra_mutex_unlock(&p->mutex);
889 void rec_rm(Record *recpp)
895 for (i = 0; i < REC_NO_INFO; i++)
896 xfree((*recpp)->info[i]);
901 Record rec_cp(Record rec)
906 n = (Record) xmalloc(sizeof(*n));
907 n->sysno = rec->sysno;
908 for (i = 0; i < REC_NO_INFO; i++)
916 n->size[i] = rec->size[i];
917 n->info[i] = (char *) xmalloc(rec->size[i]);
918 memcpy(n->info[i], rec->info[i], rec->size[i]);
924 char *rec_strdup(const char *s, size_t *len)
934 p = (char *) xmalloc(*len);