2 * Copyright (C) 1994-1995, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.35 1995-11-28 14:26:21 adam
8 * Bug fix: recordId with constant wasn't right.
9 * Bug fix: recordId dictionary entry wasn't deleted when needed.
11 * Revision 1.34 1995/11/28 09:09:38 adam
12 * Zebra config renamed.
13 * Use setting 'recordId' to identify record now.
14 * Bug fix in recindex.c: rec_release_blocks was invokeded even
15 * though the blocks were already released.
16 * File traversal properly deletes records when needed.
18 * Revision 1.33 1995/11/27 09:56:20 adam
19 * Record info elements better enumerated. Internal store of records.
21 * Revision 1.32 1995/11/25 10:24:05 adam
22 * More record fields - they are enumerated now.
23 * New options: flagStoreData flagStoreKey.
25 * Revision 1.31 1995/11/24 11:31:35 adam
26 * Commands add & del read filenames from stdin if source directory is
28 * Match criteria supports 'constant' strings.
30 * Revision 1.30 1995/11/22 17:19:16 adam
31 * Record management uses the bfile system.
33 * Revision 1.29 1995/11/21 15:01:14 adam
34 * New general match criteria implemented.
35 * New feature: document groups.
37 * Revision 1.28 1995/11/21 09:20:30 adam
38 * Yet more work on record match.
40 * Revision 1.27 1995/11/20 16:59:45 adam
41 * New update method: the 'old' keys are saved for each records.
43 * Revision 1.26 1995/11/20 11:56:24 adam
44 * Work on new traversal.
46 * Revision 1.25 1995/11/16 15:34:54 adam
47 * Uses new record management system in both indexer and server.
49 * Revision 1.24 1995/11/15 19:13:08 adam
50 * Work on record management.
52 * Revision 1.23 1995/10/27 14:00:10 adam
53 * Implemented detection of database availability.
55 * Revision 1.22 1995/10/17 18:02:07 adam
56 * New feature: databases. Implemented as prefix to words in dictionary.
58 * Revision 1.21 1995/10/10 12:24:38 adam
59 * Temporary sort files are compressed.
61 * Revision 1.20 1995/10/06 13:52:05 adam
62 * Bug fixes. Handler may abort further scanning.
64 * Revision 1.19 1995/10/04 12:55:16 adam
65 * Bug fix in ranked search. Use=Any keys inserted.
67 * Revision 1.18 1995/10/04 09:37:08 quinn
70 * Revision 1.17 1995/10/03 14:28:57 adam
71 * Buffered read in extract works.
73 * Revision 1.16 1995/10/03 14:28:45 adam
74 * Work on more effecient read handler in extract.
76 * Revision 1.15 1995/10/02 15:42:53 adam
77 * Extract uses file descriptors instead of FILE pointers.
79 * Revision 1.14 1995/10/02 15:29:13 adam
80 * More logging in file_extract.
82 * Revision 1.13 1995/09/29 14:01:39 adam
85 * Revision 1.12 1995/09/28 14:22:56 adam
86 * Sort uses smaller temporary files.
88 * Revision 1.11 1995/09/28 12:10:31 adam
89 * Bug fixes. Field prefix used in queries.
91 * Revision 1.10 1995/09/28 09:19:41 adam
92 * xfree/xmalloc used everywhere.
93 * Extract/retrieve method seems to work for text records.
95 * Revision 1.9 1995/09/27 12:22:28 adam
96 * More work on extract in record control.
97 * Field name is not in isam keys but in prefix in dictionary words.
99 * Revision 1.8 1995/09/14 07:48:22 adam
100 * Record control management.
102 * Revision 1.7 1995/09/11 13:09:32 adam
103 * More work on relevance feedback.
105 * Revision 1.6 1995/09/08 14:52:27 adam
106 * Minor changes. Dictionary is lower case now.
108 * Revision 1.5 1995/09/06 16:11:16 adam
109 * Option: only one word key per file.
111 * Revision 1.4 1995/09/05 15:28:39 adam
112 * More work on search engine.
114 * Revision 1.3 1995/09/04 12:33:41 adam
115 * Various cleanup. YAZ util used instead.
117 * Revision 1.2 1995/09/04 09:10:34 adam
118 * More work on index add/del/update.
119 * Merge sort implemented.
120 * Initial work on z39 server.
122 * Revision 1.1 1995/09/01 14:06:35 adam
123 * Split of work into more files.
132 #include <alexutil.h>
136 #include "recindex.h"
138 static Dict matchDict;
140 static Records records = NULL;
142 static char **key_buf;
143 static size_t ptr_top;
145 static size_t key_buf_used;
146 static int key_file_no;
148 static int records_inserted = 0;
149 static int records_updated = 0;
150 static int records_deleted = 0;
152 #define MATCH_DICT "match"
154 void key_open (int mem)
158 key_buf = xmalloc (mem);
159 ptr_top = mem/sizeof(char*);
165 if (!(matchDict = dict_open (MATCH_DICT, 20, 1)))
167 logf (LOG_FATAL, "dict_open fail of %s", MATCH_DICT);
171 records = rec_open (1);
180 void encode_key_init (struct encode_info *i)
186 char *encode_key_int (int d, char *bp)
195 else if (d <= 4194303)
197 *bp++ = 128 + (d>>16);
198 *bp++ = (d>>8) & 255;
203 *bp++ = 192 + (d>>24);
204 *bp++ = (d>>16) & 255;
205 *bp++ = (d>>8) & 255;
211 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
216 while ((*bp++ = *k++))
218 memcpy (&key, k+1, sizeof(struct it_key));
219 bp = encode_key_int ( (key.sysno - i->sysno) * 2 + *k, bp);
220 if (i->sysno != key.sysno)
222 i->sysno = key.sysno;
225 bp = encode_key_int (key.seqno - i->seqno, bp);
226 i->seqno = key.seqno;
227 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
229 logf (LOG_FATAL|LOG_ERRNO, "fwrite");
234 void key_flush (void)
239 struct encode_info encode_info;
245 logf (LOG_LOG, "sorting section %d", key_file_no);
246 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_qsort_compare);
247 sprintf (out_fname, TEMP_FNAME, key_file_no);
249 if (!(outf = fopen (out_fname, "w")))
251 logf (LOG_FATAL|LOG_ERRNO, "fopen (4) %s", out_fname);
254 logf (LOG_LOG, "writing section %d", key_file_no);
255 prevcp = cp = key_buf[ptr_top-ptr_i];
257 encode_key_init (&encode_info);
258 encode_key_write (cp, &encode_info, outf);
261 cp = key_buf[ptr_top-ptr_i];
262 if (strcmp (cp, prevcp))
264 encode_key_init (&encode_info);
265 encode_key_write (cp, &encode_info, outf);
269 encode_key_write (cp + strlen(cp), &encode_info, outf);
273 logf (LOG_FATAL|LOG_ERRNO, "fclose %s", out_fname);
276 logf (LOG_LOG, "finished section %d", key_file_no);
285 rec_close (&records);
286 dict_close (matchDict);
288 logf (LOG_LOG, "Records inserted %6d", records_inserted);
289 logf (LOG_LOG, "Records updated %6d", records_updated);
290 logf (LOG_LOG, "Records deleted %6d", records_deleted);
294 static void wordInit (RecWord *p)
298 p->which = Word_String;
307 static void addRecordKey (const RecWord *p)
314 if (reckeys.buf_used+1024 > reckeys.buf_max)
318 b = malloc (reckeys.buf_max += 65000);
319 if (reckeys.buf_used > 0)
320 memcpy (b, reckeys.buf, reckeys.buf_used);
324 dst = reckeys.buf + reckeys.buf_used;
328 attrSet = p->attrSet;
329 memcpy (dst, &attrSet, sizeof(attrSet));
330 dst += sizeof(attrSet);
332 attrUse = p->attrUse;
333 memcpy (dst, &attrUse, sizeof(attrUse));
334 dst += sizeof(attrUse);
336 for (i = 0; p->u.string[i]; i++)
337 *dst++ = p->u.string[i];
340 memcpy (dst, &p->seqno, sizeof(p->seqno));
341 dst += sizeof(p->seqno);
347 reckeys.buf_used = dst - reckeys.buf;
350 static void flushRecordKeys (SYSNO sysno, int cmd, struct recKeys *reckeys,
351 const char *databaseName)
354 while (off < reckeys->buf_used)
356 const char *src = reckeys->buf + off;
361 memcpy (&attrSet, src, sizeof(attrSet));
362 src += sizeof(attrSet);
364 memcpy (&attrUse, src, sizeof(attrUse));
365 src += sizeof(attrUse);
367 if (key_buf_used + 1024 > (ptr_top-ptr_i)*sizeof(char*))
370 key_buf[ptr_top-ptr_i] = (char*)key_buf + key_buf_used;
371 key_buf_used += index_word_prefix ((char*)key_buf + key_buf_used,
372 attrSet, attrUse, databaseName);
374 ((char*)key_buf) [key_buf_used++] = index_char_cvt (*src++);
376 ((char*)key_buf) [key_buf_used++] = '\0';
378 ((char*) key_buf)[key_buf_used++] = cmd;
380 memcpy (&key.seqno, src, sizeof(key.seqno));
381 src += sizeof(key.seqno);
383 memcpy ((char*)key_buf + key_buf_used, &key, sizeof(key));
384 key_buf_used += sizeof(key);
385 off = src - reckeys->buf;
387 assert (off == reckeys->buf_used);
390 static const char **searchRecordKey (struct recKeys *reckeys,
391 int attrSetS, int attrUseS)
393 static const char *ws[32];
398 for (i = 0; i<32; i++)
401 while (off < reckeys->buf_used)
403 const char *src = reckeys->buf + off;
409 memcpy (&attrSet, src, sizeof(attrSet));
410 src += sizeof(attrSet);
412 memcpy (&attrUse, src, sizeof(attrUse));
413 src += sizeof(attrUse);
419 memcpy (&seqno, src, sizeof(seqno));
420 src += sizeof(seqno);
423 logf (LOG_LOG, "(%d,%d) %d %s", attrSet, attrUse, seqno, wstart);
425 if (attrUseS == attrUse && attrSetS == attrSet)
432 woff = seqno - startSeq;
433 if (woff >= 0 && woff < 31)
437 off = src - reckeys->buf;
439 assert (off == reckeys->buf_used);
443 static void addRecordKeyAny (const RecWord *p)
445 if (p->attrSet != 1 || p->attrUse != 1016)
449 memcpy (&w, p, sizeof(w));
457 #define FILE_READ_BUFSIZE 4096
459 static char *file_buf;
460 static int file_offset;
461 static int file_bufsize;
462 static int file_noread;
464 static void file_read_start (int fd)
467 file_buf = xmalloc (FILE_READ_BUFSIZE);
468 file_bufsize = read (fd, file_buf, FILE_READ_BUFSIZE);
472 static void file_read_stop (int fd)
478 static int file_read (int fd, char *buf, size_t count)
480 int l = file_bufsize - file_offset;
486 memcpy (buf, file_buf + file_offset, l);
488 if (count > file_bufsize)
490 if ((r = read (fd, buf + l, count)) == -1)
492 logf (LOG_FATAL|LOG_ERRNO, "read");
500 file_bufsize = r = read (fd, file_buf, FILE_READ_BUFSIZE);
503 logf (LOG_FATAL|LOG_ERRNO, "read");
509 memcpy (buf + l, file_buf, r);
510 file_noread += (l+r);
516 memcpy (buf + l, file_buf, count - l);
517 file_noread += count;
521 memcpy (buf, file_buf + file_offset, count);
522 file_offset += count;
523 file_noread += count;
527 static int atois (const char **s)
530 while ( (c=**s) >= '0' && c <= '9')
532 val = val*10 + c - '0';
538 static char *fileMatchStr (struct recKeys *reckeys, struct recordGroup *rGroup,
542 static char dstBuf[2048];
544 const char *s = spec;
545 static const char **w;
550 while (*s == ' ' || *s == '\t')
557 int attrSet, attrUse;
561 attrSet = atois (&s);
564 logf (LOG_WARN, "Missing , in match criteria %s in group %s",
565 spec, rGroup->groupName ? rGroup->groupName : "none");
569 attrUse = atois (&s);
570 w = searchRecordKey (reckeys, attrSet, attrUse);
575 for (i = 0; i<32; i++)
580 logf (LOG_WARN, "Missing ) in match criteria %s in group %s",
581 spec, rGroup->groupName ? rGroup->groupName : "none");
586 for (i = 0; i<32; i++)
587 if (matchFlag[i] && w[i])
599 logf (LOG_WARN, "Record in file %s didn't contain match"
600 " fields in (%d,%d)", fname, attrSet, attrUse);
608 const char *spec_src = NULL;
609 const char *s1 = ++s;
610 while (*s1 && *s1 != ' ' && *s1 != '\t')
616 memcpy (special, s, spec_len);
617 special[spec_len] = '\0';
620 if (!strcmp (special, "group"))
621 spec_src = rGroup->groupName;
622 else if (!strcmp (special, "database"))
623 spec_src = rGroup->databaseName;
624 else if (!strcmp (special, "filename"))
626 else if (!strcmp (special, "type"))
627 spec_src = rGroup->recordType;
632 strcpy (dst, spec_src);
633 dst += strlen (spec_src);
636 else if (*s == '\"' || *s == '\'')
638 int stopMarker = *s++;
642 while (*s && *s != stopMarker)
645 tmpString[i++] = *s++;
650 strcpy (dst, tmpString);
651 dst += strlen (tmpString);
655 logf (LOG_WARN, "Syntax error in match criteria %s in group %s",
656 spec, rGroup->groupName ? rGroup->groupName : "none");
663 logf (LOG_WARN, "No match criteria for record %s in group %s",
664 fname, rGroup->groupName ? rGroup->groupName : "none");
670 static int recordExtract (SYSNO *sysno, const char *fname,
671 struct recordGroup *rGroup, int deleteFlag,
675 struct recExtractCtrl extractCtrl;
685 extractCtrl.subType = "";
686 extractCtrl.init = wordInit;
687 extractCtrl.add = addRecordKeyAny;
689 reckeys.buf_used = 0;
690 extractCtrl.readf = file_read;
691 r = (*recType->extract)(&extractCtrl);
695 logf (LOG_WARN, "Couldn't extract file %s, code %d", fname, r);
700 /* perform match if sysno not known and if match criteria is specified */
707 if (rGroup->recordId && *rGroup->recordId)
711 matchStr = fileMatchStr (&reckeys, rGroup, fname,
715 rinfo = dict_lookup (matchDict, matchStr);
717 memcpy (sysno, rinfo+1, sizeof(*sysno));
721 logf (LOG_WARN, "Record not inserted");
732 logf (LOG_LOG, "? %s", fname);
735 logf (LOG_LOG, "add %s %s", rGroup->recordType, fname);
736 rec = rec_new (records);
741 dict_insert (matchDict, matchStr, sizeof(*sysno), sysno);
743 flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
749 struct recKeys delkeys;
751 rec = rec_get (records, *sysno);
753 delkeys.buf_used = rec->size[recInfo_delKeys];
754 delkeys.buf = rec->info[recInfo_delKeys];
755 flushRecordKeys (*sysno, 0, &delkeys, rec->info[recInfo_databaseName]);
758 if (!delkeys.buf_used)
760 logf (LOG_WARN, "cannot delete %s - no delete keys", fname);
765 logf (LOG_LOG, "delete %s %s", rGroup->recordType, fname);
768 dict_insert (matchDict, matchStr, sizeof(sysnoz), &sysnoz);
769 rec_del (records, &rec);
775 if (!delkeys.buf_used)
777 logf (LOG_WARN, "cannot update %s - no delete keys",
782 logf (LOG_LOG, "update %s %s", rGroup->recordType,
784 flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
789 free (rec->info[recInfo_fileType]);
790 rec->info[recInfo_fileType] =
791 rec_strdup (rGroup->recordType, &rec->size[recInfo_fileType]);
793 free (rec->info[recInfo_filename]);
794 rec->info[recInfo_filename] =
795 rec_strdup (fname, &rec->size[recInfo_filename]);
797 free (rec->info[recInfo_delKeys]);
798 if (reckeys.buf_used > 0 && rGroup->flagStoreKeys == 1)
800 rec->info[recInfo_delKeys] = malloc (reckeys.buf_used);
801 rec->size[recInfo_delKeys] = reckeys.buf_used;
802 memcpy (rec->info[recInfo_delKeys], reckeys.buf,
803 rec->size[recInfo_delKeys]);
807 rec->info[recInfo_delKeys] = NULL;
808 rec->size[recInfo_delKeys] = 0;
811 free (rec->info[recInfo_storeData]);
812 if (rGroup->flagStoreData == 1)
814 rec->size[recInfo_storeData] = file_noread;
815 rec->info[recInfo_storeData] = malloc (file_noread);
816 if (file_noread < FILE_READ_BUFSIZE)
817 memcpy (rec->info[recInfo_storeData], file_buf, file_noread);
820 if (lseek (fd, 0L, SEEK_SET) < 0)
822 logf (LOG_ERRNO|LOG_FATAL, "seek to 0 in %s", fname);
825 if (read (fd, rec->info[recInfo_storeData], file_noread)
828 logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
836 rec->info[recInfo_storeData] = NULL;
837 rec->size[recInfo_storeData] = 0;
839 free (rec->info[recInfo_databaseName]);
840 rec->info[recInfo_databaseName] =
841 rec_strdup (rGroup->databaseName, &rec->size[recInfo_databaseName]);
843 rec_put (records, &rec);
847 int fileExtract (SYSNO *sysno, const char *fname,
848 const struct recordGroup *rGroupP, int deleteFlag)
855 struct recordGroup rGroupM;
856 struct recordGroup *rGroup = &rGroupM;
858 memcpy (rGroup, rGroupP, sizeof(*rGroupP));
860 if (!rGroup->groupName || !*rGroup->groupName)
863 sprintf (gprefix, "%s.", rGroup->groupName);
865 logf (LOG_DEBUG, "fileExtract %s", fname);
867 /* determine file extension */
868 for (i = strlen(fname); --i >= 0; )
874 else if (fname[i] == '.')
876 strcpy (ext, fname+i+1);
879 /* determine file type - depending on extension */
880 if (!rGroup->recordType)
882 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
883 if (!(rGroup->recordType = res_get (common_resource, ext_res)))
885 sprintf (ext_res, "%srecordType", gprefix);
886 if (!(rGroup->recordType = res_get (common_resource, ext_res)))
888 logf (LOG_LOG, "? %s", fname);
893 if (!rGroup->recordType)
895 logf (LOG_LOG, "? record %s", fname);
898 if (!(recType = recType_byName (rGroup->recordType)))
900 logf (LOG_WARN, "No such record type: %s", rGroup->recordType);
904 /* determine match criteria */
905 if (!rGroup->recordId)
907 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
908 rGroup->recordId = res_get (common_resource, ext_res);
911 /* determine database name */
912 if (!rGroup->databaseName)
914 sprintf (ext_res, "%sdatabase.%s", gprefix, ext);
915 if (!(rGroup->databaseName = res_get (common_resource, ext_res)))
917 sprintf (ext_res, "%sdatabase", gprefix);
918 rGroup->databaseName = res_get (common_resource, ext_res);
921 if (!rGroup->databaseName)
922 rGroup->databaseName = "Default";
924 if (rGroup->flagStoreData == -1)
927 sprintf (ext_res, "%sstoreData.%s", gprefix, ext);
928 if (!(sval = res_get (common_resource, ext_res)))
930 sprintf (ext_res, "%sstoreData", gprefix);
931 sval = res_get (common_resource, ext_res);
934 rGroup->flagStoreData = atoi (sval);
936 if (rGroup->flagStoreData == -1)
937 rGroup->flagStoreData = 0;
939 if (rGroup->flagStoreKeys == -1)
943 sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
944 if (!(sval = res_get (common_resource, ext_res)))
946 sprintf (ext_res, "%sstoreKeys", gprefix);
947 sval = res_get (common_resource, ext_res);
950 rGroup->flagStoreKeys = atoi (sval);
952 if (rGroup->flagStoreKeys == -1)
953 rGroup->flagStoreKeys = 0;
955 if (sysno && deleteFlag)
959 if ((fd = open (fname, O_RDONLY)) == -1)
961 logf (LOG_WARN|LOG_ERRNO, "open %s", fname);
965 file_read_start (fd);
966 recordExtract (sysno, fname, rGroup, deleteFlag, fd, recType);