1 /* $Id: recindex.c,v 1.34.2.1 2004-08-18 16:17:13 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
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 Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
27 * Format of first block
32 * Format of subsequent blocks
36 * Format of each record
38 * (length, data) - pairs
39 * length = 0 if same as previous
50 static void rec_write_head (Records p)
55 assert (p->index_BFile);
57 r = bf_write (p->index_BFile, 0, 0, sizeof(p->head), &p->head);
60 logf (LOG_FATAL|LOG_ERRNO, "write head of %s", p->index_fname);
65 static void rec_tmp_expand (Records p, int size)
67 if (p->tmp_size < size + 2048 ||
68 p->tmp_size < p->head.block_size[REC_BLOCK_TYPES-1]*2)
71 p->tmp_size = size + p->head.block_size[REC_BLOCK_TYPES-1]*2 + 2048;
72 p->tmp_buf = (char *) xmalloc (p->tmp_size);
76 static int read_indx (Records p, int sysno, void *buf, int itemsize,
80 int pos = (sysno-1)*itemsize;
82 r = bf_read (p->index_BFile, 1+pos/128, pos%128, itemsize, buf);
83 if (r != 1 && !ignoreError)
85 logf (LOG_FATAL|LOG_ERRNO, "read in %s at pos %ld",
86 p->index_fname, (long) pos);
92 static void write_indx (Records p, int sysno, void *buf, int itemsize)
94 int pos = (sysno-1)*itemsize;
96 bf_write (p->index_BFile, 1+pos/128, pos%128, itemsize, buf);
99 static void rec_release_blocks (Records p, int sysno)
101 struct record_index_entry entry;
103 char block_and_ref[sizeof(int) + sizeof(short)];
107 if (read_indx (p, sysno, &entry, sizeof(entry), 1) != 1)
110 freeblock = entry.next;
111 assert (freeblock > 0);
112 dst_type = freeblock & 7;
113 assert (dst_type < REC_BLOCK_TYPES);
114 freeblock = freeblock / 8;
117 if (bf_read (p->data_BFile[dst_type], freeblock, 0,
118 first ? sizeof(block_and_ref) : sizeof(int),
121 logf (LOG_FATAL|LOG_ERRNO, "read in rec_del_single");
127 memcpy (&ref, block_and_ref + sizeof(int), sizeof(ref));
129 memcpy (block_and_ref + sizeof(int), &ref, sizeof(ref));
132 if (bf_write (p->data_BFile[dst_type], freeblock, 0,
133 sizeof(block_and_ref), block_and_ref))
135 logf (LOG_FATAL|LOG_ERRNO, "write in rec_del_single");
143 if (bf_write (p->data_BFile[dst_type], freeblock, 0, sizeof(freeblock),
144 &p->head.block_free[dst_type]))
146 logf (LOG_FATAL|LOG_ERRNO, "write in rec_del_single");
149 p->head.block_free[dst_type] = freeblock;
150 memcpy (&freeblock, block_and_ref, sizeof(int));
152 p->head.block_used[dst_type]--;
154 p->head.total_bytes -= entry.size;
157 static void rec_delete_single (Records p, Record rec)
159 struct record_index_entry entry;
161 rec_release_blocks (p, rec->sysno);
163 entry.next = p->head.index_free;
165 p->head.index_free = rec->sysno;
166 write_indx (p, rec->sysno, &entry, sizeof(entry));
169 static void rec_write_tmp_buf (Records p, int size, int *sysnos)
171 struct record_index_entry entry;
173 char *cptr = p->tmp_buf;
174 int block_prev = -1, block_free;
178 for (i = 1; i<REC_BLOCK_TYPES; i++)
179 if (size >= p->head.block_move[i])
181 while (no_written < size)
183 block_free = p->head.block_free[dst_type];
186 if (bf_read (p->data_BFile[dst_type],
187 block_free, 0, sizeof(*p->head.block_free),
188 &p->head.block_free[dst_type]) != 1)
190 logf (LOG_FATAL|LOG_ERRNO, "read in %s at free block %d",
191 p->data_fname[dst_type], block_free);
196 block_free = p->head.block_last[dst_type]++;
197 if (block_prev == -1)
199 entry.next = block_free*8 + dst_type;
201 p->head.total_bytes += size;
204 write_indx (p, *sysnos, &entry, sizeof(entry));
210 memcpy (cptr, &block_free, sizeof(int));
211 bf_write (p->data_BFile[dst_type], block_prev, 0, 0, cptr);
212 cptr = p->tmp_buf + no_written;
214 block_prev = block_free;
215 no_written += p->head.block_size[dst_type] - sizeof(int);
216 p->head.block_used[dst_type]++;
218 assert (block_prev != -1);
220 memcpy (cptr, &block_free, sizeof(int));
221 bf_write (p->data_BFile[dst_type], block_prev, 0,
222 sizeof(int) + (p->tmp_buf+size) - cptr, cptr);
225 Records rec_open (BFiles bfs, int rw, int compression_method)
231 p = (Records) xmalloc (sizeof(*p));
232 p->compression_method = compression_method;
235 p->tmp_buf = (char *) xmalloc (p->tmp_size);
236 p->index_fname = "reci";
237 p->index_BFile = bf_open (bfs, p->index_fname, 128, rw);
238 if (p->index_BFile == NULL)
240 logf (LOG_FATAL|LOG_ERRNO, "open %s", p->index_fname);
243 r = bf_read (p->index_BFile, 0, 0, 0, p->tmp_buf);
247 memcpy (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic));
248 sprintf (p->head.version, "%3d", REC_VERSION);
249 p->head.index_free = 0;
250 p->head.index_last = 1;
251 p->head.no_records = 0;
252 p->head.total_bytes = 0;
253 for (i = 0; i<REC_BLOCK_TYPES; i++)
255 p->head.block_free[i] = 0;
256 p->head.block_last[i] = 1;
257 p->head.block_used[i] = 0;
259 p->head.block_size[0] = 128;
260 p->head.block_move[0] = 0;
261 for (i = 1; i<REC_BLOCK_TYPES; i++)
263 p->head.block_size[i] = p->head.block_size[i-1] * 4;
264 p->head.block_move[i] = p->head.block_size[i] * 24;
270 memcpy (&p->head, p->tmp_buf, sizeof(p->head));
271 if (memcmp (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic)))
273 logf (LOG_FATAL, "file %s has bad format", p->index_fname);
276 version = atoi (p->head.version);
277 if (version != REC_VERSION)
279 logf (LOG_FATAL, "file %s is version %d, but version"
280 " %d is required", p->index_fname, version, REC_VERSION);
285 for (i = 0; i<REC_BLOCK_TYPES; i++)
288 sprintf (str, "recd%c", i + 'A');
289 p->data_fname[i] = (char *) xmalloc (strlen(str)+1);
290 strcpy (p->data_fname[i], str);
291 p->data_BFile[i] = NULL;
293 for (i = 0; i<REC_BLOCK_TYPES; i++)
295 if (!(p->data_BFile[i] = bf_open (bfs, p->data_fname[i],
296 p->head.block_size[i],
299 logf (LOG_FATAL|LOG_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);
311 static void rec_encode_unsigned (unsigned n, unsigned char *buf, int *len)
316 buf[*len] = 128 + (n & 127);
324 static void rec_decode_unsigned(unsigned *np, unsigned char *buf, int *len)
330 while (buf[*len] > 127)
332 n += w*(buf[*len] & 127);
341 static void rec_cache_flush_block1 (Records p, Record rec, Record last_rec,
342 char **out_buf, int *out_size,
348 for (i = 0; i<REC_NO_INFO; i++)
350 if (*out_offset + (int) rec->size[i] + 20 > *out_size)
352 int new_size = *out_offset + rec->size[i] + 65536;
353 char *np = (char *) xmalloc (new_size);
355 memcpy (np, *out_buf, *out_offset);
357 *out_size = new_size;
362 rec_encode_unsigned (rec->sysno, *out_buf + *out_offset, &len);
363 (*out_offset) += len;
365 if (rec->size[i] == 0)
367 rec_encode_unsigned (1, *out_buf + *out_offset, &len);
368 (*out_offset) += len;
370 else if (last_rec && rec->size[i] == last_rec->size[i] &&
371 !memcmp (rec->info[i], last_rec->info[i], rec->size[i]))
373 rec_encode_unsigned (0, *out_buf + *out_offset, &len);
374 (*out_offset) += len;
378 rec_encode_unsigned (rec->size[i]+1, *out_buf + *out_offset, &len);
379 (*out_offset) += len;
380 memcpy (*out_buf + *out_offset, rec->info[i], rec->size[i]);
381 (*out_offset) += rec->size[i];
386 static void rec_write_multiple (Records p, int saveCount)
390 char compression_method;
394 char *out_buf = (char *) xmalloc (out_size);
395 int *sysnos = (int *) xmalloc (sizeof(*sysnos) * (p->cache_cur + 1));
396 int *sysnop = sysnos;
398 for (i = 0; i<p->cache_cur - saveCount; i++)
400 struct record_cache_entry *e = p->record_cache + i;
404 rec_cache_flush_block1 (p, e->rec, last_rec, &out_buf,
405 &out_size, &out_offset);
406 *sysnop++ = e->rec->sysno;
408 e->flag = recordFlagNop;
411 case recordFlagWrite:
412 rec_release_blocks (p, e->rec->sysno);
413 rec_cache_flush_block1 (p, e->rec, last_rec, &out_buf,
414 &out_size, &out_offset);
415 *sysnop++ = e->rec->sysno;
417 e->flag = recordFlagNop;
420 case recordFlagDelete:
421 rec_delete_single (p, e->rec);
422 e->flag = recordFlagNop;
432 int csize = 0; /* indicate compression "not performed yet" */
433 compression_method = p->compression_method;
434 switch (compression_method)
436 case REC_COMPRESS_BZIP2:
438 csize = out_offset + (out_offset >> 6) + 620;
439 rec_tmp_expand (p, csize);
440 #ifdef BZ_CONFIG_ERROR
441 i = BZ2_bzBuffToBuffCompress
443 i = bzBuffToBuffCompress
445 (p->tmp_buf+sizeof(int)+sizeof(short)+
447 &csize, out_buf, out_offset, 1, 0, 30);
450 logf (LOG_WARN, "bzBuffToBuffCompress error code=%d", i);
453 logf (LOG_LOG, "compress %4d %5d %5d", ref_count, out_offset,
457 case REC_COMPRESS_NONE:
462 /* either no compression or compression not supported ... */
464 rec_tmp_expand (p, csize);
465 memcpy (p->tmp_buf + sizeof(int) + sizeof(short) + sizeof(char),
466 out_buf, out_offset);
468 compression_method = REC_COMPRESS_NONE;
470 memcpy (p->tmp_buf + sizeof(int), &ref_count, sizeof(ref_count));
471 memcpy (p->tmp_buf + sizeof(int)+sizeof(short),
472 &compression_method, sizeof(compression_method));
474 /* -------- compression */
475 rec_write_tmp_buf (p, csize + sizeof(short) + sizeof(char), sysnos);
481 static void rec_cache_flush (Records p, int saveCount)
485 if (saveCount >= p->cache_cur)
488 rec_write_multiple (p, saveCount);
490 for (i = 0; i<p->cache_cur - saveCount; i++)
492 struct record_cache_entry *e = p->record_cache + i;
495 /* i still being used ... */
496 for (j = 0; j<saveCount; j++, i++)
497 memcpy (p->record_cache+j, p->record_cache+i,
498 sizeof(*p->record_cache));
499 p->cache_cur = saveCount;
502 static Record *rec_cache_lookup (Records p, int sysno,
503 enum recordCacheFlag flag)
506 for (i = 0; i<p->cache_cur; i++)
508 struct record_cache_entry *e = p->record_cache + i;
509 if (e->rec->sysno == sysno)
511 if (flag != recordFlagNop && e->flag == recordFlagNop)
519 static void rec_cache_insert (Records p, Record rec, enum recordCacheFlag flag)
521 struct record_cache_entry *e;
523 if (p->cache_cur == p->cache_max)
524 rec_cache_flush (p, 1);
525 else if (p->cache_cur > 0)
529 for (i = 0; i<p->cache_cur; i++)
531 Record r = (p->record_cache + i)->rec;
532 for (j = 0; j<REC_NO_INFO; j++)
536 rec_cache_flush (p, 1);
538 assert (p->cache_cur < p->cache_max);
540 e = p->record_cache + (p->cache_cur)++;
542 e->rec = rec_cp (rec);
545 void rec_close (Records *pp)
552 zebra_mutex_destroy (&p->mutex);
553 rec_cache_flush (p, 0);
554 xfree (p->record_cache);
560 bf_close (p->index_BFile);
562 for (i = 0; i<REC_BLOCK_TYPES; i++)
564 if (p->data_BFile[i])
565 bf_close (p->data_BFile[i]);
566 xfree (p->data_fname[i]);
573 static Record rec_get_int (Records p, int sysno)
577 struct record_index_entry entry;
578 int freeblock, dst_type;
585 char compression_method;
590 if ((recp = rec_cache_lookup (p, sysno, recordFlagNop)))
591 return rec_cp (*recp);
593 if (read_indx (p, sysno, &entry, sizeof(entry), 1) < 1)
594 return NULL; /* record is not there! */
597 return NULL; /* record is deleted */
599 dst_type = entry.next & 7;
600 assert (dst_type < REC_BLOCK_TYPES);
601 freeblock = entry.next / 8;
603 assert (freeblock > 0);
605 rec_tmp_expand (p, entry.size);
608 r = bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
611 memcpy (&freeblock, cptr, sizeof(freeblock));
617 cptr += p->head.block_size[dst_type] - sizeof(freeblock);
619 memcpy (&tmp, cptr, sizeof(tmp));
620 r = bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
623 memcpy (&freeblock, cptr, sizeof(freeblock));
624 memcpy (cptr, &tmp, sizeof(tmp));
627 rec = (Record) xmalloc (sizeof(*rec));
629 memcpy (&compression_method, p->tmp_buf + sizeof(int) + sizeof(short),
630 sizeof(compression_method));
631 in_buf = p->tmp_buf + sizeof(int) + sizeof(short) + sizeof(char);
632 in_size = entry.size - sizeof(short) - sizeof(char);
633 switch (compression_method)
635 case REC_COMPRESS_BZIP2:
637 bz_size = entry.size * 20 + 100;
640 bz_buf = (char *) xmalloc (bz_size);
641 #ifdef BZ_CONFIG_ERROR
642 i = BZ2_bzBuffToBuffDecompress
644 i = bzBuffToBuffDecompress
646 (bz_buf, &bz_size, in_buf, in_size, 0, 0);
647 logf (LOG_LOG, "decompress %5d %5d", in_size, bz_size);
650 logf (LOG_LOG, "failed");
657 logf (LOG_FATAL, "cannot decompress record(s) in BZIP2 format");
661 case REC_COMPRESS_NONE:
664 for (i = 0; i<REC_NO_INFO; i++)
667 nptr = in_buf; /* skip ref count */
668 while (nptr < in_buf + in_size)
672 rec_decode_unsigned (&this_sysno, nptr, &len);
675 for (i = 0; i < REC_NO_INFO; i++)
678 rec_decode_unsigned (&this_size, nptr, &len);
683 rec->size[i] = this_size-1;
688 nptr += rec->size[i];
693 if (this_sysno == sysno)
696 for (i = 0; i<REC_NO_INFO; i++)
698 if (rec->info[i] && rec->size[i])
700 char *np = xmalloc (rec->size[i]+1);
701 memcpy (np, rec->info[i], rec->size[i]);
702 np[rec->size[i]] = '\0';
707 assert (rec->info[i] == 0);
708 assert (rec->size[i] == 0);
712 rec_cache_insert (p, rec, recordFlagNop);
716 Record rec_get (Records p, int sysno)
719 zebra_mutex_lock (&p->mutex);
721 rec = rec_get_int (p, sysno);
722 zebra_mutex_unlock (&p->mutex);
726 static Record rec_new_int (Records p)
732 rec = (Record) xmalloc (sizeof(*rec));
733 if (1 || p->head.index_free == 0)
734 sysno = (p->head.index_last)++;
737 struct record_index_entry entry;
739 read_indx (p, p->head.index_free, &entry, sizeof(entry), 0);
740 sysno = p->head.index_free;
741 p->head.index_free = entry.next;
743 (p->head.no_records)++;
745 for (i = 0; i < REC_NO_INFO; i++)
750 rec_cache_insert (p, rec, recordFlagNew);
754 Record rec_new (Records p)
757 zebra_mutex_lock (&p->mutex);
759 rec = rec_new_int (p);
760 zebra_mutex_unlock (&p->mutex);
764 void rec_del (Records p, Record *recpp)
768 zebra_mutex_lock (&p->mutex);
769 (p->head.no_records)--;
770 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagDelete)))
777 rec_cache_insert (p, *recpp, recordFlagDelete);
780 zebra_mutex_unlock (&p->mutex);
784 void rec_put (Records p, Record *recpp)
788 zebra_mutex_lock (&p->mutex);
789 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagWrite)))
796 rec_cache_insert (p, *recpp, recordFlagWrite);
799 zebra_mutex_unlock (&p->mutex);
803 void rec_rm (Record *recpp)
809 for (i = 0; i < REC_NO_INFO; i++)
810 xfree ((*recpp)->info[i]);
815 Record rec_cp (Record rec)
820 n = (Record) xmalloc (sizeof(*n));
821 n->sysno = rec->sysno;
822 for (i = 0; i < REC_NO_INFO; i++)
830 n->size[i] = rec->size[i];
831 n->info[i] = (char *) xmalloc (rec->size[i]);
832 memcpy (n->info[i], rec->info[i], rec->size[i]);
838 char *rec_strdup (const char *s, size_t *len)
848 p = (char *) xmalloc (*len);