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