1 /* $Id: recindex.c,v 1.56 2007-01-15 15:10:17 adam Exp $
2 Copyright (C) 1995-2007
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 /* all data in entry must be reset, since it's written verbatim */
206 memset(&entry, '\0', sizeof(entry));
207 if (rec_release_blocks(p, rec_sysno_to_int(rec->sysno)) != ZEBRA_OK)
210 entry.next = p->head.index_free;
212 p->head.index_free = rec_sysno_to_int(rec->sysno);
213 write_indx(p, rec_sysno_to_int(rec->sysno), &entry, sizeof(entry));
217 static ZEBRA_RES rec_write_tmp_buf(Records p, int size, zint *sysnos)
219 struct record_index_entry entry;
221 char *cptr = p->tmp_buf;
222 zint block_prev = -1, block_free;
226 /* all data in entry must be reset, since it's written verbatim */
227 memset(&entry, '\0', sizeof(entry));
229 for (i = 1; i<REC_BLOCK_TYPES; i++)
230 if (size >= p->head.block_move[i])
232 while (no_written < size)
234 block_free = p->head.block_free[dst_type];
237 if (bf_read(p->data_BFile[dst_type],
238 block_free, 0, sizeof(*p->head.block_free),
239 &p->head.block_free[dst_type]) != 1)
241 yaz_log(YLOG_FATAL|YLOG_ERRNO, "read in %s at free block "
243 p->data_fname[dst_type], block_free);
248 block_free = p->head.block_last[dst_type]++;
249 if (block_prev == -1)
251 entry.next = block_free*8 + dst_type;
253 p->head.total_bytes += size;
256 write_indx(p, *sysnos, &entry, sizeof(entry));
262 memcpy(cptr, &block_free, sizeof(block_free));
263 bf_write(p->data_BFile[dst_type], block_prev, 0, 0, cptr);
264 cptr = p->tmp_buf + no_written;
266 block_prev = block_free;
267 no_written += CAST_ZINT_TO_INT(p->head.block_size[dst_type])
269 p->head.block_used[dst_type]++;
271 assert(block_prev != -1);
273 memcpy(cptr, &block_free, sizeof(block_free));
274 bf_write(p->data_BFile[dst_type], block_prev, 0,
275 sizeof(block_free) + (p->tmp_buf+size) - cptr, cptr);
279 Records rec_open(BFiles bfs, int rw, int compression_method)
284 ZEBRA_RES ret = ZEBRA_OK;
286 p = (Records) xmalloc(sizeof(*p));
287 memset(&p->head, '\0', sizeof(p->head));
288 p->compression_method = compression_method;
291 p->index_fname = "reci";
292 p->index_BFile = bf_open(bfs, p->index_fname, RIDX_CHUNK, rw);
293 if (p->index_BFile == NULL)
295 yaz_log(YLOG_FATAL|YLOG_ERRNO, "open %s", p->index_fname);
299 p->tmp_buf = (char *) xmalloc(p->tmp_size);
300 r = bf_read(p->index_BFile, 0, 0, 0, p->tmp_buf);
304 memcpy(p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic));
305 sprintf(p->head.version, "%3d", REC_VERSION);
306 p->head.index_free = 0;
307 p->head.index_last = 1;
308 p->head.no_records = 0;
309 p->head.total_bytes = 0;
310 for (i = 0; i<REC_BLOCK_TYPES; i++)
312 p->head.block_free[i] = 0;
313 p->head.block_last[i] = 1;
314 p->head.block_used[i] = 0;
316 p->head.block_size[0] = 128;
317 p->head.block_move[0] = 0;
318 for (i = 1; i<REC_BLOCK_TYPES; i++)
320 p->head.block_size[i] = p->head.block_size[i-1] * 4;
321 p->head.block_move[i] = p->head.block_size[i] * 24;
325 if (rec_write_head(p) != ZEBRA_OK)
330 memcpy(&p->head, p->tmp_buf, sizeof(p->head));
331 if (memcmp(p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic)))
333 yaz_log(YLOG_FATAL, "file %s has bad format", p->index_fname);
336 version = atoi(p->head.version);
337 if (version != REC_VERSION)
339 yaz_log(YLOG_FATAL, "file %s is version %d, but version"
340 " %d is required", p->index_fname, version, REC_VERSION);
345 for (i = 0; i<REC_BLOCK_TYPES; i++)
348 sprintf(str, "recd%c", i + 'A');
349 p->data_fname[i] = (char *) xmalloc(strlen(str)+1);
350 strcpy(p->data_fname[i], str);
351 p->data_BFile[i] = NULL;
353 for (i = 0; i<REC_BLOCK_TYPES; i++)
355 if (!(p->data_BFile[i] =
356 bf_open(bfs, p->data_fname[i],
357 CAST_ZINT_TO_INT(p->head.block_size[i]), rw)))
359 yaz_log(YLOG_FATAL|YLOG_ERRNO, "bf_open %s", p->data_fname[i]);
366 p->record_cache = (struct record_cache_entry *)
367 xmalloc(sizeof(*p->record_cache)*p->cache_max);
368 zebra_mutex_init(&p->mutex);
369 if (ret == ZEBRA_FAIL)
374 static void rec_encode_unsigned(unsigned n, unsigned char *buf, int *len)
379 buf[*len] = 128 + (n & 127);
387 static void rec_decode_unsigned(unsigned *np, unsigned char *buf, int *len)
393 while (buf[*len] > 127)
395 n += w*(buf[*len] & 127);
404 static void rec_encode_zint(zint n, unsigned char *buf, int *len)
409 buf[*len] = (unsigned) (128 + (n & 127));
413 buf[*len] = (unsigned) n;
417 static void rec_decode_zint(zint *np, unsigned char *buf, int *len)
423 while (buf[*len] > 127)
425 n += w*(buf[*len] & 127);
434 static void rec_cache_flush_block1(Records p, Record rec, Record last_rec,
435 char **out_buf, int *out_size,
441 for (i = 0; i<REC_NO_INFO; i++)
443 if (*out_offset + CAST_ZINT_TO_INT(rec->size[i]) + 20 > *out_size)
445 int new_size = *out_offset + rec->size[i] + 65536;
446 char *np = (char *) xmalloc(new_size);
448 memcpy(np, *out_buf, *out_offset);
450 *out_size = new_size;
455 rec_encode_zint(rec_sysno_to_int(rec->sysno),
456 (unsigned char *) *out_buf + *out_offset, &len);
457 (*out_offset) += len;
459 if (rec->size[i] == 0)
461 rec_encode_unsigned(1, (unsigned char *) *out_buf + *out_offset,
463 (*out_offset) += len;
465 else if (last_rec && rec->size[i] == last_rec->size[i] &&
466 !memcmp(rec->info[i], last_rec->info[i], rec->size[i]))
468 rec_encode_unsigned(0, (unsigned char *) *out_buf + *out_offset,
470 (*out_offset) += len;
474 rec_encode_unsigned(rec->size[i]+1,
475 (unsigned char *) *out_buf + *out_offset,
477 (*out_offset) += len;
478 memcpy(*out_buf + *out_offset, rec->info[i], rec->size[i]);
479 (*out_offset) += rec->size[i];
484 static ZEBRA_RES rec_write_multiple(Records p, int saveCount)
488 char compression_method;
492 char *out_buf = (char *) xmalloc(out_size);
493 zint *sysnos = (zint *) xmalloc(sizeof(*sysnos) * (p->cache_cur + 1));
494 zint *sysnop = sysnos;
495 ZEBRA_RES ret = ZEBRA_OK;
497 for (i = 0; i<p->cache_cur - saveCount; i++)
499 struct record_cache_entry *e = p->record_cache + i;
503 rec_cache_flush_block1(p, e->rec, last_rec, &out_buf,
504 &out_size, &out_offset);
505 *sysnop++ = rec_sysno_to_int(e->rec->sysno);
507 e->flag = recordFlagNop;
510 case recordFlagWrite:
511 if (rec_release_blocks(p, rec_sysno_to_int(e->rec->sysno))
515 rec_cache_flush_block1(p, e->rec, last_rec, &out_buf,
516 &out_size, &out_offset);
517 *sysnop++ = rec_sysno_to_int(e->rec->sysno);
519 e->flag = recordFlagNop;
522 case recordFlagDelete:
523 if (rec_delete_single(p, e->rec) != ZEBRA_OK)
526 e->flag = recordFlagNop;
536 unsigned int csize = 0; /* indicate compression "not performed yet" */
537 compression_method = p->compression_method;
538 switch (compression_method)
540 case REC_COMPRESS_BZIP2:
542 csize = out_offset + (out_offset >> 6) + 620;
543 rec_tmp_expand(p, csize);
544 #ifdef BZ_CONFIG_ERROR
545 i = BZ2_bzBuffToBuffCompress
547 i = bzBuffToBuffCompress
549 (p->tmp_buf+sizeof(zint)+sizeof(short)+
551 &csize, out_buf, out_offset, 1, 0, 30);
554 yaz_log(YLOG_WARN, "bzBuffToBuffCompress error code=%d", i);
557 yaz_log(YLOG_LOG, "compress %4d %5d %5d", ref_count, out_offset,
561 case REC_COMPRESS_NONE:
566 /* either no compression or compression not supported ... */
568 rec_tmp_expand(p, csize);
569 memcpy(p->tmp_buf + sizeof(zint) + sizeof(short) + sizeof(char),
570 out_buf, out_offset);
572 compression_method = REC_COMPRESS_NONE;
574 memcpy(p->tmp_buf + sizeof(zint), &ref_count, sizeof(ref_count));
575 memcpy(p->tmp_buf + sizeof(zint)+sizeof(short),
576 &compression_method, sizeof(compression_method));
578 /* -------- compression */
579 if (rec_write_tmp_buf(p, csize + sizeof(short) + sizeof(char), sysnos)
588 static ZEBRA_RES rec_cache_flush(Records p, int saveCount)
593 if (saveCount >= p->cache_cur)
596 ret = rec_write_multiple(p, saveCount);
598 for (i = 0; i<p->cache_cur - saveCount; i++)
600 struct record_cache_entry *e = p->record_cache + i;
603 /* i still being used ... */
604 for (j = 0; j<saveCount; j++, i++)
605 memcpy(p->record_cache+j, p->record_cache+i,
606 sizeof(*p->record_cache));
607 p->cache_cur = saveCount;
611 static Record *rec_cache_lookup(Records p, zint sysno,
612 enum recordCacheFlag flag)
615 for (i = 0; i<p->cache_cur; i++)
617 struct record_cache_entry *e = p->record_cache + i;
618 if (e->rec->sysno == sysno)
620 if (flag != recordFlagNop && e->flag == recordFlagNop)
628 static ZEBRA_RES rec_cache_insert(Records p, Record rec, enum recordCacheFlag flag)
630 struct record_cache_entry *e;
631 ZEBRA_RES ret = ZEBRA_OK;
633 if (p->cache_cur == p->cache_max)
634 ret = rec_cache_flush(p, 1);
635 else if (p->cache_cur > 0)
639 for (i = 0; i<p->cache_cur; i++)
641 Record r = (p->record_cache + i)->rec;
642 for (j = 0; j<REC_NO_INFO; j++)
646 ret = rec_cache_flush(p, 1);
648 assert(p->cache_cur < p->cache_max);
650 e = p->record_cache + (p->cache_cur)++;
652 e->rec = rec_cp(rec);
656 ZEBRA_RES rec_close(Records *pp)
660 ZEBRA_RES ret = ZEBRA_OK;
665 zebra_mutex_destroy(&p->mutex);
666 if (rec_cache_flush(p, 0) != ZEBRA_OK)
669 xfree(p->record_cache);
673 if (rec_write_head(p) != ZEBRA_OK)
678 bf_close(p->index_BFile);
680 for (i = 0; i<REC_BLOCK_TYPES; i++)
682 if (p->data_BFile[i])
683 bf_close(p->data_BFile[i]);
684 xfree(p->data_fname[i]);
692 static Record rec_get_int(Records p, zint sysno)
696 struct record_index_entry entry;
703 unsigned int bz_size;
705 char compression_method;
710 if ((recp = rec_cache_lookup(p, sysno, recordFlagNop)))
711 return rec_cp(*recp);
713 if (read_indx(p, rec_sysno_to_int(sysno), &entry, sizeof(entry), 1) < 1)
714 return NULL; /* record is not there! */
717 return NULL; /* record is deleted */
719 dst_type = (int) (entry.next & 7);
720 assert(dst_type < REC_BLOCK_TYPES);
721 freeblock = entry.next / 8;
723 assert(freeblock > 0);
725 rec_tmp_expand(p, entry.size);
728 r = bf_read(p->data_BFile[dst_type], freeblock, 0, 0, cptr);
731 memcpy(&freeblock, cptr, sizeof(freeblock));
737 cptr += p->head.block_size[dst_type] - sizeof(freeblock);
739 memcpy(&tmp, cptr, sizeof(tmp));
740 r = bf_read(p->data_BFile[dst_type], freeblock, 0, 0, cptr);
743 memcpy(&freeblock, cptr, sizeof(freeblock));
744 memcpy(cptr, &tmp, sizeof(tmp));
747 rec = (Record) xmalloc(sizeof(*rec));
749 memcpy(&compression_method, p->tmp_buf + sizeof(zint) + sizeof(short),
750 sizeof(compression_method));
751 in_buf = p->tmp_buf + sizeof(zint) + sizeof(short) + sizeof(char);
752 in_size = entry.size - sizeof(short) - sizeof(char);
753 switch (compression_method)
755 case REC_COMPRESS_BZIP2:
757 bz_size = entry.size * 20 + 100;
760 bz_buf = (char *) xmalloc(bz_size);
761 #ifdef BZ_CONFIG_ERROR
762 i = BZ2_bzBuffToBuffDecompress
764 i = bzBuffToBuffDecompress
766 (bz_buf, &bz_size, in_buf, in_size, 0, 0);
767 yaz_log(YLOG_LOG, "decompress %5d %5d", in_size, bz_size);
770 yaz_log(YLOG_LOG, "failed");
777 yaz_log(YLOG_FATAL, "cannot decompress record(s) in BZIP2 format");
781 case REC_COMPRESS_NONE:
784 for (i = 0; i<REC_NO_INFO; i++)
787 nptr = in_buf; /* skip ref count */
788 while (nptr < in_buf + in_size)
792 rec_decode_zint(&this_sysno, (unsigned char *) nptr, &len);
795 for (i = 0; i < REC_NO_INFO; i++)
797 unsigned int this_size;
798 rec_decode_unsigned(&this_size, (unsigned char *) nptr, &len);
803 rec->size[i] = this_size-1;
808 nptr += rec->size[i];
813 if (this_sysno == rec_sysno_to_int(sysno))
816 for (i = 0; i<REC_NO_INFO; i++)
818 if (rec->info[i] && rec->size[i])
820 char *np = xmalloc(rec->size[i]+1);
821 memcpy(np, rec->info[i], rec->size[i]);
822 np[rec->size[i]] = '\0';
827 assert(rec->info[i] == 0);
828 assert(rec->size[i] == 0);
832 if (rec_cache_insert(p, rec, recordFlagNop) != ZEBRA_OK)
837 Record rec_get(Records p, zint sysno)
840 zebra_mutex_lock(&p->mutex);
842 rec = rec_get_int(p, sysno);
843 zebra_mutex_unlock(&p->mutex);
847 Record rec_get_root(Records p)
849 return rec_get(p, rec_sysno_to_ext(1));
852 static Record rec_new_int(Records p)
859 rec = (Record) xmalloc(sizeof(*rec));
860 if (1 || p->head.index_free == 0)
861 sysno = (p->head.index_last)++;
864 struct record_index_entry entry;
866 if (read_indx(p, p->head.index_free, &entry, sizeof(entry), 0) < 1)
871 sysno = p->head.index_free;
872 p->head.index_free = entry.next;
874 (p->head.no_records)++;
875 rec->sysno = rec_sysno_to_ext(sysno);
876 for (i = 0; i < REC_NO_INFO; i++)
881 rec_cache_insert(p, rec, recordFlagNew);
885 Record rec_new(Records p)
888 zebra_mutex_lock(&p->mutex);
890 rec = rec_new_int(p);
891 zebra_mutex_unlock(&p->mutex);
895 ZEBRA_RES rec_del(Records p, Record *recpp)
898 ZEBRA_RES ret = ZEBRA_OK;
900 zebra_mutex_lock(&p->mutex);
901 (p->head.no_records)--;
902 if ((recp = rec_cache_lookup(p, (*recpp)->sysno, recordFlagDelete)))
909 ret = rec_cache_insert(p, *recpp, recordFlagDelete);
912 zebra_mutex_unlock(&p->mutex);
917 ZEBRA_RES rec_put(Records p, Record *recpp)
920 ZEBRA_RES ret = ZEBRA_OK;
922 zebra_mutex_lock(&p->mutex);
923 if ((recp = rec_cache_lookup(p, (*recpp)->sysno, recordFlagWrite)))
930 ret = rec_cache_insert(p, *recpp, recordFlagWrite);
933 zebra_mutex_unlock(&p->mutex);
938 void rec_free(Record *recpp)
944 for (i = 0; i < REC_NO_INFO; i++)
945 xfree((*recpp)->info[i]);
950 Record rec_cp(Record rec)
955 n = (Record) xmalloc(sizeof(*n));
956 n->sysno = rec->sysno;
957 for (i = 0; i < REC_NO_INFO; i++)
965 n->size[i] = rec->size[i];
966 n->info[i] = (char *) xmalloc(rec->size[i]+1);
967 memcpy(n->info[i], rec->info[i], rec->size[i]);
968 n->info[i][rec->size[i]] = '\0';
974 char *rec_strdup(const char *s, size_t *len)
984 p = (char *) xmalloc(*len);
992 * indent-tabs-mode: nil
994 * vim: shiftwidth=4 tabstop=8 expandtab