2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.41 1995-12-06 16:06:42 adam
8 * Better diagnostics. Work on 'real' dictionary deletion.
10 * Revision 1.40 1995/12/05 16:57:40 adam
11 * More work on regular patterns.
13 * Revision 1.39 1995/12/05 13:20:18 adam
14 * Bug fix: file_read sometimes returned early EOF.
16 * Revision 1.38 1995/12/04 17:59:21 adam
17 * More work on regular expression conversion.
19 * Revision 1.37 1995/12/04 14:22:27 adam
20 * Extra arg to recType_byName.
21 * Started work on new regular expression parsed input to
24 * Revision 1.36 1995/11/30 08:34:29 adam
25 * Started work on commit facility.
26 * Changed a few malloc/free to xmalloc/xfree.
28 * Revision 1.35 1995/11/28 14:26:21 adam
29 * Bug fix: recordId with constant wasn't right.
30 * Bug fix: recordId dictionary entry wasn't deleted when needed.
32 * Revision 1.34 1995/11/28 09:09:38 adam
33 * Zebra config renamed.
34 * Use setting 'recordId' to identify record now.
35 * Bug fix in recindex.c: rec_release_blocks was invokeded even
36 * though the blocks were already released.
37 * File traversal properly deletes records when needed.
39 * Revision 1.33 1995/11/27 09:56:20 adam
40 * Record info elements better enumerated. Internal store of records.
42 * Revision 1.32 1995/11/25 10:24:05 adam
43 * More record fields - they are enumerated now.
44 * New options: flagStoreData flagStoreKey.
46 * Revision 1.31 1995/11/24 11:31:35 adam
47 * Commands add & del read filenames from stdin if source directory is
49 * Match criteria supports 'constant' strings.
51 * Revision 1.30 1995/11/22 17:19:16 adam
52 * Record management uses the bfile system.
54 * Revision 1.29 1995/11/21 15:01:14 adam
55 * New general match criteria implemented.
56 * New feature: document groups.
58 * Revision 1.28 1995/11/21 09:20:30 adam
59 * Yet more work on record match.
61 * Revision 1.27 1995/11/20 16:59:45 adam
62 * New update method: the 'old' keys are saved for each records.
64 * Revision 1.26 1995/11/20 11:56:24 adam
65 * Work on new traversal.
67 * Revision 1.25 1995/11/16 15:34:54 adam
68 * Uses new record management system in both indexer and server.
70 * Revision 1.24 1995/11/15 19:13:08 adam
71 * Work on record management.
73 * Revision 1.23 1995/10/27 14:00:10 adam
74 * Implemented detection of database availability.
76 * Revision 1.22 1995/10/17 18:02:07 adam
77 * New feature: databases. Implemented as prefix to words in dictionary.
79 * Revision 1.21 1995/10/10 12:24:38 adam
80 * Temporary sort files are compressed.
82 * Revision 1.20 1995/10/06 13:52:05 adam
83 * Bug fixes. Handler may abort further scanning.
85 * Revision 1.19 1995/10/04 12:55:16 adam
86 * Bug fix in ranked search. Use=Any keys inserted.
88 * Revision 1.18 1995/10/04 09:37:08 quinn
91 * Revision 1.17 1995/10/03 14:28:57 adam
92 * Buffered read in extract works.
94 * Revision 1.16 1995/10/03 14:28:45 adam
95 * Work on more effecient read handler in extract.
97 * Revision 1.15 1995/10/02 15:42:53 adam
98 * Extract uses file descriptors instead of FILE pointers.
100 * Revision 1.14 1995/10/02 15:29:13 adam
101 * More logging in file_extract.
103 * Revision 1.13 1995/09/29 14:01:39 adam
106 * Revision 1.12 1995/09/28 14:22:56 adam
107 * Sort uses smaller temporary files.
109 * Revision 1.11 1995/09/28 12:10:31 adam
110 * Bug fixes. Field prefix used in queries.
112 * Revision 1.10 1995/09/28 09:19:41 adam
113 * xfree/xmalloc used everywhere.
114 * Extract/retrieve method seems to work for text records.
116 * Revision 1.9 1995/09/27 12:22:28 adam
117 * More work on extract in record control.
118 * Field name is not in isam keys but in prefix in dictionary words.
120 * Revision 1.8 1995/09/14 07:48:22 adam
121 * Record control management.
123 * Revision 1.7 1995/09/11 13:09:32 adam
124 * More work on relevance feedback.
126 * Revision 1.6 1995/09/08 14:52:27 adam
127 * Minor changes. Dictionary is lower case now.
129 * Revision 1.5 1995/09/06 16:11:16 adam
130 * Option: only one word key per file.
132 * Revision 1.4 1995/09/05 15:28:39 adam
133 * More work on search engine.
135 * Revision 1.3 1995/09/04 12:33:41 adam
136 * Various cleanup. YAZ util used instead.
138 * Revision 1.2 1995/09/04 09:10:34 adam
139 * More work on index add/del/update.
140 * Merge sort implemented.
141 * Initial work on z39 server.
143 * Revision 1.1 1995/09/01 14:06:35 adam
144 * Split of work into more files.
153 #include <alexutil.h>
157 #include "recindex.h"
159 static Dict matchDict;
161 static Records records = NULL;
163 static char **key_buf;
164 static size_t ptr_top;
166 static size_t key_buf_used;
167 static int key_file_no;
169 static int records_inserted = 0;
170 static int records_updated = 0;
171 static int records_deleted = 0;
173 #define MATCH_DICT "match"
175 void key_open (int mem)
179 key_buf = xmalloc (mem);
180 ptr_top = mem/sizeof(char*);
186 if (!(matchDict = dict_open (MATCH_DICT, 20, 1)))
188 logf (LOG_FATAL, "dict_open fail of %s", MATCH_DICT);
192 records = rec_open (1);
201 void encode_key_init (struct encode_info *i)
207 char *encode_key_int (int d, char *bp)
216 else if (d <= 4194303)
218 *bp++ = 128 + (d>>16);
219 *bp++ = (d>>8) & 255;
224 *bp++ = 192 + (d>>24);
225 *bp++ = (d>>16) & 255;
226 *bp++ = (d>>8) & 255;
232 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
237 while ((*bp++ = *k++))
239 memcpy (&key, k+1, sizeof(struct it_key));
240 bp = encode_key_int ( (key.sysno - i->sysno) * 2 + *k, bp);
241 if (i->sysno != key.sysno)
243 i->sysno = key.sysno;
246 bp = encode_key_int (key.seqno - i->seqno, bp);
247 i->seqno = key.seqno;
248 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
250 logf (LOG_FATAL|LOG_ERRNO, "fwrite");
255 void key_flush (void)
260 struct encode_info encode_info;
266 logf (LOG_LOG, "sorting section %d", key_file_no);
267 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_qsort_compare);
268 sprintf (out_fname, TEMP_FNAME, key_file_no);
270 if (!(outf = fopen (out_fname, "w")))
272 logf (LOG_FATAL|LOG_ERRNO, "fopen (4) %s", out_fname);
275 logf (LOG_LOG, "writing section %d", key_file_no);
276 prevcp = cp = key_buf[ptr_top-ptr_i];
278 encode_key_init (&encode_info);
279 encode_key_write (cp, &encode_info, outf);
282 cp = key_buf[ptr_top-ptr_i];
283 if (strcmp (cp, prevcp))
285 encode_key_init (&encode_info);
286 encode_key_write (cp, &encode_info, outf);
290 encode_key_write (cp + strlen(cp), &encode_info, outf);
294 logf (LOG_FATAL|LOG_ERRNO, "fclose %s", out_fname);
297 logf (LOG_LOG, "finished section %d", key_file_no);
306 rec_close (&records);
307 dict_close (matchDict);
309 logf (LOG_LOG, "Records inserted %6d", records_inserted);
310 logf (LOG_LOG, "Records updated %6d", records_updated);
311 logf (LOG_LOG, "Records deleted %6d", records_deleted);
315 static void wordInit (RecWord *p)
319 p->which = Word_String;
328 static void addRecordKey (const RecWord *p)
335 if (reckeys.buf_used+1024 > reckeys.buf_max)
339 b = xmalloc (reckeys.buf_max += 65000);
340 if (reckeys.buf_used > 0)
341 memcpy (b, reckeys.buf, reckeys.buf_used);
345 dst = reckeys.buf + reckeys.buf_used;
349 attrSet = p->attrSet;
350 memcpy (dst, &attrSet, sizeof(attrSet));
351 dst += sizeof(attrSet);
353 attrUse = p->attrUse;
354 memcpy (dst, &attrUse, sizeof(attrUse));
355 dst += sizeof(attrUse);
357 for (i = 0; p->u.string[i]; i++)
358 *dst++ = p->u.string[i];
361 memcpy (dst, &p->seqno, sizeof(p->seqno));
362 dst += sizeof(p->seqno);
368 reckeys.buf_used = dst - reckeys.buf;
371 static void flushRecordKeys (SYSNO sysno, int cmd, struct recKeys *reckeys,
372 const char *databaseName)
375 while (off < reckeys->buf_used)
377 const char *src = reckeys->buf + off;
382 memcpy (&attrSet, src, sizeof(attrSet));
383 src += sizeof(attrSet);
385 memcpy (&attrUse, src, sizeof(attrUse));
386 src += sizeof(attrUse);
388 if (key_buf_used + 1024 > (ptr_top-ptr_i)*sizeof(char*))
391 key_buf[ptr_top-ptr_i] = (char*)key_buf + key_buf_used;
392 key_buf_used += index_word_prefix ((char*)key_buf + key_buf_used,
393 attrSet, attrUse, databaseName);
395 ((char*)key_buf) [key_buf_used++] = index_char_cvt (*src++);
397 ((char*)key_buf) [key_buf_used++] = '\0';
399 ((char*) key_buf)[key_buf_used++] = cmd;
401 memcpy (&key.seqno, src, sizeof(key.seqno));
402 src += sizeof(key.seqno);
404 memcpy ((char*)key_buf + key_buf_used, &key, sizeof(key));
405 key_buf_used += sizeof(key);
406 off = src - reckeys->buf;
408 assert (off == reckeys->buf_used);
411 static const char **searchRecordKey (struct recKeys *reckeys,
412 int attrSetS, int attrUseS)
414 static const char *ws[32];
419 for (i = 0; i<32; i++)
422 while (off < reckeys->buf_used)
424 const char *src = reckeys->buf + off;
430 memcpy (&attrSet, src, sizeof(attrSet));
431 src += sizeof(attrSet);
433 memcpy (&attrUse, src, sizeof(attrUse));
434 src += sizeof(attrUse);
440 memcpy (&seqno, src, sizeof(seqno));
441 src += sizeof(seqno);
444 logf (LOG_LOG, "(%d,%d) %d %s", attrSet, attrUse, seqno, wstart);
446 if (attrUseS == attrUse && attrSetS == attrSet)
453 woff = seqno - startSeq;
454 if (woff >= 0 && woff < 31)
458 off = src - reckeys->buf;
460 assert (off == reckeys->buf_used);
464 static void addRecordKeyAny (const RecWord *p)
466 if (p->attrSet != 1 || p->attrUse != 1016)
470 memcpy (&w, p, sizeof(w));
479 #define FILE_READ_BUFSIZE 4096
481 static int file_noread;
482 #if FILE_READ_BUFSIZE
483 static char *file_buf;
484 static int file_offset;
485 static int file_bufsize;
488 static void file_read_start (int fd)
491 #if FILE_READ_BUFSIZE
493 file_buf = xmalloc (FILE_READ_BUFSIZE);
494 file_bufsize = read (fd, file_buf, FILE_READ_BUFSIZE);
498 static void file_read_stop (int fd)
500 #if FILE_READ_BUFSIZE
506 static int file_read (int fd, char *buf, size_t count)
508 #if FILE_READ_BUFSIZE
509 int l = file_bufsize - file_offset;
515 memcpy (buf, file_buf + file_offset, l);
517 if (count > FILE_READ_BUFSIZE)
519 if ((r = read (fd, buf + l, count)) == -1)
521 logf (LOG_FATAL|LOG_ERRNO, "read");
529 file_bufsize = r = read (fd, file_buf, FILE_READ_BUFSIZE);
532 logf (LOG_FATAL|LOG_ERRNO, "read");
538 memcpy (buf + l, file_buf, r);
545 memcpy (buf + l, file_buf, count - l);
546 file_noread += count;
550 memcpy (buf, file_buf + file_offset, count);
551 file_offset += count;
552 file_noread += count;
556 r = read (fd, buf, count);
563 static int atois (const char **s)
566 while ( (c=**s) >= '0' && c <= '9')
568 val = val*10 + c - '0';
574 static char *fileMatchStr (struct recKeys *reckeys, struct recordGroup *rGroup,
578 static char dstBuf[2048];
580 const char *s = spec;
581 static const char **w;
586 while (*s == ' ' || *s == '\t')
593 int attrSet, attrUse;
597 attrSet = atois (&s);
600 logf (LOG_WARN, "Missing , in match criteria %s in group %s",
601 spec, rGroup->groupName ? rGroup->groupName : "none");
605 attrUse = atois (&s);
606 w = searchRecordKey (reckeys, attrSet, attrUse);
611 for (i = 0; i<32; i++)
616 logf (LOG_WARN, "Missing ) in match criteria %s in group %s",
617 spec, rGroup->groupName ? rGroup->groupName : "none");
622 for (i = 0; i<32; i++)
623 if (matchFlag[i] && w[i])
635 logf (LOG_WARN, "Record in file %s didn't contain match"
636 " fields in (%d,%d)", fname, attrSet, attrUse);
644 const char *spec_src = NULL;
645 const char *s1 = ++s;
646 while (*s1 && *s1 != ' ' && *s1 != '\t')
652 memcpy (special, s, spec_len);
653 special[spec_len] = '\0';
656 if (!strcmp (special, "group"))
657 spec_src = rGroup->groupName;
658 else if (!strcmp (special, "database"))
659 spec_src = rGroup->databaseName;
660 else if (!strcmp (special, "filename"))
662 else if (!strcmp (special, "type"))
663 spec_src = rGroup->recordType;
668 strcpy (dst, spec_src);
669 dst += strlen (spec_src);
672 else if (*s == '\"' || *s == '\'')
674 int stopMarker = *s++;
678 while (*s && *s != stopMarker)
681 tmpString[i++] = *s++;
686 strcpy (dst, tmpString);
687 dst += strlen (tmpString);
691 logf (LOG_WARN, "Syntax error in match criteria %s in group %s",
692 spec, rGroup->groupName ? rGroup->groupName : "none");
699 logf (LOG_WARN, "No match criteria for record %s in group %s",
700 fname, rGroup->groupName ? rGroup->groupName : "none");
706 static int recordExtract (SYSNO *sysno, const char *fname,
707 struct recordGroup *rGroup, int deleteFlag,
708 int fd, RecType recType, char *subType)
710 struct recExtractCtrl extractCtrl;
720 extractCtrl.subType = subType;
721 extractCtrl.init = wordInit;
722 extractCtrl.add = addRecordKeyAny;
724 reckeys.buf_used = 0;
725 extractCtrl.readf = file_read;
726 r = (*recType->extract)(&extractCtrl);
730 logf (LOG_WARN, "Couldn't extract file %s, code %d", fname, r);
735 /* perform match if sysno not known and if match criteria is specified */
742 if (rGroup->recordId && *rGroup->recordId)
746 matchStr = fileMatchStr (&reckeys, rGroup, fname,
750 rinfo = dict_lookup (matchDict, matchStr);
752 memcpy (sysno, rinfo+1, sizeof(*sysno));
756 logf (LOG_WARN, "Record not inserted");
767 logf (LOG_LOG, "? %s", fname);
770 logf (LOG_LOG, "add %s %s", rGroup->recordType, fname);
771 rec = rec_new (records);
776 dict_insert (matchDict, matchStr, sizeof(*sysno), sysno);
778 flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
784 struct recKeys delkeys;
786 rec = rec_get (records, *sysno);
788 delkeys.buf_used = rec->size[recInfo_delKeys];
789 delkeys.buf = rec->info[recInfo_delKeys];
790 flushRecordKeys (*sysno, 0, &delkeys, rec->info[recInfo_databaseName]);
793 if (!delkeys.buf_used)
795 logf (LOG_WARN, "cannot delete %s: storeKeys false",
801 logf (LOG_LOG, "delete %s %s", rGroup->recordType, fname);
804 dict_insert (matchDict, matchStr, sizeof(sysnoz), &sysnoz);
805 rec_del (records, &rec);
811 if (!delkeys.buf_used)
813 logf (LOG_WARN, "cannot update %s: storeKeys false",
818 logf (LOG_LOG, "update %s %s", rGroup->recordType,
820 flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
825 xfree (rec->info[recInfo_fileType]);
826 rec->info[recInfo_fileType] =
827 rec_strdup (rGroup->recordType, &rec->size[recInfo_fileType]);
829 xfree (rec->info[recInfo_filename]);
830 rec->info[recInfo_filename] =
831 rec_strdup (fname, &rec->size[recInfo_filename]);
833 xfree (rec->info[recInfo_delKeys]);
834 if (reckeys.buf_used > 0 && rGroup->flagStoreKeys == 1)
836 rec->info[recInfo_delKeys] = xmalloc (reckeys.buf_used);
837 rec->size[recInfo_delKeys] = reckeys.buf_used;
838 memcpy (rec->info[recInfo_delKeys], reckeys.buf,
839 rec->size[recInfo_delKeys]);
843 rec->info[recInfo_delKeys] = NULL;
844 rec->size[recInfo_delKeys] = 0;
847 xfree (rec->info[recInfo_storeData]);
848 if (rGroup->flagStoreData == 1)
850 rec->size[recInfo_storeData] = file_noread;
851 rec->info[recInfo_storeData] = xmalloc (file_noread);
852 #if FILE_READ_BUFSIZE
853 if (file_noread < FILE_READ_BUFSIZE)
854 memcpy (rec->info[recInfo_storeData], file_buf, file_noread);
858 if (lseek (fd, 0L, SEEK_SET) < 0)
860 logf (LOG_ERRNO|LOG_FATAL, "seek to 0 in %s", fname);
863 if (read (fd, rec->info[recInfo_storeData], file_noread)
866 logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
874 rec->info[recInfo_storeData] = NULL;
875 rec->size[recInfo_storeData] = 0;
877 xfree (rec->info[recInfo_databaseName]);
878 rec->info[recInfo_databaseName] =
879 rec_strdup (rGroup->databaseName, &rec->size[recInfo_databaseName]);
881 rec_put (records, &rec);
885 int fileExtract (SYSNO *sysno, const char *fname,
886 const struct recordGroup *rGroupP, int deleteFlag)
894 struct recordGroup rGroupM;
895 struct recordGroup *rGroup = &rGroupM;
897 memcpy (rGroup, rGroupP, sizeof(*rGroupP));
899 if (!rGroup->groupName || !*rGroup->groupName)
902 sprintf (gprefix, "%s.", rGroup->groupName);
904 logf (LOG_DEBUG, "fileExtract %s", fname);
906 /* determine file extension */
907 for (i = strlen(fname); --i >= 0; )
913 else if (fname[i] == '.')
915 strcpy (ext, fname+i+1);
918 /* determine file type - depending on extension */
919 if (!rGroup->recordType)
921 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
922 if (!(rGroup->recordType = res_get (common_resource, ext_res)))
924 sprintf (ext_res, "%srecordType", gprefix);
925 if (!(rGroup->recordType = res_get (common_resource, ext_res)))
927 logf (LOG_LOG, "? %s", fname);
932 if (!rGroup->recordType)
934 logf (LOG_LOG, "? record %s", fname);
937 if (!(recType = recType_byName (rGroup->recordType, subType)))
939 logf (LOG_WARN, "No such record type: %s", rGroup->recordType);
943 /* determine match criteria */
944 if (!rGroup->recordId)
946 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
947 rGroup->recordId = res_get (common_resource, ext_res);
950 /* determine database name */
951 if (!rGroup->databaseName)
953 sprintf (ext_res, "%sdatabase.%s", gprefix, ext);
954 if (!(rGroup->databaseName = res_get (common_resource, ext_res)))
956 sprintf (ext_res, "%sdatabase", gprefix);
957 rGroup->databaseName = res_get (common_resource, ext_res);
960 if (!rGroup->databaseName)
961 rGroup->databaseName = "Default";
963 if (rGroup->flagStoreData == -1)
966 sprintf (ext_res, "%sstoreData.%s", gprefix, ext);
967 if (!(sval = res_get (common_resource, ext_res)))
969 sprintf (ext_res, "%sstoreData", gprefix);
970 sval = res_get (common_resource, ext_res);
973 rGroup->flagStoreData = atoi (sval);
975 if (rGroup->flagStoreData == -1)
976 rGroup->flagStoreData = 0;
978 if (rGroup->flagStoreKeys == -1)
982 sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
983 if (!(sval = res_get (common_resource, ext_res)))
985 sprintf (ext_res, "%sstoreKeys", gprefix);
986 sval = res_get (common_resource, ext_res);
989 rGroup->flagStoreKeys = atoi (sval);
991 if (rGroup->flagStoreKeys == -1)
992 rGroup->flagStoreKeys = 0;
994 if (sysno && deleteFlag)
998 if ((fd = open (fname, O_RDONLY)) == -1)
1000 logf (LOG_WARN|LOG_ERRNO, "open %s", fname);
1004 file_read_start (fd);
1005 recordExtract (sysno, fname, rGroup, deleteFlag, fd, recType, subType);
1006 file_read_stop (fd);