2 * Copyright (C) 1994-1997, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.71 1997-12-18 10:54:24 adam
8 * New method result set method rs_hits that returns the number of
9 * hits in result-set (if known). The ranked result set returns real
10 * number of hits but only when not combined with other operands.
12 * Revision 1.70 1997/10/31 12:34:43 adam
13 * Changed a few log statements.
15 * Revision 1.69 1997/10/29 12:05:02 adam
16 * Server produces diagnostic "Unsupported Attribute Set" when appropriate.
18 * Revision 1.68 1997/10/27 14:33:06 adam
19 * Moved towards generic character mapping depending on "structure"
20 * field in abstract syntax file. Fixed a few memory leaks. Fixed
21 * bug with negative integers when doing searches with relational
24 * Revision 1.67 1997/09/29 09:06:10 adam
25 * Removed one static var in order to make this module thread safe.
27 * Revision 1.66 1997/09/25 14:58:03 adam
30 * Revision 1.65 1997/09/22 12:39:06 adam
31 * Added get_pos method for the ranked result sets.
33 * Revision 1.64 1997/09/18 08:59:20 adam
34 * Extra generic handle for the character mapping routines.
36 * Revision 1.63 1997/09/17 12:19:18 adam
37 * Zebra version corresponds to YAZ version 1.4.
38 * Changed Zebra server so that it doesn't depend on global common_resource.
40 * Revision 1.62 1997/09/05 15:30:09 adam
41 * Changed prototype for chr_map_input - added const.
42 * Added support for C++, headers uses extern "C" for public definitions.
44 * Revision 1.61 1997/02/10 10:21:14 adam
45 * Bug fix: in search terms character (^) wasn't observed.
47 * Revision 1.60 1997/01/31 11:10:34 adam
48 * Bug fix: Leading and trailing white space weren't removed in scan tokens.
50 * Revision 1.59 1997/01/17 11:31:46 adam
51 * Bug fix: complete phrase search didn't work.
53 * Revision 1.58 1996/12/23 15:30:45 adam
55 * Bug fix: result sets weren't deleted after server shut down.
57 * Revision 1.57 1996/11/11 13:38:02 adam
58 * Added proximity support in search.
60 * Revision 1.56 1996/11/08 11:10:32 adam
61 * Buffers used during file match got bigger.
62 * Compressed ISAM support everywhere.
63 * Bug fixes regarding masking characters in queries.
64 * Redesigned Regexp-2 queries.
66 * Revision 1.55 1996/11/04 14:07:44 adam
67 * Moved truncation code to trunc.c.
69 * Revision 1.54 1996/10/29 14:09:52 adam
70 * Use of cisam system - enabled if setting isamc is 1.
72 * Revision 1.53 1996/06/26 09:21:43 adam
73 * Bug fix: local attribute set wasn't obeyed in scan.
75 * Revision 1.52 1996/06/17 14:26:20 adam
76 * Function gen_regular_rel changed to handle negative numbers.
78 * Revision 1.51 1996/06/11 10:54:15 quinn
81 * Revision 1.50 1996/06/07 08:51:53 adam
82 * Bug fix: Character mapping was broken (introducued by last revision).
84 * Revision 1.49 1996/06/04 10:18:11 adam
85 * Search/scan uses character mapping module.
87 * Revision 1.48 1996/05/28 15:15:01 adam
88 * Bug fix: Didn't handle unknown database correctly.
90 * Revision 1.47 1996/05/15 18:36:28 adam
91 * Function trans_term transforms unsearchable characters to blanks.
93 * Revision 1.46 1996/05/15 11:57:56 adam
94 * Fixed bug introduced by set/field mapping in search operations.
96 * Revision 1.45 1996/05/14 11:34:00 adam
97 * Scan support in multiple registers/databases.
99 * Revision 1.44 1996/05/14 06:16:44 adam
100 * Compact use/set bytes used in search service.
102 * Revision 1.43 1996/05/09 09:54:43 adam
103 * Server supports maps from one logical attributes to a list of physical
105 * The extraction process doesn't make space consuming 'any' keys.
107 * Revision 1.42 1996/05/09 07:28:56 quinn
108 * Work towards phrases and multiple registers
110 * Revision 1.41 1996/03/20 09:36:43 adam
111 * Function dict_lookup_grep got extra parameter, init_pos, which marks
112 * from which position in pattern approximate pattern matching should occur.
113 * Approximate pattern matching is used in relevance=re-2.
115 * Revision 1.40 1996/02/02 13:44:44 adam
116 * The public dictionary functions simply use char instead of Dict_char
117 * to represent search strings. Dict_char is used internally only.
119 * Revision 1.39 1996/01/03 16:22:13 quinn
120 * operator->roperator
122 * Revision 1.38 1995/12/11 09:12:55 adam
123 * The rec_get function returns NULL if record doesn't exist - will
124 * happen in the server if the result set records have been deleted since
125 * the creation of the set (i.e. the search).
126 * The server saves a result temporarily if it is 'volatile', i.e. the
127 * set is register dependent.
129 * Revision 1.37 1995/12/06 15:05:28 adam
130 * More verbose in count_set.
132 * Revision 1.36 1995/12/06 12:41:27 adam
133 * New command 'stat' for the index program.
134 * Filenames can be read from stdin by specifying '-'.
135 * Bug fix/enhancement of the transformation from terms to regular
136 * expressons in the search engine.
138 * Revision 1.35 1995/11/27 09:29:00 adam
139 * Bug fixes regarding conversion to regular expressions.
141 * Revision 1.34 1995/11/16 17:00:56 adam
142 * Better logging of rpn query.
144 * Revision 1.33 1995/11/01 13:58:28 quinn
145 * Moving data1 to yaz/retrieval
147 * Revision 1.32 1995/10/27 14:00:11 adam
148 * Implemented detection of database availability.
150 * Revision 1.31 1995/10/17 18:02:10 adam
151 * New feature: databases. Implemented as prefix to words in dictionary.
153 * Revision 1.30 1995/10/16 09:32:38 adam
154 * More work on relational op.
156 * Revision 1.29 1995/10/13 16:01:49 adam
159 * Revision 1.28 1995/10/13 12:26:43 adam
160 * Optimization of truncation.
162 * Revision 1.27 1995/10/12 17:07:22 adam
165 * Revision 1.26 1995/10/12 12:40:54 adam
166 * Bug fixes in rpn_prox.
168 * Revision 1.25 1995/10/10 13:59:24 adam
169 * Function rset_open changed its wflag parameter to general flags.
171 * Revision 1.24 1995/10/09 16:18:37 adam
172 * Function dict_lookup_grep got extra client data parameter.
174 * Revision 1.23 1995/10/06 16:33:37 adam
175 * Use attribute mappings.
177 * Revision 1.22 1995/10/06 15:07:39 adam
178 * Structure 'local-number' handled.
180 * Revision 1.21 1995/10/06 13:52:06 adam
181 * Bug fixes. Handler may abort further scanning.
183 * Revision 1.20 1995/10/06 11:06:33 adam
184 * Scan entries include 'occurrences' now.
186 * Revision 1.19 1995/10/06 10:43:56 adam
187 * Scan added. 'occurrences' in scan entries not set yet.
189 * Revision 1.18 1995/10/04 16:57:20 adam
190 * Key input and merge sort in one pass.
192 * Revision 1.17 1995/10/04 12:55:17 adam
193 * Bug fix in ranked search. Use=Any keys inserted.
195 * Revision 1.16 1995/10/02 16:24:40 adam
196 * Use attribute actually used in search requests.
198 * Revision 1.15 1995/10/02 15:18:52 adam
199 * New member in recRetrieveCtrl: diagnostic.
201 * Revision 1.14 1995/09/28 12:10:32 adam
202 * Bug fixes. Field prefix used in queries.
204 * Revision 1.13 1995/09/18 14:17:50 adam
207 * Revision 1.12 1995/09/15 14:45:21 adam
209 * Work on truncation.
211 * Revision 1.11 1995/09/14 11:53:27 adam
212 * First work on regular expressions/truncations.
214 * Revision 1.10 1995/09/11 15:23:26 adam
215 * More work on relevance search.
217 * Revision 1.9 1995/09/11 13:09:35 adam
218 * More work on relevance feedback.
220 * Revision 1.8 1995/09/08 14:52:27 adam
221 * Minor changes. Dictionary is lower case now.
223 * Revision 1.7 1995/09/07 13:58:36 adam
224 * New parameter: result-set file descriptor (RSFD) to support multiple
225 * positions within the same result-set.
226 * Boolean operators: and, or, not implemented.
227 * Result-set references.
229 * Revision 1.6 1995/09/06 16:11:18 adam
230 * Option: only one word key per file.
232 * Revision 1.5 1995/09/06 10:33:04 adam
233 * More work on present. Some log messages removed.
235 * Revision 1.4 1995/09/05 15:28:40 adam
236 * More work on search engine.
238 * Revision 1.3 1995/09/04 15:20:22 adam
241 * Revision 1.2 1995/09/04 12:33:43 adam
242 * Various cleanup. YAZ util used instead.
244 * Revision 1.1 1995/09/04 09:10:40 adam
245 * More work on index add/del/update.
246 * Merge sort implemented.
247 * Initial work on z39 server.
267 struct rpn_char_map_info {
272 static const char **rpn_char_map_handler (void *vp, const char **from, int len)
274 struct rpn_char_map_info *p = vp;
275 return zebra_maps_input (p->zm, p->reg_type, from, len);
278 static void rpn_char_map_prepare (ZServerInfo *zi, int reg_type,
279 struct rpn_char_map_info *map_info)
281 map_info->zm = zi->zebra_maps;
282 map_info->reg_type = reg_type;
283 dict_grep_cmap (zi->dict, map_info, rpn_char_map_handler);
290 Z_AttributesPlusTerm *zapt;
293 static int attr_find (AttrType *src, oid_value *attributeSetP)
295 while (src->major < src->zapt->num_attributes)
297 Z_AttributeElement *element;
299 element = src->zapt->attributeList[src->major];
300 if (src->type == *element->attributeType)
302 switch (element->which)
304 case Z_AttributeValue_numeric:
306 if (element->attributeSet && attributeSetP)
310 attrset = oid_getentbyoid (element->attributeSet);
311 *attributeSetP = attrset->value;
313 return *element->value.numeric;
315 case Z_AttributeValue_complex:
316 if (src->minor >= element->value.complex->num_list ||
317 element->value.complex->list[src->minor]->which !=
318 Z_StringOrNumeric_numeric)
321 if (element->attributeSet && attributeSetP)
325 attrset = oid_getentbyoid (element->attributeSet);
326 *attributeSetP = attrset->value;
328 return *element->value.complex->list[src->minor-1]->u.numeric;
338 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
358 static void add_isam_p (const char *info, struct grep_info *p)
360 if (p->isam_p_indx == p->isam_p_size)
362 ISAM_P *new_isam_p_buf;
367 p->isam_p_size = 2*p->isam_p_size + 100;
368 new_isam_p_buf = xmalloc (sizeof(*new_isam_p_buf) *
372 memcpy (new_isam_p_buf, p->isam_p_buf,
373 p->isam_p_indx * sizeof(*p->isam_p_buf));
374 xfree (p->isam_p_buf);
376 p->isam_p_buf = new_isam_p_buf;
379 new_term_no = xmalloc (sizeof(*new_term_no) *
383 memcpy (new_term_no, p->isam_p_buf,
384 p->isam_p_indx * sizeof(*p->term_no));
387 p->term_no = new_term_no;
390 assert (*info == sizeof(*p->isam_p_buf));
391 memcpy (p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
395 static int grep_handle (char *name, const char *info, void *p)
397 add_isam_p (info, p);
401 static int term_pre (ZebraMaps zebra_maps, int reg_type, const char **src,
402 const char *ct1, const char *ct2)
404 const char *s1, *s0 = *src;
407 /* skip white space */
410 if (ct1 && strchr (ct1, *s0))
412 if (ct2 && strchr (ct2, *s0))
415 map = zebra_maps_input (zebra_maps, reg_type, &s1, strlen(s1));
416 if (**map != *CHR_SPACE)
424 static int term_100 (ZebraMaps zebra_maps, int reg_type,
425 const char **src, char *dst, int space_split)
431 if (!term_pre (zebra_maps, reg_type, src, NULL, NULL))
437 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
438 if (space_split && **map == *CHR_SPACE)
442 if (!isalnum (*s1) && *s1 != '-')
452 static int term_101 (ZebraMaps zebra_maps, int reg_type,
453 const char **src, char *dst, int space_split)
459 if (!term_pre (zebra_maps, reg_type, src, "#", "#"))
473 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
474 if (space_split && **map == *CHR_SPACE)
490 static int term_103 (ZebraMaps zebra_maps, int reg_type, const char **src,
491 char *dst, int *errors, int space_split)
497 if (!term_pre (zebra_maps, reg_type, src, "^\\()[].*+?|", "("))
500 if (errors && *s0 == '+' && s0[1] && s0[2] == '+' && s0[3] &&
503 *errors = s0[1] - '0';
510 if (strchr ("^\\()[].*+?|-", *s0))
515 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
516 if (**map == *CHR_SPACE)
531 static int term_102 (ZebraMaps zebra_maps, int reg_type, const char **src,
532 char *dst, int space_split)
534 return term_103 (zebra_maps, reg_type, src, dst, NULL, space_split);
537 /* gen_regular_rel - generate regular expression from relation
538 * val: border value (inclusive)
539 * islt: 1 if <=; 0 if >=.
541 static void gen_regular_rel (char *dst, int val, int islt)
548 logf (LOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
552 strcpy (dst, "(-[0-9]+|(");
560 strcpy (dst, "([0-9]+|-(");
561 dst_p = strlen (dst);
571 dst_p = strlen (dst);
572 sprintf (numstr, "%d", val);
573 for (w = strlen(numstr); --w >= 0; pos++)
592 strcpy (dst + dst_p, numstr);
593 dst_p = strlen(dst) - pos - 1;
621 for (i = 0; i<pos; i++)
634 for (i=1; i<pos; i++)
635 strcat (dst, "[0-9]?");
639 for (i = 0; i <= pos; i++)
640 strcat (dst, "[0-9]");
641 strcat (dst, "[0-9]*");
646 static int relational_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
647 const char **term_sub,
649 oid_value attributeSet,
650 struct grep_info *grep_info,
658 char *term_tmp = term_dict + strlen(term_dict);
660 attr_init (&relation, zapt, 2);
661 relation_value = attr_find (&relation, NULL);
663 logf (LOG_DEBUG, "relation value=%d", relation_value);
664 switch (relation_value)
667 if (!term_100 (zi->zebra_maps, reg_type, term_sub, term_tmp, 1))
669 term_value = atoi (term_tmp);
670 logf (LOG_DEBUG, "Relation <");
671 gen_regular_rel (term_tmp, term_value-1, 1);
674 if (!term_100 (zi->zebra_maps, reg_type, term_sub, term_tmp, 1))
676 term_value = atoi (term_tmp);
677 logf (LOG_DEBUG, "Relation <=");
678 gen_regular_rel (term_tmp, term_value, 1);
681 if (!term_100 (zi->zebra_maps, reg_type, term_sub, term_tmp, 1))
683 term_value = atoi (term_tmp);
684 logf (LOG_DEBUG, "Relation >=");
685 gen_regular_rel (term_tmp, term_value, 0);
688 if (!term_100 (zi->zebra_maps, reg_type, term_sub, term_tmp, 1))
690 term_value = atoi (term_tmp);
691 logf (LOG_DEBUG, "Relation >");
692 gen_regular_rel (term_tmp, term_value+1, 0);
697 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_tmp);
698 r = dict_lookup_grep (zi->dict, term_dict, 0, grep_info, max_pos,
701 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
702 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
706 static int field_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
707 const char **term_sub,
708 oid_value attributeSet, struct grep_info *grep_info,
709 int reg_type, int complete_flag,
710 int num_bases, char **basenames)
712 char term_dict[2*IT_MAX_WORD+2];
715 int truncation_value;
718 oid_value curAttributeSet = attributeSet;
720 struct rpn_char_map_info rcmi;
721 int space_split = complete_flag ? 0 : 1;
723 rpn_char_map_prepare (zi, reg_type, &rcmi);
724 attr_init (&use, zapt, 1);
725 use_value = attr_find (&use, &curAttributeSet);
726 logf (LOG_DEBUG, "field_term, use value %d", use_value);
727 attr_init (&truncation, zapt, 5);
728 truncation_value = attr_find (&truncation, NULL);
729 logf (LOG_DEBUG, "truncation value %d", truncation_value);
734 for (base_no = 0; base_no < num_bases; base_no++)
737 data1_local_attribute *local_attr;
738 int max_pos, prefix_len = 0;
741 if ((r=att_getentbyatt (zi, &attp, curAttributeSet, use_value)))
743 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
744 curAttributeSet, use_value, r);
751 if (zebTargetInfo_curDatabase (zi->zti, basenames[base_no]))
753 zi->errCode = 109; /* Database unavailable */
754 zi->errString = basenames[base_no];
757 for (local_attr = attp.local_attributes; local_attr;
758 local_attr = local_attr->next)
762 ord = zebTargetInfo_lookupSU (zi->zti, attp.attset_ordinal,
767 term_dict[prefix_len++] = '|';
769 term_dict[prefix_len++] = '(';
770 term_dict[prefix_len++] = 1;
771 term_dict[prefix_len++] = ord;
778 term_dict[prefix_len++] = ')';
779 term_dict[prefix_len++] = 1;
780 term_dict[prefix_len++] = reg_type;
781 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
782 term_dict[prefix_len] = '\0';
783 if (!relational_term (zi, zapt, &termp, term_dict,
784 attributeSet, grep_info, &max_pos, reg_type))
787 switch (truncation_value)
789 case -1: /* not specified */
790 case 100: /* do not truncate */
791 term_dict[j++] = '(';
792 if (!term_100 (zi->zebra_maps, reg_type,
793 &termp, term_dict + j, space_split))
795 strcat (term_dict, ")");
796 r = dict_lookup_grep (zi->dict, term_dict, 0, grep_info,
797 &max_pos, 0, grep_handle);
799 logf (LOG_WARN, "dict_lookup_grep err, trunc=none:%d", r);
801 case 1: /* right truncation */
802 term_dict[j++] = '(';
803 if (!term_100 (zi->zebra_maps, reg_type,
804 &termp, term_dict + j, space_split))
806 strcat (term_dict, ".*)");
807 dict_lookup_grep (zi->dict, term_dict, 0, grep_info,
808 &max_pos, 0, grep_handle);
810 case 2: /* left truncation */
811 case 3: /* left&right truncation */
814 case 101: /* process # in term */
815 term_dict[j++] = '(';
816 if (!term_101 (zi->zebra_maps, reg_type,
817 &termp, term_dict + j, space_split))
819 strcat (term_dict, ")");
820 r = dict_lookup_grep (zi->dict, term_dict, 0, grep_info,
821 &max_pos, 0, grep_handle);
823 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d", r);
825 case 102: /* Regexp-1 */
826 term_dict[j++] = '(';
827 if (!term_102 (zi->zebra_maps, reg_type,
828 &termp, term_dict + j, space_split))
830 strcat (term_dict, ")");
831 logf (LOG_DEBUG, "Regexp-1 tolerance=%d", r);
832 r = dict_lookup_grep (zi->dict, term_dict, 0, grep_info,
833 &max_pos, 0, grep_handle);
835 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
838 case 103: /* Regexp-2 */
840 term_dict[j++] = '(';
841 if (!term_103 (zi->zebra_maps, reg_type,
842 &termp, term_dict + j, &r, space_split))
844 strcat (term_dict, ")");
845 logf (LOG_DEBUG, "Regexp-2 tolerance=%d", r);
846 r = dict_lookup_grep (zi->dict, term_dict, r, grep_info,
847 &max_pos, 2, grep_handle);
849 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
856 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
860 static void trans_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
864 Z_Term *term = zapt->term;
866 sizez = term->u.general->len;
867 if (sizez > IT_MAX_WORD-1)
868 sizez = IT_MAX_WORD-1;
869 memcpy (termz, term->u.general->buf, sizez);
873 static void trans_scan_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
874 char *termz, int reg_type)
876 Z_Term *term = zapt->term;
878 const char *cp = (const char *) term->u.general->buf;
879 const char *cp_end = cp + term->u.general->len;
882 const char *space_map = NULL;
885 while ((len = (cp_end - cp)) > 0)
887 map = zebra_maps_input (zi->zebra_maps, reg_type, &cp, len);
888 if (**map == *CHR_SPACE)
893 for (src = space_map; *src; src++)
896 for (src = *map; *src; src++)
903 static RSET rpn_search_APT_relevance (ZServerInfo *zi,
904 Z_AttributesPlusTerm *zapt,
905 oid_value attributeSet,
906 int reg_type, int complete_flag,
907 int num_bases, char **basenames)
909 rset_relevance_parms parms;
910 char termz[IT_MAX_WORD+1];
911 const char *termp = termz;
912 struct grep_info grep_info;
917 parms.key_size = sizeof(struct it_key);
919 parms.cmp = key_compare_it;
920 parms.get_pos = key_get_pos;
922 parms.isc = zi->isamc;
924 parms.method = RSREL_METHOD_A;
926 if (zapt->term->which != Z_Term_general)
931 trans_term (zi, zapt, termz);
934 grep_info.term_no = 0;
936 grep_info.isam_p_indx = 0;
937 grep_info.isam_p_size = 0;
938 grep_info.isam_p_buf = NULL;
941 r = field_term (zi, zapt, &termp, attributeSet, &grep_info,
942 reg_type, complete_flag, num_bases, basenames);
946 for (; term_index < grep_info.isam_p_indx; term_index++)
947 grep_info.term_no[term_index] = parms.no_terms;
951 parms.term_no = grep_info.term_no;
952 parms.isam_positions = grep_info.isam_p_buf;
953 parms.no_isam_positions = grep_info.isam_p_indx;
954 if (grep_info.isam_p_indx > 0)
955 result = rset_create (rset_kind_relevance, &parms);
957 result = rset_create (rset_kind_null, NULL);
959 xfree(grep_info.term_no);
961 xfree (grep_info.isam_p_buf);
965 static RSET rpn_proximity (ZServerInfo *zi, RSET rset1, RSET rset2,
967 int exclusion, int relation, int distance)
972 struct it_key buf1, buf2;
975 rset_temp_parms parms;
977 rsfd1 = rset_open (rset1, RSETF_READ|RSETF_SORT_SYSNO);
978 more1 = rset_read (rset1, rsfd1, &buf1);
980 rsfd2 = rset_open (rset2, RSETF_READ|RSETF_SORT_SYSNO);
981 more2 = rset_read (rset2, rsfd2, &buf2);
983 parms.key_size = sizeof (struct it_key);
984 parms.temp_path = res_get (zi->res, "setTmpDir");
985 result = rset_create (rset_kind_temp, &parms);
986 rsfd_result = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
988 logf (LOG_DEBUG, "rpn_proximity excl=%d ord=%d rel=%d dis=%d",
989 exclusion, ordered, relation, distance);
990 while (more1 && more2)
992 int cmp = key_compare_it (&buf1, &buf2);
994 more1 = rset_read (rset1, rsfd1, &buf1);
996 more2 = rset_read (rset2, rsfd2, &buf2);
999 int sysno = buf1.sysno;
1003 seqno[n++] = buf1.seqno;
1004 while ((more1 = rset_read (rset1, rsfd1, &buf1)) &&
1005 sysno == buf1.sysno)
1007 seqno[n++] = buf1.seqno;
1010 for (i = 0; i<n; i++)
1012 int diff = buf2.seqno - seqno[i];
1013 int excl = exclusion;
1014 if (!ordered && diff < 0)
1019 if (diff < distance)
1023 if (diff <= distance)
1027 if (diff == distance)
1031 if (diff >= distance)
1035 if (diff > distance)
1039 if (diff != distance)
1044 rset_write (result, rsfd_result, &buf2);
1046 } while ((more2 = rset_read (rset2, rsfd2, &buf2)) &&
1047 sysno == buf2.sysno);
1050 rset_close (result, rsfd_result);
1051 rset_close (rset1, rsfd1);
1052 rset_close (rset2, rsfd2);
1056 static RSET rpn_prox (ZServerInfo *zi, RSET *rset, int rset_no)
1061 struct it_key **buf;
1064 rset_temp_parms parms;
1066 rsfd = xmalloc (sizeof(*rsfd)*rset_no);
1067 more = xmalloc (sizeof(*more)*rset_no);
1068 buf = xmalloc (sizeof(*buf)*rset_no);
1070 for (i = 0; i<rset_no; i++)
1072 buf[i] = xmalloc (sizeof(**buf));
1073 rsfd[i] = rset_open (rset[i], RSETF_READ|RSETF_SORT_SYSNO);
1074 if (!(more[i] = rset_read (rset[i], rsfd[i], buf[i])))
1078 rset_close (rset[i], rsfd[i]);
1085 return rset_create (rset_kind_null, NULL);
1088 parms.key_size = sizeof (struct it_key);
1089 parms.temp_path = res_get (zi->res, "setTmpDir");
1090 result = rset_create (rset_kind_temp, &parms);
1091 rsfd_result = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
1095 for (i = 1; i<rset_no; i++)
1104 cmp = key_compare_it (buf[i], buf[i-1]);
1107 more[i-1] = rset_read (rset[i-1], rsfd[i-1], buf[i-1]);
1112 if (buf[i-1]->seqno+1 != buf[i]->seqno)
1114 more[i-1] = rset_read (rset[i-1], rsfd[i-1], buf[i-1]);
1120 more[i] = rset_read (rset[i], rsfd[i], buf[i]);
1126 rset_write (result, rsfd_result, buf[0]);
1127 more[0] = rset_read (*rset, *rsfd, *buf);
1131 for (i = 0; i<rset_no; i++)
1133 rset_close (rset[i], rsfd[i]);
1136 rset_close (result, rsfd_result);
1143 static RSET rpn_search_APT_phrase (ZServerInfo *zi,
1144 Z_AttributesPlusTerm *zapt,
1145 oid_value attributeSet,
1146 int reg_type, int complete_flag,
1147 int num_bases, char **basenames)
1149 char termz[IT_MAX_WORD+1];
1150 const char *termp = termz;
1151 RSET rset[60], result;
1152 int i, r, rset_no = 0;
1153 struct grep_info grep_info;
1155 if (zapt->term->which != Z_Term_general)
1160 trans_term (zi, zapt, termz);
1163 grep_info.term_no = 0;
1165 grep_info.isam_p_size = 0;
1166 grep_info.isam_p_buf = NULL;
1170 logf (LOG_DEBUG, "APT_phrase termp=%s", termp);
1171 grep_info.isam_p_indx = 0;
1172 r = field_term (zi, zapt, &termp, attributeSet, &grep_info,
1173 reg_type, complete_flag, num_bases, basenames);
1176 rset[rset_no] = rset_trunc (zi, grep_info.isam_p_buf,
1177 grep_info.isam_p_indx);
1178 assert (rset[rset_no]);
1179 if (++rset_no >= sizeof(rset)/sizeof(*rset))
1183 xfree(grep_info.term_no);
1185 xfree (grep_info.isam_p_buf);
1187 return rset_create (rset_kind_null, NULL);
1188 else if (rset_no == 1)
1190 result = rpn_prox (zi, rset, rset_no);
1191 for (i = 0; i<rset_no; i++)
1192 rset_delete (rset[i]);
1196 static RSET rpn_search_APT_local (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1197 oid_value attributeSet)
1202 rset_temp_parms parms;
1203 char termz[IT_MAX_WORD+1];
1205 if (zapt->term->which != Z_Term_general)
1210 parms.key_size = sizeof (struct it_key);
1211 parms.temp_path = res_get (zi->res, "setTmpDir");
1212 result = rset_create (rset_kind_temp, &parms);
1213 rsfd = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
1215 trans_term (zi, zapt, termz);
1217 key.sysno = atoi (termz);
1220 rset_write (result, rsfd, &key);
1221 rset_close (result, rsfd);
1225 static RSET rpn_search_APT (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1226 oid_value attributeSet,
1227 int num_bases, char **basenames)
1230 char *search_type = NULL;
1233 zebra_maps_attr (zi->zebra_maps, zapt, ®_type, &search_type,
1236 logf (LOG_DEBUG, "reg_type=%c", reg_type);
1237 logf (LOG_DEBUG, "complete_flag=%d", complete_flag);
1238 logf (LOG_DEBUG, "search_type=%s", search_type);
1239 if (!strcmp (search_type, "phrase"))
1241 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1242 reg_type, complete_flag,
1243 num_bases, basenames);
1245 else if (!strcmp (search_type, "ranked"))
1247 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1248 reg_type, complete_flag,
1249 num_bases, basenames);
1251 else if (!strcmp (search_type, "local"))
1253 return rpn_search_APT_local (zi, zapt, attributeSet);
1259 static RSET rpn_search_ref (ZServerInfo *zi, Z_ResultSetId *resultSetId)
1263 if (!(s = resultSetGet (zi, resultSetId)))
1264 return rset_create (rset_kind_null, NULL);
1268 static RSET rpn_search_structure (ZServerInfo *zi, Z_RPNStructure *zs,
1269 oid_value attributeSet,
1270 int num_bases, char **basenames)
1273 if (zs->which == Z_RPNStructure_complex)
1275 Z_Operator *zop = zs->u.complex->roperator;
1276 rset_bool_parms bool_parms;
1280 bool_parms.rset_l = rpn_search_structure (zi, zs->u.complex->s1,
1282 num_bases, basenames);
1283 if (bool_parms.rset_l == NULL)
1285 if (rset_is_ranked(bool_parms.rset_l))
1287 bool_parms.rset_r = rpn_search_structure (zi, zs->u.complex->s2,
1289 num_bases, basenames);
1290 if (bool_parms.rset_r == NULL)
1292 rset_delete (bool_parms.rset_l);
1295 if (rset_is_ranked(bool_parms.rset_r))
1297 bool_parms.key_size = sizeof(struct it_key);
1298 bool_parms.cmp = key_compare_it;
1302 case Z_Operator_and:
1303 r = rset_create (soft ? rset_kind_sand:rset_kind_and, &bool_parms);
1306 r = rset_create (soft ? rset_kind_sor:rset_kind_or, &bool_parms);
1308 case Z_Operator_and_not:
1309 r = rset_create (soft ? rset_kind_snot:rset_kind_not, &bool_parms);
1311 case Z_Operator_prox:
1312 if (zop->u.prox->which != Z_ProxCode_known)
1317 if (*zop->u.prox->proximityUnitCode != Z_ProxUnit_word)
1319 char *val = odr_malloc (zi->odr, 16);
1321 zi->errString = val;
1322 sprintf (val, "%d", *zop->u.prox->proximityUnitCode);
1325 r = rpn_proximity (zi, bool_parms.rset_l, bool_parms.rset_r,
1326 *zop->u.prox->ordered,
1327 (!zop->u.prox->exclusion ? 0 :
1328 *zop->u.prox->exclusion),
1329 *zop->u.prox->relationType,
1330 *zop->u.prox->distance);
1337 else if (zs->which == Z_RPNStructure_simple)
1339 if (zs->u.simple->which == Z_Operand_APT)
1341 logf (LOG_DEBUG, "rpn_search_APT");
1342 r = rpn_search_APT (zi, zs->u.simple->u.attributesPlusTerm,
1343 attributeSet, num_bases, basenames);
1345 else if (zs->u.simple->which == Z_Operand_resultSetId)
1347 logf (LOG_DEBUG, "rpn_search_ref");
1348 r = rpn_search_ref (zi, zs->u.simple->u.resultSetId);
1364 void count_set_save (ZServerInfo *zi, RSET *r, int *count)
1371 rset_temp_parms parms;
1372 int maxResultSetSize = atoi (res_get_def (zi->res,
1373 "maxResultSetSize", "400"));
1374 logf (LOG_DEBUG, "count_set_save");
1376 parms.key_size = sizeof(struct it_key);
1377 parms.temp_path = res_get (zi->res, "setTmpDir");
1378 w = rset_create (rset_kind_temp, &parms);
1379 wfd = rset_open (w, RSETF_WRITE|RSETF_SORT_SYSNO);
1380 rfd = rset_open (*r, RSETF_READ|RSETF_SORT_SYSNO);
1381 while (rset_read (*r, rfd, &key))
1383 if (key.sysno != psysno)
1385 if (*count < maxResultSetSize)
1386 rset_write (w, wfd, &key);
1392 rset_close (*r, rfd);
1394 rset_close (w, wfd);
1396 logf (LOG_DEBUG, "%d keys, %d distinct sysnos", kno, *count);
1399 static void count_set (RSET r, int *count)
1406 logf (LOG_DEBUG, "count_set");
1409 rfd = rset_open (r, RSETF_READ|RSETF_SORT_SYSNO);
1410 while (rset_read (r, rfd, &key))
1412 if (key.sysno != psysno)
1419 rset_close (r, rfd);
1420 logf (LOG_DEBUG, "%d keys, %d distinct sysnos", kno, *count);
1423 int rpn_search (ZServerInfo *zi,
1424 Z_RPNQuery *rpn, int num_bases, char **basenames,
1425 const char *setname, int *hits)
1430 oid_value attributeSet;
1435 zi->errString = NULL;
1437 attrset = oid_getentbyoid (rpn->attributeSetId);
1438 attributeSet = attrset->value;
1439 rset = rpn_search_structure (zi, rpn->RPNStructure, attributeSet,
1440 num_bases, basenames);
1443 if (rset_is_volatile(rset))
1444 count_set_save(zi, &rset, hits);
1445 else if ((i = rset_hits (rset)) >= 0)
1448 count_set (rset, hits);
1449 resultSetAdd (zi, setname, 1, rset);
1451 logf (LOG_DEBUG, "search error: %d", zi->errCode);
1455 struct scan_info_entry {
1461 struct scan_info_entry *list;
1467 static int scan_handle (char *name, const char *info, int pos, void *client)
1469 int len_prefix, idx;
1470 struct scan_info *scan_info = client;
1472 len_prefix = strlen(scan_info->prefix);
1473 if (memcmp (name, scan_info->prefix, len_prefix))
1476 idx = scan_info->after - pos + scan_info->before;
1479 scan_info->list[idx].term = odr_malloc (scan_info->odr,
1480 strlen(name + len_prefix)+1);
1481 strcpy (scan_info->list[idx].term, name + len_prefix);
1482 assert (*info == sizeof(ISAM_P));
1483 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAM_P));
1488 static void scan_term_untrans (ZServerInfo *zi, int reg_type,
1489 char **dstp, const char *src)
1491 char *dst = odr_malloc (zi->odr, strlen(src)*2+1);
1496 const char *cp = zebra_maps_output (zi->zebra_maps, reg_type, &src);
1503 int rpn_scan (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1504 oid_value attributeset,
1505 int num_bases, char **basenames,
1506 int *position, int *num_entries, struct scan_entry **list,
1510 int pos = *position;
1511 int num = *num_entries;
1515 char termz[IT_MAX_WORD+20];
1518 struct scan_info *scan_info_array;
1519 struct scan_entry *glist;
1520 int ords[32], ord_no = 0;
1524 char *search_type = NULL;
1527 logf (LOG_DEBUG, "scan, position = %d, num = %d", pos, num);
1529 if (attributeset == VAL_NONE)
1530 attributeset = VAL_BIB1;
1532 attr_init (&use, zapt, 1);
1533 use_value = attr_find (&use, &attributeset);
1534 logf (LOG_DEBUG, "use value %d", use_value);
1536 if (zebra_maps_attr (zi->zebra_maps, zapt, ®_type, &search_type,
1543 if (use_value == -1)
1545 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
1549 data1_local_attribute *local_attr;
1551 if ((r=att_getentbyatt (zi, &attp, attributeset, use_value)))
1553 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
1554 attributeset, use_value);
1560 if (zebTargetInfo_curDatabase (zi->zti, basenames[base_no]))
1562 zi->errString = basenames[base_no];
1563 return zi->errCode = 109; /* Database unavailable */
1565 for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
1566 local_attr = local_attr->next)
1570 ord = zebTargetInfo_lookupSU (zi->zti, attp.attset_ordinal,
1573 ords[ord_no++] = ord;
1577 return zi->errCode = 113;
1580 scan_info_array = odr_malloc (zi->odr, ord_no * sizeof(*scan_info_array));
1581 for (i = 0; i < ord_no; i++)
1583 int j, prefix_len = 0;
1584 int before_tmp = before, after_tmp = after;
1585 struct scan_info *scan_info = scan_info_array + i;
1586 struct rpn_char_map_info rcmi;
1588 rpn_char_map_prepare (zi, reg_type, &rcmi);
1590 scan_info->before = before;
1591 scan_info->after = after;
1592 scan_info->odr = zi->odr;
1594 scan_info->list = odr_malloc (zi->odr, (before+after)*
1595 sizeof(*scan_info->list));
1596 for (j = 0; j<before+after; j++)
1597 scan_info->list[j].term = NULL;
1598 termz[prefix_len++] = ords[i];
1599 termz[prefix_len++] = reg_type;
1600 termz[prefix_len] = 0;
1601 strcpy (scan_info->prefix, termz);
1603 trans_scan_term (zi, zapt, termz+prefix_len, reg_type);
1605 dict_scan (zi->dict, termz, &before_tmp, &after_tmp, scan_info,
1608 glist = odr_malloc (zi->odr, (before+after)*sizeof(*glist));
1609 for (i = 0; i < ord_no; i++)
1612 *status = BEND_SCAN_SUCCESS;
1613 for (i = 0; i<after; i++)
1616 const char *mterm = NULL;
1620 for (j = 0; j < ord_no; j++)
1622 if (ptr[j] < before+after &&
1623 (tst=scan_info_array[j].list[ptr[j]].term) &&
1624 (!mterm || strcmp (tst, mterm) < 0))
1632 scan_term_untrans (zi, reg_type, &glist[i+before].term, mterm);
1633 rset = rset_trunc (zi, &scan_info_array[j0].list[ptr[j0]].isam_p, 1);
1636 for (j = j0+1; j<ord_no; j++)
1638 if (ptr[j] < before+after &&
1639 (tst=scan_info_array[j].list[ptr[j]].term) &&
1640 !strcmp (tst, mterm))
1642 rset_bool_parms bool_parms;
1646 rset_trunc (zi, &scan_info_array[j].list[ptr[j]].isam_p, 1);
1648 bool_parms.key_size = sizeof(struct it_key);
1649 bool_parms.cmp = key_compare_it;
1650 bool_parms.rset_l = rset;
1651 bool_parms.rset_r = rset2;
1653 rset = rset_create (rset_kind_or, &bool_parms);
1658 count_set (rset, &glist[i+before].occurrences);
1663 *num_entries -= (after-i);
1664 *status = BEND_SCAN_PARTIAL;
1667 for (i = 0; i<ord_no; i++)
1670 for (i = 0; i<before; i++)
1673 const char *mterm = NULL;
1677 for (j = 0; j <ord_no; j++)
1679 if (ptr[j] < before &&
1680 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
1681 (!mterm || strcmp (tst, mterm) > 0))
1690 scan_term_untrans (zi, reg_type, &glist[before-1-i].term, mterm);
1693 (zi, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1);
1697 for (j = j0+1; j<ord_no; j++)
1699 if (ptr[j] < before &&
1700 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
1701 !strcmp (tst, mterm))
1703 rset_bool_parms bool_parms;
1706 rset2 = rset_trunc (zi,
1707 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1);
1709 bool_parms.key_size = sizeof(struct it_key);
1710 bool_parms.cmp = key_compare_it;
1711 bool_parms.rset_l = rset;
1712 bool_parms.rset_r = rset2;
1714 rset = rset_create (rset_kind_or, &bool_parms);
1719 count_set (rset, &glist[before-1-i].occurrences);
1725 *status = BEND_SCAN_PARTIAL;
1729 *list = glist + i; /* list is set to first 'real' entry */
1731 logf (LOG_DEBUG, "position = %d, num_entries = %d",
1732 *position, *num_entries);
1734 logf (LOG_DEBUG, "scan error: %d", zi->errCode);