1 /* $Id: recindex.c,v 1.44 2005-05-11 12:36:45 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 "
210 p->data_fname[dst_type], block_free);
215 block_free = p->head.block_last[dst_type]++;
216 if (block_prev == -1)
218 entry.next = block_free*8 + dst_type;
220 p->head.total_bytes += size;
223 write_indx (p, *sysnos, &entry, sizeof(entry));
229 memcpy(cptr, &block_free, sizeof(block_free));
230 bf_write(p->data_BFile[dst_type], block_prev, 0, 0, cptr);
231 cptr = p->tmp_buf + no_written;
233 block_prev = block_free;
234 no_written += (int)(p->head.block_size[dst_type]) - sizeof(zint);
235 p->head.block_used[dst_type]++;
237 assert(block_prev != -1);
239 memcpy(cptr, &block_free, sizeof(block_free));
240 bf_write(p->data_BFile[dst_type], block_prev, 0,
241 sizeof(block_free) + (p->tmp_buf+size) - cptr, cptr);
244 Records rec_open(BFiles bfs, int rw, int compression_method)
250 p = (Records) xmalloc (sizeof(*p));
251 p->compression_method = compression_method;
254 p->tmp_buf = (char *) xmalloc (p->tmp_size);
255 p->index_fname = "reci";
256 p->index_BFile = bf_open (bfs, p->index_fname, RIDX_CHUNK, rw);
257 if (p->index_BFile == NULL)
259 yaz_log(YLOG_FATAL|YLOG_ERRNO, "open %s", p->index_fname);
262 r = bf_read(p->index_BFile, 0, 0, 0, p->tmp_buf);
266 memcpy(p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic));
267 sprintf (p->head.version, "%3d", REC_VERSION);
268 p->head.index_free = 0;
269 p->head.index_last = 1;
270 p->head.no_records = 0;
271 p->head.total_bytes = 0;
272 for (i = 0; i<REC_BLOCK_TYPES; i++)
274 p->head.block_free[i] = 0;
275 p->head.block_last[i] = 1;
276 p->head.block_used[i] = 0;
278 p->head.block_size[0] = 128;
279 p->head.block_move[0] = 0;
280 for (i = 1; i<REC_BLOCK_TYPES; i++)
282 p->head.block_size[i] = p->head.block_size[i-1] * 4;
283 p->head.block_move[i] = p->head.block_size[i] * 24;
289 memcpy(&p->head, p->tmp_buf, sizeof(p->head));
290 if (memcmp (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic)))
292 yaz_log(YLOG_FATAL, "file %s has bad format", p->index_fname);
295 version = atoi (p->head.version);
296 if (version != REC_VERSION)
298 yaz_log(YLOG_FATAL, "file %s is version %d, but version"
299 " %d is required", p->index_fname, version, REC_VERSION);
304 for (i = 0; i<REC_BLOCK_TYPES; i++)
307 sprintf (str, "recd%c", i + 'A');
308 p->data_fname[i] = (char *) xmalloc (strlen(str)+1);
309 strcpy(p->data_fname[i], str);
310 p->data_BFile[i] = NULL;
312 for (i = 0; i<REC_BLOCK_TYPES; i++)
314 if (!(p->data_BFile[i] = bf_open (bfs, p->data_fname[i],
315 (int) (p->head.block_size[i]),
318 yaz_log(YLOG_FATAL|YLOG_ERRNO, "bf_open %s", p->data_fname[i]);
324 p->record_cache = (struct record_cache_entry *)
325 xmalloc (sizeof(*p->record_cache)*p->cache_max);
326 zebra_mutex_init (&p->mutex);
330 static void rec_encode_unsigned(unsigned n, unsigned char *buf, int *len)
335 buf[*len] = 128 + (n & 127);
343 static void rec_decode_unsigned(unsigned *np, unsigned char *buf, int *len)
349 while (buf[*len] > 127)
351 n += w*(buf[*len] & 127);
360 static void rec_encode_zint(zint n, unsigned char *buf, int *len)
365 buf[*len] = (unsigned) (128 + (n & 127));
369 buf[*len] = (unsigned) n;
373 static void rec_decode_zint(zint *np, unsigned char *buf, int *len)
379 while (buf[*len] > 127)
381 n += w*(buf[*len] & 127);
390 static void rec_cache_flush_block1(Records p, Record rec, Record last_rec,
391 char **out_buf, int *out_size,
397 for (i = 0; i<REC_NO_INFO; i++)
399 if (*out_offset + (int) rec->size[i] + 20 > *out_size)
401 int new_size = *out_offset + rec->size[i] + 65536;
402 char *np = (char *) xmalloc (new_size);
404 memcpy(np, *out_buf, *out_offset);
406 *out_size = new_size;
411 rec_encode_zint (rec->sysno, *out_buf + *out_offset, &len);
412 (*out_offset) += len;
414 if (rec->size[i] == 0)
416 rec_encode_unsigned (1, *out_buf + *out_offset, &len);
417 (*out_offset) += len;
419 else if (last_rec && rec->size[i] == last_rec->size[i] &&
420 !memcmp (rec->info[i], last_rec->info[i], rec->size[i]))
422 rec_encode_unsigned (0, *out_buf + *out_offset, &len);
423 (*out_offset) += len;
427 rec_encode_unsigned (rec->size[i]+1, *out_buf + *out_offset, &len);
428 (*out_offset) += len;
429 memcpy(*out_buf + *out_offset, rec->info[i], rec->size[i]);
430 (*out_offset) += rec->size[i];
435 static void rec_write_multiple(Records p, int saveCount)
439 char compression_method;
443 char *out_buf = (char *) xmalloc (out_size);
444 SYSNO *sysnos = (SYSNO *) xmalloc (sizeof(*sysnos) * (p->cache_cur + 1));
445 SYSNO *sysnop = sysnos;
447 for (i = 0; i<p->cache_cur - saveCount; i++)
449 struct record_cache_entry *e = p->record_cache + i;
453 rec_cache_flush_block1 (p, e->rec, last_rec, &out_buf,
454 &out_size, &out_offset);
455 *sysnop++ = e->rec->sysno;
457 e->flag = recordFlagNop;
460 case recordFlagWrite:
461 rec_release_blocks (p, e->rec->sysno);
462 rec_cache_flush_block1 (p, e->rec, last_rec, &out_buf,
463 &out_size, &out_offset);
464 *sysnop++ = e->rec->sysno;
466 e->flag = recordFlagNop;
469 case recordFlagDelete:
470 rec_delete_single (p, e->rec);
471 e->flag = recordFlagNop;
481 int csize = 0; /* indicate compression "not performed yet" */
482 compression_method = p->compression_method;
483 switch (compression_method)
485 case REC_COMPRESS_BZIP2:
487 csize = out_offset + (out_offset >> 6) + 620;
488 rec_tmp_expand (p, csize);
489 #ifdef BZ_CONFIG_ERROR
490 i = BZ2_bzBuffToBuffCompress
492 i = bzBuffToBuffCompress
494 (p->tmp_buf+sizeof(zint)+sizeof(short)+
496 &csize, out_buf, out_offset, 1, 0, 30);
499 yaz_log(YLOG_WARN, "bzBuffToBuffCompress error code=%d", i);
502 yaz_log(YLOG_LOG, "compress %4d %5d %5d", ref_count, out_offset,
506 case REC_COMPRESS_NONE:
511 /* either no compression or compression not supported ... */
513 rec_tmp_expand (p, csize);
514 memcpy(p->tmp_buf + sizeof(zint) + sizeof(short) + sizeof(char),
515 out_buf, out_offset);
517 compression_method = REC_COMPRESS_NONE;
519 memcpy(p->tmp_buf + sizeof(zint), &ref_count, sizeof(ref_count));
520 memcpy(p->tmp_buf + sizeof(zint)+sizeof(short),
521 &compression_method, sizeof(compression_method));
523 /* -------- compression */
524 rec_write_tmp_buf (p, csize + sizeof(short) + sizeof(char), sysnos);
530 static void rec_cache_flush(Records p, int saveCount)
534 if (saveCount >= p->cache_cur)
537 rec_write_multiple (p, saveCount);
539 for (i = 0; i<p->cache_cur - saveCount; i++)
541 struct record_cache_entry *e = p->record_cache + i;
544 /* i still being used ... */
545 for (j = 0; j<saveCount; j++, i++)
546 memcpy(p->record_cache+j, p->record_cache+i,
547 sizeof(*p->record_cache));
548 p->cache_cur = saveCount;
551 static Record *rec_cache_lookup(Records p, SYSNO sysno,
552 enum recordCacheFlag flag)
555 for (i = 0; i<p->cache_cur; i++)
557 struct record_cache_entry *e = p->record_cache + i;
558 if (e->rec->sysno == sysno)
560 if (flag != recordFlagNop && e->flag == recordFlagNop)
568 static void rec_cache_insert(Records p, Record rec, enum recordCacheFlag flag)
570 struct record_cache_entry *e;
572 if (p->cache_cur == p->cache_max)
573 rec_cache_flush (p, 1);
574 else if (p->cache_cur > 0)
578 for (i = 0; i<p->cache_cur; i++)
580 Record r = (p->record_cache + i)->rec;
581 for (j = 0; j<REC_NO_INFO; j++)
585 rec_cache_flush (p, 1);
587 assert(p->cache_cur < p->cache_max);
589 e = p->record_cache + (p->cache_cur)++;
591 e->rec = rec_cp (rec);
594 void rec_close(Records *pp)
601 zebra_mutex_destroy(&p->mutex);
602 rec_cache_flush (p, 0);
603 xfree (p->record_cache);
609 bf_close (p->index_BFile);
611 for (i = 0; i<REC_BLOCK_TYPES; i++)
613 if (p->data_BFile[i])
614 bf_close (p->data_BFile[i]);
615 xfree (p->data_fname[i]);
622 static Record rec_get_int(Records p, SYSNO sysno)
626 struct record_index_entry entry;
635 char compression_method;
640 if ((recp = rec_cache_lookup (p, sysno, recordFlagNop)))
641 return rec_cp (*recp);
643 if (read_indx (p, sysno, &entry, sizeof(entry), 1) < 1)
644 return NULL; /* record is not there! */
647 return NULL; /* record is deleted */
649 dst_type = (int) (entry.next & 7);
650 assert(dst_type < REC_BLOCK_TYPES);
651 freeblock = entry.next / 8;
653 assert(freeblock > 0);
655 rec_tmp_expand (p, entry.size);
658 r = bf_read(p->data_BFile[dst_type], freeblock, 0, 0, cptr);
661 memcpy(&freeblock, cptr, sizeof(freeblock));
667 cptr += p->head.block_size[dst_type] - sizeof(freeblock);
669 memcpy(&tmp, cptr, sizeof(tmp));
670 r = bf_read(p->data_BFile[dst_type], freeblock, 0, 0, cptr);
673 memcpy(&freeblock, cptr, sizeof(freeblock));
674 memcpy(cptr, &tmp, sizeof(tmp));
677 rec = (Record) xmalloc (sizeof(*rec));
679 memcpy(&compression_method, p->tmp_buf + sizeof(zint) + sizeof(short),
680 sizeof(compression_method));
681 in_buf = p->tmp_buf + sizeof(zint) + sizeof(short) + sizeof(char);
682 in_size = entry.size - sizeof(short) - sizeof(char);
683 switch (compression_method)
685 case REC_COMPRESS_BZIP2:
687 bz_size = entry.size * 20 + 100;
690 bz_buf = (char *) xmalloc (bz_size);
691 #ifdef BZ_CONFIG_ERROR
692 i = BZ2_bzBuffToBuffDecompress
694 i = bzBuffToBuffDecompress
696 (bz_buf, &bz_size, in_buf, in_size, 0, 0);
697 yaz_log(YLOG_LOG, "decompress %5d %5d", in_size, bz_size);
700 yaz_log(YLOG_LOG, "failed");
707 yaz_log(YLOG_FATAL, "cannot decompress record(s) in BZIP2 format");
711 case REC_COMPRESS_NONE:
714 for (i = 0; i<REC_NO_INFO; i++)
717 nptr = in_buf; /* skip ref count */
718 while (nptr < in_buf + in_size)
722 rec_decode_zint (&this_sysno, nptr, &len);
725 for (i = 0; i < REC_NO_INFO; i++)
728 rec_decode_unsigned (&this_size, nptr, &len);
733 rec->size[i] = this_size-1;
738 nptr += rec->size[i];
743 if (this_sysno == sysno)
746 for (i = 0; i<REC_NO_INFO; i++)
748 if (rec->info[i] && rec->size[i])
750 char *np = xmalloc (rec->size[i]+1);
751 memcpy(np, rec->info[i], rec->size[i]);
752 np[rec->size[i]] = '\0';
757 assert(rec->info[i] == 0);
758 assert(rec->size[i] == 0);
762 rec_cache_insert(p, rec, recordFlagNop);
766 Record rec_get(Records p, SYSNO sysno)
769 zebra_mutex_lock (&p->mutex);
771 rec = rec_get_int (p, sysno);
772 zebra_mutex_unlock (&p->mutex);
776 static Record rec_new_int(Records p)
783 rec = (Record) xmalloc (sizeof(*rec));
784 if (1 || p->head.index_free == 0)
785 sysno = (p->head.index_last)++;
788 struct record_index_entry entry;
790 read_indx (p, p->head.index_free, &entry, sizeof(entry), 0);
791 sysno = p->head.index_free;
792 p->head.index_free = entry.next;
794 (p->head.no_records)++;
796 for (i = 0; i < REC_NO_INFO; i++)
801 rec_cache_insert(p, rec, recordFlagNew);
805 Record rec_new(Records p)
808 zebra_mutex_lock (&p->mutex);
810 rec = rec_new_int (p);
811 zebra_mutex_unlock (&p->mutex);
815 void rec_del(Records p, Record *recpp)
819 zebra_mutex_lock (&p->mutex);
820 (p->head.no_records)--;
821 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagDelete)))
828 rec_cache_insert(p, *recpp, recordFlagDelete);
831 zebra_mutex_unlock (&p->mutex);
835 void rec_put(Records p, Record *recpp)
839 zebra_mutex_lock (&p->mutex);
840 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagWrite)))
847 rec_cache_insert(p, *recpp, recordFlagWrite);
850 zebra_mutex_unlock (&p->mutex);
854 void rec_rm(Record *recpp)
860 for (i = 0; i < REC_NO_INFO; i++)
861 xfree ((*recpp)->info[i]);
866 Record rec_cp(Record rec)
871 n = (Record) xmalloc (sizeof(*n));
872 n->sysno = rec->sysno;
873 for (i = 0; i < REC_NO_INFO; i++)
881 n->size[i] = rec->size[i];
882 n->info[i] = (char *) xmalloc (rec->size[i]);
883 memcpy(n->info[i], rec->info[i], rec->size[i]);
889 char *rec_strdup(const char *s, size_t *len)
899 p = (char *) xmalloc (*len);