2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.48 1996-02-01 20:53:26 adam
8 * The temporary per-record keys are compacted a little, and duplication
9 * of the per-records keys are avoided when they are saved in the record
12 * Revision 1.47 1996/01/17 14:57:48 adam
13 * Prototype changed for reader functions in extract/retrieve. File
14 * is identified by 'void *' instead of 'int.
16 * Revision 1.46 1995/12/15 14:57:16 adam
19 * Revision 1.45 1995/12/15 12:37:41 adam
20 * In addRecordKeyAny: Writes key only when attrSet != -1.
22 * Revision 1.44 1995/12/12 16:00:54 adam
23 * System call sync(2) used after update/commit.
24 * Locking (based on fcntl) uses F_EXLCK and F_SHLCK instead of F_WRLCK
27 * Revision 1.43 1995/12/11 09:12:46 adam
28 * The rec_get function returns NULL if record doesn't exist - will
29 * happen in the server if the result set records have been deleted since
30 * the creation of the set (i.e. the search).
31 * The server saves a result temporarily if it is 'volatile', i.e. the
32 * set is register dependent.
34 * Revision 1.42 1995/12/07 17:38:46 adam
35 * Work locking mechanisms for concurrent updates/commit.
37 * Revision 1.41 1995/12/06 16:06:42 adam
38 * Better diagnostics. Work on 'real' dictionary deletion.
40 * Revision 1.40 1995/12/05 16:57:40 adam
41 * More work on regular patterns.
43 * Revision 1.39 1995/12/05 13:20:18 adam
44 * Bug fix: file_read sometimes returned early EOF.
46 * Revision 1.38 1995/12/04 17:59:21 adam
47 * More work on regular expression conversion.
49 * Revision 1.37 1995/12/04 14:22:27 adam
50 * Extra arg to recType_byName.
51 * Started work on new regular expression parsed input to
54 * Revision 1.36 1995/11/30 08:34:29 adam
55 * Started work on commit facility.
56 * Changed a few malloc/free to xmalloc/xfree.
58 * Revision 1.35 1995/11/28 14:26:21 adam
59 * Bug fix: recordId with constant wasn't right.
60 * Bug fix: recordId dictionary entry wasn't deleted when needed.
62 * Revision 1.34 1995/11/28 09:09:38 adam
63 * Zebra config renamed.
64 * Use setting 'recordId' to identify record now.
65 * Bug fix in recindex.c: rec_release_blocks was invokeded even
66 * though the blocks were already released.
67 * File traversal properly deletes records when needed.
69 * Revision 1.33 1995/11/27 09:56:20 adam
70 * Record info elements better enumerated. Internal store of records.
72 * Revision 1.32 1995/11/25 10:24:05 adam
73 * More record fields - they are enumerated now.
74 * New options: flagStoreData flagStoreKey.
76 * Revision 1.31 1995/11/24 11:31:35 adam
77 * Commands add & del read filenames from stdin if source directory is
79 * Match criteria supports 'constant' strings.
81 * Revision 1.30 1995/11/22 17:19:16 adam
82 * Record management uses the bfile system.
84 * Revision 1.29 1995/11/21 15:01:14 adam
85 * New general match criteria implemented.
86 * New feature: document groups.
88 * Revision 1.28 1995/11/21 09:20:30 adam
89 * Yet more work on record match.
91 * Revision 1.27 1995/11/20 16:59:45 adam
92 * New update method: the 'old' keys are saved for each records.
94 * Revision 1.26 1995/11/20 11:56:24 adam
95 * Work on new traversal.
97 * Revision 1.25 1995/11/16 15:34:54 adam
98 * Uses new record management system in both indexer and server.
100 * Revision 1.24 1995/11/15 19:13:08 adam
101 * Work on record management.
103 * Revision 1.23 1995/10/27 14:00:10 adam
104 * Implemented detection of database availability.
106 * Revision 1.22 1995/10/17 18:02:07 adam
107 * New feature: databases. Implemented as prefix to words in dictionary.
109 * Revision 1.21 1995/10/10 12:24:38 adam
110 * Temporary sort files are compressed.
112 * Revision 1.20 1995/10/06 13:52:05 adam
113 * Bug fixes. Handler may abort further scanning.
115 * Revision 1.19 1995/10/04 12:55:16 adam
116 * Bug fix in ranked search. Use=Any keys inserted.
118 * Revision 1.18 1995/10/04 09:37:08 quinn
121 * Revision 1.17 1995/10/03 14:28:57 adam
122 * Buffered read in extract works.
124 * Revision 1.16 1995/10/03 14:28:45 adam
125 * Work on more effecient read handler in extract.
127 * Revision 1.15 1995/10/02 15:42:53 adam
128 * Extract uses file descriptors instead of FILE pointers.
130 * Revision 1.14 1995/10/02 15:29:13 adam
131 * More logging in file_extract.
133 * Revision 1.13 1995/09/29 14:01:39 adam
136 * Revision 1.12 1995/09/28 14:22:56 adam
137 * Sort uses smaller temporary files.
139 * Revision 1.11 1995/09/28 12:10:31 adam
140 * Bug fixes. Field prefix used in queries.
142 * Revision 1.10 1995/09/28 09:19:41 adam
143 * xfree/xmalloc used everywhere.
144 * Extract/retrieve method seems to work for text records.
146 * Revision 1.9 1995/09/27 12:22:28 adam
147 * More work on extract in record control.
148 * Field name is not in isam keys but in prefix in dictionary words.
150 * Revision 1.8 1995/09/14 07:48:22 adam
151 * Record control management.
153 * Revision 1.7 1995/09/11 13:09:32 adam
154 * More work on relevance feedback.
156 * Revision 1.6 1995/09/08 14:52:27 adam
157 * Minor changes. Dictionary is lower case now.
159 * Revision 1.5 1995/09/06 16:11:16 adam
160 * Option: only one word key per file.
162 * Revision 1.4 1995/09/05 15:28:39 adam
163 * More work on search engine.
165 * Revision 1.3 1995/09/04 12:33:41 adam
166 * Various cleanup. YAZ util used instead.
168 * Revision 1.2 1995/09/04 09:10:34 adam
169 * More work on index add/del/update.
170 * Merge sort implemented.
171 * Initial work on z39 server.
173 * Revision 1.1 1995/09/01 14:06:35 adam
174 * Split of work into more files.
183 #include <alexutil.h>
187 #include "recindex.h"
189 static Dict matchDict;
191 static Records records = NULL;
193 static char **key_buf;
194 static size_t ptr_top;
196 static size_t key_buf_used;
197 static int key_file_no;
199 static int records_inserted = 0;
200 static int records_updated = 0;
201 static int records_deleted = 0;
203 void key_open (int mem)
207 key_buf = xmalloc (mem);
208 ptr_top = mem/sizeof(char*);
214 if (!(matchDict = dict_open (GMATCH_DICT, 50, 1)))
216 logf (LOG_FATAL, "dict_open fail of %s", GMATCH_DICT);
220 records = rec_open (1);
229 void encode_key_init (struct encode_info *i)
235 char *encode_key_int (int d, char *bp)
244 else if (d <= 4194303)
246 *bp++ = 128 + (d>>16);
247 *bp++ = (d>>8) & 255;
252 *bp++ = 192 + (d>>24);
253 *bp++ = (d>>16) & 255;
254 *bp++ = (d>>8) & 255;
260 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
265 while ((*bp++ = *k++))
267 memcpy (&key, k+1, sizeof(struct it_key));
268 bp = encode_key_int ( (key.sysno - i->sysno) * 2 + *k, bp);
269 if (i->sysno != key.sysno)
271 i->sysno = key.sysno;
274 bp = encode_key_int (key.seqno - i->seqno, bp);
275 i->seqno = key.seqno;
276 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
278 logf (LOG_FATAL|LOG_ERRNO, "fwrite");
283 void key_flush (void)
288 struct encode_info encode_info;
294 logf (LOG_LOG, "sorting section %d", key_file_no);
295 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_qsort_compare);
296 sprintf (out_fname, TEMP_FNAME, key_file_no);
298 if (!(outf = fopen (out_fname, "w")))
300 logf (LOG_FATAL|LOG_ERRNO, "fopen (4) %s", out_fname);
303 logf (LOG_LOG, "writing section %d", key_file_no);
304 prevcp = cp = key_buf[ptr_top-ptr_i];
306 encode_key_init (&encode_info);
307 encode_key_write (cp, &encode_info, outf);
310 cp = key_buf[ptr_top-ptr_i];
311 if (strcmp (cp, prevcp))
313 encode_key_init (&encode_info);
314 encode_key_write (cp, &encode_info, outf);
318 encode_key_write (cp + strlen(cp), &encode_info, outf);
322 logf (LOG_FATAL|LOG_ERRNO, "fclose %s", out_fname);
325 logf (LOG_LOG, "finished section %d", key_file_no);
334 rec_close (&records);
335 dict_close (matchDict);
337 logf (LOG_LOG, "Records inserted %6d", records_inserted);
338 logf (LOG_LOG, "Records updated %6d", records_updated);
339 logf (LOG_LOG, "Records deleted %6d", records_deleted);
343 static void wordInit (RecWord *p)
347 p->which = Word_String;
358 static void addRecordKey (const RecWord *p)
366 if (reckeys.buf_used+1024 > reckeys.buf_max)
370 b = xmalloc (reckeys.buf_max += 128000);
371 if (reckeys.buf_used > 0)
372 memcpy (b, reckeys.buf, reckeys.buf_used);
376 dst = reckeys.buf + reckeys.buf_used;
378 attrSet = p->attrSet;
379 if (reckeys.buf_used > 0 && reckeys.prevAttrSet == attrSet)
382 reckeys.prevAttrSet = attrSet;
383 attrUse = p->attrUse;
384 if (reckeys.buf_used > 0 && reckeys.prevAttrUse == attrUse)
387 reckeys.prevAttrUse = attrUse;
396 memcpy (dst, &attrSet, sizeof(attrSet));
397 dst += sizeof(attrSet);
401 memcpy (dst, &attrUse, sizeof(attrUse));
402 dst += sizeof(attrUse);
404 for (i = 0; p->u.string[i]; i++)
405 *dst++ = p->u.string[i];
408 memcpy (dst, &p->seqno, sizeof(p->seqno));
409 dst += sizeof(p->seqno);
415 reckeys.buf_used = dst - reckeys.buf;
418 static void flushRecordKeys (SYSNO sysno, int cmd, struct recKeys *reckeys,
419 const char *databaseName)
424 while (off < reckeys->buf_used)
426 const char *src = reckeys->buf + off;
434 memcpy (&attrSet, src, sizeof(attrSet));
435 src += sizeof(attrSet);
439 memcpy (&attrUse, src, sizeof(attrUse));
440 src += sizeof(attrUse);
442 if (key_buf_used + 1024 > (ptr_top-ptr_i)*sizeof(char*))
445 key_buf[ptr_top-ptr_i] = (char*)key_buf + key_buf_used;
446 key_buf_used += index_word_prefix ((char*)key_buf + key_buf_used,
447 attrSet, attrUse, databaseName);
449 ((char*)key_buf) [key_buf_used++] = index_char_cvt (*src++);
451 ((char*)key_buf) [key_buf_used++] = '\0';
453 ((char*) key_buf)[key_buf_used++] = cmd;
455 memcpy (&key.seqno, src, sizeof(key.seqno));
456 src += sizeof(key.seqno);
458 memcpy ((char*)key_buf + key_buf_used, &key, sizeof(key));
459 key_buf_used += sizeof(key);
460 off = src - reckeys->buf;
462 assert (off == reckeys->buf_used);
465 static const char **searchRecordKey (struct recKeys *reckeys,
466 int attrSetS, int attrUseS)
468 static const char *ws[32];
473 for (i = 0; i<32; i++)
476 while (off < reckeys->buf_used)
478 const char *src = reckeys->buf + off;
484 memcpy (&attrSet, src, sizeof(attrSet));
485 src += sizeof(attrSet);
487 memcpy (&attrUse, src, sizeof(attrUse));
488 src += sizeof(attrUse);
494 memcpy (&seqno, src, sizeof(seqno));
495 src += sizeof(seqno);
498 logf (LOG_LOG, "(%d,%d) %d %s", attrSet, attrUse, seqno, wstart);
500 if (attrUseS == attrUse && attrSetS == attrSet)
507 woff = seqno - startSeq;
508 if (woff >= 0 && woff < 31)
512 off = src - reckeys->buf;
514 assert (off == reckeys->buf_used);
518 static void addRecordKeyAny (const RecWord *p)
520 if (p->attrSet != 1 || p->attrUse != 1016)
524 memcpy (&w, p, sizeof(w));
529 if (p->attrSet != -1)
533 #define FILE_READ_BUFSIZE 4096
534 struct file_read_info {
537 #if FILE_READ_BUFSIZE
544 static struct file_read_info *file_read_start (int fd)
546 struct file_read_info *fi = xmalloc (sizeof(*fi));
550 #if FILE_READ_BUFSIZE
552 fi->file_buf = xmalloc (FILE_READ_BUFSIZE);
553 fi->file_bufsize = read (fd, fi->file_buf, FILE_READ_BUFSIZE);
558 static void file_read_stop (struct file_read_info *fi)
561 #if FILE_READ_BUFSIZE
562 xfree (fi->file_buf);
568 static int file_read (void *handle, char *buf, size_t count)
570 struct file_read_info *p = handle;
572 #if FILE_READ_BUFSIZE
573 int l = p->file_bufsize - p->file_offset;
579 memcpy (buf, p->file_buf + p->file_offset, l);
581 if (count > FILE_READ_BUFSIZE)
583 if ((r = read (fd, buf + l, count)) == -1)
585 logf (LOG_FATAL|LOG_ERRNO, "read");
590 p->file_noread += l+r;
593 p->file_bufsize = r = read (fd, p->file_buf, FILE_READ_BUFSIZE);
596 logf (LOG_FATAL|LOG_ERRNO, "read");
602 memcpy (buf + l, p->file_buf, r);
603 p->file_noread += l+r;
608 p->file_offset = count;
609 memcpy (buf + l, p->file_buf, count - l);
610 p->file_noread += count;
614 memcpy (buf, p->file_buf + p->file_offset, count);
615 p->file_offset += count;
616 p->file_noread += count;
620 r = read (fd, buf, count);
627 static int atois (const char **s)
630 while ( (c=**s) >= '0' && c <= '9')
632 val = val*10 + c - '0';
638 static char *fileMatchStr (struct recKeys *reckeys, struct recordGroup *rGroup,
642 static char dstBuf[2048];
644 const char *s = spec;
645 static const char **w;
650 while (*s == ' ' || *s == '\t')
657 int attrSet, attrUse;
661 attrSet = atois (&s);
664 logf (LOG_WARN, "Missing , in match criteria %s in group %s",
665 spec, rGroup->groupName ? rGroup->groupName : "none");
669 attrUse = atois (&s);
670 w = searchRecordKey (reckeys, attrSet, attrUse);
675 for (i = 0; i<32; i++)
680 logf (LOG_WARN, "Missing ) in match criteria %s in group %s",
681 spec, rGroup->groupName ? rGroup->groupName : "none");
686 for (i = 0; i<32; i++)
687 if (matchFlag[i] && w[i])
699 logf (LOG_WARN, "Record in file %s didn't contain match"
700 " fields in (%d,%d)", fname, attrSet, attrUse);
708 const char *spec_src = NULL;
709 const char *s1 = ++s;
710 while (*s1 && *s1 != ' ' && *s1 != '\t')
716 memcpy (special, s, spec_len);
717 special[spec_len] = '\0';
720 if (!strcmp (special, "group"))
721 spec_src = rGroup->groupName;
722 else if (!strcmp (special, "database"))
723 spec_src = rGroup->databaseName;
724 else if (!strcmp (special, "filename"))
726 else if (!strcmp (special, "type"))
727 spec_src = rGroup->recordType;
732 strcpy (dst, spec_src);
733 dst += strlen (spec_src);
736 else if (*s == '\"' || *s == '\'')
738 int stopMarker = *s++;
742 while (*s && *s != stopMarker)
745 tmpString[i++] = *s++;
750 strcpy (dst, tmpString);
751 dst += strlen (tmpString);
755 logf (LOG_WARN, "Syntax error in match criteria %s in group %s",
756 spec, rGroup->groupName ? rGroup->groupName : "none");
763 logf (LOG_WARN, "No match criteria for record %s in group %s",
764 fname, rGroup->groupName ? rGroup->groupName : "none");
770 static int recordExtract (SYSNO *sysno, const char *fname,
771 struct recordGroup *rGroup, int deleteFlag,
772 struct file_read_info *fi, RecType recType,
775 struct recExtractCtrl extractCtrl;
785 extractCtrl.subType = subType;
786 extractCtrl.init = wordInit;
787 extractCtrl.add = addRecordKeyAny;
789 reckeys.buf_used = 0;
790 reckeys.prevAttrUse = -1;
791 reckeys.prevAttrSet = -1;
792 extractCtrl.readf = file_read;
793 r = (*recType->extract)(&extractCtrl);
797 logf (LOG_WARN, "Couldn't extract file %s, code %d", fname, r);
802 /* perform match if sysno not known and if match criteria is specified */
809 if (rGroup->recordId && *rGroup->recordId)
813 matchStr = fileMatchStr (&reckeys, rGroup, fname,
817 rinfo = dict_lookup (matchDict, matchStr);
819 memcpy (sysno, rinfo+1, sizeof(*sysno));
823 logf (LOG_WARN, "Record not inserted");
834 logf (LOG_LOG, "? %s", fname);
837 logf (LOG_LOG, "add %s %s", rGroup->recordType, fname);
838 rec = rec_new (records);
843 dict_insert (matchDict, matchStr, sizeof(*sysno), sysno);
845 flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
851 struct recKeys delkeys;
853 rec = rec_get (records, *sysno);
855 delkeys.buf_used = rec->size[recInfo_delKeys];
856 delkeys.buf = rec->info[recInfo_delKeys];
857 flushRecordKeys (*sysno, 0, &delkeys, rec->info[recInfo_databaseName]);
860 if (!delkeys.buf_used)
862 logf (LOG_WARN, "cannot delete %s: storeKeys false",
867 logf (LOG_LOG, "delete %s %s", rGroup->recordType, fname);
870 dict_delete (matchDict, matchStr);
871 rec_del (records, &rec);
877 if (!delkeys.buf_used)
879 logf (LOG_WARN, "cannot update %s: storeKeys false",
884 logf (LOG_LOG, "update %s %s", rGroup->recordType,
886 flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
891 xfree (rec->info[recInfo_fileType]);
892 rec->info[recInfo_fileType] =
893 rec_strdup (rGroup->recordType, &rec->size[recInfo_fileType]);
895 xfree (rec->info[recInfo_filename]);
896 rec->info[recInfo_filename] =
897 rec_strdup (fname, &rec->size[recInfo_filename]);
899 xfree (rec->info[recInfo_delKeys]);
900 if (reckeys.buf_used > 0 && rGroup->flagStoreKeys == 1)
903 rec->size[recInfo_delKeys] = reckeys.buf_used;
904 rec->info[recInfo_delKeys] = reckeys.buf;
908 rec->info[recInfo_delKeys] = xmalloc (reckeys.buf_used);
909 rec->size[recInfo_delKeys] = reckeys.buf_used;
910 memcpy (rec->info[recInfo_delKeys], reckeys.buf,
911 rec->size[recInfo_delKeys]);
916 rec->info[recInfo_delKeys] = NULL;
917 rec->size[recInfo_delKeys] = 0;
920 xfree (rec->info[recInfo_storeData]);
921 if (rGroup->flagStoreData == 1)
923 rec->size[recInfo_storeData] = fi->file_noread;
924 rec->info[recInfo_storeData] = xmalloc (fi->file_noread);
925 #if FILE_READ_BUFSIZE
926 if (fi->file_noread < FILE_READ_BUFSIZE)
927 memcpy (rec->info[recInfo_storeData], fi->file_buf,
932 if (lseek (fi->fd, 0L, SEEK_SET) < 0)
934 logf (LOG_ERRNO|LOG_FATAL, "seek to 0 in %s", fname);
937 if (read (fi->fd, rec->info[recInfo_storeData], fi->file_noread)
940 logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
941 fi->file_noread, fname);
948 rec->info[recInfo_storeData] = NULL;
949 rec->size[recInfo_storeData] = 0;
951 xfree (rec->info[recInfo_databaseName]);
952 rec->info[recInfo_databaseName] =
953 rec_strdup (rGroup->databaseName, &rec->size[recInfo_databaseName]);
955 rec_put (records, &rec);
959 int fileExtract (SYSNO *sysno, const char *fname,
960 const struct recordGroup *rGroupP, int deleteFlag)
968 struct recordGroup rGroupM;
969 struct recordGroup *rGroup = &rGroupM;
970 struct file_read_info *fi;
972 memcpy (rGroup, rGroupP, sizeof(*rGroupP));
974 if (!rGroup->groupName || !*rGroup->groupName)
977 sprintf (gprefix, "%s.", rGroup->groupName);
979 logf (LOG_DEBUG, "fileExtract %s", fname);
981 /* determine file extension */
982 for (i = strlen(fname); --i >= 0; )
988 else if (fname[i] == '.')
990 strcpy (ext, fname+i+1);
993 /* determine file type - depending on extension */
994 if (!rGroup->recordType)
996 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
997 if (!(rGroup->recordType = res_get (common_resource, ext_res)))
999 sprintf (ext_res, "%srecordType", gprefix);
1000 if (!(rGroup->recordType = res_get (common_resource, ext_res)))
1002 logf (LOG_LOG, "? %s", fname);
1007 if (!rGroup->recordType)
1009 logf (LOG_LOG, "? record %s", fname);
1012 if (!(recType = recType_byName (rGroup->recordType, subType)))
1014 logf (LOG_WARN, "No such record type: %s", rGroup->recordType);
1018 /* determine match criteria */
1019 if (!rGroup->recordId)
1021 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
1022 rGroup->recordId = res_get (common_resource, ext_res);
1025 /* determine database name */
1026 if (!rGroup->databaseName)
1028 sprintf (ext_res, "%sdatabase.%s", gprefix, ext);
1029 if (!(rGroup->databaseName = res_get (common_resource, ext_res)))
1031 sprintf (ext_res, "%sdatabase", gprefix);
1032 rGroup->databaseName = res_get (common_resource, ext_res);
1035 if (!rGroup->databaseName)
1036 rGroup->databaseName = "Default";
1038 if (rGroup->flagStoreData == -1)
1041 sprintf (ext_res, "%sstoreData.%s", gprefix, ext);
1042 if (!(sval = res_get (common_resource, ext_res)))
1044 sprintf (ext_res, "%sstoreData", gprefix);
1045 sval = res_get (common_resource, ext_res);
1048 rGroup->flagStoreData = atoi (sval);
1050 if (rGroup->flagStoreData == -1)
1051 rGroup->flagStoreData = 0;
1053 if (rGroup->flagStoreKeys == -1)
1057 sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
1058 if (!(sval = res_get (common_resource, ext_res)))
1060 sprintf (ext_res, "%sstoreKeys", gprefix);
1061 sval = res_get (common_resource, ext_res);
1064 rGroup->flagStoreKeys = atoi (sval);
1066 if (rGroup->flagStoreKeys == -1)
1067 rGroup->flagStoreKeys = 0;
1069 if (sysno && deleteFlag)
1073 if ((fd = open (fname, O_RDONLY)) == -1)
1075 logf (LOG_WARN|LOG_ERRNO, "open %s", fname);
1079 fi = file_read_start (fd);
1080 recordExtract (sysno, fname, rGroup, deleteFlag, fi, recType, subType);
1081 file_read_stop (fi);