1 /* $Id: recindex.c,v 1.48 2006-05-10 08:13:22 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>
52 /* Modify argument to if below: 1=normal, 0=sysno testing */
54 /* If this is used sysno are not converted (no testing) */
56 #define USUAL_RANGE 6000000000LL
59 /* Use a fake > 2^32 offset so we can test for proper 64-bit handling */
60 #define FAKE_OFFSET 6000000000LL
61 #define USUAL_RANGE 2000000000LL
64 static SYSNO rec_sysno_to_ext(SYSNO sysno)
66 assert(sysno >= 0 && sysno <= USUAL_RANGE);
67 return sysno + FAKE_OFFSET;
70 SYSNO rec_sysno_to_int(SYSNO sysno)
72 assert(sysno >= FAKE_OFFSET && sysno <= FAKE_OFFSET + USUAL_RANGE);
73 return sysno - FAKE_OFFSET;
76 static ZEBRA_RES rec_write_head(Records p)
81 assert(p->index_BFile);
83 r = bf_write(p->index_BFile, 0, 0, sizeof(p->head), &p->head);
86 yaz_log(YLOG_FATAL|YLOG_ERRNO, "write head of %s", p->index_fname);
92 static void rec_tmp_expand(Records p, int size)
94 if (p->tmp_size < size + 2048 ||
95 p->tmp_size < p->head.block_size[REC_BLOCK_TYPES-1]*2)
98 p->tmp_size = size + (int)
99 (p->head.block_size[REC_BLOCK_TYPES-1])*2 + 2048;
100 p->tmp_buf = (char *) xmalloc(p->tmp_size);
104 static int read_indx(Records p, SYSNO sysno, void *buf, int itemsize,
108 zint pos = (sysno-1)*itemsize;
109 int off = (int) (pos%RIDX_CHUNK);
110 int sz1 = RIDX_CHUNK - off; /* sz1 is size of buffer to read.. */
113 sz1 = itemsize; /* no more than itemsize bytes */
115 r = bf_read(p->index_BFile, 1+pos/RIDX_CHUNK, off, sz1, buf);
116 if (r == 1 && sz1 < itemsize) /* boundary? - must read second part */
117 r = bf_read(p->index_BFile, 2+pos/RIDX_CHUNK, 0, itemsize - sz1,
119 if (r != 1 && !ignoreError)
121 yaz_log(YLOG_FATAL|YLOG_ERRNO, "read in %s at pos %ld",
122 p->index_fname, (long) pos);
127 static void write_indx(Records p, SYSNO sysno, void *buf, int itemsize)
129 zint pos = (sysno-1)*itemsize;
130 int off = (int) (pos%RIDX_CHUNK);
131 int sz1 = RIDX_CHUNK - off; /* sz1 is size of buffer to read.. */
134 sz1 = itemsize; /* no more than itemsize bytes */
136 bf_write(p->index_BFile, 1+pos/RIDX_CHUNK, off, sz1, buf);
137 if (sz1 < itemsize) /* boundary? must write second part */
138 bf_write(p->index_BFile, 2+pos/RIDX_CHUNK, 0, itemsize - sz1,
142 static ZEBRA_RES rec_release_blocks(Records p, SYSNO sysno)
144 struct record_index_entry entry;
146 char block_and_ref[sizeof(zint) + sizeof(short)];
150 if (read_indx(p, sysno, &entry, sizeof(entry), 1) != 1)
153 freeblock = entry.next;
154 assert(freeblock > 0);
155 dst_type = (int) (freeblock & 7);
156 assert(dst_type < REC_BLOCK_TYPES);
157 freeblock = freeblock / 8;
160 if (bf_read(p->data_BFile[dst_type], freeblock, 0,
161 first ? sizeof(block_and_ref) : sizeof(zint),
164 yaz_log(YLOG_FATAL|YLOG_ERRNO, "read in rec_del_single");
170 memcpy(&ref, block_and_ref + sizeof(freeblock), sizeof(ref));
172 memcpy(block_and_ref + sizeof(freeblock), &ref, sizeof(ref));
175 if (bf_write(p->data_BFile[dst_type], freeblock, 0,
176 sizeof(block_and_ref), block_and_ref))
178 yaz_log(YLOG_FATAL|YLOG_ERRNO, "write in rec_del_single");
186 if (bf_write(p->data_BFile[dst_type], freeblock, 0, sizeof(freeblock),
187 &p->head.block_free[dst_type]))
189 yaz_log(YLOG_FATAL|YLOG_ERRNO, "write in rec_del_single");
192 p->head.block_free[dst_type] = freeblock;
193 memcpy(&freeblock, block_and_ref, sizeof(freeblock));
195 p->head.block_used[dst_type]--;
197 p->head.total_bytes -= entry.size;
201 static ZEBRA_RES rec_delete_single(Records p, Record rec)
203 struct record_index_entry entry;
205 if (rec_release_blocks(p, rec_sysno_to_int(rec->sysno)) != ZEBRA_OK)
208 entry.next = p->head.index_free;
210 p->head.index_free = rec_sysno_to_int(rec->sysno);
211 write_indx(p, rec_sysno_to_int(rec->sysno), &entry, sizeof(entry));
215 static ZEBRA_RES rec_write_tmp_buf(Records p, int size, SYSNO *sysnos)
217 struct record_index_entry entry;
219 char *cptr = p->tmp_buf;
220 zint block_prev = -1, block_free;
224 for (i = 1; i<REC_BLOCK_TYPES; i++)
225 if (size >= p->head.block_move[i])
227 while (no_written < size)
229 block_free = p->head.block_free[dst_type];
232 if (bf_read(p->data_BFile[dst_type],
233 block_free, 0, sizeof(*p->head.block_free),
234 &p->head.block_free[dst_type]) != 1)
236 yaz_log(YLOG_FATAL|YLOG_ERRNO, "read in %s at free block "
238 p->data_fname[dst_type], block_free);
243 block_free = p->head.block_last[dst_type]++;
244 if (block_prev == -1)
246 entry.next = block_free*8 + dst_type;
248 p->head.total_bytes += size;
251 write_indx(p, *sysnos, &entry, sizeof(entry));
257 memcpy(cptr, &block_free, sizeof(block_free));
258 bf_write(p->data_BFile[dst_type], block_prev, 0, 0, cptr);
259 cptr = p->tmp_buf + no_written;
261 block_prev = block_free;
262 no_written += (int)(p->head.block_size[dst_type]) - sizeof(zint);
263 p->head.block_used[dst_type]++;
265 assert(block_prev != -1);
267 memcpy(cptr, &block_free, sizeof(block_free));
268 bf_write(p->data_BFile[dst_type], block_prev, 0,
269 sizeof(block_free) + (p->tmp_buf+size) - cptr, cptr);
273 Records rec_open(BFiles bfs, int rw, int compression_method)
278 ZEBRA_RES ret = ZEBRA_OK;
280 p = (Records) xmalloc(sizeof(*p));
281 p->compression_method = compression_method;
284 p->index_fname = "reci";
285 p->index_BFile = bf_open(bfs, p->index_fname, RIDX_CHUNK, rw);
286 if (p->index_BFile == NULL)
288 yaz_log(YLOG_FATAL|YLOG_ERRNO, "open %s", p->index_fname);
292 p->tmp_buf = (char *) xmalloc(p->tmp_size);
293 r = bf_read(p->index_BFile, 0, 0, 0, p->tmp_buf);
297 memcpy(p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic));
298 sprintf(p->head.version, "%3d", REC_VERSION);
299 p->head.index_free = 0;
300 p->head.index_last = 1;
301 p->head.no_records = 0;
302 p->head.total_bytes = 0;
303 for (i = 0; i<REC_BLOCK_TYPES; i++)
305 p->head.block_free[i] = 0;
306 p->head.block_last[i] = 1;
307 p->head.block_used[i] = 0;
309 p->head.block_size[0] = 128;
310 p->head.block_move[0] = 0;
311 for (i = 1; i<REC_BLOCK_TYPES; i++)
313 p->head.block_size[i] = p->head.block_size[i-1] * 4;
314 p->head.block_move[i] = p->head.block_size[i] * 24;
318 if (rec_write_head(p) != ZEBRA_OK)
323 memcpy(&p->head, p->tmp_buf, sizeof(p->head));
324 if (memcmp(p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic)))
326 yaz_log(YLOG_FATAL, "file %s has bad format", p->index_fname);
329 version = atoi(p->head.version);
330 if (version != REC_VERSION)
332 yaz_log(YLOG_FATAL, "file %s is version %d, but version"
333 " %d is required", p->index_fname, version, REC_VERSION);
338 for (i = 0; i<REC_BLOCK_TYPES; i++)
341 sprintf(str, "recd%c", i + 'A');
342 p->data_fname[i] = (char *) xmalloc(strlen(str)+1);
343 strcpy(p->data_fname[i], str);
344 p->data_BFile[i] = NULL;
346 for (i = 0; i<REC_BLOCK_TYPES; i++)
348 if (!(p->data_BFile[i] = bf_open(bfs, p->data_fname[i],
349 (int) (p->head.block_size[i]),
352 yaz_log(YLOG_FATAL|YLOG_ERRNO, "bf_open %s", p->data_fname[i]);
358 p->record_cache = (struct record_cache_entry *)
359 xmalloc(sizeof(*p->record_cache)*p->cache_max);
360 zebra_mutex_init(&p->mutex);
361 if (ret == ZEBRA_FAIL)
366 static void rec_encode_unsigned(unsigned n, unsigned char *buf, int *len)
371 buf[*len] = 128 + (n & 127);
379 static void rec_decode_unsigned(unsigned *np, unsigned char *buf, int *len)
385 while (buf[*len] > 127)
387 n += w*(buf[*len] & 127);
396 static void rec_encode_zint(zint n, unsigned char *buf, int *len)
401 buf[*len] = (unsigned) (128 + (n & 127));
405 buf[*len] = (unsigned) n;
409 static void rec_decode_zint(zint *np, unsigned char *buf, int *len)
415 while (buf[*len] > 127)
417 n += w*(buf[*len] & 127);
426 static void rec_cache_flush_block1(Records p, Record rec, Record last_rec,
427 char **out_buf, int *out_size,
433 for (i = 0; i<REC_NO_INFO; i++)
435 if (*out_offset + (int) rec->size[i] + 20 > *out_size)
437 int new_size = *out_offset + rec->size[i] + 65536;
438 char *np = (char *) xmalloc(new_size);
440 memcpy(np, *out_buf, *out_offset);
442 *out_size = new_size;
447 rec_encode_zint(rec_sysno_to_int(rec->sysno),
448 (unsigned char *) *out_buf + *out_offset, &len);
449 (*out_offset) += len;
451 if (rec->size[i] == 0)
453 rec_encode_unsigned(1, (unsigned char *) *out_buf + *out_offset,
455 (*out_offset) += len;
457 else if (last_rec && rec->size[i] == last_rec->size[i] &&
458 !memcmp(rec->info[i], last_rec->info[i], rec->size[i]))
460 rec_encode_unsigned(0, (unsigned char *) *out_buf + *out_offset,
462 (*out_offset) += len;
466 rec_encode_unsigned(rec->size[i]+1,
467 (unsigned char *) *out_buf + *out_offset,
469 (*out_offset) += len;
470 memcpy(*out_buf + *out_offset, rec->info[i], rec->size[i]);
471 (*out_offset) += rec->size[i];
476 static ZEBRA_RES rec_write_multiple(Records p, int saveCount)
480 char compression_method;
484 char *out_buf = (char *) xmalloc(out_size);
485 SYSNO *sysnos = (SYSNO *) xmalloc(sizeof(*sysnos) * (p->cache_cur + 1));
486 SYSNO *sysnop = sysnos;
487 ZEBRA_RES ret = ZEBRA_OK;
489 for (i = 0; i<p->cache_cur - saveCount; i++)
491 struct record_cache_entry *e = p->record_cache + i;
495 rec_cache_flush_block1(p, e->rec, last_rec, &out_buf,
496 &out_size, &out_offset);
497 *sysnop++ = rec_sysno_to_int(e->rec->sysno);
499 e->flag = recordFlagNop;
502 case recordFlagWrite:
503 if (rec_release_blocks(p, rec_sysno_to_int(e->rec->sysno))
507 rec_cache_flush_block1(p, e->rec, last_rec, &out_buf,
508 &out_size, &out_offset);
509 *sysnop++ = rec_sysno_to_int(e->rec->sysno);
511 e->flag = recordFlagNop;
514 case recordFlagDelete:
515 if (rec_delete_single(p, e->rec) != ZEBRA_OK)
518 e->flag = recordFlagNop;
528 unsigned int csize = 0; /* indicate compression "not performed yet" */
529 compression_method = p->compression_method;
530 switch (compression_method)
532 case REC_COMPRESS_BZIP2:
534 csize = out_offset + (out_offset >> 6) + 620;
535 rec_tmp_expand(p, csize);
536 #ifdef BZ_CONFIG_ERROR
537 i = BZ2_bzBuffToBuffCompress
539 i = bzBuffToBuffCompress
541 (p->tmp_buf+sizeof(zint)+sizeof(short)+
543 &csize, out_buf, out_offset, 1, 0, 30);
546 yaz_log(YLOG_WARN, "bzBuffToBuffCompress error code=%d", i);
549 yaz_log(YLOG_LOG, "compress %4d %5d %5d", ref_count, out_offset,
553 case REC_COMPRESS_NONE:
558 /* either no compression or compression not supported ... */
560 rec_tmp_expand(p, csize);
561 memcpy(p->tmp_buf + sizeof(zint) + sizeof(short) + sizeof(char),
562 out_buf, out_offset);
564 compression_method = REC_COMPRESS_NONE;
566 memcpy(p->tmp_buf + sizeof(zint), &ref_count, sizeof(ref_count));
567 memcpy(p->tmp_buf + sizeof(zint)+sizeof(short),
568 &compression_method, sizeof(compression_method));
570 /* -------- compression */
571 if (rec_write_tmp_buf(p, csize + sizeof(short) + sizeof(char), sysnos)
580 static ZEBRA_RES rec_cache_flush(Records p, int saveCount)
585 if (saveCount >= p->cache_cur)
588 ret = rec_write_multiple(p, saveCount);
590 for (i = 0; i<p->cache_cur - saveCount; i++)
592 struct record_cache_entry *e = p->record_cache + i;
595 /* i still being used ... */
596 for (j = 0; j<saveCount; j++, i++)
597 memcpy(p->record_cache+j, p->record_cache+i,
598 sizeof(*p->record_cache));
599 p->cache_cur = saveCount;
603 static Record *rec_cache_lookup(Records p, SYSNO sysno,
604 enum recordCacheFlag flag)
607 for (i = 0; i<p->cache_cur; i++)
609 struct record_cache_entry *e = p->record_cache + i;
610 if (e->rec->sysno == sysno)
612 if (flag != recordFlagNop && e->flag == recordFlagNop)
620 static ZEBRA_RES rec_cache_insert(Records p, Record rec, enum recordCacheFlag flag)
622 struct record_cache_entry *e;
623 ZEBRA_RES ret = ZEBRA_OK;
625 if (p->cache_cur == p->cache_max)
626 ret = rec_cache_flush(p, 1);
627 else if (p->cache_cur > 0)
631 for (i = 0; i<p->cache_cur; i++)
633 Record r = (p->record_cache + i)->rec;
634 for (j = 0; j<REC_NO_INFO; j++)
638 ret = rec_cache_flush(p, 1);
640 assert(p->cache_cur < p->cache_max);
642 e = p->record_cache + (p->cache_cur)++;
644 e->rec = rec_cp(rec);
648 ZEBRA_RES rec_close(Records *pp)
652 ZEBRA_RES ret = ZEBRA_OK;
657 zebra_mutex_destroy(&p->mutex);
658 if (rec_cache_flush(p, 0) != ZEBRA_OK)
661 xfree(p->record_cache);
665 if (rec_write_head(p) != ZEBRA_OK)
670 bf_close(p->index_BFile);
672 for (i = 0; i<REC_BLOCK_TYPES; i++)
674 if (p->data_BFile[i])
675 bf_close(p->data_BFile[i]);
676 xfree(p->data_fname[i]);
684 static Record rec_get_int(Records p, SYSNO sysno)
688 struct record_index_entry entry;
695 unsigned int bz_size;
697 char compression_method;
702 if ((recp = rec_cache_lookup(p, sysno, recordFlagNop)))
703 return rec_cp(*recp);
705 if (read_indx(p, rec_sysno_to_int(sysno), &entry, sizeof(entry), 1) < 1)
706 return NULL; /* record is not there! */
709 return NULL; /* record is deleted */
711 dst_type = (int) (entry.next & 7);
712 assert(dst_type < REC_BLOCK_TYPES);
713 freeblock = entry.next / 8;
715 assert(freeblock > 0);
717 rec_tmp_expand(p, entry.size);
720 r = bf_read(p->data_BFile[dst_type], freeblock, 0, 0, cptr);
723 memcpy(&freeblock, cptr, sizeof(freeblock));
729 cptr += p->head.block_size[dst_type] - sizeof(freeblock);
731 memcpy(&tmp, cptr, sizeof(tmp));
732 r = bf_read(p->data_BFile[dst_type], freeblock, 0, 0, cptr);
735 memcpy(&freeblock, cptr, sizeof(freeblock));
736 memcpy(cptr, &tmp, sizeof(tmp));
739 rec = (Record) xmalloc(sizeof(*rec));
741 memcpy(&compression_method, p->tmp_buf + sizeof(zint) + sizeof(short),
742 sizeof(compression_method));
743 in_buf = p->tmp_buf + sizeof(zint) + sizeof(short) + sizeof(char);
744 in_size = entry.size - sizeof(short) - sizeof(char);
745 switch (compression_method)
747 case REC_COMPRESS_BZIP2:
749 bz_size = entry.size * 20 + 100;
752 bz_buf = (char *) xmalloc(bz_size);
753 #ifdef BZ_CONFIG_ERROR
754 i = BZ2_bzBuffToBuffDecompress
756 i = bzBuffToBuffDecompress
758 (bz_buf, &bz_size, in_buf, in_size, 0, 0);
759 yaz_log(YLOG_LOG, "decompress %5d %5d", in_size, bz_size);
762 yaz_log(YLOG_LOG, "failed");
769 yaz_log(YLOG_FATAL, "cannot decompress record(s) in BZIP2 format");
773 case REC_COMPRESS_NONE:
776 for (i = 0; i<REC_NO_INFO; i++)
779 nptr = in_buf; /* skip ref count */
780 while (nptr < in_buf + in_size)
784 rec_decode_zint(&this_sysno, (unsigned char *) nptr, &len);
787 for (i = 0; i < REC_NO_INFO; i++)
789 unsigned int this_size;
790 rec_decode_unsigned(&this_size, (unsigned char *) nptr, &len);
795 rec->size[i] = this_size-1;
800 nptr += rec->size[i];
805 if (this_sysno == rec_sysno_to_int(sysno))
808 for (i = 0; i<REC_NO_INFO; i++)
810 if (rec->info[i] && rec->size[i])
812 char *np = xmalloc(rec->size[i]+1);
813 memcpy(np, rec->info[i], rec->size[i]);
814 np[rec->size[i]] = '\0';
819 assert(rec->info[i] == 0);
820 assert(rec->size[i] == 0);
824 if (rec_cache_insert(p, rec, recordFlagNop) != ZEBRA_OK)
829 Record rec_get(Records p, SYSNO sysno)
832 zebra_mutex_lock(&p->mutex);
834 rec = rec_get_int(p, sysno);
835 zebra_mutex_unlock(&p->mutex);
839 Record rec_get_root(Records p)
841 return rec_get(p, rec_sysno_to_ext(1));
844 static Record rec_new_int(Records p)
851 rec = (Record) xmalloc(sizeof(*rec));
852 if (1 || p->head.index_free == 0)
853 sysno = (p->head.index_last)++;
856 struct record_index_entry entry;
858 if (read_indx(p, p->head.index_free, &entry, sizeof(entry), 0) < 1)
863 sysno = p->head.index_free;
864 p->head.index_free = entry.next;
866 (p->head.no_records)++;
867 rec->sysno = rec_sysno_to_ext(sysno);
868 for (i = 0; i < REC_NO_INFO; i++)
873 rec_cache_insert(p, rec, recordFlagNew);
877 Record rec_new(Records p)
880 zebra_mutex_lock(&p->mutex);
882 rec = rec_new_int(p);
883 zebra_mutex_unlock(&p->mutex);
887 ZEBRA_RES rec_del(Records p, Record *recpp)
890 ZEBRA_RES ret = ZEBRA_OK;
892 zebra_mutex_lock(&p->mutex);
893 (p->head.no_records)--;
894 if ((recp = rec_cache_lookup(p, (*recpp)->sysno, recordFlagDelete)))
901 ret = rec_cache_insert(p, *recpp, recordFlagDelete);
904 zebra_mutex_unlock(&p->mutex);
909 ZEBRA_RES rec_put(Records p, Record *recpp)
912 ZEBRA_RES ret = ZEBRA_OK;
914 zebra_mutex_lock(&p->mutex);
915 if ((recp = rec_cache_lookup(p, (*recpp)->sysno, recordFlagWrite)))
922 ret = rec_cache_insert(p, *recpp, recordFlagWrite);
925 zebra_mutex_unlock(&p->mutex);
930 void rec_rm(Record *recpp)
936 for (i = 0; i < REC_NO_INFO; i++)
937 xfree((*recpp)->info[i]);
942 Record rec_cp(Record rec)
947 n = (Record) xmalloc(sizeof(*n));
948 n->sysno = rec->sysno;
949 for (i = 0; i < REC_NO_INFO; i++)
957 n->size[i] = rec->size[i];
958 n->info[i] = (char *) xmalloc(rec->size[i]);
959 memcpy(n->info[i], rec->info[i], rec->size[i]);
965 char *rec_strdup(const char *s, size_t *len)
975 p = (char *) xmalloc(*len);
983 * indent-tabs-mode: nil
985 * vim: shiftwidth=4 tabstop=8 expandtab