1 /* $Id: records.c,v 1.1 2007-11-23 13:52:52 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>
50 /* Modify argument to if below: 1=normal, 0=sysno testing */
52 /* If this is used sysno are not converted (no testing) */
54 #define USUAL_RANGE 6000000000LL
57 /* Use a fake > 2^32 offset so we can test for proper 64-bit handling */
58 #define FAKE_OFFSET 6000000000LL
59 #define USUAL_RANGE 2000000000LL
62 static zint rec_sysno_to_ext(zint sysno)
64 assert(sysno >= 0 && sysno <= USUAL_RANGE);
65 return sysno + FAKE_OFFSET;
68 zint rec_sysno_to_int(zint sysno)
70 assert(sysno >= FAKE_OFFSET && sysno <= FAKE_OFFSET + USUAL_RANGE);
71 return sysno - FAKE_OFFSET;
74 static void rec_tmp_expand(Records p, int size)
76 if (p->tmp_size < size + 2048 ||
77 p->tmp_size < p->head.block_size[REC_BLOCK_TYPES-1]*2)
80 p->tmp_size = size + (int)
81 (p->head.block_size[REC_BLOCK_TYPES-1])*2 + 2048;
82 p->tmp_buf = (char *) xmalloc(p->tmp_size);
86 static ZEBRA_RES rec_release_blocks(Records p, zint sysno)
88 struct record_index_entry entry;
90 char block_and_ref[sizeof(zint) + sizeof(short)];
94 if (recindex_read_indx(p->recindex, sysno, &entry, sizeof(entry), 1) != 1)
97 freeblock = entry.next;
98 assert(freeblock > 0);
99 dst_type = CAST_ZINT_TO_INT(freeblock & 7);
100 assert(dst_type < REC_BLOCK_TYPES);
101 freeblock = freeblock / 8;
104 if (bf_read(p->data_BFile[dst_type], freeblock, 0,
105 first ? sizeof(block_and_ref) : sizeof(zint),
108 yaz_log(YLOG_FATAL|YLOG_ERRNO, "read in rec_del_single");
114 memcpy(&ref, block_and_ref + sizeof(freeblock), sizeof(ref));
116 memcpy(block_and_ref + sizeof(freeblock), &ref, sizeof(ref));
119 if (bf_write(p->data_BFile[dst_type], freeblock, 0,
120 sizeof(block_and_ref), block_and_ref))
122 yaz_log(YLOG_FATAL|YLOG_ERRNO, "write in rec_del_single");
130 if (bf_write(p->data_BFile[dst_type], freeblock, 0, sizeof(freeblock),
131 &p->head.block_free[dst_type]))
133 yaz_log(YLOG_FATAL|YLOG_ERRNO, "write in rec_del_single");
136 p->head.block_free[dst_type] = freeblock;
137 memcpy(&freeblock, block_and_ref, sizeof(freeblock));
139 p->head.block_used[dst_type]--;
141 p->head.total_bytes -= entry.size;
145 static ZEBRA_RES rec_delete_single(Records p, Record rec)
147 struct record_index_entry entry;
149 /* all data in entry must be reset, since it's written verbatim */
150 memset(&entry, '\0', sizeof(entry));
151 if (rec_release_blocks(p, rec_sysno_to_int(rec->sysno)) != ZEBRA_OK)
154 entry.next = p->head.index_free;
156 p->head.index_free = rec_sysno_to_int(rec->sysno);
157 recindex_write_indx(p->recindex, rec_sysno_to_int(rec->sysno), &entry, sizeof(entry));
161 static ZEBRA_RES rec_write_tmp_buf(Records p, int size, zint *sysnos)
163 struct record_index_entry entry;
165 char *cptr = p->tmp_buf;
166 zint block_prev = -1, block_free;
170 /* all data in entry must be reset, since it's written verbatim */
171 memset(&entry, '\0', sizeof(entry));
173 for (i = 1; i<REC_BLOCK_TYPES; i++)
174 if (size >= p->head.block_move[i])
176 while (no_written < size)
178 block_free = p->head.block_free[dst_type];
181 if (bf_read(p->data_BFile[dst_type],
182 block_free, 0, sizeof(*p->head.block_free),
183 &p->head.block_free[dst_type]) != 1)
185 yaz_log(YLOG_FATAL|YLOG_ERRNO, "read in %s at free block "
187 p->data_fname[dst_type], block_free);
192 block_free = p->head.block_last[dst_type]++;
193 if (block_prev == -1)
195 entry.next = block_free*8 + dst_type;
197 p->head.total_bytes += size;
200 recindex_write_indx(p->recindex, *sysnos, &entry, sizeof(entry));
206 memcpy(cptr, &block_free, sizeof(block_free));
207 bf_write(p->data_BFile[dst_type], block_prev, 0, 0, cptr);
208 cptr = p->tmp_buf + no_written;
210 block_prev = block_free;
211 no_written += CAST_ZINT_TO_INT(p->head.block_size[dst_type])
213 p->head.block_used[dst_type]++;
215 assert(block_prev != -1);
217 memcpy(cptr, &block_free, sizeof(block_free));
218 bf_write(p->data_BFile[dst_type], block_prev, 0,
219 sizeof(block_free) + (p->tmp_buf+size) - cptr, cptr);
223 Records rec_open(BFiles bfs, int rw, int compression_method)
228 ZEBRA_RES ret = ZEBRA_OK;
230 p = (Records) xmalloc(sizeof(*p));
231 memset(&p->head, '\0', sizeof(p->head));
232 p->compression_method = compression_method;
235 p->recindex = recindex_open(bfs, rw);
236 p->tmp_buf = (char *) xmalloc(p->tmp_size);
237 r = recindex_read_head(p->recindex, p->tmp_buf);
241 memcpy(p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic));
242 sprintf(p->head.version, "%3d", REC_VERSION);
243 p->head.index_free = 0;
244 p->head.index_last = 1;
245 p->head.no_records = 0;
246 p->head.total_bytes = 0;
247 for (i = 0; i<REC_BLOCK_TYPES; i++)
249 p->head.block_free[i] = 0;
250 p->head.block_last[i] = 1;
251 p->head.block_used[i] = 0;
253 p->head.block_size[0] = 128;
254 p->head.block_move[0] = 0;
255 for (i = 1; i<REC_BLOCK_TYPES; i++)
257 p->head.block_size[i] = p->head.block_size[i-1] * 4;
258 p->head.block_move[i] = p->head.block_size[i] * 24;
262 if (recindex_write_head(p->recindex, &p->head, sizeof(p->head)) != ZEBRA_OK)
267 memcpy(&p->head, p->tmp_buf, sizeof(p->head));
268 if (memcmp(p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic)))
270 yaz_log(YLOG_FATAL, "file %s has bad format",
271 recindex_get_fname(p->recindex));
274 version = atoi(p->head.version);
275 if (version != REC_VERSION)
277 yaz_log(YLOG_FATAL, "file %s is version %d, but version"
279 recindex_get_fname(p->recindex), version, REC_VERSION);
284 for (i = 0; i<REC_BLOCK_TYPES; i++)
287 sprintf(str, "recd%c", i + 'A');
288 p->data_fname[i] = (char *) xmalloc(strlen(str)+1);
289 strcpy(p->data_fname[i], str);
290 p->data_BFile[i] = NULL;
292 for (i = 0; i<REC_BLOCK_TYPES; i++)
294 if (!(p->data_BFile[i] =
295 bf_open(bfs, p->data_fname[i],
296 CAST_ZINT_TO_INT(p->head.block_size[i]), rw)))
298 yaz_log(YLOG_FATAL|YLOG_ERRNO, "bf_open %s", p->data_fname[i]);
305 p->record_cache = (struct record_cache_entry *)
306 xmalloc(sizeof(*p->record_cache)*p->cache_max);
307 zebra_mutex_init(&p->mutex);
308 if (ret == ZEBRA_FAIL)
313 static void rec_encode_unsigned(unsigned n, unsigned char *buf, int *len)
318 buf[*len] = 128 + (n & 127);
326 static void rec_decode_unsigned(unsigned *np, unsigned char *buf, int *len)
332 while (buf[*len] > 127)
334 n += w*(buf[*len] & 127);
343 static void rec_encode_zint(zint n, unsigned char *buf, int *len)
348 buf[*len] = (unsigned) (128 + (n & 127));
352 buf[*len] = (unsigned) n;
356 static void rec_decode_zint(zint *np, unsigned char *buf, int *len)
362 while (buf[*len] > 127)
364 n += w*(buf[*len] & 127);
373 static void rec_cache_flush_block1(Records p, Record rec, Record last_rec,
374 char **out_buf, int *out_size,
380 for (i = 0; i<REC_NO_INFO; i++)
382 if (*out_offset + CAST_ZINT_TO_INT(rec->size[i]) + 20 > *out_size)
384 int new_size = *out_offset + rec->size[i] + 65536;
385 char *np = (char *) xmalloc(new_size);
387 memcpy(np, *out_buf, *out_offset);
389 *out_size = new_size;
394 rec_encode_zint(rec_sysno_to_int(rec->sysno),
395 (unsigned char *) *out_buf + *out_offset, &len);
396 (*out_offset) += len;
398 if (rec->size[i] == 0)
400 rec_encode_unsigned(1, (unsigned char *) *out_buf + *out_offset,
402 (*out_offset) += len;
404 else if (last_rec && rec->size[i] == last_rec->size[i] &&
405 !memcmp(rec->info[i], last_rec->info[i], rec->size[i]))
407 rec_encode_unsigned(0, (unsigned char *) *out_buf + *out_offset,
409 (*out_offset) += len;
413 rec_encode_unsigned(rec->size[i]+1,
414 (unsigned char *) *out_buf + *out_offset,
416 (*out_offset) += len;
417 memcpy(*out_buf + *out_offset, rec->info[i], rec->size[i]);
418 (*out_offset) += rec->size[i];
423 static ZEBRA_RES rec_write_multiple(Records p, int saveCount)
427 char compression_method;
431 char *out_buf = (char *) xmalloc(out_size);
432 zint *sysnos = (zint *) xmalloc(sizeof(*sysnos) * (p->cache_cur + 1));
433 zint *sysnop = sysnos;
434 ZEBRA_RES ret = ZEBRA_OK;
436 for (i = 0; i<p->cache_cur - saveCount; i++)
438 struct record_cache_entry *e = p->record_cache + i;
442 rec_cache_flush_block1(p, e->rec, last_rec, &out_buf,
443 &out_size, &out_offset);
444 *sysnop++ = rec_sysno_to_int(e->rec->sysno);
446 e->flag = recordFlagNop;
449 case recordFlagWrite:
450 if (rec_release_blocks(p, rec_sysno_to_int(e->rec->sysno))
454 rec_cache_flush_block1(p, e->rec, last_rec, &out_buf,
455 &out_size, &out_offset);
456 *sysnop++ = rec_sysno_to_int(e->rec->sysno);
458 e->flag = recordFlagNop;
461 case recordFlagDelete:
462 if (rec_delete_single(p, e->rec) != ZEBRA_OK)
465 e->flag = recordFlagNop;
475 unsigned int csize = 0; /* indicate compression "not performed yet" */
476 compression_method = p->compression_method;
477 switch (compression_method)
479 case REC_COMPRESS_BZIP2:
481 csize = out_offset + (out_offset >> 6) + 620;
482 rec_tmp_expand(p, csize);
483 #ifdef BZ_CONFIG_ERROR
484 i = BZ2_bzBuffToBuffCompress
486 i = bzBuffToBuffCompress
488 (p->tmp_buf+sizeof(zint)+sizeof(short)+
490 &csize, out_buf, out_offset, 1, 0, 30);
493 yaz_log(YLOG_WARN, "bzBuffToBuffCompress error code=%d", i);
496 yaz_log(YLOG_LOG, "compress %4d %5d %5d", ref_count, out_offset,
500 case REC_COMPRESS_NONE:
505 /* either no compression or compression not supported ... */
507 rec_tmp_expand(p, csize);
508 memcpy(p->tmp_buf + sizeof(zint) + sizeof(short) + sizeof(char),
509 out_buf, out_offset);
511 compression_method = REC_COMPRESS_NONE;
513 memcpy(p->tmp_buf + sizeof(zint), &ref_count, sizeof(ref_count));
514 memcpy(p->tmp_buf + sizeof(zint)+sizeof(short),
515 &compression_method, sizeof(compression_method));
517 /* -------- compression */
518 if (rec_write_tmp_buf(p, csize + sizeof(short) + sizeof(char), sysnos)
527 static ZEBRA_RES rec_cache_flush(Records p, int saveCount)
532 if (saveCount >= p->cache_cur)
535 ret = rec_write_multiple(p, saveCount);
537 for (i = 0; i<p->cache_cur - saveCount; i++)
539 struct record_cache_entry *e = p->record_cache + i;
542 /* i still being used ... */
543 for (j = 0; j<saveCount; j++, i++)
544 memcpy(p->record_cache+j, p->record_cache+i,
545 sizeof(*p->record_cache));
546 p->cache_cur = saveCount;
550 static Record *rec_cache_lookup(Records p, zint sysno,
551 enum recordCacheFlag flag)
554 for (i = 0; i<p->cache_cur; i++)
556 struct record_cache_entry *e = p->record_cache + i;
557 if (e->rec->sysno == sysno)
559 if (flag != recordFlagNop && e->flag == recordFlagNop)
567 static ZEBRA_RES rec_cache_insert(Records p, Record rec, enum recordCacheFlag flag)
569 struct record_cache_entry *e;
570 ZEBRA_RES ret = ZEBRA_OK;
572 if (p->cache_cur == p->cache_max)
573 ret = rec_cache_flush(p, 1);
574 else if (p->cache_cur > 0)
578 for (i = 0; i<p->cache_cur; i++)
580 Record r = (p->record_cache + i)->rec;
581 for (j = 0; j<REC_NO_INFO; j++)
585 ret = rec_cache_flush(p, 1);
587 assert(p->cache_cur < p->cache_max);
589 e = p->record_cache + (p->cache_cur)++;
591 e->rec = rec_cp(rec);
595 ZEBRA_RES rec_close(Records *pp)
599 ZEBRA_RES ret = ZEBRA_OK;
604 zebra_mutex_destroy(&p->mutex);
605 if (rec_cache_flush(p, 0) != ZEBRA_OK)
608 xfree(p->record_cache);
612 if (recindex_write_head(p->recindex, &p->head, sizeof(p->head)) != ZEBRA_OK)
616 recindex_close(p->recindex);
618 for (i = 0; i<REC_BLOCK_TYPES; i++)
620 if (p->data_BFile[i])
621 bf_close(p->data_BFile[i]);
622 xfree(p->data_fname[i]);
630 static Record rec_get_int(Records p, zint sysno)
634 struct record_index_entry entry;
641 unsigned int bz_size;
643 char compression_method;
648 if ((recp = rec_cache_lookup(p, sysno, recordFlagNop)))
649 return rec_cp(*recp);
651 if (recindex_read_indx(p->recindex, rec_sysno_to_int(sysno), &entry, sizeof(entry), 1) < 1)
652 return NULL; /* record is not there! */
655 return NULL; /* record is deleted */
657 dst_type = (int) (entry.next & 7);
658 assert(dst_type < REC_BLOCK_TYPES);
659 freeblock = entry.next / 8;
661 assert(freeblock > 0);
663 rec_tmp_expand(p, entry.size);
666 r = bf_read(p->data_BFile[dst_type], freeblock, 0, 0, cptr);
669 memcpy(&freeblock, cptr, sizeof(freeblock));
675 cptr += p->head.block_size[dst_type] - sizeof(freeblock);
677 memcpy(&tmp, cptr, sizeof(tmp));
678 r = bf_read(p->data_BFile[dst_type], freeblock, 0, 0, cptr);
681 memcpy(&freeblock, cptr, sizeof(freeblock));
682 memcpy(cptr, &tmp, sizeof(tmp));
685 rec = (Record) xmalloc(sizeof(*rec));
687 memcpy(&compression_method, p->tmp_buf + sizeof(zint) + sizeof(short),
688 sizeof(compression_method));
689 in_buf = p->tmp_buf + sizeof(zint) + sizeof(short) + sizeof(char);
690 in_size = entry.size - sizeof(short) - sizeof(char);
691 switch (compression_method)
693 case REC_COMPRESS_BZIP2:
695 bz_size = entry.size * 20 + 100;
698 bz_buf = (char *) xmalloc(bz_size);
699 #ifdef BZ_CONFIG_ERROR
700 i = BZ2_bzBuffToBuffDecompress
702 i = bzBuffToBuffDecompress
704 (bz_buf, &bz_size, in_buf, in_size, 0, 0);
705 yaz_log(YLOG_LOG, "decompress %5d %5d", in_size, bz_size);
708 yaz_log(YLOG_LOG, "failed");
715 yaz_log(YLOG_FATAL, "cannot decompress record(s) in BZIP2 format");
719 case REC_COMPRESS_NONE:
722 for (i = 0; i<REC_NO_INFO; i++)
725 nptr = in_buf; /* skip ref count */
726 while (nptr < in_buf + in_size)
730 rec_decode_zint(&this_sysno, (unsigned char *) nptr, &len);
733 for (i = 0; i < REC_NO_INFO; i++)
735 unsigned int this_size;
736 rec_decode_unsigned(&this_size, (unsigned char *) nptr, &len);
741 rec->size[i] = this_size-1;
746 nptr += rec->size[i];
751 if (this_sysno == rec_sysno_to_int(sysno))
754 for (i = 0; i<REC_NO_INFO; i++)
756 if (rec->info[i] && rec->size[i])
758 char *np = xmalloc(rec->size[i]+1);
759 memcpy(np, rec->info[i], rec->size[i]);
760 np[rec->size[i]] = '\0';
765 assert(rec->info[i] == 0);
766 assert(rec->size[i] == 0);
770 if (rec_cache_insert(p, rec, recordFlagNop) != ZEBRA_OK)
775 Record rec_get(Records p, zint sysno)
778 zebra_mutex_lock(&p->mutex);
780 rec = rec_get_int(p, sysno);
781 zebra_mutex_unlock(&p->mutex);
785 Record rec_get_root(Records p)
787 return rec_get(p, rec_sysno_to_ext(1));
790 static Record rec_new_int(Records p)
797 rec = (Record) xmalloc(sizeof(*rec));
798 if (1 || p->head.index_free == 0)
799 sysno = (p->head.index_last)++;
802 struct record_index_entry entry;
804 if (recindex_read_indx(p->recindex, p->head.index_free, &entry, sizeof(entry), 0) < 1)
809 sysno = p->head.index_free;
810 p->head.index_free = entry.next;
812 (p->head.no_records)++;
813 rec->sysno = rec_sysno_to_ext(sysno);
814 for (i = 0; i < REC_NO_INFO; i++)
819 rec_cache_insert(p, rec, recordFlagNew);
823 Record rec_new(Records p)
826 zebra_mutex_lock(&p->mutex);
828 rec = rec_new_int(p);
829 zebra_mutex_unlock(&p->mutex);
833 ZEBRA_RES rec_del(Records p, Record *recpp)
836 ZEBRA_RES ret = ZEBRA_OK;
838 zebra_mutex_lock(&p->mutex);
839 (p->head.no_records)--;
840 if ((recp = rec_cache_lookup(p, (*recpp)->sysno, recordFlagDelete)))
847 ret = rec_cache_insert(p, *recpp, recordFlagDelete);
850 zebra_mutex_unlock(&p->mutex);
855 ZEBRA_RES rec_put(Records p, Record *recpp)
858 ZEBRA_RES ret = ZEBRA_OK;
860 zebra_mutex_lock(&p->mutex);
861 if ((recp = rec_cache_lookup(p, (*recpp)->sysno, recordFlagWrite)))
868 ret = rec_cache_insert(p, *recpp, recordFlagWrite);
871 zebra_mutex_unlock(&p->mutex);
876 void rec_free(Record *recpp)
882 for (i = 0; i < REC_NO_INFO; i++)
883 xfree((*recpp)->info[i]);
888 Record rec_cp(Record rec)
893 n = (Record) xmalloc(sizeof(*n));
894 n->sysno = rec->sysno;
895 for (i = 0; i < REC_NO_INFO; i++)
903 n->size[i] = rec->size[i];
904 n->info[i] = (char *) xmalloc(rec->size[i]+1);
905 memcpy(n->info[i], rec->info[i], rec->size[i]);
906 n->info[i][rec->size[i]] = '\0';
912 char *rec_strdup(const char *s, size_t *len)
922 p = (char *) xmalloc(*len);
930 * indent-tabs-mode: nil
932 * vim: shiftwidth=4 tabstop=8 expandtab