2 * Copyright (C) 1994-1996, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.60 1997-01-31 11:10:34 adam
8 * Bug fix: Leading and trailing white space weren't removed in scan tokens.
10 * Revision 1.59 1997/01/17 11:31:46 adam
11 * Bug fix: complete phrase search didn't work.
13 * Revision 1.58 1996/12/23 15:30:45 adam
15 * Bug fix: result sets weren't deleted after server shut down.
17 * Revision 1.57 1996/11/11 13:38:02 adam
18 * Added proximity support in search.
20 * Revision 1.56 1996/11/08 11:10:32 adam
21 * Buffers used during file match got bigger.
22 * Compressed ISAM support everywhere.
23 * Bug fixes regarding masking characters in queries.
24 * Redesigned Regexp-2 queries.
26 * Revision 1.55 1996/11/04 14:07:44 adam
27 * Moved truncation code to trunc.c.
29 * Revision 1.54 1996/10/29 14:09:52 adam
30 * Use of cisam system - enabled if setting isamc is 1.
32 * Revision 1.53 1996/06/26 09:21:43 adam
33 * Bug fix: local attribute set wasn't obeyed in scan.
35 * Revision 1.52 1996/06/17 14:26:20 adam
36 * Function gen_regular_rel changed to handle negative numbers.
38 * Revision 1.51 1996/06/11 10:54:15 quinn
41 * Revision 1.50 1996/06/07 08:51:53 adam
42 * Bug fix: Character mapping was broken (introducued by last revision).
44 * Revision 1.49 1996/06/04 10:18:11 adam
45 * Search/scan uses character mapping module.
47 * Revision 1.48 1996/05/28 15:15:01 adam
48 * Bug fix: Didn't handle unknown database correctly.
50 * Revision 1.47 1996/05/15 18:36:28 adam
51 * Function trans_term transforms unsearchable characters to blanks.
53 * Revision 1.46 1996/05/15 11:57:56 adam
54 * Fixed bug introduced by set/field mapping in search operations.
56 * Revision 1.45 1996/05/14 11:34:00 adam
57 * Scan support in multiple registers/databases.
59 * Revision 1.44 1996/05/14 06:16:44 adam
60 * Compact use/set bytes used in search service.
62 * Revision 1.43 1996/05/09 09:54:43 adam
63 * Server supports maps from one logical attributes to a list of physical
65 * The extraction process doesn't make space consuming 'any' keys.
67 * Revision 1.42 1996/05/09 07:28:56 quinn
68 * Work towards phrases and multiple registers
70 * Revision 1.41 1996/03/20 09:36:43 adam
71 * Function dict_lookup_grep got extra parameter, init_pos, which marks
72 * from which position in pattern approximate pattern matching should occur.
73 * Approximate pattern matching is used in relevance=re-2.
75 * Revision 1.40 1996/02/02 13:44:44 adam
76 * The public dictionary functions simply use char instead of Dict_char
77 * to represent search strings. Dict_char is used internally only.
79 * Revision 1.39 1996/01/03 16:22:13 quinn
82 * Revision 1.38 1995/12/11 09:12:55 adam
83 * The rec_get function returns NULL if record doesn't exist - will
84 * happen in the server if the result set records have been deleted since
85 * the creation of the set (i.e. the search).
86 * The server saves a result temporarily if it is 'volatile', i.e. the
87 * set is register dependent.
89 * Revision 1.37 1995/12/06 15:05:28 adam
90 * More verbose in count_set.
92 * Revision 1.36 1995/12/06 12:41:27 adam
93 * New command 'stat' for the index program.
94 * Filenames can be read from stdin by specifying '-'.
95 * Bug fix/enhancement of the transformation from terms to regular
96 * expressons in the search engine.
98 * Revision 1.35 1995/11/27 09:29:00 adam
99 * Bug fixes regarding conversion to regular expressions.
101 * Revision 1.34 1995/11/16 17:00:56 adam
102 * Better logging of rpn query.
104 * Revision 1.33 1995/11/01 13:58:28 quinn
105 * Moving data1 to yaz/retrieval
107 * Revision 1.32 1995/10/27 14:00:11 adam
108 * Implemented detection of database availability.
110 * Revision 1.31 1995/10/17 18:02:10 adam
111 * New feature: databases. Implemented as prefix to words in dictionary.
113 * Revision 1.30 1995/10/16 09:32:38 adam
114 * More work on relational op.
116 * Revision 1.29 1995/10/13 16:01:49 adam
119 * Revision 1.28 1995/10/13 12:26:43 adam
120 * Optimization of truncation.
122 * Revision 1.27 1995/10/12 17:07:22 adam
125 * Revision 1.26 1995/10/12 12:40:54 adam
126 * Bug fixes in rpn_prox.
128 * Revision 1.25 1995/10/10 13:59:24 adam
129 * Function rset_open changed its wflag parameter to general flags.
131 * Revision 1.24 1995/10/09 16:18:37 adam
132 * Function dict_lookup_grep got extra client data parameter.
134 * Revision 1.23 1995/10/06 16:33:37 adam
135 * Use attribute mappings.
137 * Revision 1.22 1995/10/06 15:07:39 adam
138 * Structure 'local-number' handled.
140 * Revision 1.21 1995/10/06 13:52:06 adam
141 * Bug fixes. Handler may abort further scanning.
143 * Revision 1.20 1995/10/06 11:06:33 adam
144 * Scan entries include 'occurrences' now.
146 * Revision 1.19 1995/10/06 10:43:56 adam
147 * Scan added. 'occurrences' in scan entries not set yet.
149 * Revision 1.18 1995/10/04 16:57:20 adam
150 * Key input and merge sort in one pass.
152 * Revision 1.17 1995/10/04 12:55:17 adam
153 * Bug fix in ranked search. Use=Any keys inserted.
155 * Revision 1.16 1995/10/02 16:24:40 adam
156 * Use attribute actually used in search requests.
158 * Revision 1.15 1995/10/02 15:18:52 adam
159 * New member in recRetrieveCtrl: diagnostic.
161 * Revision 1.14 1995/09/28 12:10:32 adam
162 * Bug fixes. Field prefix used in queries.
164 * Revision 1.13 1995/09/18 14:17:50 adam
167 * Revision 1.12 1995/09/15 14:45:21 adam
169 * Work on truncation.
171 * Revision 1.11 1995/09/14 11:53:27 adam
172 * First work on regular expressions/truncations.
174 * Revision 1.10 1995/09/11 15:23:26 adam
175 * More work on relevance search.
177 * Revision 1.9 1995/09/11 13:09:35 adam
178 * More work on relevance feedback.
180 * Revision 1.8 1995/09/08 14:52:27 adam
181 * Minor changes. Dictionary is lower case now.
183 * Revision 1.7 1995/09/07 13:58:36 adam
184 * New parameter: result-set file descriptor (RSFD) to support multiple
185 * positions within the same result-set.
186 * Boolean operators: and, or, not implemented.
187 * Result-set references.
189 * Revision 1.6 1995/09/06 16:11:18 adam
190 * Option: only one word key per file.
192 * Revision 1.5 1995/09/06 10:33:04 adam
193 * More work on present. Some log messages removed.
195 * Revision 1.4 1995/09/05 15:28:40 adam
196 * More work on search engine.
198 * Revision 1.3 1995/09/04 15:20:22 adam
201 * Revision 1.2 1995/09/04 12:33:43 adam
202 * Various cleanup. YAZ util used instead.
204 * Revision 1.1 1995/09/04 09:10:40 adam
205 * More work on index add/del/update.
206 * Merge sort implemented.
207 * Initial work on z39 server.
216 #include "attribute.h"
228 Z_AttributesPlusTerm *zapt;
231 static int attr_find (AttrType *src, oid_value *attributeSetP)
233 while (src->major < src->zapt->num_attributes)
235 Z_AttributeElement *element;
237 element = src->zapt->attributeList[src->major];
238 if (src->type == *element->attributeType)
240 switch (element->which)
242 case Z_AttributeValue_numeric:
244 if (element->attributeSet && attributeSetP)
248 attrset = oid_getentbyoid (element->attributeSet);
249 *attributeSetP = attrset->value;
251 return *element->value.numeric;
253 case Z_AttributeValue_complex:
254 if (src->minor >= element->value.complex->num_list ||
255 element->value.complex->list[src->minor]->which !=
256 Z_StringOrNumeric_numeric)
259 if (element->attributeSet && attributeSetP)
263 attrset = oid_getentbyoid (element->attributeSet);
264 *attributeSetP = attrset->value;
266 return *element->value.complex->list[src->minor-1]->u.numeric;
276 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
296 static void add_isam_p (const char *info, struct grep_info *p)
298 if (p->isam_p_indx == p->isam_p_size)
300 ISAM_P *new_isam_p_buf;
305 p->isam_p_size = 2*p->isam_p_size + 100;
306 new_isam_p_buf = xmalloc (sizeof(*new_isam_p_buf) *
310 memcpy (new_isam_p_buf, p->isam_p_buf,
311 p->isam_p_indx * sizeof(*p->isam_p_buf));
312 xfree (p->isam_p_buf);
314 p->isam_p_buf = new_isam_p_buf;
317 new_term_no = xmalloc (sizeof(*new_term_no) *
321 memcpy (new_term_no, p->isam_p_buf,
322 p->isam_p_indx * sizeof(*p->term_no));
325 p->term_no = new_term_no;
328 assert (*info == sizeof(*p->isam_p_buf));
329 memcpy (p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
333 static int grep_handle (char *name, const char *info, void *p)
335 add_isam_p (info, p);
339 static int term_pre (char **src, const char *ct1, const char *ct2)
341 char *s1, *s0 = *src;
344 /* skip white space */
347 if (ct1 && strchr (ct1, *s0))
349 if (ct2 && strchr (ct2, *s0))
352 map = map_chrs_input (&s1, strlen(s1));
353 if (**map != *CHR_SPACE)
361 static int term_100 (char **src, char *dst, int space_split)
363 char *s0, *s1, **map;
366 if (!term_pre (src, NULL, NULL))
372 map = map_chrs_input (&s0, strlen(s0));
373 if (space_split && **map == *CHR_SPACE)
387 static int term_101 (char **src, char *dst, int space_split)
389 char *s0, *s1, **map;
392 if (!term_pre (src, "#", "#"))
406 map = map_chrs_input (&s0, strlen(s0));
407 if (space_split && **map == *CHR_SPACE)
423 static int term_103 (char **src, char *dst, int *errors, int space_split)
426 char *s0, *s1, **map;
428 if (!term_pre (src, "\\()[].*+?|", "("))
431 if (errors && *s0 == '+' && s0[1] && s0[2] == '+' && s0[3] &&
434 *errors = s0[1] - '0';
441 if (strchr ("\\()[].*+?|-", *s0))
446 map = map_chrs_input (&s0, strlen(s0));
447 if (**map == *CHR_SPACE)
462 static int term_102 (char **src, char *dst, int space_split)
464 return term_103 (src, dst, NULL, space_split);
467 /* gen_regular_rel - generate regular expression from relation
468 * val: border value (inclusive)
469 * islt: 1 if <=; 0 if >=.
471 static void gen_regular_rel (char *dst, int val, int islt)
478 logf (LOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
482 strcpy (dst, "(-[0-9]+|");
490 strcpy (dst, "([0-9]+|-");
491 dst_p = strlen (dst);
501 dst_p = strlen (dst);
502 sprintf (numstr, "%d", val);
503 for (w = strlen(numstr); --w >= 0; pos++)
522 strcpy (dst + dst_p, numstr);
523 dst_p = strlen(dst) - pos - 1;
551 for (i = 0; i<pos; i++)
564 for (i=1; i<pos; i++)
565 strcat (dst, "[0-9]?");
569 for (i = 0; i <= pos; i++)
570 strcat (dst, "[0-9]");
571 strcat (dst, "[0-9]*");
576 static int relational_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
579 oid_value attributeSet,
580 struct grep_info *grep_info,
588 attr_init (&relation, zapt, 2);
589 relation_value = attr_find (&relation, NULL);
591 switch (relation_value)
594 if (!term_100 (term_sub, term_dict, 1))
596 term_value = atoi (term_dict);
599 logf (LOG_DEBUG, "Relation <");
600 gen_regular_rel (term_dict + strlen(term_dict), term_value-1, 1);
603 if (!term_100 (term_sub, term_dict, 1))
605 term_value = atoi (term_dict);
608 logf (LOG_DEBUG, "Relation <=");
609 gen_regular_rel (term_dict + strlen(term_dict), term_value, 1);
612 if (!term_100 (term_sub, term_dict, 1))
614 term_value = atoi (term_dict);
617 logf (LOG_DEBUG, "Relation >=");
618 gen_regular_rel (term_dict + strlen(term_dict), term_value, 0);
621 if (!term_100 (term_sub, term_dict, 1))
623 term_value = atoi (term_dict);
626 logf (LOG_DEBUG, "Relation >");
627 gen_regular_rel (term_dict + strlen(term_dict), term_value+1, 0);
632 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_dict);
633 r = dict_lookup_grep (zi->dict, term_dict, 0, grep_info, max_pos,
636 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
637 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
641 static int field_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
642 char **term_sub, int regType,
643 oid_value attributeSet, struct grep_info *grep_info,
644 int num_bases, char **basenames, int space_split)
646 char term_dict[2*IT_MAX_WORD+2];
649 int truncation_value;
652 oid_value curAttributeSet = attributeSet;
655 attr_init (&use, zapt, 1);
656 use_value = attr_find (&use, &curAttributeSet);
657 logf (LOG_DEBUG, "field_term, use value %d", use_value);
658 attr_init (&truncation, zapt, 5);
659 truncation_value = attr_find (&truncation, NULL);
660 logf (LOG_DEBUG, "truncation value %d", truncation_value);
665 for (base_no = 0; base_no < num_bases; base_no++)
668 data1_local_attribute *local_attr;
669 int max_pos, prefix_len = 0;
672 attp = att_getentbyatt (curAttributeSet, use_value);
675 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
676 curAttributeSet, use_value);
680 if (zebTargetInfo_curDatabase (zi->zti, basenames[base_no]))
682 zi->errCode = 109; /* Database unavailable */
683 zi->errString = basenames[base_no];
686 for (local_attr = attp->local_attributes; local_attr;
687 local_attr = local_attr->next)
691 ord = zebTargetInfo_lookupSU (zi->zti, attp->attset_ordinal,
696 term_dict[prefix_len++] = '|';
698 term_dict[prefix_len++] = '(';
699 term_dict[prefix_len++] = 1;
700 term_dict[prefix_len++] = ord;
707 term_dict[prefix_len++] = ')';
708 term_dict[prefix_len++] = 1;
709 term_dict[prefix_len++] = regType;
710 term_dict[prefix_len] = '\0';
711 if (!relational_term (zi, zapt, &termp, term_dict,
712 attributeSet, grep_info, &max_pos))
715 switch (truncation_value)
717 case -1: /* not specified */
718 case 100: /* do not truncate */
719 term_dict[j++] = '(';
720 if (!term_100 (&termp, term_dict + j, space_split))
722 strcat (term_dict, ")");
723 r = dict_lookup_grep (zi->dict, term_dict, 0, grep_info,
724 &max_pos, 0, grep_handle);
726 logf (LOG_WARN, "dict_lookup_grep err, trunc=none:%d", r);
728 case 1: /* right truncation */
729 term_dict[j++] = '(';
730 if (!term_100 (&termp, term_dict + j, space_split))
732 strcat (term_dict, ".*)");
733 dict_lookup_grep (zi->dict, term_dict, 0, grep_info,
734 &max_pos, 0, grep_handle);
736 case 2: /* left truncation */
737 case 3: /* left&right truncation */
740 case 101: /* process # in term */
741 term_dict[j++] = '(';
742 if (!term_101 (&termp, term_dict + j, space_split))
744 strcat (term_dict, ")");
745 r = dict_lookup_grep (zi->dict, term_dict, 0, grep_info,
746 &max_pos, 0, grep_handle);
748 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d", r);
750 case 102: /* Regexp-1 */
751 term_dict[j++] = '(';
752 if (!term_102 (&termp, term_dict + j, space_split))
754 strcat (term_dict, ")");
755 logf (LOG_DEBUG, "Regexp-1 tolerance=%d", r);
756 r = dict_lookup_grep (zi->dict, term_dict, 0, grep_info,
757 &max_pos, 0, grep_handle);
759 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
762 case 103: /* Regexp-1 */
764 term_dict[j++] = '(';
765 if (!term_103 (&termp, term_dict + j, &r, space_split))
767 strcat (term_dict, ")");
768 logf (LOG_DEBUG, "Regexp-2 tolerance=%d", r);
769 r = dict_lookup_grep (zi->dict, term_dict, r, grep_info,
770 &max_pos, 2, grep_handle);
772 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
779 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
783 static void trans_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
787 Z_Term *term = zapt->term;
789 sizez = term->u.general->len;
790 if (sizez > IT_MAX_WORD-1)
791 sizez = IT_MAX_WORD-1;
792 memcpy (termz, term->u.general->buf, sizez);
796 static void trans_scan_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
799 Z_Term *term = zapt->term;
801 char *cp = (char*) term->u.general->buf;
802 const char *cp_end = cp + term->u.general->len;
805 const char *space_map = NULL;
808 while ((len = (cp_end - cp)) > 0)
810 map = map_chrs_input (&cp, len);
811 if (**map == *CHR_SPACE)
816 for (src = space_map; *src; src++)
819 for (src = *map; *src; src++)
826 static RSET rpn_search_APT_relevance (ZServerInfo *zi,
827 Z_AttributesPlusTerm *zapt,
828 oid_value attributeSet,
829 int num_bases, char **basenames)
831 rset_relevance_parms parms;
832 char termz[IT_MAX_WORD+1];
834 struct grep_info grep_info;
839 parms.key_size = sizeof(struct it_key);
840 parms.max_rec = 1000;
841 parms.cmp = key_compare_it;
843 parms.isc = zi->isamc;
846 if (zapt->term->which != Z_Term_general)
851 trans_term (zi, zapt, termz);
854 grep_info.term_no = 0;
856 grep_info.isam_p_indx = 0;
857 grep_info.isam_p_size = 0;
858 grep_info.isam_p_buf = NULL;
861 r = field_term (zi, zapt, &termp, 'w', attributeSet, &grep_info,
862 num_bases, basenames, 1);
866 for (; term_index < grep_info.isam_p_indx; term_index++)
867 grep_info.term_no[term_index] = parms.no_terms;
871 parms.term_no = grep_info.term_no;
872 parms.isam_positions = grep_info.isam_p_buf;
873 parms.no_isam_positions = grep_info.isam_p_indx;
874 if (grep_info.isam_p_indx > 0)
875 result = rset_create (rset_kind_relevance, &parms);
877 result = rset_create (rset_kind_null, NULL);
879 xfree(grep_info.term_no);
881 xfree (grep_info.isam_p_buf);
885 static RSET rpn_search_APT_cphrase (ZServerInfo *zi,
886 Z_AttributesPlusTerm *zapt,
887 oid_value attributeSet,
888 int num_bases, char **basenames)
890 char termz[IT_MAX_WORD+1];
891 struct grep_info grep_info;
896 if (zapt->term->which != Z_Term_general)
901 trans_term (zi, zapt, termz);
904 grep_info.term_no = 0;
906 grep_info.isam_p_indx = 0;
907 grep_info.isam_p_size = 0;
908 grep_info.isam_p_buf = NULL;
910 r = field_term (zi, zapt, &termp, 'p', attributeSet, &grep_info,
911 num_bases, basenames, 0);
912 result = rset_trunc (zi, grep_info.isam_p_buf, grep_info.isam_p_indx);
914 xfree(grep_info.term_no);
916 xfree (grep_info.isam_p_buf);
920 static RSET rpn_proximity (RSET rset1, RSET rset2, int ordered,
921 int exclusion, int relation, int distance)
926 struct it_key buf1, buf2;
929 rset_temp_parms parms;
931 rsfd1 = rset_open (rset1, RSETF_READ|RSETF_SORT_SYSNO);
932 more1 = rset_read (rset1, rsfd1, &buf1);
934 rsfd2 = rset_open (rset2, RSETF_READ|RSETF_SORT_SYSNO);
935 more2 = rset_read (rset2, rsfd2, &buf2);
937 parms.key_size = sizeof (struct it_key);
938 result = rset_create (rset_kind_temp, &parms);
939 rsfd_result = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
941 logf (LOG_DEBUG, "rpn_proximity excl=%d ord=%d rel=%d dis=%d",
942 exclusion, ordered, relation, distance);
943 while (more1 && more2)
945 int cmp = key_compare_it (&buf1, &buf2);
947 more1 = rset_read (rset1, rsfd1, &buf1);
949 more2 = rset_read (rset2, rsfd2, &buf2);
952 int sysno = buf1.sysno;
956 seqno[n++] = buf1.seqno;
957 while ((more1 = rset_read (rset1, rsfd1, &buf1)) &&
960 seqno[n++] = buf1.seqno;
963 for (i = 0; i<n; i++)
965 int diff = buf2.seqno - seqno[i];
966 int excl = exclusion;
967 if (!ordered && diff < 0)
976 if (diff <= distance)
980 if (diff == distance)
984 if (diff >= distance)
992 if (diff != distance)
997 rset_write (result, rsfd_result, &buf2);
999 } while ((more2 = rset_read (rset2, rsfd2, &buf2)) &&
1000 sysno == buf2.sysno);
1003 rset_close (result, rsfd_result);
1004 rset_close (rset1, rsfd1);
1005 rset_close (rset2, rsfd2);
1009 static RSET rpn_prox (RSET *rset, int rset_no)
1014 struct it_key **buf;
1017 rset_temp_parms parms;
1019 rsfd = xmalloc (sizeof(*rsfd)*rset_no);
1020 more = xmalloc (sizeof(*more)*rset_no);
1021 buf = xmalloc (sizeof(*buf)*rset_no);
1023 for (i = 0; i<rset_no; i++)
1025 buf[i] = xmalloc (sizeof(**buf));
1026 rsfd[i] = rset_open (rset[i], RSETF_READ|RSETF_SORT_SYSNO);
1027 if (!(more[i] = rset_read (rset[i], rsfd[i], buf[i])))
1031 rset_close (rset[i], rsfd[i]);
1038 return rset_create (rset_kind_null, NULL);
1041 parms.key_size = sizeof (struct it_key);
1042 result = rset_create (rset_kind_temp, &parms);
1043 rsfd_result = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
1047 for (i = 1; i<rset_no; i++)
1056 cmp = key_compare_it (buf[i], buf[i-1]);
1059 more[i-1] = rset_read (rset[i-1], rsfd[i-1], buf[i-1]);
1064 if (buf[i-1]->seqno+1 != buf[i]->seqno)
1066 more[i-1] = rset_read (rset[i-1], rsfd[i-1], buf[i-1]);
1072 more[i] = rset_read (rset[i], rsfd[i], buf[i]);
1078 rset_write (result, rsfd_result, buf[0]);
1079 more[0] = rset_read (*rset, *rsfd, *buf);
1083 for (i = 0; i<rset_no; i++)
1085 rset_close (rset[i], rsfd[i]);
1088 rset_close (result, rsfd_result);
1095 static RSET rpn_search_APT_phrase (ZServerInfo *zi,
1096 Z_AttributesPlusTerm *zapt,
1097 oid_value attributeSet,
1098 int num_bases, char **basenames)
1100 char termz[IT_MAX_WORD+1];
1101 char *termp = termz;
1102 RSET rset[60], result;
1103 int i, r, rset_no = 0;
1104 struct grep_info grep_info;
1106 if (zapt->term->which != Z_Term_general)
1111 trans_term (zi, zapt, termz);
1114 grep_info.term_no = 0;
1116 grep_info.isam_p_size = 0;
1117 grep_info.isam_p_buf = NULL;
1121 grep_info.isam_p_indx = 0;
1122 r = field_term (zi, zapt, &termp, 'w', attributeSet, &grep_info,
1123 num_bases, basenames, 1);
1126 rset[rset_no] = rset_trunc (zi, grep_info.isam_p_buf,
1127 grep_info.isam_p_indx);
1128 assert (rset[rset_no]);
1129 if (++rset_no >= sizeof(rset)/sizeof(*rset))
1133 xfree(grep_info.term_no);
1135 xfree (grep_info.isam_p_buf);
1137 return rset_create (rset_kind_null, NULL);
1138 else if (rset_no == 1)
1140 result = rpn_prox (rset, rset_no);
1141 for (i = 0; i<rset_no; i++)
1142 rset_delete (rset[i]);
1146 static RSET rpn_search_APT_local (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1147 oid_value attributeSet)
1152 rset_temp_parms parms;
1153 char termz[IT_MAX_WORD+1];
1155 if (zapt->term->which != Z_Term_general)
1160 parms.key_size = sizeof (struct it_key);
1161 result = rset_create (rset_kind_temp, &parms);
1162 rsfd = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
1164 trans_term (zi, zapt, termz);
1166 key.sysno = atoi (termz);
1169 rset_write (result, rsfd, &key);
1170 rset_close (result, rsfd);
1174 static RSET rpn_search_APT (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1175 oid_value attributeSet,
1176 int num_bases, char **basenames)
1180 AttrType completeness;
1181 int relation_value, structure_value, completeness_value;
1183 attr_init (&relation, zapt, 2);
1184 attr_init (&structure, zapt, 4);
1185 attr_init (&completeness, zapt, 6);
1187 relation_value = attr_find (&relation, NULL);
1188 structure_value = attr_find (&structure, NULL);
1189 completeness_value = attr_find (&completeness, NULL);
1190 switch (structure_value)
1193 if (relation_value == 102) /* relevance relation */
1194 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1195 num_bases, basenames);
1196 if (completeness_value == 2 || completeness_value == 3)
1197 return rpn_search_APT_cphrase (zi, zapt, attributeSet,
1198 num_bases, basenames);
1199 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1200 num_bases, basenames);
1201 case 1: /* phrase */
1202 if (relation_value == 102) /* relevance relation */
1203 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1204 num_bases, basenames);
1205 if (completeness_value == 2 || completeness_value == 3)
1206 return rpn_search_APT_cphrase (zi, zapt, attributeSet,
1207 num_bases, basenames);
1208 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1209 num_bases, basenames);
1212 if (relation_value == 102) /* relevance relation */
1213 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1214 num_bases, basenames);
1215 if (completeness_value == 2 || completeness_value == 3)
1216 return rpn_search_APT_cphrase (zi, zapt, attributeSet,
1217 num_bases, basenames);
1218 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1219 num_bases, basenames);
1224 case 5: /* date - normalized */
1226 case 6: /* word list */
1227 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1228 num_bases, basenames);
1229 case 100: /* date - un-normalized */
1231 case 101: /* name - normalized */
1233 case 102: /* date - un-normalized */
1235 case 103: /* structure */
1239 case 105: /* free-form-text */
1240 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1241 num_bases, basenames);
1242 case 106: /* document-text */
1243 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1244 num_bases, basenames);
1245 case 107: /* local-number */
1246 return rpn_search_APT_local (zi, zapt, attributeSet);
1247 case 108: /* string */
1248 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1249 num_bases, basenames);
1250 case 109: /* numeric string */
1257 static RSET rpn_search_ref (ZServerInfo *zi, Z_ResultSetId *resultSetId)
1261 if (!(s = resultSetGet (zi, resultSetId)))
1262 return rset_create (rset_kind_null, NULL);
1266 static RSET rpn_search_structure (ZServerInfo *zi, Z_RPNStructure *zs,
1267 oid_value attributeSet,
1268 int num_bases, char **basenames)
1271 if (zs->which == Z_RPNStructure_complex)
1273 Z_Operator *zop = zs->u.complex->roperator;
1274 rset_bool_parms bool_parms;
1278 bool_parms.rset_l = rpn_search_structure (zi, zs->u.complex->s1,
1280 num_bases, basenames);
1281 if (bool_parms.rset_l == NULL)
1283 if (rset_is_ranked(bool_parms.rset_l))
1285 bool_parms.rset_r = rpn_search_structure (zi, zs->u.complex->s2,
1287 num_bases, basenames);
1288 if (bool_parms.rset_r == NULL)
1290 rset_delete (bool_parms.rset_l);
1293 if (rset_is_ranked(bool_parms.rset_r))
1295 bool_parms.key_size = sizeof(struct it_key);
1296 bool_parms.cmp = key_compare_it;
1300 case Z_Operator_and:
1301 r = rset_create (soft ? rset_kind_sand:rset_kind_and, &bool_parms);
1304 r = rset_create (soft ? rset_kind_sor:rset_kind_or, &bool_parms);
1306 case Z_Operator_and_not:
1307 r = rset_create (soft ? rset_kind_snot:rset_kind_not, &bool_parms);
1309 case Z_Operator_prox:
1310 if (zop->u.prox->which != Z_ProxCode_known)
1315 if (*zop->u.prox->proximityUnitCode != Z_ProxUnit_word)
1317 static char val[16];
1319 zi->errString = val;
1320 sprintf (val, "%d", *zop->u.prox->proximityUnitCode);
1323 r = rpn_proximity (bool_parms.rset_l, bool_parms.rset_r,
1324 *zop->u.prox->ordered,
1325 (!zop->u.prox->exclusion ? 0 :
1326 *zop->u.prox->exclusion),
1327 *zop->u.prox->relationType,
1328 *zop->u.prox->distance);
1335 else if (zs->which == Z_RPNStructure_simple)
1337 if (zs->u.simple->which == Z_Operand_APT)
1339 logf (LOG_DEBUG, "rpn_search_APT");
1340 r = rpn_search_APT (zi, zs->u.simple->u.attributesPlusTerm,
1341 attributeSet, num_bases, basenames);
1343 else if (zs->u.simple->which == Z_Operand_resultSetId)
1345 logf (LOG_DEBUG, "rpn_search_ref");
1346 r = rpn_search_ref (zi, zs->u.simple->u.resultSetId);
1362 void count_set_save (RSET *r, int *count)
1369 rset_temp_parms parms;
1370 int maxResultSetSize = atoi (res_get_def (common_resource,
1371 "maxResultSetSize", "400"));
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)
1382 if (*count < maxResultSetSize)
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);