2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.11 1995-12-06 13:58:26 adam
8 * Improved flushing of records - all flushes except the last one
9 * don't write the last accessed. Also flush takes place if record
10 * info occupy more than about 256k.
12 * Revision 1.10 1995/12/06 12:41:24 adam
13 * New command 'stat' for the index program.
14 * Filenames can be read from stdin by specifying '-'.
15 * Bug fix/enhancement of the transformation from terms to regular
16 * expressons in the search engine.
18 * Revision 1.9 1995/11/30 08:34:33 adam
19 * Started work on commit facility.
20 * Changed a few malloc/free to xmalloc/xfree.
22 * Revision 1.8 1995/11/28 14:26:21 adam
23 * Bug fix: recordId with constant wasn't right.
24 * Bug fix: recordId dictionary entry wasn't deleted when needed.
26 * Revision 1.7 1995/11/28 09:09:43 adam
27 * Zebra config renamed.
28 * Use setting 'recordId' to identify record now.
29 * Bug fix in recindex.c: rec_release_blocks was invokeded even
30 * though the blocks were already released.
31 * File traversal properly deletes records when needed.
33 * Revision 1.6 1995/11/25 10:24:06 adam
34 * More record fields - they are enumerated now.
35 * New options: flagStoreData flagStoreKey.
37 * Revision 1.5 1995/11/22 17:19:18 adam
38 * Record management uses the bfile system.
40 * Revision 1.4 1995/11/20 16:59:46 adam
41 * New update method: the 'old' keys are saved for each records.
43 * Revision 1.3 1995/11/16 15:34:55 adam
44 * Uses new record management system in both indexer and server.
46 * Revision 1.2 1995/11/15 19:13:08 adam
47 * Work on record management.
49 * Revision 1.1 1995/11/15 14:46:20 adam
50 * Started work on better record management system.
60 static void rec_write_head (Records p)
65 assert (p->index_BFile);
67 r = bf_write (p->index_BFile, 0, 0, sizeof(p->head), &p->head);
70 logf (LOG_FATAL|LOG_ERRNO, "write head of %s", p->index_fname);
75 static void rec_tmp_expand (Records p, int size, int dst_type)
77 if (p->tmp_size < size + 256 ||
78 p->tmp_size < p->head.block_size[dst_type]*2)
81 p->tmp_size = size + p->head.block_size[dst_type]*2 + 256;
82 p->tmp_buf = xmalloc (p->tmp_size);
86 static int read_indx (Records p, int sysno, void *buf, int itemsize,
90 int pos = (sysno-1)*itemsize;
92 r = bf_read (p->index_BFile, 1+pos/128, pos%128, itemsize, buf);
93 if (r != 1 && !ignoreError)
95 logf (LOG_FATAL|LOG_ERRNO, "read in %s at pos %ld",
96 p->index_fname, (long) pos);
103 static void write_indx (Records p, int sysno, void *buf, int itemsize)
105 int pos = (sysno-1)*itemsize;
107 bf_write (p->index_BFile, 1+pos/128, pos%128, itemsize, buf);
110 static void rec_release_blocks (Records p, int sysno)
112 struct record_index_entry entry;
113 int freeblock, freenext;
116 if (read_indx (p, sysno, &entry, sizeof(entry), 1) != 1)
118 p->head.total_bytes -= entry.u.used.size;
119 freeblock = entry.u.used.next;
120 assert (freeblock > 0);
121 dst_type = freeblock & 7;
122 assert (dst_type < REC_BLOCK_TYPES);
123 freeblock = freeblock / 8;
126 if (bf_read (p->data_BFile[dst_type], freeblock, 0, sizeof(freenext),
129 logf (LOG_FATAL|LOG_ERRNO, "read in rec_del_single");
132 if (bf_write (p->data_BFile[dst_type], freeblock, 0, sizeof(freenext),
133 &p->head.block_free[dst_type]))
135 logf (LOG_FATAL|LOG_ERRNO, "write in rec_del_single");
138 p->head.block_free[dst_type] = freeblock;
139 freeblock = freenext;
140 p->head.block_used[dst_type]--;
144 static void rec_delete_single (Records p, Record rec)
146 struct record_index_entry entry;
148 rec_release_blocks (p, rec->sysno);
150 entry.u.free.next = p->head.index_free;
151 p->head.index_free = rec->sysno;
152 write_indx (p, rec->sysno, &entry, sizeof(entry));
156 static void rec_write_single (Records p, Record rec)
162 int block_prev = -1, block_free;
163 struct record_index_entry entry;
165 for (i = 0; i < REC_NO_INFO; i++)
167 size += sizeof(*rec->size);
169 size += sizeof(*rec->size) + rec->size[i];
171 for (i = 1; i<REC_BLOCK_TYPES; i++)
172 if (size >= p->head.block_move[i])
175 rec_tmp_expand (p, size, dst_type);
177 cptr = p->tmp_buf + sizeof(int); /* a hack! */
178 for (i = 0; i < REC_NO_INFO; i++)
180 memcpy (cptr, &rec->size[i], sizeof(*rec->size));
181 cptr += sizeof(*rec->size);
184 memcpy (cptr, rec->info[i], rec->size[i]);
185 cptr += rec->size[i];
189 while (no_written < size)
191 block_free = p->head.block_free[dst_type];
194 if (bf_read (p->data_BFile[dst_type],
195 block_free, 0, sizeof(*p->head.block_free),
196 &p->head.block_free[dst_type]) != 1)
198 logf (LOG_FATAL|LOG_ERRNO, "read in %s at free block %d",
199 p->data_fname[dst_type], block_free);
203 block_free = p->head.block_last[dst_type]++;
204 if (block_prev == -1)
206 entry.u.used.next = block_free*8 + dst_type;
207 entry.u.used.size = size;
208 p->head.total_bytes += size;
209 write_indx (p, rec->sysno, &entry, sizeof(entry));
213 memcpy (cptr, &block_free, sizeof(int));
214 bf_write (p->data_BFile[dst_type], block_prev, 0, 0, cptr);
215 cptr = p->tmp_buf + no_written;
217 block_prev = block_free;
218 no_written += p->head.block_size[dst_type] - sizeof(int);
219 p->head.block_used[dst_type]++;
221 assert (block_prev != -1);
223 memcpy (cptr, &block_free, sizeof(int));
224 bf_write (p->data_BFile[dst_type], block_prev, 0,
225 sizeof(int) + (p->tmp_buf+size) - cptr, cptr);
228 static void rec_update_single (Records p, Record rec)
230 rec_release_blocks (p, rec->sysno);
231 rec_write_single (p, rec);
234 Records rec_open (int rw)
239 p = xmalloc (sizeof(*p));
242 p->tmp_buf = xmalloc (p->tmp_size);
243 p->index_fname = "recindex";
244 p->index_BFile = bf_open (p->index_fname, 128, rw);
245 if (p->index_BFile == NULL)
247 logf (LOG_FATAL|LOG_ERRNO, "open %s", p->index_fname);
250 r = bf_read (p->index_BFile, 0, 0, 0, p->tmp_buf);
254 memcpy (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic));
255 p->head.index_free = 0;
256 p->head.index_last = 1;
257 p->head.no_records = 0;
258 p->head.total_bytes = 0;
259 for (i = 0; i<REC_BLOCK_TYPES; i++)
261 p->head.block_free[i] = 0;
262 p->head.block_last[i] = 1;
263 p->head.block_used[i] = 0;
265 p->head.block_size[0] = 128;
266 p->head.block_move[0] = 0;
267 for (i = 1; i<REC_BLOCK_TYPES; i++)
269 p->head.block_size[i] = p->head.block_size[i-1] * 4;
270 p->head.block_move[i] = p->head.block_size[i] * 3;
276 memcpy (&p->head, p->tmp_buf, sizeof(p->head));
277 if (memcmp (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic)))
279 logf (LOG_FATAL, "read %s. bad header", p->index_fname);
284 for (i = 0; i<REC_BLOCK_TYPES; i++)
287 sprintf (str, "recdata%c", i + 'A');
288 p->data_fname[i] = xmalloc (strlen(str)+1);
289 strcpy (p->data_fname[i], str);
290 p->data_BFile[i] = NULL;
292 for (i = 0; i<REC_BLOCK_TYPES; i++)
294 if (!(p->data_BFile[i] = bf_open (p->data_fname[i],
295 p->head.block_size[i],
298 logf (LOG_FATAL|LOG_ERRNO, "bf_open %s", p->data_fname[i]);
304 p->record_cache = xmalloc (sizeof(*p->record_cache)*p->cache_max);
308 static void rec_cache_flush (Records p, int saveCount)
312 if (saveCount >= p->cache_cur)
314 for (i = 0; i<p->cache_cur - saveCount; i++)
316 struct record_cache_entry *e = p->record_cache + i;
322 rec_write_single (p, e->rec);
324 case recordFlagWrite:
325 rec_update_single (p, e->rec);
327 case recordFlagDelete:
328 rec_delete_single (p, e->rec);
333 for (j = 0; j<saveCount; j++, i++)
334 memcpy (p->record_cache+j, p->record_cache+i,
335 sizeof(*p->record_cache));
336 p->cache_cur = saveCount;
339 static Record *rec_cache_lookup (Records p, int sysno,
340 enum recordCacheFlag flag)
343 for (i = 0; i<p->cache_cur; i++)
345 struct record_cache_entry *e = p->record_cache + i;
346 if (e->rec->sysno == sysno)
348 if (flag != recordFlagNop && e->flag == recordFlagNop)
356 static void rec_cache_insert (Records p, Record rec, enum recordCacheFlag flag)
358 struct record_cache_entry *e;
360 if (p->cache_cur == p->cache_max)
361 rec_cache_flush (p, 1);
362 else if (p->cache_cur > 2)
366 for (i = 0; i<p->cache_cur; i++)
368 Record r = (p->record_cache + i)->rec;
369 for (j = 0; j<REC_NO_INFO; j++)
373 rec_cache_flush (p, 1);
375 assert (p->cache_cur < p->cache_max);
377 e = p->record_cache + (p->cache_cur)++;
379 e->rec = rec_cp (rec);
382 void rec_close (Records *pp)
389 rec_cache_flush (p, 0);
390 xfree (p->record_cache);
396 bf_close (p->index_BFile);
398 for (i = 0; i<REC_BLOCK_TYPES; i++)
400 if (p->data_BFile[i])
401 bf_close (p->data_BFile[i]);
402 xfree (p->data_fname[i]);
410 Record rec_get (Records p, int sysno)
414 struct record_index_entry entry;
415 int freeblock, dst_type;
421 if ((recp = rec_cache_lookup (p, sysno, recordFlagNop)))
422 return rec_cp (*recp);
424 read_indx (p, sysno, &entry, sizeof(entry), 0);
426 dst_type = entry.u.used.next & 7;
427 assert (dst_type < REC_BLOCK_TYPES);
428 freeblock = entry.u.used.next / 8;
430 assert (freeblock > 0);
432 rec = xmalloc (sizeof(*rec));
433 rec_tmp_expand (p, entry.u.used.size, dst_type);
436 bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
437 memcpy (&freeblock, cptr, sizeof(freeblock));
443 cptr += p->head.block_size[dst_type] - sizeof(freeblock);
445 memcpy (&tmp, cptr, sizeof(tmp));
446 bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
447 memcpy (&freeblock, cptr, sizeof(freeblock));
448 memcpy (cptr, &tmp, sizeof(tmp));
452 nptr = p->tmp_buf + sizeof(freeblock);
453 for (i = 0; i < REC_NO_INFO; i++)
455 memcpy (&rec->size[i], nptr, sizeof(*rec->size));
456 nptr += sizeof(*rec->size);
459 rec->info[i] = xmalloc (rec->size[i]);
460 memcpy (rec->info[i], nptr, rec->size[i]);
461 nptr += rec->size[i];
466 rec_cache_insert (p, rec, recordFlagNop);
470 Record rec_new (Records p)
476 rec = xmalloc (sizeof(*rec));
477 if (p->head.index_free == 0)
478 sysno = (p->head.index_last)++;
481 struct record_index_entry entry;
483 read_indx (p, p->head.index_free, &entry, sizeof(entry), 0);
484 sysno = p->head.index_free;
485 p->head.index_free = entry.u.free.next;
487 (p->head.no_records)++;
489 for (i = 0; i < REC_NO_INFO; i++)
494 rec_cache_insert (p, rec, recordFlagNew);
498 void rec_del (Records p, Record *recpp)
502 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagDelete)))
509 rec_cache_insert (p, *recpp, recordFlagDelete);
515 void rec_put (Records p, Record *recpp)
519 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagWrite)))
526 rec_cache_insert (p, *recpp, recordFlagWrite);
532 void rec_rm (Record *recpp)
535 for (i = 0; i < REC_NO_INFO; i++)
536 xfree ((*recpp)->info[i]);
541 Record rec_cp (Record rec)
546 n = xmalloc (sizeof(*n));
547 n->sysno = rec->sysno;
548 for (i = 0; i < REC_NO_INFO; i++)
556 n->size[i] = rec->size[i];
557 n->info[i] = xmalloc (rec->size[i]);
558 memcpy (n->info[i], rec->info[i], rec->size[i]);
564 char *rec_strdup (const char *s, size_t *len)