2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.33 1995-11-27 09:56:20 adam
8 * Record info elements better enumerated. Internal store of records.
10 * Revision 1.32 1995/11/25 10:24:05 adam
11 * More record fields - they are enumerated now.
12 * New options: flagStoreData flagStoreKey.
14 * Revision 1.31 1995/11/24 11:31:35 adam
15 * Commands add & del read filenames from stdin if source directory is
17 * Match criteria supports 'constant' strings.
19 * Revision 1.30 1995/11/22 17:19:16 adam
20 * Record management uses the bfile system.
22 * Revision 1.29 1995/11/21 15:01:14 adam
23 * New general match criteria implemented.
24 * New feature: document groups.
26 * Revision 1.28 1995/11/21 09:20:30 adam
27 * Yet more work on record match.
29 * Revision 1.27 1995/11/20 16:59:45 adam
30 * New update method: the 'old' keys are saved for each records.
32 * Revision 1.26 1995/11/20 11:56:24 adam
33 * Work on new traversal.
35 * Revision 1.25 1995/11/16 15:34:54 adam
36 * Uses new record management system in both indexer and server.
38 * Revision 1.24 1995/11/15 19:13:08 adam
39 * Work on record management.
41 * Revision 1.23 1995/10/27 14:00:10 adam
42 * Implemented detection of database availability.
44 * Revision 1.22 1995/10/17 18:02:07 adam
45 * New feature: databases. Implemented as prefix to words in dictionary.
47 * Revision 1.21 1995/10/10 12:24:38 adam
48 * Temporary sort files are compressed.
50 * Revision 1.20 1995/10/06 13:52:05 adam
51 * Bug fixes. Handler may abort further scanning.
53 * Revision 1.19 1995/10/04 12:55:16 adam
54 * Bug fix in ranked search. Use=Any keys inserted.
56 * Revision 1.18 1995/10/04 09:37:08 quinn
59 * Revision 1.17 1995/10/03 14:28:57 adam
60 * Buffered read in extract works.
62 * Revision 1.16 1995/10/03 14:28:45 adam
63 * Work on more effecient read handler in extract.
65 * Revision 1.15 1995/10/02 15:42:53 adam
66 * Extract uses file descriptors instead of FILE pointers.
68 * Revision 1.14 1995/10/02 15:29:13 adam
69 * More logging in file_extract.
71 * Revision 1.13 1995/09/29 14:01:39 adam
74 * Revision 1.12 1995/09/28 14:22:56 adam
75 * Sort uses smaller temporary files.
77 * Revision 1.11 1995/09/28 12:10:31 adam
78 * Bug fixes. Field prefix used in queries.
80 * Revision 1.10 1995/09/28 09:19:41 adam
81 * xfree/xmalloc used everywhere.
82 * Extract/retrieve method seems to work for text records.
84 * Revision 1.9 1995/09/27 12:22:28 adam
85 * More work on extract in record control.
86 * Field name is not in isam keys but in prefix in dictionary words.
88 * Revision 1.8 1995/09/14 07:48:22 adam
89 * Record control management.
91 * Revision 1.7 1995/09/11 13:09:32 adam
92 * More work on relevance feedback.
94 * Revision 1.6 1995/09/08 14:52:27 adam
95 * Minor changes. Dictionary is lower case now.
97 * Revision 1.5 1995/09/06 16:11:16 adam
98 * Option: only one word key per file.
100 * Revision 1.4 1995/09/05 15:28:39 adam
101 * More work on search engine.
103 * Revision 1.3 1995/09/04 12:33:41 adam
104 * Various cleanup. YAZ util used instead.
106 * Revision 1.2 1995/09/04 09:10:34 adam
107 * More work on index add/del/update.
108 * Merge sort implemented.
109 * Initial work on z39 server.
111 * Revision 1.1 1995/09/01 14:06:35 adam
112 * Split of work into more files.
121 #include <alexutil.h>
125 #include "recindex.h"
127 static Dict matchDict;
129 static Records records = NULL;
131 static char **key_buf;
132 static size_t ptr_top;
134 static size_t key_buf_used;
135 static int key_file_no;
137 static int records_inserted = 0;
138 static int records_updated = 0;
139 static int records_deleted = 0;
141 #define MATCH_DICT "match"
143 void key_open (int mem)
147 key_buf = xmalloc (mem);
148 ptr_top = mem/sizeof(char*);
154 if (!(matchDict = dict_open (MATCH_DICT, 20, 1)))
156 logf (LOG_FATAL, "dict_open fail of %s", MATCH_DICT);
160 records = rec_open (1);
169 void encode_key_init (struct encode_info *i)
175 char *encode_key_int (int d, char *bp)
184 else if (d <= 4194303)
186 *bp++ = 128 + (d>>16);
187 *bp++ = (d>>8) & 255;
192 *bp++ = 192 + (d>>24);
193 *bp++ = (d>>16) & 255;
194 *bp++ = (d>>8) & 255;
200 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
205 while ((*bp++ = *k++))
207 memcpy (&key, k+1, sizeof(struct it_key));
208 bp = encode_key_int ( (key.sysno - i->sysno) * 2 + *k, bp);
209 if (i->sysno != key.sysno)
211 i->sysno = key.sysno;
214 bp = encode_key_int (key.seqno - i->seqno, bp);
215 i->seqno = key.seqno;
216 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
218 logf (LOG_FATAL|LOG_ERRNO, "fwrite");
223 void key_flush (void)
228 struct encode_info encode_info;
234 logf (LOG_LOG, "sorting section %d", key_file_no);
235 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_qsort_compare);
236 sprintf (out_fname, TEMP_FNAME, key_file_no);
238 if (!(outf = fopen (out_fname, "w")))
240 logf (LOG_FATAL|LOG_ERRNO, "fopen (4) %s", out_fname);
243 logf (LOG_LOG, "writing section %d", key_file_no);
244 prevcp = cp = key_buf[ptr_top-ptr_i];
246 encode_key_init (&encode_info);
247 encode_key_write (cp, &encode_info, outf);
250 cp = key_buf[ptr_top-ptr_i];
251 if (strcmp (cp, prevcp))
253 encode_key_init (&encode_info);
254 encode_key_write (cp, &encode_info, outf);
258 encode_key_write (cp + strlen(cp), &encode_info, outf);
262 logf (LOG_FATAL|LOG_ERRNO, "fclose %s", out_fname);
265 logf (LOG_LOG, "finished section %d", key_file_no);
274 rec_close (&records);
275 dict_close (matchDict);
277 logf (LOG_LOG, "Records inserted %6d", records_inserted);
278 logf (LOG_LOG, "Records updated %6d", records_updated);
279 logf (LOG_LOG, "Records deleted %6d", records_deleted);
283 static void wordInit (RecWord *p)
287 p->which = Word_String;
296 static void addRecordKey (const RecWord *p)
303 if (reckeys.buf_used+1024 > reckeys.buf_max)
307 b = malloc (reckeys.buf_max += 65000);
308 if (reckeys.buf_used > 0)
309 memcpy (b, reckeys.buf, reckeys.buf_used);
313 dst = reckeys.buf + reckeys.buf_used;
317 attrSet = p->attrSet;
318 memcpy (dst, &attrSet, sizeof(attrSet));
319 dst += sizeof(attrSet);
321 attrUse = p->attrUse;
322 memcpy (dst, &attrUse, sizeof(attrUse));
323 dst += sizeof(attrUse);
325 for (i = 0; p->u.string[i]; i++)
326 *dst++ = p->u.string[i];
329 memcpy (dst, &p->seqno, sizeof(p->seqno));
330 dst += sizeof(p->seqno);
336 reckeys.buf_used = dst - reckeys.buf;
339 static void flushRecordKeys (SYSNO sysno, int cmd, struct recKeys *reckeys,
340 const char *databaseName)
343 while (off < reckeys->buf_used)
345 const char *src = reckeys->buf + off;
350 memcpy (&attrSet, src, sizeof(attrSet));
351 src += sizeof(attrSet);
353 memcpy (&attrUse, src, sizeof(attrUse));
354 src += sizeof(attrUse);
356 if (key_buf_used + 1024 > (ptr_top-ptr_i)*sizeof(char*))
359 key_buf[ptr_top-ptr_i] = (char*)key_buf + key_buf_used;
360 key_buf_used += index_word_prefix ((char*)key_buf + key_buf_used,
361 attrSet, attrUse, databaseName);
363 ((char*)key_buf) [key_buf_used++] = index_char_cvt (*src++);
365 ((char*)key_buf) [key_buf_used++] = '\0';
367 ((char*) key_buf)[key_buf_used++] = cmd;
369 memcpy (&key.seqno, src, sizeof(key.seqno));
370 src += sizeof(key.seqno);
372 memcpy ((char*)key_buf + key_buf_used, &key, sizeof(key));
373 key_buf_used += sizeof(key);
374 off = src - reckeys->buf;
376 assert (off == reckeys->buf_used);
379 static const char **searchRecordKey (struct recKeys *reckeys,
380 int attrSetS, int attrUseS)
382 static const char *ws[32];
387 for (i = 0; i<32; i++)
390 while (off < reckeys->buf_used)
392 const char *src = reckeys->buf + off;
398 memcpy (&attrSet, src, sizeof(attrSet));
399 src += sizeof(attrSet);
401 memcpy (&attrUse, src, sizeof(attrUse));
402 src += sizeof(attrUse);
408 memcpy (&seqno, src, sizeof(seqno));
409 src += sizeof(seqno);
412 logf (LOG_LOG, "(%d,%d) %d %s", attrSet, attrUse, seqno, wstart);
414 if (attrUseS == attrUse && attrSetS == attrSet)
421 woff = seqno - startSeq;
422 if (woff >= 0 && woff < 31)
426 off = src - reckeys->buf;
428 assert (off == reckeys->buf_used);
432 static void addRecordKeyAny (const RecWord *p)
434 if (p->attrSet != 1 || p->attrUse != 1016)
438 memcpy (&w, p, sizeof(w));
446 #define FILE_READ_BUFSIZE 4096
448 static char *file_buf;
449 static int file_offset;
450 static int file_bufsize;
451 static int file_noread;
453 static void file_read_start (int fd)
456 file_buf = xmalloc (FILE_READ_BUFSIZE);
457 file_bufsize = read (fd, file_buf, FILE_READ_BUFSIZE);
461 static void file_read_stop (int fd)
467 static int file_read (int fd, char *buf, size_t count)
469 int l = file_bufsize - file_offset;
475 memcpy (buf, file_buf + file_offset, l);
477 if (count > file_bufsize)
479 if ((r = read (fd, buf + l, count)) == -1)
481 logf (LOG_FATAL|LOG_ERRNO, "read");
489 file_bufsize = r = read (fd, file_buf, FILE_READ_BUFSIZE);
492 logf (LOG_FATAL|LOG_ERRNO, "read");
498 memcpy (buf + l, file_buf, r);
499 file_noread += (l+r);
505 memcpy (buf + l, file_buf, count - l);
506 file_noread += count;
510 memcpy (buf, file_buf + file_offset, count);
511 file_offset += count;
512 file_noread += count;
516 static int atois (const char **s)
519 while ( (c=**s) >= '0' && c <= '9')
521 val = val*10 + c - '0';
527 static char *fileMatchStr (struct recKeys *reckeys, struct recordGroup *rGroup,
529 const char *recordType,
532 static char dstBuf[2048];
534 const char *s = spec;
535 static const char **w;
540 while (*s == ' ' || *s == '\t')
547 int attrSet, attrUse;
551 attrSet = atois (&s);
554 logf (LOG_WARN, "Missing , in match criteria %s in group %s",
555 spec, rGroup->groupName ? rGroup->groupName : "none");
559 attrUse = atois (&s);
560 w = searchRecordKey (reckeys, attrSet, attrUse);
565 for (i = 0; i<32; i++)
570 logf (LOG_WARN, "Missing ) in match criteria %s in group %s",
571 spec, rGroup->groupName ? rGroup->groupName : "none");
576 for (i = 0; i<32; i++)
577 if (matchFlag[i] && w[i])
589 logf (LOG_WARN, "Record in file %s didn't contain match"
590 " fields in (%d,%d)", fname, attrSet, attrUse);
598 const char *spec_src = NULL;
599 const char *s1 = ++s;
600 while (*s1 && *s1 != ' ' && *s1 != '\t')
606 memcpy (special, s, spec_len);
607 special[spec_len] = '\0';
610 if (strcmp (special, "group"))
611 spec_src = rGroup->groupName;
612 else if (strcmp (special, "database"))
613 spec_src = rGroup->databaseName;
614 else if (strcmp (special, "filename"))
616 else if (strcmp (special, "type"))
617 spec_src = recordType;
622 strcpy (dst, spec_src);
623 dst += strlen (spec_src);
626 else if (*s == '\"' || *s == '\'')
628 int stopMarker = *s++;
632 while (*s && *s != stopMarker)
640 strcpy (dst, tmpString);
641 dst += strlen (tmpString);
645 logf (LOG_WARN, "Syntax error in match criteria %s in group %s",
646 spec, rGroup->groupName ? rGroup->groupName : "none");
653 logf (LOG_WARN, "No match criteria for record %s in group %s",
654 fname, rGroup->groupName ? rGroup->groupName : "none");
660 static int recordExtract (SYSNO *sysno, const char *fname,
661 struct recordGroup *rGroup, int deleteFlag,
663 const char *file_type,
666 struct recExtractCtrl extractCtrl;
675 extractCtrl.subType = "";
676 extractCtrl.init = wordInit;
677 extractCtrl.add = addRecordKeyAny;
679 reckeys.buf_used = 0;
680 extractCtrl.readf = file_read;
681 r = (*recType->extract)(&extractCtrl);
685 logf (LOG_WARN, "Couldn't extract file %s, code %d", fname, r);
689 /* perform match if sysno not known and if match criteria is specified */
696 if (rGroup->fileMatch)
700 matchStr = fileMatchStr(&reckeys, rGroup, fname, file_type,
704 rinfo = dict_lookup (matchDict, matchStr);
706 memcpy (sysno, rinfo+1, sizeof(*sysno));
710 logf (LOG_WARN, "Record not inserted");
721 logf (LOG_LOG, "? record %s", fname);
724 logf (LOG_LOG, "add record %s", fname);
725 rec = rec_new (records);
729 dict_insert (matchDict, matchStr, sizeof(*sysno), sysno);
730 flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
736 struct recKeys delkeys;
738 rec = rec_get (records, *sysno);
740 delkeys.buf_used = rec->size[recInfo_delKeys];
741 delkeys.buf = rec->info[recInfo_delKeys];
742 flushRecordKeys (*sysno, 0, &delkeys, rec->info[recInfo_databaseName]);
745 if (!delkeys.buf_used)
747 logf (LOG_WARN, "cannot delete %s - no delete keys",
751 logf (LOG_LOG, "delete record %s", fname);
753 rec_del (records, &rec);
758 if (!delkeys.buf_used)
760 logf (LOG_WARN, "cannot update %s - no delete keys",
765 logf (LOG_LOG, "update record %s", fname);
766 flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
771 free (rec->info[recInfo_fileType]);
772 rec->info[recInfo_fileType] =
773 rec_strdup (file_type, &rec->size[recInfo_fileType]);
775 free (rec->info[recInfo_filename]);
776 rec->info[recInfo_filename] =
777 rec_strdup (fname, &rec->size[recInfo_filename]);
779 free (rec->info[recInfo_delKeys]);
780 if (reckeys.buf_used > 0 && rGroup->flagStoreKeys == 1)
782 rec->info[recInfo_delKeys] = malloc (reckeys.buf_used);
783 rec->size[recInfo_delKeys] = reckeys.buf_used;
784 memcpy (rec->info[recInfo_delKeys], reckeys.buf,
785 rec->size[recInfo_delKeys]);
789 rec->info[recInfo_delKeys] = NULL;
790 rec->size[recInfo_delKeys] = 0;
793 free (rec->info[recInfo_storeData]);
794 if (rGroup->flagStoreData == 1)
796 rec->size[recInfo_storeData] = file_noread;
797 rec->info[recInfo_storeData] = malloc (file_noread);
798 if (file_noread < FILE_READ_BUFSIZE)
799 memcpy (rec->info[recInfo_storeData], file_buf, file_noread);
802 if (lseek (fd, 0L, SEEK_SET) < 0)
804 logf (LOG_ERRNO|LOG_FATAL, "seek to 0 in %s", fname);
807 if (read (fd, rec->info[recInfo_storeData], file_noread)
810 logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
818 rec->info[recInfo_storeData] = NULL;
819 rec->size[recInfo_storeData] = 0;
821 free (rec->info[recInfo_databaseName]);
822 rec->info[recInfo_databaseName] =
823 rec_strdup (rGroup->databaseName, &rec->size[recInfo_databaseName]);
825 rec_put (records, &rec);
829 int fileExtract (SYSNO *sysno, const char *fname, struct recordGroup *rGroup,
836 const char *file_type;
839 if (!rGroup->groupName || !*rGroup->groupName)
842 sprintf (gprefix, "%s.", rGroup->groupName);
844 logf (LOG_DEBUG, "fileExtract %s", fname);
846 /* determine file extension */
847 for (i = strlen(fname); --i >= 0; )
853 else if (fname[i] == '.')
855 strcpy (ext, fname+i+1);
858 /* determine file type - depending on extension */
859 sprintf (ext_res, "%sfileExtension.%s", gprefix, ext);
860 if (!(file_type = res_get (common_resource, ext_res)))
862 if (!(recType = recType_byName (file_type)))
865 /* determine match criteria */
866 if (rGroup->fileMatch)
868 sprintf (ext_res, "%sfileMatch.%s", gprefix, ext);
869 rGroup->fileMatch = res_get (common_resource, ext_res);
870 if (!rGroup->fileMatch)
872 sprintf (ext_res, "%sfileMatch", gprefix);
873 rGroup->fileMatch = res_get (common_resource, ext_res);
877 /* determine database name */
878 if (!rGroup->databaseName)
880 sprintf (ext_res, "%sdatabase.%s", gprefix, ext);
881 if (!(rGroup->databaseName = res_get (common_resource, ext_res)))
883 sprintf (ext_res, "%sdatabase", gprefix);
884 rGroup->databaseName = res_get (common_resource, ext_res);
887 if (!rGroup->databaseName)
888 rGroup->databaseName = "Default";
890 if (rGroup->flagStoreData == -1)
893 sprintf (ext_res, "%sstoreData.%s", gprefix, ext);
894 if (!(sval = res_get (common_resource, ext_res)))
896 sprintf (ext_res, "%sstoreData", gprefix);
897 sval = res_get (common_resource, ext_res);
900 rGroup->flagStoreData = atoi (sval);
902 if (rGroup->flagStoreData == -1)
903 rGroup->flagStoreData = 0;
905 if (rGroup->flagStoreKeys == -1)
909 sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
910 if (!(sval = res_get (common_resource, ext_res)))
912 sprintf (ext_res, "%sstoreKeys", gprefix);
913 sval = res_get (common_resource, ext_res);
916 rGroup->flagStoreKeys = atoi (sval);
918 if (rGroup->flagStoreKeys == -1)
919 rGroup->flagStoreKeys = 0;
921 /* open input file */
922 if ((fd = open (fname, O_RDONLY)) == -1)
924 logf (LOG_WARN|LOG_ERRNO, "open %s", fname);
927 file_read_start (fd);
928 recordExtract (sysno, fname, rGroup, deleteFlag, fd,