2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.38 1995-12-04 17:59:21 adam
8 * More work on regular expression conversion.
10 * Revision 1.37 1995/12/04 14:22:27 adam
11 * Extra arg to recType_byName.
12 * Started work on new regular expression parsed input to
15 * Revision 1.36 1995/11/30 08:34:29 adam
16 * Started work on commit facility.
17 * Changed a few malloc/free to xmalloc/xfree.
19 * Revision 1.35 1995/11/28 14:26:21 adam
20 * Bug fix: recordId with constant wasn't right.
21 * Bug fix: recordId dictionary entry wasn't deleted when needed.
23 * Revision 1.34 1995/11/28 09:09:38 adam
24 * Zebra config renamed.
25 * Use setting 'recordId' to identify record now.
26 * Bug fix in recindex.c: rec_release_blocks was invokeded even
27 * though the blocks were already released.
28 * File traversal properly deletes records when needed.
30 * Revision 1.33 1995/11/27 09:56:20 adam
31 * Record info elements better enumerated. Internal store of records.
33 * Revision 1.32 1995/11/25 10:24:05 adam
34 * More record fields - they are enumerated now.
35 * New options: flagStoreData flagStoreKey.
37 * Revision 1.31 1995/11/24 11:31:35 adam
38 * Commands add & del read filenames from stdin if source directory is
40 * Match criteria supports 'constant' strings.
42 * Revision 1.30 1995/11/22 17:19:16 adam
43 * Record management uses the bfile system.
45 * Revision 1.29 1995/11/21 15:01:14 adam
46 * New general match criteria implemented.
47 * New feature: document groups.
49 * Revision 1.28 1995/11/21 09:20:30 adam
50 * Yet more work on record match.
52 * Revision 1.27 1995/11/20 16:59:45 adam
53 * New update method: the 'old' keys are saved for each records.
55 * Revision 1.26 1995/11/20 11:56:24 adam
56 * Work on new traversal.
58 * Revision 1.25 1995/11/16 15:34:54 adam
59 * Uses new record management system in both indexer and server.
61 * Revision 1.24 1995/11/15 19:13:08 adam
62 * Work on record management.
64 * Revision 1.23 1995/10/27 14:00:10 adam
65 * Implemented detection of database availability.
67 * Revision 1.22 1995/10/17 18:02:07 adam
68 * New feature: databases. Implemented as prefix to words in dictionary.
70 * Revision 1.21 1995/10/10 12:24:38 adam
71 * Temporary sort files are compressed.
73 * Revision 1.20 1995/10/06 13:52:05 adam
74 * Bug fixes. Handler may abort further scanning.
76 * Revision 1.19 1995/10/04 12:55:16 adam
77 * Bug fix in ranked search. Use=Any keys inserted.
79 * Revision 1.18 1995/10/04 09:37:08 quinn
82 * Revision 1.17 1995/10/03 14:28:57 adam
83 * Buffered read in extract works.
85 * Revision 1.16 1995/10/03 14:28:45 adam
86 * Work on more effecient read handler in extract.
88 * Revision 1.15 1995/10/02 15:42:53 adam
89 * Extract uses file descriptors instead of FILE pointers.
91 * Revision 1.14 1995/10/02 15:29:13 adam
92 * More logging in file_extract.
94 * Revision 1.13 1995/09/29 14:01:39 adam
97 * Revision 1.12 1995/09/28 14:22:56 adam
98 * Sort uses smaller temporary files.
100 * Revision 1.11 1995/09/28 12:10:31 adam
101 * Bug fixes. Field prefix used in queries.
103 * Revision 1.10 1995/09/28 09:19:41 adam
104 * xfree/xmalloc used everywhere.
105 * Extract/retrieve method seems to work for text records.
107 * Revision 1.9 1995/09/27 12:22:28 adam
108 * More work on extract in record control.
109 * Field name is not in isam keys but in prefix in dictionary words.
111 * Revision 1.8 1995/09/14 07:48:22 adam
112 * Record control management.
114 * Revision 1.7 1995/09/11 13:09:32 adam
115 * More work on relevance feedback.
117 * Revision 1.6 1995/09/08 14:52:27 adam
118 * Minor changes. Dictionary is lower case now.
120 * Revision 1.5 1995/09/06 16:11:16 adam
121 * Option: only one word key per file.
123 * Revision 1.4 1995/09/05 15:28:39 adam
124 * More work on search engine.
126 * Revision 1.3 1995/09/04 12:33:41 adam
127 * Various cleanup. YAZ util used instead.
129 * Revision 1.2 1995/09/04 09:10:34 adam
130 * More work on index add/del/update.
131 * Merge sort implemented.
132 * Initial work on z39 server.
134 * Revision 1.1 1995/09/01 14:06:35 adam
135 * Split of work into more files.
144 #include <alexutil.h>
148 #include "recindex.h"
150 static Dict matchDict;
152 static Records records = NULL;
154 static char **key_buf;
155 static size_t ptr_top;
157 static size_t key_buf_used;
158 static int key_file_no;
160 static int records_inserted = 0;
161 static int records_updated = 0;
162 static int records_deleted = 0;
164 #define MATCH_DICT "match"
166 void key_open (int mem)
170 key_buf = xmalloc (mem);
171 ptr_top = mem/sizeof(char*);
177 if (!(matchDict = dict_open (MATCH_DICT, 20, 1)))
179 logf (LOG_FATAL, "dict_open fail of %s", MATCH_DICT);
183 records = rec_open (1);
192 void encode_key_init (struct encode_info *i)
198 char *encode_key_int (int d, char *bp)
207 else if (d <= 4194303)
209 *bp++ = 128 + (d>>16);
210 *bp++ = (d>>8) & 255;
215 *bp++ = 192 + (d>>24);
216 *bp++ = (d>>16) & 255;
217 *bp++ = (d>>8) & 255;
223 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
228 while ((*bp++ = *k++))
230 memcpy (&key, k+1, sizeof(struct it_key));
231 bp = encode_key_int ( (key.sysno - i->sysno) * 2 + *k, bp);
232 if (i->sysno != key.sysno)
234 i->sysno = key.sysno;
237 bp = encode_key_int (key.seqno - i->seqno, bp);
238 i->seqno = key.seqno;
239 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
241 logf (LOG_FATAL|LOG_ERRNO, "fwrite");
246 void key_flush (void)
251 struct encode_info encode_info;
257 logf (LOG_LOG, "sorting section %d", key_file_no);
258 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_qsort_compare);
259 sprintf (out_fname, TEMP_FNAME, key_file_no);
261 if (!(outf = fopen (out_fname, "w")))
263 logf (LOG_FATAL|LOG_ERRNO, "fopen (4) %s", out_fname);
266 logf (LOG_LOG, "writing section %d", key_file_no);
267 prevcp = cp = key_buf[ptr_top-ptr_i];
269 encode_key_init (&encode_info);
270 encode_key_write (cp, &encode_info, outf);
273 cp = key_buf[ptr_top-ptr_i];
274 if (strcmp (cp, prevcp))
276 encode_key_init (&encode_info);
277 encode_key_write (cp, &encode_info, outf);
281 encode_key_write (cp + strlen(cp), &encode_info, outf);
285 logf (LOG_FATAL|LOG_ERRNO, "fclose %s", out_fname);
288 logf (LOG_LOG, "finished section %d", key_file_no);
297 rec_close (&records);
298 dict_close (matchDict);
300 logf (LOG_LOG, "Records inserted %6d", records_inserted);
301 logf (LOG_LOG, "Records updated %6d", records_updated);
302 logf (LOG_LOG, "Records deleted %6d", records_deleted);
306 static void wordInit (RecWord *p)
310 p->which = Word_String;
319 static void addRecordKey (const RecWord *p)
326 if (reckeys.buf_used+1024 > reckeys.buf_max)
330 b = xmalloc (reckeys.buf_max += 65000);
331 if (reckeys.buf_used > 0)
332 memcpy (b, reckeys.buf, reckeys.buf_used);
336 dst = reckeys.buf + reckeys.buf_used;
340 attrSet = p->attrSet;
341 memcpy (dst, &attrSet, sizeof(attrSet));
342 dst += sizeof(attrSet);
344 attrUse = p->attrUse;
345 memcpy (dst, &attrUse, sizeof(attrUse));
346 dst += sizeof(attrUse);
348 for (i = 0; p->u.string[i]; i++)
349 *dst++ = p->u.string[i];
352 memcpy (dst, &p->seqno, sizeof(p->seqno));
353 dst += sizeof(p->seqno);
359 reckeys.buf_used = dst - reckeys.buf;
362 static void flushRecordKeys (SYSNO sysno, int cmd, struct recKeys *reckeys,
363 const char *databaseName)
366 while (off < reckeys->buf_used)
368 const char *src = reckeys->buf + off;
373 memcpy (&attrSet, src, sizeof(attrSet));
374 src += sizeof(attrSet);
376 memcpy (&attrUse, src, sizeof(attrUse));
377 src += sizeof(attrUse);
379 if (key_buf_used + 1024 > (ptr_top-ptr_i)*sizeof(char*))
382 key_buf[ptr_top-ptr_i] = (char*)key_buf + key_buf_used;
383 key_buf_used += index_word_prefix ((char*)key_buf + key_buf_used,
384 attrSet, attrUse, databaseName);
386 ((char*)key_buf) [key_buf_used++] = index_char_cvt (*src++);
388 ((char*)key_buf) [key_buf_used++] = '\0';
390 ((char*) key_buf)[key_buf_used++] = cmd;
392 memcpy (&key.seqno, src, sizeof(key.seqno));
393 src += sizeof(key.seqno);
395 memcpy ((char*)key_buf + key_buf_used, &key, sizeof(key));
396 key_buf_used += sizeof(key);
397 off = src - reckeys->buf;
399 assert (off == reckeys->buf_used);
402 static const char **searchRecordKey (struct recKeys *reckeys,
403 int attrSetS, int attrUseS)
405 static const char *ws[32];
410 for (i = 0; i<32; i++)
413 while (off < reckeys->buf_used)
415 const char *src = reckeys->buf + off;
421 memcpy (&attrSet, src, sizeof(attrSet));
422 src += sizeof(attrSet);
424 memcpy (&attrUse, src, sizeof(attrUse));
425 src += sizeof(attrUse);
431 memcpy (&seqno, src, sizeof(seqno));
432 src += sizeof(seqno);
435 logf (LOG_LOG, "(%d,%d) %d %s", attrSet, attrUse, seqno, wstart);
437 if (attrUseS == attrUse && attrSetS == attrSet)
444 woff = seqno - startSeq;
445 if (woff >= 0 && woff < 31)
449 off = src - reckeys->buf;
451 assert (off == reckeys->buf_used);
455 static void addRecordKeyAny (const RecWord *p)
457 if (p->attrSet != 1 || p->attrUse != 1016)
461 memcpy (&w, p, sizeof(w));
469 #define FILE_READ_BUFSIZE 4096
471 static char *file_buf;
472 static int file_offset;
473 static int file_bufsize;
474 static int file_noread;
476 static void file_read_start (int fd)
479 file_buf = xmalloc (FILE_READ_BUFSIZE);
480 file_bufsize = read (fd, file_buf, FILE_READ_BUFSIZE);
484 static void file_read_stop (int fd)
490 static int file_read (int fd, char *buf, size_t count)
492 int l = file_bufsize - file_offset;
498 memcpy (buf, file_buf + file_offset, l);
500 if (count > file_bufsize)
502 if ((r = read (fd, buf + l, count)) == -1)
504 logf (LOG_FATAL|LOG_ERRNO, "read");
512 file_bufsize = r = read (fd, file_buf, FILE_READ_BUFSIZE);
515 logf (LOG_FATAL|LOG_ERRNO, "read");
521 memcpy (buf + l, file_buf, r);
522 file_noread += (l+r);
528 memcpy (buf + l, file_buf, count - l);
529 file_noread += count;
533 memcpy (buf, file_buf + file_offset, count);
534 file_offset += count;
535 file_noread += count;
539 static int atois (const char **s)
542 while ( (c=**s) >= '0' && c <= '9')
544 val = val*10 + c - '0';
550 static char *fileMatchStr (struct recKeys *reckeys, struct recordGroup *rGroup,
554 static char dstBuf[2048];
556 const char *s = spec;
557 static const char **w;
562 while (*s == ' ' || *s == '\t')
569 int attrSet, attrUse;
573 attrSet = atois (&s);
576 logf (LOG_WARN, "Missing , in match criteria %s in group %s",
577 spec, rGroup->groupName ? rGroup->groupName : "none");
581 attrUse = atois (&s);
582 w = searchRecordKey (reckeys, attrSet, attrUse);
587 for (i = 0; i<32; i++)
592 logf (LOG_WARN, "Missing ) in match criteria %s in group %s",
593 spec, rGroup->groupName ? rGroup->groupName : "none");
598 for (i = 0; i<32; i++)
599 if (matchFlag[i] && w[i])
611 logf (LOG_WARN, "Record in file %s didn't contain match"
612 " fields in (%d,%d)", fname, attrSet, attrUse);
620 const char *spec_src = NULL;
621 const char *s1 = ++s;
622 while (*s1 && *s1 != ' ' && *s1 != '\t')
628 memcpy (special, s, spec_len);
629 special[spec_len] = '\0';
632 if (!strcmp (special, "group"))
633 spec_src = rGroup->groupName;
634 else if (!strcmp (special, "database"))
635 spec_src = rGroup->databaseName;
636 else if (!strcmp (special, "filename"))
638 else if (!strcmp (special, "type"))
639 spec_src = rGroup->recordType;
644 strcpy (dst, spec_src);
645 dst += strlen (spec_src);
648 else if (*s == '\"' || *s == '\'')
650 int stopMarker = *s++;
654 while (*s && *s != stopMarker)
657 tmpString[i++] = *s++;
662 strcpy (dst, tmpString);
663 dst += strlen (tmpString);
667 logf (LOG_WARN, "Syntax error in match criteria %s in group %s",
668 spec, rGroup->groupName ? rGroup->groupName : "none");
675 logf (LOG_WARN, "No match criteria for record %s in group %s",
676 fname, rGroup->groupName ? rGroup->groupName : "none");
682 static int recordExtract (SYSNO *sysno, const char *fname,
683 struct recordGroup *rGroup, int deleteFlag,
688 struct recExtractCtrl extractCtrl;
698 extractCtrl.subType = subType;
699 extractCtrl.init = wordInit;
700 extractCtrl.add = addRecordKeyAny;
702 reckeys.buf_used = 0;
703 extractCtrl.readf = file_read;
704 r = (*recType->extract)(&extractCtrl);
708 logf (LOG_WARN, "Couldn't extract file %s, code %d", fname, r);
713 /* perform match if sysno not known and if match criteria is specified */
720 if (rGroup->recordId && *rGroup->recordId)
724 matchStr = fileMatchStr (&reckeys, rGroup, fname,
728 rinfo = dict_lookup (matchDict, matchStr);
730 memcpy (sysno, rinfo+1, sizeof(*sysno));
734 logf (LOG_WARN, "Record not inserted");
745 logf (LOG_LOG, "? %s", fname);
748 logf (LOG_LOG, "add %s %s", rGroup->recordType, fname);
749 rec = rec_new (records);
754 dict_insert (matchDict, matchStr, sizeof(*sysno), sysno);
756 flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
762 struct recKeys delkeys;
764 rec = rec_get (records, *sysno);
766 delkeys.buf_used = rec->size[recInfo_delKeys];
767 delkeys.buf = rec->info[recInfo_delKeys];
768 flushRecordKeys (*sysno, 0, &delkeys, rec->info[recInfo_databaseName]);
771 if (!delkeys.buf_used)
773 logf (LOG_WARN, "cannot delete %s - no delete keys", fname);
778 logf (LOG_LOG, "delete %s %s", rGroup->recordType, fname);
781 dict_insert (matchDict, matchStr, sizeof(sysnoz), &sysnoz);
782 rec_del (records, &rec);
788 if (!delkeys.buf_used)
790 logf (LOG_WARN, "cannot update %s - no delete keys",
795 logf (LOG_LOG, "update %s %s", rGroup->recordType,
797 flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
802 xfree (rec->info[recInfo_fileType]);
803 rec->info[recInfo_fileType] =
804 rec_strdup (rGroup->recordType, &rec->size[recInfo_fileType]);
806 xfree (rec->info[recInfo_filename]);
807 rec->info[recInfo_filename] =
808 rec_strdup (fname, &rec->size[recInfo_filename]);
810 xfree (rec->info[recInfo_delKeys]);
811 if (reckeys.buf_used > 0 && rGroup->flagStoreKeys == 1)
813 rec->info[recInfo_delKeys] = xmalloc (reckeys.buf_used);
814 rec->size[recInfo_delKeys] = reckeys.buf_used;
815 memcpy (rec->info[recInfo_delKeys], reckeys.buf,
816 rec->size[recInfo_delKeys]);
820 rec->info[recInfo_delKeys] = NULL;
821 rec->size[recInfo_delKeys] = 0;
824 xfree (rec->info[recInfo_storeData]);
825 if (rGroup->flagStoreData == 1)
827 rec->size[recInfo_storeData] = file_noread;
828 rec->info[recInfo_storeData] = xmalloc (file_noread);
829 if (file_noread < FILE_READ_BUFSIZE)
830 memcpy (rec->info[recInfo_storeData], file_buf, file_noread);
833 if (lseek (fd, 0L, SEEK_SET) < 0)
835 logf (LOG_ERRNO|LOG_FATAL, "seek to 0 in %s", fname);
838 if (read (fd, rec->info[recInfo_storeData], file_noread)
841 logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
849 rec->info[recInfo_storeData] = NULL;
850 rec->size[recInfo_storeData] = 0;
852 xfree (rec->info[recInfo_databaseName]);
853 rec->info[recInfo_databaseName] =
854 rec_strdup (rGroup->databaseName, &rec->size[recInfo_databaseName]);
856 rec_put (records, &rec);
860 int fileExtract (SYSNO *sysno, const char *fname,
861 const struct recordGroup *rGroupP, int deleteFlag)
869 struct recordGroup rGroupM;
870 struct recordGroup *rGroup = &rGroupM;
872 memcpy (rGroup, rGroupP, sizeof(*rGroupP));
874 if (!rGroup->groupName || !*rGroup->groupName)
877 sprintf (gprefix, "%s.", rGroup->groupName);
879 logf (LOG_DEBUG, "fileExtract %s", fname);
881 /* determine file extension */
882 for (i = strlen(fname); --i >= 0; )
888 else if (fname[i] == '.')
890 strcpy (ext, fname+i+1);
893 /* determine file type - depending on extension */
894 if (!rGroup->recordType)
896 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
897 if (!(rGroup->recordType = res_get (common_resource, ext_res)))
899 sprintf (ext_res, "%srecordType", gprefix);
900 if (!(rGroup->recordType = res_get (common_resource, ext_res)))
902 logf (LOG_LOG, "? %s", fname);
907 if (!rGroup->recordType)
909 logf (LOG_LOG, "? record %s", fname);
912 if (!(recType = recType_byName (rGroup->recordType, subType)))
914 logf (LOG_WARN, "No such record type: %s", rGroup->recordType);
918 /* determine match criteria */
919 if (!rGroup->recordId)
921 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
922 rGroup->recordId = res_get (common_resource, ext_res);
925 /* determine database name */
926 if (!rGroup->databaseName)
928 sprintf (ext_res, "%sdatabase.%s", gprefix, ext);
929 if (!(rGroup->databaseName = res_get (common_resource, ext_res)))
931 sprintf (ext_res, "%sdatabase", gprefix);
932 rGroup->databaseName = res_get (common_resource, ext_res);
935 if (!rGroup->databaseName)
936 rGroup->databaseName = "Default";
938 if (rGroup->flagStoreData == -1)
941 sprintf (ext_res, "%sstoreData.%s", gprefix, ext);
942 if (!(sval = res_get (common_resource, ext_res)))
944 sprintf (ext_res, "%sstoreData", gprefix);
945 sval = res_get (common_resource, ext_res);
948 rGroup->flagStoreData = atoi (sval);
950 if (rGroup->flagStoreData == -1)
951 rGroup->flagStoreData = 0;
953 if (rGroup->flagStoreKeys == -1)
957 sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
958 if (!(sval = res_get (common_resource, ext_res)))
960 sprintf (ext_res, "%sstoreKeys", gprefix);
961 sval = res_get (common_resource, ext_res);
964 rGroup->flagStoreKeys = atoi (sval);
966 if (rGroup->flagStoreKeys == -1)
967 rGroup->flagStoreKeys = 0;
969 if (sysno && deleteFlag)
973 if ((fd = open (fname, O_RDONLY)) == -1)
975 logf (LOG_WARN|LOG_ERRNO, "open %s", fname);
979 file_read_start (fd);
980 recordExtract (sysno, fname, rGroup, deleteFlag, fd, recType, subType);