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