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