2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.36 1995-11-30 08:34:29 adam
8 * Started work on commit facility.
9 * Changed a few malloc/free to xmalloc/xfree.
11 * Revision 1.35 1995/11/28 14:26:21 adam
12 * Bug fix: recordId with constant wasn't right.
13 * Bug fix: recordId dictionary entry wasn't deleted when needed.
15 * Revision 1.34 1995/11/28 09:09:38 adam
16 * Zebra config renamed.
17 * Use setting 'recordId' to identify record now.
18 * Bug fix in recindex.c: rec_release_blocks was invokeded even
19 * though the blocks were already released.
20 * File traversal properly deletes records when needed.
22 * Revision 1.33 1995/11/27 09:56:20 adam
23 * Record info elements better enumerated. Internal store of records.
25 * Revision 1.32 1995/11/25 10:24:05 adam
26 * More record fields - they are enumerated now.
27 * New options: flagStoreData flagStoreKey.
29 * Revision 1.31 1995/11/24 11:31:35 adam
30 * Commands add & del read filenames from stdin if source directory is
32 * Match criteria supports 'constant' strings.
34 * Revision 1.30 1995/11/22 17:19:16 adam
35 * Record management uses the bfile system.
37 * Revision 1.29 1995/11/21 15:01:14 adam
38 * New general match criteria implemented.
39 * New feature: document groups.
41 * Revision 1.28 1995/11/21 09:20:30 adam
42 * Yet more work on record match.
44 * Revision 1.27 1995/11/20 16:59:45 adam
45 * New update method: the 'old' keys are saved for each records.
47 * Revision 1.26 1995/11/20 11:56:24 adam
48 * Work on new traversal.
50 * Revision 1.25 1995/11/16 15:34:54 adam
51 * Uses new record management system in both indexer and server.
53 * Revision 1.24 1995/11/15 19:13:08 adam
54 * Work on record management.
56 * Revision 1.23 1995/10/27 14:00:10 adam
57 * Implemented detection of database availability.
59 * Revision 1.22 1995/10/17 18:02:07 adam
60 * New feature: databases. Implemented as prefix to words in dictionary.
62 * Revision 1.21 1995/10/10 12:24:38 adam
63 * Temporary sort files are compressed.
65 * Revision 1.20 1995/10/06 13:52:05 adam
66 * Bug fixes. Handler may abort further scanning.
68 * Revision 1.19 1995/10/04 12:55:16 adam
69 * Bug fix in ranked search. Use=Any keys inserted.
71 * Revision 1.18 1995/10/04 09:37:08 quinn
74 * Revision 1.17 1995/10/03 14:28:57 adam
75 * Buffered read in extract works.
77 * Revision 1.16 1995/10/03 14:28:45 adam
78 * Work on more effecient read handler in extract.
80 * Revision 1.15 1995/10/02 15:42:53 adam
81 * Extract uses file descriptors instead of FILE pointers.
83 * Revision 1.14 1995/10/02 15:29:13 adam
84 * More logging in file_extract.
86 * Revision 1.13 1995/09/29 14:01:39 adam
89 * Revision 1.12 1995/09/28 14:22:56 adam
90 * Sort uses smaller temporary files.
92 * Revision 1.11 1995/09/28 12:10:31 adam
93 * Bug fixes. Field prefix used in queries.
95 * Revision 1.10 1995/09/28 09:19:41 adam
96 * xfree/xmalloc used everywhere.
97 * Extract/retrieve method seems to work for text records.
99 * Revision 1.9 1995/09/27 12:22:28 adam
100 * More work on extract in record control.
101 * Field name is not in isam keys but in prefix in dictionary words.
103 * Revision 1.8 1995/09/14 07:48:22 adam
104 * Record control management.
106 * Revision 1.7 1995/09/11 13:09:32 adam
107 * More work on relevance feedback.
109 * Revision 1.6 1995/09/08 14:52:27 adam
110 * Minor changes. Dictionary is lower case now.
112 * Revision 1.5 1995/09/06 16:11:16 adam
113 * Option: only one word key per file.
115 * Revision 1.4 1995/09/05 15:28:39 adam
116 * More work on search engine.
118 * Revision 1.3 1995/09/04 12:33:41 adam
119 * Various cleanup. YAZ util used instead.
121 * Revision 1.2 1995/09/04 09:10:34 adam
122 * More work on index add/del/update.
123 * Merge sort implemented.
124 * Initial work on z39 server.
126 * Revision 1.1 1995/09/01 14:06:35 adam
127 * Split of work into more files.
136 #include <alexutil.h>
140 #include "recindex.h"
142 static Dict matchDict;
144 static Records records = NULL;
146 static char **key_buf;
147 static size_t ptr_top;
149 static size_t key_buf_used;
150 static int key_file_no;
152 static int records_inserted = 0;
153 static int records_updated = 0;
154 static int records_deleted = 0;
156 #define MATCH_DICT "match"
158 void key_open (int mem)
162 key_buf = xmalloc (mem);
163 ptr_top = mem/sizeof(char*);
169 if (!(matchDict = dict_open (MATCH_DICT, 20, 1)))
171 logf (LOG_FATAL, "dict_open fail of %s", MATCH_DICT);
175 records = rec_open (1);
184 void encode_key_init (struct encode_info *i)
190 char *encode_key_int (int d, char *bp)
199 else if (d <= 4194303)
201 *bp++ = 128 + (d>>16);
202 *bp++ = (d>>8) & 255;
207 *bp++ = 192 + (d>>24);
208 *bp++ = (d>>16) & 255;
209 *bp++ = (d>>8) & 255;
215 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
220 while ((*bp++ = *k++))
222 memcpy (&key, k+1, sizeof(struct it_key));
223 bp = encode_key_int ( (key.sysno - i->sysno) * 2 + *k, bp);
224 if (i->sysno != key.sysno)
226 i->sysno = key.sysno;
229 bp = encode_key_int (key.seqno - i->seqno, bp);
230 i->seqno = key.seqno;
231 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
233 logf (LOG_FATAL|LOG_ERRNO, "fwrite");
238 void key_flush (void)
243 struct encode_info encode_info;
249 logf (LOG_LOG, "sorting section %d", key_file_no);
250 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_qsort_compare);
251 sprintf (out_fname, TEMP_FNAME, key_file_no);
253 if (!(outf = fopen (out_fname, "w")))
255 logf (LOG_FATAL|LOG_ERRNO, "fopen (4) %s", out_fname);
258 logf (LOG_LOG, "writing section %d", key_file_no);
259 prevcp = cp = key_buf[ptr_top-ptr_i];
261 encode_key_init (&encode_info);
262 encode_key_write (cp, &encode_info, outf);
265 cp = key_buf[ptr_top-ptr_i];
266 if (strcmp (cp, prevcp))
268 encode_key_init (&encode_info);
269 encode_key_write (cp, &encode_info, outf);
273 encode_key_write (cp + strlen(cp), &encode_info, outf);
277 logf (LOG_FATAL|LOG_ERRNO, "fclose %s", out_fname);
280 logf (LOG_LOG, "finished section %d", key_file_no);
289 rec_close (&records);
290 dict_close (matchDict);
292 logf (LOG_LOG, "Records inserted %6d", records_inserted);
293 logf (LOG_LOG, "Records updated %6d", records_updated);
294 logf (LOG_LOG, "Records deleted %6d", records_deleted);
298 static void wordInit (RecWord *p)
302 p->which = Word_String;
311 static void addRecordKey (const RecWord *p)
318 if (reckeys.buf_used+1024 > reckeys.buf_max)
322 b = xmalloc (reckeys.buf_max += 65000);
323 if (reckeys.buf_used > 0)
324 memcpy (b, reckeys.buf, reckeys.buf_used);
328 dst = reckeys.buf + reckeys.buf_used;
332 attrSet = p->attrSet;
333 memcpy (dst, &attrSet, sizeof(attrSet));
334 dst += sizeof(attrSet);
336 attrUse = p->attrUse;
337 memcpy (dst, &attrUse, sizeof(attrUse));
338 dst += sizeof(attrUse);
340 for (i = 0; p->u.string[i]; i++)
341 *dst++ = p->u.string[i];
344 memcpy (dst, &p->seqno, sizeof(p->seqno));
345 dst += sizeof(p->seqno);
351 reckeys.buf_used = dst - reckeys.buf;
354 static void flushRecordKeys (SYSNO sysno, int cmd, struct recKeys *reckeys,
355 const char *databaseName)
358 while (off < reckeys->buf_used)
360 const char *src = reckeys->buf + off;
365 memcpy (&attrSet, src, sizeof(attrSet));
366 src += sizeof(attrSet);
368 memcpy (&attrUse, src, sizeof(attrUse));
369 src += sizeof(attrUse);
371 if (key_buf_used + 1024 > (ptr_top-ptr_i)*sizeof(char*))
374 key_buf[ptr_top-ptr_i] = (char*)key_buf + key_buf_used;
375 key_buf_used += index_word_prefix ((char*)key_buf + key_buf_used,
376 attrSet, attrUse, databaseName);
378 ((char*)key_buf) [key_buf_used++] = index_char_cvt (*src++);
380 ((char*)key_buf) [key_buf_used++] = '\0';
382 ((char*) key_buf)[key_buf_used++] = cmd;
384 memcpy (&key.seqno, src, sizeof(key.seqno));
385 src += sizeof(key.seqno);
387 memcpy ((char*)key_buf + key_buf_used, &key, sizeof(key));
388 key_buf_used += sizeof(key);
389 off = src - reckeys->buf;
391 assert (off == reckeys->buf_used);
394 static const char **searchRecordKey (struct recKeys *reckeys,
395 int attrSetS, int attrUseS)
397 static const char *ws[32];
402 for (i = 0; i<32; i++)
405 while (off < reckeys->buf_used)
407 const char *src = reckeys->buf + off;
413 memcpy (&attrSet, src, sizeof(attrSet));
414 src += sizeof(attrSet);
416 memcpy (&attrUse, src, sizeof(attrUse));
417 src += sizeof(attrUse);
423 memcpy (&seqno, src, sizeof(seqno));
424 src += sizeof(seqno);
427 logf (LOG_LOG, "(%d,%d) %d %s", attrSet, attrUse, seqno, wstart);
429 if (attrUseS == attrUse && attrSetS == attrSet)
436 woff = seqno - startSeq;
437 if (woff >= 0 && woff < 31)
441 off = src - reckeys->buf;
443 assert (off == reckeys->buf_used);
447 static void addRecordKeyAny (const RecWord *p)
449 if (p->attrSet != 1 || p->attrUse != 1016)
453 memcpy (&w, p, sizeof(w));
461 #define FILE_READ_BUFSIZE 4096
463 static char *file_buf;
464 static int file_offset;
465 static int file_bufsize;
466 static int file_noread;
468 static void file_read_start (int fd)
471 file_buf = xmalloc (FILE_READ_BUFSIZE);
472 file_bufsize = read (fd, file_buf, FILE_READ_BUFSIZE);
476 static void file_read_stop (int fd)
482 static int file_read (int fd, char *buf, size_t count)
484 int l = file_bufsize - file_offset;
490 memcpy (buf, file_buf + file_offset, l);
492 if (count > file_bufsize)
494 if ((r = read (fd, buf + l, count)) == -1)
496 logf (LOG_FATAL|LOG_ERRNO, "read");
504 file_bufsize = r = read (fd, file_buf, FILE_READ_BUFSIZE);
507 logf (LOG_FATAL|LOG_ERRNO, "read");
513 memcpy (buf + l, file_buf, r);
514 file_noread += (l+r);
520 memcpy (buf + l, file_buf, count - l);
521 file_noread += count;
525 memcpy (buf, file_buf + file_offset, count);
526 file_offset += count;
527 file_noread += count;
531 static int atois (const char **s)
534 while ( (c=**s) >= '0' && c <= '9')
536 val = val*10 + c - '0';
542 static char *fileMatchStr (struct recKeys *reckeys, struct recordGroup *rGroup,
546 static char dstBuf[2048];
548 const char *s = spec;
549 static const char **w;
554 while (*s == ' ' || *s == '\t')
561 int attrSet, attrUse;
565 attrSet = atois (&s);
568 logf (LOG_WARN, "Missing , in match criteria %s in group %s",
569 spec, rGroup->groupName ? rGroup->groupName : "none");
573 attrUse = atois (&s);
574 w = searchRecordKey (reckeys, attrSet, attrUse);
579 for (i = 0; i<32; i++)
584 logf (LOG_WARN, "Missing ) in match criteria %s in group %s",
585 spec, rGroup->groupName ? rGroup->groupName : "none");
590 for (i = 0; i<32; i++)
591 if (matchFlag[i] && w[i])
603 logf (LOG_WARN, "Record in file %s didn't contain match"
604 " fields in (%d,%d)", fname, attrSet, attrUse);
612 const char *spec_src = NULL;
613 const char *s1 = ++s;
614 while (*s1 && *s1 != ' ' && *s1 != '\t')
620 memcpy (special, s, spec_len);
621 special[spec_len] = '\0';
624 if (!strcmp (special, "group"))
625 spec_src = rGroup->groupName;
626 else if (!strcmp (special, "database"))
627 spec_src = rGroup->databaseName;
628 else if (!strcmp (special, "filename"))
630 else if (!strcmp (special, "type"))
631 spec_src = rGroup->recordType;
636 strcpy (dst, spec_src);
637 dst += strlen (spec_src);
640 else if (*s == '\"' || *s == '\'')
642 int stopMarker = *s++;
646 while (*s && *s != stopMarker)
649 tmpString[i++] = *s++;
654 strcpy (dst, tmpString);
655 dst += strlen (tmpString);
659 logf (LOG_WARN, "Syntax error in match criteria %s in group %s",
660 spec, rGroup->groupName ? rGroup->groupName : "none");
667 logf (LOG_WARN, "No match criteria for record %s in group %s",
668 fname, rGroup->groupName ? rGroup->groupName : "none");
674 static int recordExtract (SYSNO *sysno, const char *fname,
675 struct recordGroup *rGroup, int deleteFlag,
679 struct recExtractCtrl extractCtrl;
689 extractCtrl.subType = "";
690 extractCtrl.init = wordInit;
691 extractCtrl.add = addRecordKeyAny;
693 reckeys.buf_used = 0;
694 extractCtrl.readf = file_read;
695 r = (*recType->extract)(&extractCtrl);
699 logf (LOG_WARN, "Couldn't extract file %s, code %d", fname, r);
704 /* perform match if sysno not known and if match criteria is specified */
711 if (rGroup->recordId && *rGroup->recordId)
715 matchStr = fileMatchStr (&reckeys, rGroup, fname,
719 rinfo = dict_lookup (matchDict, matchStr);
721 memcpy (sysno, rinfo+1, sizeof(*sysno));
725 logf (LOG_WARN, "Record not inserted");
736 logf (LOG_LOG, "? %s", fname);
739 logf (LOG_LOG, "add %s %s", rGroup->recordType, fname);
740 rec = rec_new (records);
745 dict_insert (matchDict, matchStr, sizeof(*sysno), sysno);
747 flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
753 struct recKeys delkeys;
755 rec = rec_get (records, *sysno);
757 delkeys.buf_used = rec->size[recInfo_delKeys];
758 delkeys.buf = rec->info[recInfo_delKeys];
759 flushRecordKeys (*sysno, 0, &delkeys, rec->info[recInfo_databaseName]);
762 if (!delkeys.buf_used)
764 logf (LOG_WARN, "cannot delete %s - no delete keys", fname);
769 logf (LOG_LOG, "delete %s %s", rGroup->recordType, fname);
772 dict_insert (matchDict, matchStr, sizeof(sysnoz), &sysnoz);
773 rec_del (records, &rec);
779 if (!delkeys.buf_used)
781 logf (LOG_WARN, "cannot update %s - no delete keys",
786 logf (LOG_LOG, "update %s %s", rGroup->recordType,
788 flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
793 xfree (rec->info[recInfo_fileType]);
794 rec->info[recInfo_fileType] =
795 rec_strdup (rGroup->recordType, &rec->size[recInfo_fileType]);
797 xfree (rec->info[recInfo_filename]);
798 rec->info[recInfo_filename] =
799 rec_strdup (fname, &rec->size[recInfo_filename]);
801 xfree (rec->info[recInfo_delKeys]);
802 if (reckeys.buf_used > 0 && rGroup->flagStoreKeys == 1)
804 rec->info[recInfo_delKeys] = xmalloc (reckeys.buf_used);
805 rec->size[recInfo_delKeys] = reckeys.buf_used;
806 memcpy (rec->info[recInfo_delKeys], reckeys.buf,
807 rec->size[recInfo_delKeys]);
811 rec->info[recInfo_delKeys] = NULL;
812 rec->size[recInfo_delKeys] = 0;
815 xfree (rec->info[recInfo_storeData]);
816 if (rGroup->flagStoreData == 1)
818 rec->size[recInfo_storeData] = file_noread;
819 rec->info[recInfo_storeData] = xmalloc (file_noread);
820 if (file_noread < FILE_READ_BUFSIZE)
821 memcpy (rec->info[recInfo_storeData], file_buf, file_noread);
824 if (lseek (fd, 0L, SEEK_SET) < 0)
826 logf (LOG_ERRNO|LOG_FATAL, "seek to 0 in %s", fname);
829 if (read (fd, rec->info[recInfo_storeData], file_noread)
832 logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
840 rec->info[recInfo_storeData] = NULL;
841 rec->size[recInfo_storeData] = 0;
843 xfree (rec->info[recInfo_databaseName]);
844 rec->info[recInfo_databaseName] =
845 rec_strdup (rGroup->databaseName, &rec->size[recInfo_databaseName]);
847 rec_put (records, &rec);
851 int fileExtract (SYSNO *sysno, const char *fname,
852 const struct recordGroup *rGroupP, int deleteFlag)
859 struct recordGroup rGroupM;
860 struct recordGroup *rGroup = &rGroupM;
862 memcpy (rGroup, rGroupP, sizeof(*rGroupP));
864 if (!rGroup->groupName || !*rGroup->groupName)
867 sprintf (gprefix, "%s.", rGroup->groupName);
869 logf (LOG_DEBUG, "fileExtract %s", fname);
871 /* determine file extension */
872 for (i = strlen(fname); --i >= 0; )
878 else if (fname[i] == '.')
880 strcpy (ext, fname+i+1);
883 /* determine file type - depending on extension */
884 if (!rGroup->recordType)
886 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
887 if (!(rGroup->recordType = res_get (common_resource, ext_res)))
889 sprintf (ext_res, "%srecordType", gprefix);
890 if (!(rGroup->recordType = res_get (common_resource, ext_res)))
892 logf (LOG_LOG, "? %s", fname);
897 if (!rGroup->recordType)
899 logf (LOG_LOG, "? record %s", fname);
902 if (!(recType = recType_byName (rGroup->recordType)))
904 logf (LOG_WARN, "No such record type: %s", rGroup->recordType);
908 /* determine match criteria */
909 if (!rGroup->recordId)
911 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
912 rGroup->recordId = res_get (common_resource, ext_res);
915 /* determine database name */
916 if (!rGroup->databaseName)
918 sprintf (ext_res, "%sdatabase.%s", gprefix, ext);
919 if (!(rGroup->databaseName = res_get (common_resource, ext_res)))
921 sprintf (ext_res, "%sdatabase", gprefix);
922 rGroup->databaseName = res_get (common_resource, ext_res);
925 if (!rGroup->databaseName)
926 rGroup->databaseName = "Default";
928 if (rGroup->flagStoreData == -1)
931 sprintf (ext_res, "%sstoreData.%s", gprefix, ext);
932 if (!(sval = res_get (common_resource, ext_res)))
934 sprintf (ext_res, "%sstoreData", gprefix);
935 sval = res_get (common_resource, ext_res);
938 rGroup->flagStoreData = atoi (sval);
940 if (rGroup->flagStoreData == -1)
941 rGroup->flagStoreData = 0;
943 if (rGroup->flagStoreKeys == -1)
947 sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
948 if (!(sval = res_get (common_resource, ext_res)))
950 sprintf (ext_res, "%sstoreKeys", gprefix);
951 sval = res_get (common_resource, ext_res);
954 rGroup->flagStoreKeys = atoi (sval);
956 if (rGroup->flagStoreKeys == -1)
957 rGroup->flagStoreKeys = 0;
959 if (sysno && deleteFlag)
963 if ((fd = open (fname, O_RDONLY)) == -1)
965 logf (LOG_WARN|LOG_ERRNO, "open %s", fname);
969 file_read_start (fd);
970 recordExtract (sysno, fname, rGroup, deleteFlag, fd, recType);