1 /* $Id: recindex.c,v 1.36 2004-08-06 12:28:22 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(short) + sizeof(zint)];
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 sizeof(block_and_ref), block_and_ref) != 1)
118 logf (LOG_FATAL|LOG_ERRNO, "read in rec_del_single");
124 memcpy (&ref, block_and_ref + sizeof(freeblock), sizeof(ref));
126 memcpy (block_and_ref + sizeof(freeblock), &ref, sizeof(ref));
129 if (bf_write (p->data_BFile[dst_type], freeblock, 0,
130 sizeof(block_and_ref), block_and_ref))
132 logf (LOG_FATAL|LOG_ERRNO, "write in rec_del_single");
140 if (bf_write (p->data_BFile[dst_type], freeblock, 0, sizeof(freeblock),
141 &p->head.block_free[dst_type]))
143 logf (LOG_FATAL|LOG_ERRNO, "write in rec_del_single");
146 p->head.block_free[dst_type] = freeblock;
147 memcpy (&freeblock, block_and_ref, sizeof(freeblock));
149 p->head.block_used[dst_type]--;
151 p->head.total_bytes -= entry.size;
154 static void rec_delete_single (Records p, Record rec)
156 struct record_index_entry entry;
158 rec_release_blocks (p, rec->sysno);
160 entry.next = p->head.index_free;
162 p->head.index_free = rec->sysno;
163 write_indx (p, rec->sysno, &entry, sizeof(entry));
166 static void rec_write_tmp_buf (Records p, int size, SYSNO *sysnos)
168 struct record_index_entry entry;
170 char *cptr = p->tmp_buf;
171 zint block_prev = -1, block_free;
175 for (i = 1; i<REC_BLOCK_TYPES; i++)
176 if (size >= p->head.block_move[i])
178 while (no_written < size)
180 block_free = p->head.block_free[dst_type];
183 if (bf_read (p->data_BFile[dst_type],
184 block_free, 0, sizeof(*p->head.block_free),
185 &p->head.block_free[dst_type]) != 1)
187 logf (LOG_FATAL|LOG_ERRNO, "read in %s at free block " ZINT_FORMAT,
188 p->data_fname[dst_type], block_free);
193 block_free = p->head.block_last[dst_type]++;
194 if (block_prev == -1)
196 entry.next = block_free*8 + dst_type;
198 p->head.total_bytes += size;
201 write_indx (p, *sysnos, &entry, sizeof(entry));
207 memcpy (cptr, &block_free, sizeof(block_free));
208 bf_write (p->data_BFile[dst_type], block_prev, 0, 0, cptr);
209 cptr = p->tmp_buf + no_written;
211 block_prev = block_free;
212 no_written += p->head.block_size[dst_type] - sizeof(zint);
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);
222 Records rec_open (BFiles bfs, int rw, int compression_method)
228 p = (Records) xmalloc (sizeof(*p));
229 p->compression_method = compression_method;
232 p->tmp_buf = (char *) xmalloc (p->tmp_size);
233 p->index_fname = "reci";
234 p->index_BFile = bf_open (bfs, p->index_fname, 128, rw);
235 if (p->index_BFile == NULL)
237 logf (LOG_FATAL|LOG_ERRNO, "open %s", p->index_fname);
240 r = bf_read (p->index_BFile, 0, 0, 0, p->tmp_buf);
244 memcpy (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic));
245 sprintf (p->head.version, "%3d", REC_VERSION);
246 p->head.index_free = 0;
247 p->head.index_last = 1;
248 p->head.no_records = 0;
249 p->head.total_bytes = 0;
250 for (i = 0; i<REC_BLOCK_TYPES; i++)
252 p->head.block_free[i] = 0;
253 p->head.block_last[i] = 1;
254 p->head.block_used[i] = 0;
256 p->head.block_size[0] = 128;
257 p->head.block_move[0] = 0;
258 for (i = 1; i<REC_BLOCK_TYPES; i++)
260 p->head.block_size[i] = p->head.block_size[i-1] * 4;
261 p->head.block_move[i] = p->head.block_size[i] * 24;
267 memcpy (&p->head, p->tmp_buf, sizeof(p->head));
268 if (memcmp (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic)))
270 logf (LOG_FATAL, "file %s has bad format", p->index_fname);
273 version = atoi (p->head.version);
274 if (version != REC_VERSION)
276 logf (LOG_FATAL, "file %s is version %d, but version"
277 " %d is required", p->index_fname, version, REC_VERSION);
282 for (i = 0; i<REC_BLOCK_TYPES; i++)
285 sprintf (str, "recd%c", i + 'A');
286 p->data_fname[i] = (char *) xmalloc (strlen(str)+1);
287 strcpy (p->data_fname[i], str);
288 p->data_BFile[i] = NULL;
290 for (i = 0; i<REC_BLOCK_TYPES; i++)
292 if (!(p->data_BFile[i] = bf_open (bfs, p->data_fname[i],
293 p->head.block_size[i],
296 logf (LOG_FATAL|LOG_ERRNO, "bf_open %s", p->data_fname[i]);
302 p->record_cache = (struct record_cache_entry *)
303 xmalloc (sizeof(*p->record_cache)*p->cache_max);
304 zebra_mutex_init (&p->mutex);
308 static void rec_encode_unsigned (unsigned n, unsigned char *buf, int *len)
313 buf[*len] = 128 + (n & 127);
321 static void rec_decode_unsigned(unsigned *np, unsigned char *buf, int *len)
327 while (buf[*len] > 127)
329 n += w*(buf[*len] & 127);
338 static void rec_encode_zint (zint n, unsigned char *buf, int *len)
343 buf[*len] = (unsigned) (128 + (n & 127));
347 buf[*len] = (unsigned) n;
351 static void rec_decode_zint(zint *np, unsigned char *buf, int *len)
357 while (buf[*len] > 127)
359 n += w*(buf[*len] & 127);
368 static void rec_cache_flush_block1 (Records p, Record rec, Record last_rec,
369 char **out_buf, int *out_size,
375 for (i = 0; i<REC_NO_INFO; i++)
377 if (*out_offset + (int) rec->size[i] + 20 > *out_size)
379 int new_size = *out_offset + rec->size[i] + 65536;
380 char *np = (char *) xmalloc (new_size);
382 memcpy (np, *out_buf, *out_offset);
384 *out_size = new_size;
389 rec_encode_zint (rec->sysno, *out_buf + *out_offset, &len);
390 (*out_offset) += len;
392 if (rec->size[i] == 0)
394 rec_encode_unsigned (1, *out_buf + *out_offset, &len);
395 (*out_offset) += len;
397 else if (last_rec && rec->size[i] == last_rec->size[i] &&
398 !memcmp (rec->info[i], last_rec->info[i], rec->size[i]))
400 rec_encode_unsigned (0, *out_buf + *out_offset, &len);
401 (*out_offset) += len;
405 rec_encode_unsigned (rec->size[i]+1, *out_buf + *out_offset, &len);
406 (*out_offset) += len;
407 memcpy (*out_buf + *out_offset, rec->info[i], rec->size[i]);
408 (*out_offset) += rec->size[i];
413 static void rec_write_multiple (Records p, int saveCount)
417 char compression_method;
421 char *out_buf = (char *) xmalloc (out_size);
422 SYSNO *sysnos = (SYSNO *) xmalloc (sizeof(*sysnos) * (p->cache_cur + 1));
423 SYSNO *sysnop = sysnos;
425 for (i = 0; i<p->cache_cur - saveCount; i++)
427 struct record_cache_entry *e = p->record_cache + i;
431 rec_cache_flush_block1 (p, e->rec, last_rec, &out_buf,
432 &out_size, &out_offset);
433 *sysnop++ = e->rec->sysno;
435 e->flag = recordFlagNop;
438 case recordFlagWrite:
439 rec_release_blocks (p, e->rec->sysno);
440 rec_cache_flush_block1 (p, e->rec, last_rec, &out_buf,
441 &out_size, &out_offset);
442 *sysnop++ = e->rec->sysno;
444 e->flag = recordFlagNop;
447 case recordFlagDelete:
448 rec_delete_single (p, e->rec);
449 e->flag = recordFlagNop;
459 int csize = 0; /* indicate compression "not performed yet" */
460 compression_method = p->compression_method;
461 switch (compression_method)
463 case REC_COMPRESS_BZIP2:
465 csize = out_offset + (out_offset >> 6) + 620;
466 rec_tmp_expand (p, csize);
467 #ifdef BZ_CONFIG_ERROR
468 i = BZ2_bzBuffToBuffCompress
470 i = bzBuffToBuffCompress
472 (p->tmp_buf+sizeof(zint)+sizeof(short)+
474 &csize, out_buf, out_offset, 1, 0, 30);
477 logf (LOG_WARN, "bzBuffToBuffCompress error code=%d", i);
480 logf (LOG_LOG, "compress %4d %5d %5d", ref_count, out_offset,
484 case REC_COMPRESS_NONE:
489 /* either no compression or compression not supported ... */
491 rec_tmp_expand (p, csize);
492 memcpy (p->tmp_buf + sizeof(zint) + sizeof(short) + sizeof(char),
493 out_buf, out_offset);
495 compression_method = REC_COMPRESS_NONE;
497 memcpy (p->tmp_buf + sizeof(zint), &ref_count, sizeof(ref_count));
498 memcpy (p->tmp_buf + sizeof(zint)+sizeof(short),
499 &compression_method, sizeof(compression_method));
501 /* -------- compression */
502 rec_write_tmp_buf (p, csize + sizeof(short) + sizeof(char), sysnos);
508 static void rec_cache_flush (Records p, int saveCount)
512 if (saveCount >= p->cache_cur)
515 rec_write_multiple (p, saveCount);
517 for (i = 0; i<p->cache_cur - saveCount; i++)
519 struct record_cache_entry *e = p->record_cache + i;
522 /* i still being used ... */
523 for (j = 0; j<saveCount; j++, i++)
524 memcpy (p->record_cache+j, p->record_cache+i,
525 sizeof(*p->record_cache));
526 p->cache_cur = saveCount;
529 static Record *rec_cache_lookup (Records p, SYSNO sysno,
530 enum recordCacheFlag flag)
533 for (i = 0; i<p->cache_cur; i++)
535 struct record_cache_entry *e = p->record_cache + i;
536 if (e->rec->sysno == sysno)
538 if (flag != recordFlagNop && e->flag == recordFlagNop)
546 static void rec_cache_insert (Records p, Record rec, enum recordCacheFlag flag)
548 struct record_cache_entry *e;
550 if (p->cache_cur == p->cache_max)
551 rec_cache_flush (p, 1);
552 else if (p->cache_cur > 0)
556 for (i = 0; i<p->cache_cur; i++)
558 Record r = (p->record_cache + i)->rec;
559 for (j = 0; j<REC_NO_INFO; j++)
563 rec_cache_flush (p, 1);
565 assert (p->cache_cur < p->cache_max);
567 e = p->record_cache + (p->cache_cur)++;
569 e->rec = rec_cp (rec);
572 void rec_close (Records *pp)
579 zebra_mutex_destroy (&p->mutex);
580 rec_cache_flush (p, 0);
581 xfree (p->record_cache);
587 bf_close (p->index_BFile);
589 for (i = 0; i<REC_BLOCK_TYPES; i++)
591 if (p->data_BFile[i])
592 bf_close (p->data_BFile[i]);
593 xfree (p->data_fname[i]);
600 static Record rec_get_int (Records p, SYSNO sysno)
604 struct record_index_entry entry;
613 char compression_method;
618 if ((recp = rec_cache_lookup (p, sysno, recordFlagNop)))
619 return rec_cp (*recp);
621 if (read_indx (p, sysno, &entry, sizeof(entry), 1) < 1)
622 return NULL; /* record is not there! */
625 return NULL; /* record is deleted */
627 dst_type = (int) (entry.next & 7);
628 assert (dst_type < REC_BLOCK_TYPES);
629 freeblock = entry.next / 8;
631 assert (freeblock > 0);
633 rec_tmp_expand (p, entry.size);
636 r = bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
639 memcpy (&freeblock, cptr, sizeof(freeblock));
645 cptr += p->head.block_size[dst_type] - sizeof(freeblock);
647 memcpy (&tmp, cptr, sizeof(tmp));
648 r = bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
651 memcpy (&freeblock, cptr, sizeof(freeblock));
652 memcpy (cptr, &tmp, sizeof(tmp));
655 rec = (Record) xmalloc (sizeof(*rec));
657 memcpy (&compression_method, p->tmp_buf + sizeof(zint) + sizeof(short),
658 sizeof(compression_method));
659 in_buf = p->tmp_buf + sizeof(zint) + sizeof(short) + sizeof(char);
660 in_size = entry.size - sizeof(short) - sizeof(char);
661 switch (compression_method)
663 case REC_COMPRESS_BZIP2:
665 bz_size = entry.size * 20 + 100;
668 bz_buf = (char *) xmalloc (bz_size);
669 #ifdef BZ_CONFIG_ERROR
670 i = BZ2_bzBuffToBuffDecompress
672 i = bzBuffToBuffDecompress
674 (bz_buf, &bz_size, in_buf, in_size, 0, 0);
675 logf (LOG_LOG, "decompress %5d %5d", in_size, bz_size);
678 logf (LOG_LOG, "failed");
685 logf (LOG_FATAL, "cannot decompress record(s) in BZIP2 format");
689 case REC_COMPRESS_NONE:
692 for (i = 0; i<REC_NO_INFO; i++)
695 nptr = in_buf; /* skip ref count */
696 while (nptr < in_buf + in_size)
700 rec_decode_zint (&this_sysno, nptr, &len);
703 for (i = 0; i < REC_NO_INFO; i++)
706 rec_decode_unsigned (&this_size, nptr, &len);
711 rec->size[i] = this_size-1;
716 nptr += rec->size[i];
721 if (this_sysno == sysno)
724 for (i = 0; i<REC_NO_INFO; i++)
726 if (rec->info[i] && rec->size[i])
728 char *np = xmalloc (rec->size[i]+1);
729 memcpy (np, rec->info[i], rec->size[i]);
730 np[rec->size[i]] = '\0';
735 assert (rec->info[i] == 0);
736 assert (rec->size[i] == 0);
740 rec_cache_insert (p, rec, recordFlagNop);
744 Record rec_get (Records p, SYSNO sysno)
747 zebra_mutex_lock (&p->mutex);
749 rec = rec_get_int (p, sysno);
750 zebra_mutex_unlock (&p->mutex);
754 static Record rec_new_int (Records p)
761 rec = (Record) xmalloc (sizeof(*rec));
762 if (1 || p->head.index_free == 0)
763 sysno = (p->head.index_last)++;
766 struct record_index_entry entry;
768 read_indx (p, p->head.index_free, &entry, sizeof(entry), 0);
769 sysno = p->head.index_free;
770 p->head.index_free = entry.next;
772 (p->head.no_records)++;
774 for (i = 0; i < REC_NO_INFO; i++)
779 rec_cache_insert (p, rec, recordFlagNew);
783 Record rec_new (Records p)
786 zebra_mutex_lock (&p->mutex);
788 rec = rec_new_int (p);
789 zebra_mutex_unlock (&p->mutex);
793 void rec_del (Records p, Record *recpp)
797 zebra_mutex_lock (&p->mutex);
798 (p->head.no_records)--;
799 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagDelete)))
806 rec_cache_insert (p, *recpp, recordFlagDelete);
809 zebra_mutex_unlock (&p->mutex);
813 void rec_put (Records p, Record *recpp)
817 zebra_mutex_lock (&p->mutex);
818 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagWrite)))
825 rec_cache_insert (p, *recpp, recordFlagWrite);
828 zebra_mutex_unlock (&p->mutex);
832 void rec_rm (Record *recpp)
838 for (i = 0; i < REC_NO_INFO; i++)
839 xfree ((*recpp)->info[i]);
844 Record rec_cp (Record rec)
849 n = (Record) xmalloc (sizeof(*n));
850 n->sysno = rec->sysno;
851 for (i = 0; i < REC_NO_INFO; i++)
859 n->size[i] = rec->size[i];
860 n->info[i] = (char *) xmalloc (rec->size[i]);
861 memcpy (n->info[i], rec->info[i], rec->size[i]);
867 char *rec_strdup (const char *s, size_t *len)
877 p = (char *) xmalloc (*len);