2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.40 1995-12-05 16:57:40 adam
8 * More work on regular patterns.
10 * Revision 1.39 1995/12/05 13:20:18 adam
11 * Bug fix: file_read sometimes returned early EOF.
13 * Revision 1.38 1995/12/04 17:59:21 adam
14 * More work on regular expression conversion.
16 * Revision 1.37 1995/12/04 14:22:27 adam
17 * Extra arg to recType_byName.
18 * Started work on new regular expression parsed input to
21 * Revision 1.36 1995/11/30 08:34:29 adam
22 * Started work on commit facility.
23 * Changed a few malloc/free to xmalloc/xfree.
25 * Revision 1.35 1995/11/28 14:26:21 adam
26 * Bug fix: recordId with constant wasn't right.
27 * Bug fix: recordId dictionary entry wasn't deleted when needed.
29 * Revision 1.34 1995/11/28 09:09:38 adam
30 * Zebra config renamed.
31 * Use setting 'recordId' to identify record now.
32 * Bug fix in recindex.c: rec_release_blocks was invokeded even
33 * though the blocks were already released.
34 * File traversal properly deletes records when needed.
36 * Revision 1.33 1995/11/27 09:56:20 adam
37 * Record info elements better enumerated. Internal store of records.
39 * Revision 1.32 1995/11/25 10:24:05 adam
40 * More record fields - they are enumerated now.
41 * New options: flagStoreData flagStoreKey.
43 * Revision 1.31 1995/11/24 11:31:35 adam
44 * Commands add & del read filenames from stdin if source directory is
46 * Match criteria supports 'constant' strings.
48 * Revision 1.30 1995/11/22 17:19:16 adam
49 * Record management uses the bfile system.
51 * Revision 1.29 1995/11/21 15:01:14 adam
52 * New general match criteria implemented.
53 * New feature: document groups.
55 * Revision 1.28 1995/11/21 09:20:30 adam
56 * Yet more work on record match.
58 * Revision 1.27 1995/11/20 16:59:45 adam
59 * New update method: the 'old' keys are saved for each records.
61 * Revision 1.26 1995/11/20 11:56:24 adam
62 * Work on new traversal.
64 * Revision 1.25 1995/11/16 15:34:54 adam
65 * Uses new record management system in both indexer and server.
67 * Revision 1.24 1995/11/15 19:13:08 adam
68 * Work on record management.
70 * Revision 1.23 1995/10/27 14:00:10 adam
71 * Implemented detection of database availability.
73 * Revision 1.22 1995/10/17 18:02:07 adam
74 * New feature: databases. Implemented as prefix to words in dictionary.
76 * Revision 1.21 1995/10/10 12:24:38 adam
77 * Temporary sort files are compressed.
79 * Revision 1.20 1995/10/06 13:52:05 adam
80 * Bug fixes. Handler may abort further scanning.
82 * Revision 1.19 1995/10/04 12:55:16 adam
83 * Bug fix in ranked search. Use=Any keys inserted.
85 * Revision 1.18 1995/10/04 09:37:08 quinn
88 * Revision 1.17 1995/10/03 14:28:57 adam
89 * Buffered read in extract works.
91 * Revision 1.16 1995/10/03 14:28:45 adam
92 * Work on more effecient read handler in extract.
94 * Revision 1.15 1995/10/02 15:42:53 adam
95 * Extract uses file descriptors instead of FILE pointers.
97 * Revision 1.14 1995/10/02 15:29:13 adam
98 * More logging in file_extract.
100 * Revision 1.13 1995/09/29 14:01:39 adam
103 * Revision 1.12 1995/09/28 14:22:56 adam
104 * Sort uses smaller temporary files.
106 * Revision 1.11 1995/09/28 12:10:31 adam
107 * Bug fixes. Field prefix used in queries.
109 * Revision 1.10 1995/09/28 09:19:41 adam
110 * xfree/xmalloc used everywhere.
111 * Extract/retrieve method seems to work for text records.
113 * Revision 1.9 1995/09/27 12:22:28 adam
114 * More work on extract in record control.
115 * Field name is not in isam keys but in prefix in dictionary words.
117 * Revision 1.8 1995/09/14 07:48:22 adam
118 * Record control management.
120 * Revision 1.7 1995/09/11 13:09:32 adam
121 * More work on relevance feedback.
123 * Revision 1.6 1995/09/08 14:52:27 adam
124 * Minor changes. Dictionary is lower case now.
126 * Revision 1.5 1995/09/06 16:11:16 adam
127 * Option: only one word key per file.
129 * Revision 1.4 1995/09/05 15:28:39 adam
130 * More work on search engine.
132 * Revision 1.3 1995/09/04 12:33:41 adam
133 * Various cleanup. YAZ util used instead.
135 * Revision 1.2 1995/09/04 09:10:34 adam
136 * More work on index add/del/update.
137 * Merge sort implemented.
138 * Initial work on z39 server.
140 * Revision 1.1 1995/09/01 14:06:35 adam
141 * Split of work into more files.
150 #include <alexutil.h>
154 #include "recindex.h"
156 static Dict matchDict;
158 static Records records = NULL;
160 static char **key_buf;
161 static size_t ptr_top;
163 static size_t key_buf_used;
164 static int key_file_no;
166 static int records_inserted = 0;
167 static int records_updated = 0;
168 static int records_deleted = 0;
170 #define MATCH_DICT "match"
172 void key_open (int mem)
176 key_buf = xmalloc (mem);
177 ptr_top = mem/sizeof(char*);
183 if (!(matchDict = dict_open (MATCH_DICT, 20, 1)))
185 logf (LOG_FATAL, "dict_open fail of %s", MATCH_DICT);
189 records = rec_open (1);
198 void encode_key_init (struct encode_info *i)
204 char *encode_key_int (int d, char *bp)
213 else if (d <= 4194303)
215 *bp++ = 128 + (d>>16);
216 *bp++ = (d>>8) & 255;
221 *bp++ = 192 + (d>>24);
222 *bp++ = (d>>16) & 255;
223 *bp++ = (d>>8) & 255;
229 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
234 while ((*bp++ = *k++))
236 memcpy (&key, k+1, sizeof(struct it_key));
237 bp = encode_key_int ( (key.sysno - i->sysno) * 2 + *k, bp);
238 if (i->sysno != key.sysno)
240 i->sysno = key.sysno;
243 bp = encode_key_int (key.seqno - i->seqno, bp);
244 i->seqno = key.seqno;
245 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
247 logf (LOG_FATAL|LOG_ERRNO, "fwrite");
252 void key_flush (void)
257 struct encode_info encode_info;
263 logf (LOG_LOG, "sorting section %d", key_file_no);
264 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_qsort_compare);
265 sprintf (out_fname, TEMP_FNAME, key_file_no);
267 if (!(outf = fopen (out_fname, "w")))
269 logf (LOG_FATAL|LOG_ERRNO, "fopen (4) %s", out_fname);
272 logf (LOG_LOG, "writing section %d", key_file_no);
273 prevcp = cp = key_buf[ptr_top-ptr_i];
275 encode_key_init (&encode_info);
276 encode_key_write (cp, &encode_info, outf);
279 cp = key_buf[ptr_top-ptr_i];
280 if (strcmp (cp, prevcp))
282 encode_key_init (&encode_info);
283 encode_key_write (cp, &encode_info, outf);
287 encode_key_write (cp + strlen(cp), &encode_info, outf);
291 logf (LOG_FATAL|LOG_ERRNO, "fclose %s", out_fname);
294 logf (LOG_LOG, "finished section %d", key_file_no);
303 rec_close (&records);
304 dict_close (matchDict);
306 logf (LOG_LOG, "Records inserted %6d", records_inserted);
307 logf (LOG_LOG, "Records updated %6d", records_updated);
308 logf (LOG_LOG, "Records deleted %6d", records_deleted);
312 static void wordInit (RecWord *p)
316 p->which = Word_String;
325 static void addRecordKey (const RecWord *p)
332 if (reckeys.buf_used+1024 > reckeys.buf_max)
336 b = xmalloc (reckeys.buf_max += 65000);
337 if (reckeys.buf_used > 0)
338 memcpy (b, reckeys.buf, reckeys.buf_used);
342 dst = reckeys.buf + reckeys.buf_used;
346 attrSet = p->attrSet;
347 memcpy (dst, &attrSet, sizeof(attrSet));
348 dst += sizeof(attrSet);
350 attrUse = p->attrUse;
351 memcpy (dst, &attrUse, sizeof(attrUse));
352 dst += sizeof(attrUse);
354 for (i = 0; p->u.string[i]; i++)
355 *dst++ = p->u.string[i];
358 memcpy (dst, &p->seqno, sizeof(p->seqno));
359 dst += sizeof(p->seqno);
365 reckeys.buf_used = dst - reckeys.buf;
368 static void flushRecordKeys (SYSNO sysno, int cmd, struct recKeys *reckeys,
369 const char *databaseName)
372 while (off < reckeys->buf_used)
374 const char *src = reckeys->buf + off;
379 memcpy (&attrSet, src, sizeof(attrSet));
380 src += sizeof(attrSet);
382 memcpy (&attrUse, src, sizeof(attrUse));
383 src += sizeof(attrUse);
385 if (key_buf_used + 1024 > (ptr_top-ptr_i)*sizeof(char*))
388 key_buf[ptr_top-ptr_i] = (char*)key_buf + key_buf_used;
389 key_buf_used += index_word_prefix ((char*)key_buf + key_buf_used,
390 attrSet, attrUse, databaseName);
392 ((char*)key_buf) [key_buf_used++] = index_char_cvt (*src++);
394 ((char*)key_buf) [key_buf_used++] = '\0';
396 ((char*) key_buf)[key_buf_used++] = cmd;
398 memcpy (&key.seqno, src, sizeof(key.seqno));
399 src += sizeof(key.seqno);
401 memcpy ((char*)key_buf + key_buf_used, &key, sizeof(key));
402 key_buf_used += sizeof(key);
403 off = src - reckeys->buf;
405 assert (off == reckeys->buf_used);
408 static const char **searchRecordKey (struct recKeys *reckeys,
409 int attrSetS, int attrUseS)
411 static const char *ws[32];
416 for (i = 0; i<32; i++)
419 while (off < reckeys->buf_used)
421 const char *src = reckeys->buf + off;
427 memcpy (&attrSet, src, sizeof(attrSet));
428 src += sizeof(attrSet);
430 memcpy (&attrUse, src, sizeof(attrUse));
431 src += sizeof(attrUse);
437 memcpy (&seqno, src, sizeof(seqno));
438 src += sizeof(seqno);
441 logf (LOG_LOG, "(%d,%d) %d %s", attrSet, attrUse, seqno, wstart);
443 if (attrUseS == attrUse && attrSetS == attrSet)
450 woff = seqno - startSeq;
451 if (woff >= 0 && woff < 31)
455 off = src - reckeys->buf;
457 assert (off == reckeys->buf_used);
461 static void addRecordKeyAny (const RecWord *p)
463 if (p->attrSet != 1 || p->attrUse != 1016)
467 memcpy (&w, p, sizeof(w));
476 #define FILE_READ_BUFSIZE 4096
478 static int file_noread;
479 #if FILE_READ_BUFSIZE
480 static char *file_buf;
481 static int file_offset;
482 static int file_bufsize;
485 static void file_read_start (int fd)
488 #if FILE_READ_BUFSIZE
490 file_buf = xmalloc (FILE_READ_BUFSIZE);
491 file_bufsize = read (fd, file_buf, FILE_READ_BUFSIZE);
495 static void file_read_stop (int fd)
497 #if FILE_READ_BUFSIZE
503 static int file_read (int fd, char *buf, size_t count)
505 #if FILE_READ_BUFSIZE
506 int l = file_bufsize - file_offset;
512 memcpy (buf, file_buf + file_offset, l);
514 if (count > FILE_READ_BUFSIZE)
516 if ((r = read (fd, buf + l, count)) == -1)
518 logf (LOG_FATAL|LOG_ERRNO, "read");
526 file_bufsize = r = read (fd, file_buf, FILE_READ_BUFSIZE);
529 logf (LOG_FATAL|LOG_ERRNO, "read");
535 memcpy (buf + l, file_buf, r);
542 memcpy (buf + l, file_buf, count - l);
543 file_noread += count;
547 memcpy (buf, file_buf + file_offset, count);
548 file_offset += count;
549 file_noread += count;
553 r = read (fd, buf, count);
560 static int atois (const char **s)
563 while ( (c=**s) >= '0' && c <= '9')
565 val = val*10 + c - '0';
571 static char *fileMatchStr (struct recKeys *reckeys, struct recordGroup *rGroup,
575 static char dstBuf[2048];
577 const char *s = spec;
578 static const char **w;
583 while (*s == ' ' || *s == '\t')
590 int attrSet, attrUse;
594 attrSet = atois (&s);
597 logf (LOG_WARN, "Missing , in match criteria %s in group %s",
598 spec, rGroup->groupName ? rGroup->groupName : "none");
602 attrUse = atois (&s);
603 w = searchRecordKey (reckeys, attrSet, attrUse);
608 for (i = 0; i<32; i++)
613 logf (LOG_WARN, "Missing ) in match criteria %s in group %s",
614 spec, rGroup->groupName ? rGroup->groupName : "none");
619 for (i = 0; i<32; i++)
620 if (matchFlag[i] && w[i])
632 logf (LOG_WARN, "Record in file %s didn't contain match"
633 " fields in (%d,%d)", fname, attrSet, attrUse);
641 const char *spec_src = NULL;
642 const char *s1 = ++s;
643 while (*s1 && *s1 != ' ' && *s1 != '\t')
649 memcpy (special, s, spec_len);
650 special[spec_len] = '\0';
653 if (!strcmp (special, "group"))
654 spec_src = rGroup->groupName;
655 else if (!strcmp (special, "database"))
656 spec_src = rGroup->databaseName;
657 else if (!strcmp (special, "filename"))
659 else if (!strcmp (special, "type"))
660 spec_src = rGroup->recordType;
665 strcpy (dst, spec_src);
666 dst += strlen (spec_src);
669 else if (*s == '\"' || *s == '\'')
671 int stopMarker = *s++;
675 while (*s && *s != stopMarker)
678 tmpString[i++] = *s++;
683 strcpy (dst, tmpString);
684 dst += strlen (tmpString);
688 logf (LOG_WARN, "Syntax error in match criteria %s in group %s",
689 spec, rGroup->groupName ? rGroup->groupName : "none");
696 logf (LOG_WARN, "No match criteria for record %s in group %s",
697 fname, rGroup->groupName ? rGroup->groupName : "none");
703 static int recordExtract (SYSNO *sysno, const char *fname,
704 struct recordGroup *rGroup, int deleteFlag,
705 int fd, RecType recType, char *subType)
707 struct recExtractCtrl extractCtrl;
717 extractCtrl.subType = subType;
718 extractCtrl.init = wordInit;
719 extractCtrl.add = addRecordKeyAny;
721 reckeys.buf_used = 0;
722 extractCtrl.readf = file_read;
723 r = (*recType->extract)(&extractCtrl);
727 logf (LOG_WARN, "Couldn't extract file %s, code %d", fname, r);
732 /* perform match if sysno not known and if match criteria is specified */
739 if (rGroup->recordId && *rGroup->recordId)
743 matchStr = fileMatchStr (&reckeys, rGroup, fname,
747 rinfo = dict_lookup (matchDict, matchStr);
749 memcpy (sysno, rinfo+1, sizeof(*sysno));
753 logf (LOG_WARN, "Record not inserted");
764 logf (LOG_LOG, "? %s", fname);
767 logf (LOG_LOG, "add %s %s", rGroup->recordType, fname);
768 rec = rec_new (records);
773 dict_insert (matchDict, matchStr, sizeof(*sysno), sysno);
775 flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
781 struct recKeys delkeys;
783 rec = rec_get (records, *sysno);
785 delkeys.buf_used = rec->size[recInfo_delKeys];
786 delkeys.buf = rec->info[recInfo_delKeys];
787 flushRecordKeys (*sysno, 0, &delkeys, rec->info[recInfo_databaseName]);
790 if (!delkeys.buf_used)
792 logf (LOG_WARN, "cannot delete %s - no delete keys", fname);
797 logf (LOG_LOG, "delete %s %s", rGroup->recordType, fname);
800 dict_insert (matchDict, matchStr, sizeof(sysnoz), &sysnoz);
801 rec_del (records, &rec);
807 if (!delkeys.buf_used)
809 logf (LOG_WARN, "cannot update %s - no delete keys",
814 logf (LOG_LOG, "update %s %s", rGroup->recordType,
816 flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
821 xfree (rec->info[recInfo_fileType]);
822 rec->info[recInfo_fileType] =
823 rec_strdup (rGroup->recordType, &rec->size[recInfo_fileType]);
825 xfree (rec->info[recInfo_filename]);
826 rec->info[recInfo_filename] =
827 rec_strdup (fname, &rec->size[recInfo_filename]);
829 xfree (rec->info[recInfo_delKeys]);
830 if (reckeys.buf_used > 0 && rGroup->flagStoreKeys == 1)
832 rec->info[recInfo_delKeys] = xmalloc (reckeys.buf_used);
833 rec->size[recInfo_delKeys] = reckeys.buf_used;
834 memcpy (rec->info[recInfo_delKeys], reckeys.buf,
835 rec->size[recInfo_delKeys]);
839 rec->info[recInfo_delKeys] = NULL;
840 rec->size[recInfo_delKeys] = 0;
843 xfree (rec->info[recInfo_storeData]);
844 if (rGroup->flagStoreData == 1)
846 rec->size[recInfo_storeData] = file_noread;
847 rec->info[recInfo_storeData] = xmalloc (file_noread);
848 #if FILE_READ_BUFSIZE
849 if (file_noread < FILE_READ_BUFSIZE)
850 memcpy (rec->info[recInfo_storeData], file_buf, file_noread);
854 if (lseek (fd, 0L, SEEK_SET) < 0)
856 logf (LOG_ERRNO|LOG_FATAL, "seek to 0 in %s", fname);
859 if (read (fd, rec->info[recInfo_storeData], file_noread)
862 logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
870 rec->info[recInfo_storeData] = NULL;
871 rec->size[recInfo_storeData] = 0;
873 xfree (rec->info[recInfo_databaseName]);
874 rec->info[recInfo_databaseName] =
875 rec_strdup (rGroup->databaseName, &rec->size[recInfo_databaseName]);
877 rec_put (records, &rec);
881 int fileExtract (SYSNO *sysno, const char *fname,
882 const struct recordGroup *rGroupP, int deleteFlag)
890 struct recordGroup rGroupM;
891 struct recordGroup *rGroup = &rGroupM;
893 memcpy (rGroup, rGroupP, sizeof(*rGroupP));
895 if (!rGroup->groupName || !*rGroup->groupName)
898 sprintf (gprefix, "%s.", rGroup->groupName);
900 logf (LOG_DEBUG, "fileExtract %s", fname);
902 /* determine file extension */
903 for (i = strlen(fname); --i >= 0; )
909 else if (fname[i] == '.')
911 strcpy (ext, fname+i+1);
914 /* determine file type - depending on extension */
915 if (!rGroup->recordType)
917 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
918 if (!(rGroup->recordType = res_get (common_resource, ext_res)))
920 sprintf (ext_res, "%srecordType", gprefix);
921 if (!(rGroup->recordType = res_get (common_resource, ext_res)))
923 logf (LOG_LOG, "? %s", fname);
928 if (!rGroup->recordType)
930 logf (LOG_LOG, "? record %s", fname);
933 if (!(recType = recType_byName (rGroup->recordType, subType)))
935 logf (LOG_WARN, "No such record type: %s", rGroup->recordType);
939 /* determine match criteria */
940 if (!rGroup->recordId)
942 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
943 rGroup->recordId = res_get (common_resource, ext_res);
946 /* determine database name */
947 if (!rGroup->databaseName)
949 sprintf (ext_res, "%sdatabase.%s", gprefix, ext);
950 if (!(rGroup->databaseName = res_get (common_resource, ext_res)))
952 sprintf (ext_res, "%sdatabase", gprefix);
953 rGroup->databaseName = res_get (common_resource, ext_res);
956 if (!rGroup->databaseName)
957 rGroup->databaseName = "Default";
959 if (rGroup->flagStoreData == -1)
962 sprintf (ext_res, "%sstoreData.%s", gprefix, ext);
963 if (!(sval = res_get (common_resource, ext_res)))
965 sprintf (ext_res, "%sstoreData", gprefix);
966 sval = res_get (common_resource, ext_res);
969 rGroup->flagStoreData = atoi (sval);
971 if (rGroup->flagStoreData == -1)
972 rGroup->flagStoreData = 0;
974 if (rGroup->flagStoreKeys == -1)
978 sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
979 if (!(sval = res_get (common_resource, ext_res)))
981 sprintf (ext_res, "%sstoreKeys", gprefix);
982 sval = res_get (common_resource, ext_res);
985 rGroup->flagStoreKeys = atoi (sval);
987 if (rGroup->flagStoreKeys == -1)
988 rGroup->flagStoreKeys = 0;
990 if (sysno && deleteFlag)
994 if ((fd = open (fname, O_RDONLY)) == -1)
996 logf (LOG_WARN|LOG_ERRNO, "open %s", fname);
1000 file_read_start (fd);
1001 recordExtract (sysno, fname, rGroup, deleteFlag, fd, recType, subType);
1002 file_read_stop (fd);