1 /* This file is part of the Zebra server.
2 Copyright (C) 1995-2008 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 if (bf_write(p->data_BFile[dst_type], freeblock, 0,
175 sizeof(block_and_ref), block_and_ref))
177 yaz_log(YLOG_FATAL|YLOG_ERRNO, "write in rec_del_single");
185 if (bf_write(p->data_BFile[dst_type], freeblock, 0, sizeof(freeblock),
186 &p->head.block_free[dst_type]))
188 yaz_log(YLOG_FATAL|YLOG_ERRNO, "write in rec_del_single");
191 p->head.block_free[dst_type] = freeblock;
192 memcpy(&freeblock, block_and_ref, sizeof(freeblock));
194 p->head.block_used[dst_type]--;
196 p->head.total_bytes -= entry.size;
200 static ZEBRA_RES rec_delete_single(Records p, Record rec)
202 struct record_index_entry entry;
204 /* all data in entry must be reset, since it's written verbatim */
205 memset(&entry, '\0', sizeof(entry));
206 if (rec_release_blocks(p, rec_sysno_to_int(rec->sysno)) != ZEBRA_OK)
209 entry.next = p->head.index_free;
211 p->head.index_free = rec_sysno_to_int(rec->sysno);
212 recindex_write_indx(p->recindex, rec_sysno_to_int(rec->sysno), &entry, sizeof(entry));
216 static ZEBRA_RES rec_write_tmp_buf(Records p, int size, zint *sysnos)
218 struct record_index_entry entry;
220 char *cptr = p->tmp_buf;
221 zint block_prev = -1, block_free;
225 /* all data in entry must be reset, since it's written verbatim */
226 memset(&entry, '\0', sizeof(entry));
228 for (i = 1; i<REC_BLOCK_TYPES; i++)
229 if (size >= p->head.block_move[i])
231 while (no_written < size)
233 block_free = p->head.block_free[dst_type];
236 if (bf_read(p->data_BFile[dst_type],
237 block_free, 0, sizeof(*p->head.block_free),
238 &p->head.block_free[dst_type]) != 1)
240 yaz_log(YLOG_FATAL|YLOG_ERRNO, "read in %s at free block "
242 p->data_fname[dst_type], block_free);
247 block_free = p->head.block_last[dst_type]++;
248 if (block_prev == -1)
250 entry.next = block_free*8 + dst_type;
252 p->head.total_bytes += size;
255 recindex_write_indx(p->recindex, *sysnos, &entry, sizeof(entry));
261 memcpy(cptr, &block_free, sizeof(block_free));
262 bf_write(p->data_BFile[dst_type], block_prev, 0, 0, cptr);
263 cptr = p->tmp_buf + no_written;
265 block_prev = block_free;
266 no_written += CAST_ZINT_TO_INT(p->head.block_size[dst_type])
268 p->head.block_used[dst_type]++;
270 assert(block_prev != -1);
272 memcpy(cptr, &block_free, sizeof(block_free));
273 bf_write(p->data_BFile[dst_type], block_prev, 0,
274 sizeof(block_free) + (p->tmp_buf+size) - cptr, cptr);
278 Records rec_open(BFiles bfs, int rw, int compression_method)
283 ZEBRA_RES ret = ZEBRA_OK;
285 p = (Records) xmalloc(sizeof(*p));
286 memset(&p->head, '\0', sizeof(p->head));
287 p->compression_method = compression_method;
290 p->tmp_buf = (char *) xmalloc(p->tmp_size);
291 p->compression_chunk_size = 0;
292 p->recindex = recindex_open(bfs, rw, 0 /* 1=isamb for recindex */);
293 r = recindex_read_head(p->recindex, 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 (recindex_write_head(p->recindex,
319 &p->head, sizeof(p->head)) != 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",
328 recindex_get_fname(p->recindex));
331 version = atoi(p->head.version);
332 if (version != REC_VERSION)
334 yaz_log(YLOG_FATAL, "file %s is version %d, but version"
336 recindex_get_fname(p->recindex), version, REC_VERSION);
339 p->compression_chunk_size = 90000; /* good for BZIP2 */
342 for (i = 0; i<REC_BLOCK_TYPES; i++)
345 sprintf(str, "recd%c", i + 'A');
346 p->data_fname[i] = (char *) xmalloc(strlen(str)+1);
347 strcpy(p->data_fname[i], str);
348 p->data_BFile[i] = NULL;
350 for (i = 0; i<REC_BLOCK_TYPES; i++)
352 if (!(p->data_BFile[i] =
353 bf_open(bfs, p->data_fname[i],
354 CAST_ZINT_TO_INT(p->head.block_size[i]), rw)))
356 yaz_log(YLOG_FATAL|YLOG_ERRNO, "bf_open %s", p->data_fname[i]);
363 p->record_cache = (struct record_cache_entry *)
364 xmalloc(sizeof(*p->record_cache)*p->cache_max);
365 zebra_mutex_init(&p->mutex);
366 if (ret == ZEBRA_FAIL)
371 static void rec_encode_unsigned(unsigned n, unsigned char *buf, int *len)
376 buf[*len] = 128 + (n & 127);
384 static void rec_decode_unsigned(unsigned *np, unsigned char *buf, int *len)
390 while (buf[*len] > 127)
392 n += w*(buf[*len] & 127);
401 static void rec_encode_zint(zint n, unsigned char *buf, int *len)
406 buf[*len] = (unsigned) (128 + (n & 127));
410 buf[*len] = (unsigned) n;
414 static void rec_decode_zint(zint *np, unsigned char *buf, int *len)
420 while (buf[*len] > 127)
422 n += w*(buf[*len] & 127);
431 static void rec_cache_flush_block1(Records p, Record rec, Record last_rec,
432 char **out_buf, int *out_size,
438 for (i = 0; i<REC_NO_INFO; i++)
440 if (*out_offset + CAST_ZINT_TO_INT(rec->size[i]) + 20 > *out_size)
442 int new_size = *out_offset + rec->size[i] + 65536;
443 char *np = (char *) xmalloc(new_size);
445 memcpy(np, *out_buf, *out_offset);
447 *out_size = new_size;
452 rec_encode_zint(rec_sysno_to_int(rec->sysno),
453 (unsigned char *) *out_buf + *out_offset, &len);
454 (*out_offset) += len;
456 if (rec->size[i] == 0)
458 rec_encode_unsigned(1, (unsigned char *) *out_buf + *out_offset,
460 (*out_offset) += len;
462 else if (last_rec && rec->size[i] == last_rec->size[i] &&
463 !memcmp(rec->info[i], last_rec->info[i], rec->size[i]))
465 rec_encode_unsigned(0, (unsigned char *) *out_buf + *out_offset,
467 (*out_offset) += len;
471 rec_encode_unsigned(rec->size[i]+1,
472 (unsigned char *) *out_buf + *out_offset,
474 (*out_offset) += len;
475 memcpy(*out_buf + *out_offset, rec->info[i], rec->size[i]);
476 (*out_offset) += rec->size[i];
481 static ZEBRA_RES rec_write_multiple(Records p, int saveCount)
485 char compression_method;
489 char *out_buf = (char *) xmalloc(out_size);
490 zint *sysnos = (zint *) xmalloc(sizeof(*sysnos) * (p->cache_cur + 1));
491 zint *sysnop = sysnos;
492 ZEBRA_RES ret = ZEBRA_OK;
494 for (i = 0; i<p->cache_cur - saveCount; i++)
496 struct record_cache_entry *e = p->record_cache + i;
500 rec_cache_flush_block1(p, e->rec, last_rec, &out_buf,
501 &out_size, &out_offset);
502 *sysnop++ = rec_sysno_to_int(e->rec->sysno);
504 e->flag = recordFlagNop;
507 case recordFlagWrite:
508 if (rec_release_blocks(p, rec_sysno_to_int(e->rec->sysno))
512 rec_cache_flush_block1(p, e->rec, last_rec, &out_buf,
513 &out_size, &out_offset);
514 *sysnop++ = rec_sysno_to_int(e->rec->sysno);
516 e->flag = recordFlagNop;
519 case recordFlagDelete:
520 if (rec_delete_single(p, e->rec) != ZEBRA_OK)
523 e->flag = recordFlagNop;
533 unsigned int csize = 0; /* indicate compression "not performed yet" */
534 compression_method = p->compression_method;
535 switch (compression_method)
537 case REC_COMPRESS_BZIP2:
539 csize = out_offset + (out_offset >> 6) + 620;
540 rec_tmp_expand(p, csize);
541 #ifdef BZ_CONFIG_ERROR
542 i = BZ2_bzBuffToBuffCompress
544 i = bzBuffToBuffCompress
546 (p->tmp_buf+sizeof(zint)+sizeof(short)+
548 &csize, out_buf, out_offset, 1, 0, 30);
551 yaz_log(YLOG_WARN, "bzBuffToBuffCompress error code=%d", i);
554 yaz_log(YLOG_LOG, "compress %4d %5d %5d", ref_count, out_offset,
558 case REC_COMPRESS_NONE:
563 /* either no compression or compression not supported ... */
565 rec_tmp_expand(p, csize);
566 memcpy(p->tmp_buf + sizeof(zint) + sizeof(short) + sizeof(char),
567 out_buf, out_offset);
569 compression_method = REC_COMPRESS_NONE;
571 memcpy(p->tmp_buf + sizeof(zint), &ref_count, sizeof(ref_count));
572 memcpy(p->tmp_buf + sizeof(zint)+sizeof(short),
573 &compression_method, sizeof(compression_method));
575 /* -------- compression */
576 if (rec_write_tmp_buf(p, csize + sizeof(short) + sizeof(char), sysnos)
585 static ZEBRA_RES rec_cache_flush(Records p, int saveCount)
590 if (saveCount >= p->cache_cur)
593 ret = rec_write_multiple(p, saveCount);
595 for (i = 0; i<p->cache_cur - saveCount; i++)
597 struct record_cache_entry *e = p->record_cache + i;
600 /* i still being used ... */
601 for (j = 0; j<saveCount; j++, i++)
602 memcpy(p->record_cache+j, p->record_cache+i,
603 sizeof(*p->record_cache));
604 p->cache_cur = saveCount;
608 static Record *rec_cache_lookup(Records p, zint sysno,
609 enum recordCacheFlag flag)
612 for (i = 0; i<p->cache_cur; i++)
614 struct record_cache_entry *e = p->record_cache + i;
615 if (e->rec->sysno == sysno)
617 if (flag != recordFlagNop && e->flag == recordFlagNop)
625 static ZEBRA_RES rec_cache_insert(Records p, Record rec, enum recordCacheFlag flag)
627 struct record_cache_entry *e;
628 ZEBRA_RES ret = ZEBRA_OK;
630 if (p->cache_cur == p->cache_max)
631 ret = rec_cache_flush(p, 1);
632 else if (p->cache_cur > 0)
636 for (i = 0; i<p->cache_cur; i++)
638 Record r = (p->record_cache + i)->rec;
639 for (j = 0; j<REC_NO_INFO; j++)
642 if (used > p->compression_chunk_size)
643 ret = rec_cache_flush(p, 1);
645 assert(p->cache_cur < p->cache_max);
647 e = p->record_cache + (p->cache_cur)++;
649 e->rec = rec_cp(rec);
653 ZEBRA_RES rec_close(Records *pp)
657 ZEBRA_RES ret = ZEBRA_OK;
662 zebra_mutex_destroy(&p->mutex);
663 if (rec_cache_flush(p, 0) != ZEBRA_OK)
666 xfree(p->record_cache);
670 if (recindex_write_head(p->recindex, &p->head, sizeof(p->head)) != ZEBRA_OK)
674 recindex_close(p->recindex);
676 for (i = 0; i<REC_BLOCK_TYPES; i++)
678 if (p->data_BFile[i])
679 bf_close(p->data_BFile[i]);
680 xfree(p->data_fname[i]);
688 static Record rec_get_int(Records p, zint sysno)
692 struct record_index_entry entry;
699 unsigned int bz_size;
701 char compression_method;
706 if ((recp = rec_cache_lookup(p, sysno, recordFlagNop)))
707 return rec_cp(*recp);
709 if (recindex_read_indx(p->recindex, rec_sysno_to_int(sysno), &entry, sizeof(entry), 1) < 1)
710 return NULL; /* record is not there! */
713 return NULL; /* record is deleted */
715 dst_type = (int) (entry.next & 7);
716 assert(dst_type < REC_BLOCK_TYPES);
717 freeblock = entry.next / 8;
719 assert(freeblock > 0);
721 rec_tmp_expand(p, entry.size);
724 r = bf_read(p->data_BFile[dst_type], freeblock, 0, 0, cptr);
727 memcpy(&freeblock, cptr, sizeof(freeblock));
733 cptr += p->head.block_size[dst_type] - sizeof(freeblock);
735 memcpy(&tmp, cptr, sizeof(tmp));
736 r = bf_read(p->data_BFile[dst_type], freeblock, 0, 0, cptr);
739 memcpy(&freeblock, cptr, sizeof(freeblock));
740 memcpy(cptr, &tmp, sizeof(tmp));
743 rec = (Record) xmalloc(sizeof(*rec));
745 memcpy(&compression_method, p->tmp_buf + sizeof(zint) + sizeof(short),
746 sizeof(compression_method));
747 in_buf = p->tmp_buf + sizeof(zint) + sizeof(short) + sizeof(char);
748 in_size = entry.size - sizeof(short) - sizeof(char);
749 switch (compression_method)
751 case REC_COMPRESS_BZIP2:
753 bz_size = entry.size * 20 + 100;
756 bz_buf = (char *) xmalloc(bz_size);
757 #ifdef BZ_CONFIG_ERROR
758 i = BZ2_bzBuffToBuffDecompress
760 i = bzBuffToBuffDecompress
762 (bz_buf, &bz_size, in_buf, in_size, 0, 0);
763 yaz_log(YLOG_LOG, "decompress %5d %5d", in_size, bz_size);
766 yaz_log(YLOG_LOG, "failed");
773 yaz_log(YLOG_FATAL, "cannot decompress record(s) in BZIP2 format");
777 case REC_COMPRESS_NONE:
780 for (i = 0; i<REC_NO_INFO; i++)
783 nptr = in_buf; /* skip ref count */
784 while (nptr < in_buf + in_size)
788 rec_decode_zint(&this_sysno, (unsigned char *) nptr, &len);
791 for (i = 0; i < REC_NO_INFO; i++)
793 unsigned int this_size;
794 rec_decode_unsigned(&this_size, (unsigned char *) nptr, &len);
799 rec->size[i] = this_size-1;
804 nptr += rec->size[i];
809 if (this_sysno == rec_sysno_to_int(sysno))
812 for (i = 0; i<REC_NO_INFO; i++)
814 if (rec->info[i] && rec->size[i])
816 char *np = xmalloc(rec->size[i]+1);
817 memcpy(np, rec->info[i], rec->size[i]);
818 np[rec->size[i]] = '\0';
823 assert(rec->info[i] == 0);
824 assert(rec->size[i] == 0);
828 if (rec_cache_insert(p, rec, recordFlagNop) != ZEBRA_OK)
833 Record rec_get(Records p, zint sysno)
836 zebra_mutex_lock(&p->mutex);
838 rec = rec_get_int(p, sysno);
839 zebra_mutex_unlock(&p->mutex);
843 Record rec_get_root(Records p)
845 return rec_get(p, rec_sysno_to_ext(1));
848 static Record rec_new_int(Records p)
855 rec = (Record) xmalloc(sizeof(*rec));
856 if (1 || p->head.index_free == 0)
857 sysno = (p->head.index_last)++;
860 struct record_index_entry entry;
862 if (recindex_read_indx(p->recindex, p->head.index_free, &entry, sizeof(entry), 0) < 1)
867 sysno = p->head.index_free;
868 p->head.index_free = entry.next;
870 (p->head.no_records)++;
871 rec->sysno = rec_sysno_to_ext(sysno);
872 for (i = 0; i < REC_NO_INFO; i++)
877 rec_cache_insert(p, rec, recordFlagNew);
881 Record rec_new(Records p)
884 zebra_mutex_lock(&p->mutex);
886 rec = rec_new_int(p);
887 zebra_mutex_unlock(&p->mutex);
891 ZEBRA_RES rec_del(Records p, Record *recpp)
894 ZEBRA_RES ret = ZEBRA_OK;
896 zebra_mutex_lock(&p->mutex);
897 (p->head.no_records)--;
898 if ((recp = rec_cache_lookup(p, (*recpp)->sysno, recordFlagDelete)))
905 ret = rec_cache_insert(p, *recpp, recordFlagDelete);
908 zebra_mutex_unlock(&p->mutex);
913 ZEBRA_RES rec_put(Records p, Record *recpp)
916 ZEBRA_RES ret = ZEBRA_OK;
918 zebra_mutex_lock(&p->mutex);
919 if ((recp = rec_cache_lookup(p, (*recpp)->sysno, recordFlagWrite)))
926 ret = rec_cache_insert(p, *recpp, recordFlagWrite);
929 zebra_mutex_unlock(&p->mutex);
934 void rec_free(Record *recpp)
940 for (i = 0; i < REC_NO_INFO; i++)
941 xfree((*recpp)->info[i]);
946 Record rec_cp(Record rec)
951 n = (Record) xmalloc(sizeof(*n));
952 n->sysno = rec->sysno;
953 for (i = 0; i < REC_NO_INFO; i++)
961 n->size[i] = rec->size[i];
962 n->info[i] = (char *) xmalloc(rec->size[i]+1);
963 memcpy(n->info[i], rec->info[i], rec->size[i]);
964 n->info[i][rec->size[i]] = '\0';
970 char *rec_strdup(const char *s, size_t *len)
980 p = (char *) xmalloc(*len);
985 void rec_prstat(Records records)
988 zint total_bytes = 0;
991 "Total records %8" ZINT_FORMAT0,
992 records->head.no_records);
994 for (i = 0; i< REC_BLOCK_TYPES; i++)
996 yaz_log (YLOG_LOG, "Record blocks of size "ZINT_FORMAT,
997 records->head.block_size[i]);
999 " Used/Total/Bytes used "
1000 ZINT_FORMAT "/" ZINT_FORMAT "/" ZINT_FORMAT,
1001 records->head.block_used[i], records->head.block_last[i]-1,
1002 records->head.block_used[i] * records->head.block_size[i]);
1004 records->head.block_used[i] * records->head.block_size[i];
1007 "Total size of record index in bytes %8" ZINT_FORMAT0,
1008 records->head.total_bytes);
1010 "Total size with overhead %8" ZINT_FORMAT0,
1017 * indent-tabs-mode: nil
1019 * vim: shiftwidth=4 tabstop=8 expandtab