1 /* $Id: recindex.c,v 1.57 2007-11-23 13:11:08 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
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 zint rec_sysno_to_ext(zint sysno)
66 assert(sysno >= 0 && sysno <= USUAL_RANGE);
67 return sysno + FAKE_OFFSET;
70 zint rec_sysno_to_int(zint sysno)
72 assert(sysno >= FAKE_OFFSET && sysno <= FAKE_OFFSET + USUAL_RANGE);
73 return sysno - FAKE_OFFSET;
76 static int rec_read_head(recindex_t p, void *buf)
78 return bf_read(p->index_BFile, 0, 0, 0, buf);
81 static const char *recindex_get_fname(recindex_t p)
83 return p->index_fname;
86 static ZEBRA_RES rec_write_head(recindex_t p, const void *buf, size_t len)
91 assert(p->index_BFile);
93 r = bf_write(p->index_BFile, 0, 0, len, buf);
96 yaz_log(YLOG_FATAL|YLOG_ERRNO, "write head of %s", p->index_fname);
102 static void rec_tmp_expand(Records p, int size)
104 if (p->tmp_size < size + 2048 ||
105 p->tmp_size < p->head.block_size[REC_BLOCK_TYPES-1]*2)
108 p->tmp_size = size + (int)
109 (p->head.block_size[REC_BLOCK_TYPES-1])*2 + 2048;
110 p->tmp_buf = (char *) xmalloc(p->tmp_size);
114 static int read_indx(recindex_t p, zint sysno, void *buf, int itemsize,
118 zint pos = (sysno-1)*itemsize;
119 int off = CAST_ZINT_TO_INT(pos%RIDX_CHUNK);
120 int sz1 = RIDX_CHUNK - off; /* sz1 is size of buffer to read.. */
123 sz1 = itemsize; /* no more than itemsize bytes */
125 r = bf_read(p->index_BFile, 1+pos/RIDX_CHUNK, off, sz1, buf);
126 if (r == 1 && sz1 < itemsize) /* boundary? - must read second part */
127 r = bf_read(p->index_BFile, 2+pos/RIDX_CHUNK, 0, itemsize - sz1,
129 if (r != 1 && !ignoreError)
131 yaz_log(YLOG_FATAL|YLOG_ERRNO, "read in %s at pos %ld",
132 p->index_fname, (long) pos);
137 static void write_indx(recindex_t p, zint sysno, void *buf, int itemsize)
139 zint pos = (sysno-1)*itemsize;
140 int off = CAST_ZINT_TO_INT(pos%RIDX_CHUNK);
141 int sz1 = RIDX_CHUNK - off; /* sz1 is size of buffer to read.. */
144 sz1 = itemsize; /* no more than itemsize bytes */
146 bf_write(p->index_BFile, 1+pos/RIDX_CHUNK, off, sz1, buf);
147 if (sz1 < itemsize) /* boundary? must write second part */
148 bf_write(p->index_BFile, 2+pos/RIDX_CHUNK, 0, itemsize - sz1,
152 static ZEBRA_RES rec_release_blocks(Records p, zint sysno)
154 struct record_index_entry entry;
156 char block_and_ref[sizeof(zint) + sizeof(short)];
160 if (read_indx(p->recindex, sysno, &entry, sizeof(entry), 1) != 1)
163 freeblock = entry.next;
164 assert(freeblock > 0);
165 dst_type = CAST_ZINT_TO_INT(freeblock & 7);
166 assert(dst_type < REC_BLOCK_TYPES);
167 freeblock = freeblock / 8;
170 if (bf_read(p->data_BFile[dst_type], freeblock, 0,
171 first ? sizeof(block_and_ref) : sizeof(zint),
174 yaz_log(YLOG_FATAL|YLOG_ERRNO, "read in rec_del_single");
180 memcpy(&ref, block_and_ref + sizeof(freeblock), sizeof(ref));
182 memcpy(block_and_ref + sizeof(freeblock), &ref, sizeof(ref));
185 if (bf_write(p->data_BFile[dst_type], freeblock, 0,
186 sizeof(block_and_ref), block_and_ref))
188 yaz_log(YLOG_FATAL|YLOG_ERRNO, "write in rec_del_single");
196 if (bf_write(p->data_BFile[dst_type], freeblock, 0, sizeof(freeblock),
197 &p->head.block_free[dst_type]))
199 yaz_log(YLOG_FATAL|YLOG_ERRNO, "write in rec_del_single");
202 p->head.block_free[dst_type] = freeblock;
203 memcpy(&freeblock, block_and_ref, sizeof(freeblock));
205 p->head.block_used[dst_type]--;
207 p->head.total_bytes -= entry.size;
211 static ZEBRA_RES rec_delete_single(Records p, Record rec)
213 struct record_index_entry entry;
215 /* all data in entry must be reset, since it's written verbatim */
216 memset(&entry, '\0', sizeof(entry));
217 if (rec_release_blocks(p, rec_sysno_to_int(rec->sysno)) != ZEBRA_OK)
220 entry.next = p->head.index_free;
222 p->head.index_free = rec_sysno_to_int(rec->sysno);
223 write_indx(p->recindex, rec_sysno_to_int(rec->sysno), &entry, sizeof(entry));
227 static ZEBRA_RES rec_write_tmp_buf(Records p, int size, zint *sysnos)
229 struct record_index_entry entry;
231 char *cptr = p->tmp_buf;
232 zint block_prev = -1, block_free;
236 /* all data in entry must be reset, since it's written verbatim */
237 memset(&entry, '\0', sizeof(entry));
239 for (i = 1; i<REC_BLOCK_TYPES; i++)
240 if (size >= p->head.block_move[i])
242 while (no_written < size)
244 block_free = p->head.block_free[dst_type];
247 if (bf_read(p->data_BFile[dst_type],
248 block_free, 0, sizeof(*p->head.block_free),
249 &p->head.block_free[dst_type]) != 1)
251 yaz_log(YLOG_FATAL|YLOG_ERRNO, "read in %s at free block "
253 p->data_fname[dst_type], block_free);
258 block_free = p->head.block_last[dst_type]++;
259 if (block_prev == -1)
261 entry.next = block_free*8 + dst_type;
263 p->head.total_bytes += size;
266 write_indx(p->recindex, *sysnos, &entry, sizeof(entry));
272 memcpy(cptr, &block_free, sizeof(block_free));
273 bf_write(p->data_BFile[dst_type], block_prev, 0, 0, cptr);
274 cptr = p->tmp_buf + no_written;
276 block_prev = block_free;
277 no_written += CAST_ZINT_TO_INT(p->head.block_size[dst_type])
279 p->head.block_used[dst_type]++;
281 assert(block_prev != -1);
283 memcpy(cptr, &block_free, sizeof(block_free));
284 bf_write(p->data_BFile[dst_type], block_prev, 0,
285 sizeof(block_free) + (p->tmp_buf+size) - cptr, cptr);
289 recindex_t recindex_open(BFiles bfs, int rw)
291 recindex_t p = xmalloc(sizeof(*p));
292 p->index_fname = "reci";
293 p->index_BFile = bf_open(bfs, p->index_fname, RIDX_CHUNK, rw);
294 if (p->index_BFile == NULL)
296 yaz_log(YLOG_FATAL|YLOG_ERRNO, "open %s", p->index_fname);
303 void recindex_close(recindex_t p)
308 bf_close(p->index_BFile);
314 Records rec_open(BFiles bfs, int rw, int compression_method)
319 ZEBRA_RES ret = ZEBRA_OK;
321 p = (Records) xmalloc(sizeof(*p));
322 memset(&p->head, '\0', sizeof(p->head));
323 p->compression_method = compression_method;
326 p->recindex = recindex_open(bfs, rw);
327 p->tmp_buf = (char *) xmalloc(p->tmp_size);
328 r = rec_read_head(p->recindex, p->tmp_buf);
332 memcpy(p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic));
333 sprintf(p->head.version, "%3d", REC_VERSION);
334 p->head.index_free = 0;
335 p->head.index_last = 1;
336 p->head.no_records = 0;
337 p->head.total_bytes = 0;
338 for (i = 0; i<REC_BLOCK_TYPES; i++)
340 p->head.block_free[i] = 0;
341 p->head.block_last[i] = 1;
342 p->head.block_used[i] = 0;
344 p->head.block_size[0] = 128;
345 p->head.block_move[0] = 0;
346 for (i = 1; i<REC_BLOCK_TYPES; i++)
348 p->head.block_size[i] = p->head.block_size[i-1] * 4;
349 p->head.block_move[i] = p->head.block_size[i] * 24;
353 if (rec_write_head(p->recindex, &p->head, sizeof(p->head)) != ZEBRA_OK)
358 memcpy(&p->head, p->tmp_buf, sizeof(p->head));
359 if (memcmp(p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic)))
361 yaz_log(YLOG_FATAL, "file %s has bad format",
362 recindex_get_fname(p->recindex));
365 version = atoi(p->head.version);
366 if (version != REC_VERSION)
368 yaz_log(YLOG_FATAL, "file %s is version %d, but version"
370 recindex_get_fname(p->recindex), version, REC_VERSION);
375 for (i = 0; i<REC_BLOCK_TYPES; i++)
378 sprintf(str, "recd%c", i + 'A');
379 p->data_fname[i] = (char *) xmalloc(strlen(str)+1);
380 strcpy(p->data_fname[i], str);
381 p->data_BFile[i] = NULL;
383 for (i = 0; i<REC_BLOCK_TYPES; i++)
385 if (!(p->data_BFile[i] =
386 bf_open(bfs, p->data_fname[i],
387 CAST_ZINT_TO_INT(p->head.block_size[i]), rw)))
389 yaz_log(YLOG_FATAL|YLOG_ERRNO, "bf_open %s", p->data_fname[i]);
396 p->record_cache = (struct record_cache_entry *)
397 xmalloc(sizeof(*p->record_cache)*p->cache_max);
398 zebra_mutex_init(&p->mutex);
399 if (ret == ZEBRA_FAIL)
404 static void rec_encode_unsigned(unsigned n, unsigned char *buf, int *len)
409 buf[*len] = 128 + (n & 127);
417 static void rec_decode_unsigned(unsigned *np, unsigned char *buf, int *len)
423 while (buf[*len] > 127)
425 n += w*(buf[*len] & 127);
434 static void rec_encode_zint(zint n, unsigned char *buf, int *len)
439 buf[*len] = (unsigned) (128 + (n & 127));
443 buf[*len] = (unsigned) n;
447 static void rec_decode_zint(zint *np, unsigned char *buf, int *len)
453 while (buf[*len] > 127)
455 n += w*(buf[*len] & 127);
464 static void rec_cache_flush_block1(Records p, Record rec, Record last_rec,
465 char **out_buf, int *out_size,
471 for (i = 0; i<REC_NO_INFO; i++)
473 if (*out_offset + CAST_ZINT_TO_INT(rec->size[i]) + 20 > *out_size)
475 int new_size = *out_offset + rec->size[i] + 65536;
476 char *np = (char *) xmalloc(new_size);
478 memcpy(np, *out_buf, *out_offset);
480 *out_size = new_size;
485 rec_encode_zint(rec_sysno_to_int(rec->sysno),
486 (unsigned char *) *out_buf + *out_offset, &len);
487 (*out_offset) += len;
489 if (rec->size[i] == 0)
491 rec_encode_unsigned(1, (unsigned char *) *out_buf + *out_offset,
493 (*out_offset) += len;
495 else if (last_rec && rec->size[i] == last_rec->size[i] &&
496 !memcmp(rec->info[i], last_rec->info[i], rec->size[i]))
498 rec_encode_unsigned(0, (unsigned char *) *out_buf + *out_offset,
500 (*out_offset) += len;
504 rec_encode_unsigned(rec->size[i]+1,
505 (unsigned char *) *out_buf + *out_offset,
507 (*out_offset) += len;
508 memcpy(*out_buf + *out_offset, rec->info[i], rec->size[i]);
509 (*out_offset) += rec->size[i];
514 static ZEBRA_RES rec_write_multiple(Records p, int saveCount)
518 char compression_method;
522 char *out_buf = (char *) xmalloc(out_size);
523 zint *sysnos = (zint *) xmalloc(sizeof(*sysnos) * (p->cache_cur + 1));
524 zint *sysnop = sysnos;
525 ZEBRA_RES ret = ZEBRA_OK;
527 for (i = 0; i<p->cache_cur - saveCount; i++)
529 struct record_cache_entry *e = p->record_cache + i;
533 rec_cache_flush_block1(p, e->rec, last_rec, &out_buf,
534 &out_size, &out_offset);
535 *sysnop++ = rec_sysno_to_int(e->rec->sysno);
537 e->flag = recordFlagNop;
540 case recordFlagWrite:
541 if (rec_release_blocks(p, rec_sysno_to_int(e->rec->sysno))
545 rec_cache_flush_block1(p, e->rec, last_rec, &out_buf,
546 &out_size, &out_offset);
547 *sysnop++ = rec_sysno_to_int(e->rec->sysno);
549 e->flag = recordFlagNop;
552 case recordFlagDelete:
553 if (rec_delete_single(p, e->rec) != ZEBRA_OK)
556 e->flag = recordFlagNop;
566 unsigned int csize = 0; /* indicate compression "not performed yet" */
567 compression_method = p->compression_method;
568 switch (compression_method)
570 case REC_COMPRESS_BZIP2:
572 csize = out_offset + (out_offset >> 6) + 620;
573 rec_tmp_expand(p, csize);
574 #ifdef BZ_CONFIG_ERROR
575 i = BZ2_bzBuffToBuffCompress
577 i = bzBuffToBuffCompress
579 (p->tmp_buf+sizeof(zint)+sizeof(short)+
581 &csize, out_buf, out_offset, 1, 0, 30);
584 yaz_log(YLOG_WARN, "bzBuffToBuffCompress error code=%d", i);
587 yaz_log(YLOG_LOG, "compress %4d %5d %5d", ref_count, out_offset,
591 case REC_COMPRESS_NONE:
596 /* either no compression or compression not supported ... */
598 rec_tmp_expand(p, csize);
599 memcpy(p->tmp_buf + sizeof(zint) + sizeof(short) + sizeof(char),
600 out_buf, out_offset);
602 compression_method = REC_COMPRESS_NONE;
604 memcpy(p->tmp_buf + sizeof(zint), &ref_count, sizeof(ref_count));
605 memcpy(p->tmp_buf + sizeof(zint)+sizeof(short),
606 &compression_method, sizeof(compression_method));
608 /* -------- compression */
609 if (rec_write_tmp_buf(p, csize + sizeof(short) + sizeof(char), sysnos)
618 static ZEBRA_RES rec_cache_flush(Records p, int saveCount)
623 if (saveCount >= p->cache_cur)
626 ret = rec_write_multiple(p, saveCount);
628 for (i = 0; i<p->cache_cur - saveCount; i++)
630 struct record_cache_entry *e = p->record_cache + i;
633 /* i still being used ... */
634 for (j = 0; j<saveCount; j++, i++)
635 memcpy(p->record_cache+j, p->record_cache+i,
636 sizeof(*p->record_cache));
637 p->cache_cur = saveCount;
641 static Record *rec_cache_lookup(Records p, zint sysno,
642 enum recordCacheFlag flag)
645 for (i = 0; i<p->cache_cur; i++)
647 struct record_cache_entry *e = p->record_cache + i;
648 if (e->rec->sysno == sysno)
650 if (flag != recordFlagNop && e->flag == recordFlagNop)
658 static ZEBRA_RES rec_cache_insert(Records p, Record rec, enum recordCacheFlag flag)
660 struct record_cache_entry *e;
661 ZEBRA_RES ret = ZEBRA_OK;
663 if (p->cache_cur == p->cache_max)
664 ret = rec_cache_flush(p, 1);
665 else if (p->cache_cur > 0)
669 for (i = 0; i<p->cache_cur; i++)
671 Record r = (p->record_cache + i)->rec;
672 for (j = 0; j<REC_NO_INFO; j++)
676 ret = rec_cache_flush(p, 1);
678 assert(p->cache_cur < p->cache_max);
680 e = p->record_cache + (p->cache_cur)++;
682 e->rec = rec_cp(rec);
686 ZEBRA_RES rec_close(Records *pp)
690 ZEBRA_RES ret = ZEBRA_OK;
695 zebra_mutex_destroy(&p->mutex);
696 if (rec_cache_flush(p, 0) != ZEBRA_OK)
699 xfree(p->record_cache);
703 if (rec_write_head(p->recindex, &p->head, sizeof(p->head)) != ZEBRA_OK)
707 recindex_close(p->recindex);
709 for (i = 0; i<REC_BLOCK_TYPES; i++)
711 if (p->data_BFile[i])
712 bf_close(p->data_BFile[i]);
713 xfree(p->data_fname[i]);
721 static Record rec_get_int(Records p, zint sysno)
725 struct record_index_entry entry;
732 unsigned int bz_size;
734 char compression_method;
739 if ((recp = rec_cache_lookup(p, sysno, recordFlagNop)))
740 return rec_cp(*recp);
742 if (read_indx(p->recindex, rec_sysno_to_int(sysno), &entry, sizeof(entry), 1) < 1)
743 return NULL; /* record is not there! */
746 return NULL; /* record is deleted */
748 dst_type = (int) (entry.next & 7);
749 assert(dst_type < REC_BLOCK_TYPES);
750 freeblock = entry.next / 8;
752 assert(freeblock > 0);
754 rec_tmp_expand(p, entry.size);
757 r = bf_read(p->data_BFile[dst_type], freeblock, 0, 0, cptr);
760 memcpy(&freeblock, cptr, sizeof(freeblock));
766 cptr += p->head.block_size[dst_type] - sizeof(freeblock);
768 memcpy(&tmp, cptr, sizeof(tmp));
769 r = bf_read(p->data_BFile[dst_type], freeblock, 0, 0, cptr);
772 memcpy(&freeblock, cptr, sizeof(freeblock));
773 memcpy(cptr, &tmp, sizeof(tmp));
776 rec = (Record) xmalloc(sizeof(*rec));
778 memcpy(&compression_method, p->tmp_buf + sizeof(zint) + sizeof(short),
779 sizeof(compression_method));
780 in_buf = p->tmp_buf + sizeof(zint) + sizeof(short) + sizeof(char);
781 in_size = entry.size - sizeof(short) - sizeof(char);
782 switch (compression_method)
784 case REC_COMPRESS_BZIP2:
786 bz_size = entry.size * 20 + 100;
789 bz_buf = (char *) xmalloc(bz_size);
790 #ifdef BZ_CONFIG_ERROR
791 i = BZ2_bzBuffToBuffDecompress
793 i = bzBuffToBuffDecompress
795 (bz_buf, &bz_size, in_buf, in_size, 0, 0);
796 yaz_log(YLOG_LOG, "decompress %5d %5d", in_size, bz_size);
799 yaz_log(YLOG_LOG, "failed");
806 yaz_log(YLOG_FATAL, "cannot decompress record(s) in BZIP2 format");
810 case REC_COMPRESS_NONE:
813 for (i = 0; i<REC_NO_INFO; i++)
816 nptr = in_buf; /* skip ref count */
817 while (nptr < in_buf + in_size)
821 rec_decode_zint(&this_sysno, (unsigned char *) nptr, &len);
824 for (i = 0; i < REC_NO_INFO; i++)
826 unsigned int this_size;
827 rec_decode_unsigned(&this_size, (unsigned char *) nptr, &len);
832 rec->size[i] = this_size-1;
837 nptr += rec->size[i];
842 if (this_sysno == rec_sysno_to_int(sysno))
845 for (i = 0; i<REC_NO_INFO; i++)
847 if (rec->info[i] && rec->size[i])
849 char *np = xmalloc(rec->size[i]+1);
850 memcpy(np, rec->info[i], rec->size[i]);
851 np[rec->size[i]] = '\0';
856 assert(rec->info[i] == 0);
857 assert(rec->size[i] == 0);
861 if (rec_cache_insert(p, rec, recordFlagNop) != ZEBRA_OK)
866 Record rec_get(Records p, zint sysno)
869 zebra_mutex_lock(&p->mutex);
871 rec = rec_get_int(p, sysno);
872 zebra_mutex_unlock(&p->mutex);
876 Record rec_get_root(Records p)
878 return rec_get(p, rec_sysno_to_ext(1));
881 static Record rec_new_int(Records p)
888 rec = (Record) xmalloc(sizeof(*rec));
889 if (1 || p->head.index_free == 0)
890 sysno = (p->head.index_last)++;
893 struct record_index_entry entry;
895 if (read_indx(p->recindex, p->head.index_free, &entry, sizeof(entry), 0) < 1)
900 sysno = p->head.index_free;
901 p->head.index_free = entry.next;
903 (p->head.no_records)++;
904 rec->sysno = rec_sysno_to_ext(sysno);
905 for (i = 0; i < REC_NO_INFO; i++)
910 rec_cache_insert(p, rec, recordFlagNew);
914 Record rec_new(Records p)
917 zebra_mutex_lock(&p->mutex);
919 rec = rec_new_int(p);
920 zebra_mutex_unlock(&p->mutex);
924 ZEBRA_RES rec_del(Records p, Record *recpp)
927 ZEBRA_RES ret = ZEBRA_OK;
929 zebra_mutex_lock(&p->mutex);
930 (p->head.no_records)--;
931 if ((recp = rec_cache_lookup(p, (*recpp)->sysno, recordFlagDelete)))
938 ret = rec_cache_insert(p, *recpp, recordFlagDelete);
941 zebra_mutex_unlock(&p->mutex);
946 ZEBRA_RES rec_put(Records p, Record *recpp)
949 ZEBRA_RES ret = ZEBRA_OK;
951 zebra_mutex_lock(&p->mutex);
952 if ((recp = rec_cache_lookup(p, (*recpp)->sysno, recordFlagWrite)))
959 ret = rec_cache_insert(p, *recpp, recordFlagWrite);
962 zebra_mutex_unlock(&p->mutex);
967 void rec_free(Record *recpp)
973 for (i = 0; i < REC_NO_INFO; i++)
974 xfree((*recpp)->info[i]);
979 Record rec_cp(Record rec)
984 n = (Record) xmalloc(sizeof(*n));
985 n->sysno = rec->sysno;
986 for (i = 0; i < REC_NO_INFO; i++)
994 n->size[i] = rec->size[i];
995 n->info[i] = (char *) xmalloc(rec->size[i]+1);
996 memcpy(n->info[i], rec->info[i], rec->size[i]);
997 n->info[i][rec->size[i]] = '\0';
1003 char *rec_strdup(const char *s, size_t *len)
1013 p = (char *) xmalloc(*len);
1021 * indent-tabs-mode: nil
1023 * vim: shiftwidth=4 tabstop=8 expandtab