2 * Copyright (C) 1994-1998, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.20 1998-01-12 15:04:08 adam
8 * The test option (-s) only uses read-lock (and not write lock).
10 * Revision 1.19 1997/09/17 12:19:16 adam
11 * Zebra version corresponds to YAZ version 1.4.
12 * Changed Zebra server so that it doesn't depend on global common_resource.
14 * Revision 1.18 1997/07/15 16:28:42 adam
15 * Bug fix: storeData didn't work with files with multiple records.
16 * Bug fix: fixed memory management with records; not really well
19 * Revision 1.17 1997/02/12 20:39:46 adam
20 * Implemented options -f <n> that limits the log to the first <n>
22 * Changed some log messages also.
24 * Revision 1.16 1996/06/04 10:19:00 adam
25 * Minor changes - removed include of ctype.h.
27 * Revision 1.15 1996/05/13 14:23:06 adam
28 * Work on compaction of set/use bytes in dictionary.
30 * Revision 1.14 1996/02/01 20:48:15 adam
31 * The total size of records are always checked in rec_cache_insert to
32 * reduce memory usage.
34 * Revision 1.13 1995/12/11 09:12:49 adam
35 * The rec_get function returns NULL if record doesn't exist - will
36 * happen in the server if the result set records have been deleted since
37 * the creation of the set (i.e. the search).
38 * The server saves a result temporarily if it is 'volatile', i.e. the
39 * set is register dependent.
41 * Revision 1.12 1995/12/07 17:38:47 adam
42 * Work locking mechanisms for concurrent updates/commit.
44 * Revision 1.11 1995/12/06 13:58:26 adam
45 * Improved flushing of records - all flushes except the last one
46 * don't write the last accessed. Also flush takes place if record
47 * info occupy more than about 256k.
49 * Revision 1.10 1995/12/06 12:41:24 adam
50 * New command 'stat' for the index program.
51 * Filenames can be read from stdin by specifying '-'.
52 * Bug fix/enhancement of the transformation from terms to regular
53 * expressons in the search engine.
55 * Revision 1.9 1995/11/30 08:34:33 adam
56 * Started work on commit facility.
57 * Changed a few malloc/free to xmalloc/xfree.
59 * Revision 1.8 1995/11/28 14:26:21 adam
60 * Bug fix: recordId with constant wasn't right.
61 * Bug fix: recordId dictionary entry wasn't deleted when needed.
63 * Revision 1.7 1995/11/28 09:09:43 adam
64 * Zebra config renamed.
65 * Use setting 'recordId' to identify record now.
66 * Bug fix in recindex.c: rec_release_blocks was invokeded even
67 * though the blocks were already released.
68 * File traversal properly deletes records when needed.
70 * Revision 1.6 1995/11/25 10:24:06 adam
71 * More record fields - they are enumerated now.
72 * New options: flagStoreData flagStoreKey.
74 * Revision 1.5 1995/11/22 17:19:18 adam
75 * Record management uses the bfile system.
77 * Revision 1.4 1995/11/20 16:59:46 adam
78 * New update method: the 'old' keys are saved for each records.
80 * Revision 1.3 1995/11/16 15:34:55 adam
81 * Uses new record management system in both indexer and server.
83 * Revision 1.2 1995/11/15 19:13:08 adam
84 * Work on record management.
86 * Revision 1.1 1995/11/15 14:46:20 adam
87 * Started work on better record management system.
96 static void rec_write_head (Records p)
101 assert (p->index_BFile);
103 r = bf_write (p->index_BFile, 0, 0, sizeof(p->head), &p->head);
106 logf (LOG_FATAL|LOG_ERRNO, "write head of %s", p->index_fname);
111 static void rec_tmp_expand (Records p, int size, int dst_type)
113 if (p->tmp_size < size + 2048 ||
114 p->tmp_size < p->head.block_size[dst_type]*2)
117 p->tmp_size = size + p->head.block_size[dst_type]*2 + 2048;
118 p->tmp_buf = xmalloc (p->tmp_size);
122 static int read_indx (Records p, int sysno, void *buf, int itemsize,
126 int pos = (sysno-1)*itemsize;
128 r = bf_read (p->index_BFile, 1+pos/128, pos%128, itemsize, buf);
129 if (r != 1 && !ignoreError)
131 logf (LOG_FATAL|LOG_ERRNO, "read in %s at pos %ld",
132 p->index_fname, (long) pos);
138 static void write_indx (Records p, int sysno, void *buf, int itemsize)
140 int pos = (sysno-1)*itemsize;
142 bf_write (p->index_BFile, 1+pos/128, pos%128, itemsize, buf);
145 static void rec_release_blocks (Records p, int sysno)
147 struct record_index_entry entry;
148 int freeblock, freenext;
151 if (read_indx (p, sysno, &entry, sizeof(entry), 1) != 1)
153 p->head.total_bytes -= entry.size;
154 freeblock = entry.next;
155 assert (freeblock > 0);
156 dst_type = freeblock & 7;
157 assert (dst_type < REC_BLOCK_TYPES);
158 freeblock = freeblock / 8;
161 if (bf_read (p->data_BFile[dst_type], freeblock, 0, sizeof(freenext),
164 logf (LOG_FATAL|LOG_ERRNO, "read in rec_del_single");
167 if (bf_write (p->data_BFile[dst_type], freeblock, 0, sizeof(freenext),
168 &p->head.block_free[dst_type]))
170 logf (LOG_FATAL|LOG_ERRNO, "write in rec_del_single");
173 p->head.block_free[dst_type] = freeblock;
174 freeblock = freenext;
175 p->head.block_used[dst_type]--;
179 static void rec_delete_single (Records p, Record rec)
181 struct record_index_entry entry;
183 rec_release_blocks (p, rec->sysno);
185 entry.next = p->head.index_free;
187 p->head.index_free = rec->sysno;
188 write_indx (p, rec->sysno, &entry, sizeof(entry));
192 static void rec_write_single (Records p, Record rec)
198 int block_prev = -1, block_free;
199 struct record_index_entry entry;
201 for (i = 0; i < REC_NO_INFO; i++)
203 size += sizeof(*rec->size);
205 size += sizeof(*rec->size) + rec->size[i];
207 for (i = 1; i<REC_BLOCK_TYPES; i++)
208 if (size >= p->head.block_move[i])
211 rec_tmp_expand (p, size, dst_type);
213 cptr = p->tmp_buf + sizeof(int); /* a hack! */
214 for (i = 0; i < REC_NO_INFO; i++)
216 memcpy (cptr, &rec->size[i], sizeof(*rec->size));
217 cptr += sizeof(*rec->size);
220 memcpy (cptr, rec->info[i], rec->size[i]);
221 cptr += rec->size[i];
225 while (no_written < size)
227 block_free = p->head.block_free[dst_type];
230 if (bf_read (p->data_BFile[dst_type],
231 block_free, 0, sizeof(*p->head.block_free),
232 &p->head.block_free[dst_type]) != 1)
234 logf (LOG_FATAL|LOG_ERRNO, "read in %s at free block %d",
235 p->data_fname[dst_type], block_free);
240 block_free = p->head.block_last[dst_type]++;
241 if (block_prev == -1)
243 entry.next = block_free*8 + dst_type;
245 p->head.total_bytes += size;
246 write_indx (p, rec->sysno, &entry, sizeof(entry));
250 memcpy (cptr, &block_free, sizeof(int));
251 bf_write (p->data_BFile[dst_type], block_prev, 0, 0, cptr);
252 cptr = p->tmp_buf + no_written;
254 block_prev = block_free;
255 no_written += p->head.block_size[dst_type] - sizeof(int);
256 p->head.block_used[dst_type]++;
258 assert (block_prev != -1);
260 memcpy (cptr, &block_free, sizeof(int));
261 bf_write (p->data_BFile[dst_type], block_prev, 0,
262 sizeof(int) + (p->tmp_buf+size) - cptr, cptr);
265 static void rec_update_single (Records p, Record rec)
267 rec_release_blocks (p, rec->sysno);
268 rec_write_single (p, rec);
271 Records rec_open (BFiles bfs, int rw)
276 p = xmalloc (sizeof(*p));
279 p->tmp_buf = xmalloc (p->tmp_size);
280 p->index_fname = "recindex";
281 p->index_BFile = bf_open (bfs, p->index_fname, 128, rw);
282 if (p->index_BFile == NULL)
284 logf (LOG_FATAL|LOG_ERRNO, "open %s", p->index_fname);
287 r = bf_read (p->index_BFile, 0, 0, 0, p->tmp_buf);
291 memcpy (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic));
292 p->head.index_free = 0;
293 p->head.index_last = 1;
294 p->head.no_records = 0;
295 p->head.total_bytes = 0;
296 for (i = 0; i<REC_BLOCK_TYPES; i++)
298 p->head.block_free[i] = 0;
299 p->head.block_last[i] = 1;
300 p->head.block_used[i] = 0;
302 p->head.block_size[0] = 128;
303 p->head.block_move[0] = 0;
304 for (i = 1; i<REC_BLOCK_TYPES; i++)
306 p->head.block_size[i] = p->head.block_size[i-1] * 4;
307 p->head.block_move[i] = p->head.block_size[i] * 3;
313 memcpy (&p->head, p->tmp_buf, sizeof(p->head));
314 if (memcmp (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic)))
316 logf (LOG_FATAL, "read %s. bad header", p->index_fname);
321 for (i = 0; i<REC_BLOCK_TYPES; i++)
324 sprintf (str, "recdata%c", i + 'A');
325 p->data_fname[i] = xmalloc (strlen(str)+1);
326 strcpy (p->data_fname[i], str);
327 p->data_BFile[i] = NULL;
329 for (i = 0; i<REC_BLOCK_TYPES; i++)
331 if (!(p->data_BFile[i] = bf_open (bfs, p->data_fname[i],
332 p->head.block_size[i],
335 logf (LOG_FATAL|LOG_ERRNO, "bf_open %s", p->data_fname[i]);
341 p->record_cache = xmalloc (sizeof(*p->record_cache)*p->cache_max);
345 static void rec_cache_flush (Records p, int saveCount)
349 if (saveCount >= p->cache_cur)
351 for (i = 0; i<p->cache_cur - saveCount; i++)
353 struct record_cache_entry *e = p->record_cache + i;
359 rec_write_single (p, e->rec);
361 case recordFlagWrite:
362 rec_update_single (p, e->rec);
364 case recordFlagDelete:
365 rec_delete_single (p, e->rec);
370 for (j = 0; j<saveCount; j++, i++)
371 memcpy (p->record_cache+j, p->record_cache+i,
372 sizeof(*p->record_cache));
373 p->cache_cur = saveCount;
376 static Record *rec_cache_lookup (Records p, int sysno,
377 enum recordCacheFlag flag)
380 for (i = 0; i<p->cache_cur; i++)
382 struct record_cache_entry *e = p->record_cache + i;
383 if (e->rec->sysno == sysno)
385 if (flag != recordFlagNop && e->flag == recordFlagNop)
393 static void rec_cache_insert (Records p, Record rec, enum recordCacheFlag flag)
395 struct record_cache_entry *e;
397 if (p->cache_cur == p->cache_max)
398 rec_cache_flush (p, 1);
399 else if (p->cache_cur > 0)
403 for (i = 0; i<p->cache_cur; i++)
405 Record r = (p->record_cache + i)->rec;
406 for (j = 0; j<REC_NO_INFO; j++)
410 rec_cache_flush (p, 1);
412 assert (p->cache_cur < p->cache_max);
414 e = p->record_cache + (p->cache_cur)++;
416 e->rec = rec_cp (rec);
419 void rec_close (Records *pp)
426 rec_cache_flush (p, 0);
427 xfree (p->record_cache);
433 bf_close (p->index_BFile);
435 for (i = 0; i<REC_BLOCK_TYPES; i++)
437 if (p->data_BFile[i])
438 bf_close (p->data_BFile[i]);
439 xfree (p->data_fname[i]);
447 Record rec_get (Records p, int sysno)
451 struct record_index_entry entry;
452 int freeblock, dst_type;
458 if ((recp = rec_cache_lookup (p, sysno, recordFlagNop)))
459 return rec_cp (*recp);
461 if (!read_indx (p, sysno, &entry, sizeof(entry), 1))
462 return NULL; /* record is not there! */
465 return NULL; /* record is deleted */
467 dst_type = entry.next & 7;
468 assert (dst_type < REC_BLOCK_TYPES);
469 freeblock = entry.next / 8;
471 assert (freeblock > 0);
473 rec = xmalloc (sizeof(*rec));
474 rec_tmp_expand (p, entry.size, dst_type);
477 bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
478 memcpy (&freeblock, cptr, sizeof(freeblock));
484 cptr += p->head.block_size[dst_type] - sizeof(freeblock);
486 memcpy (&tmp, cptr, sizeof(tmp));
487 bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
488 memcpy (&freeblock, cptr, sizeof(freeblock));
489 memcpy (cptr, &tmp, sizeof(tmp));
493 nptr = p->tmp_buf + sizeof(freeblock);
494 for (i = 0; i < REC_NO_INFO; i++)
496 memcpy (&rec->size[i], nptr, sizeof(*rec->size));
497 nptr += sizeof(*rec->size);
500 rec->info[i] = xmalloc (rec->size[i]);
501 memcpy (rec->info[i], nptr, rec->size[i]);
502 nptr += rec->size[i];
507 rec_cache_insert (p, rec, recordFlagNop);
511 Record rec_new (Records p)
517 rec = xmalloc (sizeof(*rec));
518 if (1 || p->head.index_free == 0)
519 sysno = (p->head.index_last)++;
522 struct record_index_entry entry;
524 read_indx (p, p->head.index_free, &entry, sizeof(entry), 0);
525 sysno = p->head.index_free;
526 p->head.index_free = entry.next;
528 (p->head.no_records)++;
530 for (i = 0; i < REC_NO_INFO; i++)
535 rec_cache_insert (p, rec, recordFlagNew);
539 void rec_del (Records p, Record *recpp)
543 (p->head.no_records)--;
544 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagDelete)))
551 rec_cache_insert (p, *recpp, recordFlagDelete);
557 void rec_put (Records p, Record *recpp)
561 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagWrite)))
568 rec_cache_insert (p, *recpp, recordFlagWrite);
574 void rec_rm (Record *recpp)
580 for (i = 0; i < REC_NO_INFO; i++)
581 xfree ((*recpp)->info[i]);
586 Record rec_cp (Record rec)
591 n = xmalloc (sizeof(*n));
592 n->sysno = rec->sysno;
593 for (i = 0; i < REC_NO_INFO; i++)
601 n->size[i] = rec->size[i];
602 n->info[i] = xmalloc (rec->size[i]);
603 memcpy (n->info[i], rec->info[i], rec->size[i]);
609 char *rec_strdup (const char *s, size_t *len)