2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.32 2002-04-05 08:46:26 adam
8 * Zebra with full functionality
10 * Revision 1.31 2001/02/26 22:14:59 adam
11 * Updated for BZIP2 1.0.X. Configure script doesn't enable 64 bit LFS
12 * on broken glibc on Redhat 7.0.
14 * Revision 1.30 2000/07/13 10:14:20 heikki
15 * Removed compiler warnings when making zebra
17 * Revision 1.29 2000/04/05 09:49:35 adam
18 * On Unix, zebra/z'mbol uses automake.
20 * Revision 1.28 1999/12/08 22:44:45 adam
21 * Zebra/Z'mbol dependencies added.
23 * Revision 1.27 1999/10/29 10:02:33 adam
24 * Fixed decompression buffer overflow.
26 * Revision 1.26 1999/07/06 13:34:57 adam
27 * Fixed bug (introduced by previous commit).
29 * Revision 1.25 1999/07/06 12:28:04 adam
30 * Updated record index structure. Format includes version ID. Compression
31 * algorithm ID is stored for each record block.
33 * Revision 1.24 1999/06/25 13:48:02 adam
34 * Updated MSVC project files.
35 * Added BZIP2 record compression (not very well tested).
37 * Revision 1.23 1999/05/26 07:49:13 adam
40 * Revision 1.22 1999/02/18 12:49:34 adam
41 * Changed file naming scheme for register files as well as record
44 * Revision 1.21 1999/02/02 14:51:03 adam
45 * Updated WIN32 code specific sections. Changed header.
47 * Revision 1.20 1998/01/12 15:04:08 adam
48 * The test option (-s) only uses read-lock (and not write lock).
50 * Revision 1.19 1997/09/17 12:19:16 adam
51 * Zebra version corresponds to YAZ version 1.4.
52 * Changed Zebra server so that it doesn't depend on global common_resource.
54 * Revision 1.18 1997/07/15 16:28:42 adam
55 * Bug fix: storeData didn't work with files with multiple records.
56 * Bug fix: fixed memory management with records; not really well
59 * Revision 1.17 1997/02/12 20:39:46 adam
60 * Implemented options -f <n> that limits the log to the first <n>
62 * Changed some log messages also.
64 * Revision 1.16 1996/06/04 10:19:00 adam
65 * Minor changes - removed include of ctype.h.
67 * Revision 1.15 1996/05/13 14:23:06 adam
68 * Work on compaction of set/use bytes in dictionary.
70 * Revision 1.14 1996/02/01 20:48:15 adam
71 * The total size of records are always checked in rec_cache_insert to
72 * reduce memory usage.
74 * Revision 1.13 1995/12/11 09:12:49 adam
75 * The rec_get function returns NULL if record doesn't exist - will
76 * happen in the server if the result set records have been deleted since
77 * the creation of the set (i.e. the search).
78 * The server saves a result temporarily if it is 'volatile', i.e. the
79 * set is register dependent.
81 * Revision 1.12 1995/12/07 17:38:47 adam
82 * Work locking mechanisms for concurrent updates/commit.
84 * Revision 1.11 1995/12/06 13:58:26 adam
85 * Improved flushing of records - all flushes except the last one
86 * don't write the last accessed. Also flush takes place if record
87 * info occupy more than about 256k.
89 * Revision 1.10 1995/12/06 12:41:24 adam
90 * New command 'stat' for the index program.
91 * Filenames can be read from stdin by specifying '-'.
92 * Bug fix/enhancement of the transformation from terms to regular
93 * expressons in the search engine.
95 * Revision 1.9 1995/11/30 08:34:33 adam
96 * Started work on commit facility.
97 * Changed a few malloc/free to xmalloc/xfree.
99 * Revision 1.8 1995/11/28 14:26:21 adam
100 * Bug fix: recordId with constant wasn't right.
101 * Bug fix: recordId dictionary entry wasn't deleted when needed.
103 * Revision 1.7 1995/11/28 09:09:43 adam
104 * Zebra config renamed.
105 * Use setting 'recordId' to identify record now.
106 * Bug fix in recindex.c: rec_release_blocks was invokeded even
107 * though the blocks were already released.
108 * File traversal properly deletes records when needed.
110 * Revision 1.6 1995/11/25 10:24:06 adam
111 * More record fields - they are enumerated now.
112 * New options: flagStoreData flagStoreKey.
114 * Revision 1.5 1995/11/22 17:19:18 adam
115 * Record management uses the bfile system.
117 * Revision 1.4 1995/11/20 16:59:46 adam
118 * New update method: the 'old' keys are saved for each records.
120 * Revision 1.3 1995/11/16 15:34:55 adam
121 * Uses new record management system in both indexer and server.
123 * Revision 1.2 1995/11/15 19:13:08 adam
124 * Work on record management.
126 * Revision 1.1 1995/11/15 14:46:20 adam
127 * Started work on better record management system.
133 * Format of first block
135 * ref_count (4 bytes)
138 * Format of subsequent blocks
142 * Format of each record
144 * (length, data) - pairs
145 * length = 0 if same as previous
151 #include "recindxp.h"
156 static void rec_write_head (Records p)
161 assert (p->index_BFile);
163 r = bf_write (p->index_BFile, 0, 0, sizeof(p->head), &p->head);
166 logf (LOG_FATAL|LOG_ERRNO, "write head of %s", p->index_fname);
171 static void rec_tmp_expand (Records p, int size)
173 if (p->tmp_size < size + 2048 ||
174 p->tmp_size < p->head.block_size[REC_BLOCK_TYPES-1]*2)
177 p->tmp_size = size + p->head.block_size[REC_BLOCK_TYPES-1]*2 + 2048;
178 p->tmp_buf = (char *) xmalloc (p->tmp_size);
182 static int read_indx (Records p, int sysno, void *buf, int itemsize,
186 int pos = (sysno-1)*itemsize;
188 r = bf_read (p->index_BFile, 1+pos/128, pos%128, itemsize, buf);
189 if (r != 1 && !ignoreError)
191 logf (LOG_FATAL|LOG_ERRNO, "read in %s at pos %ld",
192 p->index_fname, (long) pos);
198 static void write_indx (Records p, int sysno, void *buf, int itemsize)
200 int pos = (sysno-1)*itemsize;
202 bf_write (p->index_BFile, 1+pos/128, pos%128, itemsize, buf);
205 static void rec_release_blocks (Records p, int sysno)
207 struct record_index_entry entry;
209 char block_and_ref[sizeof(short) + sizeof(int)];
213 if (read_indx (p, sysno, &entry, sizeof(entry), 1) != 1)
216 freeblock = entry.next;
217 assert (freeblock > 0);
218 dst_type = freeblock & 7;
219 assert (dst_type < REC_BLOCK_TYPES);
220 freeblock = freeblock / 8;
223 if (bf_read (p->data_BFile[dst_type], freeblock, 0,
224 sizeof(block_and_ref), block_and_ref) != 1)
226 logf (LOG_FATAL|LOG_ERRNO, "read in rec_del_single");
232 memcpy (&ref, block_and_ref + sizeof(int), sizeof(ref));
234 memcpy (block_and_ref + sizeof(int), &ref, sizeof(ref));
237 if (bf_write (p->data_BFile[dst_type], freeblock, 0,
238 sizeof(block_and_ref), block_and_ref))
240 logf (LOG_FATAL|LOG_ERRNO, "write in rec_del_single");
248 if (bf_write (p->data_BFile[dst_type], freeblock, 0, sizeof(freeblock),
249 &p->head.block_free[dst_type]))
251 logf (LOG_FATAL|LOG_ERRNO, "write in rec_del_single");
254 p->head.block_free[dst_type] = freeblock;
255 memcpy (&freeblock, block_and_ref, sizeof(int));
257 p->head.block_used[dst_type]--;
259 p->head.total_bytes -= entry.size;
262 static void rec_delete_single (Records p, Record rec)
264 struct record_index_entry entry;
266 rec_release_blocks (p, rec->sysno);
268 entry.next = p->head.index_free;
270 p->head.index_free = rec->sysno;
271 write_indx (p, rec->sysno, &entry, sizeof(entry));
274 static void rec_write_tmp_buf (Records p, int size, int *sysnos)
276 struct record_index_entry entry;
278 char *cptr = p->tmp_buf;
279 int block_prev = -1, block_free;
283 for (i = 1; i<REC_BLOCK_TYPES; i++)
284 if (size >= p->head.block_move[i])
286 while (no_written < size)
288 block_free = p->head.block_free[dst_type];
291 if (bf_read (p->data_BFile[dst_type],
292 block_free, 0, sizeof(*p->head.block_free),
293 &p->head.block_free[dst_type]) != 1)
295 logf (LOG_FATAL|LOG_ERRNO, "read in %s at free block %d",
296 p->data_fname[dst_type], block_free);
301 block_free = p->head.block_last[dst_type]++;
302 if (block_prev == -1)
304 entry.next = block_free*8 + dst_type;
306 p->head.total_bytes += size;
309 write_indx (p, *sysnos, &entry, sizeof(entry));
315 memcpy (cptr, &block_free, sizeof(int));
316 bf_write (p->data_BFile[dst_type], block_prev, 0, 0, cptr);
317 cptr = p->tmp_buf + no_written;
319 block_prev = block_free;
320 no_written += p->head.block_size[dst_type] - sizeof(int);
321 p->head.block_used[dst_type]++;
323 assert (block_prev != -1);
325 memcpy (cptr, &block_free, sizeof(int));
326 bf_write (p->data_BFile[dst_type], block_prev, 0,
327 sizeof(int) + (p->tmp_buf+size) - cptr, cptr);
330 Records rec_open (BFiles bfs, int rw, int compression_method)
336 p = (Records) xmalloc (sizeof(*p));
337 p->compression_method = compression_method;
340 p->tmp_buf = (char *) xmalloc (p->tmp_size);
341 p->index_fname = "reci";
342 p->index_BFile = bf_open (bfs, p->index_fname, 128, rw);
343 if (p->index_BFile == NULL)
345 logf (LOG_FATAL|LOG_ERRNO, "open %s", p->index_fname);
348 r = bf_read (p->index_BFile, 0, 0, 0, p->tmp_buf);
352 memcpy (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic));
353 sprintf (p->head.version, "%3d", REC_VERSION);
354 p->head.index_free = 0;
355 p->head.index_last = 1;
356 p->head.no_records = 0;
357 p->head.total_bytes = 0;
358 for (i = 0; i<REC_BLOCK_TYPES; i++)
360 p->head.block_free[i] = 0;
361 p->head.block_last[i] = 1;
362 p->head.block_used[i] = 0;
364 p->head.block_size[0] = 128;
365 p->head.block_move[0] = 0;
366 for (i = 1; i<REC_BLOCK_TYPES; i++)
368 p->head.block_size[i] = p->head.block_size[i-1] * 4;
369 p->head.block_move[i] = p->head.block_size[i] * 24;
375 memcpy (&p->head, p->tmp_buf, sizeof(p->head));
376 if (memcmp (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic)))
378 logf (LOG_FATAL, "file %s has bad format", p->index_fname);
381 version = atoi (p->head.version);
382 if (version != REC_VERSION)
384 logf (LOG_FATAL, "file %s is version %d, but version"
385 " %d is required", p->index_fname, version, REC_VERSION);
390 for (i = 0; i<REC_BLOCK_TYPES; i++)
393 sprintf (str, "recd%c", i + 'A');
394 p->data_fname[i] = (char *) xmalloc (strlen(str)+1);
395 strcpy (p->data_fname[i], str);
396 p->data_BFile[i] = NULL;
398 for (i = 0; i<REC_BLOCK_TYPES; i++)
400 if (!(p->data_BFile[i] = bf_open (bfs, p->data_fname[i],
401 p->head.block_size[i],
404 logf (LOG_FATAL|LOG_ERRNO, "bf_open %s", p->data_fname[i]);
410 p->record_cache = (struct record_cache_entry *)
411 xmalloc (sizeof(*p->record_cache)*p->cache_max);
412 zebra_mutex_init (&p->mutex);
416 static void rec_encode_unsigned (unsigned n, unsigned char *buf, int *len)
421 buf[*len] = 128 + (n & 127);
429 static void rec_decode_unsigned(unsigned *np, unsigned char *buf, int *len)
435 while (buf[*len] > 127)
437 n += w*(buf[*len] & 127);
446 static void rec_cache_flush_block1 (Records p, Record rec, Record last_rec,
447 char **out_buf, int *out_size,
453 for (i = 0; i<REC_NO_INFO; i++)
455 if (*out_offset + (int) rec->size[i] + 20 > *out_size)
457 int new_size = *out_offset + rec->size[i] + 65536;
458 char *np = (char *) xmalloc (new_size);
460 memcpy (np, *out_buf, *out_offset);
462 *out_size = new_size;
467 rec_encode_unsigned (rec->sysno, *out_buf + *out_offset, &len);
468 (*out_offset) += len;
470 if (rec->size[i] == 0)
472 rec_encode_unsigned (1, *out_buf + *out_offset, &len);
473 (*out_offset) += len;
475 else if (last_rec && rec->size[i] == last_rec->size[i] &&
476 !memcmp (rec->info[i], last_rec->info[i], rec->size[i]))
478 rec_encode_unsigned (0, *out_buf + *out_offset, &len);
479 (*out_offset) += len;
483 rec_encode_unsigned (rec->size[i]+1, *out_buf + *out_offset, &len);
484 (*out_offset) += len;
485 memcpy (*out_buf + *out_offset, rec->info[i], rec->size[i]);
486 (*out_offset) += rec->size[i];
491 static void rec_write_multiple (Records p, int saveCount)
495 char compression_method;
499 char *out_buf = (char *) xmalloc (out_size);
500 int *sysnos = (int *) xmalloc (sizeof(*sysnos) * (p->cache_cur + 1));
501 int *sysnop = sysnos;
503 for (i = 0; i<p->cache_cur - saveCount; i++)
505 struct record_cache_entry *e = p->record_cache + i;
509 rec_cache_flush_block1 (p, e->rec, last_rec, &out_buf,
510 &out_size, &out_offset);
511 *sysnop++ = e->rec->sysno;
513 e->flag = recordFlagNop;
516 case recordFlagWrite:
517 rec_release_blocks (p, e->rec->sysno);
518 rec_cache_flush_block1 (p, e->rec, last_rec, &out_buf,
519 &out_size, &out_offset);
520 *sysnop++ = e->rec->sysno;
522 e->flag = recordFlagNop;
525 case recordFlagDelete:
526 rec_delete_single (p, e->rec);
527 e->flag = recordFlagNop;
537 int csize = 0; /* indicate compression "not performed yet" */
538 compression_method = p->compression_method;
539 switch (compression_method)
541 case REC_COMPRESS_BZIP2:
543 csize = out_offset + (out_offset >> 6) + 620;
544 rec_tmp_expand (p, csize);
545 #ifdef BZ_CONFIG_ERROR
546 i = BZ2_bzBuffToBuffCompress
548 i = bzBuffToBuffCompress
550 (p->tmp_buf+sizeof(int)+sizeof(short)+
552 &csize, out_buf, out_offset, 1, 0, 30);
555 logf (LOG_WARN, "bzBuffToBuffCompress error code=%d", i);
558 logf (LOG_LOG, "compress %4d %5d %5d", ref_count, out_offset,
562 case REC_COMPRESS_NONE:
567 /* either no compression or compression not supported ... */
569 rec_tmp_expand (p, csize);
570 memcpy (p->tmp_buf + sizeof(int) + sizeof(short) + sizeof(char),
571 out_buf, out_offset);
573 compression_method = REC_COMPRESS_NONE;
575 memcpy (p->tmp_buf + sizeof(int), &ref_count, sizeof(ref_count));
576 memcpy (p->tmp_buf + sizeof(int)+sizeof(short),
577 &compression_method, sizeof(compression_method));
579 /* -------- compression */
580 rec_write_tmp_buf (p, csize + sizeof(short) + sizeof(char), sysnos);
586 static void rec_cache_flush (Records p, int saveCount)
590 if (saveCount >= p->cache_cur)
593 rec_write_multiple (p, saveCount);
595 for (i = 0; i<p->cache_cur - saveCount; i++)
597 struct record_cache_entry *e = p->record_cache + i;
600 /* i still being used ... */
601 for (j = 0; j<saveCount; j++, i++)
602 memcpy (p->record_cache+j, p->record_cache+i,
603 sizeof(*p->record_cache));
604 p->cache_cur = saveCount;
607 static Record *rec_cache_lookup (Records p, int sysno,
608 enum recordCacheFlag flag)
611 for (i = 0; i<p->cache_cur; i++)
613 struct record_cache_entry *e = p->record_cache + i;
614 if (e->rec->sysno == sysno)
616 if (flag != recordFlagNop && e->flag == recordFlagNop)
624 static void rec_cache_insert (Records p, Record rec, enum recordCacheFlag flag)
626 struct record_cache_entry *e;
628 if (p->cache_cur == p->cache_max)
629 rec_cache_flush (p, 1);
630 else if (p->cache_cur > 0)
634 for (i = 0; i<p->cache_cur; i++)
636 Record r = (p->record_cache + i)->rec;
637 for (j = 0; j<REC_NO_INFO; j++)
641 rec_cache_flush (p, 1);
643 assert (p->cache_cur < p->cache_max);
645 e = p->record_cache + (p->cache_cur)++;
647 e->rec = rec_cp (rec);
650 void rec_close (Records *pp)
657 zebra_mutex_destroy (&p->mutex);
658 rec_cache_flush (p, 0);
659 xfree (p->record_cache);
665 bf_close (p->index_BFile);
667 for (i = 0; i<REC_BLOCK_TYPES; i++)
669 if (p->data_BFile[i])
670 bf_close (p->data_BFile[i]);
671 xfree (p->data_fname[i]);
678 static Record rec_get_int (Records p, int sysno)
682 struct record_index_entry entry;
683 int freeblock, dst_type;
690 char compression_method;
695 if ((recp = rec_cache_lookup (p, sysno, recordFlagNop)))
696 return rec_cp (*recp);
698 if (read_indx (p, sysno, &entry, sizeof(entry), 1) < 1)
699 return NULL; /* record is not there! */
702 return NULL; /* record is deleted */
704 dst_type = entry.next & 7;
705 assert (dst_type < REC_BLOCK_TYPES);
706 freeblock = entry.next / 8;
708 assert (freeblock > 0);
710 rec_tmp_expand (p, entry.size);
713 r = bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
716 memcpy (&freeblock, cptr, sizeof(freeblock));
722 cptr += p->head.block_size[dst_type] - sizeof(freeblock);
724 memcpy (&tmp, cptr, sizeof(tmp));
725 r = bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
728 memcpy (&freeblock, cptr, sizeof(freeblock));
729 memcpy (cptr, &tmp, sizeof(tmp));
732 rec = (Record) xmalloc (sizeof(*rec));
734 memcpy (&compression_method, p->tmp_buf + sizeof(int) + sizeof(short),
735 sizeof(compression_method));
736 in_buf = p->tmp_buf + sizeof(int) + sizeof(short) + sizeof(char);
737 in_size = entry.size - sizeof(short) - sizeof(char);
738 switch (compression_method)
740 case REC_COMPRESS_BZIP2:
742 bz_size = entry.size * 20 + 100;
745 bz_buf = (char *) xmalloc (bz_size);
746 #ifdef BZ_CONFIG_ERROR
747 i = BZ2_bzBuffToBuffDecompress
749 i = bzBuffToBuffDecompress
751 (bz_buf, &bz_size, in_buf, in_size, 0, 0);
752 logf (LOG_LOG, "decompress %5d %5d", in_size, bz_size);
755 logf (LOG_LOG, "failed");
762 logf (LOG_FATAL, "cannot decompress record(s) in BZIP2 format");
766 case REC_COMPRESS_NONE:
769 for (i = 0; i<REC_NO_INFO; i++)
772 nptr = in_buf; /* skip ref count */
773 while (nptr < in_buf + in_size)
777 rec_decode_unsigned (&this_sysno, nptr, &len);
780 for (i = 0; i < REC_NO_INFO; i++)
783 rec_decode_unsigned (&this_size, nptr, &len);
788 rec->size[i] = this_size-1;
793 nptr += rec->size[i];
798 if (this_sysno == sysno)
801 for (i = 0; i<REC_NO_INFO; i++)
803 if (rec->info[i] && rec->size[i])
805 char *np = xmalloc (rec->size[i]);
806 memcpy (np, rec->info[i], rec->size[i]);
811 assert (rec->info[i] == 0);
812 assert (rec->size[i] == 0);
816 rec_cache_insert (p, rec, recordFlagNop);
820 Record rec_get (Records p, int sysno)
823 zebra_mutex_lock (&p->mutex);
825 rec = rec_get_int (p, sysno);
826 zebra_mutex_unlock (&p->mutex);
830 static Record rec_new_int (Records p)
836 rec = (Record) xmalloc (sizeof(*rec));
837 if (1 || p->head.index_free == 0)
838 sysno = (p->head.index_last)++;
841 struct record_index_entry entry;
843 read_indx (p, p->head.index_free, &entry, sizeof(entry), 0);
844 sysno = p->head.index_free;
845 p->head.index_free = entry.next;
847 (p->head.no_records)++;
849 for (i = 0; i < REC_NO_INFO; i++)
854 rec_cache_insert (p, rec, recordFlagNew);
858 Record rec_new (Records p)
861 zebra_mutex_lock (&p->mutex);
863 rec = rec_new_int (p);
864 zebra_mutex_unlock (&p->mutex);
868 void rec_del (Records p, Record *recpp)
872 zebra_mutex_lock (&p->mutex);
873 (p->head.no_records)--;
874 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagDelete)))
881 rec_cache_insert (p, *recpp, recordFlagDelete);
884 zebra_mutex_unlock (&p->mutex);
888 void rec_put (Records p, Record *recpp)
892 zebra_mutex_lock (&p->mutex);
893 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagWrite)))
900 rec_cache_insert (p, *recpp, recordFlagWrite);
903 zebra_mutex_unlock (&p->mutex);
907 void rec_rm (Record *recpp)
913 for (i = 0; i < REC_NO_INFO; i++)
914 xfree ((*recpp)->info[i]);
919 Record rec_cp (Record rec)
924 n = (Record) xmalloc (sizeof(*n));
925 n->sysno = rec->sysno;
926 for (i = 0; i < REC_NO_INFO; i++)
934 n->size[i] = rec->size[i];
935 n->info[i] = (char *) xmalloc (rec->size[i]);
936 memcpy (n->info[i], rec->info[i], rec->size[i]);
942 char *rec_strdup (const char *s, size_t *len)
952 p = (char *) xmalloc (*len);