2 * Copyright (C) 1994-1996, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.59 1997-01-17 11:31:46 adam
8 * Bug fix: complete phrase search didn't work.
10 * Revision 1.58 1996/12/23 15:30:45 adam
12 * Bug fix: result sets weren't deleted after server shut down.
14 * Revision 1.57 1996/11/11 13:38:02 adam
15 * Added proximity support in search.
17 * Revision 1.56 1996/11/08 11:10:32 adam
18 * Buffers used during file match got bigger.
19 * Compressed ISAM support everywhere.
20 * Bug fixes regarding masking characters in queries.
21 * Redesigned Regexp-2 queries.
23 * Revision 1.55 1996/11/04 14:07:44 adam
24 * Moved truncation code to trunc.c.
26 * Revision 1.54 1996/10/29 14:09:52 adam
27 * Use of cisam system - enabled if setting isamc is 1.
29 * Revision 1.53 1996/06/26 09:21:43 adam
30 * Bug fix: local attribute set wasn't obeyed in scan.
32 * Revision 1.52 1996/06/17 14:26:20 adam
33 * Function gen_regular_rel changed to handle negative numbers.
35 * Revision 1.51 1996/06/11 10:54:15 quinn
38 * Revision 1.50 1996/06/07 08:51:53 adam
39 * Bug fix: Character mapping was broken (introducued by last revision).
41 * Revision 1.49 1996/06/04 10:18:11 adam
42 * Search/scan uses character mapping module.
44 * Revision 1.48 1996/05/28 15:15:01 adam
45 * Bug fix: Didn't handle unknown database correctly.
47 * Revision 1.47 1996/05/15 18:36:28 adam
48 * Function trans_term transforms unsearchable characters to blanks.
50 * Revision 1.46 1996/05/15 11:57:56 adam
51 * Fixed bug introduced by set/field mapping in search operations.
53 * Revision 1.45 1996/05/14 11:34:00 adam
54 * Scan support in multiple registers/databases.
56 * Revision 1.44 1996/05/14 06:16:44 adam
57 * Compact use/set bytes used in search service.
59 * Revision 1.43 1996/05/09 09:54:43 adam
60 * Server supports maps from one logical attributes to a list of physical
62 * The extraction process doesn't make space consuming 'any' keys.
64 * Revision 1.42 1996/05/09 07:28:56 quinn
65 * Work towards phrases and multiple registers
67 * Revision 1.41 1996/03/20 09:36:43 adam
68 * Function dict_lookup_grep got extra parameter, init_pos, which marks
69 * from which position in pattern approximate pattern matching should occur.
70 * Approximate pattern matching is used in relevance=re-2.
72 * Revision 1.40 1996/02/02 13:44:44 adam
73 * The public dictionary functions simply use char instead of Dict_char
74 * to represent search strings. Dict_char is used internally only.
76 * Revision 1.39 1996/01/03 16:22:13 quinn
79 * Revision 1.38 1995/12/11 09:12:55 adam
80 * The rec_get function returns NULL if record doesn't exist - will
81 * happen in the server if the result set records have been deleted since
82 * the creation of the set (i.e. the search).
83 * The server saves a result temporarily if it is 'volatile', i.e. the
84 * set is register dependent.
86 * Revision 1.37 1995/12/06 15:05:28 adam
87 * More verbose in count_set.
89 * Revision 1.36 1995/12/06 12:41:27 adam
90 * New command 'stat' for the index program.
91 * Filenames can be read from stdin by specifying '-'.
92 * Bug fix/enhancement of the transformation from terms to regular
93 * expressons in the search engine.
95 * Revision 1.35 1995/11/27 09:29:00 adam
96 * Bug fixes regarding conversion to regular expressions.
98 * Revision 1.34 1995/11/16 17:00:56 adam
99 * Better logging of rpn query.
101 * Revision 1.33 1995/11/01 13:58:28 quinn
102 * Moving data1 to yaz/retrieval
104 * Revision 1.32 1995/10/27 14:00:11 adam
105 * Implemented detection of database availability.
107 * Revision 1.31 1995/10/17 18:02:10 adam
108 * New feature: databases. Implemented as prefix to words in dictionary.
110 * Revision 1.30 1995/10/16 09:32:38 adam
111 * More work on relational op.
113 * Revision 1.29 1995/10/13 16:01:49 adam
116 * Revision 1.28 1995/10/13 12:26:43 adam
117 * Optimization of truncation.
119 * Revision 1.27 1995/10/12 17:07:22 adam
122 * Revision 1.26 1995/10/12 12:40:54 adam
123 * Bug fixes in rpn_prox.
125 * Revision 1.25 1995/10/10 13:59:24 adam
126 * Function rset_open changed its wflag parameter to general flags.
128 * Revision 1.24 1995/10/09 16:18:37 adam
129 * Function dict_lookup_grep got extra client data parameter.
131 * Revision 1.23 1995/10/06 16:33:37 adam
132 * Use attribute mappings.
134 * Revision 1.22 1995/10/06 15:07:39 adam
135 * Structure 'local-number' handled.
137 * Revision 1.21 1995/10/06 13:52:06 adam
138 * Bug fixes. Handler may abort further scanning.
140 * Revision 1.20 1995/10/06 11:06:33 adam
141 * Scan entries include 'occurrences' now.
143 * Revision 1.19 1995/10/06 10:43:56 adam
144 * Scan added. 'occurrences' in scan entries not set yet.
146 * Revision 1.18 1995/10/04 16:57:20 adam
147 * Key input and merge sort in one pass.
149 * Revision 1.17 1995/10/04 12:55:17 adam
150 * Bug fix in ranked search. Use=Any keys inserted.
152 * Revision 1.16 1995/10/02 16:24:40 adam
153 * Use attribute actually used in search requests.
155 * Revision 1.15 1995/10/02 15:18:52 adam
156 * New member in recRetrieveCtrl: diagnostic.
158 * Revision 1.14 1995/09/28 12:10:32 adam
159 * Bug fixes. Field prefix used in queries.
161 * Revision 1.13 1995/09/18 14:17:50 adam
164 * Revision 1.12 1995/09/15 14:45:21 adam
166 * Work on truncation.
168 * Revision 1.11 1995/09/14 11:53:27 adam
169 * First work on regular expressions/truncations.
171 * Revision 1.10 1995/09/11 15:23:26 adam
172 * More work on relevance search.
174 * Revision 1.9 1995/09/11 13:09:35 adam
175 * More work on relevance feedback.
177 * Revision 1.8 1995/09/08 14:52:27 adam
178 * Minor changes. Dictionary is lower case now.
180 * Revision 1.7 1995/09/07 13:58:36 adam
181 * New parameter: result-set file descriptor (RSFD) to support multiple
182 * positions within the same result-set.
183 * Boolean operators: and, or, not implemented.
184 * Result-set references.
186 * Revision 1.6 1995/09/06 16:11:18 adam
187 * Option: only one word key per file.
189 * Revision 1.5 1995/09/06 10:33:04 adam
190 * More work on present. Some log messages removed.
192 * Revision 1.4 1995/09/05 15:28:40 adam
193 * More work on search engine.
195 * Revision 1.3 1995/09/04 15:20:22 adam
198 * Revision 1.2 1995/09/04 12:33:43 adam
199 * Various cleanup. YAZ util used instead.
201 * Revision 1.1 1995/09/04 09:10:40 adam
202 * More work on index add/del/update.
203 * Merge sort implemented.
204 * Initial work on z39 server.
213 #include "attribute.h"
225 Z_AttributesPlusTerm *zapt;
228 static int attr_find (AttrType *src, oid_value *attributeSetP)
230 while (src->major < src->zapt->num_attributes)
232 Z_AttributeElement *element;
234 element = src->zapt->attributeList[src->major];
235 if (src->type == *element->attributeType)
237 switch (element->which)
239 case Z_AttributeValue_numeric:
241 if (element->attributeSet && attributeSetP)
245 attrset = oid_getentbyoid (element->attributeSet);
246 *attributeSetP = attrset->value;
248 return *element->value.numeric;
250 case Z_AttributeValue_complex:
251 if (src->minor >= element->value.complex->num_list ||
252 element->value.complex->list[src->minor]->which !=
253 Z_StringOrNumeric_numeric)
256 if (element->attributeSet && attributeSetP)
260 attrset = oid_getentbyoid (element->attributeSet);
261 *attributeSetP = attrset->value;
263 return *element->value.complex->list[src->minor-1]->u.numeric;
273 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
293 static void add_isam_p (const char *info, struct grep_info *p)
295 if (p->isam_p_indx == p->isam_p_size)
297 ISAM_P *new_isam_p_buf;
302 p->isam_p_size = 2*p->isam_p_size + 100;
303 new_isam_p_buf = xmalloc (sizeof(*new_isam_p_buf) *
307 memcpy (new_isam_p_buf, p->isam_p_buf,
308 p->isam_p_indx * sizeof(*p->isam_p_buf));
309 xfree (p->isam_p_buf);
311 p->isam_p_buf = new_isam_p_buf;
314 new_term_no = xmalloc (sizeof(*new_term_no) *
318 memcpy (new_term_no, p->isam_p_buf,
319 p->isam_p_indx * sizeof(*p->term_no));
322 p->term_no = new_term_no;
325 assert (*info == sizeof(*p->isam_p_buf));
326 memcpy (p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
330 static int grep_handle (char *name, const char *info, void *p)
332 add_isam_p (info, p);
336 static int term_pre (char **src, const char *ct1, const char *ct2)
338 char *s1, *s0 = *src;
341 /* skip white space */
344 if (ct1 && strchr (ct1, *s0))
346 if (ct2 && strchr (ct2, *s0))
349 map = map_chrs_input (&s1, strlen(s1));
350 if (**map != *CHR_SPACE)
358 static int term_100 (char **src, char *dst, int space_split)
360 char *s0, *s1, **map;
363 if (!term_pre (src, NULL, NULL))
369 map = map_chrs_input (&s0, strlen(s0));
370 if (space_split && **map == *CHR_SPACE)
384 static int term_101 (char **src, char *dst, int space_split)
386 char *s0, *s1, **map;
389 if (!term_pre (src, "#", "#"))
403 map = map_chrs_input (&s0, strlen(s0));
404 if (space_split && **map == *CHR_SPACE)
420 static int term_103 (char **src, char *dst, int *errors, int space_split)
423 char *s0, *s1, **map;
425 if (!term_pre (src, "\\()[].*+?|", "("))
428 if (errors && *s0 == '+' && s0[1] && s0[2] == '+' && s0[3] &&
431 *errors = s0[1] - '0';
438 if (strchr ("\\()[].*+?|-", *s0))
443 map = map_chrs_input (&s0, strlen(s0));
444 if (**map == *CHR_SPACE)
459 static int term_102 (char **src, char *dst, int space_split)
461 return term_103 (src, dst, NULL, space_split);
464 /* gen_regular_rel - generate regular expression from relation
465 * val: border value (inclusive)
466 * islt: 1 if <=; 0 if >=.
468 static void gen_regular_rel (char *dst, int val, int islt)
475 logf (LOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
479 strcpy (dst, "(-[0-9]+|");
487 strcpy (dst, "([0-9]+|-");
488 dst_p = strlen (dst);
498 dst_p = strlen (dst);
499 sprintf (numstr, "%d", val);
500 for (w = strlen(numstr); --w >= 0; pos++)
519 strcpy (dst + dst_p, numstr);
520 dst_p = strlen(dst) - pos - 1;
548 for (i = 0; i<pos; i++)
561 for (i=1; i<pos; i++)
562 strcat (dst, "[0-9]?");
566 for (i = 0; i <= pos; i++)
567 strcat (dst, "[0-9]");
568 strcat (dst, "[0-9]*");
573 static int relational_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
576 oid_value attributeSet,
577 struct grep_info *grep_info,
585 attr_init (&relation, zapt, 2);
586 relation_value = attr_find (&relation, NULL);
588 switch (relation_value)
591 if (!term_100 (term_sub, term_dict, 1))
593 term_value = atoi (term_dict);
596 logf (LOG_DEBUG, "Relation <");
597 gen_regular_rel (term_dict + strlen(term_dict), term_value-1, 1);
600 if (!term_100 (term_sub, term_dict, 1))
602 term_value = atoi (term_dict);
605 logf (LOG_DEBUG, "Relation <=");
606 gen_regular_rel (term_dict + strlen(term_dict), term_value, 1);
609 if (!term_100 (term_sub, term_dict, 1))
611 term_value = atoi (term_dict);
614 logf (LOG_DEBUG, "Relation >=");
615 gen_regular_rel (term_dict + strlen(term_dict), term_value, 0);
618 if (!term_100 (term_sub, term_dict, 1))
620 term_value = atoi (term_dict);
623 logf (LOG_DEBUG, "Relation >");
624 gen_regular_rel (term_dict + strlen(term_dict), term_value+1, 0);
629 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_dict);
630 r = dict_lookup_grep (zi->dict, term_dict, 0, grep_info, max_pos,
633 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
634 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
638 static int field_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
639 char **term_sub, int regType,
640 oid_value attributeSet, struct grep_info *grep_info,
641 int num_bases, char **basenames, int space_split)
643 char term_dict[2*IT_MAX_WORD+2];
646 int truncation_value;
649 oid_value curAttributeSet = attributeSet;
652 attr_init (&use, zapt, 1);
653 use_value = attr_find (&use, &curAttributeSet);
654 logf (LOG_DEBUG, "field_term, use value %d", use_value);
655 attr_init (&truncation, zapt, 5);
656 truncation_value = attr_find (&truncation, NULL);
657 logf (LOG_DEBUG, "truncation value %d", truncation_value);
662 for (base_no = 0; base_no < num_bases; base_no++)
665 data1_local_attribute *local_attr;
666 int max_pos, prefix_len = 0;
669 attp = att_getentbyatt (curAttributeSet, use_value);
672 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
673 curAttributeSet, use_value);
677 if (zebTargetInfo_curDatabase (zi->zti, basenames[base_no]))
679 zi->errCode = 109; /* Database unavailable */
680 zi->errString = basenames[base_no];
683 for (local_attr = attp->local_attributes; local_attr;
684 local_attr = local_attr->next)
688 ord = zebTargetInfo_lookupSU (zi->zti, attp->attset_ordinal,
693 term_dict[prefix_len++] = '|';
695 term_dict[prefix_len++] = '(';
696 term_dict[prefix_len++] = 1;
697 term_dict[prefix_len++] = ord;
704 term_dict[prefix_len++] = ')';
705 term_dict[prefix_len++] = 1;
706 term_dict[prefix_len++] = regType;
707 term_dict[prefix_len] = '\0';
708 if (!relational_term (zi, zapt, &termp, term_dict,
709 attributeSet, grep_info, &max_pos))
712 logf (LOG_LOG, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
713 switch (truncation_value)
715 case -1: /* not specified */
716 case 100: /* do not truncate */
717 term_dict[j++] = '(';
718 logf (LOG_LOG, "termp=%s", termp);
719 if (!term_100 (&termp, term_dict + j, space_split))
721 logf (LOG_LOG, "aaaaaaaaaaaaargh");
724 strcat (term_dict, ")");
725 r = dict_lookup_grep (zi->dict, term_dict, 0, grep_info,
726 &max_pos, 0, grep_handle);
728 logf (LOG_WARN, "dict_lookup_grep err, trunc=none:%d", r);
730 case 1: /* right truncation */
731 term_dict[j++] = '(';
732 if (!term_100 (&termp, term_dict + j, space_split))
734 strcat (term_dict, ".*)");
735 dict_lookup_grep (zi->dict, term_dict, 0, grep_info,
736 &max_pos, 0, grep_handle);
738 case 2: /* left truncation */
739 case 3: /* left&right truncation */
742 case 101: /* process # in term */
743 term_dict[j++] = '(';
744 if (!term_101 (&termp, term_dict + j, space_split))
746 strcat (term_dict, ")");
747 r = dict_lookup_grep (zi->dict, term_dict, 0, grep_info,
748 &max_pos, 0, grep_handle);
750 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d", r);
752 case 102: /* Regexp-1 */
753 term_dict[j++] = '(';
754 if (!term_102 (&termp, term_dict + j, space_split))
756 strcat (term_dict, ")");
757 logf (LOG_DEBUG, "Regexp-1 tolerance=%d", r);
758 r = dict_lookup_grep (zi->dict, term_dict, 0, grep_info,
759 &max_pos, 0, grep_handle);
761 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
764 case 103: /* Regexp-1 */
766 term_dict[j++] = '(';
767 if (!term_103 (&termp, term_dict + j, &r, space_split))
769 strcat (term_dict, ")");
770 logf (LOG_DEBUG, "Regexp-2 tolerance=%d", r);
771 r = dict_lookup_grep (zi->dict, term_dict, r, grep_info,
772 &max_pos, 2, grep_handle);
774 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
781 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
785 static void trans_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
789 Z_Term *term = zapt->term;
791 sizez = term->u.general->len;
792 if (sizez > IT_MAX_WORD-1)
793 sizez = IT_MAX_WORD-1;
794 memcpy (termz, term->u.general->buf, sizez);
798 static void trans_scan_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
801 Z_Term *term = zapt->term;
803 char *cp = (char*) term->u.general->buf;
804 const char *cp_end = cp + term->u.general->len;
810 while ((len = (cp_end - cp)) > 0)
812 map = map_chrs_input (&cp, len);
813 if (**map == *CHR_SPACE)
821 for (src = *map; *src; src++)
827 static RSET rpn_search_APT_relevance (ZServerInfo *zi,
828 Z_AttributesPlusTerm *zapt,
829 oid_value attributeSet,
830 int num_bases, char **basenames)
832 rset_relevance_parms parms;
833 char termz[IT_MAX_WORD+1];
835 struct grep_info grep_info;
840 parms.key_size = sizeof(struct it_key);
841 parms.max_rec = 1000;
842 parms.cmp = key_compare_it;
844 parms.isc = zi->isamc;
847 if (zapt->term->which != Z_Term_general)
852 trans_term (zi, zapt, termz);
855 grep_info.term_no = 0;
857 grep_info.isam_p_indx = 0;
858 grep_info.isam_p_size = 0;
859 grep_info.isam_p_buf = NULL;
862 r = field_term (zi, zapt, &termp, 'w', attributeSet, &grep_info,
863 num_bases, basenames, 1);
867 for (; term_index < grep_info.isam_p_indx; term_index++)
868 grep_info.term_no[term_index] = parms.no_terms;
872 parms.term_no = grep_info.term_no;
873 parms.isam_positions = grep_info.isam_p_buf;
874 parms.no_isam_positions = grep_info.isam_p_indx;
875 if (grep_info.isam_p_indx > 0)
876 result = rset_create (rset_kind_relevance, &parms);
878 result = rset_create (rset_kind_null, NULL);
880 xfree(grep_info.term_no);
882 xfree (grep_info.isam_p_buf);
886 static RSET rpn_search_APT_cphrase (ZServerInfo *zi,
887 Z_AttributesPlusTerm *zapt,
888 oid_value attributeSet,
889 int num_bases, char **basenames)
891 char termz[IT_MAX_WORD+1];
892 struct grep_info grep_info;
897 if (zapt->term->which != Z_Term_general)
902 trans_term (zi, zapt, termz);
905 grep_info.term_no = 0;
907 grep_info.isam_p_indx = 0;
908 grep_info.isam_p_size = 0;
909 grep_info.isam_p_buf = NULL;
911 r = field_term (zi, zapt, &termp, 'p', attributeSet, &grep_info,
912 num_bases, basenames, 0);
913 result = rset_trunc (zi, grep_info.isam_p_buf, grep_info.isam_p_indx);
915 xfree(grep_info.term_no);
917 xfree (grep_info.isam_p_buf);
921 static RSET rpn_proximity (RSET rset1, RSET rset2, int ordered,
922 int exclusion, int relation, int distance)
927 struct it_key buf1, buf2;
930 rset_temp_parms parms;
932 rsfd1 = rset_open (rset1, RSETF_READ|RSETF_SORT_SYSNO);
933 more1 = rset_read (rset1, rsfd1, &buf1);
935 rsfd2 = rset_open (rset2, RSETF_READ|RSETF_SORT_SYSNO);
936 more2 = rset_read (rset2, rsfd2, &buf2);
938 parms.key_size = sizeof (struct it_key);
939 result = rset_create (rset_kind_temp, &parms);
940 rsfd_result = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
942 logf (LOG_DEBUG, "rpn_proximity excl=%d ord=%d rel=%d dis=%d",
943 exclusion, ordered, relation, distance);
944 while (more1 && more2)
946 int cmp = key_compare_it (&buf1, &buf2);
948 more1 = rset_read (rset1, rsfd1, &buf1);
950 more2 = rset_read (rset2, rsfd2, &buf2);
953 int sysno = buf1.sysno;
957 seqno[n++] = buf1.seqno;
958 while ((more1 = rset_read (rset1, rsfd1, &buf1)) &&
961 seqno[n++] = buf1.seqno;
964 for (i = 0; i<n; i++)
966 int diff = buf2.seqno - seqno[i];
967 int excl = exclusion;
968 if (!ordered && diff < 0)
977 if (diff <= distance)
981 if (diff == distance)
985 if (diff >= distance)
993 if (diff != distance)
998 rset_write (result, rsfd_result, &buf2);
1000 } while ((more2 = rset_read (rset2, rsfd2, &buf2)) &&
1001 sysno == buf2.sysno);
1004 rset_close (result, rsfd_result);
1005 rset_close (rset1, rsfd1);
1006 rset_close (rset2, rsfd2);
1010 static RSET rpn_prox (RSET *rset, int rset_no)
1015 struct it_key **buf;
1018 rset_temp_parms parms;
1020 rsfd = xmalloc (sizeof(*rsfd)*rset_no);
1021 more = xmalloc (sizeof(*more)*rset_no);
1022 buf = xmalloc (sizeof(*buf)*rset_no);
1024 for (i = 0; i<rset_no; i++)
1026 buf[i] = xmalloc (sizeof(**buf));
1027 rsfd[i] = rset_open (rset[i], RSETF_READ|RSETF_SORT_SYSNO);
1028 if (!(more[i] = rset_read (rset[i], rsfd[i], buf[i])))
1032 rset_close (rset[i], rsfd[i]);
1039 return rset_create (rset_kind_null, NULL);
1042 parms.key_size = sizeof (struct it_key);
1043 result = rset_create (rset_kind_temp, &parms);
1044 rsfd_result = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
1048 for (i = 1; i<rset_no; i++)
1057 cmp = key_compare_it (buf[i], buf[i-1]);
1060 more[i-1] = rset_read (rset[i-1], rsfd[i-1], buf[i-1]);
1065 if (buf[i-1]->seqno+1 != buf[i]->seqno)
1067 more[i-1] = rset_read (rset[i-1], rsfd[i-1], buf[i-1]);
1073 more[i] = rset_read (rset[i], rsfd[i], buf[i]);
1079 rset_write (result, rsfd_result, buf[0]);
1080 more[0] = rset_read (*rset, *rsfd, *buf);
1084 for (i = 0; i<rset_no; i++)
1086 rset_close (rset[i], rsfd[i]);
1089 rset_close (result, rsfd_result);
1096 static RSET rpn_search_APT_phrase (ZServerInfo *zi,
1097 Z_AttributesPlusTerm *zapt,
1098 oid_value attributeSet,
1099 int num_bases, char **basenames)
1101 char termz[IT_MAX_WORD+1];
1102 char *termp = termz;
1103 RSET rset[60], result;
1104 int i, r, rset_no = 0;
1105 struct grep_info grep_info;
1107 if (zapt->term->which != Z_Term_general)
1112 trans_term (zi, zapt, termz);
1115 grep_info.term_no = 0;
1117 grep_info.isam_p_size = 0;
1118 grep_info.isam_p_buf = NULL;
1122 grep_info.isam_p_indx = 0;
1123 r = field_term (zi, zapt, &termp, 'w', attributeSet, &grep_info,
1124 num_bases, basenames, 1);
1127 rset[rset_no] = rset_trunc (zi, grep_info.isam_p_buf,
1128 grep_info.isam_p_indx);
1129 assert (rset[rset_no]);
1130 if (++rset_no >= sizeof(rset)/sizeof(*rset))
1134 xfree(grep_info.term_no);
1136 xfree (grep_info.isam_p_buf);
1138 return rset_create (rset_kind_null, NULL);
1139 else if (rset_no == 1)
1141 result = rpn_prox (rset, rset_no);
1142 for (i = 0; i<rset_no; i++)
1143 rset_delete (rset[i]);
1147 static RSET rpn_search_APT_local (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1148 oid_value attributeSet)
1153 rset_temp_parms parms;
1154 char termz[IT_MAX_WORD+1];
1156 if (zapt->term->which != Z_Term_general)
1161 parms.key_size = sizeof (struct it_key);
1162 result = rset_create (rset_kind_temp, &parms);
1163 rsfd = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
1165 trans_term (zi, zapt, termz);
1167 key.sysno = atoi (termz);
1170 rset_write (result, rsfd, &key);
1171 rset_close (result, rsfd);
1175 static RSET rpn_search_APT (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1176 oid_value attributeSet,
1177 int num_bases, char **basenames)
1181 AttrType completeness;
1182 int relation_value, structure_value, completeness_value;
1184 attr_init (&relation, zapt, 2);
1185 attr_init (&structure, zapt, 4);
1186 attr_init (&completeness, zapt, 6);
1188 relation_value = attr_find (&relation, NULL);
1189 structure_value = attr_find (&structure, NULL);
1190 completeness_value = attr_find (&completeness, NULL);
1191 switch (structure_value)
1194 if (relation_value == 102) /* relevance relation */
1195 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1196 num_bases, basenames);
1197 if (completeness_value == 2 || completeness_value == 3)
1198 return rpn_search_APT_cphrase (zi, zapt, attributeSet,
1199 num_bases, basenames);
1200 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1201 num_bases, basenames);
1202 case 1: /* phrase */
1203 if (relation_value == 102) /* relevance relation */
1204 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1205 num_bases, basenames);
1206 if (completeness_value == 2 || completeness_value == 3)
1207 return rpn_search_APT_cphrase (zi, zapt, attributeSet,
1208 num_bases, basenames);
1209 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1210 num_bases, basenames);
1213 if (relation_value == 102) /* relevance relation */
1214 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1215 num_bases, basenames);
1216 if (completeness_value == 2 || completeness_value == 3)
1217 return rpn_search_APT_cphrase (zi, zapt, attributeSet,
1218 num_bases, basenames);
1219 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1220 num_bases, basenames);
1225 case 5: /* date - normalized */
1227 case 6: /* word list */
1228 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1229 num_bases, basenames);
1230 case 100: /* date - un-normalized */
1232 case 101: /* name - normalized */
1234 case 102: /* date - un-normalized */
1236 case 103: /* structure */
1240 case 105: /* free-form-text */
1241 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1242 num_bases, basenames);
1243 case 106: /* document-text */
1244 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1245 num_bases, basenames);
1246 case 107: /* local-number */
1247 return rpn_search_APT_local (zi, zapt, attributeSet);
1248 case 108: /* string */
1249 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1250 num_bases, basenames);
1251 case 109: /* numeric string */
1258 static RSET rpn_search_ref (ZServerInfo *zi, Z_ResultSetId *resultSetId)
1262 if (!(s = resultSetGet (zi, resultSetId)))
1263 return rset_create (rset_kind_null, NULL);
1267 static RSET rpn_search_structure (ZServerInfo *zi, Z_RPNStructure *zs,
1268 oid_value attributeSet,
1269 int num_bases, char **basenames)
1272 if (zs->which == Z_RPNStructure_complex)
1274 Z_Operator *zop = zs->u.complex->roperator;
1275 rset_bool_parms bool_parms;
1279 bool_parms.rset_l = rpn_search_structure (zi, zs->u.complex->s1,
1281 num_bases, basenames);
1282 if (bool_parms.rset_l == NULL)
1284 if (rset_is_ranked(bool_parms.rset_l))
1286 bool_parms.rset_r = rpn_search_structure (zi, zs->u.complex->s2,
1288 num_bases, basenames);
1289 if (bool_parms.rset_r == NULL)
1291 rset_delete (bool_parms.rset_l);
1294 if (rset_is_ranked(bool_parms.rset_r))
1296 bool_parms.key_size = sizeof(struct it_key);
1297 bool_parms.cmp = key_compare_it;
1301 case Z_Operator_and:
1302 r = rset_create (soft ? rset_kind_sand:rset_kind_and, &bool_parms);
1305 r = rset_create (soft ? rset_kind_sor:rset_kind_or, &bool_parms);
1307 case Z_Operator_and_not:
1308 r = rset_create (soft ? rset_kind_snot:rset_kind_not, &bool_parms);
1310 case Z_Operator_prox:
1311 if (zop->u.prox->which != Z_ProxCode_known)
1316 if (*zop->u.prox->proximityUnitCode != Z_ProxUnit_word)
1318 static char val[16];
1320 zi->errString = val;
1321 sprintf (val, "%d", *zop->u.prox->proximityUnitCode);
1324 r = rpn_proximity (bool_parms.rset_l, bool_parms.rset_r,
1325 *zop->u.prox->ordered,
1326 (!zop->u.prox->exclusion ? 0 :
1327 *zop->u.prox->exclusion),
1328 *zop->u.prox->relationType,
1329 *zop->u.prox->distance);
1336 else if (zs->which == Z_RPNStructure_simple)
1338 if (zs->u.simple->which == Z_Operand_APT)
1340 logf (LOG_DEBUG, "rpn_search_APT");
1341 r = rpn_search_APT (zi, zs->u.simple->u.attributesPlusTerm,
1342 attributeSet, num_bases, basenames);
1344 else if (zs->u.simple->which == Z_Operand_resultSetId)
1346 logf (LOG_DEBUG, "rpn_search_ref");
1347 r = rpn_search_ref (zi, zs->u.simple->u.resultSetId);
1363 void count_set_save (RSET *r, int *count)
1370 rset_temp_parms parms;
1372 logf (LOG_DEBUG, "count_set_save");
1374 parms.key_size = sizeof(struct it_key);
1375 w = rset_create (rset_kind_temp, &parms);
1376 wfd = rset_open (w, RSETF_WRITE|RSETF_SORT_SYSNO);
1377 rfd = rset_open (*r, RSETF_READ|RSETF_SORT_SYSNO);
1378 while (rset_read (*r, rfd, &key))
1380 if (key.sysno != psysno)
1383 rset_write (w, wfd, &key);
1389 rset_close (*r, rfd);
1391 rset_close (w, wfd);
1393 logf (LOG_DEBUG, "%d keys, %d distinct sysnos", kno, *count);
1396 static void count_set (RSET r, int *count)
1403 logf (LOG_DEBUG, "count_set");
1405 rfd = rset_open (r, RSETF_READ|RSETF_SORT_SYSNO);
1406 while (rset_read (r, rfd, &key))
1408 if (key.sysno != psysno)
1415 rset_close (r, rfd);
1416 logf (LOG_DEBUG, "%d keys, %d distinct sysnos", kno, *count);
1419 int rpn_search (ZServerInfo *zi,
1420 Z_RPNQuery *rpn, int num_bases, char **basenames,
1421 const char *setname, int *hits)
1425 oid_value attributeSet;
1427 dict_grep_cmap (zi->dict, map_chrs_input);
1431 zi->errString = NULL;
1433 attrset = oid_getentbyoid (rpn->attributeSetId);
1434 attributeSet = attrset->value;
1435 rset = rpn_search_structure (zi, rpn->RPNStructure, attributeSet,
1436 num_bases, basenames);
1439 if (rset_is_volatile(rset))
1440 count_set_save(&rset,hits);
1442 count_set (rset, hits);
1443 resultSetAdd (zi, setname, 1, rset);
1445 logf (LOG_DEBUG, "search error: %d", zi->errCode);
1449 struct scan_info_entry {
1455 struct scan_info_entry *list;
1461 static int scan_handle (char *name, const char *info, int pos, void *client)
1463 int len_prefix, idx;
1464 struct scan_info *scan_info = client;
1466 len_prefix = strlen(scan_info->prefix);
1467 if (memcmp (name, scan_info->prefix, len_prefix))
1470 idx = scan_info->after - pos + scan_info->before;
1473 scan_info->list[idx].term = odr_malloc (scan_info->odr,
1474 strlen(name + len_prefix)+1);
1475 strcpy (scan_info->list[idx].term, name + len_prefix);
1476 assert (*info == sizeof(ISAM_P));
1477 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAM_P));
1482 static void scan_term_untrans (ODR odr, char **dstp, const char *src)
1484 char *dst = odr_malloc (odr, strlen(src)*2+1);
1489 const char *cp = map_chrs_output (&src);
1496 int rpn_scan (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1497 oid_value attributeset,
1498 int num_bases, char **basenames,
1499 int *position, int *num_entries, struct scan_entry **list,
1503 int pos = *position;
1504 int num = *num_entries;
1508 char termz[IT_MAX_WORD+20];
1511 AttrType completeness;
1512 int completeness_value;
1513 struct scan_info *scan_info_array;
1514 struct scan_entry *glist;
1515 int ords[32], ord_no = 0;
1518 logf (LOG_DEBUG, "scan, position = %d, num = %d", pos, num);
1520 if (attributeset == VAL_NONE)
1521 attributeset = VAL_BIB1;
1523 attr_init (&use, zapt, 1);
1524 use_value = attr_find (&use, &attributeset);
1525 logf (LOG_DEBUG, "use value %d", use_value);
1527 attr_init (&completeness, zapt, 6);
1528 completeness_value = attr_find (&completeness, NULL);
1529 logf (LOG_DEBUG, "completeness value %d", completeness_value);
1531 if (use_value == -1)
1533 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
1536 data1_local_attribute *local_attr;
1538 attp = att_getentbyatt (attributeset, use_value);
1541 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
1542 attributeset, use_value);
1543 return zi->errCode = 114;
1545 if (zebTargetInfo_curDatabase (zi->zti, basenames[base_no]))
1547 zi->errString = basenames[base_no];
1548 return zi->errCode = 109; /* Database unavailable */
1550 for (local_attr = attp->local_attributes; local_attr && ord_no < 32;
1551 local_attr = local_attr->next)
1555 ord = zebTargetInfo_lookupSU (zi->zti, attp->attset_ordinal,
1558 ords[ord_no++] = ord;
1562 return zi->errCode = 113;
1565 scan_info_array = odr_malloc (zi->odr, ord_no * sizeof(*scan_info_array));
1566 for (i = 0; i < ord_no; i++)
1568 int j, prefix_len = 0;
1569 int before_tmp = before, after_tmp = after;
1570 struct scan_info *scan_info = scan_info_array + i;
1572 scan_info->before = before;
1573 scan_info->after = after;
1574 scan_info->odr = zi->odr;
1576 scan_info->list = odr_malloc (zi->odr, (before+after)*
1577 sizeof(*scan_info->list));
1578 for (j = 0; j<before+after; j++)
1579 scan_info->list[j].term = NULL;
1580 termz[prefix_len++] = ords[i];
1581 termz[prefix_len++] =
1582 (completeness_value==2 || completeness_value==3) ? 'p': 'w';
1583 termz[prefix_len] = 0;
1584 strcpy (scan_info->prefix, termz);
1586 trans_scan_term (zi, zapt, termz+prefix_len);
1588 dict_scan (zi->dict, termz, &before_tmp, &after_tmp, scan_info,
1591 glist = odr_malloc (zi->odr, (before+after)*sizeof(*glist));
1592 for (i = 0; i < ord_no; i++)
1595 *status = BEND_SCAN_SUCCESS;
1596 for (i = 0; i<after; i++)
1599 const char *mterm = NULL;
1603 for (j = 0; j < ord_no; j++)
1605 if (ptr[j] < before+after &&
1606 (tst=scan_info_array[j].list[ptr[j]].term) &&
1607 (!mterm || strcmp (tst, mterm) < 0))
1615 scan_term_untrans (zi->odr, &glist[i+before].term, mterm);
1616 rset = rset_trunc (zi, &scan_info_array[j0].list[ptr[j0]].isam_p, 1);
1619 for (j = j0+1; j<ord_no; j++)
1621 if (ptr[j] < before+after &&
1622 (tst=scan_info_array[j].list[ptr[j]].term) &&
1623 !strcmp (tst, mterm))
1625 rset_bool_parms bool_parms;
1629 rset_trunc (zi, &scan_info_array[j].list[ptr[j]].isam_p, 1);
1631 bool_parms.key_size = sizeof(struct it_key);
1632 bool_parms.cmp = key_compare_it;
1633 bool_parms.rset_l = rset;
1634 bool_parms.rset_r = rset2;
1636 rset = rset_create (rset_kind_or, &bool_parms);
1641 count_set (rset, &glist[i+before].occurrences);
1646 *num_entries -= (after-i);
1647 *status = BEND_SCAN_PARTIAL;
1650 for (i = 0; i<ord_no; i++)
1653 for (i = 0; i<before; i++)
1656 const char *mterm = NULL;
1660 for (j = 0; j <ord_no; j++)
1662 if (ptr[j] < before &&
1663 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
1664 (!mterm || strcmp (tst, mterm) > 0))
1673 scan_term_untrans (zi->odr, &glist[before-1-i].term, mterm);
1676 (zi, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1);
1680 for (j = j0+1; j<ord_no; j++)
1682 if (ptr[j] < before &&
1683 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
1684 !strcmp (tst, mterm))
1686 rset_bool_parms bool_parms;
1689 rset2 = rset_trunc (zi,
1690 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1);
1692 bool_parms.key_size = sizeof(struct it_key);
1693 bool_parms.cmp = key_compare_it;
1694 bool_parms.rset_l = rset;
1695 bool_parms.rset_r = rset2;
1697 rset = rset_create (rset_kind_or, &bool_parms);
1702 count_set (rset, &glist[before-1-i].occurrences);
1708 *status = BEND_SCAN_PARTIAL;
1712 *list = glist + i; /* list is set to first 'real' entry */
1714 logf (LOG_DEBUG, "position = %d, num_entries = %d",
1715 *position, *num_entries);
1717 logf (LOG_DEBUG, "scan error: %d", zi->errCode);