2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.13 1995-12-11 09:12:49 adam
8 * The rec_get function returns NULL if record doesn't exist - will
9 * happen in the server if the result set records have been deleted since
10 * the creation of the set (i.e. the search).
11 * The server saves a result temporarily if it is 'volatile', i.e. the
12 * set is register dependent.
14 * Revision 1.12 1995/12/07 17:38:47 adam
15 * Work locking mechanisms for concurrent updates/commit.
17 * Revision 1.11 1995/12/06 13:58:26 adam
18 * Improved flushing of records - all flushes except the last one
19 * don't write the last accessed. Also flush takes place if record
20 * info occupy more than about 256k.
22 * Revision 1.10 1995/12/06 12:41:24 adam
23 * New command 'stat' for the index program.
24 * Filenames can be read from stdin by specifying '-'.
25 * Bug fix/enhancement of the transformation from terms to regular
26 * expressons in the search engine.
28 * Revision 1.9 1995/11/30 08:34:33 adam
29 * Started work on commit facility.
30 * Changed a few malloc/free to xmalloc/xfree.
32 * Revision 1.8 1995/11/28 14:26:21 adam
33 * Bug fix: recordId with constant wasn't right.
34 * Bug fix: recordId dictionary entry wasn't deleted when needed.
36 * Revision 1.7 1995/11/28 09:09:43 adam
37 * Zebra config renamed.
38 * Use setting 'recordId' to identify record now.
39 * Bug fix in recindex.c: rec_release_blocks was invokeded even
40 * though the blocks were already released.
41 * File traversal properly deletes records when needed.
43 * Revision 1.6 1995/11/25 10:24:06 adam
44 * More record fields - they are enumerated now.
45 * New options: flagStoreData flagStoreKey.
47 * Revision 1.5 1995/11/22 17:19:18 adam
48 * Record management uses the bfile system.
50 * Revision 1.4 1995/11/20 16:59:46 adam
51 * New update method: the 'old' keys are saved for each records.
53 * Revision 1.3 1995/11/16 15:34:55 adam
54 * Uses new record management system in both indexer and server.
56 * Revision 1.2 1995/11/15 19:13:08 adam
57 * Work on record management.
59 * Revision 1.1 1995/11/15 14:46:20 adam
60 * Started work on better record management system.
70 static void rec_write_head (Records p)
75 assert (p->index_BFile);
77 r = bf_write (p->index_BFile, 0, 0, sizeof(p->head), &p->head);
80 logf (LOG_FATAL|LOG_ERRNO, "write head of %s", p->index_fname);
85 static void rec_tmp_expand (Records p, int size, int dst_type)
87 if (p->tmp_size < size + 256 ||
88 p->tmp_size < p->head.block_size[dst_type]*2)
91 p->tmp_size = size + p->head.block_size[dst_type]*2 + 256;
92 p->tmp_buf = xmalloc (p->tmp_size);
96 static int read_indx (Records p, int sysno, void *buf, int itemsize,
100 int pos = (sysno-1)*itemsize;
102 r = bf_read (p->index_BFile, 1+pos/128, pos%128, itemsize, buf);
103 if (r != 1 && !ignoreError)
105 logf (LOG_FATAL|LOG_ERRNO, "read in %s at pos %ld",
106 p->index_fname, (long) pos);
113 static void write_indx (Records p, int sysno, void *buf, int itemsize)
115 int pos = (sysno-1)*itemsize;
117 bf_write (p->index_BFile, 1+pos/128, pos%128, itemsize, buf);
120 static void rec_release_blocks (Records p, int sysno)
122 struct record_index_entry entry;
123 int freeblock, freenext;
126 if (read_indx (p, sysno, &entry, sizeof(entry), 1) != 1)
128 p->head.total_bytes -= entry.size;
129 freeblock = entry.next;
130 assert (freeblock > 0);
131 dst_type = freeblock & 7;
132 assert (dst_type < REC_BLOCK_TYPES);
133 freeblock = freeblock / 8;
136 if (bf_read (p->data_BFile[dst_type], freeblock, 0, sizeof(freenext),
139 logf (LOG_FATAL|LOG_ERRNO, "read in rec_del_single");
142 if (bf_write (p->data_BFile[dst_type], freeblock, 0, sizeof(freenext),
143 &p->head.block_free[dst_type]))
145 logf (LOG_FATAL|LOG_ERRNO, "write in rec_del_single");
148 p->head.block_free[dst_type] = freeblock;
149 freeblock = freenext;
150 p->head.block_used[dst_type]--;
154 static void rec_delete_single (Records p, Record rec)
156 struct record_index_entry entry;
158 rec_release_blocks (p, rec->sysno);
160 entry.next = p->head.index_free;
162 p->head.index_free = rec->sysno;
163 write_indx (p, rec->sysno, &entry, sizeof(entry));
167 static void rec_write_single (Records p, Record rec)
173 int block_prev = -1, block_free;
174 struct record_index_entry entry;
176 for (i = 0; i < REC_NO_INFO; i++)
178 size += sizeof(*rec->size);
180 size += sizeof(*rec->size) + rec->size[i];
182 for (i = 1; i<REC_BLOCK_TYPES; i++)
183 if (size >= p->head.block_move[i])
186 rec_tmp_expand (p, size, dst_type);
188 cptr = p->tmp_buf + sizeof(int); /* a hack! */
189 for (i = 0; i < REC_NO_INFO; i++)
191 memcpy (cptr, &rec->size[i], sizeof(*rec->size));
192 cptr += sizeof(*rec->size);
195 memcpy (cptr, rec->info[i], rec->size[i]);
196 cptr += rec->size[i];
200 while (no_written < size)
202 block_free = p->head.block_free[dst_type];
205 if (bf_read (p->data_BFile[dst_type],
206 block_free, 0, sizeof(*p->head.block_free),
207 &p->head.block_free[dst_type]) != 1)
209 logf (LOG_FATAL|LOG_ERRNO, "read in %s at free block %d",
210 p->data_fname[dst_type], block_free);
214 block_free = p->head.block_last[dst_type]++;
215 if (block_prev == -1)
217 entry.next = block_free*8 + dst_type;
219 p->head.total_bytes += size;
220 write_indx (p, rec->sysno, &entry, sizeof(entry));
224 memcpy (cptr, &block_free, sizeof(int));
225 bf_write (p->data_BFile[dst_type], block_prev, 0, 0, cptr);
226 cptr = p->tmp_buf + no_written;
228 block_prev = block_free;
229 no_written += p->head.block_size[dst_type] - sizeof(int);
230 p->head.block_used[dst_type]++;
232 assert (block_prev != -1);
234 memcpy (cptr, &block_free, sizeof(int));
235 bf_write (p->data_BFile[dst_type], block_prev, 0,
236 sizeof(int) + (p->tmp_buf+size) - cptr, cptr);
239 static void rec_update_single (Records p, Record rec)
241 rec_release_blocks (p, rec->sysno);
242 rec_write_single (p, rec);
245 Records rec_open (int rw)
250 p = xmalloc (sizeof(*p));
253 p->tmp_buf = xmalloc (p->tmp_size);
254 p->index_fname = "recindex";
255 p->index_BFile = bf_open (p->index_fname, 128, rw);
256 if (p->index_BFile == NULL)
258 logf (LOG_FATAL|LOG_ERRNO, "open %s", p->index_fname);
261 r = bf_read (p->index_BFile, 0, 0, 0, p->tmp_buf);
265 memcpy (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic));
266 p->head.index_free = 0;
267 p->head.index_last = 1;
268 p->head.no_records = 0;
269 p->head.total_bytes = 0;
270 for (i = 0; i<REC_BLOCK_TYPES; i++)
272 p->head.block_free[i] = 0;
273 p->head.block_last[i] = 1;
274 p->head.block_used[i] = 0;
276 p->head.block_size[0] = 128;
277 p->head.block_move[0] = 0;
278 for (i = 1; i<REC_BLOCK_TYPES; i++)
280 p->head.block_size[i] = p->head.block_size[i-1] * 4;
281 p->head.block_move[i] = p->head.block_size[i] * 3;
287 memcpy (&p->head, p->tmp_buf, sizeof(p->head));
288 if (memcmp (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic)))
290 logf (LOG_FATAL, "read %s. bad header", p->index_fname);
295 for (i = 0; i<REC_BLOCK_TYPES; i++)
298 sprintf (str, "recdata%c", i + 'A');
299 p->data_fname[i] = xmalloc (strlen(str)+1);
300 strcpy (p->data_fname[i], str);
301 p->data_BFile[i] = NULL;
303 for (i = 0; i<REC_BLOCK_TYPES; i++)
305 if (!(p->data_BFile[i] = bf_open (p->data_fname[i],
306 p->head.block_size[i],
309 logf (LOG_FATAL|LOG_ERRNO, "bf_open %s", p->data_fname[i]);
315 p->record_cache = xmalloc (sizeof(*p->record_cache)*p->cache_max);
319 static void rec_cache_flush (Records p, int saveCount)
323 if (saveCount >= p->cache_cur)
325 for (i = 0; i<p->cache_cur - saveCount; i++)
327 struct record_cache_entry *e = p->record_cache + i;
333 rec_write_single (p, e->rec);
335 case recordFlagWrite:
336 rec_update_single (p, e->rec);
338 case recordFlagDelete:
339 rec_delete_single (p, e->rec);
344 for (j = 0; j<saveCount; j++, i++)
345 memcpy (p->record_cache+j, p->record_cache+i,
346 sizeof(*p->record_cache));
347 p->cache_cur = saveCount;
350 static Record *rec_cache_lookup (Records p, int sysno,
351 enum recordCacheFlag flag)
354 for (i = 0; i<p->cache_cur; i++)
356 struct record_cache_entry *e = p->record_cache + i;
357 if (e->rec->sysno == sysno)
359 if (flag != recordFlagNop && e->flag == recordFlagNop)
367 static void rec_cache_insert (Records p, Record rec, enum recordCacheFlag flag)
369 struct record_cache_entry *e;
371 if (p->cache_cur == p->cache_max)
372 rec_cache_flush (p, 1);
373 else if (p->cache_cur > 2)
377 for (i = 0; i<p->cache_cur; i++)
379 Record r = (p->record_cache + i)->rec;
380 for (j = 0; j<REC_NO_INFO; j++)
384 rec_cache_flush (p, 1);
386 assert (p->cache_cur < p->cache_max);
388 e = p->record_cache + (p->cache_cur)++;
390 e->rec = rec_cp (rec);
393 void rec_close (Records *pp)
400 rec_cache_flush (p, 0);
401 xfree (p->record_cache);
407 bf_close (p->index_BFile);
409 for (i = 0; i<REC_BLOCK_TYPES; i++)
411 if (p->data_BFile[i])
412 bf_close (p->data_BFile[i]);
413 xfree (p->data_fname[i]);
421 Record rec_get (Records p, int sysno)
425 struct record_index_entry entry;
426 int freeblock, dst_type;
432 if ((recp = rec_cache_lookup (p, sysno, recordFlagNop)))
433 return rec_cp (*recp);
435 if (!read_indx (p, sysno, &entry, sizeof(entry), 1))
436 return NULL; /* record is not there! */
439 return NULL; /* record is deleted */
441 dst_type = entry.next & 7;
442 assert (dst_type < REC_BLOCK_TYPES);
443 freeblock = entry.next / 8;
445 assert (freeblock > 0);
447 rec = xmalloc (sizeof(*rec));
448 rec_tmp_expand (p, entry.size, dst_type);
451 bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
452 memcpy (&freeblock, cptr, sizeof(freeblock));
458 cptr += p->head.block_size[dst_type] - sizeof(freeblock);
460 memcpy (&tmp, cptr, sizeof(tmp));
461 bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
462 memcpy (&freeblock, cptr, sizeof(freeblock));
463 memcpy (cptr, &tmp, sizeof(tmp));
467 nptr = p->tmp_buf + sizeof(freeblock);
468 for (i = 0; i < REC_NO_INFO; i++)
470 memcpy (&rec->size[i], nptr, sizeof(*rec->size));
471 nptr += sizeof(*rec->size);
474 rec->info[i] = xmalloc (rec->size[i]);
475 memcpy (rec->info[i], nptr, rec->size[i]);
476 nptr += rec->size[i];
481 rec_cache_insert (p, rec, recordFlagNop);
485 Record rec_new (Records p)
491 rec = xmalloc (sizeof(*rec));
492 if (p->head.index_free == 0)
493 sysno = (p->head.index_last)++;
496 struct record_index_entry entry;
498 read_indx (p, p->head.index_free, &entry, sizeof(entry), 0);
499 sysno = p->head.index_free;
500 p->head.index_free = entry.next;
502 (p->head.no_records)++;
504 for (i = 0; i < REC_NO_INFO; i++)
509 rec_cache_insert (p, rec, recordFlagNew);
513 void rec_del (Records p, Record *recpp)
517 (p->head.no_records)--;
518 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagDelete)))
525 rec_cache_insert (p, *recpp, recordFlagDelete);
531 void rec_put (Records p, Record *recpp)
535 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagWrite)))
542 rec_cache_insert (p, *recpp, recordFlagWrite);
548 void rec_rm (Record *recpp)
551 for (i = 0; i < REC_NO_INFO; i++)
552 xfree ((*recpp)->info[i]);
557 Record rec_cp (Record rec)
562 n = xmalloc (sizeof(*n));
563 n->sysno = rec->sysno;
564 for (i = 0; i < REC_NO_INFO; i++)
572 n->size[i] = rec->size[i];
573 n->info[i] = xmalloc (rec->size[i]);
574 memcpy (n->info[i], rec->info[i], rec->size[i]);
580 char *rec_strdup (const char *s, size_t *len)