1 /* $Id: recindex.c,v 1.43 2005-04-15 10:47:48 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>
51 static void rec_write_head (Records p)
56 assert (p->index_BFile);
58 r = bf_write (p->index_BFile, 0, 0, sizeof(p->head), &p->head);
61 yaz_log (YLOG_FATAL|YLOG_ERRNO, "write head of %s", p->index_fname);
66 static void rec_tmp_expand (Records p, int size)
68 if (p->tmp_size < size + 2048 ||
69 p->tmp_size < p->head.block_size[REC_BLOCK_TYPES-1]*2)
72 p->tmp_size = size + (int)
73 (p->head.block_size[REC_BLOCK_TYPES-1])*2 + 2048;
74 p->tmp_buf = (char *) xmalloc (p->tmp_size);
78 static int read_indx (Records p, SYSNO sysno, void *buf, int itemsize,
82 zint pos = (sysno-1)*itemsize;
83 int off = (int) (pos%RIDX_CHUNK);
84 int sz1 = RIDX_CHUNK - off; /* sz1 is size of buffer to read.. */
87 sz1 = itemsize; /* no more than itemsize bytes */
89 r = bf_read (p->index_BFile, 1+pos/RIDX_CHUNK, off, sz1, buf);
90 if (r == 1 && sz1 < itemsize) /* boundary? - must read second part */
91 r = bf_read (p->index_BFile, 2+pos/RIDX_CHUNK, 0, itemsize - sz1,
93 if (r != 1 && !ignoreError)
95 yaz_log (YLOG_FATAL|YLOG_ERRNO, "read in %s at pos %ld",
96 p->index_fname, (long) pos);
102 static void write_indx (Records p, SYSNO sysno, void *buf, int itemsize)
104 zint pos = (sysno-1)*itemsize;
105 int off = (int) (pos%RIDX_CHUNK);
106 int sz1 = RIDX_CHUNK - off; /* sz1 is size of buffer to read.. */
109 sz1 = itemsize; /* no more than itemsize bytes */
111 bf_write(p->index_BFile, 1+pos/RIDX_CHUNK, off, sz1, buf);
112 if (sz1 < itemsize) /* boundary? must write second part */
113 bf_write(p->index_BFile, 2+pos/RIDX_CHUNK, 0, itemsize - sz1,
117 static void rec_release_blocks (Records p, SYSNO sysno)
119 struct record_index_entry entry;
121 char block_and_ref[sizeof(zint) + sizeof(short)];
125 if (read_indx (p, sysno, &entry, sizeof(entry), 1) != 1)
128 freeblock = entry.next;
129 assert (freeblock > 0);
130 dst_type = (int) (freeblock & 7);
131 assert (dst_type < REC_BLOCK_TYPES);
132 freeblock = freeblock / 8;
135 if (bf_read (p->data_BFile[dst_type], freeblock, 0,
136 first ? sizeof(block_and_ref) : sizeof(zint),
139 yaz_log (YLOG_FATAL|YLOG_ERRNO, "read in rec_del_single");
145 memcpy (&ref, block_and_ref + sizeof(freeblock), sizeof(ref));
147 memcpy (block_and_ref + sizeof(freeblock), &ref, sizeof(ref));
150 if (bf_write (p->data_BFile[dst_type], freeblock, 0,
151 sizeof(block_and_ref), block_and_ref))
153 yaz_log (YLOG_FATAL|YLOG_ERRNO, "write in rec_del_single");
161 if (bf_write (p->data_BFile[dst_type], freeblock, 0, sizeof(freeblock),
162 &p->head.block_free[dst_type]))
164 yaz_log (YLOG_FATAL|YLOG_ERRNO, "write in rec_del_single");
167 p->head.block_free[dst_type] = freeblock;
168 memcpy (&freeblock, block_and_ref, sizeof(freeblock));
170 p->head.block_used[dst_type]--;
172 p->head.total_bytes -= entry.size;
175 static void rec_delete_single (Records p, Record rec)
177 struct record_index_entry entry;
179 rec_release_blocks (p, rec->sysno);
181 entry.next = p->head.index_free;
183 p->head.index_free = rec->sysno;
184 write_indx (p, rec->sysno, &entry, sizeof(entry));
187 static void rec_write_tmp_buf (Records p, int size, SYSNO *sysnos)
189 struct record_index_entry entry;
191 char *cptr = p->tmp_buf;
192 zint block_prev = -1, block_free;
196 for (i = 1; i<REC_BLOCK_TYPES; i++)
197 if (size >= p->head.block_move[i])
199 while (no_written < size)
201 block_free = p->head.block_free[dst_type];
204 if (bf_read (p->data_BFile[dst_type],
205 block_free, 0, sizeof(*p->head.block_free),
206 &p->head.block_free[dst_type]) != 1)
208 yaz_log (YLOG_FATAL|YLOG_ERRNO, "read in %s at free block " ZINT_FORMAT,
209 p->data_fname[dst_type], block_free);
214 block_free = p->head.block_last[dst_type]++;
215 if (block_prev == -1)
217 entry.next = block_free*8 + dst_type;
219 p->head.total_bytes += size;
222 write_indx (p, *sysnos, &entry, sizeof(entry));
228 memcpy (cptr, &block_free, sizeof(block_free));
229 bf_write (p->data_BFile[dst_type], block_prev, 0, 0, cptr);
230 cptr = p->tmp_buf + no_written;
232 block_prev = block_free;
233 no_written += (int)(p->head.block_size[dst_type]) - sizeof(zint);
234 p->head.block_used[dst_type]++;
236 assert (block_prev != -1);
238 memcpy (cptr, &block_free, sizeof(block_free));
239 bf_write (p->data_BFile[dst_type], block_prev, 0,
240 sizeof(block_free) + (p->tmp_buf+size) - cptr, cptr);
243 Records rec_open (BFiles bfs, int rw, int compression_method)
249 p = (Records) xmalloc (sizeof(*p));
250 p->compression_method = compression_method;
253 p->tmp_buf = (char *) xmalloc (p->tmp_size);
254 p->index_fname = "reci";
255 p->index_BFile = bf_open (bfs, p->index_fname, RIDX_CHUNK, rw);
256 if (p->index_BFile == NULL)
258 yaz_log (YLOG_FATAL|YLOG_ERRNO, "open %s", p->index_fname);
261 r = bf_read (p->index_BFile, 0, 0, 0, p->tmp_buf);
265 memcpy (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic));
266 sprintf (p->head.version, "%3d", REC_VERSION);
267 p->head.index_free = 0;
268 p->head.index_last = 1;
269 p->head.no_records = 0;
270 p->head.total_bytes = 0;
271 for (i = 0; i<REC_BLOCK_TYPES; i++)
273 p->head.block_free[i] = 0;
274 p->head.block_last[i] = 1;
275 p->head.block_used[i] = 0;
277 p->head.block_size[0] = 128;
278 p->head.block_move[0] = 0;
279 for (i = 1; i<REC_BLOCK_TYPES; i++)
281 p->head.block_size[i] = p->head.block_size[i-1] * 4;
282 p->head.block_move[i] = p->head.block_size[i] * 24;
288 memcpy (&p->head, p->tmp_buf, sizeof(p->head));
289 if (memcmp (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic)))
291 yaz_log (YLOG_FATAL, "file %s has bad format", p->index_fname);
294 version = atoi (p->head.version);
295 if (version != REC_VERSION)
297 yaz_log (YLOG_FATAL, "file %s is version %d, but version"
298 " %d is required", p->index_fname, version, REC_VERSION);
303 for (i = 0; i<REC_BLOCK_TYPES; i++)
306 sprintf (str, "recd%c", i + 'A');
307 p->data_fname[i] = (char *) xmalloc (strlen(str)+1);
308 strcpy (p->data_fname[i], str);
309 p->data_BFile[i] = NULL;
311 for (i = 0; i<REC_BLOCK_TYPES; i++)
313 if (!(p->data_BFile[i] = bf_open (bfs, p->data_fname[i],
314 (int) (p->head.block_size[i]),
317 yaz_log (YLOG_FATAL|YLOG_ERRNO, "bf_open %s", p->data_fname[i]);
323 p->record_cache = (struct record_cache_entry *)
324 xmalloc (sizeof(*p->record_cache)*p->cache_max);
325 zebra_mutex_init (&p->mutex);
329 static void rec_encode_unsigned (unsigned n, unsigned char *buf, int *len)
334 buf[*len] = 128 + (n & 127);
342 static void rec_decode_unsigned(unsigned *np, unsigned char *buf, int *len)
348 while (buf[*len] > 127)
350 n += w*(buf[*len] & 127);
359 static void rec_encode_zint (zint n, unsigned char *buf, int *len)
364 buf[*len] = (unsigned) (128 + (n & 127));
368 buf[*len] = (unsigned) n;
372 static void rec_decode_zint(zint *np, unsigned char *buf, int *len)
378 while (buf[*len] > 127)
380 n += w*(buf[*len] & 127);
389 static void rec_cache_flush_block1 (Records p, Record rec, Record last_rec,
390 char **out_buf, int *out_size,
396 for (i = 0; i<REC_NO_INFO; i++)
398 if (*out_offset + (int) rec->size[i] + 20 > *out_size)
400 int new_size = *out_offset + rec->size[i] + 65536;
401 char *np = (char *) xmalloc (new_size);
403 memcpy (np, *out_buf, *out_offset);
405 *out_size = new_size;
410 rec_encode_zint (rec->sysno, *out_buf + *out_offset, &len);
411 (*out_offset) += len;
413 if (rec->size[i] == 0)
415 rec_encode_unsigned (1, *out_buf + *out_offset, &len);
416 (*out_offset) += len;
418 else if (last_rec && rec->size[i] == last_rec->size[i] &&
419 !memcmp (rec->info[i], last_rec->info[i], rec->size[i]))
421 rec_encode_unsigned (0, *out_buf + *out_offset, &len);
422 (*out_offset) += len;
426 rec_encode_unsigned (rec->size[i]+1, *out_buf + *out_offset, &len);
427 (*out_offset) += len;
428 memcpy (*out_buf + *out_offset, rec->info[i], rec->size[i]);
429 (*out_offset) += rec->size[i];
434 static void rec_write_multiple (Records p, int saveCount)
438 char compression_method;
442 char *out_buf = (char *) xmalloc (out_size);
443 SYSNO *sysnos = (SYSNO *) xmalloc (sizeof(*sysnos) * (p->cache_cur + 1));
444 SYSNO *sysnop = sysnos;
446 for (i = 0; i<p->cache_cur - saveCount; i++)
448 struct record_cache_entry *e = p->record_cache + i;
452 rec_cache_flush_block1 (p, e->rec, last_rec, &out_buf,
453 &out_size, &out_offset);
454 *sysnop++ = e->rec->sysno;
456 e->flag = recordFlagNop;
459 case recordFlagWrite:
460 rec_release_blocks (p, e->rec->sysno);
461 rec_cache_flush_block1 (p, e->rec, last_rec, &out_buf,
462 &out_size, &out_offset);
463 *sysnop++ = e->rec->sysno;
465 e->flag = recordFlagNop;
468 case recordFlagDelete:
469 rec_delete_single (p, e->rec);
470 e->flag = recordFlagNop;
480 int csize = 0; /* indicate compression "not performed yet" */
481 compression_method = p->compression_method;
482 switch (compression_method)
484 case REC_COMPRESS_BZIP2:
486 csize = out_offset + (out_offset >> 6) + 620;
487 rec_tmp_expand (p, csize);
488 #ifdef BZ_CONFIG_ERROR
489 i = BZ2_bzBuffToBuffCompress
491 i = bzBuffToBuffCompress
493 (p->tmp_buf+sizeof(zint)+sizeof(short)+
495 &csize, out_buf, out_offset, 1, 0, 30);
498 yaz_log (YLOG_WARN, "bzBuffToBuffCompress error code=%d", i);
501 yaz_log (YLOG_LOG, "compress %4d %5d %5d", ref_count, out_offset,
505 case REC_COMPRESS_NONE:
510 /* either no compression or compression not supported ... */
512 rec_tmp_expand (p, csize);
513 memcpy (p->tmp_buf + sizeof(zint) + sizeof(short) + sizeof(char),
514 out_buf, out_offset);
516 compression_method = REC_COMPRESS_NONE;
518 memcpy (p->tmp_buf + sizeof(zint), &ref_count, sizeof(ref_count));
519 memcpy (p->tmp_buf + sizeof(zint)+sizeof(short),
520 &compression_method, sizeof(compression_method));
522 /* -------- compression */
523 rec_write_tmp_buf (p, csize + sizeof(short) + sizeof(char), sysnos);
529 static void rec_cache_flush (Records p, int saveCount)
533 if (saveCount >= p->cache_cur)
536 rec_write_multiple (p, saveCount);
538 for (i = 0; i<p->cache_cur - saveCount; i++)
540 struct record_cache_entry *e = p->record_cache + i;
543 /* i still being used ... */
544 for (j = 0; j<saveCount; j++, i++)
545 memcpy (p->record_cache+j, p->record_cache+i,
546 sizeof(*p->record_cache));
547 p->cache_cur = saveCount;
550 static Record *rec_cache_lookup (Records p, SYSNO sysno,
551 enum recordCacheFlag flag)
554 for (i = 0; i<p->cache_cur; i++)
556 struct record_cache_entry *e = p->record_cache + i;
557 if (e->rec->sysno == sysno)
559 if (flag != recordFlagNop && e->flag == recordFlagNop)
567 static void rec_cache_insert (Records p, Record rec, enum recordCacheFlag flag)
569 struct record_cache_entry *e;
571 if (p->cache_cur == p->cache_max)
572 rec_cache_flush (p, 1);
573 else if (p->cache_cur > 0)
577 for (i = 0; i<p->cache_cur; i++)
579 Record r = (p->record_cache + i)->rec;
580 for (j = 0; j<REC_NO_INFO; j++)
584 rec_cache_flush (p, 1);
586 assert (p->cache_cur < p->cache_max);
588 e = p->record_cache + (p->cache_cur)++;
590 e->rec = rec_cp (rec);
593 void rec_close (Records *pp)
600 zebra_mutex_destroy (&p->mutex);
601 rec_cache_flush (p, 0);
602 xfree (p->record_cache);
608 bf_close (p->index_BFile);
610 for (i = 0; i<REC_BLOCK_TYPES; i++)
612 if (p->data_BFile[i])
613 bf_close (p->data_BFile[i]);
614 xfree (p->data_fname[i]);
621 static Record rec_get_int (Records p, SYSNO sysno)
625 struct record_index_entry entry;
634 char compression_method;
639 if ((recp = rec_cache_lookup (p, sysno, recordFlagNop)))
640 return rec_cp (*recp);
642 if (read_indx (p, sysno, &entry, sizeof(entry), 1) < 1)
643 return NULL; /* record is not there! */
646 return NULL; /* record is deleted */
648 dst_type = (int) (entry.next & 7);
649 assert (dst_type < REC_BLOCK_TYPES);
650 freeblock = entry.next / 8;
652 assert (freeblock > 0);
654 rec_tmp_expand (p, entry.size);
657 r = bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
660 memcpy (&freeblock, cptr, sizeof(freeblock));
666 cptr += p->head.block_size[dst_type] - sizeof(freeblock);
668 memcpy (&tmp, cptr, sizeof(tmp));
669 r = bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
672 memcpy (&freeblock, cptr, sizeof(freeblock));
673 memcpy (cptr, &tmp, sizeof(tmp));
676 rec = (Record) xmalloc (sizeof(*rec));
678 memcpy (&compression_method, p->tmp_buf + sizeof(zint) + sizeof(short),
679 sizeof(compression_method));
680 in_buf = p->tmp_buf + sizeof(zint) + sizeof(short) + sizeof(char);
681 in_size = entry.size - sizeof(short) - sizeof(char);
682 switch (compression_method)
684 case REC_COMPRESS_BZIP2:
686 bz_size = entry.size * 20 + 100;
689 bz_buf = (char *) xmalloc (bz_size);
690 #ifdef BZ_CONFIG_ERROR
691 i = BZ2_bzBuffToBuffDecompress
693 i = bzBuffToBuffDecompress
695 (bz_buf, &bz_size, in_buf, in_size, 0, 0);
696 yaz_log (YLOG_LOG, "decompress %5d %5d", in_size, bz_size);
699 yaz_log (YLOG_LOG, "failed");
706 yaz_log (YLOG_FATAL, "cannot decompress record(s) in BZIP2 format");
710 case REC_COMPRESS_NONE:
713 for (i = 0; i<REC_NO_INFO; i++)
716 nptr = in_buf; /* skip ref count */
717 while (nptr < in_buf + in_size)
721 rec_decode_zint (&this_sysno, nptr, &len);
724 for (i = 0; i < REC_NO_INFO; i++)
727 rec_decode_unsigned (&this_size, nptr, &len);
732 rec->size[i] = this_size-1;
737 nptr += rec->size[i];
742 if (this_sysno == sysno)
745 for (i = 0; i<REC_NO_INFO; i++)
747 if (rec->info[i] && rec->size[i])
749 char *np = xmalloc (rec->size[i]+1);
750 memcpy (np, rec->info[i], rec->size[i]);
751 np[rec->size[i]] = '\0';
756 assert (rec->info[i] == 0);
757 assert (rec->size[i] == 0);
761 rec_cache_insert (p, rec, recordFlagNop);
765 Record rec_get (Records p, SYSNO sysno)
768 zebra_mutex_lock (&p->mutex);
770 rec = rec_get_int (p, sysno);
771 zebra_mutex_unlock (&p->mutex);
775 static Record rec_new_int (Records p)
782 rec = (Record) xmalloc (sizeof(*rec));
783 if (1 || p->head.index_free == 0)
784 sysno = (p->head.index_last)++;
787 struct record_index_entry entry;
789 read_indx (p, p->head.index_free, &entry, sizeof(entry), 0);
790 sysno = p->head.index_free;
791 p->head.index_free = entry.next;
793 (p->head.no_records)++;
795 for (i = 0; i < REC_NO_INFO; i++)
800 rec_cache_insert (p, rec, recordFlagNew);
804 Record rec_new (Records p)
807 zebra_mutex_lock (&p->mutex);
809 rec = rec_new_int (p);
810 zebra_mutex_unlock (&p->mutex);
814 void rec_del (Records p, Record *recpp)
818 zebra_mutex_lock (&p->mutex);
819 (p->head.no_records)--;
820 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagDelete)))
827 rec_cache_insert (p, *recpp, recordFlagDelete);
830 zebra_mutex_unlock (&p->mutex);
834 void rec_put (Records p, Record *recpp)
838 zebra_mutex_lock (&p->mutex);
839 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagWrite)))
846 rec_cache_insert (p, *recpp, recordFlagWrite);
849 zebra_mutex_unlock (&p->mutex);
853 void rec_rm (Record *recpp)
859 for (i = 0; i < REC_NO_INFO; i++)
860 xfree ((*recpp)->info[i]);
865 Record rec_cp (Record rec)
870 n = (Record) xmalloc (sizeof(*n));
871 n->sysno = rec->sysno;
872 for (i = 0; i < REC_NO_INFO; i++)
880 n->size[i] = rec->size[i];
881 n->info[i] = (char *) xmalloc (rec->size[i]);
882 memcpy (n->info[i], rec->info[i], rec->size[i]);
888 char *rec_strdup (const char *s, size_t *len)
898 p = (char *) xmalloc (*len);