2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.29 2000-04-05 09:49:35 adam
8 * On Unix, zebra/z'mbol uses automake.
10 * Revision 1.28 1999/12/08 22:44:45 adam
11 * Zebra/Z'mbol dependencies added.
13 * Revision 1.27 1999/10/29 10:02:33 adam
14 * Fixed decompression buffer overflow.
16 * Revision 1.26 1999/07/06 13:34:57 adam
17 * Fixed bug (introduced by previous commit).
19 * Revision 1.25 1999/07/06 12:28:04 adam
20 * Updated record index structure. Format includes version ID. Compression
21 * algorithm ID is stored for each record block.
23 * Revision 1.24 1999/06/25 13:48:02 adam
24 * Updated MSVC project files.
25 * Added BZIP2 record compression (not very well tested).
27 * Revision 1.23 1999/05/26 07:49:13 adam
30 * Revision 1.22 1999/02/18 12:49:34 adam
31 * Changed file naming scheme for register files as well as record
34 * Revision 1.21 1999/02/02 14:51:03 adam
35 * Updated WIN32 code specific sections. Changed header.
37 * Revision 1.20 1998/01/12 15:04:08 adam
38 * The test option (-s) only uses read-lock (and not write lock).
40 * Revision 1.19 1997/09/17 12:19:16 adam
41 * Zebra version corresponds to YAZ version 1.4.
42 * Changed Zebra server so that it doesn't depend on global common_resource.
44 * Revision 1.18 1997/07/15 16:28:42 adam
45 * Bug fix: storeData didn't work with files with multiple records.
46 * Bug fix: fixed memory management with records; not really well
49 * Revision 1.17 1997/02/12 20:39:46 adam
50 * Implemented options -f <n> that limits the log to the first <n>
52 * Changed some log messages also.
54 * Revision 1.16 1996/06/04 10:19:00 adam
55 * Minor changes - removed include of ctype.h.
57 * Revision 1.15 1996/05/13 14:23:06 adam
58 * Work on compaction of set/use bytes in dictionary.
60 * Revision 1.14 1996/02/01 20:48:15 adam
61 * The total size of records are always checked in rec_cache_insert to
62 * reduce memory usage.
64 * Revision 1.13 1995/12/11 09:12:49 adam
65 * The rec_get function returns NULL if record doesn't exist - will
66 * happen in the server if the result set records have been deleted since
67 * the creation of the set (i.e. the search).
68 * The server saves a result temporarily if it is 'volatile', i.e. the
69 * set is register dependent.
71 * Revision 1.12 1995/12/07 17:38:47 adam
72 * Work locking mechanisms for concurrent updates/commit.
74 * Revision 1.11 1995/12/06 13:58:26 adam
75 * Improved flushing of records - all flushes except the last one
76 * don't write the last accessed. Also flush takes place if record
77 * info occupy more than about 256k.
79 * Revision 1.10 1995/12/06 12:41:24 adam
80 * New command 'stat' for the index program.
81 * Filenames can be read from stdin by specifying '-'.
82 * Bug fix/enhancement of the transformation from terms to regular
83 * expressons in the search engine.
85 * Revision 1.9 1995/11/30 08:34:33 adam
86 * Started work on commit facility.
87 * Changed a few malloc/free to xmalloc/xfree.
89 * Revision 1.8 1995/11/28 14:26:21 adam
90 * Bug fix: recordId with constant wasn't right.
91 * Bug fix: recordId dictionary entry wasn't deleted when needed.
93 * Revision 1.7 1995/11/28 09:09:43 adam
94 * Zebra config renamed.
95 * Use setting 'recordId' to identify record now.
96 * Bug fix in recindex.c: rec_release_blocks was invokeded even
97 * though the blocks were already released.
98 * File traversal properly deletes records when needed.
100 * Revision 1.6 1995/11/25 10:24:06 adam
101 * More record fields - they are enumerated now.
102 * New options: flagStoreData flagStoreKey.
104 * Revision 1.5 1995/11/22 17:19:18 adam
105 * Record management uses the bfile system.
107 * Revision 1.4 1995/11/20 16:59:46 adam
108 * New update method: the 'old' keys are saved for each records.
110 * Revision 1.3 1995/11/16 15:34:55 adam
111 * Uses new record management system in both indexer and server.
113 * Revision 1.2 1995/11/15 19:13:08 adam
114 * Work on record management.
116 * Revision 1.1 1995/11/15 14:46:20 adam
117 * Started work on better record management system.
123 * Format of first block
125 * ref_count (4 bytes)
128 * Format of subsequent blocks
132 * Format of each record
134 * (length, data) - pairs
135 * length = 0 if same as previous
141 #include "recindxp.h"
146 static void rec_write_head (Records p)
151 assert (p->index_BFile);
153 r = bf_write (p->index_BFile, 0, 0, sizeof(p->head), &p->head);
156 logf (LOG_FATAL|LOG_ERRNO, "write head of %s", p->index_fname);
161 static void rec_tmp_expand (Records p, int size)
163 if (p->tmp_size < size + 2048 ||
164 p->tmp_size < p->head.block_size[REC_BLOCK_TYPES-1]*2)
167 p->tmp_size = size + p->head.block_size[REC_BLOCK_TYPES-1]*2 + 2048;
168 p->tmp_buf = (char *) xmalloc (p->tmp_size);
172 static int read_indx (Records p, int sysno, void *buf, int itemsize,
176 int pos = (sysno-1)*itemsize;
178 r = bf_read (p->index_BFile, 1+pos/128, pos%128, itemsize, buf);
179 if (r != 1 && !ignoreError)
181 logf (LOG_FATAL|LOG_ERRNO, "read in %s at pos %ld",
182 p->index_fname, (long) pos);
188 static void write_indx (Records p, int sysno, void *buf, int itemsize)
190 int pos = (sysno-1)*itemsize;
192 bf_write (p->index_BFile, 1+pos/128, pos%128, itemsize, buf);
195 static void rec_release_blocks (Records p, int sysno)
197 struct record_index_entry entry;
199 char block_and_ref[sizeof(short) + sizeof(int)];
203 if (read_indx (p, sysno, &entry, sizeof(entry), 1) != 1)
206 freeblock = entry.next;
207 assert (freeblock > 0);
208 dst_type = freeblock & 7;
209 assert (dst_type < REC_BLOCK_TYPES);
210 freeblock = freeblock / 8;
213 if (bf_read (p->data_BFile[dst_type], freeblock, 0,
214 sizeof(block_and_ref), block_and_ref) != 1)
216 logf (LOG_FATAL|LOG_ERRNO, "read in rec_del_single");
222 memcpy (&ref, block_and_ref + sizeof(int), sizeof(ref));
224 memcpy (block_and_ref + sizeof(int), &ref, sizeof(ref));
227 if (bf_write (p->data_BFile[dst_type], freeblock, 0,
228 sizeof(block_and_ref), block_and_ref))
230 logf (LOG_FATAL|LOG_ERRNO, "write in rec_del_single");
238 if (bf_write (p->data_BFile[dst_type], freeblock, 0, sizeof(freeblock),
239 &p->head.block_free[dst_type]))
241 logf (LOG_FATAL|LOG_ERRNO, "write in rec_del_single");
244 p->head.block_free[dst_type] = freeblock;
245 memcpy (&freeblock, block_and_ref, sizeof(int));
247 p->head.block_used[dst_type]--;
249 p->head.total_bytes -= entry.size;
252 static void rec_delete_single (Records p, Record rec)
254 struct record_index_entry entry;
256 rec_release_blocks (p, rec->sysno);
258 entry.next = p->head.index_free;
260 p->head.index_free = rec->sysno;
261 write_indx (p, rec->sysno, &entry, sizeof(entry));
264 static void rec_write_tmp_buf (Records p, int size, int *sysnos)
266 struct record_index_entry entry;
268 char *cptr = p->tmp_buf;
269 int block_prev = -1, block_free;
273 for (i = 1; i<REC_BLOCK_TYPES; i++)
274 if (size >= p->head.block_move[i])
276 while (no_written < size)
278 block_free = p->head.block_free[dst_type];
281 if (bf_read (p->data_BFile[dst_type],
282 block_free, 0, sizeof(*p->head.block_free),
283 &p->head.block_free[dst_type]) != 1)
285 logf (LOG_FATAL|LOG_ERRNO, "read in %s at free block %d",
286 p->data_fname[dst_type], block_free);
291 block_free = p->head.block_last[dst_type]++;
292 if (block_prev == -1)
294 entry.next = block_free*8 + dst_type;
296 p->head.total_bytes += size;
299 write_indx (p, *sysnos, &entry, sizeof(entry));
305 memcpy (cptr, &block_free, sizeof(int));
306 bf_write (p->data_BFile[dst_type], block_prev, 0, 0, cptr);
307 cptr = p->tmp_buf + no_written;
309 block_prev = block_free;
310 no_written += p->head.block_size[dst_type] - sizeof(int);
311 p->head.block_used[dst_type]++;
313 assert (block_prev != -1);
315 memcpy (cptr, &block_free, sizeof(int));
316 bf_write (p->data_BFile[dst_type], block_prev, 0,
317 sizeof(int) + (p->tmp_buf+size) - cptr, cptr);
320 Records rec_open (BFiles bfs, int rw, int compression_method)
326 p = (Records) xmalloc (sizeof(*p));
327 p->compression_method = compression_method;
330 p->tmp_buf = (char *) xmalloc (p->tmp_size);
331 p->index_fname = "reci";
332 p->index_BFile = bf_open (bfs, p->index_fname, 128, rw);
333 if (p->index_BFile == NULL)
335 logf (LOG_FATAL|LOG_ERRNO, "open %s", p->index_fname);
338 r = bf_read (p->index_BFile, 0, 0, 0, p->tmp_buf);
342 memcpy (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic));
343 sprintf (p->head.version, "%3d", REC_VERSION);
344 p->head.index_free = 0;
345 p->head.index_last = 1;
346 p->head.no_records = 0;
347 p->head.total_bytes = 0;
348 for (i = 0; i<REC_BLOCK_TYPES; i++)
350 p->head.block_free[i] = 0;
351 p->head.block_last[i] = 1;
352 p->head.block_used[i] = 0;
354 p->head.block_size[0] = 128;
355 p->head.block_move[0] = 0;
356 for (i = 1; i<REC_BLOCK_TYPES; i++)
358 p->head.block_size[i] = p->head.block_size[i-1] * 4;
359 p->head.block_move[i] = p->head.block_size[i] * 24;
365 memcpy (&p->head, p->tmp_buf, sizeof(p->head));
366 if (memcmp (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic)))
368 logf (LOG_FATAL, "file %s has bad format", p->index_fname);
371 version = atoi (p->head.version);
372 if (version != REC_VERSION)
374 logf (LOG_FATAL, "file %s is version %d, but version"
375 " %d is required", p->index_fname, version, REC_VERSION);
380 for (i = 0; i<REC_BLOCK_TYPES; i++)
383 sprintf (str, "recd%c", i + 'A');
384 p->data_fname[i] = (char *) xmalloc (strlen(str)+1);
385 strcpy (p->data_fname[i], str);
386 p->data_BFile[i] = NULL;
388 for (i = 0; i<REC_BLOCK_TYPES; i++)
390 if (!(p->data_BFile[i] = bf_open (bfs, p->data_fname[i],
391 p->head.block_size[i],
394 logf (LOG_FATAL|LOG_ERRNO, "bf_open %s", p->data_fname[i]);
400 p->record_cache = (struct record_cache_entry *)
401 xmalloc (sizeof(*p->record_cache)*p->cache_max);
402 zebra_mutex_init (&p->mutex);
406 static void rec_encode_unsigned (unsigned n, unsigned char *buf, int *len)
411 buf[*len] = 128 + (n & 127);
419 static void rec_decode_unsigned(unsigned *np, unsigned char *buf, int *len)
425 while (buf[*len] > 127)
427 n += w*(buf[*len] & 127);
436 static void rec_cache_flush_block1 (Records p, Record rec, Record last_rec,
437 char **out_buf, int *out_size,
443 for (i = 0; i<REC_NO_INFO; i++)
445 if (*out_offset + (int) rec->size[i] + 20 > *out_size)
447 int new_size = *out_offset + rec->size[i] + 65536;
448 char *np = (char *) xmalloc (new_size);
450 memcpy (np, *out_buf, *out_offset);
452 *out_size = new_size;
457 rec_encode_unsigned (rec->sysno, *out_buf + *out_offset, &len);
458 (*out_offset) += len;
460 if (rec->size[i] == 0)
462 rec_encode_unsigned (1, *out_buf + *out_offset, &len);
463 (*out_offset) += len;
465 else if (last_rec && rec->size[i] == last_rec->size[i] &&
466 !memcmp (rec->info[i], last_rec->info[i], rec->size[i]))
468 rec_encode_unsigned (0, *out_buf + *out_offset, &len);
469 (*out_offset) += len;
473 rec_encode_unsigned (rec->size[i]+1, *out_buf + *out_offset, &len);
474 (*out_offset) += len;
475 memcpy (*out_buf + *out_offset, rec->info[i], rec->size[i]);
476 (*out_offset) += rec->size[i];
481 static void rec_write_multiple (Records p, int saveCount)
485 char compression_method;
489 char *out_buf = (char *) xmalloc (out_size);
490 int *sysnos = (int *) xmalloc (sizeof(*sysnos) * (p->cache_cur + 1));
491 int *sysnop = sysnos;
493 for (i = 0; i<p->cache_cur - saveCount; i++)
495 struct record_cache_entry *e = p->record_cache + i;
499 rec_cache_flush_block1 (p, e->rec, last_rec, &out_buf,
500 &out_size, &out_offset);
501 *sysnop++ = e->rec->sysno;
503 e->flag = recordFlagNop;
506 case recordFlagWrite:
507 rec_release_blocks (p, e->rec->sysno);
508 rec_cache_flush_block1 (p, e->rec, last_rec, &out_buf,
509 &out_size, &out_offset);
510 *sysnop++ = e->rec->sysno;
512 e->flag = recordFlagNop;
515 case recordFlagDelete:
516 rec_delete_single (p, e->rec);
517 e->flag = recordFlagNop;
527 int csize = 0; /* indicate compression "not performed yet" */
528 compression_method = p->compression_method;
529 switch (compression_method)
531 case REC_COMPRESS_BZIP2:
533 csize = out_offset + (out_offset >> 6) + 620;
534 rec_tmp_expand (p, csize);
535 i = bzBuffToBuffCompress (p->tmp_buf+sizeof(int)+sizeof(short)+
537 &csize, out_buf, out_offset, 1, 0, 30);
540 logf (LOG_WARN, "bzBuffToBuffCompress error code=%d", i);
543 logf (LOG_LOG, "compress %4d %5d %5d", ref_count, out_offset,
547 case REC_COMPRESS_NONE:
552 /* either no compression or compression not supported ... */
554 rec_tmp_expand (p, csize);
555 memcpy (p->tmp_buf + sizeof(int) + sizeof(short) + sizeof(char),
556 out_buf, out_offset);
558 compression_method = REC_COMPRESS_NONE;
560 memcpy (p->tmp_buf + sizeof(int), &ref_count, sizeof(ref_count));
561 memcpy (p->tmp_buf + sizeof(int)+sizeof(short),
562 &compression_method, sizeof(compression_method));
564 /* -------- compression */
565 rec_write_tmp_buf (p, csize + sizeof(short) + sizeof(char), sysnos);
571 static void rec_cache_flush (Records p, int saveCount)
575 if (saveCount >= p->cache_cur)
578 rec_write_multiple (p, saveCount);
580 for (i = 0; i<p->cache_cur - saveCount; i++)
582 struct record_cache_entry *e = p->record_cache + i;
585 /* i still being used ... */
586 for (j = 0; j<saveCount; j++, i++)
587 memcpy (p->record_cache+j, p->record_cache+i,
588 sizeof(*p->record_cache));
589 p->cache_cur = saveCount;
592 static Record *rec_cache_lookup (Records p, int sysno,
593 enum recordCacheFlag flag)
596 for (i = 0; i<p->cache_cur; i++)
598 struct record_cache_entry *e = p->record_cache + i;
599 if (e->rec->sysno == sysno)
601 if (flag != recordFlagNop && e->flag == recordFlagNop)
609 static void rec_cache_insert (Records p, Record rec, enum recordCacheFlag flag)
611 struct record_cache_entry *e;
613 if (p->cache_cur == p->cache_max)
614 rec_cache_flush (p, 1);
615 else if (p->cache_cur > 0)
619 for (i = 0; i<p->cache_cur; i++)
621 Record r = (p->record_cache + i)->rec;
622 for (j = 0; j<REC_NO_INFO; j++)
626 rec_cache_flush (p, 1);
628 assert (p->cache_cur < p->cache_max);
630 e = p->record_cache + (p->cache_cur)++;
632 e->rec = rec_cp (rec);
635 void rec_close (Records *pp)
642 zebra_mutex_destroy (&p->mutex);
643 rec_cache_flush (p, 0);
644 xfree (p->record_cache);
650 bf_close (p->index_BFile);
652 for (i = 0; i<REC_BLOCK_TYPES; i++)
654 if (p->data_BFile[i])
655 bf_close (p->data_BFile[i]);
656 xfree (p->data_fname[i]);
663 static Record rec_get_int (Records p, int sysno)
667 struct record_index_entry entry;
668 int freeblock, dst_type;
673 char compression_method;
678 if ((recp = rec_cache_lookup (p, sysno, recordFlagNop)))
679 return rec_cp (*recp);
681 if (read_indx (p, sysno, &entry, sizeof(entry), 1) < 1)
682 return NULL; /* record is not there! */
685 return NULL; /* record is deleted */
687 dst_type = entry.next & 7;
688 assert (dst_type < REC_BLOCK_TYPES);
689 freeblock = entry.next / 8;
691 assert (freeblock > 0);
693 rec_tmp_expand (p, entry.size);
696 r = bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
699 memcpy (&freeblock, cptr, sizeof(freeblock));
705 cptr += p->head.block_size[dst_type] - sizeof(freeblock);
707 memcpy (&tmp, cptr, sizeof(tmp));
708 r = bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
711 memcpy (&freeblock, cptr, sizeof(freeblock));
712 memcpy (cptr, &tmp, sizeof(tmp));
715 rec = (Record) xmalloc (sizeof(*rec));
717 memcpy (&compression_method, p->tmp_buf + sizeof(int) + sizeof(short),
718 sizeof(compression_method));
719 in_buf = p->tmp_buf + sizeof(int) + sizeof(short) + sizeof(char);
720 in_size = entry.size - sizeof(short) - sizeof(char);
721 switch (compression_method)
723 case REC_COMPRESS_BZIP2:
725 bz_size = entry.size * 20 + 100;
728 bz_buf = (char *) xmalloc (bz_size);
729 i = bzBuffToBuffDecompress (bz_buf, &bz_size, in_buf, in_size, 0, 0);
730 logf (LOG_LOG, "decompress %5d %5d", in_size, bz_size);
733 logf (LOG_LOG, "failed");
740 logf (LOG_FATAL, "cannot decompress record(s) in BZIP2 format");
744 case REC_COMPRESS_NONE:
747 for (i = 0; i<REC_NO_INFO; i++)
750 nptr = in_buf; /* skip ref count */
751 while (nptr < in_buf + in_size)
755 rec_decode_unsigned (&this_sysno, nptr, &len);
758 for (i = 0; i < REC_NO_INFO; i++)
761 rec_decode_unsigned (&this_size, nptr, &len);
766 rec->size[i] = this_size-1;
771 nptr += rec->size[i];
776 if (this_sysno == sysno)
779 for (i = 0; i<REC_NO_INFO; i++)
781 if (rec->info[i] && rec->size[i])
783 char *np = xmalloc (rec->size[i]);
784 memcpy (np, rec->info[i], rec->size[i]);
789 assert (rec->info[i] == 0);
790 assert (rec->size[i] == 0);
794 rec_cache_insert (p, rec, recordFlagNop);
798 Record rec_get (Records p, int sysno)
801 zebra_mutex_lock (&p->mutex);
803 rec = rec_get_int (p, sysno);
804 zebra_mutex_unlock (&p->mutex);
808 static Record rec_new_int (Records p)
814 rec = (Record) xmalloc (sizeof(*rec));
815 if (1 || p->head.index_free == 0)
816 sysno = (p->head.index_last)++;
819 struct record_index_entry entry;
821 read_indx (p, p->head.index_free, &entry, sizeof(entry), 0);
822 sysno = p->head.index_free;
823 p->head.index_free = entry.next;
829 logf (LOG_FATAL, "100,000 record limit reached");
833 (p->head.no_records)++;
835 for (i = 0; i < REC_NO_INFO; i++)
840 rec_cache_insert (p, rec, recordFlagNew);
844 Record rec_new (Records p)
847 zebra_mutex_lock (&p->mutex);
849 rec = rec_new_int (p);
850 zebra_mutex_unlock (&p->mutex);
854 void rec_del (Records p, Record *recpp)
858 zebra_mutex_lock (&p->mutex);
859 (p->head.no_records)--;
860 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagDelete)))
867 rec_cache_insert (p, *recpp, recordFlagDelete);
870 zebra_mutex_unlock (&p->mutex);
874 void rec_put (Records p, Record *recpp)
878 zebra_mutex_lock (&p->mutex);
879 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagWrite)))
886 rec_cache_insert (p, *recpp, recordFlagWrite);
889 zebra_mutex_unlock (&p->mutex);
893 void rec_rm (Record *recpp)
899 for (i = 0; i < REC_NO_INFO; i++)
900 xfree ((*recpp)->info[i]);
905 Record rec_cp (Record rec)
910 n = (Record) xmalloc (sizeof(*n));
911 n->sysno = rec->sysno;
912 for (i = 0; i < REC_NO_INFO; i++)
920 n->size[i] = rec->size[i];
921 n->info[i] = (char *) xmalloc (rec->size[i]);
922 memcpy (n->info[i], rec->info[i], rec->size[i]);
928 char *rec_strdup (const char *s, size_t *len)
938 p = (char *) xmalloc (*len);