Implemented options -f <n> that limits the log to the first <n>
[idzebra-moved-to-github.git] / index / kinput.c
1 /*
2  * Copyright (C) 1994-1996, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: kinput.c,v $
7  * Revision 1.22  1997-02-12 20:39:45  adam
8  * Implemented options -f <n> that limits the log to the first <n>
9  * records.
10  * Changed some log messages also.
11  *
12  * Revision 1.21  1996/11/08 11:10:23  adam
13  * Buffers used during file match got bigger.
14  * Compressed ISAM support everywhere.
15  * Bug fixes regarding masking characters in queries.
16  * Redesigned Regexp-2 queries.
17  *
18  * Revision 1.20  1996/11/01 08:58:41  adam
19  * Interface to isamc system now includes update and delete.
20  *
21  * Revision 1.19  1996/10/29 14:09:46  adam
22  * Use of cisam system - enabled if setting isamc is 1.
23  *
24  * Revision 1.18  1996/06/04 10:18:59  adam
25  * Minor changes - removed include of ctype.h.
26  *
27  * Revision 1.17  1996/05/14  15:47:07  adam
28  * Cleanup of various buffer size entities.
29  *
30  * Revision 1.16  1996/04/09  10:05:20  adam
31  * Bug fix: prev_name buffer possibly too small; allocated in key_file_init.
32  *
33  * Revision 1.15  1996/03/21  14:50:09  adam
34  * File update uses modify-time instead of change-time.
35  *
36  * Revision 1.14  1996/02/07  14:06:37  adam
37  * Better progress report during register merge.
38  * New command: clean - removes temporary shadow files.
39  *
40  * Revision 1.13  1996/02/05  12:30:00  adam
41  * Logging reduced a bit.
42  * The remaining running time is estimated during register merge.
43  *
44  * Revision 1.12  1995/12/06  17:49:19  adam
45  * Uses dict_delete now.
46  *
47  * Revision 1.11  1995/12/06  16:06:43  adam
48  * Better diagnostics. Work on 'real' dictionary deletion.
49  *
50  * Revision 1.10  1995/12/06  12:41:22  adam
51  * New command 'stat' for the index program.
52  * Filenames can be read from stdin by specifying '-'.
53  * Bug fix/enhancement of the transformation from terms to regular
54  * expressons in the search engine.
55  *
56  * Revision 1.9  1995/10/10  12:24:39  adam
57  * Temporary sort files are compressed.
58  *
59  * Revision 1.8  1995/10/04  16:57:19  adam
60  * Key input and merge sort in one pass.
61  *
62  * Revision 1.7  1995/10/02  15:18:52  adam
63  * New member in recRetrieveCtrl: diagnostic.
64  *
65  * Revision 1.6  1995/09/29  15:51:56  adam
66  * First work on multi-way read.
67  *
68  * Revision 1.5  1995/09/29  14:01:43  adam
69  * Bug fixes.
70  *
71  * Revision 1.4  1995/09/28  14:22:57  adam
72  * Sort uses smaller temporary files.
73  *
74  * Revision 1.3  1995/09/06  16:11:17  adam
75  * Option: only one word key per file.
76  *
77  * Revision 1.2  1995/09/04  12:33:42  adam
78  * Various cleanup. YAZ util used instead.
79  *
80  * Revision 1.1  1995/09/04  09:10:37  adam
81  * More work on index add/del/update.
82  * Merge sort implemented.
83  * Initial work on z39 server.
84  *
85  */
86
87 #include <fcntl.h>
88 #include <unistd.h>
89 #include <stdlib.h>
90 #include <string.h>
91 #include <stdio.h>
92 #include <assert.h>
93
94 #include "index.h"
95
96 #define KEY_SIZE (1+sizeof(struct it_key))
97 #define INP_NAME_MAX 768
98 #define INP_BUF_START 60000
99 #define INP_BUF_ADD  400000
100
101 static int no_diffs   = 0;
102 static int no_updates = 0;
103 static int no_deletions = 0;
104 static int no_insertions = 0;
105 static int no_iterations = 0;
106
107 struct key_file {
108     int   no;            /* file no */
109     off_t offset;        /* file offset */
110     unsigned char *buf;  /* buffer block */
111     size_t buf_size;     /* number of read bytes in block */
112     size_t chunk;        /* number of bytes allocated */
113     size_t buf_ptr;      /* current position in buffer */
114     char *prev_name;     /* last word read */
115     int   sysno;         /* last sysno */
116     int   seqno;         /* last seqno */
117     off_t length;        /* length of file */
118                          /* handler invoked in each read */
119     void (*readHandler)(struct key_file *keyp, void *rinfo);
120     void *readInfo;
121 };
122
123 void getFnameTmp (char *fname, int no)
124 {
125     const char *pre;
126     
127     pre = res_get_def (common_resource, "keyTmpDir", ".");
128     sprintf (fname, "%s/key%d.tmp", pre, no);
129 }
130
131 void key_file_chunk_read (struct key_file *f)
132 {
133     int nr = 0, r, fd;
134     char fname[1024];
135     getFnameTmp (fname, f->no);
136     fd = open (fname, O_RDONLY);
137     if (fd == -1)
138     {
139         logf (LOG_FATAL|LOG_ERRNO, "cannot open %s", fname);
140         exit (1);
141     }
142     if (!f->length)
143     {
144         if ((f->length = lseek (fd, 0L, SEEK_END)) == (off_t) -1)
145         {
146             logf (LOG_FATAL|LOG_ERRNO, "cannot seek %s", fname);
147             exit (1);
148         }
149     }
150     if (lseek (fd, f->offset, SEEK_SET) == -1)
151     {
152         logf (LOG_FATAL|LOG_ERRNO, "cannot seek %s", fname);
153         exit (1);
154     }
155     while (f->chunk - nr > 0)
156     {
157         r = read (fd, f->buf + nr, f->chunk - nr);
158         if (r <= 0)
159             break;
160         nr += r;
161     }
162     if (r == -1)
163     {
164         logf (LOG_FATAL|LOG_ERRNO, "read of %s", fname);
165         exit (1);
166     }
167     f->buf_size = nr;
168     f->buf_ptr = 0;
169     if (f->readHandler)
170         (*f->readHandler)(f, f->readInfo);
171     close (fd);
172 }
173
174 struct key_file *key_file_init (int no, int chunk)
175 {
176     struct key_file *f;
177
178     f = xmalloc (sizeof(*f));
179     f->sysno = 0;
180     f->seqno = 0;
181     f->no = no;
182     f->chunk = chunk;
183     f->offset = 0;
184     f->length = 0;
185     f->readHandler = NULL;
186     f->buf = xmalloc (f->chunk);
187     f->prev_name = xmalloc (INP_NAME_MAX);
188     *f->prev_name = '\0';
189     key_file_chunk_read (f);
190     return f;
191 }
192
193 int key_file_getc (struct key_file *f)
194 {
195     if (f->buf_ptr < f->buf_size)
196         return f->buf[(f->buf_ptr)++];
197     if (f->buf_size < f->chunk)
198         return EOF;
199     f->offset += f->buf_size;
200     key_file_chunk_read (f);
201     if (f->buf_ptr < f->buf_size)
202         return f->buf[(f->buf_ptr)++];
203     else
204         return EOF;
205 }
206
207 int key_file_decode (struct key_file *f)
208 {
209     int c, d;
210
211     c = key_file_getc (f);
212     switch (c & 192) 
213     {
214     case 0:
215         d = c;
216         break;
217     case 64:
218         d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
219         break;
220     case 128:
221         d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
222         d = (d << 8) + (key_file_getc (f) & 0xff);
223         break;
224     case 192:
225         d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
226         d = (d << 8) + (key_file_getc (f) & 0xff);
227         d = (d << 8) + (key_file_getc (f) & 0xff);
228         break;
229     }
230     return d;
231 }
232
233 int key_file_read (struct key_file *f, char *key)
234 {
235     int i, d, c;
236     struct it_key itkey;
237
238     c = key_file_getc (f);
239     if (c == 0)
240     {
241         strcpy (key, f->prev_name);
242         i = 1+strlen (key);
243     }
244     else if (c == EOF)
245         return 0;
246     else
247     {
248         i = 0;
249         key[i++] = c;
250         while ((key[i++] = key_file_getc (f)))
251             ;
252         strcpy (f->prev_name, key);
253         f->sysno = 0;
254     }
255     d = key_file_decode (f);
256     key[i++] = d & 1;
257     d = d >> 1;
258     itkey.sysno = d + f->sysno;
259     if (d) 
260     {
261         f->sysno = itkey.sysno;
262         f->seqno = 0;
263     }
264     d = key_file_decode (f);
265     itkey.seqno = d + f->seqno;
266     f->seqno = itkey.seqno;
267     memcpy (key + i, &itkey, sizeof(struct it_key));
268     return i + sizeof (struct it_key);
269 }
270
271 struct heap_info {
272     struct {
273         struct key_file **file;
274         char   **buf;
275     } info;
276     int    heapnum;
277     int    *ptr;
278     int    (*cmp)(const void *p1, const void *p2);
279     Dict dict;
280     ISAM isam;
281     ISAMC isamc;
282 };
283
284 struct heap_info *key_heap_init (int nkeys,
285                                  int (*cmp)(const void *p1, const void *p2))
286 {
287     struct heap_info *hi;
288     int i;
289
290     hi = xmalloc (sizeof(*hi));
291     hi->info.file = xmalloc (sizeof(*hi->info.file) * (1+nkeys));
292     hi->info.buf = xmalloc (sizeof(*hi->info.buf) * (1+nkeys));
293     hi->heapnum = 0;
294     hi->ptr = xmalloc (sizeof(*hi->ptr) * (1+nkeys));
295     hi->cmp = cmp;
296     for (i = 0; i<= nkeys; i++)
297     {
298         hi->ptr[i] = i;
299         hi->info.buf[i] = xmalloc (INP_NAME_MAX);
300     }
301     return hi;
302 }
303
304 static void key_heap_swap (struct heap_info *hi, int i1, int i2)
305 {
306     int swap;
307
308     swap = hi->ptr[i1];
309     hi->ptr[i1] = hi->ptr[i2];
310     hi->ptr[i2] = swap;
311 }
312
313
314 static void key_heap_delete (struct heap_info *hi)
315 {
316     int cur = 1, child = 2;
317
318     assert (hi->heapnum > 0);
319
320     key_heap_swap (hi, 1, hi->heapnum);
321     hi->heapnum--;
322     while (child <= hi->heapnum) {
323         if (child < hi->heapnum &&
324             (*hi->cmp)(&hi->info.buf[hi->ptr[child]],
325                        &hi->info.buf[hi->ptr[child+1]]) > 0)
326             child++;
327         if ((*hi->cmp)(&hi->info.buf[hi->ptr[cur]],
328                        &hi->info.buf[hi->ptr[child]]) > 0)
329         {            
330             key_heap_swap (hi, cur, child);
331             cur = child;
332             child = 2*cur;
333         }
334         else
335             break;
336     }
337 }
338
339 static void key_heap_insert (struct heap_info *hi, const char *buf, int nbytes,
340                              struct key_file *kf)
341 {
342     int cur, parent;
343
344     cur = ++(hi->heapnum);
345     memcpy (hi->info.buf[hi->ptr[cur]], buf, nbytes);
346     hi->info.file[hi->ptr[cur]] = kf;
347
348     parent = cur/2;
349     while (parent && (*hi->cmp)(&hi->info.buf[hi->ptr[parent]],
350                                 &hi->info.buf[hi->ptr[cur]]) > 0)
351     {
352         key_heap_swap (hi, cur, parent);
353         cur = parent;
354         parent = cur/2;
355     }
356 }
357
358 static int heap_read_one (struct heap_info *hi, char *name, char *key)
359 {
360     int n, r;
361     char rbuf[INP_NAME_MAX];
362     struct key_file *kf;
363
364     if (!hi->heapnum)
365         return 0;
366     n = hi->ptr[1];
367     strcpy (name, hi->info.buf[n]);
368     kf = hi->info.file[n];
369     r = strlen(name);
370     memcpy (key, hi->info.buf[n] + r+1, KEY_SIZE);
371     key_heap_delete (hi);
372     if ((r = key_file_read (kf, rbuf)))
373         key_heap_insert (hi, rbuf, r, kf);
374     no_iterations++;
375     return 1;
376 }
377
378 struct heap_cread_info {
379     char prev_name[INP_NAME_MAX];
380     char cur_name[INP_NAME_MAX];
381     char *key;
382     struct heap_info *hi;
383     int mode;
384     int more;
385 };
386       
387 int heap_cread_item (void *vp, char **dst, int *insertMode)
388 {
389     struct heap_cread_info *p = vp;
390     struct heap_info *hi = p->hi;
391
392     if (p->mode == 1)
393     {
394         *insertMode = p->key[0];
395         memcpy (*dst, p->key+1, sizeof(struct it_key));
396         (*dst) += sizeof(struct it_key);
397         p->mode = 2;
398         return 1;
399     }
400     strcpy (p->prev_name, p->cur_name);
401     if (!(p->more = heap_read_one (hi, p->cur_name, p->key)))
402         return 0;
403     if (*p->cur_name && strcmp (p->cur_name, p->prev_name))
404     {
405         p->mode = 1;
406         return 0;
407     }
408     *insertMode = p->key[0];
409     memcpy (*dst, p->key+1, sizeof(struct it_key));
410     (*dst) += sizeof(struct it_key);
411     return 1;
412 }
413
414 int heap_inpc (struct heap_info *hi)
415 {
416     struct heap_cread_info hci;
417     ISAMC_I isamc_i = xmalloc (sizeof(*isamc_i));
418
419     hci.key = xmalloc (KEY_SIZE);
420     hci.mode = 1;
421     hci.hi = hi;
422     hci.more = heap_read_one (hi, hci.cur_name, hci.key);
423
424     isamc_i->clientData = &hci;
425     isamc_i->read_item = heap_cread_item;
426
427     while (hci.more)
428     {
429         char this_name[INP_NAME_MAX];
430         ISAMC_P isamc_p, isamc_p2;
431         char *dict_info;
432
433         strcpy (this_name, hci.cur_name);
434         logf (LOG_DEBUG, "inserting %s", 1+hci.cur_name);
435         no_diffs++;
436         if ((dict_info = dict_lookup (hi->dict, hci.cur_name)))
437         {
438             memcpy (&isamc_p, dict_info+1, sizeof(ISAMC_P));
439             isamc_p2 = isc_merge (hi->isamc, isamc_p, isamc_i);
440             if (!isamc_p2)
441             {
442                 no_deletions++;
443                 if (!dict_delete (hi->dict, this_name))
444                     abort();
445             }
446             else 
447             {
448                 no_updates++;
449                 if (isamc_p2 != isamc_p)
450                     dict_insert (hi->dict, this_name,
451                                  sizeof(ISAMC_P), &isamc_p2);
452             }
453         } 
454         else
455         {
456             isamc_p = isc_merge (hi->isamc, 0, isamc_i);
457             no_insertions++;
458             dict_insert (hi->dict, this_name, sizeof(ISAMC_P), &isamc_p);
459         }
460     }
461     xfree (isamc_i);
462     return 0;
463
464
465 int heap_inp (struct heap_info *hi)
466 {
467     char *info;
468     char next_name[INP_NAME_MAX];
469     char cur_name[INP_NAME_MAX];
470     int key_buf_size = INP_BUF_START;
471     int key_buf_ptr;
472     char *next_key;
473     char *key_buf;
474     int more;
475     
476     next_key = xmalloc (KEY_SIZE);
477     key_buf = xmalloc (key_buf_size);
478     more = heap_read_one (hi, cur_name, key_buf);
479     while (more)                   /* EOF ? */
480     {
481         int nmemb;
482         key_buf_ptr = KEY_SIZE;
483         while (1)
484         {
485             if (!(more = heap_read_one (hi, next_name, next_key)))
486                 break;
487             if (*next_name && strcmp (next_name, cur_name))
488                 break;
489             memcpy (key_buf + key_buf_ptr, next_key, KEY_SIZE);
490             key_buf_ptr += KEY_SIZE;
491             if (key_buf_ptr+KEY_SIZE >= key_buf_size)
492             {
493                 char *new_key_buf;
494                 new_key_buf = xmalloc (key_buf_size + INP_BUF_ADD);
495                 memcpy (new_key_buf, key_buf, key_buf_size);
496                 key_buf_size += INP_BUF_ADD;
497                 xfree (key_buf);
498                 key_buf = new_key_buf;
499             }
500         }
501         no_diffs++;
502         nmemb = key_buf_ptr / KEY_SIZE;
503         assert (nmemb*KEY_SIZE == key_buf_ptr);
504         if ((info = dict_lookup (hi->dict, cur_name)))
505         {
506             ISAM_P isam_p, isam_p2;
507             logf (LOG_DEBUG, "updating %s", 1+cur_name);
508             memcpy (&isam_p, info+1, sizeof(ISAM_P));
509             isam_p2 = is_merge (hi->isam, isam_p, nmemb, key_buf);
510             if (!isam_p2)
511             {
512                 no_deletions++;
513                 if (!dict_delete (hi->dict, cur_name))
514                     abort ();
515             }
516             else 
517             {
518                 no_updates++;
519                 if (isam_p2 != isam_p)
520                     dict_insert (hi->dict, cur_name, sizeof(ISAM_P), &isam_p2);
521             }
522         }
523         else
524         {
525             ISAM_P isam_p;
526             logf (LOG_DEBUG, "inserting %s", 1+cur_name);
527             no_insertions++;
528             isam_p = is_merge (hi->isam, 0, nmemb, key_buf);
529             dict_insert (hi->dict, cur_name, sizeof(ISAM_P), &isam_p);
530         }
531         memcpy (key_buf, next_key, KEY_SIZE);
532         strcpy (cur_name, next_name);
533     }
534     return 0;
535 }
536
537 struct progressInfo {
538     time_t   startTime;
539     time_t   lastTime;
540     off_t    totalBytes;
541     off_t    totalOffset;
542 };
543
544 void progressFunc (struct key_file *keyp, void *info)
545 {
546     struct progressInfo *p = info;
547     time_t now, remaining;
548
549     if (keyp->buf_size <= 0 || p->totalBytes <= 0)
550         return ;
551     time (&now);
552
553     if (now >= p->lastTime+10)
554     {
555         p->lastTime = now;
556         remaining = (now - p->startTime)*
557             ((double) p->totalBytes/p->totalOffset - 1.0);
558         if (remaining <= 130)
559             logf (LOG_LOG, "Merge %2.1f%% completed; %ld seconds remaining",
560                  (100.0*p->totalOffset) / p->totalBytes, (long) remaining);
561         else
562             logf (LOG_LOG, "Merge %2.1f%% completed; %ld minutes remaining",
563                  (100.0*p->totalOffset) / p->totalBytes, (long) remaining/60);
564     }
565     p->totalOffset += keyp->buf_size;
566 }
567
568 void key_input (int nkeys, int cache)
569                 
570 {
571     Dict dict;
572     ISAM isam = NULL;
573     ISAMC isamc = NULL;
574     struct key_file **kf;
575     char rbuf[1024];
576     int i, r;
577     struct heap_info *hi;
578     struct progressInfo progressInfo;
579
580     if (nkeys < 0)
581     {
582         char fname[1024];
583         nkeys = 0;
584         while (1)
585         {
586             getFnameTmp (fname, nkeys+1);
587             if (access (fname, R_OK) == -1)
588                 break;
589             nkeys++;
590         }
591         if (!nkeys)
592             return ;
593     }
594     dict = dict_open (FNAME_DICT, cache, 1);
595     if (!dict)
596     {
597         logf (LOG_FATAL, "dict_open fail");
598         exit (1);
599     }
600     if (res_get_match (common_resource, "isam", "c", NULL))
601     {
602         isamc = isc_open (FNAME_ISAMC, 1, key_isamc_m ());
603         if (!isamc)
604         {
605             logf (LOG_FATAL, "isc_open fail");
606             exit (1);
607         }
608     }
609     else
610     {
611         isam = is_open (FNAME_ISAM, key_compare, 1, sizeof(struct it_key));
612         if (!isam)
613         {
614             logf (LOG_FATAL, "is_open fail");
615             exit (1);
616         }
617     }
618     kf = xmalloc ((1+nkeys) * sizeof(*kf));
619     progressInfo.totalBytes = 0;
620     progressInfo.totalOffset = 0;
621     time (&progressInfo.startTime);
622     time (&progressInfo.lastTime);
623     for (i = 1; i<=nkeys; i++)
624     {
625         kf[i] = key_file_init (i, 32768);
626         kf[i]->readHandler = progressFunc;
627         kf[i]->readInfo = &progressInfo;
628         progressInfo.totalBytes += kf[i]->length;
629         progressInfo.totalOffset += kf[i]->buf_size;
630     }
631     hi = key_heap_init (nkeys, key_qsort_compare);
632     hi->dict = dict;
633     hi->isam = isam;
634     hi->isamc = isamc;
635
636     for (i = 1; i<=nkeys; i++)
637         if ((r = key_file_read (kf[i], rbuf)))
638             key_heap_insert (hi, rbuf, r, kf[i]);
639     if (isamc)
640         heap_inpc (hi);
641     else
642         heap_inp (hi);
643     dict_close (dict);
644     if (isam)
645         is_close (isam);
646     if (isamc)
647         isc_close (isamc);
648    
649     for (i = 1; i<=nkeys; i++)
650     {
651         getFnameTmp (rbuf, i);
652         unlink (rbuf);
653     }
654     logf (LOG_LOG, "Iterations . . .%7d", no_iterations);
655     logf (LOG_LOG, "Distinct words .%7d", no_diffs);
656     logf (LOG_LOG, "Updates. . . . .%7d", no_updates);
657     logf (LOG_LOG, "Deletions. . . .%7d", no_deletions);
658     logf (LOG_LOG, "Insertions . . .%7d", no_insertions);
659 }
660
661