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