2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.34 1995-11-28 09:09:38 adam
8 * Zebra config renamed.
9 * Use setting 'recordId' to identify record now.
10 * Bug fix in recindex.c: rec_release_blocks was invokeded even
11 * though the blocks were already released.
12 * File traversal properly deletes records when needed.
14 * Revision 1.33 1995/11/27 09:56:20 adam
15 * Record info elements better enumerated. Internal store of records.
17 * Revision 1.32 1995/11/25 10:24:05 adam
18 * More record fields - they are enumerated now.
19 * New options: flagStoreData flagStoreKey.
21 * Revision 1.31 1995/11/24 11:31:35 adam
22 * Commands add & del read filenames from stdin if source directory is
24 * Match criteria supports 'constant' strings.
26 * Revision 1.30 1995/11/22 17:19:16 adam
27 * Record management uses the bfile system.
29 * Revision 1.29 1995/11/21 15:01:14 adam
30 * New general match criteria implemented.
31 * New feature: document groups.
33 * Revision 1.28 1995/11/21 09:20:30 adam
34 * Yet more work on record match.
36 * Revision 1.27 1995/11/20 16:59:45 adam
37 * New update method: the 'old' keys are saved for each records.
39 * Revision 1.26 1995/11/20 11:56:24 adam
40 * Work on new traversal.
42 * Revision 1.25 1995/11/16 15:34:54 adam
43 * Uses new record management system in both indexer and server.
45 * Revision 1.24 1995/11/15 19:13:08 adam
46 * Work on record management.
48 * Revision 1.23 1995/10/27 14:00:10 adam
49 * Implemented detection of database availability.
51 * Revision 1.22 1995/10/17 18:02:07 adam
52 * New feature: databases. Implemented as prefix to words in dictionary.
54 * Revision 1.21 1995/10/10 12:24:38 adam
55 * Temporary sort files are compressed.
57 * Revision 1.20 1995/10/06 13:52:05 adam
58 * Bug fixes. Handler may abort further scanning.
60 * Revision 1.19 1995/10/04 12:55:16 adam
61 * Bug fix in ranked search. Use=Any keys inserted.
63 * Revision 1.18 1995/10/04 09:37:08 quinn
66 * Revision 1.17 1995/10/03 14:28:57 adam
67 * Buffered read in extract works.
69 * Revision 1.16 1995/10/03 14:28:45 adam
70 * Work on more effecient read handler in extract.
72 * Revision 1.15 1995/10/02 15:42:53 adam
73 * Extract uses file descriptors instead of FILE pointers.
75 * Revision 1.14 1995/10/02 15:29:13 adam
76 * More logging in file_extract.
78 * Revision 1.13 1995/09/29 14:01:39 adam
81 * Revision 1.12 1995/09/28 14:22:56 adam
82 * Sort uses smaller temporary files.
84 * Revision 1.11 1995/09/28 12:10:31 adam
85 * Bug fixes. Field prefix used in queries.
87 * Revision 1.10 1995/09/28 09:19:41 adam
88 * xfree/xmalloc used everywhere.
89 * Extract/retrieve method seems to work for text records.
91 * Revision 1.9 1995/09/27 12:22:28 adam
92 * More work on extract in record control.
93 * Field name is not in isam keys but in prefix in dictionary words.
95 * Revision 1.8 1995/09/14 07:48:22 adam
96 * Record control management.
98 * Revision 1.7 1995/09/11 13:09:32 adam
99 * More work on relevance feedback.
101 * Revision 1.6 1995/09/08 14:52:27 adam
102 * Minor changes. Dictionary is lower case now.
104 * Revision 1.5 1995/09/06 16:11:16 adam
105 * Option: only one word key per file.
107 * Revision 1.4 1995/09/05 15:28:39 adam
108 * More work on search engine.
110 * Revision 1.3 1995/09/04 12:33:41 adam
111 * Various cleanup. YAZ util used instead.
113 * Revision 1.2 1995/09/04 09:10:34 adam
114 * More work on index add/del/update.
115 * Merge sort implemented.
116 * Initial work on z39 server.
118 * Revision 1.1 1995/09/01 14:06:35 adam
119 * Split of work into more files.
128 #include <alexutil.h>
132 #include "recindex.h"
134 static Dict matchDict;
136 static Records records = NULL;
138 static char **key_buf;
139 static size_t ptr_top;
141 static size_t key_buf_used;
142 static int key_file_no;
144 static int records_inserted = 0;
145 static int records_updated = 0;
146 static int records_deleted = 0;
148 #define MATCH_DICT "match"
150 void key_open (int mem)
154 key_buf = xmalloc (mem);
155 ptr_top = mem/sizeof(char*);
161 if (!(matchDict = dict_open (MATCH_DICT, 20, 1)))
163 logf (LOG_FATAL, "dict_open fail of %s", MATCH_DICT);
167 records = rec_open (1);
176 void encode_key_init (struct encode_info *i)
182 char *encode_key_int (int d, char *bp)
191 else if (d <= 4194303)
193 *bp++ = 128 + (d>>16);
194 *bp++ = (d>>8) & 255;
199 *bp++ = 192 + (d>>24);
200 *bp++ = (d>>16) & 255;
201 *bp++ = (d>>8) & 255;
207 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
212 while ((*bp++ = *k++))
214 memcpy (&key, k+1, sizeof(struct it_key));
215 bp = encode_key_int ( (key.sysno - i->sysno) * 2 + *k, bp);
216 if (i->sysno != key.sysno)
218 i->sysno = key.sysno;
221 bp = encode_key_int (key.seqno - i->seqno, bp);
222 i->seqno = key.seqno;
223 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
225 logf (LOG_FATAL|LOG_ERRNO, "fwrite");
230 void key_flush (void)
235 struct encode_info encode_info;
241 logf (LOG_LOG, "sorting section %d", key_file_no);
242 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_qsort_compare);
243 sprintf (out_fname, TEMP_FNAME, key_file_no);
245 if (!(outf = fopen (out_fname, "w")))
247 logf (LOG_FATAL|LOG_ERRNO, "fopen (4) %s", out_fname);
250 logf (LOG_LOG, "writing section %d", key_file_no);
251 prevcp = cp = key_buf[ptr_top-ptr_i];
253 encode_key_init (&encode_info);
254 encode_key_write (cp, &encode_info, outf);
257 cp = key_buf[ptr_top-ptr_i];
258 if (strcmp (cp, prevcp))
260 encode_key_init (&encode_info);
261 encode_key_write (cp, &encode_info, outf);
265 encode_key_write (cp + strlen(cp), &encode_info, outf);
269 logf (LOG_FATAL|LOG_ERRNO, "fclose %s", out_fname);
272 logf (LOG_LOG, "finished section %d", key_file_no);
281 rec_close (&records);
282 dict_close (matchDict);
284 logf (LOG_LOG, "Records inserted %6d", records_inserted);
285 logf (LOG_LOG, "Records updated %6d", records_updated);
286 logf (LOG_LOG, "Records deleted %6d", records_deleted);
290 static void wordInit (RecWord *p)
294 p->which = Word_String;
303 static void addRecordKey (const RecWord *p)
310 if (reckeys.buf_used+1024 > reckeys.buf_max)
314 b = malloc (reckeys.buf_max += 65000);
315 if (reckeys.buf_used > 0)
316 memcpy (b, reckeys.buf, reckeys.buf_used);
320 dst = reckeys.buf + reckeys.buf_used;
324 attrSet = p->attrSet;
325 memcpy (dst, &attrSet, sizeof(attrSet));
326 dst += sizeof(attrSet);
328 attrUse = p->attrUse;
329 memcpy (dst, &attrUse, sizeof(attrUse));
330 dst += sizeof(attrUse);
332 for (i = 0; p->u.string[i]; i++)
333 *dst++ = p->u.string[i];
336 memcpy (dst, &p->seqno, sizeof(p->seqno));
337 dst += sizeof(p->seqno);
343 reckeys.buf_used = dst - reckeys.buf;
346 static void flushRecordKeys (SYSNO sysno, int cmd, struct recKeys *reckeys,
347 const char *databaseName)
350 while (off < reckeys->buf_used)
352 const char *src = reckeys->buf + off;
357 memcpy (&attrSet, src, sizeof(attrSet));
358 src += sizeof(attrSet);
360 memcpy (&attrUse, src, sizeof(attrUse));
361 src += sizeof(attrUse);
363 if (key_buf_used + 1024 > (ptr_top-ptr_i)*sizeof(char*))
366 key_buf[ptr_top-ptr_i] = (char*)key_buf + key_buf_used;
367 key_buf_used += index_word_prefix ((char*)key_buf + key_buf_used,
368 attrSet, attrUse, databaseName);
370 ((char*)key_buf) [key_buf_used++] = index_char_cvt (*src++);
372 ((char*)key_buf) [key_buf_used++] = '\0';
374 ((char*) key_buf)[key_buf_used++] = cmd;
376 memcpy (&key.seqno, src, sizeof(key.seqno));
377 src += sizeof(key.seqno);
379 memcpy ((char*)key_buf + key_buf_used, &key, sizeof(key));
380 key_buf_used += sizeof(key);
381 off = src - reckeys->buf;
383 assert (off == reckeys->buf_used);
386 static const char **searchRecordKey (struct recKeys *reckeys,
387 int attrSetS, int attrUseS)
389 static const char *ws[32];
394 for (i = 0; i<32; i++)
397 while (off < reckeys->buf_used)
399 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);
415 memcpy (&seqno, src, sizeof(seqno));
416 src += sizeof(seqno);
419 logf (LOG_LOG, "(%d,%d) %d %s", attrSet, attrUse, seqno, wstart);
421 if (attrUseS == attrUse && attrSetS == attrSet)
428 woff = seqno - startSeq;
429 if (woff >= 0 && woff < 31)
433 off = src - reckeys->buf;
435 assert (off == reckeys->buf_used);
439 static void addRecordKeyAny (const RecWord *p)
441 if (p->attrSet != 1 || p->attrUse != 1016)
445 memcpy (&w, p, sizeof(w));
453 #define FILE_READ_BUFSIZE 4096
455 static char *file_buf;
456 static int file_offset;
457 static int file_bufsize;
458 static int file_noread;
460 static void file_read_start (int fd)
463 file_buf = xmalloc (FILE_READ_BUFSIZE);
464 file_bufsize = read (fd, file_buf, FILE_READ_BUFSIZE);
468 static void file_read_stop (int fd)
474 static int file_read (int fd, char *buf, size_t count)
476 int l = file_bufsize - file_offset;
482 memcpy (buf, file_buf + file_offset, l);
484 if (count > file_bufsize)
486 if ((r = read (fd, buf + l, count)) == -1)
488 logf (LOG_FATAL|LOG_ERRNO, "read");
496 file_bufsize = r = read (fd, file_buf, FILE_READ_BUFSIZE);
499 logf (LOG_FATAL|LOG_ERRNO, "read");
505 memcpy (buf + l, file_buf, r);
506 file_noread += (l+r);
512 memcpy (buf + l, file_buf, count - l);
513 file_noread += count;
517 memcpy (buf, file_buf + file_offset, count);
518 file_offset += count;
519 file_noread += count;
523 static int atois (const char **s)
526 while ( (c=**s) >= '0' && c <= '9')
528 val = val*10 + c - '0';
534 static char *fileMatchStr (struct recKeys *reckeys, struct recordGroup *rGroup,
538 static char dstBuf[2048];
540 const char *s = spec;
541 static const char **w;
546 while (*s == ' ' || *s == '\t')
553 int attrSet, attrUse;
557 attrSet = atois (&s);
560 logf (LOG_WARN, "Missing , in match criteria %s in group %s",
561 spec, rGroup->groupName ? rGroup->groupName : "none");
565 attrUse = atois (&s);
566 w = searchRecordKey (reckeys, attrSet, attrUse);
571 for (i = 0; i<32; i++)
576 logf (LOG_WARN, "Missing ) in match criteria %s in group %s",
577 spec, rGroup->groupName ? rGroup->groupName : "none");
582 for (i = 0; i<32; i++)
583 if (matchFlag[i] && w[i])
595 logf (LOG_WARN, "Record in file %s didn't contain match"
596 " fields in (%d,%d)", fname, attrSet, attrUse);
604 const char *spec_src = NULL;
605 const char *s1 = ++s;
606 while (*s1 && *s1 != ' ' && *s1 != '\t')
612 memcpy (special, s, spec_len);
613 special[spec_len] = '\0';
616 if (strcmp (special, "group"))
617 spec_src = rGroup->groupName;
618 else if (strcmp (special, "database"))
619 spec_src = rGroup->databaseName;
620 else if (strcmp (special, "filename"))
622 else if (strcmp (special, "type"))
623 spec_src = rGroup->recordType;
628 strcpy (dst, spec_src);
629 dst += strlen (spec_src);
632 else if (*s == '\"' || *s == '\'')
634 int stopMarker = *s++;
638 while (*s && *s != stopMarker)
646 strcpy (dst, tmpString);
647 dst += strlen (tmpString);
651 logf (LOG_WARN, "Syntax error in match criteria %s in group %s",
652 spec, rGroup->groupName ? rGroup->groupName : "none");
659 logf (LOG_WARN, "No match criteria for record %s in group %s",
660 fname, rGroup->groupName ? rGroup->groupName : "none");
666 static int recordExtract (SYSNO *sysno, const char *fname,
667 struct recordGroup *rGroup, int deleteFlag,
671 struct recExtractCtrl extractCtrl;
681 extractCtrl.subType = "";
682 extractCtrl.init = wordInit;
683 extractCtrl.add = addRecordKeyAny;
685 reckeys.buf_used = 0;
686 extractCtrl.readf = file_read;
687 r = (*recType->extract)(&extractCtrl);
691 logf (LOG_WARN, "Couldn't extract file %s, code %d", fname, r);
696 /* perform match if sysno not known and if match criteria is specified */
703 if (rGroup->recordId && *rGroup->recordId)
707 matchStr = fileMatchStr (&reckeys, rGroup, fname,
711 rinfo = dict_lookup (matchDict, matchStr);
713 memcpy (sysno, rinfo+1, sizeof(*sysno));
717 logf (LOG_WARN, "Record not inserted");
728 logf (LOG_LOG, "? record %s", fname);
731 logf (LOG_LOG, "add %s record %s", rGroup->recordType, fname);
732 rec = rec_new (records);
736 dict_insert (matchDict, matchStr, sizeof(*sysno), sysno);
737 flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
743 struct recKeys delkeys;
745 rec = rec_get (records, *sysno);
747 delkeys.buf_used = rec->size[recInfo_delKeys];
748 delkeys.buf = rec->info[recInfo_delKeys];
749 flushRecordKeys (*sysno, 0, &delkeys, rec->info[recInfo_databaseName]);
752 if (!delkeys.buf_used)
754 logf (LOG_WARN, "cannot delete %s - no delete keys",
758 logf (LOG_LOG, "delete %s record %s", rGroup->recordType,
761 rec_del (records, &rec);
766 if (!delkeys.buf_used)
768 logf (LOG_WARN, "cannot update %s - no delete keys",
773 logf (LOG_LOG, "update %s record %s", rGroup->recordType,
775 flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
780 free (rec->info[recInfo_fileType]);
781 rec->info[recInfo_fileType] =
782 rec_strdup (rGroup->recordType, &rec->size[recInfo_fileType]);
784 free (rec->info[recInfo_filename]);
785 rec->info[recInfo_filename] =
786 rec_strdup (fname, &rec->size[recInfo_filename]);
788 free (rec->info[recInfo_delKeys]);
789 if (reckeys.buf_used > 0 && rGroup->flagStoreKeys == 1)
791 rec->info[recInfo_delKeys] = malloc (reckeys.buf_used);
792 rec->size[recInfo_delKeys] = reckeys.buf_used;
793 memcpy (rec->info[recInfo_delKeys], reckeys.buf,
794 rec->size[recInfo_delKeys]);
798 rec->info[recInfo_delKeys] = NULL;
799 rec->size[recInfo_delKeys] = 0;
802 free (rec->info[recInfo_storeData]);
803 if (rGroup->flagStoreData == 1)
805 rec->size[recInfo_storeData] = file_noread;
806 rec->info[recInfo_storeData] = malloc (file_noread);
807 if (file_noread < FILE_READ_BUFSIZE)
808 memcpy (rec->info[recInfo_storeData], file_buf, file_noread);
811 if (lseek (fd, 0L, SEEK_SET) < 0)
813 logf (LOG_ERRNO|LOG_FATAL, "seek to 0 in %s", fname);
816 if (read (fd, rec->info[recInfo_storeData], file_noread)
819 logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
827 rec->info[recInfo_storeData] = NULL;
828 rec->size[recInfo_storeData] = 0;
830 free (rec->info[recInfo_databaseName]);
831 rec->info[recInfo_databaseName] =
832 rec_strdup (rGroup->databaseName, &rec->size[recInfo_databaseName]);
834 rec_put (records, &rec);
838 int fileExtract (SYSNO *sysno, const char *fname,
839 const struct recordGroup *rGroupP, int deleteFlag)
846 struct recordGroup rGroupM;
847 struct recordGroup *rGroup = &rGroupM;
849 memcpy (rGroup, rGroupP, sizeof(*rGroupP));
851 if (!rGroup->groupName || !*rGroup->groupName)
854 sprintf (gprefix, "%s.", rGroup->groupName);
856 logf (LOG_DEBUG, "fileExtract %s", fname);
858 /* determine file extension */
859 for (i = strlen(fname); --i >= 0; )
865 else if (fname[i] == '.')
867 strcpy (ext, fname+i+1);
870 /* determine file type - depending on extension */
871 if (!rGroup->recordType)
873 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
874 if (!(rGroup->recordType = res_get (common_resource, ext_res)))
876 sprintf (ext_res, "%srecordType", gprefix);
877 if (!(rGroup->recordType = res_get (common_resource, ext_res)))
879 logf (LOG_LOG, "? record %s", fname);
884 if (!rGroup->recordType)
886 logf (LOG_LOG, "? record %s", fname);
889 if (!(recType = recType_byName (rGroup->recordType)))
891 logf (LOG_WARN, "No such record type: %s", rGroup->recordType);
895 /* determine match criteria */
896 if (!rGroup->recordId)
898 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
899 rGroup->recordId = res_get (common_resource, ext_res);
902 /* determine database name */
903 if (!rGroup->databaseName)
905 sprintf (ext_res, "%sdatabase.%s", gprefix, ext);
906 if (!(rGroup->databaseName = res_get (common_resource, ext_res)))
908 sprintf (ext_res, "%sdatabase", gprefix);
909 rGroup->databaseName = res_get (common_resource, ext_res);
912 if (!rGroup->databaseName)
913 rGroup->databaseName = "Default";
915 if (rGroup->flagStoreData == -1)
918 sprintf (ext_res, "%sstoreData.%s", gprefix, ext);
919 if (!(sval = res_get (common_resource, ext_res)))
921 sprintf (ext_res, "%sstoreData", gprefix);
922 sval = res_get (common_resource, ext_res);
925 rGroup->flagStoreData = atoi (sval);
927 if (rGroup->flagStoreData == -1)
928 rGroup->flagStoreData = 0;
930 if (rGroup->flagStoreKeys == -1)
934 sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
935 if (!(sval = res_get (common_resource, ext_res)))
937 sprintf (ext_res, "%sstoreKeys", gprefix);
938 sval = res_get (common_resource, ext_res);
941 rGroup->flagStoreKeys = atoi (sval);
943 if (rGroup->flagStoreKeys == -1)
944 rGroup->flagStoreKeys = 0;
946 if (sysno && deleteFlag)
950 if ((fd = open (fname, O_RDONLY)) == -1)
952 logf (LOG_WARN|LOG_ERRNO, "open %s", fname);
956 file_read_start (fd);
957 recordExtract (sysno, fname, rGroup, deleteFlag, fd, recType);