2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.30 2000-07-13 10:14:20 heikki
8 * Removed compiler warnings when making zebra
10 * Revision 1.29 2000/04/05 09:49:35 adam
11 * On Unix, zebra/z'mbol uses automake.
13 * Revision 1.28 1999/12/08 22:44:45 adam
14 * Zebra/Z'mbol dependencies added.
16 * Revision 1.27 1999/10/29 10:02:33 adam
17 * Fixed decompression buffer overflow.
19 * Revision 1.26 1999/07/06 13:34:57 adam
20 * Fixed bug (introduced by previous commit).
22 * Revision 1.25 1999/07/06 12:28:04 adam
23 * Updated record index structure. Format includes version ID. Compression
24 * algorithm ID is stored for each record block.
26 * Revision 1.24 1999/06/25 13:48:02 adam
27 * Updated MSVC project files.
28 * Added BZIP2 record compression (not very well tested).
30 * Revision 1.23 1999/05/26 07:49:13 adam
33 * Revision 1.22 1999/02/18 12:49:34 adam
34 * Changed file naming scheme for register files as well as record
37 * Revision 1.21 1999/02/02 14:51:03 adam
38 * Updated WIN32 code specific sections. Changed header.
40 * Revision 1.20 1998/01/12 15:04:08 adam
41 * The test option (-s) only uses read-lock (and not write lock).
43 * Revision 1.19 1997/09/17 12:19:16 adam
44 * Zebra version corresponds to YAZ version 1.4.
45 * Changed Zebra server so that it doesn't depend on global common_resource.
47 * Revision 1.18 1997/07/15 16:28:42 adam
48 * Bug fix: storeData didn't work with files with multiple records.
49 * Bug fix: fixed memory management with records; not really well
52 * Revision 1.17 1997/02/12 20:39:46 adam
53 * Implemented options -f <n> that limits the log to the first <n>
55 * Changed some log messages also.
57 * Revision 1.16 1996/06/04 10:19:00 adam
58 * Minor changes - removed include of ctype.h.
60 * Revision 1.15 1996/05/13 14:23:06 adam
61 * Work on compaction of set/use bytes in dictionary.
63 * Revision 1.14 1996/02/01 20:48:15 adam
64 * The total size of records are always checked in rec_cache_insert to
65 * reduce memory usage.
67 * Revision 1.13 1995/12/11 09:12:49 adam
68 * The rec_get function returns NULL if record doesn't exist - will
69 * happen in the server if the result set records have been deleted since
70 * the creation of the set (i.e. the search).
71 * The server saves a result temporarily if it is 'volatile', i.e. the
72 * set is register dependent.
74 * Revision 1.12 1995/12/07 17:38:47 adam
75 * Work locking mechanisms for concurrent updates/commit.
77 * Revision 1.11 1995/12/06 13:58:26 adam
78 * Improved flushing of records - all flushes except the last one
79 * don't write the last accessed. Also flush takes place if record
80 * info occupy more than about 256k.
82 * Revision 1.10 1995/12/06 12:41:24 adam
83 * New command 'stat' for the index program.
84 * Filenames can be read from stdin by specifying '-'.
85 * Bug fix/enhancement of the transformation from terms to regular
86 * expressons in the search engine.
88 * Revision 1.9 1995/11/30 08:34:33 adam
89 * Started work on commit facility.
90 * Changed a few malloc/free to xmalloc/xfree.
92 * Revision 1.8 1995/11/28 14:26:21 adam
93 * Bug fix: recordId with constant wasn't right.
94 * Bug fix: recordId dictionary entry wasn't deleted when needed.
96 * Revision 1.7 1995/11/28 09:09:43 adam
97 * Zebra config renamed.
98 * Use setting 'recordId' to identify record now.
99 * Bug fix in recindex.c: rec_release_blocks was invokeded even
100 * though the blocks were already released.
101 * File traversal properly deletes records when needed.
103 * Revision 1.6 1995/11/25 10:24:06 adam
104 * More record fields - they are enumerated now.
105 * New options: flagStoreData flagStoreKey.
107 * Revision 1.5 1995/11/22 17:19:18 adam
108 * Record management uses the bfile system.
110 * Revision 1.4 1995/11/20 16:59:46 adam
111 * New update method: the 'old' keys are saved for each records.
113 * Revision 1.3 1995/11/16 15:34:55 adam
114 * Uses new record management system in both indexer and server.
116 * Revision 1.2 1995/11/15 19:13:08 adam
117 * Work on record management.
119 * Revision 1.1 1995/11/15 14:46:20 adam
120 * Started work on better record management system.
126 * Format of first block
128 * ref_count (4 bytes)
131 * Format of subsequent blocks
135 * Format of each record
137 * (length, data) - pairs
138 * length = 0 if same as previous
144 #include "recindxp.h"
149 static void rec_write_head (Records p)
154 assert (p->index_BFile);
156 r = bf_write (p->index_BFile, 0, 0, sizeof(p->head), &p->head);
159 logf (LOG_FATAL|LOG_ERRNO, "write head of %s", p->index_fname);
164 static void rec_tmp_expand (Records p, int size)
166 if (p->tmp_size < size + 2048 ||
167 p->tmp_size < p->head.block_size[REC_BLOCK_TYPES-1]*2)
170 p->tmp_size = size + p->head.block_size[REC_BLOCK_TYPES-1]*2 + 2048;
171 p->tmp_buf = (char *) xmalloc (p->tmp_size);
175 static int read_indx (Records p, int sysno, void *buf, int itemsize,
179 int pos = (sysno-1)*itemsize;
181 r = bf_read (p->index_BFile, 1+pos/128, pos%128, itemsize, buf);
182 if (r != 1 && !ignoreError)
184 logf (LOG_FATAL|LOG_ERRNO, "read in %s at pos %ld",
185 p->index_fname, (long) pos);
191 static void write_indx (Records p, int sysno, void *buf, int itemsize)
193 int pos = (sysno-1)*itemsize;
195 bf_write (p->index_BFile, 1+pos/128, pos%128, itemsize, buf);
198 static void rec_release_blocks (Records p, int sysno)
200 struct record_index_entry entry;
202 char block_and_ref[sizeof(short) + sizeof(int)];
206 if (read_indx (p, sysno, &entry, sizeof(entry), 1) != 1)
209 freeblock = entry.next;
210 assert (freeblock > 0);
211 dst_type = freeblock & 7;
212 assert (dst_type < REC_BLOCK_TYPES);
213 freeblock = freeblock / 8;
216 if (bf_read (p->data_BFile[dst_type], freeblock, 0,
217 sizeof(block_and_ref), block_and_ref) != 1)
219 logf (LOG_FATAL|LOG_ERRNO, "read in rec_del_single");
225 memcpy (&ref, block_and_ref + sizeof(int), sizeof(ref));
227 memcpy (block_and_ref + sizeof(int), &ref, sizeof(ref));
230 if (bf_write (p->data_BFile[dst_type], freeblock, 0,
231 sizeof(block_and_ref), block_and_ref))
233 logf (LOG_FATAL|LOG_ERRNO, "write in rec_del_single");
241 if (bf_write (p->data_BFile[dst_type], freeblock, 0, sizeof(freeblock),
242 &p->head.block_free[dst_type]))
244 logf (LOG_FATAL|LOG_ERRNO, "write in rec_del_single");
247 p->head.block_free[dst_type] = freeblock;
248 memcpy (&freeblock, block_and_ref, sizeof(int));
250 p->head.block_used[dst_type]--;
252 p->head.total_bytes -= entry.size;
255 static void rec_delete_single (Records p, Record rec)
257 struct record_index_entry entry;
259 rec_release_blocks (p, rec->sysno);
261 entry.next = p->head.index_free;
263 p->head.index_free = rec->sysno;
264 write_indx (p, rec->sysno, &entry, sizeof(entry));
267 static void rec_write_tmp_buf (Records p, int size, int *sysnos)
269 struct record_index_entry entry;
271 char *cptr = p->tmp_buf;
272 int block_prev = -1, block_free;
276 for (i = 1; i<REC_BLOCK_TYPES; i++)
277 if (size >= p->head.block_move[i])
279 while (no_written < size)
281 block_free = p->head.block_free[dst_type];
284 if (bf_read (p->data_BFile[dst_type],
285 block_free, 0, sizeof(*p->head.block_free),
286 &p->head.block_free[dst_type]) != 1)
288 logf (LOG_FATAL|LOG_ERRNO, "read in %s at free block %d",
289 p->data_fname[dst_type], block_free);
294 block_free = p->head.block_last[dst_type]++;
295 if (block_prev == -1)
297 entry.next = block_free*8 + dst_type;
299 p->head.total_bytes += size;
302 write_indx (p, *sysnos, &entry, sizeof(entry));
308 memcpy (cptr, &block_free, sizeof(int));
309 bf_write (p->data_BFile[dst_type], block_prev, 0, 0, cptr);
310 cptr = p->tmp_buf + no_written;
312 block_prev = block_free;
313 no_written += p->head.block_size[dst_type] - sizeof(int);
314 p->head.block_used[dst_type]++;
316 assert (block_prev != -1);
318 memcpy (cptr, &block_free, sizeof(int));
319 bf_write (p->data_BFile[dst_type], block_prev, 0,
320 sizeof(int) + (p->tmp_buf+size) - cptr, cptr);
323 Records rec_open (BFiles bfs, int rw, int compression_method)
329 p = (Records) xmalloc (sizeof(*p));
330 p->compression_method = compression_method;
333 p->tmp_buf = (char *) xmalloc (p->tmp_size);
334 p->index_fname = "reci";
335 p->index_BFile = bf_open (bfs, p->index_fname, 128, rw);
336 if (p->index_BFile == NULL)
338 logf (LOG_FATAL|LOG_ERRNO, "open %s", p->index_fname);
341 r = bf_read (p->index_BFile, 0, 0, 0, p->tmp_buf);
345 memcpy (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic));
346 sprintf (p->head.version, "%3d", REC_VERSION);
347 p->head.index_free = 0;
348 p->head.index_last = 1;
349 p->head.no_records = 0;
350 p->head.total_bytes = 0;
351 for (i = 0; i<REC_BLOCK_TYPES; i++)
353 p->head.block_free[i] = 0;
354 p->head.block_last[i] = 1;
355 p->head.block_used[i] = 0;
357 p->head.block_size[0] = 128;
358 p->head.block_move[0] = 0;
359 for (i = 1; i<REC_BLOCK_TYPES; i++)
361 p->head.block_size[i] = p->head.block_size[i-1] * 4;
362 p->head.block_move[i] = p->head.block_size[i] * 24;
368 memcpy (&p->head, p->tmp_buf, sizeof(p->head));
369 if (memcmp (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic)))
371 logf (LOG_FATAL, "file %s has bad format", p->index_fname);
374 version = atoi (p->head.version);
375 if (version != REC_VERSION)
377 logf (LOG_FATAL, "file %s is version %d, but version"
378 " %d is required", p->index_fname, version, REC_VERSION);
383 for (i = 0; i<REC_BLOCK_TYPES; i++)
386 sprintf (str, "recd%c", i + 'A');
387 p->data_fname[i] = (char *) xmalloc (strlen(str)+1);
388 strcpy (p->data_fname[i], str);
389 p->data_BFile[i] = NULL;
391 for (i = 0; i<REC_BLOCK_TYPES; i++)
393 if (!(p->data_BFile[i] = bf_open (bfs, p->data_fname[i],
394 p->head.block_size[i],
397 logf (LOG_FATAL|LOG_ERRNO, "bf_open %s", p->data_fname[i]);
403 p->record_cache = (struct record_cache_entry *)
404 xmalloc (sizeof(*p->record_cache)*p->cache_max);
405 zebra_mutex_init (&p->mutex);
409 static void rec_encode_unsigned (unsigned n, unsigned char *buf, int *len)
414 buf[*len] = 128 + (n & 127);
422 static void rec_decode_unsigned(unsigned *np, unsigned char *buf, int *len)
428 while (buf[*len] > 127)
430 n += w*(buf[*len] & 127);
439 static void rec_cache_flush_block1 (Records p, Record rec, Record last_rec,
440 char **out_buf, int *out_size,
446 for (i = 0; i<REC_NO_INFO; i++)
448 if (*out_offset + (int) rec->size[i] + 20 > *out_size)
450 int new_size = *out_offset + rec->size[i] + 65536;
451 char *np = (char *) xmalloc (new_size);
453 memcpy (np, *out_buf, *out_offset);
455 *out_size = new_size;
460 rec_encode_unsigned (rec->sysno, *out_buf + *out_offset, &len);
461 (*out_offset) += len;
463 if (rec->size[i] == 0)
465 rec_encode_unsigned (1, *out_buf + *out_offset, &len);
466 (*out_offset) += len;
468 else if (last_rec && rec->size[i] == last_rec->size[i] &&
469 !memcmp (rec->info[i], last_rec->info[i], rec->size[i]))
471 rec_encode_unsigned (0, *out_buf + *out_offset, &len);
472 (*out_offset) += len;
476 rec_encode_unsigned (rec->size[i]+1, *out_buf + *out_offset, &len);
477 (*out_offset) += len;
478 memcpy (*out_buf + *out_offset, rec->info[i], rec->size[i]);
479 (*out_offset) += rec->size[i];
484 static void rec_write_multiple (Records p, int saveCount)
488 char compression_method;
492 char *out_buf = (char *) xmalloc (out_size);
493 int *sysnos = (int *) xmalloc (sizeof(*sysnos) * (p->cache_cur + 1));
494 int *sysnop = sysnos;
496 for (i = 0; i<p->cache_cur - saveCount; i++)
498 struct record_cache_entry *e = p->record_cache + i;
502 rec_cache_flush_block1 (p, e->rec, last_rec, &out_buf,
503 &out_size, &out_offset);
504 *sysnop++ = e->rec->sysno;
506 e->flag = recordFlagNop;
509 case recordFlagWrite:
510 rec_release_blocks (p, e->rec->sysno);
511 rec_cache_flush_block1 (p, e->rec, last_rec, &out_buf,
512 &out_size, &out_offset);
513 *sysnop++ = e->rec->sysno;
515 e->flag = recordFlagNop;
518 case recordFlagDelete:
519 rec_delete_single (p, e->rec);
520 e->flag = recordFlagNop;
530 int csize = 0; /* indicate compression "not performed yet" */
531 compression_method = p->compression_method;
532 switch (compression_method)
534 case REC_COMPRESS_BZIP2:
536 csize = out_offset + (out_offset >> 6) + 620;
537 rec_tmp_expand (p, csize);
538 i = bzBuffToBuffCompress (p->tmp_buf+sizeof(int)+sizeof(short)+
540 &csize, out_buf, out_offset, 1, 0, 30);
543 logf (LOG_WARN, "bzBuffToBuffCompress error code=%d", i);
546 logf (LOG_LOG, "compress %4d %5d %5d", ref_count, out_offset,
550 case REC_COMPRESS_NONE:
555 /* either no compression or compression not supported ... */
557 rec_tmp_expand (p, csize);
558 memcpy (p->tmp_buf + sizeof(int) + sizeof(short) + sizeof(char),
559 out_buf, out_offset);
561 compression_method = REC_COMPRESS_NONE;
563 memcpy (p->tmp_buf + sizeof(int), &ref_count, sizeof(ref_count));
564 memcpy (p->tmp_buf + sizeof(int)+sizeof(short),
565 &compression_method, sizeof(compression_method));
567 /* -------- compression */
568 rec_write_tmp_buf (p, csize + sizeof(short) + sizeof(char), sysnos);
574 static void rec_cache_flush (Records p, int saveCount)
578 if (saveCount >= p->cache_cur)
581 rec_write_multiple (p, saveCount);
583 for (i = 0; i<p->cache_cur - saveCount; i++)
585 struct record_cache_entry *e = p->record_cache + i;
588 /* i still being used ... */
589 for (j = 0; j<saveCount; j++, i++)
590 memcpy (p->record_cache+j, p->record_cache+i,
591 sizeof(*p->record_cache));
592 p->cache_cur = saveCount;
595 static Record *rec_cache_lookup (Records p, int sysno,
596 enum recordCacheFlag flag)
599 for (i = 0; i<p->cache_cur; i++)
601 struct record_cache_entry *e = p->record_cache + i;
602 if (e->rec->sysno == sysno)
604 if (flag != recordFlagNop && e->flag == recordFlagNop)
612 static void rec_cache_insert (Records p, Record rec, enum recordCacheFlag flag)
614 struct record_cache_entry *e;
616 if (p->cache_cur == p->cache_max)
617 rec_cache_flush (p, 1);
618 else if (p->cache_cur > 0)
622 for (i = 0; i<p->cache_cur; i++)
624 Record r = (p->record_cache + i)->rec;
625 for (j = 0; j<REC_NO_INFO; j++)
629 rec_cache_flush (p, 1);
631 assert (p->cache_cur < p->cache_max);
633 e = p->record_cache + (p->cache_cur)++;
635 e->rec = rec_cp (rec);
638 void rec_close (Records *pp)
645 zebra_mutex_destroy (&p->mutex);
646 rec_cache_flush (p, 0);
647 xfree (p->record_cache);
653 bf_close (p->index_BFile);
655 for (i = 0; i<REC_BLOCK_TYPES; i++)
657 if (p->data_BFile[i])
658 bf_close (p->data_BFile[i]);
659 xfree (p->data_fname[i]);
666 static Record rec_get_int (Records p, int sysno)
670 struct record_index_entry entry;
671 int freeblock, dst_type;
678 char compression_method;
683 if ((recp = rec_cache_lookup (p, sysno, recordFlagNop)))
684 return rec_cp (*recp);
686 if (read_indx (p, sysno, &entry, sizeof(entry), 1) < 1)
687 return NULL; /* record is not there! */
690 return NULL; /* record is deleted */
692 dst_type = entry.next & 7;
693 assert (dst_type < REC_BLOCK_TYPES);
694 freeblock = entry.next / 8;
696 assert (freeblock > 0);
698 rec_tmp_expand (p, entry.size);
701 r = bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
704 memcpy (&freeblock, cptr, sizeof(freeblock));
710 cptr += p->head.block_size[dst_type] - sizeof(freeblock);
712 memcpy (&tmp, cptr, sizeof(tmp));
713 r = bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
716 memcpy (&freeblock, cptr, sizeof(freeblock));
717 memcpy (cptr, &tmp, sizeof(tmp));
720 rec = (Record) xmalloc (sizeof(*rec));
722 memcpy (&compression_method, p->tmp_buf + sizeof(int) + sizeof(short),
723 sizeof(compression_method));
724 in_buf = p->tmp_buf + sizeof(int) + sizeof(short) + sizeof(char);
725 in_size = entry.size - sizeof(short) - sizeof(char);
726 switch (compression_method)
728 case REC_COMPRESS_BZIP2:
730 bz_size = entry.size * 20 + 100;
733 bz_buf = (char *) xmalloc (bz_size);
734 i = bzBuffToBuffDecompress (bz_buf, &bz_size, in_buf, in_size, 0, 0);
735 logf (LOG_LOG, "decompress %5d %5d", in_size, bz_size);
738 logf (LOG_LOG, "failed");
745 logf (LOG_FATAL, "cannot decompress record(s) in BZIP2 format");
749 case REC_COMPRESS_NONE:
752 for (i = 0; i<REC_NO_INFO; i++)
755 nptr = in_buf; /* skip ref count */
756 while (nptr < in_buf + in_size)
760 rec_decode_unsigned (&this_sysno, nptr, &len);
763 for (i = 0; i < REC_NO_INFO; i++)
766 rec_decode_unsigned (&this_size, nptr, &len);
771 rec->size[i] = this_size-1;
776 nptr += rec->size[i];
781 if (this_sysno == sysno)
784 for (i = 0; i<REC_NO_INFO; i++)
786 if (rec->info[i] && rec->size[i])
788 char *np = xmalloc (rec->size[i]);
789 memcpy (np, rec->info[i], rec->size[i]);
794 assert (rec->info[i] == 0);
795 assert (rec->size[i] == 0);
799 rec_cache_insert (p, rec, recordFlagNop);
803 Record rec_get (Records p, int sysno)
806 zebra_mutex_lock (&p->mutex);
808 rec = rec_get_int (p, sysno);
809 zebra_mutex_unlock (&p->mutex);
813 static Record rec_new_int (Records p)
819 rec = (Record) xmalloc (sizeof(*rec));
820 if (1 || p->head.index_free == 0)
821 sysno = (p->head.index_last)++;
824 struct record_index_entry entry;
826 read_indx (p, p->head.index_free, &entry, sizeof(entry), 0);
827 sysno = p->head.index_free;
828 p->head.index_free = entry.next;
834 logf (LOG_FATAL, "100,000 record limit reached");
838 (p->head.no_records)++;
840 for (i = 0; i < REC_NO_INFO; i++)
845 rec_cache_insert (p, rec, recordFlagNew);
849 Record rec_new (Records p)
852 zebra_mutex_lock (&p->mutex);
854 rec = rec_new_int (p);
855 zebra_mutex_unlock (&p->mutex);
859 void rec_del (Records p, Record *recpp)
863 zebra_mutex_lock (&p->mutex);
864 (p->head.no_records)--;
865 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagDelete)))
872 rec_cache_insert (p, *recpp, recordFlagDelete);
875 zebra_mutex_unlock (&p->mutex);
879 void rec_put (Records p, Record *recpp)
883 zebra_mutex_lock (&p->mutex);
884 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagWrite)))
891 rec_cache_insert (p, *recpp, recordFlagWrite);
894 zebra_mutex_unlock (&p->mutex);
898 void rec_rm (Record *recpp)
904 for (i = 0; i < REC_NO_INFO; i++)
905 xfree ((*recpp)->info[i]);
910 Record rec_cp (Record rec)
915 n = (Record) xmalloc (sizeof(*n));
916 n->sysno = rec->sysno;
917 for (i = 0; i < REC_NO_INFO; i++)
925 n->size[i] = rec->size[i];
926 n->info[i] = (char *) xmalloc (rec->size[i]);
927 memcpy (n->info[i], rec->info[i], rec->size[i]);
933 char *rec_strdup (const char *s, size_t *len)
943 p = (char *) xmalloc (*len);