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