2 * Copyright (C) 1995-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.99 1999-12-23 09:03:32 adam
8 * Changed behaviour of trunc=105 so that * is regular .* and ! is regular .
10 * Revision 1.98 1999/11/30 13:48:04 adam
11 * Improved installation. Updated for inclusion of YAZ header files.
13 * Revision 1.97 1999/10/14 14:33:50 adam
14 * Added truncation 5=106.
16 * Revision 1.96 1999/09/23 10:05:05 adam
17 * Implemented structure=105 searching.
19 * Revision 1.95 1999/09/07 07:19:21 adam
20 * Work on character mapping. Implemented replace rules.
22 * Revision 1.94 1999/07/20 13:59:18 adam
23 * Fixed bug that occurred when phrases had 0 hits.
25 * Revision 1.93 1999/06/17 14:38:40 adam
26 * Bug fix: Scan SEGV'ed when getting unknown use attribute.
28 * Revision 1.92 1999/05/26 07:49:13 adam
31 * Revision 1.91 1999/02/02 14:51:13 adam
32 * Updated WIN32 code specific sections. Changed header.
34 * Revision 1.90 1998/11/16 16:03:43 adam
35 * Moved loggin utilities to Yaz. Was implemented in file zlogs.c.
37 * Revision 1.89 1998/11/16 10:11:55 adam
38 * Added addtional info for error 114 - unsupported use attribute.
40 * Revision 1.88 1998/10/18 07:54:52 adam
41 * Additional info added for diagnostics 114 (Unsupported use attribute) and
42 * 121 (Unsupported attribute set).
44 * Revision 1.87 1998/09/28 11:19:12 adam
45 * Fix for Compiled ASN.1.
47 * Revision 1.86 1998/09/22 10:48:20 adam
48 * Minor changes in search API.
50 * Revision 1.85 1998/09/22 10:03:43 adam
51 * Changed result sets to be persistent in the sense that they can
52 * be re-searched if needed.
53 * Fixed memory leak in rsm_or.
55 * Revision 1.84 1998/09/18 12:41:00 adam
56 * Fixed bug with numerical relations.
58 * Revision 1.83 1998/09/02 13:53:19 adam
59 * Extra parameter decode added to search routines to implement
62 * Revision 1.82 1998/06/26 11:16:40 quinn
63 * Added support (un-optimised) for left and left/right truncation
65 * Revision 1.81 1998/06/24 12:16:14 adam
66 * Support for relations on text operands. Open range support in
67 * DFA module (i.e. [-j], [g-]).
69 * Revision 1.80 1998/06/23 15:33:34 adam
70 * Added feature to specify sort criteria in query (type 7 specifies
73 * Revision 1.79 1998/06/22 11:35:09 adam
76 * Revision 1.78 1998/06/08 14:43:17 adam
77 * Added suport for EXPLAIN Proxy servers - added settings databasePath
78 * and explainDatabase to facilitate this. Increased maximum number
79 * of databases and attributes in one register.
81 * Revision 1.77 1998/05/20 10:12:22 adam
82 * Implemented automatic EXPLAIN database maintenance.
83 * Modified Zebra to work with ASN.1 compiled version of YAZ.
85 * Revision 1.76 1998/04/02 14:35:29 adam
86 * First version of Zebra that works with compiled ASN.1.
88 * Revision 1.75 1998/03/05 08:45:13 adam
89 * New result set model and modular ranking system. Moved towards
90 * descent server API. System information stored as "SGML" records.
92 * Revision 1.74 1998/02/10 12:03:06 adam
95 * Revision 1.73 1998/01/29 13:40:11 adam
96 * Better logging for scan service.
98 * Revision 1.72 1998/01/07 13:53:41 adam
99 * Queries using simple ranked operands returns right number of hits.
101 * Revision 1.71 1997/12/18 10:54:24 adam
102 * New method result set method rs_hits that returns the number of
103 * hits in result-set (if known). The ranked result set returns real
104 * number of hits but only when not combined with other operands.
106 * Revision 1.70 1997/10/31 12:34:43 adam
107 * Changed a few log statements.
109 * Revision 1.69 1997/10/29 12:05:02 adam
110 * Server produces diagnostic "Unsupported Attribute Set" when appropriate.
112 * Revision 1.68 1997/10/27 14:33:06 adam
113 * Moved towards generic character mapping depending on "structure"
114 * field in abstract syntax file. Fixed a few memory leaks. Fixed
115 * bug with negative integers when doing searches with relational
118 * Revision 1.67 1997/09/29 09:06:10 adam
119 * Removed one static var in order to make this module thread safe.
121 * Revision 1.66 1997/09/25 14:58:03 adam
124 * Revision 1.65 1997/09/22 12:39:06 adam
125 * Added get_pos method for the ranked result sets.
127 * Revision 1.64 1997/09/18 08:59:20 adam
128 * Extra generic handle for the character mapping routines.
130 * Revision 1.63 1997/09/17 12:19:18 adam
131 * Zebra version corresponds to YAZ version 1.4.
132 * Changed Zebra server so that it doesn't depend on global common_resource.
134 * Revision 1.62 1997/09/05 15:30:09 adam
135 * Changed prototype for chr_map_input - added const.
136 * Added support for C++, headers uses extern "C" for public definitions.
138 * Revision 1.61 1997/02/10 10:21:14 adam
139 * Bug fix: in search terms character (^) wasn't observed.
141 * Revision 1.60 1997/01/31 11:10:34 adam
142 * Bug fix: Leading and trailing white space weren't removed in scan tokens.
144 * Revision 1.59 1997/01/17 11:31:46 adam
145 * Bug fix: complete phrase search didn't work.
147 * Revision 1.58 1996/12/23 15:30:45 adam
148 * Work on truncation.
149 * Bug fix: result sets weren't deleted after server shut down.
151 * Revision 1.57 1996/11/11 13:38:02 adam
152 * Added proximity support in search.
154 * Revision 1.56 1996/11/08 11:10:32 adam
155 * Buffers used during file match got bigger.
156 * Compressed ISAM support everywhere.
157 * Bug fixes regarding masking characters in queries.
158 * Redesigned Regexp-2 queries.
160 * Revision 1.55 1996/11/04 14:07:44 adam
161 * Moved truncation code to trunc.c.
163 * Revision 1.54 1996/10/29 14:09:52 adam
164 * Use of cisam system - enabled if setting isamc is 1.
166 * Revision 1.53 1996/06/26 09:21:43 adam
167 * Bug fix: local attribute set wasn't obeyed in scan.
169 * Revision 1.52 1996/06/17 14:26:20 adam
170 * Function gen_regular_rel changed to handle negative numbers.
172 * Revision 1.51 1996/06/11 10:54:15 quinn
175 * Revision 1.50 1996/06/07 08:51:53 adam
176 * Bug fix: Character mapping was broken (introducued by last revision).
178 * Revision 1.49 1996/06/04 10:18:11 adam
179 * Search/scan uses character mapping module.
181 * Revision 1.48 1996/05/28 15:15:01 adam
182 * Bug fix: Didn't handle unknown database correctly.
184 * Revision 1.47 1996/05/15 18:36:28 adam
185 * Function trans_term transforms unsearchable characters to blanks.
187 * Revision 1.46 1996/05/15 11:57:56 adam
188 * Fixed bug introduced by set/field mapping in search operations.
190 * Revision 1.45 1996/05/14 11:34:00 adam
191 * Scan support in multiple registers/databases.
193 * Revision 1.44 1996/05/14 06:16:44 adam
194 * Compact use/set bytes used in search service.
196 * Revision 1.43 1996/05/09 09:54:43 adam
197 * Server supports maps from one logical attributes to a list of physical
199 * The extraction process doesn't make space consuming 'any' keys.
201 * Revision 1.42 1996/05/09 07:28:56 quinn
202 * Work towards phrases and multiple registers
204 * Revision 1.41 1996/03/20 09:36:43 adam
205 * Function dict_lookup_grep got extra parameter, init_pos, which marks
206 * from which position in pattern approximate pattern matching should occur.
207 * Approximate pattern matching is used in relevance=re-2.
209 * Revision 1.40 1996/02/02 13:44:44 adam
210 * The public dictionary functions simply use char instead of Dict_char
211 * to represent search strings. Dict_char is used internally only.
213 * Revision 1.39 1996/01/03 16:22:13 quinn
214 * operator->roperator
216 * Revision 1.38 1995/12/11 09:12:55 adam
217 * The rec_get function returns NULL if record doesn't exist - will
218 * happen in the server if the result set records have been deleted since
219 * the creation of the set (i.e. the search).
220 * The server saves a result temporarily if it is 'volatile', i.e. the
221 * set is register dependent.
223 * Revision 1.37 1995/12/06 15:05:28 adam
224 * More verbose in count_set.
226 * Revision 1.36 1995/12/06 12:41:27 adam
227 * New command 'stat' for the index program.
228 * Filenames can be read from stdin by specifying '-'.
229 * Bug fix/enhancement of the transformation from terms to regular
230 * expressons in the search engine.
232 * Revision 1.35 1995/11/27 09:29:00 adam
233 * Bug fixes regarding conversion to regular expressions.
235 * Revision 1.34 1995/11/16 17:00:56 adam
236 * Better logging of rpn query.
238 * Revision 1.33 1995/11/01 13:58:28 quinn
239 * Moving data1 to yaz/retrieval
241 * Revision 1.32 1995/10/27 14:00:11 adam
242 * Implemented detection of database availability.
244 * Revision 1.31 1995/10/17 18:02:10 adam
245 * New feature: databases. Implemented as prefix to words in dictionary.
247 * Revision 1.30 1995/10/16 09:32:38 adam
248 * More work on relational op.
250 * Revision 1.29 1995/10/13 16:01:49 adam
253 * Revision 1.28 1995/10/13 12:26:43 adam
254 * Optimization of truncation.
256 * Revision 1.27 1995/10/12 17:07:22 adam
259 * Revision 1.26 1995/10/12 12:40:54 adam
260 * Bug fixes in rpn_prox.
262 * Revision 1.25 1995/10/10 13:59:24 adam
263 * Function rset_open changed its wflag parameter to general flags.
265 * Revision 1.24 1995/10/09 16:18:37 adam
266 * Function dict_lookup_grep got extra client data parameter.
268 * Revision 1.23 1995/10/06 16:33:37 adam
269 * Use attribute mappings.
271 * Revision 1.22 1995/10/06 15:07:39 adam
272 * Structure 'local-number' handled.
274 * Revision 1.21 1995/10/06 13:52:06 adam
275 * Bug fixes. Handler may abort further scanning.
277 * Revision 1.20 1995/10/06 11:06:33 adam
278 * Scan entries include 'occurrences' now.
280 * Revision 1.19 1995/10/06 10:43:56 adam
281 * Scan added. 'occurrences' in scan entries not set yet.
283 * Revision 1.18 1995/10/04 16:57:20 adam
284 * Key input and merge sort in one pass.
286 * Revision 1.17 1995/10/04 12:55:17 adam
287 * Bug fix in ranked search. Use=Any keys inserted.
289 * Revision 1.16 1995/10/02 16:24:40 adam
290 * Use attribute actually used in search requests.
292 * Revision 1.15 1995/10/02 15:18:52 adam
293 * New member in recRetrieveCtrl: diagnostic.
295 * Revision 1.14 1995/09/28 12:10:32 adam
296 * Bug fixes. Field prefix used in queries.
298 * Revision 1.13 1995/09/18 14:17:50 adam
301 * Revision 1.12 1995/09/15 14:45:21 adam
303 * Work on truncation.
305 * Revision 1.11 1995/09/14 11:53:27 adam
306 * First work on regular expressions/truncations.
308 * Revision 1.10 1995/09/11 15:23:26 adam
309 * More work on relevance search.
311 * Revision 1.9 1995/09/11 13:09:35 adam
312 * More work on relevance feedback.
314 * Revision 1.8 1995/09/08 14:52:27 adam
315 * Minor changes. Dictionary is lower case now.
317 * Revision 1.7 1995/09/07 13:58:36 adam
318 * New parameter: result-set file descriptor (RSFD) to support multiple
319 * positions within the same result-set.
320 * Boolean operators: and, or, not implemented.
321 * Result-set references.
323 * Revision 1.6 1995/09/06 16:11:18 adam
324 * Option: only one word key per file.
326 * Revision 1.5 1995/09/06 10:33:04 adam
327 * More work on present. Some log messages removed.
329 * Revision 1.4 1995/09/05 15:28:40 adam
330 * More work on search engine.
332 * Revision 1.3 1995/09/04 15:20:22 adam
335 * Revision 1.2 1995/09/04 12:33:43 adam
336 * Various cleanup. YAZ util used instead.
338 * Revision 1.1 1995/09/04 09:10:40 adam
339 * More work on index add/del/update.
340 * Merge sort implemented.
341 * Initial work on z39 server.
360 struct rpn_char_map_info {
365 static const char **rpn_char_map_handler (void *vp, const char **from, int len)
367 struct rpn_char_map_info *p = (struct rpn_char_map_info *) vp;
368 return zebra_maps_input (p->zm, p->reg_type, from, len);
371 static void rpn_char_map_prepare (ZebraHandle zh, int reg_type,
372 struct rpn_char_map_info *map_info)
374 map_info->zm = zh->zebra_maps;
375 map_info->reg_type = reg_type;
376 dict_grep_cmap (zh->dict, map_info, rpn_char_map_handler);
383 Z_AttributesPlusTerm *zapt;
386 static int attr_find (AttrType *src, oid_value *attributeSetP)
391 num_attributes = src->zapt->attributes->num_attributes;
393 num_attributes = src->zapt->num_attributes;
395 while (src->major < num_attributes)
397 Z_AttributeElement *element;
400 element = src->zapt->attributes->attributes[src->major];
402 element = src->zapt->attributeList[src->major];
404 if (src->type == *element->attributeType)
406 switch (element->which)
408 case Z_AttributeValue_numeric:
410 if (element->attributeSet && attributeSetP)
414 attrset = oid_getentbyoid (element->attributeSet);
415 *attributeSetP = attrset->value;
417 return *element->value.numeric;
419 case Z_AttributeValue_complex:
420 if (src->minor >= element->value.complex->num_list ||
421 element->value.complex->list[src->minor]->which !=
422 Z_StringOrNumeric_numeric)
425 if (element->attributeSet && attributeSetP)
429 attrset = oid_getentbyoid (element->attributeSet);
430 *attributeSetP = attrset->value;
432 return *element->value.complex->list[src->minor-1]->u.numeric;
442 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
464 static void term_untrans (ZebraHandle zh, int reg_type,
465 char *dst, const char *src)
469 const char *cp = zebra_maps_output (zh->zebra_maps, reg_type, &src);
479 static void add_isam_p (const char *name, const char *info,
482 if (p->isam_p_indx == p->isam_p_size)
484 ISAMS_P *new_isam_p_buf;
488 p->isam_p_size = 2*p->isam_p_size + 100;
489 new_isam_p_buf = (ISAMS_P *) xmalloc (sizeof(*new_isam_p_buf) *
493 memcpy (new_isam_p_buf, p->isam_p_buf,
494 p->isam_p_indx * sizeof(*p->isam_p_buf));
495 xfree (p->isam_p_buf);
497 p->isam_p_buf = new_isam_p_buf;
500 new_term_no = (int *) xmalloc (sizeof(*new_term_no) *
504 memcpy (new_term_no, p->isam_p_buf,
505 p->isam_p_indx * sizeof(*p->term_no));
508 p->term_no = new_term_no;
511 assert (*info == sizeof(*p->isam_p_buf));
512 memcpy (p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
515 term_untrans (p->zh, p->reg_type, term_tmp, name+2);
516 logf (LOG_DEBUG, "grep: %s", term_tmp);
521 static int grep_handle (char *name, const char *info, void *p)
523 add_isam_p (name, info, (struct grep_info *) p);
527 static int term_pre (ZebraMaps zebra_maps, int reg_type, const char **src,
528 const char *ct1, const char *ct2)
530 const char *s1, *s0 = *src;
533 /* skip white space */
536 if (ct1 && strchr (ct1, *s0))
538 if (ct2 && strchr (ct2, *s0))
541 map = zebra_maps_input (zebra_maps, reg_type, &s1, strlen(s1));
542 if (**map != *CHR_SPACE)
550 /* term_100: handle term, where trunc=none (no operators at all) */
551 static int term_100 (ZebraMaps zebra_maps, int reg_type,
552 const char **src, char *dst, int space_split,
560 if (!term_pre (zebra_maps, reg_type, src, NULL, NULL))
566 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
567 if (space_split && **map == *CHR_SPACE)
571 if (!isalnum (*s1) && *s1 != '-')
583 /* term_101: handle term, where trunc=Process # */
584 static int term_101 (ZebraMaps zebra_maps, int reg_type,
585 const char **src, char *dst, int space_split,
593 if (!term_pre (zebra_maps, reg_type, src, "#", "#"))
602 dst_term[j++] = *s0++;
607 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
608 if (space_split && **map == *CHR_SPACE)
620 dst_term[j++] = '\0';
625 /* term_103: handle term, where trunc=re-2 (regular expressions) */
626 static int term_103 (ZebraMaps zebra_maps, int reg_type, const char **src,
627 char *dst, int *errors, int space_split,
635 if (!term_pre (zebra_maps, reg_type, src, "^\\()[].*+?|", "("))
638 if (errors && *s0 == '+' && s0[1] && s0[2] == '+' && s0[3] &&
641 *errors = s0[1] - '0';
648 if (strchr ("^\\()[].*+?|-", *s0))
656 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
657 if (**map == *CHR_SPACE)
674 /* term_103: handle term, where trunc=re-1 (regular expressions) */
675 static int term_102 (ZebraMaps zebra_maps, int reg_type, const char **src,
676 char *dst, int space_split, char *dst_term)
678 return term_103 (zebra_maps, reg_type, src, dst, NULL, space_split,
683 /* term_104: handle term, where trunc=Process # and ! */
684 static int term_104 (ZebraMaps zebra_maps, int reg_type,
685 const char **src, char *dst, int space_split,
693 if (!term_pre (zebra_maps, reg_type, src, "#!", "#!"))
702 dst_term[j++] = *s0++;
707 dst_term[j++] = *s0++;
711 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
712 if (space_split && **map == *CHR_SPACE)
724 dst_term[j++] = '\0';
729 /* term_105/106: handle term, where trunc=Process * and ! and right trunc */
730 static int term_105 (ZebraMaps zebra_maps, int reg_type,
731 const char **src, char *dst, int space_split,
732 char *dst_term, int right_truncate)
740 if (!term_pre (zebra_maps, reg_type, src, "*!", "*!"))
749 dst_term[j++] = *s0++;
754 dst_term[j++] = *s0++;
758 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
759 if (space_split && **map == *CHR_SPACE)
777 dst_term[j++] = '\0';
783 /* gen_regular_rel - generate regular expression from relation
784 * val: border value (inclusive)
785 * islt: 1 if <=; 0 if >=.
787 static void gen_regular_rel (char *dst, int val, int islt)
794 logf (LOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
798 strcpy (dst, "(-[0-9]+|(");
806 strcpy (dst, "([0-9]+|-(");
807 dst_p = strlen (dst);
817 dst_p = strlen (dst);
818 sprintf (numstr, "%d", val);
819 for (w = strlen(numstr); --w >= 0; pos++)
838 strcpy (dst + dst_p, numstr);
839 dst_p = strlen(dst) - pos - 1;
867 for (i = 0; i<pos; i++)
880 /* match everything less than 10^(pos-1) */
882 for (i=1; i<pos; i++)
883 strcat (dst, "[0-9]?");
887 /* match everything greater than 10^pos */
888 for (i = 0; i <= pos; i++)
889 strcat (dst, "[0-9]");
890 strcat (dst, "[0-9]*");
895 void string_rel_add_char (char **term_p, const char *src, int *indx)
897 if (src[*indx] == '\\')
898 *(*term_p)++ = src[(*indx)++];
899 *(*term_p)++ = src[(*indx)++];
903 * > abc ([b-].*|a[c-].*|ab[d-].*|abc.+)
904 * ([^-a].*|a[^-b].*ab[^-c].*|abc.+)
905 * >= abc ([b-].*|a[c-].*|ab[c-].*)
906 * ([^-a].*|a[^-b].*|ab[c-].*)
907 * < abc ([-0].*|a[-a].*|ab[-b].*)
908 * ([^a-].*|a[^b-].*|ab[^c-].*)
909 * <= abc ([-0].*|a[-a].*|ab[-b].*|abc)
910 * ([^a-].*|a[^b-].*|ab[^c-].*|abc)
912 static int string_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
913 const char **term_sub, char *term_dict,
914 oid_value attributeSet,
915 int reg_type, int space_split, char *term_dst)
920 char *term_tmp = term_dict + strlen(term_dict);
921 char term_component[256];
923 attr_init (&relation, zapt, 2);
924 relation_value = attr_find (&relation, NULL);
926 logf (LOG_DEBUG, "string relation value=%d", relation_value);
927 switch (relation_value)
930 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
931 space_split, term_dst))
933 logf (LOG_DEBUG, "Relation <");
936 for (i = 0; term_component[i]; )
943 string_rel_add_char (&term_tmp, term_component, &j);
948 string_rel_add_char (&term_tmp, term_component, &i);
959 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
960 space_split, term_dst))
962 logf (LOG_DEBUG, "Relation <=");
965 for (i = 0; term_component[i]; )
970 string_rel_add_char (&term_tmp, term_component, &j);
974 string_rel_add_char (&term_tmp, term_component, &i);
983 for (i = 0; term_component[i]; )
984 string_rel_add_char (&term_tmp, term_component, &i);
989 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
990 space_split, term_dst))
992 logf (LOG_DEBUG, "Relation >");
995 for (i = 0; term_component[i];)
1000 string_rel_add_char (&term_tmp, term_component, &j);
1005 string_rel_add_char (&term_tmp, term_component, &i);
1013 for (i = 0; term_component[i];)
1014 string_rel_add_char (&term_tmp, term_component, &i);
1021 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
1022 space_split, term_dst))
1024 logf (LOG_DEBUG, "Relation >=");
1027 for (i = 0; term_component[i];)
1034 string_rel_add_char (&term_tmp, term_component, &j);
1037 if (term_component[i+1])
1041 string_rel_add_char (&term_tmp, term_component, &i);
1045 string_rel_add_char (&term_tmp, term_component, &i);
1057 logf (LOG_DEBUG, "Relation =");
1058 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
1059 space_split, term_dst))
1061 strcat (term_tmp, "(");
1062 strcat (term_tmp, term_component);
1063 strcat (term_tmp, ")");
1068 static int string_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1069 const char **term_sub,
1070 oid_value attributeSet, NMEM stream,
1071 struct grep_info *grep_info,
1072 int reg_type, int complete_flag,
1073 int num_bases, char **basenames,
1076 char term_dict[2*IT_MAX_WORD+4000];
1078 AttrType truncation;
1079 int truncation_value;
1082 oid_value curAttributeSet = attributeSet;
1084 struct rpn_char_map_info rcmi;
1085 int space_split = complete_flag ? 0 : 1;
1087 rpn_char_map_prepare (zh, reg_type, &rcmi);
1088 attr_init (&use, zapt, 1);
1089 use_value = attr_find (&use, &curAttributeSet);
1090 logf (LOG_DEBUG, "string_term, use value %d", use_value);
1091 attr_init (&truncation, zapt, 5);
1092 truncation_value = attr_find (&truncation, NULL);
1093 logf (LOG_DEBUG, "truncation value %d", truncation_value);
1095 if (use_value == -1)
1098 for (base_no = 0; base_no < num_bases; base_no++)
1101 data1_local_attribute *local_attr;
1102 int max_pos, prefix_len = 0;
1105 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
1107 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1108 curAttributeSet, use_value, r);
1112 sprintf (val_str, "%d", use_value);
1114 zh->errString = nmem_strdup (stream, val_str);
1119 struct oident oident;
1121 oident.proto = PROTO_Z3950;
1122 oident.oclass = CLASS_ATTSET;
1123 oident.value = curAttributeSet;
1124 oid_ent_to_oid (&oident, oid);
1127 zh->errString = nmem_strdup (stream, oident.desc);
1131 if (zebraExplain_curDatabase (zh->zei, basenames[base_no]))
1133 zh->errCode = 109; /* Database unavailable */
1134 zh->errString = basenames[base_no];
1137 for (local_attr = attp.local_attributes; local_attr;
1138 local_attr = local_attr->next)
1144 ord = zebraExplain_lookupSU (zh->zei, attp.attset_ordinal,
1149 term_dict[prefix_len++] = '|';
1151 term_dict[prefix_len++] = '(';
1153 ord_len = key_SU_code (ord, ord_buf);
1154 for (i = 0; i<ord_len; i++)
1156 term_dict[prefix_len++] = 1;
1157 term_dict[prefix_len++] = ord_buf[i];
1163 sprintf (val_str, "%d", use_value);
1165 zh->errString = nmem_strdup (stream, val_str);
1168 term_dict[prefix_len++] = ')';
1169 term_dict[prefix_len++] = 1;
1170 term_dict[prefix_len++] = reg_type;
1171 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1172 term_dict[prefix_len] = '\0';
1174 switch (truncation_value)
1176 case -1: /* not specified */
1177 case 100: /* do not truncate */
1178 if (!string_relation (zh, zapt, &termp, term_dict,
1180 reg_type, space_split, term_dst))
1182 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_dict+prefix_len);
1183 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info, &max_pos,
1186 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1188 case 1: /* right truncation */
1189 term_dict[j++] = '(';
1190 if (!term_100 (zh->zebra_maps, reg_type,
1191 &termp, term_dict + j, space_split, term_dst))
1193 strcat (term_dict, ".*)");
1194 dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1195 &max_pos, 0, grep_handle);
1197 case 2: /* keft truncation */
1198 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1199 if (!term_100 (zh->zebra_maps, reg_type,
1200 &termp, term_dict + j, space_split, term_dst))
1202 strcat (term_dict, ")");
1203 dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1204 &max_pos, 0, grep_handle);
1206 case 3: /* left&right truncation */
1207 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1208 if (!term_100 (zh->zebra_maps, reg_type,
1209 &termp, term_dict + j, space_split, term_dst))
1211 strcat (term_dict, ".*)");
1212 dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1213 &max_pos, 0, grep_handle);
1217 case 101: /* process # in term */
1218 term_dict[j++] = '(';
1219 if (!term_101 (zh->zebra_maps, reg_type,
1220 &termp, term_dict + j, space_split, term_dst))
1222 strcat (term_dict, ")");
1223 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1224 &max_pos, 0, grep_handle);
1226 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d", r);
1228 case 102: /* Regexp-1 */
1229 term_dict[j++] = '(';
1230 if (!term_102 (zh->zebra_maps, reg_type,
1231 &termp, term_dict + j, space_split, term_dst))
1233 strcat (term_dict, ")");
1234 logf (LOG_DEBUG, "Regexp-1 tolerance=%d", r);
1235 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1236 &max_pos, 0, grep_handle);
1238 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
1241 case 103: /* Regexp-2 */
1243 term_dict[j++] = '(';
1244 if (!term_103 (zh->zebra_maps, reg_type,
1245 &termp, term_dict + j, &r, space_split, term_dst))
1247 strcat (term_dict, ")");
1248 logf (LOG_DEBUG, "Regexp-2 tolerance=%d", r);
1249 r = dict_lookup_grep (zh->dict, term_dict, r, grep_info,
1250 &max_pos, 2, grep_handle);
1252 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
1255 case 104: /* process # and ! in term */
1256 term_dict[j++] = '(';
1257 if (!term_104 (zh->zebra_maps, reg_type,
1258 &termp, term_dict + j, space_split, term_dst))
1260 strcat (term_dict, ")");
1261 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1262 &max_pos, 0, grep_handle);
1264 logf (LOG_WARN, "dict_lookup_grep err, trunc=#/!: %d", r);
1266 case 105: /* process * and ! in term */
1267 term_dict[j++] = '(';
1268 if (!term_105 (zh->zebra_maps, reg_type,
1269 &termp, term_dict + j, space_split, term_dst, 1))
1271 strcat (term_dict, ")");
1272 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1273 &max_pos, 0, grep_handle);
1275 logf (LOG_WARN, "dict_lookup_grep err, trunc=*/!: %d", r);
1277 case 106: /* process * and ! in term */
1278 term_dict[j++] = '(';
1279 if (!term_105 (zh->zebra_maps, reg_type,
1280 &termp, term_dict + j, space_split, term_dst, 0))
1282 strcat (term_dict, ")");
1283 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1284 &max_pos, 0, grep_handle);
1286 logf (LOG_WARN, "dict_lookup_grep err, trunc=*/!: %d", r);
1291 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1295 static void trans_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1299 Z_Term *term = zapt->term;
1301 sizez = term->u.general->len;
1302 if (sizez > IT_MAX_WORD-1)
1303 sizez = IT_MAX_WORD-1;
1304 memcpy (termz, term->u.general->buf, sizez);
1305 termz[sizez] = '\0';
1308 static void trans_scan_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1309 char *termz, int reg_type)
1311 Z_Term *term = zapt->term;
1313 const char *cp = (const char *) term->u.general->buf;
1314 const char *cp_end = cp + term->u.general->len;
1317 const char *space_map = NULL;
1320 while ((len = (cp_end - cp)) > 0)
1322 map = zebra_maps_input (zh->zebra_maps, reg_type, &cp, len);
1323 if (**map == *CHR_SPACE)
1328 for (src = space_map; *src; src++)
1331 for (src = *map; *src; src++)
1338 static RSET rpn_proximity (ZebraHandle zh, RSET rset1, RSET rset2,
1340 int exclusion, int relation, int distance)
1345 struct it_key buf1, buf2;
1348 rset_temp_parms parms;
1351 rsfd1 = rset_open (rset1, RSETF_READ);
1352 more1 = rset_read (rset1, rsfd1, &buf1, &term_index);
1354 rsfd2 = rset_open (rset2, RSETF_READ);
1355 more2 = rset_read (rset2, rsfd2, &buf2, &term_index);
1357 parms.key_size = sizeof (struct it_key);
1358 parms.temp_path = res_get (zh->res, "setTmpDir");
1359 result = rset_create (rset_kind_temp, &parms);
1360 rsfd_result = rset_open (result, RSETF_WRITE);
1362 logf (LOG_DEBUG, "rpn_proximity excl=%d ord=%d rel=%d dis=%d",
1363 exclusion, ordered, relation, distance);
1364 while (more1 && more2)
1366 int cmp = key_compare_it (&buf1, &buf2);
1368 more1 = rset_read (rset1, rsfd1, &buf1, &term_index);
1370 more2 = rset_read (rset2, rsfd2, &buf2, &term_index);
1373 int sysno = buf1.sysno;
1377 seqno[n++] = buf1.seqno;
1378 while ((more1 = rset_read (rset1, rsfd1, &buf1, &term_index)) &&
1379 sysno == buf1.sysno)
1381 seqno[n++] = buf1.seqno;
1384 for (i = 0; i<n; i++)
1386 int diff = buf2.seqno - seqno[i];
1387 int excl = exclusion;
1388 if (!ordered && diff < 0)
1393 if (diff < distance)
1397 if (diff <= distance)
1401 if (diff == distance)
1405 if (diff >= distance)
1409 if (diff > distance)
1413 if (diff != distance)
1418 rset_write (result, rsfd_result, &buf2);
1420 } while ((more2 = rset_read (rset2, rsfd2, &buf2, &term_index)) &&
1421 sysno == buf2.sysno);
1424 rset_close (result, rsfd_result);
1425 rset_close (rset1, rsfd1);
1426 rset_close (rset2, rsfd2);
1430 static RSET rpn_prox (ZebraHandle zh, RSET *rset, int rset_no)
1435 struct it_key **buf;
1437 char prox_term[1024];
1438 int length_prox_term = 0;
1439 int min_nn = 10000000;
1441 const char *flags = NULL;
1443 rsfd = (RSFD *) xmalloc (sizeof(*rsfd)*rset_no);
1444 more = (int *) xmalloc (sizeof(*more)*rset_no);
1445 buf = (struct it_key **) xmalloc (sizeof(*buf)*rset_no);
1448 for (i = 0; i<rset_no; i++)
1451 for (j = 0; j<rset[i]->no_rset_terms; j++)
1453 const char *nflags = rset[i]->rset_terms[j]->flags;
1454 char *term = rset[i]->rset_terms[j]->name;
1455 int lterm = strlen(term);
1456 if (lterm + length_prox_term < sizeof(prox_term)-1)
1458 if (length_prox_term)
1459 prox_term[length_prox_term++] = ' ';
1460 strcpy (prox_term + length_prox_term, term);
1461 length_prox_term += lterm;
1463 if (min_nn > rset[i]->rset_terms[j]->nn)
1464 min_nn = rset[i]->rset_terms[j]->nn;
1468 for (i = 0; i<rset_no; i++)
1470 buf[i] = (struct it_key *) xmalloc (sizeof(**buf));
1471 rsfd[i] = rset_open (rset[i], RSETF_READ);
1472 if (!(more[i] = rset_read (rset[i], rsfd[i], buf[i], &term_index)))
1477 rset_null_parms parms;
1481 rset_close (rset[i], rsfd[i]);
1485 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1487 parms.rset_term->nn = 0;
1488 result = rset_create (rset_kind_null, &parms);
1492 rset_temp_parms parms;
1495 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1497 parms.rset_term->nn = min_nn;
1498 parms.key_size = sizeof (struct it_key);
1499 parms.temp_path = res_get (zh->res, "setTmpDir");
1500 result = rset_create (rset_kind_temp, &parms);
1501 rsfd_result = rset_open (result, RSETF_WRITE);
1505 for (i = 1; i<rset_no; i++)
1514 cmp = key_compare_it (buf[i], buf[i-1]);
1517 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1518 buf[i-1], &term_index);
1523 if (buf[i-1]->seqno+1 != buf[i]->seqno)
1525 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1526 buf[i-1], &term_index);
1532 more[i] = rset_read (rset[i], rsfd[i], buf[i],
1539 rset_write (result, rsfd_result, buf[0]);
1540 more[0] = rset_read (*rset, *rsfd, *buf, &term_index);
1544 for (i = 0; i<rset_no; i++)
1546 rset_close (rset[i], rsfd[i]);
1549 rset_close (result, rsfd_result);
1558 char *normalize_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1559 const char *termz, NMEM stream, unsigned reg_id)
1562 AttrType truncation;
1563 int truncation_value;
1566 attr_init (&truncation, zapt, 5);
1567 truncation_value = attr_find (&truncation, NULL);
1569 switch (truncation_value)
1589 wrbuf = zebra_replace(zh->zebra_maps, reg_id, ex_list,
1590 termz, strlen(termz));
1592 return nmem_strdup(stream, termz);
1595 char *buf = (char*) nmem_malloc (stream, wrbuf_len(wrbuf)+1);
1596 memcpy (buf, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1597 buf[wrbuf_len(wrbuf)] = '\0';
1602 static RSET rpn_search_APT_phrase (ZebraHandle zh,
1603 Z_AttributesPlusTerm *zapt,
1604 const char *termz_org,
1605 oid_value attributeSet,
1607 int reg_type, int complete_flag,
1608 const char *rank_type,
1609 int num_bases, char **basenames)
1611 char term_dst[IT_MAX_WORD+1];
1612 RSET rset[60], result;
1613 int i, r, rset_no = 0;
1614 struct grep_info grep_info;
1615 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1616 const char *termp = termz;
1619 grep_info.term_no = 0;
1621 grep_info.isam_p_size = 0;
1622 grep_info.isam_p_buf = NULL;
1624 grep_info.reg_type = reg_type;
1628 logf (LOG_DEBUG, "APT_phrase termp=%s", termp);
1629 grep_info.isam_p_indx = 0;
1630 r = string_term (zh, zapt, &termp, attributeSet, stream, &grep_info,
1631 reg_type, complete_flag, num_bases, basenames,
1635 logf (LOG_DEBUG, "term: %s", term_dst);
1636 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1637 grep_info.isam_p_indx, term_dst,
1638 strlen(term_dst), rank_type);
1639 assert (rset[rset_no]);
1640 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1644 xfree(grep_info.term_no);
1646 xfree (grep_info.isam_p_buf);
1649 rset_null_parms parms;
1651 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1652 return rset_create (rset_kind_null, &parms);
1654 else if (rset_no == 1)
1656 result = rpn_prox (zh, rset, rset_no);
1657 for (i = 0; i<rset_no; i++)
1658 rset_delete (rset[i]);
1662 static RSET rpn_search_APT_or_list (ZebraHandle zh,
1663 Z_AttributesPlusTerm *zapt,
1664 const char *termz_org,
1665 oid_value attributeSet,
1667 int reg_type, int complete_flag,
1668 const char *rank_type,
1669 int num_bases, char **basenames)
1671 char term_dst[IT_MAX_WORD+1];
1672 RSET rset[60], result;
1673 int i, r, rset_no = 0;
1674 struct grep_info grep_info;
1675 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1676 const char *termp = termz;
1678 grep_info.term_no = 0;
1680 grep_info.isam_p_size = 0;
1681 grep_info.isam_p_buf = NULL;
1683 grep_info.reg_type = reg_type;
1687 logf (LOG_DEBUG, "APT_or_list termp=%s", termp);
1688 grep_info.isam_p_indx = 0;
1689 r = string_term (zh, zapt, &termp, attributeSet, stream, &grep_info,
1690 reg_type, complete_flag, num_bases, basenames,
1694 logf (LOG_DEBUG, "term: %s", term_dst);
1695 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1696 grep_info.isam_p_indx, term_dst,
1697 strlen(term_dst), rank_type);
1698 assert (rset[rset_no]);
1699 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1703 xfree(grep_info.term_no);
1705 xfree (grep_info.isam_p_buf);
1708 rset_null_parms parms;
1710 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1711 return rset_create (rset_kind_null, &parms);
1714 for (i = 1; i<rset_no; i++)
1716 rset_bool_parms bool_parms;
1718 bool_parms.rset_l = result;
1719 bool_parms.rset_r = rset[i];
1720 bool_parms.key_size = sizeof(struct it_key);
1721 bool_parms.cmp = key_compare_it;
1722 result = rset_create (rset_kind_or, &bool_parms);
1727 static RSET rpn_search_APT_and_list (ZebraHandle zh,
1728 Z_AttributesPlusTerm *zapt,
1729 const char *termz_org,
1730 oid_value attributeSet,
1732 int reg_type, int complete_flag,
1733 const char *rank_type,
1734 int num_bases, char **basenames)
1736 char term_dst[IT_MAX_WORD+1];
1737 RSET rset[60], result;
1738 int i, r, rset_no = 0;
1739 struct grep_info grep_info;
1740 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1741 const char *termp = termz;
1744 grep_info.term_no = 0;
1746 grep_info.isam_p_size = 0;
1747 grep_info.isam_p_buf = NULL;
1749 grep_info.reg_type = reg_type;
1753 logf (LOG_DEBUG, "APT_and_list termp=%s", termp);
1754 grep_info.isam_p_indx = 0;
1755 r = string_term (zh, zapt, &termp, attributeSet, stream, &grep_info,
1756 reg_type, complete_flag, num_bases, basenames,
1760 logf (LOG_DEBUG, "term: %s", term_dst);
1761 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1762 grep_info.isam_p_indx, term_dst,
1763 strlen(term_dst), rank_type);
1764 assert (rset[rset_no]);
1765 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1769 xfree(grep_info.term_no);
1771 xfree (grep_info.isam_p_buf);
1774 rset_null_parms parms;
1776 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1777 return rset_create (rset_kind_null, &parms);
1780 for (i = 1; i<rset_no; i++)
1782 rset_bool_parms bool_parms;
1784 bool_parms.rset_l = result;
1785 bool_parms.rset_r = rset[i];
1786 bool_parms.key_size = sizeof(struct it_key);
1787 bool_parms.cmp = key_compare_it;
1788 result = rset_create (rset_kind_and, &bool_parms);
1793 static int numeric_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1794 const char **term_sub,
1796 oid_value attributeSet,
1797 struct grep_info *grep_info,
1806 char *term_tmp = term_dict + strlen(term_dict);
1808 attr_init (&relation, zapt, 2);
1809 relation_value = attr_find (&relation, NULL);
1811 logf (LOG_DEBUG, "numeric relation value=%d", relation_value);
1813 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_tmp, 1,
1816 term_value = atoi (term_tmp);
1817 switch (relation_value)
1820 logf (LOG_DEBUG, "Relation <");
1821 gen_regular_rel (term_tmp, term_value-1, 1);
1824 logf (LOG_DEBUG, "Relation <=");
1825 gen_regular_rel (term_tmp, term_value, 1);
1828 logf (LOG_DEBUG, "Relation >=");
1829 gen_regular_rel (term_tmp, term_value, 0);
1832 logf (LOG_DEBUG, "Relation >");
1833 gen_regular_rel (term_tmp, term_value+1, 0);
1837 logf (LOG_DEBUG, "Relation =");
1838 sprintf (term_tmp, "(0*%d)", term_value);
1840 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_tmp);
1841 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info, max_pos,
1844 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1845 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1849 static int numeric_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1850 const char **term_sub,
1851 oid_value attributeSet, struct grep_info *grep_info,
1852 int reg_type, int complete_flag,
1853 int num_bases, char **basenames,
1856 char term_dict[2*IT_MAX_WORD+2];
1860 oid_value curAttributeSet = attributeSet;
1862 struct rpn_char_map_info rcmi;
1864 rpn_char_map_prepare (zh, reg_type, &rcmi);
1865 attr_init (&use, zapt, 1);
1866 use_value = attr_find (&use, &curAttributeSet);
1867 logf (LOG_DEBUG, "numeric_term, use value %d", use_value);
1869 if (use_value == -1)
1872 for (base_no = 0; base_no < num_bases; base_no++)
1875 data1_local_attribute *local_attr;
1876 int max_pos, prefix_len = 0;
1879 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
1881 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1882 curAttributeSet, use_value, r);
1889 if (zebraExplain_curDatabase (zh->zei, basenames[base_no]))
1891 zh->errCode = 109; /* Database unavailable */
1892 zh->errString = basenames[base_no];
1895 for (local_attr = attp.local_attributes; local_attr;
1896 local_attr = local_attr->next)
1902 ord = zebraExplain_lookupSU (zh->zei, attp.attset_ordinal,
1907 term_dict[prefix_len++] = '|';
1909 term_dict[prefix_len++] = '(';
1911 ord_len = key_SU_code (ord, ord_buf);
1912 for (i = 0; i<ord_len; i++)
1914 term_dict[prefix_len++] = 1;
1915 term_dict[prefix_len++] = ord_buf[i];
1923 term_dict[prefix_len++] = ')';
1924 term_dict[prefix_len++] = 1;
1925 term_dict[prefix_len++] = reg_type;
1926 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1927 term_dict[prefix_len] = '\0';
1928 if (!numeric_relation (zh, zapt, &termp, term_dict,
1929 attributeSet, grep_info, &max_pos, reg_type,
1934 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1938 static RSET rpn_search_APT_numeric (ZebraHandle zh,
1939 Z_AttributesPlusTerm *zapt,
1941 oid_value attributeSet,
1943 int reg_type, int complete_flag,
1944 const char *rank_type,
1945 int num_bases, char **basenames)
1947 char term_dst[IT_MAX_WORD+1];
1948 const char *termp = termz;
1949 RSET rset[60], result;
1950 int i, r, rset_no = 0;
1951 struct grep_info grep_info;
1954 grep_info.term_no = 0;
1956 grep_info.isam_p_size = 0;
1957 grep_info.isam_p_buf = NULL;
1959 grep_info.reg_type = reg_type;
1963 logf (LOG_DEBUG, "APT_numeric termp=%s", termp);
1964 grep_info.isam_p_indx = 0;
1965 r = numeric_term (zh, zapt, &termp, attributeSet, &grep_info,
1966 reg_type, complete_flag, num_bases, basenames,
1970 logf (LOG_DEBUG, "term: %s", term_dst);
1971 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1972 grep_info.isam_p_indx, term_dst,
1973 strlen(term_dst), rank_type);
1974 assert (rset[rset_no]);
1975 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1979 xfree(grep_info.term_no);
1981 xfree (grep_info.isam_p_buf);
1984 rset_null_parms parms;
1986 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1987 return rset_create (rset_kind_null, &parms);
1990 for (i = 1; i<rset_no; i++)
1992 rset_bool_parms bool_parms;
1994 bool_parms.rset_l = result;
1995 bool_parms.rset_r = rset[i];
1996 bool_parms.key_size = sizeof(struct it_key);
1997 bool_parms.cmp = key_compare_it;
1998 result = rset_create (rset_kind_and, &bool_parms);
2003 static RSET rpn_search_APT_local (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2005 oid_value attributeSet,
2007 const char *rank_type)
2012 rset_temp_parms parms;
2014 parms.rset_term = rset_term_create (termz, -1, rank_type);
2015 parms.key_size = sizeof (struct it_key);
2016 parms.temp_path = res_get (zh->res, "setTmpDir");
2017 result = rset_create (rset_kind_temp, &parms);
2018 rsfd = rset_open (result, RSETF_WRITE);
2020 key.sysno = atoi (termz);
2024 rset_write (result, rsfd, &key);
2025 rset_close (result, rsfd);
2029 static RSET rpn_sort_spec (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2030 oid_value attributeSet, NMEM stream,
2031 Z_SortKeySpecList *sort_sequence,
2032 const char *rank_type)
2034 rset_null_parms parms;
2036 int sort_relation_value;
2037 AttrType sort_relation_type;
2042 Z_AttributeElement *ae;
2046 attr_init (&sort_relation_type, zapt, 7);
2047 sort_relation_value = attr_find (&sort_relation_type, &attributeSet);
2049 attr_init (&use_type, zapt, 1);
2050 use_value = attr_find (&use_type, &attributeSet);
2052 if (!sort_sequence->specs)
2054 sort_sequence->num_specs = 10;
2055 sort_sequence->specs = (Z_SortKeySpec **)
2056 nmem_malloc (stream, sort_sequence->num_specs *
2057 sizeof(*sort_sequence->specs));
2058 for (i = 0; i<sort_sequence->num_specs; i++)
2059 sort_sequence->specs[i] = 0;
2061 if (zapt->term->which != Z_Term_general)
2064 i = atoi_n ((char *) zapt->term->u.general->buf,
2065 zapt->term->u.general->len);
2066 if (i >= sort_sequence->num_specs)
2069 oe.proto = PROTO_Z3950;
2070 oe.oclass = CLASS_ATTSET;
2071 oe.value = attributeSet;
2072 if (!oid_ent_to_oid (&oe, oid))
2075 sks = (Z_SortKeySpec *) nmem_malloc (stream, sizeof(*sks));
2076 sks->sortElement = (Z_SortElement *)
2077 nmem_malloc (stream, sizeof(*sks->sortElement));
2078 sks->sortElement->which = Z_SortElement_generic;
2079 sk = sks->sortElement->u.generic = (Z_SortKey *)
2080 nmem_malloc (stream, sizeof(*sk));
2081 sk->which = Z_SortKey_sortAttributes;
2082 sk->u.sortAttributes = (Z_SortAttributes *)
2083 nmem_malloc (stream, sizeof(*sk->u.sortAttributes));
2085 sk->u.sortAttributes->id = oid;
2086 sk->u.sortAttributes->list = (Z_AttributeList *)
2087 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list));
2088 sk->u.sortAttributes->list->num_attributes = 1;
2089 sk->u.sortAttributes->list->attributes = (Z_AttributeElement **)
2090 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list->attributes));
2091 ae = *sk->u.sortAttributes->list->attributes = (Z_AttributeElement *)
2092 nmem_malloc (stream, sizeof(**sk->u.sortAttributes->list->attributes));
2093 ae->attributeSet = 0;
2094 ae->attributeType = (int *)
2095 nmem_malloc (stream, sizeof(*ae->attributeType));
2096 *ae->attributeType = 1;
2097 ae->which = Z_AttributeValue_numeric;
2098 ae->value.numeric = (int *)
2099 nmem_malloc (stream, sizeof(*ae->value.numeric));
2100 *ae->value.numeric = use_value;
2102 sks->sortRelation = (int *)
2103 nmem_malloc (stream, sizeof(*sks->sortRelation));
2104 if (sort_relation_value == 1)
2105 *sks->sortRelation = Z_SortRelation_ascending;
2106 else if (sort_relation_value == 2)
2107 *sks->sortRelation = Z_SortRelation_descending;
2109 *sks->sortRelation = Z_SortRelation_ascending;
2111 sks->caseSensitivity = (int *)
2112 nmem_malloc (stream, sizeof(*sks->caseSensitivity));
2113 *sks->caseSensitivity = 0;
2116 sks->which = Z_SortKeySpec_null;
2117 sks->u.null = odr_nullval ();
2119 sks->missingValueAction = 0;
2122 sort_sequence->specs[i] = sks;
2124 parms.rset_term = rset_term_create ("", -1, rank_type);
2125 return rset_create (rset_kind_null, &parms);
2129 static RSET rpn_search_APT (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2130 oid_value attributeSet, NMEM stream,
2131 Z_SortKeySpecList *sort_sequence,
2132 int num_bases, char **basenames)
2135 char *search_type = NULL;
2136 char *rank_type = NULL;
2139 char termz[IT_MAX_WORD+1];
2141 zebra_maps_attr (zh->zebra_maps, zapt, ®_id, &search_type,
2142 &rank_type, &complete_flag, &sort_flag);
2144 logf (LOG_DEBUG, "reg_id=%c", reg_id);
2145 logf (LOG_DEBUG, "complete_flag=%d", complete_flag);
2146 logf (LOG_DEBUG, "search_type=%s", search_type);
2147 logf (LOG_DEBUG, "rank_type=%s", rank_type);
2149 if (zapt->term->which != Z_Term_general)
2154 trans_term (zh, zapt, termz);
2157 return rpn_sort_spec (zh, zapt, attributeSet, stream, sort_sequence,
2160 if (!strcmp (search_type, "phrase"))
2162 return rpn_search_APT_phrase (zh, zapt, termz, attributeSet, stream,
2163 reg_id, complete_flag, rank_type,
2164 num_bases, basenames);
2166 else if (!strcmp (search_type, "and-list"))
2168 return rpn_search_APT_and_list (zh, zapt, termz, attributeSet, stream,
2169 reg_id, complete_flag, rank_type,
2170 num_bases, basenames);
2172 else if (!strcmp (search_type, "or-list"))
2174 return rpn_search_APT_or_list (zh, zapt, termz, attributeSet, stream,
2175 reg_id, complete_flag, rank_type,
2176 num_bases, basenames);
2178 else if (!strcmp (search_type, "local"))
2180 return rpn_search_APT_local (zh, zapt, termz, attributeSet, stream,
2183 else if (!strcmp (search_type, "numeric"))
2185 return rpn_search_APT_numeric (zh, zapt, termz, attributeSet, stream,
2186 reg_id, complete_flag, rank_type,
2187 num_bases, basenames);
2193 static RSET rpn_search_structure (ZebraHandle zh, Z_RPNStructure *zs,
2194 oid_value attributeSet, NMEM stream,
2195 Z_SortKeySpecList *sort_sequence,
2196 int num_bases, char **basenames)
2199 if (zs->which == Z_RPNStructure_complex)
2201 Z_Operator *zop = zs->u.complex->roperator;
2202 rset_bool_parms bool_parms;
2204 bool_parms.rset_l = rpn_search_structure (zh, zs->u.complex->s1,
2205 attributeSet, stream,
2207 num_bases, basenames);
2208 if (bool_parms.rset_l == NULL)
2210 bool_parms.rset_r = rpn_search_structure (zh, zs->u.complex->s2,
2211 attributeSet, stream,
2213 num_bases, basenames);
2214 if (bool_parms.rset_r == NULL)
2216 rset_delete (bool_parms.rset_l);
2219 bool_parms.key_size = sizeof(struct it_key);
2220 bool_parms.cmp = key_compare_it;
2224 case Z_Operator_and:
2225 r = rset_create (rset_kind_and, &bool_parms);
2228 r = rset_create (rset_kind_or, &bool_parms);
2230 case Z_Operator_and_not:
2231 r = rset_create (rset_kind_not, &bool_parms);
2233 case Z_Operator_prox:
2235 if (zop->u.prox->which != Z_ProximityOperator_known)
2241 if (zop->u.prox->which != Z_ProxCode_known)
2249 if (*zop->u.prox->u.known != Z_ProxUnit_word)
2251 char *val = (char *) nmem_malloc (stream, 16);
2253 zh->errString = val;
2254 sprintf (val, "%d", *zop->u.prox->u.known);
2258 if (*zop->u.prox->proximityUnitCode != Z_ProxUnit_word)
2260 char *val = (char *) nmem_malloc (stream, 16);
2262 zh->errString = val;
2263 sprintf (val, "%d", *zop->u.prox->proximityUnitCode);
2267 r = rpn_proximity (zh, bool_parms.rset_l, bool_parms.rset_r,
2268 *zop->u.prox->ordered,
2269 (!zop->u.prox->exclusion ? 0 :
2270 *zop->u.prox->exclusion),
2271 *zop->u.prox->relationType,
2272 *zop->u.prox->distance);
2279 else if (zs->which == Z_RPNStructure_simple)
2281 if (zs->u.simple->which == Z_Operand_APT)
2283 logf (LOG_DEBUG, "rpn_search_APT");
2284 r = rpn_search_APT (zh, zs->u.simple->u.attributesPlusTerm,
2285 attributeSet, stream, sort_sequence,
2286 num_bases, basenames);
2288 else if (zs->u.simple->which == Z_Operand_resultSetId)
2290 logf (LOG_DEBUG, "rpn_search_ref");
2291 r = resultSetRef (zh, zs->u.simple->u.resultSetId);
2293 r = rset_create (rset_kind_null, NULL);
2310 RSET rpn_search (ZebraHandle zh, NMEM nmem,
2311 Z_RPNQuery *rpn, int num_bases, char **basenames,
2312 const char *setname,
2317 oid_value attributeSet;
2318 Z_SortKeySpecList *sort_sequence;
2322 zh->errString = NULL;
2325 sort_sequence = (Z_SortKeySpecList *)
2326 nmem_malloc (nmem, sizeof(*sort_sequence));
2327 sort_sequence->num_specs = 10;
2328 sort_sequence->specs = (Z_SortKeySpec **)
2329 nmem_malloc (nmem, sort_sequence->num_specs *
2330 sizeof(*sort_sequence->specs));
2331 for (i = 0; i<sort_sequence->num_specs; i++)
2332 sort_sequence->specs[i] = 0;
2334 attrset = oid_getentbyoid (rpn->attributeSetId);
2335 attributeSet = attrset->value;
2336 rset = rpn_search_structure (zh, rpn->RPNStructure, attributeSet,
2337 nmem, sort_sequence, num_bases, basenames);
2342 logf (LOG_DEBUG, "search error: %d", zh->errCode);
2344 for (i = 0; sort_sequence->specs[i]; i++)
2346 sort_sequence->num_specs = i;
2348 resultSetRank (zh, sset, rset);
2351 logf (LOG_DEBUG, "resultSetSortSingle in rpn_search");
2352 resultSetSortSingle (zh, nmem, sset, rset,
2353 sort_sequence, &sort_status);
2356 logf (LOG_DEBUG, "resultSetSortSingle status = %d", zh->errCode);
2362 struct scan_info_entry {
2368 struct scan_info_entry *list;
2374 static int scan_handle (char *name, const char *info, int pos, void *client)
2376 int len_prefix, idx;
2377 struct scan_info *scan_info = (struct scan_info *) client;
2379 len_prefix = strlen(scan_info->prefix);
2380 if (memcmp (name, scan_info->prefix, len_prefix))
2382 if (pos > 0) idx = scan_info->after - pos + scan_info->before;
2385 scan_info->list[idx].term = (char *)
2386 odr_malloc (scan_info->odr, strlen(name + len_prefix)+1);
2387 strcpy (scan_info->list[idx].term, name + len_prefix);
2388 assert (*info == sizeof(ISAMS_P));
2389 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAMS_P));
2393 static void scan_term_untrans (ZebraHandle zh, NMEM stream, int reg_type,
2394 char **dst, const char *src)
2396 char term_dst[1024];
2398 term_untrans (zh, reg_type, term_dst, src);
2400 *dst = (char *) nmem_malloc (stream, strlen(term_dst)+1);
2401 strcpy (*dst, term_dst);
2404 static void count_set (RSET r, int *count)
2412 logf (LOG_DEBUG, "count_set");
2415 rfd = rset_open (r, RSETF_READ);
2416 while (rset_read (r, rfd, &key, &term_index))
2418 if (key.sysno != psysno)
2425 rset_close (r, rfd);
2426 logf (LOG_DEBUG, "%d keys, %d records", kno, *count);
2429 void rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
2430 oid_value attributeset,
2431 int num_bases, char **basenames,
2432 int *position, int *num_entries, ZebraScanEntry **list,
2436 int pos = *position;
2437 int num = *num_entries;
2441 char termz[IT_MAX_WORD+20];
2444 struct scan_info *scan_info_array;
2445 ZebraScanEntry *glist;
2446 int ords[32], ord_no = 0;
2450 char *search_type = NULL;
2451 char *rank_type = NULL;
2456 if (attributeset == VAL_NONE)
2457 attributeset = VAL_BIB1;
2459 logf (LOG_DEBUG, "position = %d, num = %d", pos, num);
2461 attr_init (&use, zapt, 1);
2462 use_value = attr_find (&use, &attributeset);
2464 if (zebra_maps_attr (zh->zebra_maps, zapt, ®_id, &search_type,
2465 &rank_type, &complete_flag, &sort_flag))
2471 if (use_value == -1)
2473 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
2477 data1_local_attribute *local_attr;
2479 if ((r=att_getentbyatt (zh, &attp, attributeset, use_value)))
2481 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
2482 attributeset, use_value);
2490 if (zebraExplain_curDatabase (zh->zei, basenames[base_no]))
2492 zh->errString = basenames[base_no];
2493 zh->errCode = 109; /* Database unavailable */
2496 for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
2497 local_attr = local_attr->next)
2501 ord = zebraExplain_lookupSU (zh->zei, attp.attset_ordinal,
2504 ords[ord_no++] = ord;
2512 /* prepare dictionary scanning */
2515 scan_info_array = (struct scan_info *)
2516 odr_malloc (stream, ord_no * sizeof(*scan_info_array));
2517 for (i = 0; i < ord_no; i++)
2519 int j, prefix_len = 0;
2520 int before_tmp = before, after_tmp = after;
2521 struct scan_info *scan_info = scan_info_array + i;
2522 struct rpn_char_map_info rcmi;
2524 rpn_char_map_prepare (zh, reg_id, &rcmi);
2526 scan_info->before = before;
2527 scan_info->after = after;
2528 scan_info->odr = stream;
2530 scan_info->list = (struct scan_info_entry *)
2531 odr_malloc (stream, (before+after) * sizeof(*scan_info->list));
2532 for (j = 0; j<before+after; j++)
2533 scan_info->list[j].term = NULL;
2535 prefix_len += key_SU_code (ords[i], termz + prefix_len);
2536 termz[prefix_len++] = reg_id;
2537 termz[prefix_len] = 0;
2538 strcpy (scan_info->prefix, termz);
2540 trans_scan_term (zh, zapt, termz+prefix_len, reg_id);
2542 dict_scan (zh->dict, termz, &before_tmp, &after_tmp, scan_info,
2545 glist = (ZebraScanEntry *)
2546 odr_malloc (stream, (before+after)*sizeof(*glist));
2548 /* consider terms after main term */
2549 for (i = 0; i < ord_no; i++)
2553 for (i = 0; i<after; i++)
2556 const char *mterm = NULL;
2560 for (j = 0; j < ord_no; j++)
2562 if (ptr[j] < before+after &&
2563 (tst=scan_info_array[j].list[ptr[j]].term) &&
2564 (!mterm || strcmp (tst, mterm) < 0))
2572 scan_term_untrans (zh, stream->mem, reg_id,
2573 &glist[i+before].term, mterm);
2574 rset = rset_trunc (zh, &scan_info_array[j0].list[ptr[j0]].isam_p, 1,
2575 glist[i+before].term, strlen(glist[i+before].term),
2579 for (j = j0+1; j<ord_no; j++)
2581 if (ptr[j] < before+after &&
2582 (tst=scan_info_array[j].list[ptr[j]].term) &&
2583 !strcmp (tst, mterm))
2585 rset_bool_parms bool_parms;
2589 rset_trunc (zh, &scan_info_array[j].list[ptr[j]].isam_p, 1,
2590 glist[i+before].term,
2591 strlen(glist[i+before].term), NULL);
2593 bool_parms.key_size = sizeof(struct it_key);
2594 bool_parms.cmp = key_compare_it;
2595 bool_parms.rset_l = rset;
2596 bool_parms.rset_r = rset2;
2598 rset = rset_create (rset_kind_or, &bool_parms);
2603 count_set (rset, &glist[i+before].occurrences);
2608 *num_entries -= (after-i);
2612 /* consider terms before main term */
2613 for (i = 0; i<ord_no; i++)
2616 for (i = 0; i<before; i++)
2619 const char *mterm = NULL;
2623 for (j = 0; j <ord_no; j++)
2625 if (ptr[j] < before &&
2626 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2627 (!mterm || strcmp (tst, mterm) > 0))
2636 scan_term_untrans (zh, stream->mem, reg_id,
2637 &glist[before-1-i].term, mterm);
2640 (zh, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1,
2641 glist[before-1-i].term, strlen(glist[before-1-i].term),
2646 for (j = j0+1; j<ord_no; j++)
2648 if (ptr[j] < before &&
2649 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2650 !strcmp (tst, mterm))
2652 rset_bool_parms bool_parms;
2655 rset2 = rset_trunc (zh,
2656 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1,
2657 glist[before-1-i].term,
2658 strlen(glist[before-1-i].term), NULL);
2660 bool_parms.key_size = sizeof(struct it_key);
2661 bool_parms.cmp = key_compare_it;
2662 bool_parms.rset_l = rset;
2663 bool_parms.rset_r = rset2;
2665 rset = rset_create (rset_kind_or, &bool_parms);
2670 count_set (rset, &glist[before-1-i].occurrences);
2680 *list = glist + i; /* list is set to first 'real' entry */
2682 logf (LOG_DEBUG, "position = %d, num_entries = %d",
2683 *position, *num_entries);
2685 logf (LOG_DEBUG, "scan error: %d", zh->errCode);