1 /* $Id: records.c,v 1.3 2007-11-28 11:16:32 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
24 * Format of first block
29 * Format of subsequent blocks
33 * Format of each record
35 * (length, data) - pairs
36 * length = 0 if same as previous
43 #include <yaz/yaz-util.h>
44 #include <idzebra/bfile.h>
51 #define REC_BLOCK_TYPES 2
52 #define REC_HEAD_MAGIC "recindex"
57 int compression_method;
61 char *data_fname[REC_BLOCK_TYPES];
62 BFile data_BFile[REC_BLOCK_TYPES];
67 struct record_cache_entry *record_cache;
77 zint block_size[REC_BLOCK_TYPES];
78 zint block_free[REC_BLOCK_TYPES];
79 zint block_last[REC_BLOCK_TYPES];
80 zint block_used[REC_BLOCK_TYPES];
81 zint block_move[REC_BLOCK_TYPES];
91 enum recordCacheFlag { recordFlagNop, recordFlagWrite, recordFlagNew,
94 struct record_cache_entry {
96 enum recordCacheFlag flag;
99 struct record_index_entry {
100 zint next; /* first block of record info / next free entry */
101 int size; /* size of record or 0 if free entry */
104 Record rec_cp(Record rec);
106 /* Modify argument to if below: 1=normal, 0=sysno testing */
108 /* If this is used sysno are not converted (no testing) */
109 #define FAKE_OFFSET 0
110 #define USUAL_RANGE 6000000000LL
113 /* Use a fake > 2^32 offset so we can test for proper 64-bit handling */
114 #define FAKE_OFFSET 6000000000LL
115 #define USUAL_RANGE 2000000000LL
118 static zint rec_sysno_to_ext(zint sysno)
120 assert(sysno >= 0 && sysno <= USUAL_RANGE);
121 return sysno + FAKE_OFFSET;
124 zint rec_sysno_to_int(zint sysno)
126 assert(sysno >= FAKE_OFFSET && sysno <= FAKE_OFFSET + USUAL_RANGE);
127 return sysno - FAKE_OFFSET;
130 static void rec_tmp_expand(Records p, int size)
132 if (p->tmp_size < size + 2048 ||
133 p->tmp_size < p->head.block_size[REC_BLOCK_TYPES-1]*2)
136 p->tmp_size = size + (int)
137 (p->head.block_size[REC_BLOCK_TYPES-1])*2 + 2048;
138 p->tmp_buf = (char *) xmalloc(p->tmp_size);
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 (recindex_read_indx(p->recindex, 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 recindex_write_indx(p->recindex, 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 recindex_write_indx(p->recindex, *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->tmp_buf = (char *) xmalloc(p->tmp_size);
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);
341 for (i = 0; i<REC_BLOCK_TYPES; i++)
344 sprintf(str, "recd%c", i + 'A');
345 p->data_fname[i] = (char *) xmalloc(strlen(str)+1);
346 strcpy(p->data_fname[i], str);
347 p->data_BFile[i] = NULL;
349 for (i = 0; i<REC_BLOCK_TYPES; i++)
351 if (!(p->data_BFile[i] =
352 bf_open(bfs, p->data_fname[i],
353 CAST_ZINT_TO_INT(p->head.block_size[i]), rw)))
355 yaz_log(YLOG_FATAL|YLOG_ERRNO, "bf_open %s", p->data_fname[i]);
362 p->record_cache = (struct record_cache_entry *)
363 xmalloc(sizeof(*p->record_cache)*p->cache_max);
364 zebra_mutex_init(&p->mutex);
365 if (ret == ZEBRA_FAIL)
370 static void rec_encode_unsigned(unsigned n, unsigned char *buf, int *len)
375 buf[*len] = 128 + (n & 127);
383 static void rec_decode_unsigned(unsigned *np, unsigned char *buf, int *len)
389 while (buf[*len] > 127)
391 n += w*(buf[*len] & 127);
400 static void rec_encode_zint(zint n, unsigned char *buf, int *len)
405 buf[*len] = (unsigned) (128 + (n & 127));
409 buf[*len] = (unsigned) n;
413 static void rec_decode_zint(zint *np, unsigned char *buf, int *len)
419 while (buf[*len] > 127)
421 n += w*(buf[*len] & 127);
430 static void rec_cache_flush_block1(Records p, Record rec, Record last_rec,
431 char **out_buf, int *out_size,
437 for (i = 0; i<REC_NO_INFO; i++)
439 if (*out_offset + CAST_ZINT_TO_INT(rec->size[i]) + 20 > *out_size)
441 int new_size = *out_offset + rec->size[i] + 65536;
442 char *np = (char *) xmalloc(new_size);
444 memcpy(np, *out_buf, *out_offset);
446 *out_size = new_size;
451 rec_encode_zint(rec_sysno_to_int(rec->sysno),
452 (unsigned char *) *out_buf + *out_offset, &len);
453 (*out_offset) += len;
455 if (rec->size[i] == 0)
457 rec_encode_unsigned(1, (unsigned char *) *out_buf + *out_offset,
459 (*out_offset) += len;
461 else if (last_rec && rec->size[i] == last_rec->size[i] &&
462 !memcmp(rec->info[i], last_rec->info[i], rec->size[i]))
464 rec_encode_unsigned(0, (unsigned char *) *out_buf + *out_offset,
466 (*out_offset) += len;
470 rec_encode_unsigned(rec->size[i]+1,
471 (unsigned char *) *out_buf + *out_offset,
473 (*out_offset) += len;
474 memcpy(*out_buf + *out_offset, rec->info[i], rec->size[i]);
475 (*out_offset) += rec->size[i];
480 static ZEBRA_RES rec_write_multiple(Records p, int saveCount)
484 char compression_method;
488 char *out_buf = (char *) xmalloc(out_size);
489 zint *sysnos = (zint *) xmalloc(sizeof(*sysnos) * (p->cache_cur + 1));
490 zint *sysnop = sysnos;
491 ZEBRA_RES ret = ZEBRA_OK;
493 for (i = 0; i<p->cache_cur - saveCount; i++)
495 struct record_cache_entry *e = p->record_cache + i;
499 rec_cache_flush_block1(p, e->rec, last_rec, &out_buf,
500 &out_size, &out_offset);
501 *sysnop++ = rec_sysno_to_int(e->rec->sysno);
503 e->flag = recordFlagNop;
506 case recordFlagWrite:
507 if (rec_release_blocks(p, rec_sysno_to_int(e->rec->sysno))
511 rec_cache_flush_block1(p, e->rec, last_rec, &out_buf,
512 &out_size, &out_offset);
513 *sysnop++ = rec_sysno_to_int(e->rec->sysno);
515 e->flag = recordFlagNop;
518 case recordFlagDelete:
519 if (rec_delete_single(p, e->rec) != ZEBRA_OK)
522 e->flag = recordFlagNop;
532 unsigned int csize = 0; /* indicate compression "not performed yet" */
533 compression_method = p->compression_method;
534 switch (compression_method)
536 case REC_COMPRESS_BZIP2:
538 csize = out_offset + (out_offset >> 6) + 620;
539 rec_tmp_expand(p, csize);
540 #ifdef BZ_CONFIG_ERROR
541 i = BZ2_bzBuffToBuffCompress
543 i = bzBuffToBuffCompress
545 (p->tmp_buf+sizeof(zint)+sizeof(short)+
547 &csize, out_buf, out_offset, 1, 0, 30);
550 yaz_log(YLOG_WARN, "bzBuffToBuffCompress error code=%d", i);
553 yaz_log(YLOG_LOG, "compress %4d %5d %5d", ref_count, out_offset,
557 case REC_COMPRESS_NONE:
562 /* either no compression or compression not supported ... */
564 rec_tmp_expand(p, csize);
565 memcpy(p->tmp_buf + sizeof(zint) + sizeof(short) + sizeof(char),
566 out_buf, out_offset);
568 compression_method = REC_COMPRESS_NONE;
570 memcpy(p->tmp_buf + sizeof(zint), &ref_count, sizeof(ref_count));
571 memcpy(p->tmp_buf + sizeof(zint)+sizeof(short),
572 &compression_method, sizeof(compression_method));
574 /* -------- compression */
575 if (rec_write_tmp_buf(p, csize + sizeof(short) + sizeof(char), sysnos)
584 static ZEBRA_RES rec_cache_flush(Records p, int saveCount)
589 if (saveCount >= p->cache_cur)
592 ret = rec_write_multiple(p, saveCount);
594 for (i = 0; i<p->cache_cur - saveCount; i++)
596 struct record_cache_entry *e = p->record_cache + i;
599 /* i still being used ... */
600 for (j = 0; j<saveCount; j++, i++)
601 memcpy(p->record_cache+j, p->record_cache+i,
602 sizeof(*p->record_cache));
603 p->cache_cur = saveCount;
607 static Record *rec_cache_lookup(Records p, zint sysno,
608 enum recordCacheFlag flag)
611 for (i = 0; i<p->cache_cur; i++)
613 struct record_cache_entry *e = p->record_cache + i;
614 if (e->rec->sysno == sysno)
616 if (flag != recordFlagNop && e->flag == recordFlagNop)
624 static ZEBRA_RES rec_cache_insert(Records p, Record rec, enum recordCacheFlag flag)
626 struct record_cache_entry *e;
627 ZEBRA_RES ret = ZEBRA_OK;
629 if (p->cache_cur == p->cache_max)
630 ret = rec_cache_flush(p, 1);
631 else if (p->cache_cur > 0)
635 for (i = 0; i<p->cache_cur; i++)
637 Record r = (p->record_cache + i)->rec;
638 for (j = 0; j<REC_NO_INFO; j++)
642 ret = rec_cache_flush(p, 1);
644 assert(p->cache_cur < p->cache_max);
646 e = p->record_cache + (p->cache_cur)++;
648 e->rec = rec_cp(rec);
652 ZEBRA_RES rec_close(Records *pp)
656 ZEBRA_RES ret = ZEBRA_OK;
661 zebra_mutex_destroy(&p->mutex);
662 if (rec_cache_flush(p, 0) != ZEBRA_OK)
665 xfree(p->record_cache);
669 if (recindex_write_head(p->recindex, &p->head, sizeof(p->head)) != ZEBRA_OK)
673 recindex_close(p->recindex);
675 for (i = 0; i<REC_BLOCK_TYPES; i++)
677 if (p->data_BFile[i])
678 bf_close(p->data_BFile[i]);
679 xfree(p->data_fname[i]);
687 static Record rec_get_int(Records p, zint sysno)
691 struct record_index_entry entry;
698 unsigned int bz_size;
700 char compression_method;
705 if ((recp = rec_cache_lookup(p, sysno, recordFlagNop)))
706 return rec_cp(*recp);
708 if (recindex_read_indx(p->recindex, rec_sysno_to_int(sysno), &entry, sizeof(entry), 1) < 1)
709 return NULL; /* record is not there! */
712 return NULL; /* record is deleted */
714 dst_type = (int) (entry.next & 7);
715 assert(dst_type < REC_BLOCK_TYPES);
716 freeblock = entry.next / 8;
718 assert(freeblock > 0);
720 rec_tmp_expand(p, entry.size);
723 r = bf_read(p->data_BFile[dst_type], freeblock, 0, 0, cptr);
726 memcpy(&freeblock, cptr, sizeof(freeblock));
732 cptr += p->head.block_size[dst_type] - sizeof(freeblock);
734 memcpy(&tmp, cptr, sizeof(tmp));
735 r = bf_read(p->data_BFile[dst_type], freeblock, 0, 0, cptr);
738 memcpy(&freeblock, cptr, sizeof(freeblock));
739 memcpy(cptr, &tmp, sizeof(tmp));
742 rec = (Record) xmalloc(sizeof(*rec));
744 memcpy(&compression_method, p->tmp_buf + sizeof(zint) + sizeof(short),
745 sizeof(compression_method));
746 in_buf = p->tmp_buf + sizeof(zint) + sizeof(short) + sizeof(char);
747 in_size = entry.size - sizeof(short) - sizeof(char);
748 switch (compression_method)
750 case REC_COMPRESS_BZIP2:
752 bz_size = entry.size * 20 + 100;
755 bz_buf = (char *) xmalloc(bz_size);
756 #ifdef BZ_CONFIG_ERROR
757 i = BZ2_bzBuffToBuffDecompress
759 i = bzBuffToBuffDecompress
761 (bz_buf, &bz_size, in_buf, in_size, 0, 0);
762 yaz_log(YLOG_LOG, "decompress %5d %5d", in_size, bz_size);
765 yaz_log(YLOG_LOG, "failed");
772 yaz_log(YLOG_FATAL, "cannot decompress record(s) in BZIP2 format");
776 case REC_COMPRESS_NONE:
779 for (i = 0; i<REC_NO_INFO; i++)
782 nptr = in_buf; /* skip ref count */
783 while (nptr < in_buf + in_size)
787 rec_decode_zint(&this_sysno, (unsigned char *) nptr, &len);
790 for (i = 0; i < REC_NO_INFO; i++)
792 unsigned int this_size;
793 rec_decode_unsigned(&this_size, (unsigned char *) nptr, &len);
798 rec->size[i] = this_size-1;
803 nptr += rec->size[i];
808 if (this_sysno == rec_sysno_to_int(sysno))
811 for (i = 0; i<REC_NO_INFO; i++)
813 if (rec->info[i] && rec->size[i])
815 char *np = xmalloc(rec->size[i]+1);
816 memcpy(np, rec->info[i], rec->size[i]);
817 np[rec->size[i]] = '\0';
822 assert(rec->info[i] == 0);
823 assert(rec->size[i] == 0);
827 if (rec_cache_insert(p, rec, recordFlagNop) != ZEBRA_OK)
832 Record rec_get(Records p, zint sysno)
835 zebra_mutex_lock(&p->mutex);
837 rec = rec_get_int(p, sysno);
838 zebra_mutex_unlock(&p->mutex);
842 Record rec_get_root(Records p)
844 return rec_get(p, rec_sysno_to_ext(1));
847 static Record rec_new_int(Records p)
854 rec = (Record) xmalloc(sizeof(*rec));
855 if (1 || p->head.index_free == 0)
856 sysno = (p->head.index_last)++;
859 struct record_index_entry entry;
861 if (recindex_read_indx(p->recindex, p->head.index_free, &entry, sizeof(entry), 0) < 1)
866 sysno = p->head.index_free;
867 p->head.index_free = entry.next;
869 (p->head.no_records)++;
870 rec->sysno = rec_sysno_to_ext(sysno);
871 for (i = 0; i < REC_NO_INFO; i++)
876 rec_cache_insert(p, rec, recordFlagNew);
880 Record rec_new(Records p)
883 zebra_mutex_lock(&p->mutex);
885 rec = rec_new_int(p);
886 zebra_mutex_unlock(&p->mutex);
890 ZEBRA_RES rec_del(Records p, Record *recpp)
893 ZEBRA_RES ret = ZEBRA_OK;
895 zebra_mutex_lock(&p->mutex);
896 (p->head.no_records)--;
897 if ((recp = rec_cache_lookup(p, (*recpp)->sysno, recordFlagDelete)))
904 ret = rec_cache_insert(p, *recpp, recordFlagDelete);
907 zebra_mutex_unlock(&p->mutex);
912 ZEBRA_RES rec_put(Records p, Record *recpp)
915 ZEBRA_RES ret = ZEBRA_OK;
917 zebra_mutex_lock(&p->mutex);
918 if ((recp = rec_cache_lookup(p, (*recpp)->sysno, recordFlagWrite)))
925 ret = rec_cache_insert(p, *recpp, recordFlagWrite);
928 zebra_mutex_unlock(&p->mutex);
933 void rec_free(Record *recpp)
939 for (i = 0; i < REC_NO_INFO; i++)
940 xfree((*recpp)->info[i]);
945 Record rec_cp(Record rec)
950 n = (Record) xmalloc(sizeof(*n));
951 n->sysno = rec->sysno;
952 for (i = 0; i < REC_NO_INFO; i++)
960 n->size[i] = rec->size[i];
961 n->info[i] = (char *) xmalloc(rec->size[i]+1);
962 memcpy(n->info[i], rec->info[i], rec->size[i]);
963 n->info[i][rec->size[i]] = '\0';
969 char *rec_strdup(const char *s, size_t *len)
979 p = (char *) xmalloc(*len);
984 void rec_prstat(Records records)
987 zint total_bytes = 0;
990 "Total records %8" ZINT_FORMAT0,
991 records->head.no_records);
993 for (i = 0; i< REC_BLOCK_TYPES; i++)
995 yaz_log (YLOG_LOG, "Record blocks of size "ZINT_FORMAT,
996 records->head.block_size[i]);
998 " Used/Total/Bytes used "
999 ZINT_FORMAT "/" ZINT_FORMAT "/" ZINT_FORMAT,
1000 records->head.block_used[i], records->head.block_last[i]-1,
1001 records->head.block_used[i] * records->head.block_size[i]);
1003 records->head.block_used[i] * records->head.block_size[i];
1006 "Total size of record index in bytes %8" ZINT_FORMAT0,
1007 records->head.total_bytes);
1009 "Total size with overhead %8" ZINT_FORMAT0,
1016 * indent-tabs-mode: nil
1018 * vim: shiftwidth=4 tabstop=8 expandtab