1 /* $Id: recindex.c,v 1.54 2006-11-21 22:17:49 adam Exp $
2 Copyright (C) 1995-2006
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 this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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 zint rec_sysno_to_ext(zint sysno)
66 assert(sysno >= 0 && sysno <= USUAL_RANGE);
67 return sysno + FAKE_OFFSET;
70 zint rec_sysno_to_int(zint 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, zint sysno, void *buf, int itemsize,
108 zint pos = (sysno-1)*itemsize;
109 int off = CAST_ZINT_TO_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, zint sysno, void *buf, int itemsize)
129 zint pos = (sysno-1)*itemsize;
130 int off = CAST_ZINT_TO_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, zint 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 = CAST_ZINT_TO_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, zint *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 += CAST_ZINT_TO_INT(p->head.block_size[dst_type])
264 p->head.block_used[dst_type]++;
266 assert(block_prev != -1);
268 memcpy(cptr, &block_free, sizeof(block_free));
269 bf_write(p->data_BFile[dst_type], block_prev, 0,
270 sizeof(block_free) + (p->tmp_buf+size) - cptr, cptr);
274 Records rec_open(BFiles bfs, int rw, int compression_method)
279 ZEBRA_RES ret = ZEBRA_OK;
281 p = (Records) xmalloc(sizeof(*p));
282 p->compression_method = compression_method;
285 p->index_fname = "reci";
286 p->index_BFile = bf_open(bfs, p->index_fname, RIDX_CHUNK, rw);
287 if (p->index_BFile == NULL)
289 yaz_log(YLOG_FATAL|YLOG_ERRNO, "open %s", p->index_fname);
293 p->tmp_buf = (char *) xmalloc(p->tmp_size);
294 r = bf_read(p->index_BFile, 0, 0, 0, p->tmp_buf);
298 memcpy(p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic));
299 sprintf(p->head.version, "%3d", REC_VERSION);
300 p->head.index_free = 0;
301 p->head.index_last = 1;
302 p->head.no_records = 0;
303 p->head.total_bytes = 0;
304 for (i = 0; i<REC_BLOCK_TYPES; i++)
306 p->head.block_free[i] = 0;
307 p->head.block_last[i] = 1;
308 p->head.block_used[i] = 0;
310 p->head.block_size[0] = 128;
311 p->head.block_move[0] = 0;
312 for (i = 1; i<REC_BLOCK_TYPES; i++)
314 p->head.block_size[i] = p->head.block_size[i-1] * 4;
315 p->head.block_move[i] = p->head.block_size[i] * 24;
319 if (rec_write_head(p) != ZEBRA_OK)
324 memcpy(&p->head, p->tmp_buf, sizeof(p->head));
325 if (memcmp(p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic)))
327 yaz_log(YLOG_FATAL, "file %s has bad format", p->index_fname);
330 version = atoi(p->head.version);
331 if (version != REC_VERSION)
333 yaz_log(YLOG_FATAL, "file %s is version %d, but version"
334 " %d is required", p->index_fname, version, REC_VERSION);
339 for (i = 0; i<REC_BLOCK_TYPES; i++)
342 sprintf(str, "recd%c", i + 'A');
343 p->data_fname[i] = (char *) xmalloc(strlen(str)+1);
344 strcpy(p->data_fname[i], str);
345 p->data_BFile[i] = NULL;
347 for (i = 0; i<REC_BLOCK_TYPES; i++)
349 if (!(p->data_BFile[i] =
350 bf_open(bfs, p->data_fname[i],
351 CAST_ZINT_TO_INT(p->head.block_size[i]), rw)))
353 yaz_log(YLOG_FATAL|YLOG_ERRNO, "bf_open %s", p->data_fname[i]);
360 p->record_cache = (struct record_cache_entry *)
361 xmalloc(sizeof(*p->record_cache)*p->cache_max);
362 zebra_mutex_init(&p->mutex);
363 if (ret == ZEBRA_FAIL)
368 static void rec_encode_unsigned(unsigned n, unsigned char *buf, int *len)
373 buf[*len] = 128 + (n & 127);
381 static void rec_decode_unsigned(unsigned *np, unsigned char *buf, int *len)
387 while (buf[*len] > 127)
389 n += w*(buf[*len] & 127);
398 static void rec_encode_zint(zint n, unsigned char *buf, int *len)
403 buf[*len] = (unsigned) (128 + (n & 127));
407 buf[*len] = (unsigned) n;
411 static void rec_decode_zint(zint *np, unsigned char *buf, int *len)
417 while (buf[*len] > 127)
419 n += w*(buf[*len] & 127);
428 static void rec_cache_flush_block1(Records p, Record rec, Record last_rec,
429 char **out_buf, int *out_size,
435 for (i = 0; i<REC_NO_INFO; i++)
437 if (*out_offset + CAST_ZINT_TO_INT(rec->size[i]) + 20 > *out_size)
439 int new_size = *out_offset + rec->size[i] + 65536;
440 char *np = (char *) xmalloc(new_size);
442 memcpy(np, *out_buf, *out_offset);
444 *out_size = new_size;
449 rec_encode_zint(rec_sysno_to_int(rec->sysno),
450 (unsigned char *) *out_buf + *out_offset, &len);
451 (*out_offset) += len;
453 if (rec->size[i] == 0)
455 rec_encode_unsigned(1, (unsigned char *) *out_buf + *out_offset,
457 (*out_offset) += len;
459 else if (last_rec && rec->size[i] == last_rec->size[i] &&
460 !memcmp(rec->info[i], last_rec->info[i], rec->size[i]))
462 rec_encode_unsigned(0, (unsigned char *) *out_buf + *out_offset,
464 (*out_offset) += len;
468 rec_encode_unsigned(rec->size[i]+1,
469 (unsigned char *) *out_buf + *out_offset,
471 (*out_offset) += len;
472 memcpy(*out_buf + *out_offset, rec->info[i], rec->size[i]);
473 (*out_offset) += rec->size[i];
478 static ZEBRA_RES rec_write_multiple(Records p, int saveCount)
482 char compression_method;
486 char *out_buf = (char *) xmalloc(out_size);
487 zint *sysnos = (zint *) xmalloc(sizeof(*sysnos) * (p->cache_cur + 1));
488 zint *sysnop = sysnos;
489 ZEBRA_RES ret = ZEBRA_OK;
491 for (i = 0; i<p->cache_cur - saveCount; i++)
493 struct record_cache_entry *e = p->record_cache + i;
497 rec_cache_flush_block1(p, e->rec, last_rec, &out_buf,
498 &out_size, &out_offset);
499 *sysnop++ = rec_sysno_to_int(e->rec->sysno);
501 e->flag = recordFlagNop;
504 case recordFlagWrite:
505 if (rec_release_blocks(p, rec_sysno_to_int(e->rec->sysno))
509 rec_cache_flush_block1(p, e->rec, last_rec, &out_buf,
510 &out_size, &out_offset);
511 *sysnop++ = rec_sysno_to_int(e->rec->sysno);
513 e->flag = recordFlagNop;
516 case recordFlagDelete:
517 if (rec_delete_single(p, e->rec) != ZEBRA_OK)
520 e->flag = recordFlagNop;
530 unsigned int csize = 0; /* indicate compression "not performed yet" */
531 compression_method = p->compression_method;
532 switch (compression_method)
534 case REC_COMPRESS_BZIP2:
536 csize = out_offset + (out_offset >> 6) + 620;
537 rec_tmp_expand(p, csize);
538 #ifdef BZ_CONFIG_ERROR
539 i = BZ2_bzBuffToBuffCompress
541 i = bzBuffToBuffCompress
543 (p->tmp_buf+sizeof(zint)+sizeof(short)+
545 &csize, out_buf, out_offset, 1, 0, 30);
548 yaz_log(YLOG_WARN, "bzBuffToBuffCompress error code=%d", i);
551 yaz_log(YLOG_LOG, "compress %4d %5d %5d", ref_count, out_offset,
555 case REC_COMPRESS_NONE:
560 /* either no compression or compression not supported ... */
562 rec_tmp_expand(p, csize);
563 memcpy(p->tmp_buf + sizeof(zint) + sizeof(short) + sizeof(char),
564 out_buf, out_offset);
566 compression_method = REC_COMPRESS_NONE;
568 memcpy(p->tmp_buf + sizeof(zint), &ref_count, sizeof(ref_count));
569 memcpy(p->tmp_buf + sizeof(zint)+sizeof(short),
570 &compression_method, sizeof(compression_method));
572 /* -------- compression */
573 if (rec_write_tmp_buf(p, csize + sizeof(short) + sizeof(char), sysnos)
582 static ZEBRA_RES rec_cache_flush(Records p, int saveCount)
587 if (saveCount >= p->cache_cur)
590 ret = rec_write_multiple(p, saveCount);
592 for (i = 0; i<p->cache_cur - saveCount; i++)
594 struct record_cache_entry *e = p->record_cache + i;
597 /* i still being used ... */
598 for (j = 0; j<saveCount; j++, i++)
599 memcpy(p->record_cache+j, p->record_cache+i,
600 sizeof(*p->record_cache));
601 p->cache_cur = saveCount;
605 static Record *rec_cache_lookup(Records p, zint sysno,
606 enum recordCacheFlag flag)
609 for (i = 0; i<p->cache_cur; i++)
611 struct record_cache_entry *e = p->record_cache + i;
612 if (e->rec->sysno == sysno)
614 if (flag != recordFlagNop && e->flag == recordFlagNop)
622 static ZEBRA_RES rec_cache_insert(Records p, Record rec, enum recordCacheFlag flag)
624 struct record_cache_entry *e;
625 ZEBRA_RES ret = ZEBRA_OK;
627 if (p->cache_cur == p->cache_max)
628 ret = rec_cache_flush(p, 1);
629 else if (p->cache_cur > 0)
633 for (i = 0; i<p->cache_cur; i++)
635 Record r = (p->record_cache + i)->rec;
636 for (j = 0; j<REC_NO_INFO; j++)
640 ret = rec_cache_flush(p, 1);
642 assert(p->cache_cur < p->cache_max);
644 e = p->record_cache + (p->cache_cur)++;
646 e->rec = rec_cp(rec);
650 ZEBRA_RES rec_close(Records *pp)
654 ZEBRA_RES ret = ZEBRA_OK;
659 zebra_mutex_destroy(&p->mutex);
660 if (rec_cache_flush(p, 0) != ZEBRA_OK)
663 xfree(p->record_cache);
667 if (rec_write_head(p) != ZEBRA_OK)
672 bf_close(p->index_BFile);
674 for (i = 0; i<REC_BLOCK_TYPES; i++)
676 if (p->data_BFile[i])
677 bf_close(p->data_BFile[i]);
678 xfree(p->data_fname[i]);
686 static Record rec_get_int(Records p, zint sysno)
690 struct record_index_entry entry;
697 unsigned int bz_size;
699 char compression_method;
704 if ((recp = rec_cache_lookup(p, sysno, recordFlagNop)))
705 return rec_cp(*recp);
707 if (read_indx(p, rec_sysno_to_int(sysno), &entry, sizeof(entry), 1) < 1)
708 return NULL; /* record is not there! */
711 return NULL; /* record is deleted */
713 dst_type = (int) (entry.next & 7);
714 assert(dst_type < REC_BLOCK_TYPES);
715 freeblock = entry.next / 8;
717 assert(freeblock > 0);
719 rec_tmp_expand(p, entry.size);
722 r = bf_read(p->data_BFile[dst_type], freeblock, 0, 0, cptr);
725 memcpy(&freeblock, cptr, sizeof(freeblock));
731 cptr += p->head.block_size[dst_type] - sizeof(freeblock);
733 memcpy(&tmp, cptr, sizeof(tmp));
734 r = bf_read(p->data_BFile[dst_type], freeblock, 0, 0, cptr);
737 memcpy(&freeblock, cptr, sizeof(freeblock));
738 memcpy(cptr, &tmp, sizeof(tmp));
741 rec = (Record) xmalloc(sizeof(*rec));
743 memcpy(&compression_method, p->tmp_buf + sizeof(zint) + sizeof(short),
744 sizeof(compression_method));
745 in_buf = p->tmp_buf + sizeof(zint) + sizeof(short) + sizeof(char);
746 in_size = entry.size - sizeof(short) - sizeof(char);
747 switch (compression_method)
749 case REC_COMPRESS_BZIP2:
751 bz_size = entry.size * 20 + 100;
754 bz_buf = (char *) xmalloc(bz_size);
755 #ifdef BZ_CONFIG_ERROR
756 i = BZ2_bzBuffToBuffDecompress
758 i = bzBuffToBuffDecompress
760 (bz_buf, &bz_size, in_buf, in_size, 0, 0);
761 yaz_log(YLOG_LOG, "decompress %5d %5d", in_size, bz_size);
764 yaz_log(YLOG_LOG, "failed");
771 yaz_log(YLOG_FATAL, "cannot decompress record(s) in BZIP2 format");
775 case REC_COMPRESS_NONE:
778 for (i = 0; i<REC_NO_INFO; i++)
781 nptr = in_buf; /* skip ref count */
782 while (nptr < in_buf + in_size)
786 rec_decode_zint(&this_sysno, (unsigned char *) nptr, &len);
789 for (i = 0; i < REC_NO_INFO; i++)
791 unsigned int this_size;
792 rec_decode_unsigned(&this_size, (unsigned char *) nptr, &len);
797 rec->size[i] = this_size-1;
802 nptr += rec->size[i];
807 if (this_sysno == rec_sysno_to_int(sysno))
810 for (i = 0; i<REC_NO_INFO; i++)
812 if (rec->info[i] && rec->size[i])
814 char *np = xmalloc(rec->size[i]+1);
815 memcpy(np, rec->info[i], rec->size[i]);
816 np[rec->size[i]] = '\0';
821 assert(rec->info[i] == 0);
822 assert(rec->size[i] == 0);
826 if (rec_cache_insert(p, rec, recordFlagNop) != ZEBRA_OK)
831 Record rec_get(Records p, zint sysno)
834 zebra_mutex_lock(&p->mutex);
836 rec = rec_get_int(p, sysno);
837 zebra_mutex_unlock(&p->mutex);
841 Record rec_get_root(Records p)
843 return rec_get(p, rec_sysno_to_ext(1));
846 static Record rec_new_int(Records p)
853 rec = (Record) xmalloc(sizeof(*rec));
854 if (1 || p->head.index_free == 0)
855 sysno = (p->head.index_last)++;
858 struct record_index_entry entry;
860 if (read_indx(p, p->head.index_free, &entry, sizeof(entry), 0) < 1)
865 sysno = p->head.index_free;
866 p->head.index_free = entry.next;
868 (p->head.no_records)++;
869 rec->sysno = rec_sysno_to_ext(sysno);
870 for (i = 0; i < REC_NO_INFO; i++)
875 rec_cache_insert(p, rec, recordFlagNew);
879 Record rec_new(Records p)
882 zebra_mutex_lock(&p->mutex);
884 rec = rec_new_int(p);
885 zebra_mutex_unlock(&p->mutex);
889 ZEBRA_RES rec_del(Records p, Record *recpp)
892 ZEBRA_RES ret = ZEBRA_OK;
894 zebra_mutex_lock(&p->mutex);
895 (p->head.no_records)--;
896 if ((recp = rec_cache_lookup(p, (*recpp)->sysno, recordFlagDelete)))
903 ret = rec_cache_insert(p, *recpp, recordFlagDelete);
906 zebra_mutex_unlock(&p->mutex);
911 ZEBRA_RES rec_put(Records p, Record *recpp)
914 ZEBRA_RES ret = ZEBRA_OK;
916 zebra_mutex_lock(&p->mutex);
917 if ((recp = rec_cache_lookup(p, (*recpp)->sysno, recordFlagWrite)))
924 ret = rec_cache_insert(p, *recpp, recordFlagWrite);
927 zebra_mutex_unlock(&p->mutex);
932 void rec_free(Record *recpp)
938 for (i = 0; i < REC_NO_INFO; i++)
939 xfree((*recpp)->info[i]);
944 Record rec_cp(Record rec)
949 n = (Record) xmalloc(sizeof(*n));
950 n->sysno = rec->sysno;
951 for (i = 0; i < REC_NO_INFO; i++)
959 n->size[i] = rec->size[i];
960 n->info[i] = (char *) xmalloc(rec->size[i]+1);
961 memcpy(n->info[i], rec->info[i], rec->size[i]);
962 n->info[i][rec->size[i]] = '\0';
968 char *rec_strdup(const char *s, size_t *len)
978 p = (char *) xmalloc(*len);
986 * indent-tabs-mode: nil
988 * vim: shiftwidth=4 tabstop=8 expandtab