2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.10 1995-12-06 12:41:24 adam
8 * New command 'stat' for the index program.
9 * Filenames can be read from stdin by specifying '-'.
10 * Bug fix/enhancement of the transformation from terms to regular
11 * expressons in the search engine.
13 * Revision 1.9 1995/11/30 08:34:33 adam
14 * Started work on commit facility.
15 * Changed a few malloc/free to xmalloc/xfree.
17 * Revision 1.8 1995/11/28 14:26:21 adam
18 * Bug fix: recordId with constant wasn't right.
19 * Bug fix: recordId dictionary entry wasn't deleted when needed.
21 * Revision 1.7 1995/11/28 09:09:43 adam
22 * Zebra config renamed.
23 * Use setting 'recordId' to identify record now.
24 * Bug fix in recindex.c: rec_release_blocks was invokeded even
25 * though the blocks were already released.
26 * File traversal properly deletes records when needed.
28 * Revision 1.6 1995/11/25 10:24:06 adam
29 * More record fields - they are enumerated now.
30 * New options: flagStoreData flagStoreKey.
32 * Revision 1.5 1995/11/22 17:19:18 adam
33 * Record management uses the bfile system.
35 * Revision 1.4 1995/11/20 16:59:46 adam
36 * New update method: the 'old' keys are saved for each records.
38 * Revision 1.3 1995/11/16 15:34:55 adam
39 * Uses new record management system in both indexer and server.
41 * Revision 1.2 1995/11/15 19:13:08 adam
42 * Work on record management.
44 * Revision 1.1 1995/11/15 14:46:20 adam
45 * Started work on better record management system.
55 static void rec_write_head (Records p)
60 assert (p->index_BFile);
62 r = bf_write (p->index_BFile, 0, 0, sizeof(p->head), &p->head);
65 logf (LOG_FATAL|LOG_ERRNO, "write head of %s", p->index_fname);
70 static void rec_tmp_expand (Records p, int size, int dst_type)
72 if (p->tmp_size < size + 256 ||
73 p->tmp_size < p->head.block_size[dst_type]*2)
76 p->tmp_size = size + p->head.block_size[dst_type]*2 + 256;
77 p->tmp_buf = xmalloc (p->tmp_size);
81 static int read_indx (Records p, int sysno, void *buf, int itemsize,
85 int pos = (sysno-1)*itemsize;
87 r = bf_read (p->index_BFile, 1+pos/128, pos%128, itemsize, buf);
88 if (r != 1 && !ignoreError)
90 logf (LOG_FATAL|LOG_ERRNO, "read in %s at pos %ld",
91 p->index_fname, (long) pos);
98 static void write_indx (Records p, int sysno, void *buf, int itemsize)
100 int pos = (sysno-1)*itemsize;
102 bf_write (p->index_BFile, 1+pos/128, pos%128, itemsize, buf);
105 static void rec_release_blocks (Records p, int sysno)
107 struct record_index_entry entry;
108 int freeblock, freenext;
111 if (read_indx (p, sysno, &entry, sizeof(entry), 1) != 1)
113 p->head.total_bytes -= entry.u.used.size;
114 freeblock = entry.u.used.next;
115 assert (freeblock > 0);
116 dst_type = freeblock & 7;
117 assert (dst_type < REC_BLOCK_TYPES);
118 freeblock = freeblock / 8;
121 if (bf_read (p->data_BFile[dst_type], freeblock, 0, sizeof(freenext),
124 logf (LOG_FATAL|LOG_ERRNO, "read in rec_del_single");
127 if (bf_write (p->data_BFile[dst_type], freeblock, 0, sizeof(freenext),
128 &p->head.block_free[dst_type]))
130 logf (LOG_FATAL|LOG_ERRNO, "write in rec_del_single");
133 p->head.block_free[dst_type] = freeblock;
134 freeblock = freenext;
135 p->head.block_used[dst_type]--;
139 static void rec_delete_single (Records p, Record rec)
141 struct record_index_entry entry;
143 rec_release_blocks (p, rec->sysno);
145 entry.u.free.next = p->head.index_free;
146 p->head.index_free = rec->sysno;
147 write_indx (p, rec->sysno, &entry, sizeof(entry));
151 static void rec_write_single (Records p, Record rec)
157 int block_prev = -1, block_free;
158 struct record_index_entry entry;
160 for (i = 0; i < REC_NO_INFO; i++)
162 size += sizeof(*rec->size);
164 size += sizeof(*rec->size) + rec->size[i];
166 for (i = 1; i<REC_BLOCK_TYPES; i++)
167 if (size >= p->head.block_move[i])
170 rec_tmp_expand (p, size, dst_type);
172 cptr = p->tmp_buf + sizeof(int); /* a hack! */
173 for (i = 0; i < REC_NO_INFO; i++)
175 memcpy (cptr, &rec->size[i], sizeof(*rec->size));
176 cptr += sizeof(*rec->size);
179 memcpy (cptr, rec->info[i], rec->size[i]);
180 cptr += rec->size[i];
184 while (no_written < size)
186 block_free = p->head.block_free[dst_type];
189 if (bf_read (p->data_BFile[dst_type],
190 block_free, 0, sizeof(*p->head.block_free),
191 &p->head.block_free[dst_type]) != 1)
193 logf (LOG_FATAL|LOG_ERRNO, "read in %s at free block %d",
194 p->data_fname[dst_type], block_free);
198 block_free = p->head.block_last[dst_type]++;
199 if (block_prev == -1)
201 entry.u.used.next = block_free*8 + dst_type;
202 entry.u.used.size = size;
203 p->head.total_bytes += size;
204 write_indx (p, rec->sysno, &entry, sizeof(entry));
208 memcpy (cptr, &block_free, sizeof(int));
209 bf_write (p->data_BFile[dst_type], block_prev, 0, 0, cptr);
210 cptr = p->tmp_buf + no_written;
212 block_prev = block_free;
213 no_written += p->head.block_size[dst_type] - sizeof(int);
214 p->head.block_used[dst_type]++;
216 assert (block_prev != -1);
218 memcpy (cptr, &block_free, sizeof(int));
219 bf_write (p->data_BFile[dst_type], block_prev, 0,
220 sizeof(int) + (p->tmp_buf+size) - cptr, cptr);
223 static void rec_update_single (Records p, Record rec)
225 rec_release_blocks (p, rec->sysno);
226 rec_write_single (p, rec);
229 Records rec_open (int rw)
234 p = xmalloc (sizeof(*p));
237 p->tmp_buf = xmalloc (p->tmp_size);
238 p->index_fname = "recindex";
239 p->index_BFile = bf_open (p->index_fname, 128, rw);
240 if (p->index_BFile == NULL)
242 logf (LOG_FATAL|LOG_ERRNO, "open %s", p->index_fname);
245 r = bf_read (p->index_BFile, 0, 0, 0, p->tmp_buf);
249 memcpy (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic));
250 p->head.index_free = 0;
251 p->head.index_last = 1;
252 p->head.no_records = 0;
253 p->head.total_bytes = 0;
254 for (i = 0; i<REC_BLOCK_TYPES; i++)
256 p->head.block_free[i] = 0;
257 p->head.block_last[i] = 1;
258 p->head.block_used[i] = 0;
260 p->head.block_size[0] = 128;
261 p->head.block_move[0] = 0;
262 for (i = 1; i<REC_BLOCK_TYPES; i++)
264 p->head.block_size[i] = p->head.block_size[i-1] * 4;
265 p->head.block_move[i] = p->head.block_size[i] * 3;
271 memcpy (&p->head, p->tmp_buf, sizeof(p->head));
272 if (memcmp (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic)))
274 logf (LOG_FATAL, "read %s. bad header", p->index_fname);
279 for (i = 0; i<REC_BLOCK_TYPES; i++)
282 sprintf (str, "recdata%c", i + 'A');
283 p->data_fname[i] = xmalloc (strlen(str)+1);
284 strcpy (p->data_fname[i], str);
285 p->data_BFile[i] = NULL;
287 for (i = 0; i<REC_BLOCK_TYPES; i++)
289 if (!(p->data_BFile[i] = bf_open (p->data_fname[i],
290 p->head.block_size[i],
293 logf (LOG_FATAL|LOG_ERRNO, "bf_open %s", p->data_fname[i]);
299 p->record_cache = xmalloc (sizeof(*p->record_cache)*p->cache_max);
303 static void rec_cache_flush (Records p)
306 for (i = 0; i<p->cache_cur; i++)
308 struct record_cache_entry *e = p->record_cache + i;
314 rec_write_single (p, e->rec);
316 case recordFlagWrite:
317 rec_update_single (p, e->rec);
319 case recordFlagDelete:
320 rec_delete_single (p, e->rec);
328 static Record *rec_cache_lookup (Records p, int sysno,
329 enum recordCacheFlag flag)
332 for (i = 0; i<p->cache_cur; i++)
334 struct record_cache_entry *e = p->record_cache + i;
335 if (e->rec->sysno == sysno)
337 if (flag != recordFlagNop && e->flag == recordFlagNop)
345 static void rec_cache_insert (Records p, Record rec, enum recordCacheFlag flag)
347 struct record_cache_entry *e;
349 if (p->cache_cur == p->cache_max)
351 assert (p->cache_cur < p->cache_max);
353 e = p->record_cache + (p->cache_cur)++;
355 e->rec = rec_cp (rec);
358 void rec_close (Records *pp)
366 xfree (p->record_cache);
372 bf_close (p->index_BFile);
374 for (i = 0; i<REC_BLOCK_TYPES; i++)
376 if (p->data_BFile[i])
377 bf_close (p->data_BFile[i]);
378 xfree (p->data_fname[i]);
386 Record rec_get (Records p, int sysno)
390 struct record_index_entry entry;
391 int freeblock, dst_type;
397 if ((recp = rec_cache_lookup (p, sysno, recordFlagNop)))
398 return rec_cp (*recp);
400 read_indx (p, sysno, &entry, sizeof(entry), 0);
402 dst_type = entry.u.used.next & 7;
403 assert (dst_type < REC_BLOCK_TYPES);
404 freeblock = entry.u.used.next / 8;
406 assert (freeblock > 0);
408 rec = xmalloc (sizeof(*rec));
409 rec_tmp_expand (p, entry.u.used.size, dst_type);
412 bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
413 memcpy (&freeblock, cptr, sizeof(freeblock));
419 cptr += p->head.block_size[dst_type] - sizeof(freeblock);
421 memcpy (&tmp, cptr, sizeof(tmp));
422 bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
423 memcpy (&freeblock, cptr, sizeof(freeblock));
424 memcpy (cptr, &tmp, sizeof(tmp));
428 nptr = p->tmp_buf + sizeof(freeblock);
429 for (i = 0; i < REC_NO_INFO; i++)
431 memcpy (&rec->size[i], nptr, sizeof(*rec->size));
432 nptr += sizeof(*rec->size);
435 rec->info[i] = xmalloc (rec->size[i]);
436 memcpy (rec->info[i], nptr, rec->size[i]);
437 nptr += rec->size[i];
442 rec_cache_insert (p, rec, recordFlagNop);
446 Record rec_new (Records p)
452 rec = xmalloc (sizeof(*rec));
453 if (p->head.index_free == 0)
454 sysno = (p->head.index_last)++;
457 struct record_index_entry entry;
459 read_indx (p, p->head.index_free, &entry, sizeof(entry), 0);
460 sysno = p->head.index_free;
461 p->head.index_free = entry.u.free.next;
463 (p->head.no_records)++;
465 for (i = 0; i < REC_NO_INFO; i++)
470 rec_cache_insert (p, rec, recordFlagNew);
474 void rec_del (Records p, Record *recpp)
478 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagDelete)))
485 rec_cache_insert (p, *recpp, recordFlagDelete);
491 void rec_put (Records p, Record *recpp)
495 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagWrite)))
502 rec_cache_insert (p, *recpp, recordFlagWrite);
508 void rec_rm (Record *recpp)
511 for (i = 0; i < REC_NO_INFO; i++)
512 xfree ((*recpp)->info[i]);
517 Record rec_cp (Record rec)
522 n = xmalloc (sizeof(*n));
523 n->sysno = rec->sysno;
524 for (i = 0; i < REC_NO_INFO; i++)
532 n->size[i] = rec->size[i];
533 n->info[i] = xmalloc (rec->size[i]);
534 memcpy (n->info[i], rec->info[i], rec->size[i]);
540 char *rec_strdup (const char *s, size_t *len)