2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.28 1999-12-08 22:44:45 adam
8 * Zebra/Z'mbol dependencies added.
10 * Revision 1.27 1999/10/29 10:02:33 adam
11 * Fixed decompression buffer overflow.
13 * Revision 1.26 1999/07/06 13:34:57 adam
14 * Fixed bug (introduced by previous commit).
16 * Revision 1.25 1999/07/06 12:28:04 adam
17 * Updated record index structure. Format includes version ID. Compression
18 * algorithm ID is stored for each record block.
20 * Revision 1.24 1999/06/25 13:48:02 adam
21 * Updated MSVC project files.
22 * Added BZIP2 record compression (not very well tested).
24 * Revision 1.23 1999/05/26 07:49:13 adam
27 * Revision 1.22 1999/02/18 12:49:34 adam
28 * Changed file naming scheme for register files as well as record
31 * Revision 1.21 1999/02/02 14:51:03 adam
32 * Updated WIN32 code specific sections. Changed header.
34 * Revision 1.20 1998/01/12 15:04:08 adam
35 * The test option (-s) only uses read-lock (and not write lock).
37 * Revision 1.19 1997/09/17 12:19:16 adam
38 * Zebra version corresponds to YAZ version 1.4.
39 * Changed Zebra server so that it doesn't depend on global common_resource.
41 * Revision 1.18 1997/07/15 16:28:42 adam
42 * Bug fix: storeData didn't work with files with multiple records.
43 * Bug fix: fixed memory management with records; not really well
46 * Revision 1.17 1997/02/12 20:39:46 adam
47 * Implemented options -f <n> that limits the log to the first <n>
49 * Changed some log messages also.
51 * Revision 1.16 1996/06/04 10:19:00 adam
52 * Minor changes - removed include of ctype.h.
54 * Revision 1.15 1996/05/13 14:23:06 adam
55 * Work on compaction of set/use bytes in dictionary.
57 * Revision 1.14 1996/02/01 20:48:15 adam
58 * The total size of records are always checked in rec_cache_insert to
59 * reduce memory usage.
61 * Revision 1.13 1995/12/11 09:12:49 adam
62 * The rec_get function returns NULL if record doesn't exist - will
63 * happen in the server if the result set records have been deleted since
64 * the creation of the set (i.e. the search).
65 * The server saves a result temporarily if it is 'volatile', i.e. the
66 * set is register dependent.
68 * Revision 1.12 1995/12/07 17:38:47 adam
69 * Work locking mechanisms for concurrent updates/commit.
71 * Revision 1.11 1995/12/06 13:58:26 adam
72 * Improved flushing of records - all flushes except the last one
73 * don't write the last accessed. Also flush takes place if record
74 * info occupy more than about 256k.
76 * Revision 1.10 1995/12/06 12:41:24 adam
77 * New command 'stat' for the index program.
78 * Filenames can be read from stdin by specifying '-'.
79 * Bug fix/enhancement of the transformation from terms to regular
80 * expressons in the search engine.
82 * Revision 1.9 1995/11/30 08:34:33 adam
83 * Started work on commit facility.
84 * Changed a few malloc/free to xmalloc/xfree.
86 * Revision 1.8 1995/11/28 14:26:21 adam
87 * Bug fix: recordId with constant wasn't right.
88 * Bug fix: recordId dictionary entry wasn't deleted when needed.
90 * Revision 1.7 1995/11/28 09:09:43 adam
91 * Zebra config renamed.
92 * Use setting 'recordId' to identify record now.
93 * Bug fix in recindex.c: rec_release_blocks was invokeded even
94 * though the blocks were already released.
95 * File traversal properly deletes records when needed.
97 * Revision 1.6 1995/11/25 10:24:06 adam
98 * More record fields - they are enumerated now.
99 * New options: flagStoreData flagStoreKey.
101 * Revision 1.5 1995/11/22 17:19:18 adam
102 * Record management uses the bfile system.
104 * Revision 1.4 1995/11/20 16:59:46 adam
105 * New update method: the 'old' keys are saved for each records.
107 * Revision 1.3 1995/11/16 15:34:55 adam
108 * Uses new record management system in both indexer and server.
110 * Revision 1.2 1995/11/15 19:13:08 adam
111 * Work on record management.
113 * Revision 1.1 1995/11/15 14:46:20 adam
114 * Started work on better record management system.
120 * Format of first block
122 * ref_count (4 bytes)
125 * Format of subsequent blocks
129 * Format of each record
131 * (length, data) - pairs
132 * length = 0 if same as previous
138 #include "recindxp.h"
143 static void rec_write_head (Records p)
148 assert (p->index_BFile);
150 r = bf_write (p->index_BFile, 0, 0, sizeof(p->head), &p->head);
153 logf (LOG_FATAL|LOG_ERRNO, "write head of %s", p->index_fname);
158 static void rec_tmp_expand (Records p, int size)
160 if (p->tmp_size < size + 2048 ||
161 p->tmp_size < p->head.block_size[REC_BLOCK_TYPES-1]*2)
164 p->tmp_size = size + p->head.block_size[REC_BLOCK_TYPES-1]*2 + 2048;
165 p->tmp_buf = (char *) xmalloc (p->tmp_size);
169 static int read_indx (Records p, int sysno, void *buf, int itemsize,
173 int pos = (sysno-1)*itemsize;
175 r = bf_read (p->index_BFile, 1+pos/128, pos%128, itemsize, buf);
176 if (r != 1 && !ignoreError)
178 logf (LOG_FATAL|LOG_ERRNO, "read in %s at pos %ld",
179 p->index_fname, (long) pos);
185 static void write_indx (Records p, int sysno, void *buf, int itemsize)
187 int pos = (sysno-1)*itemsize;
189 bf_write (p->index_BFile, 1+pos/128, pos%128, itemsize, buf);
192 static void rec_release_blocks (Records p, int sysno)
194 struct record_index_entry entry;
196 char block_and_ref[sizeof(short) + sizeof(int)];
200 if (read_indx (p, sysno, &entry, sizeof(entry), 1) != 1)
203 freeblock = entry.next;
204 assert (freeblock > 0);
205 dst_type = freeblock & 7;
206 assert (dst_type < REC_BLOCK_TYPES);
207 freeblock = freeblock / 8;
210 if (bf_read (p->data_BFile[dst_type], freeblock, 0,
211 sizeof(block_and_ref), block_and_ref) != 1)
213 logf (LOG_FATAL|LOG_ERRNO, "read in rec_del_single");
219 memcpy (&ref, block_and_ref + sizeof(int), sizeof(ref));
221 memcpy (block_and_ref + sizeof(int), &ref, sizeof(ref));
224 if (bf_write (p->data_BFile[dst_type], freeblock, 0,
225 sizeof(block_and_ref), block_and_ref))
227 logf (LOG_FATAL|LOG_ERRNO, "write in rec_del_single");
235 if (bf_write (p->data_BFile[dst_type], freeblock, 0, sizeof(freeblock),
236 &p->head.block_free[dst_type]))
238 logf (LOG_FATAL|LOG_ERRNO, "write in rec_del_single");
241 p->head.block_free[dst_type] = freeblock;
242 memcpy (&freeblock, block_and_ref, sizeof(int));
244 p->head.block_used[dst_type]--;
246 p->head.total_bytes -= entry.size;
249 static void rec_delete_single (Records p, Record rec)
251 struct record_index_entry entry;
253 rec_release_blocks (p, rec->sysno);
255 entry.next = p->head.index_free;
257 p->head.index_free = rec->sysno;
258 write_indx (p, rec->sysno, &entry, sizeof(entry));
261 static void rec_write_tmp_buf (Records p, int size, int *sysnos)
263 struct record_index_entry entry;
265 char *cptr = p->tmp_buf;
266 int block_prev = -1, block_free;
270 for (i = 1; i<REC_BLOCK_TYPES; i++)
271 if (size >= p->head.block_move[i])
273 while (no_written < size)
275 block_free = p->head.block_free[dst_type];
278 if (bf_read (p->data_BFile[dst_type],
279 block_free, 0, sizeof(*p->head.block_free),
280 &p->head.block_free[dst_type]) != 1)
282 logf (LOG_FATAL|LOG_ERRNO, "read in %s at free block %d",
283 p->data_fname[dst_type], block_free);
288 block_free = p->head.block_last[dst_type]++;
289 if (block_prev == -1)
291 entry.next = block_free*8 + dst_type;
293 p->head.total_bytes += size;
296 write_indx (p, *sysnos, &entry, sizeof(entry));
302 memcpy (cptr, &block_free, sizeof(int));
303 bf_write (p->data_BFile[dst_type], block_prev, 0, 0, cptr);
304 cptr = p->tmp_buf + no_written;
306 block_prev = block_free;
307 no_written += p->head.block_size[dst_type] - sizeof(int);
308 p->head.block_used[dst_type]++;
310 assert (block_prev != -1);
312 memcpy (cptr, &block_free, sizeof(int));
313 bf_write (p->data_BFile[dst_type], block_prev, 0,
314 sizeof(int) + (p->tmp_buf+size) - cptr, cptr);
317 Records rec_open (BFiles bfs, int rw, int compression_method)
323 p = (Records) xmalloc (sizeof(*p));
324 p->compression_method = compression_method;
327 p->tmp_buf = (char *) xmalloc (p->tmp_size);
328 p->index_fname = "reci";
329 p->index_BFile = bf_open (bfs, p->index_fname, 128, rw);
330 if (p->index_BFile == NULL)
332 logf (LOG_FATAL|LOG_ERRNO, "open %s", p->index_fname);
335 r = bf_read (p->index_BFile, 0, 0, 0, p->tmp_buf);
339 memcpy (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic));
340 sprintf (p->head.version, "%3d", REC_VERSION);
341 p->head.index_free = 0;
342 p->head.index_last = 1;
343 p->head.no_records = 0;
344 p->head.total_bytes = 0;
345 for (i = 0; i<REC_BLOCK_TYPES; i++)
347 p->head.block_free[i] = 0;
348 p->head.block_last[i] = 1;
349 p->head.block_used[i] = 0;
351 p->head.block_size[0] = 128;
352 p->head.block_move[0] = 0;
353 for (i = 1; i<REC_BLOCK_TYPES; i++)
355 p->head.block_size[i] = p->head.block_size[i-1] * 4;
356 p->head.block_move[i] = p->head.block_size[i] * 24;
362 memcpy (&p->head, p->tmp_buf, sizeof(p->head));
363 if (memcmp (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic)))
365 logf (LOG_FATAL, "file %s has bad format", p->index_fname);
368 version = atoi (p->head.version);
369 if (version != REC_VERSION)
371 logf (LOG_FATAL, "file %s is version %d, but version"
372 " %d is required", p->index_fname, version, REC_VERSION);
377 for (i = 0; i<REC_BLOCK_TYPES; i++)
380 sprintf (str, "recd%c", i + 'A');
381 p->data_fname[i] = (char *) xmalloc (strlen(str)+1);
382 strcpy (p->data_fname[i], str);
383 p->data_BFile[i] = NULL;
385 for (i = 0; i<REC_BLOCK_TYPES; i++)
387 if (!(p->data_BFile[i] = bf_open (bfs, p->data_fname[i],
388 p->head.block_size[i],
391 logf (LOG_FATAL|LOG_ERRNO, "bf_open %s", p->data_fname[i]);
397 p->record_cache = (struct record_cache_entry *)
398 xmalloc (sizeof(*p->record_cache)*p->cache_max);
402 static void rec_encode_unsigned (unsigned n, unsigned char *buf, int *len)
407 buf[*len] = 128 + (n & 127);
415 static void rec_decode_unsigned(unsigned *np, unsigned char *buf, int *len)
421 while (buf[*len] > 127)
423 n += w*(buf[*len] & 127);
432 static void rec_cache_flush_block1 (Records p, Record rec, Record last_rec,
433 char **out_buf, int *out_size,
439 for (i = 0; i<REC_NO_INFO; i++)
441 if (*out_offset + (int) rec->size[i] + 20 > *out_size)
443 int new_size = *out_offset + rec->size[i] + 65536;
444 char *np = (char *) xmalloc (new_size);
446 memcpy (np, *out_buf, *out_offset);
448 *out_size = new_size;
453 rec_encode_unsigned (rec->sysno, *out_buf + *out_offset, &len);
454 (*out_offset) += len;
456 if (rec->size[i] == 0)
458 rec_encode_unsigned (1, *out_buf + *out_offset, &len);
459 (*out_offset) += len;
461 else if (last_rec && rec->size[i] == last_rec->size[i] &&
462 !memcmp (rec->info[i], last_rec->info[i], rec->size[i]))
464 rec_encode_unsigned (0, *out_buf + *out_offset, &len);
465 (*out_offset) += len;
469 rec_encode_unsigned (rec->size[i]+1, *out_buf + *out_offset, &len);
470 (*out_offset) += len;
471 memcpy (*out_buf + *out_offset, rec->info[i], rec->size[i]);
472 (*out_offset) += rec->size[i];
477 static void rec_write_multiple (Records p, int saveCount)
481 char compression_method;
485 char *out_buf = (char *) xmalloc (out_size);
486 int *sysnos = (int *) xmalloc (sizeof(*sysnos) * (p->cache_cur + 1));
487 int *sysnop = sysnos;
489 for (i = 0; i<p->cache_cur - saveCount; i++)
491 struct record_cache_entry *e = p->record_cache + i;
495 rec_cache_flush_block1 (p, e->rec, last_rec, &out_buf,
496 &out_size, &out_offset);
497 *sysnop++ = e->rec->sysno;
499 e->flag = recordFlagNop;
502 case recordFlagWrite:
503 rec_release_blocks (p, e->rec->sysno);
504 rec_cache_flush_block1 (p, e->rec, last_rec, &out_buf,
505 &out_size, &out_offset);
506 *sysnop++ = e->rec->sysno;
508 e->flag = recordFlagNop;
511 case recordFlagDelete:
512 rec_delete_single (p, e->rec);
513 e->flag = recordFlagNop;
523 int csize = 0; /* indicate compression "not performed yet" */
524 compression_method = p->compression_method;
525 switch (compression_method)
527 case REC_COMPRESS_BZIP2:
529 csize = out_offset + (out_offset >> 6) + 620;
530 rec_tmp_expand (p, csize);
531 i = bzBuffToBuffCompress (p->tmp_buf+sizeof(int)+sizeof(short)+
533 &csize, out_buf, out_offset, 1, 0, 30);
536 logf (LOG_WARN, "bzBuffToBuffCompress error code=%d", i);
539 logf (LOG_LOG, "compress %4d %5d %5d", ref_count, out_offset,
543 case REC_COMPRESS_NONE:
548 /* either no compression or compression not supported ... */
550 rec_tmp_expand (p, csize);
551 memcpy (p->tmp_buf + sizeof(int) + sizeof(short) + sizeof(char),
552 out_buf, out_offset);
554 compression_method = REC_COMPRESS_NONE;
556 memcpy (p->tmp_buf + sizeof(int), &ref_count, sizeof(ref_count));
557 memcpy (p->tmp_buf + sizeof(int)+sizeof(short),
558 &compression_method, sizeof(compression_method));
560 /* -------- compression */
561 rec_write_tmp_buf (p, csize + sizeof(short) + sizeof(char), sysnos);
567 static void rec_cache_flush (Records p, int saveCount)
571 if (saveCount >= p->cache_cur)
574 rec_write_multiple (p, saveCount);
576 for (i = 0; i<p->cache_cur - saveCount; i++)
578 struct record_cache_entry *e = p->record_cache + i;
581 /* i still being used ... */
582 for (j = 0; j<saveCount; j++, i++)
583 memcpy (p->record_cache+j, p->record_cache+i,
584 sizeof(*p->record_cache));
585 p->cache_cur = saveCount;
588 static Record *rec_cache_lookup (Records p, int sysno,
589 enum recordCacheFlag flag)
592 for (i = 0; i<p->cache_cur; i++)
594 struct record_cache_entry *e = p->record_cache + i;
595 if (e->rec->sysno == sysno)
597 if (flag != recordFlagNop && e->flag == recordFlagNop)
605 static void rec_cache_insert (Records p, Record rec, enum recordCacheFlag flag)
607 struct record_cache_entry *e;
609 if (p->cache_cur == p->cache_max)
610 rec_cache_flush (p, 1);
611 else if (p->cache_cur > 0)
615 for (i = 0; i<p->cache_cur; i++)
617 Record r = (p->record_cache + i)->rec;
618 for (j = 0; j<REC_NO_INFO; j++)
622 rec_cache_flush (p, 1);
624 assert (p->cache_cur < p->cache_max);
626 e = p->record_cache + (p->cache_cur)++;
628 e->rec = rec_cp (rec);
631 void rec_close (Records *pp)
638 rec_cache_flush (p, 0);
639 xfree (p->record_cache);
645 bf_close (p->index_BFile);
647 for (i = 0; i<REC_BLOCK_TYPES; i++)
649 if (p->data_BFile[i])
650 bf_close (p->data_BFile[i]);
651 xfree (p->data_fname[i]);
659 Record rec_get (Records p, int sysno)
663 struct record_index_entry entry;
664 int freeblock, dst_type;
669 char compression_method;
674 if ((recp = rec_cache_lookup (p, sysno, recordFlagNop)))
675 return rec_cp (*recp);
677 if (read_indx (p, sysno, &entry, sizeof(entry), 1) < 1)
678 return NULL; /* record is not there! */
681 return NULL; /* record is deleted */
683 dst_type = entry.next & 7;
684 assert (dst_type < REC_BLOCK_TYPES);
685 freeblock = entry.next / 8;
687 assert (freeblock > 0);
689 rec_tmp_expand (p, entry.size);
692 r = bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
695 memcpy (&freeblock, cptr, sizeof(freeblock));
701 cptr += p->head.block_size[dst_type] - sizeof(freeblock);
703 memcpy (&tmp, cptr, sizeof(tmp));
704 r = bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
707 memcpy (&freeblock, cptr, sizeof(freeblock));
708 memcpy (cptr, &tmp, sizeof(tmp));
711 rec = (Record) xmalloc (sizeof(*rec));
713 memcpy (&compression_method, p->tmp_buf + sizeof(int) + sizeof(short),
714 sizeof(compression_method));
715 in_buf = p->tmp_buf + sizeof(int) + sizeof(short) + sizeof(char);
716 in_size = entry.size - sizeof(short) - sizeof(char);
717 switch (compression_method)
719 case REC_COMPRESS_BZIP2:
721 bz_size = entry.size * 20 + 100;
724 bz_buf = (char *) xmalloc (bz_size);
725 i = bzBuffToBuffDecompress (bz_buf, &bz_size, in_buf, in_size, 0, 0);
726 logf (LOG_LOG, "decompress %5d %5d", in_size, bz_size);
729 logf (LOG_LOG, "failed");
736 logf (LOG_FATAL, "cannot decompress record(s) in BZIP2 format");
740 case REC_COMPRESS_NONE:
743 for (i = 0; i<REC_NO_INFO; i++)
746 nptr = in_buf; /* skip ref count */
747 while (nptr < in_buf + in_size)
751 rec_decode_unsigned (&this_sysno, nptr, &len);
754 for (i = 0; i < REC_NO_INFO; i++)
757 rec_decode_unsigned (&this_size, nptr, &len);
762 rec->size[i] = this_size-1;
767 nptr += rec->size[i];
772 if (this_sysno == sysno)
775 for (i = 0; i<REC_NO_INFO; i++)
777 if (rec->info[i] && rec->size[i])
779 char *np = xmalloc (rec->size[i]);
780 memcpy (np, rec->info[i], rec->size[i]);
785 assert (rec->info[i] == 0);
786 assert (rec->size[i] == 0);
790 rec_cache_insert (p, rec, recordFlagNop);
794 Record rec_new (Records p)
800 rec = (Record) xmalloc (sizeof(*rec));
801 if (1 || p->head.index_free == 0)
802 sysno = (p->head.index_last)++;
805 struct record_index_entry entry;
807 read_indx (p, p->head.index_free, &entry, sizeof(entry), 0);
808 sysno = p->head.index_free;
809 p->head.index_free = entry.next;
815 logf (LOG_FATAL, "100,000 record limit reached");
819 (p->head.no_records)++;
821 for (i = 0; i < REC_NO_INFO; i++)
826 rec_cache_insert (p, rec, recordFlagNew);
830 void rec_del (Records p, Record *recpp)
834 (p->head.no_records)--;
835 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagDelete)))
842 rec_cache_insert (p, *recpp, recordFlagDelete);
848 void rec_put (Records p, Record *recpp)
852 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagWrite)))
859 rec_cache_insert (p, *recpp, recordFlagWrite);
865 void rec_rm (Record *recpp)
871 for (i = 0; i < REC_NO_INFO; i++)
872 xfree ((*recpp)->info[i]);
877 Record rec_cp (Record rec)
882 n = (Record) xmalloc (sizeof(*n));
883 n->sysno = rec->sysno;
884 for (i = 0; i < REC_NO_INFO; i++)
892 n->size[i] = rec->size[i];
893 n->info[i] = (char *) xmalloc (rec->size[i]);
894 memcpy (n->info[i], rec->info[i], rec->size[i]);
900 char *rec_strdup (const char *s, size_t *len)
910 p = (char *) xmalloc (*len);