2 * Copyright (C) 1994-1996, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.16 1996-06-04 10:19:00 adam
8 * Minor changes - removed include of ctype.h.
10 * Revision 1.15 1996/05/13 14:23:06 adam
11 * Work on compaction of set/use bytes in dictionary.
13 * Revision 1.14 1996/02/01 20:48:15 adam
14 * The total size of records are always checked in rec_cache_insert to
15 * reduce memory usage.
17 * Revision 1.13 1995/12/11 09:12:49 adam
18 * The rec_get function returns NULL if record doesn't exist - will
19 * happen in the server if the result set records have been deleted since
20 * the creation of the set (i.e. the search).
21 * The server saves a result temporarily if it is 'volatile', i.e. the
22 * set is register dependent.
24 * Revision 1.12 1995/12/07 17:38:47 adam
25 * Work locking mechanisms for concurrent updates/commit.
27 * Revision 1.11 1995/12/06 13:58:26 adam
28 * Improved flushing of records - all flushes except the last one
29 * don't write the last accessed. Also flush takes place if record
30 * info occupy more than about 256k.
32 * Revision 1.10 1995/12/06 12:41:24 adam
33 * New command 'stat' for the index program.
34 * Filenames can be read from stdin by specifying '-'.
35 * Bug fix/enhancement of the transformation from terms to regular
36 * expressons in the search engine.
38 * Revision 1.9 1995/11/30 08:34:33 adam
39 * Started work on commit facility.
40 * Changed a few malloc/free to xmalloc/xfree.
42 * Revision 1.8 1995/11/28 14:26:21 adam
43 * Bug fix: recordId with constant wasn't right.
44 * Bug fix: recordId dictionary entry wasn't deleted when needed.
46 * Revision 1.7 1995/11/28 09:09:43 adam
47 * Zebra config renamed.
48 * Use setting 'recordId' to identify record now.
49 * Bug fix in recindex.c: rec_release_blocks was invokeded even
50 * though the blocks were already released.
51 * File traversal properly deletes records when needed.
53 * Revision 1.6 1995/11/25 10:24:06 adam
54 * More record fields - they are enumerated now.
55 * New options: flagStoreData flagStoreKey.
57 * Revision 1.5 1995/11/22 17:19:18 adam
58 * Record management uses the bfile system.
60 * Revision 1.4 1995/11/20 16:59:46 adam
61 * New update method: the 'old' keys are saved for each records.
63 * Revision 1.3 1995/11/16 15:34:55 adam
64 * Uses new record management system in both indexer and server.
66 * Revision 1.2 1995/11/15 19:13:08 adam
67 * Work on record management.
69 * Revision 1.1 1995/11/15 14:46:20 adam
70 * Started work on better record management system.
79 static void rec_write_head (Records p)
84 assert (p->index_BFile);
86 r = bf_write (p->index_BFile, 0, 0, sizeof(p->head), &p->head);
89 logf (LOG_FATAL|LOG_ERRNO, "write head of %s", p->index_fname);
94 static void rec_tmp_expand (Records p, int size, int dst_type)
96 if (p->tmp_size < size + 256 ||
97 p->tmp_size < p->head.block_size[dst_type]*2)
100 p->tmp_size = size + p->head.block_size[dst_type]*2 + 256;
101 p->tmp_buf = xmalloc (p->tmp_size);
105 static int read_indx (Records p, int sysno, void *buf, int itemsize,
109 int pos = (sysno-1)*itemsize;
111 r = bf_read (p->index_BFile, 1+pos/128, pos%128, itemsize, buf);
112 if (r != 1 && !ignoreError)
114 logf (LOG_FATAL|LOG_ERRNO, "read in %s at pos %ld",
115 p->index_fname, (long) pos);
122 static void write_indx (Records p, int sysno, void *buf, int itemsize)
124 int pos = (sysno-1)*itemsize;
126 bf_write (p->index_BFile, 1+pos/128, pos%128, itemsize, buf);
129 static void rec_release_blocks (Records p, int sysno)
131 struct record_index_entry entry;
132 int freeblock, freenext;
135 if (read_indx (p, sysno, &entry, sizeof(entry), 1) != 1)
137 p->head.total_bytes -= entry.size;
138 freeblock = entry.next;
139 assert (freeblock > 0);
140 dst_type = freeblock & 7;
141 assert (dst_type < REC_BLOCK_TYPES);
142 freeblock = freeblock / 8;
145 if (bf_read (p->data_BFile[dst_type], freeblock, 0, sizeof(freenext),
148 logf (LOG_FATAL|LOG_ERRNO, "read in rec_del_single");
151 if (bf_write (p->data_BFile[dst_type], freeblock, 0, sizeof(freenext),
152 &p->head.block_free[dst_type]))
154 logf (LOG_FATAL|LOG_ERRNO, "write in rec_del_single");
157 p->head.block_free[dst_type] = freeblock;
158 freeblock = freenext;
159 p->head.block_used[dst_type]--;
163 static void rec_delete_single (Records p, Record rec)
165 struct record_index_entry entry;
167 rec_release_blocks (p, rec->sysno);
169 entry.next = p->head.index_free;
171 p->head.index_free = rec->sysno;
172 write_indx (p, rec->sysno, &entry, sizeof(entry));
176 static void rec_write_single (Records p, Record rec)
182 int block_prev = -1, block_free;
183 struct record_index_entry entry;
185 for (i = 0; i < REC_NO_INFO; i++)
187 size += sizeof(*rec->size);
189 size += sizeof(*rec->size) + rec->size[i];
191 for (i = 1; i<REC_BLOCK_TYPES; i++)
192 if (size >= p->head.block_move[i])
195 rec_tmp_expand (p, size, dst_type);
197 cptr = p->tmp_buf + sizeof(int); /* a hack! */
198 for (i = 0; i < REC_NO_INFO; i++)
200 memcpy (cptr, &rec->size[i], sizeof(*rec->size));
201 cptr += sizeof(*rec->size);
204 memcpy (cptr, rec->info[i], rec->size[i]);
205 cptr += rec->size[i];
209 while (no_written < size)
211 block_free = p->head.block_free[dst_type];
214 if (bf_read (p->data_BFile[dst_type],
215 block_free, 0, sizeof(*p->head.block_free),
216 &p->head.block_free[dst_type]) != 1)
218 logf (LOG_FATAL|LOG_ERRNO, "read in %s at free block %d",
219 p->data_fname[dst_type], block_free);
223 block_free = p->head.block_last[dst_type]++;
224 if (block_prev == -1)
226 entry.next = block_free*8 + dst_type;
228 p->head.total_bytes += size;
229 write_indx (p, rec->sysno, &entry, sizeof(entry));
233 memcpy (cptr, &block_free, sizeof(int));
234 bf_write (p->data_BFile[dst_type], block_prev, 0, 0, cptr);
235 cptr = p->tmp_buf + no_written;
237 block_prev = block_free;
238 no_written += p->head.block_size[dst_type] - sizeof(int);
239 p->head.block_used[dst_type]++;
241 assert (block_prev != -1);
243 memcpy (cptr, &block_free, sizeof(int));
244 bf_write (p->data_BFile[dst_type], block_prev, 0,
245 sizeof(int) + (p->tmp_buf+size) - cptr, cptr);
248 static void rec_update_single (Records p, Record rec)
250 rec_release_blocks (p, rec->sysno);
251 rec_write_single (p, rec);
254 Records rec_open (int rw)
259 p = xmalloc (sizeof(*p));
262 p->tmp_buf = xmalloc (p->tmp_size);
263 p->index_fname = "recindex";
264 p->index_BFile = bf_open (p->index_fname, 128, rw);
265 if (p->index_BFile == NULL)
267 logf (LOG_FATAL|LOG_ERRNO, "open %s", p->index_fname);
270 r = bf_read (p->index_BFile, 0, 0, 0, p->tmp_buf);
274 memcpy (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic));
275 p->head.index_free = 0;
276 p->head.index_last = 1;
277 p->head.no_records = 0;
278 p->head.total_bytes = 0;
279 for (i = 0; i<REC_BLOCK_TYPES; i++)
281 p->head.block_free[i] = 0;
282 p->head.block_last[i] = 1;
283 p->head.block_used[i] = 0;
285 p->head.block_size[0] = 128;
286 p->head.block_move[0] = 0;
287 for (i = 1; i<REC_BLOCK_TYPES; i++)
289 p->head.block_size[i] = p->head.block_size[i-1] * 4;
290 p->head.block_move[i] = p->head.block_size[i] * 3;
296 memcpy (&p->head, p->tmp_buf, sizeof(p->head));
297 if (memcmp (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic)))
299 logf (LOG_FATAL, "read %s. bad header", p->index_fname);
304 for (i = 0; i<REC_BLOCK_TYPES; i++)
307 sprintf (str, "recdata%c", i + 'A');
308 p->data_fname[i] = xmalloc (strlen(str)+1);
309 strcpy (p->data_fname[i], str);
310 p->data_BFile[i] = NULL;
312 for (i = 0; i<REC_BLOCK_TYPES; i++)
314 if (!(p->data_BFile[i] = bf_open (p->data_fname[i],
315 p->head.block_size[i],
318 logf (LOG_FATAL|LOG_ERRNO, "bf_open %s", p->data_fname[i]);
324 p->record_cache = xmalloc (sizeof(*p->record_cache)*p->cache_max);
328 static void rec_cache_flush (Records p, int saveCount)
332 if (saveCount >= p->cache_cur)
334 for (i = 0; i<p->cache_cur - saveCount; i++)
336 struct record_cache_entry *e = p->record_cache + i;
342 rec_write_single (p, e->rec);
344 case recordFlagWrite:
345 rec_update_single (p, e->rec);
347 case recordFlagDelete:
348 rec_delete_single (p, e->rec);
353 for (j = 0; j<saveCount; j++, i++)
354 memcpy (p->record_cache+j, p->record_cache+i,
355 sizeof(*p->record_cache));
356 p->cache_cur = saveCount;
359 static Record *rec_cache_lookup (Records p, int sysno,
360 enum recordCacheFlag flag)
363 for (i = 0; i<p->cache_cur; i++)
365 struct record_cache_entry *e = p->record_cache + i;
366 if (e->rec->sysno == sysno)
368 if (flag != recordFlagNop && e->flag == recordFlagNop)
376 static void rec_cache_insert (Records p, Record rec, enum recordCacheFlag flag)
378 struct record_cache_entry *e;
380 if (p->cache_cur == p->cache_max)
381 rec_cache_flush (p, 1);
382 else if (p->cache_cur > 0)
386 for (i = 0; i<p->cache_cur; i++)
388 Record r = (p->record_cache + i)->rec;
389 for (j = 0; j<REC_NO_INFO; j++)
393 rec_cache_flush (p, 1);
395 assert (p->cache_cur < p->cache_max);
397 e = p->record_cache + (p->cache_cur)++;
399 e->rec = rec_cp (rec);
402 void rec_close (Records *pp)
409 rec_cache_flush (p, 0);
410 xfree (p->record_cache);
416 bf_close (p->index_BFile);
418 for (i = 0; i<REC_BLOCK_TYPES; i++)
420 if (p->data_BFile[i])
421 bf_close (p->data_BFile[i]);
422 xfree (p->data_fname[i]);
430 Record rec_get (Records p, int sysno)
434 struct record_index_entry entry;
435 int freeblock, dst_type;
441 if ((recp = rec_cache_lookup (p, sysno, recordFlagNop)))
442 return rec_cp (*recp);
444 if (!read_indx (p, sysno, &entry, sizeof(entry), 1))
445 return NULL; /* record is not there! */
448 return NULL; /* record is deleted */
450 dst_type = entry.next & 7;
451 assert (dst_type < REC_BLOCK_TYPES);
452 freeblock = entry.next / 8;
454 assert (freeblock > 0);
456 rec = xmalloc (sizeof(*rec));
457 rec_tmp_expand (p, entry.size, dst_type);
460 bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
461 memcpy (&freeblock, cptr, sizeof(freeblock));
467 cptr += p->head.block_size[dst_type] - sizeof(freeblock);
469 memcpy (&tmp, cptr, sizeof(tmp));
470 bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
471 memcpy (&freeblock, cptr, sizeof(freeblock));
472 memcpy (cptr, &tmp, sizeof(tmp));
476 nptr = p->tmp_buf + sizeof(freeblock);
477 for (i = 0; i < REC_NO_INFO; i++)
479 memcpy (&rec->size[i], nptr, sizeof(*rec->size));
480 nptr += sizeof(*rec->size);
483 rec->info[i] = xmalloc (rec->size[i]);
484 memcpy (rec->info[i], nptr, rec->size[i]);
485 nptr += rec->size[i];
490 rec_cache_insert (p, rec, recordFlagNop);
494 Record rec_new (Records p)
500 rec = xmalloc (sizeof(*rec));
501 if (p->head.index_free == 0)
502 sysno = (p->head.index_last)++;
505 struct record_index_entry entry;
507 read_indx (p, p->head.index_free, &entry, sizeof(entry), 0);
508 sysno = p->head.index_free;
509 p->head.index_free = entry.next;
511 (p->head.no_records)++;
513 for (i = 0; i < REC_NO_INFO; i++)
518 rec_cache_insert (p, rec, recordFlagNew);
522 void rec_del (Records p, Record *recpp)
526 (p->head.no_records)--;
527 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagDelete)))
534 rec_cache_insert (p, *recpp, recordFlagDelete);
540 void rec_put (Records p, Record *recpp)
544 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagWrite)))
551 rec_cache_insert (p, *recpp, recordFlagWrite);
557 void rec_rm (Record *recpp)
563 for (i = 0; i < REC_NO_INFO; i++)
564 xfree ((*recpp)->info[i]);
569 Record rec_cp (Record rec)
574 n = xmalloc (sizeof(*n));
575 n->sysno = rec->sysno;
576 for (i = 0; i < REC_NO_INFO; i++)
584 n->size[i] = rec->size[i];
585 n->info[i] = xmalloc (rec->size[i]);
586 memcpy (n->info[i], rec->info[i], rec->size[i]);
592 char *rec_strdup (const char *s, size_t *len)