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