2 * Copyright (C) 1995-1998, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.85 1998-09-22 10:03:43 adam
8 * Changed result sets to be persistent in the sense that they can
9 * be re-searched if needed.
10 * Fixed memory leak in rsm_or.
12 * Revision 1.84 1998/09/18 12:41:00 adam
13 * Fixed bug with numerical relations.
15 * Revision 1.83 1998/09/02 13:53:19 adam
16 * Extra parameter decode added to search routines to implement
19 * Revision 1.82 1998/06/26 11:16:40 quinn
20 * Added support (un-optimised) for left and left/right truncation
22 * Revision 1.81 1998/06/24 12:16:14 adam
23 * Support for relations on text operands. Open range support in
24 * DFA module (i.e. [-j], [g-]).
26 * Revision 1.80 1998/06/23 15:33:34 adam
27 * Added feature to specify sort criteria in query (type 7 specifies
30 * Revision 1.79 1998/06/22 11:35:09 adam
33 * Revision 1.78 1998/06/08 14:43:17 adam
34 * Added suport for EXPLAIN Proxy servers - added settings databasePath
35 * and explainDatabase to facilitate this. Increased maximum number
36 * of databases and attributes in one register.
38 * Revision 1.77 1998/05/20 10:12:22 adam
39 * Implemented automatic EXPLAIN database maintenance.
40 * Modified Zebra to work with ASN.1 compiled version of YAZ.
42 * Revision 1.76 1998/04/02 14:35:29 adam
43 * First version of Zebra that works with compiled ASN.1.
45 * Revision 1.75 1998/03/05 08:45:13 adam
46 * New result set model and modular ranking system. Moved towards
47 * descent server API. System information stored as "SGML" records.
49 * Revision 1.74 1998/02/10 12:03:06 adam
52 * Revision 1.73 1998/01/29 13:40:11 adam
53 * Better logging for scan service.
55 * Revision 1.72 1998/01/07 13:53:41 adam
56 * Queries using simple ranked operands returns right number of hits.
58 * Revision 1.71 1997/12/18 10:54:24 adam
59 * New method result set method rs_hits that returns the number of
60 * hits in result-set (if known). The ranked result set returns real
61 * number of hits but only when not combined with other operands.
63 * Revision 1.70 1997/10/31 12:34:43 adam
64 * Changed a few log statements.
66 * Revision 1.69 1997/10/29 12:05:02 adam
67 * Server produces diagnostic "Unsupported Attribute Set" when appropriate.
69 * Revision 1.68 1997/10/27 14:33:06 adam
70 * Moved towards generic character mapping depending on "structure"
71 * field in abstract syntax file. Fixed a few memory leaks. Fixed
72 * bug with negative integers when doing searches with relational
75 * Revision 1.67 1997/09/29 09:06:10 adam
76 * Removed one static var in order to make this module thread safe.
78 * Revision 1.66 1997/09/25 14:58:03 adam
81 * Revision 1.65 1997/09/22 12:39:06 adam
82 * Added get_pos method for the ranked result sets.
84 * Revision 1.64 1997/09/18 08:59:20 adam
85 * Extra generic handle for the character mapping routines.
87 * Revision 1.63 1997/09/17 12:19:18 adam
88 * Zebra version corresponds to YAZ version 1.4.
89 * Changed Zebra server so that it doesn't depend on global common_resource.
91 * Revision 1.62 1997/09/05 15:30:09 adam
92 * Changed prototype for chr_map_input - added const.
93 * Added support for C++, headers uses extern "C" for public definitions.
95 * Revision 1.61 1997/02/10 10:21:14 adam
96 * Bug fix: in search terms character (^) wasn't observed.
98 * Revision 1.60 1997/01/31 11:10:34 adam
99 * Bug fix: Leading and trailing white space weren't removed in scan tokens.
101 * Revision 1.59 1997/01/17 11:31:46 adam
102 * Bug fix: complete phrase search didn't work.
104 * Revision 1.58 1996/12/23 15:30:45 adam
105 * Work on truncation.
106 * Bug fix: result sets weren't deleted after server shut down.
108 * Revision 1.57 1996/11/11 13:38:02 adam
109 * Added proximity support in search.
111 * Revision 1.56 1996/11/08 11:10:32 adam
112 * Buffers used during file match got bigger.
113 * Compressed ISAM support everywhere.
114 * Bug fixes regarding masking characters in queries.
115 * Redesigned Regexp-2 queries.
117 * Revision 1.55 1996/11/04 14:07:44 adam
118 * Moved truncation code to trunc.c.
120 * Revision 1.54 1996/10/29 14:09:52 adam
121 * Use of cisam system - enabled if setting isamc is 1.
123 * Revision 1.53 1996/06/26 09:21:43 adam
124 * Bug fix: local attribute set wasn't obeyed in scan.
126 * Revision 1.52 1996/06/17 14:26:20 adam
127 * Function gen_regular_rel changed to handle negative numbers.
129 * Revision 1.51 1996/06/11 10:54:15 quinn
132 * Revision 1.50 1996/06/07 08:51:53 adam
133 * Bug fix: Character mapping was broken (introducued by last revision).
135 * Revision 1.49 1996/06/04 10:18:11 adam
136 * Search/scan uses character mapping module.
138 * Revision 1.48 1996/05/28 15:15:01 adam
139 * Bug fix: Didn't handle unknown database correctly.
141 * Revision 1.47 1996/05/15 18:36:28 adam
142 * Function trans_term transforms unsearchable characters to blanks.
144 * Revision 1.46 1996/05/15 11:57:56 adam
145 * Fixed bug introduced by set/field mapping in search operations.
147 * Revision 1.45 1996/05/14 11:34:00 adam
148 * Scan support in multiple registers/databases.
150 * Revision 1.44 1996/05/14 06:16:44 adam
151 * Compact use/set bytes used in search service.
153 * Revision 1.43 1996/05/09 09:54:43 adam
154 * Server supports maps from one logical attributes to a list of physical
156 * The extraction process doesn't make space consuming 'any' keys.
158 * Revision 1.42 1996/05/09 07:28:56 quinn
159 * Work towards phrases and multiple registers
161 * Revision 1.41 1996/03/20 09:36:43 adam
162 * Function dict_lookup_grep got extra parameter, init_pos, which marks
163 * from which position in pattern approximate pattern matching should occur.
164 * Approximate pattern matching is used in relevance=re-2.
166 * Revision 1.40 1996/02/02 13:44:44 adam
167 * The public dictionary functions simply use char instead of Dict_char
168 * to represent search strings. Dict_char is used internally only.
170 * Revision 1.39 1996/01/03 16:22:13 quinn
171 * operator->roperator
173 * Revision 1.38 1995/12/11 09:12:55 adam
174 * The rec_get function returns NULL if record doesn't exist - will
175 * happen in the server if the result set records have been deleted since
176 * the creation of the set (i.e. the search).
177 * The server saves a result temporarily if it is 'volatile', i.e. the
178 * set is register dependent.
180 * Revision 1.37 1995/12/06 15:05:28 adam
181 * More verbose in count_set.
183 * Revision 1.36 1995/12/06 12:41:27 adam
184 * New command 'stat' for the index program.
185 * Filenames can be read from stdin by specifying '-'.
186 * Bug fix/enhancement of the transformation from terms to regular
187 * expressons in the search engine.
189 * Revision 1.35 1995/11/27 09:29:00 adam
190 * Bug fixes regarding conversion to regular expressions.
192 * Revision 1.34 1995/11/16 17:00:56 adam
193 * Better logging of rpn query.
195 * Revision 1.33 1995/11/01 13:58:28 quinn
196 * Moving data1 to yaz/retrieval
198 * Revision 1.32 1995/10/27 14:00:11 adam
199 * Implemented detection of database availability.
201 * Revision 1.31 1995/10/17 18:02:10 adam
202 * New feature: databases. Implemented as prefix to words in dictionary.
204 * Revision 1.30 1995/10/16 09:32:38 adam
205 * More work on relational op.
207 * Revision 1.29 1995/10/13 16:01:49 adam
210 * Revision 1.28 1995/10/13 12:26:43 adam
211 * Optimization of truncation.
213 * Revision 1.27 1995/10/12 17:07:22 adam
216 * Revision 1.26 1995/10/12 12:40:54 adam
217 * Bug fixes in rpn_prox.
219 * Revision 1.25 1995/10/10 13:59:24 adam
220 * Function rset_open changed its wflag parameter to general flags.
222 * Revision 1.24 1995/10/09 16:18:37 adam
223 * Function dict_lookup_grep got extra client data parameter.
225 * Revision 1.23 1995/10/06 16:33:37 adam
226 * Use attribute mappings.
228 * Revision 1.22 1995/10/06 15:07:39 adam
229 * Structure 'local-number' handled.
231 * Revision 1.21 1995/10/06 13:52:06 adam
232 * Bug fixes. Handler may abort further scanning.
234 * Revision 1.20 1995/10/06 11:06:33 adam
235 * Scan entries include 'occurrences' now.
237 * Revision 1.19 1995/10/06 10:43:56 adam
238 * Scan added. 'occurrences' in scan entries not set yet.
240 * Revision 1.18 1995/10/04 16:57:20 adam
241 * Key input and merge sort in one pass.
243 * Revision 1.17 1995/10/04 12:55:17 adam
244 * Bug fix in ranked search. Use=Any keys inserted.
246 * Revision 1.16 1995/10/02 16:24:40 adam
247 * Use attribute actually used in search requests.
249 * Revision 1.15 1995/10/02 15:18:52 adam
250 * New member in recRetrieveCtrl: diagnostic.
252 * Revision 1.14 1995/09/28 12:10:32 adam
253 * Bug fixes. Field prefix used in queries.
255 * Revision 1.13 1995/09/18 14:17:50 adam
258 * Revision 1.12 1995/09/15 14:45:21 adam
260 * Work on truncation.
262 * Revision 1.11 1995/09/14 11:53:27 adam
263 * First work on regular expressions/truncations.
265 * Revision 1.10 1995/09/11 15:23:26 adam
266 * More work on relevance search.
268 * Revision 1.9 1995/09/11 13:09:35 adam
269 * More work on relevance feedback.
271 * Revision 1.8 1995/09/08 14:52:27 adam
272 * Minor changes. Dictionary is lower case now.
274 * Revision 1.7 1995/09/07 13:58:36 adam
275 * New parameter: result-set file descriptor (RSFD) to support multiple
276 * positions within the same result-set.
277 * Boolean operators: and, or, not implemented.
278 * Result-set references.
280 * Revision 1.6 1995/09/06 16:11:18 adam
281 * Option: only one word key per file.
283 * Revision 1.5 1995/09/06 10:33:04 adam
284 * More work on present. Some log messages removed.
286 * Revision 1.4 1995/09/05 15:28:40 adam
287 * More work on search engine.
289 * Revision 1.3 1995/09/04 15:20:22 adam
292 * Revision 1.2 1995/09/04 12:33:43 adam
293 * Various cleanup. YAZ util used instead.
295 * Revision 1.1 1995/09/04 09:10:40 adam
296 * More work on index add/del/update.
297 * Merge sort implemented.
298 * Initial work on z39 server.
317 struct rpn_char_map_info {
322 static const char **rpn_char_map_handler (void *vp, const char **from, int len)
324 struct rpn_char_map_info *p = vp;
325 return zebra_maps_input (p->zm, p->reg_type, from, len);
328 static void rpn_char_map_prepare (ZebraHandle zh, int reg_type,
329 struct rpn_char_map_info *map_info)
331 map_info->zm = zh->zebra_maps;
332 map_info->reg_type = reg_type;
333 dict_grep_cmap (zh->dict, map_info, rpn_char_map_handler);
340 Z_AttributesPlusTerm *zapt;
343 static int attr_find (AttrType *src, oid_value *attributeSetP)
348 num_attributes = src->zapt->attributes->num_attributes;
350 num_attributes = src->zapt->num_attributes;
352 while (src->major < num_attributes)
354 Z_AttributeElement *element;
357 element = src->zapt->attributes->attributes[src->major];
359 element = src->zapt->attributeList[src->major];
361 if (src->type == *element->attributeType)
363 switch (element->which)
365 case Z_AttributeValue_numeric:
367 if (element->attributeSet && attributeSetP)
371 attrset = oid_getentbyoid (element->attributeSet);
372 *attributeSetP = attrset->value;
374 return *element->value.numeric;
376 case Z_AttributeValue_complex:
377 if (src->minor >= element->value.complex->num_list ||
378 element->value.complex->list[src->minor]->which !=
379 Z_StringOrNumeric_numeric)
382 if (element->attributeSet && attributeSetP)
386 attrset = oid_getentbyoid (element->attributeSet);
387 *attributeSetP = attrset->value;
389 return *element->value.complex->list[src->minor-1]->u.numeric;
399 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
421 static void term_untrans (ZebraHandle zh, int reg_type,
422 char *dst, const char *src)
426 const char *cp = zebra_maps_output (zh->zebra_maps, reg_type, &src);
433 static void add_isam_p (const char *name, const char *info,
436 if (p->isam_p_indx == p->isam_p_size)
438 ISAM_P *new_isam_p_buf;
442 p->isam_p_size = 2*p->isam_p_size + 100;
443 new_isam_p_buf = xmalloc (sizeof(*new_isam_p_buf) *
447 memcpy (new_isam_p_buf, p->isam_p_buf,
448 p->isam_p_indx * sizeof(*p->isam_p_buf));
449 xfree (p->isam_p_buf);
451 p->isam_p_buf = new_isam_p_buf;
454 new_term_no = xmalloc (sizeof(*new_term_no) *
458 memcpy (new_term_no, p->isam_p_buf,
459 p->isam_p_indx * sizeof(*p->term_no));
462 p->term_no = new_term_no;
465 assert (*info == sizeof(*p->isam_p_buf));
466 memcpy (p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
469 term_untrans (p->zh, p->reg_type, term_tmp, name+2);
470 logf (LOG_DEBUG, "grep: %s", term_tmp);
475 static int grep_handle (char *name, const char *info, void *p)
477 add_isam_p (name, info, p);
481 static int term_pre (ZebraMaps zebra_maps, int reg_type, const char **src,
482 const char *ct1, const char *ct2)
484 const char *s1, *s0 = *src;
487 /* skip white space */
490 if (ct1 && strchr (ct1, *s0))
492 if (ct2 && strchr (ct2, *s0))
495 map = zebra_maps_input (zebra_maps, reg_type, &s1, strlen(s1));
496 if (**map != *CHR_SPACE)
504 static int term_100 (ZebraMaps zebra_maps, int reg_type,
505 const char **src, char *dst, int space_split,
513 if (!term_pre (zebra_maps, reg_type, src, NULL, NULL))
519 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
520 if (space_split && **map == *CHR_SPACE)
524 if (!isalnum (*s1) && *s1 != '-')
536 static int term_101 (ZebraMaps zebra_maps, int reg_type,
537 const char **src, char *dst, int space_split,
545 if (!term_pre (zebra_maps, reg_type, src, "#", "#"))
554 dst_term[j++] = *s0++;
559 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
560 if (space_split && **map == *CHR_SPACE)
572 dst_term[j++] = '\0';
578 static int term_103 (ZebraMaps zebra_maps, int reg_type, const char **src,
579 char *dst, int *errors, int space_split,
587 if (!term_pre (zebra_maps, reg_type, src, "^\\()[].*+?|", "("))
590 if (errors && *s0 == '+' && s0[1] && s0[2] == '+' && s0[3] &&
593 *errors = s0[1] - '0';
600 if (strchr ("^\\()[].*+?|-", *s0))
608 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
609 if (**map == *CHR_SPACE)
626 static int term_102 (ZebraMaps zebra_maps, int reg_type, const char **src,
627 char *dst, int space_split, char *dst_term)
629 return term_103 (zebra_maps, reg_type, src, dst, NULL, space_split,
633 /* gen_regular_rel - generate regular expression from relation
634 * val: border value (inclusive)
635 * islt: 1 if <=; 0 if >=.
637 static void gen_regular_rel (char *dst, int val, int islt)
644 logf (LOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
648 strcpy (dst, "(-[0-9]+|(");
656 strcpy (dst, "([0-9]+|-(");
657 dst_p = strlen (dst);
667 dst_p = strlen (dst);
668 sprintf (numstr, "%d", val);
669 for (w = strlen(numstr); --w >= 0; pos++)
688 strcpy (dst + dst_p, numstr);
689 dst_p = strlen(dst) - pos - 1;
717 for (i = 0; i<pos; i++)
730 for (i=1; i<pos; i++)
731 strcat (dst, "[0-9]?");
735 for (i = 0; i <= pos; i++)
736 strcat (dst, "[0-9]");
737 strcat (dst, "[0-9]*");
742 void string_rel_add_char (char **term_p, const char *src, int *indx)
744 if (src[*indx] == '\\')
745 *(*term_p)++ = src[(*indx)++];
746 *(*term_p)++ = src[(*indx)++];
750 * > abc ([b-].*|a[c-].*|ab[d-].*|abc.+)
751 * ([^-a].*|a[^-b].*ab[^-c].*|abc.+)
752 * >= abc ([b-].*|a[c-].*|ab[c-].*)
753 * ([^-a].*|a[^-b].*|ab[c-].*)
754 * < abc ([-0].*|a[-a].*|ab[-b].*)
755 * ([^a-].*|a[^b-].*|ab[^c-].*)
756 * <= abc ([-0].*|a[-a].*|ab[-b].*|abc)
757 * ([^a-].*|a[^b-].*|ab[^c-].*|abc)
759 static int string_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
760 const char **term_sub, char *term_dict,
761 oid_value attributeSet,
762 int reg_type, int space_split, char *term_dst)
767 char *term_tmp = term_dict + strlen(term_dict);
768 char term_component[256];
770 attr_init (&relation, zapt, 2);
771 relation_value = attr_find (&relation, NULL);
773 logf (LOG_DEBUG, "string relation value=%d", relation_value);
774 switch (relation_value)
777 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
778 space_split, term_dst))
780 logf (LOG_DEBUG, "Relation <");
783 for (i = 0; term_component[i]; )
790 string_rel_add_char (&term_tmp, term_component, &j);
795 string_rel_add_char (&term_tmp, term_component, &i);
806 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
807 space_split, term_dst))
809 logf (LOG_DEBUG, "Relation <=");
812 for (i = 0; term_component[i]; )
817 string_rel_add_char (&term_tmp, term_component, &j);
821 string_rel_add_char (&term_tmp, term_component, &i);
830 for (i = 0; term_component[i]; )
831 string_rel_add_char (&term_tmp, term_component, &i);
836 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
837 space_split, term_dst))
839 logf (LOG_DEBUG, "Relation >");
842 for (i = 0; term_component[i];)
847 string_rel_add_char (&term_tmp, term_component, &j);
852 string_rel_add_char (&term_tmp, term_component, &i);
860 for (i = 0; term_component[i];)
861 string_rel_add_char (&term_tmp, term_component, &i);
868 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
869 space_split, term_dst))
871 logf (LOG_DEBUG, "Relation >=");
874 for (i = 0; term_component[i];)
881 string_rel_add_char (&term_tmp, term_component, &j);
884 if (term_component[i+1])
888 string_rel_add_char (&term_tmp, term_component, &i);
892 string_rel_add_char (&term_tmp, term_component, &i);
904 logf (LOG_DEBUG, "Relation =");
905 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
906 space_split, term_dst))
908 strcat (term_tmp, "(");
909 strcat (term_tmp, term_component);
910 strcat (term_tmp, ")");
915 static int string_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
916 const char **term_sub,
917 oid_value attributeSet, struct grep_info *grep_info,
918 int reg_type, int complete_flag,
919 int num_bases, char **basenames,
922 char term_dict[2*IT_MAX_WORD+4000];
925 int truncation_value;
928 oid_value curAttributeSet = attributeSet;
930 struct rpn_char_map_info rcmi;
931 int space_split = complete_flag ? 0 : 1;
933 rpn_char_map_prepare (zh, reg_type, &rcmi);
934 attr_init (&use, zapt, 1);
935 use_value = attr_find (&use, &curAttributeSet);
936 logf (LOG_DEBUG, "string_term, use value %d", use_value);
937 attr_init (&truncation, zapt, 5);
938 truncation_value = attr_find (&truncation, NULL);
939 logf (LOG_DEBUG, "truncation value %d", truncation_value);
944 for (base_no = 0; base_no < num_bases; base_no++)
947 data1_local_attribute *local_attr;
948 int max_pos, prefix_len = 0;
951 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
953 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
954 curAttributeSet, use_value, r);
961 if (zebraExplain_curDatabase (zh->zei, basenames[base_no]))
963 zh->errCode = 109; /* Database unavailable */
964 zh->errString = basenames[base_no];
967 for (local_attr = attp.local_attributes; local_attr;
968 local_attr = local_attr->next)
974 ord = zebraExplain_lookupSU (zh->zei, attp.attset_ordinal,
979 term_dict[prefix_len++] = '|';
981 term_dict[prefix_len++] = '(';
983 ord_len = key_SU_code (ord, ord_buf);
984 for (i = 0; i<ord_len; i++)
986 term_dict[prefix_len++] = 1;
987 term_dict[prefix_len++] = ord_buf[i];
995 term_dict[prefix_len++] = ')';
996 term_dict[prefix_len++] = 1;
997 term_dict[prefix_len++] = reg_type;
998 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
999 term_dict[prefix_len] = '\0';
1001 switch (truncation_value)
1003 case -1: /* not specified */
1004 case 100: /* do not truncate */
1005 if (!string_relation (zh, zapt, &termp, term_dict,
1007 reg_type, space_split, term_dst))
1009 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_dict+prefix_len);
1010 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info, &max_pos,
1013 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1015 case 1: /* right truncation */
1016 term_dict[j++] = '(';
1017 if (!term_100 (zh->zebra_maps, reg_type,
1018 &termp, term_dict + j, space_split, term_dst))
1020 strcat (term_dict, ".*)");
1021 dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1022 &max_pos, 0, grep_handle);
1024 case 2: /* keft truncation */
1025 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1026 if (!term_100 (zh->zebra_maps, reg_type,
1027 &termp, term_dict + j, space_split, term_dst))
1029 strcat (term_dict, ")");
1030 dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1031 &max_pos, 0, grep_handle);
1033 case 3: /* left&right truncation */
1034 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1035 if (!term_100 (zh->zebra_maps, reg_type,
1036 &termp, term_dict + j, space_split, term_dst))
1038 strcat (term_dict, ".*)");
1039 dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1040 &max_pos, 0, grep_handle);
1044 case 101: /* process # in term */
1045 term_dict[j++] = '(';
1046 if (!term_101 (zh->zebra_maps, reg_type,
1047 &termp, term_dict + j, space_split, term_dst))
1049 strcat (term_dict, ")");
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=#: %d", r);
1055 case 102: /* Regexp-1 */
1056 term_dict[j++] = '(';
1057 if (!term_102 (zh->zebra_maps, reg_type,
1058 &termp, term_dict + j, space_split, term_dst))
1060 strcat (term_dict, ")");
1061 logf (LOG_DEBUG, "Regexp-1 tolerance=%d", r);
1062 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1063 &max_pos, 0, grep_handle);
1065 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
1068 case 103: /* Regexp-2 */
1070 term_dict[j++] = '(';
1071 if (!term_103 (zh->zebra_maps, reg_type,
1072 &termp, term_dict + j, &r, space_split, term_dst))
1074 strcat (term_dict, ")");
1075 logf (LOG_DEBUG, "Regexp-2 tolerance=%d", r);
1076 r = dict_lookup_grep (zh->dict, term_dict, r, grep_info,
1077 &max_pos, 2, grep_handle);
1079 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
1085 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1089 static void trans_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1093 Z_Term *term = zapt->term;
1095 sizez = term->u.general->len;
1096 if (sizez > IT_MAX_WORD-1)
1097 sizez = IT_MAX_WORD-1;
1098 memcpy (termz, term->u.general->buf, sizez);
1099 termz[sizez] = '\0';
1102 static void trans_scan_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1103 char *termz, int reg_type)
1105 Z_Term *term = zapt->term;
1107 const char *cp = (const char *) term->u.general->buf;
1108 const char *cp_end = cp + term->u.general->len;
1111 const char *space_map = NULL;
1114 while ((len = (cp_end - cp)) > 0)
1116 map = zebra_maps_input (zh->zebra_maps, reg_type, &cp, len);
1117 if (**map == *CHR_SPACE)
1122 for (src = space_map; *src; src++)
1125 for (src = *map; *src; src++)
1132 static RSET rpn_proximity (ZebraHandle zh, RSET rset1, RSET rset2,
1134 int exclusion, int relation, int distance)
1139 struct it_key buf1, buf2;
1142 rset_temp_parms parms;
1145 rsfd1 = rset_open (rset1, RSETF_READ);
1146 more1 = rset_read (rset1, rsfd1, &buf1, &term_index);
1148 rsfd2 = rset_open (rset2, RSETF_READ);
1149 more2 = rset_read (rset2, rsfd2, &buf2, &term_index);
1151 parms.key_size = sizeof (struct it_key);
1152 parms.temp_path = res_get (zh->res, "setTmpDir");
1153 result = rset_create (rset_kind_temp, &parms);
1154 rsfd_result = rset_open (result, RSETF_WRITE);
1156 logf (LOG_DEBUG, "rpn_proximity excl=%d ord=%d rel=%d dis=%d",
1157 exclusion, ordered, relation, distance);
1158 while (more1 && more2)
1160 int cmp = key_compare_it (&buf1, &buf2);
1162 more1 = rset_read (rset1, rsfd1, &buf1, &term_index);
1164 more2 = rset_read (rset2, rsfd2, &buf2, &term_index);
1167 int sysno = buf1.sysno;
1171 seqno[n++] = buf1.seqno;
1172 while ((more1 = rset_read (rset1, rsfd1, &buf1, &term_index)) &&
1173 sysno == buf1.sysno)
1175 seqno[n++] = buf1.seqno;
1178 for (i = 0; i<n; i++)
1180 int diff = buf2.seqno - seqno[i];
1181 int excl = exclusion;
1182 if (!ordered && diff < 0)
1187 if (diff < distance)
1191 if (diff <= distance)
1195 if (diff == distance)
1199 if (diff >= distance)
1203 if (diff > distance)
1207 if (diff != distance)
1212 rset_write (result, rsfd_result, &buf2);
1214 } while ((more2 = rset_read (rset2, rsfd2, &buf2, &term_index)) &&
1215 sysno == buf2.sysno);
1218 rset_close (result, rsfd_result);
1219 rset_close (rset1, rsfd1);
1220 rset_close (rset2, rsfd2);
1224 static RSET rpn_prox (ZebraHandle zh, RSET *rset, int rset_no)
1229 struct it_key **buf;
1231 char prox_term[1024];
1232 int length_prox_term = 0;
1233 int min_nn = 10000000;
1235 const char *flags = NULL;
1237 rsfd = xmalloc (sizeof(*rsfd)*rset_no);
1238 more = xmalloc (sizeof(*more)*rset_no);
1239 buf = xmalloc (sizeof(*buf)*rset_no);
1241 for (i = 0; i<rset_no; i++)
1244 buf[i] = xmalloc (sizeof(**buf));
1245 rsfd[i] = rset_open (rset[i], RSETF_READ);
1246 if (!(more[i] = rset_read (rset[i], rsfd[i], buf[i], &term_index)))
1248 for (j = 0; j<rset[i]->no_rset_terms; j++)
1250 const char *nflags = rset[i]->rset_terms[j]->flags;
1251 char *term = rset[i]->rset_terms[j]->name;
1252 int lterm = strlen(term);
1253 if (length_prox_term)
1254 prox_term[length_prox_term++] = ' ';
1255 strcpy (prox_term + length_prox_term, term);
1256 length_prox_term += lterm;
1257 if (min_nn > rset[i]->rset_terms[j]->nn)
1258 min_nn = rset[i]->rset_terms[j]->nn;
1264 rset_null_parms parms;
1268 rset_close (rset[i], rsfd[i]);
1272 parms.rset_term = rset_term_create (prox_term, -1, flags);
1273 parms.rset_term->nn = 0;
1274 result = rset_create (rset_kind_null, &parms);
1278 rset_temp_parms parms;
1281 parms.rset_term = rset_term_create (prox_term, -1, flags);
1282 parms.rset_term->nn = min_nn;
1283 parms.key_size = sizeof (struct it_key);
1284 parms.temp_path = res_get (zh->res, "setTmpDir");
1285 result = rset_create (rset_kind_temp, &parms);
1286 rsfd_result = rset_open (result, RSETF_WRITE);
1290 for (i = 1; i<rset_no; i++)
1299 cmp = key_compare_it (buf[i], buf[i-1]);
1302 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1303 buf[i-1], &term_index);
1308 if (buf[i-1]->seqno+1 != buf[i]->seqno)
1310 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1311 buf[i-1], &term_index);
1317 more[i] = rset_read (rset[i], rsfd[i], buf[i],
1324 rset_write (result, rsfd_result, buf[0]);
1325 more[0] = rset_read (*rset, *rsfd, *buf, &term_index);
1329 for (i = 0; i<rset_no; i++)
1331 rset_close (rset[i], rsfd[i]);
1334 rset_close (result, rsfd_result);
1342 static RSET rpn_search_APT_phrase (ZebraHandle zh,
1343 Z_AttributesPlusTerm *zapt,
1345 oid_value attributeSet,
1346 int reg_type, int complete_flag,
1347 const char *rank_type,
1348 int num_bases, char **basenames)
1350 char term_dst[IT_MAX_WORD+1];
1351 const char *termp = termz;
1352 RSET rset[60], result;
1353 int i, r, rset_no = 0;
1354 struct grep_info grep_info;
1357 grep_info.term_no = 0;
1359 grep_info.isam_p_size = 0;
1360 grep_info.isam_p_buf = NULL;
1362 grep_info.reg_type = reg_type;
1366 logf (LOG_DEBUG, "APT_phrase termp=%s", termp);
1367 grep_info.isam_p_indx = 0;
1368 r = string_term (zh, zapt, &termp, attributeSet, &grep_info,
1369 reg_type, complete_flag, num_bases, basenames,
1373 logf (LOG_DEBUG, "term: %s", term_dst);
1374 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1375 grep_info.isam_p_indx, term_dst,
1376 strlen(term_dst), rank_type);
1377 assert (rset[rset_no]);
1378 if (++rset_no >= sizeof(rset)/sizeof(*rset))
1382 xfree(grep_info.term_no);
1384 xfree (grep_info.isam_p_buf);
1387 rset_null_parms parms;
1389 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1390 return rset_create (rset_kind_null, &parms);
1392 else if (rset_no == 1)
1394 result = rpn_prox (zh, rset, rset_no);
1395 for (i = 0; i<rset_no; i++)
1396 rset_delete (rset[i]);
1400 static RSET rpn_search_APT_or_list (ZebraHandle zh,
1401 Z_AttributesPlusTerm *zapt,
1403 oid_value attributeSet,
1404 int reg_type, int complete_flag,
1405 const char *rank_type,
1406 int num_bases, char **basenames)
1408 char term_dst[IT_MAX_WORD+1];
1409 const char *termp = termz;
1410 RSET rset[60], result;
1411 int i, r, rset_no = 0;
1412 struct grep_info grep_info;
1415 grep_info.term_no = 0;
1417 grep_info.isam_p_size = 0;
1418 grep_info.isam_p_buf = NULL;
1420 grep_info.reg_type = reg_type;
1424 logf (LOG_DEBUG, "APT_or_list termp=%s", termp);
1425 grep_info.isam_p_indx = 0;
1426 r = string_term (zh, zapt, &termp, attributeSet, &grep_info,
1427 reg_type, complete_flag, num_bases, basenames,
1431 logf (LOG_DEBUG, "term: %s", term_dst);
1432 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1433 grep_info.isam_p_indx, term_dst,
1434 strlen(term_dst), rank_type);
1435 assert (rset[rset_no]);
1436 if (++rset_no >= sizeof(rset)/sizeof(*rset))
1440 xfree(grep_info.term_no);
1442 xfree (grep_info.isam_p_buf);
1445 rset_null_parms parms;
1447 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1448 return rset_create (rset_kind_null, &parms);
1451 for (i = 1; i<rset_no; i++)
1453 rset_bool_parms bool_parms;
1455 bool_parms.rset_l = result;
1456 bool_parms.rset_r = rset[i];
1457 bool_parms.key_size = sizeof(struct it_key);
1458 bool_parms.cmp = key_compare_it;
1459 result = rset_create (rset_kind_or, &bool_parms);
1464 static RSET rpn_search_APT_and_list (ZebraHandle zh,
1465 Z_AttributesPlusTerm *zapt,
1467 oid_value attributeSet,
1468 int reg_type, int complete_flag,
1469 const char *rank_type,
1470 int num_bases, char **basenames)
1472 char term_dst[IT_MAX_WORD+1];
1473 const char *termp = termz;
1474 RSET rset[60], result;
1475 int i, r, rset_no = 0;
1476 struct grep_info grep_info;
1479 grep_info.term_no = 0;
1481 grep_info.isam_p_size = 0;
1482 grep_info.isam_p_buf = NULL;
1484 grep_info.reg_type = reg_type;
1488 logf (LOG_DEBUG, "APT_and_list termp=%s", termp);
1489 grep_info.isam_p_indx = 0;
1490 r = string_term (zh, zapt, &termp, attributeSet, &grep_info,
1491 reg_type, complete_flag, num_bases, basenames,
1495 logf (LOG_DEBUG, "term: %s", term_dst);
1496 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1497 grep_info.isam_p_indx, term_dst,
1498 strlen(term_dst), rank_type);
1499 assert (rset[rset_no]);
1500 if (++rset_no >= sizeof(rset)/sizeof(*rset))
1504 xfree(grep_info.term_no);
1506 xfree (grep_info.isam_p_buf);
1509 rset_null_parms parms;
1511 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1512 return rset_create (rset_kind_null, &parms);
1515 for (i = 1; i<rset_no; i++)
1517 rset_bool_parms bool_parms;
1519 bool_parms.rset_l = result;
1520 bool_parms.rset_r = rset[i];
1521 bool_parms.key_size = sizeof(struct it_key);
1522 bool_parms.cmp = key_compare_it;
1523 result = rset_create (rset_kind_and, &bool_parms);
1528 static int numeric_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1529 const char **term_sub,
1531 oid_value attributeSet,
1532 struct grep_info *grep_info,
1541 char *term_tmp = term_dict + strlen(term_dict);
1543 attr_init (&relation, zapt, 2);
1544 relation_value = attr_find (&relation, NULL);
1546 logf (LOG_DEBUG, "numeric relation value=%d", relation_value);
1548 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_tmp, 1,
1551 term_value = atoi (term_tmp);
1552 switch (relation_value)
1555 logf (LOG_DEBUG, "Relation <");
1556 gen_regular_rel (term_tmp, term_value-1, 1);
1559 logf (LOG_DEBUG, "Relation <=");
1560 gen_regular_rel (term_tmp, term_value, 1);
1563 logf (LOG_DEBUG, "Relation >=");
1564 gen_regular_rel (term_tmp, term_value, 0);
1567 logf (LOG_DEBUG, "Relation >");
1568 gen_regular_rel (term_tmp, term_value+1, 0);
1572 logf (LOG_DEBUG, "Relation =");
1573 sprintf (term_tmp, "(0*%d)", term_value);
1575 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_tmp);
1576 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info, max_pos,
1579 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1580 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1584 static int numeric_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1585 const char **term_sub,
1586 oid_value attributeSet, struct grep_info *grep_info,
1587 int reg_type, int complete_flag,
1588 int num_bases, char **basenames,
1591 char term_dict[2*IT_MAX_WORD+2];
1595 oid_value curAttributeSet = attributeSet;
1597 struct rpn_char_map_info rcmi;
1599 rpn_char_map_prepare (zh, reg_type, &rcmi);
1600 attr_init (&use, zapt, 1);
1601 use_value = attr_find (&use, &curAttributeSet);
1602 logf (LOG_DEBUG, "numeric_term, use value %d", use_value);
1604 if (use_value == -1)
1607 for (base_no = 0; base_no < num_bases; base_no++)
1610 data1_local_attribute *local_attr;
1611 int max_pos, prefix_len = 0;
1614 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
1616 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1617 curAttributeSet, use_value, r);
1624 if (zebraExplain_curDatabase (zh->zei, basenames[base_no]))
1626 zh->errCode = 109; /* Database unavailable */
1627 zh->errString = basenames[base_no];
1630 for (local_attr = attp.local_attributes; local_attr;
1631 local_attr = local_attr->next)
1637 ord = zebraExplain_lookupSU (zh->zei, attp.attset_ordinal,
1642 term_dict[prefix_len++] = '|';
1644 term_dict[prefix_len++] = '(';
1646 ord_len = key_SU_code (ord, ord_buf);
1647 for (i = 0; i<ord_len; i++)
1649 term_dict[prefix_len++] = 1;
1650 term_dict[prefix_len++] = ord_buf[i];
1658 term_dict[prefix_len++] = ')';
1659 term_dict[prefix_len++] = 1;
1660 term_dict[prefix_len++] = reg_type;
1661 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1662 term_dict[prefix_len] = '\0';
1663 if (!numeric_relation (zh, zapt, &termp, term_dict,
1664 attributeSet, grep_info, &max_pos, reg_type,
1669 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1673 static RSET rpn_search_APT_numeric (ZebraHandle zh,
1674 Z_AttributesPlusTerm *zapt,
1676 oid_value attributeSet,
1677 int reg_type, int complete_flag,
1678 const char *rank_type,
1679 int num_bases, char **basenames)
1681 char term_dst[IT_MAX_WORD+1];
1682 const char *termp = termz;
1683 RSET rset[60], result;
1684 int i, r, rset_no = 0;
1685 struct grep_info grep_info;
1688 grep_info.term_no = 0;
1690 grep_info.isam_p_size = 0;
1691 grep_info.isam_p_buf = NULL;
1693 grep_info.reg_type = reg_type;
1697 logf (LOG_DEBUG, "APT_numeric termp=%s", termp);
1698 grep_info.isam_p_indx = 0;
1699 r = numeric_term (zh, zapt, &termp, attributeSet, &grep_info,
1700 reg_type, complete_flag, num_bases, basenames,
1704 logf (LOG_DEBUG, "term: %s", term_dst);
1705 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1706 grep_info.isam_p_indx, term_dst,
1707 strlen(term_dst), rank_type);
1708 assert (rset[rset_no]);
1709 if (++rset_no >= sizeof(rset)/sizeof(*rset))
1713 xfree(grep_info.term_no);
1715 xfree (grep_info.isam_p_buf);
1718 rset_null_parms parms;
1720 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1721 return rset_create (rset_kind_null, &parms);
1724 for (i = 1; i<rset_no; i++)
1726 rset_bool_parms bool_parms;
1728 bool_parms.rset_l = result;
1729 bool_parms.rset_r = rset[i];
1730 bool_parms.key_size = sizeof(struct it_key);
1731 bool_parms.cmp = key_compare_it;
1732 result = rset_create (rset_kind_and, &bool_parms);
1737 static RSET rpn_search_APT_local (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1739 oid_value attributeSet,
1740 const char *rank_type)
1745 rset_temp_parms parms;
1747 parms.rset_term = rset_term_create (termz, -1, rank_type);
1748 parms.key_size = sizeof (struct it_key);
1749 parms.temp_path = res_get (zh->res, "setTmpDir");
1750 result = rset_create (rset_kind_temp, &parms);
1751 rsfd = rset_open (result, RSETF_WRITE);
1753 key.sysno = atoi (termz);
1757 rset_write (result, rsfd, &key);
1758 rset_close (result, rsfd);
1762 static RSET rpn_sort_spec (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1763 oid_value attributeSet, NMEM stream,
1764 Z_SortKeySpecList *sort_sequence,
1765 const char *rank_type)
1767 rset_null_parms parms;
1769 int sort_relation_value;
1770 AttrType sort_relation_type;
1775 Z_AttributeElement *ae;
1779 attr_init (&sort_relation_type, zapt, 7);
1780 sort_relation_value = attr_find (&sort_relation_type, &attributeSet);
1782 attr_init (&use_type, zapt, 1);
1783 use_value = attr_find (&use_type, &attributeSet);
1785 if (!sort_sequence->specs)
1787 sort_sequence->num_specs = 10;
1788 sort_sequence->specs = nmem_malloc (stream, sort_sequence->num_specs *
1789 sizeof(*sort_sequence->specs));
1790 for (i = 0; i<sort_sequence->num_specs; i++)
1791 sort_sequence->specs[i] = 0;
1793 if (zapt->term->which != Z_Term_general)
1796 i = atoi_n (zapt->term->u.general->buf, zapt->term->u.general->len);
1797 if (i >= sort_sequence->num_specs)
1800 oe.proto = PROTO_Z3950;
1801 oe.oclass = CLASS_ATTSET;
1802 oe.value = attributeSet;
1803 if (!oid_ent_to_oid (&oe, oid))
1806 sks = nmem_malloc (stream, sizeof(*sks));
1807 sks->sortElement = nmem_malloc (stream, sizeof(*sks->sortElement));
1808 sks->sortElement->which = Z_SortElement_generic;
1809 sk = sks->sortElement->u.generic = nmem_malloc (stream, sizeof(*sk));
1810 sk->which = Z_SortKey_sortAttributes;
1811 sk->u.sortAttributes = nmem_malloc (stream, sizeof(*sk->u.sortAttributes));
1813 sk->u.sortAttributes->id = oid;
1814 sk->u.sortAttributes->list =
1815 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list));
1816 sk->u.sortAttributes->list->num_attributes = 1;
1817 sk->u.sortAttributes->list->attributes =
1818 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list->attributes));
1819 ae = *sk->u.sortAttributes->list->attributes =
1820 nmem_malloc (stream, sizeof(**sk->u.sortAttributes->list->attributes));
1821 ae->attributeSet = 0;
1822 ae->attributeType = nmem_malloc (stream, sizeof(*ae->attributeType));
1823 *ae->attributeType = 1;
1824 ae->which = Z_AttributeValue_numeric;
1825 ae->value.numeric = nmem_malloc (stream, sizeof(*ae->value.numeric));
1826 *ae->value.numeric = use_value;
1828 sks->sortRelation = nmem_malloc (stream, sizeof(*sks->sortRelation));
1829 if (sort_relation_value == 1)
1830 *sks->sortRelation = Z_SortRelation_ascending;
1831 else if (sort_relation_value == 2)
1832 *sks->sortRelation = Z_SortRelation_descending;
1834 *sks->sortRelation = Z_SortRelation_ascending;
1836 sks->caseSensitivity = nmem_malloc (stream, sizeof(*sks->caseSensitivity));
1837 *sks->caseSensitivity = 0;
1839 sks->missingValueAction = 0;
1841 sort_sequence->specs[i] = sks;
1843 parms.rset_term = rset_term_create ("", -1, rank_type);
1844 return rset_create (rset_kind_null, &parms);
1848 static RSET rpn_search_APT (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1849 oid_value attributeSet, NMEM stream,
1850 Z_SortKeySpecList *sort_sequence,
1851 int num_bases, char **basenames)
1854 char *search_type = NULL;
1855 char *rank_type = NULL;
1858 char termz[IT_MAX_WORD+1];
1860 zebra_maps_attr (zh->zebra_maps, zapt, ®_id, &search_type,
1861 &rank_type, &complete_flag, &sort_flag);
1863 logf (LOG_DEBUG, "reg_id=%c", reg_id);
1864 logf (LOG_DEBUG, "complete_flag=%d", complete_flag);
1865 logf (LOG_DEBUG, "search_type=%s", search_type);
1866 logf (LOG_DEBUG, "rank_type=%s", rank_type);
1868 if (zapt->term->which != Z_Term_general)
1873 trans_term (zh, zapt, termz);
1876 return rpn_sort_spec (zh, zapt, attributeSet, stream, sort_sequence,
1879 if (!strcmp (search_type, "phrase"))
1881 return rpn_search_APT_phrase (zh, zapt, termz, attributeSet,
1882 reg_id, complete_flag, rank_type,
1883 num_bases, basenames);
1885 else if (!strcmp (search_type, "and-list"))
1887 return rpn_search_APT_and_list (zh, zapt, termz, attributeSet,
1888 reg_id, complete_flag, rank_type,
1889 num_bases, basenames);
1891 else if (!strcmp (search_type, "or-list"))
1893 return rpn_search_APT_or_list (zh, zapt, termz, attributeSet,
1894 reg_id, complete_flag, rank_type,
1895 num_bases, basenames);
1897 else if (!strcmp (search_type, "local"))
1899 return rpn_search_APT_local (zh, zapt, termz, attributeSet,
1902 else if (!strcmp (search_type, "numeric"))
1904 return rpn_search_APT_numeric (zh, zapt, termz, attributeSet,
1905 reg_id, complete_flag, rank_type,
1906 num_bases, basenames);
1912 static RSET rpn_search_structure (ZebraHandle zh, Z_RPNStructure *zs,
1913 oid_value attributeSet, NMEM stream,
1914 Z_SortKeySpecList *sort_sequence,
1915 int num_bases, char **basenames)
1918 if (zs->which == Z_RPNStructure_complex)
1920 Z_Operator *zop = zs->u.complex->roperator;
1921 rset_bool_parms bool_parms;
1923 bool_parms.rset_l = rpn_search_structure (zh, zs->u.complex->s1,
1924 attributeSet, stream,
1926 num_bases, basenames);
1927 if (bool_parms.rset_l == NULL)
1929 bool_parms.rset_r = rpn_search_structure (zh, zs->u.complex->s2,
1930 attributeSet, stream,
1932 num_bases, basenames);
1933 if (bool_parms.rset_r == NULL)
1935 rset_delete (bool_parms.rset_l);
1938 bool_parms.key_size = sizeof(struct it_key);
1939 bool_parms.cmp = key_compare_it;
1943 case Z_Operator_and:
1944 r = rset_create (rset_kind_and, &bool_parms);
1947 r = rset_create (rset_kind_or, &bool_parms);
1949 case Z_Operator_and_not:
1950 r = rset_create (rset_kind_not, &bool_parms);
1952 case Z_Operator_prox:
1954 if (zop->u.prox->which != Z_ProximityOperator_known)
1960 if (zop->u.prox->which != Z_ProxCode_known)
1968 if (*zop->u.prox->u.known != Z_ProxUnit_word)
1970 char *val = nmem_malloc (stream, 16);
1972 zh->errString = val;
1973 sprintf (val, "%d", *zop->u.prox->u.known);
1977 if (*zop->u.prox->proximityUnitCode != Z_ProxUnit_word)
1979 char *val = nmem_malloc (stream, 16);
1981 zh->errString = val;
1982 sprintf (val, "%d", *zop->u.prox->proximityUnitCode);
1986 r = rpn_proximity (zh, bool_parms.rset_l, bool_parms.rset_r,
1987 *zop->u.prox->ordered,
1988 (!zop->u.prox->exclusion ? 0 :
1989 *zop->u.prox->exclusion),
1990 *zop->u.prox->relationType,
1991 *zop->u.prox->distance);
1998 else if (zs->which == Z_RPNStructure_simple)
2000 if (zs->u.simple->which == Z_Operand_APT)
2002 logf (LOG_DEBUG, "rpn_search_APT");
2003 r = rpn_search_APT (zh, zs->u.simple->u.attributesPlusTerm,
2004 attributeSet, stream, sort_sequence,
2005 num_bases, basenames);
2007 else if (zs->u.simple->which == Z_Operand_resultSetId)
2009 logf (LOG_DEBUG, "rpn_search_ref");
2010 r = resultSetRef (zh, zs->u.simple->u.resultSetId);
2012 r = rset_create (rset_kind_null, NULL);
2029 RSET rpn_search (ZebraHandle zh, NMEM nmem,
2030 Z_RPNQuery *rpn, int num_bases, char **basenames,
2031 const char *setname,
2036 oid_value attributeSet;
2037 Z_SortKeySpecList *sort_sequence;
2043 zh->errString = NULL;
2046 sort_sequence = nmem_malloc (nmem, sizeof(*sort_sequence));
2047 sort_sequence->num_specs = 10;
2048 sort_sequence->specs = nmem_malloc (nmem, sort_sequence->num_specs *
2049 sizeof(*sort_sequence->specs));
2050 for (i = 0; i<sort_sequence->num_specs; i++)
2051 sort_sequence->specs[i] = 0;
2053 attrset = oid_getentbyoid (rpn->attributeSetId);
2054 attributeSet = attrset->value;
2055 rset = rpn_search_structure (zh, rpn->RPNStructure, attributeSet,
2056 nmem, sort_sequence, num_bases, basenames);
2061 logf (LOG_DEBUG, "search error: %d", zh->errCode);
2063 for (i = 0; sort_sequence->specs[i]; i++)
2065 sort_sequence->num_specs = i;
2067 resultSetRank (zh, sset, rset);
2070 logf (LOG_DEBUG, "resultSetSortSingle in rpn_search");
2071 resultSetSortSingle (zh, nmem, sset, rset,
2072 sort_sequence, &sort_status);
2075 logf (LOG_DEBUG, "resultSetSortSingle status = %d", zh->errCode);
2081 struct scan_info_entry {
2087 struct scan_info_entry *list;
2093 static int scan_handle (char *name, const char *info, int pos, void *client)
2095 int len_prefix, idx;
2096 struct scan_info *scan_info = client;
2098 len_prefix = strlen(scan_info->prefix);
2099 if (memcmp (name, scan_info->prefix, len_prefix))
2102 idx = scan_info->after - pos + scan_info->before;
2105 scan_info->list[idx].term = odr_malloc (scan_info->odr,
2106 strlen(name + len_prefix)+1);
2107 strcpy (scan_info->list[idx].term, name + len_prefix);
2108 assert (*info == sizeof(ISAM_P));
2109 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAM_P));
2113 static void scan_term_untrans (ZebraHandle zh, NMEM stream, int reg_type,
2114 char **dst, const char *src)
2116 char term_dst[1024];
2118 term_untrans (zh, reg_type, term_dst, src);
2120 *dst = nmem_malloc (stream, strlen(term_dst)+1);
2121 strcpy (*dst, term_dst);
2124 static void count_set (RSET r, int *count)
2132 logf (LOG_DEBUG, "count_set");
2135 rfd = rset_open (r, RSETF_READ);
2136 while (rset_read (r, rfd, &key, &term_index))
2138 if (key.sysno != psysno)
2145 rset_close (r, rfd);
2146 logf (LOG_DEBUG, "%d keys, %d records", kno, *count);
2149 void rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
2150 oid_value attributeset,
2151 int num_bases, char **basenames,
2152 int *position, int *num_entries, ZebraScanEntry **list,
2156 int pos = *position;
2157 int num = *num_entries;
2161 char termz[IT_MAX_WORD+20];
2164 struct scan_info *scan_info_array;
2165 ZebraScanEntry *glist;
2166 int ords[32], ord_no = 0;
2170 char *search_type = NULL;
2171 char *rank_type = NULL;
2175 if (attributeset == VAL_NONE)
2176 attributeset = VAL_BIB1;
2178 zlog_scan (zapt, attributeset);
2179 logf (LOG_DEBUG, "position = %d, num = %d", pos, num);
2181 attr_init (&use, zapt, 1);
2182 use_value = attr_find (&use, &attributeset);
2184 if (zebra_maps_attr (zh->zebra_maps, zapt, ®_id, &search_type,
2185 &rank_type, &complete_flag, &sort_flag))
2191 if (use_value == -1)
2193 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
2197 data1_local_attribute *local_attr;
2199 if ((r=att_getentbyatt (zh, &attp, attributeset, use_value)))
2201 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
2202 attributeset, use_value);
2208 if (zebraExplain_curDatabase (zh->zei, basenames[base_no]))
2210 zh->errString = basenames[base_no];
2211 zh->errCode = 109; /* Database unavailable */
2214 for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
2215 local_attr = local_attr->next)
2219 ord = zebraExplain_lookupSU (zh->zei, attp.attset_ordinal,
2222 ords[ord_no++] = ord;
2230 /* prepare dictionary scanning */
2233 scan_info_array = odr_malloc (stream, ord_no * sizeof(*scan_info_array));
2234 for (i = 0; i < ord_no; i++)
2236 int j, prefix_len = 0;
2237 int before_tmp = before, after_tmp = after;
2238 struct scan_info *scan_info = scan_info_array + i;
2239 struct rpn_char_map_info rcmi;
2241 rpn_char_map_prepare (zh, reg_id, &rcmi);
2243 scan_info->before = before;
2244 scan_info->after = after;
2245 scan_info->odr = stream;
2247 scan_info->list = odr_malloc (stream, (before+after)*
2248 sizeof(*scan_info->list));
2249 for (j = 0; j<before+after; j++)
2250 scan_info->list[j].term = NULL;
2252 prefix_len += key_SU_code (ords[i], termz + prefix_len);
2253 termz[prefix_len++] = reg_id;
2254 termz[prefix_len] = 0;
2255 strcpy (scan_info->prefix, termz);
2257 trans_scan_term (zh, zapt, termz+prefix_len, reg_id);
2259 dict_scan (zh->dict, termz, &before_tmp, &after_tmp, scan_info,
2262 glist = odr_malloc (stream, (before+after)*sizeof(*glist));
2264 /* consider terms after main term */
2265 for (i = 0; i < ord_no; i++)
2269 for (i = 0; i<after; i++)
2272 const char *mterm = NULL;
2276 for (j = 0; j < ord_no; j++)
2278 if (ptr[j] < before+after &&
2279 (tst=scan_info_array[j].list[ptr[j]].term) &&
2280 (!mterm || strcmp (tst, mterm) < 0))
2288 scan_term_untrans (zh, stream->mem, reg_id,
2289 &glist[i+before].term, mterm);
2290 rset = rset_trunc (zh, &scan_info_array[j0].list[ptr[j0]].isam_p, 1,
2291 glist[i+before].term, strlen(glist[i+before].term),
2295 for (j = j0+1; j<ord_no; j++)
2297 if (ptr[j] < before+after &&
2298 (tst=scan_info_array[j].list[ptr[j]].term) &&
2299 !strcmp (tst, mterm))
2301 rset_bool_parms bool_parms;
2305 rset_trunc (zh, &scan_info_array[j].list[ptr[j]].isam_p, 1,
2306 glist[i+before].term,
2307 strlen(glist[i+before].term), NULL);
2309 bool_parms.key_size = sizeof(struct it_key);
2310 bool_parms.cmp = key_compare_it;
2311 bool_parms.rset_l = rset;
2312 bool_parms.rset_r = rset2;
2314 rset = rset_create (rset_kind_or, &bool_parms);
2319 count_set (rset, &glist[i+before].occurrences);
2324 *num_entries -= (after-i);
2328 /* consider terms before main term */
2329 for (i = 0; i<ord_no; i++)
2332 for (i = 0; i<before; i++)
2335 const char *mterm = NULL;
2339 for (j = 0; j <ord_no; j++)
2341 if (ptr[j] < before &&
2342 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2343 (!mterm || strcmp (tst, mterm) > 0))
2352 scan_term_untrans (zh, stream->mem, reg_id,
2353 &glist[before-1-i].term, mterm);
2356 (zh, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1,
2357 glist[before-1-i].term, strlen(glist[before-1-i].term),
2362 for (j = j0+1; j<ord_no; j++)
2364 if (ptr[j] < before &&
2365 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2366 !strcmp (tst, mterm))
2368 rset_bool_parms bool_parms;
2371 rset2 = rset_trunc (zh,
2372 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1,
2373 glist[before-1-i].term,
2374 strlen(glist[before-1-i].term), NULL);
2376 bool_parms.key_size = sizeof(struct it_key);
2377 bool_parms.cmp = key_compare_it;
2378 bool_parms.rset_l = rset;
2379 bool_parms.rset_r = rset2;
2381 rset = rset_create (rset_kind_or, &bool_parms);
2386 count_set (rset, &glist[before-1-i].occurrences);
2396 *list = glist + i; /* list is set to first 'real' entry */
2398 logf (LOG_DEBUG, "position = %d, num_entries = %d",
2399 *position, *num_entries);
2401 logf (LOG_DEBUG, "scan error: %d", zh->errCode);