Better progress report during register merge.
[idzebra-moved-to-github.git] / index / kinput.c
1 /*
2  * Copyright (C) 1994-1995, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: kinput.c,v $
7  * Revision 1.14  1996-02-07 14:06:37  adam
8  * Better progress report during register merge.
9  * New command: clean - removes temporary shadow files.
10  *
11  * Revision 1.13  1996/02/05  12:30:00  adam
12  * Logging reduced a bit.
13  * The remaining running time is estimated during register merge.
14  *
15  * Revision 1.12  1995/12/06  17:49:19  adam
16  * Uses dict_delete now.
17  *
18  * Revision 1.11  1995/12/06  16:06:43  adam
19  * Better diagnostics. Work on 'real' dictionary deletion.
20  *
21  * Revision 1.10  1995/12/06  12:41:22  adam
22  * New command 'stat' for the index program.
23  * Filenames can be read from stdin by specifying '-'.
24  * Bug fix/enhancement of the transformation from terms to regular
25  * expressons in the search engine.
26  *
27  * Revision 1.9  1995/10/10  12:24:39  adam
28  * Temporary sort files are compressed.
29  *
30  * Revision 1.8  1995/10/04  16:57:19  adam
31  * Key input and merge sort in one pass.
32  *
33  * Revision 1.7  1995/10/02  15:18:52  adam
34  * New member in recRetrieveCtrl: diagnostic.
35  *
36  * Revision 1.6  1995/09/29  15:51:56  adam
37  * First work on multi-way read.
38  *
39  * Revision 1.5  1995/09/29  14:01:43  adam
40  * Bug fixes.
41  *
42  * Revision 1.4  1995/09/28  14:22:57  adam
43  * Sort uses smaller temporary files.
44  *
45  * Revision 1.3  1995/09/06  16:11:17  adam
46  * Option: only one word key per file.
47  *
48  * Revision 1.2  1995/09/04  12:33:42  adam
49  * Various cleanup. YAZ util used instead.
50  *
51  * Revision 1.1  1995/09/04  09:10:37  adam
52  * More work on index add/del/update.
53  * Merge sort implemented.
54  * Initial work on z39 server.
55  *
56  */
57
58 #include <fcntl.h>
59 #include <unistd.h>
60 #include <stdlib.h>
61 #include <time.h>
62 #include <string.h>
63 #include <stdio.h>
64 #include <ctype.h>
65 #include <assert.h>
66
67 #include "index.h"
68
69 #define KEY_SIZE (1+sizeof(struct it_key))
70 #define INP_NAME_MAX 8192
71 #define INP_BUF_START 60000
72 #define INP_BUF_ADD  400000
73
74 static int no_diffs   = 0;
75 static int no_updates = 0;
76 static int no_deletions = 0;
77 static int no_insertions = 0;
78 static int no_iterations = 0;
79
80 struct key_file {
81     int   no;            /* file no */
82     off_t offset;        /* file offset */
83     unsigned char *buf;  /* buffer block */
84     size_t buf_size;     /* number of read bytes in block */
85     size_t chunk;        /* number of bytes allocated */
86     size_t buf_ptr;      /* current position in buffer */
87     char *prev_name;     /* last word read */
88     int   sysno;         /* last sysno */
89     int   seqno;         /* last seqno */
90     off_t length;        /* length of file */
91                          /* handler invoked in each read */
92     void (*readHandler)(struct key_file *keyp, void *rinfo);
93     void *readInfo;
94 };
95
96 void getFnameTmp (char *fname, int no)
97 {
98     sprintf (fname, TEMP_FNAME, no);
99 }
100
101 void key_file_chunk_read (struct key_file *f)
102 {
103     int nr = 0, r, fd;
104     char fname[256];
105     getFnameTmp (fname, f->no);
106     fd = open (fname, O_RDONLY);
107     if (fd == -1)
108     {
109         logf (LOG_FATAL|LOG_ERRNO, "cannot open %s", fname);
110         exit (1);
111     }
112     if (!f->length)
113     {
114         if ((f->length = lseek (fd, 0L, SEEK_END)) == (off_t) -1)
115         {
116             logf (LOG_FATAL|LOG_ERRNO, "cannot seek %s", fname);
117             exit (1);
118         }
119     }
120     if (lseek (fd, f->offset, SEEK_SET) == -1)
121     {
122         logf (LOG_FATAL|LOG_ERRNO, "cannot seek %s", fname);
123         exit (1);
124     }
125     while (f->chunk - nr > 0)
126     {
127         r = read (fd, f->buf + nr, f->chunk - nr);
128         if (r <= 0)
129             break;
130         nr += r;
131     }
132     if (r == -1)
133     {
134         logf (LOG_FATAL|LOG_ERRNO, "read of %s", fname);
135         exit (1);
136     }
137     f->buf_size = nr;
138     f->buf_ptr = 0;
139     if (f->readHandler)
140         (*f->readHandler)(f, f->readInfo);
141     close (fd);
142 }
143
144 struct key_file *key_file_init (int no, int chunk)
145 {
146     struct key_file *f;
147
148     f = xmalloc (sizeof(*f));
149     f->sysno = 0;
150     f->seqno = 0;
151     f->no = no;
152     f->chunk = chunk;
153     f->offset = 0;
154     f->length = 0;
155     f->readHandler = NULL;
156     f->buf = xmalloc (f->chunk);
157     f->prev_name = xmalloc (256);
158     *f->prev_name = '\0';
159     key_file_chunk_read (f);
160     return f;
161 }
162
163 int key_file_getc (struct key_file *f)
164 {
165     if (f->buf_ptr < f->buf_size)
166         return f->buf[(f->buf_ptr)++];
167     if (f->buf_size < f->chunk)
168         return EOF;
169     f->offset += f->buf_size;
170     key_file_chunk_read (f);
171     if (f->buf_ptr < f->buf_size)
172         return f->buf[(f->buf_ptr)++];
173     else
174         return EOF;
175 }
176
177 int key_file_decode (struct key_file *f)
178 {
179     int c, d;
180
181     c = key_file_getc (f);
182     switch (c & 192) 
183     {
184     case 0:
185         d = c;
186         break;
187     case 64:
188         d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
189         break;
190     case 128:
191         d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
192         d = (d << 8) + (key_file_getc (f) & 0xff);
193         break;
194     case 192:
195         d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
196         d = (d << 8) + (key_file_getc (f) & 0xff);
197         d = (d << 8) + (key_file_getc (f) & 0xff);
198         break;
199     }
200     return d;
201 }
202
203 int key_file_read (struct key_file *f, char *key)
204 {
205     int i, d, c;
206     struct it_key itkey;
207
208     c = key_file_getc (f);
209     if (c == 0)
210     {
211         strcpy (key, f->prev_name);
212         i = 1+strlen (key);
213     }
214     else if (c == EOF)
215         return 0;
216     else
217     {
218         i = 0;
219         key[i++] = c;
220         while ((key[i++] = key_file_getc (f)))
221             ;
222         strcpy (f->prev_name, key);
223         f->sysno = 0;
224     }
225     d = key_file_decode (f);
226     key[i++] = d & 1;
227     d = d >> 1;
228     itkey.sysno = d + f->sysno;
229     if (d) 
230     {
231         f->sysno = itkey.sysno;
232         f->seqno = 0;
233     }
234     d = key_file_decode (f);
235     itkey.seqno = d + f->seqno;
236     f->seqno = itkey.seqno;
237     memcpy (key + i, &itkey, sizeof(struct it_key));
238     return i + sizeof (struct it_key);
239 }
240
241 struct heap_info {
242     struct {
243         struct key_file **file;
244         char   **buf;
245     } info;
246     int    heapnum;
247     int    *ptr;
248     int    (*cmp)(const void *p1, const void *p2);
249 };
250
251 struct heap_info *key_heap_init (int nkeys,
252                                  int (*cmp)(const void *p1, const void *p2))
253 {
254     struct heap_info *hi;
255     int i;
256
257     hi = xmalloc (sizeof(*hi));
258     hi->info.file = xmalloc (sizeof(*hi->info.file) * (1+nkeys));
259     hi->info.buf = xmalloc (sizeof(*hi->info.buf) * (1+nkeys));
260     hi->heapnum = 0;
261     hi->ptr = xmalloc (sizeof(*hi->ptr) * (1+nkeys));
262     hi->cmp = cmp;
263     for (i = 0; i<= nkeys; i++)
264     {
265         hi->ptr[i] = i;
266         hi->info.buf[i] = xmalloc (512);
267     }
268     return hi;
269 }
270
271 static void key_heap_swap (struct heap_info *hi, int i1, int i2)
272 {
273     int swap;
274
275     swap = hi->ptr[i1];
276     hi->ptr[i1] = hi->ptr[i2];
277     hi->ptr[i2] = swap;
278 }
279
280
281 static void key_heap_delete (struct heap_info *hi)
282 {
283     int cur = 1, child = 2;
284
285     assert (hi->heapnum > 0);
286
287     key_heap_swap (hi, 1, hi->heapnum);
288     hi->heapnum--;
289     while (child <= hi->heapnum) {
290         if (child < hi->heapnum &&
291             (*hi->cmp)(&hi->info.buf[hi->ptr[child]],
292                        &hi->info.buf[hi->ptr[child+1]]) > 0)
293             child++;
294         if ((*hi->cmp)(&hi->info.buf[hi->ptr[cur]],
295                        &hi->info.buf[hi->ptr[child]]) > 0)
296         {            
297             key_heap_swap (hi, cur, child);
298             cur = child;
299             child = 2*cur;
300         }
301         else
302             break;
303     }
304 }
305
306 static void key_heap_insert (struct heap_info *hi, const char *buf, int nbytes,
307                              struct key_file *kf)
308 {
309     int cur, parent;
310
311     cur = ++(hi->heapnum);
312     memcpy (hi->info.buf[hi->ptr[cur]], buf, nbytes);
313     hi->info.file[hi->ptr[cur]] = kf;
314
315     parent = cur/2;
316     while (parent && (*hi->cmp)(&hi->info.buf[hi->ptr[parent]],
317                                 &hi->info.buf[hi->ptr[cur]]) > 0)
318     {
319         key_heap_swap (hi, cur, parent);
320         cur = parent;
321         parent = cur/2;
322     }
323 }
324
325 static int heap_read_one (struct heap_info *hi, char *name, char *key)
326 {
327     int n, r;
328     char rbuf[512];
329     struct key_file *kf;
330
331     if (!hi->heapnum)
332         return 0;
333     n = hi->ptr[1];
334     strcpy (name, hi->info.buf[n]);
335     kf = hi->info.file[n];
336     r = strlen(name);
337     memcpy (key, hi->info.buf[n] + r+1, KEY_SIZE);
338     key_heap_delete (hi);
339     if ((r = key_file_read (kf, rbuf)))
340         key_heap_insert (hi, rbuf, r, kf);
341     no_iterations++;
342     return 1;
343 }
344
345 int heap_inp (Dict dict, ISAM isam, struct heap_info *hi)
346 {
347     char *info;
348     char next_name[INP_NAME_MAX+1];
349     char cur_name[INP_NAME_MAX+1];
350     int key_buf_size = INP_BUF_START;
351     int key_buf_ptr;
352     char *next_key;
353     char *key_buf;
354     int more;
355     
356     next_key = xmalloc (KEY_SIZE);
357     key_buf = xmalloc (key_buf_size * (KEY_SIZE));
358     more = heap_read_one (hi, cur_name, key_buf);
359     while (more)                   /* EOF ? */
360     {
361         int nmemb;
362         key_buf_ptr = KEY_SIZE;
363         while (1)
364         {
365             if (!(more = heap_read_one (hi, next_name, next_key)))
366                 break;
367             if (*next_name && strcmp (next_name, cur_name))
368                 break;
369             memcpy (key_buf + key_buf_ptr, next_key, KEY_SIZE);
370             key_buf_ptr += KEY_SIZE;
371             if (key_buf_ptr+KEY_SIZE >= key_buf_size)
372             {
373                 char *new_key_buf;
374                 new_key_buf = xmalloc (key_buf_size + INP_BUF_ADD);
375                 memcpy (new_key_buf, key_buf, key_buf_size);
376                 key_buf_size += INP_BUF_ADD;
377                 xfree (key_buf);
378                 key_buf = new_key_buf;
379             }
380         }
381         no_diffs++;
382         nmemb = key_buf_ptr / KEY_SIZE;
383         assert (nmemb*KEY_SIZE == key_buf_ptr);
384         if ((info = dict_lookup (dict, cur_name)))
385         {
386             ISAM_P isam_p, isam_p2;
387             logf (LOG_DEBUG, "updating %s", cur_name);
388             memcpy (&isam_p, info+1, sizeof(ISAM_P));
389             isam_p2 = is_merge (isam, isam_p, nmemb, key_buf);
390             if (!isam_p2)
391             {
392                 no_deletions++;
393                 if (!dict_delete (dict, cur_name))
394                     abort ();
395             }
396             else 
397             {
398                 no_updates++;
399                 if (isam_p2 != isam_p)
400                     dict_insert (dict, cur_name, sizeof(ISAM_P), &isam_p2);
401             }
402         }
403         else
404         {
405             ISAM_P isam_p;
406             logf (LOG_DEBUG, "inserting %s", cur_name);
407             no_insertions++;
408             isam_p = is_merge (isam, 0, nmemb, key_buf);
409             dict_insert (dict, cur_name, sizeof(ISAM_P), &isam_p);
410         }
411         memcpy (key_buf, next_key, KEY_SIZE);
412         strcpy (cur_name, next_name);
413     }
414     return 0;
415 }
416
417 struct progressInfo {
418     time_t   startTime;
419     time_t   lastTime;
420     off_t    totalBytes;
421     off_t    totalOffset;
422 };
423
424 void progressFunc (struct key_file *keyp, void *info)
425 {
426     struct progressInfo *p = info;
427     time_t now, remaining;
428
429     if (keyp->buf_size <= 0 || p->totalBytes <= 0)
430         return ;
431     time (&now);
432
433     if (now >= p->lastTime+10)
434     {
435         p->lastTime = now;
436         remaining = (now - p->startTime)*
437             ((double) p->totalBytes/p->totalOffset - 1.0);
438         if (remaining <= 130)
439             logf (LOG_LOG, "Merge %2.1f%% completed; %ld seconds remaining",
440                  (100.0*p->totalOffset) / p->totalBytes, (long) remaining);
441         else
442             logf (LOG_LOG, "Merge %2.1f%% completed; %ld minutes remaining",
443                  (100.0*p->totalOffset) / p->totalBytes, (long) remaining/60);
444     }
445     p->totalOffset += keyp->buf_size;
446 }
447
448 void key_input (const char *dict_fname, const char *isam_fname,
449                 int nkeys, int cache)
450                 
451 {
452     Dict dict;
453     ISAM isam;
454     struct key_file **kf;
455     char rbuf[1024];
456     int i, r;
457     struct heap_info *hi;
458     struct progressInfo progressInfo;
459
460     dict = dict_open (dict_fname, cache, 1);
461     if (!dict)
462     {
463         logf (LOG_FATAL, "dict_open fail of `%s'", dict_fname);
464         exit (1);
465     }
466     isam = is_open (isam_fname, key_compare, 1, sizeof(struct it_key));
467     if (!isam)
468     {
469         logf (LOG_FATAL, "is_open fail of `%s'", isam_fname);
470         exit (1);
471     }
472
473     kf = xmalloc ((1+nkeys) * sizeof(*kf));
474     progressInfo.totalBytes = 0;
475     progressInfo.totalOffset = 0;
476     time (&progressInfo.startTime);
477     time (&progressInfo.lastTime);
478     for (i = 1; i<=nkeys; i++)
479     {
480         kf[i] = key_file_init (i, 32768);
481         kf[i]->readHandler = progressFunc;
482         kf[i]->readInfo = &progressInfo;
483         progressInfo.totalBytes += kf[i]->length;
484         progressInfo.totalOffset += kf[i]->buf_size;
485     }
486     hi = key_heap_init (nkeys, key_qsort_compare);
487     for (i = 1; i<=nkeys; i++)
488         if ((r = key_file_read (kf[i], rbuf)))
489             key_heap_insert (hi, rbuf, r, kf[i]);
490     heap_inp (dict, isam, hi);
491
492     dict_close (dict);
493     is_close (isam);
494     
495     for (i = 1; i<=nkeys; i++)
496     {
497         getFnameTmp (rbuf, i);
498         unlink (rbuf);
499     }
500     logf (LOG_LOG, "Iterations . . .%7d", no_iterations);
501     logf (LOG_LOG, "Distinct words .%7d", no_diffs);
502     logf (LOG_LOG, "Updates. . . . .%7d", no_updates);
503     logf (LOG_LOG, "Deletions. . . .%7d", no_deletions);
504     logf (LOG_LOG, "Insertions . . .%7d", no_insertions);
505 }
506
507