2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.12 1995-12-07 17:38:47 adam
8 * Work locking mechanisms for concurrent updates/commit.
10 * Revision 1.11 1995/12/06 13:58:26 adam
11 * Improved flushing of records - all flushes except the last one
12 * don't write the last accessed. Also flush takes place if record
13 * info occupy more than about 256k.
15 * Revision 1.10 1995/12/06 12:41:24 adam
16 * New command 'stat' for the index program.
17 * Filenames can be read from stdin by specifying '-'.
18 * Bug fix/enhancement of the transformation from terms to regular
19 * expressons in the search engine.
21 * Revision 1.9 1995/11/30 08:34:33 adam
22 * Started work on commit facility.
23 * Changed a few malloc/free to xmalloc/xfree.
25 * Revision 1.8 1995/11/28 14:26:21 adam
26 * Bug fix: recordId with constant wasn't right.
27 * Bug fix: recordId dictionary entry wasn't deleted when needed.
29 * Revision 1.7 1995/11/28 09:09:43 adam
30 * Zebra config renamed.
31 * Use setting 'recordId' to identify record now.
32 * Bug fix in recindex.c: rec_release_blocks was invokeded even
33 * though the blocks were already released.
34 * File traversal properly deletes records when needed.
36 * Revision 1.6 1995/11/25 10:24:06 adam
37 * More record fields - they are enumerated now.
38 * New options: flagStoreData flagStoreKey.
40 * Revision 1.5 1995/11/22 17:19:18 adam
41 * Record management uses the bfile system.
43 * Revision 1.4 1995/11/20 16:59:46 adam
44 * New update method: the 'old' keys are saved for each records.
46 * Revision 1.3 1995/11/16 15:34:55 adam
47 * Uses new record management system in both indexer and server.
49 * Revision 1.2 1995/11/15 19:13:08 adam
50 * Work on record management.
52 * Revision 1.1 1995/11/15 14:46:20 adam
53 * Started work on better record management system.
63 static void rec_write_head (Records p)
68 assert (p->index_BFile);
70 r = bf_write (p->index_BFile, 0, 0, sizeof(p->head), &p->head);
73 logf (LOG_FATAL|LOG_ERRNO, "write head of %s", p->index_fname);
78 static void rec_tmp_expand (Records p, int size, int dst_type)
80 if (p->tmp_size < size + 256 ||
81 p->tmp_size < p->head.block_size[dst_type]*2)
84 p->tmp_size = size + p->head.block_size[dst_type]*2 + 256;
85 p->tmp_buf = xmalloc (p->tmp_size);
89 static int read_indx (Records p, int sysno, void *buf, int itemsize,
93 int pos = (sysno-1)*itemsize;
95 r = bf_read (p->index_BFile, 1+pos/128, pos%128, itemsize, buf);
96 if (r != 1 && !ignoreError)
98 logf (LOG_FATAL|LOG_ERRNO, "read in %s at pos %ld",
99 p->index_fname, (long) pos);
106 static void write_indx (Records p, int sysno, void *buf, int itemsize)
108 int pos = (sysno-1)*itemsize;
110 bf_write (p->index_BFile, 1+pos/128, pos%128, itemsize, buf);
113 static void rec_release_blocks (Records p, int sysno)
115 struct record_index_entry entry;
116 int freeblock, freenext;
119 if (read_indx (p, sysno, &entry, sizeof(entry), 1) != 1)
121 p->head.total_bytes -= entry.u.used.size;
122 freeblock = entry.u.used.next;
123 assert (freeblock > 0);
124 dst_type = freeblock & 7;
125 assert (dst_type < REC_BLOCK_TYPES);
126 freeblock = freeblock / 8;
129 if (bf_read (p->data_BFile[dst_type], freeblock, 0, sizeof(freenext),
132 logf (LOG_FATAL|LOG_ERRNO, "read in rec_del_single");
135 if (bf_write (p->data_BFile[dst_type], freeblock, 0, sizeof(freenext),
136 &p->head.block_free[dst_type]))
138 logf (LOG_FATAL|LOG_ERRNO, "write in rec_del_single");
141 p->head.block_free[dst_type] = freeblock;
142 freeblock = freenext;
143 p->head.block_used[dst_type]--;
147 static void rec_delete_single (Records p, Record rec)
149 struct record_index_entry entry;
151 rec_release_blocks (p, rec->sysno);
153 entry.u.free.next = p->head.index_free;
154 p->head.index_free = rec->sysno;
155 write_indx (p, rec->sysno, &entry, sizeof(entry));
159 static void rec_write_single (Records p, Record rec)
165 int block_prev = -1, block_free;
166 struct record_index_entry entry;
168 for (i = 0; i < REC_NO_INFO; i++)
170 size += sizeof(*rec->size);
172 size += sizeof(*rec->size) + rec->size[i];
174 for (i = 1; i<REC_BLOCK_TYPES; i++)
175 if (size >= p->head.block_move[i])
178 rec_tmp_expand (p, size, dst_type);
180 cptr = p->tmp_buf + sizeof(int); /* a hack! */
181 for (i = 0; i < REC_NO_INFO; i++)
183 memcpy (cptr, &rec->size[i], sizeof(*rec->size));
184 cptr += sizeof(*rec->size);
187 memcpy (cptr, rec->info[i], rec->size[i]);
188 cptr += rec->size[i];
192 while (no_written < size)
194 block_free = p->head.block_free[dst_type];
197 if (bf_read (p->data_BFile[dst_type],
198 block_free, 0, sizeof(*p->head.block_free),
199 &p->head.block_free[dst_type]) != 1)
201 logf (LOG_FATAL|LOG_ERRNO, "read in %s at free block %d",
202 p->data_fname[dst_type], block_free);
206 block_free = p->head.block_last[dst_type]++;
207 if (block_prev == -1)
209 entry.u.used.next = block_free*8 + dst_type;
210 entry.u.used.size = size;
211 p->head.total_bytes += size;
212 write_indx (p, rec->sysno, &entry, sizeof(entry));
216 memcpy (cptr, &block_free, sizeof(int));
217 bf_write (p->data_BFile[dst_type], block_prev, 0, 0, cptr);
218 cptr = p->tmp_buf + no_written;
220 block_prev = block_free;
221 no_written += p->head.block_size[dst_type] - sizeof(int);
222 p->head.block_used[dst_type]++;
224 assert (block_prev != -1);
226 memcpy (cptr, &block_free, sizeof(int));
227 bf_write (p->data_BFile[dst_type], block_prev, 0,
228 sizeof(int) + (p->tmp_buf+size) - cptr, cptr);
231 static void rec_update_single (Records p, Record rec)
233 rec_release_blocks (p, rec->sysno);
234 rec_write_single (p, rec);
237 Records rec_open (int rw)
242 p = xmalloc (sizeof(*p));
245 p->tmp_buf = xmalloc (p->tmp_size);
246 p->index_fname = "recindex";
247 p->index_BFile = bf_open (p->index_fname, 128, rw);
248 if (p->index_BFile == NULL)
250 logf (LOG_FATAL|LOG_ERRNO, "open %s", p->index_fname);
253 r = bf_read (p->index_BFile, 0, 0, 0, p->tmp_buf);
257 memcpy (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic));
258 p->head.index_free = 0;
259 p->head.index_last = 1;
260 p->head.no_records = 0;
261 p->head.total_bytes = 0;
262 for (i = 0; i<REC_BLOCK_TYPES; i++)
264 p->head.block_free[i] = 0;
265 p->head.block_last[i] = 1;
266 p->head.block_used[i] = 0;
268 p->head.block_size[0] = 128;
269 p->head.block_move[0] = 0;
270 for (i = 1; i<REC_BLOCK_TYPES; i++)
272 p->head.block_size[i] = p->head.block_size[i-1] * 4;
273 p->head.block_move[i] = p->head.block_size[i] * 3;
279 memcpy (&p->head, p->tmp_buf, sizeof(p->head));
280 if (memcmp (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic)))
282 logf (LOG_FATAL, "read %s. bad header", p->index_fname);
287 for (i = 0; i<REC_BLOCK_TYPES; i++)
290 sprintf (str, "recdata%c", i + 'A');
291 p->data_fname[i] = xmalloc (strlen(str)+1);
292 strcpy (p->data_fname[i], str);
293 p->data_BFile[i] = NULL;
295 for (i = 0; i<REC_BLOCK_TYPES; i++)
297 if (!(p->data_BFile[i] = bf_open (p->data_fname[i],
298 p->head.block_size[i],
301 logf (LOG_FATAL|LOG_ERRNO, "bf_open %s", p->data_fname[i]);
307 p->record_cache = xmalloc (sizeof(*p->record_cache)*p->cache_max);
311 static void rec_cache_flush (Records p, int saveCount)
315 if (saveCount >= p->cache_cur)
317 for (i = 0; i<p->cache_cur - saveCount; i++)
319 struct record_cache_entry *e = p->record_cache + i;
325 rec_write_single (p, e->rec);
327 case recordFlagWrite:
328 rec_update_single (p, e->rec);
330 case recordFlagDelete:
331 rec_delete_single (p, e->rec);
336 for (j = 0; j<saveCount; j++, i++)
337 memcpy (p->record_cache+j, p->record_cache+i,
338 sizeof(*p->record_cache));
339 p->cache_cur = saveCount;
342 static Record *rec_cache_lookup (Records p, int sysno,
343 enum recordCacheFlag flag)
346 for (i = 0; i<p->cache_cur; i++)
348 struct record_cache_entry *e = p->record_cache + i;
349 if (e->rec->sysno == sysno)
351 if (flag != recordFlagNop && e->flag == recordFlagNop)
359 static void rec_cache_insert (Records p, Record rec, enum recordCacheFlag flag)
361 struct record_cache_entry *e;
363 if (p->cache_cur == p->cache_max)
364 rec_cache_flush (p, 1);
365 else if (p->cache_cur > 2)
369 for (i = 0; i<p->cache_cur; i++)
371 Record r = (p->record_cache + i)->rec;
372 for (j = 0; j<REC_NO_INFO; j++)
376 rec_cache_flush (p, 1);
378 assert (p->cache_cur < p->cache_max);
380 e = p->record_cache + (p->cache_cur)++;
382 e->rec = rec_cp (rec);
385 void rec_close (Records *pp)
392 rec_cache_flush (p, 0);
393 xfree (p->record_cache);
399 bf_close (p->index_BFile);
401 for (i = 0; i<REC_BLOCK_TYPES; i++)
403 if (p->data_BFile[i])
404 bf_close (p->data_BFile[i]);
405 xfree (p->data_fname[i]);
413 Record rec_get (Records p, int sysno)
417 struct record_index_entry entry;
418 int freeblock, dst_type;
424 if ((recp = rec_cache_lookup (p, sysno, recordFlagNop)))
425 return rec_cp (*recp);
427 read_indx (p, sysno, &entry, sizeof(entry), 0);
429 dst_type = entry.u.used.next & 7;
430 assert (dst_type < REC_BLOCK_TYPES);
431 freeblock = entry.u.used.next / 8;
433 assert (freeblock > 0);
435 rec = xmalloc (sizeof(*rec));
436 rec_tmp_expand (p, entry.u.used.size, dst_type);
439 bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
440 memcpy (&freeblock, cptr, sizeof(freeblock));
446 cptr += p->head.block_size[dst_type] - sizeof(freeblock);
448 memcpy (&tmp, cptr, sizeof(tmp));
449 bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
450 memcpy (&freeblock, cptr, sizeof(freeblock));
451 memcpy (cptr, &tmp, sizeof(tmp));
455 nptr = p->tmp_buf + sizeof(freeblock);
456 for (i = 0; i < REC_NO_INFO; i++)
458 memcpy (&rec->size[i], nptr, sizeof(*rec->size));
459 nptr += sizeof(*rec->size);
462 rec->info[i] = xmalloc (rec->size[i]);
463 memcpy (rec->info[i], nptr, rec->size[i]);
464 nptr += rec->size[i];
469 rec_cache_insert (p, rec, recordFlagNop);
473 Record rec_new (Records p)
479 rec = xmalloc (sizeof(*rec));
480 if (p->head.index_free == 0)
481 sysno = (p->head.index_last)++;
484 struct record_index_entry entry;
486 read_indx (p, p->head.index_free, &entry, sizeof(entry), 0);
487 sysno = p->head.index_free;
488 p->head.index_free = entry.u.free.next;
490 (p->head.no_records)++;
492 for (i = 0; i < REC_NO_INFO; i++)
497 rec_cache_insert (p, rec, recordFlagNew);
501 void rec_del (Records p, Record *recpp)
505 (p->head.no_records)--;
506 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagDelete)))
513 rec_cache_insert (p, *recpp, recordFlagDelete);
519 void rec_put (Records p, Record *recpp)
523 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagWrite)))
530 rec_cache_insert (p, *recpp, recordFlagWrite);
536 void rec_rm (Record *recpp)
539 for (i = 0; i < REC_NO_INFO; i++)
540 xfree ((*recpp)->info[i]);
545 Record rec_cp (Record rec)
550 n = xmalloc (sizeof(*n));
551 n->sysno = rec->sysno;
552 for (i = 0; i < REC_NO_INFO; i++)
560 n->size[i] = rec->size[i];
561 n->info[i] = xmalloc (rec->size[i]);
562 memcpy (n->info[i], rec->info[i], rec->size[i]);
568 char *rec_strdup (const char *s, size_t *len)