1 /* $Id: recindex.c,v 1.34 2002-08-02 19:26:55 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(short) + sizeof(int)];
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 sizeof(block_and_ref), block_and_ref) != 1)
120 logf (LOG_FATAL|LOG_ERRNO, "read in rec_del_single");
126 memcpy (&ref, block_and_ref + sizeof(int), sizeof(ref));
128 memcpy (block_and_ref + sizeof(int), &ref, sizeof(ref));
131 if (bf_write (p->data_BFile[dst_type], freeblock, 0,
132 sizeof(block_and_ref), block_and_ref))
134 logf (LOG_FATAL|LOG_ERRNO, "write in rec_del_single");
142 if (bf_write (p->data_BFile[dst_type], freeblock, 0, sizeof(freeblock),
143 &p->head.block_free[dst_type]))
145 logf (LOG_FATAL|LOG_ERRNO, "write in rec_del_single");
148 p->head.block_free[dst_type] = freeblock;
149 memcpy (&freeblock, block_and_ref, sizeof(int));
151 p->head.block_used[dst_type]--;
153 p->head.total_bytes -= entry.size;
156 static void rec_delete_single (Records p, Record rec)
158 struct record_index_entry entry;
160 rec_release_blocks (p, rec->sysno);
162 entry.next = p->head.index_free;
164 p->head.index_free = rec->sysno;
165 write_indx (p, rec->sysno, &entry, sizeof(entry));
168 static void rec_write_tmp_buf (Records p, int size, int *sysnos)
170 struct record_index_entry entry;
172 char *cptr = p->tmp_buf;
173 int block_prev = -1, block_free;
177 for (i = 1; i<REC_BLOCK_TYPES; i++)
178 if (size >= p->head.block_move[i])
180 while (no_written < size)
182 block_free = p->head.block_free[dst_type];
185 if (bf_read (p->data_BFile[dst_type],
186 block_free, 0, sizeof(*p->head.block_free),
187 &p->head.block_free[dst_type]) != 1)
189 logf (LOG_FATAL|LOG_ERRNO, "read in %s at free block %d",
190 p->data_fname[dst_type], block_free);
195 block_free = p->head.block_last[dst_type]++;
196 if (block_prev == -1)
198 entry.next = block_free*8 + dst_type;
200 p->head.total_bytes += size;
203 write_indx (p, *sysnos, &entry, sizeof(entry));
209 memcpy (cptr, &block_free, sizeof(int));
210 bf_write (p->data_BFile[dst_type], block_prev, 0, 0, cptr);
211 cptr = p->tmp_buf + no_written;
213 block_prev = block_free;
214 no_written += p->head.block_size[dst_type] - sizeof(int);
215 p->head.block_used[dst_type]++;
217 assert (block_prev != -1);
219 memcpy (cptr, &block_free, sizeof(int));
220 bf_write (p->data_BFile[dst_type], block_prev, 0,
221 sizeof(int) + (p->tmp_buf+size) - cptr, cptr);
224 Records rec_open (BFiles bfs, int rw, int compression_method)
230 p = (Records) xmalloc (sizeof(*p));
231 p->compression_method = compression_method;
234 p->tmp_buf = (char *) xmalloc (p->tmp_size);
235 p->index_fname = "reci";
236 p->index_BFile = bf_open (bfs, p->index_fname, 128, rw);
237 if (p->index_BFile == NULL)
239 logf (LOG_FATAL|LOG_ERRNO, "open %s", p->index_fname);
242 r = bf_read (p->index_BFile, 0, 0, 0, p->tmp_buf);
246 memcpy (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic));
247 sprintf (p->head.version, "%3d", REC_VERSION);
248 p->head.index_free = 0;
249 p->head.index_last = 1;
250 p->head.no_records = 0;
251 p->head.total_bytes = 0;
252 for (i = 0; i<REC_BLOCK_TYPES; i++)
254 p->head.block_free[i] = 0;
255 p->head.block_last[i] = 1;
256 p->head.block_used[i] = 0;
258 p->head.block_size[0] = 128;
259 p->head.block_move[0] = 0;
260 for (i = 1; i<REC_BLOCK_TYPES; i++)
262 p->head.block_size[i] = p->head.block_size[i-1] * 4;
263 p->head.block_move[i] = p->head.block_size[i] * 24;
269 memcpy (&p->head, p->tmp_buf, sizeof(p->head));
270 if (memcmp (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic)))
272 logf (LOG_FATAL, "file %s has bad format", p->index_fname);
275 version = atoi (p->head.version);
276 if (version != REC_VERSION)
278 logf (LOG_FATAL, "file %s is version %d, but version"
279 " %d is required", p->index_fname, 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] = bf_open (bfs, p->data_fname[i],
295 p->head.block_size[i],
298 logf (LOG_FATAL|LOG_ERRNO, "bf_open %s", p->data_fname[i]);
304 p->record_cache = (struct record_cache_entry *)
305 xmalloc (sizeof(*p->record_cache)*p->cache_max);
306 zebra_mutex_init (&p->mutex);
310 static void rec_encode_unsigned (unsigned n, unsigned char *buf, int *len)
315 buf[*len] = 128 + (n & 127);
323 static void rec_decode_unsigned(unsigned *np, unsigned char *buf, int *len)
329 while (buf[*len] > 127)
331 n += w*(buf[*len] & 127);
340 static void rec_cache_flush_block1 (Records p, Record rec, Record last_rec,
341 char **out_buf, int *out_size,
347 for (i = 0; i<REC_NO_INFO; i++)
349 if (*out_offset + (int) rec->size[i] + 20 > *out_size)
351 int new_size = *out_offset + rec->size[i] + 65536;
352 char *np = (char *) xmalloc (new_size);
354 memcpy (np, *out_buf, *out_offset);
356 *out_size = new_size;
361 rec_encode_unsigned (rec->sysno, *out_buf + *out_offset, &len);
362 (*out_offset) += len;
364 if (rec->size[i] == 0)
366 rec_encode_unsigned (1, *out_buf + *out_offset, &len);
367 (*out_offset) += len;
369 else if (last_rec && rec->size[i] == last_rec->size[i] &&
370 !memcmp (rec->info[i], last_rec->info[i], rec->size[i]))
372 rec_encode_unsigned (0, *out_buf + *out_offset, &len);
373 (*out_offset) += len;
377 rec_encode_unsigned (rec->size[i]+1, *out_buf + *out_offset, &len);
378 (*out_offset) += len;
379 memcpy (*out_buf + *out_offset, rec->info[i], rec->size[i]);
380 (*out_offset) += rec->size[i];
385 static void rec_write_multiple (Records p, int saveCount)
389 char compression_method;
393 char *out_buf = (char *) xmalloc (out_size);
394 int *sysnos = (int *) xmalloc (sizeof(*sysnos) * (p->cache_cur + 1));
395 int *sysnop = sysnos;
397 for (i = 0; i<p->cache_cur - saveCount; i++)
399 struct record_cache_entry *e = p->record_cache + i;
403 rec_cache_flush_block1 (p, e->rec, last_rec, &out_buf,
404 &out_size, &out_offset);
405 *sysnop++ = e->rec->sysno;
407 e->flag = recordFlagNop;
410 case recordFlagWrite:
411 rec_release_blocks (p, e->rec->sysno);
412 rec_cache_flush_block1 (p, e->rec, last_rec, &out_buf,
413 &out_size, &out_offset);
414 *sysnop++ = e->rec->sysno;
416 e->flag = recordFlagNop;
419 case recordFlagDelete:
420 rec_delete_single (p, e->rec);
421 e->flag = recordFlagNop;
431 int csize = 0; /* indicate compression "not performed yet" */
432 compression_method = p->compression_method;
433 switch (compression_method)
435 case REC_COMPRESS_BZIP2:
437 csize = out_offset + (out_offset >> 6) + 620;
438 rec_tmp_expand (p, csize);
439 #ifdef BZ_CONFIG_ERROR
440 i = BZ2_bzBuffToBuffCompress
442 i = bzBuffToBuffCompress
444 (p->tmp_buf+sizeof(int)+sizeof(short)+
446 &csize, out_buf, out_offset, 1, 0, 30);
449 logf (LOG_WARN, "bzBuffToBuffCompress error code=%d", i);
452 logf (LOG_LOG, "compress %4d %5d %5d", ref_count, out_offset,
456 case REC_COMPRESS_NONE:
461 /* either no compression or compression not supported ... */
463 rec_tmp_expand (p, csize);
464 memcpy (p->tmp_buf + sizeof(int) + sizeof(short) + sizeof(char),
465 out_buf, out_offset);
467 compression_method = REC_COMPRESS_NONE;
469 memcpy (p->tmp_buf + sizeof(int), &ref_count, sizeof(ref_count));
470 memcpy (p->tmp_buf + sizeof(int)+sizeof(short),
471 &compression_method, sizeof(compression_method));
473 /* -------- compression */
474 rec_write_tmp_buf (p, csize + sizeof(short) + sizeof(char), sysnos);
480 static void rec_cache_flush (Records p, int saveCount)
484 if (saveCount >= p->cache_cur)
487 rec_write_multiple (p, saveCount);
489 for (i = 0; i<p->cache_cur - saveCount; i++)
491 struct record_cache_entry *e = p->record_cache + i;
494 /* i still being used ... */
495 for (j = 0; j<saveCount; j++, i++)
496 memcpy (p->record_cache+j, p->record_cache+i,
497 sizeof(*p->record_cache));
498 p->cache_cur = saveCount;
501 static Record *rec_cache_lookup (Records p, int sysno,
502 enum recordCacheFlag flag)
505 for (i = 0; i<p->cache_cur; i++)
507 struct record_cache_entry *e = p->record_cache + i;
508 if (e->rec->sysno == sysno)
510 if (flag != recordFlagNop && e->flag == recordFlagNop)
518 static void rec_cache_insert (Records p, Record rec, enum recordCacheFlag flag)
520 struct record_cache_entry *e;
522 if (p->cache_cur == p->cache_max)
523 rec_cache_flush (p, 1);
524 else if (p->cache_cur > 0)
528 for (i = 0; i<p->cache_cur; i++)
530 Record r = (p->record_cache + i)->rec;
531 for (j = 0; j<REC_NO_INFO; j++)
535 rec_cache_flush (p, 1);
537 assert (p->cache_cur < p->cache_max);
539 e = p->record_cache + (p->cache_cur)++;
541 e->rec = rec_cp (rec);
544 void rec_close (Records *pp)
551 zebra_mutex_destroy (&p->mutex);
552 rec_cache_flush (p, 0);
553 xfree (p->record_cache);
559 bf_close (p->index_BFile);
561 for (i = 0; i<REC_BLOCK_TYPES; i++)
563 if (p->data_BFile[i])
564 bf_close (p->data_BFile[i]);
565 xfree (p->data_fname[i]);
572 static Record rec_get_int (Records p, int sysno)
576 struct record_index_entry entry;
577 int freeblock, dst_type;
584 char compression_method;
589 if ((recp = rec_cache_lookup (p, sysno, recordFlagNop)))
590 return rec_cp (*recp);
592 if (read_indx (p, sysno, &entry, sizeof(entry), 1) < 1)
593 return NULL; /* record is not there! */
596 return NULL; /* record is deleted */
598 dst_type = entry.next & 7;
599 assert (dst_type < REC_BLOCK_TYPES);
600 freeblock = entry.next / 8;
602 assert (freeblock > 0);
604 rec_tmp_expand (p, entry.size);
607 r = bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
610 memcpy (&freeblock, cptr, sizeof(freeblock));
616 cptr += p->head.block_size[dst_type] - sizeof(freeblock);
618 memcpy (&tmp, cptr, sizeof(tmp));
619 r = bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
622 memcpy (&freeblock, cptr, sizeof(freeblock));
623 memcpy (cptr, &tmp, sizeof(tmp));
626 rec = (Record) xmalloc (sizeof(*rec));
628 memcpy (&compression_method, p->tmp_buf + sizeof(int) + sizeof(short),
629 sizeof(compression_method));
630 in_buf = p->tmp_buf + sizeof(int) + sizeof(short) + sizeof(char);
631 in_size = entry.size - sizeof(short) - sizeof(char);
632 switch (compression_method)
634 case REC_COMPRESS_BZIP2:
636 bz_size = entry.size * 20 + 100;
639 bz_buf = (char *) xmalloc (bz_size);
640 #ifdef BZ_CONFIG_ERROR
641 i = BZ2_bzBuffToBuffDecompress
643 i = bzBuffToBuffDecompress
645 (bz_buf, &bz_size, in_buf, in_size, 0, 0);
646 logf (LOG_LOG, "decompress %5d %5d", in_size, bz_size);
649 logf (LOG_LOG, "failed");
656 logf (LOG_FATAL, "cannot decompress record(s) in BZIP2 format");
660 case REC_COMPRESS_NONE:
663 for (i = 0; i<REC_NO_INFO; i++)
666 nptr = in_buf; /* skip ref count */
667 while (nptr < in_buf + in_size)
671 rec_decode_unsigned (&this_sysno, nptr, &len);
674 for (i = 0; i < REC_NO_INFO; i++)
677 rec_decode_unsigned (&this_size, nptr, &len);
682 rec->size[i] = this_size-1;
687 nptr += rec->size[i];
692 if (this_sysno == sysno)
695 for (i = 0; i<REC_NO_INFO; i++)
697 if (rec->info[i] && rec->size[i])
699 char *np = xmalloc (rec->size[i]+1);
700 memcpy (np, rec->info[i], rec->size[i]);
701 np[rec->size[i]] = '\0';
706 assert (rec->info[i] == 0);
707 assert (rec->size[i] == 0);
711 rec_cache_insert (p, rec, recordFlagNop);
715 Record rec_get (Records p, int sysno)
718 zebra_mutex_lock (&p->mutex);
720 rec = rec_get_int (p, sysno);
721 zebra_mutex_unlock (&p->mutex);
725 static Record rec_new_int (Records p)
731 rec = (Record) xmalloc (sizeof(*rec));
732 if (1 || p->head.index_free == 0)
733 sysno = (p->head.index_last)++;
736 struct record_index_entry entry;
738 read_indx (p, p->head.index_free, &entry, sizeof(entry), 0);
739 sysno = p->head.index_free;
740 p->head.index_free = entry.next;
742 (p->head.no_records)++;
744 for (i = 0; i < REC_NO_INFO; i++)
749 rec_cache_insert (p, rec, recordFlagNew);
753 Record rec_new (Records p)
756 zebra_mutex_lock (&p->mutex);
758 rec = rec_new_int (p);
759 zebra_mutex_unlock (&p->mutex);
763 void rec_del (Records p, Record *recpp)
767 zebra_mutex_lock (&p->mutex);
768 (p->head.no_records)--;
769 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagDelete)))
776 rec_cache_insert (p, *recpp, recordFlagDelete);
779 zebra_mutex_unlock (&p->mutex);
783 void rec_put (Records p, Record *recpp)
787 zebra_mutex_lock (&p->mutex);
788 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagWrite)))
795 rec_cache_insert (p, *recpp, recordFlagWrite);
798 zebra_mutex_unlock (&p->mutex);
802 void rec_rm (Record *recpp)
808 for (i = 0; i < REC_NO_INFO; i++)
809 xfree ((*recpp)->info[i]);
814 Record rec_cp (Record rec)
819 n = (Record) xmalloc (sizeof(*n));
820 n->sysno = rec->sysno;
821 for (i = 0; i < REC_NO_INFO; i++)
829 n->size[i] = rec->size[i];
830 n->info[i] = (char *) xmalloc (rec->size[i]);
831 memcpy (n->info[i], rec->info[i], rec->size[i]);
837 char *rec_strdup (const char *s, size_t *len)
847 p = (char *) xmalloc (*len);