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