2 * Copyright (C) 1994-1998, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.72 1998-01-07 13:53:41 adam
8 * Queries using simple ranked operands returns right number of hits.
10 * Revision 1.71 1997/12/18 10:54:24 adam
11 * New method result set method rs_hits that returns the number of
12 * hits in result-set (if known). The ranked result set returns real
13 * number of hits but only when not combined with other operands.
15 * Revision 1.70 1997/10/31 12:34:43 adam
16 * Changed a few log statements.
18 * Revision 1.69 1997/10/29 12:05:02 adam
19 * Server produces diagnostic "Unsupported Attribute Set" when appropriate.
21 * Revision 1.68 1997/10/27 14:33:06 adam
22 * Moved towards generic character mapping depending on "structure"
23 * field in abstract syntax file. Fixed a few memory leaks. Fixed
24 * bug with negative integers when doing searches with relational
27 * Revision 1.67 1997/09/29 09:06:10 adam
28 * Removed one static var in order to make this module thread safe.
30 * Revision 1.66 1997/09/25 14:58:03 adam
33 * Revision 1.65 1997/09/22 12:39:06 adam
34 * Added get_pos method for the ranked result sets.
36 * Revision 1.64 1997/09/18 08:59:20 adam
37 * Extra generic handle for the character mapping routines.
39 * Revision 1.63 1997/09/17 12:19:18 adam
40 * Zebra version corresponds to YAZ version 1.4.
41 * Changed Zebra server so that it doesn't depend on global common_resource.
43 * Revision 1.62 1997/09/05 15:30:09 adam
44 * Changed prototype for chr_map_input - added const.
45 * Added support for C++, headers uses extern "C" for public definitions.
47 * Revision 1.61 1997/02/10 10:21:14 adam
48 * Bug fix: in search terms character (^) wasn't observed.
50 * Revision 1.60 1997/01/31 11:10:34 adam
51 * Bug fix: Leading and trailing white space weren't removed in scan tokens.
53 * Revision 1.59 1997/01/17 11:31:46 adam
54 * Bug fix: complete phrase search didn't work.
56 * Revision 1.58 1996/12/23 15:30:45 adam
58 * Bug fix: result sets weren't deleted after server shut down.
60 * Revision 1.57 1996/11/11 13:38:02 adam
61 * Added proximity support in search.
63 * Revision 1.56 1996/11/08 11:10:32 adam
64 * Buffers used during file match got bigger.
65 * Compressed ISAM support everywhere.
66 * Bug fixes regarding masking characters in queries.
67 * Redesigned Regexp-2 queries.
69 * Revision 1.55 1996/11/04 14:07:44 adam
70 * Moved truncation code to trunc.c.
72 * Revision 1.54 1996/10/29 14:09:52 adam
73 * Use of cisam system - enabled if setting isamc is 1.
75 * Revision 1.53 1996/06/26 09:21:43 adam
76 * Bug fix: local attribute set wasn't obeyed in scan.
78 * Revision 1.52 1996/06/17 14:26:20 adam
79 * Function gen_regular_rel changed to handle negative numbers.
81 * Revision 1.51 1996/06/11 10:54:15 quinn
84 * Revision 1.50 1996/06/07 08:51:53 adam
85 * Bug fix: Character mapping was broken (introducued by last revision).
87 * Revision 1.49 1996/06/04 10:18:11 adam
88 * Search/scan uses character mapping module.
90 * Revision 1.48 1996/05/28 15:15:01 adam
91 * Bug fix: Didn't handle unknown database correctly.
93 * Revision 1.47 1996/05/15 18:36:28 adam
94 * Function trans_term transforms unsearchable characters to blanks.
96 * Revision 1.46 1996/05/15 11:57:56 adam
97 * Fixed bug introduced by set/field mapping in search operations.
99 * Revision 1.45 1996/05/14 11:34:00 adam
100 * Scan support in multiple registers/databases.
102 * Revision 1.44 1996/05/14 06:16:44 adam
103 * Compact use/set bytes used in search service.
105 * Revision 1.43 1996/05/09 09:54:43 adam
106 * Server supports maps from one logical attributes to a list of physical
108 * The extraction process doesn't make space consuming 'any' keys.
110 * Revision 1.42 1996/05/09 07:28:56 quinn
111 * Work towards phrases and multiple registers
113 * Revision 1.41 1996/03/20 09:36:43 adam
114 * Function dict_lookup_grep got extra parameter, init_pos, which marks
115 * from which position in pattern approximate pattern matching should occur.
116 * Approximate pattern matching is used in relevance=re-2.
118 * Revision 1.40 1996/02/02 13:44:44 adam
119 * The public dictionary functions simply use char instead of Dict_char
120 * to represent search strings. Dict_char is used internally only.
122 * Revision 1.39 1996/01/03 16:22:13 quinn
123 * operator->roperator
125 * Revision 1.38 1995/12/11 09:12:55 adam
126 * The rec_get function returns NULL if record doesn't exist - will
127 * happen in the server if the result set records have been deleted since
128 * the creation of the set (i.e. the search).
129 * The server saves a result temporarily if it is 'volatile', i.e. the
130 * set is register dependent.
132 * Revision 1.37 1995/12/06 15:05:28 adam
133 * More verbose in count_set.
135 * Revision 1.36 1995/12/06 12:41:27 adam
136 * New command 'stat' for the index program.
137 * Filenames can be read from stdin by specifying '-'.
138 * Bug fix/enhancement of the transformation from terms to regular
139 * expressons in the search engine.
141 * Revision 1.35 1995/11/27 09:29:00 adam
142 * Bug fixes regarding conversion to regular expressions.
144 * Revision 1.34 1995/11/16 17:00:56 adam
145 * Better logging of rpn query.
147 * Revision 1.33 1995/11/01 13:58:28 quinn
148 * Moving data1 to yaz/retrieval
150 * Revision 1.32 1995/10/27 14:00:11 adam
151 * Implemented detection of database availability.
153 * Revision 1.31 1995/10/17 18:02:10 adam
154 * New feature: databases. Implemented as prefix to words in dictionary.
156 * Revision 1.30 1995/10/16 09:32:38 adam
157 * More work on relational op.
159 * Revision 1.29 1995/10/13 16:01:49 adam
162 * Revision 1.28 1995/10/13 12:26:43 adam
163 * Optimization of truncation.
165 * Revision 1.27 1995/10/12 17:07:22 adam
168 * Revision 1.26 1995/10/12 12:40:54 adam
169 * Bug fixes in rpn_prox.
171 * Revision 1.25 1995/10/10 13:59:24 adam
172 * Function rset_open changed its wflag parameter to general flags.
174 * Revision 1.24 1995/10/09 16:18:37 adam
175 * Function dict_lookup_grep got extra client data parameter.
177 * Revision 1.23 1995/10/06 16:33:37 adam
178 * Use attribute mappings.
180 * Revision 1.22 1995/10/06 15:07:39 adam
181 * Structure 'local-number' handled.
183 * Revision 1.21 1995/10/06 13:52:06 adam
184 * Bug fixes. Handler may abort further scanning.
186 * Revision 1.20 1995/10/06 11:06:33 adam
187 * Scan entries include 'occurrences' now.
189 * Revision 1.19 1995/10/06 10:43:56 adam
190 * Scan added. 'occurrences' in scan entries not set yet.
192 * Revision 1.18 1995/10/04 16:57:20 adam
193 * Key input and merge sort in one pass.
195 * Revision 1.17 1995/10/04 12:55:17 adam
196 * Bug fix in ranked search. Use=Any keys inserted.
198 * Revision 1.16 1995/10/02 16:24:40 adam
199 * Use attribute actually used in search requests.
201 * Revision 1.15 1995/10/02 15:18:52 adam
202 * New member in recRetrieveCtrl: diagnostic.
204 * Revision 1.14 1995/09/28 12:10:32 adam
205 * Bug fixes. Field prefix used in queries.
207 * Revision 1.13 1995/09/18 14:17:50 adam
210 * Revision 1.12 1995/09/15 14:45:21 adam
212 * Work on truncation.
214 * Revision 1.11 1995/09/14 11:53:27 adam
215 * First work on regular expressions/truncations.
217 * Revision 1.10 1995/09/11 15:23:26 adam
218 * More work on relevance search.
220 * Revision 1.9 1995/09/11 13:09:35 adam
221 * More work on relevance feedback.
223 * Revision 1.8 1995/09/08 14:52:27 adam
224 * Minor changes. Dictionary is lower case now.
226 * Revision 1.7 1995/09/07 13:58:36 adam
227 * New parameter: result-set file descriptor (RSFD) to support multiple
228 * positions within the same result-set.
229 * Boolean operators: and, or, not implemented.
230 * Result-set references.
232 * Revision 1.6 1995/09/06 16:11:18 adam
233 * Option: only one word key per file.
235 * Revision 1.5 1995/09/06 10:33:04 adam
236 * More work on present. Some log messages removed.
238 * Revision 1.4 1995/09/05 15:28:40 adam
239 * More work on search engine.
241 * Revision 1.3 1995/09/04 15:20:22 adam
244 * Revision 1.2 1995/09/04 12:33:43 adam
245 * Various cleanup. YAZ util used instead.
247 * Revision 1.1 1995/09/04 09:10:40 adam
248 * More work on index add/del/update.
249 * Merge sort implemented.
250 * Initial work on z39 server.
270 struct rpn_char_map_info {
275 static const char **rpn_char_map_handler (void *vp, const char **from, int len)
277 struct rpn_char_map_info *p = vp;
278 return zebra_maps_input (p->zm, p->reg_type, from, len);
281 static void rpn_char_map_prepare (ZServerInfo *zi, int reg_type,
282 struct rpn_char_map_info *map_info)
284 map_info->zm = zi->zebra_maps;
285 map_info->reg_type = reg_type;
286 dict_grep_cmap (zi->dict, map_info, rpn_char_map_handler);
293 Z_AttributesPlusTerm *zapt;
296 static int attr_find (AttrType *src, oid_value *attributeSetP)
298 while (src->major < src->zapt->num_attributes)
300 Z_AttributeElement *element;
302 element = src->zapt->attributeList[src->major];
303 if (src->type == *element->attributeType)
305 switch (element->which)
307 case Z_AttributeValue_numeric:
309 if (element->attributeSet && attributeSetP)
313 attrset = oid_getentbyoid (element->attributeSet);
314 *attributeSetP = attrset->value;
316 return *element->value.numeric;
318 case Z_AttributeValue_complex:
319 if (src->minor >= element->value.complex->num_list ||
320 element->value.complex->list[src->minor]->which !=
321 Z_StringOrNumeric_numeric)
324 if (element->attributeSet && attributeSetP)
328 attrset = oid_getentbyoid (element->attributeSet);
329 *attributeSetP = attrset->value;
331 return *element->value.complex->list[src->minor-1]->u.numeric;
341 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
361 static void add_isam_p (const char *info, struct grep_info *p)
363 if (p->isam_p_indx == p->isam_p_size)
365 ISAM_P *new_isam_p_buf;
369 p->isam_p_size = 2*p->isam_p_size + 100;
370 new_isam_p_buf = xmalloc (sizeof(*new_isam_p_buf) *
374 memcpy (new_isam_p_buf, p->isam_p_buf,
375 p->isam_p_indx * sizeof(*p->isam_p_buf));
376 xfree (p->isam_p_buf);
378 p->isam_p_buf = new_isam_p_buf;
381 new_term_no = xmalloc (sizeof(*new_term_no) *
385 memcpy (new_term_no, p->isam_p_buf,
386 p->isam_p_indx * sizeof(*p->term_no));
389 p->term_no = new_term_no;
392 assert (*info == sizeof(*p->isam_p_buf));
393 memcpy (p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
397 static int grep_handle (char *name, const char *info, void *p)
399 add_isam_p (info, p);
403 static int term_pre (ZebraMaps zebra_maps, int reg_type, const char **src,
404 const char *ct1, const char *ct2)
406 const char *s1, *s0 = *src;
409 /* skip white space */
412 if (ct1 && strchr (ct1, *s0))
414 if (ct2 && strchr (ct2, *s0))
417 map = zebra_maps_input (zebra_maps, reg_type, &s1, strlen(s1));
418 if (**map != *CHR_SPACE)
426 static int term_100 (ZebraMaps zebra_maps, int reg_type,
427 const char **src, char *dst, int space_split)
433 if (!term_pre (zebra_maps, reg_type, src, NULL, NULL))
439 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
440 if (space_split && **map == *CHR_SPACE)
444 if (!isalnum (*s1) && *s1 != '-')
454 static int term_101 (ZebraMaps zebra_maps, int reg_type,
455 const char **src, char *dst, int space_split)
461 if (!term_pre (zebra_maps, reg_type, src, "#", "#"))
475 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
476 if (space_split && **map == *CHR_SPACE)
492 static int term_103 (ZebraMaps zebra_maps, int reg_type, const char **src,
493 char *dst, int *errors, int space_split)
499 if (!term_pre (zebra_maps, reg_type, src, "^\\()[].*+?|", "("))
502 if (errors && *s0 == '+' && s0[1] && s0[2] == '+' && s0[3] &&
505 *errors = s0[1] - '0';
512 if (strchr ("^\\()[].*+?|-", *s0))
517 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
518 if (**map == *CHR_SPACE)
533 static int term_102 (ZebraMaps zebra_maps, int reg_type, const char **src,
534 char *dst, int space_split)
536 return term_103 (zebra_maps, reg_type, src, dst, NULL, space_split);
539 /* gen_regular_rel - generate regular expression from relation
540 * val: border value (inclusive)
541 * islt: 1 if <=; 0 if >=.
543 static void gen_regular_rel (char *dst, int val, int islt)
550 logf (LOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
554 strcpy (dst, "(-[0-9]+|(");
562 strcpy (dst, "([0-9]+|-(");
563 dst_p = strlen (dst);
573 dst_p = strlen (dst);
574 sprintf (numstr, "%d", val);
575 for (w = strlen(numstr); --w >= 0; pos++)
594 strcpy (dst + dst_p, numstr);
595 dst_p = strlen(dst) - pos - 1;
623 for (i = 0; i<pos; i++)
636 for (i=1; i<pos; i++)
637 strcat (dst, "[0-9]?");
641 for (i = 0; i <= pos; i++)
642 strcat (dst, "[0-9]");
643 strcat (dst, "[0-9]*");
648 static int relational_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
649 const char **term_sub,
651 oid_value attributeSet,
652 struct grep_info *grep_info,
660 char *term_tmp = term_dict + strlen(term_dict);
662 attr_init (&relation, zapt, 2);
663 relation_value = attr_find (&relation, NULL);
665 logf (LOG_DEBUG, "relation value=%d", relation_value);
666 switch (relation_value)
669 if (!term_100 (zi->zebra_maps, reg_type, term_sub, term_tmp, 1))
671 term_value = atoi (term_tmp);
672 logf (LOG_DEBUG, "Relation <");
673 gen_regular_rel (term_tmp, term_value-1, 1);
676 if (!term_100 (zi->zebra_maps, reg_type, term_sub, term_tmp, 1))
678 term_value = atoi (term_tmp);
679 logf (LOG_DEBUG, "Relation <=");
680 gen_regular_rel (term_tmp, term_value, 1);
683 if (!term_100 (zi->zebra_maps, reg_type, term_sub, term_tmp, 1))
685 term_value = atoi (term_tmp);
686 logf (LOG_DEBUG, "Relation >=");
687 gen_regular_rel (term_tmp, term_value, 0);
690 if (!term_100 (zi->zebra_maps, reg_type, term_sub, term_tmp, 1))
692 term_value = atoi (term_tmp);
693 logf (LOG_DEBUG, "Relation >");
694 gen_regular_rel (term_tmp, term_value+1, 0);
699 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_tmp);
700 r = dict_lookup_grep (zi->dict, term_dict, 0, grep_info, max_pos,
703 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
704 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
708 static int field_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
709 const char **term_sub,
710 oid_value attributeSet, struct grep_info *grep_info,
711 int reg_type, int complete_flag,
712 int num_bases, char **basenames)
714 char term_dict[2*IT_MAX_WORD+2];
717 int truncation_value;
720 oid_value curAttributeSet = attributeSet;
722 struct rpn_char_map_info rcmi;
723 int space_split = complete_flag ? 0 : 1;
725 rpn_char_map_prepare (zi, reg_type, &rcmi);
726 attr_init (&use, zapt, 1);
727 use_value = attr_find (&use, &curAttributeSet);
728 logf (LOG_DEBUG, "field_term, use value %d", use_value);
729 attr_init (&truncation, zapt, 5);
730 truncation_value = attr_find (&truncation, NULL);
731 logf (LOG_DEBUG, "truncation value %d", truncation_value);
736 for (base_no = 0; base_no < num_bases; base_no++)
739 data1_local_attribute *local_attr;
740 int max_pos, prefix_len = 0;
743 if ((r=att_getentbyatt (zi, &attp, curAttributeSet, use_value)))
745 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
746 curAttributeSet, use_value, r);
753 if (zebTargetInfo_curDatabase (zi->zti, basenames[base_no]))
755 zi->errCode = 109; /* Database unavailable */
756 zi->errString = basenames[base_no];
759 for (local_attr = attp.local_attributes; local_attr;
760 local_attr = local_attr->next)
764 ord = zebTargetInfo_lookupSU (zi->zti, attp.attset_ordinal,
769 term_dict[prefix_len++] = '|';
771 term_dict[prefix_len++] = '(';
772 term_dict[prefix_len++] = 1;
773 term_dict[prefix_len++] = ord;
780 term_dict[prefix_len++] = ')';
781 term_dict[prefix_len++] = 1;
782 term_dict[prefix_len++] = reg_type;
783 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
784 term_dict[prefix_len] = '\0';
785 if (!relational_term (zi, zapt, &termp, term_dict,
786 attributeSet, grep_info, &max_pos, reg_type))
789 switch (truncation_value)
791 case -1: /* not specified */
792 case 100: /* do not truncate */
793 term_dict[j++] = '(';
794 if (!term_100 (zi->zebra_maps, reg_type,
795 &termp, term_dict + j, space_split))
797 strcat (term_dict, ")");
798 r = dict_lookup_grep (zi->dict, term_dict, 0, grep_info,
799 &max_pos, 0, grep_handle);
801 logf (LOG_WARN, "dict_lookup_grep err, trunc=none:%d", r);
803 case 1: /* right truncation */
804 term_dict[j++] = '(';
805 if (!term_100 (zi->zebra_maps, reg_type,
806 &termp, term_dict + j, space_split))
808 strcat (term_dict, ".*)");
809 dict_lookup_grep (zi->dict, term_dict, 0, grep_info,
810 &max_pos, 0, grep_handle);
812 case 2: /* left truncation */
813 case 3: /* left&right truncation */
816 case 101: /* process # in term */
817 term_dict[j++] = '(';
818 if (!term_101 (zi->zebra_maps, reg_type,
819 &termp, term_dict + j, space_split))
821 strcat (term_dict, ")");
822 r = dict_lookup_grep (zi->dict, term_dict, 0, grep_info,
823 &max_pos, 0, grep_handle);
825 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d", r);
827 case 102: /* Regexp-1 */
828 term_dict[j++] = '(';
829 if (!term_102 (zi->zebra_maps, reg_type,
830 &termp, term_dict + j, space_split))
832 strcat (term_dict, ")");
833 logf (LOG_DEBUG, "Regexp-1 tolerance=%d", r);
834 r = dict_lookup_grep (zi->dict, term_dict, 0, grep_info,
835 &max_pos, 0, grep_handle);
837 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
840 case 103: /* Regexp-2 */
842 term_dict[j++] = '(';
843 if (!term_103 (zi->zebra_maps, reg_type,
844 &termp, term_dict + j, &r, space_split))
846 strcat (term_dict, ")");
847 logf (LOG_DEBUG, "Regexp-2 tolerance=%d", r);
848 r = dict_lookup_grep (zi->dict, term_dict, r, grep_info,
849 &max_pos, 2, grep_handle);
851 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
858 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
862 static void trans_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
866 Z_Term *term = zapt->term;
868 sizez = term->u.general->len;
869 if (sizez > IT_MAX_WORD-1)
870 sizez = IT_MAX_WORD-1;
871 memcpy (termz, term->u.general->buf, sizez);
875 static void trans_scan_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
876 char *termz, int reg_type)
878 Z_Term *term = zapt->term;
880 const char *cp = (const char *) term->u.general->buf;
881 const char *cp_end = cp + term->u.general->len;
884 const char *space_map = NULL;
887 while ((len = (cp_end - cp)) > 0)
889 map = zebra_maps_input (zi->zebra_maps, reg_type, &cp, len);
890 if (**map == *CHR_SPACE)
895 for (src = space_map; *src; src++)
898 for (src = *map; *src; src++)
905 static RSET rpn_search_APT_relevance (ZServerInfo *zi,
906 Z_AttributesPlusTerm *zapt,
907 oid_value attributeSet,
908 int reg_type, int complete_flag,
909 int num_bases, char **basenames)
911 rset_relevance_parms parms;
912 char termz[IT_MAX_WORD+1];
913 const char *termp = termz;
914 struct grep_info grep_info;
919 parms.key_size = sizeof(struct it_key);
921 parms.cmp = key_compare_it;
922 parms.get_pos = key_get_pos;
924 parms.isc = zi->isamc;
926 parms.method = RSREL_METHOD_A;
928 if (zapt->term->which != Z_Term_general)
933 trans_term (zi, zapt, termz);
936 grep_info.term_no = 0;
938 grep_info.isam_p_indx = 0;
939 grep_info.isam_p_size = 0;
940 grep_info.isam_p_buf = NULL;
943 r = field_term (zi, zapt, &termp, attributeSet, &grep_info,
944 reg_type, complete_flag, num_bases, basenames);
948 for (; term_index < grep_info.isam_p_indx; term_index++)
949 grep_info.term_no[term_index] = parms.no_terms;
953 parms.term_no = grep_info.term_no;
954 parms.isam_positions = grep_info.isam_p_buf;
955 parms.no_isam_positions = grep_info.isam_p_indx;
956 if (grep_info.isam_p_indx > 0)
957 result = rset_create (rset_kind_relevance, &parms);
959 result = rset_create (rset_kind_null, NULL);
961 xfree(grep_info.term_no);
963 xfree (grep_info.isam_p_buf);
967 static RSET rpn_proximity (ZServerInfo *zi, RSET rset1, RSET rset2,
969 int exclusion, int relation, int distance)
974 struct it_key buf1, buf2;
977 rset_temp_parms parms;
979 rsfd1 = rset_open (rset1, RSETF_READ|RSETF_SORT_SYSNO);
980 more1 = rset_read (rset1, rsfd1, &buf1);
982 rsfd2 = rset_open (rset2, RSETF_READ|RSETF_SORT_SYSNO);
983 more2 = rset_read (rset2, rsfd2, &buf2);
985 parms.key_size = sizeof (struct it_key);
986 parms.temp_path = res_get (zi->res, "setTmpDir");
987 result = rset_create (rset_kind_temp, &parms);
988 rsfd_result = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
990 logf (LOG_DEBUG, "rpn_proximity excl=%d ord=%d rel=%d dis=%d",
991 exclusion, ordered, relation, distance);
992 while (more1 && more2)
994 int cmp = key_compare_it (&buf1, &buf2);
996 more1 = rset_read (rset1, rsfd1, &buf1);
998 more2 = rset_read (rset2, rsfd2, &buf2);
1001 int sysno = buf1.sysno;
1005 seqno[n++] = buf1.seqno;
1006 while ((more1 = rset_read (rset1, rsfd1, &buf1)) &&
1007 sysno == buf1.sysno)
1009 seqno[n++] = buf1.seqno;
1012 for (i = 0; i<n; i++)
1014 int diff = buf2.seqno - seqno[i];
1015 int excl = exclusion;
1016 if (!ordered && diff < 0)
1021 if (diff < distance)
1025 if (diff <= distance)
1029 if (diff == distance)
1033 if (diff >= distance)
1037 if (diff > distance)
1041 if (diff != distance)
1046 rset_write (result, rsfd_result, &buf2);
1048 } while ((more2 = rset_read (rset2, rsfd2, &buf2)) &&
1049 sysno == buf2.sysno);
1052 rset_close (result, rsfd_result);
1053 rset_close (rset1, rsfd1);
1054 rset_close (rset2, rsfd2);
1058 static RSET rpn_prox (ZServerInfo *zi, RSET *rset, int rset_no)
1063 struct it_key **buf;
1066 rset_temp_parms parms;
1068 rsfd = xmalloc (sizeof(*rsfd)*rset_no);
1069 more = xmalloc (sizeof(*more)*rset_no);
1070 buf = xmalloc (sizeof(*buf)*rset_no);
1072 for (i = 0; i<rset_no; i++)
1074 buf[i] = xmalloc (sizeof(**buf));
1075 rsfd[i] = rset_open (rset[i], RSETF_READ|RSETF_SORT_SYSNO);
1076 if (!(more[i] = rset_read (rset[i], rsfd[i], buf[i])))
1080 rset_close (rset[i], rsfd[i]);
1087 return rset_create (rset_kind_null, NULL);
1090 parms.key_size = sizeof (struct it_key);
1091 parms.temp_path = res_get (zi->res, "setTmpDir");
1092 result = rset_create (rset_kind_temp, &parms);
1093 rsfd_result = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
1097 for (i = 1; i<rset_no; i++)
1106 cmp = key_compare_it (buf[i], buf[i-1]);
1109 more[i-1] = rset_read (rset[i-1], rsfd[i-1], buf[i-1]);
1114 if (buf[i-1]->seqno+1 != buf[i]->seqno)
1116 more[i-1] = rset_read (rset[i-1], rsfd[i-1], buf[i-1]);
1122 more[i] = rset_read (rset[i], rsfd[i], buf[i]);
1128 rset_write (result, rsfd_result, buf[0]);
1129 more[0] = rset_read (*rset, *rsfd, *buf);
1133 for (i = 0; i<rset_no; i++)
1135 rset_close (rset[i], rsfd[i]);
1138 rset_close (result, rsfd_result);
1145 static RSET rpn_search_APT_phrase (ZServerInfo *zi,
1146 Z_AttributesPlusTerm *zapt,
1147 oid_value attributeSet,
1148 int reg_type, int complete_flag,
1149 int num_bases, char **basenames)
1151 char termz[IT_MAX_WORD+1];
1152 const char *termp = termz;
1153 RSET rset[60], result;
1154 int i, r, rset_no = 0;
1155 struct grep_info grep_info;
1157 if (zapt->term->which != Z_Term_general)
1162 trans_term (zi, zapt, termz);
1165 grep_info.term_no = 0;
1167 grep_info.isam_p_size = 0;
1168 grep_info.isam_p_buf = NULL;
1172 logf (LOG_DEBUG, "APT_phrase termp=%s", termp);
1173 grep_info.isam_p_indx = 0;
1174 r = field_term (zi, zapt, &termp, attributeSet, &grep_info,
1175 reg_type, complete_flag, num_bases, basenames);
1178 rset[rset_no] = rset_trunc (zi, grep_info.isam_p_buf,
1179 grep_info.isam_p_indx);
1180 assert (rset[rset_no]);
1181 if (++rset_no >= sizeof(rset)/sizeof(*rset))
1185 xfree(grep_info.term_no);
1187 xfree (grep_info.isam_p_buf);
1189 return rset_create (rset_kind_null, NULL);
1190 else if (rset_no == 1)
1192 result = rpn_prox (zi, rset, rset_no);
1193 for (i = 0; i<rset_no; i++)
1194 rset_delete (rset[i]);
1198 static RSET rpn_search_APT_local (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1199 oid_value attributeSet)
1204 rset_temp_parms parms;
1205 char termz[IT_MAX_WORD+1];
1207 if (zapt->term->which != Z_Term_general)
1212 parms.key_size = sizeof (struct it_key);
1213 parms.temp_path = res_get (zi->res, "setTmpDir");
1214 result = rset_create (rset_kind_temp, &parms);
1215 rsfd = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
1217 trans_term (zi, zapt, termz);
1219 key.sysno = atoi (termz);
1222 rset_write (result, rsfd, &key);
1223 rset_close (result, rsfd);
1227 static RSET rpn_search_APT (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1228 oid_value attributeSet,
1229 int num_bases, char **basenames)
1232 char *search_type = NULL;
1235 zebra_maps_attr (zi->zebra_maps, zapt, ®_type, &search_type,
1238 logf (LOG_DEBUG, "reg_type=%c", reg_type);
1239 logf (LOG_DEBUG, "complete_flag=%d", complete_flag);
1240 logf (LOG_DEBUG, "search_type=%s", search_type);
1241 if (!strcmp (search_type, "phrase"))
1243 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1244 reg_type, complete_flag,
1245 num_bases, basenames);
1247 else if (!strcmp (search_type, "ranked"))
1249 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1250 reg_type, complete_flag,
1251 num_bases, basenames);
1253 else if (!strcmp (search_type, "local"))
1255 return rpn_search_APT_local (zi, zapt, attributeSet);
1261 static RSET rpn_search_ref (ZServerInfo *zi, Z_ResultSetId *resultSetId)
1265 if (!(s = resultSetGet (zi, resultSetId)))
1266 return rset_create (rset_kind_null, NULL);
1270 static RSET rpn_search_structure (ZServerInfo *zi, Z_RPNStructure *zs,
1271 oid_value attributeSet,
1272 int num_bases, char **basenames)
1275 if (zs->which == Z_RPNStructure_complex)
1277 Z_Operator *zop = zs->u.complex->roperator;
1278 rset_bool_parms bool_parms;
1282 bool_parms.rset_l = rpn_search_structure (zi, zs->u.complex->s1,
1284 num_bases, basenames);
1285 if (bool_parms.rset_l == NULL)
1287 if (rset_is_ranked(bool_parms.rset_l))
1289 bool_parms.rset_r = rpn_search_structure (zi, zs->u.complex->s2,
1291 num_bases, basenames);
1292 if (bool_parms.rset_r == NULL)
1294 rset_delete (bool_parms.rset_l);
1297 if (rset_is_ranked(bool_parms.rset_r))
1299 bool_parms.key_size = sizeof(struct it_key);
1300 bool_parms.cmp = key_compare_it;
1304 case Z_Operator_and:
1305 r = rset_create (soft ? rset_kind_sand:rset_kind_and, &bool_parms);
1308 r = rset_create (soft ? rset_kind_sor:rset_kind_or, &bool_parms);
1310 case Z_Operator_and_not:
1311 r = rset_create (soft ? rset_kind_snot:rset_kind_not, &bool_parms);
1313 case Z_Operator_prox:
1314 if (zop->u.prox->which != Z_ProxCode_known)
1319 if (*zop->u.prox->proximityUnitCode != Z_ProxUnit_word)
1321 char *val = odr_malloc (zi->odr, 16);
1323 zi->errString = val;
1324 sprintf (val, "%d", *zop->u.prox->proximityUnitCode);
1327 r = rpn_proximity (zi, bool_parms.rset_l, bool_parms.rset_r,
1328 *zop->u.prox->ordered,
1329 (!zop->u.prox->exclusion ? 0 :
1330 *zop->u.prox->exclusion),
1331 *zop->u.prox->relationType,
1332 *zop->u.prox->distance);
1339 else if (zs->which == Z_RPNStructure_simple)
1341 if (zs->u.simple->which == Z_Operand_APT)
1343 logf (LOG_DEBUG, "rpn_search_APT");
1344 r = rpn_search_APT (zi, zs->u.simple->u.attributesPlusTerm,
1345 attributeSet, num_bases, basenames);
1347 else if (zs->u.simple->which == Z_Operand_resultSetId)
1349 logf (LOG_DEBUG, "rpn_search_ref");
1350 r = rpn_search_ref (zi, zs->u.simple->u.resultSetId);
1366 void count_set_save (ZServerInfo *zi, RSET *r, int *count)
1373 rset_temp_parms parms;
1374 int maxResultSetSize = atoi (res_get_def (zi->res,
1375 "maxResultSetSize", "400"));
1376 logf (LOG_DEBUG, "count_set_save");
1378 parms.key_size = sizeof(struct it_key);
1379 parms.temp_path = res_get (zi->res, "setTmpDir");
1380 w = rset_create (rset_kind_temp, &parms);
1381 wfd = rset_open (w, RSETF_WRITE|RSETF_SORT_SYSNO);
1382 rfd = rset_open (*r, RSETF_READ|RSETF_SORT_SYSNO);
1383 while (rset_read (*r, rfd, &key))
1385 if (key.sysno != psysno)
1387 if (*count < maxResultSetSize)
1388 rset_write (w, wfd, &key);
1394 rset_close (*r, rfd);
1396 rset_close (w, wfd);
1398 logf (LOG_DEBUG, "%d keys, %d distinct sysnos", kno, *count);
1401 static void count_set (RSET r, int *count)
1408 logf (LOG_DEBUG, "count_set");
1411 rfd = rset_open (r, RSETF_READ|RSETF_SORT_SYSNO);
1412 while (rset_read (r, rfd, &key))
1414 if (key.sysno != psysno)
1421 rset_close (r, rfd);
1422 logf (LOG_DEBUG, "%d keys, %d distinct sysnos", kno, *count);
1425 int rpn_search (ZServerInfo *zi,
1426 Z_RPNQuery *rpn, int num_bases, char **basenames,
1427 const char *setname, int *hits)
1432 oid_value attributeSet;
1437 zi->errString = NULL;
1439 attrset = oid_getentbyoid (rpn->attributeSetId);
1440 attributeSet = attrset->value;
1441 rset = rpn_search_structure (zi, rpn->RPNStructure, attributeSet,
1442 num_bases, basenames);
1445 if (rset_is_volatile(rset))
1446 count_set_save(zi, &rset, hits);
1447 else if ((i = rset_hits (rset)) >= 0)
1450 count_set (rset, hits);
1451 resultSetAdd (zi, setname, 1, rset);
1453 logf (LOG_DEBUG, "search error: %d", zi->errCode);
1457 struct scan_info_entry {
1463 struct scan_info_entry *list;
1469 static int scan_handle (char *name, const char *info, int pos, void *client)
1471 int len_prefix, idx;
1472 struct scan_info *scan_info = client;
1474 len_prefix = strlen(scan_info->prefix);
1475 if (memcmp (name, scan_info->prefix, len_prefix))
1478 idx = scan_info->after - pos + scan_info->before;
1481 scan_info->list[idx].term = odr_malloc (scan_info->odr,
1482 strlen(name + len_prefix)+1);
1483 strcpy (scan_info->list[idx].term, name + len_prefix);
1484 assert (*info == sizeof(ISAM_P));
1485 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAM_P));
1490 static void scan_term_untrans (ZServerInfo *zi, int reg_type,
1491 char **dstp, const char *src)
1493 char *dst = odr_malloc (zi->odr, strlen(src)*2+1);
1498 const char *cp = zebra_maps_output (zi->zebra_maps, reg_type, &src);
1505 int rpn_scan (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1506 oid_value attributeset,
1507 int num_bases, char **basenames,
1508 int *position, int *num_entries, struct scan_entry **list,
1512 int pos = *position;
1513 int num = *num_entries;
1517 char termz[IT_MAX_WORD+20];
1520 struct scan_info *scan_info_array;
1521 struct scan_entry *glist;
1522 int ords[32], ord_no = 0;
1526 char *search_type = NULL;
1529 logf (LOG_DEBUG, "scan, position = %d, num = %d", pos, num);
1531 if (attributeset == VAL_NONE)
1532 attributeset = VAL_BIB1;
1534 attr_init (&use, zapt, 1);
1535 use_value = attr_find (&use, &attributeset);
1536 logf (LOG_DEBUG, "use value %d", use_value);
1538 if (zebra_maps_attr (zi->zebra_maps, zapt, ®_type, &search_type,
1545 if (use_value == -1)
1547 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
1551 data1_local_attribute *local_attr;
1553 if ((r=att_getentbyatt (zi, &attp, attributeset, use_value)))
1555 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
1556 attributeset, use_value);
1562 if (zebTargetInfo_curDatabase (zi->zti, basenames[base_no]))
1564 zi->errString = basenames[base_no];
1565 return zi->errCode = 109; /* Database unavailable */
1567 for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
1568 local_attr = local_attr->next)
1572 ord = zebTargetInfo_lookupSU (zi->zti, attp.attset_ordinal,
1575 ords[ord_no++] = ord;
1579 return zi->errCode = 113;
1582 scan_info_array = odr_malloc (zi->odr, ord_no * sizeof(*scan_info_array));
1583 for (i = 0; i < ord_no; i++)
1585 int j, prefix_len = 0;
1586 int before_tmp = before, after_tmp = after;
1587 struct scan_info *scan_info = scan_info_array + i;
1588 struct rpn_char_map_info rcmi;
1590 rpn_char_map_prepare (zi, reg_type, &rcmi);
1592 scan_info->before = before;
1593 scan_info->after = after;
1594 scan_info->odr = zi->odr;
1596 scan_info->list = odr_malloc (zi->odr, (before+after)*
1597 sizeof(*scan_info->list));
1598 for (j = 0; j<before+after; j++)
1599 scan_info->list[j].term = NULL;
1600 termz[prefix_len++] = ords[i];
1601 termz[prefix_len++] = reg_type;
1602 termz[prefix_len] = 0;
1603 strcpy (scan_info->prefix, termz);
1605 trans_scan_term (zi, zapt, termz+prefix_len, reg_type);
1607 dict_scan (zi->dict, termz, &before_tmp, &after_tmp, scan_info,
1610 glist = odr_malloc (zi->odr, (before+after)*sizeof(*glist));
1611 for (i = 0; i < ord_no; i++)
1614 *status = BEND_SCAN_SUCCESS;
1615 for (i = 0; i<after; i++)
1618 const char *mterm = NULL;
1622 for (j = 0; j < ord_no; j++)
1624 if (ptr[j] < before+after &&
1625 (tst=scan_info_array[j].list[ptr[j]].term) &&
1626 (!mterm || strcmp (tst, mterm) < 0))
1634 scan_term_untrans (zi, reg_type, &glist[i+before].term, mterm);
1635 rset = rset_trunc (zi, &scan_info_array[j0].list[ptr[j0]].isam_p, 1);
1638 for (j = j0+1; j<ord_no; j++)
1640 if (ptr[j] < before+after &&
1641 (tst=scan_info_array[j].list[ptr[j]].term) &&
1642 !strcmp (tst, mterm))
1644 rset_bool_parms bool_parms;
1648 rset_trunc (zi, &scan_info_array[j].list[ptr[j]].isam_p, 1);
1650 bool_parms.key_size = sizeof(struct it_key);
1651 bool_parms.cmp = key_compare_it;
1652 bool_parms.rset_l = rset;
1653 bool_parms.rset_r = rset2;
1655 rset = rset_create (rset_kind_or, &bool_parms);
1660 count_set (rset, &glist[i+before].occurrences);
1665 *num_entries -= (after-i);
1666 *status = BEND_SCAN_PARTIAL;
1669 for (i = 0; i<ord_no; i++)
1672 for (i = 0; i<before; i++)
1675 const char *mterm = NULL;
1679 for (j = 0; j <ord_no; j++)
1681 if (ptr[j] < before &&
1682 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
1683 (!mterm || strcmp (tst, mterm) > 0))
1692 scan_term_untrans (zi, reg_type, &glist[before-1-i].term, mterm);
1695 (zi, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1);
1699 for (j = j0+1; j<ord_no; j++)
1701 if (ptr[j] < before &&
1702 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
1703 !strcmp (tst, mterm))
1705 rset_bool_parms bool_parms;
1708 rset2 = rset_trunc (zi,
1709 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1);
1711 bool_parms.key_size = sizeof(struct it_key);
1712 bool_parms.cmp = key_compare_it;
1713 bool_parms.rset_l = rset;
1714 bool_parms.rset_r = rset2;
1716 rset = rset_create (rset_kind_or, &bool_parms);
1721 count_set (rset, &glist[before-1-i].occurrences);
1727 *status = BEND_SCAN_PARTIAL;
1731 *list = glist + i; /* list is set to first 'real' entry */
1733 logf (LOG_DEBUG, "position = %d, num_entries = %d",
1734 *position, *num_entries);
1736 logf (LOG_DEBUG, "scan error: %d", zi->errCode);