2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.31 2001-02-26 22:14:59 adam
8 * Updated for BZIP2 1.0.X. Configure script doesn't enable 64 bit LFS
9 * on broken glibc on Redhat 7.0.
11 * Revision 1.30 2000/07/13 10:14:20 heikki
12 * Removed compiler warnings when making zebra
14 * Revision 1.29 2000/04/05 09:49:35 adam
15 * On Unix, zebra/z'mbol uses automake.
17 * Revision 1.28 1999/12/08 22:44:45 adam
18 * Zebra/Z'mbol dependencies added.
20 * Revision 1.27 1999/10/29 10:02:33 adam
21 * Fixed decompression buffer overflow.
23 * Revision 1.26 1999/07/06 13:34:57 adam
24 * Fixed bug (introduced by previous commit).
26 * Revision 1.25 1999/07/06 12:28:04 adam
27 * Updated record index structure. Format includes version ID. Compression
28 * algorithm ID is stored for each record block.
30 * Revision 1.24 1999/06/25 13:48:02 adam
31 * Updated MSVC project files.
32 * Added BZIP2 record compression (not very well tested).
34 * Revision 1.23 1999/05/26 07:49:13 adam
37 * Revision 1.22 1999/02/18 12:49:34 adam
38 * Changed file naming scheme for register files as well as record
41 * Revision 1.21 1999/02/02 14:51:03 adam
42 * Updated WIN32 code specific sections. Changed header.
44 * Revision 1.20 1998/01/12 15:04:08 adam
45 * The test option (-s) only uses read-lock (and not write lock).
47 * Revision 1.19 1997/09/17 12:19:16 adam
48 * Zebra version corresponds to YAZ version 1.4.
49 * Changed Zebra server so that it doesn't depend on global common_resource.
51 * Revision 1.18 1997/07/15 16:28:42 adam
52 * Bug fix: storeData didn't work with files with multiple records.
53 * Bug fix: fixed memory management with records; not really well
56 * Revision 1.17 1997/02/12 20:39:46 adam
57 * Implemented options -f <n> that limits the log to the first <n>
59 * Changed some log messages also.
61 * Revision 1.16 1996/06/04 10:19:00 adam
62 * Minor changes - removed include of ctype.h.
64 * Revision 1.15 1996/05/13 14:23:06 adam
65 * Work on compaction of set/use bytes in dictionary.
67 * Revision 1.14 1996/02/01 20:48:15 adam
68 * The total size of records are always checked in rec_cache_insert to
69 * reduce memory usage.
71 * Revision 1.13 1995/12/11 09:12:49 adam
72 * The rec_get function returns NULL if record doesn't exist - will
73 * happen in the server if the result set records have been deleted since
74 * the creation of the set (i.e. the search).
75 * The server saves a result temporarily if it is 'volatile', i.e. the
76 * set is register dependent.
78 * Revision 1.12 1995/12/07 17:38:47 adam
79 * Work locking mechanisms for concurrent updates/commit.
81 * Revision 1.11 1995/12/06 13:58:26 adam
82 * Improved flushing of records - all flushes except the last one
83 * don't write the last accessed. Also flush takes place if record
84 * info occupy more than about 256k.
86 * Revision 1.10 1995/12/06 12:41:24 adam
87 * New command 'stat' for the index program.
88 * Filenames can be read from stdin by specifying '-'.
89 * Bug fix/enhancement of the transformation from terms to regular
90 * expressons in the search engine.
92 * Revision 1.9 1995/11/30 08:34:33 adam
93 * Started work on commit facility.
94 * Changed a few malloc/free to xmalloc/xfree.
96 * Revision 1.8 1995/11/28 14:26:21 adam
97 * Bug fix: recordId with constant wasn't right.
98 * Bug fix: recordId dictionary entry wasn't deleted when needed.
100 * Revision 1.7 1995/11/28 09:09:43 adam
101 * Zebra config renamed.
102 * Use setting 'recordId' to identify record now.
103 * Bug fix in recindex.c: rec_release_blocks was invokeded even
104 * though the blocks were already released.
105 * File traversal properly deletes records when needed.
107 * Revision 1.6 1995/11/25 10:24:06 adam
108 * More record fields - they are enumerated now.
109 * New options: flagStoreData flagStoreKey.
111 * Revision 1.5 1995/11/22 17:19:18 adam
112 * Record management uses the bfile system.
114 * Revision 1.4 1995/11/20 16:59:46 adam
115 * New update method: the 'old' keys are saved for each records.
117 * Revision 1.3 1995/11/16 15:34:55 adam
118 * Uses new record management system in both indexer and server.
120 * Revision 1.2 1995/11/15 19:13:08 adam
121 * Work on record management.
123 * Revision 1.1 1995/11/15 14:46:20 adam
124 * Started work on better record management system.
130 * Format of first block
132 * ref_count (4 bytes)
135 * Format of subsequent blocks
139 * Format of each record
141 * (length, data) - pairs
142 * length = 0 if same as previous
148 #include "recindxp.h"
153 static void rec_write_head (Records p)
158 assert (p->index_BFile);
160 r = bf_write (p->index_BFile, 0, 0, sizeof(p->head), &p->head);
163 logf (LOG_FATAL|LOG_ERRNO, "write head of %s", p->index_fname);
168 static void rec_tmp_expand (Records p, int size)
170 if (p->tmp_size < size + 2048 ||
171 p->tmp_size < p->head.block_size[REC_BLOCK_TYPES-1]*2)
174 p->tmp_size = size + p->head.block_size[REC_BLOCK_TYPES-1]*2 + 2048;
175 p->tmp_buf = (char *) xmalloc (p->tmp_size);
179 static int read_indx (Records p, int sysno, void *buf, int itemsize,
183 int pos = (sysno-1)*itemsize;
185 r = bf_read (p->index_BFile, 1+pos/128, pos%128, itemsize, buf);
186 if (r != 1 && !ignoreError)
188 logf (LOG_FATAL|LOG_ERRNO, "read in %s at pos %ld",
189 p->index_fname, (long) pos);
195 static void write_indx (Records p, int sysno, void *buf, int itemsize)
197 int pos = (sysno-1)*itemsize;
199 bf_write (p->index_BFile, 1+pos/128, pos%128, itemsize, buf);
202 static void rec_release_blocks (Records p, int sysno)
204 struct record_index_entry entry;
206 char block_and_ref[sizeof(short) + sizeof(int)];
210 if (read_indx (p, sysno, &entry, sizeof(entry), 1) != 1)
213 freeblock = entry.next;
214 assert (freeblock > 0);
215 dst_type = freeblock & 7;
216 assert (dst_type < REC_BLOCK_TYPES);
217 freeblock = freeblock / 8;
220 if (bf_read (p->data_BFile[dst_type], freeblock, 0,
221 sizeof(block_and_ref), block_and_ref) != 1)
223 logf (LOG_FATAL|LOG_ERRNO, "read in rec_del_single");
229 memcpy (&ref, block_and_ref + sizeof(int), sizeof(ref));
231 memcpy (block_and_ref + sizeof(int), &ref, sizeof(ref));
234 if (bf_write (p->data_BFile[dst_type], freeblock, 0,
235 sizeof(block_and_ref), block_and_ref))
237 logf (LOG_FATAL|LOG_ERRNO, "write in rec_del_single");
245 if (bf_write (p->data_BFile[dst_type], freeblock, 0, sizeof(freeblock),
246 &p->head.block_free[dst_type]))
248 logf (LOG_FATAL|LOG_ERRNO, "write in rec_del_single");
251 p->head.block_free[dst_type] = freeblock;
252 memcpy (&freeblock, block_and_ref, sizeof(int));
254 p->head.block_used[dst_type]--;
256 p->head.total_bytes -= entry.size;
259 static void rec_delete_single (Records p, Record rec)
261 struct record_index_entry entry;
263 rec_release_blocks (p, rec->sysno);
265 entry.next = p->head.index_free;
267 p->head.index_free = rec->sysno;
268 write_indx (p, rec->sysno, &entry, sizeof(entry));
271 static void rec_write_tmp_buf (Records p, int size, int *sysnos)
273 struct record_index_entry entry;
275 char *cptr = p->tmp_buf;
276 int block_prev = -1, block_free;
280 for (i = 1; i<REC_BLOCK_TYPES; i++)
281 if (size >= p->head.block_move[i])
283 while (no_written < size)
285 block_free = p->head.block_free[dst_type];
288 if (bf_read (p->data_BFile[dst_type],
289 block_free, 0, sizeof(*p->head.block_free),
290 &p->head.block_free[dst_type]) != 1)
292 logf (LOG_FATAL|LOG_ERRNO, "read in %s at free block %d",
293 p->data_fname[dst_type], block_free);
298 block_free = p->head.block_last[dst_type]++;
299 if (block_prev == -1)
301 entry.next = block_free*8 + dst_type;
303 p->head.total_bytes += size;
306 write_indx (p, *sysnos, &entry, sizeof(entry));
312 memcpy (cptr, &block_free, sizeof(int));
313 bf_write (p->data_BFile[dst_type], block_prev, 0, 0, cptr);
314 cptr = p->tmp_buf + no_written;
316 block_prev = block_free;
317 no_written += p->head.block_size[dst_type] - sizeof(int);
318 p->head.block_used[dst_type]++;
320 assert (block_prev != -1);
322 memcpy (cptr, &block_free, sizeof(int));
323 bf_write (p->data_BFile[dst_type], block_prev, 0,
324 sizeof(int) + (p->tmp_buf+size) - cptr, cptr);
327 Records rec_open (BFiles bfs, int rw, int compression_method)
333 p = (Records) xmalloc (sizeof(*p));
334 p->compression_method = compression_method;
337 p->tmp_buf = (char *) xmalloc (p->tmp_size);
338 p->index_fname = "reci";
339 p->index_BFile = bf_open (bfs, p->index_fname, 128, rw);
340 if (p->index_BFile == NULL)
342 logf (LOG_FATAL|LOG_ERRNO, "open %s", p->index_fname);
345 r = bf_read (p->index_BFile, 0, 0, 0, p->tmp_buf);
349 memcpy (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic));
350 sprintf (p->head.version, "%3d", REC_VERSION);
351 p->head.index_free = 0;
352 p->head.index_last = 1;
353 p->head.no_records = 0;
354 p->head.total_bytes = 0;
355 for (i = 0; i<REC_BLOCK_TYPES; i++)
357 p->head.block_free[i] = 0;
358 p->head.block_last[i] = 1;
359 p->head.block_used[i] = 0;
361 p->head.block_size[0] = 128;
362 p->head.block_move[0] = 0;
363 for (i = 1; i<REC_BLOCK_TYPES; i++)
365 p->head.block_size[i] = p->head.block_size[i-1] * 4;
366 p->head.block_move[i] = p->head.block_size[i] * 24;
372 memcpy (&p->head, p->tmp_buf, sizeof(p->head));
373 if (memcmp (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic)))
375 logf (LOG_FATAL, "file %s has bad format", p->index_fname);
378 version = atoi (p->head.version);
379 if (version != REC_VERSION)
381 logf (LOG_FATAL, "file %s is version %d, but version"
382 " %d is required", p->index_fname, version, REC_VERSION);
387 for (i = 0; i<REC_BLOCK_TYPES; i++)
390 sprintf (str, "recd%c", i + 'A');
391 p->data_fname[i] = (char *) xmalloc (strlen(str)+1);
392 strcpy (p->data_fname[i], str);
393 p->data_BFile[i] = NULL;
395 for (i = 0; i<REC_BLOCK_TYPES; i++)
397 if (!(p->data_BFile[i] = bf_open (bfs, p->data_fname[i],
398 p->head.block_size[i],
401 logf (LOG_FATAL|LOG_ERRNO, "bf_open %s", p->data_fname[i]);
407 p->record_cache = (struct record_cache_entry *)
408 xmalloc (sizeof(*p->record_cache)*p->cache_max);
409 zebra_mutex_init (&p->mutex);
413 static void rec_encode_unsigned (unsigned n, unsigned char *buf, int *len)
418 buf[*len] = 128 + (n & 127);
426 static void rec_decode_unsigned(unsigned *np, unsigned char *buf, int *len)
432 while (buf[*len] > 127)
434 n += w*(buf[*len] & 127);
443 static void rec_cache_flush_block1 (Records p, Record rec, Record last_rec,
444 char **out_buf, int *out_size,
450 for (i = 0; i<REC_NO_INFO; i++)
452 if (*out_offset + (int) rec->size[i] + 20 > *out_size)
454 int new_size = *out_offset + rec->size[i] + 65536;
455 char *np = (char *) xmalloc (new_size);
457 memcpy (np, *out_buf, *out_offset);
459 *out_size = new_size;
464 rec_encode_unsigned (rec->sysno, *out_buf + *out_offset, &len);
465 (*out_offset) += len;
467 if (rec->size[i] == 0)
469 rec_encode_unsigned (1, *out_buf + *out_offset, &len);
470 (*out_offset) += len;
472 else if (last_rec && rec->size[i] == last_rec->size[i] &&
473 !memcmp (rec->info[i], last_rec->info[i], rec->size[i]))
475 rec_encode_unsigned (0, *out_buf + *out_offset, &len);
476 (*out_offset) += len;
480 rec_encode_unsigned (rec->size[i]+1, *out_buf + *out_offset, &len);
481 (*out_offset) += len;
482 memcpy (*out_buf + *out_offset, rec->info[i], rec->size[i]);
483 (*out_offset) += rec->size[i];
488 static void rec_write_multiple (Records p, int saveCount)
492 char compression_method;
496 char *out_buf = (char *) xmalloc (out_size);
497 int *sysnos = (int *) xmalloc (sizeof(*sysnos) * (p->cache_cur + 1));
498 int *sysnop = sysnos;
500 for (i = 0; i<p->cache_cur - saveCount; i++)
502 struct record_cache_entry *e = p->record_cache + i;
506 rec_cache_flush_block1 (p, e->rec, last_rec, &out_buf,
507 &out_size, &out_offset);
508 *sysnop++ = e->rec->sysno;
510 e->flag = recordFlagNop;
513 case recordFlagWrite:
514 rec_release_blocks (p, e->rec->sysno);
515 rec_cache_flush_block1 (p, e->rec, last_rec, &out_buf,
516 &out_size, &out_offset);
517 *sysnop++ = e->rec->sysno;
519 e->flag = recordFlagNop;
522 case recordFlagDelete:
523 rec_delete_single (p, e->rec);
524 e->flag = recordFlagNop;
534 int csize = 0; /* indicate compression "not performed yet" */
535 compression_method = p->compression_method;
536 switch (compression_method)
538 case REC_COMPRESS_BZIP2:
540 csize = out_offset + (out_offset >> 6) + 620;
541 rec_tmp_expand (p, csize);
542 #ifdef BZ_CONFIG_ERROR
543 i = BZ2_bzBuffToBuffCompress
545 i = bzBuffToBuffCompress
547 (p->tmp_buf+sizeof(int)+sizeof(short)+
549 &csize, out_buf, out_offset, 1, 0, 30);
552 logf (LOG_WARN, "bzBuffToBuffCompress error code=%d", i);
555 logf (LOG_LOG, "compress %4d %5d %5d", ref_count, out_offset,
559 case REC_COMPRESS_NONE:
564 /* either no compression or compression not supported ... */
566 rec_tmp_expand (p, csize);
567 memcpy (p->tmp_buf + sizeof(int) + sizeof(short) + sizeof(char),
568 out_buf, out_offset);
570 compression_method = REC_COMPRESS_NONE;
572 memcpy (p->tmp_buf + sizeof(int), &ref_count, sizeof(ref_count));
573 memcpy (p->tmp_buf + sizeof(int)+sizeof(short),
574 &compression_method, sizeof(compression_method));
576 /* -------- compression */
577 rec_write_tmp_buf (p, csize + sizeof(short) + sizeof(char), sysnos);
583 static void rec_cache_flush (Records p, int saveCount)
587 if (saveCount >= p->cache_cur)
590 rec_write_multiple (p, saveCount);
592 for (i = 0; i<p->cache_cur - saveCount; i++)
594 struct record_cache_entry *e = p->record_cache + i;
597 /* i still being used ... */
598 for (j = 0; j<saveCount; j++, i++)
599 memcpy (p->record_cache+j, p->record_cache+i,
600 sizeof(*p->record_cache));
601 p->cache_cur = saveCount;
604 static Record *rec_cache_lookup (Records p, int sysno,
605 enum recordCacheFlag flag)
608 for (i = 0; i<p->cache_cur; i++)
610 struct record_cache_entry *e = p->record_cache + i;
611 if (e->rec->sysno == sysno)
613 if (flag != recordFlagNop && e->flag == recordFlagNop)
621 static void rec_cache_insert (Records p, Record rec, enum recordCacheFlag flag)
623 struct record_cache_entry *e;
625 if (p->cache_cur == p->cache_max)
626 rec_cache_flush (p, 1);
627 else if (p->cache_cur > 0)
631 for (i = 0; i<p->cache_cur; i++)
633 Record r = (p->record_cache + i)->rec;
634 for (j = 0; j<REC_NO_INFO; j++)
638 rec_cache_flush (p, 1);
640 assert (p->cache_cur < p->cache_max);
642 e = p->record_cache + (p->cache_cur)++;
644 e->rec = rec_cp (rec);
647 void rec_close (Records *pp)
654 zebra_mutex_destroy (&p->mutex);
655 rec_cache_flush (p, 0);
656 xfree (p->record_cache);
662 bf_close (p->index_BFile);
664 for (i = 0; i<REC_BLOCK_TYPES; i++)
666 if (p->data_BFile[i])
667 bf_close (p->data_BFile[i]);
668 xfree (p->data_fname[i]);
675 static Record rec_get_int (Records p, int sysno)
679 struct record_index_entry entry;
680 int freeblock, dst_type;
687 char compression_method;
692 if ((recp = rec_cache_lookup (p, sysno, recordFlagNop)))
693 return rec_cp (*recp);
695 if (read_indx (p, sysno, &entry, sizeof(entry), 1) < 1)
696 return NULL; /* record is not there! */
699 return NULL; /* record is deleted */
701 dst_type = entry.next & 7;
702 assert (dst_type < REC_BLOCK_TYPES);
703 freeblock = entry.next / 8;
705 assert (freeblock > 0);
707 rec_tmp_expand (p, entry.size);
710 r = bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
713 memcpy (&freeblock, cptr, sizeof(freeblock));
719 cptr += p->head.block_size[dst_type] - sizeof(freeblock);
721 memcpy (&tmp, cptr, sizeof(tmp));
722 r = bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
725 memcpy (&freeblock, cptr, sizeof(freeblock));
726 memcpy (cptr, &tmp, sizeof(tmp));
729 rec = (Record) xmalloc (sizeof(*rec));
731 memcpy (&compression_method, p->tmp_buf + sizeof(int) + sizeof(short),
732 sizeof(compression_method));
733 in_buf = p->tmp_buf + sizeof(int) + sizeof(short) + sizeof(char);
734 in_size = entry.size - sizeof(short) - sizeof(char);
735 switch (compression_method)
737 case REC_COMPRESS_BZIP2:
739 bz_size = entry.size * 20 + 100;
742 bz_buf = (char *) xmalloc (bz_size);
743 #ifdef BZ_CONFIG_ERROR
744 i = BZ2_bzBuffToBuffDecompress
746 i = bzBuffToBuffDecompress
748 (bz_buf, &bz_size, in_buf, in_size, 0, 0);
749 logf (LOG_LOG, "decompress %5d %5d", in_size, bz_size);
752 logf (LOG_LOG, "failed");
759 logf (LOG_FATAL, "cannot decompress record(s) in BZIP2 format");
763 case REC_COMPRESS_NONE:
766 for (i = 0; i<REC_NO_INFO; i++)
769 nptr = in_buf; /* skip ref count */
770 while (nptr < in_buf + in_size)
774 rec_decode_unsigned (&this_sysno, nptr, &len);
777 for (i = 0; i < REC_NO_INFO; i++)
780 rec_decode_unsigned (&this_size, nptr, &len);
785 rec->size[i] = this_size-1;
790 nptr += rec->size[i];
795 if (this_sysno == sysno)
798 for (i = 0; i<REC_NO_INFO; i++)
800 if (rec->info[i] && rec->size[i])
802 char *np = xmalloc (rec->size[i]);
803 memcpy (np, rec->info[i], rec->size[i]);
808 assert (rec->info[i] == 0);
809 assert (rec->size[i] == 0);
813 rec_cache_insert (p, rec, recordFlagNop);
817 Record rec_get (Records p, int sysno)
820 zebra_mutex_lock (&p->mutex);
822 rec = rec_get_int (p, sysno);
823 zebra_mutex_unlock (&p->mutex);
827 static Record rec_new_int (Records p)
833 rec = (Record) xmalloc (sizeof(*rec));
834 if (1 || p->head.index_free == 0)
835 sysno = (p->head.index_last)++;
838 struct record_index_entry entry;
840 read_indx (p, p->head.index_free, &entry, sizeof(entry), 0);
841 sysno = p->head.index_free;
842 p->head.index_free = entry.next;
848 logf (LOG_FATAL, "100,000 record limit reached");
852 (p->head.no_records)++;
854 for (i = 0; i < REC_NO_INFO; i++)
859 rec_cache_insert (p, rec, recordFlagNew);
863 Record rec_new (Records p)
866 zebra_mutex_lock (&p->mutex);
868 rec = rec_new_int (p);
869 zebra_mutex_unlock (&p->mutex);
873 void rec_del (Records p, Record *recpp)
877 zebra_mutex_lock (&p->mutex);
878 (p->head.no_records)--;
879 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagDelete)))
886 rec_cache_insert (p, *recpp, recordFlagDelete);
889 zebra_mutex_unlock (&p->mutex);
893 void rec_put (Records p, Record *recpp)
897 zebra_mutex_lock (&p->mutex);
898 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagWrite)))
905 rec_cache_insert (p, *recpp, recordFlagWrite);
908 zebra_mutex_unlock (&p->mutex);
912 void rec_rm (Record *recpp)
918 for (i = 0; i < REC_NO_INFO; i++)
919 xfree ((*recpp)->info[i]);
924 Record rec_cp (Record rec)
929 n = (Record) xmalloc (sizeof(*n));
930 n->sysno = rec->sysno;
931 for (i = 0; i < REC_NO_INFO; i++)
939 n->size[i] = rec->size[i];
940 n->info[i] = (char *) xmalloc (rec->size[i]);
941 memcpy (n->info[i], rec->info[i], rec->size[i]);
947 char *rec_strdup (const char *s, size_t *len)
957 p = (char *) xmalloc (*len);