2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.22 1995-10-17 18:02:07 adam
8 * New feature: databases. Implemented as prefix to words in dictionary.
10 * Revision 1.21 1995/10/10 12:24:38 adam
11 * Temporary sort files are compressed.
13 * Revision 1.20 1995/10/06 13:52:05 adam
14 * Bug fixes. Handler may abort further scanning.
16 * Revision 1.19 1995/10/04 12:55:16 adam
17 * Bug fix in ranked search. Use=Any keys inserted.
19 * Revision 1.18 1995/10/04 09:37:08 quinn
22 * Revision 1.17 1995/10/03 14:28:57 adam
23 * Buffered read in extract works.
25 * Revision 1.16 1995/10/03 14:28:45 adam
26 * Work on more effecient read handler in extract.
28 * Revision 1.15 1995/10/02 15:42:53 adam
29 * Extract uses file descriptors instead of FILE pointers.
31 * Revision 1.14 1995/10/02 15:29:13 adam
32 * More logging in file_extract.
34 * Revision 1.13 1995/09/29 14:01:39 adam
37 * Revision 1.12 1995/09/28 14:22:56 adam
38 * Sort uses smaller temporary files.
40 * Revision 1.11 1995/09/28 12:10:31 adam
41 * Bug fixes. Field prefix used in queries.
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.
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.
51 * Revision 1.8 1995/09/14 07:48:22 adam
52 * Record control management.
54 * Revision 1.7 1995/09/11 13:09:32 adam
55 * More work on relevance feedback.
57 * Revision 1.6 1995/09/08 14:52:27 adam
58 * Minor changes. Dictionary is lower case now.
60 * Revision 1.5 1995/09/06 16:11:16 adam
61 * Option: only one word key per file.
63 * Revision 1.4 1995/09/05 15:28:39 adam
64 * More work on search engine.
66 * Revision 1.3 1995/09/04 12:33:41 adam
67 * Various cleanup. YAZ util used instead.
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.
74 * Revision 1.1 1995/09/01 14:06:35 adam
75 * Split of work into more files.
89 static SYSNO sysno_next;
90 static int sys_idx_fd = -1;
94 static char *key_databaseName;
95 static char **key_buf;
96 static size_t ptr_top;
99 static int key_file_no;
101 void key_open (int mem)
107 key_buf = xmalloc (mem);
108 ptr_top = mem/sizeof(char*);
113 if (!(file_idx = dict_open (FNAME_FILE_DICT, 40, 1)))
115 logf (LOG_FATAL, "dict_open fail of %s", "fileidx");
118 file_key = dict_lookup (file_idx, ".");
120 memcpy (&sysno_next, (char*)file_key+1, sizeof(sysno_next));
123 if ((sys_idx_fd = open (FNAME_SYS_IDX, O_RDWR|O_CREAT, 0666)) == -1)
125 logf (LOG_FATAL|LOG_ERRNO, "open %s", FNAME_SYS_IDX);
136 void encode_key_init (struct encode_info *i)
142 char *encode_key_int (int d, char *bp)
151 else if (d <= 4194303)
153 *bp++ = 128 + (d>>16);
154 *bp++ = (d>>8) & 255;
159 *bp++ = 192 + (d>>24);
160 *bp++ = (d>>16) & 255;
161 *bp++ = (d>>8) & 255;
167 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
172 while ((*bp++ = *k++))
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)
178 i->sysno = key.sysno;
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)
185 logf (LOG_FATAL|LOG_ERRNO, "fwrite");
190 void key_flush (void)
195 struct encode_info encode_info;
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);
205 if (!(outf = fopen (out_fname, "w")))
207 logf (LOG_FATAL|LOG_ERRNO, "fopen (4) %s", out_fname);
210 logf (LOG_LOG, "writing section %d", key_file_no);
211 prevcp = cp = key_buf[ptr_top-ptr_i];
213 encode_key_init (&encode_info);
214 encode_key_write (cp, &encode_info, outf);
217 cp = key_buf[ptr_top-ptr_i];
218 if (strcmp (cp, prevcp))
220 encode_key_init (&encode_info);
221 encode_key_write (cp, &encode_info, outf);
225 encode_key_write (cp + strlen(cp), &encode_info, outf);
229 logf (LOG_FATAL|LOG_ERRNO, "fclose %s", out_fname);
232 logf (LOG_LOG, "finished section %d", key_file_no);
242 dict_insert (file_idx, ".", sizeof(sysno_next), &sysno_next);
243 dict_close (file_idx);
247 static void wordInit (RecWord *p)
251 p->which = Word_String;
254 static void wordAdd (const RecWord *p)
259 if (kused + 1024 > (ptr_top-ptr_i)*sizeof(char*))
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);
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';
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);
283 static void wordAddAny (const RecWord *p)
285 if (p->attrSet != 1 || p->attrUse != 1016)
289 memcpy (&w, p, sizeof(w));
298 #define FILE_READ_BUF 1
300 static char *file_buf;
301 static int file_offset;
302 static int file_bufsize;
304 static void file_read_start (int fd)
307 file_buf = xmalloc (4096);
308 file_bufsize = read (fd, file_buf, 4096);
311 static void file_read_stop (int fd)
316 static int file_read (int fd, char *buf, size_t count)
318 int l = file_bufsize - file_offset;
324 memcpy (buf, file_buf + file_offset, l);
326 if (count > file_bufsize)
328 if ((r = read (fd, buf + l, count)) == -1)
330 logf (LOG_FATAL|LOG_ERRNO, "read");
337 file_bufsize = r = read (fd, file_buf, 4096);
340 logf (LOG_FATAL|LOG_ERRNO, "read");
346 memcpy (buf + l, file_buf, r);
352 memcpy (buf + l, file_buf, count - l);
356 memcpy (buf, file_buf + file_offset, count);
357 file_offset += count;
361 static int file_read (int fd, char *buf, size_t count)
363 return read (fd, buf, count);
366 void file_extract (int cmd, const char *fname, const char *kname,
373 const char *file_type;
375 struct recExtractCtrl extractCtrl;
378 key_databaseName = databaseName;
379 for (i = strlen(fname); --i >= 0; )
385 else if (fname[i] == '.')
387 strcpy (ext, fname+i+1);
390 sprintf (ext_res, "fileExtension.%s", ext);
391 if (!(file_type = res_get (common_resource, ext_res)))
393 if (!(rt = recType_byName (file_type)))
395 logf (LOG_DEBUG, "%c %s k=%s", cmd, fname, kname);
396 file_info = dict_lookup (file_idx, kname);
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);
406 memcpy (&sysno, (char*) file_info+1, sizeof(sysno));
408 if ((extractCtrl.fd = open (fname, O_RDONLY)) == -1)
410 logf (LOG_WARN|LOG_ERRNO, "open %s", fname);
413 extractCtrl.subType = "";
414 extractCtrl.init = wordInit;
415 extractCtrl.add = wordAddAny;
417 file_read_start (extractCtrl.fd);
419 extractCtrl.readf = file_read;
422 r = (*rt->extract)(&extractCtrl);
424 file_read_stop (extractCtrl.fd);
426 close (extractCtrl.fd);
428 logf (LOG_WARN, "Couldn't extract file %s, code %d", fname, r);