2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.43 1995-12-11 09:12:46 adam
8 * The rec_get function returns NULL if record doesn't exist - will
9 * happen in the server if the result set records have been deleted since
10 * the creation of the set (i.e. the search).
11 * The server saves a result temporarily if it is 'volatile', i.e. the
12 * set is register dependent.
14 * Revision 1.42 1995/12/07 17:38:46 adam
15 * Work locking mechanisms for concurrent updates/commit.
17 * Revision 1.41 1995/12/06 16:06:42 adam
18 * Better diagnostics. Work on 'real' dictionary deletion.
20 * Revision 1.40 1995/12/05 16:57:40 adam
21 * More work on regular patterns.
23 * Revision 1.39 1995/12/05 13:20:18 adam
24 * Bug fix: file_read sometimes returned early EOF.
26 * Revision 1.38 1995/12/04 17:59:21 adam
27 * More work on regular expression conversion.
29 * Revision 1.37 1995/12/04 14:22:27 adam
30 * Extra arg to recType_byName.
31 * Started work on new regular expression parsed input to
34 * Revision 1.36 1995/11/30 08:34:29 adam
35 * Started work on commit facility.
36 * Changed a few malloc/free to xmalloc/xfree.
38 * Revision 1.35 1995/11/28 14:26:21 adam
39 * Bug fix: recordId with constant wasn't right.
40 * Bug fix: recordId dictionary entry wasn't deleted when needed.
42 * Revision 1.34 1995/11/28 09:09:38 adam
43 * Zebra config renamed.
44 * Use setting 'recordId' to identify record now.
45 * Bug fix in recindex.c: rec_release_blocks was invokeded even
46 * though the blocks were already released.
47 * File traversal properly deletes records when needed.
49 * Revision 1.33 1995/11/27 09:56:20 adam
50 * Record info elements better enumerated. Internal store of records.
52 * Revision 1.32 1995/11/25 10:24:05 adam
53 * More record fields - they are enumerated now.
54 * New options: flagStoreData flagStoreKey.
56 * Revision 1.31 1995/11/24 11:31:35 adam
57 * Commands add & del read filenames from stdin if source directory is
59 * Match criteria supports 'constant' strings.
61 * Revision 1.30 1995/11/22 17:19:16 adam
62 * Record management uses the bfile system.
64 * Revision 1.29 1995/11/21 15:01:14 adam
65 * New general match criteria implemented.
66 * New feature: document groups.
68 * Revision 1.28 1995/11/21 09:20:30 adam
69 * Yet more work on record match.
71 * Revision 1.27 1995/11/20 16:59:45 adam
72 * New update method: the 'old' keys are saved for each records.
74 * Revision 1.26 1995/11/20 11:56:24 adam
75 * Work on new traversal.
77 * Revision 1.25 1995/11/16 15:34:54 adam
78 * Uses new record management system in both indexer and server.
80 * Revision 1.24 1995/11/15 19:13:08 adam
81 * Work on record management.
83 * Revision 1.23 1995/10/27 14:00:10 adam
84 * Implemented detection of database availability.
86 * Revision 1.22 1995/10/17 18:02:07 adam
87 * New feature: databases. Implemented as prefix to words in dictionary.
89 * Revision 1.21 1995/10/10 12:24:38 adam
90 * Temporary sort files are compressed.
92 * Revision 1.20 1995/10/06 13:52:05 adam
93 * Bug fixes. Handler may abort further scanning.
95 * Revision 1.19 1995/10/04 12:55:16 adam
96 * Bug fix in ranked search. Use=Any keys inserted.
98 * Revision 1.18 1995/10/04 09:37:08 quinn
101 * Revision 1.17 1995/10/03 14:28:57 adam
102 * Buffered read in extract works.
104 * Revision 1.16 1995/10/03 14:28:45 adam
105 * Work on more effecient read handler in extract.
107 * Revision 1.15 1995/10/02 15:42:53 adam
108 * Extract uses file descriptors instead of FILE pointers.
110 * Revision 1.14 1995/10/02 15:29:13 adam
111 * More logging in file_extract.
113 * Revision 1.13 1995/09/29 14:01:39 adam
116 * Revision 1.12 1995/09/28 14:22:56 adam
117 * Sort uses smaller temporary files.
119 * Revision 1.11 1995/09/28 12:10:31 adam
120 * Bug fixes. Field prefix used in queries.
122 * Revision 1.10 1995/09/28 09:19:41 adam
123 * xfree/xmalloc used everywhere.
124 * Extract/retrieve method seems to work for text records.
126 * Revision 1.9 1995/09/27 12:22:28 adam
127 * More work on extract in record control.
128 * Field name is not in isam keys but in prefix in dictionary words.
130 * Revision 1.8 1995/09/14 07:48:22 adam
131 * Record control management.
133 * Revision 1.7 1995/09/11 13:09:32 adam
134 * More work on relevance feedback.
136 * Revision 1.6 1995/09/08 14:52:27 adam
137 * Minor changes. Dictionary is lower case now.
139 * Revision 1.5 1995/09/06 16:11:16 adam
140 * Option: only one word key per file.
142 * Revision 1.4 1995/09/05 15:28:39 adam
143 * More work on search engine.
145 * Revision 1.3 1995/09/04 12:33:41 adam
146 * Various cleanup. YAZ util used instead.
148 * Revision 1.2 1995/09/04 09:10:34 adam
149 * More work on index add/del/update.
150 * Merge sort implemented.
151 * Initial work on z39 server.
153 * Revision 1.1 1995/09/01 14:06:35 adam
154 * Split of work into more files.
163 #include <alexutil.h>
167 #include "recindex.h"
169 static Dict matchDict;
171 static Records records = NULL;
173 static char **key_buf;
174 static size_t ptr_top;
176 static size_t key_buf_used;
177 static int key_file_no;
179 static int records_inserted = 0;
180 static int records_updated = 0;
181 static int records_deleted = 0;
183 void key_open (int mem)
187 logf (LOG_LOG, "key_open %d", mem);
188 key_buf = xmalloc (mem);
189 ptr_top = mem/sizeof(char*);
195 if (!(matchDict = dict_open (GMATCH_DICT, 50, 1)))
197 logf (LOG_FATAL, "dict_open fail of %s", GMATCH_DICT);
201 records = rec_open (1);
210 void encode_key_init (struct encode_info *i)
216 char *encode_key_int (int d, char *bp)
225 else if (d <= 4194303)
227 *bp++ = 128 + (d>>16);
228 *bp++ = (d>>8) & 255;
233 *bp++ = 192 + (d>>24);
234 *bp++ = (d>>16) & 255;
235 *bp++ = (d>>8) & 255;
241 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
246 while ((*bp++ = *k++))
248 memcpy (&key, k+1, sizeof(struct it_key));
249 bp = encode_key_int ( (key.sysno - i->sysno) * 2 + *k, bp);
250 if (i->sysno != key.sysno)
252 i->sysno = key.sysno;
255 bp = encode_key_int (key.seqno - i->seqno, bp);
256 i->seqno = key.seqno;
257 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
259 logf (LOG_FATAL|LOG_ERRNO, "fwrite");
264 void key_flush (void)
269 struct encode_info encode_info;
275 logf (LOG_LOG, "sorting section %d", key_file_no);
276 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_qsort_compare);
277 sprintf (out_fname, TEMP_FNAME, key_file_no);
279 if (!(outf = fopen (out_fname, "w")))
281 logf (LOG_FATAL|LOG_ERRNO, "fopen (4) %s", out_fname);
284 logf (LOG_LOG, "writing section %d", key_file_no);
285 prevcp = cp = key_buf[ptr_top-ptr_i];
287 encode_key_init (&encode_info);
288 encode_key_write (cp, &encode_info, outf);
291 cp = key_buf[ptr_top-ptr_i];
292 if (strcmp (cp, prevcp))
294 encode_key_init (&encode_info);
295 encode_key_write (cp, &encode_info, outf);
299 encode_key_write (cp + strlen(cp), &encode_info, outf);
303 logf (LOG_FATAL|LOG_ERRNO, "fclose %s", out_fname);
306 logf (LOG_LOG, "finished section %d", key_file_no);
314 logf (LOG_LOG, "buf free");
316 rec_close (&records);
317 dict_close (matchDict);
319 logf (LOG_LOG, "Records inserted %6d", records_inserted);
320 logf (LOG_LOG, "Records updated %6d", records_updated);
321 logf (LOG_LOG, "Records deleted %6d", records_deleted);
325 static void wordInit (RecWord *p)
329 p->which = Word_String;
338 static void addRecordKey (const RecWord *p)
345 if (reckeys.buf_used+1024 > reckeys.buf_max)
349 b = xmalloc (reckeys.buf_max += 65000);
350 if (reckeys.buf_used > 0)
351 memcpy (b, reckeys.buf, reckeys.buf_used);
355 dst = reckeys.buf + reckeys.buf_used;
359 attrSet = p->attrSet;
360 memcpy (dst, &attrSet, sizeof(attrSet));
361 dst += sizeof(attrSet);
363 attrUse = p->attrUse;
364 memcpy (dst, &attrUse, sizeof(attrUse));
365 dst += sizeof(attrUse);
367 for (i = 0; p->u.string[i]; i++)
368 *dst++ = p->u.string[i];
371 memcpy (dst, &p->seqno, sizeof(p->seqno));
372 dst += sizeof(p->seqno);
378 reckeys.buf_used = dst - reckeys.buf;
381 static void flushRecordKeys (SYSNO sysno, int cmd, struct recKeys *reckeys,
382 const char *databaseName)
385 while (off < reckeys->buf_used)
387 const char *src = reckeys->buf + off;
392 memcpy (&attrSet, src, sizeof(attrSet));
393 src += sizeof(attrSet);
395 memcpy (&attrUse, src, sizeof(attrUse));
396 src += sizeof(attrUse);
398 if (key_buf_used + 1024 > (ptr_top-ptr_i)*sizeof(char*))
401 key_buf[ptr_top-ptr_i] = (char*)key_buf + key_buf_used;
402 key_buf_used += index_word_prefix ((char*)key_buf + key_buf_used,
403 attrSet, attrUse, databaseName);
405 ((char*)key_buf) [key_buf_used++] = index_char_cvt (*src++);
407 ((char*)key_buf) [key_buf_used++] = '\0';
409 ((char*) key_buf)[key_buf_used++] = cmd;
411 memcpy (&key.seqno, src, sizeof(key.seqno));
412 src += sizeof(key.seqno);
414 memcpy ((char*)key_buf + key_buf_used, &key, sizeof(key));
415 key_buf_used += sizeof(key);
416 off = src - reckeys->buf;
418 assert (off == reckeys->buf_used);
421 static const char **searchRecordKey (struct recKeys *reckeys,
422 int attrSetS, int attrUseS)
424 static const char *ws[32];
429 for (i = 0; i<32; i++)
432 while (off < reckeys->buf_used)
434 const char *src = reckeys->buf + off;
440 memcpy (&attrSet, src, sizeof(attrSet));
441 src += sizeof(attrSet);
443 memcpy (&attrUse, src, sizeof(attrUse));
444 src += sizeof(attrUse);
450 memcpy (&seqno, src, sizeof(seqno));
451 src += sizeof(seqno);
454 logf (LOG_LOG, "(%d,%d) %d %s", attrSet, attrUse, seqno, wstart);
456 if (attrUseS == attrUse && attrSetS == attrSet)
463 woff = seqno - startSeq;
464 if (woff >= 0 && woff < 31)
468 off = src - reckeys->buf;
470 assert (off == reckeys->buf_used);
474 static void addRecordKeyAny (const RecWord *p)
476 if (p->attrSet != 1 || p->attrUse != 1016)
480 memcpy (&w, p, sizeof(w));
489 #define FILE_READ_BUFSIZE 4096
491 static int file_noread;
492 #if FILE_READ_BUFSIZE
493 static char *file_buf;
494 static int file_offset;
495 static int file_bufsize;
498 static void file_read_start (int fd)
501 #if FILE_READ_BUFSIZE
503 file_buf = xmalloc (FILE_READ_BUFSIZE);
504 file_bufsize = read (fd, file_buf, FILE_READ_BUFSIZE);
508 static void file_read_stop (int fd)
510 #if FILE_READ_BUFSIZE
516 static int file_read (int fd, char *buf, size_t count)
518 #if FILE_READ_BUFSIZE
519 int l = file_bufsize - file_offset;
525 memcpy (buf, file_buf + file_offset, l);
527 if (count > FILE_READ_BUFSIZE)
529 if ((r = read (fd, buf + l, count)) == -1)
531 logf (LOG_FATAL|LOG_ERRNO, "read");
539 file_bufsize = r = read (fd, file_buf, FILE_READ_BUFSIZE);
542 logf (LOG_FATAL|LOG_ERRNO, "read");
548 memcpy (buf + l, file_buf, r);
555 memcpy (buf + l, file_buf, count - l);
556 file_noread += count;
560 memcpy (buf, file_buf + file_offset, count);
561 file_offset += count;
562 file_noread += count;
566 r = read (fd, buf, count);
573 static int atois (const char **s)
576 while ( (c=**s) >= '0' && c <= '9')
578 val = val*10 + c - '0';
584 static char *fileMatchStr (struct recKeys *reckeys, struct recordGroup *rGroup,
588 static char dstBuf[2048];
590 const char *s = spec;
591 static const char **w;
596 while (*s == ' ' || *s == '\t')
603 int attrSet, attrUse;
607 attrSet = atois (&s);
610 logf (LOG_WARN, "Missing , in match criteria %s in group %s",
611 spec, rGroup->groupName ? rGroup->groupName : "none");
615 attrUse = atois (&s);
616 w = searchRecordKey (reckeys, attrSet, attrUse);
621 for (i = 0; i<32; i++)
626 logf (LOG_WARN, "Missing ) in match criteria %s in group %s",
627 spec, rGroup->groupName ? rGroup->groupName : "none");
632 for (i = 0; i<32; i++)
633 if (matchFlag[i] && w[i])
645 logf (LOG_WARN, "Record in file %s didn't contain match"
646 " fields in (%d,%d)", fname, attrSet, attrUse);
654 const char *spec_src = NULL;
655 const char *s1 = ++s;
656 while (*s1 && *s1 != ' ' && *s1 != '\t')
662 memcpy (special, s, spec_len);
663 special[spec_len] = '\0';
666 if (!strcmp (special, "group"))
667 spec_src = rGroup->groupName;
668 else if (!strcmp (special, "database"))
669 spec_src = rGroup->databaseName;
670 else if (!strcmp (special, "filename"))
672 else if (!strcmp (special, "type"))
673 spec_src = rGroup->recordType;
678 strcpy (dst, spec_src);
679 dst += strlen (spec_src);
682 else if (*s == '\"' || *s == '\'')
684 int stopMarker = *s++;
688 while (*s && *s != stopMarker)
691 tmpString[i++] = *s++;
696 strcpy (dst, tmpString);
697 dst += strlen (tmpString);
701 logf (LOG_WARN, "Syntax error in match criteria %s in group %s",
702 spec, rGroup->groupName ? rGroup->groupName : "none");
709 logf (LOG_WARN, "No match criteria for record %s in group %s",
710 fname, rGroup->groupName ? rGroup->groupName : "none");
716 static int recordExtract (SYSNO *sysno, const char *fname,
717 struct recordGroup *rGroup, int deleteFlag,
718 int fd, RecType recType, char *subType)
720 struct recExtractCtrl extractCtrl;
730 extractCtrl.subType = subType;
731 extractCtrl.init = wordInit;
732 extractCtrl.add = addRecordKeyAny;
734 reckeys.buf_used = 0;
735 extractCtrl.readf = file_read;
736 r = (*recType->extract)(&extractCtrl);
740 logf (LOG_WARN, "Couldn't extract file %s, code %d", fname, r);
745 /* perform match if sysno not known and if match criteria is specified */
752 if (rGroup->recordId && *rGroup->recordId)
756 matchStr = fileMatchStr (&reckeys, rGroup, fname,
760 rinfo = dict_lookup (matchDict, matchStr);
762 memcpy (sysno, rinfo+1, sizeof(*sysno));
766 logf (LOG_WARN, "Record not inserted");
777 logf (LOG_LOG, "? %s", fname);
780 logf (LOG_LOG, "add %s %s", rGroup->recordType, fname);
781 rec = rec_new (records);
786 dict_insert (matchDict, matchStr, sizeof(*sysno), sysno);
788 flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
794 struct recKeys delkeys;
796 rec = rec_get (records, *sysno);
798 delkeys.buf_used = rec->size[recInfo_delKeys];
799 delkeys.buf = rec->info[recInfo_delKeys];
800 flushRecordKeys (*sysno, 0, &delkeys, rec->info[recInfo_databaseName]);
803 if (!delkeys.buf_used)
805 logf (LOG_WARN, "cannot delete %s: storeKeys false",
810 logf (LOG_LOG, "delete %s %s", rGroup->recordType, fname);
813 dict_delete (matchDict, matchStr);
814 rec_del (records, &rec);
820 if (!delkeys.buf_used)
822 logf (LOG_WARN, "cannot update %s: storeKeys false",
827 logf (LOG_LOG, "update %s %s", rGroup->recordType,
829 flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
834 xfree (rec->info[recInfo_fileType]);
835 rec->info[recInfo_fileType] =
836 rec_strdup (rGroup->recordType, &rec->size[recInfo_fileType]);
838 xfree (rec->info[recInfo_filename]);
839 rec->info[recInfo_filename] =
840 rec_strdup (fname, &rec->size[recInfo_filename]);
842 xfree (rec->info[recInfo_delKeys]);
843 if (reckeys.buf_used > 0 && rGroup->flagStoreKeys == 1)
845 rec->info[recInfo_delKeys] = xmalloc (reckeys.buf_used);
846 rec->size[recInfo_delKeys] = reckeys.buf_used;
847 memcpy (rec->info[recInfo_delKeys], reckeys.buf,
848 rec->size[recInfo_delKeys]);
852 rec->info[recInfo_delKeys] = NULL;
853 rec->size[recInfo_delKeys] = 0;
856 xfree (rec->info[recInfo_storeData]);
857 if (rGroup->flagStoreData == 1)
859 rec->size[recInfo_storeData] = file_noread;
860 rec->info[recInfo_storeData] = xmalloc (file_noread);
861 #if FILE_READ_BUFSIZE
862 if (file_noread < FILE_READ_BUFSIZE)
863 memcpy (rec->info[recInfo_storeData], file_buf, file_noread);
867 if (lseek (fd, 0L, SEEK_SET) < 0)
869 logf (LOG_ERRNO|LOG_FATAL, "seek to 0 in %s", fname);
872 if (read (fd, rec->info[recInfo_storeData], file_noread)
875 logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
883 rec->info[recInfo_storeData] = NULL;
884 rec->size[recInfo_storeData] = 0;
886 xfree (rec->info[recInfo_databaseName]);
887 rec->info[recInfo_databaseName] =
888 rec_strdup (rGroup->databaseName, &rec->size[recInfo_databaseName]);
890 rec_put (records, &rec);
894 int fileExtract (SYSNO *sysno, const char *fname,
895 const struct recordGroup *rGroupP, int deleteFlag)
903 struct recordGroup rGroupM;
904 struct recordGroup *rGroup = &rGroupM;
906 memcpy (rGroup, rGroupP, sizeof(*rGroupP));
908 if (!rGroup->groupName || !*rGroup->groupName)
911 sprintf (gprefix, "%s.", rGroup->groupName);
913 logf (LOG_DEBUG, "fileExtract %s", fname);
915 /* determine file extension */
916 for (i = strlen(fname); --i >= 0; )
922 else if (fname[i] == '.')
924 strcpy (ext, fname+i+1);
927 /* determine file type - depending on extension */
928 if (!rGroup->recordType)
930 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
931 if (!(rGroup->recordType = res_get (common_resource, ext_res)))
933 sprintf (ext_res, "%srecordType", gprefix);
934 if (!(rGroup->recordType = res_get (common_resource, ext_res)))
936 logf (LOG_LOG, "? %s", fname);
941 if (!rGroup->recordType)
943 logf (LOG_LOG, "? record %s", fname);
946 if (!(recType = recType_byName (rGroup->recordType, subType)))
948 logf (LOG_WARN, "No such record type: %s", rGroup->recordType);
952 /* determine match criteria */
953 if (!rGroup->recordId)
955 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
956 rGroup->recordId = res_get (common_resource, ext_res);
959 /* determine database name */
960 if (!rGroup->databaseName)
962 sprintf (ext_res, "%sdatabase.%s", gprefix, ext);
963 if (!(rGroup->databaseName = res_get (common_resource, ext_res)))
965 sprintf (ext_res, "%sdatabase", gprefix);
966 rGroup->databaseName = res_get (common_resource, ext_res);
969 if (!rGroup->databaseName)
970 rGroup->databaseName = "Default";
972 if (rGroup->flagStoreData == -1)
975 sprintf (ext_res, "%sstoreData.%s", gprefix, ext);
976 if (!(sval = res_get (common_resource, ext_res)))
978 sprintf (ext_res, "%sstoreData", gprefix);
979 sval = res_get (common_resource, ext_res);
982 rGroup->flagStoreData = atoi (sval);
984 if (rGroup->flagStoreData == -1)
985 rGroup->flagStoreData = 0;
987 if (rGroup->flagStoreKeys == -1)
991 sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
992 if (!(sval = res_get (common_resource, ext_res)))
994 sprintf (ext_res, "%sstoreKeys", gprefix);
995 sval = res_get (common_resource, ext_res);
998 rGroup->flagStoreKeys = atoi (sval);
1000 if (rGroup->flagStoreKeys == -1)
1001 rGroup->flagStoreKeys = 0;
1003 if (sysno && deleteFlag)
1007 if ((fd = open (fname, O_RDONLY)) == -1)
1009 logf (LOG_WARN|LOG_ERRNO, "open %s", fname);
1013 file_read_start (fd);
1014 recordExtract (sysno, fname, rGroup, deleteFlag, fd, recType, subType);
1015 file_read_stop (fd);