1 /* $Id: recindex.c,v 1.39 2004-11-19 10:27:03 heikki Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
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
49 static void rec_write_head (Records p)
54 assert (p->index_BFile);
56 r = bf_write (p->index_BFile, 0, 0, sizeof(p->head), &p->head);
59 yaz_log (YLOG_FATAL|YLOG_ERRNO, "write head of %s", p->index_fname);
64 static void rec_tmp_expand (Records p, int size)
66 if (p->tmp_size < size + 2048 ||
67 p->tmp_size < p->head.block_size[REC_BLOCK_TYPES-1]*2)
70 p->tmp_size = size + p->head.block_size[REC_BLOCK_TYPES-1]*2 + 2048;
71 p->tmp_buf = (char *) xmalloc (p->tmp_size);
75 static int read_indx (Records p, SYSNO sysno, void *buf, int itemsize,
79 zint pos = (sysno-1)*itemsize;
80 int off = (int) (pos%RIDX_CHUNK);
81 int sz1 = RIDX_CHUNK - off; /* sz1 is size of buffer to read.. */
84 sz1 = itemsize; /* no more than itemsize bytes */
86 r = bf_read (p->index_BFile, 1+pos/RIDX_CHUNK, off, sz1, buf);
87 if (r == 1 && sz1 < itemsize) /* boundary? - must read second part */
88 r = bf_read (p->index_BFile, 2+pos/RIDX_CHUNK, 0, itemsize - sz1, buf + sz1);
89 if (r != 1 && !ignoreError)
91 yaz_log (YLOG_FATAL|YLOG_ERRNO, "read in %s at pos %ld",
92 p->index_fname, (long) pos);
98 static void write_indx (Records p, SYSNO sysno, void *buf, int itemsize)
100 zint pos = (sysno-1)*itemsize;
101 int off = (int) (pos%RIDX_CHUNK);
102 int sz1 = RIDX_CHUNK - off; /* sz1 is size of buffer to read.. */
105 sz1 = itemsize; /* no more than itemsize bytes */
107 bf_write(p->index_BFile, 1+pos/RIDX_CHUNK, off, sz1, buf);
108 if (sz1 < itemsize) /* boundary? must write second part */
109 bf_write(p->index_BFile, 2+pos/RIDX_CHUNK, 0, itemsize - sz1, buf + sz1);
112 static void rec_release_blocks (Records p, SYSNO sysno)
114 struct record_index_entry entry;
116 char block_and_ref[sizeof(zint) + sizeof(short)];
120 if (read_indx (p, sysno, &entry, sizeof(entry), 1) != 1)
123 freeblock = entry.next;
124 assert (freeblock > 0);
125 dst_type = (int) (freeblock & 7);
126 assert (dst_type < REC_BLOCK_TYPES);
127 freeblock = freeblock / 8;
130 if (bf_read (p->data_BFile[dst_type], freeblock, 0,
131 first ? sizeof(block_and_ref) : sizeof(zint),
134 yaz_log (YLOG_FATAL|YLOG_ERRNO, "read in rec_del_single");
140 memcpy (&ref, block_and_ref + sizeof(freeblock), sizeof(ref));
142 memcpy (block_and_ref + sizeof(freeblock), &ref, sizeof(ref));
145 if (bf_write (p->data_BFile[dst_type], freeblock, 0,
146 sizeof(block_and_ref), block_and_ref))
148 yaz_log (YLOG_FATAL|YLOG_ERRNO, "write in rec_del_single");
156 if (bf_write (p->data_BFile[dst_type], freeblock, 0, sizeof(freeblock),
157 &p->head.block_free[dst_type]))
159 yaz_log (YLOG_FATAL|YLOG_ERRNO, "write in rec_del_single");
162 p->head.block_free[dst_type] = freeblock;
163 memcpy (&freeblock, block_and_ref, sizeof(freeblock));
165 p->head.block_used[dst_type]--;
167 p->head.total_bytes -= entry.size;
170 static void rec_delete_single (Records p, Record rec)
172 struct record_index_entry entry;
174 rec_release_blocks (p, rec->sysno);
176 entry.next = p->head.index_free;
178 p->head.index_free = rec->sysno;
179 write_indx (p, rec->sysno, &entry, sizeof(entry));
182 static void rec_write_tmp_buf (Records p, int size, SYSNO *sysnos)
184 struct record_index_entry entry;
186 char *cptr = p->tmp_buf;
187 zint block_prev = -1, block_free;
191 for (i = 1; i<REC_BLOCK_TYPES; i++)
192 if (size >= p->head.block_move[i])
194 while (no_written < size)
196 block_free = p->head.block_free[dst_type];
199 if (bf_read (p->data_BFile[dst_type],
200 block_free, 0, sizeof(*p->head.block_free),
201 &p->head.block_free[dst_type]) != 1)
203 yaz_log (YLOG_FATAL|YLOG_ERRNO, "read in %s at free block " ZINT_FORMAT,
204 p->data_fname[dst_type], block_free);
209 block_free = p->head.block_last[dst_type]++;
210 if (block_prev == -1)
212 entry.next = block_free*8 + dst_type;
214 p->head.total_bytes += size;
217 write_indx (p, *sysnos, &entry, sizeof(entry));
223 memcpy (cptr, &block_free, sizeof(block_free));
224 bf_write (p->data_BFile[dst_type], block_prev, 0, 0, cptr);
225 cptr = p->tmp_buf + no_written;
227 block_prev = block_free;
228 no_written += p->head.block_size[dst_type] - sizeof(zint);
229 p->head.block_used[dst_type]++;
231 assert (block_prev != -1);
233 memcpy (cptr, &block_free, sizeof(block_free));
234 bf_write (p->data_BFile[dst_type], block_prev, 0,
235 sizeof(block_free) + (p->tmp_buf+size) - cptr, cptr);
238 Records rec_open (BFiles bfs, int rw, int compression_method)
244 p = (Records) xmalloc (sizeof(*p));
245 p->compression_method = compression_method;
248 p->tmp_buf = (char *) xmalloc (p->tmp_size);
249 p->index_fname = "reci";
250 p->index_BFile = bf_open (bfs, p->index_fname, RIDX_CHUNK, rw);
251 if (p->index_BFile == NULL)
253 yaz_log (YLOG_FATAL|YLOG_ERRNO, "open %s", p->index_fname);
256 r = bf_read (p->index_BFile, 0, 0, 0, p->tmp_buf);
260 memcpy (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic));
261 sprintf (p->head.version, "%3d", REC_VERSION);
262 p->head.index_free = 0;
263 p->head.index_last = 1;
264 p->head.no_records = 0;
265 p->head.total_bytes = 0;
266 for (i = 0; i<REC_BLOCK_TYPES; i++)
268 p->head.block_free[i] = 0;
269 p->head.block_last[i] = 1;
270 p->head.block_used[i] = 0;
272 p->head.block_size[0] = 128;
273 p->head.block_move[0] = 0;
274 for (i = 1; i<REC_BLOCK_TYPES; i++)
276 p->head.block_size[i] = p->head.block_size[i-1] * 4;
277 p->head.block_move[i] = p->head.block_size[i] * 24;
283 memcpy (&p->head, p->tmp_buf, sizeof(p->head));
284 if (memcmp (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic)))
286 yaz_log (YLOG_FATAL, "file %s has bad format", p->index_fname);
289 version = atoi (p->head.version);
290 if (version != REC_VERSION)
292 yaz_log (YLOG_FATAL, "file %s is version %d, but version"
293 " %d is required", p->index_fname, version, REC_VERSION);
298 for (i = 0; i<REC_BLOCK_TYPES; i++)
301 sprintf (str, "recd%c", i + 'A');
302 p->data_fname[i] = (char *) xmalloc (strlen(str)+1);
303 strcpy (p->data_fname[i], str);
304 p->data_BFile[i] = NULL;
306 for (i = 0; i<REC_BLOCK_TYPES; i++)
308 if (!(p->data_BFile[i] = bf_open (bfs, p->data_fname[i],
309 p->head.block_size[i],
312 yaz_log (YLOG_FATAL|YLOG_ERRNO, "bf_open %s", p->data_fname[i]);
318 p->record_cache = (struct record_cache_entry *)
319 xmalloc (sizeof(*p->record_cache)*p->cache_max);
320 zebra_mutex_init (&p->mutex);
324 static void rec_encode_unsigned (unsigned n, unsigned char *buf, int *len)
329 buf[*len] = 128 + (n & 127);
337 static void rec_decode_unsigned(unsigned *np, unsigned char *buf, int *len)
343 while (buf[*len] > 127)
345 n += w*(buf[*len] & 127);
354 static void rec_encode_zint (zint n, unsigned char *buf, int *len)
359 buf[*len] = (unsigned) (128 + (n & 127));
363 buf[*len] = (unsigned) n;
367 static void rec_decode_zint(zint *np, unsigned char *buf, int *len)
373 while (buf[*len] > 127)
375 n += w*(buf[*len] & 127);
384 static void rec_cache_flush_block1 (Records p, Record rec, Record last_rec,
385 char **out_buf, int *out_size,
391 for (i = 0; i<REC_NO_INFO; i++)
393 if (*out_offset + (int) rec->size[i] + 20 > *out_size)
395 int new_size = *out_offset + rec->size[i] + 65536;
396 char *np = (char *) xmalloc (new_size);
398 memcpy (np, *out_buf, *out_offset);
400 *out_size = new_size;
405 rec_encode_zint (rec->sysno, *out_buf + *out_offset, &len);
406 (*out_offset) += len;
408 if (rec->size[i] == 0)
410 rec_encode_unsigned (1, *out_buf + *out_offset, &len);
411 (*out_offset) += len;
413 else if (last_rec && rec->size[i] == last_rec->size[i] &&
414 !memcmp (rec->info[i], last_rec->info[i], rec->size[i]))
416 rec_encode_unsigned (0, *out_buf + *out_offset, &len);
417 (*out_offset) += len;
421 rec_encode_unsigned (rec->size[i]+1, *out_buf + *out_offset, &len);
422 (*out_offset) += len;
423 memcpy (*out_buf + *out_offset, rec->info[i], rec->size[i]);
424 (*out_offset) += rec->size[i];
429 static void rec_write_multiple (Records p, int saveCount)
433 char compression_method;
437 char *out_buf = (char *) xmalloc (out_size);
438 SYSNO *sysnos = (SYSNO *) xmalloc (sizeof(*sysnos) * (p->cache_cur + 1));
439 SYSNO *sysnop = sysnos;
441 for (i = 0; i<p->cache_cur - saveCount; i++)
443 struct record_cache_entry *e = p->record_cache + i;
447 rec_cache_flush_block1 (p, e->rec, last_rec, &out_buf,
448 &out_size, &out_offset);
449 *sysnop++ = e->rec->sysno;
451 e->flag = recordFlagNop;
454 case recordFlagWrite:
455 rec_release_blocks (p, e->rec->sysno);
456 rec_cache_flush_block1 (p, e->rec, last_rec, &out_buf,
457 &out_size, &out_offset);
458 *sysnop++ = e->rec->sysno;
460 e->flag = recordFlagNop;
463 case recordFlagDelete:
464 rec_delete_single (p, e->rec);
465 e->flag = recordFlagNop;
475 int csize = 0; /* indicate compression "not performed yet" */
476 compression_method = p->compression_method;
477 switch (compression_method)
479 case REC_COMPRESS_BZIP2:
481 csize = out_offset + (out_offset >> 6) + 620;
482 rec_tmp_expand (p, csize);
483 #ifdef BZ_CONFIG_ERROR
484 i = BZ2_bzBuffToBuffCompress
486 i = bzBuffToBuffCompress
488 (p->tmp_buf+sizeof(zint)+sizeof(short)+
490 &csize, out_buf, out_offset, 1, 0, 30);
493 yaz_log (YLOG_WARN, "bzBuffToBuffCompress error code=%d", i);
496 yaz_log (YLOG_LOG, "compress %4d %5d %5d", ref_count, out_offset,
500 case REC_COMPRESS_NONE:
505 /* either no compression or compression not supported ... */
507 rec_tmp_expand (p, csize);
508 memcpy (p->tmp_buf + sizeof(zint) + sizeof(short) + sizeof(char),
509 out_buf, out_offset);
511 compression_method = REC_COMPRESS_NONE;
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));
517 /* -------- compression */
518 rec_write_tmp_buf (p, csize + sizeof(short) + sizeof(char), sysnos);
524 static void rec_cache_flush (Records p, int saveCount)
528 if (saveCount >= p->cache_cur)
531 rec_write_multiple (p, saveCount);
533 for (i = 0; i<p->cache_cur - saveCount; i++)
535 struct record_cache_entry *e = p->record_cache + i;
538 /* i still being used ... */
539 for (j = 0; j<saveCount; j++, i++)
540 memcpy (p->record_cache+j, p->record_cache+i,
541 sizeof(*p->record_cache));
542 p->cache_cur = saveCount;
545 static Record *rec_cache_lookup (Records p, SYSNO sysno,
546 enum recordCacheFlag flag)
549 for (i = 0; i<p->cache_cur; i++)
551 struct record_cache_entry *e = p->record_cache + i;
552 if (e->rec->sysno == sysno)
554 if (flag != recordFlagNop && e->flag == recordFlagNop)
562 static void rec_cache_insert (Records p, Record rec, enum recordCacheFlag flag)
564 struct record_cache_entry *e;
566 if (p->cache_cur == p->cache_max)
567 rec_cache_flush (p, 1);
568 else if (p->cache_cur > 0)
572 for (i = 0; i<p->cache_cur; i++)
574 Record r = (p->record_cache + i)->rec;
575 for (j = 0; j<REC_NO_INFO; j++)
579 rec_cache_flush (p, 1);
581 assert (p->cache_cur < p->cache_max);
583 e = p->record_cache + (p->cache_cur)++;
585 e->rec = rec_cp (rec);
588 void rec_close (Records *pp)
595 zebra_mutex_destroy (&p->mutex);
596 rec_cache_flush (p, 0);
597 xfree (p->record_cache);
603 bf_close (p->index_BFile);
605 for (i = 0; i<REC_BLOCK_TYPES; i++)
607 if (p->data_BFile[i])
608 bf_close (p->data_BFile[i]);
609 xfree (p->data_fname[i]);
616 static Record rec_get_int (Records p, SYSNO sysno)
620 struct record_index_entry entry;
629 char compression_method;
634 if ((recp = rec_cache_lookup (p, sysno, recordFlagNop)))
635 return rec_cp (*recp);
637 if (read_indx (p, sysno, &entry, sizeof(entry), 1) < 1)
638 return NULL; /* record is not there! */
641 return NULL; /* record is deleted */
643 dst_type = (int) (entry.next & 7);
644 assert (dst_type < REC_BLOCK_TYPES);
645 freeblock = entry.next / 8;
647 assert (freeblock > 0);
649 rec_tmp_expand (p, entry.size);
652 r = bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
655 memcpy (&freeblock, cptr, sizeof(freeblock));
661 cptr += p->head.block_size[dst_type] - sizeof(freeblock);
663 memcpy (&tmp, cptr, sizeof(tmp));
664 r = bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
667 memcpy (&freeblock, cptr, sizeof(freeblock));
668 memcpy (cptr, &tmp, sizeof(tmp));
671 rec = (Record) xmalloc (sizeof(*rec));
673 memcpy (&compression_method, p->tmp_buf + sizeof(zint) + sizeof(short),
674 sizeof(compression_method));
675 in_buf = p->tmp_buf + sizeof(zint) + sizeof(short) + sizeof(char);
676 in_size = entry.size - sizeof(short) - sizeof(char);
677 switch (compression_method)
679 case REC_COMPRESS_BZIP2:
681 bz_size = entry.size * 20 + 100;
684 bz_buf = (char *) xmalloc (bz_size);
685 #ifdef BZ_CONFIG_ERROR
686 i = BZ2_bzBuffToBuffDecompress
688 i = bzBuffToBuffDecompress
690 (bz_buf, &bz_size, in_buf, in_size, 0, 0);
691 yaz_log (YLOG_LOG, "decompress %5d %5d", in_size, bz_size);
694 yaz_log (YLOG_LOG, "failed");
701 yaz_log (YLOG_FATAL, "cannot decompress record(s) in BZIP2 format");
705 case REC_COMPRESS_NONE:
708 for (i = 0; i<REC_NO_INFO; i++)
711 nptr = in_buf; /* skip ref count */
712 while (nptr < in_buf + in_size)
716 rec_decode_zint (&this_sysno, nptr, &len);
719 for (i = 0; i < REC_NO_INFO; i++)
722 rec_decode_unsigned (&this_size, nptr, &len);
727 rec->size[i] = this_size-1;
732 nptr += rec->size[i];
737 if (this_sysno == sysno)
740 for (i = 0; i<REC_NO_INFO; i++)
742 if (rec->info[i] && rec->size[i])
744 char *np = xmalloc (rec->size[i]+1);
745 memcpy (np, rec->info[i], rec->size[i]);
746 np[rec->size[i]] = '\0';
751 assert (rec->info[i] == 0);
752 assert (rec->size[i] == 0);
756 rec_cache_insert (p, rec, recordFlagNop);
760 Record rec_get (Records p, SYSNO sysno)
763 zebra_mutex_lock (&p->mutex);
765 rec = rec_get_int (p, sysno);
766 zebra_mutex_unlock (&p->mutex);
770 static Record rec_new_int (Records p)
777 rec = (Record) xmalloc (sizeof(*rec));
778 if (1 || p->head.index_free == 0)
779 sysno = (p->head.index_last)++;
782 struct record_index_entry entry;
784 read_indx (p, p->head.index_free, &entry, sizeof(entry), 0);
785 sysno = p->head.index_free;
786 p->head.index_free = entry.next;
788 (p->head.no_records)++;
790 for (i = 0; i < REC_NO_INFO; i++)
795 rec_cache_insert (p, rec, recordFlagNew);
799 Record rec_new (Records p)
802 zebra_mutex_lock (&p->mutex);
804 rec = rec_new_int (p);
805 zebra_mutex_unlock (&p->mutex);
809 void rec_del (Records p, Record *recpp)
813 zebra_mutex_lock (&p->mutex);
814 (p->head.no_records)--;
815 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagDelete)))
822 rec_cache_insert (p, *recpp, recordFlagDelete);
825 zebra_mutex_unlock (&p->mutex);
829 void rec_put (Records p, Record *recpp)
833 zebra_mutex_lock (&p->mutex);
834 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagWrite)))
841 rec_cache_insert (p, *recpp, recordFlagWrite);
844 zebra_mutex_unlock (&p->mutex);
848 void rec_rm (Record *recpp)
854 for (i = 0; i < REC_NO_INFO; i++)
855 xfree ((*recpp)->info[i]);
860 Record rec_cp (Record rec)
865 n = (Record) xmalloc (sizeof(*n));
866 n->sysno = rec->sysno;
867 for (i = 0; i < REC_NO_INFO; i++)
875 n->size[i] = rec->size[i];
876 n->info[i] = (char *) xmalloc (rec->size[i]);
877 memcpy (n->info[i], rec->info[i], rec->size[i]);
883 char *rec_strdup (const char *s, size_t *len)
893 p = (char *) xmalloc (*len);