2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.15 1996-05-13 14:23:06 adam
8 * Work on compaction of set/use bytes in dictionary.
10 * Revision 1.14 1996/02/01 20:48:15 adam
11 * The total size of records are always checked in rec_cache_insert to
12 * reduce memory usage.
14 * Revision 1.13 1995/12/11 09:12:49 adam
15 * The rec_get function returns NULL if record doesn't exist - will
16 * happen in the server if the result set records have been deleted since
17 * the creation of the set (i.e. the search).
18 * The server saves a result temporarily if it is 'volatile', i.e. the
19 * set is register dependent.
21 * Revision 1.12 1995/12/07 17:38:47 adam
22 * Work locking mechanisms for concurrent updates/commit.
24 * Revision 1.11 1995/12/06 13:58:26 adam
25 * Improved flushing of records - all flushes except the last one
26 * don't write the last accessed. Also flush takes place if record
27 * info occupy more than about 256k.
29 * Revision 1.10 1995/12/06 12:41:24 adam
30 * New command 'stat' for the index program.
31 * Filenames can be read from stdin by specifying '-'.
32 * Bug fix/enhancement of the transformation from terms to regular
33 * expressons in the search engine.
35 * Revision 1.9 1995/11/30 08:34:33 adam
36 * Started work on commit facility.
37 * Changed a few malloc/free to xmalloc/xfree.
39 * Revision 1.8 1995/11/28 14:26:21 adam
40 * Bug fix: recordId with constant wasn't right.
41 * Bug fix: recordId dictionary entry wasn't deleted when needed.
43 * Revision 1.7 1995/11/28 09:09:43 adam
44 * Zebra config renamed.
45 * Use setting 'recordId' to identify record now.
46 * Bug fix in recindex.c: rec_release_blocks was invokeded even
47 * though the blocks were already released.
48 * File traversal properly deletes records when needed.
50 * Revision 1.6 1995/11/25 10:24:06 adam
51 * More record fields - they are enumerated now.
52 * New options: flagStoreData flagStoreKey.
54 * Revision 1.5 1995/11/22 17:19:18 adam
55 * Record management uses the bfile system.
57 * Revision 1.4 1995/11/20 16:59:46 adam
58 * New update method: the 'old' keys are saved for each records.
60 * Revision 1.3 1995/11/16 15:34:55 adam
61 * Uses new record management system in both indexer and server.
63 * Revision 1.2 1995/11/15 19:13:08 adam
64 * Work on record management.
66 * Revision 1.1 1995/11/15 14:46:20 adam
67 * Started work on better record management system.
77 static void rec_write_head (Records p)
82 assert (p->index_BFile);
84 r = bf_write (p->index_BFile, 0, 0, sizeof(p->head), &p->head);
87 logf (LOG_FATAL|LOG_ERRNO, "write head of %s", p->index_fname);
92 static void rec_tmp_expand (Records p, int size, int dst_type)
94 if (p->tmp_size < size + 256 ||
95 p->tmp_size < p->head.block_size[dst_type]*2)
98 p->tmp_size = size + p->head.block_size[dst_type]*2 + 256;
99 p->tmp_buf = xmalloc (p->tmp_size);
103 static int read_indx (Records p, int sysno, void *buf, int itemsize,
107 int pos = (sysno-1)*itemsize;
109 r = bf_read (p->index_BFile, 1+pos/128, pos%128, itemsize, buf);
110 if (r != 1 && !ignoreError)
112 logf (LOG_FATAL|LOG_ERRNO, "read in %s at pos %ld",
113 p->index_fname, (long) pos);
120 static void write_indx (Records p, int sysno, void *buf, int itemsize)
122 int pos = (sysno-1)*itemsize;
124 bf_write (p->index_BFile, 1+pos/128, pos%128, itemsize, buf);
127 static void rec_release_blocks (Records p, int sysno)
129 struct record_index_entry entry;
130 int freeblock, freenext;
133 if (read_indx (p, sysno, &entry, sizeof(entry), 1) != 1)
135 p->head.total_bytes -= entry.size;
136 freeblock = entry.next;
137 assert (freeblock > 0);
138 dst_type = freeblock & 7;
139 assert (dst_type < REC_BLOCK_TYPES);
140 freeblock = freeblock / 8;
143 if (bf_read (p->data_BFile[dst_type], freeblock, 0, sizeof(freenext),
146 logf (LOG_FATAL|LOG_ERRNO, "read in rec_del_single");
149 if (bf_write (p->data_BFile[dst_type], freeblock, 0, sizeof(freenext),
150 &p->head.block_free[dst_type]))
152 logf (LOG_FATAL|LOG_ERRNO, "write in rec_del_single");
155 p->head.block_free[dst_type] = freeblock;
156 freeblock = freenext;
157 p->head.block_used[dst_type]--;
161 static void rec_delete_single (Records p, Record rec)
163 struct record_index_entry entry;
165 rec_release_blocks (p, rec->sysno);
167 entry.next = p->head.index_free;
169 p->head.index_free = rec->sysno;
170 write_indx (p, rec->sysno, &entry, sizeof(entry));
174 static void rec_write_single (Records p, Record rec)
180 int block_prev = -1, block_free;
181 struct record_index_entry entry;
183 for (i = 0; i < REC_NO_INFO; i++)
185 size += sizeof(*rec->size);
187 size += sizeof(*rec->size) + rec->size[i];
189 for (i = 1; i<REC_BLOCK_TYPES; i++)
190 if (size >= p->head.block_move[i])
193 rec_tmp_expand (p, size, dst_type);
195 cptr = p->tmp_buf + sizeof(int); /* a hack! */
196 for (i = 0; i < REC_NO_INFO; i++)
198 memcpy (cptr, &rec->size[i], sizeof(*rec->size));
199 cptr += sizeof(*rec->size);
202 memcpy (cptr, rec->info[i], rec->size[i]);
203 cptr += rec->size[i];
207 while (no_written < size)
209 block_free = p->head.block_free[dst_type];
212 if (bf_read (p->data_BFile[dst_type],
213 block_free, 0, sizeof(*p->head.block_free),
214 &p->head.block_free[dst_type]) != 1)
216 logf (LOG_FATAL|LOG_ERRNO, "read in %s at free block %d",
217 p->data_fname[dst_type], block_free);
221 block_free = p->head.block_last[dst_type]++;
222 if (block_prev == -1)
224 entry.next = block_free*8 + dst_type;
226 p->head.total_bytes += size;
227 write_indx (p, rec->sysno, &entry, sizeof(entry));
231 memcpy (cptr, &block_free, sizeof(int));
232 bf_write (p->data_BFile[dst_type], block_prev, 0, 0, cptr);
233 cptr = p->tmp_buf + no_written;
235 block_prev = block_free;
236 no_written += p->head.block_size[dst_type] - sizeof(int);
237 p->head.block_used[dst_type]++;
239 assert (block_prev != -1);
241 memcpy (cptr, &block_free, sizeof(int));
242 bf_write (p->data_BFile[dst_type], block_prev, 0,
243 sizeof(int) + (p->tmp_buf+size) - cptr, cptr);
246 static void rec_update_single (Records p, Record rec)
248 rec_release_blocks (p, rec->sysno);
249 rec_write_single (p, rec);
252 Records rec_open (int rw)
257 p = xmalloc (sizeof(*p));
260 p->tmp_buf = xmalloc (p->tmp_size);
261 p->index_fname = "recindex";
262 p->index_BFile = bf_open (p->index_fname, 128, rw);
263 if (p->index_BFile == NULL)
265 logf (LOG_FATAL|LOG_ERRNO, "open %s", p->index_fname);
268 r = bf_read (p->index_BFile, 0, 0, 0, p->tmp_buf);
272 memcpy (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic));
273 p->head.index_free = 0;
274 p->head.index_last = 1;
275 p->head.no_records = 0;
276 p->head.total_bytes = 0;
277 for (i = 0; i<REC_BLOCK_TYPES; i++)
279 p->head.block_free[i] = 0;
280 p->head.block_last[i] = 1;
281 p->head.block_used[i] = 0;
283 p->head.block_size[0] = 128;
284 p->head.block_move[0] = 0;
285 for (i = 1; i<REC_BLOCK_TYPES; i++)
287 p->head.block_size[i] = p->head.block_size[i-1] * 4;
288 p->head.block_move[i] = p->head.block_size[i] * 3;
294 memcpy (&p->head, p->tmp_buf, sizeof(p->head));
295 if (memcmp (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic)))
297 logf (LOG_FATAL, "read %s. bad header", p->index_fname);
302 for (i = 0; i<REC_BLOCK_TYPES; i++)
305 sprintf (str, "recdata%c", i + 'A');
306 p->data_fname[i] = xmalloc (strlen(str)+1);
307 strcpy (p->data_fname[i], str);
308 p->data_BFile[i] = NULL;
310 for (i = 0; i<REC_BLOCK_TYPES; i++)
312 if (!(p->data_BFile[i] = bf_open (p->data_fname[i],
313 p->head.block_size[i],
316 logf (LOG_FATAL|LOG_ERRNO, "bf_open %s", p->data_fname[i]);
322 p->record_cache = xmalloc (sizeof(*p->record_cache)*p->cache_max);
326 static void rec_cache_flush (Records p, int saveCount)
330 if (saveCount >= p->cache_cur)
332 for (i = 0; i<p->cache_cur - saveCount; i++)
334 struct record_cache_entry *e = p->record_cache + i;
340 rec_write_single (p, e->rec);
342 case recordFlagWrite:
343 rec_update_single (p, e->rec);
345 case recordFlagDelete:
346 rec_delete_single (p, e->rec);
351 for (j = 0; j<saveCount; j++, i++)
352 memcpy (p->record_cache+j, p->record_cache+i,
353 sizeof(*p->record_cache));
354 p->cache_cur = saveCount;
357 static Record *rec_cache_lookup (Records p, int sysno,
358 enum recordCacheFlag flag)
361 for (i = 0; i<p->cache_cur; i++)
363 struct record_cache_entry *e = p->record_cache + i;
364 if (e->rec->sysno == sysno)
366 if (flag != recordFlagNop && e->flag == recordFlagNop)
374 static void rec_cache_insert (Records p, Record rec, enum recordCacheFlag flag)
376 struct record_cache_entry *e;
378 if (p->cache_cur == p->cache_max)
379 rec_cache_flush (p, 1);
380 else if (p->cache_cur > 0)
384 for (i = 0; i<p->cache_cur; i++)
386 Record r = (p->record_cache + i)->rec;
387 for (j = 0; j<REC_NO_INFO; j++)
391 rec_cache_flush (p, 1);
393 assert (p->cache_cur < p->cache_max);
395 e = p->record_cache + (p->cache_cur)++;
397 e->rec = rec_cp (rec);
400 void rec_close (Records *pp)
407 rec_cache_flush (p, 0);
408 xfree (p->record_cache);
414 bf_close (p->index_BFile);
416 for (i = 0; i<REC_BLOCK_TYPES; i++)
418 if (p->data_BFile[i])
419 bf_close (p->data_BFile[i]);
420 xfree (p->data_fname[i]);
428 Record rec_get (Records p, int sysno)
432 struct record_index_entry entry;
433 int freeblock, dst_type;
439 if ((recp = rec_cache_lookup (p, sysno, recordFlagNop)))
440 return rec_cp (*recp);
442 if (!read_indx (p, sysno, &entry, sizeof(entry), 1))
443 return NULL; /* record is not there! */
446 return NULL; /* record is deleted */
448 dst_type = entry.next & 7;
449 assert (dst_type < REC_BLOCK_TYPES);
450 freeblock = entry.next / 8;
452 assert (freeblock > 0);
454 rec = xmalloc (sizeof(*rec));
455 rec_tmp_expand (p, entry.size, dst_type);
458 bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
459 memcpy (&freeblock, cptr, sizeof(freeblock));
465 cptr += p->head.block_size[dst_type] - sizeof(freeblock);
467 memcpy (&tmp, cptr, sizeof(tmp));
468 bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
469 memcpy (&freeblock, cptr, sizeof(freeblock));
470 memcpy (cptr, &tmp, sizeof(tmp));
474 nptr = p->tmp_buf + sizeof(freeblock);
475 for (i = 0; i < REC_NO_INFO; i++)
477 memcpy (&rec->size[i], nptr, sizeof(*rec->size));
478 nptr += sizeof(*rec->size);
481 rec->info[i] = xmalloc (rec->size[i]);
482 memcpy (rec->info[i], nptr, rec->size[i]);
483 nptr += rec->size[i];
488 rec_cache_insert (p, rec, recordFlagNop);
492 Record rec_new (Records p)
498 rec = xmalloc (sizeof(*rec));
499 if (p->head.index_free == 0)
500 sysno = (p->head.index_last)++;
503 struct record_index_entry entry;
505 read_indx (p, p->head.index_free, &entry, sizeof(entry), 0);
506 sysno = p->head.index_free;
507 p->head.index_free = entry.next;
509 (p->head.no_records)++;
511 for (i = 0; i < REC_NO_INFO; i++)
516 rec_cache_insert (p, rec, recordFlagNew);
520 void rec_del (Records p, Record *recpp)
524 (p->head.no_records)--;
525 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagDelete)))
532 rec_cache_insert (p, *recpp, recordFlagDelete);
538 void rec_put (Records p, Record *recpp)
542 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagWrite)))
549 rec_cache_insert (p, *recpp, recordFlagWrite);
555 void rec_rm (Record *recpp)
561 for (i = 0; i < REC_NO_INFO; i++)
562 xfree ((*recpp)->info[i]);
567 Record rec_cp (Record rec)
572 n = xmalloc (sizeof(*n));
573 n->sysno = rec->sysno;
574 for (i = 0; i < REC_NO_INFO; i++)
582 n->size[i] = rec->size[i];
583 n->info[i] = xmalloc (rec->size[i]);
584 memcpy (n->info[i], rec->info[i], rec->size[i]);
590 char *rec_strdup (const char *s, size_t *len)