2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.9 1995-11-30 08:34:33 adam
8 * Started work on commit facility.
9 * Changed a few malloc/free to xmalloc/xfree.
11 * Revision 1.8 1995/11/28 14:26:21 adam
12 * Bug fix: recordId with constant wasn't right.
13 * Bug fix: recordId dictionary entry wasn't deleted when needed.
15 * Revision 1.7 1995/11/28 09:09:43 adam
16 * Zebra config renamed.
17 * Use setting 'recordId' to identify record now.
18 * Bug fix in recindex.c: rec_release_blocks was invokeded even
19 * though the blocks were already released.
20 * File traversal properly deletes records when needed.
22 * Revision 1.6 1995/11/25 10:24:06 adam
23 * More record fields - they are enumerated now.
24 * New options: flagStoreData flagStoreKey.
26 * Revision 1.5 1995/11/22 17:19:18 adam
27 * Record management uses the bfile system.
29 * Revision 1.4 1995/11/20 16:59:46 adam
30 * New update method: the 'old' keys are saved for each records.
32 * Revision 1.3 1995/11/16 15:34:55 adam
33 * Uses new record management system in both indexer and server.
35 * Revision 1.2 1995/11/15 19:13:08 adam
36 * Work on record management.
38 * Revision 1.1 1995/11/15 14:46:20 adam
39 * Started work on better record management system.
57 #define REC_BLOCK_TYPES 2
58 #define REC_HEAD_MAGIC "recindx"
67 char *data_fname[REC_BLOCK_TYPES];
68 BFile data_BFile[REC_BLOCK_TYPES];
73 struct record_cache_entry *record_cache;
80 int block_size[REC_BLOCK_TYPES];
81 int block_free[REC_BLOCK_TYPES];
82 int block_last[REC_BLOCK_TYPES];
83 int block_used[REC_BLOCK_TYPES];
84 int block_move[REC_BLOCK_TYPES];
93 enum recordCacheFlag { recordFlagNop, recordFlagWrite, recordFlagNew,
96 struct record_cache_entry {
98 enum recordCacheFlag flag;
101 struct record_index_entry {
114 static void rec_write_head (Records p)
119 assert (p->index_BFile);
121 r = bf_write (p->index_BFile, 0, 0, sizeof(p->head), &p->head);
124 logf (LOG_FATAL|LOG_ERRNO, "write head of %s", p->index_fname);
129 static void rec_tmp_expand (Records p, int size, int dst_type)
131 if (p->tmp_size < size + 256 ||
132 p->tmp_size < p->head.block_size[dst_type]*2)
135 p->tmp_size = size + p->head.block_size[dst_type]*2 + 256;
136 p->tmp_buf = xmalloc (p->tmp_size);
140 static int read_indx (Records p, int sysno, void *buf, int itemsize,
144 int pos = (sysno-1)*itemsize;
146 r = bf_read (p->index_BFile, 1+pos/128, pos%128, itemsize, buf);
147 if (r != 1 && !ignoreError)
149 logf (LOG_FATAL|LOG_ERRNO, "read in %s at pos %ld",
150 p->index_fname, (long) pos);
157 static void write_indx (Records p, int sysno, void *buf, int itemsize)
159 int pos = (sysno-1)*itemsize;
161 bf_write (p->index_BFile, 1+pos/128, pos%128, itemsize, buf);
164 static void rec_release_blocks (Records p, int sysno)
166 struct record_index_entry entry;
167 int freeblock, freenext;
170 if (read_indx (p, sysno, &entry, sizeof(entry), 1) != 1)
172 freeblock = entry.u.used.next;
173 assert (freeblock > 0);
174 dst_type = freeblock & 7;
175 assert (dst_type < REC_BLOCK_TYPES);
176 freeblock = freeblock / 8;
179 if (bf_read (p->data_BFile[dst_type], freeblock, 0, sizeof(freenext),
182 logf (LOG_FATAL|LOG_ERRNO, "read in rec_del_single");
185 if (bf_write (p->data_BFile[dst_type], freeblock, 0, sizeof(freenext),
186 &p->head.block_free[dst_type]))
188 logf (LOG_FATAL|LOG_ERRNO, "write in rec_del_single");
191 p->head.block_free[dst_type] = freeblock;
192 freeblock = freenext;
193 p->head.block_used[dst_type]--;
197 static void rec_delete_single (Records p, Record rec)
199 struct record_index_entry entry;
201 rec_release_blocks (p, rec->sysno);
203 entry.u.free.next = p->head.index_free;
204 p->head.index_free = rec->sysno;
205 write_indx (p, rec->sysno, &entry, sizeof(entry));
209 static void rec_write_single (Records p, Record rec)
215 int block_prev = -1, block_free;
216 struct record_index_entry entry;
218 for (i = 0; i < REC_NO_INFO; i++)
220 size += sizeof(*rec->size);
222 size += sizeof(*rec->size) + rec->size[i];
224 for (i = 1; i<REC_BLOCK_TYPES; i++)
225 if (size >= p->head.block_move[i])
228 rec_tmp_expand (p, size, dst_type);
230 cptr = p->tmp_buf + sizeof(int); /* a hack! */
231 for (i = 0; i < REC_NO_INFO; i++)
233 memcpy (cptr, &rec->size[i], sizeof(*rec->size));
234 cptr += sizeof(*rec->size);
237 memcpy (cptr, rec->info[i], rec->size[i]);
238 cptr += rec->size[i];
242 while (no_written < size)
244 block_free = p->head.block_free[dst_type];
247 if (bf_read (p->data_BFile[dst_type],
248 block_free, 0, sizeof(*p->head.block_free),
249 &p->head.block_free[dst_type]) != 1)
251 logf (LOG_FATAL|LOG_ERRNO, "read in %s at free block %d",
252 p->data_fname[dst_type], block_free);
256 block_free = p->head.block_last[dst_type]++;
257 if (block_prev == -1)
259 entry.u.used.next = block_free*8 + dst_type;
260 entry.u.used.size = size;
262 write_indx (p, rec->sysno, &entry, sizeof(entry));
266 memcpy (cptr, &block_free, sizeof(int));
267 bf_write (p->data_BFile[dst_type], block_prev, 0, 0, cptr);
268 cptr = p->tmp_buf + no_written;
270 block_prev = block_free;
271 no_written += p->head.block_size[dst_type] - sizeof(int);
272 p->head.block_used[dst_type]++;
274 assert (block_prev != -1);
276 memcpy (cptr, &block_free, sizeof(int));
277 bf_write (p->data_BFile[dst_type], block_prev, 0,
278 sizeof(int) + (p->tmp_buf+size) - cptr, cptr);
281 static void rec_update_single (Records p, Record rec)
283 rec_release_blocks (p, rec->sysno);
284 rec_write_single (p, rec);
287 Records rec_open (int rw)
292 p = xmalloc (sizeof(*p));
295 p->tmp_buf = xmalloc (p->tmp_size);
296 p->index_fname = "recindex";
297 p->index_BFile = bf_open (p->index_fname, 128, rw);
298 if (p->index_BFile == NULL)
300 logf (LOG_FATAL|LOG_ERRNO, "open %s", p->index_fname);
303 r = bf_read (p->index_BFile, 0, 0, 0, p->tmp_buf);
307 memcpy (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic));
308 p->head.index_free = 0;
309 p->head.index_last = 1;
310 p->head.no_records = 0;
311 for (i = 0; i<REC_BLOCK_TYPES; i++)
313 p->head.block_free[i] = 0;
314 p->head.block_last[i] = 1;
315 p->head.block_used[i] = 0;
317 p->head.block_size[0] = 128;
318 p->head.block_move[0] = 0;
319 for (i = 1; i<REC_BLOCK_TYPES; i++)
321 p->head.block_size[i] = p->head.block_size[i-1] * 4;
322 p->head.block_move[i] = p->head.block_size[i] * 3;
328 memcpy (&p->head, p->tmp_buf, sizeof(p->head));
329 if (memcmp (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic)))
331 logf (LOG_FATAL, "read %s. bad header", p->index_fname);
336 for (i = 0; i<REC_BLOCK_TYPES; i++)
339 sprintf (str, "recdata%c", i + 'A');
340 p->data_fname[i] = xmalloc (strlen(str)+1);
341 strcpy (p->data_fname[i], str);
342 p->data_BFile[i] = NULL;
344 for (i = 0; i<REC_BLOCK_TYPES; i++)
346 if (!(p->data_BFile[i] = bf_open (p->data_fname[i],
347 p->head.block_size[i],
350 logf (LOG_FATAL|LOG_ERRNO, "bf_open %s", p->data_fname[i]);
356 p->record_cache = xmalloc (sizeof(*p->record_cache)*p->cache_max);
360 static void rec_cache_flush (Records p)
363 for (i = 0; i<p->cache_cur; i++)
365 struct record_cache_entry *e = p->record_cache + i;
371 rec_write_single (p, e->rec);
373 case recordFlagWrite:
374 rec_update_single (p, e->rec);
376 case recordFlagDelete:
377 rec_delete_single (p, e->rec);
385 static Record *rec_cache_lookup (Records p, int sysno,
386 enum recordCacheFlag flag)
389 for (i = 0; i<p->cache_cur; i++)
391 struct record_cache_entry *e = p->record_cache + i;
392 if (e->rec->sysno == sysno)
394 if (flag != recordFlagNop && e->flag == recordFlagNop)
402 static void rec_cache_insert (Records p, Record rec, enum recordCacheFlag flag)
404 struct record_cache_entry *e;
406 if (p->cache_cur == p->cache_max)
408 assert (p->cache_cur < p->cache_max);
410 e = p->record_cache + (p->cache_cur)++;
412 e->rec = rec_cp (rec);
415 void rec_close (Records *pp)
423 xfree (p->record_cache);
429 bf_close (p->index_BFile);
431 for (i = 0; i<REC_BLOCK_TYPES; i++)
433 if (p->data_BFile[i])
434 bf_close (p->data_BFile[i]);
435 xfree (p->data_fname[i]);
443 Record rec_get (Records p, int sysno)
447 struct record_index_entry entry;
448 int freeblock, dst_type;
454 if ((recp = rec_cache_lookup (p, sysno, recordFlagNop)))
455 return rec_cp (*recp);
457 read_indx (p, sysno, &entry, sizeof(entry), 0);
459 dst_type = entry.u.used.next & 7;
460 assert (dst_type < REC_BLOCK_TYPES);
461 freeblock = entry.u.used.next / 8;
463 assert (freeblock > 0);
465 rec = xmalloc (sizeof(*rec));
466 rec_tmp_expand (p, entry.u.used.size, dst_type);
469 bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
470 memcpy (&freeblock, cptr, sizeof(freeblock));
476 cptr += p->head.block_size[dst_type] - sizeof(freeblock);
478 memcpy (&tmp, cptr, sizeof(tmp));
479 bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
480 memcpy (&freeblock, cptr, sizeof(freeblock));
481 memcpy (cptr, &tmp, sizeof(tmp));
485 nptr = p->tmp_buf + sizeof(freeblock);
486 for (i = 0; i < REC_NO_INFO; i++)
488 memcpy (&rec->size[i], nptr, sizeof(*rec->size));
489 nptr += sizeof(*rec->size);
492 rec->info[i] = xmalloc (rec->size[i]);
493 memcpy (rec->info[i], nptr, rec->size[i]);
494 nptr += rec->size[i];
499 rec_cache_insert (p, rec, recordFlagNop);
503 Record rec_new (Records p)
509 rec = xmalloc (sizeof(*rec));
510 if (p->head.index_free == 0)
511 sysno = (p->head.index_last)++;
514 struct record_index_entry entry;
516 read_indx (p, p->head.index_free, &entry, sizeof(entry), 0);
517 sysno = p->head.index_free;
518 p->head.index_free = entry.u.free.next;
520 (p->head.no_records)++;
522 for (i = 0; i < REC_NO_INFO; i++)
527 rec_cache_insert (p, rec, recordFlagNew);
531 void rec_del (Records p, Record *recpp)
535 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagDelete)))
542 rec_cache_insert (p, *recpp, recordFlagDelete);
548 void rec_put (Records p, Record *recpp)
552 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagWrite)))
559 rec_cache_insert (p, *recpp, recordFlagWrite);
565 void rec_rm (Record *recpp)
568 for (i = 0; i < REC_NO_INFO; i++)
569 xfree ((*recpp)->info[i]);
574 Record rec_cp (Record rec)
579 n = xmalloc (sizeof(*n));
580 n->sysno = rec->sysno;
581 for (i = 0; i < REC_NO_INFO; i++)
589 n->size[i] = rec->size[i];
590 n->info[i] = xmalloc (rec->size[i]);
591 memcpy (n->info[i], rec->info[i], rec->size[i]);
596 /* no BF --------------------------------------------------- */
599 struct records_info {
605 struct records_head {
619 struct record_cache_entry *record_cache;
622 struct record_cache_entry {
627 struct record_index_entry {
639 #define REC_HEAD_MAGIC "rechead"
641 static void rec_write_head (Records p)
646 assert (p->index_fd != -1);
647 if (lseek (p->index_fd, (off_t) 0, SEEK_SET) == -1)
649 logf (LOG_FATAL|LOG_ERRNO, "lseek to 0 in %s", p->index_fname);
652 r = write (p->index_fd, &p->head, sizeof(p->head));
656 logf (LOG_FATAL|LOG_ERRNO, "write head of %s", p->index_fname);
658 case sizeof(p->head):
661 logf (LOG_FATAL, "write head of %s. wrote %d", p->index_fname, r);
666 Records rec_open (int rw)
671 p = xmalloc (sizeof(*p));
675 p->data_fname = "recdata";
677 p->index_fname = "recindex";
678 p->index_fd = open (p->index_fname,
679 rw ? (O_RDWR|O_CREAT) : O_RDONLY, 0666);
680 if (p->index_fd == -1)
682 logf (LOG_FATAL|LOG_ERRNO, "open %s", p->index_fname);
685 r = read (p->index_fd, &p->head, sizeof(p->head));
689 logf (LOG_FATAL|LOG_ERRNO, "read %s", p->index_fname);
692 memcpy (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic));
693 p->head.index_free = 0;
694 p->head.index_last = 1;
695 p->head.no_records = 0;
696 p->head.data_size = 0;
697 p->head.data_slack = 0;
698 p->head.data_used = 0;
702 case sizeof(p->head):
703 if (memcmp (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic)))
705 logf (LOG_FATAL, "read %s. bad header", p->index_fname);
710 logf (LOG_FATAL, "read head of %s. expected %d. got %d",
711 p->index_fname, sizeof(p->head), r);
714 p->data_fd = open (p->data_fname,
715 rw ? (O_RDWR|O_CREAT) : O_RDONLY, 0666);
716 if (p->data_fd == -1)
718 logf (LOG_FATAL|LOG_ERRNO, "open %s", p->data_fname);
723 p->record_cache = xmalloc (sizeof(*p->record_cache)*p->cache_max));
727 static void read_indx (Records p, int sysno, void *buf, int itemsize)
730 off_t pos = (sysno-1)*itemsize + sizeof(p->head);
732 if (lseek (p->index_fd, pos, SEEK_SET) == (pos) -1)
734 logf (LOG_FATAL|LOG_ERRNO, "seek in %s to pos %ld",
735 p->index_fname, (long) pos);
738 r = read (p->index_fd, buf, itemsize);
742 logf (LOG_FATAL|LOG_ERRNO, "read in %s at pos %ld",
743 p->index_fname, (long) pos);
745 logf (LOG_FATAL, "read in %s at pos %ld",
746 p->index_fname, (long) pos);
751 static void rec_write_single (Records p, Record rec)
753 struct record_index_entry entry;
754 int r, i, size = 0, got;
756 off_t pos = (rec->sysno-1)*sizeof(entry) + sizeof(p->head);
758 for (i = 0; i < REC_NO_INFO; i++)
760 size += sizeof(*rec->size);
762 size += sizeof(*rec->size) + rec->size[i];
764 entry.u.used.offset = p->head.data_size;
765 entry.u.used.size = size;
766 p->head.data_size += size;
767 p->head.data_used += size;
769 if (lseek (p->index_fd, pos, SEEK_SET) == (pos) -1)
771 logf (LOG_FATAL|LOG_ERRNO, "seek in %s to pos %ld",
772 p->index_fname, (long) pos);
775 r = write (p->index_fd, &entry, sizeof(entry));
776 if (r != sizeof(entry))
779 logf (LOG_FATAL|LOG_ERRNO, "write of %s at pos %ld",
780 p->index_fname, (long) pos);
782 logf (LOG_FATAL, "write of %s at pos %ld",
783 p->index_fname, (long) pos);
786 if (lseek (p->data_fd, entry.u.used.offset, SEEK_SET) == -1)
788 logf (LOG_FATAL|LOG_ERRNO, "lseek in %s to pos %ld",
789 p->data_fname, entry.u.used.offset);
792 if (p->tmp_size < entry.u.used.size)
795 p->tmp_size = entry.u.used.size + 16384;
796 p->tmp_buf = xmalloc (p->tmp_size));
799 for (i = 0; i < REC_NO_INFO; i++)
801 memcpy (cptr, &rec->size[i], sizeof(*rec->size));
802 cptr += sizeof(*rec->size);
805 memcpy (cptr, rec->info[i], rec->size[i]);
806 cptr += rec->size[i];
809 for (got = 0; got < entry.u.used.size; got += r)
811 r = write (p->data_fd, p->tmp_buf + got, entry.u.used.size - got);
814 logf (LOG_FATAL|LOG_ERRNO, "write of %s", p->data_fname);
820 static void rec_cache_flush (Records p)
823 for (i = 0; i<p->cache_cur; i++)
825 struct record_cache_entry *e = p->record_cache + i;
827 rec_write_single (p, e->rec);
833 static Record *rec_cache_lookup (Records p, int sysno, int dirty)
836 for (i = 0; i<p->cache_cur; i++)
838 struct record_cache_entry *e = p->record_cache + i;
839 if (e->rec->sysno == sysno)
849 static void rec_cache_insert (Records p, Record rec, int dirty)
851 struct record_cache_entry *e;
853 if (p->cache_cur == p->cache_max)
855 assert (p->cache_cur < p->cache_max);
857 e = p->record_cache + (p->cache_cur)++;
859 e->rec = rec_cp (rec);
862 void rec_close (Records *p)
866 rec_cache_flush (*p);
867 xfree ((*p)->record_cache);
872 if ((*p)->index_fd != -1)
873 close ((*p)->index_fd);
875 if ((*p)->data_fd != -1)
876 close ((*p)->data_fd);
878 xfree ((*p)->tmp_buf);
884 Record rec_get (Records p, int sysno)
888 struct record_index_entry entry;
895 if ((recp = rec_cache_lookup (p, sysno, 0)))
896 return rec_cp (*recp);
898 read_indx (p, sysno, &entry, sizeof(entry));
900 rec = xmalloc (sizeof(*rec));
901 if (lseek (p->data_fd, entry.u.used.offset, SEEK_SET) == -1)
903 logf (LOG_FATAL|LOG_ERRNO, "lseek in %s to pos %ld",
904 p->data_fname, entry.u.used.offset);
907 if (p->tmp_size < entry.u.used.size)
910 p->tmp_size = entry.u.used.size + 16384;
911 p->tmp_buf = xmalloc (p->tmp_size));
913 for (got = 0; got < entry.u.used.size; got += r)
915 r = read (p->data_fd, p->tmp_buf + got, entry.u.used.size - got);
918 logf (LOG_FATAL|LOG_ERRNO, "read of %s", p->data_fname);
925 for (i = 0; i < REC_NO_INFO; i++)
927 memcpy (&rec->size[i], nptr, sizeof(*rec->size));
928 nptr += sizeof(*rec->size);
931 rec->info[i] = xmalloc (rec->size[i]);
932 memcpy (rec->info[i], nptr, rec->size[i]);
933 nptr += rec->size[i];
938 rec_cache_insert (p, rec, 0);
942 Record rec_new (Records p)
948 rec = xmalloc (sizeof(*rec));
949 if (p->head.index_free == 0)
950 sysno = (p->head.index_last)++;
953 struct record_index_entry entry;
955 read_indx (p, p->head.index_free, &entry, sizeof(entry));
956 sysno = p->head.index_free;
957 p->head.index_free = entry.u.free.next;
959 (p->head.no_records)++;
961 for (i = 0; i < REC_NO_INFO; i++)
966 rec_cache_insert (p, rec, 1);
970 void rec_put (Records p, Record *recpp)
974 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, 1)))
981 rec_cache_insert (p, *recpp, 1);
987 void rec_rm (Record *recpp)
990 for (i = 0; i < REC_NO_INFO; i++)
991 xfree ((*recpp)->info[i]);
996 Record rec_cp (Record rec)
1001 n = xmalloc (sizeof(*n));
1002 n->sysno = rec->sysno;
1003 for (i = 0; i < REC_NO_INFO; i++)
1011 n->size[i] = rec->size[i];
1012 n->info[i] = xmalloc (rec->size[i]);
1013 memcpy (n->info[i], rec->info[i], rec->size[i]);
1018 void rec_del (Records p, Record *recpp)
1026 char *rec_strdup (const char *s, size_t *len)