1 /* $Id: records.c,v 1.4 2008-03-05 09:18:51 adam Exp $
2 Copyright (C) 1995-2008
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;
72 int compression_chunk_size;
79 zint block_size[REC_BLOCK_TYPES];
80 zint block_free[REC_BLOCK_TYPES];
81 zint block_last[REC_BLOCK_TYPES];
82 zint block_used[REC_BLOCK_TYPES];
83 zint block_move[REC_BLOCK_TYPES];
93 enum recordCacheFlag { recordFlagNop, recordFlagWrite, recordFlagNew,
96 struct record_cache_entry {
98 enum recordCacheFlag flag;
101 struct record_index_entry {
102 zint next; /* first block of record info / next free entry */
103 int size; /* size of record or 0 if free entry */
106 Record rec_cp(Record rec);
108 /* Modify argument to if below: 1=normal, 0=sysno testing */
110 /* If this is used sysno are not converted (no testing) */
111 #define FAKE_OFFSET 0
112 #define USUAL_RANGE 6000000000LL
115 /* Use a fake > 2^32 offset so we can test for proper 64-bit handling */
116 #define FAKE_OFFSET 6000000000LL
117 #define USUAL_RANGE 2000000000LL
120 static zint rec_sysno_to_ext(zint sysno)
122 assert(sysno >= 0 && sysno <= USUAL_RANGE);
123 return sysno + FAKE_OFFSET;
126 zint rec_sysno_to_int(zint sysno)
128 assert(sysno >= FAKE_OFFSET && sysno <= FAKE_OFFSET + USUAL_RANGE);
129 return sysno - FAKE_OFFSET;
132 static void rec_tmp_expand(Records p, int size)
134 if (p->tmp_size < size + 2048 ||
135 p->tmp_size < p->head.block_size[REC_BLOCK_TYPES-1]*2)
138 p->tmp_size = size + (int)
139 (p->head.block_size[REC_BLOCK_TYPES-1])*2 + 2048;
140 p->tmp_buf = (char *) xmalloc(p->tmp_size);
144 static ZEBRA_RES rec_release_blocks(Records p, zint sysno)
146 struct record_index_entry entry;
148 char block_and_ref[sizeof(zint) + sizeof(short)];
152 if (recindex_read_indx(p->recindex, sysno, &entry, sizeof(entry), 1) != 1)
155 freeblock = entry.next;
156 assert(freeblock > 0);
157 dst_type = CAST_ZINT_TO_INT(freeblock & 7);
158 assert(dst_type < REC_BLOCK_TYPES);
159 freeblock = freeblock / 8;
162 if (bf_read(p->data_BFile[dst_type], freeblock, 0,
163 first ? sizeof(block_and_ref) : sizeof(zint),
166 yaz_log(YLOG_FATAL|YLOG_ERRNO, "read in rec_del_single");
172 memcpy(&ref, block_and_ref + sizeof(freeblock), sizeof(ref));
174 memcpy(block_and_ref + sizeof(freeblock), &ref, sizeof(ref));
177 if (bf_write(p->data_BFile[dst_type], freeblock, 0,
178 sizeof(block_and_ref), block_and_ref))
180 yaz_log(YLOG_FATAL|YLOG_ERRNO, "write in rec_del_single");
188 if (bf_write(p->data_BFile[dst_type], freeblock, 0, sizeof(freeblock),
189 &p->head.block_free[dst_type]))
191 yaz_log(YLOG_FATAL|YLOG_ERRNO, "write in rec_del_single");
194 p->head.block_free[dst_type] = freeblock;
195 memcpy(&freeblock, block_and_ref, sizeof(freeblock));
197 p->head.block_used[dst_type]--;
199 p->head.total_bytes -= entry.size;
203 static ZEBRA_RES rec_delete_single(Records p, Record rec)
205 struct record_index_entry entry;
207 /* all data in entry must be reset, since it's written verbatim */
208 memset(&entry, '\0', sizeof(entry));
209 if (rec_release_blocks(p, rec_sysno_to_int(rec->sysno)) != ZEBRA_OK)
212 entry.next = p->head.index_free;
214 p->head.index_free = rec_sysno_to_int(rec->sysno);
215 recindex_write_indx(p->recindex, rec_sysno_to_int(rec->sysno), &entry, sizeof(entry));
219 static ZEBRA_RES rec_write_tmp_buf(Records p, int size, zint *sysnos)
221 struct record_index_entry entry;
223 char *cptr = p->tmp_buf;
224 zint block_prev = -1, block_free;
228 /* all data in entry must be reset, since it's written verbatim */
229 memset(&entry, '\0', sizeof(entry));
231 for (i = 1; i<REC_BLOCK_TYPES; i++)
232 if (size >= p->head.block_move[i])
234 while (no_written < size)
236 block_free = p->head.block_free[dst_type];
239 if (bf_read(p->data_BFile[dst_type],
240 block_free, 0, sizeof(*p->head.block_free),
241 &p->head.block_free[dst_type]) != 1)
243 yaz_log(YLOG_FATAL|YLOG_ERRNO, "read in %s at free block "
245 p->data_fname[dst_type], block_free);
250 block_free = p->head.block_last[dst_type]++;
251 if (block_prev == -1)
253 entry.next = block_free*8 + dst_type;
255 p->head.total_bytes += size;
258 recindex_write_indx(p->recindex, *sysnos, &entry, sizeof(entry));
264 memcpy(cptr, &block_free, sizeof(block_free));
265 bf_write(p->data_BFile[dst_type], block_prev, 0, 0, cptr);
266 cptr = p->tmp_buf + no_written;
268 block_prev = block_free;
269 no_written += CAST_ZINT_TO_INT(p->head.block_size[dst_type])
271 p->head.block_used[dst_type]++;
273 assert(block_prev != -1);
275 memcpy(cptr, &block_free, sizeof(block_free));
276 bf_write(p->data_BFile[dst_type], block_prev, 0,
277 sizeof(block_free) + (p->tmp_buf+size) - cptr, cptr);
281 Records rec_open(BFiles bfs, int rw, int compression_method)
286 ZEBRA_RES ret = ZEBRA_OK;
288 p = (Records) xmalloc(sizeof(*p));
289 memset(&p->head, '\0', sizeof(p->head));
290 p->compression_method = compression_method;
293 p->tmp_buf = (char *) xmalloc(p->tmp_size);
294 p->compression_chunk_size = 0;
295 p->recindex = recindex_open(bfs, rw, 0 /* 1=isamb for recindex */);
296 r = recindex_read_head(p->recindex, p->tmp_buf);
300 memcpy(p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic));
301 sprintf(p->head.version, "%3d", REC_VERSION);
302 p->head.index_free = 0;
303 p->head.index_last = 1;
304 p->head.no_records = 0;
305 p->head.total_bytes = 0;
306 for (i = 0; i<REC_BLOCK_TYPES; i++)
308 p->head.block_free[i] = 0;
309 p->head.block_last[i] = 1;
310 p->head.block_used[i] = 0;
312 p->head.block_size[0] = 128;
313 p->head.block_move[0] = 0;
314 for (i = 1; i<REC_BLOCK_TYPES; i++)
316 p->head.block_size[i] = p->head.block_size[i-1] * 4;
317 p->head.block_move[i] = p->head.block_size[i] * 24;
321 if (recindex_write_head(p->recindex,
322 &p->head, sizeof(p->head)) != ZEBRA_OK)
327 memcpy(&p->head, p->tmp_buf, sizeof(p->head));
328 if (memcmp(p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic)))
330 yaz_log(YLOG_FATAL, "file %s has bad format",
331 recindex_get_fname(p->recindex));
334 version = atoi(p->head.version);
335 if (version != REC_VERSION)
337 yaz_log(YLOG_FATAL, "file %s is version %d, but version"
339 recindex_get_fname(p->recindex), version, REC_VERSION);
342 p->compression_chunk_size = 90000; /* good for BZIP2 */
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++)
645 if (used > p->compression_chunk_size)
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 (recindex_write_head(p->recindex, &p->head, sizeof(p->head)) != ZEBRA_OK)
677 recindex_close(p->recindex);
679 for (i = 0; i<REC_BLOCK_TYPES; i++)
681 if (p->data_BFile[i])
682 bf_close(p->data_BFile[i]);
683 xfree(p->data_fname[i]);
691 static Record rec_get_int(Records p, zint sysno)
695 struct record_index_entry entry;
702 unsigned int bz_size;
704 char compression_method;
709 if ((recp = rec_cache_lookup(p, sysno, recordFlagNop)))
710 return rec_cp(*recp);
712 if (recindex_read_indx(p->recindex, rec_sysno_to_int(sysno), &entry, sizeof(entry), 1) < 1)
713 return NULL; /* record is not there! */
716 return NULL; /* record is deleted */
718 dst_type = (int) (entry.next & 7);
719 assert(dst_type < REC_BLOCK_TYPES);
720 freeblock = entry.next / 8;
722 assert(freeblock > 0);
724 rec_tmp_expand(p, entry.size);
727 r = bf_read(p->data_BFile[dst_type], freeblock, 0, 0, cptr);
730 memcpy(&freeblock, cptr, sizeof(freeblock));
736 cptr += p->head.block_size[dst_type] - sizeof(freeblock);
738 memcpy(&tmp, cptr, sizeof(tmp));
739 r = bf_read(p->data_BFile[dst_type], freeblock, 0, 0, cptr);
742 memcpy(&freeblock, cptr, sizeof(freeblock));
743 memcpy(cptr, &tmp, sizeof(tmp));
746 rec = (Record) xmalloc(sizeof(*rec));
748 memcpy(&compression_method, p->tmp_buf + sizeof(zint) + sizeof(short),
749 sizeof(compression_method));
750 in_buf = p->tmp_buf + sizeof(zint) + sizeof(short) + sizeof(char);
751 in_size = entry.size - sizeof(short) - sizeof(char);
752 switch (compression_method)
754 case REC_COMPRESS_BZIP2:
756 bz_size = entry.size * 20 + 100;
759 bz_buf = (char *) xmalloc(bz_size);
760 #ifdef BZ_CONFIG_ERROR
761 i = BZ2_bzBuffToBuffDecompress
763 i = bzBuffToBuffDecompress
765 (bz_buf, &bz_size, in_buf, in_size, 0, 0);
766 yaz_log(YLOG_LOG, "decompress %5d %5d", in_size, bz_size);
769 yaz_log(YLOG_LOG, "failed");
776 yaz_log(YLOG_FATAL, "cannot decompress record(s) in BZIP2 format");
780 case REC_COMPRESS_NONE:
783 for (i = 0; i<REC_NO_INFO; i++)
786 nptr = in_buf; /* skip ref count */
787 while (nptr < in_buf + in_size)
791 rec_decode_zint(&this_sysno, (unsigned char *) nptr, &len);
794 for (i = 0; i < REC_NO_INFO; i++)
796 unsigned int this_size;
797 rec_decode_unsigned(&this_size, (unsigned char *) nptr, &len);
802 rec->size[i] = this_size-1;
807 nptr += rec->size[i];
812 if (this_sysno == rec_sysno_to_int(sysno))
815 for (i = 0; i<REC_NO_INFO; i++)
817 if (rec->info[i] && rec->size[i])
819 char *np = xmalloc(rec->size[i]+1);
820 memcpy(np, rec->info[i], rec->size[i]);
821 np[rec->size[i]] = '\0';
826 assert(rec->info[i] == 0);
827 assert(rec->size[i] == 0);
831 if (rec_cache_insert(p, rec, recordFlagNop) != ZEBRA_OK)
836 Record rec_get(Records p, zint sysno)
839 zebra_mutex_lock(&p->mutex);
841 rec = rec_get_int(p, sysno);
842 zebra_mutex_unlock(&p->mutex);
846 Record rec_get_root(Records p)
848 return rec_get(p, rec_sysno_to_ext(1));
851 static Record rec_new_int(Records p)
858 rec = (Record) xmalloc(sizeof(*rec));
859 if (1 || p->head.index_free == 0)
860 sysno = (p->head.index_last)++;
863 struct record_index_entry entry;
865 if (recindex_read_indx(p->recindex, p->head.index_free, &entry, sizeof(entry), 0) < 1)
870 sysno = p->head.index_free;
871 p->head.index_free = entry.next;
873 (p->head.no_records)++;
874 rec->sysno = rec_sysno_to_ext(sysno);
875 for (i = 0; i < REC_NO_INFO; i++)
880 rec_cache_insert(p, rec, recordFlagNew);
884 Record rec_new(Records p)
887 zebra_mutex_lock(&p->mutex);
889 rec = rec_new_int(p);
890 zebra_mutex_unlock(&p->mutex);
894 ZEBRA_RES rec_del(Records p, Record *recpp)
897 ZEBRA_RES ret = ZEBRA_OK;
899 zebra_mutex_lock(&p->mutex);
900 (p->head.no_records)--;
901 if ((recp = rec_cache_lookup(p, (*recpp)->sysno, recordFlagDelete)))
908 ret = rec_cache_insert(p, *recpp, recordFlagDelete);
911 zebra_mutex_unlock(&p->mutex);
916 ZEBRA_RES rec_put(Records p, Record *recpp)
919 ZEBRA_RES ret = ZEBRA_OK;
921 zebra_mutex_lock(&p->mutex);
922 if ((recp = rec_cache_lookup(p, (*recpp)->sysno, recordFlagWrite)))
929 ret = rec_cache_insert(p, *recpp, recordFlagWrite);
932 zebra_mutex_unlock(&p->mutex);
937 void rec_free(Record *recpp)
943 for (i = 0; i < REC_NO_INFO; i++)
944 xfree((*recpp)->info[i]);
949 Record rec_cp(Record rec)
954 n = (Record) xmalloc(sizeof(*n));
955 n->sysno = rec->sysno;
956 for (i = 0; i < REC_NO_INFO; i++)
964 n->size[i] = rec->size[i];
965 n->info[i] = (char *) xmalloc(rec->size[i]+1);
966 memcpy(n->info[i], rec->info[i], rec->size[i]);
967 n->info[i][rec->size[i]] = '\0';
973 char *rec_strdup(const char *s, size_t *len)
983 p = (char *) xmalloc(*len);
988 void rec_prstat(Records records)
991 zint total_bytes = 0;
994 "Total records %8" ZINT_FORMAT0,
995 records->head.no_records);
997 for (i = 0; i< REC_BLOCK_TYPES; i++)
999 yaz_log (YLOG_LOG, "Record blocks of size "ZINT_FORMAT,
1000 records->head.block_size[i]);
1002 " Used/Total/Bytes used "
1003 ZINT_FORMAT "/" ZINT_FORMAT "/" ZINT_FORMAT,
1004 records->head.block_used[i], records->head.block_last[i]-1,
1005 records->head.block_used[i] * records->head.block_size[i]);
1007 records->head.block_used[i] * records->head.block_size[i];
1010 "Total size of record index in bytes %8" ZINT_FORMAT0,
1011 records->head.total_bytes);
1013 "Total size with overhead %8" ZINT_FORMAT0,
1020 * indent-tabs-mode: nil
1022 * vim: shiftwidth=4 tabstop=8 expandtab