2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.8 1995-11-28 14:26:21 adam
8 * Bug fix: recordId with constant wasn't right.
9 * Bug fix: recordId dictionary entry wasn't deleted when needed.
11 * Revision 1.7 1995/11/28 09:09:43 adam
12 * Zebra config renamed.
13 * Use setting 'recordId' to identify record now.
14 * Bug fix in recindex.c: rec_release_blocks was invokeded even
15 * though the blocks were already released.
16 * File traversal properly deletes records when needed.
18 * Revision 1.6 1995/11/25 10:24:06 adam
19 * More record fields - they are enumerated now.
20 * New options: flagStoreData flagStoreKey.
22 * Revision 1.5 1995/11/22 17:19:18 adam
23 * Record management uses the bfile system.
25 * Revision 1.4 1995/11/20 16:59:46 adam
26 * New update method: the 'old' keys are saved for each records.
28 * Revision 1.3 1995/11/16 15:34:55 adam
29 * Uses new record management system in both indexer and server.
31 * Revision 1.2 1995/11/15 19:13:08 adam
32 * Work on record management.
34 * Revision 1.1 1995/11/15 14:46:20 adam
35 * Started work on better record management system.
53 #define REC_BLOCK_TYPES 2
54 #define REC_HEAD_MAGIC "recindx"
63 char *data_fname[REC_BLOCK_TYPES];
64 BFile data_BFile[REC_BLOCK_TYPES];
69 struct record_cache_entry *record_cache;
76 int block_size[REC_BLOCK_TYPES];
77 int block_free[REC_BLOCK_TYPES];
78 int block_last[REC_BLOCK_TYPES];
79 int block_used[REC_BLOCK_TYPES];
80 int block_move[REC_BLOCK_TYPES];
89 enum recordCacheFlag { recordFlagNop, recordFlagWrite, recordFlagNew,
92 struct record_cache_entry {
94 enum recordCacheFlag flag;
97 struct record_index_entry {
110 static void rec_write_head (Records p)
115 assert (p->index_BFile);
117 r = bf_write (p->index_BFile, 0, 0, sizeof(p->head), &p->head);
120 logf (LOG_FATAL|LOG_ERRNO, "write head of %s", p->index_fname);
125 static void rec_tmp_expand (Records p, int size, int dst_type)
127 if (p->tmp_size < size + 256 ||
128 p->tmp_size < p->head.block_size[dst_type]*2)
131 p->tmp_size = size + p->head.block_size[dst_type]*2 +
133 if (!(p->tmp_buf = malloc (p->tmp_size)))
135 logf (LOG_FATAL|LOG_ERRNO, "malloc");
141 static int read_indx (Records p, int sysno, void *buf, int itemsize,
145 int pos = (sysno-1)*itemsize;
147 r = bf_read (p->index_BFile, 1+pos/128, pos%128, itemsize, buf);
148 if (r != 1 && !ignoreError)
150 logf (LOG_FATAL|LOG_ERRNO, "read in %s at pos %ld",
151 p->index_fname, (long) pos);
158 static void write_indx (Records p, int sysno, void *buf, int itemsize)
160 int pos = (sysno-1)*itemsize;
162 bf_write (p->index_BFile, 1+pos/128, pos%128, itemsize, buf);
165 static void rec_release_blocks (Records p, int sysno)
167 struct record_index_entry entry;
168 int freeblock, freenext;
171 if (read_indx (p, sysno, &entry, sizeof(entry), 1) != 1)
173 freeblock = entry.u.used.next;
174 assert (freeblock > 0);
175 dst_type = freeblock & 7;
176 assert (dst_type < REC_BLOCK_TYPES);
177 freeblock = freeblock / 8;
180 if (bf_read (p->data_BFile[dst_type], freeblock, 0, sizeof(freenext),
183 logf (LOG_FATAL|LOG_ERRNO, "read in rec_del_single");
186 if (bf_write (p->data_BFile[dst_type], freeblock, 0, sizeof(freenext),
187 &p->head.block_free[dst_type]))
189 logf (LOG_FATAL|LOG_ERRNO, "write in rec_del_single");
192 p->head.block_free[dst_type] = freeblock;
193 freeblock = freenext;
194 p->head.block_used[dst_type]--;
198 static void rec_delete_single (Records p, Record rec)
200 struct record_index_entry entry;
202 rec_release_blocks (p, rec->sysno);
204 entry.u.free.next = p->head.index_free;
205 p->head.index_free = rec->sysno;
206 write_indx (p, rec->sysno, &entry, sizeof(entry));
210 static void rec_write_single (Records p, Record rec)
216 int block_prev = -1, block_free;
217 struct record_index_entry entry;
219 for (i = 0; i < REC_NO_INFO; i++)
221 size += sizeof(*rec->size);
223 size += sizeof(*rec->size) + rec->size[i];
225 for (i = 1; i<REC_BLOCK_TYPES; i++)
226 if (size >= p->head.block_move[i])
229 rec_tmp_expand (p, size, dst_type);
231 cptr = p->tmp_buf + sizeof(int); /* a hack! */
232 for (i = 0; i < REC_NO_INFO; i++)
234 memcpy (cptr, &rec->size[i], sizeof(*rec->size));
235 cptr += sizeof(*rec->size);
238 memcpy (cptr, rec->info[i], rec->size[i]);
239 cptr += rec->size[i];
243 while (no_written < size)
245 block_free = p->head.block_free[dst_type];
248 if (bf_read (p->data_BFile[dst_type],
249 block_free, 0, sizeof(*p->head.block_free),
250 &p->head.block_free[dst_type]) != 1)
252 logf (LOG_FATAL|LOG_ERRNO, "read in %s at free block %d",
253 p->data_fname[dst_type], block_free);
257 block_free = p->head.block_last[dst_type]++;
258 if (block_prev == -1)
260 entry.u.used.next = block_free*8 + dst_type;
261 entry.u.used.size = size;
263 write_indx (p, rec->sysno, &entry, sizeof(entry));
267 memcpy (cptr, &block_free, sizeof(int));
268 bf_write (p->data_BFile[dst_type], block_prev, 0, 0, cptr);
269 cptr = p->tmp_buf + no_written;
271 block_prev = block_free;
272 no_written += p->head.block_size[dst_type] - sizeof(int);
273 p->head.block_used[dst_type]++;
275 assert (block_prev != -1);
277 memcpy (cptr, &block_free, sizeof(int));
278 bf_write (p->data_BFile[dst_type], block_prev, 0,
279 sizeof(int) + (p->tmp_buf+size) - cptr, cptr);
282 static void rec_update_single (Records p, Record rec)
284 rec_release_blocks (p, rec->sysno);
285 rec_write_single (p, rec);
288 Records rec_open (int rw)
293 if (!(p = malloc (sizeof(*p))))
295 logf (LOG_FATAL|LOG_ERRNO, "malloc");
300 p->tmp_buf = malloc (p->tmp_size);
303 logf (LOG_FATAL|LOG_ERRNO, "malloc");
306 p->index_fname = "recindex";
307 p->index_BFile = bf_open (p->index_fname, 128, rw);
308 if (p->index_BFile == NULL)
310 logf (LOG_FATAL|LOG_ERRNO, "open %s", p->index_fname);
313 r = bf_read (p->index_BFile, 0, 0, 0, p->tmp_buf);
317 memcpy (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic));
318 p->head.index_free = 0;
319 p->head.index_last = 1;
320 p->head.no_records = 0;
321 for (i = 0; i<REC_BLOCK_TYPES; i++)
323 p->head.block_free[i] = 0;
324 p->head.block_last[i] = 1;
325 p->head.block_used[i] = 0;
327 p->head.block_size[0] = 128;
328 p->head.block_move[0] = 0;
329 for (i = 1; i<REC_BLOCK_TYPES; i++)
331 p->head.block_size[i] = p->head.block_size[i-1] * 4;
332 p->head.block_move[i] = p->head.block_size[i] * 3;
338 memcpy (&p->head, p->tmp_buf, sizeof(p->head));
339 if (memcmp (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic)))
341 logf (LOG_FATAL, "read %s. bad header", p->index_fname);
346 for (i = 0; i<REC_BLOCK_TYPES; i++)
349 sprintf (str, "recdata%c", i + 'A');
350 p->data_fname[i] = malloc (strlen(str)+1);
351 strcpy (p->data_fname[i], str);
352 p->data_BFile[i] = NULL;
354 for (i = 0; i<REC_BLOCK_TYPES; i++)
356 if (!(p->data_BFile[i] = bf_open (p->data_fname[i],
357 p->head.block_size[i],
360 logf (LOG_FATAL|LOG_ERRNO, "bf_open %s", p->data_fname[i]);
366 if (!(p->record_cache = malloc (sizeof(*p->record_cache)*p->cache_max)))
368 logf (LOG_FATAL|LOG_ERRNO, "malloc");
374 static void rec_cache_flush (Records p)
377 for (i = 0; i<p->cache_cur; i++)
379 struct record_cache_entry *e = p->record_cache + i;
385 rec_write_single (p, e->rec);
387 case recordFlagWrite:
388 rec_update_single (p, e->rec);
390 case recordFlagDelete:
391 rec_delete_single (p, e->rec);
399 static Record *rec_cache_lookup (Records p, int sysno,
400 enum recordCacheFlag flag)
403 for (i = 0; i<p->cache_cur; i++)
405 struct record_cache_entry *e = p->record_cache + i;
406 if (e->rec->sysno == sysno)
408 if (flag != recordFlagNop && e->flag == recordFlagNop)
416 static void rec_cache_insert (Records p, Record rec, enum recordCacheFlag flag)
418 struct record_cache_entry *e;
420 if (p->cache_cur == p->cache_max)
422 assert (p->cache_cur < p->cache_max);
424 e = p->record_cache + (p->cache_cur)++;
426 e->rec = rec_cp (rec);
429 void rec_close (Records *pp)
437 free (p->record_cache);
443 bf_close (p->index_BFile);
445 for (i = 0; i<REC_BLOCK_TYPES; i++)
447 if (p->data_BFile[i])
448 bf_close (p->data_BFile[i]);
449 free (p->data_fname[i]);
457 Record rec_get (Records p, int sysno)
461 struct record_index_entry entry;
462 int freeblock, dst_type;
468 if ((recp = rec_cache_lookup (p, sysno, recordFlagNop)))
469 return rec_cp (*recp);
471 read_indx (p, sysno, &entry, sizeof(entry), 0);
473 dst_type = entry.u.used.next & 7;
474 assert (dst_type < REC_BLOCK_TYPES);
475 freeblock = entry.u.used.next / 8;
477 assert (freeblock > 0);
479 if (!(rec = malloc (sizeof(*rec))))
481 logf (LOG_FATAL|LOG_ERRNO, "malloc");
484 rec_tmp_expand (p, entry.u.used.size, dst_type);
487 bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
488 memcpy (&freeblock, cptr, sizeof(freeblock));
494 cptr += p->head.block_size[dst_type] - sizeof(freeblock);
496 memcpy (&tmp, cptr, sizeof(tmp));
497 bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
498 memcpy (&freeblock, cptr, sizeof(freeblock));
499 memcpy (cptr, &tmp, sizeof(tmp));
503 nptr = p->tmp_buf + sizeof(freeblock);
504 for (i = 0; i < REC_NO_INFO; i++)
506 memcpy (&rec->size[i], nptr, sizeof(*rec->size));
507 nptr += sizeof(*rec->size);
510 rec->info[i] = malloc (rec->size[i]);
511 memcpy (rec->info[i], nptr, rec->size[i]);
512 nptr += rec->size[i];
517 rec_cache_insert (p, rec, recordFlagNop);
521 Record rec_new (Records p)
527 if (!(rec = malloc (sizeof(*rec))))
529 logf (LOG_FATAL|LOG_ERRNO, "malloc");
532 if (p->head.index_free == 0)
533 sysno = (p->head.index_last)++;
536 struct record_index_entry entry;
538 read_indx (p, p->head.index_free, &entry, sizeof(entry), 0);
539 sysno = p->head.index_free;
540 p->head.index_free = entry.u.free.next;
542 (p->head.no_records)++;
544 for (i = 0; i < REC_NO_INFO; i++)
549 rec_cache_insert (p, rec, recordFlagNew);
553 void rec_del (Records p, Record *recpp)
557 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagDelete)))
564 rec_cache_insert (p, *recpp, recordFlagDelete);
570 void rec_put (Records p, Record *recpp)
574 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagWrite)))
581 rec_cache_insert (p, *recpp, recordFlagWrite);
587 void rec_rm (Record *recpp)
590 for (i = 0; i < REC_NO_INFO; i++)
591 free ((*recpp)->info[i]);
596 Record rec_cp (Record rec)
601 if (!(n = malloc (sizeof(*n))))
603 logf (LOG_FATAL|LOG_ERRNO, "malloc");
606 n->sysno = rec->sysno;
607 for (i = 0; i < REC_NO_INFO; i++)
615 n->size[i] = rec->size[i];
616 if (!(n->info[i] = malloc (rec->size[i])))
618 logf (LOG_FATAL|LOG_ERRNO, "malloc. rec_cp");
621 memcpy (n->info[i], rec->info[i], rec->size[i]);
626 /* no BF --------------------------------------------------- */
629 struct records_info {
635 struct records_head {
649 struct record_cache_entry *record_cache;
652 struct record_cache_entry {
657 struct record_index_entry {
669 #define REC_HEAD_MAGIC "rechead"
671 static void rec_write_head (Records p)
676 assert (p->index_fd != -1);
677 if (lseek (p->index_fd, (off_t) 0, SEEK_SET) == -1)
679 logf (LOG_FATAL|LOG_ERRNO, "lseek to 0 in %s", p->index_fname);
682 r = write (p->index_fd, &p->head, sizeof(p->head));
686 logf (LOG_FATAL|LOG_ERRNO, "write head of %s", p->index_fname);
688 case sizeof(p->head):
691 logf (LOG_FATAL, "write head of %s. wrote %d", p->index_fname, r);
696 Records rec_open (int rw)
701 if (!(p = malloc (sizeof(*p))))
703 logf (LOG_FATAL|LOG_ERRNO, "malloc");
709 p->data_fname = "recdata";
711 p->index_fname = "recindex";
712 p->index_fd = open (p->index_fname,
713 rw ? (O_RDWR|O_CREAT) : O_RDONLY, 0666);
714 if (p->index_fd == -1)
716 logf (LOG_FATAL|LOG_ERRNO, "open %s", p->index_fname);
719 r = read (p->index_fd, &p->head, sizeof(p->head));
723 logf (LOG_FATAL|LOG_ERRNO, "read %s", p->index_fname);
726 memcpy (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic));
727 p->head.index_free = 0;
728 p->head.index_last = 1;
729 p->head.no_records = 0;
730 p->head.data_size = 0;
731 p->head.data_slack = 0;
732 p->head.data_used = 0;
736 case sizeof(p->head):
737 if (memcmp (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic)))
739 logf (LOG_FATAL, "read %s. bad header", p->index_fname);
744 logf (LOG_FATAL, "read head of %s. expected %d. got %d",
745 p->index_fname, sizeof(p->head), r);
748 p->data_fd = open (p->data_fname,
749 rw ? (O_RDWR|O_CREAT) : O_RDONLY, 0666);
750 if (p->data_fd == -1)
752 logf (LOG_FATAL|LOG_ERRNO, "open %s", p->data_fname);
757 if (!(p->record_cache = malloc (sizeof(*p->record_cache)*p->cache_max)))
759 logf (LOG_FATAL|LOG_ERRNO, "malloc");
765 static void read_indx (Records p, int sysno, void *buf, int itemsize)
768 off_t pos = (sysno-1)*itemsize + sizeof(p->head);
770 if (lseek (p->index_fd, pos, SEEK_SET) == (pos) -1)
772 logf (LOG_FATAL|LOG_ERRNO, "seek in %s to pos %ld",
773 p->index_fname, (long) pos);
776 r = read (p->index_fd, buf, itemsize);
780 logf (LOG_FATAL|LOG_ERRNO, "read in %s at pos %ld",
781 p->index_fname, (long) pos);
783 logf (LOG_FATAL, "read in %s at pos %ld",
784 p->index_fname, (long) pos);
789 static void rec_write_single (Records p, Record rec)
791 struct record_index_entry entry;
792 int r, i, size = 0, got;
794 off_t pos = (rec->sysno-1)*sizeof(entry) + sizeof(p->head);
796 for (i = 0; i < REC_NO_INFO; i++)
798 size += sizeof(*rec->size);
800 size += sizeof(*rec->size) + rec->size[i];
802 entry.u.used.offset = p->head.data_size;
803 entry.u.used.size = size;
804 p->head.data_size += size;
805 p->head.data_used += size;
807 if (lseek (p->index_fd, pos, SEEK_SET) == (pos) -1)
809 logf (LOG_FATAL|LOG_ERRNO, "seek in %s to pos %ld",
810 p->index_fname, (long) pos);
813 r = write (p->index_fd, &entry, sizeof(entry));
814 if (r != sizeof(entry))
817 logf (LOG_FATAL|LOG_ERRNO, "write of %s at pos %ld",
818 p->index_fname, (long) pos);
820 logf (LOG_FATAL, "write of %s at pos %ld",
821 p->index_fname, (long) pos);
824 if (lseek (p->data_fd, entry.u.used.offset, SEEK_SET) == -1)
826 logf (LOG_FATAL|LOG_ERRNO, "lseek in %s to pos %ld",
827 p->data_fname, entry.u.used.offset);
830 if (p->tmp_size < entry.u.used.size)
833 p->tmp_size = entry.u.used.size + 16384;
834 if (!(p->tmp_buf = malloc (p->tmp_size)))
836 logf (LOG_FATAL|LOG_ERRNO, "malloc");
841 for (i = 0; i < REC_NO_INFO; i++)
843 memcpy (cptr, &rec->size[i], sizeof(*rec->size));
844 cptr += sizeof(*rec->size);
847 memcpy (cptr, rec->info[i], rec->size[i]);
848 cptr += rec->size[i];
851 for (got = 0; got < entry.u.used.size; got += r)
853 r = write (p->data_fd, p->tmp_buf + got, entry.u.used.size - got);
856 logf (LOG_FATAL|LOG_ERRNO, "write of %s", p->data_fname);
862 static void rec_cache_flush (Records p)
865 for (i = 0; i<p->cache_cur; i++)
867 struct record_cache_entry *e = p->record_cache + i;
869 rec_write_single (p, e->rec);
875 static Record *rec_cache_lookup (Records p, int sysno, int dirty)
878 for (i = 0; i<p->cache_cur; i++)
880 struct record_cache_entry *e = p->record_cache + i;
881 if (e->rec->sysno == sysno)
891 static void rec_cache_insert (Records p, Record rec, int dirty)
893 struct record_cache_entry *e;
895 if (p->cache_cur == p->cache_max)
897 assert (p->cache_cur < p->cache_max);
899 e = p->record_cache + (p->cache_cur)++;
901 e->rec = rec_cp (rec);
904 void rec_close (Records *p)
908 rec_cache_flush (*p);
909 free ((*p)->record_cache);
914 if ((*p)->index_fd != -1)
915 close ((*p)->index_fd);
917 if ((*p)->data_fd != -1)
918 close ((*p)->data_fd);
920 free ((*p)->tmp_buf);
926 Record rec_get (Records p, int sysno)
930 struct record_index_entry entry;
937 if ((recp = rec_cache_lookup (p, sysno, 0)))
938 return rec_cp (*recp);
940 read_indx (p, sysno, &entry, sizeof(entry));
942 if (!(rec = malloc (sizeof(*rec))))
944 logf (LOG_FATAL|LOG_ERRNO, "malloc");
947 if (lseek (p->data_fd, entry.u.used.offset, SEEK_SET) == -1)
949 logf (LOG_FATAL|LOG_ERRNO, "lseek in %s to pos %ld",
950 p->data_fname, entry.u.used.offset);
953 if (p->tmp_size < entry.u.used.size)
956 p->tmp_size = entry.u.used.size + 16384;
957 if (!(p->tmp_buf = malloc (p->tmp_size)))
959 logf (LOG_FATAL|LOG_ERRNO, "malloc");
963 for (got = 0; got < entry.u.used.size; got += r)
965 r = read (p->data_fd, p->tmp_buf + got, entry.u.used.size - got);
968 logf (LOG_FATAL|LOG_ERRNO, "read of %s", p->data_fname);
975 for (i = 0; i < REC_NO_INFO; i++)
977 memcpy (&rec->size[i], nptr, sizeof(*rec->size));
978 nptr += sizeof(*rec->size);
981 rec->info[i] = malloc (rec->size[i]);
982 memcpy (rec->info[i], nptr, rec->size[i]);
983 nptr += rec->size[i];
988 rec_cache_insert (p, rec, 0);
992 Record rec_new (Records p)
998 if (!(rec = malloc (sizeof(*rec))))
1000 logf (LOG_FATAL|LOG_ERRNO, "malloc");
1003 if (p->head.index_free == 0)
1004 sysno = (p->head.index_last)++;
1007 struct record_index_entry entry;
1009 read_indx (p, p->head.index_free, &entry, sizeof(entry));
1010 sysno = p->head.index_free;
1011 p->head.index_free = entry.u.free.next;
1013 (p->head.no_records)++;
1015 for (i = 0; i < REC_NO_INFO; i++)
1017 rec->info[i] = NULL;
1020 rec_cache_insert (p, rec, 1);
1024 void rec_put (Records p, Record *recpp)
1028 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, 1)))
1035 rec_cache_insert (p, *recpp, 1);
1041 void rec_rm (Record *recpp)
1044 for (i = 0; i < REC_NO_INFO; i++)
1045 free ((*recpp)->info[i]);
1050 Record rec_cp (Record rec)
1055 if (!(n = malloc (sizeof(*n))))
1057 logf (LOG_FATAL|LOG_ERRNO, "malloc");
1060 n->sysno = rec->sysno;
1061 for (i = 0; i < REC_NO_INFO; i++)
1069 n->size[i] = rec->size[i];
1070 if (!(n->info[i] = malloc (rec->size[i])))
1072 logf (LOG_FATAL|LOG_ERRNO, "malloc. rec_cp");
1075 memcpy (n->info[i], rec->info[i], rec->size[i]);
1080 void rec_del (Records p, Record *recpp)
1088 char *rec_strdup (const char *s, size_t *len)
1101 logf (LOG_FATAL|LOG_ERRNO, "malloc");