2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.30 1995-11-22 17:19:16 adam
8 * Record management uses the bfile system.
10 * Revision 1.29 1995/11/21 15:01:14 adam
11 * New general match criteria implemented.
12 * New feature: document groups.
14 * Revision 1.28 1995/11/21 09:20:30 adam
15 * Yet more work on record match.
17 * Revision 1.27 1995/11/20 16:59:45 adam
18 * New update method: the 'old' keys are saved for each records.
20 * Revision 1.26 1995/11/20 11:56:24 adam
21 * Work on new traversal.
23 * Revision 1.25 1995/11/16 15:34:54 adam
24 * Uses new record management system in both indexer and server.
26 * Revision 1.24 1995/11/15 19:13:08 adam
27 * Work on record management.
29 * Revision 1.23 1995/10/27 14:00:10 adam
30 * Implemented detection of database availability.
32 * Revision 1.22 1995/10/17 18:02:07 adam
33 * New feature: databases. Implemented as prefix to words in dictionary.
35 * Revision 1.21 1995/10/10 12:24:38 adam
36 * Temporary sort files are compressed.
38 * Revision 1.20 1995/10/06 13:52:05 adam
39 * Bug fixes. Handler may abort further scanning.
41 * Revision 1.19 1995/10/04 12:55:16 adam
42 * Bug fix in ranked search. Use=Any keys inserted.
44 * Revision 1.18 1995/10/04 09:37:08 quinn
47 * Revision 1.17 1995/10/03 14:28:57 adam
48 * Buffered read in extract works.
50 * Revision 1.16 1995/10/03 14:28:45 adam
51 * Work on more effecient read handler in extract.
53 * Revision 1.15 1995/10/02 15:42:53 adam
54 * Extract uses file descriptors instead of FILE pointers.
56 * Revision 1.14 1995/10/02 15:29:13 adam
57 * More logging in file_extract.
59 * Revision 1.13 1995/09/29 14:01:39 adam
62 * Revision 1.12 1995/09/28 14:22:56 adam
63 * Sort uses smaller temporary files.
65 * Revision 1.11 1995/09/28 12:10:31 adam
66 * Bug fixes. Field prefix used in queries.
68 * Revision 1.10 1995/09/28 09:19:41 adam
69 * xfree/xmalloc used everywhere.
70 * Extract/retrieve method seems to work for text records.
72 * Revision 1.9 1995/09/27 12:22:28 adam
73 * More work on extract in record control.
74 * Field name is not in isam keys but in prefix in dictionary words.
76 * Revision 1.8 1995/09/14 07:48:22 adam
77 * Record control management.
79 * Revision 1.7 1995/09/11 13:09:32 adam
80 * More work on relevance feedback.
82 * Revision 1.6 1995/09/08 14:52:27 adam
83 * Minor changes. Dictionary is lower case now.
85 * Revision 1.5 1995/09/06 16:11:16 adam
86 * Option: only one word key per file.
88 * Revision 1.4 1995/09/05 15:28:39 adam
89 * More work on search engine.
91 * Revision 1.3 1995/09/04 12:33:41 adam
92 * Various cleanup. YAZ util used instead.
94 * Revision 1.2 1995/09/04 09:10:34 adam
95 * More work on index add/del/update.
96 * Merge sort implemented.
97 * Initial work on z39 server.
99 * Revision 1.1 1995/09/01 14:06:35 adam
100 * Split of work into more files.
109 #include <alexutil.h>
113 #include "recindex.h"
115 static Dict matchDict;
117 static Records records = NULL;
119 static char **key_buf;
120 static size_t ptr_top;
122 static size_t key_buf_used;
123 static int key_file_no;
125 static int records_inserted = 0;
126 static int records_updated = 0;
127 static int records_deleted = 0;
129 #define MATCH_DICT "match"
131 void key_open (int mem)
135 key_buf = xmalloc (mem);
136 ptr_top = mem/sizeof(char*);
142 if (!(matchDict = dict_open (MATCH_DICT, 20, 1)))
144 logf (LOG_FATAL, "dict_open fail of %s", MATCH_DICT);
148 records = rec_open (1);
157 void encode_key_init (struct encode_info *i)
163 char *encode_key_int (int d, char *bp)
172 else if (d <= 4194303)
174 *bp++ = 128 + (d>>16);
175 *bp++ = (d>>8) & 255;
180 *bp++ = 192 + (d>>24);
181 *bp++ = (d>>16) & 255;
182 *bp++ = (d>>8) & 255;
188 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
193 while ((*bp++ = *k++))
195 memcpy (&key, k+1, sizeof(struct it_key));
196 bp = encode_key_int ( (key.sysno - i->sysno) * 2 + *k, bp);
197 if (i->sysno != key.sysno)
199 i->sysno = key.sysno;
202 bp = encode_key_int (key.seqno - i->seqno, bp);
203 i->seqno = key.seqno;
204 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
206 logf (LOG_FATAL|LOG_ERRNO, "fwrite");
211 void key_flush (void)
216 struct encode_info encode_info;
222 logf (LOG_LOG, "sorting section %d", key_file_no);
223 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_qsort_compare);
224 sprintf (out_fname, TEMP_FNAME, key_file_no);
226 if (!(outf = fopen (out_fname, "w")))
228 logf (LOG_FATAL|LOG_ERRNO, "fopen (4) %s", out_fname);
231 logf (LOG_LOG, "writing section %d", key_file_no);
232 prevcp = cp = key_buf[ptr_top-ptr_i];
234 encode_key_init (&encode_info);
235 encode_key_write (cp, &encode_info, outf);
238 cp = key_buf[ptr_top-ptr_i];
239 if (strcmp (cp, prevcp))
241 encode_key_init (&encode_info);
242 encode_key_write (cp, &encode_info, outf);
246 encode_key_write (cp + strlen(cp), &encode_info, outf);
250 logf (LOG_FATAL|LOG_ERRNO, "fclose %s", out_fname);
253 logf (LOG_LOG, "finished section %d", key_file_no);
262 rec_close (&records);
263 dict_close (matchDict);
265 logf (LOG_LOG, "Records inserted %6d", records_inserted);
266 logf (LOG_LOG, "Records updated %6d", records_updated);
267 logf (LOG_LOG, "Records deleted %6d", records_deleted);
271 static void wordInit (RecWord *p)
275 p->which = Word_String;
284 static void addRecordKey (const RecWord *p)
291 if (reckeys.buf_used+1024 > reckeys.buf_max)
295 b = malloc (reckeys.buf_max += 65000);
296 if (reckeys.buf_used > 0)
297 memcpy (b, reckeys.buf, reckeys.buf_used);
301 dst = reckeys.buf + reckeys.buf_used;
305 attrSet = p->attrSet;
306 memcpy (dst, &attrSet, sizeof(attrSet));
307 dst += sizeof(attrSet);
309 attrUse = p->attrUse;
310 memcpy (dst, &attrUse, sizeof(attrUse));
311 dst += sizeof(attrUse);
313 for (i = 0; p->u.string[i]; i++)
314 *dst++ = p->u.string[i];
317 memcpy (dst, &p->seqno, sizeof(p->seqno));
318 dst += sizeof(p->seqno);
324 reckeys.buf_used = dst - reckeys.buf;
327 static void flushRecordKeys (SYSNO sysno, int cmd, struct recKeys *reckeys,
328 const char *databaseName)
331 while (off < reckeys->buf_used)
333 const char *src = reckeys->buf + off;
338 memcpy (&attrSet, src, sizeof(attrSet));
339 src += sizeof(attrSet);
341 memcpy (&attrUse, src, sizeof(attrUse));
342 src += sizeof(attrUse);
344 if (key_buf_used + 1024 > (ptr_top-ptr_i)*sizeof(char*))
347 key_buf[ptr_top-ptr_i] = (char*)key_buf + key_buf_used;
348 key_buf_used += index_word_prefix ((char*)key_buf + key_buf_used,
349 attrSet, attrUse, databaseName);
351 ((char*)key_buf) [key_buf_used++] = index_char_cvt (*src++);
353 ((char*)key_buf) [key_buf_used++] = '\0';
355 ((char*) key_buf)[key_buf_used++] = cmd;
357 memcpy (&key.seqno, src, sizeof(key.seqno));
358 src += sizeof(key.seqno);
360 memcpy ((char*)key_buf + key_buf_used, &key, sizeof(key));
361 key_buf_used += sizeof(key);
362 off = src - reckeys->buf;
364 assert (off == reckeys->buf_used);
367 static const char **searchRecordKey (struct recKeys *reckeys,
368 int attrSetS, int attrUseS)
370 static const char *ws[32];
375 for (i = 0; i<32; i++)
378 while (off < reckeys->buf_used)
380 const char *src = reckeys->buf + off;
386 memcpy (&attrSet, src, sizeof(attrSet));
387 src += sizeof(attrSet);
389 memcpy (&attrUse, src, sizeof(attrUse));
390 src += sizeof(attrUse);
396 memcpy (&seqno, src, sizeof(seqno));
397 src += sizeof(seqno);
400 logf (LOG_LOG, "(%d,%d) %d %s", attrSet, attrUse, seqno, wstart);
402 if (attrUseS == attrUse && attrSetS == attrSet)
409 woff = seqno - startSeq;
410 if (woff >= 0 && woff < 31)
414 off = src - reckeys->buf;
416 assert (off == reckeys->buf_used);
420 static void addRecordKeyAny (const RecWord *p)
422 if (p->attrSet != 1 || p->attrUse != 1016)
426 memcpy (&w, p, sizeof(w));
434 static char *file_buf;
435 static int file_offset;
436 static int file_bufsize;
438 static void file_read_start (int fd)
441 file_buf = xmalloc (4096);
442 file_bufsize = read (fd, file_buf, 4096);
445 static void file_read_stop (int fd)
450 static int file_read (int fd, char *buf, size_t count)
452 int l = file_bufsize - file_offset;
458 memcpy (buf, file_buf + file_offset, l);
460 if (count > file_bufsize)
462 if ((r = read (fd, buf + l, count)) == -1)
464 logf (LOG_FATAL|LOG_ERRNO, "read");
471 file_bufsize = r = read (fd, file_buf, 4096);
474 logf (LOG_FATAL|LOG_ERRNO, "read");
480 memcpy (buf + l, file_buf, r);
486 memcpy (buf + l, file_buf, count - l);
490 memcpy (buf, file_buf + file_offset, count);
491 file_offset += count;
495 static int atois (const char **s)
498 while ( (c=**s) >= '0' && c <= '9')
500 val = val*10 + c - '0';
506 static char *fileMatchStr (struct recKeys *reckeys, struct recordGroup *rGroup,
508 const char *recordType,
511 static char dstBuf[2048];
513 const char *s = spec;
514 static const char **w;
519 while (*s == ' ' || *s == '\t')
526 int attrSet, attrUse;
530 attrSet = atois (&s);
533 logf (LOG_WARN, "Missing , in match criteria %s in group %s",
534 spec, rGroup->groupName ? rGroup->groupName : "none");
538 attrUse = atois (&s);
539 w = searchRecordKey (reckeys, attrSet, attrUse);
544 for (i = 0; i<32; i++)
549 logf (LOG_WARN, "Missing ) in match criteria %s in group %s",
550 spec, rGroup->groupName ? rGroup->groupName : "none");
555 for (i = 0; i<32; i++)
556 if (matchFlag[i] && w[i])
568 logf (LOG_WARN, "Record in file %s didn't contain match"
569 " fields in (%d,%d)", fname, attrSet, attrUse);
577 const char *spec_src = NULL;
578 const char *s1 = ++s;
579 while (*s1 && *s1 != ' ' && *s1 != '\t')
585 memcpy (special, s, spec_len);
586 special[spec_len] = '\0';
589 if (strcmp (special, "group"))
590 spec_src = rGroup->groupName;
591 else if (strcmp (special, "database"))
592 spec_src = rGroup->databaseName;
593 else if (strcmp (special, "filename"))
595 else if (strcmp (special, "type"))
596 spec_src = recordType;
601 strcpy (dst, spec_src);
602 dst += strlen(spec_src);
607 logf (LOG_WARN, "Syntax error in match criteria %s in group %s",
608 spec, rGroup->groupName ? rGroup->groupName : "none");
615 logf (LOG_WARN, "No match criteria for record %s in group %s",
616 fname, rGroup->groupName ? rGroup->groupName : "none");
622 int fileExtract (SYSNO *sysno, const char *fname, struct recordGroup *rGroup,
630 const char *file_type;
631 const char *file_match;
632 struct recExtractCtrl extractCtrl;
637 if (!rGroup->groupName || !*rGroup->groupName)
640 sprintf (gprefix, "%s.", rGroup->groupName);
642 logf (LOG_DEBUG, "fileExtractAdd %s", fname);
644 /* determine file extension */
645 for (i = strlen(fname); --i >= 0; )
651 else if (fname[i] == '.')
653 strcpy (ext, fname+i+1);
656 /* determine file type - depending on extension */
657 sprintf (ext_res, "%sfileExtension.%s", gprefix, ext);
658 if (!(file_type = res_get (common_resource, ext_res)))
660 if (!(recType = recType_byName (file_type)))
663 /* determine match criteria */
664 sprintf (ext_res, "%sfileMatch.%s", gprefix, ext);
665 file_match = res_get (common_resource, ext_res);
668 sprintf (ext_res, "%sfileMatch", gprefix);
669 file_match = res_get (common_resource, ext_res);
672 /* determine database name */
673 if (!rGroup->databaseName)
675 sprintf (ext_res, "%sdatabase.%s", gprefix, ext);
676 if (!(rGroup->databaseName = res_get (common_resource, ext_res)))
678 sprintf (ext_res, "%sdatabase", gprefix);
679 rGroup->databaseName = res_get (common_resource, ext_res);
682 if (!rGroup->databaseName)
683 rGroup->databaseName = "Default";
685 /* open input file */
686 if ((extractCtrl.fd = open (fname, O_RDONLY)) == -1)
688 logf (LOG_WARN|LOG_ERRNO, "open %s", fname);
693 extractCtrl.subType = "";
694 extractCtrl.init = wordInit;
695 extractCtrl.add = addRecordKeyAny;
697 reckeys.buf_used = 0;
698 file_read_start (extractCtrl.fd);
699 extractCtrl.readf = file_read;
700 r = (*recType->extract)(&extractCtrl);
701 file_read_stop (extractCtrl.fd);
702 close (extractCtrl.fd);
706 logf (LOG_WARN, "Couldn't extract file %s, code %d", fname, r);
710 /* perform match if sysno not known and if match criteria is specified */
713 if (!sysno && file_match)
718 matchStr = fileMatchStr(&reckeys, rGroup, fname, file_type,
722 rinfo = dict_lookup (matchDict, matchStr);
724 memcpy (sysno, rinfo+1, sizeof(*sysno));
730 logf (LOG_WARN, "Record not inserted");
740 logf (LOG_LOG, "? record %s", fname);
743 logf (LOG_LOG, "add record %s", fname);
744 rec = rec_new (records);
748 dict_insert (matchDict, matchStr, sizeof(*sysno), sysno);
749 flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
755 struct recKeys delkeys;
757 rec = rec_get (records, *sysno);
759 delkeys.buf_used = rec->size[2];
760 delkeys.buf = rec->info[2];
761 flushRecordKeys (*sysno, 0, &delkeys, rec->info[3]);
764 logf (LOG_LOG, "delete record %s", fname);
766 rec_del (records, &rec);
771 logf (LOG_LOG, "update record %s", fname);
772 flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
777 rec->info[0] = rec_strdup (file_type, &rec->size[0]);
780 rec->info[1] = rec_strdup (fname, &rec->size[1]);
783 if (reckeys.buf_used > 0)
785 rec->info[2] = malloc (reckeys.buf_used);
786 rec->size[2] = reckeys.buf_used;
787 memcpy (rec->info[2], reckeys.buf, rec->size[2]);
795 rec->info[3] = rec_strdup (rGroup->databaseName, &rec->size[3]);
797 rec_put (records, &rec);