2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.24 1999-06-25 13:48:02 adam
8 * Updated MSVC project files.
9 * Added BZIP2 record compression (not very well tested).
11 * Revision 1.23 1999/05/26 07:49:13 adam
14 * Revision 1.22 1999/02/18 12:49:34 adam
15 * Changed file naming scheme for register files as well as record
18 * Revision 1.21 1999/02/02 14:51:03 adam
19 * Updated WIN32 code specific sections. Changed header.
21 * Revision 1.20 1998/01/12 15:04:08 adam
22 * The test option (-s) only uses read-lock (and not write lock).
24 * Revision 1.19 1997/09/17 12:19:16 adam
25 * Zebra version corresponds to YAZ version 1.4.
26 * Changed Zebra server so that it doesn't depend on global common_resource.
28 * Revision 1.18 1997/07/15 16:28:42 adam
29 * Bug fix: storeData didn't work with files with multiple records.
30 * Bug fix: fixed memory management with records; not really well
33 * Revision 1.17 1997/02/12 20:39:46 adam
34 * Implemented options -f <n> that limits the log to the first <n>
36 * Changed some log messages also.
38 * Revision 1.16 1996/06/04 10:19:00 adam
39 * Minor changes - removed include of ctype.h.
41 * Revision 1.15 1996/05/13 14:23:06 adam
42 * Work on compaction of set/use bytes in dictionary.
44 * Revision 1.14 1996/02/01 20:48:15 adam
45 * The total size of records are always checked in rec_cache_insert to
46 * reduce memory usage.
48 * Revision 1.13 1995/12/11 09:12:49 adam
49 * The rec_get function returns NULL if record doesn't exist - will
50 * happen in the server if the result set records have been deleted since
51 * the creation of the set (i.e. the search).
52 * The server saves a result temporarily if it is 'volatile', i.e. the
53 * set is register dependent.
55 * Revision 1.12 1995/12/07 17:38:47 adam
56 * Work locking mechanisms for concurrent updates/commit.
58 * Revision 1.11 1995/12/06 13:58:26 adam
59 * Improved flushing of records - all flushes except the last one
60 * don't write the last accessed. Also flush takes place if record
61 * info occupy more than about 256k.
63 * Revision 1.10 1995/12/06 12:41:24 adam
64 * New command 'stat' for the index program.
65 * Filenames can be read from stdin by specifying '-'.
66 * Bug fix/enhancement of the transformation from terms to regular
67 * expressons in the search engine.
69 * Revision 1.9 1995/11/30 08:34:33 adam
70 * Started work on commit facility.
71 * Changed a few malloc/free to xmalloc/xfree.
73 * Revision 1.8 1995/11/28 14:26:21 adam
74 * Bug fix: recordId with constant wasn't right.
75 * Bug fix: recordId dictionary entry wasn't deleted when needed.
77 * Revision 1.7 1995/11/28 09:09:43 adam
78 * Zebra config renamed.
79 * Use setting 'recordId' to identify record now.
80 * Bug fix in recindex.c: rec_release_blocks was invokeded even
81 * though the blocks were already released.
82 * File traversal properly deletes records when needed.
84 * Revision 1.6 1995/11/25 10:24:06 adam
85 * More record fields - they are enumerated now.
86 * New options: flagStoreData flagStoreKey.
88 * Revision 1.5 1995/11/22 17:19:18 adam
89 * Record management uses the bfile system.
91 * Revision 1.4 1995/11/20 16:59:46 adam
92 * New update method: the 'old' keys are saved for each records.
94 * Revision 1.3 1995/11/16 15:34:55 adam
95 * Uses new record management system in both indexer and server.
97 * Revision 1.2 1995/11/15 19:13:08 adam
98 * Work on record management.
100 * Revision 1.1 1995/11/15 14:46:20 adam
101 * Started work on better record management system.
107 * Format of first block
109 * ref_count (4 bytes)
112 * Format of subsequent blocks
116 * Format of each record
118 * (length, data) - pairs
119 * length = 0 if same as previous
125 #include "recindxp.h"
130 static void rec_write_head (Records p)
135 assert (p->index_BFile);
137 r = bf_write (p->index_BFile, 0, 0, sizeof(p->head), &p->head);
140 logf (LOG_FATAL|LOG_ERRNO, "write head of %s", p->index_fname);
145 static void rec_tmp_expand (Records p, int size)
147 if (p->tmp_size < size + 2048 ||
148 p->tmp_size < p->head.block_size[REC_BLOCK_TYPES-1]*2)
151 p->tmp_size = size + p->head.block_size[REC_BLOCK_TYPES-1]*2 + 2048;
152 p->tmp_buf = (char *) xmalloc (p->tmp_size);
156 static int read_indx (Records p, int sysno, void *buf, int itemsize,
160 int pos = (sysno-1)*itemsize;
162 r = bf_read (p->index_BFile, 1+pos/128, pos%128, itemsize, buf);
163 if (r != 1 && !ignoreError)
165 logf (LOG_FATAL|LOG_ERRNO, "read in %s at pos %ld",
166 p->index_fname, (long) pos);
172 static void write_indx (Records p, int sysno, void *buf, int itemsize)
174 int pos = (sysno-1)*itemsize;
176 bf_write (p->index_BFile, 1+pos/128, pos%128, itemsize, buf);
179 static void rec_release_blocks (Records p, int sysno)
181 struct record_index_entry entry;
183 int block_and_ref[2];
187 logf (LOG_LOG, "release_blocks for sysno=%d", sysno);
188 if (read_indx (p, sysno, &entry, sizeof(entry), 1) != 1)
191 freeblock = entry.next;
192 assert (freeblock > 0);
193 dst_type = freeblock & 7;
194 assert (dst_type < REC_BLOCK_TYPES);
195 freeblock = freeblock / 8;
198 if (bf_read (p->data_BFile[dst_type], freeblock, 0,
199 sizeof(block_and_ref), block_and_ref) != 1)
201 logf (LOG_FATAL|LOG_ERRNO, "read in rec_del_single");
207 if (block_and_ref[1])
209 if (bf_write (p->data_BFile[dst_type], freeblock, 0,
210 sizeof(block_and_ref), block_and_ref))
212 logf (LOG_FATAL|LOG_ERRNO, "write in rec_del_single");
220 if (bf_write (p->data_BFile[dst_type], freeblock, 0, sizeof(freeblock),
221 &p->head.block_free[dst_type]))
223 logf (LOG_FATAL|LOG_ERRNO, "write in rec_del_single");
226 p->head.block_free[dst_type] = freeblock;
227 freeblock = block_and_ref[0];
229 p->head.block_used[dst_type]--;
231 p->head.total_bytes -= entry.size;
234 static void rec_delete_single (Records p, Record rec)
236 struct record_index_entry entry;
238 rec_release_blocks (p, rec->sysno);
240 entry.next = p->head.index_free;
242 p->head.index_free = rec->sysno;
243 write_indx (p, rec->sysno, &entry, sizeof(entry));
246 static void rec_write_tmp_buf (Records p, int size, int *sysnos);
248 static void rec_write_single (Records p, Record rec)
255 logf (LOG_LOG, " rec_write_single !!!!!!!!!!!!!!!!!!!!!!!!!!!!");
256 for (i = 0; i < REC_NO_INFO; i++)
258 size += sizeof(*rec->size);
260 size += sizeof(*rec->size) + rec->size[i];
262 rec_tmp_expand (p, size);
264 cptr = p->tmp_buf + sizeof(int); /* a hack! */
265 for (i = 0; i < REC_NO_INFO; i++)
267 memcpy (cptr, &rec->size[i], sizeof(*rec->size));
268 cptr += sizeof(*rec->size);
271 memcpy (cptr, rec->info[i], rec->size[i]);
272 cptr += rec->size[i];
275 sysnos[0] = rec->sysno;
277 rec_write_tmp_buf (p, size, sysnos);
280 static void rec_write_tmp_buf (Records p, int size, int *sysnos)
282 struct record_index_entry entry;
284 char *cptr = p->tmp_buf;
285 int block_prev = -1, block_free;
289 for (i = 1; i<REC_BLOCK_TYPES; i++)
290 if (size >= p->head.block_move[i])
292 while (no_written < size)
294 block_free = p->head.block_free[dst_type];
297 if (bf_read (p->data_BFile[dst_type],
298 block_free, 0, sizeof(*p->head.block_free),
299 &p->head.block_free[dst_type]) != 1)
301 logf (LOG_FATAL|LOG_ERRNO, "read in %s at free block %d",
302 p->data_fname[dst_type], block_free);
307 block_free = p->head.block_last[dst_type]++;
308 if (block_prev == -1)
310 entry.next = block_free*8 + dst_type;
312 p->head.total_bytes += size;
315 write_indx (p, *sysnos, &entry, sizeof(entry));
321 memcpy (cptr, &block_free, sizeof(int));
322 bf_write (p->data_BFile[dst_type], block_prev, 0, 0, cptr);
323 cptr = p->tmp_buf + no_written;
325 block_prev = block_free;
326 no_written += p->head.block_size[dst_type] - sizeof(int);
327 p->head.block_used[dst_type]++;
329 assert (block_prev != -1);
331 memcpy (cptr, &block_free, sizeof(int));
332 bf_write (p->data_BFile[dst_type], block_prev, 0,
333 sizeof(int) + (p->tmp_buf+size) - cptr, cptr);
336 static void rec_update_single (Records p, Record rec)
338 rec_release_blocks (p, rec->sysno);
339 rec_write_single (p, rec);
342 Records rec_open (BFiles bfs, int rw)
347 p = (Records) xmalloc (sizeof(*p));
350 p->tmp_buf = (char *) xmalloc (p->tmp_size);
351 p->index_fname = "reci";
352 p->index_BFile = bf_open (bfs, p->index_fname, 128, rw);
353 if (p->index_BFile == NULL)
355 logf (LOG_FATAL|LOG_ERRNO, "open %s", p->index_fname);
358 r = bf_read (p->index_BFile, 0, 0, 0, p->tmp_buf);
362 memcpy (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic));
363 p->head.index_free = 0;
364 p->head.index_last = 1;
365 p->head.no_records = 0;
366 p->head.total_bytes = 0;
367 for (i = 0; i<REC_BLOCK_TYPES; i++)
369 p->head.block_free[i] = 0;
370 p->head.block_last[i] = 1;
371 p->head.block_used[i] = 0;
373 p->head.block_size[0] = 128;
374 p->head.block_move[0] = 0;
375 for (i = 1; i<REC_BLOCK_TYPES; i++)
377 p->head.block_size[i] = p->head.block_size[i-1] * 4;
378 p->head.block_move[i] = p->head.block_size[i] * 3;
384 memcpy (&p->head, p->tmp_buf, sizeof(p->head));
385 if (memcmp (p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic)))
387 logf (LOG_FATAL, "read %s. bad header", p->index_fname);
392 for (i = 0; i<REC_BLOCK_TYPES; i++)
395 sprintf (str, "recd%c", i + 'A');
396 p->data_fname[i] = (char *) xmalloc (strlen(str)+1);
397 strcpy (p->data_fname[i], str);
398 p->data_BFile[i] = NULL;
400 for (i = 0; i<REC_BLOCK_TYPES; i++)
402 if (!(p->data_BFile[i] = bf_open (bfs, p->data_fname[i],
403 p->head.block_size[i],
406 logf (LOG_FATAL|LOG_ERRNO, "bf_open %s", p->data_fname[i]);
412 p->record_cache = (struct record_cache_entry *)
413 xmalloc (sizeof(*p->record_cache)*p->cache_max);
417 static void rec_encode_unsigned (unsigned n, unsigned char *buf, int *len)
422 buf[*len] = 128 + (n & 127);
430 static void rec_decode_unsigned(unsigned *np, unsigned char *buf, int *len)
436 while (buf[*len] > 127)
438 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)
498 char *out_buf = (char *) xmalloc (out_size);
499 int *sysnos = (int *) xmalloc (sizeof(*sysnos) * (p->cache_cur + 1));
500 int *sysnop = sysnos;
502 for (i = 0; i<p->cache_cur - saveCount; i++)
504 struct record_cache_entry *e = p->record_cache + i;
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 recordFlagWrite:
516 rec_release_blocks (p, e->rec->sysno);
517 rec_cache_flush_block1 (p, e->rec, last_rec, &out_buf,
518 &out_size, &out_offset);
519 *sysnop++ = e->rec->sysno;
521 e->flag = recordFlagNop;
531 int csize = out_offset + (out_offset >> 6) + 620;
533 rec_tmp_expand (p, csize);
535 i = bzBuffToBuffCompress (p->tmp_buf+2*sizeof(int), &csize,
536 out_buf, out_offset, 9, 0, 30);
539 logf (LOG_FATAL, "bzBuffToCompress error code=%d", i);
543 memcpy (p->tmp_buf + 2*sizeof(int), out_buf, out_offset);
546 memcpy (p->tmp_buf + sizeof(int), &ref_count, sizeof(ref_count));
547 /* -------- compression */
548 rec_write_tmp_buf (p, csize + sizeof(int), sysnos);
554 static void rec_cache_flush (Records p, int saveCount)
558 if (saveCount >= p->cache_cur)
561 rec_write_multiple (p, saveCount);
562 for (i = 0; i<p->cache_cur - saveCount; i++)
564 struct record_cache_entry *e = p->record_cache + i;
570 rec_write_single (p, e->rec);
572 case recordFlagWrite:
573 rec_update_single (p, e->rec);
575 case recordFlagDelete:
576 rec_delete_single (p, e->rec);
581 for (j = 0; j<saveCount; j++, i++)
582 memcpy (p->record_cache+j, p->record_cache+i,
583 sizeof(*p->record_cache));
584 p->cache_cur = saveCount;
587 static Record *rec_cache_lookup (Records p, int sysno,
588 enum recordCacheFlag flag)
591 for (i = 0; i<p->cache_cur; i++)
593 struct record_cache_entry *e = p->record_cache + i;
594 if (e->rec->sysno == sysno)
596 if (flag != recordFlagNop && e->flag == recordFlagNop)
604 static void rec_cache_insert (Records p, Record rec, enum recordCacheFlag flag)
606 struct record_cache_entry *e;
608 if (p->cache_cur == p->cache_max)
609 rec_cache_flush (p, 1);
610 else if (p->cache_cur > 0)
614 for (i = 0; i<p->cache_cur; i++)
616 Record r = (p->record_cache + i)->rec;
617 for (j = 0; j<REC_NO_INFO; j++)
621 rec_cache_flush (p, 1);
623 assert (p->cache_cur < p->cache_max);
625 e = p->record_cache + (p->cache_cur)++;
627 e->rec = rec_cp (rec);
630 void rec_close (Records *pp)
637 rec_cache_flush (p, 0);
638 xfree (p->record_cache);
644 bf_close (p->index_BFile);
646 for (i = 0; i<REC_BLOCK_TYPES; i++)
648 if (p->data_BFile[i])
649 bf_close (p->data_BFile[i]);
650 xfree (p->data_fname[i]);
658 Record rec_get (Records p, int sysno)
662 struct record_index_entry entry;
663 int freeblock, dst_type;
670 if ((recp = rec_cache_lookup (p, sysno, recordFlagNop)))
671 return rec_cp (*recp);
673 if (!read_indx (p, sysno, &entry, sizeof(entry), 1))
674 return NULL; /* record is not there! */
677 return NULL; /* record is deleted */
679 dst_type = entry.next & 7;
680 assert (dst_type < REC_BLOCK_TYPES);
681 freeblock = entry.next / 8;
683 assert (freeblock > 0);
685 rec = (Record) xmalloc (sizeof(*rec));
686 rec_tmp_expand (p, entry.size);
689 bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
690 memcpy (&freeblock, cptr, sizeof(freeblock));
696 cptr += p->head.block_size[dst_type] - sizeof(freeblock);
698 memcpy (&tmp, cptr, sizeof(tmp));
699 bf_read (p->data_BFile[dst_type], freeblock, 0, 0, cptr);
700 memcpy (&freeblock, cptr, sizeof(freeblock));
701 memcpy (cptr, &tmp, sizeof(tmp));
706 in_size = entry.size * 30+100;
707 in_buf = (char *) xmalloc (in_size);
708 i = bzBuffToBuffDecompress (in_buf, &in_size, p->tmp_buf+2*sizeof(int),
709 entry.size-sizeof(int), 0, 4);
712 logf (LOG_FATAL, "bzBuffToDecompress error code=%d", i);
716 in_buf = p->tmp_buf + 2*sizeof(int);
717 in_size = entry.size - sizeof(int);
719 nptr = in_buf; /* skip ref count */
720 while (nptr < in_buf + in_size)
724 rec_decode_unsigned (&this_sysno, nptr, &len);
727 for (i = 0; i < REC_NO_INFO; i++)
730 rec_decode_unsigned (&this_size, nptr, &len);
735 rec->size[i] = this_size-1;
739 rec->info[i] = (char *) xmalloc (rec->size[i]);
740 memcpy (rec->info[i], nptr, rec->size[i]);
741 nptr += rec->size[i];
746 if (this_sysno == sysno)
750 rec_cache_insert (p, rec, recordFlagNop);
754 Record rec_new (Records p)
760 rec = (Record) xmalloc (sizeof(*rec));
761 if (1 || p->head.index_free == 0)
762 sysno = (p->head.index_last)++;
765 struct record_index_entry entry;
767 read_indx (p, p->head.index_free, &entry, sizeof(entry), 0);
768 sysno = p->head.index_free;
769 p->head.index_free = entry.next;
771 (p->head.no_records)++;
773 for (i = 0; i < REC_NO_INFO; i++)
778 rec_cache_insert (p, rec, recordFlagNew);
782 void rec_del (Records p, Record *recpp)
786 (p->head.no_records)--;
787 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagDelete)))
794 rec_cache_insert (p, *recpp, recordFlagDelete);
800 void rec_put (Records p, Record *recpp)
804 if ((recp = rec_cache_lookup (p, (*recpp)->sysno, recordFlagWrite)))
811 rec_cache_insert (p, *recpp, recordFlagWrite);
817 void rec_rm (Record *recpp)
823 for (i = 0; i < REC_NO_INFO; i++)
824 xfree ((*recpp)->info[i]);
829 Record rec_cp (Record rec)
834 n = (Record) xmalloc (sizeof(*n));
835 n->sysno = rec->sysno;
836 for (i = 0; i < REC_NO_INFO; i++)
844 n->size[i] = rec->size[i];
845 n->info[i] = (char *) xmalloc (rec->size[i]);
846 memcpy (n->info[i], rec->info[i], rec->size[i]);
852 char *rec_strdup (const char *s, size_t *len)
862 p = (char *) xmalloc (*len);