2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.28 1995-11-21 09:20:30 adam
8 * Yet more work on record match.
10 * Revision 1.27 1995/11/20 16:59:45 adam
11 * New update method: the 'old' keys are saved for each records.
13 * Revision 1.26 1995/11/20 11:56:24 adam
14 * Work on new traversal.
16 * Revision 1.25 1995/11/16 15:34:54 adam
17 * Uses new record management system in both indexer and server.
19 * Revision 1.24 1995/11/15 19:13:08 adam
20 * Work on record management.
22 * Revision 1.23 1995/10/27 14:00:10 adam
23 * Implemented detection of database availability.
25 * Revision 1.22 1995/10/17 18:02:07 adam
26 * New feature: databases. Implemented as prefix to words in dictionary.
28 * Revision 1.21 1995/10/10 12:24:38 adam
29 * Temporary sort files are compressed.
31 * Revision 1.20 1995/10/06 13:52:05 adam
32 * Bug fixes. Handler may abort further scanning.
34 * Revision 1.19 1995/10/04 12:55:16 adam
35 * Bug fix in ranked search. Use=Any keys inserted.
37 * Revision 1.18 1995/10/04 09:37:08 quinn
40 * Revision 1.17 1995/10/03 14:28:57 adam
41 * Buffered read in extract works.
43 * Revision 1.16 1995/10/03 14:28:45 adam
44 * Work on more effecient read handler in extract.
46 * Revision 1.15 1995/10/02 15:42:53 adam
47 * Extract uses file descriptors instead of FILE pointers.
49 * Revision 1.14 1995/10/02 15:29:13 adam
50 * More logging in file_extract.
52 * Revision 1.13 1995/09/29 14:01:39 adam
55 * Revision 1.12 1995/09/28 14:22:56 adam
56 * Sort uses smaller temporary files.
58 * Revision 1.11 1995/09/28 12:10:31 adam
59 * Bug fixes. Field prefix used in queries.
61 * Revision 1.10 1995/09/28 09:19:41 adam
62 * xfree/xmalloc used everywhere.
63 * Extract/retrieve method seems to work for text records.
65 * Revision 1.9 1995/09/27 12:22:28 adam
66 * More work on extract in record control.
67 * Field name is not in isam keys but in prefix in dictionary words.
69 * Revision 1.8 1995/09/14 07:48:22 adam
70 * Record control management.
72 * Revision 1.7 1995/09/11 13:09:32 adam
73 * More work on relevance feedback.
75 * Revision 1.6 1995/09/08 14:52:27 adam
76 * Minor changes. Dictionary is lower case now.
78 * Revision 1.5 1995/09/06 16:11:16 adam
79 * Option: only one word key per file.
81 * Revision 1.4 1995/09/05 15:28:39 adam
82 * More work on search engine.
84 * Revision 1.3 1995/09/04 12:33:41 adam
85 * Various cleanup. YAZ util used instead.
87 * Revision 1.2 1995/09/04 09:10:34 adam
88 * More work on index add/del/update.
89 * Merge sort implemented.
90 * Initial work on z39 server.
92 * Revision 1.1 1995/09/01 14:06:35 adam
93 * Split of work into more files.
102 #include <alexutil.h>
106 #include "recindex.h"
108 static Dict file_idx;
110 static Records records = NULL;
112 static char **key_buf;
113 static size_t ptr_top;
115 static size_t key_buf_used;
116 static int key_file_no;
118 void key_open (int mem)
122 key_buf = xmalloc (mem);
123 ptr_top = mem/sizeof(char*);
129 if (!(file_idx = dict_open (FNAME_FILE_DICT, 40, 1)))
131 logf (LOG_FATAL, "dict_open fail of %s", "fileidx");
135 records = rec_open (1);
144 void encode_key_init (struct encode_info *i)
150 char *encode_key_int (int d, char *bp)
159 else if (d <= 4194303)
161 *bp++ = 128 + (d>>16);
162 *bp++ = (d>>8) & 255;
167 *bp++ = 192 + (d>>24);
168 *bp++ = (d>>16) & 255;
169 *bp++ = (d>>8) & 255;
175 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
180 while ((*bp++ = *k++))
182 memcpy (&key, k+1, sizeof(struct it_key));
183 bp = encode_key_int ( (key.sysno - i->sysno) * 2 + *k, bp);
184 if (i->sysno != key.sysno)
186 i->sysno = key.sysno;
189 bp = encode_key_int (key.seqno - i->seqno, bp);
190 i->seqno = key.seqno;
191 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
193 logf (LOG_FATAL|LOG_ERRNO, "fwrite");
198 void key_flush (void)
203 struct encode_info encode_info;
209 logf (LOG_LOG, "sorting section %d", key_file_no);
210 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_qsort_compare);
211 sprintf (out_fname, TEMP_FNAME, key_file_no);
213 if (!(outf = fopen (out_fname, "w")))
215 logf (LOG_FATAL|LOG_ERRNO, "fopen (4) %s", out_fname);
218 logf (LOG_LOG, "writing section %d", key_file_no);
219 prevcp = cp = key_buf[ptr_top-ptr_i];
221 encode_key_init (&encode_info);
222 encode_key_write (cp, &encode_info, outf);
225 cp = key_buf[ptr_top-ptr_i];
226 if (strcmp (cp, prevcp))
228 encode_key_init (&encode_info);
229 encode_key_write (cp, &encode_info, outf);
233 encode_key_write (cp + strlen(cp), &encode_info, outf);
237 logf (LOG_FATAL|LOG_ERRNO, "fclose %s", out_fname);
240 logf (LOG_LOG, "finished section %d", key_file_no);
249 rec_close (&records);
250 dict_close (file_idx);
255 static void wordInit (RecWord *p)
259 p->which = Word_String;
268 static void addRecordKey (const RecWord *p)
275 if (reckeys.buf_used+1024 > reckeys.buf_max)
279 b = malloc (reckeys.buf_max += 65000);
280 if (reckeys.buf_used > 0)
281 memcpy (b, reckeys.buf, reckeys.buf_used);
285 dst = reckeys.buf + reckeys.buf_used;
289 attrSet = p->attrSet;
290 memcpy (dst, &attrSet, sizeof(attrSet));
291 dst += sizeof(attrSet);
293 attrUse = p->attrUse;
294 memcpy (dst, &attrUse, sizeof(attrUse));
295 dst += sizeof(attrUse);
297 for (i = 0; p->u.string[i]; i++)
298 *dst++ = p->u.string[i];
301 memcpy (dst, &p->seqno, sizeof(p->seqno));
302 dst += sizeof(p->seqno);
308 reckeys.buf_used = dst - reckeys.buf;
311 static void flushRecordKeys (SYSNO sysno, int cmd, struct recKeys *reckeys,
312 const char *databaseName)
315 while (off < reckeys->buf_used)
317 const char *src = reckeys->buf + off;
322 memcpy (&attrSet, src, sizeof(attrSet));
323 src += sizeof(attrSet);
325 memcpy (&attrUse, src, sizeof(attrUse));
326 src += sizeof(attrUse);
328 if (key_buf_used + 1024 > (ptr_top-ptr_i)*sizeof(char*))
331 key_buf[ptr_top-ptr_i] = (char*)key_buf + key_buf_used;
332 key_buf_used += index_word_prefix ((char*)key_buf + key_buf_used,
333 attrSet, attrUse, databaseName);
335 ((char*)key_buf) [key_buf_used++] = index_char_cvt (*src++);
336 ((char*)key_buf) [key_buf_used++] = '\0';
338 ((char*) key_buf)[key_buf_used++] = cmd;
340 memcpy (&key.seqno, src, sizeof(key.seqno));
341 src += sizeof(key.seqno);
343 memcpy ((char*)key_buf + key_buf_used, &key, sizeof(key));
344 key_buf_used += sizeof(key);
345 off = src - reckeys->buf;
347 assert (off = reckeys->buf_used);
352 static int key_sysno;
353 static const char *key_databaseName;
354 static int key_del_max;
355 static int key_del_used;
356 static char *key_del_buf;
358 static void wordAdd (const RecWord *p)
363 if (key_buf_used + 1024 > (ptr_top-ptr_i)*sizeof(char*))
366 key_buf[ptr_top-ptr_i] = (char*)key_buf + key_buf_used;
367 key_buf_used += index_word_prefix ((char*)key_buf + key_buf_used,
368 p->attrSet, p->attrUse,
373 for (i = 0; p->u.string[i]; i++)
374 ((char*)key_buf) [key_buf_used++] =
375 index_char_cvt (p->u.string[i]);
376 ((char*)key_buf) [key_buf_used++] = '\0';
381 ((char*) key_buf)[key_buf_used++] = ((key_cmd == 'a') ? 1 : 0);
382 key.sysno = key_sysno;
383 key.seqno = p->seqno;
384 memcpy ((char*)key_buf + key_buf_used, &key, sizeof(key));
385 key_buf_used += sizeof(key);
387 if (key_cmd == 'a' && key_del_used >= 0)
391 if (key_del_used + 1024 > key_del_max)
395 if (!(kbn = malloc (key_del_max += 64000)))
397 logf (LOG_FATAL, "malloc");
401 memcpy (kbn, key_del_buf, key_del_used);
408 for (i = 0; p->u.string[i]; i++)
409 ((char*)key_del_buf) [key_del_used++] = p->u.string[i];
410 ((char*)key_del_buf) [key_del_used++] = '\0';
415 attrSet = p->attrSet;
416 memcpy (key_del_buf + key_del_used, &attrSet, sizeof(attrSet));
417 key_del_used += sizeof(attrSet);
419 attrUse = p->attrUse;
420 memcpy (key_del_buf + key_del_used, &attrUse, sizeof(attrUse));
421 key_del_used += sizeof(attrUse);
423 memcpy (key_del_buf + key_del_used, &p->seqno, sizeof(p->seqno));
424 key_del_used += sizeof(p->seqno);
430 static void addRecordKeyAny (const RecWord *p)
432 if (p->attrSet != 1 || p->attrUse != 1016)
436 memcpy (&w, p, sizeof(w));
444 static char *file_buf;
445 static int file_offset;
446 static int file_bufsize;
448 static void file_read_start (int fd)
451 file_buf = xmalloc (4096);
452 file_bufsize = read (fd, file_buf, 4096);
455 static void file_read_stop (int fd)
460 static int file_read (int fd, char *buf, size_t count)
462 int l = file_bufsize - file_offset;
468 memcpy (buf, file_buf + file_offset, l);
470 if (count > file_bufsize)
472 if ((r = read (fd, buf + l, count)) == -1)
474 logf (LOG_FATAL|LOG_ERRNO, "read");
481 file_bufsize = r = read (fd, file_buf, 4096);
484 logf (LOG_FATAL|LOG_ERRNO, "read");
490 memcpy (buf + l, file_buf, r);
496 memcpy (buf + l, file_buf, count - l);
500 memcpy (buf, file_buf + file_offset, count);
501 file_offset += count;
505 int fileExtract (SYSNO *sysno, const char *fname, const char *databaseName,
511 const char *file_type;
512 struct recExtractCtrl extractCtrl;
516 logf (LOG_DEBUG, "fileExtractAdd %s", fname);
518 for (i = strlen(fname); --i >= 0; )
524 else if (fname[i] == '.')
526 strcpy (ext, fname+i+1);
529 sprintf (ext_res, "fileExtension.%s", ext);
530 if (!(file_type = res_get (common_resource, ext_res)))
532 if (!(rt = recType_byName (file_type)))
535 if ((extractCtrl.fd = open (fname, O_RDONLY)) == -1)
537 logf (LOG_WARN|LOG_ERRNO, "open %s", fname);
541 extractCtrl.subType = "";
542 extractCtrl.init = wordInit;
543 extractCtrl.add = addRecordKeyAny;
545 reckeys.buf_used = 0;
546 file_read_start (extractCtrl.fd);
547 extractCtrl.readf = file_read;
548 r = (*rt->extract)(&extractCtrl);
549 file_read_stop (extractCtrl.fd);
550 close (extractCtrl.fd);
554 logf (LOG_WARN, "Couldn't extract file %s, code %d", fname, r);
557 if (! *sysno) /* match criteria */
559 logf (LOG_LOG, "add record %s", fname);
560 rec = rec_new (records);
563 flushRecordKeys (*sysno, 1, &reckeys, databaseName);
567 struct recKeys delkeys;
569 rec = rec_get (records, *sysno);
571 delkeys.buf_used = rec->size[2];
572 delkeys.buf = rec->info[2];
573 flushRecordKeys (*sysno, 0, &delkeys, rec->info[3]);
574 flushRecordKeys (*sysno, 1, &reckeys, databaseName);
577 rec->info[0] = rec_strdup (file_type, &rec->size[0]);
580 rec->info[1] = rec_strdup (fname, &rec->size[1]);
583 if (reckeys.buf_used > 0)
585 rec->info[2] = malloc (reckeys.buf_used);
586 rec->size[2] = reckeys.buf_used;
587 memcpy (rec->info[2], reckeys.buf, rec->size[2]);
595 rec->info[3] = rec_strdup (databaseName, &rec->size[3]);
597 rec_put (records, &rec);