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