2 * Copyright (C) 1994-1997, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.69 1997-10-29 12:05:02 adam
8 * Server produces diagnostic "Unsupported Attribute Set" when appropriate.
10 * Revision 1.68 1997/10/27 14:33:06 adam
11 * Moved towards generic character mapping depending on "structure"
12 * field in abstract syntax file. Fixed a few memory leaks. Fixed
13 * bug with negative integers when doing searches with relational
16 * Revision 1.67 1997/09/29 09:06:10 adam
17 * Removed one static var in order to make this module thread safe.
19 * Revision 1.66 1997/09/25 14:58:03 adam
22 * Revision 1.65 1997/09/22 12:39:06 adam
23 * Added get_pos method for the ranked result sets.
25 * Revision 1.64 1997/09/18 08:59:20 adam
26 * Extra generic handle for the character mapping routines.
28 * Revision 1.63 1997/09/17 12:19:18 adam
29 * Zebra version corresponds to YAZ version 1.4.
30 * Changed Zebra server so that it doesn't depend on global common_resource.
32 * Revision 1.62 1997/09/05 15:30:09 adam
33 * Changed prototype for chr_map_input - added const.
34 * Added support for C++, headers uses extern "C" for public definitions.
36 * Revision 1.61 1997/02/10 10:21:14 adam
37 * Bug fix: in search terms character (^) wasn't observed.
39 * Revision 1.60 1997/01/31 11:10:34 adam
40 * Bug fix: Leading and trailing white space weren't removed in scan tokens.
42 * Revision 1.59 1997/01/17 11:31:46 adam
43 * Bug fix: complete phrase search didn't work.
45 * Revision 1.58 1996/12/23 15:30:45 adam
47 * Bug fix: result sets weren't deleted after server shut down.
49 * Revision 1.57 1996/11/11 13:38:02 adam
50 * Added proximity support in search.
52 * Revision 1.56 1996/11/08 11:10:32 adam
53 * Buffers used during file match got bigger.
54 * Compressed ISAM support everywhere.
55 * Bug fixes regarding masking characters in queries.
56 * Redesigned Regexp-2 queries.
58 * Revision 1.55 1996/11/04 14:07:44 adam
59 * Moved truncation code to trunc.c.
61 * Revision 1.54 1996/10/29 14:09:52 adam
62 * Use of cisam system - enabled if setting isamc is 1.
64 * Revision 1.53 1996/06/26 09:21:43 adam
65 * Bug fix: local attribute set wasn't obeyed in scan.
67 * Revision 1.52 1996/06/17 14:26:20 adam
68 * Function gen_regular_rel changed to handle negative numbers.
70 * Revision 1.51 1996/06/11 10:54:15 quinn
73 * Revision 1.50 1996/06/07 08:51:53 adam
74 * Bug fix: Character mapping was broken (introducued by last revision).
76 * Revision 1.49 1996/06/04 10:18:11 adam
77 * Search/scan uses character mapping module.
79 * Revision 1.48 1996/05/28 15:15:01 adam
80 * Bug fix: Didn't handle unknown database correctly.
82 * Revision 1.47 1996/05/15 18:36:28 adam
83 * Function trans_term transforms unsearchable characters to blanks.
85 * Revision 1.46 1996/05/15 11:57:56 adam
86 * Fixed bug introduced by set/field mapping in search operations.
88 * Revision 1.45 1996/05/14 11:34:00 adam
89 * Scan support in multiple registers/databases.
91 * Revision 1.44 1996/05/14 06:16:44 adam
92 * Compact use/set bytes used in search service.
94 * Revision 1.43 1996/05/09 09:54:43 adam
95 * Server supports maps from one logical attributes to a list of physical
97 * The extraction process doesn't make space consuming 'any' keys.
99 * Revision 1.42 1996/05/09 07:28:56 quinn
100 * Work towards phrases and multiple registers
102 * Revision 1.41 1996/03/20 09:36:43 adam
103 * Function dict_lookup_grep got extra parameter, init_pos, which marks
104 * from which position in pattern approximate pattern matching should occur.
105 * Approximate pattern matching is used in relevance=re-2.
107 * Revision 1.40 1996/02/02 13:44:44 adam
108 * The public dictionary functions simply use char instead of Dict_char
109 * to represent search strings. Dict_char is used internally only.
111 * Revision 1.39 1996/01/03 16:22:13 quinn
112 * operator->roperator
114 * Revision 1.38 1995/12/11 09:12:55 adam
115 * The rec_get function returns NULL if record doesn't exist - will
116 * happen in the server if the result set records have been deleted since
117 * the creation of the set (i.e. the search).
118 * The server saves a result temporarily if it is 'volatile', i.e. the
119 * set is register dependent.
121 * Revision 1.37 1995/12/06 15:05:28 adam
122 * More verbose in count_set.
124 * Revision 1.36 1995/12/06 12:41:27 adam
125 * New command 'stat' for the index program.
126 * Filenames can be read from stdin by specifying '-'.
127 * Bug fix/enhancement of the transformation from terms to regular
128 * expressons in the search engine.
130 * Revision 1.35 1995/11/27 09:29:00 adam
131 * Bug fixes regarding conversion to regular expressions.
133 * Revision 1.34 1995/11/16 17:00:56 adam
134 * Better logging of rpn query.
136 * Revision 1.33 1995/11/01 13:58:28 quinn
137 * Moving data1 to yaz/retrieval
139 * Revision 1.32 1995/10/27 14:00:11 adam
140 * Implemented detection of database availability.
142 * Revision 1.31 1995/10/17 18:02:10 adam
143 * New feature: databases. Implemented as prefix to words in dictionary.
145 * Revision 1.30 1995/10/16 09:32:38 adam
146 * More work on relational op.
148 * Revision 1.29 1995/10/13 16:01:49 adam
151 * Revision 1.28 1995/10/13 12:26:43 adam
152 * Optimization of truncation.
154 * Revision 1.27 1995/10/12 17:07:22 adam
157 * Revision 1.26 1995/10/12 12:40:54 adam
158 * Bug fixes in rpn_prox.
160 * Revision 1.25 1995/10/10 13:59:24 adam
161 * Function rset_open changed its wflag parameter to general flags.
163 * Revision 1.24 1995/10/09 16:18:37 adam
164 * Function dict_lookup_grep got extra client data parameter.
166 * Revision 1.23 1995/10/06 16:33:37 adam
167 * Use attribute mappings.
169 * Revision 1.22 1995/10/06 15:07:39 adam
170 * Structure 'local-number' handled.
172 * Revision 1.21 1995/10/06 13:52:06 adam
173 * Bug fixes. Handler may abort further scanning.
175 * Revision 1.20 1995/10/06 11:06:33 adam
176 * Scan entries include 'occurrences' now.
178 * Revision 1.19 1995/10/06 10:43:56 adam
179 * Scan added. 'occurrences' in scan entries not set yet.
181 * Revision 1.18 1995/10/04 16:57:20 adam
182 * Key input and merge sort in one pass.
184 * Revision 1.17 1995/10/04 12:55:17 adam
185 * Bug fix in ranked search. Use=Any keys inserted.
187 * Revision 1.16 1995/10/02 16:24:40 adam
188 * Use attribute actually used in search requests.
190 * Revision 1.15 1995/10/02 15:18:52 adam
191 * New member in recRetrieveCtrl: diagnostic.
193 * Revision 1.14 1995/09/28 12:10:32 adam
194 * Bug fixes. Field prefix used in queries.
196 * Revision 1.13 1995/09/18 14:17:50 adam
199 * Revision 1.12 1995/09/15 14:45:21 adam
201 * Work on truncation.
203 * Revision 1.11 1995/09/14 11:53:27 adam
204 * First work on regular expressions/truncations.
206 * Revision 1.10 1995/09/11 15:23:26 adam
207 * More work on relevance search.
209 * Revision 1.9 1995/09/11 13:09:35 adam
210 * More work on relevance feedback.
212 * Revision 1.8 1995/09/08 14:52:27 adam
213 * Minor changes. Dictionary is lower case now.
215 * Revision 1.7 1995/09/07 13:58:36 adam
216 * New parameter: result-set file descriptor (RSFD) to support multiple
217 * positions within the same result-set.
218 * Boolean operators: and, or, not implemented.
219 * Result-set references.
221 * Revision 1.6 1995/09/06 16:11:18 adam
222 * Option: only one word key per file.
224 * Revision 1.5 1995/09/06 10:33:04 adam
225 * More work on present. Some log messages removed.
227 * Revision 1.4 1995/09/05 15:28:40 adam
228 * More work on search engine.
230 * Revision 1.3 1995/09/04 15:20:22 adam
233 * Revision 1.2 1995/09/04 12:33:43 adam
234 * Various cleanup. YAZ util used instead.
236 * Revision 1.1 1995/09/04 09:10:40 adam
237 * More work on index add/del/update.
238 * Merge sort implemented.
239 * Initial work on z39 server.
259 struct rpn_char_map_info {
264 static const char **rpn_char_map_handler (void *vp, const char **from, int len)
266 struct rpn_char_map_info *p = vp;
267 return zebra_maps_input (p->zm, p->reg_type, from, len);
270 static void rpn_char_map_prepare (ZServerInfo *zi, int reg_type,
271 struct rpn_char_map_info *map_info)
273 map_info->zm = zi->zebra_maps;
274 map_info->reg_type = reg_type;
275 dict_grep_cmap (zi->dict, map_info, rpn_char_map_handler);
282 Z_AttributesPlusTerm *zapt;
285 static int attr_find (AttrType *src, oid_value *attributeSetP)
287 while (src->major < src->zapt->num_attributes)
289 Z_AttributeElement *element;
291 element = src->zapt->attributeList[src->major];
292 if (src->type == *element->attributeType)
294 switch (element->which)
296 case Z_AttributeValue_numeric:
298 if (element->attributeSet && attributeSetP)
302 attrset = oid_getentbyoid (element->attributeSet);
303 *attributeSetP = attrset->value;
305 return *element->value.numeric;
307 case Z_AttributeValue_complex:
308 if (src->minor >= element->value.complex->num_list ||
309 element->value.complex->list[src->minor]->which !=
310 Z_StringOrNumeric_numeric)
313 if (element->attributeSet && attributeSetP)
317 attrset = oid_getentbyoid (element->attributeSet);
318 *attributeSetP = attrset->value;
320 return *element->value.complex->list[src->minor-1]->u.numeric;
330 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
350 static void add_isam_p (const char *info, struct grep_info *p)
352 if (p->isam_p_indx == p->isam_p_size)
354 ISAM_P *new_isam_p_buf;
359 p->isam_p_size = 2*p->isam_p_size + 100;
360 new_isam_p_buf = xmalloc (sizeof(*new_isam_p_buf) *
364 memcpy (new_isam_p_buf, p->isam_p_buf,
365 p->isam_p_indx * sizeof(*p->isam_p_buf));
366 xfree (p->isam_p_buf);
368 p->isam_p_buf = new_isam_p_buf;
371 new_term_no = xmalloc (sizeof(*new_term_no) *
375 memcpy (new_term_no, p->isam_p_buf,
376 p->isam_p_indx * sizeof(*p->term_no));
379 p->term_no = new_term_no;
382 assert (*info == sizeof(*p->isam_p_buf));
383 memcpy (p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
387 static int grep_handle (char *name, const char *info, void *p)
389 add_isam_p (info, p);
393 static int term_pre (ZebraMaps zebra_maps, int reg_type, const char **src,
394 const char *ct1, const char *ct2)
396 const char *s1, *s0 = *src;
399 /* skip white space */
402 if (ct1 && strchr (ct1, *s0))
404 if (ct2 && strchr (ct2, *s0))
407 map = zebra_maps_input (zebra_maps, reg_type, &s1, strlen(s1));
408 if (**map != *CHR_SPACE)
416 static int term_100 (ZebraMaps zebra_maps, int reg_type,
417 const char **src, char *dst, int space_split)
423 if (!term_pre (zebra_maps, reg_type, src, NULL, NULL))
429 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
430 if (space_split && **map == *CHR_SPACE)
434 if (!isalnum (*s1) && *s1 != '-')
444 static int term_101 (ZebraMaps zebra_maps, int reg_type,
445 const char **src, char *dst, int space_split)
451 if (!term_pre (zebra_maps, reg_type, src, "#", "#"))
465 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
466 if (space_split && **map == *CHR_SPACE)
482 static int term_103 (ZebraMaps zebra_maps, int reg_type, const char **src,
483 char *dst, int *errors, int space_split)
489 if (!term_pre (zebra_maps, reg_type, src, "^\\()[].*+?|", "("))
492 if (errors && *s0 == '+' && s0[1] && s0[2] == '+' && s0[3] &&
495 *errors = s0[1] - '0';
502 if (strchr ("^\\()[].*+?|-", *s0))
507 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
508 if (**map == *CHR_SPACE)
523 static int term_102 (ZebraMaps zebra_maps, int reg_type, const char **src,
524 char *dst, int space_split)
526 return term_103 (zebra_maps, reg_type, src, dst, NULL, space_split);
529 /* gen_regular_rel - generate regular expression from relation
530 * val: border value (inclusive)
531 * islt: 1 if <=; 0 if >=.
533 static void gen_regular_rel (char *dst, int val, int islt)
540 logf (LOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
544 strcpy (dst, "(-[0-9]+|(");
552 strcpy (dst, "([0-9]+|-(");
553 dst_p = strlen (dst);
563 dst_p = strlen (dst);
564 sprintf (numstr, "%d", val);
565 for (w = strlen(numstr); --w >= 0; pos++)
584 strcpy (dst + dst_p, numstr);
585 dst_p = strlen(dst) - pos - 1;
613 for (i = 0; i<pos; i++)
626 for (i=1; i<pos; i++)
627 strcat (dst, "[0-9]?");
631 for (i = 0; i <= pos; i++)
632 strcat (dst, "[0-9]");
633 strcat (dst, "[0-9]*");
638 static int relational_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
639 const char **term_sub,
641 oid_value attributeSet,
642 struct grep_info *grep_info,
650 char *term_tmp = term_dict + strlen(term_dict);
652 attr_init (&relation, zapt, 2);
653 relation_value = attr_find (&relation, NULL);
655 logf (LOG_DEBUG, "relation value=%d", relation_value);
656 switch (relation_value)
659 if (!term_100 (zi->zebra_maps, reg_type, term_sub, term_tmp, 1))
661 term_value = atoi (term_tmp);
662 logf (LOG_DEBUG, "Relation <");
663 gen_regular_rel (term_tmp, term_value-1, 1);
666 if (!term_100 (zi->zebra_maps, reg_type, term_sub, term_tmp, 1))
668 term_value = atoi (term_tmp);
669 logf (LOG_DEBUG, "Relation <=");
670 gen_regular_rel (term_tmp, term_value, 1);
673 if (!term_100 (zi->zebra_maps, reg_type, term_sub, term_tmp, 1))
675 term_value = atoi (term_tmp);
676 logf (LOG_DEBUG, "Relation >=");
677 gen_regular_rel (term_tmp, term_value, 0);
680 if (!term_100 (zi->zebra_maps, reg_type, term_sub, term_tmp, 1))
682 term_value = atoi (term_tmp);
683 logf (LOG_DEBUG, "Relation >");
684 gen_regular_rel (term_tmp, term_value+1, 0);
689 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_tmp);
690 r = dict_lookup_grep (zi->dict, term_dict, 0, grep_info, max_pos,
693 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
694 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
698 static int field_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
699 const char **term_sub,
700 oid_value attributeSet, struct grep_info *grep_info,
701 int reg_type, int complete_flag,
702 int num_bases, char **basenames)
704 char term_dict[2*IT_MAX_WORD+2];
707 int truncation_value;
710 oid_value curAttributeSet = attributeSet;
712 struct rpn_char_map_info rcmi;
713 int space_split = complete_flag ? 0 : 1;
715 rpn_char_map_prepare (zi, reg_type, &rcmi);
716 attr_init (&use, zapt, 1);
717 use_value = attr_find (&use, &curAttributeSet);
718 logf (LOG_DEBUG, "field_term, use value %d", use_value);
719 attr_init (&truncation, zapt, 5);
720 truncation_value = attr_find (&truncation, NULL);
721 logf (LOG_DEBUG, "truncation value %d", truncation_value);
726 for (base_no = 0; base_no < num_bases; base_no++)
729 data1_local_attribute *local_attr;
730 int max_pos, prefix_len = 0;
733 if ((r=att_getentbyatt (zi, &attp, curAttributeSet, use_value)))
735 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
736 curAttributeSet, use_value, r);
743 if (zebTargetInfo_curDatabase (zi->zti, basenames[base_no]))
745 zi->errCode = 109; /* Database unavailable */
746 zi->errString = basenames[base_no];
749 for (local_attr = attp.local_attributes; local_attr;
750 local_attr = local_attr->next)
754 ord = zebTargetInfo_lookupSU (zi->zti, attp.attset_ordinal,
759 term_dict[prefix_len++] = '|';
761 term_dict[prefix_len++] = '(';
762 term_dict[prefix_len++] = 1;
763 term_dict[prefix_len++] = ord;
770 term_dict[prefix_len++] = ')';
771 term_dict[prefix_len++] = 1;
772 term_dict[prefix_len++] = reg_type;
773 logf (LOG_LOG, "reg_type = %d", term_dict[prefix_len-1]);
774 term_dict[prefix_len] = '\0';
775 if (!relational_term (zi, zapt, &termp, term_dict,
776 attributeSet, grep_info, &max_pos, reg_type))
779 switch (truncation_value)
781 case -1: /* not specified */
782 case 100: /* do not truncate */
783 term_dict[j++] = '(';
784 if (!term_100 (zi->zebra_maps, reg_type,
785 &termp, term_dict + j, space_split))
787 strcat (term_dict, ")");
788 r = dict_lookup_grep (zi->dict, term_dict, 0, grep_info,
789 &max_pos, 0, grep_handle);
791 logf (LOG_WARN, "dict_lookup_grep err, trunc=none:%d", r);
793 case 1: /* right truncation */
794 term_dict[j++] = '(';
795 if (!term_100 (zi->zebra_maps, reg_type,
796 &termp, term_dict + j, space_split))
798 strcat (term_dict, ".*)");
799 dict_lookup_grep (zi->dict, term_dict, 0, grep_info,
800 &max_pos, 0, grep_handle);
802 case 2: /* left truncation */
803 case 3: /* left&right truncation */
806 case 101: /* process # in term */
807 term_dict[j++] = '(';
808 if (!term_101 (zi->zebra_maps, reg_type,
809 &termp, term_dict + j, space_split))
811 strcat (term_dict, ")");
812 r = dict_lookup_grep (zi->dict, term_dict, 0, grep_info,
813 &max_pos, 0, grep_handle);
815 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d", r);
817 case 102: /* Regexp-1 */
818 term_dict[j++] = '(';
819 if (!term_102 (zi->zebra_maps, reg_type,
820 &termp, term_dict + j, space_split))
822 strcat (term_dict, ")");
823 logf (LOG_DEBUG, "Regexp-1 tolerance=%d", r);
824 r = dict_lookup_grep (zi->dict, term_dict, 0, grep_info,
825 &max_pos, 0, grep_handle);
827 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
830 case 103: /* Regexp-2 */
832 term_dict[j++] = '(';
833 if (!term_103 (zi->zebra_maps, reg_type,
834 &termp, term_dict + j, &r, space_split))
836 strcat (term_dict, ")");
837 logf (LOG_DEBUG, "Regexp-2 tolerance=%d", r);
838 r = dict_lookup_grep (zi->dict, term_dict, r, grep_info,
839 &max_pos, 2, grep_handle);
841 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
848 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
852 static void trans_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
856 Z_Term *term = zapt->term;
858 sizez = term->u.general->len;
859 if (sizez > IT_MAX_WORD-1)
860 sizez = IT_MAX_WORD-1;
861 memcpy (termz, term->u.general->buf, sizez);
865 static void trans_scan_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
866 char *termz, int reg_type)
868 Z_Term *term = zapt->term;
870 const char *cp = (const char *) term->u.general->buf;
871 const char *cp_end = cp + term->u.general->len;
874 const char *space_map = NULL;
877 while ((len = (cp_end - cp)) > 0)
879 map = zebra_maps_input (zi->zebra_maps, reg_type, &cp, len);
880 if (**map == *CHR_SPACE)
885 for (src = space_map; *src; src++)
888 for (src = *map; *src; src++)
895 static RSET rpn_search_APT_relevance (ZServerInfo *zi,
896 Z_AttributesPlusTerm *zapt,
897 oid_value attributeSet,
898 int reg_type, int complete_flag,
899 int num_bases, char **basenames)
901 rset_relevance_parms parms;
902 char termz[IT_MAX_WORD+1];
903 const char *termp = termz;
904 struct grep_info grep_info;
909 parms.key_size = sizeof(struct it_key);
910 parms.max_rec = 1000;
911 parms.cmp = key_compare_it;
912 parms.get_pos = key_get_pos;
914 parms.isc = zi->isamc;
917 if (zapt->term->which != Z_Term_general)
922 trans_term (zi, zapt, termz);
925 grep_info.term_no = 0;
927 grep_info.isam_p_indx = 0;
928 grep_info.isam_p_size = 0;
929 grep_info.isam_p_buf = NULL;
932 r = field_term (zi, zapt, &termp, attributeSet, &grep_info,
933 reg_type, complete_flag, num_bases, basenames);
937 for (; term_index < grep_info.isam_p_indx; term_index++)
938 grep_info.term_no[term_index] = parms.no_terms;
942 parms.term_no = grep_info.term_no;
943 parms.isam_positions = grep_info.isam_p_buf;
944 parms.no_isam_positions = grep_info.isam_p_indx;
945 if (grep_info.isam_p_indx > 0)
946 result = rset_create (rset_kind_relevance, &parms);
948 result = rset_create (rset_kind_null, NULL);
950 xfree(grep_info.term_no);
952 xfree (grep_info.isam_p_buf);
956 static RSET rpn_proximity (ZServerInfo *zi, RSET rset1, RSET rset2,
958 int exclusion, int relation, int distance)
963 struct it_key buf1, buf2;
966 rset_temp_parms parms;
968 rsfd1 = rset_open (rset1, RSETF_READ|RSETF_SORT_SYSNO);
969 more1 = rset_read (rset1, rsfd1, &buf1);
971 rsfd2 = rset_open (rset2, RSETF_READ|RSETF_SORT_SYSNO);
972 more2 = rset_read (rset2, rsfd2, &buf2);
974 parms.key_size = sizeof (struct it_key);
975 parms.temp_path = res_get (zi->res, "setTmpDir");
976 result = rset_create (rset_kind_temp, &parms);
977 rsfd_result = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
979 logf (LOG_DEBUG, "rpn_proximity excl=%d ord=%d rel=%d dis=%d",
980 exclusion, ordered, relation, distance);
981 while (more1 && more2)
983 int cmp = key_compare_it (&buf1, &buf2);
985 more1 = rset_read (rset1, rsfd1, &buf1);
987 more2 = rset_read (rset2, rsfd2, &buf2);
990 int sysno = buf1.sysno;
994 seqno[n++] = buf1.seqno;
995 while ((more1 = rset_read (rset1, rsfd1, &buf1)) &&
998 seqno[n++] = buf1.seqno;
1001 for (i = 0; i<n; i++)
1003 int diff = buf2.seqno - seqno[i];
1004 int excl = exclusion;
1005 if (!ordered && diff < 0)
1010 if (diff < distance)
1014 if (diff <= distance)
1018 if (diff == distance)
1022 if (diff >= distance)
1026 if (diff > distance)
1030 if (diff != distance)
1035 rset_write (result, rsfd_result, &buf2);
1037 } while ((more2 = rset_read (rset2, rsfd2, &buf2)) &&
1038 sysno == buf2.sysno);
1041 rset_close (result, rsfd_result);
1042 rset_close (rset1, rsfd1);
1043 rset_close (rset2, rsfd2);
1047 static RSET rpn_prox (ZServerInfo *zi, RSET *rset, int rset_no)
1052 struct it_key **buf;
1055 rset_temp_parms parms;
1057 rsfd = xmalloc (sizeof(*rsfd)*rset_no);
1058 more = xmalloc (sizeof(*more)*rset_no);
1059 buf = xmalloc (sizeof(*buf)*rset_no);
1061 for (i = 0; i<rset_no; i++)
1063 buf[i] = xmalloc (sizeof(**buf));
1064 rsfd[i] = rset_open (rset[i], RSETF_READ|RSETF_SORT_SYSNO);
1065 if (!(more[i] = rset_read (rset[i], rsfd[i], buf[i])))
1069 rset_close (rset[i], rsfd[i]);
1076 return rset_create (rset_kind_null, NULL);
1079 parms.key_size = sizeof (struct it_key);
1080 parms.temp_path = res_get (zi->res, "setTmpDir");
1081 result = rset_create (rset_kind_temp, &parms);
1082 rsfd_result = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
1086 for (i = 1; i<rset_no; i++)
1095 cmp = key_compare_it (buf[i], buf[i-1]);
1098 more[i-1] = rset_read (rset[i-1], rsfd[i-1], buf[i-1]);
1103 if (buf[i-1]->seqno+1 != buf[i]->seqno)
1105 more[i-1] = rset_read (rset[i-1], rsfd[i-1], buf[i-1]);
1111 more[i] = rset_read (rset[i], rsfd[i], buf[i]);
1117 rset_write (result, rsfd_result, buf[0]);
1118 more[0] = rset_read (*rset, *rsfd, *buf);
1122 for (i = 0; i<rset_no; i++)
1124 rset_close (rset[i], rsfd[i]);
1127 rset_close (result, rsfd_result);
1134 static RSET rpn_search_APT_phrase (ZServerInfo *zi,
1135 Z_AttributesPlusTerm *zapt,
1136 oid_value attributeSet,
1137 int reg_type, int complete_flag,
1138 int num_bases, char **basenames)
1140 char termz[IT_MAX_WORD+1];
1141 const char *termp = termz;
1142 RSET rset[60], result;
1143 int i, r, rset_no = 0;
1144 struct grep_info grep_info;
1146 if (zapt->term->which != Z_Term_general)
1151 trans_term (zi, zapt, termz);
1154 grep_info.term_no = 0;
1156 grep_info.isam_p_size = 0;
1157 grep_info.isam_p_buf = NULL;
1161 logf (LOG_LOG, "APT_phrase termp=%s", termp);
1162 grep_info.isam_p_indx = 0;
1163 r = field_term (zi, zapt, &termp, attributeSet, &grep_info,
1164 reg_type, complete_flag, num_bases, basenames);
1167 rset[rset_no] = rset_trunc (zi, grep_info.isam_p_buf,
1168 grep_info.isam_p_indx);
1169 assert (rset[rset_no]);
1170 if (++rset_no >= sizeof(rset)/sizeof(*rset))
1174 xfree(grep_info.term_no);
1176 xfree (grep_info.isam_p_buf);
1178 return rset_create (rset_kind_null, NULL);
1179 else if (rset_no == 1)
1181 result = rpn_prox (zi, rset, rset_no);
1182 for (i = 0; i<rset_no; i++)
1183 rset_delete (rset[i]);
1187 static RSET rpn_search_APT_local (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1188 oid_value attributeSet)
1193 rset_temp_parms parms;
1194 char termz[IT_MAX_WORD+1];
1196 if (zapt->term->which != Z_Term_general)
1201 parms.key_size = sizeof (struct it_key);
1202 parms.temp_path = res_get (zi->res, "setTmpDir");
1203 result = rset_create (rset_kind_temp, &parms);
1204 rsfd = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
1206 trans_term (zi, zapt, termz);
1208 key.sysno = atoi (termz);
1211 rset_write (result, rsfd, &key);
1212 rset_close (result, rsfd);
1216 static RSET rpn_search_APT (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1217 oid_value attributeSet,
1218 int num_bases, char **basenames)
1221 char *search_type = NULL;
1224 zebra_maps_attr (zi->zebra_maps, zapt, ®_type, &search_type,
1227 logf (LOG_DEBUG, "reg_type=%c", reg_type);
1228 logf (LOG_DEBUG, "complete_flag=%d", complete_flag);
1229 logf (LOG_DEBUG, "search_type=%s", search_type);
1230 if (!strcmp (search_type, "phrase"))
1232 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1233 reg_type, complete_flag,
1234 num_bases, basenames);
1236 else if (!strcmp (search_type, "ranked"))
1238 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1239 reg_type, complete_flag,
1240 num_bases, basenames);
1242 else if (!strcmp (search_type, "local"))
1244 return rpn_search_APT_local (zi, zapt, attributeSet);
1250 static RSET rpn_search_ref (ZServerInfo *zi, Z_ResultSetId *resultSetId)
1254 if (!(s = resultSetGet (zi, resultSetId)))
1255 return rset_create (rset_kind_null, NULL);
1259 static RSET rpn_search_structure (ZServerInfo *zi, Z_RPNStructure *zs,
1260 oid_value attributeSet,
1261 int num_bases, char **basenames)
1264 if (zs->which == Z_RPNStructure_complex)
1266 Z_Operator *zop = zs->u.complex->roperator;
1267 rset_bool_parms bool_parms;
1271 bool_parms.rset_l = rpn_search_structure (zi, zs->u.complex->s1,
1273 num_bases, basenames);
1274 if (bool_parms.rset_l == NULL)
1276 if (rset_is_ranked(bool_parms.rset_l))
1278 bool_parms.rset_r = rpn_search_structure (zi, zs->u.complex->s2,
1280 num_bases, basenames);
1281 if (bool_parms.rset_r == NULL)
1283 rset_delete (bool_parms.rset_l);
1286 if (rset_is_ranked(bool_parms.rset_r))
1288 bool_parms.key_size = sizeof(struct it_key);
1289 bool_parms.cmp = key_compare_it;
1293 case Z_Operator_and:
1294 r = rset_create (soft ? rset_kind_sand:rset_kind_and, &bool_parms);
1297 r = rset_create (soft ? rset_kind_sor:rset_kind_or, &bool_parms);
1299 case Z_Operator_and_not:
1300 r = rset_create (soft ? rset_kind_snot:rset_kind_not, &bool_parms);
1302 case Z_Operator_prox:
1303 if (zop->u.prox->which != Z_ProxCode_known)
1308 if (*zop->u.prox->proximityUnitCode != Z_ProxUnit_word)
1310 char *val = odr_malloc (zi->odr, 16);
1312 zi->errString = val;
1313 sprintf (val, "%d", *zop->u.prox->proximityUnitCode);
1316 r = rpn_proximity (zi, bool_parms.rset_l, bool_parms.rset_r,
1317 *zop->u.prox->ordered,
1318 (!zop->u.prox->exclusion ? 0 :
1319 *zop->u.prox->exclusion),
1320 *zop->u.prox->relationType,
1321 *zop->u.prox->distance);
1328 else if (zs->which == Z_RPNStructure_simple)
1330 if (zs->u.simple->which == Z_Operand_APT)
1332 logf (LOG_DEBUG, "rpn_search_APT");
1333 r = rpn_search_APT (zi, zs->u.simple->u.attributesPlusTerm,
1334 attributeSet, num_bases, basenames);
1336 else if (zs->u.simple->which == Z_Operand_resultSetId)
1338 logf (LOG_DEBUG, "rpn_search_ref");
1339 r = rpn_search_ref (zi, zs->u.simple->u.resultSetId);
1355 void count_set_save (ZServerInfo *zi, RSET *r, int *count)
1362 rset_temp_parms parms;
1363 int maxResultSetSize = atoi (res_get_def (zi->res,
1364 "maxResultSetSize", "400"));
1365 logf (LOG_DEBUG, "count_set_save");
1367 parms.key_size = sizeof(struct it_key);
1368 parms.temp_path = res_get (zi->res, "setTmpDir");
1369 w = rset_create (rset_kind_temp, &parms);
1370 wfd = rset_open (w, RSETF_WRITE|RSETF_SORT_SYSNO);
1371 rfd = rset_open (*r, RSETF_READ|RSETF_SORT_SYSNO);
1372 while (rset_read (*r, rfd, &key))
1374 if (key.sysno != psysno)
1376 if (*count < maxResultSetSize)
1377 rset_write (w, wfd, &key);
1383 rset_close (*r, rfd);
1385 rset_close (w, wfd);
1387 logf (LOG_DEBUG, "%d keys, %d distinct sysnos", kno, *count);
1390 static void count_set (RSET r, int *count)
1397 logf (LOG_DEBUG, "count_set");
1399 rfd = rset_open (r, RSETF_READ|RSETF_SORT_SYSNO);
1400 while (rset_read (r, rfd, &key))
1402 if (key.sysno != psysno)
1409 rset_close (r, rfd);
1410 logf (LOG_DEBUG, "%d keys, %d distinct sysnos", kno, *count);
1413 int rpn_search (ZServerInfo *zi,
1414 Z_RPNQuery *rpn, int num_bases, char **basenames,
1415 const char *setname, int *hits)
1419 oid_value attributeSet;
1424 zi->errString = NULL;
1426 attrset = oid_getentbyoid (rpn->attributeSetId);
1427 attributeSet = attrset->value;
1428 rset = rpn_search_structure (zi, rpn->RPNStructure, attributeSet,
1429 num_bases, basenames);
1432 if (rset_is_volatile(rset))
1433 count_set_save(zi, &rset,hits);
1435 count_set (rset, hits);
1436 resultSetAdd (zi, setname, 1, rset);
1438 logf (LOG_DEBUG, "search error: %d", zi->errCode);
1442 struct scan_info_entry {
1448 struct scan_info_entry *list;
1454 static int scan_handle (char *name, const char *info, int pos, void *client)
1456 int len_prefix, idx;
1457 struct scan_info *scan_info = client;
1459 len_prefix = strlen(scan_info->prefix);
1460 if (memcmp (name, scan_info->prefix, len_prefix))
1463 idx = scan_info->after - pos + scan_info->before;
1466 scan_info->list[idx].term = odr_malloc (scan_info->odr,
1467 strlen(name + len_prefix)+1);
1468 strcpy (scan_info->list[idx].term, name + len_prefix);
1469 assert (*info == sizeof(ISAM_P));
1470 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAM_P));
1475 static void scan_term_untrans (ZServerInfo *zi, int reg_type,
1476 char **dstp, const char *src)
1478 char *dst = odr_malloc (zi->odr, strlen(src)*2+1);
1483 const char *cp = zebra_maps_output (zi->zebra_maps, reg_type, &src);
1490 int rpn_scan (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1491 oid_value attributeset,
1492 int num_bases, char **basenames,
1493 int *position, int *num_entries, struct scan_entry **list,
1497 int pos = *position;
1498 int num = *num_entries;
1502 char termz[IT_MAX_WORD+20];
1505 struct scan_info *scan_info_array;
1506 struct scan_entry *glist;
1507 int ords[32], ord_no = 0;
1511 char *search_type = NULL;
1514 logf (LOG_DEBUG, "scan, position = %d, num = %d", pos, num);
1516 if (attributeset == VAL_NONE)
1517 attributeset = VAL_BIB1;
1519 attr_init (&use, zapt, 1);
1520 use_value = attr_find (&use, &attributeset);
1521 logf (LOG_DEBUG, "use value %d", use_value);
1523 if (zebra_maps_attr (zi->zebra_maps, zapt, ®_type, &search_type,
1530 if (use_value == -1)
1532 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
1536 data1_local_attribute *local_attr;
1538 if ((r=att_getentbyatt (zi, &attp, attributeset, use_value)))
1540 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
1541 attributeset, use_value);
1547 if (zebTargetInfo_curDatabase (zi->zti, basenames[base_no]))
1549 zi->errString = basenames[base_no];
1550 return zi->errCode = 109; /* Database unavailable */
1552 for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
1553 local_attr = local_attr->next)
1557 ord = zebTargetInfo_lookupSU (zi->zti, attp.attset_ordinal,
1560 ords[ord_no++] = ord;
1564 return zi->errCode = 113;
1567 scan_info_array = odr_malloc (zi->odr, ord_no * sizeof(*scan_info_array));
1568 for (i = 0; i < ord_no; i++)
1570 int j, prefix_len = 0;
1571 int before_tmp = before, after_tmp = after;
1572 struct scan_info *scan_info = scan_info_array + i;
1573 struct rpn_char_map_info rcmi;
1575 rpn_char_map_prepare (zi, reg_type, &rcmi);
1577 scan_info->before = before;
1578 scan_info->after = after;
1579 scan_info->odr = zi->odr;
1581 scan_info->list = odr_malloc (zi->odr, (before+after)*
1582 sizeof(*scan_info->list));
1583 for (j = 0; j<before+after; j++)
1584 scan_info->list[j].term = NULL;
1585 termz[prefix_len++] = ords[i];
1586 termz[prefix_len++] = reg_type;
1587 termz[prefix_len] = 0;
1588 strcpy (scan_info->prefix, termz);
1590 trans_scan_term (zi, zapt, termz+prefix_len, reg_type);
1592 dict_scan (zi->dict, termz, &before_tmp, &after_tmp, scan_info,
1595 glist = odr_malloc (zi->odr, (before+after)*sizeof(*glist));
1596 for (i = 0; i < ord_no; i++)
1599 *status = BEND_SCAN_SUCCESS;
1600 for (i = 0; i<after; i++)
1603 const char *mterm = NULL;
1607 for (j = 0; j < ord_no; j++)
1609 if (ptr[j] < before+after &&
1610 (tst=scan_info_array[j].list[ptr[j]].term) &&
1611 (!mterm || strcmp (tst, mterm) < 0))
1619 scan_term_untrans (zi, reg_type, &glist[i+before].term, mterm);
1620 rset = rset_trunc (zi, &scan_info_array[j0].list[ptr[j0]].isam_p, 1);
1623 for (j = j0+1; j<ord_no; j++)
1625 if (ptr[j] < before+after &&
1626 (tst=scan_info_array[j].list[ptr[j]].term) &&
1627 !strcmp (tst, mterm))
1629 rset_bool_parms bool_parms;
1633 rset_trunc (zi, &scan_info_array[j].list[ptr[j]].isam_p, 1);
1635 bool_parms.key_size = sizeof(struct it_key);
1636 bool_parms.cmp = key_compare_it;
1637 bool_parms.rset_l = rset;
1638 bool_parms.rset_r = rset2;
1640 rset = rset_create (rset_kind_or, &bool_parms);
1645 count_set (rset, &glist[i+before].occurrences);
1650 *num_entries -= (after-i);
1651 *status = BEND_SCAN_PARTIAL;
1654 for (i = 0; i<ord_no; i++)
1657 for (i = 0; i<before; i++)
1660 const char *mterm = NULL;
1664 for (j = 0; j <ord_no; j++)
1666 if (ptr[j] < before &&
1667 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
1668 (!mterm || strcmp (tst, mterm) > 0))
1677 scan_term_untrans (zi, reg_type, &glist[before-1-i].term, mterm);
1680 (zi, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1);
1684 for (j = j0+1; j<ord_no; j++)
1686 if (ptr[j] < before &&
1687 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
1688 !strcmp (tst, mterm))
1690 rset_bool_parms bool_parms;
1693 rset2 = rset_trunc (zi,
1694 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1);
1696 bool_parms.key_size = sizeof(struct it_key);
1697 bool_parms.cmp = key_compare_it;
1698 bool_parms.rset_l = rset;
1699 bool_parms.rset_r = rset2;
1701 rset = rset_create (rset_kind_or, &bool_parms);
1706 count_set (rset, &glist[before-1-i].occurrences);
1712 *status = BEND_SCAN_PARTIAL;
1716 *list = glist + i; /* list is set to first 'real' entry */
1718 logf (LOG_DEBUG, "position = %d, num_entries = %d",
1719 *position, *num_entries);
1721 logf (LOG_DEBUG, "scan error: %d", zi->errCode);