2 * Copyright (C) 1994-1996, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.18 1997-07-15 16:28:42 adam
8 * Bug fix: storeData didn't work with files with multiple records.
9 * Bug fix: fixed memory management with records; not really well
12 * Revision 1.17 1997/02/12 20:39:46 adam
13 * Implemented options -f <n> that limits the log to the first <n>
15 * Changed some log messages also.
17 * Revision 1.16 1996/06/04 10:19:00 adam
18 * Minor changes - removed include of ctype.h.
20 * Revision 1.15 1996/05/13 14:23:06 adam
21 * Work on compaction of set/use bytes in dictionary.
23 * Revision 1.14 1996/02/01 20:48:15 adam
24 * The total size of records are always checked in rec_cache_insert to
25 * reduce memory usage.
27 * Revision 1.13 1995/12/11 09:12:49 adam
28 * The rec_get function returns NULL if record doesn't exist - will
29 * happen in the server if the result set records have been deleted since
30 * the creation of the set (i.e. the search).
31 * The server saves a result temporarily if it is 'volatile', i.e. the
32 * set is register dependent.
34 * Revision 1.12 1995/12/07 17:38:47 adam
35 * Work locking mechanisms for concurrent updates/commit.
37 * Revision 1.11 1995/12/06 13:58:26 adam
38 * Improved flushing of records - all flushes except the last one
39 * don't write the last accessed. Also flush takes place if record
40 * info occupy more than about 256k.
42 * Revision 1.10 1995/12/06 12:41:24 adam
43 * New command 'stat' for the index program.
44 * Filenames can be read from stdin by specifying '-'.
45 * Bug fix/enhancement of the transformation from terms to regular
46 * expressons in the search engine.
48 * Revision 1.9 1995/11/30 08:34:33 adam
49 * Started work on commit facility.
50 * Changed a few malloc/free to xmalloc/xfree.
52 * Revision 1.8 1995/11/28 14:26:21 adam
53 * Bug fix: recordId with constant wasn't right.
54 * Bug fix: recordId dictionary entry wasn't deleted when needed.
56 * Revision 1.7 1995/11/28 09:09:43 adam
57 * Zebra config renamed.
58 * Use setting 'recordId' to identify record now.
59 * Bug fix in recindex.c: rec_release_blocks was invokeded even
60 * though the blocks were already released.
61 * File traversal properly deletes records when needed.
63 * Revision 1.6 1995/11/25 10:24:06 adam
64 * More record fields - they are enumerated now.
65 * New options: flagStoreData flagStoreKey.
67 * Revision 1.5 1995/11/22 17:19:18 adam
68 * Record management uses the bfile system.
70 * Revision 1.4 1995/11/20 16:59:46 adam
71 * New update method: the 'old' keys are saved for each records.
73 * Revision 1.3 1995/11/16 15:34:55 adam
74 * Uses new record management system in both indexer and server.
76 * Revision 1.2 1995/11/15 19:13:08 adam
77 * Work on record management.
79 * Revision 1.1 1995/11/15 14:46:20 adam
80 * Started work on better record management system.
89 static void rec_write_head (Records p)
94 assert (p->index_BFile);
96 r = bf_write (p->index_BFile, 0, 0, sizeof(p->head), &p->head);
99 logf (LOG_FATAL|LOG_ERRNO, "write head of %s", p->index_fname);
104 static void rec_tmp_expand (Records p, int size, int dst_type)
106 if (p->tmp_size < size + 2048 ||
107 p->tmp_size < p->head.block_size[dst_type]*2)
110 p->tmp_size = size + p->head.block_size[dst_type]*2 + 2048;
111 p->tmp_buf = xmalloc (p->tmp_size);
115 static int read_indx (Records p, int sysno, void *buf, int itemsize,
119 int pos = (sysno-1)*itemsize;
121 r = bf_read (p->index_BFile, 1+pos/128, pos%128, itemsize, buf);
122 if (r != 1 && !ignoreError)
124 logf (LOG_FATAL|LOG_ERRNO, "read in %s at pos %ld",
125 p->index_fname, (long) pos);
132 static void write_indx (Records p, int sysno, void *buf, int itemsize)
134 int pos = (sysno-1)*itemsize;
136 bf_write (p->index_BFile, 1+pos/128, pos%128, itemsize, buf);
139 static void rec_release_blocks (Records p, int sysno)
141 struct record_index_entry entry;
142 int freeblock, freenext;
145 if (read_indx (p, sysno, &entry, sizeof(entry), 1) != 1)
147 p->head.total_bytes -= entry.size;
148 freeblock = entry.next;
149 assert (freeblock > 0);
150 dst_type = freeblock & 7;
151 assert (dst_type < REC_BLOCK_TYPES);
152 freeblock = freeblock / 8;
155 if (bf_read (p->data_BFile[dst_type], freeblock, 0, sizeof(freenext),
158 logf (LOG_FATAL|LOG_ERRNO, "read in rec_del_single");
161 if (bf_write (p->data_BFile[dst_type], freeblock, 0, sizeof(freenext),
162 &p->head.block_free[dst_type]))
164 logf (LOG_FATAL|LOG_ERRNO, "write in rec_del_single");
167 p->head.block_free[dst_type] = freeblock;
168 freeblock = freenext;
169 p->head.block_used[dst_type]--;
173 static void rec_delete_single (Records p, Record rec)
175 struct record_index_entry entry;
177 rec_release_blocks (p, rec->sysno);
179 entry.next = p->head.index_free;
181 p->head.index_free = rec->sysno;
182 write_indx (p, rec->sysno, &entry, sizeof(entry));
186 static void rec_write_single (Records p, Record rec)
192 int block_prev = -1, block_free;
193 struct record_index_entry entry;
195 for (i = 0; i < REC_NO_INFO; i++)
197 size += sizeof(*rec->size);
199 size += sizeof(*rec->size) + rec->size[i];
201 for (i = 1; i<REC_BLOCK_TYPES; i++)
202 if (size >= p->head.block_move[i])
205 rec_tmp_expand (p, size, dst_type);
207 cptr = p->tmp_buf + sizeof(int); /* a hack! */
208 for (i = 0; i < REC_NO_INFO; i++)
210 memcpy (cptr, &rec->size[i], sizeof(*rec->size));
211 cptr += sizeof(*rec->size);
214 memcpy (cptr, rec->info[i], rec->size[i]);
215 cptr += rec->size[i];
219 while (no_written < size)
221 block_free = p->head.block_free[dst_type];
224 if (bf_read (p->data_BFile[dst_type],
225 block_free, 0, sizeof(*p->head.block_free),
226 &p->head.block_free[dst_type]) != 1)
228 logf (LOG_FATAL|LOG_ERRNO, "read in %s at free block %d",
229 p->data_fname[dst_type], block_free);
233 block_free = p->head.block_last[dst_type]++;
234 if (block_prev == -1)
236 entry.next = block_free*8 + dst_type;
238 p->head.total_bytes += size;
239 write_indx (p, rec->sysno, &entry, sizeof(entry));
243 memcpy (cptr, &block_free, sizeof(int));
244 bf_write (p->data_BFile[dst_type], block_prev, 0, 0, cptr);
245 cptr = p->tmp_buf + no_written;
247 block_prev = block_free;
248 no_written += p->head.block_size[dst_type] - sizeof(int);
249 p->head.block_used[dst_type]++;
251 assert (block_prev != -1);
253 memcpy (cptr, &block_free, sizeof(int));
254 bf_write (p->data_BFile[dst_type], block_prev, 0,
255 sizeof(int) + (p->tmp_buf+size) - cptr, cptr);
258 static void rec_update_single (Records p, Record rec)
260 rec_release_blocks (p, rec->sysno);
261 rec_write_single (p, rec);
264 Records rec_open (int rw)
269 p = xmalloc (sizeof(*p));
272 p->tmp_buf = xmalloc (p->tmp_size);
273 p->index_fname = "recindex";
274 p->index_BFile = bf_open (p->index_fname, 128, rw);
275 if (p->index_BFile == NULL)
277 logf (LOG_FATAL|LOG_ERRNO, "open %s", p->index_fname);
280 r = bf_read (p->index_BFile, 0, 0, 0, p->tmp_buf);
284 memcpy (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic));
285 p->head.index_free = 0;
286 p->head.index_last = 1;
287 p->head.no_records = 0;
288 p->head.total_bytes = 0;
289 for (i = 0; i<REC_BLOCK_TYPES; i++)
291 p->head.block_free[i] = 0;
292 p->head.block_last[i] = 1;
293 p->head.block_used[i] = 0;
295 p->head.block_size[0] = 128;
296 p->head.block_move[0] = 0;
297 for (i = 1; i<REC_BLOCK_TYPES; i++)
299 p->head.block_size[i] = p->head.block_size[i-1] * 4;
300 p->head.block_move[i] = p->head.block_size[i] * 3;
306 memcpy (&p->head, p->tmp_buf, sizeof(p->head));
307 if (memcmp (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic)))
309 logf (LOG_FATAL, "read %s. bad header", p->index_fname);
314 for (i = 0; i<REC_BLOCK_TYPES; i++)
317 sprintf (str, "recdata%c", i + 'A');
318 p->data_fname[i] = xmalloc (strlen(str)+1);
319 strcpy (p->data_fname[i], str);
320 p->data_BFile[i] = NULL;
322 for (i = 0; i<REC_BLOCK_TYPES; i++)
324 if (!(p->data_BFile[i] = bf_open (p->data_fname[i],
325 p->head.block_size[i],
328 logf (LOG_FATAL|LOG_ERRNO, "bf_open %s", p->data_fname[i]);
334 p->record_cache = xmalloc (sizeof(*p->record_cache)*p->cache_max);
338 static void rec_cache_flush (Records p, int saveCount)
342 if (saveCount >= p->cache_cur)
344 for (i = 0; i<p->cache_cur - saveCount; i++)
346 struct record_cache_entry *e = p->record_cache + i;
352 rec_write_single (p, e->rec);
354 case recordFlagWrite:
355 rec_update_single (p, e->rec);
357 case recordFlagDelete:
358 rec_delete_single (p, e->rec);
363 for (j = 0; j<saveCount; j++, i++)
364 memcpy (p->record_cache+j, p->record_cache+i,
365 sizeof(*p->record_cache));
366 p->cache_cur = saveCount;
369 static Record *rec_cache_lookup (Records p, int sysno,
370 enum recordCacheFlag flag)
373 for (i = 0; i<p->cache_cur; i++)
375 struct record_cache_entry *e = p->record_cache + i;
376 if (e->rec->sysno == sysno)
378 if (flag != recordFlagNop && e->flag == recordFlagNop)
386 static void rec_cache_insert (Records p, Record rec, enum recordCacheFlag flag)
388 struct record_cache_entry *e;
390 if (p->cache_cur == p->cache_max)
391 rec_cache_flush (p, 1);
392 else if (p->cache_cur > 0)
396 for (i = 0; i<p->cache_cur; i++)
398 Record r = (p->record_cache + i)->rec;
399 for (j = 0; j<REC_NO_INFO; j++)
403 rec_cache_flush (p, 1);
405 assert (p->cache_cur < p->cache_max);
407 e = p->record_cache + (p->cache_cur)++;
409 e->rec = rec_cp (rec);
412 void rec_close (Records *pp)
419 rec_cache_flush (p, 0);
420 xfree (p->record_cache);
426 bf_close (p->index_BFile);
428 for (i = 0; i<REC_BLOCK_TYPES; i++)
430 if (p->data_BFile[i])
431 bf_close (p->data_BFile[i]);
432 xfree (p->data_fname[i]);
440 Record rec_get (Records p, int sysno)
444 struct record_index_entry entry;
445 int freeblock, dst_type;
451 if ((recp = rec_cache_lookup (p, sysno, recordFlagNop)))
452 return rec_cp (*recp);
454 if (!read_indx (p, sysno, &entry, sizeof(entry), 1))
455 return NULL; /* record is not there! */
458 return NULL; /* record is deleted */
460 dst_type = entry.next & 7;
461 assert (dst_type < REC_BLOCK_TYPES);
462 freeblock = entry.next / 8;
464 assert (freeblock > 0);
466 rec = xmalloc (sizeof(*rec));
467 rec_tmp_expand (p, entry.size, dst_type);
470 bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
471 memcpy (&freeblock, cptr, sizeof(freeblock));
477 cptr += p->head.block_size[dst_type] - sizeof(freeblock);
479 memcpy (&tmp, cptr, sizeof(tmp));
480 bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
481 memcpy (&freeblock, cptr, sizeof(freeblock));
482 memcpy (cptr, &tmp, sizeof(tmp));
486 nptr = p->tmp_buf + sizeof(freeblock);
487 for (i = 0; i < REC_NO_INFO; i++)
489 memcpy (&rec->size[i], nptr, sizeof(*rec->size));
490 nptr += sizeof(*rec->size);
493 rec->info[i] = xmalloc (rec->size[i]);
494 memcpy (rec->info[i], nptr, rec->size[i]);
495 nptr += rec->size[i];
500 rec_cache_insert (p, rec, recordFlagNop);
504 Record rec_new (Records p)
510 rec = xmalloc (sizeof(*rec));
511 if (1 || p->head.index_free == 0)
512 sysno = (p->head.index_last)++;
515 struct record_index_entry entry;
517 read_indx (p, p->head.index_free, &entry, sizeof(entry), 0);
518 sysno = p->head.index_free;
519 p->head.index_free = entry.next;
521 (p->head.no_records)++;
523 for (i = 0; i < REC_NO_INFO; i++)
528 rec_cache_insert (p, rec, recordFlagNew);
532 void rec_del (Records p, Record *recpp)
536 (p->head.no_records)--;
537 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagDelete)))
544 rec_cache_insert (p, *recpp, recordFlagDelete);
550 void rec_put (Records p, Record *recpp)
554 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagWrite)))
561 rec_cache_insert (p, *recpp, recordFlagWrite);
567 void rec_rm (Record *recpp)
573 for (i = 0; i < REC_NO_INFO; i++)
574 xfree ((*recpp)->info[i]);
579 Record rec_cp (Record rec)
584 n = xmalloc (sizeof(*n));
585 n->sysno = rec->sysno;
586 for (i = 0; i < REC_NO_INFO; i++)
594 n->size[i] = rec->size[i];
595 n->info[i] = xmalloc (rec->size[i]);
596 memcpy (n->info[i], rec->info[i], rec->size[i]);
602 char *rec_strdup (const char *s, size_t *len)