1 /* $Id: recindex.c,v 1.42 2005-01-16 23:14:57 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
50 static void rec_write_head (Records p)
55 assert (p->index_BFile);
57 r = bf_write (p->index_BFile, 0, 0, sizeof(p->head), &p->head);
60 yaz_log (YLOG_FATAL|YLOG_ERRNO, "write head of %s", p->index_fname);
65 static void rec_tmp_expand (Records p, int size)
67 if (p->tmp_size < size + 2048 ||
68 p->tmp_size < p->head.block_size[REC_BLOCK_TYPES-1]*2)
71 p->tmp_size = size + (int)
72 (p->head.block_size[REC_BLOCK_TYPES-1])*2 + 2048;
73 p->tmp_buf = (char *) xmalloc (p->tmp_size);
77 static int read_indx (Records p, SYSNO sysno, void *buf, int itemsize,
81 zint pos = (sysno-1)*itemsize;
82 int off = (int) (pos%RIDX_CHUNK);
83 int sz1 = RIDX_CHUNK - off; /* sz1 is size of buffer to read.. */
86 sz1 = itemsize; /* no more than itemsize bytes */
88 r = bf_read (p->index_BFile, 1+pos/RIDX_CHUNK, off, sz1, buf);
89 if (r == 1 && sz1 < itemsize) /* boundary? - must read second part */
90 r = bf_read (p->index_BFile, 2+pos/RIDX_CHUNK, 0, itemsize - sz1,
92 if (r != 1 && !ignoreError)
94 yaz_log (YLOG_FATAL|YLOG_ERRNO, "read in %s at pos %ld",
95 p->index_fname, (long) pos);
101 static void write_indx (Records p, SYSNO sysno, void *buf, int itemsize)
103 zint pos = (sysno-1)*itemsize;
104 int off = (int) (pos%RIDX_CHUNK);
105 int sz1 = RIDX_CHUNK - off; /* sz1 is size of buffer to read.. */
108 sz1 = itemsize; /* no more than itemsize bytes */
110 bf_write(p->index_BFile, 1+pos/RIDX_CHUNK, off, sz1, buf);
111 if (sz1 < itemsize) /* boundary? must write second part */
112 bf_write(p->index_BFile, 2+pos/RIDX_CHUNK, 0, itemsize - sz1,
116 static void rec_release_blocks (Records p, SYSNO sysno)
118 struct record_index_entry entry;
120 char block_and_ref[sizeof(zint) + sizeof(short)];
124 if (read_indx (p, sysno, &entry, sizeof(entry), 1) != 1)
127 freeblock = entry.next;
128 assert (freeblock > 0);
129 dst_type = (int) (freeblock & 7);
130 assert (dst_type < REC_BLOCK_TYPES);
131 freeblock = freeblock / 8;
134 if (bf_read (p->data_BFile[dst_type], freeblock, 0,
135 first ? sizeof(block_and_ref) : sizeof(zint),
138 yaz_log (YLOG_FATAL|YLOG_ERRNO, "read in rec_del_single");
144 memcpy (&ref, block_and_ref + sizeof(freeblock), sizeof(ref));
146 memcpy (block_and_ref + sizeof(freeblock), &ref, sizeof(ref));
149 if (bf_write (p->data_BFile[dst_type], freeblock, 0,
150 sizeof(block_and_ref), block_and_ref))
152 yaz_log (YLOG_FATAL|YLOG_ERRNO, "write in rec_del_single");
160 if (bf_write (p->data_BFile[dst_type], freeblock, 0, sizeof(freeblock),
161 &p->head.block_free[dst_type]))
163 yaz_log (YLOG_FATAL|YLOG_ERRNO, "write in rec_del_single");
166 p->head.block_free[dst_type] = freeblock;
167 memcpy (&freeblock, block_and_ref, sizeof(freeblock));
169 p->head.block_used[dst_type]--;
171 p->head.total_bytes -= entry.size;
174 static void rec_delete_single (Records p, Record rec)
176 struct record_index_entry entry;
178 rec_release_blocks (p, rec->sysno);
180 entry.next = p->head.index_free;
182 p->head.index_free = rec->sysno;
183 write_indx (p, rec->sysno, &entry, sizeof(entry));
186 static void rec_write_tmp_buf (Records p, int size, SYSNO *sysnos)
188 struct record_index_entry entry;
190 char *cptr = p->tmp_buf;
191 zint block_prev = -1, block_free;
195 for (i = 1; i<REC_BLOCK_TYPES; i++)
196 if (size >= p->head.block_move[i])
198 while (no_written < size)
200 block_free = p->head.block_free[dst_type];
203 if (bf_read (p->data_BFile[dst_type],
204 block_free, 0, sizeof(*p->head.block_free),
205 &p->head.block_free[dst_type]) != 1)
207 yaz_log (YLOG_FATAL|YLOG_ERRNO, "read in %s at free block " ZINT_FORMAT,
208 p->data_fname[dst_type], block_free);
213 block_free = p->head.block_last[dst_type]++;
214 if (block_prev == -1)
216 entry.next = block_free*8 + dst_type;
218 p->head.total_bytes += size;
221 write_indx (p, *sysnos, &entry, sizeof(entry));
227 memcpy (cptr, &block_free, sizeof(block_free));
228 bf_write (p->data_BFile[dst_type], block_prev, 0, 0, cptr);
229 cptr = p->tmp_buf + no_written;
231 block_prev = block_free;
232 no_written += (int)(p->head.block_size[dst_type]) - sizeof(zint);
233 p->head.block_used[dst_type]++;
235 assert (block_prev != -1);
237 memcpy (cptr, &block_free, sizeof(block_free));
238 bf_write (p->data_BFile[dst_type], block_prev, 0,
239 sizeof(block_free) + (p->tmp_buf+size) - cptr, cptr);
242 Records rec_open (BFiles bfs, int rw, int compression_method)
248 p = (Records) xmalloc (sizeof(*p));
249 p->compression_method = compression_method;
252 p->tmp_buf = (char *) xmalloc (p->tmp_size);
253 p->index_fname = "reci";
254 p->index_BFile = bf_open (bfs, p->index_fname, RIDX_CHUNK, rw);
255 if (p->index_BFile == NULL)
257 yaz_log (YLOG_FATAL|YLOG_ERRNO, "open %s", p->index_fname);
260 r = bf_read (p->index_BFile, 0, 0, 0, p->tmp_buf);
264 memcpy (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic));
265 sprintf (p->head.version, "%3d", REC_VERSION);
266 p->head.index_free = 0;
267 p->head.index_last = 1;
268 p->head.no_records = 0;
269 p->head.total_bytes = 0;
270 for (i = 0; i<REC_BLOCK_TYPES; i++)
272 p->head.block_free[i] = 0;
273 p->head.block_last[i] = 1;
274 p->head.block_used[i] = 0;
276 p->head.block_size[0] = 128;
277 p->head.block_move[0] = 0;
278 for (i = 1; i<REC_BLOCK_TYPES; i++)
280 p->head.block_size[i] = p->head.block_size[i-1] * 4;
281 p->head.block_move[i] = p->head.block_size[i] * 24;
287 memcpy (&p->head, p->tmp_buf, sizeof(p->head));
288 if (memcmp (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic)))
290 yaz_log (YLOG_FATAL, "file %s has bad format", p->index_fname);
293 version = atoi (p->head.version);
294 if (version != REC_VERSION)
296 yaz_log (YLOG_FATAL, "file %s is version %d, but version"
297 " %d is required", p->index_fname, version, REC_VERSION);
302 for (i = 0; i<REC_BLOCK_TYPES; i++)
305 sprintf (str, "recd%c", i + 'A');
306 p->data_fname[i] = (char *) xmalloc (strlen(str)+1);
307 strcpy (p->data_fname[i], str);
308 p->data_BFile[i] = NULL;
310 for (i = 0; i<REC_BLOCK_TYPES; i++)
312 if (!(p->data_BFile[i] = bf_open (bfs, p->data_fname[i],
313 (int) (p->head.block_size[i]),
316 yaz_log (YLOG_FATAL|YLOG_ERRNO, "bf_open %s", p->data_fname[i]);
322 p->record_cache = (struct record_cache_entry *)
323 xmalloc (sizeof(*p->record_cache)*p->cache_max);
324 zebra_mutex_init (&p->mutex);
328 static void rec_encode_unsigned (unsigned n, unsigned char *buf, int *len)
333 buf[*len] = 128 + (n & 127);
341 static void rec_decode_unsigned(unsigned *np, unsigned char *buf, int *len)
347 while (buf[*len] > 127)
349 n += w*(buf[*len] & 127);
358 static void rec_encode_zint (zint n, unsigned char *buf, int *len)
363 buf[*len] = (unsigned) (128 + (n & 127));
367 buf[*len] = (unsigned) n;
371 static void rec_decode_zint(zint *np, unsigned char *buf, int *len)
377 while (buf[*len] > 127)
379 n += w*(buf[*len] & 127);
388 static void rec_cache_flush_block1 (Records p, Record rec, Record last_rec,
389 char **out_buf, int *out_size,
395 for (i = 0; i<REC_NO_INFO; i++)
397 if (*out_offset + (int) rec->size[i] + 20 > *out_size)
399 int new_size = *out_offset + rec->size[i] + 65536;
400 char *np = (char *) xmalloc (new_size);
402 memcpy (np, *out_buf, *out_offset);
404 *out_size = new_size;
409 rec_encode_zint (rec->sysno, *out_buf + *out_offset, &len);
410 (*out_offset) += len;
412 if (rec->size[i] == 0)
414 rec_encode_unsigned (1, *out_buf + *out_offset, &len);
415 (*out_offset) += len;
417 else if (last_rec && rec->size[i] == last_rec->size[i] &&
418 !memcmp (rec->info[i], last_rec->info[i], rec->size[i]))
420 rec_encode_unsigned (0, *out_buf + *out_offset, &len);
421 (*out_offset) += len;
425 rec_encode_unsigned (rec->size[i]+1, *out_buf + *out_offset, &len);
426 (*out_offset) += len;
427 memcpy (*out_buf + *out_offset, rec->info[i], rec->size[i]);
428 (*out_offset) += rec->size[i];
433 static void rec_write_multiple (Records p, int saveCount)
437 char compression_method;
441 char *out_buf = (char *) xmalloc (out_size);
442 SYSNO *sysnos = (SYSNO *) xmalloc (sizeof(*sysnos) * (p->cache_cur + 1));
443 SYSNO *sysnop = sysnos;
445 for (i = 0; i<p->cache_cur - saveCount; i++)
447 struct record_cache_entry *e = p->record_cache + i;
451 rec_cache_flush_block1 (p, e->rec, last_rec, &out_buf,
452 &out_size, &out_offset);
453 *sysnop++ = e->rec->sysno;
455 e->flag = recordFlagNop;
458 case recordFlagWrite:
459 rec_release_blocks (p, e->rec->sysno);
460 rec_cache_flush_block1 (p, e->rec, last_rec, &out_buf,
461 &out_size, &out_offset);
462 *sysnop++ = e->rec->sysno;
464 e->flag = recordFlagNop;
467 case recordFlagDelete:
468 rec_delete_single (p, e->rec);
469 e->flag = recordFlagNop;
479 int csize = 0; /* indicate compression "not performed yet" */
480 compression_method = p->compression_method;
481 switch (compression_method)
483 case REC_COMPRESS_BZIP2:
485 csize = out_offset + (out_offset >> 6) + 620;
486 rec_tmp_expand (p, csize);
487 #ifdef BZ_CONFIG_ERROR
488 i = BZ2_bzBuffToBuffCompress
490 i = bzBuffToBuffCompress
492 (p->tmp_buf+sizeof(zint)+sizeof(short)+
494 &csize, out_buf, out_offset, 1, 0, 30);
497 yaz_log (YLOG_WARN, "bzBuffToBuffCompress error code=%d", i);
500 yaz_log (YLOG_LOG, "compress %4d %5d %5d", ref_count, out_offset,
504 case REC_COMPRESS_NONE:
509 /* either no compression or compression not supported ... */
511 rec_tmp_expand (p, csize);
512 memcpy (p->tmp_buf + sizeof(zint) + sizeof(short) + sizeof(char),
513 out_buf, out_offset);
515 compression_method = REC_COMPRESS_NONE;
517 memcpy (p->tmp_buf + sizeof(zint), &ref_count, sizeof(ref_count));
518 memcpy (p->tmp_buf + sizeof(zint)+sizeof(short),
519 &compression_method, sizeof(compression_method));
521 /* -------- compression */
522 rec_write_tmp_buf (p, csize + sizeof(short) + sizeof(char), sysnos);
528 static void rec_cache_flush (Records p, int saveCount)
532 if (saveCount >= p->cache_cur)
535 rec_write_multiple (p, saveCount);
537 for (i = 0; i<p->cache_cur - saveCount; i++)
539 struct record_cache_entry *e = p->record_cache + i;
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;
549 static Record *rec_cache_lookup (Records p, SYSNO sysno,
550 enum recordCacheFlag flag)
553 for (i = 0; i<p->cache_cur; i++)
555 struct record_cache_entry *e = p->record_cache + i;
556 if (e->rec->sysno == sysno)
558 if (flag != recordFlagNop && e->flag == recordFlagNop)
566 static void rec_cache_insert (Records p, Record rec, enum recordCacheFlag flag)
568 struct record_cache_entry *e;
570 if (p->cache_cur == p->cache_max)
571 rec_cache_flush (p, 1);
572 else if (p->cache_cur > 0)
576 for (i = 0; i<p->cache_cur; i++)
578 Record r = (p->record_cache + i)->rec;
579 for (j = 0; j<REC_NO_INFO; j++)
583 rec_cache_flush (p, 1);
585 assert (p->cache_cur < p->cache_max);
587 e = p->record_cache + (p->cache_cur)++;
589 e->rec = rec_cp (rec);
592 void rec_close (Records *pp)
599 zebra_mutex_destroy (&p->mutex);
600 rec_cache_flush (p, 0);
601 xfree (p->record_cache);
607 bf_close (p->index_BFile);
609 for (i = 0; i<REC_BLOCK_TYPES; i++)
611 if (p->data_BFile[i])
612 bf_close (p->data_BFile[i]);
613 xfree (p->data_fname[i]);
620 static Record rec_get_int (Records p, SYSNO sysno)
624 struct record_index_entry entry;
633 char compression_method;
638 if ((recp = rec_cache_lookup (p, sysno, recordFlagNop)))
639 return rec_cp (*recp);
641 if (read_indx (p, sysno, &entry, sizeof(entry), 1) < 1)
642 return NULL; /* record is not there! */
645 return NULL; /* record is deleted */
647 dst_type = (int) (entry.next & 7);
648 assert (dst_type < REC_BLOCK_TYPES);
649 freeblock = entry.next / 8;
651 assert (freeblock > 0);
653 rec_tmp_expand (p, entry.size);
656 r = bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
659 memcpy (&freeblock, cptr, sizeof(freeblock));
665 cptr += p->head.block_size[dst_type] - sizeof(freeblock);
667 memcpy (&tmp, cptr, sizeof(tmp));
668 r = bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
671 memcpy (&freeblock, cptr, sizeof(freeblock));
672 memcpy (cptr, &tmp, sizeof(tmp));
675 rec = (Record) xmalloc (sizeof(*rec));
677 memcpy (&compression_method, p->tmp_buf + sizeof(zint) + sizeof(short),
678 sizeof(compression_method));
679 in_buf = p->tmp_buf + sizeof(zint) + sizeof(short) + sizeof(char);
680 in_size = entry.size - sizeof(short) - sizeof(char);
681 switch (compression_method)
683 case REC_COMPRESS_BZIP2:
685 bz_size = entry.size * 20 + 100;
688 bz_buf = (char *) xmalloc (bz_size);
689 #ifdef BZ_CONFIG_ERROR
690 i = BZ2_bzBuffToBuffDecompress
692 i = bzBuffToBuffDecompress
694 (bz_buf, &bz_size, in_buf, in_size, 0, 0);
695 yaz_log (YLOG_LOG, "decompress %5d %5d", in_size, bz_size);
698 yaz_log (YLOG_LOG, "failed");
705 yaz_log (YLOG_FATAL, "cannot decompress record(s) in BZIP2 format");
709 case REC_COMPRESS_NONE:
712 for (i = 0; i<REC_NO_INFO; i++)
715 nptr = in_buf; /* skip ref count */
716 while (nptr < in_buf + in_size)
720 rec_decode_zint (&this_sysno, nptr, &len);
723 for (i = 0; i < REC_NO_INFO; i++)
726 rec_decode_unsigned (&this_size, nptr, &len);
731 rec->size[i] = this_size-1;
736 nptr += rec->size[i];
741 if (this_sysno == sysno)
744 for (i = 0; i<REC_NO_INFO; i++)
746 if (rec->info[i] && rec->size[i])
748 char *np = xmalloc (rec->size[i]+1);
749 memcpy (np, rec->info[i], rec->size[i]);
750 np[rec->size[i]] = '\0';
755 assert (rec->info[i] == 0);
756 assert (rec->size[i] == 0);
760 rec_cache_insert (p, rec, recordFlagNop);
764 Record rec_get (Records p, SYSNO sysno)
767 zebra_mutex_lock (&p->mutex);
769 rec = rec_get_int (p, sysno);
770 zebra_mutex_unlock (&p->mutex);
774 static Record rec_new_int (Records p)
781 rec = (Record) xmalloc (sizeof(*rec));
782 if (1 || p->head.index_free == 0)
783 sysno = (p->head.index_last)++;
786 struct record_index_entry entry;
788 read_indx (p, p->head.index_free, &entry, sizeof(entry), 0);
789 sysno = p->head.index_free;
790 p->head.index_free = entry.next;
792 (p->head.no_records)++;
794 for (i = 0; i < REC_NO_INFO; i++)
799 rec_cache_insert (p, rec, recordFlagNew);
803 Record rec_new (Records p)
806 zebra_mutex_lock (&p->mutex);
808 rec = rec_new_int (p);
809 zebra_mutex_unlock (&p->mutex);
813 void rec_del (Records p, Record *recpp)
817 zebra_mutex_lock (&p->mutex);
818 (p->head.no_records)--;
819 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagDelete)))
826 rec_cache_insert (p, *recpp, recordFlagDelete);
829 zebra_mutex_unlock (&p->mutex);
833 void rec_put (Records p, Record *recpp)
837 zebra_mutex_lock (&p->mutex);
838 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagWrite)))
845 rec_cache_insert (p, *recpp, recordFlagWrite);
848 zebra_mutex_unlock (&p->mutex);
852 void rec_rm (Record *recpp)
858 for (i = 0; i < REC_NO_INFO; i++)
859 xfree ((*recpp)->info[i]);
864 Record rec_cp (Record rec)
869 n = (Record) xmalloc (sizeof(*n));
870 n->sysno = rec->sysno;
871 for (i = 0; i < REC_NO_INFO; i++)
879 n->size[i] = rec->size[i];
880 n->info[i] = (char *) xmalloc (rec->size[i]);
881 memcpy (n->info[i], rec->info[i], rec->size[i]);
887 char *rec_strdup (const char *s, size_t *len)
897 p = (char *) xmalloc (*len);