2 * Copyright (C) 1995-1998, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.81 1998-06-24 12:16:14 adam
8 * Support for relations on text operands. Open range support in
9 * DFA module (i.e. [-j], [g-]).
11 * Revision 1.80 1998/06/23 15:33:34 adam
12 * Added feature to specify sort criteria in query (type 7 specifies
15 * Revision 1.79 1998/06/22 11:35:09 adam
18 * Revision 1.78 1998/06/08 14:43:17 adam
19 * Added suport for EXPLAIN Proxy servers - added settings databasePath
20 * and explainDatabase to facilitate this. Increased maximum number
21 * of databases and attributes in one register.
23 * Revision 1.77 1998/05/20 10:12:22 adam
24 * Implemented automatic EXPLAIN database maintenance.
25 * Modified Zebra to work with ASN.1 compiled version of YAZ.
27 * Revision 1.76 1998/04/02 14:35:29 adam
28 * First version of Zebra that works with compiled ASN.1.
30 * Revision 1.75 1998/03/05 08:45:13 adam
31 * New result set model and modular ranking system. Moved towards
32 * descent server API. System information stored as "SGML" records.
34 * Revision 1.74 1998/02/10 12:03:06 adam
37 * Revision 1.73 1998/01/29 13:40:11 adam
38 * Better logging for scan service.
40 * Revision 1.72 1998/01/07 13:53:41 adam
41 * Queries using simple ranked operands returns right number of hits.
43 * Revision 1.71 1997/12/18 10:54:24 adam
44 * New method result set method rs_hits that returns the number of
45 * hits in result-set (if known). The ranked result set returns real
46 * number of hits but only when not combined with other operands.
48 * Revision 1.70 1997/10/31 12:34:43 adam
49 * Changed a few log statements.
51 * Revision 1.69 1997/10/29 12:05:02 adam
52 * Server produces diagnostic "Unsupported Attribute Set" when appropriate.
54 * Revision 1.68 1997/10/27 14:33:06 adam
55 * Moved towards generic character mapping depending on "structure"
56 * field in abstract syntax file. Fixed a few memory leaks. Fixed
57 * bug with negative integers when doing searches with relational
60 * Revision 1.67 1997/09/29 09:06:10 adam
61 * Removed one static var in order to make this module thread safe.
63 * Revision 1.66 1997/09/25 14:58:03 adam
66 * Revision 1.65 1997/09/22 12:39:06 adam
67 * Added get_pos method for the ranked result sets.
69 * Revision 1.64 1997/09/18 08:59:20 adam
70 * Extra generic handle for the character mapping routines.
72 * Revision 1.63 1997/09/17 12:19:18 adam
73 * Zebra version corresponds to YAZ version 1.4.
74 * Changed Zebra server so that it doesn't depend on global common_resource.
76 * Revision 1.62 1997/09/05 15:30:09 adam
77 * Changed prototype for chr_map_input - added const.
78 * Added support for C++, headers uses extern "C" for public definitions.
80 * Revision 1.61 1997/02/10 10:21:14 adam
81 * Bug fix: in search terms character (^) wasn't observed.
83 * Revision 1.60 1997/01/31 11:10:34 adam
84 * Bug fix: Leading and trailing white space weren't removed in scan tokens.
86 * Revision 1.59 1997/01/17 11:31:46 adam
87 * Bug fix: complete phrase search didn't work.
89 * Revision 1.58 1996/12/23 15:30:45 adam
91 * Bug fix: result sets weren't deleted after server shut down.
93 * Revision 1.57 1996/11/11 13:38:02 adam
94 * Added proximity support in search.
96 * Revision 1.56 1996/11/08 11:10:32 adam
97 * Buffers used during file match got bigger.
98 * Compressed ISAM support everywhere.
99 * Bug fixes regarding masking characters in queries.
100 * Redesigned Regexp-2 queries.
102 * Revision 1.55 1996/11/04 14:07:44 adam
103 * Moved truncation code to trunc.c.
105 * Revision 1.54 1996/10/29 14:09:52 adam
106 * Use of cisam system - enabled if setting isamc is 1.
108 * Revision 1.53 1996/06/26 09:21:43 adam
109 * Bug fix: local attribute set wasn't obeyed in scan.
111 * Revision 1.52 1996/06/17 14:26:20 adam
112 * Function gen_regular_rel changed to handle negative numbers.
114 * Revision 1.51 1996/06/11 10:54:15 quinn
117 * Revision 1.50 1996/06/07 08:51:53 adam
118 * Bug fix: Character mapping was broken (introducued by last revision).
120 * Revision 1.49 1996/06/04 10:18:11 adam
121 * Search/scan uses character mapping module.
123 * Revision 1.48 1996/05/28 15:15:01 adam
124 * Bug fix: Didn't handle unknown database correctly.
126 * Revision 1.47 1996/05/15 18:36:28 adam
127 * Function trans_term transforms unsearchable characters to blanks.
129 * Revision 1.46 1996/05/15 11:57:56 adam
130 * Fixed bug introduced by set/field mapping in search operations.
132 * Revision 1.45 1996/05/14 11:34:00 adam
133 * Scan support in multiple registers/databases.
135 * Revision 1.44 1996/05/14 06:16:44 adam
136 * Compact use/set bytes used in search service.
138 * Revision 1.43 1996/05/09 09:54:43 adam
139 * Server supports maps from one logical attributes to a list of physical
141 * The extraction process doesn't make space consuming 'any' keys.
143 * Revision 1.42 1996/05/09 07:28:56 quinn
144 * Work towards phrases and multiple registers
146 * Revision 1.41 1996/03/20 09:36:43 adam
147 * Function dict_lookup_grep got extra parameter, init_pos, which marks
148 * from which position in pattern approximate pattern matching should occur.
149 * Approximate pattern matching is used in relevance=re-2.
151 * Revision 1.40 1996/02/02 13:44:44 adam
152 * The public dictionary functions simply use char instead of Dict_char
153 * to represent search strings. Dict_char is used internally only.
155 * Revision 1.39 1996/01/03 16:22:13 quinn
156 * operator->roperator
158 * Revision 1.38 1995/12/11 09:12:55 adam
159 * The rec_get function returns NULL if record doesn't exist - will
160 * happen in the server if the result set records have been deleted since
161 * the creation of the set (i.e. the search).
162 * The server saves a result temporarily if it is 'volatile', i.e. the
163 * set is register dependent.
165 * Revision 1.37 1995/12/06 15:05:28 adam
166 * More verbose in count_set.
168 * Revision 1.36 1995/12/06 12:41:27 adam
169 * New command 'stat' for the index program.
170 * Filenames can be read from stdin by specifying '-'.
171 * Bug fix/enhancement of the transformation from terms to regular
172 * expressons in the search engine.
174 * Revision 1.35 1995/11/27 09:29:00 adam
175 * Bug fixes regarding conversion to regular expressions.
177 * Revision 1.34 1995/11/16 17:00:56 adam
178 * Better logging of rpn query.
180 * Revision 1.33 1995/11/01 13:58:28 quinn
181 * Moving data1 to yaz/retrieval
183 * Revision 1.32 1995/10/27 14:00:11 adam
184 * Implemented detection of database availability.
186 * Revision 1.31 1995/10/17 18:02:10 adam
187 * New feature: databases. Implemented as prefix to words in dictionary.
189 * Revision 1.30 1995/10/16 09:32:38 adam
190 * More work on relational op.
192 * Revision 1.29 1995/10/13 16:01:49 adam
195 * Revision 1.28 1995/10/13 12:26:43 adam
196 * Optimization of truncation.
198 * Revision 1.27 1995/10/12 17:07:22 adam
201 * Revision 1.26 1995/10/12 12:40:54 adam
202 * Bug fixes in rpn_prox.
204 * Revision 1.25 1995/10/10 13:59:24 adam
205 * Function rset_open changed its wflag parameter to general flags.
207 * Revision 1.24 1995/10/09 16:18:37 adam
208 * Function dict_lookup_grep got extra client data parameter.
210 * Revision 1.23 1995/10/06 16:33:37 adam
211 * Use attribute mappings.
213 * Revision 1.22 1995/10/06 15:07:39 adam
214 * Structure 'local-number' handled.
216 * Revision 1.21 1995/10/06 13:52:06 adam
217 * Bug fixes. Handler may abort further scanning.
219 * Revision 1.20 1995/10/06 11:06:33 adam
220 * Scan entries include 'occurrences' now.
222 * Revision 1.19 1995/10/06 10:43:56 adam
223 * Scan added. 'occurrences' in scan entries not set yet.
225 * Revision 1.18 1995/10/04 16:57:20 adam
226 * Key input and merge sort in one pass.
228 * Revision 1.17 1995/10/04 12:55:17 adam
229 * Bug fix in ranked search. Use=Any keys inserted.
231 * Revision 1.16 1995/10/02 16:24:40 adam
232 * Use attribute actually used in search requests.
234 * Revision 1.15 1995/10/02 15:18:52 adam
235 * New member in recRetrieveCtrl: diagnostic.
237 * Revision 1.14 1995/09/28 12:10:32 adam
238 * Bug fixes. Field prefix used in queries.
240 * Revision 1.13 1995/09/18 14:17:50 adam
243 * Revision 1.12 1995/09/15 14:45:21 adam
245 * Work on truncation.
247 * Revision 1.11 1995/09/14 11:53:27 adam
248 * First work on regular expressions/truncations.
250 * Revision 1.10 1995/09/11 15:23:26 adam
251 * More work on relevance search.
253 * Revision 1.9 1995/09/11 13:09:35 adam
254 * More work on relevance feedback.
256 * Revision 1.8 1995/09/08 14:52:27 adam
257 * Minor changes. Dictionary is lower case now.
259 * Revision 1.7 1995/09/07 13:58:36 adam
260 * New parameter: result-set file descriptor (RSFD) to support multiple
261 * positions within the same result-set.
262 * Boolean operators: and, or, not implemented.
263 * Result-set references.
265 * Revision 1.6 1995/09/06 16:11:18 adam
266 * Option: only one word key per file.
268 * Revision 1.5 1995/09/06 10:33:04 adam
269 * More work on present. Some log messages removed.
271 * Revision 1.4 1995/09/05 15:28:40 adam
272 * More work on search engine.
274 * Revision 1.3 1995/09/04 15:20:22 adam
277 * Revision 1.2 1995/09/04 12:33:43 adam
278 * Various cleanup. YAZ util used instead.
280 * Revision 1.1 1995/09/04 09:10:40 adam
281 * More work on index add/del/update.
282 * Merge sort implemented.
283 * Initial work on z39 server.
302 struct rpn_char_map_info {
307 static const char **rpn_char_map_handler (void *vp, const char **from, int len)
309 struct rpn_char_map_info *p = vp;
310 return zebra_maps_input (p->zm, p->reg_type, from, len);
313 static void rpn_char_map_prepare (ZebraHandle zh, int reg_type,
314 struct rpn_char_map_info *map_info)
316 map_info->zm = zh->zebra_maps;
317 map_info->reg_type = reg_type;
318 dict_grep_cmap (zh->dict, map_info, rpn_char_map_handler);
325 Z_AttributesPlusTerm *zapt;
328 static int attr_find (AttrType *src, oid_value *attributeSetP)
333 num_attributes = src->zapt->attributes->num_attributes;
335 num_attributes = src->zapt->num_attributes;
337 while (src->major < num_attributes)
339 Z_AttributeElement *element;
342 element = src->zapt->attributes->attributes[src->major];
344 element = src->zapt->attributeList[src->major];
346 if (src->type == *element->attributeType)
348 switch (element->which)
350 case Z_AttributeValue_numeric:
352 if (element->attributeSet && attributeSetP)
356 attrset = oid_getentbyoid (element->attributeSet);
357 *attributeSetP = attrset->value;
359 return *element->value.numeric;
361 case Z_AttributeValue_complex:
362 if (src->minor >= element->value.complex->num_list ||
363 element->value.complex->list[src->minor]->which !=
364 Z_StringOrNumeric_numeric)
367 if (element->attributeSet && attributeSetP)
371 attrset = oid_getentbyoid (element->attributeSet);
372 *attributeSetP = attrset->value;
374 return *element->value.complex->list[src->minor-1]->u.numeric;
384 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
406 static void term_untrans (ZebraHandle zh, int reg_type,
407 char *dst, const char *src)
411 const char *cp = zebra_maps_output (zh->zebra_maps, reg_type, &src);
418 static void add_isam_p (const char *name, const char *info,
421 if (p->isam_p_indx == p->isam_p_size)
423 ISAM_P *new_isam_p_buf;
427 p->isam_p_size = 2*p->isam_p_size + 100;
428 new_isam_p_buf = xmalloc (sizeof(*new_isam_p_buf) *
432 memcpy (new_isam_p_buf, p->isam_p_buf,
433 p->isam_p_indx * sizeof(*p->isam_p_buf));
434 xfree (p->isam_p_buf);
436 p->isam_p_buf = new_isam_p_buf;
439 new_term_no = xmalloc (sizeof(*new_term_no) *
443 memcpy (new_term_no, p->isam_p_buf,
444 p->isam_p_indx * sizeof(*p->term_no));
447 p->term_no = new_term_no;
450 assert (*info == sizeof(*p->isam_p_buf));
451 memcpy (p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
454 term_untrans (p->zh, p->reg_type, term_tmp, name+2);
455 logf (LOG_DEBUG, "grep: %s", term_tmp);
460 static int grep_handle (char *name, const char *info, void *p)
462 add_isam_p (name, info, p);
466 static int term_pre (ZebraMaps zebra_maps, int reg_type, const char **src,
467 const char *ct1, const char *ct2)
469 const char *s1, *s0 = *src;
472 /* skip white space */
475 if (ct1 && strchr (ct1, *s0))
477 if (ct2 && strchr (ct2, *s0))
480 map = zebra_maps_input (zebra_maps, reg_type, &s1, strlen(s1));
481 if (**map != *CHR_SPACE)
489 static int term_100 (ZebraMaps zebra_maps, int reg_type,
490 const char **src, char *dst, int space_split,
498 if (!term_pre (zebra_maps, reg_type, src, NULL, NULL))
504 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
505 if (space_split && **map == *CHR_SPACE)
509 if (!isalnum (*s1) && *s1 != '-')
521 static int term_101 (ZebraMaps zebra_maps, int reg_type,
522 const char **src, char *dst, int space_split,
530 if (!term_pre (zebra_maps, reg_type, src, "#", "#"))
539 dst_term[j++] = *s0++;
544 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
545 if (space_split && **map == *CHR_SPACE)
557 dst_term[j++] = '\0';
563 static int term_103 (ZebraMaps zebra_maps, int reg_type, const char **src,
564 char *dst, int *errors, int space_split,
572 if (!term_pre (zebra_maps, reg_type, src, "^\\()[].*+?|", "("))
575 if (errors && *s0 == '+' && s0[1] && s0[2] == '+' && s0[3] &&
578 *errors = s0[1] - '0';
585 if (strchr ("^\\()[].*+?|-", *s0))
593 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
594 if (**map == *CHR_SPACE)
611 static int term_102 (ZebraMaps zebra_maps, int reg_type, const char **src,
612 char *dst, int space_split, char *dst_term)
614 return term_103 (zebra_maps, reg_type, src, dst, NULL, space_split,
618 /* gen_regular_rel - generate regular expression from relation
619 * val: border value (inclusive)
620 * islt: 1 if <=; 0 if >=.
622 static void gen_regular_rel (char *dst, int val, int islt)
629 logf (LOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
633 strcpy (dst, "(-[0-9]+|(");
641 strcpy (dst, "([0-9]+|-(");
642 dst_p = strlen (dst);
652 dst_p = strlen (dst);
653 sprintf (numstr, "%d", val);
654 for (w = strlen(numstr); --w >= 0; pos++)
673 strcpy (dst + dst_p, numstr);
674 dst_p = strlen(dst) - pos - 1;
702 for (i = 0; i<pos; i++)
715 for (i=1; i<pos; i++)
716 strcat (dst, "[0-9]?");
720 for (i = 0; i <= pos; i++)
721 strcat (dst, "[0-9]");
722 strcat (dst, "[0-9]*");
727 void string_rel_add_char (char **term_p, const char *src, int *indx)
729 if (src[*indx] == '\\')
730 *(*term_p)++ = src[(*indx)++];
731 *(*term_p)++ = src[(*indx)++];
735 * > abc ([b-].*|a[c-].*|ab[d-].*|abc.+)
736 * ([^-a].*|a[^-b].*ab[^-c].*|abc.+)
737 * >= abc ([b-].*|a[c-].*|ab[c-].*)
738 * ([^-a].*|a[^-b].*|ab[c-].*)
739 * < abc ([-0].*|a[-a].*|ab[-b].*)
740 * ([^a-].*|a[^b-].*|ab[^c-].*)
741 * <= abc ([-0].*|a[-a].*|ab[-b].*|abc)
742 * ([^a-].*|a[^b-].*|ab[^c-].*|abc)
744 static int string_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
745 const char **term_sub, char *term_dict,
746 oid_value attributeSet,
747 int reg_type, int space_split, char *term_dst)
752 char *term_tmp = term_dict + strlen(term_dict);
753 char term_component[256];
755 attr_init (&relation, zapt, 2);
756 relation_value = attr_find (&relation, NULL);
758 logf (LOG_DEBUG, "string relation value=%d", relation_value);
759 switch (relation_value)
762 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
763 space_split, term_dst))
765 logf (LOG_DEBUG, "Relation <");
768 for (i = 0; term_component[i]; )
775 string_rel_add_char (&term_tmp, term_component, &j);
780 string_rel_add_char (&term_tmp, term_component, &i);
791 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
792 space_split, term_dst))
794 logf (LOG_DEBUG, "Relation <=");
797 for (i = 0; term_component[i]; )
802 string_rel_add_char (&term_tmp, term_component, &j);
806 string_rel_add_char (&term_tmp, term_component, &i);
815 for (i = 0; term_component[i]; )
816 string_rel_add_char (&term_tmp, term_component, &i);
821 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
822 space_split, term_dst))
824 logf (LOG_DEBUG, "Relation >");
827 for (i = 0; term_component[i];)
832 string_rel_add_char (&term_tmp, term_component, &j);
837 string_rel_add_char (&term_tmp, term_component, &i);
845 for (i = 0; term_component[i];)
846 string_rel_add_char (&term_tmp, term_component, &i);
853 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
854 space_split, term_dst))
856 logf (LOG_DEBUG, "Relation >=");
859 for (i = 0; term_component[i];)
866 string_rel_add_char (&term_tmp, term_component, &j);
869 if (term_component[i+1])
873 string_rel_add_char (&term_tmp, term_component, &i);
877 string_rel_add_char (&term_tmp, term_component, &i);
889 logf (LOG_DEBUG, "Relation =");
890 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
891 space_split, term_dst))
893 strcat (term_tmp, "(");
894 strcat (term_tmp, term_component);
895 strcat (term_tmp, ")");
900 static int string_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
901 const char **term_sub,
902 oid_value attributeSet, struct grep_info *grep_info,
903 int reg_type, int complete_flag,
904 int num_bases, char **basenames,
907 char term_dict[2*IT_MAX_WORD+4000];
910 int truncation_value;
913 oid_value curAttributeSet = attributeSet;
915 struct rpn_char_map_info rcmi;
916 int space_split = complete_flag ? 0 : 1;
918 rpn_char_map_prepare (zh, reg_type, &rcmi);
919 attr_init (&use, zapt, 1);
920 use_value = attr_find (&use, &curAttributeSet);
921 logf (LOG_DEBUG, "string_term, use value %d", use_value);
922 attr_init (&truncation, zapt, 5);
923 truncation_value = attr_find (&truncation, NULL);
924 logf (LOG_DEBUG, "truncation value %d", truncation_value);
929 for (base_no = 0; base_no < num_bases; base_no++)
932 data1_local_attribute *local_attr;
933 int max_pos, prefix_len = 0;
936 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
938 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
939 curAttributeSet, use_value, r);
946 if (zebraExplain_curDatabase (zh->zei, basenames[base_no]))
948 zh->errCode = 109; /* Database unavailable */
949 zh->errString = basenames[base_no];
952 for (local_attr = attp.local_attributes; local_attr;
953 local_attr = local_attr->next)
959 ord = zebraExplain_lookupSU (zh->zei, attp.attset_ordinal,
964 term_dict[prefix_len++] = '|';
966 term_dict[prefix_len++] = '(';
968 ord_len = key_SU_code (ord, ord_buf);
969 for (i = 0; i<ord_len; i++)
971 term_dict[prefix_len++] = 1;
972 term_dict[prefix_len++] = ord_buf[i];
980 term_dict[prefix_len++] = ')';
981 term_dict[prefix_len++] = 1;
982 term_dict[prefix_len++] = reg_type;
983 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
984 term_dict[prefix_len] = '\0';
986 switch (truncation_value)
988 case -1: /* not specified */
989 case 100: /* do not truncate */
990 if (!string_relation (zh, zapt, &termp, term_dict,
992 reg_type, space_split, term_dst))
994 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_dict+prefix_len);
995 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info, &max_pos,
998 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1000 case 1: /* right truncation */
1001 term_dict[j++] = '(';
1002 if (!term_100 (zh->zebra_maps, reg_type,
1003 &termp, term_dict + j, space_split, term_dst))
1005 strcat (term_dict, ".*)");
1006 dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1007 &max_pos, 0, grep_handle);
1009 case 2: /* left truncation */
1010 case 3: /* left&right truncation */
1013 case 101: /* process # in term */
1014 term_dict[j++] = '(';
1015 if (!term_101 (zh->zebra_maps, reg_type,
1016 &termp, term_dict + j, space_split, term_dst))
1018 strcat (term_dict, ")");
1019 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1020 &max_pos, 0, grep_handle);
1022 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d", r);
1024 case 102: /* Regexp-1 */
1025 term_dict[j++] = '(';
1026 if (!term_102 (zh->zebra_maps, reg_type,
1027 &termp, term_dict + j, space_split, term_dst))
1029 strcat (term_dict, ")");
1030 logf (LOG_DEBUG, "Regexp-1 tolerance=%d", r);
1031 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1032 &max_pos, 0, grep_handle);
1034 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
1037 case 103: /* Regexp-2 */
1039 term_dict[j++] = '(';
1040 if (!term_103 (zh->zebra_maps, reg_type,
1041 &termp, term_dict + j, &r, space_split, term_dst))
1043 strcat (term_dict, ")");
1044 logf (LOG_DEBUG, "Regexp-2 tolerance=%d", r);
1045 r = dict_lookup_grep (zh->dict, term_dict, r, grep_info,
1046 &max_pos, 2, grep_handle);
1048 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
1054 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1058 static void trans_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1062 Z_Term *term = zapt->term;
1064 sizez = term->u.general->len;
1065 if (sizez > IT_MAX_WORD-1)
1066 sizez = IT_MAX_WORD-1;
1067 memcpy (termz, term->u.general->buf, sizez);
1068 termz[sizez] = '\0';
1071 static void trans_scan_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1072 char *termz, int reg_type)
1074 Z_Term *term = zapt->term;
1076 const char *cp = (const char *) term->u.general->buf;
1077 const char *cp_end = cp + term->u.general->len;
1080 const char *space_map = NULL;
1083 while ((len = (cp_end - cp)) > 0)
1085 map = zebra_maps_input (zh->zebra_maps, reg_type, &cp, len);
1086 if (**map == *CHR_SPACE)
1091 for (src = space_map; *src; src++)
1094 for (src = *map; *src; src++)
1101 static RSET rpn_proximity (ZebraHandle zh, RSET rset1, RSET rset2,
1103 int exclusion, int relation, int distance)
1108 struct it_key buf1, buf2;
1111 rset_temp_parms parms;
1114 rsfd1 = rset_open (rset1, RSETF_READ);
1115 more1 = rset_read (rset1, rsfd1, &buf1, &term_index);
1117 rsfd2 = rset_open (rset2, RSETF_READ);
1118 more2 = rset_read (rset2, rsfd2, &buf2, &term_index);
1120 parms.key_size = sizeof (struct it_key);
1121 parms.temp_path = res_get (zh->res, "setTmpDir");
1122 result = rset_create (rset_kind_temp, &parms);
1123 rsfd_result = rset_open (result, RSETF_WRITE);
1125 logf (LOG_DEBUG, "rpn_proximity excl=%d ord=%d rel=%d dis=%d",
1126 exclusion, ordered, relation, distance);
1127 while (more1 && more2)
1129 int cmp = key_compare_it (&buf1, &buf2);
1131 more1 = rset_read (rset1, rsfd1, &buf1, &term_index);
1133 more2 = rset_read (rset2, rsfd2, &buf2, &term_index);
1136 int sysno = buf1.sysno;
1140 seqno[n++] = buf1.seqno;
1141 while ((more1 = rset_read (rset1, rsfd1, &buf1, &term_index)) &&
1142 sysno == buf1.sysno)
1144 seqno[n++] = buf1.seqno;
1147 for (i = 0; i<n; i++)
1149 int diff = buf2.seqno - seqno[i];
1150 int excl = exclusion;
1151 if (!ordered && diff < 0)
1156 if (diff < distance)
1160 if (diff <= distance)
1164 if (diff == distance)
1168 if (diff >= distance)
1172 if (diff > distance)
1176 if (diff != distance)
1181 rset_write (result, rsfd_result, &buf2);
1183 } while ((more2 = rset_read (rset2, rsfd2, &buf2, &term_index)) &&
1184 sysno == buf2.sysno);
1187 rset_close (result, rsfd_result);
1188 rset_close (rset1, rsfd1);
1189 rset_close (rset2, rsfd2);
1193 static RSET rpn_prox (ZebraHandle zh, RSET *rset, int rset_no)
1198 struct it_key **buf;
1200 char prox_term[1024];
1201 int length_prox_term = 0;
1202 int min_nn = 10000000;
1204 const char *flags = NULL;
1206 rsfd = xmalloc (sizeof(*rsfd)*rset_no);
1207 more = xmalloc (sizeof(*more)*rset_no);
1208 buf = xmalloc (sizeof(*buf)*rset_no);
1210 for (i = 0; i<rset_no; i++)
1213 buf[i] = xmalloc (sizeof(**buf));
1214 rsfd[i] = rset_open (rset[i], RSETF_READ);
1215 if (!(more[i] = rset_read (rset[i], rsfd[i], buf[i], &term_index)))
1217 for (j = 0; j<rset[i]->no_rset_terms; j++)
1219 const char *nflags = rset[i]->rset_terms[j]->flags;
1220 char *term = rset[i]->rset_terms[j]->name;
1221 int lterm = strlen(term);
1222 if (length_prox_term)
1223 prox_term[length_prox_term++] = ' ';
1224 strcpy (prox_term + length_prox_term, term);
1225 length_prox_term += lterm;
1226 if (min_nn > rset[i]->rset_terms[j]->nn)
1227 min_nn = rset[i]->rset_terms[j]->nn;
1233 rset_null_parms parms;
1237 rset_close (rset[i], rsfd[i]);
1241 parms.rset_term = rset_term_create (prox_term, -1, flags);
1242 parms.rset_term->nn = 0;
1243 result = rset_create (rset_kind_null, &parms);
1247 rset_temp_parms parms;
1250 parms.rset_term = rset_term_create (prox_term, -1, flags);
1251 parms.rset_term->nn = min_nn;
1252 parms.key_size = sizeof (struct it_key);
1253 parms.temp_path = res_get (zh->res, "setTmpDir");
1254 result = rset_create (rset_kind_temp, &parms);
1255 rsfd_result = rset_open (result, RSETF_WRITE);
1259 for (i = 1; i<rset_no; i++)
1268 cmp = key_compare_it (buf[i], buf[i-1]);
1271 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1272 buf[i-1], &term_index);
1277 if (buf[i-1]->seqno+1 != buf[i]->seqno)
1279 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1280 buf[i-1], &term_index);
1286 more[i] = rset_read (rset[i], rsfd[i], buf[i],
1293 rset_write (result, rsfd_result, buf[0]);
1294 more[0] = rset_read (*rset, *rsfd, *buf, &term_index);
1298 for (i = 0; i<rset_no; i++)
1300 rset_close (rset[i], rsfd[i]);
1303 rset_close (result, rsfd_result);
1311 static RSET rpn_search_APT_phrase (ZebraHandle zh,
1312 Z_AttributesPlusTerm *zapt,
1314 oid_value attributeSet,
1315 int reg_type, int complete_flag,
1316 const char *rank_type,
1317 int num_bases, char **basenames)
1319 char term_dst[IT_MAX_WORD+1];
1320 const char *termp = termz;
1321 RSET rset[60], result;
1322 int i, r, rset_no = 0;
1323 struct grep_info grep_info;
1326 grep_info.term_no = 0;
1328 grep_info.isam_p_size = 0;
1329 grep_info.isam_p_buf = NULL;
1331 grep_info.reg_type = reg_type;
1335 logf (LOG_DEBUG, "APT_phrase termp=%s", termp);
1336 grep_info.isam_p_indx = 0;
1337 r = string_term (zh, zapt, &termp, attributeSet, &grep_info,
1338 reg_type, complete_flag, num_bases, basenames,
1342 logf (LOG_DEBUG, "term: %s", term_dst);
1343 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1344 grep_info.isam_p_indx, term_dst,
1345 strlen(term_dst), rank_type);
1346 assert (rset[rset_no]);
1347 if (++rset_no >= sizeof(rset)/sizeof(*rset))
1351 xfree(grep_info.term_no);
1353 xfree (grep_info.isam_p_buf);
1356 rset_null_parms parms;
1358 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1359 return rset_create (rset_kind_null, &parms);
1361 else if (rset_no == 1)
1363 result = rpn_prox (zh, rset, rset_no);
1364 for (i = 0; i<rset_no; i++)
1365 rset_delete (rset[i]);
1369 static RSET rpn_search_APT_or_list (ZebraHandle zh,
1370 Z_AttributesPlusTerm *zapt,
1372 oid_value attributeSet,
1373 int reg_type, int complete_flag,
1374 const char *rank_type,
1375 int num_bases, char **basenames)
1377 char term_dst[IT_MAX_WORD+1];
1378 const char *termp = termz;
1379 RSET rset[60], result;
1380 int i, r, rset_no = 0;
1381 struct grep_info grep_info;
1384 grep_info.term_no = 0;
1386 grep_info.isam_p_size = 0;
1387 grep_info.isam_p_buf = NULL;
1389 grep_info.reg_type = reg_type;
1393 logf (LOG_DEBUG, "APT_or_list termp=%s", termp);
1394 grep_info.isam_p_indx = 0;
1395 r = string_term (zh, zapt, &termp, attributeSet, &grep_info,
1396 reg_type, complete_flag, num_bases, basenames,
1400 logf (LOG_DEBUG, "term: %s", term_dst);
1401 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1402 grep_info.isam_p_indx, term_dst,
1403 strlen(term_dst), rank_type);
1404 assert (rset[rset_no]);
1405 if (++rset_no >= sizeof(rset)/sizeof(*rset))
1409 xfree(grep_info.term_no);
1411 xfree (grep_info.isam_p_buf);
1414 rset_null_parms parms;
1416 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1417 return rset_create (rset_kind_null, &parms);
1420 for (i = 1; i<rset_no; i++)
1422 rset_bool_parms bool_parms;
1424 bool_parms.rset_l = result;
1425 bool_parms.rset_r = rset[i];
1426 bool_parms.key_size = sizeof(struct it_key);
1427 bool_parms.cmp = key_compare_it;
1428 result = rset_create (rset_kind_or, &bool_parms);
1433 static RSET rpn_search_APT_and_list (ZebraHandle zh,
1434 Z_AttributesPlusTerm *zapt,
1436 oid_value attributeSet,
1437 int reg_type, int complete_flag,
1438 const char *rank_type,
1439 int num_bases, char **basenames)
1441 char term_dst[IT_MAX_WORD+1];
1442 const char *termp = termz;
1443 RSET rset[60], result;
1444 int i, r, rset_no = 0;
1445 struct grep_info grep_info;
1448 grep_info.term_no = 0;
1450 grep_info.isam_p_size = 0;
1451 grep_info.isam_p_buf = NULL;
1453 grep_info.reg_type = reg_type;
1457 logf (LOG_DEBUG, "APT_and_list termp=%s", termp);
1458 grep_info.isam_p_indx = 0;
1459 r = string_term (zh, zapt, &termp, attributeSet, &grep_info,
1460 reg_type, complete_flag, num_bases, basenames,
1464 logf (LOG_DEBUG, "term: %s", term_dst);
1465 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1466 grep_info.isam_p_indx, term_dst,
1467 strlen(term_dst), rank_type);
1468 assert (rset[rset_no]);
1469 if (++rset_no >= sizeof(rset)/sizeof(*rset))
1473 xfree(grep_info.term_no);
1475 xfree (grep_info.isam_p_buf);
1478 rset_null_parms parms;
1480 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1481 return rset_create (rset_kind_null, &parms);
1484 for (i = 1; i<rset_no; i++)
1486 rset_bool_parms bool_parms;
1488 bool_parms.rset_l = result;
1489 bool_parms.rset_r = rset[i];
1490 bool_parms.key_size = sizeof(struct it_key);
1491 bool_parms.cmp = key_compare_it;
1492 result = rset_create (rset_kind_and, &bool_parms);
1497 static int numeric_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1498 const char **term_sub,
1500 oid_value attributeSet,
1501 struct grep_info *grep_info,
1510 char *term_tmp = term_dict + strlen(term_dict);
1512 attr_init (&relation, zapt, 2);
1513 relation_value = attr_find (&relation, NULL);
1515 logf (LOG_DEBUG, "numeric relation value=%d", relation_value);
1517 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_tmp, 1,
1520 term_value = atoi (term_tmp);
1521 switch (relation_value)
1524 logf (LOG_DEBUG, "Relation <");
1525 gen_regular_rel (term_tmp, term_value-1, 1);
1528 logf (LOG_DEBUG, "Relation <=");
1529 gen_regular_rel (term_tmp, term_value, 1);
1532 logf (LOG_DEBUG, "Relation >=");
1533 gen_regular_rel (term_tmp, term_value, 0);
1536 logf (LOG_DEBUG, "Relation >");
1537 gen_regular_rel (term_tmp, term_value+1, 0);
1541 logf (LOG_DEBUG, "Relation =");
1542 sprintf (term_tmp, "(0*%d)", term_value);
1544 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_tmp);
1545 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info, max_pos,
1548 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1549 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1553 static int numeric_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1554 const char **term_sub,
1555 oid_value attributeSet, struct grep_info *grep_info,
1556 int reg_type, int complete_flag,
1557 int num_bases, char **basenames,
1560 char term_dict[2*IT_MAX_WORD+2];
1564 oid_value curAttributeSet = attributeSet;
1566 struct rpn_char_map_info rcmi;
1568 rpn_char_map_prepare (zh, reg_type, &rcmi);
1569 attr_init (&use, zapt, 1);
1570 use_value = attr_find (&use, &curAttributeSet);
1571 logf (LOG_DEBUG, "numeric_term, use value %d", use_value);
1573 if (use_value == -1)
1576 for (base_no = 0; base_no < num_bases; base_no++)
1579 data1_local_attribute *local_attr;
1580 int max_pos, prefix_len = 0;
1583 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
1585 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1586 curAttributeSet, use_value, r);
1593 if (zebraExplain_curDatabase (zh->zei, basenames[base_no]))
1595 zh->errCode = 109; /* Database unavailable */
1596 zh->errString = basenames[base_no];
1599 for (local_attr = attp.local_attributes; local_attr;
1600 local_attr = local_attr->next)
1606 ord = zebraExplain_lookupSU (zh->zei, attp.attset_ordinal,
1611 term_dict[prefix_len++] = '|';
1613 term_dict[prefix_len++] = '(';
1615 ord_len = key_SU_code (ord, ord_buf);
1616 for (i = 0; i<ord_len; i++)
1618 term_dict[prefix_len++] = 1;
1619 term_dict[prefix_len++] = ord_buf[i];
1627 term_dict[prefix_len++] = ')';
1628 term_dict[prefix_len++] = 1;
1629 term_dict[prefix_len++] = reg_type;
1630 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1631 term_dict[prefix_len] = '\0';
1632 if (!numeric_relation (zh, zapt, &termp, term_dict,
1633 attributeSet, grep_info, &max_pos, reg_type,
1638 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1642 static RSET rpn_search_APT_numeric (ZebraHandle zh,
1643 Z_AttributesPlusTerm *zapt,
1645 oid_value attributeSet,
1646 int reg_type, int complete_flag,
1647 const char *rank_type,
1648 int num_bases, char **basenames)
1650 char term_dst[IT_MAX_WORD+1];
1651 const char *termp = termz;
1652 RSET rset[60], result;
1653 int i, r, rset_no = 0;
1654 struct grep_info grep_info;
1657 grep_info.term_no = 0;
1659 grep_info.isam_p_size = 0;
1660 grep_info.isam_p_buf = NULL;
1662 grep_info.reg_type = reg_type;
1666 logf (LOG_DEBUG, "APT_numeric termp=%s", termp);
1667 grep_info.isam_p_indx = 0;
1668 r = numeric_term (zh, zapt, &termp, attributeSet, &grep_info,
1669 reg_type, complete_flag, num_bases, basenames,
1673 logf (LOG_DEBUG, "term: %s", term_dst);
1674 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1675 grep_info.isam_p_indx, term_dst,
1676 strlen(term_dst), rank_type);
1677 assert (rset[rset_no]);
1678 if (++rset_no >= sizeof(rset)/sizeof(*rset))
1682 xfree(grep_info.term_no);
1684 xfree (grep_info.isam_p_buf);
1687 rset_null_parms parms;
1689 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1690 return rset_create (rset_kind_null, &parms);
1693 for (i = 1; i<rset_no; i++)
1695 rset_bool_parms bool_parms;
1697 bool_parms.rset_l = result;
1698 bool_parms.rset_r = rset[i];
1699 bool_parms.key_size = sizeof(struct it_key);
1700 bool_parms.cmp = key_compare_it;
1701 result = rset_create (rset_kind_and, &bool_parms);
1706 static RSET rpn_search_APT_local (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1708 oid_value attributeSet,
1709 const char *rank_type)
1714 rset_temp_parms parms;
1716 parms.rset_term = rset_term_create (termz, -1, rank_type);
1717 parms.key_size = sizeof (struct it_key);
1718 parms.temp_path = res_get (zh->res, "setTmpDir");
1719 result = rset_create (rset_kind_temp, &parms);
1720 rsfd = rset_open (result, RSETF_WRITE);
1722 key.sysno = atoi (termz);
1726 rset_write (result, rsfd, &key);
1727 rset_close (result, rsfd);
1731 static RSET rpn_sort_spec (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1732 oid_value attributeSet, ODR stream,
1733 Z_SortKeySpecList *sort_sequence,
1734 const char *rank_type)
1736 rset_null_parms parms;
1738 int sort_relation_value;
1739 AttrType sort_relation_type;
1744 Z_AttributeElement *ae;
1748 attr_init (&sort_relation_type, zapt, 7);
1749 sort_relation_value = attr_find (&sort_relation_type, &attributeSet);
1751 attr_init (&use_type, zapt, 1);
1752 use_value = attr_find (&use_type, &attributeSet);
1754 if (!sort_sequence->specs)
1756 sort_sequence->num_specs = 10;
1757 sort_sequence->specs = odr_malloc (stream, sort_sequence->num_specs *
1758 sizeof(*sort_sequence->specs));
1759 for (i = 0; i<sort_sequence->num_specs; i++)
1760 sort_sequence->specs[i] = 0;
1762 if (zapt->term->which != Z_Term_general)
1765 i = atoi_n (zapt->term->u.general->buf, zapt->term->u.general->len);
1766 if (i >= sort_sequence->num_specs)
1769 oe.proto = PROTO_Z3950;
1770 oe.oclass = CLASS_ATTSET;
1771 oe.value = attributeSet;
1772 if (!oid_ent_to_oid (&oe, oid))
1775 sks = odr_malloc (stream, sizeof(*sks));
1776 sks->sortElement = odr_malloc (stream, sizeof(*sks->sortElement));
1777 sks->sortElement->which = Z_SortElement_generic;
1778 sk = sks->sortElement->u.generic = odr_malloc (stream, sizeof(*sk));
1779 sk->which = Z_SortKey_sortAttributes;
1780 sk->u.sortAttributes = odr_malloc (stream, sizeof(*sk->u.sortAttributes));
1782 sk->u.sortAttributes->id = oid;
1783 sk->u.sortAttributes->list =
1784 odr_malloc (stream, sizeof(*sk->u.sortAttributes->list));
1785 sk->u.sortAttributes->list->num_attributes = 1;
1786 sk->u.sortAttributes->list->attributes =
1787 odr_malloc (stream, sizeof(*sk->u.sortAttributes->list->attributes));
1788 ae = *sk->u.sortAttributes->list->attributes =
1789 odr_malloc (stream, sizeof(**sk->u.sortAttributes->list->attributes));
1790 ae->attributeSet = 0;
1791 ae->attributeType = odr_malloc (stream, sizeof(*ae->attributeType));
1792 *ae->attributeType = 1;
1793 ae->which = Z_AttributeValue_numeric;
1794 ae->value.numeric = odr_malloc (stream, sizeof(*ae->value.numeric));
1795 *ae->value.numeric = use_value;
1797 sks->sortRelation = odr_malloc (stream, sizeof(*sks->sortRelation));
1798 if (sort_relation_value == 1)
1799 *sks->sortRelation = Z_SortRelation_ascending;
1800 else if (sort_relation_value == 2)
1801 *sks->sortRelation = Z_SortRelation_descending;
1803 *sks->sortRelation = Z_SortRelation_ascending;
1805 sks->caseSensitivity = odr_malloc (stream, sizeof(*sks->caseSensitivity));
1806 *sks->caseSensitivity = 0;
1808 sks->missingValueAction = 0;
1810 sort_sequence->specs[i] = sks;
1812 parms.rset_term = rset_term_create ("", -1, rank_type);
1813 return rset_create (rset_kind_null, &parms);
1817 static RSET rpn_search_APT (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1818 oid_value attributeSet, ODR stream,
1819 Z_SortKeySpecList *sort_sequence,
1820 int num_bases, char **basenames)
1823 char *search_type = NULL;
1824 char *rank_type = NULL;
1827 char termz[IT_MAX_WORD+1];
1829 zebra_maps_attr (zh->zebra_maps, zapt, ®_id, &search_type,
1830 &rank_type, &complete_flag, &sort_flag);
1832 logf (LOG_DEBUG, "reg_id=%c", reg_id);
1833 logf (LOG_DEBUG, "complete_flag=%d", complete_flag);
1834 logf (LOG_DEBUG, "search_type=%s", search_type);
1835 logf (LOG_DEBUG, "rank_type=%s", rank_type);
1837 if (zapt->term->which != Z_Term_general)
1842 trans_term (zh, zapt, termz);
1845 return rpn_sort_spec (zh, zapt, attributeSet, stream, sort_sequence,
1848 if (!strcmp (search_type, "phrase"))
1850 return rpn_search_APT_phrase (zh, zapt, termz, attributeSet,
1851 reg_id, complete_flag, rank_type,
1852 num_bases, basenames);
1854 else if (!strcmp (search_type, "and-list"))
1856 return rpn_search_APT_and_list (zh, zapt, termz, attributeSet,
1857 reg_id, complete_flag, rank_type,
1858 num_bases, basenames);
1860 else if (!strcmp (search_type, "or-list"))
1862 return rpn_search_APT_or_list (zh, zapt, termz, attributeSet,
1863 reg_id, complete_flag, rank_type,
1864 num_bases, basenames);
1866 else if (!strcmp (search_type, "local"))
1868 return rpn_search_APT_local (zh, zapt, termz, attributeSet,
1871 else if (!strcmp (search_type, "numeric"))
1873 return rpn_search_APT_numeric (zh, zapt, termz, attributeSet,
1874 reg_id, complete_flag, rank_type,
1875 num_bases, basenames);
1881 static RSET rpn_search_structure (ZebraHandle zh, Z_RPNStructure *zs,
1882 oid_value attributeSet, ODR stream,
1883 Z_SortKeySpecList *sort_sequence,
1884 int num_bases, char **basenames)
1887 if (zs->which == Z_RPNStructure_complex)
1889 Z_Operator *zop = zs->u.complex->roperator;
1890 rset_bool_parms bool_parms;
1892 bool_parms.rset_l = rpn_search_structure (zh, zs->u.complex->s1,
1893 attributeSet, stream,
1895 num_bases, basenames);
1896 if (bool_parms.rset_l == NULL)
1898 bool_parms.rset_r = rpn_search_structure (zh, zs->u.complex->s2,
1899 attributeSet, stream,
1901 num_bases, basenames);
1902 if (bool_parms.rset_r == NULL)
1904 rset_delete (bool_parms.rset_l);
1907 bool_parms.key_size = sizeof(struct it_key);
1908 bool_parms.cmp = key_compare_it;
1912 case Z_Operator_and:
1913 r = rset_create (rset_kind_and, &bool_parms);
1916 r = rset_create (rset_kind_or, &bool_parms);
1918 case Z_Operator_and_not:
1919 r = rset_create (rset_kind_not, &bool_parms);
1921 case Z_Operator_prox:
1923 if (zop->u.prox->which != Z_ProximityOperator_known)
1929 if (zop->u.prox->which != Z_ProxCode_known)
1937 if (*zop->u.prox->u.known != Z_ProxUnit_word)
1939 char *val = odr_malloc (stream, 16);
1941 zh->errString = val;
1942 sprintf (val, "%d", *zop->u.prox->u.known);
1946 if (*zop->u.prox->proximityUnitCode != Z_ProxUnit_word)
1948 char *val = odr_malloc (stream, 16);
1950 zh->errString = val;
1951 sprintf (val, "%d", *zop->u.prox->proximityUnitCode);
1955 r = rpn_proximity (zh, bool_parms.rset_l, bool_parms.rset_r,
1956 *zop->u.prox->ordered,
1957 (!zop->u.prox->exclusion ? 0 :
1958 *zop->u.prox->exclusion),
1959 *zop->u.prox->relationType,
1960 *zop->u.prox->distance);
1967 else if (zs->which == Z_RPNStructure_simple)
1969 if (zs->u.simple->which == Z_Operand_APT)
1971 logf (LOG_DEBUG, "rpn_search_APT");
1972 r = rpn_search_APT (zh, zs->u.simple->u.attributesPlusTerm,
1973 attributeSet, stream, sort_sequence,
1974 num_bases, basenames);
1976 else if (zs->u.simple->which == Z_Operand_resultSetId)
1978 logf (LOG_DEBUG, "rpn_search_ref");
1979 r = resultSetRef (zh, zs->u.simple->u.resultSetId);
1981 r = rset_create (rset_kind_null, NULL);
1997 void rpn_search (ZebraHandle zh, ODR stream,
1998 Z_RPNQuery *rpn, int num_bases, char **basenames,
1999 const char *setname)
2003 oid_value attributeSet;
2004 Z_SortKeySpecList *sort_sequence;
2010 zh->errString = NULL;
2013 sort_sequence = odr_malloc (stream, sizeof(*sort_sequence));
2014 sort_sequence->num_specs = 10;
2015 sort_sequence->specs = odr_malloc (stream, sort_sequence->num_specs *
2016 sizeof(*sort_sequence->specs));
2017 for (i = 0; i<sort_sequence->num_specs; i++)
2018 sort_sequence->specs[i] = 0;
2020 attrset = oid_getentbyoid (rpn->attributeSetId);
2021 attributeSet = attrset->value;
2022 rset = rpn_search_structure (zh, rpn->RPNStructure, attributeSet, stream,
2024 num_bases, basenames);
2028 resultSetAdd (zh, setname, 1, rset, &zh->hits);
2030 logf (LOG_DEBUG, "search error: %d", zh->errCode);
2032 for (i = 0; sort_sequence->specs[i]; i++)
2034 sort_sequence->num_specs = i;
2036 resultSetSort (zh, stream, 1, &setname, setname, sort_sequence,
2040 struct scan_info_entry {
2046 struct scan_info_entry *list;
2052 static int scan_handle (char *name, const char *info, int pos, void *client)
2054 int len_prefix, idx;
2055 struct scan_info *scan_info = client;
2057 len_prefix = strlen(scan_info->prefix);
2058 if (memcmp (name, scan_info->prefix, len_prefix))
2061 idx = scan_info->after - pos + scan_info->before;
2064 scan_info->list[idx].term = odr_malloc (scan_info->odr,
2065 strlen(name + len_prefix)+1);
2066 strcpy (scan_info->list[idx].term, name + len_prefix);
2067 assert (*info == sizeof(ISAM_P));
2068 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAM_P));
2072 static void scan_term_untrans (ZebraHandle zh, ODR stream, int reg_type,
2073 char **dst, const char *src)
2075 char term_dst[1024];
2077 term_untrans (zh, reg_type, term_dst, src);
2079 *dst = odr_malloc (stream, strlen(term_dst)+1);
2080 strcpy (*dst, term_dst);
2083 static void count_set (RSET r, int *count)
2091 logf (LOG_DEBUG, "count_set");
2094 rfd = rset_open (r, RSETF_READ);
2095 while (rset_read (r, rfd, &key, &term_index))
2097 if (key.sysno != psysno)
2104 rset_close (r, rfd);
2105 logf (LOG_DEBUG, "%d keys, %d records", kno, *count);
2108 void rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
2109 oid_value attributeset,
2110 int num_bases, char **basenames,
2111 int *position, int *num_entries, ZebraScanEntry **list,
2115 int pos = *position;
2116 int num = *num_entries;
2120 char termz[IT_MAX_WORD+20];
2123 struct scan_info *scan_info_array;
2124 ZebraScanEntry *glist;
2125 int ords[32], ord_no = 0;
2129 char *search_type = NULL;
2130 char *rank_type = NULL;
2134 if (attributeset == VAL_NONE)
2135 attributeset = VAL_BIB1;
2137 zlog_scan (zapt, attributeset);
2138 logf (LOG_DEBUG, "position = %d, num = %d", pos, num);
2140 attr_init (&use, zapt, 1);
2141 use_value = attr_find (&use, &attributeset);
2143 if (zebra_maps_attr (zh->zebra_maps, zapt, ®_id, &search_type,
2144 &rank_type, &complete_flag, &sort_flag))
2150 if (use_value == -1)
2152 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
2156 data1_local_attribute *local_attr;
2158 if ((r=att_getentbyatt (zh, &attp, attributeset, use_value)))
2160 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
2161 attributeset, use_value);
2167 if (zebraExplain_curDatabase (zh->zei, basenames[base_no]))
2169 zh->errString = basenames[base_no];
2170 zh->errCode = 109; /* Database unavailable */
2173 for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
2174 local_attr = local_attr->next)
2178 ord = zebraExplain_lookupSU (zh->zei, attp.attset_ordinal,
2181 ords[ord_no++] = ord;
2189 /* prepare dictionary scanning */
2192 scan_info_array = odr_malloc (stream, ord_no * sizeof(*scan_info_array));
2193 for (i = 0; i < ord_no; i++)
2195 int j, prefix_len = 0;
2196 int before_tmp = before, after_tmp = after;
2197 struct scan_info *scan_info = scan_info_array + i;
2198 struct rpn_char_map_info rcmi;
2200 rpn_char_map_prepare (zh, reg_id, &rcmi);
2202 scan_info->before = before;
2203 scan_info->after = after;
2204 scan_info->odr = stream;
2206 scan_info->list = odr_malloc (stream, (before+after)*
2207 sizeof(*scan_info->list));
2208 for (j = 0; j<before+after; j++)
2209 scan_info->list[j].term = NULL;
2211 prefix_len += key_SU_code (ords[i], termz + prefix_len);
2212 termz[prefix_len++] = reg_id;
2213 termz[prefix_len] = 0;
2214 strcpy (scan_info->prefix, termz);
2216 trans_scan_term (zh, zapt, termz+prefix_len, reg_id);
2218 dict_scan (zh->dict, termz, &before_tmp, &after_tmp, scan_info,
2221 glist = odr_malloc (stream, (before+after)*sizeof(*glist));
2223 /* consider terms after main term */
2224 for (i = 0; i < ord_no; i++)
2228 for (i = 0; i<after; i++)
2231 const char *mterm = NULL;
2235 for (j = 0; j < ord_no; j++)
2237 if (ptr[j] < before+after &&
2238 (tst=scan_info_array[j].list[ptr[j]].term) &&
2239 (!mterm || strcmp (tst, mterm) < 0))
2247 scan_term_untrans (zh, stream, reg_id,
2248 &glist[i+before].term, mterm);
2249 rset = rset_trunc (zh, &scan_info_array[j0].list[ptr[j0]].isam_p, 1,
2250 glist[i+before].term, strlen(glist[i+before].term),
2254 for (j = j0+1; j<ord_no; j++)
2256 if (ptr[j] < before+after &&
2257 (tst=scan_info_array[j].list[ptr[j]].term) &&
2258 !strcmp (tst, mterm))
2260 rset_bool_parms bool_parms;
2264 rset_trunc (zh, &scan_info_array[j].list[ptr[j]].isam_p, 1,
2265 glist[i+before].term,
2266 strlen(glist[i+before].term), NULL);
2268 bool_parms.key_size = sizeof(struct it_key);
2269 bool_parms.cmp = key_compare_it;
2270 bool_parms.rset_l = rset;
2271 bool_parms.rset_r = rset2;
2273 rset = rset_create (rset_kind_or, &bool_parms);
2278 count_set (rset, &glist[i+before].occurrences);
2283 *num_entries -= (after-i);
2287 /* consider terms before main term */
2288 for (i = 0; i<ord_no; i++)
2291 for (i = 0; i<before; i++)
2294 const char *mterm = NULL;
2298 for (j = 0; j <ord_no; j++)
2300 if (ptr[j] < before &&
2301 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2302 (!mterm || strcmp (tst, mterm) > 0))
2311 scan_term_untrans (zh, stream, reg_id,
2312 &glist[before-1-i].term, mterm);
2315 (zh, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1,
2316 glist[before-1-i].term, strlen(glist[before-1-i].term),
2321 for (j = j0+1; j<ord_no; j++)
2323 if (ptr[j] < before &&
2324 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2325 !strcmp (tst, mterm))
2327 rset_bool_parms bool_parms;
2330 rset2 = rset_trunc (zh,
2331 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1,
2332 glist[before-1-i].term,
2333 strlen(glist[before-1-i].term), NULL);
2335 bool_parms.key_size = sizeof(struct it_key);
2336 bool_parms.cmp = key_compare_it;
2337 bool_parms.rset_l = rset;
2338 bool_parms.rset_r = rset2;
2340 rset = rset_create (rset_kind_or, &bool_parms);
2345 count_set (rset, &glist[before-1-i].occurrences);
2355 *list = glist + i; /* list is set to first 'real' entry */
2357 logf (LOG_DEBUG, "position = %d, num_entries = %d",
2358 *position, *num_entries);
2360 logf (LOG_DEBUG, "scan error: %d", zh->errCode);