1 /* $Id: recindex.c,v 1.40 2005-01-15 13:03:29 adam 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 + (int)
71 (p->head.block_size[REC_BLOCK_TYPES-1])*2 + 2048;
72 p->tmp_buf = (char *) xmalloc (p->tmp_size);
76 static int read_indx (Records p, SYSNO sysno, void *buf, int itemsize,
80 zint pos = (sysno-1)*itemsize;
81 int off = (int) (pos%RIDX_CHUNK);
82 int sz1 = RIDX_CHUNK - off; /* sz1 is size of buffer to read.. */
85 sz1 = itemsize; /* no more than itemsize bytes */
87 r = bf_read (p->index_BFile, 1+pos/RIDX_CHUNK, off, sz1, buf);
88 if (r == 1 && sz1 < itemsize) /* boundary? - must read second part */
89 r = bf_read (p->index_BFile, 2+pos/RIDX_CHUNK, 0, itemsize - sz1,
91 if (r != 1 && !ignoreError)
93 yaz_log (YLOG_FATAL|YLOG_ERRNO, "read in %s at pos %ld",
94 p->index_fname, (long) pos);
100 static void write_indx (Records p, SYSNO sysno, void *buf, int itemsize)
102 zint pos = (sysno-1)*itemsize;
103 int off = (int) (pos%RIDX_CHUNK);
104 int sz1 = RIDX_CHUNK - off; /* sz1 is size of buffer to read.. */
107 sz1 = itemsize; /* no more than itemsize bytes */
109 bf_write(p->index_BFile, 1+pos/RIDX_CHUNK, off, sz1, buf);
110 if (sz1 < itemsize) /* boundary? must write second part */
111 bf_write(p->index_BFile, 2+pos/RIDX_CHUNK, 0, itemsize - sz1,
115 static void rec_release_blocks (Records p, SYSNO sysno)
117 struct record_index_entry entry;
119 char block_and_ref[sizeof(zint) + sizeof(short)];
123 if (read_indx (p, sysno, &entry, sizeof(entry), 1) != 1)
126 freeblock = entry.next;
127 assert (freeblock > 0);
128 dst_type = (int) (freeblock & 7);
129 assert (dst_type < REC_BLOCK_TYPES);
130 freeblock = freeblock / 8;
133 if (bf_read (p->data_BFile[dst_type], freeblock, 0,
134 first ? sizeof(block_and_ref) : sizeof(zint),
137 yaz_log (YLOG_FATAL|YLOG_ERRNO, "read in rec_del_single");
143 memcpy (&ref, block_and_ref + sizeof(freeblock), sizeof(ref));
145 memcpy (block_and_ref + sizeof(freeblock), &ref, sizeof(ref));
148 if (bf_write (p->data_BFile[dst_type], freeblock, 0,
149 sizeof(block_and_ref), block_and_ref))
151 yaz_log (YLOG_FATAL|YLOG_ERRNO, "write in rec_del_single");
159 if (bf_write (p->data_BFile[dst_type], freeblock, 0, sizeof(freeblock),
160 &p->head.block_free[dst_type]))
162 yaz_log (YLOG_FATAL|YLOG_ERRNO, "write in rec_del_single");
165 p->head.block_free[dst_type] = freeblock;
166 memcpy (&freeblock, block_and_ref, sizeof(freeblock));
168 p->head.block_used[dst_type]--;
170 p->head.total_bytes -= entry.size;
173 static void rec_delete_single (Records p, Record rec)
175 struct record_index_entry entry;
177 rec_release_blocks (p, rec->sysno);
179 entry.next = p->head.index_free;
181 p->head.index_free = rec->sysno;
182 write_indx (p, rec->sysno, &entry, sizeof(entry));
185 static void rec_write_tmp_buf (Records p, int size, SYSNO *sysnos)
187 struct record_index_entry entry;
189 char *cptr = p->tmp_buf;
190 zint block_prev = -1, block_free;
194 for (i = 1; i<REC_BLOCK_TYPES; i++)
195 if (size >= p->head.block_move[i])
197 while (no_written < size)
199 block_free = p->head.block_free[dst_type];
202 if (bf_read (p->data_BFile[dst_type],
203 block_free, 0, sizeof(*p->head.block_free),
204 &p->head.block_free[dst_type]) != 1)
206 yaz_log (YLOG_FATAL|YLOG_ERRNO, "read in %s at free block " ZINT_FORMAT,
207 p->data_fname[dst_type], block_free);
212 block_free = p->head.block_last[dst_type]++;
213 if (block_prev == -1)
215 entry.next = block_free*8 + dst_type;
217 p->head.total_bytes += size;
220 write_indx (p, *sysnos, &entry, sizeof(entry));
226 memcpy (cptr, &block_free, sizeof(block_free));
227 bf_write (p->data_BFile[dst_type], block_prev, 0, 0, cptr);
228 cptr = p->tmp_buf + no_written;
230 block_prev = block_free;
231 no_written += (int)(p->head.block_size[dst_type]) - sizeof(zint);
232 p->head.block_used[dst_type]++;
234 assert (block_prev != -1);
236 memcpy (cptr, &block_free, sizeof(block_free));
237 bf_write (p->data_BFile[dst_type], block_prev, 0,
238 sizeof(block_free) + (p->tmp_buf+size) - cptr, cptr);
241 Records rec_open (BFiles bfs, int rw, int compression_method)
247 p = (Records) xmalloc (sizeof(*p));
248 p->compression_method = compression_method;
251 p->tmp_buf = (char *) xmalloc (p->tmp_size);
252 p->index_fname = "reci";
253 p->index_BFile = bf_open (bfs, p->index_fname, RIDX_CHUNK, rw);
254 if (p->index_BFile == NULL)
256 yaz_log (YLOG_FATAL|YLOG_ERRNO, "open %s", p->index_fname);
259 r = bf_read (p->index_BFile, 0, 0, 0, p->tmp_buf);
263 memcpy (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic));
264 sprintf (p->head.version, "%3d", REC_VERSION);
265 p->head.index_free = 0;
266 p->head.index_last = 1;
267 p->head.no_records = 0;
268 p->head.total_bytes = 0;
269 for (i = 0; i<REC_BLOCK_TYPES; i++)
271 p->head.block_free[i] = 0;
272 p->head.block_last[i] = 1;
273 p->head.block_used[i] = 0;
275 p->head.block_size[0] = 128;
276 p->head.block_move[0] = 0;
277 for (i = 1; i<REC_BLOCK_TYPES; i++)
279 p->head.block_size[i] = p->head.block_size[i-1] * 4;
280 p->head.block_move[i] = p->head.block_size[i] * 24;
286 memcpy (&p->head, p->tmp_buf, sizeof(p->head));
287 if (memcmp (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic)))
289 yaz_log (YLOG_FATAL, "file %s has bad format", p->index_fname);
292 version = atoi (p->head.version);
293 if (version != REC_VERSION)
295 yaz_log (YLOG_FATAL, "file %s is version %d, but version"
296 " %d is required", p->index_fname, version, REC_VERSION);
301 for (i = 0; i<REC_BLOCK_TYPES; i++)
304 sprintf (str, "recd%c", i + 'A');
305 p->data_fname[i] = (char *) xmalloc (strlen(str)+1);
306 strcpy (p->data_fname[i], str);
307 p->data_BFile[i] = NULL;
309 for (i = 0; i<REC_BLOCK_TYPES; i++)
311 if (!(p->data_BFile[i] = bf_open (bfs, p->data_fname[i],
312 (int) (p->head.block_size[i]),
315 yaz_log (YLOG_FATAL|YLOG_ERRNO, "bf_open %s", p->data_fname[i]);
321 p->record_cache = (struct record_cache_entry *)
322 xmalloc (sizeof(*p->record_cache)*p->cache_max);
323 zebra_mutex_init (&p->mutex);
327 static void rec_encode_unsigned (unsigned n, unsigned char *buf, int *len)
332 buf[*len] = 128 + (n & 127);
340 static void rec_decode_unsigned(unsigned *np, unsigned char *buf, int *len)
346 while (buf[*len] > 127)
348 n += w*(buf[*len] & 127);
357 static void rec_encode_zint (zint n, unsigned char *buf, int *len)
362 buf[*len] = (unsigned) (128 + (n & 127));
366 buf[*len] = (unsigned) n;
370 static void rec_decode_zint(zint *np, unsigned char *buf, int *len)
376 while (buf[*len] > 127)
378 n += w*(buf[*len] & 127);
387 static void rec_cache_flush_block1 (Records p, Record rec, Record last_rec,
388 char **out_buf, int *out_size,
394 for (i = 0; i<REC_NO_INFO; i++)
396 if (*out_offset + (int) rec->size[i] + 20 > *out_size)
398 int new_size = *out_offset + rec->size[i] + 65536;
399 char *np = (char *) xmalloc (new_size);
401 memcpy (np, *out_buf, *out_offset);
403 *out_size = new_size;
408 rec_encode_zint (rec->sysno, *out_buf + *out_offset, &len);
409 (*out_offset) += len;
411 if (rec->size[i] == 0)
413 rec_encode_unsigned (1, *out_buf + *out_offset, &len);
414 (*out_offset) += len;
416 else if (last_rec && rec->size[i] == last_rec->size[i] &&
417 !memcmp (rec->info[i], last_rec->info[i], rec->size[i]))
419 rec_encode_unsigned (0, *out_buf + *out_offset, &len);
420 (*out_offset) += len;
424 rec_encode_unsigned (rec->size[i]+1, *out_buf + *out_offset, &len);
425 (*out_offset) += len;
426 memcpy (*out_buf + *out_offset, rec->info[i], rec->size[i]);
427 (*out_offset) += rec->size[i];
432 static void rec_write_multiple (Records p, int saveCount)
436 char compression_method;
440 char *out_buf = (char *) xmalloc (out_size);
441 SYSNO *sysnos = (SYSNO *) xmalloc (sizeof(*sysnos) * (p->cache_cur + 1));
442 SYSNO *sysnop = sysnos;
444 for (i = 0; i<p->cache_cur - saveCount; i++)
446 struct record_cache_entry *e = p->record_cache + i;
450 rec_cache_flush_block1 (p, e->rec, last_rec, &out_buf,
451 &out_size, &out_offset);
452 *sysnop++ = e->rec->sysno;
454 e->flag = recordFlagNop;
457 case recordFlagWrite:
458 rec_release_blocks (p, e->rec->sysno);
459 rec_cache_flush_block1 (p, e->rec, last_rec, &out_buf,
460 &out_size, &out_offset);
461 *sysnop++ = e->rec->sysno;
463 e->flag = recordFlagNop;
466 case recordFlagDelete:
467 rec_delete_single (p, e->rec);
468 e->flag = recordFlagNop;
478 int csize = 0; /* indicate compression "not performed yet" */
479 compression_method = p->compression_method;
480 switch (compression_method)
482 case REC_COMPRESS_BZIP2:
484 csize = out_offset + (out_offset >> 6) + 620;
485 rec_tmp_expand (p, csize);
486 #ifdef BZ_CONFIG_ERROR
487 i = BZ2_bzBuffToBuffCompress
489 i = bzBuffToBuffCompress
491 (p->tmp_buf+sizeof(zint)+sizeof(short)+
493 &csize, out_buf, out_offset, 1, 0, 30);
496 yaz_log (YLOG_WARN, "bzBuffToBuffCompress error code=%d", i);
499 yaz_log (YLOG_LOG, "compress %4d %5d %5d", ref_count, out_offset,
503 case REC_COMPRESS_NONE:
508 /* either no compression or compression not supported ... */
510 rec_tmp_expand (p, csize);
511 memcpy (p->tmp_buf + sizeof(zint) + sizeof(short) + sizeof(char),
512 out_buf, out_offset);
514 compression_method = REC_COMPRESS_NONE;
516 memcpy (p->tmp_buf + sizeof(zint), &ref_count, sizeof(ref_count));
517 memcpy (p->tmp_buf + sizeof(zint)+sizeof(short),
518 &compression_method, sizeof(compression_method));
520 /* -------- compression */
521 rec_write_tmp_buf (p, csize + sizeof(short) + sizeof(char), sysnos);
527 static void rec_cache_flush (Records p, int saveCount)
531 if (saveCount >= p->cache_cur)
534 rec_write_multiple (p, saveCount);
536 for (i = 0; i<p->cache_cur - saveCount; i++)
538 struct record_cache_entry *e = p->record_cache + i;
541 /* i still being used ... */
542 for (j = 0; j<saveCount; j++, i++)
543 memcpy (p->record_cache+j, p->record_cache+i,
544 sizeof(*p->record_cache));
545 p->cache_cur = saveCount;
548 static Record *rec_cache_lookup (Records p, SYSNO sysno,
549 enum recordCacheFlag flag)
552 for (i = 0; i<p->cache_cur; i++)
554 struct record_cache_entry *e = p->record_cache + i;
555 if (e->rec->sysno == sysno)
557 if (flag != recordFlagNop && e->flag == recordFlagNop)
565 static void rec_cache_insert (Records p, Record rec, enum recordCacheFlag flag)
567 struct record_cache_entry *e;
569 if (p->cache_cur == p->cache_max)
570 rec_cache_flush (p, 1);
571 else if (p->cache_cur > 0)
575 for (i = 0; i<p->cache_cur; i++)
577 Record r = (p->record_cache + i)->rec;
578 for (j = 0; j<REC_NO_INFO; j++)
582 rec_cache_flush (p, 1);
584 assert (p->cache_cur < p->cache_max);
586 e = p->record_cache + (p->cache_cur)++;
588 e->rec = rec_cp (rec);
591 void rec_close (Records *pp)
598 zebra_mutex_destroy (&p->mutex);
599 rec_cache_flush (p, 0);
600 xfree (p->record_cache);
606 bf_close (p->index_BFile);
608 for (i = 0; i<REC_BLOCK_TYPES; i++)
610 if (p->data_BFile[i])
611 bf_close (p->data_BFile[i]);
612 xfree (p->data_fname[i]);
619 static Record rec_get_int (Records p, SYSNO sysno)
623 struct record_index_entry entry;
632 char compression_method;
637 if ((recp = rec_cache_lookup (p, sysno, recordFlagNop)))
638 return rec_cp (*recp);
640 if (read_indx (p, sysno, &entry, sizeof(entry), 1) < 1)
641 return NULL; /* record is not there! */
644 return NULL; /* record is deleted */
646 dst_type = (int) (entry.next & 7);
647 assert (dst_type < REC_BLOCK_TYPES);
648 freeblock = entry.next / 8;
650 assert (freeblock > 0);
652 rec_tmp_expand (p, entry.size);
655 r = bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
658 memcpy (&freeblock, cptr, sizeof(freeblock));
664 cptr += p->head.block_size[dst_type] - sizeof(freeblock);
666 memcpy (&tmp, cptr, sizeof(tmp));
667 r = bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
670 memcpy (&freeblock, cptr, sizeof(freeblock));
671 memcpy (cptr, &tmp, sizeof(tmp));
674 rec = (Record) xmalloc (sizeof(*rec));
676 memcpy (&compression_method, p->tmp_buf + sizeof(zint) + sizeof(short),
677 sizeof(compression_method));
678 in_buf = p->tmp_buf + sizeof(zint) + sizeof(short) + sizeof(char);
679 in_size = entry.size - sizeof(short) - sizeof(char);
680 switch (compression_method)
682 case REC_COMPRESS_BZIP2:
684 bz_size = entry.size * 20 + 100;
687 bz_buf = (char *) xmalloc (bz_size);
688 #ifdef BZ_CONFIG_ERROR
689 i = BZ2_bzBuffToBuffDecompress
691 i = bzBuffToBuffDecompress
693 (bz_buf, &bz_size, in_buf, in_size, 0, 0);
694 yaz_log (YLOG_LOG, "decompress %5d %5d", in_size, bz_size);
697 yaz_log (YLOG_LOG, "failed");
704 yaz_log (YLOG_FATAL, "cannot decompress record(s) in BZIP2 format");
708 case REC_COMPRESS_NONE:
711 for (i = 0; i<REC_NO_INFO; i++)
714 nptr = in_buf; /* skip ref count */
715 while (nptr < in_buf + in_size)
719 rec_decode_zint (&this_sysno, nptr, &len);
722 for (i = 0; i < REC_NO_INFO; i++)
725 rec_decode_unsigned (&this_size, nptr, &len);
730 rec->size[i] = this_size-1;
735 nptr += rec->size[i];
740 if (this_sysno == sysno)
743 for (i = 0; i<REC_NO_INFO; i++)
745 if (rec->info[i] && rec->size[i])
747 char *np = xmalloc (rec->size[i]+1);
748 memcpy (np, rec->info[i], rec->size[i]);
749 np[rec->size[i]] = '\0';
754 assert (rec->info[i] == 0);
755 assert (rec->size[i] == 0);
759 rec_cache_insert (p, rec, recordFlagNop);
763 Record rec_get (Records p, SYSNO sysno)
766 zebra_mutex_lock (&p->mutex);
768 rec = rec_get_int (p, sysno);
769 zebra_mutex_unlock (&p->mutex);
773 static Record rec_new_int (Records p)
780 rec = (Record) xmalloc (sizeof(*rec));
781 if (1 || p->head.index_free == 0)
782 sysno = (p->head.index_last)++;
785 struct record_index_entry entry;
787 read_indx (p, p->head.index_free, &entry, sizeof(entry), 0);
788 sysno = p->head.index_free;
789 p->head.index_free = entry.next;
791 (p->head.no_records)++;
793 for (i = 0; i < REC_NO_INFO; i++)
798 rec_cache_insert (p, rec, recordFlagNew);
802 Record rec_new (Records p)
805 zebra_mutex_lock (&p->mutex);
807 rec = rec_new_int (p);
808 zebra_mutex_unlock (&p->mutex);
812 void rec_del (Records p, Record *recpp)
816 zebra_mutex_lock (&p->mutex);
817 (p->head.no_records)--;
818 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagDelete)))
825 rec_cache_insert (p, *recpp, recordFlagDelete);
828 zebra_mutex_unlock (&p->mutex);
832 void rec_put (Records p, Record *recpp)
836 zebra_mutex_lock (&p->mutex);
837 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagWrite)))
844 rec_cache_insert (p, *recpp, recordFlagWrite);
847 zebra_mutex_unlock (&p->mutex);
851 void rec_rm (Record *recpp)
857 for (i = 0; i < REC_NO_INFO; i++)
858 xfree ((*recpp)->info[i]);
863 Record rec_cp (Record rec)
868 n = (Record) xmalloc (sizeof(*n));
869 n->sysno = rec->sysno;
870 for (i = 0; i < REC_NO_INFO; i++)
878 n->size[i] = rec->size[i];
879 n->info[i] = (char *) xmalloc (rec->size[i]);
880 memcpy (n->info[i], rec->info[i], rec->size[i]);
886 char *rec_strdup (const char *s, size_t *len)
896 p = (char *) xmalloc (*len);