2 * Copyright (C) 1995-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.101 2000-03-02 14:35:03 adam
8 * Fixed proximity handling.
10 * Revision 1.100 1999/12/28 15:48:12 adam
13 * Revision 1.99 1999/12/23 09:03:32 adam
14 * Changed behaviour of trunc=105 so that * is regular .* and ! is regular .
16 * Revision 1.98 1999/11/30 13:48:04 adam
17 * Improved installation. Updated for inclusion of YAZ header files.
19 * Revision 1.97 1999/10/14 14:33:50 adam
20 * Added truncation 5=106.
22 * Revision 1.96 1999/09/23 10:05:05 adam
23 * Implemented structure=105 searching.
25 * Revision 1.95 1999/09/07 07:19:21 adam
26 * Work on character mapping. Implemented replace rules.
28 * Revision 1.94 1999/07/20 13:59:18 adam
29 * Fixed bug that occurred when phrases had 0 hits.
31 * Revision 1.93 1999/06/17 14:38:40 adam
32 * Bug fix: Scan SEGV'ed when getting unknown use attribute.
34 * Revision 1.92 1999/05/26 07:49:13 adam
37 * Revision 1.91 1999/02/02 14:51:13 adam
38 * Updated WIN32 code specific sections. Changed header.
40 * Revision 1.90 1998/11/16 16:03:43 adam
41 * Moved loggin utilities to Yaz. Was implemented in file zlogs.c.
43 * Revision 1.89 1998/11/16 10:11:55 adam
44 * Added addtional info for error 114 - unsupported use attribute.
46 * Revision 1.88 1998/10/18 07:54:52 adam
47 * Additional info added for diagnostics 114 (Unsupported use attribute) and
48 * 121 (Unsupported attribute set).
50 * Revision 1.87 1998/09/28 11:19:12 adam
51 * Fix for Compiled ASN.1.
53 * Revision 1.86 1998/09/22 10:48:20 adam
54 * Minor changes in search API.
56 * Revision 1.85 1998/09/22 10:03:43 adam
57 * Changed result sets to be persistent in the sense that they can
58 * be re-searched if needed.
59 * Fixed memory leak in rsm_or.
61 * Revision 1.84 1998/09/18 12:41:00 adam
62 * Fixed bug with numerical relations.
64 * Revision 1.83 1998/09/02 13:53:19 adam
65 * Extra parameter decode added to search routines to implement
68 * Revision 1.82 1998/06/26 11:16:40 quinn
69 * Added support (un-optimised) for left and left/right truncation
71 * Revision 1.81 1998/06/24 12:16:14 adam
72 * Support for relations on text operands. Open range support in
73 * DFA module (i.e. [-j], [g-]).
75 * Revision 1.80 1998/06/23 15:33:34 adam
76 * Added feature to specify sort criteria in query (type 7 specifies
79 * Revision 1.79 1998/06/22 11:35:09 adam
82 * Revision 1.78 1998/06/08 14:43:17 adam
83 * Added suport for EXPLAIN Proxy servers - added settings databasePath
84 * and explainDatabase to facilitate this. Increased maximum number
85 * of databases and attributes in one register.
87 * Revision 1.77 1998/05/20 10:12:22 adam
88 * Implemented automatic EXPLAIN database maintenance.
89 * Modified Zebra to work with ASN.1 compiled version of YAZ.
91 * Revision 1.76 1998/04/02 14:35:29 adam
92 * First version of Zebra that works with compiled ASN.1.
94 * Revision 1.75 1998/03/05 08:45:13 adam
95 * New result set model and modular ranking system. Moved towards
96 * descent server API. System information stored as "SGML" records.
98 * Revision 1.74 1998/02/10 12:03:06 adam
101 * Revision 1.73 1998/01/29 13:40:11 adam
102 * Better logging for scan service.
104 * Revision 1.72 1998/01/07 13:53:41 adam
105 * Queries using simple ranked operands returns right number of hits.
107 * Revision 1.71 1997/12/18 10:54:24 adam
108 * New method result set method rs_hits that returns the number of
109 * hits in result-set (if known). The ranked result set returns real
110 * number of hits but only when not combined with other operands.
112 * Revision 1.70 1997/10/31 12:34:43 adam
113 * Changed a few log statements.
115 * Revision 1.69 1997/10/29 12:05:02 adam
116 * Server produces diagnostic "Unsupported Attribute Set" when appropriate.
118 * Revision 1.68 1997/10/27 14:33:06 adam
119 * Moved towards generic character mapping depending on "structure"
120 * field in abstract syntax file. Fixed a few memory leaks. Fixed
121 * bug with negative integers when doing searches with relational
124 * Revision 1.67 1997/09/29 09:06:10 adam
125 * Removed one static var in order to make this module thread safe.
127 * Revision 1.66 1997/09/25 14:58:03 adam
130 * Revision 1.65 1997/09/22 12:39:06 adam
131 * Added get_pos method for the ranked result sets.
133 * Revision 1.64 1997/09/18 08:59:20 adam
134 * Extra generic handle for the character mapping routines.
136 * Revision 1.63 1997/09/17 12:19:18 adam
137 * Zebra version corresponds to YAZ version 1.4.
138 * Changed Zebra server so that it doesn't depend on global common_resource.
140 * Revision 1.62 1997/09/05 15:30:09 adam
141 * Changed prototype for chr_map_input - added const.
142 * Added support for C++, headers uses extern "C" for public definitions.
144 * Revision 1.61 1997/02/10 10:21:14 adam
145 * Bug fix: in search terms character (^) wasn't observed.
147 * Revision 1.60 1997/01/31 11:10:34 adam
148 * Bug fix: Leading and trailing white space weren't removed in scan tokens.
150 * Revision 1.59 1997/01/17 11:31:46 adam
151 * Bug fix: complete phrase search didn't work.
153 * Revision 1.58 1996/12/23 15:30:45 adam
154 * Work on truncation.
155 * Bug fix: result sets weren't deleted after server shut down.
157 * Revision 1.57 1996/11/11 13:38:02 adam
158 * Added proximity support in search.
160 * Revision 1.56 1996/11/08 11:10:32 adam
161 * Buffers used during file match got bigger.
162 * Compressed ISAM support everywhere.
163 * Bug fixes regarding masking characters in queries.
164 * Redesigned Regexp-2 queries.
166 * Revision 1.55 1996/11/04 14:07:44 adam
167 * Moved truncation code to trunc.c.
169 * Revision 1.54 1996/10/29 14:09:52 adam
170 * Use of cisam system - enabled if setting isamc is 1.
172 * Revision 1.53 1996/06/26 09:21:43 adam
173 * Bug fix: local attribute set wasn't obeyed in scan.
175 * Revision 1.52 1996/06/17 14:26:20 adam
176 * Function gen_regular_rel changed to handle negative numbers.
178 * Revision 1.51 1996/06/11 10:54:15 quinn
181 * Revision 1.50 1996/06/07 08:51:53 adam
182 * Bug fix: Character mapping was broken (introducued by last revision).
184 * Revision 1.49 1996/06/04 10:18:11 adam
185 * Search/scan uses character mapping module.
187 * Revision 1.48 1996/05/28 15:15:01 adam
188 * Bug fix: Didn't handle unknown database correctly.
190 * Revision 1.47 1996/05/15 18:36:28 adam
191 * Function trans_term transforms unsearchable characters to blanks.
193 * Revision 1.46 1996/05/15 11:57:56 adam
194 * Fixed bug introduced by set/field mapping in search operations.
196 * Revision 1.45 1996/05/14 11:34:00 adam
197 * Scan support in multiple registers/databases.
199 * Revision 1.44 1996/05/14 06:16:44 adam
200 * Compact use/set bytes used in search service.
202 * Revision 1.43 1996/05/09 09:54:43 adam
203 * Server supports maps from one logical attributes to a list of physical
205 * The extraction process doesn't make space consuming 'any' keys.
207 * Revision 1.42 1996/05/09 07:28:56 quinn
208 * Work towards phrases and multiple registers
210 * Revision 1.41 1996/03/20 09:36:43 adam
211 * Function dict_lookup_grep got extra parameter, init_pos, which marks
212 * from which position in pattern approximate pattern matching should occur.
213 * Approximate pattern matching is used in relevance=re-2.
215 * Revision 1.40 1996/02/02 13:44:44 adam
216 * The public dictionary functions simply use char instead of Dict_char
217 * to represent search strings. Dict_char is used internally only.
219 * Revision 1.39 1996/01/03 16:22:13 quinn
220 * operator->roperator
222 * Revision 1.38 1995/12/11 09:12:55 adam
223 * The rec_get function returns NULL if record doesn't exist - will
224 * happen in the server if the result set records have been deleted since
225 * the creation of the set (i.e. the search).
226 * The server saves a result temporarily if it is 'volatile', i.e. the
227 * set is register dependent.
229 * Revision 1.37 1995/12/06 15:05:28 adam
230 * More verbose in count_set.
232 * Revision 1.36 1995/12/06 12:41:27 adam
233 * New command 'stat' for the index program.
234 * Filenames can be read from stdin by specifying '-'.
235 * Bug fix/enhancement of the transformation from terms to regular
236 * expressons in the search engine.
238 * Revision 1.35 1995/11/27 09:29:00 adam
239 * Bug fixes regarding conversion to regular expressions.
241 * Revision 1.34 1995/11/16 17:00:56 adam
242 * Better logging of rpn query.
244 * Revision 1.33 1995/11/01 13:58:28 quinn
245 * Moving data1 to yaz/retrieval
247 * Revision 1.32 1995/10/27 14:00:11 adam
248 * Implemented detection of database availability.
250 * Revision 1.31 1995/10/17 18:02:10 adam
251 * New feature: databases. Implemented as prefix to words in dictionary.
253 * Revision 1.30 1995/10/16 09:32:38 adam
254 * More work on relational op.
256 * Revision 1.29 1995/10/13 16:01:49 adam
259 * Revision 1.28 1995/10/13 12:26:43 adam
260 * Optimization of truncation.
262 * Revision 1.27 1995/10/12 17:07:22 adam
265 * Revision 1.26 1995/10/12 12:40:54 adam
266 * Bug fixes in rpn_prox.
268 * Revision 1.25 1995/10/10 13:59:24 adam
269 * Function rset_open changed its wflag parameter to general flags.
271 * Revision 1.24 1995/10/09 16:18:37 adam
272 * Function dict_lookup_grep got extra client data parameter.
274 * Revision 1.23 1995/10/06 16:33:37 adam
275 * Use attribute mappings.
277 * Revision 1.22 1995/10/06 15:07:39 adam
278 * Structure 'local-number' handled.
280 * Revision 1.21 1995/10/06 13:52:06 adam
281 * Bug fixes. Handler may abort further scanning.
283 * Revision 1.20 1995/10/06 11:06:33 adam
284 * Scan entries include 'occurrences' now.
286 * Revision 1.19 1995/10/06 10:43:56 adam
287 * Scan added. 'occurrences' in scan entries not set yet.
289 * Revision 1.18 1995/10/04 16:57:20 adam
290 * Key input and merge sort in one pass.
292 * Revision 1.17 1995/10/04 12:55:17 adam
293 * Bug fix in ranked search. Use=Any keys inserted.
295 * Revision 1.16 1995/10/02 16:24:40 adam
296 * Use attribute actually used in search requests.
298 * Revision 1.15 1995/10/02 15:18:52 adam
299 * New member in recRetrieveCtrl: diagnostic.
301 * Revision 1.14 1995/09/28 12:10:32 adam
302 * Bug fixes. Field prefix used in queries.
304 * Revision 1.13 1995/09/18 14:17:50 adam
307 * Revision 1.12 1995/09/15 14:45:21 adam
309 * Work on truncation.
311 * Revision 1.11 1995/09/14 11:53:27 adam
312 * First work on regular expressions/truncations.
314 * Revision 1.10 1995/09/11 15:23:26 adam
315 * More work on relevance search.
317 * Revision 1.9 1995/09/11 13:09:35 adam
318 * More work on relevance feedback.
320 * Revision 1.8 1995/09/08 14:52:27 adam
321 * Minor changes. Dictionary is lower case now.
323 * Revision 1.7 1995/09/07 13:58:36 adam
324 * New parameter: result-set file descriptor (RSFD) to support multiple
325 * positions within the same result-set.
326 * Boolean operators: and, or, not implemented.
327 * Result-set references.
329 * Revision 1.6 1995/09/06 16:11:18 adam
330 * Option: only one word key per file.
332 * Revision 1.5 1995/09/06 10:33:04 adam
333 * More work on present. Some log messages removed.
335 * Revision 1.4 1995/09/05 15:28:40 adam
336 * More work on search engine.
338 * Revision 1.3 1995/09/04 15:20:22 adam
341 * Revision 1.2 1995/09/04 12:33:43 adam
342 * Various cleanup. YAZ util used instead.
344 * Revision 1.1 1995/09/04 09:10:40 adam
345 * More work on index add/del/update.
346 * Merge sort implemented.
347 * Initial work on z39 server.
366 struct rpn_char_map_info {
371 static const char **rpn_char_map_handler (void *vp, const char **from, int len)
373 struct rpn_char_map_info *p = (struct rpn_char_map_info *) vp;
374 return zebra_maps_input (p->zm, p->reg_type, from, len);
377 static void rpn_char_map_prepare (ZebraHandle zh, int reg_type,
378 struct rpn_char_map_info *map_info)
380 map_info->zm = zh->zebra_maps;
381 map_info->reg_type = reg_type;
382 dict_grep_cmap (zh->dict, map_info, rpn_char_map_handler);
389 Z_AttributesPlusTerm *zapt;
392 static int attr_find (AttrType *src, oid_value *attributeSetP)
397 num_attributes = src->zapt->attributes->num_attributes;
399 num_attributes = src->zapt->num_attributes;
401 while (src->major < num_attributes)
403 Z_AttributeElement *element;
406 element = src->zapt->attributes->attributes[src->major];
408 element = src->zapt->attributeList[src->major];
410 if (src->type == *element->attributeType)
412 switch (element->which)
414 case Z_AttributeValue_numeric:
416 if (element->attributeSet && attributeSetP)
420 attrset = oid_getentbyoid (element->attributeSet);
421 *attributeSetP = attrset->value;
423 return *element->value.numeric;
425 case Z_AttributeValue_complex:
426 if (src->minor >= element->value.complex->num_list ||
427 element->value.complex->list[src->minor]->which !=
428 Z_StringOrNumeric_numeric)
431 if (element->attributeSet && attributeSetP)
435 attrset = oid_getentbyoid (element->attributeSet);
436 *attributeSetP = attrset->value;
438 return *element->value.complex->list[src->minor-1]->u.numeric;
448 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
470 static void term_untrans (ZebraHandle zh, int reg_type,
471 char *dst, const char *src)
475 const char *cp = zebra_maps_output (zh->zebra_maps, reg_type, &src);
485 static void add_isam_p (const char *name, const char *info,
488 if (p->isam_p_indx == p->isam_p_size)
490 ISAMS_P *new_isam_p_buf;
494 p->isam_p_size = 2*p->isam_p_size + 100;
495 new_isam_p_buf = (ISAMS_P *) xmalloc (sizeof(*new_isam_p_buf) *
499 memcpy (new_isam_p_buf, p->isam_p_buf,
500 p->isam_p_indx * sizeof(*p->isam_p_buf));
501 xfree (p->isam_p_buf);
503 p->isam_p_buf = new_isam_p_buf;
506 new_term_no = (int *) xmalloc (sizeof(*new_term_no) *
510 memcpy (new_term_no, p->isam_p_buf,
511 p->isam_p_indx * sizeof(*p->term_no));
514 p->term_no = new_term_no;
517 assert (*info == sizeof(*p->isam_p_buf));
518 memcpy (p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
521 term_untrans (p->zh, p->reg_type, term_tmp, name+2);
522 logf (LOG_DEBUG, "grep: %s", term_tmp);
527 static int grep_handle (char *name, const char *info, void *p)
529 add_isam_p (name, info, (struct grep_info *) p);
533 static int term_pre (ZebraMaps zebra_maps, int reg_type, const char **src,
534 const char *ct1, const char *ct2)
536 const char *s1, *s0 = *src;
539 /* skip white space */
542 if (ct1 && strchr (ct1, *s0))
544 if (ct2 && strchr (ct2, *s0))
547 map = zebra_maps_input (zebra_maps, reg_type, &s1, strlen(s1));
548 if (**map != *CHR_SPACE)
556 /* term_100: handle term, where trunc=none (no operators at all) */
557 static int term_100 (ZebraMaps zebra_maps, int reg_type,
558 const char **src, char *dst, int space_split,
566 if (!term_pre (zebra_maps, reg_type, src, NULL, NULL))
572 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
573 if (space_split && **map == *CHR_SPACE)
577 if (!isalnum (*s1) && *s1 != '-')
589 /* term_101: handle term, where trunc=Process # */
590 static int term_101 (ZebraMaps zebra_maps, int reg_type,
591 const char **src, char *dst, int space_split,
599 if (!term_pre (zebra_maps, reg_type, src, "#", "#"))
608 dst_term[j++] = *s0++;
613 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
614 if (space_split && **map == *CHR_SPACE)
626 dst_term[j++] = '\0';
631 /* term_103: handle term, where trunc=re-2 (regular expressions) */
632 static int term_103 (ZebraMaps zebra_maps, int reg_type, const char **src,
633 char *dst, int *errors, int space_split,
641 if (!term_pre (zebra_maps, reg_type, src, "^\\()[].*+?|", "("))
644 if (errors && *s0 == '+' && s0[1] && s0[2] == '+' && s0[3] &&
647 *errors = s0[1] - '0';
654 if (strchr ("^\\()[].*+?|-", *s0))
662 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
663 if (**map == *CHR_SPACE)
680 /* term_103: handle term, where trunc=re-1 (regular expressions) */
681 static int term_102 (ZebraMaps zebra_maps, int reg_type, const char **src,
682 char *dst, int space_split, char *dst_term)
684 return term_103 (zebra_maps, reg_type, src, dst, NULL, space_split,
689 /* term_104: handle term, where trunc=Process # and ! */
690 static int term_104 (ZebraMaps zebra_maps, int reg_type,
691 const char **src, char *dst, int space_split,
699 if (!term_pre (zebra_maps, reg_type, src, "#!", "#!"))
708 dst_term[j++] = *s0++;
713 dst_term[j++] = *s0++;
717 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
718 if (space_split && **map == *CHR_SPACE)
730 dst_term[j++] = '\0';
735 /* term_105/106: handle term, where trunc=Process * and ! and right trunc */
736 static int term_105 (ZebraMaps zebra_maps, int reg_type,
737 const char **src, char *dst, int space_split,
738 char *dst_term, int right_truncate)
745 if (!term_pre (zebra_maps, reg_type, src, "*!", "*!"))
754 dst_term[j++] = *s0++;
759 dst_term[j++] = *s0++;
763 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
764 if (space_split && **map == *CHR_SPACE)
782 dst_term[j++] = '\0';
788 /* gen_regular_rel - generate regular expression from relation
789 * val: border value (inclusive)
790 * islt: 1 if <=; 0 if >=.
792 static void gen_regular_rel (char *dst, int val, int islt)
799 logf (LOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
803 strcpy (dst, "(-[0-9]+|(");
811 strcpy (dst, "([0-9]+|-(");
812 dst_p = strlen (dst);
822 dst_p = strlen (dst);
823 sprintf (numstr, "%d", val);
824 for (w = strlen(numstr); --w >= 0; pos++)
843 strcpy (dst + dst_p, numstr);
844 dst_p = strlen(dst) - pos - 1;
872 for (i = 0; i<pos; i++)
885 /* match everything less than 10^(pos-1) */
887 for (i=1; i<pos; i++)
888 strcat (dst, "[0-9]?");
892 /* match everything greater than 10^pos */
893 for (i = 0; i <= pos; i++)
894 strcat (dst, "[0-9]");
895 strcat (dst, "[0-9]*");
900 void string_rel_add_char (char **term_p, const char *src, int *indx)
902 if (src[*indx] == '\\')
903 *(*term_p)++ = src[(*indx)++];
904 *(*term_p)++ = src[(*indx)++];
908 * > abc ([b-].*|a[c-].*|ab[d-].*|abc.+)
909 * ([^-a].*|a[^-b].*ab[^-c].*|abc.+)
910 * >= abc ([b-].*|a[c-].*|ab[c-].*)
911 * ([^-a].*|a[^-b].*|ab[c-].*)
912 * < abc ([-0].*|a[-a].*|ab[-b].*)
913 * ([^a-].*|a[^b-].*|ab[^c-].*)
914 * <= abc ([-0].*|a[-a].*|ab[-b].*|abc)
915 * ([^a-].*|a[^b-].*|ab[^c-].*|abc)
917 static int string_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
918 const char **term_sub, char *term_dict,
919 oid_value attributeSet,
920 int reg_type, int space_split, char *term_dst)
925 char *term_tmp = term_dict + strlen(term_dict);
926 char term_component[256];
928 attr_init (&relation, zapt, 2);
929 relation_value = attr_find (&relation, NULL);
931 logf (LOG_DEBUG, "string relation value=%d", relation_value);
932 switch (relation_value)
935 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
936 space_split, term_dst))
938 logf (LOG_DEBUG, "Relation <");
941 for (i = 0; term_component[i]; )
948 string_rel_add_char (&term_tmp, term_component, &j);
953 string_rel_add_char (&term_tmp, term_component, &i);
964 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
965 space_split, term_dst))
967 logf (LOG_DEBUG, "Relation <=");
970 for (i = 0; term_component[i]; )
975 string_rel_add_char (&term_tmp, term_component, &j);
979 string_rel_add_char (&term_tmp, term_component, &i);
988 for (i = 0; term_component[i]; )
989 string_rel_add_char (&term_tmp, term_component, &i);
994 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
995 space_split, term_dst))
997 logf (LOG_DEBUG, "Relation >");
1000 for (i = 0; term_component[i];)
1005 string_rel_add_char (&term_tmp, term_component, &j);
1010 string_rel_add_char (&term_tmp, term_component, &i);
1018 for (i = 0; term_component[i];)
1019 string_rel_add_char (&term_tmp, term_component, &i);
1026 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
1027 space_split, term_dst))
1029 logf (LOG_DEBUG, "Relation >=");
1032 for (i = 0; term_component[i];)
1039 string_rel_add_char (&term_tmp, term_component, &j);
1042 if (term_component[i+1])
1046 string_rel_add_char (&term_tmp, term_component, &i);
1050 string_rel_add_char (&term_tmp, term_component, &i);
1062 logf (LOG_DEBUG, "Relation =");
1063 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
1064 space_split, term_dst))
1066 strcat (term_tmp, "(");
1067 strcat (term_tmp, term_component);
1068 strcat (term_tmp, ")");
1073 static int string_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1074 const char **term_sub,
1075 oid_value attributeSet, NMEM stream,
1076 struct grep_info *grep_info,
1077 int reg_type, int complete_flag,
1078 int num_bases, char **basenames,
1081 char term_dict[2*IT_MAX_WORD+4000];
1083 AttrType truncation;
1084 int truncation_value;
1087 oid_value curAttributeSet = attributeSet;
1089 struct rpn_char_map_info rcmi;
1090 int space_split = complete_flag ? 0 : 1;
1092 rpn_char_map_prepare (zh, reg_type, &rcmi);
1093 attr_init (&use, zapt, 1);
1094 use_value = attr_find (&use, &curAttributeSet);
1095 logf (LOG_DEBUG, "string_term, use value %d", use_value);
1096 attr_init (&truncation, zapt, 5);
1097 truncation_value = attr_find (&truncation, NULL);
1098 logf (LOG_DEBUG, "truncation value %d", truncation_value);
1100 if (use_value == -1)
1103 for (base_no = 0; base_no < num_bases; base_no++)
1106 data1_local_attribute *local_attr;
1107 int max_pos, prefix_len = 0;
1110 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
1112 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1113 curAttributeSet, use_value, r);
1117 sprintf (val_str, "%d", use_value);
1119 zh->errString = nmem_strdup (stream, val_str);
1124 struct oident oident;
1126 oident.proto = PROTO_Z3950;
1127 oident.oclass = CLASS_ATTSET;
1128 oident.value = curAttributeSet;
1129 oid_ent_to_oid (&oident, oid);
1132 zh->errString = nmem_strdup (stream, oident.desc);
1136 if (zebraExplain_curDatabase (zh->zei, basenames[base_no]))
1138 zh->errCode = 109; /* Database unavailable */
1139 zh->errString = basenames[base_no];
1142 for (local_attr = attp.local_attributes; local_attr;
1143 local_attr = local_attr->next)
1149 ord = zebraExplain_lookupSU (zh->zei, attp.attset_ordinal,
1154 term_dict[prefix_len++] = '|';
1156 term_dict[prefix_len++] = '(';
1158 ord_len = key_SU_code (ord, ord_buf);
1159 for (i = 0; i<ord_len; i++)
1161 term_dict[prefix_len++] = 1;
1162 term_dict[prefix_len++] = ord_buf[i];
1168 sprintf (val_str, "%d", use_value);
1170 zh->errString = nmem_strdup (stream, val_str);
1173 term_dict[prefix_len++] = ')';
1174 term_dict[prefix_len++] = 1;
1175 term_dict[prefix_len++] = reg_type;
1176 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1177 term_dict[prefix_len] = '\0';
1179 switch (truncation_value)
1181 case -1: /* not specified */
1182 case 100: /* do not truncate */
1183 if (!string_relation (zh, zapt, &termp, term_dict,
1185 reg_type, space_split, term_dst))
1187 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_dict+prefix_len);
1188 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info, &max_pos,
1191 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1193 case 1: /* right truncation */
1194 term_dict[j++] = '(';
1195 if (!term_100 (zh->zebra_maps, reg_type,
1196 &termp, term_dict + j, space_split, term_dst))
1198 strcat (term_dict, ".*)");
1199 dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1200 &max_pos, 0, grep_handle);
1202 case 2: /* keft truncation */
1203 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1204 if (!term_100 (zh->zebra_maps, reg_type,
1205 &termp, term_dict + j, space_split, term_dst))
1207 strcat (term_dict, ")");
1208 dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1209 &max_pos, 0, grep_handle);
1211 case 3: /* left&right truncation */
1212 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1213 if (!term_100 (zh->zebra_maps, reg_type,
1214 &termp, term_dict + j, space_split, term_dst))
1216 strcat (term_dict, ".*)");
1217 dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1218 &max_pos, 0, grep_handle);
1222 case 101: /* process # in term */
1223 term_dict[j++] = '(';
1224 if (!term_101 (zh->zebra_maps, reg_type,
1225 &termp, term_dict + j, space_split, term_dst))
1227 strcat (term_dict, ")");
1228 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1229 &max_pos, 0, grep_handle);
1231 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d", r);
1233 case 102: /* Regexp-1 */
1234 term_dict[j++] = '(';
1235 if (!term_102 (zh->zebra_maps, reg_type,
1236 &termp, term_dict + j, space_split, term_dst))
1238 strcat (term_dict, ")");
1239 logf (LOG_DEBUG, "Regexp-1 tolerance=%d", r);
1240 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1241 &max_pos, 0, grep_handle);
1243 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
1246 case 103: /* Regexp-2 */
1248 term_dict[j++] = '(';
1249 if (!term_103 (zh->zebra_maps, reg_type,
1250 &termp, term_dict + j, &r, space_split, term_dst))
1252 strcat (term_dict, ")");
1253 logf (LOG_DEBUG, "Regexp-2 tolerance=%d", r);
1254 r = dict_lookup_grep (zh->dict, term_dict, r, grep_info,
1255 &max_pos, 2, grep_handle);
1257 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
1260 case 104: /* process # and ! in term */
1261 term_dict[j++] = '(';
1262 if (!term_104 (zh->zebra_maps, reg_type,
1263 &termp, term_dict + j, space_split, term_dst))
1265 strcat (term_dict, ")");
1266 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1267 &max_pos, 0, grep_handle);
1269 logf (LOG_WARN, "dict_lookup_grep err, trunc=#/!: %d", r);
1271 case 105: /* process * and ! in term */
1272 term_dict[j++] = '(';
1273 if (!term_105 (zh->zebra_maps, reg_type,
1274 &termp, term_dict + j, space_split, term_dst, 1))
1276 strcat (term_dict, ")");
1277 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1278 &max_pos, 0, grep_handle);
1280 logf (LOG_WARN, "dict_lookup_grep err, trunc=*/!: %d", r);
1282 case 106: /* process * and ! in term */
1283 term_dict[j++] = '(';
1284 if (!term_105 (zh->zebra_maps, reg_type,
1285 &termp, term_dict + j, space_split, term_dst, 0))
1287 strcat (term_dict, ")");
1288 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1289 &max_pos, 0, grep_handle);
1291 logf (LOG_WARN, "dict_lookup_grep err, trunc=*/!: %d", r);
1296 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1300 static void trans_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1304 Z_Term *term = zapt->term;
1306 sizez = term->u.general->len;
1307 if (sizez > IT_MAX_WORD-1)
1308 sizez = IT_MAX_WORD-1;
1309 memcpy (termz, term->u.general->buf, sizez);
1310 termz[sizez] = '\0';
1313 static void trans_scan_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1314 char *termz, int reg_type)
1316 Z_Term *term = zapt->term;
1318 const char *cp = (const char *) term->u.general->buf;
1319 const char *cp_end = cp + term->u.general->len;
1322 const char *space_map = NULL;
1325 while ((len = (cp_end - cp)) > 0)
1327 map = zebra_maps_input (zh->zebra_maps, reg_type, &cp, len);
1328 if (**map == *CHR_SPACE)
1333 for (src = space_map; *src; src++)
1336 for (src = *map; *src; src++)
1343 static RSET rpn_prox (ZebraHandle zh, RSET *rset, int rset_no,
1344 int ordered, int exclusion, int relation, int distance)
1349 struct it_key **buf;
1351 char prox_term[1024];
1352 int length_prox_term = 0;
1353 int min_nn = 10000000;
1355 const char *flags = NULL;
1357 rsfd = (RSFD *) xmalloc (sizeof(*rsfd)*rset_no);
1358 more = (int *) xmalloc (sizeof(*more)*rset_no);
1359 buf = (struct it_key **) xmalloc (sizeof(*buf)*rset_no);
1362 for (i = 0; i<rset_no; i++)
1365 for (j = 0; j<rset[i]->no_rset_terms; j++)
1367 const char *nflags = rset[i]->rset_terms[j]->flags;
1368 char *term = rset[i]->rset_terms[j]->name;
1369 int lterm = strlen(term);
1370 if (lterm + length_prox_term < sizeof(prox_term)-1)
1372 if (length_prox_term)
1373 prox_term[length_prox_term++] = ' ';
1374 strcpy (prox_term + length_prox_term, term);
1375 length_prox_term += lterm;
1377 if (min_nn > rset[i]->rset_terms[j]->nn)
1378 min_nn = rset[i]->rset_terms[j]->nn;
1382 for (i = 0; i<rset_no; i++)
1387 for (i = 0; i<rset_no; i++)
1389 buf[i] = (struct it_key *) xmalloc (sizeof(**buf));
1390 rsfd[i] = rset_open (rset[i], RSETF_READ);
1391 if (!(more[i] = rset_read (rset[i], rsfd[i], buf[i], &term_index)))
1396 /* at least one is empty ... return null set */
1397 rset_null_parms parms;
1399 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1401 parms.rset_term->nn = 0;
1402 result = rset_create (rset_kind_null, &parms);
1404 else if (ordered && relation == 3 && exclusion == 0 && distance == 1)
1406 /* special proximity case = phrase search ... */
1407 rset_temp_parms parms;
1410 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1412 parms.rset_term->nn = min_nn;
1413 parms.key_size = sizeof (struct it_key);
1414 parms.temp_path = res_get (zh->res, "setTmpDir");
1415 result = rset_create (rset_kind_temp, &parms);
1416 rsfd_result = rset_open (result, RSETF_WRITE);
1420 for (i = 1; i<rset_no; i++)
1429 cmp = key_compare_it (buf[i], buf[i-1]);
1432 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1433 buf[i-1], &term_index);
1438 if (buf[i-1]->seqno+1 != buf[i]->seqno)
1440 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1441 buf[i-1], &term_index);
1447 more[i] = rset_read (rset[i], rsfd[i], buf[i],
1454 rset_write (result, rsfd_result, buf[0]);
1455 more[0] = rset_read (*rset, *rsfd, *buf, &term_index);
1458 rset_close (result, rsfd_result);
1460 else if (rset_no == 2)
1462 /* generic proximity case (two input sets only) ... */
1463 rset_temp_parms parms;
1466 logf (LOG_LOG, "generic prox, dist = %d, relation = %d, ordered =%d, exclusion=%d",
1467 distance, relation, ordered, exclusion);
1468 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1470 parms.rset_term->nn = min_nn;
1471 parms.key_size = sizeof (struct it_key);
1472 parms.temp_path = res_get (zh->res, "setTmpDir");
1473 result = rset_create (rset_kind_temp, &parms);
1474 rsfd_result = rset_open (result, RSETF_WRITE);
1476 while (more[0] && more[1])
1478 int cmp = key_compare_it (buf[0], buf[1]);
1480 more[0] = rset_read (rset[0], rsfd[0], buf[0], &term_index);
1482 more[1] = rset_read (rset[1], rsfd[1], buf[1], &term_index);
1485 int sysno = buf[0]->sysno;
1489 seqno[n++] = buf[0]->seqno;
1490 while ((more[0] = rset_read (rset[0], rsfd[0], buf[0],
1492 sysno == buf[0]->sysno)
1494 seqno[n++] = buf[0]->seqno;
1497 for (i = 0; i<n; i++)
1499 int diff = buf[1]->seqno - seqno[i];
1500 int excl = exclusion;
1501 if (!ordered && diff < 0)
1506 if (diff < distance && diff >= 0)
1510 if (diff <= distance && diff >= 0)
1514 if (diff == distance && diff >= 0)
1518 if (diff >= distance && diff >= 0)
1522 if (diff > distance && diff >= 0)
1526 if (diff != distance && diff >= 0)
1532 rset_write (result, rsfd_result, buf[1]);
1536 } while ((more[1] = rset_read (rset[1], rsfd[1], buf[1],
1538 sysno == buf[1]->sysno);
1541 rset_close (result, rsfd_result);
1545 rset_null_parms parms;
1547 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1549 parms.rset_term->nn = 0;
1550 result = rset_create (rset_kind_null, &parms);
1552 for (i = 0; i<rset_no; i++)
1555 rset_close (rset[i], rsfd[i]);
1565 char *normalize_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1566 const char *termz, NMEM stream, unsigned reg_id)
1569 AttrType truncation;
1570 int truncation_value;
1573 attr_init (&truncation, zapt, 5);
1574 truncation_value = attr_find (&truncation, NULL);
1576 switch (truncation_value)
1596 wrbuf = zebra_replace(zh->zebra_maps, reg_id, ex_list,
1597 termz, strlen(termz));
1599 return nmem_strdup(stream, termz);
1602 char *buf = (char*) nmem_malloc (stream, wrbuf_len(wrbuf)+1);
1603 memcpy (buf, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1604 buf[wrbuf_len(wrbuf)] = '\0';
1609 static RSET rpn_search_APT_phrase (ZebraHandle zh,
1610 Z_AttributesPlusTerm *zapt,
1611 const char *termz_org,
1612 oid_value attributeSet,
1614 int reg_type, int complete_flag,
1615 const char *rank_type,
1616 int num_bases, char **basenames)
1618 char term_dst[IT_MAX_WORD+1];
1619 RSET rset[60], result;
1620 int i, r, rset_no = 0;
1621 struct grep_info grep_info;
1622 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1623 const char *termp = termz;
1626 grep_info.term_no = 0;
1628 grep_info.isam_p_size = 0;
1629 grep_info.isam_p_buf = NULL;
1631 grep_info.reg_type = reg_type;
1635 logf (LOG_DEBUG, "APT_phrase termp=%s", termp);
1636 grep_info.isam_p_indx = 0;
1637 r = string_term (zh, zapt, &termp, attributeSet, stream, &grep_info,
1638 reg_type, complete_flag, num_bases, basenames,
1642 logf (LOG_DEBUG, "term: %s", term_dst);
1643 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1644 grep_info.isam_p_indx, term_dst,
1645 strlen(term_dst), rank_type);
1646 assert (rset[rset_no]);
1647 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1651 xfree(grep_info.term_no);
1653 xfree (grep_info.isam_p_buf);
1656 rset_null_parms parms;
1658 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1659 return rset_create (rset_kind_null, &parms);
1661 else if (rset_no == 1)
1663 result = rpn_prox (zh, rset, rset_no, 1, 0, 3, 1);
1664 for (i = 0; i<rset_no; i++)
1665 rset_delete (rset[i]);
1669 static RSET rpn_search_APT_or_list (ZebraHandle zh,
1670 Z_AttributesPlusTerm *zapt,
1671 const char *termz_org,
1672 oid_value attributeSet,
1674 int reg_type, int complete_flag,
1675 const char *rank_type,
1676 int num_bases, char **basenames)
1678 char term_dst[IT_MAX_WORD+1];
1679 RSET rset[60], result;
1680 int i, r, rset_no = 0;
1681 struct grep_info grep_info;
1682 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1683 const char *termp = termz;
1685 grep_info.term_no = 0;
1687 grep_info.isam_p_size = 0;
1688 grep_info.isam_p_buf = NULL;
1690 grep_info.reg_type = reg_type;
1694 logf (LOG_DEBUG, "APT_or_list termp=%s", termp);
1695 grep_info.isam_p_indx = 0;
1696 r = string_term (zh, zapt, &termp, attributeSet, stream, &grep_info,
1697 reg_type, complete_flag, num_bases, basenames,
1701 logf (LOG_DEBUG, "term: %s", term_dst);
1702 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1703 grep_info.isam_p_indx, term_dst,
1704 strlen(term_dst), rank_type);
1705 assert (rset[rset_no]);
1706 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1710 xfree(grep_info.term_no);
1712 xfree (grep_info.isam_p_buf);
1715 rset_null_parms parms;
1717 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1718 return rset_create (rset_kind_null, &parms);
1721 for (i = 1; i<rset_no; i++)
1723 rset_bool_parms bool_parms;
1725 bool_parms.rset_l = result;
1726 bool_parms.rset_r = rset[i];
1727 bool_parms.key_size = sizeof(struct it_key);
1728 bool_parms.cmp = key_compare_it;
1729 result = rset_create (rset_kind_or, &bool_parms);
1734 static RSET rpn_search_APT_and_list (ZebraHandle zh,
1735 Z_AttributesPlusTerm *zapt,
1736 const char *termz_org,
1737 oid_value attributeSet,
1739 int reg_type, int complete_flag,
1740 const char *rank_type,
1741 int num_bases, char **basenames)
1743 char term_dst[IT_MAX_WORD+1];
1744 RSET rset[60], result;
1745 int i, r, rset_no = 0;
1746 struct grep_info grep_info;
1747 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1748 const char *termp = termz;
1751 grep_info.term_no = 0;
1753 grep_info.isam_p_size = 0;
1754 grep_info.isam_p_buf = NULL;
1756 grep_info.reg_type = reg_type;
1760 logf (LOG_DEBUG, "APT_and_list termp=%s", termp);
1761 grep_info.isam_p_indx = 0;
1762 r = string_term (zh, zapt, &termp, attributeSet, stream, &grep_info,
1763 reg_type, complete_flag, num_bases, basenames,
1767 logf (LOG_DEBUG, "term: %s", term_dst);
1768 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1769 grep_info.isam_p_indx, term_dst,
1770 strlen(term_dst), rank_type);
1771 assert (rset[rset_no]);
1772 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1776 xfree(grep_info.term_no);
1778 xfree (grep_info.isam_p_buf);
1781 rset_null_parms parms;
1783 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1784 return rset_create (rset_kind_null, &parms);
1787 for (i = 1; i<rset_no; i++)
1789 rset_bool_parms bool_parms;
1791 bool_parms.rset_l = result;
1792 bool_parms.rset_r = rset[i];
1793 bool_parms.key_size = sizeof(struct it_key);
1794 bool_parms.cmp = key_compare_it;
1795 result = rset_create (rset_kind_and, &bool_parms);
1800 static int numeric_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1801 const char **term_sub,
1803 oid_value attributeSet,
1804 struct grep_info *grep_info,
1813 char *term_tmp = term_dict + strlen(term_dict);
1815 attr_init (&relation, zapt, 2);
1816 relation_value = attr_find (&relation, NULL);
1818 logf (LOG_DEBUG, "numeric relation value=%d", relation_value);
1820 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_tmp, 1,
1823 term_value = atoi (term_tmp);
1824 switch (relation_value)
1827 logf (LOG_DEBUG, "Relation <");
1828 gen_regular_rel (term_tmp, term_value-1, 1);
1831 logf (LOG_DEBUG, "Relation <=");
1832 gen_regular_rel (term_tmp, term_value, 1);
1835 logf (LOG_DEBUG, "Relation >=");
1836 gen_regular_rel (term_tmp, term_value, 0);
1839 logf (LOG_DEBUG, "Relation >");
1840 gen_regular_rel (term_tmp, term_value+1, 0);
1844 logf (LOG_DEBUG, "Relation =");
1845 sprintf (term_tmp, "(0*%d)", term_value);
1847 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_tmp);
1848 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info, max_pos,
1851 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1852 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1856 static int numeric_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1857 const char **term_sub,
1858 oid_value attributeSet, struct grep_info *grep_info,
1859 int reg_type, int complete_flag,
1860 int num_bases, char **basenames,
1863 char term_dict[2*IT_MAX_WORD+2];
1867 oid_value curAttributeSet = attributeSet;
1869 struct rpn_char_map_info rcmi;
1871 rpn_char_map_prepare (zh, reg_type, &rcmi);
1872 attr_init (&use, zapt, 1);
1873 use_value = attr_find (&use, &curAttributeSet);
1874 logf (LOG_DEBUG, "numeric_term, use value %d", use_value);
1876 if (use_value == -1)
1879 for (base_no = 0; base_no < num_bases; base_no++)
1882 data1_local_attribute *local_attr;
1883 int max_pos, prefix_len = 0;
1886 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
1888 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1889 curAttributeSet, use_value, r);
1896 if (zebraExplain_curDatabase (zh->zei, basenames[base_no]))
1898 zh->errCode = 109; /* Database unavailable */
1899 zh->errString = basenames[base_no];
1902 for (local_attr = attp.local_attributes; local_attr;
1903 local_attr = local_attr->next)
1909 ord = zebraExplain_lookupSU (zh->zei, attp.attset_ordinal,
1914 term_dict[prefix_len++] = '|';
1916 term_dict[prefix_len++] = '(';
1918 ord_len = key_SU_code (ord, ord_buf);
1919 for (i = 0; i<ord_len; i++)
1921 term_dict[prefix_len++] = 1;
1922 term_dict[prefix_len++] = ord_buf[i];
1930 term_dict[prefix_len++] = ')';
1931 term_dict[prefix_len++] = 1;
1932 term_dict[prefix_len++] = reg_type;
1933 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1934 term_dict[prefix_len] = '\0';
1935 if (!numeric_relation (zh, zapt, &termp, term_dict,
1936 attributeSet, grep_info, &max_pos, reg_type,
1941 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1945 static RSET rpn_search_APT_numeric (ZebraHandle zh,
1946 Z_AttributesPlusTerm *zapt,
1948 oid_value attributeSet,
1950 int reg_type, int complete_flag,
1951 const char *rank_type,
1952 int num_bases, char **basenames)
1954 char term_dst[IT_MAX_WORD+1];
1955 const char *termp = termz;
1956 RSET rset[60], result;
1957 int i, r, rset_no = 0;
1958 struct grep_info grep_info;
1961 grep_info.term_no = 0;
1963 grep_info.isam_p_size = 0;
1964 grep_info.isam_p_buf = NULL;
1966 grep_info.reg_type = reg_type;
1970 logf (LOG_DEBUG, "APT_numeric termp=%s", termp);
1971 grep_info.isam_p_indx = 0;
1972 r = numeric_term (zh, zapt, &termp, attributeSet, &grep_info,
1973 reg_type, complete_flag, num_bases, basenames,
1977 logf (LOG_DEBUG, "term: %s", term_dst);
1978 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1979 grep_info.isam_p_indx, term_dst,
1980 strlen(term_dst), rank_type);
1981 assert (rset[rset_no]);
1982 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1986 xfree(grep_info.term_no);
1988 xfree (grep_info.isam_p_buf);
1991 rset_null_parms parms;
1993 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1994 return rset_create (rset_kind_null, &parms);
1997 for (i = 1; i<rset_no; i++)
1999 rset_bool_parms bool_parms;
2001 bool_parms.rset_l = result;
2002 bool_parms.rset_r = rset[i];
2003 bool_parms.key_size = sizeof(struct it_key);
2004 bool_parms.cmp = key_compare_it;
2005 result = rset_create (rset_kind_and, &bool_parms);
2010 static RSET rpn_search_APT_local (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2012 oid_value attributeSet,
2014 const char *rank_type)
2019 rset_temp_parms parms;
2021 parms.rset_term = rset_term_create (termz, -1, rank_type);
2022 parms.key_size = sizeof (struct it_key);
2023 parms.temp_path = res_get (zh->res, "setTmpDir");
2024 result = rset_create (rset_kind_temp, &parms);
2025 rsfd = rset_open (result, RSETF_WRITE);
2027 key.sysno = atoi (termz);
2031 rset_write (result, rsfd, &key);
2032 rset_close (result, rsfd);
2036 static RSET rpn_sort_spec (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2037 oid_value attributeSet, NMEM stream,
2038 Z_SortKeySpecList *sort_sequence,
2039 const char *rank_type)
2041 rset_null_parms parms;
2043 int sort_relation_value;
2044 AttrType sort_relation_type;
2049 Z_AttributeElement *ae;
2053 attr_init (&sort_relation_type, zapt, 7);
2054 sort_relation_value = attr_find (&sort_relation_type, &attributeSet);
2056 attr_init (&use_type, zapt, 1);
2057 use_value = attr_find (&use_type, &attributeSet);
2059 if (!sort_sequence->specs)
2061 sort_sequence->num_specs = 10;
2062 sort_sequence->specs = (Z_SortKeySpec **)
2063 nmem_malloc (stream, sort_sequence->num_specs *
2064 sizeof(*sort_sequence->specs));
2065 for (i = 0; i<sort_sequence->num_specs; i++)
2066 sort_sequence->specs[i] = 0;
2068 if (zapt->term->which != Z_Term_general)
2071 i = atoi_n ((char *) zapt->term->u.general->buf,
2072 zapt->term->u.general->len);
2073 if (i >= sort_sequence->num_specs)
2076 oe.proto = PROTO_Z3950;
2077 oe.oclass = CLASS_ATTSET;
2078 oe.value = attributeSet;
2079 if (!oid_ent_to_oid (&oe, oid))
2082 sks = (Z_SortKeySpec *) nmem_malloc (stream, sizeof(*sks));
2083 sks->sortElement = (Z_SortElement *)
2084 nmem_malloc (stream, sizeof(*sks->sortElement));
2085 sks->sortElement->which = Z_SortElement_generic;
2086 sk = sks->sortElement->u.generic = (Z_SortKey *)
2087 nmem_malloc (stream, sizeof(*sk));
2088 sk->which = Z_SortKey_sortAttributes;
2089 sk->u.sortAttributes = (Z_SortAttributes *)
2090 nmem_malloc (stream, sizeof(*sk->u.sortAttributes));
2092 sk->u.sortAttributes->id = oid;
2093 sk->u.sortAttributes->list = (Z_AttributeList *)
2094 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list));
2095 sk->u.sortAttributes->list->num_attributes = 1;
2096 sk->u.sortAttributes->list->attributes = (Z_AttributeElement **)
2097 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list->attributes));
2098 ae = *sk->u.sortAttributes->list->attributes = (Z_AttributeElement *)
2099 nmem_malloc (stream, sizeof(**sk->u.sortAttributes->list->attributes));
2100 ae->attributeSet = 0;
2101 ae->attributeType = (int *)
2102 nmem_malloc (stream, sizeof(*ae->attributeType));
2103 *ae->attributeType = 1;
2104 ae->which = Z_AttributeValue_numeric;
2105 ae->value.numeric = (int *)
2106 nmem_malloc (stream, sizeof(*ae->value.numeric));
2107 *ae->value.numeric = use_value;
2109 sks->sortRelation = (int *)
2110 nmem_malloc (stream, sizeof(*sks->sortRelation));
2111 if (sort_relation_value == 1)
2112 *sks->sortRelation = Z_SortRelation_ascending;
2113 else if (sort_relation_value == 2)
2114 *sks->sortRelation = Z_SortRelation_descending;
2116 *sks->sortRelation = Z_SortRelation_ascending;
2118 sks->caseSensitivity = (int *)
2119 nmem_malloc (stream, sizeof(*sks->caseSensitivity));
2120 *sks->caseSensitivity = 0;
2123 sks->which = Z_SortKeySpec_null;
2124 sks->u.null = odr_nullval ();
2126 sks->missingValueAction = 0;
2129 sort_sequence->specs[i] = sks;
2131 parms.rset_term = rset_term_create ("", -1, rank_type);
2132 return rset_create (rset_kind_null, &parms);
2136 static RSET rpn_search_APT (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2137 oid_value attributeSet, NMEM stream,
2138 Z_SortKeySpecList *sort_sequence,
2139 int num_bases, char **basenames)
2142 char *search_type = NULL;
2143 char *rank_type = NULL;
2146 char termz[IT_MAX_WORD+1];
2148 zebra_maps_attr (zh->zebra_maps, zapt, ®_id, &search_type,
2149 &rank_type, &complete_flag, &sort_flag);
2151 logf (LOG_DEBUG, "reg_id=%c", reg_id);
2152 logf (LOG_DEBUG, "complete_flag=%d", complete_flag);
2153 logf (LOG_DEBUG, "search_type=%s", search_type);
2154 logf (LOG_DEBUG, "rank_type=%s", rank_type);
2156 if (zapt->term->which != Z_Term_general)
2161 trans_term (zh, zapt, termz);
2164 return rpn_sort_spec (zh, zapt, attributeSet, stream, sort_sequence,
2167 if (!strcmp (search_type, "phrase"))
2169 return rpn_search_APT_phrase (zh, zapt, termz, attributeSet, stream,
2170 reg_id, complete_flag, rank_type,
2171 num_bases, basenames);
2173 else if (!strcmp (search_type, "and-list"))
2175 return rpn_search_APT_and_list (zh, zapt, termz, attributeSet, stream,
2176 reg_id, complete_flag, rank_type,
2177 num_bases, basenames);
2179 else if (!strcmp (search_type, "or-list"))
2181 return rpn_search_APT_or_list (zh, zapt, termz, attributeSet, stream,
2182 reg_id, complete_flag, rank_type,
2183 num_bases, basenames);
2185 else if (!strcmp (search_type, "local"))
2187 return rpn_search_APT_local (zh, zapt, termz, attributeSet, stream,
2190 else if (!strcmp (search_type, "numeric"))
2192 return rpn_search_APT_numeric (zh, zapt, termz, attributeSet, stream,
2193 reg_id, complete_flag, rank_type,
2194 num_bases, basenames);
2200 static RSET rpn_search_structure (ZebraHandle zh, Z_RPNStructure *zs,
2201 oid_value attributeSet, NMEM stream,
2202 Z_SortKeySpecList *sort_sequence,
2203 int num_bases, char **basenames)
2206 if (zs->which == Z_RPNStructure_complex)
2208 Z_Operator *zop = zs->u.complex->roperator;
2209 rset_bool_parms bool_parms;
2211 bool_parms.rset_l = rpn_search_structure (zh, zs->u.complex->s1,
2212 attributeSet, stream,
2214 num_bases, basenames);
2215 if (bool_parms.rset_l == NULL)
2217 bool_parms.rset_r = rpn_search_structure (zh, zs->u.complex->s2,
2218 attributeSet, stream,
2220 num_bases, basenames);
2221 if (bool_parms.rset_r == NULL)
2223 rset_delete (bool_parms.rset_l);
2226 bool_parms.key_size = sizeof(struct it_key);
2227 bool_parms.cmp = key_compare_it;
2231 case Z_Operator_and:
2232 r = rset_create (rset_kind_and, &bool_parms);
2235 r = rset_create (rset_kind_or, &bool_parms);
2237 case Z_Operator_and_not:
2238 r = rset_create (rset_kind_not, &bool_parms);
2240 case Z_Operator_prox:
2242 if (zop->u.prox->which != Z_ProximityOperator_known)
2248 if (zop->u.prox->which != Z_ProxCode_known)
2256 if (*zop->u.prox->u.known != Z_ProxUnit_word)
2258 char *val = (char *) nmem_malloc (stream, 16);
2260 zh->errString = val;
2261 sprintf (val, "%d", *zop->u.prox->u.known);
2265 if (*zop->u.prox->proximityUnitCode != Z_ProxUnit_word)
2267 char *val = (char *) nmem_malloc (stream, 16);
2269 zh->errString = val;
2270 sprintf (val, "%d", *zop->u.prox->proximityUnitCode);
2278 rsets[0] = bool_parms.rset_l;
2279 rsets[1] = bool_parms.rset_r;
2281 r = rpn_prox (zh, rsets, 2,
2282 *zop->u.prox->ordered,
2283 (!zop->u.prox->exclusion ? 0 :
2284 *zop->u.prox->exclusion),
2285 *zop->u.prox->relationType,
2286 *zop->u.prox->distance);
2287 rset_delete (rsets[0]);
2288 rset_delete (rsets[1]);
2296 else if (zs->which == Z_RPNStructure_simple)
2298 if (zs->u.simple->which == Z_Operand_APT)
2300 logf (LOG_DEBUG, "rpn_search_APT");
2301 r = rpn_search_APT (zh, zs->u.simple->u.attributesPlusTerm,
2302 attributeSet, stream, sort_sequence,
2303 num_bases, basenames);
2305 else if (zs->u.simple->which == Z_Operand_resultSetId)
2307 logf (LOG_DEBUG, "rpn_search_ref");
2308 r = resultSetRef (zh, zs->u.simple->u.resultSetId);
2310 r = rset_create (rset_kind_null, NULL);
2327 RSET rpn_search (ZebraHandle zh, NMEM nmem,
2328 Z_RPNQuery *rpn, int num_bases, char **basenames,
2329 const char *setname,
2334 oid_value attributeSet;
2335 Z_SortKeySpecList *sort_sequence;
2339 zh->errString = NULL;
2342 sort_sequence = (Z_SortKeySpecList *)
2343 nmem_malloc (nmem, sizeof(*sort_sequence));
2344 sort_sequence->num_specs = 10;
2345 sort_sequence->specs = (Z_SortKeySpec **)
2346 nmem_malloc (nmem, sort_sequence->num_specs *
2347 sizeof(*sort_sequence->specs));
2348 for (i = 0; i<sort_sequence->num_specs; i++)
2349 sort_sequence->specs[i] = 0;
2351 attrset = oid_getentbyoid (rpn->attributeSetId);
2352 attributeSet = attrset->value;
2353 rset = rpn_search_structure (zh, rpn->RPNStructure, attributeSet,
2354 nmem, sort_sequence, num_bases, basenames);
2359 logf (LOG_DEBUG, "search error: %d", zh->errCode);
2361 for (i = 0; sort_sequence->specs[i]; i++)
2363 sort_sequence->num_specs = i;
2365 resultSetRank (zh, sset, rset);
2368 logf (LOG_DEBUG, "resultSetSortSingle in rpn_search");
2369 resultSetSortSingle (zh, nmem, sset, rset,
2370 sort_sequence, &sort_status);
2373 logf (LOG_DEBUG, "resultSetSortSingle status = %d", zh->errCode);
2379 struct scan_info_entry {
2385 struct scan_info_entry *list;
2391 static int scan_handle (char *name, const char *info, int pos, void *client)
2393 int len_prefix, idx;
2394 struct scan_info *scan_info = (struct scan_info *) client;
2396 len_prefix = strlen(scan_info->prefix);
2397 if (memcmp (name, scan_info->prefix, len_prefix))
2399 if (pos > 0) idx = scan_info->after - pos + scan_info->before;
2402 scan_info->list[idx].term = (char *)
2403 odr_malloc (scan_info->odr, strlen(name + len_prefix)+1);
2404 strcpy (scan_info->list[idx].term, name + len_prefix);
2405 assert (*info == sizeof(ISAMS_P));
2406 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAMS_P));
2410 static void scan_term_untrans (ZebraHandle zh, NMEM stream, int reg_type,
2411 char **dst, const char *src)
2413 char term_dst[1024];
2415 term_untrans (zh, reg_type, term_dst, src);
2417 *dst = (char *) nmem_malloc (stream, strlen(term_dst)+1);
2418 strcpy (*dst, term_dst);
2421 static void count_set (RSET r, int *count)
2429 logf (LOG_DEBUG, "count_set");
2432 rfd = rset_open (r, RSETF_READ);
2433 while (rset_read (r, rfd, &key, &term_index))
2435 if (key.sysno != psysno)
2442 rset_close (r, rfd);
2443 logf (LOG_DEBUG, "%d keys, %d records", kno, *count);
2446 void rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
2447 oid_value attributeset,
2448 int num_bases, char **basenames,
2449 int *position, int *num_entries, ZebraScanEntry **list,
2453 int pos = *position;
2454 int num = *num_entries;
2458 char termz[IT_MAX_WORD+20];
2461 struct scan_info *scan_info_array;
2462 ZebraScanEntry *glist;
2463 int ords[32], ord_no = 0;
2467 char *search_type = NULL;
2468 char *rank_type = NULL;
2473 if (attributeset == VAL_NONE)
2474 attributeset = VAL_BIB1;
2476 logf (LOG_DEBUG, "position = %d, num = %d", pos, num);
2478 attr_init (&use, zapt, 1);
2479 use_value = attr_find (&use, &attributeset);
2481 if (zebra_maps_attr (zh->zebra_maps, zapt, ®_id, &search_type,
2482 &rank_type, &complete_flag, &sort_flag))
2488 if (use_value == -1)
2490 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
2494 data1_local_attribute *local_attr;
2496 if ((r=att_getentbyatt (zh, &attp, attributeset, use_value)))
2498 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
2499 attributeset, use_value);
2507 if (zebraExplain_curDatabase (zh->zei, basenames[base_no]))
2509 zh->errString = basenames[base_no];
2510 zh->errCode = 109; /* Database unavailable */
2513 for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
2514 local_attr = local_attr->next)
2518 ord = zebraExplain_lookupSU (zh->zei, attp.attset_ordinal,
2521 ords[ord_no++] = ord;
2529 /* prepare dictionary scanning */
2532 scan_info_array = (struct scan_info *)
2533 odr_malloc (stream, ord_no * sizeof(*scan_info_array));
2534 for (i = 0; i < ord_no; i++)
2536 int j, prefix_len = 0;
2537 int before_tmp = before, after_tmp = after;
2538 struct scan_info *scan_info = scan_info_array + i;
2539 struct rpn_char_map_info rcmi;
2541 rpn_char_map_prepare (zh, reg_id, &rcmi);
2543 scan_info->before = before;
2544 scan_info->after = after;
2545 scan_info->odr = stream;
2547 scan_info->list = (struct scan_info_entry *)
2548 odr_malloc (stream, (before+after) * sizeof(*scan_info->list));
2549 for (j = 0; j<before+after; j++)
2550 scan_info->list[j].term = NULL;
2552 prefix_len += key_SU_code (ords[i], termz + prefix_len);
2553 termz[prefix_len++] = reg_id;
2554 termz[prefix_len] = 0;
2555 strcpy (scan_info->prefix, termz);
2557 trans_scan_term (zh, zapt, termz+prefix_len, reg_id);
2559 dict_scan (zh->dict, termz, &before_tmp, &after_tmp, scan_info,
2562 glist = (ZebraScanEntry *)
2563 odr_malloc (stream, (before+after)*sizeof(*glist));
2565 /* consider terms after main term */
2566 for (i = 0; i < ord_no; i++)
2570 for (i = 0; i<after; i++)
2573 const char *mterm = NULL;
2577 for (j = 0; j < ord_no; j++)
2579 if (ptr[j] < before+after &&
2580 (tst=scan_info_array[j].list[ptr[j]].term) &&
2581 (!mterm || strcmp (tst, mterm) < 0))
2589 scan_term_untrans (zh, stream->mem, reg_id,
2590 &glist[i+before].term, mterm);
2591 rset = rset_trunc (zh, &scan_info_array[j0].list[ptr[j0]].isam_p, 1,
2592 glist[i+before].term, strlen(glist[i+before].term),
2596 for (j = j0+1; j<ord_no; j++)
2598 if (ptr[j] < before+after &&
2599 (tst=scan_info_array[j].list[ptr[j]].term) &&
2600 !strcmp (tst, mterm))
2602 rset_bool_parms bool_parms;
2606 rset_trunc (zh, &scan_info_array[j].list[ptr[j]].isam_p, 1,
2607 glist[i+before].term,
2608 strlen(glist[i+before].term), NULL);
2610 bool_parms.key_size = sizeof(struct it_key);
2611 bool_parms.cmp = key_compare_it;
2612 bool_parms.rset_l = rset;
2613 bool_parms.rset_r = rset2;
2615 rset = rset_create (rset_kind_or, &bool_parms);
2620 count_set (rset, &glist[i+before].occurrences);
2625 *num_entries -= (after-i);
2629 /* consider terms before main term */
2630 for (i = 0; i<ord_no; i++)
2633 for (i = 0; i<before; i++)
2636 const char *mterm = NULL;
2640 for (j = 0; j <ord_no; j++)
2642 if (ptr[j] < before &&
2643 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2644 (!mterm || strcmp (tst, mterm) > 0))
2653 scan_term_untrans (zh, stream->mem, reg_id,
2654 &glist[before-1-i].term, mterm);
2657 (zh, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1,
2658 glist[before-1-i].term, strlen(glist[before-1-i].term),
2663 for (j = j0+1; j<ord_no; j++)
2665 if (ptr[j] < before &&
2666 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2667 !strcmp (tst, mterm))
2669 rset_bool_parms bool_parms;
2672 rset2 = rset_trunc (zh,
2673 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1,
2674 glist[before-1-i].term,
2675 strlen(glist[before-1-i].term), NULL);
2677 bool_parms.key_size = sizeof(struct it_key);
2678 bool_parms.cmp = key_compare_it;
2679 bool_parms.rset_l = rset;
2680 bool_parms.rset_r = rset2;
2682 rset = rset_create (rset_kind_or, &bool_parms);
2687 count_set (rset, &glist[before-1-i].occurrences);
2697 *list = glist + i; /* list is set to first 'real' entry */
2699 logf (LOG_DEBUG, "position = %d, num_entries = %d",
2700 *position, *num_entries);
2702 logf (LOG_DEBUG, "scan error: %d", zh->errCode);