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