2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss, Heikki Levanto
23 #define KEY_SIZE (1+sizeof(struct it_key))
24 #define INP_NAME_MAX 768
25 #define INP_BUF_START 60000
26 #define INP_BUF_ADD 400000
28 static int no_diffs = 0;
29 static int no_updates = 0;
30 static int no_deletions = 0;
31 static int no_insertions = 0;
32 static int no_iterations = 0;
36 off_t offset; /* file offset */
37 unsigned char *buf; /* buffer block */
38 size_t buf_size; /* number of read bytes in block */
39 size_t chunk; /* number of bytes allocated */
40 size_t buf_ptr; /* current position in buffer */
41 char *prev_name; /* last word read */
42 int sysno; /* last sysno */
43 int seqno; /* last seqno */
44 off_t length; /* length of file */
45 /* handler invoked in each read */
46 void (*readHandler)(struct key_file *keyp, void *rinfo);
50 void getFnameTmp (char *fname, int no)
54 pre = res_get_def (common_resource, "keyTmpDir", ".");
55 sprintf (fname, "%s/key%d.tmp", pre, no);
58 void key_file_chunk_read (struct key_file *f)
60 int nr = 0, r = 0, fd;
62 getFnameTmp (fname, f->no);
63 fd = open (fname, O_BINARY|O_RDONLY);
66 logf (LOG_FATAL|LOG_ERRNO, "cannot open %s", fname);
71 if ((f->length = lseek (fd, 0L, SEEK_END)) == (off_t) -1)
73 logf (LOG_FATAL|LOG_ERRNO, "cannot seek %s", fname);
77 if (lseek (fd, f->offset, SEEK_SET) == -1)
79 logf (LOG_FATAL|LOG_ERRNO, "cannot seek %s", fname);
82 while (f->chunk - nr > 0)
84 r = read (fd, f->buf + nr, f->chunk - nr);
91 logf (LOG_FATAL|LOG_ERRNO, "read of %s", fname);
97 (*f->readHandler)(f, f->readInfo);
101 struct key_file *key_file_init (int no, int chunk)
105 f = (struct key_file *) xmalloc (sizeof(*f));
112 f->readHandler = NULL;
113 f->buf = (unsigned char *) xmalloc (f->chunk);
114 f->prev_name = (char *) xmalloc (INP_NAME_MAX);
115 *f->prev_name = '\0';
116 key_file_chunk_read (f);
120 int key_file_getc (struct key_file *f)
122 if (f->buf_ptr < f->buf_size)
123 return f->buf[(f->buf_ptr)++];
124 if (f->buf_size < f->chunk)
126 f->offset += f->buf_size;
127 key_file_chunk_read (f);
128 if (f->buf_ptr < f->buf_size)
129 return f->buf[(f->buf_ptr)++];
134 int key_file_decode (struct key_file *f)
138 c = key_file_getc (f);
145 d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
148 d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
149 d = (d << 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);
154 d = (d << 8) + (key_file_getc (f) & 0xff);
160 int key_file_read (struct key_file *f, char *key)
165 c = key_file_getc (f);
168 strcpy (key, f->prev_name);
177 while ((key[i++] = key_file_getc (f)))
179 strcpy (f->prev_name, key);
182 d = key_file_decode (f);
185 itkey.sysno = d + f->sysno;
188 f->sysno = itkey.sysno;
191 d = key_file_decode (f);
192 itkey.seqno = d + f->seqno;
193 f->seqno = itkey.seqno;
194 memcpy (key + i, &itkey, sizeof(struct it_key));
195 return i + sizeof (struct it_key);
200 struct key_file **file;
205 int (*cmp)(const void *p1, const void *p2);
214 struct heap_info *key_heap_init (int nkeys,
215 int (*cmp)(const void *p1, const void *p2))
217 struct heap_info *hi;
220 hi = (struct heap_info *) xmalloc (sizeof(*hi));
221 hi->info.file = (struct key_file **)
222 xmalloc (sizeof(*hi->info.file) * (1+nkeys));
223 hi->info.buf = (char **) xmalloc (sizeof(*hi->info.buf) * (1+nkeys));
225 hi->ptr = (int *) xmalloc (sizeof(*hi->ptr) * (1+nkeys));
227 for (i = 0; i<= nkeys; i++)
230 hi->info.buf[i] = (char *) xmalloc (INP_NAME_MAX);
235 static void key_heap_swap (struct heap_info *hi, int i1, int i2)
240 hi->ptr[i1] = hi->ptr[i2];
245 static void key_heap_delete (struct heap_info *hi)
247 int cur = 1, child = 2;
249 assert (hi->heapnum > 0);
251 key_heap_swap (hi, 1, hi->heapnum);
253 while (child <= hi->heapnum) {
254 if (child < hi->heapnum &&
255 (*hi->cmp)(&hi->info.buf[hi->ptr[child]],
256 &hi->info.buf[hi->ptr[child+1]]) > 0)
258 if ((*hi->cmp)(&hi->info.buf[hi->ptr[cur]],
259 &hi->info.buf[hi->ptr[child]]) > 0)
261 key_heap_swap (hi, cur, child);
270 static void key_heap_insert (struct heap_info *hi, const char *buf, int nbytes,
275 cur = ++(hi->heapnum);
276 memcpy (hi->info.buf[hi->ptr[cur]], buf, nbytes);
277 hi->info.file[hi->ptr[cur]] = kf;
280 while (parent && (*hi->cmp)(&hi->info.buf[hi->ptr[parent]],
281 &hi->info.buf[hi->ptr[cur]]) > 0)
283 key_heap_swap (hi, cur, parent);
289 static int heap_read_one (struct heap_info *hi, char *name, char *key)
292 char rbuf[INP_NAME_MAX];
298 strcpy (name, hi->info.buf[n]);
299 kf = hi->info.file[n];
301 memcpy (key, hi->info.buf[n] + r+1, KEY_SIZE);
302 key_heap_delete (hi);
303 if ((r = key_file_read (kf, rbuf)))
304 key_heap_insert (hi, rbuf, r, kf);
309 struct heap_cread_info {
310 char prev_name[INP_NAME_MAX];
311 char cur_name[INP_NAME_MAX];
313 struct heap_info *hi;
318 int heap_cread_item (void *vp, char **dst, int *insertMode)
320 struct heap_cread_info *p = (struct heap_cread_info *) vp;
321 struct heap_info *hi = p->hi;
325 *insertMode = p->key[0];
326 memcpy (*dst, p->key+1, sizeof(struct it_key));
327 (*dst) += sizeof(struct it_key);
331 strcpy (p->prev_name, p->cur_name);
332 if (!(p->more = heap_read_one (hi, p->cur_name, p->key)))
334 if (*p->cur_name && strcmp (p->cur_name, p->prev_name))
339 *insertMode = p->key[0];
340 memcpy (*dst, p->key+1, sizeof(struct it_key));
341 (*dst) += sizeof(struct it_key);
345 int heap_inpc (struct heap_info *hi)
347 struct heap_cread_info hci;
348 ISAMC_I isamc_i = (ISAMC_I) xmalloc (sizeof(*isamc_i));
350 hci.key = (char *) xmalloc (KEY_SIZE);
353 hci.more = heap_read_one (hi, hci.cur_name, hci.key);
355 isamc_i->clientData = &hci;
356 isamc_i->read_item = heap_cread_item;
360 char this_name[INP_NAME_MAX];
361 ISAMC_P isamc_p, isamc_p2;
364 strcpy (this_name, hci.cur_name);
365 assert (hci.cur_name[1]);
367 if ((dict_info = dict_lookup (hi->dict, hci.cur_name)))
369 memcpy (&isamc_p, dict_info+1, sizeof(ISAMC_P));
370 isamc_p2 = isc_merge (hi->isamc, isamc_p, isamc_i);
374 if (!dict_delete (hi->dict, this_name))
380 if (isamc_p2 != isamc_p)
381 dict_insert (hi->dict, this_name,
382 sizeof(ISAMC_P), &isamc_p2);
387 isamc_p = isc_merge (hi->isamc, 0, isamc_i);
389 dict_insert (hi->dict, this_name, sizeof(ISAMC_P), &isamc_p);
396 int heap_inps (struct heap_info *hi)
398 struct heap_cread_info hci;
399 ISAMS_I isams_i = (ISAMS_I) xmalloc (sizeof(*isams_i));
401 hci.key = (char *) xmalloc (KEY_SIZE);
404 hci.more = heap_read_one (hi, hci.cur_name, hci.key);
406 isams_i->clientData = &hci;
407 isams_i->read_item = heap_cread_item;
411 char this_name[INP_NAME_MAX];
415 strcpy (this_name, hci.cur_name);
416 assert (hci.cur_name[1]);
418 if (!(dict_info = dict_lookup (hi->dict, hci.cur_name)))
420 isams_p = isams_merge (hi->isams, isams_i);
422 dict_insert (hi->dict, this_name, sizeof(ISAMS_P), &isams_p);
431 int heap_inph (struct heap_info *hi)
433 struct heap_cread_info hci;
434 ISAMH_I isamh_i = (ISAMH_I) xmalloc (sizeof(*isamh_i));
436 hci.key = (char *) xmalloc (KEY_SIZE);
439 hci.more = heap_read_one (hi, hci.cur_name, hci.key);
441 isamh_i->clientData = &hci;
442 isamh_i->read_item = heap_cread_item;
446 char this_name[INP_NAME_MAX];
447 ISAMH_P isamh_p, isamh_p2;
450 strcpy (this_name, hci.cur_name);
451 assert (hci.cur_name[1]);
453 if ((dict_info = dict_lookup (hi->dict, hci.cur_name)))
455 memcpy (&isamh_p, dict_info+1, sizeof(ISAMH_P));
456 isamh_p2 = isamh_append (hi->isamh, isamh_p, isamh_i);
460 if (!dict_delete (hi->dict, this_name))
466 if (isamh_p2 != isamh_p)
467 dict_insert (hi->dict, this_name,
468 sizeof(ISAMH_P), &isamh_p2);
473 isamh_p = isamh_append (hi->isamh, 0, isamh_i);
475 dict_insert (hi->dict, this_name, sizeof(ISAMH_P), &isamh_p);
482 int heap_inpd (struct heap_info *hi)
484 struct heap_cread_info hci;
485 ISAMD_I isamd_i = (ISAMD_I) xmalloc (sizeof(*isamd_i));
487 hci.key = (char *) xmalloc (KEY_SIZE);
490 hci.more = heap_read_one (hi, hci.cur_name, hci.key);
492 isamd_i->clientData = &hci;
493 isamd_i->read_item = heap_cread_item;
497 char this_name[INP_NAME_MAX];
498 ISAMD_P isamd_p, isamd_p2;
501 strcpy (this_name, hci.cur_name);
502 assert (hci.cur_name[1]);
504 if ((dict_info = dict_lookup (hi->dict, hci.cur_name)))
506 memcpy (&isamd_p, dict_info+1, sizeof(ISAMD_P));
507 isamd_p2 = isamd_append (hi->isamd, isamd_p, isamd_i);
511 if (!dict_delete (hi->dict, this_name))
517 if (isamd_p2 != isamd_p)
518 dict_insert (hi->dict, this_name,
519 sizeof(ISAMD_P), &isamd_p2);
524 isamd_p = isamd_append (hi->isamd, 0, isamd_i);
526 dict_insert (hi->dict, this_name, sizeof(ISAMD_P), &isamd_p);
536 int heap_inp (struct heap_info *hi)
539 char next_name[INP_NAME_MAX];
540 char cur_name[INP_NAME_MAX];
541 int key_buf_size = INP_BUF_START;
547 next_key = (char *) xmalloc (KEY_SIZE);
548 key_buf = (char *) xmalloc (key_buf_size);
549 more = heap_read_one (hi, cur_name, key_buf);
550 while (more) /* EOF ? */
553 key_buf_ptr = KEY_SIZE;
556 if (!(more = heap_read_one (hi, next_name, next_key)))
558 if (*next_name && strcmp (next_name, cur_name))
560 memcpy (key_buf + key_buf_ptr, next_key, KEY_SIZE);
561 key_buf_ptr += KEY_SIZE;
562 if (key_buf_ptr+(int) KEY_SIZE >= key_buf_size)
565 new_key_buf = (char *) xmalloc (key_buf_size + INP_BUF_ADD);
566 memcpy (new_key_buf, key_buf, key_buf_size);
567 key_buf_size += INP_BUF_ADD;
569 key_buf = new_key_buf;
573 nmemb = key_buf_ptr / KEY_SIZE;
574 assert (nmemb * (int) KEY_SIZE == key_buf_ptr);
575 if ((info = dict_lookup (hi->dict, cur_name)))
577 ISAM_P isam_p, isam_p2;
578 memcpy (&isam_p, info+1, sizeof(ISAM_P));
579 isam_p2 = is_merge (hi->isam, isam_p, nmemb, key_buf);
583 if (!dict_delete (hi->dict, cur_name))
589 if (isam_p2 != isam_p)
590 dict_insert (hi->dict, cur_name, sizeof(ISAM_P), &isam_p2);
597 isam_p = is_merge (hi->isam, 0, nmemb, key_buf);
598 dict_insert (hi->dict, cur_name, sizeof(ISAM_P), &isam_p);
600 memcpy (key_buf, next_key, KEY_SIZE);
601 strcpy (cur_name, next_name);
606 struct progressInfo {
613 void progressFunc (struct key_file *keyp, void *info)
615 struct progressInfo *p = (struct progressInfo *) info;
616 time_t now, remaining;
618 if (keyp->buf_size <= 0 || p->totalBytes <= 0)
622 if (now >= p->lastTime+10)
625 remaining = (time_t) ((now - p->startTime)*
626 ((double) p->totalBytes/p->totalOffset - 1.0));
627 if (remaining <= 130)
628 logf (LOG_LOG, "Merge %2.1f%% completed; %ld seconds remaining",
629 (100.0*p->totalOffset) / p->totalBytes, (long) remaining);
631 logf (LOG_LOG, "Merge %2.1f%% completed; %ld minutes remaining",
632 (100.0*p->totalOffset) / p->totalBytes, (long) remaining/60);
634 p->totalOffset += keyp->buf_size;
641 void key_input (BFiles bfs, int nkeys, int cache)
650 struct key_file **kf;
653 struct heap_info *hi;
654 struct progressInfo progressInfo;
662 getFnameTmp (fname, nkeys+1);
663 if (access (fname, R_OK) == -1)
670 dict = dict_open (bfs, FNAME_DICT, cache, 1, 0);
673 logf (LOG_FATAL, "dict_open fail");
676 if (res_get_match (common_resource, "isam", "s", NULL))
678 struct ISAMS_M_s isams_m;
679 isams = isams_open (bfs, FNAME_ISAMS, 1,
680 key_isams_m (common_resource, &isams_m));
683 logf (LOG_FATAL, "isams_open fail");
687 else if (res_get_match (common_resource, "isam", "i", NULL))
689 isam = is_open (bfs, FNAME_ISAM, key_compare, 1,
690 sizeof(struct it_key), common_resource);
693 logf (LOG_FATAL, "is_open fail");
697 else if (res_get_match (common_resource, "isam", "h", NULL))
699 isamh = isamh_open (bfs, FNAME_ISAMH, 1,
700 key_isamh_m (common_resource));
703 logf (LOG_FATAL, "isamh_open fail");
707 else if (res_get_match (common_resource, "isam", "d", NULL))
709 struct ISAMD_M_s isamd_m;
710 isamd = isamd_open (bfs, FNAME_ISAMD, 1,
711 key_isamd_m (common_resource,&isamd_m));
714 logf (LOG_FATAL, "isamd_open fail");
720 struct ISAMC_M_s isamc_m;
721 isamc = isc_open (bfs, FNAME_ISAMC, 1,
722 key_isamc_m (common_resource, &isamc_m));
725 logf (LOG_FATAL, "isc_open fail");
729 kf = (struct key_file **) xmalloc ((1+nkeys) * sizeof(*kf));
730 progressInfo.totalBytes = 0;
731 progressInfo.totalOffset = 0;
732 time (&progressInfo.startTime);
733 time (&progressInfo.lastTime);
734 for (i = 1; i<=nkeys; i++)
736 kf[i] = key_file_init (i, 32768);
737 kf[i]->readHandler = progressFunc;
738 kf[i]->readInfo = &progressInfo;
739 progressInfo.totalBytes += kf[i]->length;
740 progressInfo.totalOffset += kf[i]->buf_size;
742 hi = key_heap_init (nkeys, key_qsort_compare);
750 for (i = 1; i<=nkeys; i++)
751 if ((r = key_file_read (kf[i], rbuf)))
752 key_heap_insert (hi, rbuf, r, kf[i]);
776 for (i = 1; i<=nkeys; i++)
778 getFnameTmp (rbuf, i);
781 logf (LOG_LOG, "Iterations . . .%7d", no_iterations);
782 logf (LOG_LOG, "Distinct words .%7d", no_diffs);
783 logf (LOG_LOG, "Updates. . . . .%7d", no_updates);
784 logf (LOG_LOG, "Deletions. . . .%7d", no_deletions);
785 logf (LOG_LOG, "Insertions . . .%7d", no_insertions);
792 * Revision 1.37 1999-07-14 13:21:34 heikki
793 * Added isam-d files. Compiles (almost) clean. Doesn't work at all
795 * Revision 1.36 1999/07/14 10:59:26 adam
796 * Changed functions isc_getmethod, isams_getmethod.
797 * Improved fatal error handling (such as missing EXPLAIN schema).
799 * Revision 1.35 1999/06/30 15:07:23 heikki
802 * Revision 1.34 1999/05/26 07:49:13 adam
805 * Revision 1.33 1999/05/15 14:36:38 adam
806 * Updated dictionary. Implemented "compression" of dictionary.
808 * Revision 1.32 1999/05/12 13:08:06 adam
809 * First version of ISAMS.
811 * Revision 1.31 1999/02/02 14:50:56 adam
812 * Updated WIN32 code specific sections. Changed header.
814 * Revision 1.30 1998/10/28 10:53:57 adam
815 * Added type cast to prevent warning.
817 * Revision 1.29 1998/06/11 15:41:39 adam
820 * Revision 1.28 1998/03/05 08:45:12 adam
821 * New result set model and modular ranking system. Moved towards
822 * descent server API. System information stored as "SGML" records.
824 * Revision 1.27 1998/02/17 10:32:52 adam
825 * Fixed bug: binary files weren't opened with flag b on NT.
827 * Revision 1.26 1998/01/29 13:39:13 adam
828 * Compress ISAM is default.
830 * Revision 1.25 1997/09/17 12:19:14 adam
831 * Zebra version corresponds to YAZ version 1.4.
832 * Changed Zebra server so that it doesn't depend on global common_resource.
834 * Revision 1.24 1997/09/09 13:38:07 adam
835 * Partial port to WIN95/NT.
837 * Revision 1.23 1997/09/04 13:57:39 adam
838 * Added O_BINARY for open calls.
840 * Revision 1.22 1997/02/12 20:39:45 adam
841 * Implemented options -f <n> that limits the log to the first <n>
843 * Changed some log messages also.
845 * Revision 1.21 1996/11/08 11:10:23 adam
846 * Buffers used during file match got bigger.
847 * Compressed ISAM support everywhere.
848 * Bug fixes regarding masking characters in queries.
849 * Redesigned Regexp-2 queries.
851 * Revision 1.20 1996/11/01 08:58:41 adam
852 * Interface to isamc system now includes update and delete.
854 * Revision 1.19 1996/10/29 14:09:46 adam
855 * Use of cisam system - enabled if setting isamc is 1.
857 * Revision 1.18 1996/06/04 10:18:59 adam
858 * Minor changes - removed include of ctype.h.
860 * Revision 1.17 1996/05/14 15:47:07 adam
861 * Cleanup of various buffer size entities.
863 * Revision 1.16 1996/04/09 10:05:20 adam
864 * Bug fix: prev_name buffer possibly too small; allocated in key_file_init.
866 * Revision 1.15 1996/03/21 14:50:09 adam
867 * File update uses modify-time instead of change-time.
869 * Revision 1.14 1996/02/07 14:06:37 adam
870 * Better progress report during register merge.
871 * New command: clean - removes temporary shadow files.
873 * Revision 1.13 1996/02/05 12:30:00 adam
874 * Logging reduced a bit.
875 * The remaining running time is estimated during register merge.
877 * Revision 1.12 1995/12/06 17:49:19 adam
878 * Uses dict_delete now.
880 * Revision 1.11 1995/12/06 16:06:43 adam
881 * Better diagnostics. Work on 'real' dictionary deletion.
883 * Revision 1.10 1995/12/06 12:41:22 adam
884 * New command 'stat' for the index program.
885 * Filenames can be read from stdin by specifying '-'.
886 * Bug fix/enhancement of the transformation from terms to regular
887 * expressons in the search engine.
889 * Revision 1.9 1995/10/10 12:24:39 adam
890 * Temporary sort files are compressed.
892 * Revision 1.8 1995/10/04 16:57:19 adam
893 * Key input and merge sort in one pass.
895 * Revision 1.7 1995/10/02 15:18:52 adam
896 * New member in recRetrieveCtrl: diagnostic.
898 * Revision 1.6 1995/09/29 15:51:56 adam
899 * First work on multi-way read.
901 * Revision 1.5 1995/09/29 14:01:43 adam
904 * Revision 1.4 1995/09/28 14:22:57 adam
905 * Sort uses smaller temporary files.
907 * Revision 1.3 1995/09/06 16:11:17 adam
908 * Option: only one word key per file.
910 * Revision 1.2 1995/09/04 12:33:42 adam
911 * Various cleanup. YAZ util used instead.
913 * Revision 1.1 1995/09/04 09:10:37 adam
914 * More work on index add/del/update.
915 * Merge sort implemented.
916 * Initial work on z39 server.