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