2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.26 1999-07-06 13:34:57 adam
8 * Fixed bug (introduced by previous commit).
10 * Revision 1.25 1999/07/06 12:28:04 adam
11 * Updated record index structure. Format includes version ID. Compression
12 * algorithm ID is stored for each record block.
14 * Revision 1.24 1999/06/25 13:48:02 adam
15 * Updated MSVC project files.
16 * Added BZIP2 record compression (not very well tested).
18 * Revision 1.23 1999/05/26 07:49:13 adam
21 * Revision 1.22 1999/02/18 12:49:34 adam
22 * Changed file naming scheme for register files as well as record
25 * Revision 1.21 1999/02/02 14:51:03 adam
26 * Updated WIN32 code specific sections. Changed header.
28 * Revision 1.20 1998/01/12 15:04:08 adam
29 * The test option (-s) only uses read-lock (and not write lock).
31 * Revision 1.19 1997/09/17 12:19:16 adam
32 * Zebra version corresponds to YAZ version 1.4.
33 * Changed Zebra server so that it doesn't depend on global common_resource.
35 * Revision 1.18 1997/07/15 16:28:42 adam
36 * Bug fix: storeData didn't work with files with multiple records.
37 * Bug fix: fixed memory management with records; not really well
40 * Revision 1.17 1997/02/12 20:39:46 adam
41 * Implemented options -f <n> that limits the log to the first <n>
43 * Changed some log messages also.
45 * Revision 1.16 1996/06/04 10:19:00 adam
46 * Minor changes - removed include of ctype.h.
48 * Revision 1.15 1996/05/13 14:23:06 adam
49 * Work on compaction of set/use bytes in dictionary.
51 * Revision 1.14 1996/02/01 20:48:15 adam
52 * The total size of records are always checked in rec_cache_insert to
53 * reduce memory usage.
55 * Revision 1.13 1995/12/11 09:12:49 adam
56 * The rec_get function returns NULL if record doesn't exist - will
57 * happen in the server if the result set records have been deleted since
58 * the creation of the set (i.e. the search).
59 * The server saves a result temporarily if it is 'volatile', i.e. the
60 * set is register dependent.
62 * Revision 1.12 1995/12/07 17:38:47 adam
63 * Work locking mechanisms for concurrent updates/commit.
65 * Revision 1.11 1995/12/06 13:58:26 adam
66 * Improved flushing of records - all flushes except the last one
67 * don't write the last accessed. Also flush takes place if record
68 * info occupy more than about 256k.
70 * Revision 1.10 1995/12/06 12:41:24 adam
71 * New command 'stat' for the index program.
72 * Filenames can be read from stdin by specifying '-'.
73 * Bug fix/enhancement of the transformation from terms to regular
74 * expressons in the search engine.
76 * Revision 1.9 1995/11/30 08:34:33 adam
77 * Started work on commit facility.
78 * Changed a few malloc/free to xmalloc/xfree.
80 * Revision 1.8 1995/11/28 14:26:21 adam
81 * Bug fix: recordId with constant wasn't right.
82 * Bug fix: recordId dictionary entry wasn't deleted when needed.
84 * Revision 1.7 1995/11/28 09:09:43 adam
85 * Zebra config renamed.
86 * Use setting 'recordId' to identify record now.
87 * Bug fix in recindex.c: rec_release_blocks was invokeded even
88 * though the blocks were already released.
89 * File traversal properly deletes records when needed.
91 * Revision 1.6 1995/11/25 10:24:06 adam
92 * More record fields - they are enumerated now.
93 * New options: flagStoreData flagStoreKey.
95 * Revision 1.5 1995/11/22 17:19:18 adam
96 * Record management uses the bfile system.
98 * Revision 1.4 1995/11/20 16:59:46 adam
99 * New update method: the 'old' keys are saved for each records.
101 * Revision 1.3 1995/11/16 15:34:55 adam
102 * Uses new record management system in both indexer and server.
104 * Revision 1.2 1995/11/15 19:13:08 adam
105 * Work on record management.
107 * Revision 1.1 1995/11/15 14:46:20 adam
108 * Started work on better record management system.
114 * Format of first block
116 * ref_count (4 bytes)
119 * Format of subsequent blocks
123 * Format of each record
125 * (length, data) - pairs
126 * length = 0 if same as previous
132 #include "recindxp.h"
137 static void rec_write_head (Records p)
142 assert (p->index_BFile);
144 r = bf_write (p->index_BFile, 0, 0, sizeof(p->head), &p->head);
147 logf (LOG_FATAL|LOG_ERRNO, "write head of %s", p->index_fname);
152 static void rec_tmp_expand (Records p, int size)
154 if (p->tmp_size < size + 2048 ||
155 p->tmp_size < p->head.block_size[REC_BLOCK_TYPES-1]*2)
158 p->tmp_size = size + p->head.block_size[REC_BLOCK_TYPES-1]*2 + 2048;
159 p->tmp_buf = (char *) xmalloc (p->tmp_size);
163 static int read_indx (Records p, int sysno, void *buf, int itemsize,
167 int pos = (sysno-1)*itemsize;
169 r = bf_read (p->index_BFile, 1+pos/128, pos%128, itemsize, buf);
170 if (r != 1 && !ignoreError)
172 logf (LOG_FATAL|LOG_ERRNO, "read in %s at pos %ld",
173 p->index_fname, (long) pos);
179 static void write_indx (Records p, int sysno, void *buf, int itemsize)
181 int pos = (sysno-1)*itemsize;
183 bf_write (p->index_BFile, 1+pos/128, pos%128, itemsize, buf);
186 static void rec_release_blocks (Records p, int sysno)
188 struct record_index_entry entry;
190 char block_and_ref[sizeof(short) + sizeof(int)];
194 if (read_indx (p, sysno, &entry, sizeof(entry), 1) != 1)
197 freeblock = entry.next;
198 assert (freeblock > 0);
199 dst_type = freeblock & 7;
200 assert (dst_type < REC_BLOCK_TYPES);
201 freeblock = freeblock / 8;
204 if (bf_read (p->data_BFile[dst_type], freeblock, 0,
205 sizeof(block_and_ref), block_and_ref) != 1)
207 logf (LOG_FATAL|LOG_ERRNO, "read in rec_del_single");
213 memcpy (&ref, block_and_ref + sizeof(int), sizeof(ref));
215 memcpy (block_and_ref + sizeof(int), &ref, sizeof(ref));
218 if (bf_write (p->data_BFile[dst_type], freeblock, 0,
219 sizeof(block_and_ref), block_and_ref))
221 logf (LOG_FATAL|LOG_ERRNO, "write in rec_del_single");
229 if (bf_write (p->data_BFile[dst_type], freeblock, 0, sizeof(freeblock),
230 &p->head.block_free[dst_type]))
232 logf (LOG_FATAL|LOG_ERRNO, "write in rec_del_single");
235 p->head.block_free[dst_type] = freeblock;
236 memcpy (&freeblock, block_and_ref, sizeof(int));
238 p->head.block_used[dst_type]--;
240 p->head.total_bytes -= entry.size;
243 static void rec_delete_single (Records p, Record rec)
245 struct record_index_entry entry;
247 rec_release_blocks (p, rec->sysno);
249 entry.next = p->head.index_free;
251 p->head.index_free = rec->sysno;
252 write_indx (p, rec->sysno, &entry, sizeof(entry));
255 static void rec_write_tmp_buf (Records p, int size, int *sysnos)
257 struct record_index_entry entry;
259 char *cptr = p->tmp_buf;
260 int block_prev = -1, block_free;
264 for (i = 1; i<REC_BLOCK_TYPES; i++)
265 if (size >= p->head.block_move[i])
267 while (no_written < size)
269 block_free = p->head.block_free[dst_type];
272 if (bf_read (p->data_BFile[dst_type],
273 block_free, 0, sizeof(*p->head.block_free),
274 &p->head.block_free[dst_type]) != 1)
276 logf (LOG_FATAL|LOG_ERRNO, "read in %s at free block %d",
277 p->data_fname[dst_type], block_free);
282 block_free = p->head.block_last[dst_type]++;
283 if (block_prev == -1)
285 entry.next = block_free*8 + dst_type;
287 p->head.total_bytes += size;
290 write_indx (p, *sysnos, &entry, sizeof(entry));
296 memcpy (cptr, &block_free, sizeof(int));
297 bf_write (p->data_BFile[dst_type], block_prev, 0, 0, cptr);
298 cptr = p->tmp_buf + no_written;
300 block_prev = block_free;
301 no_written += p->head.block_size[dst_type] - sizeof(int);
302 p->head.block_used[dst_type]++;
304 assert (block_prev != -1);
306 memcpy (cptr, &block_free, sizeof(int));
307 bf_write (p->data_BFile[dst_type], block_prev, 0,
308 sizeof(int) + (p->tmp_buf+size) - cptr, cptr);
311 Records rec_open (BFiles bfs, int rw, int compression_method)
317 p = (Records) xmalloc (sizeof(*p));
318 p->compression_method = compression_method;
321 p->tmp_buf = (char *) xmalloc (p->tmp_size);
322 p->index_fname = "reci";
323 p->index_BFile = bf_open (bfs, p->index_fname, 128, rw);
324 if (p->index_BFile == NULL)
326 logf (LOG_FATAL|LOG_ERRNO, "open %s", p->index_fname);
329 r = bf_read (p->index_BFile, 0, 0, 0, p->tmp_buf);
333 memcpy (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic));
334 sprintf (p->head.version, "%3d", REC_VERSION);
335 p->head.index_free = 0;
336 p->head.index_last = 1;
337 p->head.no_records = 0;
338 p->head.total_bytes = 0;
339 for (i = 0; i<REC_BLOCK_TYPES; i++)
341 p->head.block_free[i] = 0;
342 p->head.block_last[i] = 1;
343 p->head.block_used[i] = 0;
345 p->head.block_size[0] = 128;
346 p->head.block_move[0] = 0;
347 for (i = 1; i<REC_BLOCK_TYPES; i++)
349 p->head.block_size[i] = p->head.block_size[i-1] * 4;
350 p->head.block_move[i] = p->head.block_size[i] * 24;
356 memcpy (&p->head, p->tmp_buf, sizeof(p->head));
357 if (memcmp (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic)))
359 logf (LOG_FATAL, "file %s has bad format", p->index_fname);
362 version = atoi (p->head.version);
363 if (version != REC_VERSION)
365 logf (LOG_FATAL, "file %s is version %d, but version"
366 " %d is required", p->index_fname, version, REC_VERSION);
371 for (i = 0; i<REC_BLOCK_TYPES; i++)
374 sprintf (str, "recd%c", i + 'A');
375 p->data_fname[i] = (char *) xmalloc (strlen(str)+1);
376 strcpy (p->data_fname[i], str);
377 p->data_BFile[i] = NULL;
379 for (i = 0; i<REC_BLOCK_TYPES; i++)
381 if (!(p->data_BFile[i] = bf_open (bfs, p->data_fname[i],
382 p->head.block_size[i],
385 logf (LOG_FATAL|LOG_ERRNO, "bf_open %s", p->data_fname[i]);
391 p->record_cache = (struct record_cache_entry *)
392 xmalloc (sizeof(*p->record_cache)*p->cache_max);
396 static void rec_encode_unsigned (unsigned n, unsigned char *buf, int *len)
401 buf[*len] = 128 + (n & 127);
409 static void rec_decode_unsigned(unsigned *np, unsigned char *buf, int *len)
415 while (buf[*len] > 127)
417 n += w*(buf[*len] & 127);
426 static void rec_cache_flush_block1 (Records p, Record rec, Record last_rec,
427 char **out_buf, int *out_size,
433 for (i = 0; i<REC_NO_INFO; i++)
435 if (*out_offset + (int) rec->size[i] + 20 > *out_size)
437 int new_size = *out_offset + rec->size[i] + 65536;
438 char *np = (char *) xmalloc (new_size);
440 memcpy (np, *out_buf, *out_offset);
442 *out_size = new_size;
447 rec_encode_unsigned (rec->sysno, *out_buf + *out_offset, &len);
448 (*out_offset) += len;
450 if (rec->size[i] == 0)
452 rec_encode_unsigned (1, *out_buf + *out_offset, &len);
453 (*out_offset) += len;
455 else if (last_rec && rec->size[i] == last_rec->size[i] &&
456 !memcmp (rec->info[i], last_rec->info[i], rec->size[i]))
458 rec_encode_unsigned (0, *out_buf + *out_offset, &len);
459 (*out_offset) += len;
463 rec_encode_unsigned (rec->size[i]+1, *out_buf + *out_offset, &len);
464 (*out_offset) += len;
465 memcpy (*out_buf + *out_offset, rec->info[i], rec->size[i]);
466 (*out_offset) += rec->size[i];
471 static void rec_write_multiple (Records p, int saveCount)
475 char compression_method;
479 char *out_buf = (char *) xmalloc (out_size);
480 int *sysnos = (int *) xmalloc (sizeof(*sysnos) * (p->cache_cur + 1));
481 int *sysnop = sysnos;
483 for (i = 0; i<p->cache_cur - saveCount; i++)
485 struct record_cache_entry *e = p->record_cache + i;
489 rec_cache_flush_block1 (p, e->rec, last_rec, &out_buf,
490 &out_size, &out_offset);
491 *sysnop++ = e->rec->sysno;
493 e->flag = recordFlagNop;
496 case recordFlagWrite:
497 rec_release_blocks (p, e->rec->sysno);
498 rec_cache_flush_block1 (p, e->rec, last_rec, &out_buf,
499 &out_size, &out_offset);
500 *sysnop++ = e->rec->sysno;
502 e->flag = recordFlagNop;
505 case recordFlagDelete:
506 rec_delete_single (p, e->rec);
507 e->flag = recordFlagNop;
517 int csize = 0; /* indicate compression "not performed yet" */
518 compression_method = p->compression_method;
519 switch (compression_method)
521 case REC_COMPRESS_BZIP2:
523 csize = out_offset + (out_offset >> 6) + 620;
524 rec_tmp_expand (p, csize);
525 i = bzBuffToBuffCompress (p->tmp_buf+sizeof(int)+sizeof(short)+
527 &csize, out_buf, out_offset, 1, 0, 30);
530 logf (LOG_WARN, "bzBuffToBuffCompress error code=%d", i);
533 logf (LOG_LOG, "compress %4d %5d %5d", ref_count, out_offset,
537 case REC_COMPRESS_NONE:
542 /* either no compression or compression not supported ... */
544 rec_tmp_expand (p, csize);
545 memcpy (p->tmp_buf + sizeof(int) + sizeof(short) + sizeof(char),
546 out_buf, out_offset);
548 compression_method = REC_COMPRESS_NONE;
550 memcpy (p->tmp_buf + sizeof(int), &ref_count, sizeof(ref_count));
551 memcpy (p->tmp_buf + sizeof(int)+sizeof(short),
552 &compression_method, sizeof(compression_method));
554 /* -------- compression */
555 rec_write_tmp_buf (p, csize + sizeof(short) + sizeof(char), sysnos);
561 static void rec_cache_flush (Records p, int saveCount)
565 if (saveCount >= p->cache_cur)
568 rec_write_multiple (p, saveCount);
570 for (i = 0; i<p->cache_cur - saveCount; i++)
572 struct record_cache_entry *e = p->record_cache + i;
575 /* i still being used ... */
576 for (j = 0; j<saveCount; j++, i++)
577 memcpy (p->record_cache+j, p->record_cache+i,
578 sizeof(*p->record_cache));
579 p->cache_cur = saveCount;
582 static Record *rec_cache_lookup (Records p, int sysno,
583 enum recordCacheFlag flag)
586 for (i = 0; i<p->cache_cur; i++)
588 struct record_cache_entry *e = p->record_cache + i;
589 if (e->rec->sysno == sysno)
591 if (flag != recordFlagNop && e->flag == recordFlagNop)
599 static void rec_cache_insert (Records p, Record rec, enum recordCacheFlag flag)
601 struct record_cache_entry *e;
603 if (p->cache_cur == p->cache_max)
604 rec_cache_flush (p, 1);
605 else if (p->cache_cur > 0)
609 for (i = 0; i<p->cache_cur; i++)
611 Record r = (p->record_cache + i)->rec;
612 for (j = 0; j<REC_NO_INFO; j++)
616 rec_cache_flush (p, 1);
618 assert (p->cache_cur < p->cache_max);
620 e = p->record_cache + (p->cache_cur)++;
622 e->rec = rec_cp (rec);
625 void rec_close (Records *pp)
632 rec_cache_flush (p, 0);
633 xfree (p->record_cache);
639 bf_close (p->index_BFile);
641 for (i = 0; i<REC_BLOCK_TYPES; i++)
643 if (p->data_BFile[i])
644 bf_close (p->data_BFile[i]);
645 xfree (p->data_fname[i]);
653 Record rec_get (Records p, int sysno)
657 struct record_index_entry entry;
658 int freeblock, dst_type;
663 char compression_method;
668 if ((recp = rec_cache_lookup (p, sysno, recordFlagNop)))
669 return rec_cp (*recp);
671 if (!read_indx (p, sysno, &entry, sizeof(entry), 1))
672 return NULL; /* record is not there! */
675 return NULL; /* record is deleted */
677 dst_type = entry.next & 7;
678 assert (dst_type < REC_BLOCK_TYPES);
679 freeblock = entry.next / 8;
681 assert (freeblock > 0);
683 rec = (Record) xmalloc (sizeof(*rec));
684 rec_tmp_expand (p, entry.size);
687 bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
688 memcpy (&freeblock, cptr, sizeof(freeblock));
694 cptr += p->head.block_size[dst_type] - sizeof(freeblock);
696 memcpy (&tmp, cptr, sizeof(tmp));
697 bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
698 memcpy (&freeblock, cptr, sizeof(freeblock));
699 memcpy (cptr, &tmp, sizeof(tmp));
703 memcpy (&compression_method, p->tmp_buf + sizeof(int) + sizeof(short),
704 sizeof(compression_method));
705 in_buf = p->tmp_buf + sizeof(int) + sizeof(short) + sizeof(char);
706 in_size = entry.size - sizeof(short) - sizeof(char);
707 switch (compression_method)
709 case REC_COMPRESS_BZIP2:
711 bz_size = entry.size * 30+100;
712 bz_buf = (char *) xmalloc (bz_size);
713 i = bzBuffToBuffDecompress (bz_buf, &bz_size, in_buf, in_size, 0, 0);
714 logf (LOG_LOG, "decompress %5d %5d", in_size, bz_size);
717 logf (LOG_FATAL, "bzBuffToBuffDecompress error code=%d", i);
723 logf (LOG_FATAL, "cannot decompress record(s) in BZIP2 format");
727 case REC_COMPRESS_NONE:
730 for (i = 0; i<REC_NO_INFO; i++)
733 nptr = in_buf; /* skip ref count */
734 while (nptr < in_buf + in_size)
738 rec_decode_unsigned (&this_sysno, nptr, &len);
741 for (i = 0; i < REC_NO_INFO; i++)
744 rec_decode_unsigned (&this_size, nptr, &len);
749 rec->size[i] = this_size-1;
754 nptr += rec->size[i];
759 if (this_sysno == sysno)
762 for (i = 0; i<REC_NO_INFO; i++)
764 if (rec->info[i] && rec->size[i])
766 char *np = xmalloc (rec->size[i]);
767 memcpy (np, rec->info[i], rec->size[i]);
772 assert (rec->info[i] == 0);
773 assert (rec->size[i] == 0);
777 rec_cache_insert (p, rec, recordFlagNop);
781 Record rec_new (Records p)
787 rec = (Record) xmalloc (sizeof(*rec));
788 if (1 || p->head.index_free == 0)
789 sysno = (p->head.index_last)++;
792 struct record_index_entry entry;
794 read_indx (p, p->head.index_free, &entry, sizeof(entry), 0);
795 sysno = p->head.index_free;
796 p->head.index_free = entry.next;
798 (p->head.no_records)++;
800 for (i = 0; i < REC_NO_INFO; i++)
805 rec_cache_insert (p, rec, recordFlagNew);
809 void rec_del (Records p, Record *recpp)
813 (p->head.no_records)--;
814 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagDelete)))
821 rec_cache_insert (p, *recpp, recordFlagDelete);
827 void rec_put (Records p, Record *recpp)
831 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagWrite)))
838 rec_cache_insert (p, *recpp, recordFlagWrite);
844 void rec_rm (Record *recpp)
850 for (i = 0; i < REC_NO_INFO; i++)
851 xfree ((*recpp)->info[i]);
856 Record rec_cp (Record rec)
861 n = (Record) xmalloc (sizeof(*n));
862 n->sysno = rec->sysno;
863 for (i = 0; i < REC_NO_INFO; i++)
871 n->size[i] = rec->size[i];
872 n->info[i] = (char *) xmalloc (rec->size[i]);
873 memcpy (n->info[i], rec->info[i], rec->size[i]);
879 char *rec_strdup (const char *s, size_t *len)
889 p = (char *) xmalloc (*len);