1 /* This file is part of the Zebra server.
2 Copyright (C) 1994-2009 Index Data
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 * Format of first block
26 * Format of subsequent blocks
30 * Format of each record
32 * (length, data) - pairs
33 * length = 0 if same as previous
40 #include <yaz/yaz-util.h>
41 #include <idzebra/bfile.h>
48 #define REC_BLOCK_TYPES 2
49 #define REC_HEAD_MAGIC "recindex"
54 int compression_method;
58 char *data_fname[REC_BLOCK_TYPES];
59 BFile data_BFile[REC_BLOCK_TYPES];
64 struct record_cache_entry *record_cache;
69 int compression_chunk_size;
76 zint block_size[REC_BLOCK_TYPES];
77 zint block_free[REC_BLOCK_TYPES];
78 zint block_last[REC_BLOCK_TYPES];
79 zint block_used[REC_BLOCK_TYPES];
80 zint block_move[REC_BLOCK_TYPES];
90 enum recordCacheFlag { recordFlagNop, recordFlagWrite, recordFlagNew,
93 struct record_cache_entry {
95 enum recordCacheFlag flag;
98 struct record_index_entry {
99 zint next; /* first block of record info / next free entry */
100 int size; /* size of record or 0 if free entry */
103 Record rec_cp(Record rec);
105 /* Modify argument to if below: 1=normal, 0=sysno testing */
107 /* If this is used sysno are not converted (no testing) */
108 #define FAKE_OFFSET 0
109 #define USUAL_RANGE 6000000000LL
112 /* Use a fake > 2^32 offset so we can test for proper 64-bit handling */
113 #define FAKE_OFFSET 6000000000LL
114 #define USUAL_RANGE 2000000000LL
117 static zint rec_sysno_to_ext(zint sysno)
119 assert(sysno >= 0 && sysno <= USUAL_RANGE);
120 return sysno + FAKE_OFFSET;
123 zint rec_sysno_to_int(zint sysno)
125 assert(sysno >= FAKE_OFFSET && sysno <= FAKE_OFFSET + USUAL_RANGE);
126 return sysno - FAKE_OFFSET;
129 static void rec_tmp_expand(Records p, int size)
131 if (p->tmp_size < size + 2048 ||
132 p->tmp_size < p->head.block_size[REC_BLOCK_TYPES-1]*2)
135 p->tmp_size = size + (int)
136 (p->head.block_size[REC_BLOCK_TYPES-1])*2 + 2048;
137 p->tmp_buf = (char *) xmalloc(p->tmp_size);
141 static ZEBRA_RES rec_release_blocks(Records p, zint sysno)
143 struct record_index_entry entry;
145 char block_and_ref[sizeof(zint) + sizeof(short)];
149 if (recindex_read_indx(p->recindex, sysno, &entry, sizeof(entry), 1) != 1)
152 freeblock = entry.next;
153 assert(freeblock > 0);
154 dst_type = CAST_ZINT_TO_INT(freeblock & 7);
155 assert(dst_type < REC_BLOCK_TYPES);
156 freeblock = freeblock / 8;
159 if (bf_read(p->data_BFile[dst_type], freeblock, 0,
160 first ? sizeof(block_and_ref) : sizeof(zint),
163 yaz_log(YLOG_FATAL|YLOG_ERRNO, "read in rec_del_single");
169 memcpy(&ref, block_and_ref + sizeof(freeblock), sizeof(ref));
171 memcpy(block_and_ref + sizeof(freeblock), &ref, sizeof(ref));
174 /* there is still a reference to this block.. */
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");
183 /* the list of blocks can all be removed (ref == 0) */
187 if (bf_write(p->data_BFile[dst_type], freeblock, 0, sizeof(freeblock),
188 &p->head.block_free[dst_type]))
190 yaz_log(YLOG_FATAL|YLOG_ERRNO, "write in rec_del_single");
193 p->head.block_free[dst_type] = freeblock;
194 memcpy(&freeblock, block_and_ref, sizeof(freeblock));
196 p->head.block_used[dst_type]--;
198 p->head.total_bytes -= entry.size;
202 static ZEBRA_RES rec_delete_single(Records p, Record rec)
204 struct record_index_entry entry;
206 /* all data in entry must be reset, since it's written verbatim */
207 memset(&entry, '\0', sizeof(entry));
208 if (rec_release_blocks(p, rec_sysno_to_int(rec->sysno)) != ZEBRA_OK)
211 entry.next = p->head.index_free;
213 p->head.index_free = rec_sysno_to_int(rec->sysno);
214 recindex_write_indx(p->recindex, rec_sysno_to_int(rec->sysno), &entry, sizeof(entry));
218 static ZEBRA_RES rec_write_tmp_buf(Records p, int size, zint *sysnos)
220 struct record_index_entry entry;
222 char *cptr = p->tmp_buf;
223 zint block_prev = -1, block_free;
227 /* all data in entry must be reset, since it's written verbatim */
228 memset(&entry, '\0', sizeof(entry));
230 for (i = 1; i<REC_BLOCK_TYPES; i++)
231 if (size >= p->head.block_move[i])
233 while (no_written < size)
235 block_free = p->head.block_free[dst_type];
238 if (bf_read(p->data_BFile[dst_type],
239 block_free, 0, sizeof(*p->head.block_free),
240 &p->head.block_free[dst_type]) != 1)
242 yaz_log(YLOG_FATAL|YLOG_ERRNO, "read in %s at free block "
244 p->data_fname[dst_type], block_free);
249 block_free = p->head.block_last[dst_type]++;
250 if (block_prev == -1)
252 entry.next = block_free*8 + dst_type;
254 p->head.total_bytes += size;
257 recindex_write_indx(p->recindex, *sysnos, &entry, sizeof(entry));
263 memcpy(cptr, &block_free, sizeof(block_free));
264 bf_write(p->data_BFile[dst_type], block_prev, 0, 0, cptr);
265 cptr = p->tmp_buf + no_written;
267 block_prev = block_free;
268 no_written += CAST_ZINT_TO_INT(p->head.block_size[dst_type])
270 p->head.block_used[dst_type]++;
272 assert(block_prev != -1);
274 memcpy(cptr, &block_free, sizeof(block_free));
275 bf_write(p->data_BFile[dst_type], block_prev, 0,
276 sizeof(block_free) + (p->tmp_buf+size) - cptr, cptr);
280 Records rec_open(BFiles bfs, int rw, int compression_method)
285 ZEBRA_RES ret = ZEBRA_OK;
287 p = (Records) xmalloc(sizeof(*p));
288 memset(&p->head, '\0', sizeof(p->head));
289 p->compression_method = compression_method;
292 p->tmp_buf = (char *) xmalloc(p->tmp_size);
293 p->compression_chunk_size = 0;
294 p->recindex = recindex_open(bfs, rw, 0 /* 1=isamb for recindex */);
295 r = recindex_read_head(p->recindex, p->tmp_buf);
299 memcpy(p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic));
300 sprintf(p->head.version, "%3d", REC_VERSION);
301 p->head.index_free = 0;
302 p->head.index_last = 1;
303 p->head.no_records = 0;
304 p->head.total_bytes = 0;
305 for (i = 0; i<REC_BLOCK_TYPES; i++)
307 p->head.block_free[i] = 0;
308 p->head.block_last[i] = 1;
309 p->head.block_used[i] = 0;
311 p->head.block_size[0] = 128;
312 p->head.block_move[0] = 0;
313 for (i = 1; i<REC_BLOCK_TYPES; i++)
315 p->head.block_size[i] = p->head.block_size[i-1] * 4;
316 p->head.block_move[i] = p->head.block_size[i] * 24;
320 if (recindex_write_head(p->recindex,
321 &p->head, sizeof(p->head)) != ZEBRA_OK)
326 memcpy(&p->head, p->tmp_buf, sizeof(p->head));
327 if (memcmp(p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic)))
329 yaz_log(YLOG_FATAL, "file %s has bad format",
330 recindex_get_fname(p->recindex));
333 version = atoi(p->head.version);
334 if (version != REC_VERSION)
336 yaz_log(YLOG_FATAL, "file %s is version %d, but version"
338 recindex_get_fname(p->recindex), version, REC_VERSION);
341 p->compression_chunk_size = 90000; /* good for BZIP2 */
344 for (i = 0; i<REC_BLOCK_TYPES; i++)
347 sprintf(str, "recd%c", i + 'A');
348 p->data_fname[i] = (char *) xmalloc(strlen(str)+1);
349 strcpy(p->data_fname[i], str);
350 p->data_BFile[i] = NULL;
352 for (i = 0; i<REC_BLOCK_TYPES; i++)
354 if (!(p->data_BFile[i] =
355 bf_open(bfs, p->data_fname[i],
356 CAST_ZINT_TO_INT(p->head.block_size[i]), rw)))
358 yaz_log(YLOG_FATAL|YLOG_ERRNO, "bf_open %s", p->data_fname[i]);
365 p->record_cache = (struct record_cache_entry *)
366 xmalloc(sizeof(*p->record_cache)*p->cache_max);
367 zebra_mutex_init(&p->mutex);
368 if (ret == ZEBRA_FAIL)
373 static void rec_encode_unsigned(unsigned n, unsigned char *buf, int *len)
378 buf[*len] = 128 + (n & 127);
386 static void rec_decode_unsigned(unsigned *np, unsigned char *buf, int *len)
392 while (buf[*len] > 127)
394 n += w*(buf[*len] & 127);
403 static void rec_encode_zint(zint n, unsigned char *buf, int *len)
408 buf[*len] = (unsigned) (128 + (n & 127));
412 buf[*len] = (unsigned) n;
416 static void rec_decode_zint(zint *np, unsigned char *buf, int *len)
422 while (buf[*len] > 127)
424 n += w*(buf[*len] & 127);
433 static void rec_cache_flush_block1(Records p, Record rec, Record last_rec,
434 char **out_buf, int *out_size,
440 for (i = 0; i<REC_NO_INFO; i++)
442 if (*out_offset + CAST_ZINT_TO_INT(rec->size[i]) + 20 > *out_size)
444 int new_size = *out_offset + rec->size[i] + 65536;
445 char *np = (char *) xmalloc(new_size);
447 memcpy(np, *out_buf, *out_offset);
449 *out_size = new_size;
454 rec_encode_zint(rec_sysno_to_int(rec->sysno),
455 (unsigned char *) *out_buf + *out_offset, &len);
456 (*out_offset) += len;
458 if (rec->size[i] == 0)
460 rec_encode_unsigned(1, (unsigned char *) *out_buf + *out_offset,
462 (*out_offset) += len;
464 else if (last_rec && rec->size[i] == last_rec->size[i] &&
465 !memcmp(rec->info[i], last_rec->info[i], rec->size[i]))
467 rec_encode_unsigned(0, (unsigned char *) *out_buf + *out_offset,
469 (*out_offset) += len;
473 rec_encode_unsigned(rec->size[i]+1,
474 (unsigned char *) *out_buf + *out_offset,
476 (*out_offset) += len;
477 memcpy(*out_buf + *out_offset, rec->info[i], rec->size[i]);
478 (*out_offset) += rec->size[i];
483 static ZEBRA_RES rec_write_multiple(Records p, int saveCount)
487 char compression_method;
491 char *out_buf = (char *) xmalloc(out_size);
492 zint *sysnos = (zint *) xmalloc(sizeof(*sysnos) * (p->cache_cur + 1));
493 zint *sysnop = sysnos;
494 ZEBRA_RES ret = ZEBRA_OK;
496 for (i = 0; i<p->cache_cur - saveCount; i++)
498 struct record_cache_entry *e = p->record_cache + i;
502 rec_cache_flush_block1(p, e->rec, last_rec, &out_buf,
503 &out_size, &out_offset);
504 *sysnop++ = rec_sysno_to_int(e->rec->sysno);
506 e->flag = recordFlagNop;
509 case recordFlagWrite:
510 if (rec_release_blocks(p, rec_sysno_to_int(e->rec->sysno))
514 rec_cache_flush_block1(p, e->rec, last_rec, &out_buf,
515 &out_size, &out_offset);
516 *sysnop++ = rec_sysno_to_int(e->rec->sysno);
518 e->flag = recordFlagNop;
521 case recordFlagDelete:
522 if (rec_delete_single(p, e->rec) != ZEBRA_OK)
525 e->flag = recordFlagNop;
537 unsigned int csize = 0; /* indicate compression "not performed yet" */
538 compression_method = p->compression_method;
539 switch (compression_method)
541 case REC_COMPRESS_BZIP2:
543 csize = out_offset + (out_offset >> 6) + 620;
544 rec_tmp_expand(p, csize);
545 #ifdef BZ_CONFIG_ERROR
546 i = BZ2_bzBuffToBuffCompress
548 i = bzBuffToBuffCompress
550 (p->tmp_buf+sizeof(zint)+sizeof(short)+
552 &csize, out_buf, out_offset, 1, 0, 30);
555 yaz_log(YLOG_WARN, "bzBuffToBuffCompress error code=%d", i);
558 yaz_log(YLOG_LOG, "compress %4d %5d %5d", ref_count, out_offset,
562 case REC_COMPRESS_NONE:
567 /* either no compression or compression not supported ... */
569 rec_tmp_expand(p, csize);
570 memcpy(p->tmp_buf + sizeof(zint) + sizeof(short) + sizeof(char),
571 out_buf, out_offset);
573 compression_method = REC_COMPRESS_NONE;
575 memcpy(p->tmp_buf + sizeof(zint), &ref_count, sizeof(ref_count));
576 memcpy(p->tmp_buf + sizeof(zint)+sizeof(short),
577 &compression_method, sizeof(compression_method));
579 /* -------- compression */
580 if (rec_write_tmp_buf(p, csize + sizeof(short) + sizeof(char), sysnos)
589 static ZEBRA_RES rec_cache_flush(Records p, int saveCount)
594 if (saveCount >= p->cache_cur)
597 ret = rec_write_multiple(p, saveCount);
599 for (i = 0; i<p->cache_cur - saveCount; i++)
601 struct record_cache_entry *e = p->record_cache + i;
604 /* i still being used ... */
605 for (j = 0; j<saveCount; j++, i++)
606 memcpy(p->record_cache+j, p->record_cache+i,
607 sizeof(*p->record_cache));
608 p->cache_cur = saveCount;
612 static Record *rec_cache_lookup(Records p, zint sysno,
613 enum recordCacheFlag flag)
616 for (i = 0; i<p->cache_cur; i++)
618 struct record_cache_entry *e = p->record_cache + i;
619 if (e->rec->sysno == sysno)
621 if (flag != recordFlagNop && e->flag == recordFlagNop)
629 static ZEBRA_RES rec_cache_insert(Records p, Record rec, enum recordCacheFlag flag)
631 struct record_cache_entry *e;
632 ZEBRA_RES ret = ZEBRA_OK;
634 if (p->cache_cur == p->cache_max)
635 ret = rec_cache_flush(p, 1);
636 else if (p->cache_cur > 0)
640 for (i = 0; i<p->cache_cur; i++)
642 Record r = (p->record_cache + i)->rec;
643 for (j = 0; j<REC_NO_INFO; j++)
646 if (used > p->compression_chunk_size)
647 ret = rec_cache_flush(p, 1);
649 assert(p->cache_cur < p->cache_max);
651 e = p->record_cache + (p->cache_cur)++;
653 e->rec = rec_cp(rec);
657 ZEBRA_RES rec_close(Records *pp)
661 ZEBRA_RES ret = ZEBRA_OK;
666 zebra_mutex_destroy(&p->mutex);
667 if (rec_cache_flush(p, 0) != ZEBRA_OK)
670 xfree(p->record_cache);
674 if (recindex_write_head(p->recindex, &p->head, sizeof(p->head)) != ZEBRA_OK)
678 recindex_close(p->recindex);
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 (recindex_read_indx(p->recindex, 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 (recindex_read_indx(p->recindex, 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);
989 void rec_prstat(Records records, int verbose)
992 zint total_bytes = 0;
995 "Total records %8" ZINT_FORMAT0,
996 records->head.no_records);
998 for (i = 0; i< REC_BLOCK_TYPES; i++)
1000 yaz_log (YLOG_LOG, "Record blocks of size "ZINT_FORMAT,
1001 records->head.block_size[i]);
1003 " Used/Total/Bytes used "
1004 ZINT_FORMAT "/" ZINT_FORMAT "/" ZINT_FORMAT,
1005 records->head.block_used[i], records->head.block_last[i]-1,
1006 records->head.block_used[i] * records->head.block_size[i]);
1008 records->head.block_used[i] * records->head.block_size[i];
1010 yaz_log(YLOG_LOG, " Block Last " ZINT_FORMAT, records->head.block_last[i]);
1012 { /* analyse free lists */
1014 zint block_free = records->head.block_free[i];
1015 WRBUF w = wrbuf_alloc();
1020 wrbuf_printf(w, " " ZINT_FORMAT, block_free);
1021 if (bf_read(records->data_BFile[i],
1022 block_free, 0, sizeof(nblock), &nblock) != 1)
1024 yaz_log(YLOG_FATAL|YLOG_ERRNO, "read in %s at free block "
1026 records->data_fname[i], block_free);
1029 block_free = nblock;
1032 " Number in free list %8" ZINT_FORMAT0, no_free);
1034 yaz_log(YLOG_LOG, "%s", wrbuf_cstr(w));
1039 "Total size of record index in bytes %8" ZINT_FORMAT0,
1040 records->head.total_bytes);
1042 "Total size with overhead %8" ZINT_FORMAT0,
1049 * c-file-style: "Stroustrup"
1050 * indent-tabs-mode: nil
1052 * vim: shiftwidth=4 tabstop=8 expandtab