1 /* $Id: recindex.c,v 1.34.2.3 2006-08-14 10:38:59 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 this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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
51 static void rec_write_head (Records p)
56 assert (p->index_BFile);
58 r = bf_write (p->index_BFile, 0, 0, sizeof(p->head), &p->head);
61 logf (LOG_FATAL|LOG_ERRNO, "write head of %s", p->index_fname);
66 static void rec_tmp_expand (Records p, int size)
68 if (p->tmp_size < size + 2048 ||
69 p->tmp_size < p->head.block_size[REC_BLOCK_TYPES-1]*2)
72 p->tmp_size = size + p->head.block_size[REC_BLOCK_TYPES-1]*2 + 2048;
73 p->tmp_buf = (char *) xmalloc (p->tmp_size);
77 static int read_indx (Records p, int sysno, void *buf, int itemsize,
81 int pos = (sysno-1)*itemsize;
83 r = bf_read (p->index_BFile, 1+pos/128, pos%128, itemsize, buf);
84 if (r != 1 && !ignoreError)
86 logf (LOG_FATAL|LOG_ERRNO, "read in %s at pos %ld",
87 p->index_fname, (long) pos);
93 static void write_indx (Records p, int sysno, void *buf, int itemsize)
95 int pos = (sysno-1)*itemsize;
97 bf_write (p->index_BFile, 1+pos/128, pos%128, itemsize, buf);
100 static void rec_release_blocks (Records p, int sysno)
102 struct record_index_entry entry;
104 char block_and_ref[sizeof(int) + sizeof(short)];
108 if (read_indx (p, sysno, &entry, sizeof(entry), 1) != 1)
111 freeblock = entry.next;
112 assert (freeblock > 0);
113 dst_type = freeblock & 7;
114 assert (dst_type < REC_BLOCK_TYPES);
115 freeblock = freeblock / 8;
118 if (bf_read (p->data_BFile[dst_type], freeblock, 0,
119 first ? sizeof(block_and_ref) : sizeof(int),
122 logf (LOG_FATAL|LOG_ERRNO, "read in rec_del_single");
128 memcpy (&ref, block_and_ref + sizeof(int), sizeof(ref));
130 memcpy (block_and_ref + sizeof(int), &ref, sizeof(ref));
133 if (bf_write (p->data_BFile[dst_type], freeblock, 0,
134 sizeof(block_and_ref), block_and_ref))
136 logf (LOG_FATAL|LOG_ERRNO, "write in rec_del_single");
144 if (bf_write (p->data_BFile[dst_type], freeblock, 0, sizeof(freeblock),
145 &p->head.block_free[dst_type]))
147 logf (LOG_FATAL|LOG_ERRNO, "write in rec_del_single");
150 p->head.block_free[dst_type] = freeblock;
151 memcpy (&freeblock, block_and_ref, sizeof(int));
153 p->head.block_used[dst_type]--;
155 p->head.total_bytes -= entry.size;
158 static void rec_delete_single (Records p, Record rec)
160 struct record_index_entry entry;
162 rec_release_blocks (p, rec->sysno);
164 entry.next = p->head.index_free;
166 p->head.index_free = rec->sysno;
167 write_indx (p, rec->sysno, &entry, sizeof(entry));
170 static void rec_write_tmp_buf (Records p, int size, int *sysnos)
172 struct record_index_entry entry;
174 char *cptr = p->tmp_buf;
175 int block_prev = -1, block_free;
179 for (i = 1; i<REC_BLOCK_TYPES; i++)
180 if (size >= p->head.block_move[i])
182 while (no_written < size)
184 block_free = p->head.block_free[dst_type];
187 if (bf_read (p->data_BFile[dst_type],
188 block_free, 0, sizeof(*p->head.block_free),
189 &p->head.block_free[dst_type]) != 1)
191 logf (LOG_FATAL|LOG_ERRNO, "read in %s at free block %d",
192 p->data_fname[dst_type], block_free);
197 block_free = p->head.block_last[dst_type]++;
198 if (block_prev == -1)
200 entry.next = block_free*8 + dst_type;
202 p->head.total_bytes += size;
205 write_indx (p, *sysnos, &entry, sizeof(entry));
211 memcpy (cptr, &block_free, sizeof(int));
212 bf_write (p->data_BFile[dst_type], block_prev, 0, 0, cptr);
213 cptr = p->tmp_buf + no_written;
215 block_prev = block_free;
216 no_written += p->head.block_size[dst_type] - sizeof(int);
217 p->head.block_used[dst_type]++;
219 assert (block_prev != -1);
221 memcpy (cptr, &block_free, sizeof(int));
222 bf_write (p->data_BFile[dst_type], block_prev, 0,
223 sizeof(int) + (p->tmp_buf+size) - cptr, cptr);
226 Records rec_open (BFiles bfs, int rw, int compression_method)
232 p = (Records) xmalloc (sizeof(*p));
233 p->compression_method = compression_method;
236 p->tmp_buf = (char *) xmalloc (p->tmp_size);
237 p->index_fname = "reci";
238 p->index_BFile = bf_open (bfs, p->index_fname, 128, rw);
239 if (p->index_BFile == NULL)
241 logf (LOG_FATAL|LOG_ERRNO, "open %s", p->index_fname);
244 r = bf_read (p->index_BFile, 0, 0, 0, p->tmp_buf);
248 memcpy (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic));
249 sprintf (p->head.version, "%3d", REC_VERSION);
250 p->head.index_free = 0;
251 p->head.index_last = 1;
252 p->head.no_records = 0;
253 p->head.total_bytes = 0;
254 for (i = 0; i<REC_BLOCK_TYPES; i++)
256 p->head.block_free[i] = 0;
257 p->head.block_last[i] = 1;
258 p->head.block_used[i] = 0;
260 p->head.block_size[0] = 128;
261 p->head.block_move[0] = 0;
262 for (i = 1; i<REC_BLOCK_TYPES; i++)
264 p->head.block_size[i] = p->head.block_size[i-1] * 4;
265 p->head.block_move[i] = p->head.block_size[i] * 24;
271 memcpy (&p->head, p->tmp_buf, sizeof(p->head));
272 if (memcmp (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic)))
274 logf (LOG_FATAL, "file %s has bad format", p->index_fname);
277 version = atoi (p->head.version);
278 if (version != REC_VERSION)
280 logf (LOG_FATAL, "file %s is version %d, but version"
281 " %d is required", p->index_fname, version, REC_VERSION);
286 for (i = 0; i<REC_BLOCK_TYPES; i++)
289 sprintf (str, "recd%c", i + 'A');
290 p->data_fname[i] = (char *) xmalloc (strlen(str)+1);
291 strcpy (p->data_fname[i], str);
292 p->data_BFile[i] = NULL;
294 for (i = 0; i<REC_BLOCK_TYPES; i++)
296 if (!(p->data_BFile[i] = bf_open (bfs, p->data_fname[i],
297 p->head.block_size[i],
300 logf (LOG_FATAL|LOG_ERRNO, "bf_open %s", p->data_fname[i]);
306 p->record_cache = (struct record_cache_entry *)
307 xmalloc (sizeof(*p->record_cache)*p->cache_max);
308 zebra_mutex_init (&p->mutex);
312 static void rec_encode_unsigned (unsigned n, unsigned char *buf, int *len)
317 buf[*len] = 128 + (n & 127);
325 static void rec_decode_unsigned(unsigned *np, unsigned char *buf, int *len)
331 while (buf[*len] > 127)
333 n += w*(buf[*len] & 127);
342 static void rec_cache_flush_block1 (Records p, Record rec, Record last_rec,
343 char **out_buf, int *out_size,
349 for (i = 0; i<REC_NO_INFO; i++)
351 if (*out_offset + (int) rec->size[i] + 20 > *out_size)
353 int new_size = *out_offset + rec->size[i] + 65536;
354 char *np = (char *) xmalloc (new_size);
356 memcpy (np, *out_buf, *out_offset);
358 *out_size = new_size;
363 rec_encode_unsigned (rec->sysno, *out_buf + *out_offset, &len);
364 (*out_offset) += len;
366 if (rec->size[i] == 0)
368 rec_encode_unsigned (1, *out_buf + *out_offset, &len);
369 (*out_offset) += len;
371 else if (last_rec && rec->size[i] == last_rec->size[i] &&
372 !memcmp (rec->info[i], last_rec->info[i], rec->size[i]))
374 rec_encode_unsigned (0, *out_buf + *out_offset, &len);
375 (*out_offset) += len;
379 rec_encode_unsigned (rec->size[i]+1, *out_buf + *out_offset, &len);
380 (*out_offset) += len;
381 memcpy (*out_buf + *out_offset, rec->info[i], rec->size[i]);
382 (*out_offset) += rec->size[i];
387 static void rec_write_multiple (Records p, int saveCount)
391 char compression_method;
395 char *out_buf = (char *) xmalloc (out_size);
396 int *sysnos = (int *) xmalloc (sizeof(*sysnos) * (p->cache_cur + 1));
397 int *sysnop = sysnos;
399 for (i = 0; i<p->cache_cur - saveCount; i++)
401 struct record_cache_entry *e = p->record_cache + i;
405 rec_cache_flush_block1 (p, e->rec, last_rec, &out_buf,
406 &out_size, &out_offset);
407 *sysnop++ = e->rec->sysno;
409 e->flag = recordFlagNop;
412 case recordFlagWrite:
413 rec_release_blocks (p, e->rec->sysno);
414 rec_cache_flush_block1 (p, e->rec, last_rec, &out_buf,
415 &out_size, &out_offset);
416 *sysnop++ = e->rec->sysno;
418 e->flag = recordFlagNop;
421 case recordFlagDelete:
422 rec_delete_single (p, e->rec);
423 e->flag = recordFlagNop;
433 int csize = 0; /* indicate compression "not performed yet" */
434 compression_method = p->compression_method;
435 switch (compression_method)
437 case REC_COMPRESS_BZIP2:
439 csize = out_offset + (out_offset >> 6) + 620;
440 rec_tmp_expand (p, csize);
441 #ifdef BZ_CONFIG_ERROR
442 i = BZ2_bzBuffToBuffCompress
444 i = bzBuffToBuffCompress
446 (p->tmp_buf+sizeof(int)+sizeof(short)+
448 &csize, out_buf, out_offset, 1, 0, 30);
451 logf (LOG_WARN, "bzBuffToBuffCompress error code=%d", i);
454 logf (LOG_LOG, "compress %4d %5d %5d", ref_count, out_offset,
458 case REC_COMPRESS_NONE:
463 /* either no compression or compression not supported ... */
465 rec_tmp_expand (p, csize);
466 memcpy (p->tmp_buf + sizeof(int) + sizeof(short) + sizeof(char),
467 out_buf, out_offset);
469 compression_method = REC_COMPRESS_NONE;
471 memcpy (p->tmp_buf + sizeof(int), &ref_count, sizeof(ref_count));
472 memcpy (p->tmp_buf + sizeof(int)+sizeof(short),
473 &compression_method, sizeof(compression_method));
475 /* -------- compression */
476 rec_write_tmp_buf (p, csize + sizeof(short) + sizeof(char), sysnos);
482 static void rec_cache_flush (Records p, int saveCount)
486 if (saveCount >= p->cache_cur)
489 rec_write_multiple (p, saveCount);
491 for (i = 0; i<p->cache_cur - saveCount; i++)
493 struct record_cache_entry *e = p->record_cache + i;
496 /* i still being used ... */
497 for (j = 0; j<saveCount; j++, i++)
498 memcpy (p->record_cache+j, p->record_cache+i,
499 sizeof(*p->record_cache));
500 p->cache_cur = saveCount;
503 static Record *rec_cache_lookup (Records p, int sysno,
504 enum recordCacheFlag flag)
507 for (i = 0; i<p->cache_cur; i++)
509 struct record_cache_entry *e = p->record_cache + i;
510 if (e->rec->sysno == sysno)
512 if (flag != recordFlagNop && e->flag == recordFlagNop)
520 static void rec_cache_insert (Records p, Record rec, enum recordCacheFlag flag)
522 struct record_cache_entry *e;
524 if (p->cache_cur == p->cache_max)
525 rec_cache_flush (p, 1);
526 else if (p->cache_cur > 0)
530 for (i = 0; i<p->cache_cur; i++)
532 Record r = (p->record_cache + i)->rec;
533 for (j = 0; j<REC_NO_INFO; j++)
537 rec_cache_flush (p, 1);
539 assert (p->cache_cur < p->cache_max);
541 e = p->record_cache + (p->cache_cur)++;
543 e->rec = rec_cp (rec);
546 void rec_close (Records *pp)
553 zebra_mutex_destroy (&p->mutex);
554 rec_cache_flush (p, 0);
555 xfree (p->record_cache);
561 bf_close (p->index_BFile);
563 for (i = 0; i<REC_BLOCK_TYPES; i++)
565 if (p->data_BFile[i])
566 bf_close (p->data_BFile[i]);
567 xfree (p->data_fname[i]);
574 static Record rec_get_int (Records p, int sysno)
578 struct record_index_entry entry;
579 int freeblock, dst_type;
586 char compression_method;
591 if ((recp = rec_cache_lookup (p, sysno, recordFlagNop)))
592 return rec_cp (*recp);
594 if (read_indx (p, sysno, &entry, sizeof(entry), 1) < 1)
595 return NULL; /* record is not there! */
598 return NULL; /* record is deleted */
600 dst_type = entry.next & 7;
601 assert (dst_type < REC_BLOCK_TYPES);
602 freeblock = entry.next / 8;
604 assert (freeblock > 0);
606 rec_tmp_expand (p, entry.size);
609 r = bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
612 memcpy (&freeblock, cptr, sizeof(freeblock));
618 cptr += p->head.block_size[dst_type] - sizeof(freeblock);
620 memcpy (&tmp, cptr, sizeof(tmp));
621 r = bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
624 memcpy (&freeblock, cptr, sizeof(freeblock));
625 memcpy (cptr, &tmp, sizeof(tmp));
628 rec = (Record) xmalloc (sizeof(*rec));
630 memcpy (&compression_method, p->tmp_buf + sizeof(int) + sizeof(short),
631 sizeof(compression_method));
632 in_buf = p->tmp_buf + sizeof(int) + sizeof(short) + sizeof(char);
633 in_size = entry.size - sizeof(short) - sizeof(char);
634 switch (compression_method)
636 case REC_COMPRESS_BZIP2:
638 bz_size = entry.size * 20 + 100;
641 bz_buf = (char *) xmalloc (bz_size);
642 #ifdef BZ_CONFIG_ERROR
643 i = BZ2_bzBuffToBuffDecompress
645 i = bzBuffToBuffDecompress
647 (bz_buf, &bz_size, in_buf, in_size, 0, 0);
648 logf (LOG_LOG, "decompress %5d %5d", in_size, bz_size);
651 logf (LOG_LOG, "failed");
658 logf (LOG_FATAL, "cannot decompress record(s) in BZIP2 format");
662 case REC_COMPRESS_NONE:
665 for (i = 0; i<REC_NO_INFO; i++)
668 nptr = in_buf; /* skip ref count */
669 while (nptr < in_buf + in_size)
673 rec_decode_unsigned (&this_sysno, nptr, &len);
676 for (i = 0; i < REC_NO_INFO; i++)
679 rec_decode_unsigned (&this_size, nptr, &len);
684 rec->size[i] = this_size-1;
689 nptr += rec->size[i];
694 if (this_sysno == sysno)
697 for (i = 0; i<REC_NO_INFO; i++)
699 if (rec->info[i] && rec->size[i])
701 char *np = xmalloc (rec->size[i]+1);
702 memcpy (np, rec->info[i], rec->size[i]);
703 np[rec->size[i]] = '\0';
708 assert (rec->info[i] == 0);
709 assert (rec->size[i] == 0);
713 rec_cache_insert (p, rec, recordFlagNop);
717 Record rec_get (Records p, int sysno)
720 zebra_mutex_lock (&p->mutex);
722 rec = rec_get_int (p, sysno);
723 zebra_mutex_unlock (&p->mutex);
727 static Record rec_new_int (Records p)
733 rec = (Record) xmalloc (sizeof(*rec));
734 if (1 || p->head.index_free == 0)
735 sysno = (p->head.index_last)++;
738 struct record_index_entry entry;
740 read_indx (p, p->head.index_free, &entry, sizeof(entry), 0);
741 sysno = p->head.index_free;
742 p->head.index_free = entry.next;
744 (p->head.no_records)++;
746 for (i = 0; i < REC_NO_INFO; i++)
751 rec_cache_insert (p, rec, recordFlagNew);
755 Record rec_new (Records p)
758 zebra_mutex_lock (&p->mutex);
760 rec = rec_new_int (p);
761 zebra_mutex_unlock (&p->mutex);
765 void rec_del (Records p, Record *recpp)
769 zebra_mutex_lock (&p->mutex);
770 (p->head.no_records)--;
771 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagDelete)))
778 rec_cache_insert (p, *recpp, recordFlagDelete);
781 zebra_mutex_unlock (&p->mutex);
785 void rec_put (Records p, Record *recpp)
789 zebra_mutex_lock (&p->mutex);
790 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagWrite)))
797 rec_cache_insert (p, *recpp, recordFlagWrite);
800 zebra_mutex_unlock (&p->mutex);
804 void rec_rm (Record *recpp)
810 for (i = 0; i < REC_NO_INFO; i++)
811 xfree ((*recpp)->info[i]);
816 Record rec_cp (Record rec)
821 n = (Record) xmalloc (sizeof(*n));
822 n->sysno = rec->sysno;
823 for (i = 0; i < REC_NO_INFO; i++)
831 n->size[i] = rec->size[i];
832 n->info[i] = (char *) xmalloc (rec->size[i]);
833 memcpy (n->info[i], rec->info[i], rec->size[i]);
839 char *rec_strdup (const char *s, size_t *len)
849 p = (char *) xmalloc (*len);