Implemented detection of database availability.
[idzebra-moved-to-github.git] / index / extract.c
1 /*
2  * Copyright (C) 1994-1995, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: extract.c,v $
7  * Revision 1.23  1995-10-27 14:00:10  adam
8  * Implemented detection of database availability.
9  *
10  * Revision 1.22  1995/10/17  18:02:07  adam
11  * New feature: databases. Implemented as prefix to words in dictionary.
12  *
13  * Revision 1.21  1995/10/10  12:24:38  adam
14  * Temporary sort files are compressed.
15  *
16  * Revision 1.20  1995/10/06  13:52:05  adam
17  * Bug fixes. Handler may abort further scanning.
18  *
19  * Revision 1.19  1995/10/04  12:55:16  adam
20  * Bug fix in ranked search. Use=Any keys inserted.
21  *
22  * Revision 1.18  1995/10/04  09:37:08  quinn
23  * Fixed bug.
24  *
25  * Revision 1.17  1995/10/03  14:28:57  adam
26  * Buffered read in extract works.
27  *
28  * Revision 1.16  1995/10/03  14:28:45  adam
29  * Work on more effecient read handler in extract.
30  *
31  * Revision 1.15  1995/10/02  15:42:53  adam
32  * Extract uses file descriptors instead of FILE pointers.
33  *
34  * Revision 1.14  1995/10/02  15:29:13  adam
35  * More logging in file_extract.
36  *
37  * Revision 1.13  1995/09/29  14:01:39  adam
38  * Bug fixes.
39  *
40  * Revision 1.12  1995/09/28  14:22:56  adam
41  * Sort uses smaller temporary files.
42  *
43  * Revision 1.11  1995/09/28  12:10:31  adam
44  * Bug fixes. Field prefix used in queries.
45  *
46  * Revision 1.10  1995/09/28  09:19:41  adam
47  * xfree/xmalloc used everywhere.
48  * Extract/retrieve method seems to work for text records.
49  *
50  * Revision 1.9  1995/09/27  12:22:28  adam
51  * More work on extract in record control.
52  * Field name is not in isam keys but in prefix in dictionary words.
53  *
54  * Revision 1.8  1995/09/14  07:48:22  adam
55  * Record control management.
56  *
57  * Revision 1.7  1995/09/11  13:09:32  adam
58  * More work on relevance feedback.
59  *
60  * Revision 1.6  1995/09/08  14:52:27  adam
61  * Minor changes. Dictionary is lower case now.
62  *
63  * Revision 1.5  1995/09/06  16:11:16  adam
64  * Option: only one word key per file.
65  *
66  * Revision 1.4  1995/09/05  15:28:39  adam
67  * More work on search engine.
68  *
69  * Revision 1.3  1995/09/04  12:33:41  adam
70  * Various cleanup. YAZ util used instead.
71  *
72  * Revision 1.2  1995/09/04  09:10:34  adam
73  * More work on index add/del/update.
74  * Merge sort implemented.
75  * Initial work on z39 server.
76  *
77  * Revision 1.1  1995/09/01  14:06:35  adam
78  * Split of work into more files.
79  *
80  */
81 #include <stdio.h>
82 #include <assert.h>
83 #include <unistd.h>
84 #include <fcntl.h>
85 #include <ctype.h>
86
87 #include <alexutil.h>
88 #include <recctrl.h>
89 #include "index.h"
90
91 static Dict file_idx;
92 static SYSNO sysno_next;
93 static int sys_idx_fd = -1;
94
95 static int key_cmd;
96 static int key_sysno;
97 static char *key_databaseName;
98 static char **key_buf;
99 static size_t ptr_top;
100 static size_t ptr_i;
101 static size_t kused;
102 static int key_file_no;
103
104 void key_open (int mem)
105 {
106     void *file_key;
107
108     if (mem < 50000)
109         mem = 50000;
110     key_buf = xmalloc (mem);
111     ptr_top = mem/sizeof(char*);
112     ptr_i = 0;
113     kused = 0;
114     key_file_no = 0;
115
116     if (!(file_idx = dict_open (FNAME_FILE_DICT, 40, 1)))
117     {
118         logf (LOG_FATAL, "dict_open fail of %s", "fileidx");
119         exit (1);
120     }
121     file_key = dict_lookup (file_idx, ".");
122     if (file_key)
123         memcpy (&sysno_next, (char*)file_key+1, sizeof(sysno_next));
124     else
125         sysno_next = 1;
126     if ((sys_idx_fd = open (FNAME_SYS_IDX, O_RDWR|O_CREAT, 0666)) == -1)
127     {
128         logf (LOG_FATAL|LOG_ERRNO, "open %s", FNAME_SYS_IDX);
129         exit (1);
130     }
131 }
132
133 struct encode_info {
134     int  sysno;
135     int  seqno;
136     char buf[512];
137 };
138
139 void encode_key_init (struct encode_info *i)
140 {
141     i->sysno = 0;
142     i->seqno = 0;
143 }
144
145 char *encode_key_int (int d, char *bp)
146 {
147     if (d <= 63)
148         *bp++ = d;
149     else if (d <= 16383)
150     {
151         *bp++ = 64 + (d>>8);
152         *bp++ = d  & 255;
153     }
154     else if (d <= 4194303)
155     {
156         *bp++ = 128 + (d>>16);
157         *bp++ = (d>>8) & 255;
158         *bp++ = d & 255;
159     }
160     else
161     {
162         *bp++ = 192 + (d>>24);
163         *bp++ = (d>>16) & 255;
164         *bp++ = (d>>8) & 255;
165         *bp++ = d & 255;
166     }
167     return bp;
168 }
169
170 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
171 {
172     struct it_key key;
173     char *bp = i->buf;
174
175     while ((*bp++ = *k++))
176         ;
177     memcpy (&key, k+1, sizeof(struct it_key));
178     bp = encode_key_int ( (key.sysno - i->sysno) * 2 + *k, bp);
179     if (i->sysno != key.sysno)
180     {
181         i->sysno = key.sysno;
182         i->seqno = 0;
183     }
184     bp = encode_key_int (key.seqno - i->seqno, bp);
185     i->seqno = key.seqno;
186     if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
187     {
188         logf (LOG_FATAL|LOG_ERRNO, "fwrite");
189         exit (1);
190     }
191 }
192
193 void key_flush (void)
194 {
195     FILE *outf;
196     char out_fname[200];
197     char *prevcp, *cp;
198     struct encode_info encode_info;
199     
200     if (ptr_i <= 0)
201         return;
202
203     key_file_no++;
204     logf (LOG_LOG, "sorting section %d", key_file_no);
205     qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_qsort_compare);
206     sprintf (out_fname, TEMP_FNAME, key_file_no);
207
208     if (!(outf = fopen (out_fname, "w")))
209     {
210         logf (LOG_FATAL|LOG_ERRNO, "fopen (4) %s", out_fname);
211         exit (1);
212     }
213     logf (LOG_LOG, "writing section %d", key_file_no);
214     prevcp = cp = key_buf[ptr_top-ptr_i];
215     
216     encode_key_init (&encode_info);
217     encode_key_write (cp, &encode_info, outf);
218     while (--ptr_i > 0)
219     {
220         cp = key_buf[ptr_top-ptr_i];
221         if (strcmp (cp, prevcp))
222         {
223             encode_key_init (&encode_info);
224             encode_key_write (cp, &encode_info, outf);
225             prevcp = cp;
226         }
227         else
228             encode_key_write (cp + strlen(cp), &encode_info, outf);
229     }
230     if (fclose (outf))
231     {
232         logf (LOG_FATAL|LOG_ERRNO, "fclose %s", out_fname);
233         exit (1);
234     }
235     logf (LOG_LOG, "finished section %d", key_file_no);
236     ptr_i = 0;
237     kused = 0;
238 }
239
240 int key_close (void)
241 {
242     key_flush ();
243     xfree (key_buf);
244     close (sys_idx_fd);
245     dict_insert (file_idx, ".", sizeof(sysno_next), &sysno_next);
246     dict_close (file_idx);
247     return key_file_no;
248 }
249
250 static void wordInit (RecWord *p)
251 {
252     p->attrSet = 1;
253     p->attrUse = 1016;
254     p->which = Word_String;
255 }
256
257 static void wordAdd (const RecWord *p)
258 {
259     struct it_key key;
260     size_t i;
261
262     if (kused + 1024 > (ptr_top-ptr_i)*sizeof(char*))
263         key_flush ();
264     ++ptr_i;
265     key_buf[ptr_top-ptr_i] = (char*)key_buf + kused;
266     kused += index_word_prefix ((char*)key_buf + kused,
267                                 p->attrSet, p->attrUse,
268                                 key_databaseName);
269     switch (p->which)
270     {
271     case Word_String:
272         for (i = 0; p->u.string[i]; i++)
273             ((char*)key_buf) [kused++] = index_char_cvt (p->u.string[i]);
274         ((char*)key_buf) [kused++] = '\0';
275         break;
276     default:
277         return ;
278     }
279     ((char*) key_buf)[kused++] = ((key_cmd == 'a') ? 1 : 0);
280     key.sysno = key_sysno;
281     key.seqno = p->seqno;
282     memcpy ((char*)key_buf + kused, &key, sizeof(key));
283     kused += sizeof(key);
284 }
285
286 static void wordAddAny (const RecWord *p)
287 {
288     if (p->attrSet != 1 || p->attrUse != 1016)
289     {
290         RecWord w;
291
292         memcpy (&w, p, sizeof(w));
293         w.attrSet = 1;
294         w.attrUse = 1016;
295         wordAdd (&w);
296     }
297     wordAdd (p);
298 }
299
300
301 #define FILE_READ_BUF 1
302 #if FILE_READ_BUF
303 static char *file_buf;
304 static int file_offset;
305 static int file_bufsize;
306
307 static void file_read_start (int fd)
308 {
309     file_offset = 0;
310     file_buf = xmalloc (4096);
311     file_bufsize = read (fd, file_buf, 4096);
312 }
313
314 static void file_read_stop (int fd)
315 {
316     xfree (file_buf);
317 }
318
319 static int file_read (int fd, char *buf, size_t count)
320 {
321     int l = file_bufsize - file_offset;
322
323     if (count > l)
324     {
325         int r;
326         if (l > 0)
327             memcpy (buf, file_buf + file_offset, l);
328         count = count-l;
329         if (count > file_bufsize)
330         {
331             if ((r = read (fd, buf + l, count)) == -1)
332             {
333                 logf (LOG_FATAL|LOG_ERRNO, "read");
334                 exit (1);
335             }
336             file_bufsize = 0;
337             file_offset = 0;
338             return r;
339         }
340         file_bufsize = r = read (fd, file_buf, 4096);
341         if (r == -1)
342         {
343             logf (LOG_FATAL|LOG_ERRNO, "read");
344             exit (1);
345         }
346         else if (r <= count)
347         {
348             file_offset = r;
349             memcpy (buf + l, file_buf, r);
350             return l + r;
351         }
352         else
353         {
354             file_offset = count;
355             memcpy (buf + l, file_buf, count - l);
356             return count;
357         }
358     }
359     memcpy (buf, file_buf + file_offset, count);
360     file_offset += count;
361     return count;
362 }
363 #else
364 static int file_read (int fd, char *buf, size_t count)
365 {
366     return read (fd, buf, count);
367 }
368 #endif
369 void file_extract (int cmd, const char *fname, const char *kname,
370                    char *databaseName)
371 {
372     int i, r;
373     char ext[128];
374     SYSNO sysno;
375     char ext_res[128];
376     const char *file_type;
377     void *file_info;
378     struct recExtractCtrl extractCtrl;
379     RecType rt;
380
381     key_databaseName = databaseName;
382     for (i = strlen(fname); --i >= 0; )
383         if (fname[i] == '/')
384         {
385             strcpy (ext, "");
386             break;
387         }
388         else if (fname[i] == '.')
389         {
390             strcpy (ext, fname+i+1);
391             break;
392         }
393     sprintf (ext_res, "fileExtension.%s", ext);
394     if (!(file_type = res_get (common_resource, ext_res)))
395         return;
396     if (!(rt = recType_byName (file_type)))
397         return;
398     logf (LOG_DEBUG, "%c %s k=%s", cmd, fname, kname);
399     file_info = dict_lookup (file_idx, kname);
400     if (!file_info)
401     {
402         sysno = sysno_next++;
403         dict_insert (file_idx, kname, sizeof(sysno), &sysno);
404         lseek (sys_idx_fd, sysno * SYS_IDX_ENTRY_LEN, SEEK_SET);
405         write (sys_idx_fd, file_type, strlen (file_type)+1);
406         write (sys_idx_fd, kname, strlen(kname)+1);
407     }
408     else
409         memcpy (&sysno, (char*) file_info+1, sizeof(sysno));
410
411     if ((extractCtrl.fd = open (fname, O_RDONLY)) == -1)
412     {
413         logf (LOG_WARN|LOG_ERRNO, "open %s", fname);
414         return;
415     }
416     extractCtrl.subType = "";
417     extractCtrl.init = wordInit;
418     extractCtrl.add = wordAddAny;
419 #if FILE_READ_BUF
420     file_read_start (extractCtrl.fd);
421 #endif
422     extractCtrl.readf = file_read;
423     key_sysno = sysno;
424     key_cmd = cmd;
425     r = (*rt->extract)(&extractCtrl);
426 #if FILE_READ_BUF
427     file_read_stop (extractCtrl.fd);
428 #endif
429     close (extractCtrl.fd);
430     if (r)
431         logf (LOG_WARN, "Couldn't extract file %s, code %d", fname, r);
432 }