2 * Copyright (C) 1995-1998, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.86 1998-09-22 10:48:20 adam
8 * Minor changes in search API.
10 * Revision 1.85 1998/09/22 10:03:43 adam
11 * Changed result sets to be persistent in the sense that they can
12 * be re-searched if needed.
13 * Fixed memory leak in rsm_or.
15 * Revision 1.84 1998/09/18 12:41:00 adam
16 * Fixed bug with numerical relations.
18 * Revision 1.83 1998/09/02 13:53:19 adam
19 * Extra parameter decode added to search routines to implement
22 * Revision 1.82 1998/06/26 11:16:40 quinn
23 * Added support (un-optimised) for left and left/right truncation
25 * Revision 1.81 1998/06/24 12:16:14 adam
26 * Support for relations on text operands. Open range support in
27 * DFA module (i.e. [-j], [g-]).
29 * Revision 1.80 1998/06/23 15:33:34 adam
30 * Added feature to specify sort criteria in query (type 7 specifies
33 * Revision 1.79 1998/06/22 11:35:09 adam
36 * Revision 1.78 1998/06/08 14:43:17 adam
37 * Added suport for EXPLAIN Proxy servers - added settings databasePath
38 * and explainDatabase to facilitate this. Increased maximum number
39 * of databases and attributes in one register.
41 * Revision 1.77 1998/05/20 10:12:22 adam
42 * Implemented automatic EXPLAIN database maintenance.
43 * Modified Zebra to work with ASN.1 compiled version of YAZ.
45 * Revision 1.76 1998/04/02 14:35:29 adam
46 * First version of Zebra that works with compiled ASN.1.
48 * Revision 1.75 1998/03/05 08:45:13 adam
49 * New result set model and modular ranking system. Moved towards
50 * descent server API. System information stored as "SGML" records.
52 * Revision 1.74 1998/02/10 12:03:06 adam
55 * Revision 1.73 1998/01/29 13:40:11 adam
56 * Better logging for scan service.
58 * Revision 1.72 1998/01/07 13:53:41 adam
59 * Queries using simple ranked operands returns right number of hits.
61 * Revision 1.71 1997/12/18 10:54:24 adam
62 * New method result set method rs_hits that returns the number of
63 * hits in result-set (if known). The ranked result set returns real
64 * number of hits but only when not combined with other operands.
66 * Revision 1.70 1997/10/31 12:34:43 adam
67 * Changed a few log statements.
69 * Revision 1.69 1997/10/29 12:05:02 adam
70 * Server produces diagnostic "Unsupported Attribute Set" when appropriate.
72 * Revision 1.68 1997/10/27 14:33:06 adam
73 * Moved towards generic character mapping depending on "structure"
74 * field in abstract syntax file. Fixed a few memory leaks. Fixed
75 * bug with negative integers when doing searches with relational
78 * Revision 1.67 1997/09/29 09:06:10 adam
79 * Removed one static var in order to make this module thread safe.
81 * Revision 1.66 1997/09/25 14:58:03 adam
84 * Revision 1.65 1997/09/22 12:39:06 adam
85 * Added get_pos method for the ranked result sets.
87 * Revision 1.64 1997/09/18 08:59:20 adam
88 * Extra generic handle for the character mapping routines.
90 * Revision 1.63 1997/09/17 12:19:18 adam
91 * Zebra version corresponds to YAZ version 1.4.
92 * Changed Zebra server so that it doesn't depend on global common_resource.
94 * Revision 1.62 1997/09/05 15:30:09 adam
95 * Changed prototype for chr_map_input - added const.
96 * Added support for C++, headers uses extern "C" for public definitions.
98 * Revision 1.61 1997/02/10 10:21:14 adam
99 * Bug fix: in search terms character (^) wasn't observed.
101 * Revision 1.60 1997/01/31 11:10:34 adam
102 * Bug fix: Leading and trailing white space weren't removed in scan tokens.
104 * Revision 1.59 1997/01/17 11:31:46 adam
105 * Bug fix: complete phrase search didn't work.
107 * Revision 1.58 1996/12/23 15:30:45 adam
108 * Work on truncation.
109 * Bug fix: result sets weren't deleted after server shut down.
111 * Revision 1.57 1996/11/11 13:38:02 adam
112 * Added proximity support in search.
114 * Revision 1.56 1996/11/08 11:10:32 adam
115 * Buffers used during file match got bigger.
116 * Compressed ISAM support everywhere.
117 * Bug fixes regarding masking characters in queries.
118 * Redesigned Regexp-2 queries.
120 * Revision 1.55 1996/11/04 14:07:44 adam
121 * Moved truncation code to trunc.c.
123 * Revision 1.54 1996/10/29 14:09:52 adam
124 * Use of cisam system - enabled if setting isamc is 1.
126 * Revision 1.53 1996/06/26 09:21:43 adam
127 * Bug fix: local attribute set wasn't obeyed in scan.
129 * Revision 1.52 1996/06/17 14:26:20 adam
130 * Function gen_regular_rel changed to handle negative numbers.
132 * Revision 1.51 1996/06/11 10:54:15 quinn
135 * Revision 1.50 1996/06/07 08:51:53 adam
136 * Bug fix: Character mapping was broken (introducued by last revision).
138 * Revision 1.49 1996/06/04 10:18:11 adam
139 * Search/scan uses character mapping module.
141 * Revision 1.48 1996/05/28 15:15:01 adam
142 * Bug fix: Didn't handle unknown database correctly.
144 * Revision 1.47 1996/05/15 18:36:28 adam
145 * Function trans_term transforms unsearchable characters to blanks.
147 * Revision 1.46 1996/05/15 11:57:56 adam
148 * Fixed bug introduced by set/field mapping in search operations.
150 * Revision 1.45 1996/05/14 11:34:00 adam
151 * Scan support in multiple registers/databases.
153 * Revision 1.44 1996/05/14 06:16:44 adam
154 * Compact use/set bytes used in search service.
156 * Revision 1.43 1996/05/09 09:54:43 adam
157 * Server supports maps from one logical attributes to a list of physical
159 * The extraction process doesn't make space consuming 'any' keys.
161 * Revision 1.42 1996/05/09 07:28:56 quinn
162 * Work towards phrases and multiple registers
164 * Revision 1.41 1996/03/20 09:36:43 adam
165 * Function dict_lookup_grep got extra parameter, init_pos, which marks
166 * from which position in pattern approximate pattern matching should occur.
167 * Approximate pattern matching is used in relevance=re-2.
169 * Revision 1.40 1996/02/02 13:44:44 adam
170 * The public dictionary functions simply use char instead of Dict_char
171 * to represent search strings. Dict_char is used internally only.
173 * Revision 1.39 1996/01/03 16:22:13 quinn
174 * operator->roperator
176 * Revision 1.38 1995/12/11 09:12:55 adam
177 * The rec_get function returns NULL if record doesn't exist - will
178 * happen in the server if the result set records have been deleted since
179 * the creation of the set (i.e. the search).
180 * The server saves a result temporarily if it is 'volatile', i.e. the
181 * set is register dependent.
183 * Revision 1.37 1995/12/06 15:05:28 adam
184 * More verbose in count_set.
186 * Revision 1.36 1995/12/06 12:41:27 adam
187 * New command 'stat' for the index program.
188 * Filenames can be read from stdin by specifying '-'.
189 * Bug fix/enhancement of the transformation from terms to regular
190 * expressons in the search engine.
192 * Revision 1.35 1995/11/27 09:29:00 adam
193 * Bug fixes regarding conversion to regular expressions.
195 * Revision 1.34 1995/11/16 17:00:56 adam
196 * Better logging of rpn query.
198 * Revision 1.33 1995/11/01 13:58:28 quinn
199 * Moving data1 to yaz/retrieval
201 * Revision 1.32 1995/10/27 14:00:11 adam
202 * Implemented detection of database availability.
204 * Revision 1.31 1995/10/17 18:02:10 adam
205 * New feature: databases. Implemented as prefix to words in dictionary.
207 * Revision 1.30 1995/10/16 09:32:38 adam
208 * More work on relational op.
210 * Revision 1.29 1995/10/13 16:01:49 adam
213 * Revision 1.28 1995/10/13 12:26:43 adam
214 * Optimization of truncation.
216 * Revision 1.27 1995/10/12 17:07:22 adam
219 * Revision 1.26 1995/10/12 12:40:54 adam
220 * Bug fixes in rpn_prox.
222 * Revision 1.25 1995/10/10 13:59:24 adam
223 * Function rset_open changed its wflag parameter to general flags.
225 * Revision 1.24 1995/10/09 16:18:37 adam
226 * Function dict_lookup_grep got extra client data parameter.
228 * Revision 1.23 1995/10/06 16:33:37 adam
229 * Use attribute mappings.
231 * Revision 1.22 1995/10/06 15:07:39 adam
232 * Structure 'local-number' handled.
234 * Revision 1.21 1995/10/06 13:52:06 adam
235 * Bug fixes. Handler may abort further scanning.
237 * Revision 1.20 1995/10/06 11:06:33 adam
238 * Scan entries include 'occurrences' now.
240 * Revision 1.19 1995/10/06 10:43:56 adam
241 * Scan added. 'occurrences' in scan entries not set yet.
243 * Revision 1.18 1995/10/04 16:57:20 adam
244 * Key input and merge sort in one pass.
246 * Revision 1.17 1995/10/04 12:55:17 adam
247 * Bug fix in ranked search. Use=Any keys inserted.
249 * Revision 1.16 1995/10/02 16:24:40 adam
250 * Use attribute actually used in search requests.
252 * Revision 1.15 1995/10/02 15:18:52 adam
253 * New member in recRetrieveCtrl: diagnostic.
255 * Revision 1.14 1995/09/28 12:10:32 adam
256 * Bug fixes. Field prefix used in queries.
258 * Revision 1.13 1995/09/18 14:17:50 adam
261 * Revision 1.12 1995/09/15 14:45:21 adam
263 * Work on truncation.
265 * Revision 1.11 1995/09/14 11:53:27 adam
266 * First work on regular expressions/truncations.
268 * Revision 1.10 1995/09/11 15:23:26 adam
269 * More work on relevance search.
271 * Revision 1.9 1995/09/11 13:09:35 adam
272 * More work on relevance feedback.
274 * Revision 1.8 1995/09/08 14:52:27 adam
275 * Minor changes. Dictionary is lower case now.
277 * Revision 1.7 1995/09/07 13:58:36 adam
278 * New parameter: result-set file descriptor (RSFD) to support multiple
279 * positions within the same result-set.
280 * Boolean operators: and, or, not implemented.
281 * Result-set references.
283 * Revision 1.6 1995/09/06 16:11:18 adam
284 * Option: only one word key per file.
286 * Revision 1.5 1995/09/06 10:33:04 adam
287 * More work on present. Some log messages removed.
289 * Revision 1.4 1995/09/05 15:28:40 adam
290 * More work on search engine.
292 * Revision 1.3 1995/09/04 15:20:22 adam
295 * Revision 1.2 1995/09/04 12:33:43 adam
296 * Various cleanup. YAZ util used instead.
298 * Revision 1.1 1995/09/04 09:10:40 adam
299 * More work on index add/del/update.
300 * Merge sort implemented.
301 * Initial work on z39 server.
320 struct rpn_char_map_info {
325 static const char **rpn_char_map_handler (void *vp, const char **from, int len)
327 struct rpn_char_map_info *p = vp;
328 return zebra_maps_input (p->zm, p->reg_type, from, len);
331 static void rpn_char_map_prepare (ZebraHandle zh, int reg_type,
332 struct rpn_char_map_info *map_info)
334 map_info->zm = zh->zebra_maps;
335 map_info->reg_type = reg_type;
336 dict_grep_cmap (zh->dict, map_info, rpn_char_map_handler);
343 Z_AttributesPlusTerm *zapt;
346 static int attr_find (AttrType *src, oid_value *attributeSetP)
351 num_attributes = src->zapt->attributes->num_attributes;
353 num_attributes = src->zapt->num_attributes;
355 while (src->major < num_attributes)
357 Z_AttributeElement *element;
360 element = src->zapt->attributes->attributes[src->major];
362 element = src->zapt->attributeList[src->major];
364 if (src->type == *element->attributeType)
366 switch (element->which)
368 case Z_AttributeValue_numeric:
370 if (element->attributeSet && attributeSetP)
374 attrset = oid_getentbyoid (element->attributeSet);
375 *attributeSetP = attrset->value;
377 return *element->value.numeric;
379 case Z_AttributeValue_complex:
380 if (src->minor >= element->value.complex->num_list ||
381 element->value.complex->list[src->minor]->which !=
382 Z_StringOrNumeric_numeric)
385 if (element->attributeSet && attributeSetP)
389 attrset = oid_getentbyoid (element->attributeSet);
390 *attributeSetP = attrset->value;
392 return *element->value.complex->list[src->minor-1]->u.numeric;
402 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
424 static void term_untrans (ZebraHandle zh, int reg_type,
425 char *dst, const char *src)
429 const char *cp = zebra_maps_output (zh->zebra_maps, reg_type, &src);
436 static void add_isam_p (const char *name, const char *info,
439 if (p->isam_p_indx == p->isam_p_size)
441 ISAM_P *new_isam_p_buf;
445 p->isam_p_size = 2*p->isam_p_size + 100;
446 new_isam_p_buf = xmalloc (sizeof(*new_isam_p_buf) *
450 memcpy (new_isam_p_buf, p->isam_p_buf,
451 p->isam_p_indx * sizeof(*p->isam_p_buf));
452 xfree (p->isam_p_buf);
454 p->isam_p_buf = new_isam_p_buf;
457 new_term_no = xmalloc (sizeof(*new_term_no) *
461 memcpy (new_term_no, p->isam_p_buf,
462 p->isam_p_indx * sizeof(*p->term_no));
465 p->term_no = new_term_no;
468 assert (*info == sizeof(*p->isam_p_buf));
469 memcpy (p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
472 term_untrans (p->zh, p->reg_type, term_tmp, name+2);
473 logf (LOG_DEBUG, "grep: %s", term_tmp);
478 static int grep_handle (char *name, const char *info, void *p)
480 add_isam_p (name, info, p);
484 static int term_pre (ZebraMaps zebra_maps, int reg_type, const char **src,
485 const char *ct1, const char *ct2)
487 const char *s1, *s0 = *src;
490 /* skip white space */
493 if (ct1 && strchr (ct1, *s0))
495 if (ct2 && strchr (ct2, *s0))
498 map = zebra_maps_input (zebra_maps, reg_type, &s1, strlen(s1));
499 if (**map != *CHR_SPACE)
507 static int term_100 (ZebraMaps zebra_maps, int reg_type,
508 const char **src, char *dst, int space_split,
516 if (!term_pre (zebra_maps, reg_type, src, NULL, NULL))
522 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
523 if (space_split && **map == *CHR_SPACE)
527 if (!isalnum (*s1) && *s1 != '-')
539 static int term_101 (ZebraMaps zebra_maps, int reg_type,
540 const char **src, char *dst, int space_split,
548 if (!term_pre (zebra_maps, reg_type, src, "#", "#"))
557 dst_term[j++] = *s0++;
562 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
563 if (space_split && **map == *CHR_SPACE)
575 dst_term[j++] = '\0';
581 static int term_103 (ZebraMaps zebra_maps, int reg_type, const char **src,
582 char *dst, int *errors, int space_split,
590 if (!term_pre (zebra_maps, reg_type, src, "^\\()[].*+?|", "("))
593 if (errors && *s0 == '+' && s0[1] && s0[2] == '+' && s0[3] &&
596 *errors = s0[1] - '0';
603 if (strchr ("^\\()[].*+?|-", *s0))
611 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
612 if (**map == *CHR_SPACE)
629 static int term_102 (ZebraMaps zebra_maps, int reg_type, const char **src,
630 char *dst, int space_split, char *dst_term)
632 return term_103 (zebra_maps, reg_type, src, dst, NULL, space_split,
636 /* gen_regular_rel - generate regular expression from relation
637 * val: border value (inclusive)
638 * islt: 1 if <=; 0 if >=.
640 static void gen_regular_rel (char *dst, int val, int islt)
647 logf (LOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
651 strcpy (dst, "(-[0-9]+|(");
659 strcpy (dst, "([0-9]+|-(");
660 dst_p = strlen (dst);
670 dst_p = strlen (dst);
671 sprintf (numstr, "%d", val);
672 for (w = strlen(numstr); --w >= 0; pos++)
691 strcpy (dst + dst_p, numstr);
692 dst_p = strlen(dst) - pos - 1;
720 for (i = 0; i<pos; i++)
733 for (i=1; i<pos; i++)
734 strcat (dst, "[0-9]?");
738 for (i = 0; i <= pos; i++)
739 strcat (dst, "[0-9]");
740 strcat (dst, "[0-9]*");
745 void string_rel_add_char (char **term_p, const char *src, int *indx)
747 if (src[*indx] == '\\')
748 *(*term_p)++ = src[(*indx)++];
749 *(*term_p)++ = src[(*indx)++];
753 * > abc ([b-].*|a[c-].*|ab[d-].*|abc.+)
754 * ([^-a].*|a[^-b].*ab[^-c].*|abc.+)
755 * >= abc ([b-].*|a[c-].*|ab[c-].*)
756 * ([^-a].*|a[^-b].*|ab[c-].*)
757 * < abc ([-0].*|a[-a].*|ab[-b].*)
758 * ([^a-].*|a[^b-].*|ab[^c-].*)
759 * <= abc ([-0].*|a[-a].*|ab[-b].*|abc)
760 * ([^a-].*|a[^b-].*|ab[^c-].*|abc)
762 static int string_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
763 const char **term_sub, char *term_dict,
764 oid_value attributeSet,
765 int reg_type, int space_split, char *term_dst)
770 char *term_tmp = term_dict + strlen(term_dict);
771 char term_component[256];
773 attr_init (&relation, zapt, 2);
774 relation_value = attr_find (&relation, NULL);
776 logf (LOG_DEBUG, "string relation value=%d", relation_value);
777 switch (relation_value)
780 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
781 space_split, term_dst))
783 logf (LOG_DEBUG, "Relation <");
786 for (i = 0; term_component[i]; )
793 string_rel_add_char (&term_tmp, term_component, &j);
798 string_rel_add_char (&term_tmp, term_component, &i);
809 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
810 space_split, term_dst))
812 logf (LOG_DEBUG, "Relation <=");
815 for (i = 0; term_component[i]; )
820 string_rel_add_char (&term_tmp, term_component, &j);
824 string_rel_add_char (&term_tmp, term_component, &i);
833 for (i = 0; term_component[i]; )
834 string_rel_add_char (&term_tmp, term_component, &i);
839 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
840 space_split, term_dst))
842 logf (LOG_DEBUG, "Relation >");
845 for (i = 0; term_component[i];)
850 string_rel_add_char (&term_tmp, term_component, &j);
855 string_rel_add_char (&term_tmp, term_component, &i);
863 for (i = 0; term_component[i];)
864 string_rel_add_char (&term_tmp, term_component, &i);
871 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
872 space_split, term_dst))
874 logf (LOG_DEBUG, "Relation >=");
877 for (i = 0; term_component[i];)
884 string_rel_add_char (&term_tmp, term_component, &j);
887 if (term_component[i+1])
891 string_rel_add_char (&term_tmp, term_component, &i);
895 string_rel_add_char (&term_tmp, term_component, &i);
907 logf (LOG_DEBUG, "Relation =");
908 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
909 space_split, term_dst))
911 strcat (term_tmp, "(");
912 strcat (term_tmp, term_component);
913 strcat (term_tmp, ")");
918 static int string_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
919 const char **term_sub,
920 oid_value attributeSet, struct grep_info *grep_info,
921 int reg_type, int complete_flag,
922 int num_bases, char **basenames,
925 char term_dict[2*IT_MAX_WORD+4000];
928 int truncation_value;
931 oid_value curAttributeSet = attributeSet;
933 struct rpn_char_map_info rcmi;
934 int space_split = complete_flag ? 0 : 1;
936 rpn_char_map_prepare (zh, reg_type, &rcmi);
937 attr_init (&use, zapt, 1);
938 use_value = attr_find (&use, &curAttributeSet);
939 logf (LOG_DEBUG, "string_term, use value %d", use_value);
940 attr_init (&truncation, zapt, 5);
941 truncation_value = attr_find (&truncation, NULL);
942 logf (LOG_DEBUG, "truncation value %d", truncation_value);
947 for (base_no = 0; base_no < num_bases; base_no++)
950 data1_local_attribute *local_attr;
951 int max_pos, prefix_len = 0;
954 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
956 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
957 curAttributeSet, use_value, r);
964 if (zebraExplain_curDatabase (zh->zei, basenames[base_no]))
966 zh->errCode = 109; /* Database unavailable */
967 zh->errString = basenames[base_no];
970 for (local_attr = attp.local_attributes; local_attr;
971 local_attr = local_attr->next)
977 ord = zebraExplain_lookupSU (zh->zei, attp.attset_ordinal,
982 term_dict[prefix_len++] = '|';
984 term_dict[prefix_len++] = '(';
986 ord_len = key_SU_code (ord, ord_buf);
987 for (i = 0; i<ord_len; i++)
989 term_dict[prefix_len++] = 1;
990 term_dict[prefix_len++] = ord_buf[i];
998 term_dict[prefix_len++] = ')';
999 term_dict[prefix_len++] = 1;
1000 term_dict[prefix_len++] = reg_type;
1001 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1002 term_dict[prefix_len] = '\0';
1004 switch (truncation_value)
1006 case -1: /* not specified */
1007 case 100: /* do not truncate */
1008 if (!string_relation (zh, zapt, &termp, term_dict,
1010 reg_type, space_split, term_dst))
1012 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_dict+prefix_len);
1013 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info, &max_pos,
1016 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1018 case 1: /* right truncation */
1019 term_dict[j++] = '(';
1020 if (!term_100 (zh->zebra_maps, reg_type,
1021 &termp, term_dict + j, space_split, term_dst))
1023 strcat (term_dict, ".*)");
1024 dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1025 &max_pos, 0, grep_handle);
1027 case 2: /* keft truncation */
1028 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1029 if (!term_100 (zh->zebra_maps, reg_type,
1030 &termp, term_dict + j, space_split, term_dst))
1032 strcat (term_dict, ")");
1033 dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1034 &max_pos, 0, grep_handle);
1036 case 3: /* left&right truncation */
1037 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1038 if (!term_100 (zh->zebra_maps, reg_type,
1039 &termp, term_dict + j, space_split, term_dst))
1041 strcat (term_dict, ".*)");
1042 dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1043 &max_pos, 0, grep_handle);
1047 case 101: /* process # in term */
1048 term_dict[j++] = '(';
1049 if (!term_101 (zh->zebra_maps, reg_type,
1050 &termp, term_dict + j, space_split, term_dst))
1052 strcat (term_dict, ")");
1053 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1054 &max_pos, 0, grep_handle);
1056 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d", r);
1058 case 102: /* Regexp-1 */
1059 term_dict[j++] = '(';
1060 if (!term_102 (zh->zebra_maps, reg_type,
1061 &termp, term_dict + j, space_split, term_dst))
1063 strcat (term_dict, ")");
1064 logf (LOG_DEBUG, "Regexp-1 tolerance=%d", r);
1065 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1066 &max_pos, 0, grep_handle);
1068 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
1071 case 103: /* Regexp-2 */
1073 term_dict[j++] = '(';
1074 if (!term_103 (zh->zebra_maps, reg_type,
1075 &termp, term_dict + j, &r, space_split, term_dst))
1077 strcat (term_dict, ")");
1078 logf (LOG_DEBUG, "Regexp-2 tolerance=%d", r);
1079 r = dict_lookup_grep (zh->dict, term_dict, r, grep_info,
1080 &max_pos, 2, grep_handle);
1082 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
1088 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1092 static void trans_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1096 Z_Term *term = zapt->term;
1098 sizez = term->u.general->len;
1099 if (sizez > IT_MAX_WORD-1)
1100 sizez = IT_MAX_WORD-1;
1101 memcpy (termz, term->u.general->buf, sizez);
1102 termz[sizez] = '\0';
1105 static void trans_scan_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1106 char *termz, int reg_type)
1108 Z_Term *term = zapt->term;
1110 const char *cp = (const char *) term->u.general->buf;
1111 const char *cp_end = cp + term->u.general->len;
1114 const char *space_map = NULL;
1117 while ((len = (cp_end - cp)) > 0)
1119 map = zebra_maps_input (zh->zebra_maps, reg_type, &cp, len);
1120 if (**map == *CHR_SPACE)
1125 for (src = space_map; *src; src++)
1128 for (src = *map; *src; src++)
1135 static RSET rpn_proximity (ZebraHandle zh, RSET rset1, RSET rset2,
1137 int exclusion, int relation, int distance)
1142 struct it_key buf1, buf2;
1145 rset_temp_parms parms;
1148 rsfd1 = rset_open (rset1, RSETF_READ);
1149 more1 = rset_read (rset1, rsfd1, &buf1, &term_index);
1151 rsfd2 = rset_open (rset2, RSETF_READ);
1152 more2 = rset_read (rset2, rsfd2, &buf2, &term_index);
1154 parms.key_size = sizeof (struct it_key);
1155 parms.temp_path = res_get (zh->res, "setTmpDir");
1156 result = rset_create (rset_kind_temp, &parms);
1157 rsfd_result = rset_open (result, RSETF_WRITE);
1159 logf (LOG_DEBUG, "rpn_proximity excl=%d ord=%d rel=%d dis=%d",
1160 exclusion, ordered, relation, distance);
1161 while (more1 && more2)
1163 int cmp = key_compare_it (&buf1, &buf2);
1165 more1 = rset_read (rset1, rsfd1, &buf1, &term_index);
1167 more2 = rset_read (rset2, rsfd2, &buf2, &term_index);
1170 int sysno = buf1.sysno;
1174 seqno[n++] = buf1.seqno;
1175 while ((more1 = rset_read (rset1, rsfd1, &buf1, &term_index)) &&
1176 sysno == buf1.sysno)
1178 seqno[n++] = buf1.seqno;
1181 for (i = 0; i<n; i++)
1183 int diff = buf2.seqno - seqno[i];
1184 int excl = exclusion;
1185 if (!ordered && diff < 0)
1190 if (diff < distance)
1194 if (diff <= distance)
1198 if (diff == distance)
1202 if (diff >= distance)
1206 if (diff > distance)
1210 if (diff != distance)
1215 rset_write (result, rsfd_result, &buf2);
1217 } while ((more2 = rset_read (rset2, rsfd2, &buf2, &term_index)) &&
1218 sysno == buf2.sysno);
1221 rset_close (result, rsfd_result);
1222 rset_close (rset1, rsfd1);
1223 rset_close (rset2, rsfd2);
1227 static RSET rpn_prox (ZebraHandle zh, RSET *rset, int rset_no)
1232 struct it_key **buf;
1234 char prox_term[1024];
1235 int length_prox_term = 0;
1236 int min_nn = 10000000;
1238 const char *flags = NULL;
1240 rsfd = xmalloc (sizeof(*rsfd)*rset_no);
1241 more = xmalloc (sizeof(*more)*rset_no);
1242 buf = xmalloc (sizeof(*buf)*rset_no);
1244 for (i = 0; i<rset_no; i++)
1247 buf[i] = xmalloc (sizeof(**buf));
1248 rsfd[i] = rset_open (rset[i], RSETF_READ);
1249 if (!(more[i] = rset_read (rset[i], rsfd[i], buf[i], &term_index)))
1251 for (j = 0; j<rset[i]->no_rset_terms; j++)
1253 const char *nflags = rset[i]->rset_terms[j]->flags;
1254 char *term = rset[i]->rset_terms[j]->name;
1255 int lterm = strlen(term);
1256 if (length_prox_term)
1257 prox_term[length_prox_term++] = ' ';
1258 strcpy (prox_term + length_prox_term, term);
1259 length_prox_term += lterm;
1260 if (min_nn > rset[i]->rset_terms[j]->nn)
1261 min_nn = rset[i]->rset_terms[j]->nn;
1267 rset_null_parms parms;
1271 rset_close (rset[i], rsfd[i]);
1275 parms.rset_term = rset_term_create (prox_term, -1, flags);
1276 parms.rset_term->nn = 0;
1277 result = rset_create (rset_kind_null, &parms);
1281 rset_temp_parms parms;
1284 parms.rset_term = rset_term_create (prox_term, -1, flags);
1285 parms.rset_term->nn = min_nn;
1286 parms.key_size = sizeof (struct it_key);
1287 parms.temp_path = res_get (zh->res, "setTmpDir");
1288 result = rset_create (rset_kind_temp, &parms);
1289 rsfd_result = rset_open (result, RSETF_WRITE);
1293 for (i = 1; i<rset_no; i++)
1302 cmp = key_compare_it (buf[i], buf[i-1]);
1305 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1306 buf[i-1], &term_index);
1311 if (buf[i-1]->seqno+1 != buf[i]->seqno)
1313 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1314 buf[i-1], &term_index);
1320 more[i] = rset_read (rset[i], rsfd[i], buf[i],
1327 rset_write (result, rsfd_result, buf[0]);
1328 more[0] = rset_read (*rset, *rsfd, *buf, &term_index);
1332 for (i = 0; i<rset_no; i++)
1334 rset_close (rset[i], rsfd[i]);
1337 rset_close (result, rsfd_result);
1345 static RSET rpn_search_APT_phrase (ZebraHandle zh,
1346 Z_AttributesPlusTerm *zapt,
1348 oid_value attributeSet,
1349 int reg_type, int complete_flag,
1350 const char *rank_type,
1351 int num_bases, char **basenames)
1353 char term_dst[IT_MAX_WORD+1];
1354 const char *termp = termz;
1355 RSET rset[60], result;
1356 int i, r, rset_no = 0;
1357 struct grep_info grep_info;
1360 grep_info.term_no = 0;
1362 grep_info.isam_p_size = 0;
1363 grep_info.isam_p_buf = NULL;
1365 grep_info.reg_type = reg_type;
1369 logf (LOG_DEBUG, "APT_phrase termp=%s", termp);
1370 grep_info.isam_p_indx = 0;
1371 r = string_term (zh, zapt, &termp, attributeSet, &grep_info,
1372 reg_type, complete_flag, num_bases, basenames,
1376 logf (LOG_DEBUG, "term: %s", term_dst);
1377 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1378 grep_info.isam_p_indx, term_dst,
1379 strlen(term_dst), rank_type);
1380 assert (rset[rset_no]);
1381 if (++rset_no >= sizeof(rset)/sizeof(*rset))
1385 xfree(grep_info.term_no);
1387 xfree (grep_info.isam_p_buf);
1390 rset_null_parms parms;
1392 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1393 return rset_create (rset_kind_null, &parms);
1395 else if (rset_no == 1)
1397 result = rpn_prox (zh, rset, rset_no);
1398 for (i = 0; i<rset_no; i++)
1399 rset_delete (rset[i]);
1403 static RSET rpn_search_APT_or_list (ZebraHandle zh,
1404 Z_AttributesPlusTerm *zapt,
1406 oid_value attributeSet,
1407 int reg_type, int complete_flag,
1408 const char *rank_type,
1409 int num_bases, char **basenames)
1411 char term_dst[IT_MAX_WORD+1];
1412 const char *termp = termz;
1413 RSET rset[60], result;
1414 int i, r, rset_no = 0;
1415 struct grep_info grep_info;
1418 grep_info.term_no = 0;
1420 grep_info.isam_p_size = 0;
1421 grep_info.isam_p_buf = NULL;
1423 grep_info.reg_type = reg_type;
1427 logf (LOG_DEBUG, "APT_or_list termp=%s", termp);
1428 grep_info.isam_p_indx = 0;
1429 r = string_term (zh, zapt, &termp, attributeSet, &grep_info,
1430 reg_type, complete_flag, num_bases, basenames,
1434 logf (LOG_DEBUG, "term: %s", term_dst);
1435 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1436 grep_info.isam_p_indx, term_dst,
1437 strlen(term_dst), rank_type);
1438 assert (rset[rset_no]);
1439 if (++rset_no >= sizeof(rset)/sizeof(*rset))
1443 xfree(grep_info.term_no);
1445 xfree (grep_info.isam_p_buf);
1448 rset_null_parms parms;
1450 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1451 return rset_create (rset_kind_null, &parms);
1454 for (i = 1; i<rset_no; i++)
1456 rset_bool_parms bool_parms;
1458 bool_parms.rset_l = result;
1459 bool_parms.rset_r = rset[i];
1460 bool_parms.key_size = sizeof(struct it_key);
1461 bool_parms.cmp = key_compare_it;
1462 result = rset_create (rset_kind_or, &bool_parms);
1467 static RSET rpn_search_APT_and_list (ZebraHandle zh,
1468 Z_AttributesPlusTerm *zapt,
1470 oid_value attributeSet,
1471 int reg_type, int complete_flag,
1472 const char *rank_type,
1473 int num_bases, char **basenames)
1475 char term_dst[IT_MAX_WORD+1];
1476 const char *termp = termz;
1477 RSET rset[60], result;
1478 int i, r, rset_no = 0;
1479 struct grep_info grep_info;
1482 grep_info.term_no = 0;
1484 grep_info.isam_p_size = 0;
1485 grep_info.isam_p_buf = NULL;
1487 grep_info.reg_type = reg_type;
1491 logf (LOG_DEBUG, "APT_and_list termp=%s", termp);
1492 grep_info.isam_p_indx = 0;
1493 r = string_term (zh, zapt, &termp, attributeSet, &grep_info,
1494 reg_type, complete_flag, num_bases, basenames,
1498 logf (LOG_DEBUG, "term: %s", term_dst);
1499 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1500 grep_info.isam_p_indx, term_dst,
1501 strlen(term_dst), rank_type);
1502 assert (rset[rset_no]);
1503 if (++rset_no >= sizeof(rset)/sizeof(*rset))
1507 xfree(grep_info.term_no);
1509 xfree (grep_info.isam_p_buf);
1512 rset_null_parms parms;
1514 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1515 return rset_create (rset_kind_null, &parms);
1518 for (i = 1; i<rset_no; i++)
1520 rset_bool_parms bool_parms;
1522 bool_parms.rset_l = result;
1523 bool_parms.rset_r = rset[i];
1524 bool_parms.key_size = sizeof(struct it_key);
1525 bool_parms.cmp = key_compare_it;
1526 result = rset_create (rset_kind_and, &bool_parms);
1531 static int numeric_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1532 const char **term_sub,
1534 oid_value attributeSet,
1535 struct grep_info *grep_info,
1544 char *term_tmp = term_dict + strlen(term_dict);
1546 attr_init (&relation, zapt, 2);
1547 relation_value = attr_find (&relation, NULL);
1549 logf (LOG_DEBUG, "numeric relation value=%d", relation_value);
1551 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_tmp, 1,
1554 term_value = atoi (term_tmp);
1555 switch (relation_value)
1558 logf (LOG_DEBUG, "Relation <");
1559 gen_regular_rel (term_tmp, term_value-1, 1);
1562 logf (LOG_DEBUG, "Relation <=");
1563 gen_regular_rel (term_tmp, term_value, 1);
1566 logf (LOG_DEBUG, "Relation >=");
1567 gen_regular_rel (term_tmp, term_value, 0);
1570 logf (LOG_DEBUG, "Relation >");
1571 gen_regular_rel (term_tmp, term_value+1, 0);
1575 logf (LOG_DEBUG, "Relation =");
1576 sprintf (term_tmp, "(0*%d)", term_value);
1578 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_tmp);
1579 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info, max_pos,
1582 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1583 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1587 static int numeric_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1588 const char **term_sub,
1589 oid_value attributeSet, struct grep_info *grep_info,
1590 int reg_type, int complete_flag,
1591 int num_bases, char **basenames,
1594 char term_dict[2*IT_MAX_WORD+2];
1598 oid_value curAttributeSet = attributeSet;
1600 struct rpn_char_map_info rcmi;
1602 rpn_char_map_prepare (zh, reg_type, &rcmi);
1603 attr_init (&use, zapt, 1);
1604 use_value = attr_find (&use, &curAttributeSet);
1605 logf (LOG_DEBUG, "numeric_term, use value %d", use_value);
1607 if (use_value == -1)
1610 for (base_no = 0; base_no < num_bases; base_no++)
1613 data1_local_attribute *local_attr;
1614 int max_pos, prefix_len = 0;
1617 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
1619 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1620 curAttributeSet, use_value, r);
1627 if (zebraExplain_curDatabase (zh->zei, basenames[base_no]))
1629 zh->errCode = 109; /* Database unavailable */
1630 zh->errString = basenames[base_no];
1633 for (local_attr = attp.local_attributes; local_attr;
1634 local_attr = local_attr->next)
1640 ord = zebraExplain_lookupSU (zh->zei, attp.attset_ordinal,
1645 term_dict[prefix_len++] = '|';
1647 term_dict[prefix_len++] = '(';
1649 ord_len = key_SU_code (ord, ord_buf);
1650 for (i = 0; i<ord_len; i++)
1652 term_dict[prefix_len++] = 1;
1653 term_dict[prefix_len++] = ord_buf[i];
1661 term_dict[prefix_len++] = ')';
1662 term_dict[prefix_len++] = 1;
1663 term_dict[prefix_len++] = reg_type;
1664 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1665 term_dict[prefix_len] = '\0';
1666 if (!numeric_relation (zh, zapt, &termp, term_dict,
1667 attributeSet, grep_info, &max_pos, reg_type,
1672 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1676 static RSET rpn_search_APT_numeric (ZebraHandle zh,
1677 Z_AttributesPlusTerm *zapt,
1679 oid_value attributeSet,
1680 int reg_type, int complete_flag,
1681 const char *rank_type,
1682 int num_bases, char **basenames)
1684 char term_dst[IT_MAX_WORD+1];
1685 const char *termp = termz;
1686 RSET rset[60], result;
1687 int i, r, rset_no = 0;
1688 struct grep_info grep_info;
1691 grep_info.term_no = 0;
1693 grep_info.isam_p_size = 0;
1694 grep_info.isam_p_buf = NULL;
1696 grep_info.reg_type = reg_type;
1700 logf (LOG_DEBUG, "APT_numeric termp=%s", termp);
1701 grep_info.isam_p_indx = 0;
1702 r = numeric_term (zh, zapt, &termp, attributeSet, &grep_info,
1703 reg_type, complete_flag, num_bases, basenames,
1707 logf (LOG_DEBUG, "term: %s", term_dst);
1708 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1709 grep_info.isam_p_indx, term_dst,
1710 strlen(term_dst), rank_type);
1711 assert (rset[rset_no]);
1712 if (++rset_no >= sizeof(rset)/sizeof(*rset))
1716 xfree(grep_info.term_no);
1718 xfree (grep_info.isam_p_buf);
1721 rset_null_parms parms;
1723 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1724 return rset_create (rset_kind_null, &parms);
1727 for (i = 1; i<rset_no; i++)
1729 rset_bool_parms bool_parms;
1731 bool_parms.rset_l = result;
1732 bool_parms.rset_r = rset[i];
1733 bool_parms.key_size = sizeof(struct it_key);
1734 bool_parms.cmp = key_compare_it;
1735 result = rset_create (rset_kind_and, &bool_parms);
1740 static RSET rpn_search_APT_local (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1742 oid_value attributeSet,
1743 const char *rank_type)
1748 rset_temp_parms parms;
1750 parms.rset_term = rset_term_create (termz, -1, rank_type);
1751 parms.key_size = sizeof (struct it_key);
1752 parms.temp_path = res_get (zh->res, "setTmpDir");
1753 result = rset_create (rset_kind_temp, &parms);
1754 rsfd = rset_open (result, RSETF_WRITE);
1756 key.sysno = atoi (termz);
1760 rset_write (result, rsfd, &key);
1761 rset_close (result, rsfd);
1765 static RSET rpn_sort_spec (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1766 oid_value attributeSet, NMEM stream,
1767 Z_SortKeySpecList *sort_sequence,
1768 const char *rank_type)
1770 rset_null_parms parms;
1772 int sort_relation_value;
1773 AttrType sort_relation_type;
1778 Z_AttributeElement *ae;
1782 attr_init (&sort_relation_type, zapt, 7);
1783 sort_relation_value = attr_find (&sort_relation_type, &attributeSet);
1785 attr_init (&use_type, zapt, 1);
1786 use_value = attr_find (&use_type, &attributeSet);
1788 if (!sort_sequence->specs)
1790 sort_sequence->num_specs = 10;
1791 sort_sequence->specs = nmem_malloc (stream, sort_sequence->num_specs *
1792 sizeof(*sort_sequence->specs));
1793 for (i = 0; i<sort_sequence->num_specs; i++)
1794 sort_sequence->specs[i] = 0;
1796 if (zapt->term->which != Z_Term_general)
1799 i = atoi_n (zapt->term->u.general->buf, zapt->term->u.general->len);
1800 if (i >= sort_sequence->num_specs)
1803 oe.proto = PROTO_Z3950;
1804 oe.oclass = CLASS_ATTSET;
1805 oe.value = attributeSet;
1806 if (!oid_ent_to_oid (&oe, oid))
1809 sks = nmem_malloc (stream, sizeof(*sks));
1810 sks->sortElement = nmem_malloc (stream, sizeof(*sks->sortElement));
1811 sks->sortElement->which = Z_SortElement_generic;
1812 sk = sks->sortElement->u.generic = nmem_malloc (stream, sizeof(*sk));
1813 sk->which = Z_SortKey_sortAttributes;
1814 sk->u.sortAttributes = nmem_malloc (stream, sizeof(*sk->u.sortAttributes));
1816 sk->u.sortAttributes->id = oid;
1817 sk->u.sortAttributes->list =
1818 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list));
1819 sk->u.sortAttributes->list->num_attributes = 1;
1820 sk->u.sortAttributes->list->attributes =
1821 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list->attributes));
1822 ae = *sk->u.sortAttributes->list->attributes =
1823 nmem_malloc (stream, sizeof(**sk->u.sortAttributes->list->attributes));
1824 ae->attributeSet = 0;
1825 ae->attributeType = nmem_malloc (stream, sizeof(*ae->attributeType));
1826 *ae->attributeType = 1;
1827 ae->which = Z_AttributeValue_numeric;
1828 ae->value.numeric = nmem_malloc (stream, sizeof(*ae->value.numeric));
1829 *ae->value.numeric = use_value;
1831 sks->sortRelation = nmem_malloc (stream, sizeof(*sks->sortRelation));
1832 if (sort_relation_value == 1)
1833 *sks->sortRelation = Z_SortRelation_ascending;
1834 else if (sort_relation_value == 2)
1835 *sks->sortRelation = Z_SortRelation_descending;
1837 *sks->sortRelation = Z_SortRelation_ascending;
1839 sks->caseSensitivity = nmem_malloc (stream, sizeof(*sks->caseSensitivity));
1840 *sks->caseSensitivity = 0;
1842 sks->missingValueAction = 0;
1844 sort_sequence->specs[i] = sks;
1846 parms.rset_term = rset_term_create ("", -1, rank_type);
1847 return rset_create (rset_kind_null, &parms);
1851 static RSET rpn_search_APT (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1852 oid_value attributeSet, NMEM stream,
1853 Z_SortKeySpecList *sort_sequence,
1854 int num_bases, char **basenames)
1857 char *search_type = NULL;
1858 char *rank_type = NULL;
1861 char termz[IT_MAX_WORD+1];
1863 zebra_maps_attr (zh->zebra_maps, zapt, ®_id, &search_type,
1864 &rank_type, &complete_flag, &sort_flag);
1866 logf (LOG_DEBUG, "reg_id=%c", reg_id);
1867 logf (LOG_DEBUG, "complete_flag=%d", complete_flag);
1868 logf (LOG_DEBUG, "search_type=%s", search_type);
1869 logf (LOG_DEBUG, "rank_type=%s", rank_type);
1871 if (zapt->term->which != Z_Term_general)
1876 trans_term (zh, zapt, termz);
1879 return rpn_sort_spec (zh, zapt, attributeSet, stream, sort_sequence,
1882 if (!strcmp (search_type, "phrase"))
1884 return rpn_search_APT_phrase (zh, zapt, termz, attributeSet,
1885 reg_id, complete_flag, rank_type,
1886 num_bases, basenames);
1888 else if (!strcmp (search_type, "and-list"))
1890 return rpn_search_APT_and_list (zh, zapt, termz, attributeSet,
1891 reg_id, complete_flag, rank_type,
1892 num_bases, basenames);
1894 else if (!strcmp (search_type, "or-list"))
1896 return rpn_search_APT_or_list (zh, zapt, termz, attributeSet,
1897 reg_id, complete_flag, rank_type,
1898 num_bases, basenames);
1900 else if (!strcmp (search_type, "local"))
1902 return rpn_search_APT_local (zh, zapt, termz, attributeSet,
1905 else if (!strcmp (search_type, "numeric"))
1907 return rpn_search_APT_numeric (zh, zapt, termz, attributeSet,
1908 reg_id, complete_flag, rank_type,
1909 num_bases, basenames);
1915 static RSET rpn_search_structure (ZebraHandle zh, Z_RPNStructure *zs,
1916 oid_value attributeSet, NMEM stream,
1917 Z_SortKeySpecList *sort_sequence,
1918 int num_bases, char **basenames)
1921 if (zs->which == Z_RPNStructure_complex)
1923 Z_Operator *zop = zs->u.complex->roperator;
1924 rset_bool_parms bool_parms;
1926 bool_parms.rset_l = rpn_search_structure (zh, zs->u.complex->s1,
1927 attributeSet, stream,
1929 num_bases, basenames);
1930 if (bool_parms.rset_l == NULL)
1932 bool_parms.rset_r = rpn_search_structure (zh, zs->u.complex->s2,
1933 attributeSet, stream,
1935 num_bases, basenames);
1936 if (bool_parms.rset_r == NULL)
1938 rset_delete (bool_parms.rset_l);
1941 bool_parms.key_size = sizeof(struct it_key);
1942 bool_parms.cmp = key_compare_it;
1946 case Z_Operator_and:
1947 r = rset_create (rset_kind_and, &bool_parms);
1950 r = rset_create (rset_kind_or, &bool_parms);
1952 case Z_Operator_and_not:
1953 r = rset_create (rset_kind_not, &bool_parms);
1955 case Z_Operator_prox:
1957 if (zop->u.prox->which != Z_ProximityOperator_known)
1963 if (zop->u.prox->which != Z_ProxCode_known)
1971 if (*zop->u.prox->u.known != Z_ProxUnit_word)
1973 char *val = nmem_malloc (stream, 16);
1975 zh->errString = val;
1976 sprintf (val, "%d", *zop->u.prox->u.known);
1980 if (*zop->u.prox->proximityUnitCode != Z_ProxUnit_word)
1982 char *val = nmem_malloc (stream, 16);
1984 zh->errString = val;
1985 sprintf (val, "%d", *zop->u.prox->proximityUnitCode);
1989 r = rpn_proximity (zh, bool_parms.rset_l, bool_parms.rset_r,
1990 *zop->u.prox->ordered,
1991 (!zop->u.prox->exclusion ? 0 :
1992 *zop->u.prox->exclusion),
1993 *zop->u.prox->relationType,
1994 *zop->u.prox->distance);
2001 else if (zs->which == Z_RPNStructure_simple)
2003 if (zs->u.simple->which == Z_Operand_APT)
2005 logf (LOG_DEBUG, "rpn_search_APT");
2006 r = rpn_search_APT (zh, zs->u.simple->u.attributesPlusTerm,
2007 attributeSet, stream, sort_sequence,
2008 num_bases, basenames);
2010 else if (zs->u.simple->which == Z_Operand_resultSetId)
2012 logf (LOG_DEBUG, "rpn_search_ref");
2013 r = resultSetRef (zh, zs->u.simple->u.resultSetId);
2015 r = rset_create (rset_kind_null, NULL);
2032 RSET rpn_search (ZebraHandle zh, NMEM nmem,
2033 Z_RPNQuery *rpn, int num_bases, char **basenames,
2034 const char *setname,
2039 oid_value attributeSet;
2040 Z_SortKeySpecList *sort_sequence;
2044 zh->errString = NULL;
2047 sort_sequence = nmem_malloc (nmem, sizeof(*sort_sequence));
2048 sort_sequence->num_specs = 10;
2049 sort_sequence->specs = nmem_malloc (nmem, sort_sequence->num_specs *
2050 sizeof(*sort_sequence->specs));
2051 for (i = 0; i<sort_sequence->num_specs; i++)
2052 sort_sequence->specs[i] = 0;
2054 attrset = oid_getentbyoid (rpn->attributeSetId);
2055 attributeSet = attrset->value;
2056 rset = rpn_search_structure (zh, rpn->RPNStructure, attributeSet,
2057 nmem, sort_sequence, num_bases, basenames);
2062 logf (LOG_DEBUG, "search error: %d", zh->errCode);
2064 for (i = 0; sort_sequence->specs[i]; i++)
2066 sort_sequence->num_specs = i;
2068 resultSetRank (zh, sset, rset);
2071 logf (LOG_DEBUG, "resultSetSortSingle in rpn_search");
2072 resultSetSortSingle (zh, nmem, sset, rset,
2073 sort_sequence, &sort_status);
2076 logf (LOG_DEBUG, "resultSetSortSingle status = %d", zh->errCode);
2082 struct scan_info_entry {
2088 struct scan_info_entry *list;
2094 static int scan_handle (char *name, const char *info, int pos, void *client)
2096 int len_prefix, idx;
2097 struct scan_info *scan_info = client;
2099 len_prefix = strlen(scan_info->prefix);
2100 if (memcmp (name, scan_info->prefix, len_prefix))
2103 idx = scan_info->after - pos + scan_info->before;
2106 scan_info->list[idx].term = odr_malloc (scan_info->odr,
2107 strlen(name + len_prefix)+1);
2108 strcpy (scan_info->list[idx].term, name + len_prefix);
2109 assert (*info == sizeof(ISAM_P));
2110 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAM_P));
2114 static void scan_term_untrans (ZebraHandle zh, NMEM stream, int reg_type,
2115 char **dst, const char *src)
2117 char term_dst[1024];
2119 term_untrans (zh, reg_type, term_dst, src);
2121 *dst = nmem_malloc (stream, strlen(term_dst)+1);
2122 strcpy (*dst, term_dst);
2125 static void count_set (RSET r, int *count)
2133 logf (LOG_DEBUG, "count_set");
2136 rfd = rset_open (r, RSETF_READ);
2137 while (rset_read (r, rfd, &key, &term_index))
2139 if (key.sysno != psysno)
2146 rset_close (r, rfd);
2147 logf (LOG_DEBUG, "%d keys, %d records", kno, *count);
2150 void rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
2151 oid_value attributeset,
2152 int num_bases, char **basenames,
2153 int *position, int *num_entries, ZebraScanEntry **list,
2157 int pos = *position;
2158 int num = *num_entries;
2162 char termz[IT_MAX_WORD+20];
2165 struct scan_info *scan_info_array;
2166 ZebraScanEntry *glist;
2167 int ords[32], ord_no = 0;
2171 char *search_type = NULL;
2172 char *rank_type = NULL;
2176 if (attributeset == VAL_NONE)
2177 attributeset = VAL_BIB1;
2179 zlog_scan (zapt, attributeset);
2180 logf (LOG_DEBUG, "position = %d, num = %d", pos, num);
2182 attr_init (&use, zapt, 1);
2183 use_value = attr_find (&use, &attributeset);
2185 if (zebra_maps_attr (zh->zebra_maps, zapt, ®_id, &search_type,
2186 &rank_type, &complete_flag, &sort_flag))
2192 if (use_value == -1)
2194 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
2198 data1_local_attribute *local_attr;
2200 if ((r=att_getentbyatt (zh, &attp, attributeset, use_value)))
2202 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
2203 attributeset, use_value);
2209 if (zebraExplain_curDatabase (zh->zei, basenames[base_no]))
2211 zh->errString = basenames[base_no];
2212 zh->errCode = 109; /* Database unavailable */
2215 for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
2216 local_attr = local_attr->next)
2220 ord = zebraExplain_lookupSU (zh->zei, attp.attset_ordinal,
2223 ords[ord_no++] = ord;
2231 /* prepare dictionary scanning */
2234 scan_info_array = odr_malloc (stream, ord_no * sizeof(*scan_info_array));
2235 for (i = 0; i < ord_no; i++)
2237 int j, prefix_len = 0;
2238 int before_tmp = before, after_tmp = after;
2239 struct scan_info *scan_info = scan_info_array + i;
2240 struct rpn_char_map_info rcmi;
2242 rpn_char_map_prepare (zh, reg_id, &rcmi);
2244 scan_info->before = before;
2245 scan_info->after = after;
2246 scan_info->odr = stream;
2248 scan_info->list = odr_malloc (stream, (before+after)*
2249 sizeof(*scan_info->list));
2250 for (j = 0; j<before+after; j++)
2251 scan_info->list[j].term = NULL;
2253 prefix_len += key_SU_code (ords[i], termz + prefix_len);
2254 termz[prefix_len++] = reg_id;
2255 termz[prefix_len] = 0;
2256 strcpy (scan_info->prefix, termz);
2258 trans_scan_term (zh, zapt, termz+prefix_len, reg_id);
2260 dict_scan (zh->dict, termz, &before_tmp, &after_tmp, scan_info,
2263 glist = odr_malloc (stream, (before+after)*sizeof(*glist));
2265 /* consider terms after main term */
2266 for (i = 0; i < ord_no; i++)
2270 for (i = 0; i<after; i++)
2273 const char *mterm = NULL;
2277 for (j = 0; j < ord_no; j++)
2279 if (ptr[j] < before+after &&
2280 (tst=scan_info_array[j].list[ptr[j]].term) &&
2281 (!mterm || strcmp (tst, mterm) < 0))
2289 scan_term_untrans (zh, stream->mem, reg_id,
2290 &glist[i+before].term, mterm);
2291 rset = rset_trunc (zh, &scan_info_array[j0].list[ptr[j0]].isam_p, 1,
2292 glist[i+before].term, strlen(glist[i+before].term),
2296 for (j = j0+1; j<ord_no; j++)
2298 if (ptr[j] < before+after &&
2299 (tst=scan_info_array[j].list[ptr[j]].term) &&
2300 !strcmp (tst, mterm))
2302 rset_bool_parms bool_parms;
2306 rset_trunc (zh, &scan_info_array[j].list[ptr[j]].isam_p, 1,
2307 glist[i+before].term,
2308 strlen(glist[i+before].term), NULL);
2310 bool_parms.key_size = sizeof(struct it_key);
2311 bool_parms.cmp = key_compare_it;
2312 bool_parms.rset_l = rset;
2313 bool_parms.rset_r = rset2;
2315 rset = rset_create (rset_kind_or, &bool_parms);
2320 count_set (rset, &glist[i+before].occurrences);
2325 *num_entries -= (after-i);
2329 /* consider terms before main term */
2330 for (i = 0; i<ord_no; i++)
2333 for (i = 0; i<before; i++)
2336 const char *mterm = NULL;
2340 for (j = 0; j <ord_no; j++)
2342 if (ptr[j] < before &&
2343 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2344 (!mterm || strcmp (tst, mterm) > 0))
2353 scan_term_untrans (zh, stream->mem, reg_id,
2354 &glist[before-1-i].term, mterm);
2357 (zh, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1,
2358 glist[before-1-i].term, strlen(glist[before-1-i].term),
2363 for (j = j0+1; j<ord_no; j++)
2365 if (ptr[j] < before &&
2366 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2367 !strcmp (tst, mterm))
2369 rset_bool_parms bool_parms;
2372 rset2 = rset_trunc (zh,
2373 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1,
2374 glist[before-1-i].term,
2375 strlen(glist[before-1-i].term), NULL);
2377 bool_parms.key_size = sizeof(struct it_key);
2378 bool_parms.cmp = key_compare_it;
2379 bool_parms.rset_l = rset;
2380 bool_parms.rset_r = rset2;
2382 rset = rset_create (rset_kind_or, &bool_parms);
2387 count_set (rset, &glist[before-1-i].occurrences);
2397 *list = glist + i; /* list is set to first 'real' entry */
2399 logf (LOG_DEBUG, "position = %d, num_entries = %d",
2400 *position, *num_entries);
2402 logf (LOG_DEBUG, "scan error: %d", zh->errCode);