2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.25 1999-07-06 12:28:04 adam
8 * Updated record index structure. Format includes version ID. Compression
9 * algorithm ID is stored for each record block.
11 * Revision 1.24 1999/06/25 13:48:02 adam
12 * Updated MSVC project files.
13 * Added BZIP2 record compression (not very well tested).
15 * Revision 1.23 1999/05/26 07:49:13 adam
18 * Revision 1.22 1999/02/18 12:49:34 adam
19 * Changed file naming scheme for register files as well as record
22 * Revision 1.21 1999/02/02 14:51:03 adam
23 * Updated WIN32 code specific sections. Changed header.
25 * Revision 1.20 1998/01/12 15:04:08 adam
26 * The test option (-s) only uses read-lock (and not write lock).
28 * Revision 1.19 1997/09/17 12:19:16 adam
29 * Zebra version corresponds to YAZ version 1.4.
30 * Changed Zebra server so that it doesn't depend on global common_resource.
32 * Revision 1.18 1997/07/15 16:28:42 adam
33 * Bug fix: storeData didn't work with files with multiple records.
34 * Bug fix: fixed memory management with records; not really well
37 * Revision 1.17 1997/02/12 20:39:46 adam
38 * Implemented options -f <n> that limits the log to the first <n>
40 * Changed some log messages also.
42 * Revision 1.16 1996/06/04 10:19:00 adam
43 * Minor changes - removed include of ctype.h.
45 * Revision 1.15 1996/05/13 14:23:06 adam
46 * Work on compaction of set/use bytes in dictionary.
48 * Revision 1.14 1996/02/01 20:48:15 adam
49 * The total size of records are always checked in rec_cache_insert to
50 * reduce memory usage.
52 * Revision 1.13 1995/12/11 09:12:49 adam
53 * The rec_get function returns NULL if record doesn't exist - will
54 * happen in the server if the result set records have been deleted since
55 * the creation of the set (i.e. the search).
56 * The server saves a result temporarily if it is 'volatile', i.e. the
57 * set is register dependent.
59 * Revision 1.12 1995/12/07 17:38:47 adam
60 * Work locking mechanisms for concurrent updates/commit.
62 * Revision 1.11 1995/12/06 13:58:26 adam
63 * Improved flushing of records - all flushes except the last one
64 * don't write the last accessed. Also flush takes place if record
65 * info occupy more than about 256k.
67 * Revision 1.10 1995/12/06 12:41:24 adam
68 * New command 'stat' for the index program.
69 * Filenames can be read from stdin by specifying '-'.
70 * Bug fix/enhancement of the transformation from terms to regular
71 * expressons in the search engine.
73 * Revision 1.9 1995/11/30 08:34:33 adam
74 * Started work on commit facility.
75 * Changed a few malloc/free to xmalloc/xfree.
77 * Revision 1.8 1995/11/28 14:26:21 adam
78 * Bug fix: recordId with constant wasn't right.
79 * Bug fix: recordId dictionary entry wasn't deleted when needed.
81 * Revision 1.7 1995/11/28 09:09:43 adam
82 * Zebra config renamed.
83 * Use setting 'recordId' to identify record now.
84 * Bug fix in recindex.c: rec_release_blocks was invokeded even
85 * though the blocks were already released.
86 * File traversal properly deletes records when needed.
88 * Revision 1.6 1995/11/25 10:24:06 adam
89 * More record fields - they are enumerated now.
90 * New options: flagStoreData flagStoreKey.
92 * Revision 1.5 1995/11/22 17:19:18 adam
93 * Record management uses the bfile system.
95 * Revision 1.4 1995/11/20 16:59:46 adam
96 * New update method: the 'old' keys are saved for each records.
98 * Revision 1.3 1995/11/16 15:34:55 adam
99 * Uses new record management system in both indexer and server.
101 * Revision 1.2 1995/11/15 19:13:08 adam
102 * Work on record management.
104 * Revision 1.1 1995/11/15 14:46:20 adam
105 * Started work on better record management system.
111 * Format of first block
113 * ref_count (4 bytes)
116 * Format of subsequent blocks
120 * Format of each record
122 * (length, data) - pairs
123 * length = 0 if same as previous
129 #include "recindxp.h"
134 static void rec_write_head (Records p)
139 assert (p->index_BFile);
141 r = bf_write (p->index_BFile, 0, 0, sizeof(p->head), &p->head);
144 logf (LOG_FATAL|LOG_ERRNO, "write head of %s", p->index_fname);
149 static void rec_tmp_expand (Records p, int size)
151 if (p->tmp_size < size + 2048 ||
152 p->tmp_size < p->head.block_size[REC_BLOCK_TYPES-1]*2)
155 p->tmp_size = size + p->head.block_size[REC_BLOCK_TYPES-1]*2 + 2048;
156 p->tmp_buf = (char *) xmalloc (p->tmp_size);
160 static int read_indx (Records p, int sysno, void *buf, int itemsize,
164 int pos = (sysno-1)*itemsize;
166 r = bf_read (p->index_BFile, 1+pos/128, pos%128, itemsize, buf);
167 if (r != 1 && !ignoreError)
169 logf (LOG_FATAL|LOG_ERRNO, "read in %s at pos %ld",
170 p->index_fname, (long) pos);
176 static void write_indx (Records p, int sysno, void *buf, int itemsize)
178 int pos = (sysno-1)*itemsize;
180 bf_write (p->index_BFile, 1+pos/128, pos%128, itemsize, buf);
183 static void rec_release_blocks (Records p, int sysno)
185 struct record_index_entry entry;
187 char block_and_ref[sizeof(short) + sizeof(int)];
191 if (read_indx (p, sysno, &entry, sizeof(entry), 1) != 1)
194 freeblock = entry.next;
195 assert (freeblock > 0);
196 dst_type = freeblock & 7;
197 assert (dst_type < REC_BLOCK_TYPES);
198 freeblock = freeblock / 8;
201 if (bf_read (p->data_BFile[dst_type], freeblock, 0,
202 sizeof(block_and_ref), block_and_ref) != 1)
204 logf (LOG_FATAL|LOG_ERRNO, "read in rec_del_single");
210 memcpy (&ref, block_and_ref + sizeof(int), sizeof(ref));
212 memcpy (block_and_ref + sizeof(int), &ref, sizeof(ref));
215 if (bf_write (p->data_BFile[dst_type], freeblock, 0,
216 sizeof(block_and_ref), block_and_ref))
218 logf (LOG_FATAL|LOG_ERRNO, "write in rec_del_single");
226 if (bf_write (p->data_BFile[dst_type], freeblock, 0, sizeof(freeblock),
227 &p->head.block_free[dst_type]))
229 logf (LOG_FATAL|LOG_ERRNO, "write in rec_del_single");
232 p->head.block_free[dst_type] = freeblock;
233 memcpy (&freeblock, block_and_ref, sizeof(int));
235 p->head.block_used[dst_type]--;
237 p->head.total_bytes -= entry.size;
240 static void rec_delete_single (Records p, Record rec)
242 struct record_index_entry entry;
244 rec_release_blocks (p, rec->sysno);
246 entry.next = p->head.index_free;
248 p->head.index_free = rec->sysno;
249 write_indx (p, rec->sysno, &entry, sizeof(entry));
252 static void rec_write_tmp_buf (Records p, int size, int *sysnos)
254 struct record_index_entry entry;
256 char *cptr = p->tmp_buf;
257 int block_prev = -1, block_free;
261 for (i = 1; i<REC_BLOCK_TYPES; i++)
262 if (size >= p->head.block_move[i])
264 while (no_written < size)
266 block_free = p->head.block_free[dst_type];
269 if (bf_read (p->data_BFile[dst_type],
270 block_free, 0, sizeof(*p->head.block_free),
271 &p->head.block_free[dst_type]) != 1)
273 logf (LOG_FATAL|LOG_ERRNO, "read in %s at free block %d",
274 p->data_fname[dst_type], block_free);
279 block_free = p->head.block_last[dst_type]++;
280 if (block_prev == -1)
282 entry.next = block_free*8 + dst_type;
284 p->head.total_bytes += size;
287 write_indx (p, *sysnos, &entry, sizeof(entry));
293 memcpy (cptr, &block_free, sizeof(int));
294 bf_write (p->data_BFile[dst_type], block_prev, 0, 0, cptr);
295 cptr = p->tmp_buf + no_written;
297 block_prev = block_free;
298 no_written += p->head.block_size[dst_type] - sizeof(int);
299 p->head.block_used[dst_type]++;
301 assert (block_prev != -1);
303 memcpy (cptr, &block_free, sizeof(int));
304 bf_write (p->data_BFile[dst_type], block_prev, 0,
305 sizeof(int) + (p->tmp_buf+size) - cptr, cptr);
308 Records rec_open (BFiles bfs, int rw, int compression_method)
314 p = (Records) xmalloc (sizeof(*p));
315 p->compression_method = compression_method;
318 p->tmp_buf = (char *) xmalloc (p->tmp_size);
319 p->index_fname = "reci";
320 p->index_BFile = bf_open (bfs, p->index_fname, 128, rw);
321 if (p->index_BFile == NULL)
323 logf (LOG_FATAL|LOG_ERRNO, "open %s", p->index_fname);
326 r = bf_read (p->index_BFile, 0, 0, 0, p->tmp_buf);
330 memcpy (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic));
331 sprintf (p->head.version, "%3d", REC_VERSION);
332 p->head.index_free = 0;
333 p->head.index_last = 1;
334 p->head.no_records = 0;
335 p->head.total_bytes = 0;
336 for (i = 0; i<REC_BLOCK_TYPES; i++)
338 p->head.block_free[i] = 0;
339 p->head.block_last[i] = 1;
340 p->head.block_used[i] = 0;
342 p->head.block_size[0] = 128;
343 p->head.block_move[0] = 0;
344 for (i = 1; i<REC_BLOCK_TYPES; i++)
346 p->head.block_size[i] = p->head.block_size[i-1] * 4;
347 p->head.block_move[i] = p->head.block_size[i] * 24;
353 memcpy (&p->head, p->tmp_buf, sizeof(p->head));
354 if (memcmp (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic)))
356 logf (LOG_FATAL, "file %s has bad format", p->index_fname);
359 version = atoi (p->head.version);
360 if (version != REC_VERSION)
362 logf (LOG_FATAL, "file %s is version %d, but version"
363 " %d is required", p->index_fname, version, REC_VERSION);
368 for (i = 0; i<REC_BLOCK_TYPES; i++)
371 sprintf (str, "recd%c", i + 'A');
372 p->data_fname[i] = (char *) xmalloc (strlen(str)+1);
373 strcpy (p->data_fname[i], str);
374 p->data_BFile[i] = NULL;
376 for (i = 0; i<REC_BLOCK_TYPES; i++)
378 if (!(p->data_BFile[i] = bf_open (bfs, p->data_fname[i],
379 p->head.block_size[i],
382 logf (LOG_FATAL|LOG_ERRNO, "bf_open %s", p->data_fname[i]);
388 p->record_cache = (struct record_cache_entry *)
389 xmalloc (sizeof(*p->record_cache)*p->cache_max);
393 static void rec_encode_unsigned (unsigned n, unsigned char *buf, int *len)
398 buf[*len] = 128 + (n & 127);
406 static void rec_decode_unsigned(unsigned *np, unsigned char *buf, int *len)
412 while (buf[*len] > 127)
414 n += w*(buf[*len] & 127);
422 static void rec_cache_flush_block1 (Records p, Record rec, Record last_rec,
423 char **out_buf, int *out_size,
429 for (i = 0; i<REC_NO_INFO; i++)
431 if (*out_offset + (int) rec->size[i] + 20 > *out_size)
433 int new_size = *out_offset + rec->size[i] + 65536;
434 char *np = (char *) xmalloc (new_size);
436 memcpy (np, *out_buf, *out_offset);
438 *out_size = new_size;
443 rec_encode_unsigned (rec->sysno, *out_buf + *out_offset, &len);
444 (*out_offset) += len;
446 if (rec->size[i] == 0)
448 rec_encode_unsigned (1, *out_buf + *out_offset, &len);
449 (*out_offset) += len;
451 else if (last_rec && rec->size[i] == last_rec->size[i] &&
452 !memcmp (rec->info[i], last_rec->info[i], rec->size[i]))
454 rec_encode_unsigned (0, *out_buf + *out_offset, &len);
455 (*out_offset) += len;
459 rec_encode_unsigned (rec->size[i]+1, *out_buf + *out_offset, &len);
460 (*out_offset) += len;
461 memcpy (*out_buf + *out_offset, rec->info[i], rec->size[i]);
462 (*out_offset) += rec->size[i];
467 static void rec_write_multiple (Records p, int saveCount)
471 char compression_method;
475 char *out_buf = (char *) xmalloc (out_size);
476 int *sysnos = (int *) xmalloc (sizeof(*sysnos) * (p->cache_cur + 1));
477 int *sysnop = sysnos;
479 for (i = 0; i<p->cache_cur - saveCount; i++)
481 struct record_cache_entry *e = p->record_cache + i;
485 rec_cache_flush_block1 (p, e->rec, last_rec, &out_buf,
486 &out_size, &out_offset);
487 *sysnop++ = e->rec->sysno;
489 e->flag = recordFlagNop;
492 case recordFlagWrite:
493 rec_release_blocks (p, e->rec->sysno);
494 rec_cache_flush_block1 (p, e->rec, last_rec, &out_buf,
495 &out_size, &out_offset);
496 *sysnop++ = e->rec->sysno;
498 e->flag = recordFlagNop;
501 case recordFlagDelete:
502 rec_delete_single (p, e->rec);
503 e->flag = recordFlagNop;
513 int csize = 0; /* indicate compression "not performed yet" */
514 compression_method = p->compression_method;
515 switch (compression_method)
517 case REC_COMPRESS_BZIP2:
519 csize = out_offset + (out_offset >> 6) + 620;
520 rec_tmp_expand (p, csize);
521 i = bzBuffToBuffCompress (p->tmp_buf+sizeof(int)+sizeof(short)+
523 &csize, out_buf, out_offset, 1, 0, 30);
526 logf (LOG_WARN, "bzBuffToBuffCompress error code=%d", i);
529 logf (LOG_LOG, "compress %4d %5d %5d", ref_count, out_offset,
533 case REC_COMPRESS_NONE:
538 /* either no compression or compression not supported ... */
540 rec_tmp_expand (p, csize);
541 memcpy (p->tmp_buf + sizeof(int) + sizeof(short) + sizeof(char),
542 out_buf, out_offset);
544 compression_method = REC_COMPRESS_NONE;
546 memcpy (p->tmp_buf + sizeof(int), &ref_count, sizeof(ref_count));
547 memcpy (p->tmp_buf + sizeof(int)+sizeof(short),
548 &compression_method, sizeof(compression_method));
550 /* -------- compression */
551 rec_write_tmp_buf (p, csize + sizeof(short) + sizeof(char), sysnos);
557 static void rec_cache_flush (Records p, int saveCount)
561 if (saveCount >= p->cache_cur)
564 rec_write_multiple (p, saveCount);
565 for (j = 0; j<saveCount; j++, i++)
566 memcpy (p->record_cache+j, p->record_cache+i,
567 sizeof(*p->record_cache));
568 p->cache_cur = saveCount;
571 static Record *rec_cache_lookup (Records p, int sysno,
572 enum recordCacheFlag flag)
575 for (i = 0; i<p->cache_cur; i++)
577 struct record_cache_entry *e = p->record_cache + i;
578 if (e->rec->sysno == sysno)
580 if (flag != recordFlagNop && e->flag == recordFlagNop)
588 static void rec_cache_insert (Records p, Record rec, enum recordCacheFlag flag)
590 struct record_cache_entry *e;
592 if (p->cache_cur == p->cache_max)
593 rec_cache_flush (p, 1);
594 else if (p->cache_cur > 0)
598 for (i = 0; i<p->cache_cur; i++)
600 Record r = (p->record_cache + i)->rec;
601 for (j = 0; j<REC_NO_INFO; j++)
605 rec_cache_flush (p, 1);
607 assert (p->cache_cur < p->cache_max);
609 e = p->record_cache + (p->cache_cur)++;
611 e->rec = rec_cp (rec);
614 void rec_close (Records *pp)
621 rec_cache_flush (p, 0);
622 xfree (p->record_cache);
628 bf_close (p->index_BFile);
630 for (i = 0; i<REC_BLOCK_TYPES; i++)
632 if (p->data_BFile[i])
633 bf_close (p->data_BFile[i]);
634 xfree (p->data_fname[i]);
642 Record rec_get (Records p, int sysno)
646 struct record_index_entry entry;
647 int freeblock, dst_type;
652 char compression_method;
657 if ((recp = rec_cache_lookup (p, sysno, recordFlagNop)))
658 return rec_cp (*recp);
660 if (!read_indx (p, sysno, &entry, sizeof(entry), 1))
661 return NULL; /* record is not there! */
664 return NULL; /* record is deleted */
666 dst_type = entry.next & 7;
667 assert (dst_type < REC_BLOCK_TYPES);
668 freeblock = entry.next / 8;
670 assert (freeblock > 0);
672 rec = (Record) xmalloc (sizeof(*rec));
673 rec_tmp_expand (p, entry.size);
676 bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
677 memcpy (&freeblock, cptr, sizeof(freeblock));
683 cptr += p->head.block_size[dst_type] - sizeof(freeblock);
685 memcpy (&tmp, cptr, sizeof(tmp));
686 bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
687 memcpy (&freeblock, cptr, sizeof(freeblock));
688 memcpy (cptr, &tmp, sizeof(tmp));
692 memcpy (&compression_method, p->tmp_buf + sizeof(int) + sizeof(short),
693 sizeof(compression_method));
694 in_buf = p->tmp_buf + sizeof(int) + sizeof(short) + sizeof(char);
695 in_size = entry.size - sizeof(short) - sizeof(char);
696 switch (compression_method)
698 case REC_COMPRESS_BZIP2:
700 bz_size = entry.size * 30+100;
701 bz_buf = (char *) xmalloc (bz_size);
702 i = bzBuffToBuffDecompress (bz_buf, &bz_size, in_buf, in_size, 0, 0);
703 logf (LOG_LOG, "decompress %5d %5d", in_size, bz_size);
706 logf (LOG_FATAL, "bzBuffToBuffDecompress error code=%d", i);
712 logf (LOG_FATAL, "cannot decompress record(s) in BZIP2 format");
716 case REC_COMPRESS_NONE:
719 for (i = 0; i<REC_NO_INFO; i++)
722 nptr = in_buf; /* skip ref count */
723 while (nptr < in_buf + in_size)
727 rec_decode_unsigned (&this_sysno, nptr, &len);
730 for (i = 0; i < REC_NO_INFO; i++)
733 rec_decode_unsigned (&this_size, nptr, &len);
738 rec->size[i] = this_size-1;
743 nptr += rec->size[i];
748 if (this_sysno == sysno)
751 for (i = 0; i<REC_NO_INFO; i++)
753 if (rec->info[i] && rec->size[i])
755 char *np = xmalloc (rec->size[i]);
756 memcpy (np, rec->info[i], rec->size[i]);
761 assert (rec->info[i] == 0);
762 assert (rec->size[i] == 0);
766 rec_cache_insert (p, rec, recordFlagNop);
770 Record rec_new (Records p)
776 rec = (Record) xmalloc (sizeof(*rec));
777 if (1 || p->head.index_free == 0)
778 sysno = (p->head.index_last)++;
781 struct record_index_entry entry;
783 read_indx (p, p->head.index_free, &entry, sizeof(entry), 0);
784 sysno = p->head.index_free;
785 p->head.index_free = entry.next;
787 (p->head.no_records)++;
789 for (i = 0; i < REC_NO_INFO; i++)
794 rec_cache_insert (p, rec, recordFlagNew);
798 void rec_del (Records p, Record *recpp)
802 (p->head.no_records)--;
803 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagDelete)))
810 rec_cache_insert (p, *recpp, recordFlagDelete);
816 void rec_put (Records p, Record *recpp)
820 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagWrite)))
827 rec_cache_insert (p, *recpp, recordFlagWrite);
833 void rec_rm (Record *recpp)
839 for (i = 0; i < REC_NO_INFO; i++)
840 xfree ((*recpp)->info[i]);
845 Record rec_cp (Record rec)
850 n = (Record) xmalloc (sizeof(*n));
851 n->sysno = rec->sysno;
852 for (i = 0; i < REC_NO_INFO; i++)
860 n->size[i] = rec->size[i];
861 n->info[i] = (char *) xmalloc (rec->size[i]);
862 memcpy (n->info[i], rec->info[i], rec->size[i]);
868 char *rec_strdup (const char *s, size_t *len)
878 p = (char *) xmalloc (*len);