2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.32 1995-11-25 10:24:05 adam
8 * More record fields - they are enumerated now.
9 * New options: flagStoreData flagStoreKey.
11 * Revision 1.31 1995/11/24 11:31:35 adam
12 * Commands add & del read filenames from stdin if source directory is
14 * Match criteria supports 'constant' strings.
16 * Revision 1.30 1995/11/22 17:19:16 adam
17 * Record management uses the bfile system.
19 * Revision 1.29 1995/11/21 15:01:14 adam
20 * New general match criteria implemented.
21 * New feature: document groups.
23 * Revision 1.28 1995/11/21 09:20:30 adam
24 * Yet more work on record match.
26 * Revision 1.27 1995/11/20 16:59:45 adam
27 * New update method: the 'old' keys are saved for each records.
29 * Revision 1.26 1995/11/20 11:56:24 adam
30 * Work on new traversal.
32 * Revision 1.25 1995/11/16 15:34:54 adam
33 * Uses new record management system in both indexer and server.
35 * Revision 1.24 1995/11/15 19:13:08 adam
36 * Work on record management.
38 * Revision 1.23 1995/10/27 14:00:10 adam
39 * Implemented detection of database availability.
41 * Revision 1.22 1995/10/17 18:02:07 adam
42 * New feature: databases. Implemented as prefix to words in dictionary.
44 * Revision 1.21 1995/10/10 12:24:38 adam
45 * Temporary sort files are compressed.
47 * Revision 1.20 1995/10/06 13:52:05 adam
48 * Bug fixes. Handler may abort further scanning.
50 * Revision 1.19 1995/10/04 12:55:16 adam
51 * Bug fix in ranked search. Use=Any keys inserted.
53 * Revision 1.18 1995/10/04 09:37:08 quinn
56 * Revision 1.17 1995/10/03 14:28:57 adam
57 * Buffered read in extract works.
59 * Revision 1.16 1995/10/03 14:28:45 adam
60 * Work on more effecient read handler in extract.
62 * Revision 1.15 1995/10/02 15:42:53 adam
63 * Extract uses file descriptors instead of FILE pointers.
65 * Revision 1.14 1995/10/02 15:29:13 adam
66 * More logging in file_extract.
68 * Revision 1.13 1995/09/29 14:01:39 adam
71 * Revision 1.12 1995/09/28 14:22:56 adam
72 * Sort uses smaller temporary files.
74 * Revision 1.11 1995/09/28 12:10:31 adam
75 * Bug fixes. Field prefix used in queries.
77 * Revision 1.10 1995/09/28 09:19:41 adam
78 * xfree/xmalloc used everywhere.
79 * Extract/retrieve method seems to work for text records.
81 * Revision 1.9 1995/09/27 12:22:28 adam
82 * More work on extract in record control.
83 * Field name is not in isam keys but in prefix in dictionary words.
85 * Revision 1.8 1995/09/14 07:48:22 adam
86 * Record control management.
88 * Revision 1.7 1995/09/11 13:09:32 adam
89 * More work on relevance feedback.
91 * Revision 1.6 1995/09/08 14:52:27 adam
92 * Minor changes. Dictionary is lower case now.
94 * Revision 1.5 1995/09/06 16:11:16 adam
95 * Option: only one word key per file.
97 * Revision 1.4 1995/09/05 15:28:39 adam
98 * More work on search engine.
100 * Revision 1.3 1995/09/04 12:33:41 adam
101 * Various cleanup. YAZ util used instead.
103 * Revision 1.2 1995/09/04 09:10:34 adam
104 * More work on index add/del/update.
105 * Merge sort implemented.
106 * Initial work on z39 server.
108 * Revision 1.1 1995/09/01 14:06:35 adam
109 * Split of work into more files.
118 #include <alexutil.h>
122 #include "recindex.h"
124 static Dict matchDict;
126 static Records records = NULL;
128 static char **key_buf;
129 static size_t ptr_top;
131 static size_t key_buf_used;
132 static int key_file_no;
134 static int records_inserted = 0;
135 static int records_updated = 0;
136 static int records_deleted = 0;
138 #define MATCH_DICT "match"
140 void key_open (int mem)
144 key_buf = xmalloc (mem);
145 ptr_top = mem/sizeof(char*);
151 if (!(matchDict = dict_open (MATCH_DICT, 20, 1)))
153 logf (LOG_FATAL, "dict_open fail of %s", MATCH_DICT);
157 records = rec_open (1);
166 void encode_key_init (struct encode_info *i)
172 char *encode_key_int (int d, char *bp)
181 else if (d <= 4194303)
183 *bp++ = 128 + (d>>16);
184 *bp++ = (d>>8) & 255;
189 *bp++ = 192 + (d>>24);
190 *bp++ = (d>>16) & 255;
191 *bp++ = (d>>8) & 255;
197 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
202 while ((*bp++ = *k++))
204 memcpy (&key, k+1, sizeof(struct it_key));
205 bp = encode_key_int ( (key.sysno - i->sysno) * 2 + *k, bp);
206 if (i->sysno != key.sysno)
208 i->sysno = key.sysno;
211 bp = encode_key_int (key.seqno - i->seqno, bp);
212 i->seqno = key.seqno;
213 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
215 logf (LOG_FATAL|LOG_ERRNO, "fwrite");
220 void key_flush (void)
225 struct encode_info encode_info;
231 logf (LOG_LOG, "sorting section %d", key_file_no);
232 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_qsort_compare);
233 sprintf (out_fname, TEMP_FNAME, key_file_no);
235 if (!(outf = fopen (out_fname, "w")))
237 logf (LOG_FATAL|LOG_ERRNO, "fopen (4) %s", out_fname);
240 logf (LOG_LOG, "writing section %d", key_file_no);
241 prevcp = cp = key_buf[ptr_top-ptr_i];
243 encode_key_init (&encode_info);
244 encode_key_write (cp, &encode_info, outf);
247 cp = key_buf[ptr_top-ptr_i];
248 if (strcmp (cp, prevcp))
250 encode_key_init (&encode_info);
251 encode_key_write (cp, &encode_info, outf);
255 encode_key_write (cp + strlen(cp), &encode_info, outf);
259 logf (LOG_FATAL|LOG_ERRNO, "fclose %s", out_fname);
262 logf (LOG_LOG, "finished section %d", key_file_no);
271 rec_close (&records);
272 dict_close (matchDict);
274 logf (LOG_LOG, "Records inserted %6d", records_inserted);
275 logf (LOG_LOG, "Records updated %6d", records_updated);
276 logf (LOG_LOG, "Records deleted %6d", records_deleted);
280 static void wordInit (RecWord *p)
284 p->which = Word_String;
293 static void addRecordKey (const RecWord *p)
300 if (reckeys.buf_used+1024 > reckeys.buf_max)
304 b = malloc (reckeys.buf_max += 65000);
305 if (reckeys.buf_used > 0)
306 memcpy (b, reckeys.buf, reckeys.buf_used);
310 dst = reckeys.buf + reckeys.buf_used;
314 attrSet = p->attrSet;
315 memcpy (dst, &attrSet, sizeof(attrSet));
316 dst += sizeof(attrSet);
318 attrUse = p->attrUse;
319 memcpy (dst, &attrUse, sizeof(attrUse));
320 dst += sizeof(attrUse);
322 for (i = 0; p->u.string[i]; i++)
323 *dst++ = p->u.string[i];
326 memcpy (dst, &p->seqno, sizeof(p->seqno));
327 dst += sizeof(p->seqno);
333 reckeys.buf_used = dst - reckeys.buf;
336 static void flushRecordKeys (SYSNO sysno, int cmd, struct recKeys *reckeys,
337 const char *databaseName)
340 while (off < reckeys->buf_used)
342 const char *src = reckeys->buf + off;
347 memcpy (&attrSet, src, sizeof(attrSet));
348 src += sizeof(attrSet);
350 memcpy (&attrUse, src, sizeof(attrUse));
351 src += sizeof(attrUse);
353 if (key_buf_used + 1024 > (ptr_top-ptr_i)*sizeof(char*))
356 key_buf[ptr_top-ptr_i] = (char*)key_buf + key_buf_used;
357 key_buf_used += index_word_prefix ((char*)key_buf + key_buf_used,
358 attrSet, attrUse, databaseName);
360 ((char*)key_buf) [key_buf_used++] = index_char_cvt (*src++);
362 ((char*)key_buf) [key_buf_used++] = '\0';
364 ((char*) key_buf)[key_buf_used++] = cmd;
366 memcpy (&key.seqno, src, sizeof(key.seqno));
367 src += sizeof(key.seqno);
369 memcpy ((char*)key_buf + key_buf_used, &key, sizeof(key));
370 key_buf_used += sizeof(key);
371 off = src - reckeys->buf;
373 assert (off == reckeys->buf_used);
376 static const char **searchRecordKey (struct recKeys *reckeys,
377 int attrSetS, int attrUseS)
379 static const char *ws[32];
384 for (i = 0; i<32; i++)
387 while (off < reckeys->buf_used)
389 const char *src = reckeys->buf + off;
395 memcpy (&attrSet, src, sizeof(attrSet));
396 src += sizeof(attrSet);
398 memcpy (&attrUse, src, sizeof(attrUse));
399 src += sizeof(attrUse);
405 memcpy (&seqno, src, sizeof(seqno));
406 src += sizeof(seqno);
409 logf (LOG_LOG, "(%d,%d) %d %s", attrSet, attrUse, seqno, wstart);
411 if (attrUseS == attrUse && attrSetS == attrSet)
418 woff = seqno - startSeq;
419 if (woff >= 0 && woff < 31)
423 off = src - reckeys->buf;
425 assert (off == reckeys->buf_used);
429 static void addRecordKeyAny (const RecWord *p)
431 if (p->attrSet != 1 || p->attrUse != 1016)
435 memcpy (&w, p, sizeof(w));
443 static char *file_buf;
444 static int file_offset;
445 static int file_bufsize;
447 static void file_read_start (int fd)
450 file_buf = xmalloc (4096);
451 file_bufsize = read (fd, file_buf, 4096);
454 static void file_read_stop (int fd)
459 static int file_read (int fd, char *buf, size_t count)
461 int l = file_bufsize - file_offset;
467 memcpy (buf, file_buf + file_offset, l);
469 if (count > file_bufsize)
471 if ((r = read (fd, buf + l, count)) == -1)
473 logf (LOG_FATAL|LOG_ERRNO, "read");
480 file_bufsize = r = read (fd, file_buf, 4096);
483 logf (LOG_FATAL|LOG_ERRNO, "read");
489 memcpy (buf + l, file_buf, r);
495 memcpy (buf + l, file_buf, count - l);
499 memcpy (buf, file_buf + file_offset, count);
500 file_offset += count;
504 static int atois (const char **s)
507 while ( (c=**s) >= '0' && c <= '9')
509 val = val*10 + c - '0';
515 static char *fileMatchStr (struct recKeys *reckeys, struct recordGroup *rGroup,
517 const char *recordType,
520 static char dstBuf[2048];
522 const char *s = spec;
523 static const char **w;
528 while (*s == ' ' || *s == '\t')
535 int attrSet, attrUse;
539 attrSet = atois (&s);
542 logf (LOG_WARN, "Missing , in match criteria %s in group %s",
543 spec, rGroup->groupName ? rGroup->groupName : "none");
547 attrUse = atois (&s);
548 w = searchRecordKey (reckeys, attrSet, attrUse);
553 for (i = 0; i<32; i++)
558 logf (LOG_WARN, "Missing ) in match criteria %s in group %s",
559 spec, rGroup->groupName ? rGroup->groupName : "none");
564 for (i = 0; i<32; i++)
565 if (matchFlag[i] && w[i])
577 logf (LOG_WARN, "Record in file %s didn't contain match"
578 " fields in (%d,%d)", fname, attrSet, attrUse);
586 const char *spec_src = NULL;
587 const char *s1 = ++s;
588 while (*s1 && *s1 != ' ' && *s1 != '\t')
594 memcpy (special, s, spec_len);
595 special[spec_len] = '\0';
598 if (strcmp (special, "group"))
599 spec_src = rGroup->groupName;
600 else if (strcmp (special, "database"))
601 spec_src = rGroup->databaseName;
602 else if (strcmp (special, "filename"))
604 else if (strcmp (special, "type"))
605 spec_src = recordType;
610 strcpy (dst, spec_src);
611 dst += strlen (spec_src);
614 else if (*s == '\"' || *s == '\'')
616 int stopMarker = *s++;
620 while (*s && *s != stopMarker)
628 strcpy (dst, tmpString);
629 dst += strlen (tmpString);
633 logf (LOG_WARN, "Syntax error in match criteria %s in group %s",
634 spec, rGroup->groupName ? rGroup->groupName : "none");
641 logf (LOG_WARN, "No match criteria for record %s in group %s",
642 fname, rGroup->groupName ? rGroup->groupName : "none");
648 static int recordExtract (SYSNO *sysno, const char *fname,
649 struct recordGroup *rGroup, int deleteFlag,
651 const char *file_type,
654 struct recExtractCtrl extractCtrl;
663 extractCtrl.subType = "";
664 extractCtrl.init = wordInit;
665 extractCtrl.add = addRecordKeyAny;
667 reckeys.buf_used = 0;
668 file_read_start (extractCtrl.fd);
669 extractCtrl.readf = file_read;
670 r = (*recType->extract)(&extractCtrl);
671 file_read_stop (extractCtrl.fd);
672 close (extractCtrl.fd);
676 logf (LOG_WARN, "Couldn't extract file %s, code %d", fname, r);
680 /* perform match if sysno not known and if match criteria is specified */
687 if (rGroup->fileMatch)
691 matchStr = fileMatchStr(&reckeys, rGroup, fname, file_type,
695 rinfo = dict_lookup (matchDict, matchStr);
697 memcpy (sysno, rinfo+1, sizeof(*sysno));
701 logf (LOG_WARN, "Record not inserted");
712 logf (LOG_LOG, "? record %s", fname);
715 logf (LOG_LOG, "add record %s", fname);
716 rec = rec_new (records);
720 dict_insert (matchDict, matchStr, sizeof(*sysno), sysno);
721 flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
727 struct recKeys delkeys;
729 rec = rec_get (records, *sysno);
731 delkeys.buf_used = rec->size[recInfo_delKeys];
732 delkeys.buf = rec->info[recInfo_delKeys];
733 flushRecordKeys (*sysno, 0, &delkeys, rec->info[recInfo_databaseName]);
736 if (!delkeys.buf_used)
738 logf (LOG_WARN, "cannot delete %s - no delete keys",
742 logf (LOG_LOG, "delete record %s", fname);
744 rec_del (records, &rec);
749 if (!delkeys.buf_used)
751 logf (LOG_WARN, "cannot update %s - no delete keys",
756 logf (LOG_LOG, "update record %s", fname);
757 flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
762 free (rec->info[recInfo_fileType]);
763 rec->info[recInfo_fileType] =
764 rec_strdup (file_type, &rec->size[recInfo_fileType]);
766 free (rec->info[recInfo_filename]);
767 rec->info[recInfo_filename] =
768 rec_strdup (fname, &rec->size[recInfo_filename]);
770 free (rec->info[recInfo_delKeys]);
771 if (reckeys.buf_used > 0 && rGroup->flagStoreKeys == 1)
773 logf (LOG_LOG, "Storing keys...");
774 rec->info[recInfo_delKeys] = malloc (reckeys.buf_used);
775 rec->size[recInfo_delKeys] = reckeys.buf_used;
776 memcpy (rec->info[recInfo_delKeys], reckeys.buf,
777 rec->size[recInfo_delKeys]);
781 rec->info[recInfo_delKeys] = NULL;
782 rec->size[recInfo_delKeys] = 0;
784 free (rec->info[recInfo_databaseName]);
785 rec->info[recInfo_databaseName] =
786 rec_strdup (rGroup->databaseName, &rec->size[recInfo_databaseName]);
788 rec_put (records, &rec);
792 int fileExtract (SYSNO *sysno, const char *fname, struct recordGroup *rGroup,
799 const char *file_type;
802 if (!rGroup->groupName || !*rGroup->groupName)
805 sprintf (gprefix, "%s.", rGroup->groupName);
807 logf (LOG_DEBUG, "fileExtract %s", fname);
809 /* determine file extension */
810 for (i = strlen(fname); --i >= 0; )
816 else if (fname[i] == '.')
818 strcpy (ext, fname+i+1);
821 /* determine file type - depending on extension */
822 sprintf (ext_res, "%sfileExtension.%s", gprefix, ext);
823 if (!(file_type = res_get (common_resource, ext_res)))
825 if (!(recType = recType_byName (file_type)))
828 /* determine match criteria */
829 if (rGroup->fileMatch)
831 sprintf (ext_res, "%sfileMatch.%s", gprefix, ext);
832 rGroup->fileMatch = res_get (common_resource, ext_res);
833 if (!rGroup->fileMatch)
835 sprintf (ext_res, "%sfileMatch", gprefix);
836 rGroup->fileMatch = res_get (common_resource, ext_res);
840 /* determine database name */
841 if (!rGroup->databaseName)
843 sprintf (ext_res, "%sdatabase.%s", gprefix, ext);
844 if (!(rGroup->databaseName = res_get (common_resource, ext_res)))
846 sprintf (ext_res, "%sdatabase", gprefix);
847 rGroup->databaseName = res_get (common_resource, ext_res);
850 if (!rGroup->databaseName)
851 rGroup->databaseName = "Default";
853 if (rGroup->flagStoreData == -1)
856 sprintf (ext_res, "%sstoreData.%s", gprefix, ext);
857 if (!(sval = res_get (common_resource, ext_res)))
859 sprintf (ext_res, "%sstoreData", gprefix);
860 sval = res_get (common_resource, ext_res);
863 rGroup->flagStoreData = atoi (sval);
865 if (rGroup->flagStoreData == -1)
866 rGroup->flagStoreData = 0;
869 if (rGroup->flagStoreKeys == -1)
873 sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
874 if (!(sval = res_get (common_resource, ext_res)))
876 sprintf (ext_res, "%sstoreKeys", gprefix);
877 sval = res_get (common_resource, ext_res);
880 rGroup->flagStoreKeys = atoi (sval);
882 if (rGroup->flagStoreKeys == -1)
883 rGroup->flagStoreKeys = 0;
886 /* open input file */
887 if ((fd = open (fname, O_RDONLY)) == -1)
889 logf (LOG_WARN|LOG_ERRNO, "open %s", fname);
892 recordExtract (sysno, fname, rGroup, deleteFlag, fd,