2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss, Heikki Levanto
9 * - Allocates a lot of memory for the merge process, but never releases it.
10 * Doesn't matter, as the program terminates soon after.
27 #define KEY_SIZE (1+sizeof(struct it_key))
28 #define INP_NAME_MAX 768
29 #define INP_BUF_START 60000
30 #define INP_BUF_ADD 400000
32 static int no_diffs = 0;
33 static int no_updates = 0;
34 static int no_deletions = 0;
35 static int no_insertions = 0;
36 static int no_iterations = 0;
40 off_t offset; /* file offset */
41 unsigned char *buf; /* buffer block */
42 size_t buf_size; /* number of read bytes in block */
43 size_t chunk; /* number of bytes allocated */
44 size_t buf_ptr; /* current position in buffer */
45 char *prev_name; /* last word read */
46 int sysno; /* last sysno */
47 int seqno; /* last seqno */
48 off_t length; /* length of file */
49 /* handler invoked in each read */
50 void (*readHandler)(struct key_file *keyp, void *rinfo);
54 void getFnameTmp (char *fname, int no)
58 pre = res_get_def (common_resource, "keyTmpDir", ".");
59 sprintf (fname, "%s/key%d.tmp", pre, no);
62 void key_file_chunk_read (struct key_file *f)
64 int nr = 0, r = 0, fd;
66 getFnameTmp (fname, f->no);
67 fd = open (fname, O_BINARY|O_RDONLY);
70 logf (LOG_FATAL|LOG_ERRNO, "cannot open %s", fname);
75 if ((f->length = lseek (fd, 0L, SEEK_END)) == (off_t) -1)
77 logf (LOG_FATAL|LOG_ERRNO, "cannot seek %s", fname);
81 if (lseek (fd, f->offset, SEEK_SET) == -1)
83 logf (LOG_FATAL|LOG_ERRNO, "cannot seek %s", fname);
86 while (f->chunk - nr > 0)
88 r = read (fd, f->buf + nr, f->chunk - nr);
95 logf (LOG_FATAL|LOG_ERRNO, "read of %s", fname);
101 (*f->readHandler)(f, f->readInfo);
105 struct key_file *key_file_init (int no, int chunk)
109 f = (struct key_file *) xmalloc (sizeof(*f));
116 f->readHandler = NULL;
117 f->buf = (unsigned char *) xmalloc (f->chunk);
118 f->prev_name = (char *) xmalloc (INP_NAME_MAX);
119 *f->prev_name = '\0';
120 key_file_chunk_read (f);
124 int key_file_getc (struct key_file *f)
126 if (f->buf_ptr < f->buf_size)
127 return f->buf[(f->buf_ptr)++];
128 if (f->buf_size < f->chunk)
130 f->offset += f->buf_size;
131 key_file_chunk_read (f);
132 if (f->buf_ptr < f->buf_size)
133 return f->buf[(f->buf_ptr)++];
138 int key_file_decode (struct key_file *f)
142 c = key_file_getc (f);
149 d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
152 d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
153 d = (d << 8) + (key_file_getc (f) & 0xff);
156 d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
157 d = (d << 8) + (key_file_getc (f) & 0xff);
158 d = (d << 8) + (key_file_getc (f) & 0xff);
164 int key_file_read (struct key_file *f, char *key)
169 c = key_file_getc (f);
172 strcpy (key, f->prev_name);
181 while ((key[i++] = key_file_getc (f)))
183 strcpy (f->prev_name, key);
186 d = key_file_decode (f);
189 itkey.sysno = d + f->sysno;
192 f->sysno = itkey.sysno;
195 d = key_file_decode (f);
196 itkey.seqno = d + f->seqno;
197 f->seqno = itkey.seqno;
198 memcpy (key + i, &itkey, sizeof(struct it_key));
199 return i + sizeof (struct it_key);
204 struct key_file **file;
209 int (*cmp)(const void *p1, const void *p2);
219 struct heap_info *key_heap_init (int nkeys,
220 int (*cmp)(const void *p1, const void *p2))
222 struct heap_info *hi;
225 hi = (struct heap_info *) xmalloc (sizeof(*hi));
226 hi->info.file = (struct key_file **)
227 xmalloc (sizeof(*hi->info.file) * (1+nkeys));
228 hi->info.buf = (char **) xmalloc (sizeof(*hi->info.buf) * (1+nkeys));
230 hi->ptr = (int *) xmalloc (sizeof(*hi->ptr) * (1+nkeys));
232 for (i = 0; i<= nkeys; i++)
235 hi->info.buf[i] = (char *) xmalloc (INP_NAME_MAX);
240 static void key_heap_swap (struct heap_info *hi, int i1, int i2)
245 hi->ptr[i1] = hi->ptr[i2];
250 static void key_heap_delete (struct heap_info *hi)
252 int cur = 1, child = 2;
254 assert (hi->heapnum > 0);
256 key_heap_swap (hi, 1, hi->heapnum);
258 while (child <= hi->heapnum) {
259 if (child < hi->heapnum &&
260 (*hi->cmp)(&hi->info.buf[hi->ptr[child]],
261 &hi->info.buf[hi->ptr[child+1]]) > 0)
263 if ((*hi->cmp)(&hi->info.buf[hi->ptr[cur]],
264 &hi->info.buf[hi->ptr[child]]) > 0)
266 key_heap_swap (hi, cur, child);
275 static void key_heap_insert (struct heap_info *hi, const char *buf, int nbytes,
280 cur = ++(hi->heapnum);
281 memcpy (hi->info.buf[hi->ptr[cur]], buf, nbytes);
282 hi->info.file[hi->ptr[cur]] = kf;
285 while (parent && (*hi->cmp)(&hi->info.buf[hi->ptr[parent]],
286 &hi->info.buf[hi->ptr[cur]]) > 0)
288 key_heap_swap (hi, cur, parent);
294 static int heap_read_one (struct heap_info *hi, char *name, char *key)
297 char rbuf[INP_NAME_MAX];
303 strcpy (name, hi->info.buf[n]);
304 kf = hi->info.file[n];
306 memcpy (key, hi->info.buf[n] + r+1, KEY_SIZE);
307 key_heap_delete (hi);
308 if ((r = key_file_read (kf, rbuf)))
309 key_heap_insert (hi, rbuf, r, kf);
314 struct heap_cread_info {
315 char prev_name[INP_NAME_MAX];
316 char cur_name[INP_NAME_MAX];
318 struct heap_info *hi;
323 int heap_cread_item (void *vp, char **dst, int *insertMode)
325 struct heap_cread_info *p = (struct heap_cread_info *) vp;
326 struct heap_info *hi = p->hi;
330 *insertMode = p->key[0];
331 memcpy (*dst, p->key+1, sizeof(struct it_key));
332 (*dst) += sizeof(struct it_key);
336 strcpy (p->prev_name, p->cur_name);
337 if (!(p->more = heap_read_one (hi, p->cur_name, p->key)))
339 if (*p->cur_name && strcmp (p->cur_name, p->prev_name))
344 *insertMode = p->key[0];
345 memcpy (*dst, p->key+1, sizeof(struct it_key));
346 (*dst) += sizeof(struct it_key);
351 int heap_inpc (struct heap_info *hi)
353 struct heap_cread_info hci;
354 ISAMC_I isamc_i = (ISAMC_I) xmalloc (sizeof(*isamc_i));
356 hci.key = (char *) xmalloc (KEY_SIZE);
359 hci.more = heap_read_one (hi, hci.cur_name, hci.key);
361 isamc_i->clientData = &hci;
362 isamc_i->read_item = heap_cread_item;
366 char this_name[INP_NAME_MAX];
367 ISAMC_P isamc_p, isamc_p2;
370 strcpy (this_name, hci.cur_name);
371 assert (hci.cur_name[1]);
373 if ((dict_info = dict_lookup (hi->dict, hci.cur_name)))
375 memcpy (&isamc_p, dict_info+1, sizeof(ISAMC_P));
376 isamc_p2 = isc_merge (hi->isamc, isamc_p, isamc_i);
380 if (!dict_delete (hi->dict, this_name))
386 if (isamc_p2 != isamc_p)
387 dict_insert (hi->dict, this_name,
388 sizeof(ISAMC_P), &isamc_p2);
393 isamc_p = isc_merge (hi->isamc, 0, isamc_i);
395 dict_insert (hi->dict, this_name, sizeof(ISAMC_P), &isamc_p);
402 int heap_inpd (struct heap_info *hi)
404 struct heap_cread_info hci;
405 ISAMD_I isamd_i = (ISAMD_I) xmalloc (sizeof(*isamd_i));
407 hci.key = (char *) xmalloc (KEY_SIZE);
410 hci.more = heap_read_one (hi, hci.cur_name, hci.key);
412 isamd_i->clientData = &hci;
413 isamd_i->read_item = heap_cread_item;
417 char this_name[INP_NAME_MAX];
418 ISAMD_P isamd_p, isamd_p2;
421 strcpy (this_name, hci.cur_name);
422 assert (hci.cur_name[1]);
424 if ((dict_info = dict_lookup (hi->dict, hci.cur_name)))
426 memcpy (&isamd_p, dict_info+1, sizeof(ISAMD_P));
427 isamd_p2 = isamd_append (hi->isamd, isamd_p, isamd_i);
431 if (!dict_delete (hi->dict, this_name))
437 if (isamd_p2 != isamd_p)
438 dict_insert (hi->dict, this_name,
439 sizeof(ISAMD_P), &isamd_p2);
444 isamd_p = isamd_append (hi->isamd, 0, isamd_i);
446 dict_insert (hi->dict, this_name, sizeof(ISAMD_P), &isamd_p);
453 int heap_inp (struct heap_info *hi)
456 char next_name[INP_NAME_MAX];
457 char cur_name[INP_NAME_MAX];
458 int key_buf_size = INP_BUF_START;
464 next_key = (char *) xmalloc (KEY_SIZE);
465 key_buf = (char *) xmalloc (key_buf_size);
466 more = heap_read_one (hi, cur_name, key_buf);
467 while (more) /* EOF ? */
470 key_buf_ptr = KEY_SIZE;
473 if (!(more = heap_read_one (hi, next_name, next_key)))
475 if (*next_name && strcmp (next_name, cur_name))
477 memcpy (key_buf + key_buf_ptr, next_key, KEY_SIZE);
478 key_buf_ptr += KEY_SIZE;
479 if (key_buf_ptr+(int) KEY_SIZE >= key_buf_size)
482 new_key_buf = (char *) xmalloc (key_buf_size + INP_BUF_ADD);
483 memcpy (new_key_buf, key_buf, key_buf_size);
484 key_buf_size += INP_BUF_ADD;
486 key_buf = new_key_buf;
490 nmemb = key_buf_ptr / KEY_SIZE;
491 assert (nmemb * (int) KEY_SIZE == key_buf_ptr);
492 if ((info = dict_lookup (hi->dict, cur_name)))
494 ISAM_P isam_p, isam_p2;
495 memcpy (&isam_p, info+1, sizeof(ISAM_P));
496 isam_p2 = is_merge (hi->isam, isam_p, nmemb, key_buf);
500 if (!dict_delete (hi->dict, cur_name))
506 if (isam_p2 != isam_p)
507 dict_insert (hi->dict, cur_name, sizeof(ISAM_P), &isam_p2);
514 isam_p = is_merge (hi->isam, 0, nmemb, key_buf);
515 dict_insert (hi->dict, cur_name, sizeof(ISAM_P), &isam_p);
517 memcpy (key_buf, next_key, KEY_SIZE);
518 strcpy (cur_name, next_name);
525 int heap_inps (struct heap_info *hi)
527 struct heap_cread_info hci;
528 ISAMS_I isams_i = (ISAMS_I) xmalloc (sizeof(*isams_i));
530 hci.key = (char *) xmalloc (KEY_SIZE);
533 hci.more = heap_read_one (hi, hci.cur_name, hci.key);
535 isams_i->clientData = &hci;
536 isams_i->read_item = heap_cread_item;
540 char this_name[INP_NAME_MAX];
544 strcpy (this_name, hci.cur_name);
545 assert (hci.cur_name[1]);
547 if (!(dict_info = dict_lookup (hi->dict, hci.cur_name)))
549 isams_p = isams_merge (hi->isams, isams_i);
551 dict_insert (hi->dict, this_name, sizeof(ISAMS_P), &isams_p);
555 logf (LOG_FATAL, "isams doesn't support this kind of update");
563 struct progressInfo {
570 void progressFunc (struct key_file *keyp, void *info)
572 struct progressInfo *p = (struct progressInfo *) info;
573 time_t now, remaining;
575 if (keyp->buf_size <= 0 || p->totalBytes <= 0)
579 if (now >= p->lastTime+10)
582 remaining = (time_t) ((now - p->startTime)*
583 ((double) p->totalBytes/p->totalOffset - 1.0));
584 if (remaining <= 130)
585 logf (LOG_LOG, "Merge %2.1f%% completed; %ld seconds remaining",
586 (100.0*p->totalOffset) / p->totalBytes, (long) remaining);
588 logf (LOG_LOG, "Merge %2.1f%% completed; %ld minutes remaining",
589 (100.0*p->totalOffset) / p->totalBytes, (long) remaining/60);
591 p->totalOffset += keyp->buf_size;
598 void key_input (BFiles bfs, int nkeys, int cache)
608 struct key_file **kf;
611 struct heap_info *hi;
612 struct progressInfo progressInfo;
620 getFnameTmp (fname, nkeys+1);
621 if (access (fname, R_OK) == -1)
628 dict = dict_open (bfs, FNAME_DICT, cache, 1, 0);
631 logf (LOG_FATAL, "dict_open fail");
634 if (res_get_match (common_resource, "isam", "s", ISAM_DEFAULT))
636 struct ISAMS_M_s isams_m;
637 isams = isams_open (bfs, FNAME_ISAMS, 1,
638 key_isams_m (common_resource, &isams_m));
641 logf (LOG_FATAL, "isams_open fail");
644 logf (LOG_LOG, "isams opened");
647 else if (res_get_match (common_resource, "isam", "i", ISAM_DEFAULT))
649 isam = is_open (bfs, FNAME_ISAM, key_compare, 1,
650 sizeof(struct it_key), common_resource);
653 logf (LOG_FATAL, "is_open fail");
657 else if (res_get_match (common_resource, "isam", "d", ISAM_DEFAULT))
659 struct ISAMD_M_s isamd_m;
660 isamd = isamd_open (bfs, FNAME_ISAMD, 1,
661 key_isamd_m (common_resource,&isamd_m));
664 logf (LOG_FATAL, "isamd_open fail");
668 else if (res_get_match (common_resource, "isam", "c", ISAM_DEFAULT))
670 struct ISAMC_M_s isamc_m;
671 isamc = isc_open (bfs, FNAME_ISAMC, 1,
672 key_isamc_m (common_resource, &isamc_m));
675 logf (LOG_FATAL, "isc_open fail");
680 kf = (struct key_file **) xmalloc ((1+nkeys) * sizeof(*kf));
681 progressInfo.totalBytes = 0;
682 progressInfo.totalOffset = 0;
683 time (&progressInfo.startTime);
684 time (&progressInfo.lastTime);
685 for (i = 1; i<=nkeys; i++)
687 kf[i] = key_file_init (i, 32768);
688 kf[i]->readHandler = progressFunc;
689 kf[i]->readInfo = &progressInfo;
690 progressInfo.totalBytes += kf[i]->length;
691 progressInfo.totalOffset += kf[i]->buf_size;
693 hi = key_heap_init (nkeys, key_qsort_compare);
702 for (i = 1; i<=nkeys; i++)
703 if ((r = key_file_read (kf[i], rbuf)))
704 key_heap_insert (hi, rbuf, r, kf[i]);
728 for (i = 1; i<=nkeys; i++)
730 getFnameTmp (rbuf, i);
733 logf (LOG_LOG, "Iterations . . .%7d", no_iterations);
734 logf (LOG_LOG, "Distinct words .%7d", no_diffs);
735 logf (LOG_LOG, "Updates. . . . .%7d", no_updates);
736 logf (LOG_LOG, "Deletions. . . .%7d", no_deletions);
737 logf (LOG_LOG, "Insertions . . .%7d", no_insertions);
739 /* xmalloc_trav("unfreed"); while hunting leaks */
746 * Revision 1.42 1999-12-01 21:58:48 adam
747 * Proper handle of illegal use of isams.
749 * Revision 1.41 1999/11/30 13:48:03 adam
750 * Improved installation. Updated for inclusion of YAZ header files.
752 * Revision 1.40 1999/09/08 12:12:39 adam
753 * Removed log message.
755 * Revision 1.39 1999/08/18 10:39:20 heikki
756 * Added a comment on memory leaks
758 * Revision 1.38 1999/08/18 08:38:04 heikki
759 * Memory leak hunting
761 * Revision 1.37 1999/07/14 13:21:34 heikki
762 * Added isam-d files. Compiles (almost) clean. Doesn't work at all
764 * Revision 1.36 1999/07/14 10:59:26 adam
765 * Changed functions isc_getmethod, isams_getmethod.
766 * Improved fatal error handling (such as missing EXPLAIN schema).
768 * Revision 1.35 1999/06/30 15:07:23 heikki
771 * Revision 1.34 1999/05/26 07:49:13 adam
774 * Revision 1.33 1999/05/15 14:36:38 adam
775 * Updated dictionary. Implemented "compression" of dictionary.
777 * Revision 1.32 1999/05/12 13:08:06 adam
778 * First version of ISAMS.
780 * Revision 1.31 1999/02/02 14:50:56 adam
781 * Updated WIN32 code specific sections. Changed header.
783 * Revision 1.30 1998/10/28 10:53:57 adam
784 * Added type cast to prevent warning.
786 * Revision 1.29 1998/06/11 15:41:39 adam
789 * Revision 1.28 1998/03/05 08:45:12 adam
790 * New result set model and modular ranking system. Moved towards
791 * descent server API. System information stored as "SGML" records.
793 * Revision 1.27 1998/02/17 10:32:52 adam
794 * Fixed bug: binary files weren't opened with flag b on NT.
796 * Revision 1.26 1998/01/29 13:39:13 adam
797 * Compress ISAM is default.
799 * Revision 1.25 1997/09/17 12:19:14 adam
800 * Zebra version corresponds to YAZ version 1.4.
801 * Changed Zebra server so that it doesn't depend on global common_resource.
803 * Revision 1.24 1997/09/09 13:38:07 adam
804 * Partial port to WIN95/NT.
806 * Revision 1.23 1997/09/04 13:57:39 adam
807 * Added O_BINARY for open calls.
809 * Revision 1.22 1997/02/12 20:39:45 adam
810 * Implemented options -f <n> that limits the log to the first <n>
812 * Changed some log messages also.
814 * Revision 1.21 1996/11/08 11:10:23 adam
815 * Buffers used during file match got bigger.
816 * Compressed ISAM support everywhere.
817 * Bug fixes regarding masking characters in queries.
818 * Redesigned Regexp-2 queries.
820 * Revision 1.20 1996/11/01 08:58:41 adam
821 * Interface to isamc system now includes update and delete.
823 * Revision 1.19 1996/10/29 14:09:46 adam
824 * Use of cisam system - enabled if setting isamc is 1.
826 * Revision 1.18 1996/06/04 10:18:59 adam
827 * Minor changes - removed include of ctype.h.
829 * Revision 1.17 1996/05/14 15:47:07 adam
830 * Cleanup of various buffer size entities.
832 * Revision 1.16 1996/04/09 10:05:20 adam
833 * Bug fix: prev_name buffer possibly too small; allocated in key_file_init.
835 * Revision 1.15 1996/03/21 14:50:09 adam
836 * File update uses modify-time instead of change-time.
838 * Revision 1.14 1996/02/07 14:06:37 adam
839 * Better progress report during register merge.
840 * New command: clean - removes temporary shadow files.
842 * Revision 1.13 1996/02/05 12:30:00 adam
843 * Logging reduced a bit.
844 * The remaining running time is estimated during register merge.
846 * Revision 1.12 1995/12/06 17:49:19 adam
847 * Uses dict_delete now.
849 * Revision 1.11 1995/12/06 16:06:43 adam
850 * Better diagnostics. Work on 'real' dictionary deletion.
852 * Revision 1.10 1995/12/06 12:41:22 adam
853 * New command 'stat' for the index program.
854 * Filenames can be read from stdin by specifying '-'.
855 * Bug fix/enhancement of the transformation from terms to regular
856 * expressons in the search engine.
858 * Revision 1.9 1995/10/10 12:24:39 adam
859 * Temporary sort files are compressed.
861 * Revision 1.8 1995/10/04 16:57:19 adam
862 * Key input and merge sort in one pass.
864 * Revision 1.7 1995/10/02 15:18:52 adam
865 * New member in recRetrieveCtrl: diagnostic.
867 * Revision 1.6 1995/09/29 15:51:56 adam
868 * First work on multi-way read.
870 * Revision 1.5 1995/09/29 14:01:43 adam
873 * Revision 1.4 1995/09/28 14:22:57 adam
874 * Sort uses smaller temporary files.
876 * Revision 1.3 1995/09/06 16:11:17 adam
877 * Option: only one word key per file.
879 * Revision 1.2 1995/09/04 12:33:42 adam
880 * Various cleanup. YAZ util used instead.
882 * Revision 1.1 1995/09/04 09:10:37 adam
883 * More work on index add/del/update.
884 * Merge sort implemented.
885 * Initial work on z39 server.