1 /* $Id: recindex.c,v 1.52 2006-09-15 10:45:13 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 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 = 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, SYSNO 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, 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 = 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, 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 += 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]);
359 p->record_cache = (struct record_cache_entry *)
360 xmalloc(sizeof(*p->record_cache)*p->cache_max);
361 zebra_mutex_init(&p->mutex);
362 if (ret == ZEBRA_FAIL)
367 static void rec_encode_unsigned(unsigned n, unsigned char *buf, int *len)
372 buf[*len] = 128 + (n & 127);
380 static void rec_decode_unsigned(unsigned *np, unsigned char *buf, int *len)
386 while (buf[*len] > 127)
388 n += w*(buf[*len] & 127);
397 static void rec_encode_zint(zint n, unsigned char *buf, int *len)
402 buf[*len] = (unsigned) (128 + (n & 127));
406 buf[*len] = (unsigned) n;
410 static void rec_decode_zint(zint *np, unsigned char *buf, int *len)
416 while (buf[*len] > 127)
418 n += w*(buf[*len] & 127);
427 static void rec_cache_flush_block1(Records p, Record rec, Record last_rec,
428 char **out_buf, int *out_size,
434 for (i = 0; i<REC_NO_INFO; i++)
436 if (*out_offset + CAST_ZINT_TO_INT(rec->size[i]) + 20 > *out_size)
438 int new_size = *out_offset + rec->size[i] + 65536;
439 char *np = (char *) xmalloc(new_size);
441 memcpy(np, *out_buf, *out_offset);
443 *out_size = new_size;
448 rec_encode_zint(rec_sysno_to_int(rec->sysno),
449 (unsigned char *) *out_buf + *out_offset, &len);
450 (*out_offset) += len;
452 if (rec->size[i] == 0)
454 rec_encode_unsigned(1, (unsigned char *) *out_buf + *out_offset,
456 (*out_offset) += len;
458 else if (last_rec && rec->size[i] == last_rec->size[i] &&
459 !memcmp(rec->info[i], last_rec->info[i], rec->size[i]))
461 rec_encode_unsigned(0, (unsigned char *) *out_buf + *out_offset,
463 (*out_offset) += len;
467 rec_encode_unsigned(rec->size[i]+1,
468 (unsigned char *) *out_buf + *out_offset,
470 (*out_offset) += len;
471 memcpy(*out_buf + *out_offset, rec->info[i], rec->size[i]);
472 (*out_offset) += rec->size[i];
477 static ZEBRA_RES rec_write_multiple(Records p, int saveCount)
481 char compression_method;
485 char *out_buf = (char *) xmalloc(out_size);
486 SYSNO *sysnos = (SYSNO *) xmalloc(sizeof(*sysnos) * (p->cache_cur + 1));
487 SYSNO *sysnop = sysnos;
488 ZEBRA_RES ret = ZEBRA_OK;
490 for (i = 0; i<p->cache_cur - saveCount; i++)
492 struct record_cache_entry *e = p->record_cache + i;
496 rec_cache_flush_block1(p, e->rec, last_rec, &out_buf,
497 &out_size, &out_offset);
498 *sysnop++ = rec_sysno_to_int(e->rec->sysno);
500 e->flag = recordFlagNop;
503 case recordFlagWrite:
504 if (rec_release_blocks(p, rec_sysno_to_int(e->rec->sysno))
508 rec_cache_flush_block1(p, e->rec, last_rec, &out_buf,
509 &out_size, &out_offset);
510 *sysnop++ = rec_sysno_to_int(e->rec->sysno);
512 e->flag = recordFlagNop;
515 case recordFlagDelete:
516 if (rec_delete_single(p, e->rec) != ZEBRA_OK)
519 e->flag = recordFlagNop;
529 unsigned int csize = 0; /* indicate compression "not performed yet" */
530 compression_method = p->compression_method;
531 switch (compression_method)
533 case REC_COMPRESS_BZIP2:
535 csize = out_offset + (out_offset >> 6) + 620;
536 rec_tmp_expand(p, csize);
537 #ifdef BZ_CONFIG_ERROR
538 i = BZ2_bzBuffToBuffCompress
540 i = bzBuffToBuffCompress
542 (p->tmp_buf+sizeof(zint)+sizeof(short)+
544 &csize, out_buf, out_offset, 1, 0, 30);
547 yaz_log(YLOG_WARN, "bzBuffToBuffCompress error code=%d", i);
550 yaz_log(YLOG_LOG, "compress %4d %5d %5d", ref_count, out_offset,
554 case REC_COMPRESS_NONE:
559 /* either no compression or compression not supported ... */
561 rec_tmp_expand(p, csize);
562 memcpy(p->tmp_buf + sizeof(zint) + sizeof(short) + sizeof(char),
563 out_buf, out_offset);
565 compression_method = REC_COMPRESS_NONE;
567 memcpy(p->tmp_buf + sizeof(zint), &ref_count, sizeof(ref_count));
568 memcpy(p->tmp_buf + sizeof(zint)+sizeof(short),
569 &compression_method, sizeof(compression_method));
571 /* -------- compression */
572 if (rec_write_tmp_buf(p, csize + sizeof(short) + sizeof(char), sysnos)
581 static ZEBRA_RES rec_cache_flush(Records p, int saveCount)
586 if (saveCount >= p->cache_cur)
589 ret = rec_write_multiple(p, saveCount);
591 for (i = 0; i<p->cache_cur - saveCount; i++)
593 struct record_cache_entry *e = p->record_cache + i;
596 /* i still being used ... */
597 for (j = 0; j<saveCount; j++, i++)
598 memcpy(p->record_cache+j, p->record_cache+i,
599 sizeof(*p->record_cache));
600 p->cache_cur = saveCount;
604 static Record *rec_cache_lookup(Records p, SYSNO sysno,
605 enum recordCacheFlag flag)
608 for (i = 0; i<p->cache_cur; i++)
610 struct record_cache_entry *e = p->record_cache + i;
611 if (e->rec->sysno == sysno)
613 if (flag != recordFlagNop && e->flag == recordFlagNop)
621 static ZEBRA_RES rec_cache_insert(Records p, Record rec, enum recordCacheFlag flag)
623 struct record_cache_entry *e;
624 ZEBRA_RES ret = ZEBRA_OK;
626 if (p->cache_cur == p->cache_max)
627 ret = rec_cache_flush(p, 1);
628 else if (p->cache_cur > 0)
632 for (i = 0; i<p->cache_cur; i++)
634 Record r = (p->record_cache + i)->rec;
635 for (j = 0; j<REC_NO_INFO; j++)
639 ret = rec_cache_flush(p, 1);
641 assert(p->cache_cur < p->cache_max);
643 e = p->record_cache + (p->cache_cur)++;
645 e->rec = rec_cp(rec);
649 ZEBRA_RES rec_close(Records *pp)
653 ZEBRA_RES ret = ZEBRA_OK;
658 zebra_mutex_destroy(&p->mutex);
659 if (rec_cache_flush(p, 0) != ZEBRA_OK)
662 xfree(p->record_cache);
666 if (rec_write_head(p) != ZEBRA_OK)
671 bf_close(p->index_BFile);
673 for (i = 0; i<REC_BLOCK_TYPES; i++)
675 if (p->data_BFile[i])
676 bf_close(p->data_BFile[i]);
677 xfree(p->data_fname[i]);
685 static Record rec_get_int(Records p, SYSNO sysno)
689 struct record_index_entry entry;
696 unsigned int bz_size;
698 char compression_method;
703 if ((recp = rec_cache_lookup(p, sysno, recordFlagNop)))
704 return rec_cp(*recp);
706 if (read_indx(p, rec_sysno_to_int(sysno), &entry, sizeof(entry), 1) < 1)
707 return NULL; /* record is not there! */
710 return NULL; /* record is deleted */
712 dst_type = (int) (entry.next & 7);
713 assert(dst_type < REC_BLOCK_TYPES);
714 freeblock = entry.next / 8;
716 assert(freeblock > 0);
718 rec_tmp_expand(p, entry.size);
721 r = bf_read(p->data_BFile[dst_type], freeblock, 0, 0, cptr);
724 memcpy(&freeblock, cptr, sizeof(freeblock));
730 cptr += p->head.block_size[dst_type] - sizeof(freeblock);
732 memcpy(&tmp, cptr, sizeof(tmp));
733 r = bf_read(p->data_BFile[dst_type], freeblock, 0, 0, cptr);
736 memcpy(&freeblock, cptr, sizeof(freeblock));
737 memcpy(cptr, &tmp, sizeof(tmp));
740 rec = (Record) xmalloc(sizeof(*rec));
742 memcpy(&compression_method, p->tmp_buf + sizeof(zint) + sizeof(short),
743 sizeof(compression_method));
744 in_buf = p->tmp_buf + sizeof(zint) + sizeof(short) + sizeof(char);
745 in_size = entry.size - sizeof(short) - sizeof(char);
746 switch (compression_method)
748 case REC_COMPRESS_BZIP2:
750 bz_size = entry.size * 20 + 100;
753 bz_buf = (char *) xmalloc(bz_size);
754 #ifdef BZ_CONFIG_ERROR
755 i = BZ2_bzBuffToBuffDecompress
757 i = bzBuffToBuffDecompress
759 (bz_buf, &bz_size, in_buf, in_size, 0, 0);
760 yaz_log(YLOG_LOG, "decompress %5d %5d", in_size, bz_size);
763 yaz_log(YLOG_LOG, "failed");
770 yaz_log(YLOG_FATAL, "cannot decompress record(s) in BZIP2 format");
774 case REC_COMPRESS_NONE:
777 for (i = 0; i<REC_NO_INFO; i++)
780 nptr = in_buf; /* skip ref count */
781 while (nptr < in_buf + in_size)
785 rec_decode_zint(&this_sysno, (unsigned char *) nptr, &len);
788 for (i = 0; i < REC_NO_INFO; i++)
790 unsigned int this_size;
791 rec_decode_unsigned(&this_size, (unsigned char *) nptr, &len);
796 rec->size[i] = this_size-1;
801 nptr += rec->size[i];
806 if (this_sysno == rec_sysno_to_int(sysno))
809 for (i = 0; i<REC_NO_INFO; i++)
811 if (rec->info[i] && rec->size[i])
813 char *np = xmalloc(rec->size[i]+1);
814 memcpy(np, rec->info[i], rec->size[i]);
815 np[rec->size[i]] = '\0';
820 assert(rec->info[i] == 0);
821 assert(rec->size[i] == 0);
825 if (rec_cache_insert(p, rec, recordFlagNop) != ZEBRA_OK)
830 Record rec_get(Records p, SYSNO sysno)
833 zebra_mutex_lock(&p->mutex);
835 rec = rec_get_int(p, sysno);
836 zebra_mutex_unlock(&p->mutex);
840 Record rec_get_root(Records p)
842 return rec_get(p, rec_sysno_to_ext(1));
845 static Record rec_new_int(Records p)
852 rec = (Record) xmalloc(sizeof(*rec));
853 if (1 || p->head.index_free == 0)
854 sysno = (p->head.index_last)++;
857 struct record_index_entry entry;
859 if (read_indx(p, p->head.index_free, &entry, sizeof(entry), 0) < 1)
864 sysno = p->head.index_free;
865 p->head.index_free = entry.next;
867 (p->head.no_records)++;
868 rec->sysno = rec_sysno_to_ext(sysno);
869 for (i = 0; i < REC_NO_INFO; i++)
874 rec_cache_insert(p, rec, recordFlagNew);
878 Record rec_new(Records p)
881 zebra_mutex_lock(&p->mutex);
883 rec = rec_new_int(p);
884 zebra_mutex_unlock(&p->mutex);
888 ZEBRA_RES rec_del(Records p, Record *recpp)
891 ZEBRA_RES ret = ZEBRA_OK;
893 zebra_mutex_lock(&p->mutex);
894 (p->head.no_records)--;
895 if ((recp = rec_cache_lookup(p, (*recpp)->sysno, recordFlagDelete)))
902 ret = rec_cache_insert(p, *recpp, recordFlagDelete);
905 zebra_mutex_unlock(&p->mutex);
910 ZEBRA_RES rec_put(Records p, Record *recpp)
913 ZEBRA_RES ret = ZEBRA_OK;
915 zebra_mutex_lock(&p->mutex);
916 if ((recp = rec_cache_lookup(p, (*recpp)->sysno, recordFlagWrite)))
923 ret = rec_cache_insert(p, *recpp, recordFlagWrite);
926 zebra_mutex_unlock(&p->mutex);
931 void rec_free(Record *recpp)
937 for (i = 0; i < REC_NO_INFO; i++)
938 xfree((*recpp)->info[i]);
943 Record rec_cp(Record rec)
948 n = (Record) xmalloc(sizeof(*n));
949 n->sysno = rec->sysno;
950 for (i = 0; i < REC_NO_INFO; i++)
958 n->size[i] = rec->size[i];
959 n->info[i] = (char *) xmalloc(rec->size[i]+1);
960 memcpy(n->info[i], rec->info[i], rec->size[i]);
961 n->info[i][rec->size[i]] = '\0';
967 char *rec_strdup(const char *s, size_t *len)
977 p = (char *) xmalloc(*len);
985 * indent-tabs-mode: nil
987 * vim: shiftwidth=4 tabstop=8 expandtab