2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.23 1999-05-26 07:49:13 adam
10 * Revision 1.22 1999/02/18 12:49:34 adam
11 * Changed file naming scheme for register files as well as record
14 * Revision 1.21 1999/02/02 14:51:03 adam
15 * Updated WIN32 code specific sections. Changed header.
17 * Revision 1.20 1998/01/12 15:04:08 adam
18 * The test option (-s) only uses read-lock (and not write lock).
20 * Revision 1.19 1997/09/17 12:19:16 adam
21 * Zebra version corresponds to YAZ version 1.4.
22 * Changed Zebra server so that it doesn't depend on global common_resource.
24 * Revision 1.18 1997/07/15 16:28:42 adam
25 * Bug fix: storeData didn't work with files with multiple records.
26 * Bug fix: fixed memory management with records; not really well
29 * Revision 1.17 1997/02/12 20:39:46 adam
30 * Implemented options -f <n> that limits the log to the first <n>
32 * Changed some log messages also.
34 * Revision 1.16 1996/06/04 10:19:00 adam
35 * Minor changes - removed include of ctype.h.
37 * Revision 1.15 1996/05/13 14:23:06 adam
38 * Work on compaction of set/use bytes in dictionary.
40 * Revision 1.14 1996/02/01 20:48:15 adam
41 * The total size of records are always checked in rec_cache_insert to
42 * reduce memory usage.
44 * Revision 1.13 1995/12/11 09:12:49 adam
45 * The rec_get function returns NULL if record doesn't exist - will
46 * happen in the server if the result set records have been deleted since
47 * the creation of the set (i.e. the search).
48 * The server saves a result temporarily if it is 'volatile', i.e. the
49 * set is register dependent.
51 * Revision 1.12 1995/12/07 17:38:47 adam
52 * Work locking mechanisms for concurrent updates/commit.
54 * Revision 1.11 1995/12/06 13:58:26 adam
55 * Improved flushing of records - all flushes except the last one
56 * don't write the last accessed. Also flush takes place if record
57 * info occupy more than about 256k.
59 * Revision 1.10 1995/12/06 12:41:24 adam
60 * New command 'stat' for the index program.
61 * Filenames can be read from stdin by specifying '-'.
62 * Bug fix/enhancement of the transformation from terms to regular
63 * expressons in the search engine.
65 * Revision 1.9 1995/11/30 08:34:33 adam
66 * Started work on commit facility.
67 * Changed a few malloc/free to xmalloc/xfree.
69 * Revision 1.8 1995/11/28 14:26:21 adam
70 * Bug fix: recordId with constant wasn't right.
71 * Bug fix: recordId dictionary entry wasn't deleted when needed.
73 * Revision 1.7 1995/11/28 09:09:43 adam
74 * Zebra config renamed.
75 * Use setting 'recordId' to identify record now.
76 * Bug fix in recindex.c: rec_release_blocks was invokeded even
77 * though the blocks were already released.
78 * File traversal properly deletes records when needed.
80 * Revision 1.6 1995/11/25 10:24:06 adam
81 * More record fields - they are enumerated now.
82 * New options: flagStoreData flagStoreKey.
84 * Revision 1.5 1995/11/22 17:19:18 adam
85 * Record management uses the bfile system.
87 * Revision 1.4 1995/11/20 16:59:46 adam
88 * New update method: the 'old' keys are saved for each records.
90 * Revision 1.3 1995/11/16 15:34:55 adam
91 * Uses new record management system in both indexer and server.
93 * Revision 1.2 1995/11/15 19:13:08 adam
94 * Work on record management.
96 * Revision 1.1 1995/11/15 14:46:20 adam
97 * Started work on better record management system.
104 #include "recindxp.h"
106 static void rec_write_head (Records p)
111 assert (p->index_BFile);
113 r = bf_write (p->index_BFile, 0, 0, sizeof(p->head), &p->head);
116 logf (LOG_FATAL|LOG_ERRNO, "write head of %s", p->index_fname);
121 static void rec_tmp_expand (Records p, int size, int dst_type)
123 if (p->tmp_size < size + 2048 ||
124 p->tmp_size < p->head.block_size[dst_type]*2)
127 p->tmp_size = size + p->head.block_size[dst_type]*2 + 2048;
128 p->tmp_buf = (char *) xmalloc (p->tmp_size);
132 static int read_indx (Records p, int sysno, void *buf, int itemsize,
136 int pos = (sysno-1)*itemsize;
138 r = bf_read (p->index_BFile, 1+pos/128, pos%128, itemsize, buf);
139 if (r != 1 && !ignoreError)
141 logf (LOG_FATAL|LOG_ERRNO, "read in %s at pos %ld",
142 p->index_fname, (long) pos);
148 static void write_indx (Records p, int sysno, void *buf, int itemsize)
150 int pos = (sysno-1)*itemsize;
152 bf_write (p->index_BFile, 1+pos/128, pos%128, itemsize, buf);
155 static void rec_release_blocks (Records p, int sysno)
157 struct record_index_entry entry;
158 int freeblock, freenext;
161 if (read_indx (p, sysno, &entry, sizeof(entry), 1) != 1)
163 p->head.total_bytes -= entry.size;
164 freeblock = entry.next;
165 assert (freeblock > 0);
166 dst_type = freeblock & 7;
167 assert (dst_type < REC_BLOCK_TYPES);
168 freeblock = freeblock / 8;
171 if (bf_read (p->data_BFile[dst_type], freeblock, 0, sizeof(freenext),
174 logf (LOG_FATAL|LOG_ERRNO, "read in rec_del_single");
177 if (bf_write (p->data_BFile[dst_type], freeblock, 0, sizeof(freenext),
178 &p->head.block_free[dst_type]))
180 logf (LOG_FATAL|LOG_ERRNO, "write in rec_del_single");
183 p->head.block_free[dst_type] = freeblock;
184 freeblock = freenext;
185 p->head.block_used[dst_type]--;
189 static void rec_delete_single (Records p, Record rec)
191 struct record_index_entry entry;
193 rec_release_blocks (p, rec->sysno);
195 entry.next = p->head.index_free;
197 p->head.index_free = rec->sysno;
198 write_indx (p, rec->sysno, &entry, sizeof(entry));
202 static void rec_write_single (Records p, Record rec)
208 int block_prev = -1, block_free;
209 struct record_index_entry entry;
211 for (i = 0; i < REC_NO_INFO; i++)
213 size += sizeof(*rec->size);
215 size += sizeof(*rec->size) + rec->size[i];
217 for (i = 1; i<REC_BLOCK_TYPES; i++)
218 if (size >= p->head.block_move[i])
221 rec_tmp_expand (p, size, dst_type);
223 cptr = p->tmp_buf + sizeof(int); /* a hack! */
224 for (i = 0; i < REC_NO_INFO; i++)
226 memcpy (cptr, &rec->size[i], sizeof(*rec->size));
227 cptr += sizeof(*rec->size);
230 memcpy (cptr, rec->info[i], rec->size[i]);
231 cptr += rec->size[i];
235 while (no_written < size)
237 block_free = p->head.block_free[dst_type];
240 if (bf_read (p->data_BFile[dst_type],
241 block_free, 0, sizeof(*p->head.block_free),
242 &p->head.block_free[dst_type]) != 1)
244 logf (LOG_FATAL|LOG_ERRNO, "read in %s at free block %d",
245 p->data_fname[dst_type], block_free);
250 block_free = p->head.block_last[dst_type]++;
251 if (block_prev == -1)
253 entry.next = block_free*8 + dst_type;
255 p->head.total_bytes += size;
256 write_indx (p, rec->sysno, &entry, sizeof(entry));
260 memcpy (cptr, &block_free, sizeof(int));
261 bf_write (p->data_BFile[dst_type], block_prev, 0, 0, cptr);
262 cptr = p->tmp_buf + no_written;
264 block_prev = block_free;
265 no_written += p->head.block_size[dst_type] - sizeof(int);
266 p->head.block_used[dst_type]++;
268 assert (block_prev != -1);
270 memcpy (cptr, &block_free, sizeof(int));
271 bf_write (p->data_BFile[dst_type], block_prev, 0,
272 sizeof(int) + (p->tmp_buf+size) - cptr, cptr);
275 static void rec_update_single (Records p, Record rec)
277 rec_release_blocks (p, rec->sysno);
278 rec_write_single (p, rec);
281 Records rec_open (BFiles bfs, int rw)
286 p = (Records) xmalloc (sizeof(*p));
289 p->tmp_buf = (char *) xmalloc (p->tmp_size);
290 p->index_fname = "reci";
291 p->index_BFile = bf_open (bfs, p->index_fname, 128, rw);
292 if (p->index_BFile == NULL)
294 logf (LOG_FATAL|LOG_ERRNO, "open %s", p->index_fname);
297 r = bf_read (p->index_BFile, 0, 0, 0, p->tmp_buf);
301 memcpy (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic));
302 p->head.index_free = 0;
303 p->head.index_last = 1;
304 p->head.no_records = 0;
305 p->head.total_bytes = 0;
306 for (i = 0; i<REC_BLOCK_TYPES; i++)
308 p->head.block_free[i] = 0;
309 p->head.block_last[i] = 1;
310 p->head.block_used[i] = 0;
312 p->head.block_size[0] = 128;
313 p->head.block_move[0] = 0;
314 for (i = 1; i<REC_BLOCK_TYPES; i++)
316 p->head.block_size[i] = p->head.block_size[i-1] * 4;
317 p->head.block_move[i] = p->head.block_size[i] * 3;
323 memcpy (&p->head, p->tmp_buf, sizeof(p->head));
324 if (memcmp (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic)))
326 logf (LOG_FATAL, "read %s. bad header", p->index_fname);
331 for (i = 0; i<REC_BLOCK_TYPES; i++)
334 sprintf (str, "recd%c", i + 'A');
335 p->data_fname[i] = (char *) xmalloc (strlen(str)+1);
336 strcpy (p->data_fname[i], str);
337 p->data_BFile[i] = NULL;
339 for (i = 0; i<REC_BLOCK_TYPES; i++)
341 if (!(p->data_BFile[i] = bf_open (bfs, p->data_fname[i],
342 p->head.block_size[i],
345 logf (LOG_FATAL|LOG_ERRNO, "bf_open %s", p->data_fname[i]);
351 p->record_cache = (struct record_cache_entry *)
352 xmalloc (sizeof(*p->record_cache)*p->cache_max);
356 static void rec_cache_flush (Records p, int saveCount)
360 if (saveCount >= p->cache_cur)
362 for (i = 0; i<p->cache_cur - saveCount; i++)
364 struct record_cache_entry *e = p->record_cache + i;
370 rec_write_single (p, e->rec);
372 case recordFlagWrite:
373 rec_update_single (p, e->rec);
375 case recordFlagDelete:
376 rec_delete_single (p, e->rec);
381 for (j = 0; j<saveCount; j++, i++)
382 memcpy (p->record_cache+j, p->record_cache+i,
383 sizeof(*p->record_cache));
384 p->cache_cur = saveCount;
387 static Record *rec_cache_lookup (Records p, int sysno,
388 enum recordCacheFlag flag)
391 for (i = 0; i<p->cache_cur; i++)
393 struct record_cache_entry *e = p->record_cache + i;
394 if (e->rec->sysno == sysno)
396 if (flag != recordFlagNop && e->flag == recordFlagNop)
404 static void rec_cache_insert (Records p, Record rec, enum recordCacheFlag flag)
406 struct record_cache_entry *e;
408 if (p->cache_cur == p->cache_max)
409 rec_cache_flush (p, 1);
410 else if (p->cache_cur > 0)
414 for (i = 0; i<p->cache_cur; i++)
416 Record r = (p->record_cache + i)->rec;
417 for (j = 0; j<REC_NO_INFO; j++)
421 rec_cache_flush (p, 1);
423 assert (p->cache_cur < p->cache_max);
425 e = p->record_cache + (p->cache_cur)++;
427 e->rec = rec_cp (rec);
430 void rec_close (Records *pp)
437 rec_cache_flush (p, 0);
438 xfree (p->record_cache);
444 bf_close (p->index_BFile);
446 for (i = 0; i<REC_BLOCK_TYPES; i++)
448 if (p->data_BFile[i])
449 bf_close (p->data_BFile[i]);
450 xfree (p->data_fname[i]);
458 Record rec_get (Records p, int sysno)
462 struct record_index_entry entry;
463 int freeblock, dst_type;
469 if ((recp = rec_cache_lookup (p, sysno, recordFlagNop)))
470 return rec_cp (*recp);
472 if (!read_indx (p, sysno, &entry, sizeof(entry), 1))
473 return NULL; /* record is not there! */
476 return NULL; /* record is deleted */
478 dst_type = entry.next & 7;
479 assert (dst_type < REC_BLOCK_TYPES);
480 freeblock = entry.next / 8;
482 assert (freeblock > 0);
484 rec = (Record) xmalloc (sizeof(*rec));
485 rec_tmp_expand (p, entry.size, dst_type);
488 bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
489 memcpy (&freeblock, cptr, sizeof(freeblock));
495 cptr += p->head.block_size[dst_type] - sizeof(freeblock);
497 memcpy (&tmp, cptr, sizeof(tmp));
498 bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
499 memcpy (&freeblock, cptr, sizeof(freeblock));
500 memcpy (cptr, &tmp, sizeof(tmp));
504 nptr = p->tmp_buf + sizeof(freeblock);
505 for (i = 0; i < REC_NO_INFO; i++)
507 memcpy (&rec->size[i], nptr, sizeof(*rec->size));
508 nptr += sizeof(*rec->size);
511 rec->info[i] = (char *) xmalloc (rec->size[i]);
512 memcpy (rec->info[i], nptr, rec->size[i]);
513 nptr += rec->size[i];
518 rec_cache_insert (p, rec, recordFlagNop);
522 Record rec_new (Records p)
528 rec = (Record) xmalloc (sizeof(*rec));
529 if (1 || p->head.index_free == 0)
530 sysno = (p->head.index_last)++;
533 struct record_index_entry entry;
535 read_indx (p, p->head.index_free, &entry, sizeof(entry), 0);
536 sysno = p->head.index_free;
537 p->head.index_free = entry.next;
539 (p->head.no_records)++;
541 for (i = 0; i < REC_NO_INFO; i++)
546 rec_cache_insert (p, rec, recordFlagNew);
550 void rec_del (Records p, Record *recpp)
554 (p->head.no_records)--;
555 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagDelete)))
562 rec_cache_insert (p, *recpp, recordFlagDelete);
568 void rec_put (Records p, Record *recpp)
572 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagWrite)))
579 rec_cache_insert (p, *recpp, recordFlagWrite);
585 void rec_rm (Record *recpp)
591 for (i = 0; i < REC_NO_INFO; i++)
592 xfree ((*recpp)->info[i]);
597 Record rec_cp (Record rec)
602 n = (Record) xmalloc (sizeof(*n));
603 n->sysno = rec->sysno;
604 for (i = 0; i < REC_NO_INFO; i++)
612 n->size[i] = rec->size[i];
613 n->info[i] = (char *) xmalloc (rec->size[i]);
614 memcpy (n->info[i], rec->info[i], rec->size[i]);
620 char *rec_strdup (const char *s, size_t *len)
630 p = (char *) xmalloc (*len);