2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.45 1995-12-15 12:37:41 adam
8 * In addRecordKeyAny: Writes key only when attrSet != -1.
10 * Revision 1.44 1995/12/12 16:00:54 adam
11 * System call sync(2) used after update/commit.
12 * Locking (based on fcntl) uses F_EXLCK and F_SHLCK instead of F_WRLCK
15 * Revision 1.43 1995/12/11 09:12:46 adam
16 * The rec_get function returns NULL if record doesn't exist - will
17 * happen in the server if the result set records have been deleted since
18 * the creation of the set (i.e. the search).
19 * The server saves a result temporarily if it is 'volatile', i.e. the
20 * set is register dependent.
22 * Revision 1.42 1995/12/07 17:38:46 adam
23 * Work locking mechanisms for concurrent updates/commit.
25 * Revision 1.41 1995/12/06 16:06:42 adam
26 * Better diagnostics. Work on 'real' dictionary deletion.
28 * Revision 1.40 1995/12/05 16:57:40 adam
29 * More work on regular patterns.
31 * Revision 1.39 1995/12/05 13:20:18 adam
32 * Bug fix: file_read sometimes returned early EOF.
34 * Revision 1.38 1995/12/04 17:59:21 adam
35 * More work on regular expression conversion.
37 * Revision 1.37 1995/12/04 14:22:27 adam
38 * Extra arg to recType_byName.
39 * Started work on new regular expression parsed input to
42 * Revision 1.36 1995/11/30 08:34:29 adam
43 * Started work on commit facility.
44 * Changed a few malloc/free to xmalloc/xfree.
46 * Revision 1.35 1995/11/28 14:26:21 adam
47 * Bug fix: recordId with constant wasn't right.
48 * Bug fix: recordId dictionary entry wasn't deleted when needed.
50 * Revision 1.34 1995/11/28 09:09:38 adam
51 * Zebra config renamed.
52 * Use setting 'recordId' to identify record now.
53 * Bug fix in recindex.c: rec_release_blocks was invokeded even
54 * though the blocks were already released.
55 * File traversal properly deletes records when needed.
57 * Revision 1.33 1995/11/27 09:56:20 adam
58 * Record info elements better enumerated. Internal store of records.
60 * Revision 1.32 1995/11/25 10:24:05 adam
61 * More record fields - they are enumerated now.
62 * New options: flagStoreData flagStoreKey.
64 * Revision 1.31 1995/11/24 11:31:35 adam
65 * Commands add & del read filenames from stdin if source directory is
67 * Match criteria supports 'constant' strings.
69 * Revision 1.30 1995/11/22 17:19:16 adam
70 * Record management uses the bfile system.
72 * Revision 1.29 1995/11/21 15:01:14 adam
73 * New general match criteria implemented.
74 * New feature: document groups.
76 * Revision 1.28 1995/11/21 09:20:30 adam
77 * Yet more work on record match.
79 * Revision 1.27 1995/11/20 16:59:45 adam
80 * New update method: the 'old' keys are saved for each records.
82 * Revision 1.26 1995/11/20 11:56:24 adam
83 * Work on new traversal.
85 * Revision 1.25 1995/11/16 15:34:54 adam
86 * Uses new record management system in both indexer and server.
88 * Revision 1.24 1995/11/15 19:13:08 adam
89 * Work on record management.
91 * Revision 1.23 1995/10/27 14:00:10 adam
92 * Implemented detection of database availability.
94 * Revision 1.22 1995/10/17 18:02:07 adam
95 * New feature: databases. Implemented as prefix to words in dictionary.
97 * Revision 1.21 1995/10/10 12:24:38 adam
98 * Temporary sort files are compressed.
100 * Revision 1.20 1995/10/06 13:52:05 adam
101 * Bug fixes. Handler may abort further scanning.
103 * Revision 1.19 1995/10/04 12:55:16 adam
104 * Bug fix in ranked search. Use=Any keys inserted.
106 * Revision 1.18 1995/10/04 09:37:08 quinn
109 * Revision 1.17 1995/10/03 14:28:57 adam
110 * Buffered read in extract works.
112 * Revision 1.16 1995/10/03 14:28:45 adam
113 * Work on more effecient read handler in extract.
115 * Revision 1.15 1995/10/02 15:42:53 adam
116 * Extract uses file descriptors instead of FILE pointers.
118 * Revision 1.14 1995/10/02 15:29:13 adam
119 * More logging in file_extract.
121 * Revision 1.13 1995/09/29 14:01:39 adam
124 * Revision 1.12 1995/09/28 14:22:56 adam
125 * Sort uses smaller temporary files.
127 * Revision 1.11 1995/09/28 12:10:31 adam
128 * Bug fixes. Field prefix used in queries.
130 * Revision 1.10 1995/09/28 09:19:41 adam
131 * xfree/xmalloc used everywhere.
132 * Extract/retrieve method seems to work for text records.
134 * Revision 1.9 1995/09/27 12:22:28 adam
135 * More work on extract in record control.
136 * Field name is not in isam keys but in prefix in dictionary words.
138 * Revision 1.8 1995/09/14 07:48:22 adam
139 * Record control management.
141 * Revision 1.7 1995/09/11 13:09:32 adam
142 * More work on relevance feedback.
144 * Revision 1.6 1995/09/08 14:52:27 adam
145 * Minor changes. Dictionary is lower case now.
147 * Revision 1.5 1995/09/06 16:11:16 adam
148 * Option: only one word key per file.
150 * Revision 1.4 1995/09/05 15:28:39 adam
151 * More work on search engine.
153 * Revision 1.3 1995/09/04 12:33:41 adam
154 * Various cleanup. YAZ util used instead.
156 * Revision 1.2 1995/09/04 09:10:34 adam
157 * More work on index add/del/update.
158 * Merge sort implemented.
159 * Initial work on z39 server.
161 * Revision 1.1 1995/09/01 14:06:35 adam
162 * Split of work into more files.
171 #include <alexutil.h>
175 #include "recindex.h"
177 static Dict matchDict;
179 static Records records = NULL;
181 static char **key_buf;
182 static size_t ptr_top;
184 static size_t key_buf_used;
185 static int key_file_no;
187 static int records_inserted = 0;
188 static int records_updated = 0;
189 static int records_deleted = 0;
191 void key_open (int mem)
195 key_buf = xmalloc (mem);
196 ptr_top = mem/sizeof(char*);
202 if (!(matchDict = dict_open (GMATCH_DICT, 50, 1)))
204 logf (LOG_FATAL, "dict_open fail of %s", GMATCH_DICT);
208 records = rec_open (1);
217 void encode_key_init (struct encode_info *i)
223 char *encode_key_int (int d, char *bp)
232 else if (d <= 4194303)
234 *bp++ = 128 + (d>>16);
235 *bp++ = (d>>8) & 255;
240 *bp++ = 192 + (d>>24);
241 *bp++ = (d>>16) & 255;
242 *bp++ = (d>>8) & 255;
248 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
253 while ((*bp++ = *k++))
255 memcpy (&key, k+1, sizeof(struct it_key));
256 bp = encode_key_int ( (key.sysno - i->sysno) * 2 + *k, bp);
257 if (i->sysno != key.sysno)
259 i->sysno = key.sysno;
262 bp = encode_key_int (key.seqno - i->seqno, bp);
263 i->seqno = key.seqno;
264 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
266 logf (LOG_FATAL|LOG_ERRNO, "fwrite");
271 void key_flush (void)
276 struct encode_info encode_info;
282 logf (LOG_LOG, "sorting section %d", key_file_no);
283 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_qsort_compare);
284 sprintf (out_fname, TEMP_FNAME, key_file_no);
286 if (!(outf = fopen (out_fname, "w")))
288 logf (LOG_FATAL|LOG_ERRNO, "fopen (4) %s", out_fname);
291 logf (LOG_LOG, "writing section %d", key_file_no);
292 prevcp = cp = key_buf[ptr_top-ptr_i];
294 encode_key_init (&encode_info);
295 encode_key_write (cp, &encode_info, outf);
298 cp = key_buf[ptr_top-ptr_i];
299 if (strcmp (cp, prevcp))
301 encode_key_init (&encode_info);
302 encode_key_write (cp, &encode_info, outf);
306 encode_key_write (cp + strlen(cp), &encode_info, outf);
310 logf (LOG_FATAL|LOG_ERRNO, "fclose %s", out_fname);
313 logf (LOG_LOG, "finished section %d", key_file_no);
322 rec_close (&records);
323 dict_close (matchDict);
325 logf (LOG_LOG, "Records inserted %6d", records_inserted);
326 logf (LOG_LOG, "Records updated %6d", records_updated);
327 logf (LOG_LOG, "Records deleted %6d", records_deleted);
331 static void wordInit (RecWord *p)
335 p->which = Word_String;
344 static void addRecordKey (const RecWord *p)
351 if (reckeys.buf_used+1024 > reckeys.buf_max)
355 b = xmalloc (reckeys.buf_max += 65000);
356 if (reckeys.buf_used > 0)
357 memcpy (b, reckeys.buf, reckeys.buf_used);
361 dst = reckeys.buf + reckeys.buf_used;
365 attrSet = p->attrSet;
366 memcpy (dst, &attrSet, sizeof(attrSet));
367 dst += sizeof(attrSet);
369 attrUse = p->attrUse;
370 memcpy (dst, &attrUse, sizeof(attrUse));
371 dst += sizeof(attrUse);
373 for (i = 0; p->u.string[i]; i++)
374 *dst++ = p->u.string[i];
377 memcpy (dst, &p->seqno, sizeof(p->seqno));
378 dst += sizeof(p->seqno);
384 reckeys.buf_used = dst - reckeys.buf;
387 static void flushRecordKeys (SYSNO sysno, int cmd, struct recKeys *reckeys,
388 const char *databaseName)
391 while (off < reckeys->buf_used)
393 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);
404 if (key_buf_used + 1024 > (ptr_top-ptr_i)*sizeof(char*))
407 key_buf[ptr_top-ptr_i] = (char*)key_buf + key_buf_used;
408 key_buf_used += index_word_prefix ((char*)key_buf + key_buf_used,
409 attrSet, attrUse, databaseName);
411 ((char*)key_buf) [key_buf_used++] = index_char_cvt (*src++);
413 ((char*)key_buf) [key_buf_used++] = '\0';
415 ((char*) key_buf)[key_buf_used++] = cmd;
417 memcpy (&key.seqno, src, sizeof(key.seqno));
418 src += sizeof(key.seqno);
420 memcpy ((char*)key_buf + key_buf_used, &key, sizeof(key));
421 key_buf_used += sizeof(key);
422 off = src - reckeys->buf;
424 assert (off == reckeys->buf_used);
427 static const char **searchRecordKey (struct recKeys *reckeys,
428 int attrSetS, int attrUseS)
430 static const char *ws[32];
435 for (i = 0; i<32; i++)
438 while (off < reckeys->buf_used)
440 const char *src = reckeys->buf + off;
446 memcpy (&attrSet, src, sizeof(attrSet));
447 src += sizeof(attrSet);
449 memcpy (&attrUse, src, sizeof(attrUse));
450 src += sizeof(attrUse);
456 memcpy (&seqno, src, sizeof(seqno));
457 src += sizeof(seqno);
460 logf (LOG_LOG, "(%d,%d) %d %s", attrSet, attrUse, seqno, wstart);
462 if (attrUseS == attrUse && attrSetS == attrSet)
469 woff = seqno - startSeq;
470 if (woff >= 0 && woff < 31)
474 off = src - reckeys->buf;
476 assert (off == reckeys->buf_used);
480 static void addRecordKeyAny (const RecWord *p)
482 if (p->attrSet != 1 || p->attrUse != 1016)
486 memcpy (&w, p, sizeof(w));
491 else if (p->attrSet != -1)
496 #define FILE_READ_BUFSIZE 4096
498 static int file_noread;
499 #if FILE_READ_BUFSIZE
500 static char *file_buf;
501 static int file_offset;
502 static int file_bufsize;
505 static void file_read_start (int fd)
508 #if FILE_READ_BUFSIZE
510 file_buf = xmalloc (FILE_READ_BUFSIZE);
511 file_bufsize = read (fd, file_buf, FILE_READ_BUFSIZE);
515 static void file_read_stop (int fd)
517 #if FILE_READ_BUFSIZE
523 static int file_read (int fd, char *buf, size_t count)
525 #if FILE_READ_BUFSIZE
526 int l = file_bufsize - file_offset;
532 memcpy (buf, file_buf + file_offset, l);
534 if (count > FILE_READ_BUFSIZE)
536 if ((r = read (fd, buf + l, count)) == -1)
538 logf (LOG_FATAL|LOG_ERRNO, "read");
546 file_bufsize = r = read (fd, file_buf, FILE_READ_BUFSIZE);
549 logf (LOG_FATAL|LOG_ERRNO, "read");
555 memcpy (buf + l, file_buf, r);
562 memcpy (buf + l, file_buf, count - l);
563 file_noread += count;
567 memcpy (buf, file_buf + file_offset, count);
568 file_offset += count;
569 file_noread += count;
573 r = read (fd, buf, count);
580 static int atois (const char **s)
583 while ( (c=**s) >= '0' && c <= '9')
585 val = val*10 + c - '0';
591 static char *fileMatchStr (struct recKeys *reckeys, struct recordGroup *rGroup,
595 static char dstBuf[2048];
597 const char *s = spec;
598 static const char **w;
603 while (*s == ' ' || *s == '\t')
610 int attrSet, attrUse;
614 attrSet = atois (&s);
617 logf (LOG_WARN, "Missing , in match criteria %s in group %s",
618 spec, rGroup->groupName ? rGroup->groupName : "none");
622 attrUse = atois (&s);
623 w = searchRecordKey (reckeys, attrSet, attrUse);
628 for (i = 0; i<32; i++)
633 logf (LOG_WARN, "Missing ) in match criteria %s in group %s",
634 spec, rGroup->groupName ? rGroup->groupName : "none");
639 for (i = 0; i<32; i++)
640 if (matchFlag[i] && w[i])
652 logf (LOG_WARN, "Record in file %s didn't contain match"
653 " fields in (%d,%d)", fname, attrSet, attrUse);
661 const char *spec_src = NULL;
662 const char *s1 = ++s;
663 while (*s1 && *s1 != ' ' && *s1 != '\t')
669 memcpy (special, s, spec_len);
670 special[spec_len] = '\0';
673 if (!strcmp (special, "group"))
674 spec_src = rGroup->groupName;
675 else if (!strcmp (special, "database"))
676 spec_src = rGroup->databaseName;
677 else if (!strcmp (special, "filename"))
679 else if (!strcmp (special, "type"))
680 spec_src = rGroup->recordType;
685 strcpy (dst, spec_src);
686 dst += strlen (spec_src);
689 else if (*s == '\"' || *s == '\'')
691 int stopMarker = *s++;
695 while (*s && *s != stopMarker)
698 tmpString[i++] = *s++;
703 strcpy (dst, tmpString);
704 dst += strlen (tmpString);
708 logf (LOG_WARN, "Syntax error in match criteria %s in group %s",
709 spec, rGroup->groupName ? rGroup->groupName : "none");
716 logf (LOG_WARN, "No match criteria for record %s in group %s",
717 fname, rGroup->groupName ? rGroup->groupName : "none");
723 static int recordExtract (SYSNO *sysno, const char *fname,
724 struct recordGroup *rGroup, int deleteFlag,
725 int fd, RecType recType, char *subType)
727 struct recExtractCtrl extractCtrl;
737 extractCtrl.subType = subType;
738 extractCtrl.init = wordInit;
739 extractCtrl.add = addRecordKeyAny;
741 reckeys.buf_used = 0;
742 extractCtrl.readf = file_read;
743 r = (*recType->extract)(&extractCtrl);
747 logf (LOG_WARN, "Couldn't extract file %s, code %d", fname, r);
752 /* perform match if sysno not known and if match criteria is specified */
759 if (rGroup->recordId && *rGroup->recordId)
763 matchStr = fileMatchStr (&reckeys, rGroup, fname,
767 rinfo = dict_lookup (matchDict, matchStr);
769 memcpy (sysno, rinfo+1, sizeof(*sysno));
773 logf (LOG_WARN, "Record not inserted");
784 logf (LOG_LOG, "? %s", fname);
787 logf (LOG_LOG, "add %s %s", rGroup->recordType, fname);
788 rec = rec_new (records);
793 dict_insert (matchDict, matchStr, sizeof(*sysno), sysno);
795 flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
801 struct recKeys delkeys;
803 rec = rec_get (records, *sysno);
805 delkeys.buf_used = rec->size[recInfo_delKeys];
806 delkeys.buf = rec->info[recInfo_delKeys];
807 flushRecordKeys (*sysno, 0, &delkeys, rec->info[recInfo_databaseName]);
810 if (!delkeys.buf_used)
812 logf (LOG_WARN, "cannot delete %s: storeKeys false",
817 logf (LOG_LOG, "delete %s %s", rGroup->recordType, fname);
820 dict_delete (matchDict, matchStr);
821 rec_del (records, &rec);
827 if (!delkeys.buf_used)
829 logf (LOG_WARN, "cannot update %s: storeKeys false",
834 logf (LOG_LOG, "update %s %s", rGroup->recordType,
836 flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
841 xfree (rec->info[recInfo_fileType]);
842 rec->info[recInfo_fileType] =
843 rec_strdup (rGroup->recordType, &rec->size[recInfo_fileType]);
845 xfree (rec->info[recInfo_filename]);
846 rec->info[recInfo_filename] =
847 rec_strdup (fname, &rec->size[recInfo_filename]);
849 xfree (rec->info[recInfo_delKeys]);
850 if (reckeys.buf_used > 0 && rGroup->flagStoreKeys == 1)
852 rec->info[recInfo_delKeys] = xmalloc (reckeys.buf_used);
853 rec->size[recInfo_delKeys] = reckeys.buf_used;
854 memcpy (rec->info[recInfo_delKeys], reckeys.buf,
855 rec->size[recInfo_delKeys]);
859 rec->info[recInfo_delKeys] = NULL;
860 rec->size[recInfo_delKeys] = 0;
863 xfree (rec->info[recInfo_storeData]);
864 if (rGroup->flagStoreData == 1)
866 rec->size[recInfo_storeData] = file_noread;
867 rec->info[recInfo_storeData] = xmalloc (file_noread);
868 #if FILE_READ_BUFSIZE
869 if (file_noread < FILE_READ_BUFSIZE)
870 memcpy (rec->info[recInfo_storeData], file_buf, file_noread);
874 if (lseek (fd, 0L, SEEK_SET) < 0)
876 logf (LOG_ERRNO|LOG_FATAL, "seek to 0 in %s", fname);
879 if (read (fd, rec->info[recInfo_storeData], file_noread)
882 logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
890 rec->info[recInfo_storeData] = NULL;
891 rec->size[recInfo_storeData] = 0;
893 xfree (rec->info[recInfo_databaseName]);
894 rec->info[recInfo_databaseName] =
895 rec_strdup (rGroup->databaseName, &rec->size[recInfo_databaseName]);
897 rec_put (records, &rec);
901 int fileExtract (SYSNO *sysno, const char *fname,
902 const struct recordGroup *rGroupP, int deleteFlag)
910 struct recordGroup rGroupM;
911 struct recordGroup *rGroup = &rGroupM;
913 memcpy (rGroup, rGroupP, sizeof(*rGroupP));
915 if (!rGroup->groupName || !*rGroup->groupName)
918 sprintf (gprefix, "%s.", rGroup->groupName);
920 logf (LOG_DEBUG, "fileExtract %s", fname);
922 /* determine file extension */
923 for (i = strlen(fname); --i >= 0; )
929 else if (fname[i] == '.')
931 strcpy (ext, fname+i+1);
934 /* determine file type - depending on extension */
935 if (!rGroup->recordType)
937 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
938 if (!(rGroup->recordType = res_get (common_resource, ext_res)))
940 sprintf (ext_res, "%srecordType", gprefix);
941 if (!(rGroup->recordType = res_get (common_resource, ext_res)))
943 logf (LOG_LOG, "? %s", fname);
948 if (!rGroup->recordType)
950 logf (LOG_LOG, "? record %s", fname);
953 if (!(recType = recType_byName (rGroup->recordType, subType)))
955 logf (LOG_WARN, "No such record type: %s", rGroup->recordType);
959 /* determine match criteria */
960 if (!rGroup->recordId)
962 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
963 rGroup->recordId = res_get (common_resource, ext_res);
966 /* determine database name */
967 if (!rGroup->databaseName)
969 sprintf (ext_res, "%sdatabase.%s", gprefix, ext);
970 if (!(rGroup->databaseName = res_get (common_resource, ext_res)))
972 sprintf (ext_res, "%sdatabase", gprefix);
973 rGroup->databaseName = res_get (common_resource, ext_res);
976 if (!rGroup->databaseName)
977 rGroup->databaseName = "Default";
979 if (rGroup->flagStoreData == -1)
982 sprintf (ext_res, "%sstoreData.%s", gprefix, ext);
983 if (!(sval = res_get (common_resource, ext_res)))
985 sprintf (ext_res, "%sstoreData", gprefix);
986 sval = res_get (common_resource, ext_res);
989 rGroup->flagStoreData = atoi (sval);
991 if (rGroup->flagStoreData == -1)
992 rGroup->flagStoreData = 0;
994 if (rGroup->flagStoreKeys == -1)
998 sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
999 if (!(sval = res_get (common_resource, ext_res)))
1001 sprintf (ext_res, "%sstoreKeys", gprefix);
1002 sval = res_get (common_resource, ext_res);
1005 rGroup->flagStoreKeys = atoi (sval);
1007 if (rGroup->flagStoreKeys == -1)
1008 rGroup->flagStoreKeys = 0;
1010 if (sysno && deleteFlag)
1014 if ((fd = open (fname, O_RDONLY)) == -1)
1016 logf (LOG_WARN|LOG_ERRNO, "open %s", fname);
1020 file_read_start (fd);
1021 recordExtract (sysno, fname, rGroup, deleteFlag, fd, recType, subType);
1022 file_read_stop (fd);