2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.14 1996-02-01 20:48:15 adam
8 * The total size of records are always checked in rec_cache_insert to
11 * Revision 1.13 1995/12/11 09:12:49 adam
12 * The rec_get function returns NULL if record doesn't exist - will
13 * happen in the server if the result set records have been deleted since
14 * the creation of the set (i.e. the search).
15 * The server saves a result temporarily if it is 'volatile', i.e. the
16 * set is register dependent.
18 * Revision 1.12 1995/12/07 17:38:47 adam
19 * Work locking mechanisms for concurrent updates/commit.
21 * Revision 1.11 1995/12/06 13:58:26 adam
22 * Improved flushing of records - all flushes except the last one
23 * don't write the last accessed. Also flush takes place if record
24 * info occupy more than about 256k.
26 * Revision 1.10 1995/12/06 12:41:24 adam
27 * New command 'stat' for the index program.
28 * Filenames can be read from stdin by specifying '-'.
29 * Bug fix/enhancement of the transformation from terms to regular
30 * expressons in the search engine.
32 * Revision 1.9 1995/11/30 08:34:33 adam
33 * Started work on commit facility.
34 * Changed a few malloc/free to xmalloc/xfree.
36 * Revision 1.8 1995/11/28 14:26:21 adam
37 * Bug fix: recordId with constant wasn't right.
38 * Bug fix: recordId dictionary entry wasn't deleted when needed.
40 * Revision 1.7 1995/11/28 09:09:43 adam
41 * Zebra config renamed.
42 * Use setting 'recordId' to identify record now.
43 * Bug fix in recindex.c: rec_release_blocks was invokeded even
44 * though the blocks were already released.
45 * File traversal properly deletes records when needed.
47 * Revision 1.6 1995/11/25 10:24:06 adam
48 * More record fields - they are enumerated now.
49 * New options: flagStoreData flagStoreKey.
51 * Revision 1.5 1995/11/22 17:19:18 adam
52 * Record management uses the bfile system.
54 * Revision 1.4 1995/11/20 16:59:46 adam
55 * New update method: the 'old' keys are saved for each records.
57 * Revision 1.3 1995/11/16 15:34:55 adam
58 * Uses new record management system in both indexer and server.
60 * Revision 1.2 1995/11/15 19:13:08 adam
61 * Work on record management.
63 * Revision 1.1 1995/11/15 14:46:20 adam
64 * Started work on better record management system.
74 static void rec_write_head (Records p)
79 assert (p->index_BFile);
81 r = bf_write (p->index_BFile, 0, 0, sizeof(p->head), &p->head);
84 logf (LOG_FATAL|LOG_ERRNO, "write head of %s", p->index_fname);
89 static void rec_tmp_expand (Records p, int size, int dst_type)
91 if (p->tmp_size < size + 256 ||
92 p->tmp_size < p->head.block_size[dst_type]*2)
95 p->tmp_size = size + p->head.block_size[dst_type]*2 + 256;
96 p->tmp_buf = xmalloc (p->tmp_size);
100 static int read_indx (Records p, int sysno, void *buf, int itemsize,
104 int pos = (sysno-1)*itemsize;
106 r = bf_read (p->index_BFile, 1+pos/128, pos%128, itemsize, buf);
107 if (r != 1 && !ignoreError)
109 logf (LOG_FATAL|LOG_ERRNO, "read in %s at pos %ld",
110 p->index_fname, (long) pos);
117 static void write_indx (Records p, int sysno, void *buf, int itemsize)
119 int pos = (sysno-1)*itemsize;
121 bf_write (p->index_BFile, 1+pos/128, pos%128, itemsize, buf);
124 static void rec_release_blocks (Records p, int sysno)
126 struct record_index_entry entry;
127 int freeblock, freenext;
130 if (read_indx (p, sysno, &entry, sizeof(entry), 1) != 1)
132 p->head.total_bytes -= entry.size;
133 freeblock = entry.next;
134 assert (freeblock > 0);
135 dst_type = freeblock & 7;
136 assert (dst_type < REC_BLOCK_TYPES);
137 freeblock = freeblock / 8;
140 if (bf_read (p->data_BFile[dst_type], freeblock, 0, sizeof(freenext),
143 logf (LOG_FATAL|LOG_ERRNO, "read in rec_del_single");
146 if (bf_write (p->data_BFile[dst_type], freeblock, 0, sizeof(freenext),
147 &p->head.block_free[dst_type]))
149 logf (LOG_FATAL|LOG_ERRNO, "write in rec_del_single");
152 p->head.block_free[dst_type] = freeblock;
153 freeblock = freenext;
154 p->head.block_used[dst_type]--;
158 static void rec_delete_single (Records p, Record rec)
160 struct record_index_entry entry;
162 rec_release_blocks (p, rec->sysno);
164 entry.next = p->head.index_free;
166 p->head.index_free = rec->sysno;
167 write_indx (p, rec->sysno, &entry, sizeof(entry));
171 static void rec_write_single (Records p, Record rec)
177 int block_prev = -1, block_free;
178 struct record_index_entry entry;
180 for (i = 0; i < REC_NO_INFO; i++)
182 size += sizeof(*rec->size);
184 size += sizeof(*rec->size) + rec->size[i];
186 for (i = 1; i<REC_BLOCK_TYPES; i++)
187 if (size >= p->head.block_move[i])
190 rec_tmp_expand (p, size, dst_type);
192 cptr = p->tmp_buf + sizeof(int); /* a hack! */
193 for (i = 0; i < REC_NO_INFO; i++)
195 memcpy (cptr, &rec->size[i], sizeof(*rec->size));
196 cptr += sizeof(*rec->size);
199 memcpy (cptr, rec->info[i], rec->size[i]);
200 cptr += rec->size[i];
204 while (no_written < size)
206 block_free = p->head.block_free[dst_type];
209 if (bf_read (p->data_BFile[dst_type],
210 block_free, 0, sizeof(*p->head.block_free),
211 &p->head.block_free[dst_type]) != 1)
213 logf (LOG_FATAL|LOG_ERRNO, "read in %s at free block %d",
214 p->data_fname[dst_type], block_free);
218 block_free = p->head.block_last[dst_type]++;
219 if (block_prev == -1)
221 entry.next = block_free*8 + dst_type;
223 p->head.total_bytes += size;
224 write_indx (p, rec->sysno, &entry, sizeof(entry));
228 memcpy (cptr, &block_free, sizeof(int));
229 bf_write (p->data_BFile[dst_type], block_prev, 0, 0, cptr);
230 cptr = p->tmp_buf + no_written;
232 block_prev = block_free;
233 no_written += p->head.block_size[dst_type] - sizeof(int);
234 p->head.block_used[dst_type]++;
236 assert (block_prev != -1);
238 memcpy (cptr, &block_free, sizeof(int));
239 bf_write (p->data_BFile[dst_type], block_prev, 0,
240 sizeof(int) + (p->tmp_buf+size) - cptr, cptr);
243 static void rec_update_single (Records p, Record rec)
245 rec_release_blocks (p, rec->sysno);
246 rec_write_single (p, rec);
249 Records rec_open (int rw)
254 p = xmalloc (sizeof(*p));
257 p->tmp_buf = xmalloc (p->tmp_size);
258 p->index_fname = "recindex";
259 p->index_BFile = bf_open (p->index_fname, 128, rw);
260 if (p->index_BFile == NULL)
262 logf (LOG_FATAL|LOG_ERRNO, "open %s", p->index_fname);
265 r = bf_read (p->index_BFile, 0, 0, 0, p->tmp_buf);
269 memcpy (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic));
270 p->head.index_free = 0;
271 p->head.index_last = 1;
272 p->head.no_records = 0;
273 p->head.total_bytes = 0;
274 for (i = 0; i<REC_BLOCK_TYPES; i++)
276 p->head.block_free[i] = 0;
277 p->head.block_last[i] = 1;
278 p->head.block_used[i] = 0;
280 p->head.block_size[0] = 128;
281 p->head.block_move[0] = 0;
282 for (i = 1; i<REC_BLOCK_TYPES; i++)
284 p->head.block_size[i] = p->head.block_size[i-1] * 4;
285 p->head.block_move[i] = p->head.block_size[i] * 3;
291 memcpy (&p->head, p->tmp_buf, sizeof(p->head));
292 if (memcmp (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic)))
294 logf (LOG_FATAL, "read %s. bad header", p->index_fname);
299 for (i = 0; i<REC_BLOCK_TYPES; i++)
302 sprintf (str, "recdata%c", i + 'A');
303 p->data_fname[i] = xmalloc (strlen(str)+1);
304 strcpy (p->data_fname[i], str);
305 p->data_BFile[i] = NULL;
307 for (i = 0; i<REC_BLOCK_TYPES; i++)
309 if (!(p->data_BFile[i] = bf_open (p->data_fname[i],
310 p->head.block_size[i],
313 logf (LOG_FATAL|LOG_ERRNO, "bf_open %s", p->data_fname[i]);
319 p->record_cache = xmalloc (sizeof(*p->record_cache)*p->cache_max);
323 static void rec_cache_flush (Records p, int saveCount)
327 if (saveCount >= p->cache_cur)
329 for (i = 0; i<p->cache_cur - saveCount; i++)
331 struct record_cache_entry *e = p->record_cache + i;
337 rec_write_single (p, e->rec);
339 case recordFlagWrite:
340 rec_update_single (p, e->rec);
342 case recordFlagDelete:
343 rec_delete_single (p, e->rec);
348 for (j = 0; j<saveCount; j++, i++)
349 memcpy (p->record_cache+j, p->record_cache+i,
350 sizeof(*p->record_cache));
351 p->cache_cur = saveCount;
354 static Record *rec_cache_lookup (Records p, int sysno,
355 enum recordCacheFlag flag)
358 for (i = 0; i<p->cache_cur; i++)
360 struct record_cache_entry *e = p->record_cache + i;
361 if (e->rec->sysno == sysno)
363 if (flag != recordFlagNop && e->flag == recordFlagNop)
371 static void rec_cache_insert (Records p, Record rec, enum recordCacheFlag flag)
373 struct record_cache_entry *e;
375 if (p->cache_cur == p->cache_max)
376 rec_cache_flush (p, 1);
377 else if (p->cache_cur > 0)
381 for (i = 0; i<p->cache_cur; i++)
383 Record r = (p->record_cache + i)->rec;
384 for (j = 0; j<REC_NO_INFO; j++)
388 rec_cache_flush (p, 1);
390 assert (p->cache_cur < p->cache_max);
392 e = p->record_cache + (p->cache_cur)++;
394 e->rec = rec_cp (rec);
397 void rec_close (Records *pp)
404 rec_cache_flush (p, 0);
405 xfree (p->record_cache);
411 bf_close (p->index_BFile);
413 for (i = 0; i<REC_BLOCK_TYPES; i++)
415 if (p->data_BFile[i])
416 bf_close (p->data_BFile[i]);
417 xfree (p->data_fname[i]);
425 Record rec_get (Records p, int sysno)
429 struct record_index_entry entry;
430 int freeblock, dst_type;
436 if ((recp = rec_cache_lookup (p, sysno, recordFlagNop)))
437 return rec_cp (*recp);
439 if (!read_indx (p, sysno, &entry, sizeof(entry), 1))
440 return NULL; /* record is not there! */
443 return NULL; /* record is deleted */
445 dst_type = entry.next & 7;
446 assert (dst_type < REC_BLOCK_TYPES);
447 freeblock = entry.next / 8;
449 assert (freeblock > 0);
451 rec = xmalloc (sizeof(*rec));
452 rec_tmp_expand (p, entry.size, dst_type);
455 bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
456 memcpy (&freeblock, cptr, sizeof(freeblock));
462 cptr += p->head.block_size[dst_type] - sizeof(freeblock);
464 memcpy (&tmp, cptr, sizeof(tmp));
465 bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
466 memcpy (&freeblock, cptr, sizeof(freeblock));
467 memcpy (cptr, &tmp, sizeof(tmp));
471 nptr = p->tmp_buf + sizeof(freeblock);
472 for (i = 0; i < REC_NO_INFO; i++)
474 memcpy (&rec->size[i], nptr, sizeof(*rec->size));
475 nptr += sizeof(*rec->size);
478 rec->info[i] = xmalloc (rec->size[i]);
479 memcpy (rec->info[i], nptr, rec->size[i]);
480 nptr += rec->size[i];
485 rec_cache_insert (p, rec, recordFlagNop);
489 Record rec_new (Records p)
495 rec = xmalloc (sizeof(*rec));
496 if (p->head.index_free == 0)
497 sysno = (p->head.index_last)++;
500 struct record_index_entry entry;
502 read_indx (p, p->head.index_free, &entry, sizeof(entry), 0);
503 sysno = p->head.index_free;
504 p->head.index_free = entry.next;
506 (p->head.no_records)++;
508 for (i = 0; i < REC_NO_INFO; i++)
513 rec_cache_insert (p, rec, recordFlagNew);
517 void rec_del (Records p, Record *recpp)
521 (p->head.no_records)--;
522 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagDelete)))
529 rec_cache_insert (p, *recpp, recordFlagDelete);
535 void rec_put (Records p, Record *recpp)
539 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagWrite)))
546 rec_cache_insert (p, *recpp, recordFlagWrite);
552 void rec_rm (Record *recpp)
555 for (i = 0; i < REC_NO_INFO; i++)
556 xfree ((*recpp)->info[i]);
561 Record rec_cp (Record rec)
566 n = xmalloc (sizeof(*n));
567 n->sysno = rec->sysno;
568 for (i = 0; i < REC_NO_INFO; i++)
576 n->size[i] = rec->size[i];
577 n->info[i] = xmalloc (rec->size[i]);
578 memcpy (n->info[i], rec->info[i], rec->size[i]);
584 char *rec_strdup (const char *s, size_t *len)