2 * Copyright (C) 1995-1998, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.82 1998-06-26 11:16:40 quinn
8 * Added support (un-optimised) for left and left/right truncation
10 * Revision 1.81 1998/06/24 12:16:14 adam
11 * Support for relations on text operands. Open range support in
12 * DFA module (i.e. [-j], [g-]).
14 * Revision 1.80 1998/06/23 15:33:34 adam
15 * Added feature to specify sort criteria in query (type 7 specifies
18 * Revision 1.79 1998/06/22 11:35:09 adam
21 * Revision 1.78 1998/06/08 14:43:17 adam
22 * Added suport for EXPLAIN Proxy servers - added settings databasePath
23 * and explainDatabase to facilitate this. Increased maximum number
24 * of databases and attributes in one register.
26 * Revision 1.77 1998/05/20 10:12:22 adam
27 * Implemented automatic EXPLAIN database maintenance.
28 * Modified Zebra to work with ASN.1 compiled version of YAZ.
30 * Revision 1.76 1998/04/02 14:35:29 adam
31 * First version of Zebra that works with compiled ASN.1.
33 * Revision 1.75 1998/03/05 08:45:13 adam
34 * New result set model and modular ranking system. Moved towards
35 * descent server API. System information stored as "SGML" records.
37 * Revision 1.74 1998/02/10 12:03:06 adam
40 * Revision 1.73 1998/01/29 13:40:11 adam
41 * Better logging for scan service.
43 * Revision 1.72 1998/01/07 13:53:41 adam
44 * Queries using simple ranked operands returns right number of hits.
46 * Revision 1.71 1997/12/18 10:54:24 adam
47 * New method result set method rs_hits that returns the number of
48 * hits in result-set (if known). The ranked result set returns real
49 * number of hits but only when not combined with other operands.
51 * Revision 1.70 1997/10/31 12:34:43 adam
52 * Changed a few log statements.
54 * Revision 1.69 1997/10/29 12:05:02 adam
55 * Server produces diagnostic "Unsupported Attribute Set" when appropriate.
57 * Revision 1.68 1997/10/27 14:33:06 adam
58 * Moved towards generic character mapping depending on "structure"
59 * field in abstract syntax file. Fixed a few memory leaks. Fixed
60 * bug with negative integers when doing searches with relational
63 * Revision 1.67 1997/09/29 09:06:10 adam
64 * Removed one static var in order to make this module thread safe.
66 * Revision 1.66 1997/09/25 14:58:03 adam
69 * Revision 1.65 1997/09/22 12:39:06 adam
70 * Added get_pos method for the ranked result sets.
72 * Revision 1.64 1997/09/18 08:59:20 adam
73 * Extra generic handle for the character mapping routines.
75 * Revision 1.63 1997/09/17 12:19:18 adam
76 * Zebra version corresponds to YAZ version 1.4.
77 * Changed Zebra server so that it doesn't depend on global common_resource.
79 * Revision 1.62 1997/09/05 15:30:09 adam
80 * Changed prototype for chr_map_input - added const.
81 * Added support for C++, headers uses extern "C" for public definitions.
83 * Revision 1.61 1997/02/10 10:21:14 adam
84 * Bug fix: in search terms character (^) wasn't observed.
86 * Revision 1.60 1997/01/31 11:10:34 adam
87 * Bug fix: Leading and trailing white space weren't removed in scan tokens.
89 * Revision 1.59 1997/01/17 11:31:46 adam
90 * Bug fix: complete phrase search didn't work.
92 * Revision 1.58 1996/12/23 15:30:45 adam
94 * Bug fix: result sets weren't deleted after server shut down.
96 * Revision 1.57 1996/11/11 13:38:02 adam
97 * Added proximity support in search.
99 * Revision 1.56 1996/11/08 11:10:32 adam
100 * Buffers used during file match got bigger.
101 * Compressed ISAM support everywhere.
102 * Bug fixes regarding masking characters in queries.
103 * Redesigned Regexp-2 queries.
105 * Revision 1.55 1996/11/04 14:07:44 adam
106 * Moved truncation code to trunc.c.
108 * Revision 1.54 1996/10/29 14:09:52 adam
109 * Use of cisam system - enabled if setting isamc is 1.
111 * Revision 1.53 1996/06/26 09:21:43 adam
112 * Bug fix: local attribute set wasn't obeyed in scan.
114 * Revision 1.52 1996/06/17 14:26:20 adam
115 * Function gen_regular_rel changed to handle negative numbers.
117 * Revision 1.51 1996/06/11 10:54:15 quinn
120 * Revision 1.50 1996/06/07 08:51:53 adam
121 * Bug fix: Character mapping was broken (introducued by last revision).
123 * Revision 1.49 1996/06/04 10:18:11 adam
124 * Search/scan uses character mapping module.
126 * Revision 1.48 1996/05/28 15:15:01 adam
127 * Bug fix: Didn't handle unknown database correctly.
129 * Revision 1.47 1996/05/15 18:36:28 adam
130 * Function trans_term transforms unsearchable characters to blanks.
132 * Revision 1.46 1996/05/15 11:57:56 adam
133 * Fixed bug introduced by set/field mapping in search operations.
135 * Revision 1.45 1996/05/14 11:34:00 adam
136 * Scan support in multiple registers/databases.
138 * Revision 1.44 1996/05/14 06:16:44 adam
139 * Compact use/set bytes used in search service.
141 * Revision 1.43 1996/05/09 09:54:43 adam
142 * Server supports maps from one logical attributes to a list of physical
144 * The extraction process doesn't make space consuming 'any' keys.
146 * Revision 1.42 1996/05/09 07:28:56 quinn
147 * Work towards phrases and multiple registers
149 * Revision 1.41 1996/03/20 09:36:43 adam
150 * Function dict_lookup_grep got extra parameter, init_pos, which marks
151 * from which position in pattern approximate pattern matching should occur.
152 * Approximate pattern matching is used in relevance=re-2.
154 * Revision 1.40 1996/02/02 13:44:44 adam
155 * The public dictionary functions simply use char instead of Dict_char
156 * to represent search strings. Dict_char is used internally only.
158 * Revision 1.39 1996/01/03 16:22:13 quinn
159 * operator->roperator
161 * Revision 1.38 1995/12/11 09:12:55 adam
162 * The rec_get function returns NULL if record doesn't exist - will
163 * happen in the server if the result set records have been deleted since
164 * the creation of the set (i.e. the search).
165 * The server saves a result temporarily if it is 'volatile', i.e. the
166 * set is register dependent.
168 * Revision 1.37 1995/12/06 15:05:28 adam
169 * More verbose in count_set.
171 * Revision 1.36 1995/12/06 12:41:27 adam
172 * New command 'stat' for the index program.
173 * Filenames can be read from stdin by specifying '-'.
174 * Bug fix/enhancement of the transformation from terms to regular
175 * expressons in the search engine.
177 * Revision 1.35 1995/11/27 09:29:00 adam
178 * Bug fixes regarding conversion to regular expressions.
180 * Revision 1.34 1995/11/16 17:00:56 adam
181 * Better logging of rpn query.
183 * Revision 1.33 1995/11/01 13:58:28 quinn
184 * Moving data1 to yaz/retrieval
186 * Revision 1.32 1995/10/27 14:00:11 adam
187 * Implemented detection of database availability.
189 * Revision 1.31 1995/10/17 18:02:10 adam
190 * New feature: databases. Implemented as prefix to words in dictionary.
192 * Revision 1.30 1995/10/16 09:32:38 adam
193 * More work on relational op.
195 * Revision 1.29 1995/10/13 16:01:49 adam
198 * Revision 1.28 1995/10/13 12:26:43 adam
199 * Optimization of truncation.
201 * Revision 1.27 1995/10/12 17:07:22 adam
204 * Revision 1.26 1995/10/12 12:40:54 adam
205 * Bug fixes in rpn_prox.
207 * Revision 1.25 1995/10/10 13:59:24 adam
208 * Function rset_open changed its wflag parameter to general flags.
210 * Revision 1.24 1995/10/09 16:18:37 adam
211 * Function dict_lookup_grep got extra client data parameter.
213 * Revision 1.23 1995/10/06 16:33:37 adam
214 * Use attribute mappings.
216 * Revision 1.22 1995/10/06 15:07:39 adam
217 * Structure 'local-number' handled.
219 * Revision 1.21 1995/10/06 13:52:06 adam
220 * Bug fixes. Handler may abort further scanning.
222 * Revision 1.20 1995/10/06 11:06:33 adam
223 * Scan entries include 'occurrences' now.
225 * Revision 1.19 1995/10/06 10:43:56 adam
226 * Scan added. 'occurrences' in scan entries not set yet.
228 * Revision 1.18 1995/10/04 16:57:20 adam
229 * Key input and merge sort in one pass.
231 * Revision 1.17 1995/10/04 12:55:17 adam
232 * Bug fix in ranked search. Use=Any keys inserted.
234 * Revision 1.16 1995/10/02 16:24:40 adam
235 * Use attribute actually used in search requests.
237 * Revision 1.15 1995/10/02 15:18:52 adam
238 * New member in recRetrieveCtrl: diagnostic.
240 * Revision 1.14 1995/09/28 12:10:32 adam
241 * Bug fixes. Field prefix used in queries.
243 * Revision 1.13 1995/09/18 14:17:50 adam
246 * Revision 1.12 1995/09/15 14:45:21 adam
248 * Work on truncation.
250 * Revision 1.11 1995/09/14 11:53:27 adam
251 * First work on regular expressions/truncations.
253 * Revision 1.10 1995/09/11 15:23:26 adam
254 * More work on relevance search.
256 * Revision 1.9 1995/09/11 13:09:35 adam
257 * More work on relevance feedback.
259 * Revision 1.8 1995/09/08 14:52:27 adam
260 * Minor changes. Dictionary is lower case now.
262 * Revision 1.7 1995/09/07 13:58:36 adam
263 * New parameter: result-set file descriptor (RSFD) to support multiple
264 * positions within the same result-set.
265 * Boolean operators: and, or, not implemented.
266 * Result-set references.
268 * Revision 1.6 1995/09/06 16:11:18 adam
269 * Option: only one word key per file.
271 * Revision 1.5 1995/09/06 10:33:04 adam
272 * More work on present. Some log messages removed.
274 * Revision 1.4 1995/09/05 15:28:40 adam
275 * More work on search engine.
277 * Revision 1.3 1995/09/04 15:20:22 adam
280 * Revision 1.2 1995/09/04 12:33:43 adam
281 * Various cleanup. YAZ util used instead.
283 * Revision 1.1 1995/09/04 09:10:40 adam
284 * More work on index add/del/update.
285 * Merge sort implemented.
286 * Initial work on z39 server.
305 struct rpn_char_map_info {
310 static const char **rpn_char_map_handler (void *vp, const char **from, int len)
312 struct rpn_char_map_info *p = vp;
313 return zebra_maps_input (p->zm, p->reg_type, from, len);
316 static void rpn_char_map_prepare (ZebraHandle zh, int reg_type,
317 struct rpn_char_map_info *map_info)
319 map_info->zm = zh->zebra_maps;
320 map_info->reg_type = reg_type;
321 dict_grep_cmap (zh->dict, map_info, rpn_char_map_handler);
328 Z_AttributesPlusTerm *zapt;
331 static int attr_find (AttrType *src, oid_value *attributeSetP)
336 num_attributes = src->zapt->attributes->num_attributes;
338 num_attributes = src->zapt->num_attributes;
340 while (src->major < num_attributes)
342 Z_AttributeElement *element;
345 element = src->zapt->attributes->attributes[src->major];
347 element = src->zapt->attributeList[src->major];
349 if (src->type == *element->attributeType)
351 switch (element->which)
353 case Z_AttributeValue_numeric:
355 if (element->attributeSet && attributeSetP)
359 attrset = oid_getentbyoid (element->attributeSet);
360 *attributeSetP = attrset->value;
362 return *element->value.numeric;
364 case Z_AttributeValue_complex:
365 if (src->minor >= element->value.complex->num_list ||
366 element->value.complex->list[src->minor]->which !=
367 Z_StringOrNumeric_numeric)
370 if (element->attributeSet && attributeSetP)
374 attrset = oid_getentbyoid (element->attributeSet);
375 *attributeSetP = attrset->value;
377 return *element->value.complex->list[src->minor-1]->u.numeric;
387 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
409 static void term_untrans (ZebraHandle zh, int reg_type,
410 char *dst, const char *src)
414 const char *cp = zebra_maps_output (zh->zebra_maps, reg_type, &src);
421 static void add_isam_p (const char *name, const char *info,
424 if (p->isam_p_indx == p->isam_p_size)
426 ISAM_P *new_isam_p_buf;
430 p->isam_p_size = 2*p->isam_p_size + 100;
431 new_isam_p_buf = xmalloc (sizeof(*new_isam_p_buf) *
435 memcpy (new_isam_p_buf, p->isam_p_buf,
436 p->isam_p_indx * sizeof(*p->isam_p_buf));
437 xfree (p->isam_p_buf);
439 p->isam_p_buf = new_isam_p_buf;
442 new_term_no = xmalloc (sizeof(*new_term_no) *
446 memcpy (new_term_no, p->isam_p_buf,
447 p->isam_p_indx * sizeof(*p->term_no));
450 p->term_no = new_term_no;
453 assert (*info == sizeof(*p->isam_p_buf));
454 memcpy (p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
457 term_untrans (p->zh, p->reg_type, term_tmp, name+2);
458 logf (LOG_DEBUG, "grep: %s", term_tmp);
463 static int grep_handle (char *name, const char *info, void *p)
465 add_isam_p (name, info, p);
469 static int term_pre (ZebraMaps zebra_maps, int reg_type, const char **src,
470 const char *ct1, const char *ct2)
472 const char *s1, *s0 = *src;
475 /* skip white space */
478 if (ct1 && strchr (ct1, *s0))
480 if (ct2 && strchr (ct2, *s0))
483 map = zebra_maps_input (zebra_maps, reg_type, &s1, strlen(s1));
484 if (**map != *CHR_SPACE)
492 static int term_100 (ZebraMaps zebra_maps, int reg_type,
493 const char **src, char *dst, int space_split,
501 if (!term_pre (zebra_maps, reg_type, src, NULL, NULL))
507 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
508 if (space_split && **map == *CHR_SPACE)
512 if (!isalnum (*s1) && *s1 != '-')
524 static int term_101 (ZebraMaps zebra_maps, int reg_type,
525 const char **src, char *dst, int space_split,
533 if (!term_pre (zebra_maps, reg_type, src, "#", "#"))
542 dst_term[j++] = *s0++;
547 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
548 if (space_split && **map == *CHR_SPACE)
560 dst_term[j++] = '\0';
566 static int term_103 (ZebraMaps zebra_maps, int reg_type, const char **src,
567 char *dst, int *errors, int space_split,
575 if (!term_pre (zebra_maps, reg_type, src, "^\\()[].*+?|", "("))
578 if (errors && *s0 == '+' && s0[1] && s0[2] == '+' && s0[3] &&
581 *errors = s0[1] - '0';
588 if (strchr ("^\\()[].*+?|-", *s0))
596 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
597 if (**map == *CHR_SPACE)
614 static int term_102 (ZebraMaps zebra_maps, int reg_type, const char **src,
615 char *dst, int space_split, char *dst_term)
617 return term_103 (zebra_maps, reg_type, src, dst, NULL, space_split,
621 /* gen_regular_rel - generate regular expression from relation
622 * val: border value (inclusive)
623 * islt: 1 if <=; 0 if >=.
625 static void gen_regular_rel (char *dst, int val, int islt)
632 logf (LOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
636 strcpy (dst, "(-[0-9]+|(");
644 strcpy (dst, "([0-9]+|-(");
645 dst_p = strlen (dst);
655 dst_p = strlen (dst);
656 sprintf (numstr, "%d", val);
657 for (w = strlen(numstr); --w >= 0; pos++)
676 strcpy (dst + dst_p, numstr);
677 dst_p = strlen(dst) - pos - 1;
705 for (i = 0; i<pos; i++)
718 for (i=1; i<pos; i++)
719 strcat (dst, "[0-9]?");
723 for (i = 0; i <= pos; i++)
724 strcat (dst, "[0-9]");
725 strcat (dst, "[0-9]*");
730 void string_rel_add_char (char **term_p, const char *src, int *indx)
732 if (src[*indx] == '\\')
733 *(*term_p)++ = src[(*indx)++];
734 *(*term_p)++ = src[(*indx)++];
738 * > abc ([b-].*|a[c-].*|ab[d-].*|abc.+)
739 * ([^-a].*|a[^-b].*ab[^-c].*|abc.+)
740 * >= abc ([b-].*|a[c-].*|ab[c-].*)
741 * ([^-a].*|a[^-b].*|ab[c-].*)
742 * < abc ([-0].*|a[-a].*|ab[-b].*)
743 * ([^a-].*|a[^b-].*|ab[^c-].*)
744 * <= abc ([-0].*|a[-a].*|ab[-b].*|abc)
745 * ([^a-].*|a[^b-].*|ab[^c-].*|abc)
747 static int string_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
748 const char **term_sub, char *term_dict,
749 oid_value attributeSet,
750 int reg_type, int space_split, char *term_dst)
755 char *term_tmp = term_dict + strlen(term_dict);
756 char term_component[256];
758 attr_init (&relation, zapt, 2);
759 relation_value = attr_find (&relation, NULL);
761 logf (LOG_DEBUG, "string relation value=%d", relation_value);
762 switch (relation_value)
765 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
766 space_split, term_dst))
768 logf (LOG_DEBUG, "Relation <");
771 for (i = 0; term_component[i]; )
778 string_rel_add_char (&term_tmp, term_component, &j);
783 string_rel_add_char (&term_tmp, term_component, &i);
794 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
795 space_split, term_dst))
797 logf (LOG_DEBUG, "Relation <=");
800 for (i = 0; term_component[i]; )
805 string_rel_add_char (&term_tmp, term_component, &j);
809 string_rel_add_char (&term_tmp, term_component, &i);
818 for (i = 0; term_component[i]; )
819 string_rel_add_char (&term_tmp, term_component, &i);
824 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
825 space_split, term_dst))
827 logf (LOG_DEBUG, "Relation >");
830 for (i = 0; term_component[i];)
835 string_rel_add_char (&term_tmp, term_component, &j);
840 string_rel_add_char (&term_tmp, term_component, &i);
848 for (i = 0; term_component[i];)
849 string_rel_add_char (&term_tmp, term_component, &i);
856 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
857 space_split, term_dst))
859 logf (LOG_DEBUG, "Relation >=");
862 for (i = 0; term_component[i];)
869 string_rel_add_char (&term_tmp, term_component, &j);
872 if (term_component[i+1])
876 string_rel_add_char (&term_tmp, term_component, &i);
880 string_rel_add_char (&term_tmp, term_component, &i);
892 logf (LOG_DEBUG, "Relation =");
893 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
894 space_split, term_dst))
896 strcat (term_tmp, "(");
897 strcat (term_tmp, term_component);
898 strcat (term_tmp, ")");
903 static int string_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
904 const char **term_sub,
905 oid_value attributeSet, struct grep_info *grep_info,
906 int reg_type, int complete_flag,
907 int num_bases, char **basenames,
910 char term_dict[2*IT_MAX_WORD+4000];
913 int truncation_value;
916 oid_value curAttributeSet = attributeSet;
918 struct rpn_char_map_info rcmi;
919 int space_split = complete_flag ? 0 : 1;
921 rpn_char_map_prepare (zh, reg_type, &rcmi);
922 attr_init (&use, zapt, 1);
923 use_value = attr_find (&use, &curAttributeSet);
924 logf (LOG_DEBUG, "string_term, use value %d", use_value);
925 attr_init (&truncation, zapt, 5);
926 truncation_value = attr_find (&truncation, NULL);
927 logf (LOG_DEBUG, "truncation value %d", truncation_value);
932 for (base_no = 0; base_no < num_bases; base_no++)
935 data1_local_attribute *local_attr;
936 int max_pos, prefix_len = 0;
939 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
941 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
942 curAttributeSet, use_value, r);
949 if (zebraExplain_curDatabase (zh->zei, basenames[base_no]))
951 zh->errCode = 109; /* Database unavailable */
952 zh->errString = basenames[base_no];
955 for (local_attr = attp.local_attributes; local_attr;
956 local_attr = local_attr->next)
962 ord = zebraExplain_lookupSU (zh->zei, attp.attset_ordinal,
967 term_dict[prefix_len++] = '|';
969 term_dict[prefix_len++] = '(';
971 ord_len = key_SU_code (ord, ord_buf);
972 for (i = 0; i<ord_len; i++)
974 term_dict[prefix_len++] = 1;
975 term_dict[prefix_len++] = ord_buf[i];
983 term_dict[prefix_len++] = ')';
984 term_dict[prefix_len++] = 1;
985 term_dict[prefix_len++] = reg_type;
986 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
987 term_dict[prefix_len] = '\0';
989 switch (truncation_value)
991 case -1: /* not specified */
992 case 100: /* do not truncate */
993 if (!string_relation (zh, zapt, &termp, term_dict,
995 reg_type, space_split, term_dst))
997 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_dict+prefix_len);
998 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info, &max_pos,
1001 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1003 case 1: /* right truncation */
1004 term_dict[j++] = '(';
1005 if (!term_100 (zh->zebra_maps, reg_type,
1006 &termp, term_dict + j, space_split, term_dst))
1008 strcat (term_dict, ".*)");
1009 dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1010 &max_pos, 0, grep_handle);
1012 case 2: /* keft truncation */
1013 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1014 if (!term_100 (zh->zebra_maps, reg_type,
1015 &termp, term_dict + j, space_split, term_dst))
1017 strcat (term_dict, ")");
1018 dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1019 &max_pos, 0, grep_handle);
1021 case 3: /* left&right truncation */
1022 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1023 if (!term_100 (zh->zebra_maps, reg_type,
1024 &termp, term_dict + j, space_split, term_dst))
1026 strcat (term_dict, ".*)");
1027 dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1028 &max_pos, 0, grep_handle);
1032 case 101: /* process # in term */
1033 term_dict[j++] = '(';
1034 if (!term_101 (zh->zebra_maps, reg_type,
1035 &termp, term_dict + j, space_split, term_dst))
1037 strcat (term_dict, ")");
1038 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1039 &max_pos, 0, grep_handle);
1041 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d", r);
1043 case 102: /* Regexp-1 */
1044 term_dict[j++] = '(';
1045 if (!term_102 (zh->zebra_maps, reg_type,
1046 &termp, term_dict + j, space_split, term_dst))
1048 strcat (term_dict, ")");
1049 logf (LOG_DEBUG, "Regexp-1 tolerance=%d", r);
1050 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1051 &max_pos, 0, grep_handle);
1053 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
1056 case 103: /* Regexp-2 */
1058 term_dict[j++] = '(';
1059 if (!term_103 (zh->zebra_maps, reg_type,
1060 &termp, term_dict + j, &r, space_split, term_dst))
1062 strcat (term_dict, ")");
1063 logf (LOG_DEBUG, "Regexp-2 tolerance=%d", r);
1064 r = dict_lookup_grep (zh->dict, term_dict, r, grep_info,
1065 &max_pos, 2, grep_handle);
1067 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
1073 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1077 static void trans_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1081 Z_Term *term = zapt->term;
1083 sizez = term->u.general->len;
1084 if (sizez > IT_MAX_WORD-1)
1085 sizez = IT_MAX_WORD-1;
1086 memcpy (termz, term->u.general->buf, sizez);
1087 termz[sizez] = '\0';
1090 static void trans_scan_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1091 char *termz, int reg_type)
1093 Z_Term *term = zapt->term;
1095 const char *cp = (const char *) term->u.general->buf;
1096 const char *cp_end = cp + term->u.general->len;
1099 const char *space_map = NULL;
1102 while ((len = (cp_end - cp)) > 0)
1104 map = zebra_maps_input (zh->zebra_maps, reg_type, &cp, len);
1105 if (**map == *CHR_SPACE)
1110 for (src = space_map; *src; src++)
1113 for (src = *map; *src; src++)
1120 static RSET rpn_proximity (ZebraHandle zh, RSET rset1, RSET rset2,
1122 int exclusion, int relation, int distance)
1127 struct it_key buf1, buf2;
1130 rset_temp_parms parms;
1133 rsfd1 = rset_open (rset1, RSETF_READ);
1134 more1 = rset_read (rset1, rsfd1, &buf1, &term_index);
1136 rsfd2 = rset_open (rset2, RSETF_READ);
1137 more2 = rset_read (rset2, rsfd2, &buf2, &term_index);
1139 parms.key_size = sizeof (struct it_key);
1140 parms.temp_path = res_get (zh->res, "setTmpDir");
1141 result = rset_create (rset_kind_temp, &parms);
1142 rsfd_result = rset_open (result, RSETF_WRITE);
1144 logf (LOG_DEBUG, "rpn_proximity excl=%d ord=%d rel=%d dis=%d",
1145 exclusion, ordered, relation, distance);
1146 while (more1 && more2)
1148 int cmp = key_compare_it (&buf1, &buf2);
1150 more1 = rset_read (rset1, rsfd1, &buf1, &term_index);
1152 more2 = rset_read (rset2, rsfd2, &buf2, &term_index);
1155 int sysno = buf1.sysno;
1159 seqno[n++] = buf1.seqno;
1160 while ((more1 = rset_read (rset1, rsfd1, &buf1, &term_index)) &&
1161 sysno == buf1.sysno)
1163 seqno[n++] = buf1.seqno;
1166 for (i = 0; i<n; i++)
1168 int diff = buf2.seqno - seqno[i];
1169 int excl = exclusion;
1170 if (!ordered && diff < 0)
1175 if (diff < distance)
1179 if (diff <= distance)
1183 if (diff == distance)
1187 if (diff >= distance)
1191 if (diff > distance)
1195 if (diff != distance)
1200 rset_write (result, rsfd_result, &buf2);
1202 } while ((more2 = rset_read (rset2, rsfd2, &buf2, &term_index)) &&
1203 sysno == buf2.sysno);
1206 rset_close (result, rsfd_result);
1207 rset_close (rset1, rsfd1);
1208 rset_close (rset2, rsfd2);
1212 static RSET rpn_prox (ZebraHandle zh, RSET *rset, int rset_no)
1217 struct it_key **buf;
1219 char prox_term[1024];
1220 int length_prox_term = 0;
1221 int min_nn = 10000000;
1223 const char *flags = NULL;
1225 rsfd = xmalloc (sizeof(*rsfd)*rset_no);
1226 more = xmalloc (sizeof(*more)*rset_no);
1227 buf = xmalloc (sizeof(*buf)*rset_no);
1229 for (i = 0; i<rset_no; i++)
1232 buf[i] = xmalloc (sizeof(**buf));
1233 rsfd[i] = rset_open (rset[i], RSETF_READ);
1234 if (!(more[i] = rset_read (rset[i], rsfd[i], buf[i], &term_index)))
1236 for (j = 0; j<rset[i]->no_rset_terms; j++)
1238 const char *nflags = rset[i]->rset_terms[j]->flags;
1239 char *term = rset[i]->rset_terms[j]->name;
1240 int lterm = strlen(term);
1241 if (length_prox_term)
1242 prox_term[length_prox_term++] = ' ';
1243 strcpy (prox_term + length_prox_term, term);
1244 length_prox_term += lterm;
1245 if (min_nn > rset[i]->rset_terms[j]->nn)
1246 min_nn = rset[i]->rset_terms[j]->nn;
1252 rset_null_parms parms;
1256 rset_close (rset[i], rsfd[i]);
1260 parms.rset_term = rset_term_create (prox_term, -1, flags);
1261 parms.rset_term->nn = 0;
1262 result = rset_create (rset_kind_null, &parms);
1266 rset_temp_parms parms;
1269 parms.rset_term = rset_term_create (prox_term, -1, flags);
1270 parms.rset_term->nn = min_nn;
1271 parms.key_size = sizeof (struct it_key);
1272 parms.temp_path = res_get (zh->res, "setTmpDir");
1273 result = rset_create (rset_kind_temp, &parms);
1274 rsfd_result = rset_open (result, RSETF_WRITE);
1278 for (i = 1; i<rset_no; i++)
1287 cmp = key_compare_it (buf[i], buf[i-1]);
1290 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1291 buf[i-1], &term_index);
1296 if (buf[i-1]->seqno+1 != buf[i]->seqno)
1298 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1299 buf[i-1], &term_index);
1305 more[i] = rset_read (rset[i], rsfd[i], buf[i],
1312 rset_write (result, rsfd_result, buf[0]);
1313 more[0] = rset_read (*rset, *rsfd, *buf, &term_index);
1317 for (i = 0; i<rset_no; i++)
1319 rset_close (rset[i], rsfd[i]);
1322 rset_close (result, rsfd_result);
1330 static RSET rpn_search_APT_phrase (ZebraHandle zh,
1331 Z_AttributesPlusTerm *zapt,
1333 oid_value attributeSet,
1334 int reg_type, int complete_flag,
1335 const char *rank_type,
1336 int num_bases, char **basenames)
1338 char term_dst[IT_MAX_WORD+1];
1339 const char *termp = termz;
1340 RSET rset[60], result;
1341 int i, r, rset_no = 0;
1342 struct grep_info grep_info;
1345 grep_info.term_no = 0;
1347 grep_info.isam_p_size = 0;
1348 grep_info.isam_p_buf = NULL;
1350 grep_info.reg_type = reg_type;
1354 logf (LOG_DEBUG, "APT_phrase termp=%s", termp);
1355 grep_info.isam_p_indx = 0;
1356 r = string_term (zh, zapt, &termp, attributeSet, &grep_info,
1357 reg_type, complete_flag, num_bases, basenames,
1361 logf (LOG_DEBUG, "term: %s", term_dst);
1362 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1363 grep_info.isam_p_indx, term_dst,
1364 strlen(term_dst), rank_type);
1365 assert (rset[rset_no]);
1366 if (++rset_no >= sizeof(rset)/sizeof(*rset))
1370 xfree(grep_info.term_no);
1372 xfree (grep_info.isam_p_buf);
1375 rset_null_parms parms;
1377 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1378 return rset_create (rset_kind_null, &parms);
1380 else if (rset_no == 1)
1382 result = rpn_prox (zh, rset, rset_no);
1383 for (i = 0; i<rset_no; i++)
1384 rset_delete (rset[i]);
1388 static RSET rpn_search_APT_or_list (ZebraHandle zh,
1389 Z_AttributesPlusTerm *zapt,
1391 oid_value attributeSet,
1392 int reg_type, int complete_flag,
1393 const char *rank_type,
1394 int num_bases, char **basenames)
1396 char term_dst[IT_MAX_WORD+1];
1397 const char *termp = termz;
1398 RSET rset[60], result;
1399 int i, r, rset_no = 0;
1400 struct grep_info grep_info;
1403 grep_info.term_no = 0;
1405 grep_info.isam_p_size = 0;
1406 grep_info.isam_p_buf = NULL;
1408 grep_info.reg_type = reg_type;
1412 logf (LOG_DEBUG, "APT_or_list termp=%s", termp);
1413 grep_info.isam_p_indx = 0;
1414 r = string_term (zh, zapt, &termp, attributeSet, &grep_info,
1415 reg_type, complete_flag, num_bases, basenames,
1419 logf (LOG_DEBUG, "term: %s", term_dst);
1420 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1421 grep_info.isam_p_indx, term_dst,
1422 strlen(term_dst), rank_type);
1423 assert (rset[rset_no]);
1424 if (++rset_no >= sizeof(rset)/sizeof(*rset))
1428 xfree(grep_info.term_no);
1430 xfree (grep_info.isam_p_buf);
1433 rset_null_parms parms;
1435 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1436 return rset_create (rset_kind_null, &parms);
1439 for (i = 1; i<rset_no; i++)
1441 rset_bool_parms bool_parms;
1443 bool_parms.rset_l = result;
1444 bool_parms.rset_r = rset[i];
1445 bool_parms.key_size = sizeof(struct it_key);
1446 bool_parms.cmp = key_compare_it;
1447 result = rset_create (rset_kind_or, &bool_parms);
1452 static RSET rpn_search_APT_and_list (ZebraHandle zh,
1453 Z_AttributesPlusTerm *zapt,
1455 oid_value attributeSet,
1456 int reg_type, int complete_flag,
1457 const char *rank_type,
1458 int num_bases, char **basenames)
1460 char term_dst[IT_MAX_WORD+1];
1461 const char *termp = termz;
1462 RSET rset[60], result;
1463 int i, r, rset_no = 0;
1464 struct grep_info grep_info;
1467 grep_info.term_no = 0;
1469 grep_info.isam_p_size = 0;
1470 grep_info.isam_p_buf = NULL;
1472 grep_info.reg_type = reg_type;
1476 logf (LOG_DEBUG, "APT_and_list termp=%s", termp);
1477 grep_info.isam_p_indx = 0;
1478 r = string_term (zh, zapt, &termp, attributeSet, &grep_info,
1479 reg_type, complete_flag, num_bases, basenames,
1483 logf (LOG_DEBUG, "term: %s", term_dst);
1484 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1485 grep_info.isam_p_indx, term_dst,
1486 strlen(term_dst), rank_type);
1487 assert (rset[rset_no]);
1488 if (++rset_no >= sizeof(rset)/sizeof(*rset))
1492 xfree(grep_info.term_no);
1494 xfree (grep_info.isam_p_buf);
1497 rset_null_parms parms;
1499 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1500 return rset_create (rset_kind_null, &parms);
1503 for (i = 1; i<rset_no; i++)
1505 rset_bool_parms bool_parms;
1507 bool_parms.rset_l = result;
1508 bool_parms.rset_r = rset[i];
1509 bool_parms.key_size = sizeof(struct it_key);
1510 bool_parms.cmp = key_compare_it;
1511 result = rset_create (rset_kind_and, &bool_parms);
1516 static int numeric_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1517 const char **term_sub,
1519 oid_value attributeSet,
1520 struct grep_info *grep_info,
1529 char *term_tmp = term_dict + strlen(term_dict);
1531 attr_init (&relation, zapt, 2);
1532 relation_value = attr_find (&relation, NULL);
1534 logf (LOG_DEBUG, "numeric relation value=%d", relation_value);
1536 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_tmp, 1,
1539 term_value = atoi (term_tmp);
1540 switch (relation_value)
1543 logf (LOG_DEBUG, "Relation <");
1544 gen_regular_rel (term_tmp, term_value-1, 1);
1547 logf (LOG_DEBUG, "Relation <=");
1548 gen_regular_rel (term_tmp, term_value, 1);
1551 logf (LOG_DEBUG, "Relation >=");
1552 gen_regular_rel (term_tmp, term_value, 0);
1555 logf (LOG_DEBUG, "Relation >");
1556 gen_regular_rel (term_tmp, term_value+1, 0);
1560 logf (LOG_DEBUG, "Relation =");
1561 sprintf (term_tmp, "(0*%d)", term_value);
1563 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_tmp);
1564 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info, max_pos,
1567 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1568 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1572 static int numeric_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1573 const char **term_sub,
1574 oid_value attributeSet, struct grep_info *grep_info,
1575 int reg_type, int complete_flag,
1576 int num_bases, char **basenames,
1579 char term_dict[2*IT_MAX_WORD+2];
1583 oid_value curAttributeSet = attributeSet;
1585 struct rpn_char_map_info rcmi;
1587 rpn_char_map_prepare (zh, reg_type, &rcmi);
1588 attr_init (&use, zapt, 1);
1589 use_value = attr_find (&use, &curAttributeSet);
1590 logf (LOG_DEBUG, "numeric_term, use value %d", use_value);
1592 if (use_value == -1)
1595 for (base_no = 0; base_no < num_bases; base_no++)
1598 data1_local_attribute *local_attr;
1599 int max_pos, prefix_len = 0;
1602 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
1604 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1605 curAttributeSet, use_value, r);
1612 if (zebraExplain_curDatabase (zh->zei, basenames[base_no]))
1614 zh->errCode = 109; /* Database unavailable */
1615 zh->errString = basenames[base_no];
1618 for (local_attr = attp.local_attributes; local_attr;
1619 local_attr = local_attr->next)
1625 ord = zebraExplain_lookupSU (zh->zei, attp.attset_ordinal,
1630 term_dict[prefix_len++] = '|';
1632 term_dict[prefix_len++] = '(';
1634 ord_len = key_SU_code (ord, ord_buf);
1635 for (i = 0; i<ord_len; i++)
1637 term_dict[prefix_len++] = 1;
1638 term_dict[prefix_len++] = ord_buf[i];
1646 term_dict[prefix_len++] = ')';
1647 term_dict[prefix_len++] = 1;
1648 term_dict[prefix_len++] = reg_type;
1649 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1650 term_dict[prefix_len] = '\0';
1651 if (!numeric_relation (zh, zapt, &termp, term_dict,
1652 attributeSet, grep_info, &max_pos, reg_type,
1657 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1661 static RSET rpn_search_APT_numeric (ZebraHandle zh,
1662 Z_AttributesPlusTerm *zapt,
1664 oid_value attributeSet,
1665 int reg_type, int complete_flag,
1666 const char *rank_type,
1667 int num_bases, char **basenames)
1669 char term_dst[IT_MAX_WORD+1];
1670 const char *termp = termz;
1671 RSET rset[60], result;
1672 int i, r, rset_no = 0;
1673 struct grep_info grep_info;
1676 grep_info.term_no = 0;
1678 grep_info.isam_p_size = 0;
1679 grep_info.isam_p_buf = NULL;
1681 grep_info.reg_type = reg_type;
1685 logf (LOG_DEBUG, "APT_numeric termp=%s", termp);
1686 grep_info.isam_p_indx = 0;
1687 r = numeric_term (zh, zapt, &termp, attributeSet, &grep_info,
1688 reg_type, complete_flag, num_bases, basenames,
1692 logf (LOG_DEBUG, "term: %s", term_dst);
1693 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1694 grep_info.isam_p_indx, term_dst,
1695 strlen(term_dst), rank_type);
1696 assert (rset[rset_no]);
1697 if (++rset_no >= sizeof(rset)/sizeof(*rset))
1701 xfree(grep_info.term_no);
1703 xfree (grep_info.isam_p_buf);
1706 rset_null_parms parms;
1708 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1709 return rset_create (rset_kind_null, &parms);
1712 for (i = 1; i<rset_no; i++)
1714 rset_bool_parms bool_parms;
1716 bool_parms.rset_l = result;
1717 bool_parms.rset_r = rset[i];
1718 bool_parms.key_size = sizeof(struct it_key);
1719 bool_parms.cmp = key_compare_it;
1720 result = rset_create (rset_kind_and, &bool_parms);
1725 static RSET rpn_search_APT_local (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1727 oid_value attributeSet,
1728 const char *rank_type)
1733 rset_temp_parms parms;
1735 parms.rset_term = rset_term_create (termz, -1, rank_type);
1736 parms.key_size = sizeof (struct it_key);
1737 parms.temp_path = res_get (zh->res, "setTmpDir");
1738 result = rset_create (rset_kind_temp, &parms);
1739 rsfd = rset_open (result, RSETF_WRITE);
1741 key.sysno = atoi (termz);
1745 rset_write (result, rsfd, &key);
1746 rset_close (result, rsfd);
1750 static RSET rpn_sort_spec (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1751 oid_value attributeSet, ODR stream,
1752 Z_SortKeySpecList *sort_sequence,
1753 const char *rank_type)
1755 rset_null_parms parms;
1757 int sort_relation_value;
1758 AttrType sort_relation_type;
1763 Z_AttributeElement *ae;
1767 attr_init (&sort_relation_type, zapt, 7);
1768 sort_relation_value = attr_find (&sort_relation_type, &attributeSet);
1770 attr_init (&use_type, zapt, 1);
1771 use_value = attr_find (&use_type, &attributeSet);
1773 if (!sort_sequence->specs)
1775 sort_sequence->num_specs = 10;
1776 sort_sequence->specs = odr_malloc (stream, sort_sequence->num_specs *
1777 sizeof(*sort_sequence->specs));
1778 for (i = 0; i<sort_sequence->num_specs; i++)
1779 sort_sequence->specs[i] = 0;
1781 if (zapt->term->which != Z_Term_general)
1784 i = atoi_n (zapt->term->u.general->buf, zapt->term->u.general->len);
1785 if (i >= sort_sequence->num_specs)
1788 oe.proto = PROTO_Z3950;
1789 oe.oclass = CLASS_ATTSET;
1790 oe.value = attributeSet;
1791 if (!oid_ent_to_oid (&oe, oid))
1794 sks = odr_malloc (stream, sizeof(*sks));
1795 sks->sortElement = odr_malloc (stream, sizeof(*sks->sortElement));
1796 sks->sortElement->which = Z_SortElement_generic;
1797 sk = sks->sortElement->u.generic = odr_malloc (stream, sizeof(*sk));
1798 sk->which = Z_SortKey_sortAttributes;
1799 sk->u.sortAttributes = odr_malloc (stream, sizeof(*sk->u.sortAttributes));
1801 sk->u.sortAttributes->id = oid;
1802 sk->u.sortAttributes->list =
1803 odr_malloc (stream, sizeof(*sk->u.sortAttributes->list));
1804 sk->u.sortAttributes->list->num_attributes = 1;
1805 sk->u.sortAttributes->list->attributes =
1806 odr_malloc (stream, sizeof(*sk->u.sortAttributes->list->attributes));
1807 ae = *sk->u.sortAttributes->list->attributes =
1808 odr_malloc (stream, sizeof(**sk->u.sortAttributes->list->attributes));
1809 ae->attributeSet = 0;
1810 ae->attributeType = odr_malloc (stream, sizeof(*ae->attributeType));
1811 *ae->attributeType = 1;
1812 ae->which = Z_AttributeValue_numeric;
1813 ae->value.numeric = odr_malloc (stream, sizeof(*ae->value.numeric));
1814 *ae->value.numeric = use_value;
1816 sks->sortRelation = odr_malloc (stream, sizeof(*sks->sortRelation));
1817 if (sort_relation_value == 1)
1818 *sks->sortRelation = Z_SortRelation_ascending;
1819 else if (sort_relation_value == 2)
1820 *sks->sortRelation = Z_SortRelation_descending;
1822 *sks->sortRelation = Z_SortRelation_ascending;
1824 sks->caseSensitivity = odr_malloc (stream, sizeof(*sks->caseSensitivity));
1825 *sks->caseSensitivity = 0;
1827 sks->missingValueAction = 0;
1829 sort_sequence->specs[i] = sks;
1831 parms.rset_term = rset_term_create ("", -1, rank_type);
1832 return rset_create (rset_kind_null, &parms);
1836 static RSET rpn_search_APT (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1837 oid_value attributeSet, ODR stream,
1838 Z_SortKeySpecList *sort_sequence,
1839 int num_bases, char **basenames)
1842 char *search_type = NULL;
1843 char *rank_type = NULL;
1846 char termz[IT_MAX_WORD+1];
1848 zebra_maps_attr (zh->zebra_maps, zapt, ®_id, &search_type,
1849 &rank_type, &complete_flag, &sort_flag);
1851 logf (LOG_DEBUG, "reg_id=%c", reg_id);
1852 logf (LOG_DEBUG, "complete_flag=%d", complete_flag);
1853 logf (LOG_DEBUG, "search_type=%s", search_type);
1854 logf (LOG_DEBUG, "rank_type=%s", rank_type);
1856 if (zapt->term->which != Z_Term_general)
1861 trans_term (zh, zapt, termz);
1864 return rpn_sort_spec (zh, zapt, attributeSet, stream, sort_sequence,
1867 if (!strcmp (search_type, "phrase"))
1869 return rpn_search_APT_phrase (zh, zapt, termz, attributeSet,
1870 reg_id, complete_flag, rank_type,
1871 num_bases, basenames);
1873 else if (!strcmp (search_type, "and-list"))
1875 return rpn_search_APT_and_list (zh, zapt, termz, attributeSet,
1876 reg_id, complete_flag, rank_type,
1877 num_bases, basenames);
1879 else if (!strcmp (search_type, "or-list"))
1881 return rpn_search_APT_or_list (zh, zapt, termz, attributeSet,
1882 reg_id, complete_flag, rank_type,
1883 num_bases, basenames);
1885 else if (!strcmp (search_type, "local"))
1887 return rpn_search_APT_local (zh, zapt, termz, attributeSet,
1890 else if (!strcmp (search_type, "numeric"))
1892 return rpn_search_APT_numeric (zh, zapt, termz, attributeSet,
1893 reg_id, complete_flag, rank_type,
1894 num_bases, basenames);
1900 static RSET rpn_search_structure (ZebraHandle zh, Z_RPNStructure *zs,
1901 oid_value attributeSet, ODR stream,
1902 Z_SortKeySpecList *sort_sequence,
1903 int num_bases, char **basenames)
1906 if (zs->which == Z_RPNStructure_complex)
1908 Z_Operator *zop = zs->u.complex->roperator;
1909 rset_bool_parms bool_parms;
1911 bool_parms.rset_l = rpn_search_structure (zh, zs->u.complex->s1,
1912 attributeSet, stream,
1914 num_bases, basenames);
1915 if (bool_parms.rset_l == NULL)
1917 bool_parms.rset_r = rpn_search_structure (zh, zs->u.complex->s2,
1918 attributeSet, stream,
1920 num_bases, basenames);
1921 if (bool_parms.rset_r == NULL)
1923 rset_delete (bool_parms.rset_l);
1926 bool_parms.key_size = sizeof(struct it_key);
1927 bool_parms.cmp = key_compare_it;
1931 case Z_Operator_and:
1932 r = rset_create (rset_kind_and, &bool_parms);
1935 r = rset_create (rset_kind_or, &bool_parms);
1937 case Z_Operator_and_not:
1938 r = rset_create (rset_kind_not, &bool_parms);
1940 case Z_Operator_prox:
1942 if (zop->u.prox->which != Z_ProximityOperator_known)
1948 if (zop->u.prox->which != Z_ProxCode_known)
1956 if (*zop->u.prox->u.known != Z_ProxUnit_word)
1958 char *val = odr_malloc (stream, 16);
1960 zh->errString = val;
1961 sprintf (val, "%d", *zop->u.prox->u.known);
1965 if (*zop->u.prox->proximityUnitCode != Z_ProxUnit_word)
1967 char *val = odr_malloc (stream, 16);
1969 zh->errString = val;
1970 sprintf (val, "%d", *zop->u.prox->proximityUnitCode);
1974 r = rpn_proximity (zh, bool_parms.rset_l, bool_parms.rset_r,
1975 *zop->u.prox->ordered,
1976 (!zop->u.prox->exclusion ? 0 :
1977 *zop->u.prox->exclusion),
1978 *zop->u.prox->relationType,
1979 *zop->u.prox->distance);
1986 else if (zs->which == Z_RPNStructure_simple)
1988 if (zs->u.simple->which == Z_Operand_APT)
1990 logf (LOG_DEBUG, "rpn_search_APT");
1991 r = rpn_search_APT (zh, zs->u.simple->u.attributesPlusTerm,
1992 attributeSet, stream, sort_sequence,
1993 num_bases, basenames);
1995 else if (zs->u.simple->which == Z_Operand_resultSetId)
1997 logf (LOG_DEBUG, "rpn_search_ref");
1998 r = resultSetRef (zh, zs->u.simple->u.resultSetId);
2000 r = rset_create (rset_kind_null, NULL);
2016 void rpn_search (ZebraHandle zh, ODR stream,
2017 Z_RPNQuery *rpn, int num_bases, char **basenames,
2018 const char *setname)
2022 oid_value attributeSet;
2023 Z_SortKeySpecList *sort_sequence;
2029 zh->errString = NULL;
2032 sort_sequence = odr_malloc (stream, sizeof(*sort_sequence));
2033 sort_sequence->num_specs = 10;
2034 sort_sequence->specs = odr_malloc (stream, sort_sequence->num_specs *
2035 sizeof(*sort_sequence->specs));
2036 for (i = 0; i<sort_sequence->num_specs; i++)
2037 sort_sequence->specs[i] = 0;
2039 attrset = oid_getentbyoid (rpn->attributeSetId);
2040 attributeSet = attrset->value;
2041 rset = rpn_search_structure (zh, rpn->RPNStructure, attributeSet, stream,
2043 num_bases, basenames);
2047 resultSetAdd (zh, setname, 1, rset, &zh->hits);
2049 logf (LOG_DEBUG, "search error: %d", zh->errCode);
2051 for (i = 0; sort_sequence->specs[i]; i++)
2053 sort_sequence->num_specs = i;
2055 resultSetSort (zh, stream, 1, &setname, setname, sort_sequence,
2059 struct scan_info_entry {
2065 struct scan_info_entry *list;
2071 static int scan_handle (char *name, const char *info, int pos, void *client)
2073 int len_prefix, idx;
2074 struct scan_info *scan_info = client;
2076 len_prefix = strlen(scan_info->prefix);
2077 if (memcmp (name, scan_info->prefix, len_prefix))
2080 idx = scan_info->after - pos + scan_info->before;
2083 scan_info->list[idx].term = odr_malloc (scan_info->odr,
2084 strlen(name + len_prefix)+1);
2085 strcpy (scan_info->list[idx].term, name + len_prefix);
2086 assert (*info == sizeof(ISAM_P));
2087 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAM_P));
2091 static void scan_term_untrans (ZebraHandle zh, ODR stream, int reg_type,
2092 char **dst, const char *src)
2094 char term_dst[1024];
2096 term_untrans (zh, reg_type, term_dst, src);
2098 *dst = odr_malloc (stream, strlen(term_dst)+1);
2099 strcpy (*dst, term_dst);
2102 static void count_set (RSET r, int *count)
2110 logf (LOG_DEBUG, "count_set");
2113 rfd = rset_open (r, RSETF_READ);
2114 while (rset_read (r, rfd, &key, &term_index))
2116 if (key.sysno != psysno)
2123 rset_close (r, rfd);
2124 logf (LOG_DEBUG, "%d keys, %d records", kno, *count);
2127 void rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
2128 oid_value attributeset,
2129 int num_bases, char **basenames,
2130 int *position, int *num_entries, ZebraScanEntry **list,
2134 int pos = *position;
2135 int num = *num_entries;
2139 char termz[IT_MAX_WORD+20];
2142 struct scan_info *scan_info_array;
2143 ZebraScanEntry *glist;
2144 int ords[32], ord_no = 0;
2148 char *search_type = NULL;
2149 char *rank_type = NULL;
2153 if (attributeset == VAL_NONE)
2154 attributeset = VAL_BIB1;
2156 zlog_scan (zapt, attributeset);
2157 logf (LOG_DEBUG, "position = %d, num = %d", pos, num);
2159 attr_init (&use, zapt, 1);
2160 use_value = attr_find (&use, &attributeset);
2162 if (zebra_maps_attr (zh->zebra_maps, zapt, ®_id, &search_type,
2163 &rank_type, &complete_flag, &sort_flag))
2169 if (use_value == -1)
2171 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
2175 data1_local_attribute *local_attr;
2177 if ((r=att_getentbyatt (zh, &attp, attributeset, use_value)))
2179 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
2180 attributeset, use_value);
2186 if (zebraExplain_curDatabase (zh->zei, basenames[base_no]))
2188 zh->errString = basenames[base_no];
2189 zh->errCode = 109; /* Database unavailable */
2192 for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
2193 local_attr = local_attr->next)
2197 ord = zebraExplain_lookupSU (zh->zei, attp.attset_ordinal,
2200 ords[ord_no++] = ord;
2208 /* prepare dictionary scanning */
2211 scan_info_array = odr_malloc (stream, ord_no * sizeof(*scan_info_array));
2212 for (i = 0; i < ord_no; i++)
2214 int j, prefix_len = 0;
2215 int before_tmp = before, after_tmp = after;
2216 struct scan_info *scan_info = scan_info_array + i;
2217 struct rpn_char_map_info rcmi;
2219 rpn_char_map_prepare (zh, reg_id, &rcmi);
2221 scan_info->before = before;
2222 scan_info->after = after;
2223 scan_info->odr = stream;
2225 scan_info->list = odr_malloc (stream, (before+after)*
2226 sizeof(*scan_info->list));
2227 for (j = 0; j<before+after; j++)
2228 scan_info->list[j].term = NULL;
2230 prefix_len += key_SU_code (ords[i], termz + prefix_len);
2231 termz[prefix_len++] = reg_id;
2232 termz[prefix_len] = 0;
2233 strcpy (scan_info->prefix, termz);
2235 trans_scan_term (zh, zapt, termz+prefix_len, reg_id);
2237 dict_scan (zh->dict, termz, &before_tmp, &after_tmp, scan_info,
2240 glist = odr_malloc (stream, (before+after)*sizeof(*glist));
2242 /* consider terms after main term */
2243 for (i = 0; i < ord_no; i++)
2247 for (i = 0; i<after; i++)
2250 const char *mterm = NULL;
2254 for (j = 0; j < ord_no; j++)
2256 if (ptr[j] < before+after &&
2257 (tst=scan_info_array[j].list[ptr[j]].term) &&
2258 (!mterm || strcmp (tst, mterm) < 0))
2266 scan_term_untrans (zh, stream, reg_id,
2267 &glist[i+before].term, mterm);
2268 rset = rset_trunc (zh, &scan_info_array[j0].list[ptr[j0]].isam_p, 1,
2269 glist[i+before].term, strlen(glist[i+before].term),
2273 for (j = j0+1; j<ord_no; j++)
2275 if (ptr[j] < before+after &&
2276 (tst=scan_info_array[j].list[ptr[j]].term) &&
2277 !strcmp (tst, mterm))
2279 rset_bool_parms bool_parms;
2283 rset_trunc (zh, &scan_info_array[j].list[ptr[j]].isam_p, 1,
2284 glist[i+before].term,
2285 strlen(glist[i+before].term), NULL);
2287 bool_parms.key_size = sizeof(struct it_key);
2288 bool_parms.cmp = key_compare_it;
2289 bool_parms.rset_l = rset;
2290 bool_parms.rset_r = rset2;
2292 rset = rset_create (rset_kind_or, &bool_parms);
2297 count_set (rset, &glist[i+before].occurrences);
2302 *num_entries -= (after-i);
2306 /* consider terms before main term */
2307 for (i = 0; i<ord_no; i++)
2310 for (i = 0; i<before; i++)
2313 const char *mterm = NULL;
2317 for (j = 0; j <ord_no; j++)
2319 if (ptr[j] < before &&
2320 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2321 (!mterm || strcmp (tst, mterm) > 0))
2330 scan_term_untrans (zh, stream, reg_id,
2331 &glist[before-1-i].term, mterm);
2334 (zh, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1,
2335 glist[before-1-i].term, strlen(glist[before-1-i].term),
2340 for (j = j0+1; j<ord_no; j++)
2342 if (ptr[j] < before &&
2343 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2344 !strcmp (tst, mterm))
2346 rset_bool_parms bool_parms;
2349 rset2 = rset_trunc (zh,
2350 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1,
2351 glist[before-1-i].term,
2352 strlen(glist[before-1-i].term), NULL);
2354 bool_parms.key_size = sizeof(struct it_key);
2355 bool_parms.cmp = key_compare_it;
2356 bool_parms.rset_l = rset;
2357 bool_parms.rset_r = rset2;
2359 rset = rset_create (rset_kind_or, &bool_parms);
2364 count_set (rset, &glist[before-1-i].occurrences);
2374 *list = glist + i; /* list is set to first 'real' entry */
2376 logf (LOG_DEBUG, "position = %d, num_entries = %d",
2377 *position, *num_entries);
2379 logf (LOG_DEBUG, "scan error: %d", zh->errCode);