1 /* $Id: recindex.c,v 1.37 2004-08-18 17:02:05 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
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
25 * Format of first block
30 * Format of subsequent blocks
34 * Format of each record
36 * (length, data) - pairs
37 * length = 0 if same as previous
48 static void rec_write_head (Records p)
53 assert (p->index_BFile);
55 r = bf_write (p->index_BFile, 0, 0, sizeof(p->head), &p->head);
58 logf (LOG_FATAL|LOG_ERRNO, "write head of %s", p->index_fname);
63 static void rec_tmp_expand (Records p, int size)
65 if (p->tmp_size < size + 2048 ||
66 p->tmp_size < p->head.block_size[REC_BLOCK_TYPES-1]*2)
69 p->tmp_size = size + p->head.block_size[REC_BLOCK_TYPES-1]*2 + 2048;
70 p->tmp_buf = (char *) xmalloc (p->tmp_size);
74 static int read_indx (Records p, SYSNO sysno, void *buf, int itemsize,
78 zint pos = (sysno-1)*itemsize;
80 r = bf_read (p->index_BFile, 1+pos/128, (int) (pos%128), itemsize, buf);
81 if (r != 1 && !ignoreError)
83 logf (LOG_FATAL|LOG_ERRNO, "read in %s at pos %ld",
84 p->index_fname, (long) pos);
90 static void write_indx (Records p, SYSNO sysno, void *buf, int itemsize)
92 zint pos = (sysno-1)*itemsize;
94 bf_write (p->index_BFile, 1+pos/128, (int) (pos%128), itemsize, buf);
97 static void rec_release_blocks (Records p, SYSNO sysno)
99 struct record_index_entry entry;
101 char block_and_ref[sizeof(zint) + sizeof(short)];
105 if (read_indx (p, sysno, &entry, sizeof(entry), 1) != 1)
108 freeblock = entry.next;
109 assert (freeblock > 0);
110 dst_type = (int) (freeblock & 7);
111 assert (dst_type < REC_BLOCK_TYPES);
112 freeblock = freeblock / 8;
115 if (bf_read (p->data_BFile[dst_type], freeblock, 0,
116 first ? sizeof(block_and_ref) : sizeof(zint),
119 logf (LOG_FATAL|LOG_ERRNO, "read in rec_del_single");
125 memcpy (&ref, block_and_ref + sizeof(freeblock), sizeof(ref));
127 memcpy (block_and_ref + sizeof(freeblock), &ref, sizeof(ref));
130 if (bf_write (p->data_BFile[dst_type], freeblock, 0,
131 sizeof(block_and_ref), block_and_ref))
133 logf (LOG_FATAL|LOG_ERRNO, "write in rec_del_single");
141 if (bf_write (p->data_BFile[dst_type], freeblock, 0, sizeof(freeblock),
142 &p->head.block_free[dst_type]))
144 logf (LOG_FATAL|LOG_ERRNO, "write in rec_del_single");
147 p->head.block_free[dst_type] = freeblock;
148 memcpy (&freeblock, block_and_ref, sizeof(freeblock));
150 p->head.block_used[dst_type]--;
152 p->head.total_bytes -= entry.size;
155 static void rec_delete_single (Records p, Record rec)
157 struct record_index_entry entry;
159 rec_release_blocks (p, rec->sysno);
161 entry.next = p->head.index_free;
163 p->head.index_free = rec->sysno;
164 write_indx (p, rec->sysno, &entry, sizeof(entry));
167 static void rec_write_tmp_buf (Records p, int size, SYSNO *sysnos)
169 struct record_index_entry entry;
171 char *cptr = p->tmp_buf;
172 zint block_prev = -1, block_free;
176 for (i = 1; i<REC_BLOCK_TYPES; i++)
177 if (size >= p->head.block_move[i])
179 while (no_written < size)
181 block_free = p->head.block_free[dst_type];
184 if (bf_read (p->data_BFile[dst_type],
185 block_free, 0, sizeof(*p->head.block_free),
186 &p->head.block_free[dst_type]) != 1)
188 logf (LOG_FATAL|LOG_ERRNO, "read in %s at free block " ZINT_FORMAT,
189 p->data_fname[dst_type], block_free);
194 block_free = p->head.block_last[dst_type]++;
195 if (block_prev == -1)
197 entry.next = block_free*8 + dst_type;
199 p->head.total_bytes += size;
202 write_indx (p, *sysnos, &entry, sizeof(entry));
208 memcpy (cptr, &block_free, sizeof(block_free));
209 bf_write (p->data_BFile[dst_type], block_prev, 0, 0, cptr);
210 cptr = p->tmp_buf + no_written;
212 block_prev = block_free;
213 no_written += p->head.block_size[dst_type] - sizeof(zint);
214 p->head.block_used[dst_type]++;
216 assert (block_prev != -1);
218 memcpy (cptr, &block_free, sizeof(block_free));
219 bf_write (p->data_BFile[dst_type], block_prev, 0,
220 sizeof(block_free) + (p->tmp_buf+size) - cptr, cptr);
223 Records rec_open (BFiles bfs, int rw, int compression_method)
229 p = (Records) xmalloc (sizeof(*p));
230 p->compression_method = compression_method;
233 p->tmp_buf = (char *) xmalloc (p->tmp_size);
234 p->index_fname = "reci";
235 p->index_BFile = bf_open (bfs, p->index_fname, 128, rw);
236 if (p->index_BFile == NULL)
238 logf (LOG_FATAL|LOG_ERRNO, "open %s", p->index_fname);
241 r = bf_read (p->index_BFile, 0, 0, 0, p->tmp_buf);
245 memcpy (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic));
246 sprintf (p->head.version, "%3d", REC_VERSION);
247 p->head.index_free = 0;
248 p->head.index_last = 1;
249 p->head.no_records = 0;
250 p->head.total_bytes = 0;
251 for (i = 0; i<REC_BLOCK_TYPES; i++)
253 p->head.block_free[i] = 0;
254 p->head.block_last[i] = 1;
255 p->head.block_used[i] = 0;
257 p->head.block_size[0] = 128;
258 p->head.block_move[0] = 0;
259 for (i = 1; i<REC_BLOCK_TYPES; i++)
261 p->head.block_size[i] = p->head.block_size[i-1] * 4;
262 p->head.block_move[i] = p->head.block_size[i] * 24;
268 memcpy (&p->head, p->tmp_buf, sizeof(p->head));
269 if (memcmp (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic)))
271 logf (LOG_FATAL, "file %s has bad format", p->index_fname);
274 version = atoi (p->head.version);
275 if (version != REC_VERSION)
277 logf (LOG_FATAL, "file %s is version %d, but version"
278 " %d is required", p->index_fname, version, REC_VERSION);
283 for (i = 0; i<REC_BLOCK_TYPES; i++)
286 sprintf (str, "recd%c", i + 'A');
287 p->data_fname[i] = (char *) xmalloc (strlen(str)+1);
288 strcpy (p->data_fname[i], str);
289 p->data_BFile[i] = NULL;
291 for (i = 0; i<REC_BLOCK_TYPES; i++)
293 if (!(p->data_BFile[i] = bf_open (bfs, p->data_fname[i],
294 p->head.block_size[i],
297 logf (LOG_FATAL|LOG_ERRNO, "bf_open %s", p->data_fname[i]);
303 p->record_cache = (struct record_cache_entry *)
304 xmalloc (sizeof(*p->record_cache)*p->cache_max);
305 zebra_mutex_init (&p->mutex);
309 static void rec_encode_unsigned (unsigned n, unsigned char *buf, int *len)
314 buf[*len] = 128 + (n & 127);
322 static void rec_decode_unsigned(unsigned *np, unsigned char *buf, int *len)
328 while (buf[*len] > 127)
330 n += w*(buf[*len] & 127);
339 static void rec_encode_zint (zint n, unsigned char *buf, int *len)
344 buf[*len] = (unsigned) (128 + (n & 127));
348 buf[*len] = (unsigned) n;
352 static void rec_decode_zint(zint *np, unsigned char *buf, int *len)
358 while (buf[*len] > 127)
360 n += w*(buf[*len] & 127);
369 static void rec_cache_flush_block1 (Records p, Record rec, Record last_rec,
370 char **out_buf, int *out_size,
376 for (i = 0; i<REC_NO_INFO; i++)
378 if (*out_offset + (int) rec->size[i] + 20 > *out_size)
380 int new_size = *out_offset + rec->size[i] + 65536;
381 char *np = (char *) xmalloc (new_size);
383 memcpy (np, *out_buf, *out_offset);
385 *out_size = new_size;
390 rec_encode_zint (rec->sysno, *out_buf + *out_offset, &len);
391 (*out_offset) += len;
393 if (rec->size[i] == 0)
395 rec_encode_unsigned (1, *out_buf + *out_offset, &len);
396 (*out_offset) += len;
398 else if (last_rec && rec->size[i] == last_rec->size[i] &&
399 !memcmp (rec->info[i], last_rec->info[i], rec->size[i]))
401 rec_encode_unsigned (0, *out_buf + *out_offset, &len);
402 (*out_offset) += len;
406 rec_encode_unsigned (rec->size[i]+1, *out_buf + *out_offset, &len);
407 (*out_offset) += len;
408 memcpy (*out_buf + *out_offset, rec->info[i], rec->size[i]);
409 (*out_offset) += rec->size[i];
414 static void rec_write_multiple (Records p, int saveCount)
418 char compression_method;
422 char *out_buf = (char *) xmalloc (out_size);
423 SYSNO *sysnos = (SYSNO *) xmalloc (sizeof(*sysnos) * (p->cache_cur + 1));
424 SYSNO *sysnop = sysnos;
426 for (i = 0; i<p->cache_cur - saveCount; i++)
428 struct record_cache_entry *e = p->record_cache + i;
432 rec_cache_flush_block1 (p, e->rec, last_rec, &out_buf,
433 &out_size, &out_offset);
434 *sysnop++ = e->rec->sysno;
436 e->flag = recordFlagNop;
439 case recordFlagWrite:
440 rec_release_blocks (p, e->rec->sysno);
441 rec_cache_flush_block1 (p, e->rec, last_rec, &out_buf,
442 &out_size, &out_offset);
443 *sysnop++ = e->rec->sysno;
445 e->flag = recordFlagNop;
448 case recordFlagDelete:
449 rec_delete_single (p, e->rec);
450 e->flag = recordFlagNop;
460 int csize = 0; /* indicate compression "not performed yet" */
461 compression_method = p->compression_method;
462 switch (compression_method)
464 case REC_COMPRESS_BZIP2:
466 csize = out_offset + (out_offset >> 6) + 620;
467 rec_tmp_expand (p, csize);
468 #ifdef BZ_CONFIG_ERROR
469 i = BZ2_bzBuffToBuffCompress
471 i = bzBuffToBuffCompress
473 (p->tmp_buf+sizeof(zint)+sizeof(short)+
475 &csize, out_buf, out_offset, 1, 0, 30);
478 logf (LOG_WARN, "bzBuffToBuffCompress error code=%d", i);
481 logf (LOG_LOG, "compress %4d %5d %5d", ref_count, out_offset,
485 case REC_COMPRESS_NONE:
490 /* either no compression or compression not supported ... */
492 rec_tmp_expand (p, csize);
493 memcpy (p->tmp_buf + sizeof(zint) + sizeof(short) + sizeof(char),
494 out_buf, out_offset);
496 compression_method = REC_COMPRESS_NONE;
498 memcpy (p->tmp_buf + sizeof(zint), &ref_count, sizeof(ref_count));
499 memcpy (p->tmp_buf + sizeof(zint)+sizeof(short),
500 &compression_method, sizeof(compression_method));
502 /* -------- compression */
503 rec_write_tmp_buf (p, csize + sizeof(short) + sizeof(char), sysnos);
509 static void rec_cache_flush (Records p, int saveCount)
513 if (saveCount >= p->cache_cur)
516 rec_write_multiple (p, saveCount);
518 for (i = 0; i<p->cache_cur - saveCount; i++)
520 struct record_cache_entry *e = p->record_cache + i;
523 /* i still being used ... */
524 for (j = 0; j<saveCount; j++, i++)
525 memcpy (p->record_cache+j, p->record_cache+i,
526 sizeof(*p->record_cache));
527 p->cache_cur = saveCount;
530 static Record *rec_cache_lookup (Records p, SYSNO sysno,
531 enum recordCacheFlag flag)
534 for (i = 0; i<p->cache_cur; i++)
536 struct record_cache_entry *e = p->record_cache + i;
537 if (e->rec->sysno == sysno)
539 if (flag != recordFlagNop && e->flag == recordFlagNop)
547 static void rec_cache_insert (Records p, Record rec, enum recordCacheFlag flag)
549 struct record_cache_entry *e;
551 if (p->cache_cur == p->cache_max)
552 rec_cache_flush (p, 1);
553 else if (p->cache_cur > 0)
557 for (i = 0; i<p->cache_cur; i++)
559 Record r = (p->record_cache + i)->rec;
560 for (j = 0; j<REC_NO_INFO; j++)
564 rec_cache_flush (p, 1);
566 assert (p->cache_cur < p->cache_max);
568 e = p->record_cache + (p->cache_cur)++;
570 e->rec = rec_cp (rec);
573 void rec_close (Records *pp)
580 zebra_mutex_destroy (&p->mutex);
581 rec_cache_flush (p, 0);
582 xfree (p->record_cache);
588 bf_close (p->index_BFile);
590 for (i = 0; i<REC_BLOCK_TYPES; i++)
592 if (p->data_BFile[i])
593 bf_close (p->data_BFile[i]);
594 xfree (p->data_fname[i]);
601 static Record rec_get_int (Records p, SYSNO sysno)
605 struct record_index_entry entry;
614 char compression_method;
619 if ((recp = rec_cache_lookup (p, sysno, recordFlagNop)))
620 return rec_cp (*recp);
622 if (read_indx (p, sysno, &entry, sizeof(entry), 1) < 1)
623 return NULL; /* record is not there! */
626 return NULL; /* record is deleted */
628 dst_type = (int) (entry.next & 7);
629 assert (dst_type < REC_BLOCK_TYPES);
630 freeblock = entry.next / 8;
632 assert (freeblock > 0);
634 rec_tmp_expand (p, entry.size);
637 r = bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
640 memcpy (&freeblock, cptr, sizeof(freeblock));
646 cptr += p->head.block_size[dst_type] - sizeof(freeblock);
648 memcpy (&tmp, cptr, sizeof(tmp));
649 r = bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
652 memcpy (&freeblock, cptr, sizeof(freeblock));
653 memcpy (cptr, &tmp, sizeof(tmp));
656 rec = (Record) xmalloc (sizeof(*rec));
658 memcpy (&compression_method, p->tmp_buf + sizeof(zint) + sizeof(short),
659 sizeof(compression_method));
660 in_buf = p->tmp_buf + sizeof(zint) + sizeof(short) + sizeof(char);
661 in_size = entry.size - sizeof(short) - sizeof(char);
662 switch (compression_method)
664 case REC_COMPRESS_BZIP2:
666 bz_size = entry.size * 20 + 100;
669 bz_buf = (char *) xmalloc (bz_size);
670 #ifdef BZ_CONFIG_ERROR
671 i = BZ2_bzBuffToBuffDecompress
673 i = bzBuffToBuffDecompress
675 (bz_buf, &bz_size, in_buf, in_size, 0, 0);
676 logf (LOG_LOG, "decompress %5d %5d", in_size, bz_size);
679 logf (LOG_LOG, "failed");
686 logf (LOG_FATAL, "cannot decompress record(s) in BZIP2 format");
690 case REC_COMPRESS_NONE:
693 for (i = 0; i<REC_NO_INFO; i++)
696 nptr = in_buf; /* skip ref count */
697 while (nptr < in_buf + in_size)
701 rec_decode_zint (&this_sysno, nptr, &len);
704 for (i = 0; i < REC_NO_INFO; i++)
707 rec_decode_unsigned (&this_size, nptr, &len);
712 rec->size[i] = this_size-1;
717 nptr += rec->size[i];
722 if (this_sysno == sysno)
725 for (i = 0; i<REC_NO_INFO; i++)
727 if (rec->info[i] && rec->size[i])
729 char *np = xmalloc (rec->size[i]+1);
730 memcpy (np, rec->info[i], rec->size[i]);
731 np[rec->size[i]] = '\0';
736 assert (rec->info[i] == 0);
737 assert (rec->size[i] == 0);
741 rec_cache_insert (p, rec, recordFlagNop);
745 Record rec_get (Records p, SYSNO sysno)
748 zebra_mutex_lock (&p->mutex);
750 rec = rec_get_int (p, sysno);
751 zebra_mutex_unlock (&p->mutex);
755 static Record rec_new_int (Records p)
762 rec = (Record) xmalloc (sizeof(*rec));
763 if (1 || p->head.index_free == 0)
764 sysno = (p->head.index_last)++;
767 struct record_index_entry entry;
769 read_indx (p, p->head.index_free, &entry, sizeof(entry), 0);
770 sysno = p->head.index_free;
771 p->head.index_free = entry.next;
773 (p->head.no_records)++;
775 for (i = 0; i < REC_NO_INFO; i++)
780 rec_cache_insert (p, rec, recordFlagNew);
784 Record rec_new (Records p)
787 zebra_mutex_lock (&p->mutex);
789 rec = rec_new_int (p);
790 zebra_mutex_unlock (&p->mutex);
794 void rec_del (Records p, Record *recpp)
798 zebra_mutex_lock (&p->mutex);
799 (p->head.no_records)--;
800 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagDelete)))
807 rec_cache_insert (p, *recpp, recordFlagDelete);
810 zebra_mutex_unlock (&p->mutex);
814 void rec_put (Records p, Record *recpp)
818 zebra_mutex_lock (&p->mutex);
819 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagWrite)))
826 rec_cache_insert (p, *recpp, recordFlagWrite);
829 zebra_mutex_unlock (&p->mutex);
833 void rec_rm (Record *recpp)
839 for (i = 0; i < REC_NO_INFO; i++)
840 xfree ((*recpp)->info[i]);
845 Record rec_cp (Record rec)
850 n = (Record) xmalloc (sizeof(*n));
851 n->sysno = rec->sysno;
852 for (i = 0; i < REC_NO_INFO; i++)
860 n->size[i] = rec->size[i];
861 n->info[i] = (char *) xmalloc (rec->size[i]);
862 memcpy (n->info[i], rec->info[i], rec->size[i]);
868 char *rec_strdup (const char *s, size_t *len)
878 p = (char *) xmalloc (*len);