2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.17 1995-10-03 14:28:57 adam
8 * Buffered read in extract works.
10 * Revision 1.16 1995/10/03 14:28:45 adam
11 * Work on more effecient read handler in extract.
13 * Revision 1.15 1995/10/02 15:42:53 adam
14 * Extract uses file descriptors instead of FILE pointers.
16 * Revision 1.14 1995/10/02 15:29:13 adam
17 * More logging in file_extract.
19 * Revision 1.13 1995/09/29 14:01:39 adam
22 * Revision 1.12 1995/09/28 14:22:56 adam
23 * Sort uses smaller temporary files.
25 * Revision 1.11 1995/09/28 12:10:31 adam
26 * Bug fixes. Field prefix used in queries.
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.
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.
36 * Revision 1.8 1995/09/14 07:48:22 adam
37 * Record control management.
39 * Revision 1.7 1995/09/11 13:09:32 adam
40 * More work on relevance feedback.
42 * Revision 1.6 1995/09/08 14:52:27 adam
43 * Minor changes. Dictionary is lower case now.
45 * Revision 1.5 1995/09/06 16:11:16 adam
46 * Option: only one word key per file.
48 * Revision 1.4 1995/09/05 15:28:39 adam
49 * More work on search engine.
51 * Revision 1.3 1995/09/04 12:33:41 adam
52 * Various cleanup. YAZ util used instead.
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.
59 * Revision 1.1 1995/09/01 14:06:35 adam
60 * Split of work into more files.
74 static SYSNO sysno_next;
75 static int sys_idx_fd = -1;
79 static char **key_buf;
80 static size_t ptr_top;
83 static int key_file_no;
85 void key_open (int mem)
91 key_buf = xmalloc (mem);
92 ptr_top = mem/sizeof(char*);
97 if (!(file_idx = dict_open (FNAME_FILE_DICT, 40, 1)))
99 logf (LOG_FATAL, "dict_open fail of %s", "fileidx");
102 file_key = dict_lookup (file_idx, ".");
104 memcpy (&sysno_next, (char*)file_key+1, sizeof(sysno_next));
107 if ((sys_idx_fd = open (FNAME_SYS_IDX, O_RDWR|O_CREAT, 0666)) == -1)
109 logf (LOG_FATAL|LOG_ERRNO, "open %s", FNAME_SYS_IDX);
114 void key_flush (void)
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);
128 if (!(outf = fopen (out_fname, "w")))
130 logf (LOG_FATAL|LOG_ERRNO, "fopen (4) %s", out_fname);
133 logf (LOG_LOG, "writing section %d", key_file_no);
134 prevcp = cp = key_buf[ptr_top-ptr_i];
136 if (fwrite (cp, strlen (cp)+2+sizeof(struct it_key), 1, outf) != 1)
138 logf (LOG_FATAL|LOG_ERRNO, "fwrite %s", out_fname);
143 cp = key_buf[ptr_top-ptr_i];
144 if (strcmp (cp, prevcp))
146 if (fwrite (cp, strlen (cp)+2+sizeof(struct it_key), 1,
149 logf (LOG_FATAL|LOG_ERRNO, "fwrite %s", out_fname);
156 cp = strlen (cp) + cp;
157 if (fwrite (cp, 2+sizeof(struct it_key), 1, outf) != 1)
159 logf (LOG_FATAL|LOG_ERRNO, "fwrite %s", out_fname);
166 logf (LOG_FATAL|LOG_ERRNO, "fclose %s", out_fname);
169 logf (LOG_LOG, "finished section %d", key_file_no);
179 dict_insert (file_idx, ".", sizeof(sysno_next), &sysno_next);
180 dict_close (file_idx);
184 static void wordInit (RecWord *p)
188 p->which = Word_String;
191 static void wordAdd (const RecWord *p)
196 if (kused + 1024 > (ptr_top-ptr_i)*sizeof(char*))
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);
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';
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);
219 #define FILE_READ_BUF 1
221 static char *file_buf;
222 static int file_offset;
223 static int file_bufsize;
225 static void file_read_start (int fd)
228 file_buf = xmalloc (4096);
229 file_bufsize = read (fd, file_buf, 4096);
232 static void file_read_stop (int fd)
237 static int file_read (int fd, char *buf, size_t count)
239 int l = file_bufsize - file_offset;
245 memcpy (buf, file_buf + file_offset, l);
247 if (count > file_bufsize)
249 if ((r = read (fd, buf + l, count)) == -1)
251 logf (LOG_FATAL|LOG_ERRNO, "read");
258 file_bufsize = r = read (fd, file_buf, 4096);
261 logf (LOG_FATAL|LOG_ERRNO, "read");
267 memcpy (buf + l, file_buf, r);
273 memcpy (buf + l, file_buf, count - l);
277 memcpy (buf, file_buf + file_offset, count);
278 file_offset += count;
282 static int file_read (int fd, char *buf, size_t count)
284 return read (fd, buf, count);
287 void file_extract (int cmd, const char *fname, const char *kname)
293 const char *file_type;
295 struct recExtractCtrl extractCtrl;
298 for (i = strlen(fname); --i >= 0; )
304 else if (fname[i] == '.')
306 strcpy (ext, fname+i+1);
309 sprintf (ext_res, "fileExtension.%s", ext);
310 if (!(file_type = res_get (common_resource, ext_res)))
312 if (!(rt = recType_byName (file_type)))
314 logf (LOG_DEBUG, "%c %s k=%s", cmd, fname, kname);
315 file_info = dict_lookup (file_idx, kname);
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);
325 memcpy (&sysno, (char*) file_info+1, sizeof(sysno));
327 if ((extractCtrl.fd = open (fname, O_RDONLY)) == -1)
329 logf (LOG_WARN|LOG_ERRNO, "open %s", fname);
332 extractCtrl.subType = "";
333 extractCtrl.init = wordInit;
334 extractCtrl.add = wordAdd;
336 file_read_start (extractCtrl.fd);
338 extractCtrl.readf = file_read;
340 file_read_stop (extractCtrl.fd);
344 r = (*rt->extract)(&extractCtrl);
345 close (extractCtrl.fd);
347 logf (LOG_WARN, "Couldn't extract file %s, code %d", fname, r);