2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.47 1996-01-17 14:57:48 adam
8 * Prototype changed for reader functions in extract/retrieve. File
9 * is identified by 'void *' instead of 'int.
11 * Revision 1.46 1995/12/15 14:57:16 adam
14 * Revision 1.45 1995/12/15 12:37:41 adam
15 * In addRecordKeyAny: Writes key only when attrSet != -1.
17 * Revision 1.44 1995/12/12 16:00:54 adam
18 * System call sync(2) used after update/commit.
19 * Locking (based on fcntl) uses F_EXLCK and F_SHLCK instead of F_WRLCK
22 * Revision 1.43 1995/12/11 09:12:46 adam
23 * The rec_get function returns NULL if record doesn't exist - will
24 * happen in the server if the result set records have been deleted since
25 * the creation of the set (i.e. the search).
26 * The server saves a result temporarily if it is 'volatile', i.e. the
27 * set is register dependent.
29 * Revision 1.42 1995/12/07 17:38:46 adam
30 * Work locking mechanisms for concurrent updates/commit.
32 * Revision 1.41 1995/12/06 16:06:42 adam
33 * Better diagnostics. Work on 'real' dictionary deletion.
35 * Revision 1.40 1995/12/05 16:57:40 adam
36 * More work on regular patterns.
38 * Revision 1.39 1995/12/05 13:20:18 adam
39 * Bug fix: file_read sometimes returned early EOF.
41 * Revision 1.38 1995/12/04 17:59:21 adam
42 * More work on regular expression conversion.
44 * Revision 1.37 1995/12/04 14:22:27 adam
45 * Extra arg to recType_byName.
46 * Started work on new regular expression parsed input to
49 * Revision 1.36 1995/11/30 08:34:29 adam
50 * Started work on commit facility.
51 * Changed a few malloc/free to xmalloc/xfree.
53 * Revision 1.35 1995/11/28 14:26:21 adam
54 * Bug fix: recordId with constant wasn't right.
55 * Bug fix: recordId dictionary entry wasn't deleted when needed.
57 * Revision 1.34 1995/11/28 09:09:38 adam
58 * Zebra config renamed.
59 * Use setting 'recordId' to identify record now.
60 * Bug fix in recindex.c: rec_release_blocks was invokeded even
61 * though the blocks were already released.
62 * File traversal properly deletes records when needed.
64 * Revision 1.33 1995/11/27 09:56:20 adam
65 * Record info elements better enumerated. Internal store of records.
67 * Revision 1.32 1995/11/25 10:24:05 adam
68 * More record fields - they are enumerated now.
69 * New options: flagStoreData flagStoreKey.
71 * Revision 1.31 1995/11/24 11:31:35 adam
72 * Commands add & del read filenames from stdin if source directory is
74 * Match criteria supports 'constant' strings.
76 * Revision 1.30 1995/11/22 17:19:16 adam
77 * Record management uses the bfile system.
79 * Revision 1.29 1995/11/21 15:01:14 adam
80 * New general match criteria implemented.
81 * New feature: document groups.
83 * Revision 1.28 1995/11/21 09:20:30 adam
84 * Yet more work on record match.
86 * Revision 1.27 1995/11/20 16:59:45 adam
87 * New update method: the 'old' keys are saved for each records.
89 * Revision 1.26 1995/11/20 11:56:24 adam
90 * Work on new traversal.
92 * Revision 1.25 1995/11/16 15:34:54 adam
93 * Uses new record management system in both indexer and server.
95 * Revision 1.24 1995/11/15 19:13:08 adam
96 * Work on record management.
98 * Revision 1.23 1995/10/27 14:00:10 adam
99 * Implemented detection of database availability.
101 * Revision 1.22 1995/10/17 18:02:07 adam
102 * New feature: databases. Implemented as prefix to words in dictionary.
104 * Revision 1.21 1995/10/10 12:24:38 adam
105 * Temporary sort files are compressed.
107 * Revision 1.20 1995/10/06 13:52:05 adam
108 * Bug fixes. Handler may abort further scanning.
110 * Revision 1.19 1995/10/04 12:55:16 adam
111 * Bug fix in ranked search. Use=Any keys inserted.
113 * Revision 1.18 1995/10/04 09:37:08 quinn
116 * Revision 1.17 1995/10/03 14:28:57 adam
117 * Buffered read in extract works.
119 * Revision 1.16 1995/10/03 14:28:45 adam
120 * Work on more effecient read handler in extract.
122 * Revision 1.15 1995/10/02 15:42:53 adam
123 * Extract uses file descriptors instead of FILE pointers.
125 * Revision 1.14 1995/10/02 15:29:13 adam
126 * More logging in file_extract.
128 * Revision 1.13 1995/09/29 14:01:39 adam
131 * Revision 1.12 1995/09/28 14:22:56 adam
132 * Sort uses smaller temporary files.
134 * Revision 1.11 1995/09/28 12:10:31 adam
135 * Bug fixes. Field prefix used in queries.
137 * Revision 1.10 1995/09/28 09:19:41 adam
138 * xfree/xmalloc used everywhere.
139 * Extract/retrieve method seems to work for text records.
141 * Revision 1.9 1995/09/27 12:22:28 adam
142 * More work on extract in record control.
143 * Field name is not in isam keys but in prefix in dictionary words.
145 * Revision 1.8 1995/09/14 07:48:22 adam
146 * Record control management.
148 * Revision 1.7 1995/09/11 13:09:32 adam
149 * More work on relevance feedback.
151 * Revision 1.6 1995/09/08 14:52:27 adam
152 * Minor changes. Dictionary is lower case now.
154 * Revision 1.5 1995/09/06 16:11:16 adam
155 * Option: only one word key per file.
157 * Revision 1.4 1995/09/05 15:28:39 adam
158 * More work on search engine.
160 * Revision 1.3 1995/09/04 12:33:41 adam
161 * Various cleanup. YAZ util used instead.
163 * Revision 1.2 1995/09/04 09:10:34 adam
164 * More work on index add/del/update.
165 * Merge sort implemented.
166 * Initial work on z39 server.
168 * Revision 1.1 1995/09/01 14:06:35 adam
169 * Split of work into more files.
178 #include <alexutil.h>
182 #include "recindex.h"
184 static Dict matchDict;
186 static Records records = NULL;
188 static char **key_buf;
189 static size_t ptr_top;
191 static size_t key_buf_used;
192 static int key_file_no;
194 static int records_inserted = 0;
195 static int records_updated = 0;
196 static int records_deleted = 0;
198 void key_open (int mem)
202 key_buf = xmalloc (mem);
203 ptr_top = mem/sizeof(char*);
209 if (!(matchDict = dict_open (GMATCH_DICT, 50, 1)))
211 logf (LOG_FATAL, "dict_open fail of %s", GMATCH_DICT);
215 records = rec_open (1);
224 void encode_key_init (struct encode_info *i)
230 char *encode_key_int (int d, char *bp)
239 else if (d <= 4194303)
241 *bp++ = 128 + (d>>16);
242 *bp++ = (d>>8) & 255;
247 *bp++ = 192 + (d>>24);
248 *bp++ = (d>>16) & 255;
249 *bp++ = (d>>8) & 255;
255 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
260 while ((*bp++ = *k++))
262 memcpy (&key, k+1, sizeof(struct it_key));
263 bp = encode_key_int ( (key.sysno - i->sysno) * 2 + *k, bp);
264 if (i->sysno != key.sysno)
266 i->sysno = key.sysno;
269 bp = encode_key_int (key.seqno - i->seqno, bp);
270 i->seqno = key.seqno;
271 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
273 logf (LOG_FATAL|LOG_ERRNO, "fwrite");
278 void key_flush (void)
283 struct encode_info encode_info;
289 logf (LOG_LOG, "sorting section %d", key_file_no);
290 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_qsort_compare);
291 sprintf (out_fname, TEMP_FNAME, key_file_no);
293 if (!(outf = fopen (out_fname, "w")))
295 logf (LOG_FATAL|LOG_ERRNO, "fopen (4) %s", out_fname);
298 logf (LOG_LOG, "writing section %d", key_file_no);
299 prevcp = cp = key_buf[ptr_top-ptr_i];
301 encode_key_init (&encode_info);
302 encode_key_write (cp, &encode_info, outf);
305 cp = key_buf[ptr_top-ptr_i];
306 if (strcmp (cp, prevcp))
308 encode_key_init (&encode_info);
309 encode_key_write (cp, &encode_info, outf);
313 encode_key_write (cp + strlen(cp), &encode_info, outf);
317 logf (LOG_FATAL|LOG_ERRNO, "fclose %s", out_fname);
320 logf (LOG_LOG, "finished section %d", key_file_no);
329 rec_close (&records);
330 dict_close (matchDict);
332 logf (LOG_LOG, "Records inserted %6d", records_inserted);
333 logf (LOG_LOG, "Records updated %6d", records_updated);
334 logf (LOG_LOG, "Records deleted %6d", records_deleted);
338 static void wordInit (RecWord *p)
342 p->which = Word_String;
351 static void addRecordKey (const RecWord *p)
358 if (reckeys.buf_used+1024 > reckeys.buf_max)
362 b = xmalloc (reckeys.buf_max += 65000);
363 if (reckeys.buf_used > 0)
364 memcpy (b, reckeys.buf, reckeys.buf_used);
368 dst = reckeys.buf + reckeys.buf_used;
372 attrSet = p->attrSet;
373 memcpy (dst, &attrSet, sizeof(attrSet));
374 dst += sizeof(attrSet);
376 attrUse = p->attrUse;
377 memcpy (dst, &attrUse, sizeof(attrUse));
378 dst += sizeof(attrUse);
380 for (i = 0; p->u.string[i]; i++)
381 *dst++ = p->u.string[i];
384 memcpy (dst, &p->seqno, sizeof(p->seqno));
385 dst += sizeof(p->seqno);
391 reckeys.buf_used = dst - reckeys.buf;
394 static void flushRecordKeys (SYSNO sysno, int cmd, struct recKeys *reckeys,
395 const char *databaseName)
398 while (off < reckeys->buf_used)
400 const char *src = reckeys->buf + off;
405 memcpy (&attrSet, src, sizeof(attrSet));
406 src += sizeof(attrSet);
408 memcpy (&attrUse, src, sizeof(attrUse));
409 src += sizeof(attrUse);
411 if (key_buf_used + 1024 > (ptr_top-ptr_i)*sizeof(char*))
414 key_buf[ptr_top-ptr_i] = (char*)key_buf + key_buf_used;
415 key_buf_used += index_word_prefix ((char*)key_buf + key_buf_used,
416 attrSet, attrUse, databaseName);
418 ((char*)key_buf) [key_buf_used++] = index_char_cvt (*src++);
420 ((char*)key_buf) [key_buf_used++] = '\0';
422 ((char*) key_buf)[key_buf_used++] = cmd;
424 memcpy (&key.seqno, src, sizeof(key.seqno));
425 src += sizeof(key.seqno);
427 memcpy ((char*)key_buf + key_buf_used, &key, sizeof(key));
428 key_buf_used += sizeof(key);
429 off = src - reckeys->buf;
431 assert (off == reckeys->buf_used);
434 static const char **searchRecordKey (struct recKeys *reckeys,
435 int attrSetS, int attrUseS)
437 static const char *ws[32];
442 for (i = 0; i<32; i++)
445 while (off < reckeys->buf_used)
447 const char *src = reckeys->buf + off;
453 memcpy (&attrSet, src, sizeof(attrSet));
454 src += sizeof(attrSet);
456 memcpy (&attrUse, src, sizeof(attrUse));
457 src += sizeof(attrUse);
463 memcpy (&seqno, src, sizeof(seqno));
464 src += sizeof(seqno);
467 logf (LOG_LOG, "(%d,%d) %d %s", attrSet, attrUse, seqno, wstart);
469 if (attrUseS == attrUse && attrSetS == attrSet)
476 woff = seqno - startSeq;
477 if (woff >= 0 && woff < 31)
481 off = src - reckeys->buf;
483 assert (off == reckeys->buf_used);
487 static void addRecordKeyAny (const RecWord *p)
489 if (p->attrSet != 1 || p->attrUse != 1016)
493 memcpy (&w, p, sizeof(w));
498 if (p->attrSet != -1)
502 #define FILE_READ_BUFSIZE 4096
503 struct file_read_info {
506 #if FILE_READ_BUFSIZE
513 static struct file_read_info *file_read_start (int fd)
515 struct file_read_info *fi = xmalloc (sizeof(*fi));
519 #if FILE_READ_BUFSIZE
521 fi->file_buf = xmalloc (FILE_READ_BUFSIZE);
522 fi->file_bufsize = read (fd, fi->file_buf, FILE_READ_BUFSIZE);
527 static void file_read_stop (struct file_read_info *fi)
530 #if FILE_READ_BUFSIZE
531 xfree (fi->file_buf);
537 static int file_read (void *handle, char *buf, size_t count)
539 struct file_read_info *p = handle;
541 #if FILE_READ_BUFSIZE
542 int l = p->file_bufsize - p->file_offset;
548 memcpy (buf, p->file_buf + p->file_offset, l);
550 if (count > FILE_READ_BUFSIZE)
552 if ((r = read (fd, buf + l, count)) == -1)
554 logf (LOG_FATAL|LOG_ERRNO, "read");
559 p->file_noread += l+r;
562 p->file_bufsize = r = read (fd, p->file_buf, FILE_READ_BUFSIZE);
565 logf (LOG_FATAL|LOG_ERRNO, "read");
571 memcpy (buf + l, p->file_buf, r);
572 p->file_noread += l+r;
577 p->file_offset = count;
578 memcpy (buf + l, p->file_buf, count - l);
579 p->file_noread += count;
583 memcpy (buf, p->file_buf + p->file_offset, count);
584 p->file_offset += count;
585 p->file_noread += count;
589 r = read (fd, buf, count);
596 static int atois (const char **s)
599 while ( (c=**s) >= '0' && c <= '9')
601 val = val*10 + c - '0';
607 static char *fileMatchStr (struct recKeys *reckeys, struct recordGroup *rGroup,
611 static char dstBuf[2048];
613 const char *s = spec;
614 static const char **w;
619 while (*s == ' ' || *s == '\t')
626 int attrSet, attrUse;
630 attrSet = atois (&s);
633 logf (LOG_WARN, "Missing , in match criteria %s in group %s",
634 spec, rGroup->groupName ? rGroup->groupName : "none");
638 attrUse = atois (&s);
639 w = searchRecordKey (reckeys, attrSet, attrUse);
644 for (i = 0; i<32; i++)
649 logf (LOG_WARN, "Missing ) in match criteria %s in group %s",
650 spec, rGroup->groupName ? rGroup->groupName : "none");
655 for (i = 0; i<32; i++)
656 if (matchFlag[i] && w[i])
668 logf (LOG_WARN, "Record in file %s didn't contain match"
669 " fields in (%d,%d)", fname, attrSet, attrUse);
677 const char *spec_src = NULL;
678 const char *s1 = ++s;
679 while (*s1 && *s1 != ' ' && *s1 != '\t')
685 memcpy (special, s, spec_len);
686 special[spec_len] = '\0';
689 if (!strcmp (special, "group"))
690 spec_src = rGroup->groupName;
691 else if (!strcmp (special, "database"))
692 spec_src = rGroup->databaseName;
693 else if (!strcmp (special, "filename"))
695 else if (!strcmp (special, "type"))
696 spec_src = rGroup->recordType;
701 strcpy (dst, spec_src);
702 dst += strlen (spec_src);
705 else if (*s == '\"' || *s == '\'')
707 int stopMarker = *s++;
711 while (*s && *s != stopMarker)
714 tmpString[i++] = *s++;
719 strcpy (dst, tmpString);
720 dst += strlen (tmpString);
724 logf (LOG_WARN, "Syntax error in match criteria %s in group %s",
725 spec, rGroup->groupName ? rGroup->groupName : "none");
732 logf (LOG_WARN, "No match criteria for record %s in group %s",
733 fname, rGroup->groupName ? rGroup->groupName : "none");
739 static int recordExtract (SYSNO *sysno, const char *fname,
740 struct recordGroup *rGroup, int deleteFlag,
741 struct file_read_info *fi, RecType recType,
744 struct recExtractCtrl extractCtrl;
754 extractCtrl.subType = subType;
755 extractCtrl.init = wordInit;
756 extractCtrl.add = addRecordKeyAny;
758 reckeys.buf_used = 0;
759 extractCtrl.readf = file_read;
760 r = (*recType->extract)(&extractCtrl);
764 logf (LOG_WARN, "Couldn't extract file %s, code %d", fname, r);
769 /* perform match if sysno not known and if match criteria is specified */
776 if (rGroup->recordId && *rGroup->recordId)
780 matchStr = fileMatchStr (&reckeys, rGroup, fname,
784 rinfo = dict_lookup (matchDict, matchStr);
786 memcpy (sysno, rinfo+1, sizeof(*sysno));
790 logf (LOG_WARN, "Record not inserted");
801 logf (LOG_LOG, "? %s", fname);
804 logf (LOG_LOG, "add %s %s", rGroup->recordType, fname);
805 rec = rec_new (records);
810 dict_insert (matchDict, matchStr, sizeof(*sysno), sysno);
812 flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
818 struct recKeys delkeys;
820 rec = rec_get (records, *sysno);
822 delkeys.buf_used = rec->size[recInfo_delKeys];
823 delkeys.buf = rec->info[recInfo_delKeys];
824 flushRecordKeys (*sysno, 0, &delkeys, rec->info[recInfo_databaseName]);
827 if (!delkeys.buf_used)
829 logf (LOG_WARN, "cannot delete %s: storeKeys false",
834 logf (LOG_LOG, "delete %s %s", rGroup->recordType, fname);
837 dict_delete (matchDict, matchStr);
838 rec_del (records, &rec);
844 if (!delkeys.buf_used)
846 logf (LOG_WARN, "cannot update %s: storeKeys false",
851 logf (LOG_LOG, "update %s %s", rGroup->recordType,
853 flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
858 xfree (rec->info[recInfo_fileType]);
859 rec->info[recInfo_fileType] =
860 rec_strdup (rGroup->recordType, &rec->size[recInfo_fileType]);
862 xfree (rec->info[recInfo_filename]);
863 rec->info[recInfo_filename] =
864 rec_strdup (fname, &rec->size[recInfo_filename]);
866 xfree (rec->info[recInfo_delKeys]);
867 if (reckeys.buf_used > 0 && rGroup->flagStoreKeys == 1)
869 rec->info[recInfo_delKeys] = xmalloc (reckeys.buf_used);
870 rec->size[recInfo_delKeys] = reckeys.buf_used;
871 memcpy (rec->info[recInfo_delKeys], reckeys.buf,
872 rec->size[recInfo_delKeys]);
876 rec->info[recInfo_delKeys] = NULL;
877 rec->size[recInfo_delKeys] = 0;
880 xfree (rec->info[recInfo_storeData]);
881 if (rGroup->flagStoreData == 1)
883 rec->size[recInfo_storeData] = fi->file_noread;
884 rec->info[recInfo_storeData] = xmalloc (fi->file_noread);
885 #if FILE_READ_BUFSIZE
886 if (fi->file_noread < FILE_READ_BUFSIZE)
887 memcpy (rec->info[recInfo_storeData], fi->file_buf,
892 if (lseek (fi->fd, 0L, SEEK_SET) < 0)
894 logf (LOG_ERRNO|LOG_FATAL, "seek to 0 in %s", fname);
897 if (read (fi->fd, rec->info[recInfo_storeData], fi->file_noread)
900 logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
901 fi->file_noread, fname);
908 rec->info[recInfo_storeData] = NULL;
909 rec->size[recInfo_storeData] = 0;
911 xfree (rec->info[recInfo_databaseName]);
912 rec->info[recInfo_databaseName] =
913 rec_strdup (rGroup->databaseName, &rec->size[recInfo_databaseName]);
915 rec_put (records, &rec);
919 int fileExtract (SYSNO *sysno, const char *fname,
920 const struct recordGroup *rGroupP, int deleteFlag)
928 struct recordGroup rGroupM;
929 struct recordGroup *rGroup = &rGroupM;
930 struct file_read_info *fi;
932 memcpy (rGroup, rGroupP, sizeof(*rGroupP));
934 if (!rGroup->groupName || !*rGroup->groupName)
937 sprintf (gprefix, "%s.", rGroup->groupName);
939 logf (LOG_DEBUG, "fileExtract %s", fname);
941 /* determine file extension */
942 for (i = strlen(fname); --i >= 0; )
948 else if (fname[i] == '.')
950 strcpy (ext, fname+i+1);
953 /* determine file type - depending on extension */
954 if (!rGroup->recordType)
956 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
957 if (!(rGroup->recordType = res_get (common_resource, ext_res)))
959 sprintf (ext_res, "%srecordType", gprefix);
960 if (!(rGroup->recordType = res_get (common_resource, ext_res)))
962 logf (LOG_LOG, "? %s", fname);
967 if (!rGroup->recordType)
969 logf (LOG_LOG, "? record %s", fname);
972 if (!(recType = recType_byName (rGroup->recordType, subType)))
974 logf (LOG_WARN, "No such record type: %s", rGroup->recordType);
978 /* determine match criteria */
979 if (!rGroup->recordId)
981 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
982 rGroup->recordId = res_get (common_resource, ext_res);
985 /* determine database name */
986 if (!rGroup->databaseName)
988 sprintf (ext_res, "%sdatabase.%s", gprefix, ext);
989 if (!(rGroup->databaseName = res_get (common_resource, ext_res)))
991 sprintf (ext_res, "%sdatabase", gprefix);
992 rGroup->databaseName = res_get (common_resource, ext_res);
995 if (!rGroup->databaseName)
996 rGroup->databaseName = "Default";
998 if (rGroup->flagStoreData == -1)
1001 sprintf (ext_res, "%sstoreData.%s", gprefix, ext);
1002 if (!(sval = res_get (common_resource, ext_res)))
1004 sprintf (ext_res, "%sstoreData", gprefix);
1005 sval = res_get (common_resource, ext_res);
1008 rGroup->flagStoreData = atoi (sval);
1010 if (rGroup->flagStoreData == -1)
1011 rGroup->flagStoreData = 0;
1013 if (rGroup->flagStoreKeys == -1)
1017 sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
1018 if (!(sval = res_get (common_resource, ext_res)))
1020 sprintf (ext_res, "%sstoreKeys", gprefix);
1021 sval = res_get (common_resource, ext_res);
1024 rGroup->flagStoreKeys = atoi (sval);
1026 if (rGroup->flagStoreKeys == -1)
1027 rGroup->flagStoreKeys = 0;
1029 if (sysno && deleteFlag)
1033 if ((fd = open (fname, O_RDONLY)) == -1)
1035 logf (LOG_WARN|LOG_ERRNO, "open %s", fname);
1039 fi = file_read_start (fd);
1040 recordExtract (sysno, fname, rGroup, deleteFlag, fi, recType, subType);
1041 file_read_stop (fi);