2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.6 1995-11-25 10:24:06 adam
8 * More record fields - they are enumerated now.
9 * New options: flagStoreData flagStoreKey.
11 * Revision 1.5 1995/11/22 17:19:18 adam
12 * Record management uses the bfile system.
14 * Revision 1.4 1995/11/20 16:59:46 adam
15 * New update method: the 'old' keys are saved for each records.
17 * Revision 1.3 1995/11/16 15:34:55 adam
18 * Uses new record management system in both indexer and server.
20 * Revision 1.2 1995/11/15 19:13:08 adam
21 * Work on record management.
23 * Revision 1.1 1995/11/15 14:46:20 adam
24 * Started work on better record management system.
42 #define REC_BLOCK_TYPES 2
43 #define REC_HEAD_MAGIC "recindx"
52 char *data_fname[REC_BLOCK_TYPES];
53 BFile data_BFile[REC_BLOCK_TYPES];
58 struct record_cache_entry *record_cache;
65 int block_size[REC_BLOCK_TYPES];
66 int block_free[REC_BLOCK_TYPES];
67 int block_last[REC_BLOCK_TYPES];
68 int block_used[REC_BLOCK_TYPES];
69 int block_move[REC_BLOCK_TYPES];
78 enum recordCacheFlag { recordFlagNop, recordFlagWrite, recordFlagDelete };
80 struct record_cache_entry {
82 enum recordCacheFlag flag;
85 struct record_index_entry {
98 static void rec_write_head (Records p)
103 assert (p->index_BFile);
105 r = bf_write (p->index_BFile, 0, 0, sizeof(p->head), &p->head);
108 logf (LOG_FATAL|LOG_ERRNO, "write head of %s", p->index_fname);
113 static void rec_tmp_expand (Records p, int size, int dst_type)
115 if (p->tmp_size < size + 256 ||
116 p->tmp_size < p->head.block_size[dst_type]*2)
119 p->tmp_size = size + p->head.block_size[dst_type]*2 +
121 if (!(p->tmp_buf = malloc (p->tmp_size)))
123 logf (LOG_FATAL|LOG_ERRNO, "malloc");
129 static int read_indx (Records p, int sysno, void *buf, int itemsize,
133 int pos = (sysno-1)*itemsize;
135 r = bf_read (p->index_BFile, 1+pos/128, pos%128, itemsize, buf);
136 if (r != 1 && !ignoreError)
138 logf (LOG_FATAL|LOG_ERRNO, "read in %s at pos %ld",
139 p->index_fname, (long) pos);
146 static void write_indx (Records p, int sysno, void *buf, int itemsize)
148 int pos = (sysno-1)*itemsize;
150 bf_write (p->index_BFile, 1+pos/128, pos%128, itemsize, buf);
153 static void rec_release_blocks (Records p, int sysno)
155 struct record_index_entry entry;
156 int freeblock, freenext;
159 if (read_indx (p, sysno, &entry, sizeof(entry), 1) != 1)
161 freeblock = entry.u.used.next;
162 assert (freeblock > 0);
163 dst_type = freeblock & 7;
164 freeblock = freeblock / 8;
167 if (bf_read (p->data_BFile[dst_type], freeblock, 0, sizeof(freenext),
170 logf (LOG_FATAL|LOG_ERRNO, "read in rec_del_single");
173 if (bf_write (p->data_BFile[dst_type], freeblock, 0, sizeof(freenext),
174 &p->head.block_free[dst_type]))
176 logf (LOG_FATAL|LOG_ERRNO, "write in rec_del_single");
179 p->head.block_free[dst_type] = freeblock;
180 freeblock = freenext;
181 p->head.block_used[dst_type]--;
185 static void rec_delete_single (Records p, Record rec)
187 struct record_index_entry entry;
189 rec_release_blocks (p, rec->sysno);
191 entry.u.free.next = p->head.index_free;
192 p->head.index_free = rec->sysno;
193 write_indx (p, rec->sysno, &entry, sizeof(entry));
196 static void rec_write_single (Records p, Record rec)
202 int block_prev = -1, block_free;
203 struct record_index_entry entry;
205 rec_release_blocks (p, rec->sysno);
207 for (i = 0; i < REC_NO_INFO; i++)
209 size += sizeof(*rec->size);
211 size += sizeof(*rec->size) + rec->size[i];
213 for (i = 1; i<REC_BLOCK_TYPES; i++)
214 if (size >= p->head.block_move[i])
217 rec_tmp_expand (p, size, dst_type);
219 cptr = p->tmp_buf + sizeof(int); /* a hack! */
220 for (i = 0; i < REC_NO_INFO; i++)
222 memcpy (cptr, &rec->size[i], sizeof(*rec->size));
223 cptr += sizeof(*rec->size);
226 memcpy (cptr, rec->info[i], rec->size[i]);
227 cptr += rec->size[i];
231 while (no_written < size)
233 block_free = p->head.block_free[dst_type];
236 if (bf_read (p->data_BFile[dst_type],
237 block_free, 0, sizeof(*p->head.block_free),
238 &p->head.block_free[dst_type]) != 1)
240 logf (LOG_FATAL|LOG_ERRNO, "read in %s at free block %d",
241 p->data_fname[dst_type], block_free);
245 block_free = p->head.block_last[dst_type]++;
246 if (block_prev == -1)
248 entry.u.used.next = block_free*8 + dst_type;
249 entry.u.used.size = size;
251 write_indx (p, rec->sysno, &entry, sizeof(entry));
255 memcpy (cptr, &block_free, sizeof(int));
256 logf (LOG_LOG, "writing block %d (1)", block_prev);
257 bf_write (p->data_BFile[dst_type], block_prev, 0, 0, cptr);
258 cptr = p->tmp_buf + no_written;
260 block_prev = block_free;
261 no_written += p->head.block_size[dst_type] - sizeof(int);
262 p->head.block_used[dst_type]++;
264 assert (block_prev != -1);
266 memcpy (cptr, &block_free, sizeof(int));
267 logf (LOG_LOG, "writing block %d (2) dst=%d", block_prev, dst_type);
268 bf_write (p->data_BFile[dst_type], block_prev, 0,
269 sizeof(int) + (p->tmp_buf+size) - cptr, cptr);
273 Records rec_open (int rw)
278 if (!(p = malloc (sizeof(*p))))
280 logf (LOG_FATAL|LOG_ERRNO, "malloc");
285 p->tmp_buf = malloc (p->tmp_size);
288 logf (LOG_FATAL|LOG_ERRNO, "malloc");
291 p->index_fname = "recindex";
292 p->index_BFile = bf_open (p->index_fname, 128, rw);
293 if (p->index_BFile == NULL)
295 logf (LOG_FATAL|LOG_ERRNO, "open %s", p->index_fname);
298 r = bf_read (p->index_BFile, 0, 0, 0, p->tmp_buf);
302 memcpy (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic));
303 p->head.index_free = 0;
304 p->head.index_last = 1;
305 p->head.no_records = 0;
306 for (i = 0; i<REC_BLOCK_TYPES; i++)
308 p->head.block_free[i] = 0;
309 p->head.block_last[i] = 1;
310 p->head.block_used[i] = 0;
312 p->head.block_size[0] = 128;
313 p->head.block_move[0] = 0;
314 for (i = 1; i<REC_BLOCK_TYPES; i++)
316 p->head.block_size[i] = p->head.block_size[i-1] * 4;
317 p->head.block_move[i] = p->head.block_size[i] * 3;
323 memcpy (&p->head, p->tmp_buf, sizeof(p->head));
324 if (memcmp (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic)))
326 logf (LOG_FATAL, "read %s. bad header", p->index_fname);
331 for (i = 0; i<REC_BLOCK_TYPES; i++)
334 sprintf (str, "recdata%d", i);
335 p->data_fname[i] = malloc (strlen(str)+1);
336 strcpy (p->data_fname[i], str);
337 p->data_BFile[i] = NULL;
339 for (i = 0; i<REC_BLOCK_TYPES; i++)
341 if (!(p->data_BFile[i] = bf_open (p->data_fname[i],
342 p->head.block_size[i],
345 logf (LOG_FATAL|LOG_ERRNO, "bf_open %s", p->data_fname[i]);
351 if (!(p->record_cache = malloc (sizeof(*p->record_cache)*p->cache_max)))
353 logf (LOG_FATAL|LOG_ERRNO, "malloc");
359 static void rec_cache_flush (Records p)
362 for (i = 0; i<p->cache_cur; i++)
364 struct record_cache_entry *e = p->record_cache + i;
369 case recordFlagWrite:
370 rec_write_single (p, e->rec);
372 case recordFlagDelete:
373 rec_delete_single (p, e->rec);
381 static Record *rec_cache_lookup (Records p, int sysno,
382 enum recordCacheFlag flag)
385 for (i = 0; i<p->cache_cur; i++)
387 struct record_cache_entry *e = p->record_cache + i;
388 if (e->rec->sysno == sysno)
390 if (flag != recordFlagNop)
398 static void rec_cache_insert (Records p, Record rec, enum recordCacheFlag flag)
400 struct record_cache_entry *e;
402 if (p->cache_cur == p->cache_max)
404 assert (p->cache_cur < p->cache_max);
406 e = p->record_cache + (p->cache_cur)++;
408 e->rec = rec_cp (rec);
411 void rec_close (Records *pp)
419 free (p->record_cache);
425 bf_close (p->index_BFile);
427 for (i = 0; i<REC_BLOCK_TYPES; i++)
429 if (p->data_BFile[i])
430 bf_close (p->data_BFile[i]);
431 free (p->data_fname[i]);
439 Record rec_get (Records p, int sysno)
443 struct record_index_entry entry;
444 int freeblock, dst_type;
450 if ((recp = rec_cache_lookup (p, sysno, recordFlagNop)))
451 return rec_cp (*recp);
453 read_indx (p, sysno, &entry, sizeof(entry), 0);
455 dst_type = entry.u.used.next & 7;
456 freeblock = entry.u.used.next / 8;
458 assert (freeblock > 0);
460 if (!(rec = malloc (sizeof(*rec))))
462 logf (LOG_FATAL|LOG_ERRNO, "malloc");
465 rec_tmp_expand (p, entry.u.used.size, dst_type);
468 bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
469 memcpy (&freeblock, cptr, sizeof(freeblock));
475 cptr += p->head.block_size[dst_type] - sizeof(freeblock);
477 memcpy (&tmp, cptr, sizeof(tmp));
478 bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
479 memcpy (&freeblock, cptr, sizeof(freeblock));
480 memcpy (cptr, &tmp, sizeof(tmp));
484 nptr = p->tmp_buf + sizeof(freeblock);
485 for (i = 0; i < REC_NO_INFO; i++)
487 memcpy (&rec->size[i], nptr, sizeof(*rec->size));
488 nptr += sizeof(*rec->size);
491 rec->info[i] = malloc (rec->size[i]);
492 memcpy (rec->info[i], nptr, rec->size[i]);
493 nptr += rec->size[i];
498 rec_cache_insert (p, rec, recordFlagNop);
502 Record rec_new (Records p)
508 if (!(rec = malloc (sizeof(*rec))))
510 logf (LOG_FATAL|LOG_ERRNO, "malloc");
513 if (p->head.index_free == 0)
514 sysno = (p->head.index_last)++;
517 struct record_index_entry entry;
519 read_indx (p, p->head.index_free, &entry, sizeof(entry), 0);
520 sysno = p->head.index_free;
521 p->head.index_free = entry.u.free.next;
523 (p->head.no_records)++;
525 for (i = 0; i < REC_NO_INFO; i++)
530 rec_cache_insert (p, rec, recordFlagWrite);
534 void rec_del (Records p, Record *recpp)
538 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagDelete)))
545 rec_cache_insert (p, *recpp, recordFlagDelete);
551 void rec_put (Records p, Record *recpp)
555 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagWrite)))
562 rec_cache_insert (p, *recpp, recordFlagWrite);
568 void rec_rm (Record *recpp)
571 for (i = 0; i < REC_NO_INFO; i++)
572 free ((*recpp)->info[i]);
577 Record rec_cp (Record rec)
582 if (!(n = malloc (sizeof(*n))))
584 logf (LOG_FATAL|LOG_ERRNO, "malloc");
587 n->sysno = rec->sysno;
588 for (i = 0; i < REC_NO_INFO; i++)
596 n->size[i] = rec->size[i];
597 if (!(n->info[i] = malloc (rec->size[i])))
599 logf (LOG_FATAL|LOG_ERRNO, "malloc. rec_cp");
602 memcpy (n->info[i], rec->info[i], rec->size[i]);
607 /* no BF --------------------------------------------------- */
610 struct records_info {
616 struct records_head {
630 struct record_cache_entry *record_cache;
633 struct record_cache_entry {
638 struct record_index_entry {
650 #define REC_HEAD_MAGIC "rechead"
652 static void rec_write_head (Records p)
657 assert (p->index_fd != -1);
658 if (lseek (p->index_fd, (off_t) 0, SEEK_SET) == -1)
660 logf (LOG_FATAL|LOG_ERRNO, "lseek to 0 in %s", p->index_fname);
663 r = write (p->index_fd, &p->head, sizeof(p->head));
667 logf (LOG_FATAL|LOG_ERRNO, "write head of %s", p->index_fname);
669 case sizeof(p->head):
672 logf (LOG_FATAL, "write head of %s. wrote %d", p->index_fname, r);
677 Records rec_open (int rw)
682 if (!(p = malloc (sizeof(*p))))
684 logf (LOG_FATAL|LOG_ERRNO, "malloc");
690 p->data_fname = "recdata";
692 p->index_fname = "recindex";
693 p->index_fd = open (p->index_fname,
694 rw ? (O_RDWR|O_CREAT) : O_RDONLY, 0666);
695 if (p->index_fd == -1)
697 logf (LOG_FATAL|LOG_ERRNO, "open %s", p->index_fname);
700 r = read (p->index_fd, &p->head, sizeof(p->head));
704 logf (LOG_FATAL|LOG_ERRNO, "read %s", p->index_fname);
707 memcpy (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic));
708 p->head.index_free = 0;
709 p->head.index_last = 1;
710 p->head.no_records = 0;
711 p->head.data_size = 0;
712 p->head.data_slack = 0;
713 p->head.data_used = 0;
717 case sizeof(p->head):
718 if (memcmp (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic)))
720 logf (LOG_FATAL, "read %s. bad header", p->index_fname);
725 logf (LOG_FATAL, "read head of %s. expected %d. got %d",
726 p->index_fname, sizeof(p->head), r);
729 p->data_fd = open (p->data_fname,
730 rw ? (O_RDWR|O_CREAT) : O_RDONLY, 0666);
731 if (p->data_fd == -1)
733 logf (LOG_FATAL|LOG_ERRNO, "open %s", p->data_fname);
738 if (!(p->record_cache = malloc (sizeof(*p->record_cache)*p->cache_max)))
740 logf (LOG_FATAL|LOG_ERRNO, "malloc");
746 static void read_indx (Records p, int sysno, void *buf, int itemsize)
749 off_t pos = (sysno-1)*itemsize + sizeof(p->head);
751 if (lseek (p->index_fd, pos, SEEK_SET) == (pos) -1)
753 logf (LOG_FATAL|LOG_ERRNO, "seek in %s to pos %ld",
754 p->index_fname, (long) pos);
757 r = read (p->index_fd, buf, itemsize);
761 logf (LOG_FATAL|LOG_ERRNO, "read in %s at pos %ld",
762 p->index_fname, (long) pos);
764 logf (LOG_FATAL, "read in %s at pos %ld",
765 p->index_fname, (long) pos);
770 static void rec_write_single (Records p, Record rec)
772 struct record_index_entry entry;
773 int r, i, size = 0, got;
775 off_t pos = (rec->sysno-1)*sizeof(entry) + sizeof(p->head);
777 for (i = 0; i < REC_NO_INFO; i++)
779 size += sizeof(*rec->size);
781 size += sizeof(*rec->size) + rec->size[i];
783 entry.u.used.offset = p->head.data_size;
784 entry.u.used.size = size;
785 p->head.data_size += size;
786 p->head.data_used += size;
788 if (lseek (p->index_fd, pos, SEEK_SET) == (pos) -1)
790 logf (LOG_FATAL|LOG_ERRNO, "seek in %s to pos %ld",
791 p->index_fname, (long) pos);
794 r = write (p->index_fd, &entry, sizeof(entry));
795 if (r != sizeof(entry))
798 logf (LOG_FATAL|LOG_ERRNO, "write of %s at pos %ld",
799 p->index_fname, (long) pos);
801 logf (LOG_FATAL, "write of %s at pos %ld",
802 p->index_fname, (long) pos);
805 if (lseek (p->data_fd, entry.u.used.offset, SEEK_SET) == -1)
807 logf (LOG_FATAL|LOG_ERRNO, "lseek in %s to pos %ld",
808 p->data_fname, entry.u.used.offset);
811 if (p->tmp_size < entry.u.used.size)
814 p->tmp_size = entry.u.used.size + 16384;
815 if (!(p->tmp_buf = malloc (p->tmp_size)))
817 logf (LOG_FATAL|LOG_ERRNO, "malloc");
822 for (i = 0; i < REC_NO_INFO; i++)
824 memcpy (cptr, &rec->size[i], sizeof(*rec->size));
825 cptr += sizeof(*rec->size);
828 memcpy (cptr, rec->info[i], rec->size[i]);
829 cptr += rec->size[i];
832 for (got = 0; got < entry.u.used.size; got += r)
834 r = write (p->data_fd, p->tmp_buf + got, entry.u.used.size - got);
837 logf (LOG_FATAL|LOG_ERRNO, "write of %s", p->data_fname);
843 static void rec_cache_flush (Records p)
846 for (i = 0; i<p->cache_cur; i++)
848 struct record_cache_entry *e = p->record_cache + i;
850 rec_write_single (p, e->rec);
856 static Record *rec_cache_lookup (Records p, int sysno, int dirty)
859 for (i = 0; i<p->cache_cur; i++)
861 struct record_cache_entry *e = p->record_cache + i;
862 if (e->rec->sysno == sysno)
872 static void rec_cache_insert (Records p, Record rec, int dirty)
874 struct record_cache_entry *e;
876 if (p->cache_cur == p->cache_max)
878 assert (p->cache_cur < p->cache_max);
880 e = p->record_cache + (p->cache_cur)++;
882 e->rec = rec_cp (rec);
885 void rec_close (Records *p)
889 rec_cache_flush (*p);
890 free ((*p)->record_cache);
895 if ((*p)->index_fd != -1)
896 close ((*p)->index_fd);
898 if ((*p)->data_fd != -1)
899 close ((*p)->data_fd);
901 free ((*p)->tmp_buf);
907 Record rec_get (Records p, int sysno)
911 struct record_index_entry entry;
918 if ((recp = rec_cache_lookup (p, sysno, 0)))
919 return rec_cp (*recp);
921 read_indx (p, sysno, &entry, sizeof(entry));
923 if (!(rec = malloc (sizeof(*rec))))
925 logf (LOG_FATAL|LOG_ERRNO, "malloc");
928 if (lseek (p->data_fd, entry.u.used.offset, SEEK_SET) == -1)
930 logf (LOG_FATAL|LOG_ERRNO, "lseek in %s to pos %ld",
931 p->data_fname, entry.u.used.offset);
934 if (p->tmp_size < entry.u.used.size)
937 p->tmp_size = entry.u.used.size + 16384;
938 if (!(p->tmp_buf = malloc (p->tmp_size)))
940 logf (LOG_FATAL|LOG_ERRNO, "malloc");
944 for (got = 0; got < entry.u.used.size; got += r)
946 r = read (p->data_fd, p->tmp_buf + got, entry.u.used.size - got);
949 logf (LOG_FATAL|LOG_ERRNO, "read of %s", p->data_fname);
956 for (i = 0; i < REC_NO_INFO; i++)
958 memcpy (&rec->size[i], nptr, sizeof(*rec->size));
959 nptr += sizeof(*rec->size);
962 rec->info[i] = malloc (rec->size[i]);
963 memcpy (rec->info[i], nptr, rec->size[i]);
964 nptr += rec->size[i];
969 rec_cache_insert (p, rec, 0);
973 Record rec_new (Records p)
979 if (!(rec = malloc (sizeof(*rec))))
981 logf (LOG_FATAL|LOG_ERRNO, "malloc");
984 if (p->head.index_free == 0)
985 sysno = (p->head.index_last)++;
988 struct record_index_entry entry;
990 read_indx (p, p->head.index_free, &entry, sizeof(entry));
991 sysno = p->head.index_free;
992 p->head.index_free = entry.u.free.next;
994 (p->head.no_records)++;
996 for (i = 0; i < REC_NO_INFO; i++)
1001 rec_cache_insert (p, rec, 1);
1005 void rec_put (Records p, Record *recpp)
1009 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, 1)))
1016 rec_cache_insert (p, *recpp, 1);
1022 void rec_rm (Record *recpp)
1025 for (i = 0; i < REC_NO_INFO; i++)
1026 free ((*recpp)->info[i]);
1031 Record rec_cp (Record rec)
1036 if (!(n = malloc (sizeof(*n))))
1038 logf (LOG_FATAL|LOG_ERRNO, "malloc");
1041 n->sysno = rec->sysno;
1042 for (i = 0; i < REC_NO_INFO; i++)
1050 n->size[i] = rec->size[i];
1051 if (!(n->info[i] = malloc (rec->size[i])))
1053 logf (LOG_FATAL|LOG_ERRNO, "malloc. rec_cp");
1056 memcpy (n->info[i], rec->info[i], rec->size[i]);
1061 void rec_del (Records p, Record *recpp)
1069 char *rec_strdup (const char *s, size_t *len)
1082 logf (LOG_FATAL|LOG_ERRNO, "malloc");