2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.21 1999-02-02 14:51:03 adam
8 * Updated WIN32 code specific sections. Changed header.
10 * Revision 1.20 1998/01/12 15:04:08 adam
11 * The test option (-s) only uses read-lock (and not write lock).
13 * Revision 1.19 1997/09/17 12:19:16 adam
14 * Zebra version corresponds to YAZ version 1.4.
15 * Changed Zebra server so that it doesn't depend on global common_resource.
17 * Revision 1.18 1997/07/15 16:28:42 adam
18 * Bug fix: storeData didn't work with files with multiple records.
19 * Bug fix: fixed memory management with records; not really well
22 * Revision 1.17 1997/02/12 20:39:46 adam
23 * Implemented options -f <n> that limits the log to the first <n>
25 * Changed some log messages also.
27 * Revision 1.16 1996/06/04 10:19:00 adam
28 * Minor changes - removed include of ctype.h.
30 * Revision 1.15 1996/05/13 14:23:06 adam
31 * Work on compaction of set/use bytes in dictionary.
33 * Revision 1.14 1996/02/01 20:48:15 adam
34 * The total size of records are always checked in rec_cache_insert to
35 * reduce memory usage.
37 * Revision 1.13 1995/12/11 09:12:49 adam
38 * The rec_get function returns NULL if record doesn't exist - will
39 * happen in the server if the result set records have been deleted since
40 * the creation of the set (i.e. the search).
41 * The server saves a result temporarily if it is 'volatile', i.e. the
42 * set is register dependent.
44 * Revision 1.12 1995/12/07 17:38:47 adam
45 * Work locking mechanisms for concurrent updates/commit.
47 * Revision 1.11 1995/12/06 13:58:26 adam
48 * Improved flushing of records - all flushes except the last one
49 * don't write the last accessed. Also flush takes place if record
50 * info occupy more than about 256k.
52 * Revision 1.10 1995/12/06 12:41:24 adam
53 * New command 'stat' for the index program.
54 * Filenames can be read from stdin by specifying '-'.
55 * Bug fix/enhancement of the transformation from terms to regular
56 * expressons in the search engine.
58 * Revision 1.9 1995/11/30 08:34:33 adam
59 * Started work on commit facility.
60 * Changed a few malloc/free to xmalloc/xfree.
62 * Revision 1.8 1995/11/28 14:26:21 adam
63 * Bug fix: recordId with constant wasn't right.
64 * Bug fix: recordId dictionary entry wasn't deleted when needed.
66 * Revision 1.7 1995/11/28 09:09:43 adam
67 * Zebra config renamed.
68 * Use setting 'recordId' to identify record now.
69 * Bug fix in recindex.c: rec_release_blocks was invokeded even
70 * though the blocks were already released.
71 * File traversal properly deletes records when needed.
73 * Revision 1.6 1995/11/25 10:24:06 adam
74 * More record fields - they are enumerated now.
75 * New options: flagStoreData flagStoreKey.
77 * Revision 1.5 1995/11/22 17:19:18 adam
78 * Record management uses the bfile system.
80 * Revision 1.4 1995/11/20 16:59:46 adam
81 * New update method: the 'old' keys are saved for each records.
83 * Revision 1.3 1995/11/16 15:34:55 adam
84 * Uses new record management system in both indexer and server.
86 * Revision 1.2 1995/11/15 19:13:08 adam
87 * Work on record management.
89 * Revision 1.1 1995/11/15 14:46:20 adam
90 * Started work on better record management system.
99 static void rec_write_head (Records p)
104 assert (p->index_BFile);
106 r = bf_write (p->index_BFile, 0, 0, sizeof(p->head), &p->head);
109 logf (LOG_FATAL|LOG_ERRNO, "write head of %s", p->index_fname);
114 static void rec_tmp_expand (Records p, int size, int dst_type)
116 if (p->tmp_size < size + 2048 ||
117 p->tmp_size < p->head.block_size[dst_type]*2)
120 p->tmp_size = size + p->head.block_size[dst_type]*2 + 2048;
121 p->tmp_buf = xmalloc (p->tmp_size);
125 static int read_indx (Records p, int sysno, void *buf, int itemsize,
129 int pos = (sysno-1)*itemsize;
131 r = bf_read (p->index_BFile, 1+pos/128, pos%128, itemsize, buf);
132 if (r != 1 && !ignoreError)
134 logf (LOG_FATAL|LOG_ERRNO, "read in %s at pos %ld",
135 p->index_fname, (long) pos);
141 static void write_indx (Records p, int sysno, void *buf, int itemsize)
143 int pos = (sysno-1)*itemsize;
145 bf_write (p->index_BFile, 1+pos/128, pos%128, itemsize, buf);
148 static void rec_release_blocks (Records p, int sysno)
150 struct record_index_entry entry;
151 int freeblock, freenext;
154 if (read_indx (p, sysno, &entry, sizeof(entry), 1) != 1)
156 p->head.total_bytes -= entry.size;
157 freeblock = entry.next;
158 assert (freeblock > 0);
159 dst_type = freeblock & 7;
160 assert (dst_type < REC_BLOCK_TYPES);
161 freeblock = freeblock / 8;
164 if (bf_read (p->data_BFile[dst_type], freeblock, 0, sizeof(freenext),
167 logf (LOG_FATAL|LOG_ERRNO, "read in rec_del_single");
170 if (bf_write (p->data_BFile[dst_type], freeblock, 0, sizeof(freenext),
171 &p->head.block_free[dst_type]))
173 logf (LOG_FATAL|LOG_ERRNO, "write in rec_del_single");
176 p->head.block_free[dst_type] = freeblock;
177 freeblock = freenext;
178 p->head.block_used[dst_type]--;
182 static void rec_delete_single (Records p, Record rec)
184 struct record_index_entry entry;
186 rec_release_blocks (p, rec->sysno);
188 entry.next = p->head.index_free;
190 p->head.index_free = rec->sysno;
191 write_indx (p, rec->sysno, &entry, sizeof(entry));
195 static void rec_write_single (Records p, Record rec)
201 int block_prev = -1, block_free;
202 struct record_index_entry entry;
204 for (i = 0; i < REC_NO_INFO; i++)
206 size += sizeof(*rec->size);
208 size += sizeof(*rec->size) + rec->size[i];
210 for (i = 1; i<REC_BLOCK_TYPES; i++)
211 if (size >= p->head.block_move[i])
214 rec_tmp_expand (p, size, dst_type);
216 cptr = p->tmp_buf + sizeof(int); /* a hack! */
217 for (i = 0; i < REC_NO_INFO; i++)
219 memcpy (cptr, &rec->size[i], sizeof(*rec->size));
220 cptr += sizeof(*rec->size);
223 memcpy (cptr, rec->info[i], rec->size[i]);
224 cptr += rec->size[i];
228 while (no_written < size)
230 block_free = p->head.block_free[dst_type];
233 if (bf_read (p->data_BFile[dst_type],
234 block_free, 0, sizeof(*p->head.block_free),
235 &p->head.block_free[dst_type]) != 1)
237 logf (LOG_FATAL|LOG_ERRNO, "read in %s at free block %d",
238 p->data_fname[dst_type], block_free);
243 block_free = p->head.block_last[dst_type]++;
244 if (block_prev == -1)
246 entry.next = block_free*8 + dst_type;
248 p->head.total_bytes += size;
249 write_indx (p, rec->sysno, &entry, sizeof(entry));
253 memcpy (cptr, &block_free, sizeof(int));
254 bf_write (p->data_BFile[dst_type], block_prev, 0, 0, cptr);
255 cptr = p->tmp_buf + no_written;
257 block_prev = block_free;
258 no_written += p->head.block_size[dst_type] - sizeof(int);
259 p->head.block_used[dst_type]++;
261 assert (block_prev != -1);
263 memcpy (cptr, &block_free, sizeof(int));
264 bf_write (p->data_BFile[dst_type], block_prev, 0,
265 sizeof(int) + (p->tmp_buf+size) - cptr, cptr);
268 static void rec_update_single (Records p, Record rec)
270 rec_release_blocks (p, rec->sysno);
271 rec_write_single (p, rec);
274 Records rec_open (BFiles bfs, int rw)
279 p = xmalloc (sizeof(*p));
282 p->tmp_buf = xmalloc (p->tmp_size);
283 p->index_fname = "recindex";
284 p->index_BFile = bf_open (bfs, p->index_fname, 128, rw);
285 if (p->index_BFile == NULL)
287 logf (LOG_FATAL|LOG_ERRNO, "open %s", p->index_fname);
290 r = bf_read (p->index_BFile, 0, 0, 0, p->tmp_buf);
294 memcpy (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic));
295 p->head.index_free = 0;
296 p->head.index_last = 1;
297 p->head.no_records = 0;
298 p->head.total_bytes = 0;
299 for (i = 0; i<REC_BLOCK_TYPES; i++)
301 p->head.block_free[i] = 0;
302 p->head.block_last[i] = 1;
303 p->head.block_used[i] = 0;
305 p->head.block_size[0] = 128;
306 p->head.block_move[0] = 0;
307 for (i = 1; i<REC_BLOCK_TYPES; i++)
309 p->head.block_size[i] = p->head.block_size[i-1] * 4;
310 p->head.block_move[i] = p->head.block_size[i] * 3;
316 memcpy (&p->head, p->tmp_buf, sizeof(p->head));
317 if (memcmp (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic)))
319 logf (LOG_FATAL, "read %s. bad header", p->index_fname);
324 for (i = 0; i<REC_BLOCK_TYPES; i++)
327 sprintf (str, "recdata%c", i + 'A');
328 p->data_fname[i] = xmalloc (strlen(str)+1);
329 strcpy (p->data_fname[i], str);
330 p->data_BFile[i] = NULL;
332 for (i = 0; i<REC_BLOCK_TYPES; i++)
334 if (!(p->data_BFile[i] = bf_open (bfs, p->data_fname[i],
335 p->head.block_size[i],
338 logf (LOG_FATAL|LOG_ERRNO, "bf_open %s", p->data_fname[i]);
344 p->record_cache = xmalloc (sizeof(*p->record_cache)*p->cache_max);
348 static void rec_cache_flush (Records p, int saveCount)
352 if (saveCount >= p->cache_cur)
354 for (i = 0; i<p->cache_cur - saveCount; i++)
356 struct record_cache_entry *e = p->record_cache + i;
362 rec_write_single (p, e->rec);
364 case recordFlagWrite:
365 rec_update_single (p, e->rec);
367 case recordFlagDelete:
368 rec_delete_single (p, e->rec);
373 for (j = 0; j<saveCount; j++, i++)
374 memcpy (p->record_cache+j, p->record_cache+i,
375 sizeof(*p->record_cache));
376 p->cache_cur = saveCount;
379 static Record *rec_cache_lookup (Records p, int sysno,
380 enum recordCacheFlag flag)
383 for (i = 0; i<p->cache_cur; i++)
385 struct record_cache_entry *e = p->record_cache + i;
386 if (e->rec->sysno == sysno)
388 if (flag != recordFlagNop && e->flag == recordFlagNop)
396 static void rec_cache_insert (Records p, Record rec, enum recordCacheFlag flag)
398 struct record_cache_entry *e;
400 if (p->cache_cur == p->cache_max)
401 rec_cache_flush (p, 1);
402 else if (p->cache_cur > 0)
406 for (i = 0; i<p->cache_cur; i++)
408 Record r = (p->record_cache + i)->rec;
409 for (j = 0; j<REC_NO_INFO; j++)
413 rec_cache_flush (p, 1);
415 assert (p->cache_cur < p->cache_max);
417 e = p->record_cache + (p->cache_cur)++;
419 e->rec = rec_cp (rec);
422 void rec_close (Records *pp)
429 rec_cache_flush (p, 0);
430 xfree (p->record_cache);
436 bf_close (p->index_BFile);
438 for (i = 0; i<REC_BLOCK_TYPES; i++)
440 if (p->data_BFile[i])
441 bf_close (p->data_BFile[i]);
442 xfree (p->data_fname[i]);
450 Record rec_get (Records p, int sysno)
454 struct record_index_entry entry;
455 int freeblock, dst_type;
461 if ((recp = rec_cache_lookup (p, sysno, recordFlagNop)))
462 return rec_cp (*recp);
464 if (!read_indx (p, sysno, &entry, sizeof(entry), 1))
465 return NULL; /* record is not there! */
468 return NULL; /* record is deleted */
470 dst_type = entry.next & 7;
471 assert (dst_type < REC_BLOCK_TYPES);
472 freeblock = entry.next / 8;
474 assert (freeblock > 0);
476 rec = xmalloc (sizeof(*rec));
477 rec_tmp_expand (p, entry.size, dst_type);
480 bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
481 memcpy (&freeblock, cptr, sizeof(freeblock));
487 cptr += p->head.block_size[dst_type] - sizeof(freeblock);
489 memcpy (&tmp, cptr, sizeof(tmp));
490 bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
491 memcpy (&freeblock, cptr, sizeof(freeblock));
492 memcpy (cptr, &tmp, sizeof(tmp));
496 nptr = p->tmp_buf + sizeof(freeblock);
497 for (i = 0; i < REC_NO_INFO; i++)
499 memcpy (&rec->size[i], nptr, sizeof(*rec->size));
500 nptr += sizeof(*rec->size);
503 rec->info[i] = xmalloc (rec->size[i]);
504 memcpy (rec->info[i], nptr, rec->size[i]);
505 nptr += rec->size[i];
510 rec_cache_insert (p, rec, recordFlagNop);
514 Record rec_new (Records p)
520 rec = xmalloc (sizeof(*rec));
521 if (1 || p->head.index_free == 0)
522 sysno = (p->head.index_last)++;
525 struct record_index_entry entry;
527 read_indx (p, p->head.index_free, &entry, sizeof(entry), 0);
528 sysno = p->head.index_free;
529 p->head.index_free = entry.next;
531 (p->head.no_records)++;
533 for (i = 0; i < REC_NO_INFO; i++)
538 rec_cache_insert (p, rec, recordFlagNew);
542 void rec_del (Records p, Record *recpp)
546 (p->head.no_records)--;
547 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagDelete)))
554 rec_cache_insert (p, *recpp, recordFlagDelete);
560 void rec_put (Records p, Record *recpp)
564 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagWrite)))
571 rec_cache_insert (p, *recpp, recordFlagWrite);
577 void rec_rm (Record *recpp)
583 for (i = 0; i < REC_NO_INFO; i++)
584 xfree ((*recpp)->info[i]);
589 Record rec_cp (Record rec)
594 n = xmalloc (sizeof(*n));
595 n->sysno = rec->sysno;
596 for (i = 0; i < REC_NO_INFO; i++)
604 n->size[i] = rec->size[i];
605 n->info[i] = xmalloc (rec->size[i]);
606 memcpy (n->info[i], rec->info[i], rec->size[i]);
612 char *rec_strdup (const char *s, size_t *len)