2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.37 1995-12-04 14:22:27 adam
8 * Extra arg to recType_byName.
9 * Started work on new regular expression parsed input to
12 * Revision 1.36 1995/11/30 08:34:29 adam
13 * Started work on commit facility.
14 * Changed a few malloc/free to xmalloc/xfree.
16 * Revision 1.35 1995/11/28 14:26:21 adam
17 * Bug fix: recordId with constant wasn't right.
18 * Bug fix: recordId dictionary entry wasn't deleted when needed.
20 * Revision 1.34 1995/11/28 09:09:38 adam
21 * Zebra config renamed.
22 * Use setting 'recordId' to identify record now.
23 * Bug fix in recindex.c: rec_release_blocks was invokeded even
24 * though the blocks were already released.
25 * File traversal properly deletes records when needed.
27 * Revision 1.33 1995/11/27 09:56:20 adam
28 * Record info elements better enumerated. Internal store of records.
30 * Revision 1.32 1995/11/25 10:24:05 adam
31 * More record fields - they are enumerated now.
32 * New options: flagStoreData flagStoreKey.
34 * Revision 1.31 1995/11/24 11:31:35 adam
35 * Commands add & del read filenames from stdin if source directory is
37 * Match criteria supports 'constant' strings.
39 * Revision 1.30 1995/11/22 17:19:16 adam
40 * Record management uses the bfile system.
42 * Revision 1.29 1995/11/21 15:01:14 adam
43 * New general match criteria implemented.
44 * New feature: document groups.
46 * Revision 1.28 1995/11/21 09:20:30 adam
47 * Yet more work on record match.
49 * Revision 1.27 1995/11/20 16:59:45 adam
50 * New update method: the 'old' keys are saved for each records.
52 * Revision 1.26 1995/11/20 11:56:24 adam
53 * Work on new traversal.
55 * Revision 1.25 1995/11/16 15:34:54 adam
56 * Uses new record management system in both indexer and server.
58 * Revision 1.24 1995/11/15 19:13:08 adam
59 * Work on record management.
61 * Revision 1.23 1995/10/27 14:00:10 adam
62 * Implemented detection of database availability.
64 * Revision 1.22 1995/10/17 18:02:07 adam
65 * New feature: databases. Implemented as prefix to words in dictionary.
67 * Revision 1.21 1995/10/10 12:24:38 adam
68 * Temporary sort files are compressed.
70 * Revision 1.20 1995/10/06 13:52:05 adam
71 * Bug fixes. Handler may abort further scanning.
73 * Revision 1.19 1995/10/04 12:55:16 adam
74 * Bug fix in ranked search. Use=Any keys inserted.
76 * Revision 1.18 1995/10/04 09:37:08 quinn
79 * Revision 1.17 1995/10/03 14:28:57 adam
80 * Buffered read in extract works.
82 * Revision 1.16 1995/10/03 14:28:45 adam
83 * Work on more effecient read handler in extract.
85 * Revision 1.15 1995/10/02 15:42:53 adam
86 * Extract uses file descriptors instead of FILE pointers.
88 * Revision 1.14 1995/10/02 15:29:13 adam
89 * More logging in file_extract.
91 * Revision 1.13 1995/09/29 14:01:39 adam
94 * Revision 1.12 1995/09/28 14:22:56 adam
95 * Sort uses smaller temporary files.
97 * Revision 1.11 1995/09/28 12:10:31 adam
98 * Bug fixes. Field prefix used in queries.
100 * Revision 1.10 1995/09/28 09:19:41 adam
101 * xfree/xmalloc used everywhere.
102 * Extract/retrieve method seems to work for text records.
104 * Revision 1.9 1995/09/27 12:22:28 adam
105 * More work on extract in record control.
106 * Field name is not in isam keys but in prefix in dictionary words.
108 * Revision 1.8 1995/09/14 07:48:22 adam
109 * Record control management.
111 * Revision 1.7 1995/09/11 13:09:32 adam
112 * More work on relevance feedback.
114 * Revision 1.6 1995/09/08 14:52:27 adam
115 * Minor changes. Dictionary is lower case now.
117 * Revision 1.5 1995/09/06 16:11:16 adam
118 * Option: only one word key per file.
120 * Revision 1.4 1995/09/05 15:28:39 adam
121 * More work on search engine.
123 * Revision 1.3 1995/09/04 12:33:41 adam
124 * Various cleanup. YAZ util used instead.
126 * Revision 1.2 1995/09/04 09:10:34 adam
127 * More work on index add/del/update.
128 * Merge sort implemented.
129 * Initial work on z39 server.
131 * Revision 1.1 1995/09/01 14:06:35 adam
132 * Split of work into more files.
141 #include <alexutil.h>
145 #include "recindex.h"
147 static Dict matchDict;
149 static Records records = NULL;
151 static char **key_buf;
152 static size_t ptr_top;
154 static size_t key_buf_used;
155 static int key_file_no;
157 static int records_inserted = 0;
158 static int records_updated = 0;
159 static int records_deleted = 0;
161 #define MATCH_DICT "match"
163 void key_open (int mem)
167 key_buf = xmalloc (mem);
168 ptr_top = mem/sizeof(char*);
174 if (!(matchDict = dict_open (MATCH_DICT, 20, 1)))
176 logf (LOG_FATAL, "dict_open fail of %s", MATCH_DICT);
180 records = rec_open (1);
189 void encode_key_init (struct encode_info *i)
195 char *encode_key_int (int d, char *bp)
204 else if (d <= 4194303)
206 *bp++ = 128 + (d>>16);
207 *bp++ = (d>>8) & 255;
212 *bp++ = 192 + (d>>24);
213 *bp++ = (d>>16) & 255;
214 *bp++ = (d>>8) & 255;
220 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
225 while ((*bp++ = *k++))
227 memcpy (&key, k+1, sizeof(struct it_key));
228 bp = encode_key_int ( (key.sysno - i->sysno) * 2 + *k, bp);
229 if (i->sysno != key.sysno)
231 i->sysno = key.sysno;
234 bp = encode_key_int (key.seqno - i->seqno, bp);
235 i->seqno = key.seqno;
236 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
238 logf (LOG_FATAL|LOG_ERRNO, "fwrite");
243 void key_flush (void)
248 struct encode_info encode_info;
254 logf (LOG_LOG, "sorting section %d", key_file_no);
255 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_qsort_compare);
256 sprintf (out_fname, TEMP_FNAME, key_file_no);
258 if (!(outf = fopen (out_fname, "w")))
260 logf (LOG_FATAL|LOG_ERRNO, "fopen (4) %s", out_fname);
263 logf (LOG_LOG, "writing section %d", key_file_no);
264 prevcp = cp = key_buf[ptr_top-ptr_i];
266 encode_key_init (&encode_info);
267 encode_key_write (cp, &encode_info, outf);
270 cp = key_buf[ptr_top-ptr_i];
271 if (strcmp (cp, prevcp))
273 encode_key_init (&encode_info);
274 encode_key_write (cp, &encode_info, outf);
278 encode_key_write (cp + strlen(cp), &encode_info, outf);
282 logf (LOG_FATAL|LOG_ERRNO, "fclose %s", out_fname);
285 logf (LOG_LOG, "finished section %d", key_file_no);
294 rec_close (&records);
295 dict_close (matchDict);
297 logf (LOG_LOG, "Records inserted %6d", records_inserted);
298 logf (LOG_LOG, "Records updated %6d", records_updated);
299 logf (LOG_LOG, "Records deleted %6d", records_deleted);
303 static void wordInit (RecWord *p)
307 p->which = Word_String;
316 static void addRecordKey (const RecWord *p)
323 if (reckeys.buf_used+1024 > reckeys.buf_max)
327 b = xmalloc (reckeys.buf_max += 65000);
328 if (reckeys.buf_used > 0)
329 memcpy (b, reckeys.buf, reckeys.buf_used);
333 dst = reckeys.buf + reckeys.buf_used;
337 attrSet = p->attrSet;
338 memcpy (dst, &attrSet, sizeof(attrSet));
339 dst += sizeof(attrSet);
341 attrUse = p->attrUse;
342 memcpy (dst, &attrUse, sizeof(attrUse));
343 dst += sizeof(attrUse);
345 for (i = 0; p->u.string[i]; i++)
346 *dst++ = p->u.string[i];
349 memcpy (dst, &p->seqno, sizeof(p->seqno));
350 dst += sizeof(p->seqno);
356 reckeys.buf_used = dst - reckeys.buf;
359 static void flushRecordKeys (SYSNO sysno, int cmd, struct recKeys *reckeys,
360 const char *databaseName)
363 while (off < reckeys->buf_used)
365 const char *src = reckeys->buf + off;
370 memcpy (&attrSet, src, sizeof(attrSet));
371 src += sizeof(attrSet);
373 memcpy (&attrUse, src, sizeof(attrUse));
374 src += sizeof(attrUse);
376 if (key_buf_used + 1024 > (ptr_top-ptr_i)*sizeof(char*))
379 key_buf[ptr_top-ptr_i] = (char*)key_buf + key_buf_used;
380 key_buf_used += index_word_prefix ((char*)key_buf + key_buf_used,
381 attrSet, attrUse, databaseName);
383 ((char*)key_buf) [key_buf_used++] = index_char_cvt (*src++);
385 ((char*)key_buf) [key_buf_used++] = '\0';
387 ((char*) key_buf)[key_buf_used++] = cmd;
389 memcpy (&key.seqno, src, sizeof(key.seqno));
390 src += sizeof(key.seqno);
392 memcpy ((char*)key_buf + key_buf_used, &key, sizeof(key));
393 key_buf_used += sizeof(key);
394 off = src - reckeys->buf;
396 assert (off == reckeys->buf_used);
399 static const char **searchRecordKey (struct recKeys *reckeys,
400 int attrSetS, int attrUseS)
402 static const char *ws[32];
407 for (i = 0; i<32; i++)
410 while (off < reckeys->buf_used)
412 const char *src = reckeys->buf + off;
418 memcpy (&attrSet, src, sizeof(attrSet));
419 src += sizeof(attrSet);
421 memcpy (&attrUse, src, sizeof(attrUse));
422 src += sizeof(attrUse);
428 memcpy (&seqno, src, sizeof(seqno));
429 src += sizeof(seqno);
432 logf (LOG_LOG, "(%d,%d) %d %s", attrSet, attrUse, seqno, wstart);
434 if (attrUseS == attrUse && attrSetS == attrSet)
441 woff = seqno - startSeq;
442 if (woff >= 0 && woff < 31)
446 off = src - reckeys->buf;
448 assert (off == reckeys->buf_used);
452 static void addRecordKeyAny (const RecWord *p)
454 if (p->attrSet != 1 || p->attrUse != 1016)
458 memcpy (&w, p, sizeof(w));
466 #define FILE_READ_BUFSIZE 4096
468 static char *file_buf;
469 static int file_offset;
470 static int file_bufsize;
471 static int file_noread;
473 static void file_read_start (int fd)
476 file_buf = xmalloc (FILE_READ_BUFSIZE);
477 file_bufsize = read (fd, file_buf, FILE_READ_BUFSIZE);
481 static void file_read_stop (int fd)
487 static int file_read (int fd, char *buf, size_t count)
489 int l = file_bufsize - file_offset;
495 memcpy (buf, file_buf + file_offset, l);
497 if (count > file_bufsize)
499 if ((r = read (fd, buf + l, count)) == -1)
501 logf (LOG_FATAL|LOG_ERRNO, "read");
509 file_bufsize = r = read (fd, file_buf, FILE_READ_BUFSIZE);
512 logf (LOG_FATAL|LOG_ERRNO, "read");
518 memcpy (buf + l, file_buf, r);
519 file_noread += (l+r);
525 memcpy (buf + l, file_buf, count - l);
526 file_noread += count;
530 memcpy (buf, file_buf + file_offset, count);
531 file_offset += count;
532 file_noread += count;
536 static int atois (const char **s)
539 while ( (c=**s) >= '0' && c <= '9')
541 val = val*10 + c - '0';
547 static char *fileMatchStr (struct recKeys *reckeys, struct recordGroup *rGroup,
551 static char dstBuf[2048];
553 const char *s = spec;
554 static const char **w;
559 while (*s == ' ' || *s == '\t')
566 int attrSet, attrUse;
570 attrSet = atois (&s);
573 logf (LOG_WARN, "Missing , in match criteria %s in group %s",
574 spec, rGroup->groupName ? rGroup->groupName : "none");
578 attrUse = atois (&s);
579 w = searchRecordKey (reckeys, attrSet, attrUse);
584 for (i = 0; i<32; i++)
589 logf (LOG_WARN, "Missing ) in match criteria %s in group %s",
590 spec, rGroup->groupName ? rGroup->groupName : "none");
595 for (i = 0; i<32; i++)
596 if (matchFlag[i] && w[i])
608 logf (LOG_WARN, "Record in file %s didn't contain match"
609 " fields in (%d,%d)", fname, attrSet, attrUse);
617 const char *spec_src = NULL;
618 const char *s1 = ++s;
619 while (*s1 && *s1 != ' ' && *s1 != '\t')
625 memcpy (special, s, spec_len);
626 special[spec_len] = '\0';
629 if (!strcmp (special, "group"))
630 spec_src = rGroup->groupName;
631 else if (!strcmp (special, "database"))
632 spec_src = rGroup->databaseName;
633 else if (!strcmp (special, "filename"))
635 else if (!strcmp (special, "type"))
636 spec_src = rGroup->recordType;
641 strcpy (dst, spec_src);
642 dst += strlen (spec_src);
645 else if (*s == '\"' || *s == '\'')
647 int stopMarker = *s++;
651 while (*s && *s != stopMarker)
654 tmpString[i++] = *s++;
659 strcpy (dst, tmpString);
660 dst += strlen (tmpString);
664 logf (LOG_WARN, "Syntax error in match criteria %s in group %s",
665 spec, rGroup->groupName ? rGroup->groupName : "none");
672 logf (LOG_WARN, "No match criteria for record %s in group %s",
673 fname, rGroup->groupName ? rGroup->groupName : "none");
679 static int recordExtract (SYSNO *sysno, const char *fname,
680 struct recordGroup *rGroup, int deleteFlag,
685 struct recExtractCtrl extractCtrl;
695 extractCtrl.subType = "";
696 extractCtrl.init = wordInit;
697 extractCtrl.add = addRecordKeyAny;
699 reckeys.buf_used = 0;
700 extractCtrl.readf = file_read;
701 r = (*recType->extract)(&extractCtrl);
705 logf (LOG_WARN, "Couldn't extract file %s, code %d", fname, r);
710 /* perform match if sysno not known and if match criteria is specified */
717 if (rGroup->recordId && *rGroup->recordId)
721 matchStr = fileMatchStr (&reckeys, rGroup, fname,
725 rinfo = dict_lookup (matchDict, matchStr);
727 memcpy (sysno, rinfo+1, sizeof(*sysno));
731 logf (LOG_WARN, "Record not inserted");
742 logf (LOG_LOG, "? %s", fname);
745 logf (LOG_LOG, "add %s %s", rGroup->recordType, fname);
746 rec = rec_new (records);
751 dict_insert (matchDict, matchStr, sizeof(*sysno), sysno);
753 flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
759 struct recKeys delkeys;
761 rec = rec_get (records, *sysno);
763 delkeys.buf_used = rec->size[recInfo_delKeys];
764 delkeys.buf = rec->info[recInfo_delKeys];
765 flushRecordKeys (*sysno, 0, &delkeys, rec->info[recInfo_databaseName]);
768 if (!delkeys.buf_used)
770 logf (LOG_WARN, "cannot delete %s - no delete keys", fname);
775 logf (LOG_LOG, "delete %s %s", rGroup->recordType, fname);
778 dict_insert (matchDict, matchStr, sizeof(sysnoz), &sysnoz);
779 rec_del (records, &rec);
785 if (!delkeys.buf_used)
787 logf (LOG_WARN, "cannot update %s - no delete keys",
792 logf (LOG_LOG, "update %s %s", rGroup->recordType,
794 flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
799 xfree (rec->info[recInfo_fileType]);
800 rec->info[recInfo_fileType] =
801 rec_strdup (rGroup->recordType, &rec->size[recInfo_fileType]);
803 xfree (rec->info[recInfo_filename]);
804 rec->info[recInfo_filename] =
805 rec_strdup (fname, &rec->size[recInfo_filename]);
807 xfree (rec->info[recInfo_delKeys]);
808 if (reckeys.buf_used > 0 && rGroup->flagStoreKeys == 1)
810 rec->info[recInfo_delKeys] = xmalloc (reckeys.buf_used);
811 rec->size[recInfo_delKeys] = reckeys.buf_used;
812 memcpy (rec->info[recInfo_delKeys], reckeys.buf,
813 rec->size[recInfo_delKeys]);
817 rec->info[recInfo_delKeys] = NULL;
818 rec->size[recInfo_delKeys] = 0;
821 xfree (rec->info[recInfo_storeData]);
822 if (rGroup->flagStoreData == 1)
824 rec->size[recInfo_storeData] = file_noread;
825 rec->info[recInfo_storeData] = xmalloc (file_noread);
826 if (file_noread < FILE_READ_BUFSIZE)
827 memcpy (rec->info[recInfo_storeData], file_buf, file_noread);
830 if (lseek (fd, 0L, SEEK_SET) < 0)
832 logf (LOG_ERRNO|LOG_FATAL, "seek to 0 in %s", fname);
835 if (read (fd, rec->info[recInfo_storeData], file_noread)
838 logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
846 rec->info[recInfo_storeData] = NULL;
847 rec->size[recInfo_storeData] = 0;
849 xfree (rec->info[recInfo_databaseName]);
850 rec->info[recInfo_databaseName] =
851 rec_strdup (rGroup->databaseName, &rec->size[recInfo_databaseName]);
853 rec_put (records, &rec);
857 int fileExtract (SYSNO *sysno, const char *fname,
858 const struct recordGroup *rGroupP, int deleteFlag)
866 struct recordGroup rGroupM;
867 struct recordGroup *rGroup = &rGroupM;
869 memcpy (rGroup, rGroupP, sizeof(*rGroupP));
871 if (!rGroup->groupName || !*rGroup->groupName)
874 sprintf (gprefix, "%s.", rGroup->groupName);
876 logf (LOG_DEBUG, "fileExtract %s", fname);
878 /* determine file extension */
879 for (i = strlen(fname); --i >= 0; )
885 else if (fname[i] == '.')
887 strcpy (ext, fname+i+1);
890 /* determine file type - depending on extension */
891 if (!rGroup->recordType)
893 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
894 if (!(rGroup->recordType = res_get (common_resource, ext_res)))
896 sprintf (ext_res, "%srecordType", gprefix);
897 if (!(rGroup->recordType = res_get (common_resource, ext_res)))
899 logf (LOG_LOG, "? %s", fname);
904 if (!rGroup->recordType)
906 logf (LOG_LOG, "? record %s", fname);
909 if (!(recType = recType_byName (rGroup->recordType, subType)))
911 logf (LOG_WARN, "No such record type: %s", rGroup->recordType);
915 /* determine match criteria */
916 if (!rGroup->recordId)
918 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
919 rGroup->recordId = res_get (common_resource, ext_res);
922 /* determine database name */
923 if (!rGroup->databaseName)
925 sprintf (ext_res, "%sdatabase.%s", gprefix, ext);
926 if (!(rGroup->databaseName = res_get (common_resource, ext_res)))
928 sprintf (ext_res, "%sdatabase", gprefix);
929 rGroup->databaseName = res_get (common_resource, ext_res);
932 if (!rGroup->databaseName)
933 rGroup->databaseName = "Default";
935 if (rGroup->flagStoreData == -1)
938 sprintf (ext_res, "%sstoreData.%s", gprefix, ext);
939 if (!(sval = res_get (common_resource, ext_res)))
941 sprintf (ext_res, "%sstoreData", gprefix);
942 sval = res_get (common_resource, ext_res);
945 rGroup->flagStoreData = atoi (sval);
947 if (rGroup->flagStoreData == -1)
948 rGroup->flagStoreData = 0;
950 if (rGroup->flagStoreKeys == -1)
954 sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
955 if (!(sval = res_get (common_resource, ext_res)))
957 sprintf (ext_res, "%sstoreKeys", gprefix);
958 sval = res_get (common_resource, ext_res);
961 rGroup->flagStoreKeys = atoi (sval);
963 if (rGroup->flagStoreKeys == -1)
964 rGroup->flagStoreKeys = 0;
966 if (sysno && deleteFlag)
970 if ((fd = open (fname, O_RDONLY)) == -1)
972 logf (LOG_WARN|LOG_ERRNO, "open %s", fname);
976 file_read_start (fd);
977 recordExtract (sysno, fname, rGroup, deleteFlag, fd, recType, subType);