2 * Copyright (C) 1995-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.98 1999-11-30 13:48:04 adam
8 * Improved installation. Updated for inclusion of YAZ header files.
10 * Revision 1.97 1999/10/14 14:33:50 adam
11 * Added truncation 5=106.
13 * Revision 1.96 1999/09/23 10:05:05 adam
14 * Implemented structure=105 searching.
16 * Revision 1.95 1999/09/07 07:19:21 adam
17 * Work on character mapping. Implemented replace rules.
19 * Revision 1.94 1999/07/20 13:59:18 adam
20 * Fixed bug that occurred when phrases had 0 hits.
22 * Revision 1.93 1999/06/17 14:38:40 adam
23 * Bug fix: Scan SEGV'ed when getting unknown use attribute.
25 * Revision 1.92 1999/05/26 07:49:13 adam
28 * Revision 1.91 1999/02/02 14:51:13 adam
29 * Updated WIN32 code specific sections. Changed header.
31 * Revision 1.90 1998/11/16 16:03:43 adam
32 * Moved loggin utilities to Yaz. Was implemented in file zlogs.c.
34 * Revision 1.89 1998/11/16 10:11:55 adam
35 * Added addtional info for error 114 - unsupported use attribute.
37 * Revision 1.88 1998/10/18 07:54:52 adam
38 * Additional info added for diagnostics 114 (Unsupported use attribute) and
39 * 121 (Unsupported attribute set).
41 * Revision 1.87 1998/09/28 11:19:12 adam
42 * Fix for Compiled ASN.1.
44 * Revision 1.86 1998/09/22 10:48:20 adam
45 * Minor changes in search API.
47 * Revision 1.85 1998/09/22 10:03:43 adam
48 * Changed result sets to be persistent in the sense that they can
49 * be re-searched if needed.
50 * Fixed memory leak in rsm_or.
52 * Revision 1.84 1998/09/18 12:41:00 adam
53 * Fixed bug with numerical relations.
55 * Revision 1.83 1998/09/02 13:53:19 adam
56 * Extra parameter decode added to search routines to implement
59 * Revision 1.82 1998/06/26 11:16:40 quinn
60 * Added support (un-optimised) for left and left/right truncation
62 * Revision 1.81 1998/06/24 12:16:14 adam
63 * Support for relations on text operands. Open range support in
64 * DFA module (i.e. [-j], [g-]).
66 * Revision 1.80 1998/06/23 15:33:34 adam
67 * Added feature to specify sort criteria in query (type 7 specifies
70 * Revision 1.79 1998/06/22 11:35:09 adam
73 * Revision 1.78 1998/06/08 14:43:17 adam
74 * Added suport for EXPLAIN Proxy servers - added settings databasePath
75 * and explainDatabase to facilitate this. Increased maximum number
76 * of databases and attributes in one register.
78 * Revision 1.77 1998/05/20 10:12:22 adam
79 * Implemented automatic EXPLAIN database maintenance.
80 * Modified Zebra to work with ASN.1 compiled version of YAZ.
82 * Revision 1.76 1998/04/02 14:35:29 adam
83 * First version of Zebra that works with compiled ASN.1.
85 * Revision 1.75 1998/03/05 08:45:13 adam
86 * New result set model and modular ranking system. Moved towards
87 * descent server API. System information stored as "SGML" records.
89 * Revision 1.74 1998/02/10 12:03:06 adam
92 * Revision 1.73 1998/01/29 13:40:11 adam
93 * Better logging for scan service.
95 * Revision 1.72 1998/01/07 13:53:41 adam
96 * Queries using simple ranked operands returns right number of hits.
98 * Revision 1.71 1997/12/18 10:54:24 adam
99 * New method result set method rs_hits that returns the number of
100 * hits in result-set (if known). The ranked result set returns real
101 * number of hits but only when not combined with other operands.
103 * Revision 1.70 1997/10/31 12:34:43 adam
104 * Changed a few log statements.
106 * Revision 1.69 1997/10/29 12:05:02 adam
107 * Server produces diagnostic "Unsupported Attribute Set" when appropriate.
109 * Revision 1.68 1997/10/27 14:33:06 adam
110 * Moved towards generic character mapping depending on "structure"
111 * field in abstract syntax file. Fixed a few memory leaks. Fixed
112 * bug with negative integers when doing searches with relational
115 * Revision 1.67 1997/09/29 09:06:10 adam
116 * Removed one static var in order to make this module thread safe.
118 * Revision 1.66 1997/09/25 14:58:03 adam
121 * Revision 1.65 1997/09/22 12:39:06 adam
122 * Added get_pos method for the ranked result sets.
124 * Revision 1.64 1997/09/18 08:59:20 adam
125 * Extra generic handle for the character mapping routines.
127 * Revision 1.63 1997/09/17 12:19:18 adam
128 * Zebra version corresponds to YAZ version 1.4.
129 * Changed Zebra server so that it doesn't depend on global common_resource.
131 * Revision 1.62 1997/09/05 15:30:09 adam
132 * Changed prototype for chr_map_input - added const.
133 * Added support for C++, headers uses extern "C" for public definitions.
135 * Revision 1.61 1997/02/10 10:21:14 adam
136 * Bug fix: in search terms character (^) wasn't observed.
138 * Revision 1.60 1997/01/31 11:10:34 adam
139 * Bug fix: Leading and trailing white space weren't removed in scan tokens.
141 * Revision 1.59 1997/01/17 11:31:46 adam
142 * Bug fix: complete phrase search didn't work.
144 * Revision 1.58 1996/12/23 15:30:45 adam
145 * Work on truncation.
146 * Bug fix: result sets weren't deleted after server shut down.
148 * Revision 1.57 1996/11/11 13:38:02 adam
149 * Added proximity support in search.
151 * Revision 1.56 1996/11/08 11:10:32 adam
152 * Buffers used during file match got bigger.
153 * Compressed ISAM support everywhere.
154 * Bug fixes regarding masking characters in queries.
155 * Redesigned Regexp-2 queries.
157 * Revision 1.55 1996/11/04 14:07:44 adam
158 * Moved truncation code to trunc.c.
160 * Revision 1.54 1996/10/29 14:09:52 adam
161 * Use of cisam system - enabled if setting isamc is 1.
163 * Revision 1.53 1996/06/26 09:21:43 adam
164 * Bug fix: local attribute set wasn't obeyed in scan.
166 * Revision 1.52 1996/06/17 14:26:20 adam
167 * Function gen_regular_rel changed to handle negative numbers.
169 * Revision 1.51 1996/06/11 10:54:15 quinn
172 * Revision 1.50 1996/06/07 08:51:53 adam
173 * Bug fix: Character mapping was broken (introducued by last revision).
175 * Revision 1.49 1996/06/04 10:18:11 adam
176 * Search/scan uses character mapping module.
178 * Revision 1.48 1996/05/28 15:15:01 adam
179 * Bug fix: Didn't handle unknown database correctly.
181 * Revision 1.47 1996/05/15 18:36:28 adam
182 * Function trans_term transforms unsearchable characters to blanks.
184 * Revision 1.46 1996/05/15 11:57:56 adam
185 * Fixed bug introduced by set/field mapping in search operations.
187 * Revision 1.45 1996/05/14 11:34:00 adam
188 * Scan support in multiple registers/databases.
190 * Revision 1.44 1996/05/14 06:16:44 adam
191 * Compact use/set bytes used in search service.
193 * Revision 1.43 1996/05/09 09:54:43 adam
194 * Server supports maps from one logical attributes to a list of physical
196 * The extraction process doesn't make space consuming 'any' keys.
198 * Revision 1.42 1996/05/09 07:28:56 quinn
199 * Work towards phrases and multiple registers
201 * Revision 1.41 1996/03/20 09:36:43 adam
202 * Function dict_lookup_grep got extra parameter, init_pos, which marks
203 * from which position in pattern approximate pattern matching should occur.
204 * Approximate pattern matching is used in relevance=re-2.
206 * Revision 1.40 1996/02/02 13:44:44 adam
207 * The public dictionary functions simply use char instead of Dict_char
208 * to represent search strings. Dict_char is used internally only.
210 * Revision 1.39 1996/01/03 16:22:13 quinn
211 * operator->roperator
213 * Revision 1.38 1995/12/11 09:12:55 adam
214 * The rec_get function returns NULL if record doesn't exist - will
215 * happen in the server if the result set records have been deleted since
216 * the creation of the set (i.e. the search).
217 * The server saves a result temporarily if it is 'volatile', i.e. the
218 * set is register dependent.
220 * Revision 1.37 1995/12/06 15:05:28 adam
221 * More verbose in count_set.
223 * Revision 1.36 1995/12/06 12:41:27 adam
224 * New command 'stat' for the index program.
225 * Filenames can be read from stdin by specifying '-'.
226 * Bug fix/enhancement of the transformation from terms to regular
227 * expressons in the search engine.
229 * Revision 1.35 1995/11/27 09:29:00 adam
230 * Bug fixes regarding conversion to regular expressions.
232 * Revision 1.34 1995/11/16 17:00:56 adam
233 * Better logging of rpn query.
235 * Revision 1.33 1995/11/01 13:58:28 quinn
236 * Moving data1 to yaz/retrieval
238 * Revision 1.32 1995/10/27 14:00:11 adam
239 * Implemented detection of database availability.
241 * Revision 1.31 1995/10/17 18:02:10 adam
242 * New feature: databases. Implemented as prefix to words in dictionary.
244 * Revision 1.30 1995/10/16 09:32:38 adam
245 * More work on relational op.
247 * Revision 1.29 1995/10/13 16:01:49 adam
250 * Revision 1.28 1995/10/13 12:26:43 adam
251 * Optimization of truncation.
253 * Revision 1.27 1995/10/12 17:07:22 adam
256 * Revision 1.26 1995/10/12 12:40:54 adam
257 * Bug fixes in rpn_prox.
259 * Revision 1.25 1995/10/10 13:59:24 adam
260 * Function rset_open changed its wflag parameter to general flags.
262 * Revision 1.24 1995/10/09 16:18:37 adam
263 * Function dict_lookup_grep got extra client data parameter.
265 * Revision 1.23 1995/10/06 16:33:37 adam
266 * Use attribute mappings.
268 * Revision 1.22 1995/10/06 15:07:39 adam
269 * Structure 'local-number' handled.
271 * Revision 1.21 1995/10/06 13:52:06 adam
272 * Bug fixes. Handler may abort further scanning.
274 * Revision 1.20 1995/10/06 11:06:33 adam
275 * Scan entries include 'occurrences' now.
277 * Revision 1.19 1995/10/06 10:43:56 adam
278 * Scan added. 'occurrences' in scan entries not set yet.
280 * Revision 1.18 1995/10/04 16:57:20 adam
281 * Key input and merge sort in one pass.
283 * Revision 1.17 1995/10/04 12:55:17 adam
284 * Bug fix in ranked search. Use=Any keys inserted.
286 * Revision 1.16 1995/10/02 16:24:40 adam
287 * Use attribute actually used in search requests.
289 * Revision 1.15 1995/10/02 15:18:52 adam
290 * New member in recRetrieveCtrl: diagnostic.
292 * Revision 1.14 1995/09/28 12:10:32 adam
293 * Bug fixes. Field prefix used in queries.
295 * Revision 1.13 1995/09/18 14:17:50 adam
298 * Revision 1.12 1995/09/15 14:45:21 adam
300 * Work on truncation.
302 * Revision 1.11 1995/09/14 11:53:27 adam
303 * First work on regular expressions/truncations.
305 * Revision 1.10 1995/09/11 15:23:26 adam
306 * More work on relevance search.
308 * Revision 1.9 1995/09/11 13:09:35 adam
309 * More work on relevance feedback.
311 * Revision 1.8 1995/09/08 14:52:27 adam
312 * Minor changes. Dictionary is lower case now.
314 * Revision 1.7 1995/09/07 13:58:36 adam
315 * New parameter: result-set file descriptor (RSFD) to support multiple
316 * positions within the same result-set.
317 * Boolean operators: and, or, not implemented.
318 * Result-set references.
320 * Revision 1.6 1995/09/06 16:11:18 adam
321 * Option: only one word key per file.
323 * Revision 1.5 1995/09/06 10:33:04 adam
324 * More work on present. Some log messages removed.
326 * Revision 1.4 1995/09/05 15:28:40 adam
327 * More work on search engine.
329 * Revision 1.3 1995/09/04 15:20:22 adam
332 * Revision 1.2 1995/09/04 12:33:43 adam
333 * Various cleanup. YAZ util used instead.
335 * Revision 1.1 1995/09/04 09:10:40 adam
336 * More work on index add/del/update.
337 * Merge sort implemented.
338 * Initial work on z39 server.
357 struct rpn_char_map_info {
362 static const char **rpn_char_map_handler (void *vp, const char **from, int len)
364 struct rpn_char_map_info *p = (struct rpn_char_map_info *) vp;
365 return zebra_maps_input (p->zm, p->reg_type, from, len);
368 static void rpn_char_map_prepare (ZebraHandle zh, int reg_type,
369 struct rpn_char_map_info *map_info)
371 map_info->zm = zh->zebra_maps;
372 map_info->reg_type = reg_type;
373 dict_grep_cmap (zh->dict, map_info, rpn_char_map_handler);
380 Z_AttributesPlusTerm *zapt;
383 static int attr_find (AttrType *src, oid_value *attributeSetP)
388 num_attributes = src->zapt->attributes->num_attributes;
390 num_attributes = src->zapt->num_attributes;
392 while (src->major < num_attributes)
394 Z_AttributeElement *element;
397 element = src->zapt->attributes->attributes[src->major];
399 element = src->zapt->attributeList[src->major];
401 if (src->type == *element->attributeType)
403 switch (element->which)
405 case Z_AttributeValue_numeric:
407 if (element->attributeSet && attributeSetP)
411 attrset = oid_getentbyoid (element->attributeSet);
412 *attributeSetP = attrset->value;
414 return *element->value.numeric;
416 case Z_AttributeValue_complex:
417 if (src->minor >= element->value.complex->num_list ||
418 element->value.complex->list[src->minor]->which !=
419 Z_StringOrNumeric_numeric)
422 if (element->attributeSet && attributeSetP)
426 attrset = oid_getentbyoid (element->attributeSet);
427 *attributeSetP = attrset->value;
429 return *element->value.complex->list[src->minor-1]->u.numeric;
439 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
461 static void term_untrans (ZebraHandle zh, int reg_type,
462 char *dst, const char *src)
466 const char *cp = zebra_maps_output (zh->zebra_maps, reg_type, &src);
476 static void add_isam_p (const char *name, const char *info,
479 if (p->isam_p_indx == p->isam_p_size)
481 ISAMS_P *new_isam_p_buf;
485 p->isam_p_size = 2*p->isam_p_size + 100;
486 new_isam_p_buf = (ISAMS_P *) xmalloc (sizeof(*new_isam_p_buf) *
490 memcpy (new_isam_p_buf, p->isam_p_buf,
491 p->isam_p_indx * sizeof(*p->isam_p_buf));
492 xfree (p->isam_p_buf);
494 p->isam_p_buf = new_isam_p_buf;
497 new_term_no = (int *) xmalloc (sizeof(*new_term_no) *
501 memcpy (new_term_no, p->isam_p_buf,
502 p->isam_p_indx * sizeof(*p->term_no));
505 p->term_no = new_term_no;
508 assert (*info == sizeof(*p->isam_p_buf));
509 memcpy (p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
512 term_untrans (p->zh, p->reg_type, term_tmp, name+2);
513 logf (LOG_DEBUG, "grep: %s", term_tmp);
518 static int grep_handle (char *name, const char *info, void *p)
520 add_isam_p (name, info, (struct grep_info *) p);
524 static int term_pre (ZebraMaps zebra_maps, int reg_type, const char **src,
525 const char *ct1, const char *ct2)
527 const char *s1, *s0 = *src;
530 /* skip white space */
533 if (ct1 && strchr (ct1, *s0))
535 if (ct2 && strchr (ct2, *s0))
538 map = zebra_maps_input (zebra_maps, reg_type, &s1, strlen(s1));
539 if (**map != *CHR_SPACE)
547 /* term_100: handle term, where trunc=none (no operators at all) */
548 static int term_100 (ZebraMaps zebra_maps, int reg_type,
549 const char **src, char *dst, int space_split,
557 if (!term_pre (zebra_maps, reg_type, src, NULL, NULL))
563 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
564 if (space_split && **map == *CHR_SPACE)
568 if (!isalnum (*s1) && *s1 != '-')
580 /* term_101: handle term, where trunc=Process # */
581 static int term_101 (ZebraMaps zebra_maps, int reg_type,
582 const char **src, char *dst, int space_split,
590 if (!term_pre (zebra_maps, reg_type, src, "#", "#"))
599 dst_term[j++] = *s0++;
604 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
605 if (space_split && **map == *CHR_SPACE)
617 dst_term[j++] = '\0';
622 /* term_103: handle term, where trunc=re-2 (regular expressions) */
623 static int term_103 (ZebraMaps zebra_maps, int reg_type, const char **src,
624 char *dst, int *errors, int space_split,
632 if (!term_pre (zebra_maps, reg_type, src, "^\\()[].*+?|", "("))
635 if (errors && *s0 == '+' && s0[1] && s0[2] == '+' && s0[3] &&
638 *errors = s0[1] - '0';
645 if (strchr ("^\\()[].*+?|-", *s0))
653 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
654 if (**map == *CHR_SPACE)
671 /* term_103: handle term, where trunc=re-1 (regular expressions) */
672 static int term_102 (ZebraMaps zebra_maps, int reg_type, const char **src,
673 char *dst, int space_split, char *dst_term)
675 return term_103 (zebra_maps, reg_type, src, dst, NULL, space_split,
680 /* term_104: handle term, where trunc=Process # and ! */
681 static int term_104 (ZebraMaps zebra_maps, int reg_type,
682 const char **src, char *dst, int space_split,
690 if (!term_pre (zebra_maps, reg_type, src, "#!", "#!"))
699 dst_term[j++] = *s0++;
704 dst_term[j++] = *s0++;
708 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
709 if (space_split && **map == *CHR_SPACE)
721 dst_term[j++] = '\0';
726 /* term_105/106: handle term, where trunc=Process # and ! and right trunc */
727 static int term_105 (ZebraMaps zebra_maps, int reg_type,
728 const char **src, char *dst, int space_split,
729 char *dst_term, int right_truncate)
736 if (!term_pre (zebra_maps, reg_type, src, "*!", "*!"))
745 dst_term[j++] = *s0++;
750 dst_term[j++] = *s0++;
754 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
755 if (space_split && **map == *CHR_SPACE)
773 dst_term[j++] = '\0';
779 /* gen_regular_rel - generate regular expression from relation
780 * val: border value (inclusive)
781 * islt: 1 if <=; 0 if >=.
783 static void gen_regular_rel (char *dst, int val, int islt)
790 logf (LOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
794 strcpy (dst, "(-[0-9]+|(");
802 strcpy (dst, "([0-9]+|-(");
803 dst_p = strlen (dst);
813 dst_p = strlen (dst);
814 sprintf (numstr, "%d", val);
815 for (w = strlen(numstr); --w >= 0; pos++)
834 strcpy (dst + dst_p, numstr);
835 dst_p = strlen(dst) - pos - 1;
863 for (i = 0; i<pos; i++)
876 /* match everything less than 10^(pos-1) */
878 for (i=1; i<pos; i++)
879 strcat (dst, "[0-9]?");
883 /* match everything greater than 10^pos */
884 for (i = 0; i <= pos; i++)
885 strcat (dst, "[0-9]");
886 strcat (dst, "[0-9]*");
891 void string_rel_add_char (char **term_p, const char *src, int *indx)
893 if (src[*indx] == '\\')
894 *(*term_p)++ = src[(*indx)++];
895 *(*term_p)++ = src[(*indx)++];
899 * > abc ([b-].*|a[c-].*|ab[d-].*|abc.+)
900 * ([^-a].*|a[^-b].*ab[^-c].*|abc.+)
901 * >= abc ([b-].*|a[c-].*|ab[c-].*)
902 * ([^-a].*|a[^-b].*|ab[c-].*)
903 * < abc ([-0].*|a[-a].*|ab[-b].*)
904 * ([^a-].*|a[^b-].*|ab[^c-].*)
905 * <= abc ([-0].*|a[-a].*|ab[-b].*|abc)
906 * ([^a-].*|a[^b-].*|ab[^c-].*|abc)
908 static int string_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
909 const char **term_sub, char *term_dict,
910 oid_value attributeSet,
911 int reg_type, int space_split, char *term_dst)
916 char *term_tmp = term_dict + strlen(term_dict);
917 char term_component[256];
919 attr_init (&relation, zapt, 2);
920 relation_value = attr_find (&relation, NULL);
922 logf (LOG_DEBUG, "string relation value=%d", relation_value);
923 switch (relation_value)
926 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
927 space_split, term_dst))
929 logf (LOG_DEBUG, "Relation <");
932 for (i = 0; term_component[i]; )
939 string_rel_add_char (&term_tmp, term_component, &j);
944 string_rel_add_char (&term_tmp, term_component, &i);
955 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
956 space_split, term_dst))
958 logf (LOG_DEBUG, "Relation <=");
961 for (i = 0; term_component[i]; )
966 string_rel_add_char (&term_tmp, term_component, &j);
970 string_rel_add_char (&term_tmp, term_component, &i);
979 for (i = 0; term_component[i]; )
980 string_rel_add_char (&term_tmp, term_component, &i);
985 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
986 space_split, term_dst))
988 logf (LOG_DEBUG, "Relation >");
991 for (i = 0; term_component[i];)
996 string_rel_add_char (&term_tmp, term_component, &j);
1001 string_rel_add_char (&term_tmp, term_component, &i);
1009 for (i = 0; term_component[i];)
1010 string_rel_add_char (&term_tmp, term_component, &i);
1017 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
1018 space_split, term_dst))
1020 logf (LOG_DEBUG, "Relation >=");
1023 for (i = 0; term_component[i];)
1030 string_rel_add_char (&term_tmp, term_component, &j);
1033 if (term_component[i+1])
1037 string_rel_add_char (&term_tmp, term_component, &i);
1041 string_rel_add_char (&term_tmp, term_component, &i);
1053 logf (LOG_DEBUG, "Relation =");
1054 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
1055 space_split, term_dst))
1057 strcat (term_tmp, "(");
1058 strcat (term_tmp, term_component);
1059 strcat (term_tmp, ")");
1064 static int string_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1065 const char **term_sub,
1066 oid_value attributeSet, NMEM stream,
1067 struct grep_info *grep_info,
1068 int reg_type, int complete_flag,
1069 int num_bases, char **basenames,
1072 char term_dict[2*IT_MAX_WORD+4000];
1074 AttrType truncation;
1075 int truncation_value;
1078 oid_value curAttributeSet = attributeSet;
1080 struct rpn_char_map_info rcmi;
1081 int space_split = complete_flag ? 0 : 1;
1083 rpn_char_map_prepare (zh, reg_type, &rcmi);
1084 attr_init (&use, zapt, 1);
1085 use_value = attr_find (&use, &curAttributeSet);
1086 logf (LOG_DEBUG, "string_term, use value %d", use_value);
1087 attr_init (&truncation, zapt, 5);
1088 truncation_value = attr_find (&truncation, NULL);
1089 logf (LOG_DEBUG, "truncation value %d", truncation_value);
1091 if (use_value == -1)
1094 for (base_no = 0; base_no < num_bases; base_no++)
1097 data1_local_attribute *local_attr;
1098 int max_pos, prefix_len = 0;
1101 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
1103 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1104 curAttributeSet, use_value, r);
1108 sprintf (val_str, "%d", use_value);
1110 zh->errString = nmem_strdup (stream, val_str);
1115 struct oident oident;
1117 oident.proto = PROTO_Z3950;
1118 oident.oclass = CLASS_ATTSET;
1119 oident.value = curAttributeSet;
1120 oid_ent_to_oid (&oident, oid);
1123 zh->errString = nmem_strdup (stream, oident.desc);
1127 if (zebraExplain_curDatabase (zh->zei, basenames[base_no]))
1129 zh->errCode = 109; /* Database unavailable */
1130 zh->errString = basenames[base_no];
1133 for (local_attr = attp.local_attributes; local_attr;
1134 local_attr = local_attr->next)
1140 ord = zebraExplain_lookupSU (zh->zei, attp.attset_ordinal,
1145 term_dict[prefix_len++] = '|';
1147 term_dict[prefix_len++] = '(';
1149 ord_len = key_SU_code (ord, ord_buf);
1150 for (i = 0; i<ord_len; i++)
1152 term_dict[prefix_len++] = 1;
1153 term_dict[prefix_len++] = ord_buf[i];
1159 sprintf (val_str, "%d", use_value);
1161 zh->errString = nmem_strdup (stream, val_str);
1164 term_dict[prefix_len++] = ')';
1165 term_dict[prefix_len++] = 1;
1166 term_dict[prefix_len++] = reg_type;
1167 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1168 term_dict[prefix_len] = '\0';
1170 switch (truncation_value)
1172 case -1: /* not specified */
1173 case 100: /* do not truncate */
1174 if (!string_relation (zh, zapt, &termp, term_dict,
1176 reg_type, space_split, term_dst))
1178 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_dict+prefix_len);
1179 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info, &max_pos,
1182 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1184 case 1: /* right truncation */
1185 term_dict[j++] = '(';
1186 if (!term_100 (zh->zebra_maps, reg_type,
1187 &termp, term_dict + j, space_split, term_dst))
1189 strcat (term_dict, ".*)");
1190 dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1191 &max_pos, 0, grep_handle);
1193 case 2: /* keft truncation */
1194 term_dict[j++] = '('; term_dict[j++] = '.'; 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 3: /* left&right 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);
1213 case 101: /* process # in term */
1214 term_dict[j++] = '(';
1215 if (!term_101 (zh->zebra_maps, reg_type,
1216 &termp, term_dict + j, space_split, term_dst))
1218 strcat (term_dict, ")");
1219 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1220 &max_pos, 0, grep_handle);
1222 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d", r);
1224 case 102: /* Regexp-1 */
1225 term_dict[j++] = '(';
1226 if (!term_102 (zh->zebra_maps, reg_type,
1227 &termp, term_dict + j, space_split, term_dst))
1229 strcat (term_dict, ")");
1230 logf (LOG_DEBUG, "Regexp-1 tolerance=%d", r);
1231 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1232 &max_pos, 0, grep_handle);
1234 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
1237 case 103: /* Regexp-2 */
1239 term_dict[j++] = '(';
1240 if (!term_103 (zh->zebra_maps, reg_type,
1241 &termp, term_dict + j, &r, space_split, term_dst))
1243 strcat (term_dict, ")");
1244 logf (LOG_DEBUG, "Regexp-2 tolerance=%d", r);
1245 r = dict_lookup_grep (zh->dict, term_dict, r, grep_info,
1246 &max_pos, 2, grep_handle);
1248 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
1251 case 104: /* process # and ! in term */
1252 term_dict[j++] = '(';
1253 if (!term_104 (zh->zebra_maps, reg_type,
1254 &termp, term_dict + j, space_split, term_dst))
1256 strcat (term_dict, ")");
1257 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1258 &max_pos, 0, grep_handle);
1260 logf (LOG_WARN, "dict_lookup_grep err, trunc=#/!: %d", r);
1262 case 105: /* process * and ! in term */
1263 term_dict[j++] = '(';
1264 if (!term_105 (zh->zebra_maps, reg_type,
1265 &termp, term_dict + j, space_split, term_dst, 1))
1267 strcat (term_dict, ")");
1268 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1269 &max_pos, 0, grep_handle);
1271 logf (LOG_WARN, "dict_lookup_grep err, trunc=*/!: %d", r);
1273 case 106: /* process * and ! in term */
1274 term_dict[j++] = '(';
1275 if (!term_105 (zh->zebra_maps, reg_type,
1276 &termp, term_dict + j, space_split, term_dst, 0))
1278 strcat (term_dict, ")");
1279 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1280 &max_pos, 0, grep_handle);
1282 logf (LOG_WARN, "dict_lookup_grep err, trunc=*/!: %d", r);
1287 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1291 static void trans_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1295 Z_Term *term = zapt->term;
1297 sizez = term->u.general->len;
1298 if (sizez > IT_MAX_WORD-1)
1299 sizez = IT_MAX_WORD-1;
1300 memcpy (termz, term->u.general->buf, sizez);
1301 termz[sizez] = '\0';
1304 static void trans_scan_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1305 char *termz, int reg_type)
1307 Z_Term *term = zapt->term;
1309 const char *cp = (const char *) term->u.general->buf;
1310 const char *cp_end = cp + term->u.general->len;
1313 const char *space_map = NULL;
1316 while ((len = (cp_end - cp)) > 0)
1318 map = zebra_maps_input (zh->zebra_maps, reg_type, &cp, len);
1319 if (**map == *CHR_SPACE)
1324 for (src = space_map; *src; src++)
1327 for (src = *map; *src; src++)
1334 static RSET rpn_proximity (ZebraHandle zh, RSET rset1, RSET rset2,
1336 int exclusion, int relation, int distance)
1341 struct it_key buf1, buf2;
1344 rset_temp_parms parms;
1347 rsfd1 = rset_open (rset1, RSETF_READ);
1348 more1 = rset_read (rset1, rsfd1, &buf1, &term_index);
1350 rsfd2 = rset_open (rset2, RSETF_READ);
1351 more2 = rset_read (rset2, rsfd2, &buf2, &term_index);
1353 parms.key_size = sizeof (struct it_key);
1354 parms.temp_path = res_get (zh->res, "setTmpDir");
1355 result = rset_create (rset_kind_temp, &parms);
1356 rsfd_result = rset_open (result, RSETF_WRITE);
1358 logf (LOG_DEBUG, "rpn_proximity excl=%d ord=%d rel=%d dis=%d",
1359 exclusion, ordered, relation, distance);
1360 while (more1 && more2)
1362 int cmp = key_compare_it (&buf1, &buf2);
1364 more1 = rset_read (rset1, rsfd1, &buf1, &term_index);
1366 more2 = rset_read (rset2, rsfd2, &buf2, &term_index);
1369 int sysno = buf1.sysno;
1373 seqno[n++] = buf1.seqno;
1374 while ((more1 = rset_read (rset1, rsfd1, &buf1, &term_index)) &&
1375 sysno == buf1.sysno)
1377 seqno[n++] = buf1.seqno;
1380 for (i = 0; i<n; i++)
1382 int diff = buf2.seqno - seqno[i];
1383 int excl = exclusion;
1384 if (!ordered && diff < 0)
1389 if (diff < distance)
1393 if (diff <= distance)
1397 if (diff == distance)
1401 if (diff >= distance)
1405 if (diff > distance)
1409 if (diff != distance)
1414 rset_write (result, rsfd_result, &buf2);
1416 } while ((more2 = rset_read (rset2, rsfd2, &buf2, &term_index)) &&
1417 sysno == buf2.sysno);
1420 rset_close (result, rsfd_result);
1421 rset_close (rset1, rsfd1);
1422 rset_close (rset2, rsfd2);
1426 static RSET rpn_prox (ZebraHandle zh, RSET *rset, int rset_no)
1431 struct it_key **buf;
1433 char prox_term[1024];
1434 int length_prox_term = 0;
1435 int min_nn = 10000000;
1437 const char *flags = NULL;
1439 rsfd = (RSFD *) xmalloc (sizeof(*rsfd)*rset_no);
1440 more = (int *) xmalloc (sizeof(*more)*rset_no);
1441 buf = (struct it_key **) xmalloc (sizeof(*buf)*rset_no);
1444 for (i = 0; i<rset_no; i++)
1447 for (j = 0; j<rset[i]->no_rset_terms; j++)
1449 const char *nflags = rset[i]->rset_terms[j]->flags;
1450 char *term = rset[i]->rset_terms[j]->name;
1451 int lterm = strlen(term);
1452 if (lterm + length_prox_term < sizeof(prox_term)-1)
1454 if (length_prox_term)
1455 prox_term[length_prox_term++] = ' ';
1456 strcpy (prox_term + length_prox_term, term);
1457 length_prox_term += lterm;
1459 if (min_nn > rset[i]->rset_terms[j]->nn)
1460 min_nn = rset[i]->rset_terms[j]->nn;
1464 for (i = 0; i<rset_no; i++)
1466 buf[i] = (struct it_key *) xmalloc (sizeof(**buf));
1467 rsfd[i] = rset_open (rset[i], RSETF_READ);
1468 if (!(more[i] = rset_read (rset[i], rsfd[i], buf[i], &term_index)))
1473 rset_null_parms parms;
1477 rset_close (rset[i], rsfd[i]);
1481 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1483 parms.rset_term->nn = 0;
1484 result = rset_create (rset_kind_null, &parms);
1488 rset_temp_parms parms;
1491 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1493 parms.rset_term->nn = min_nn;
1494 parms.key_size = sizeof (struct it_key);
1495 parms.temp_path = res_get (zh->res, "setTmpDir");
1496 result = rset_create (rset_kind_temp, &parms);
1497 rsfd_result = rset_open (result, RSETF_WRITE);
1501 for (i = 1; i<rset_no; i++)
1510 cmp = key_compare_it (buf[i], buf[i-1]);
1513 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1514 buf[i-1], &term_index);
1519 if (buf[i-1]->seqno+1 != buf[i]->seqno)
1521 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1522 buf[i-1], &term_index);
1528 more[i] = rset_read (rset[i], rsfd[i], buf[i],
1535 rset_write (result, rsfd_result, buf[0]);
1536 more[0] = rset_read (*rset, *rsfd, *buf, &term_index);
1540 for (i = 0; i<rset_no; i++)
1542 rset_close (rset[i], rsfd[i]);
1545 rset_close (result, rsfd_result);
1554 char *normalize_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1555 const char *termz, NMEM stream, unsigned reg_id)
1558 AttrType truncation;
1559 int truncation_value;
1562 attr_init (&truncation, zapt, 5);
1563 truncation_value = attr_find (&truncation, NULL);
1565 switch (truncation_value)
1585 wrbuf = zebra_replace(zh->zebra_maps, reg_id, ex_list,
1586 termz, strlen(termz));
1588 return nmem_strdup(stream, termz);
1591 char *buf = (char*) nmem_malloc (stream, wrbuf_len(wrbuf)+1);
1592 memcpy (buf, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1593 buf[wrbuf_len(wrbuf)] = '\0';
1598 static RSET rpn_search_APT_phrase (ZebraHandle zh,
1599 Z_AttributesPlusTerm *zapt,
1600 const char *termz_org,
1601 oid_value attributeSet,
1603 int reg_type, int complete_flag,
1604 const char *rank_type,
1605 int num_bases, char **basenames)
1607 char term_dst[IT_MAX_WORD+1];
1608 RSET rset[60], result;
1609 int i, r, rset_no = 0;
1610 struct grep_info grep_info;
1611 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1612 const char *termp = termz;
1615 grep_info.term_no = 0;
1617 grep_info.isam_p_size = 0;
1618 grep_info.isam_p_buf = NULL;
1620 grep_info.reg_type = reg_type;
1624 logf (LOG_DEBUG, "APT_phrase termp=%s", termp);
1625 grep_info.isam_p_indx = 0;
1626 r = string_term (zh, zapt, &termp, attributeSet, stream, &grep_info,
1627 reg_type, complete_flag, num_bases, basenames,
1631 logf (LOG_DEBUG, "term: %s", term_dst);
1632 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1633 grep_info.isam_p_indx, term_dst,
1634 strlen(term_dst), rank_type);
1635 assert (rset[rset_no]);
1636 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1640 xfree(grep_info.term_no);
1642 xfree (grep_info.isam_p_buf);
1645 rset_null_parms parms;
1647 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1648 return rset_create (rset_kind_null, &parms);
1650 else if (rset_no == 1)
1652 result = rpn_prox (zh, rset, rset_no);
1653 for (i = 0; i<rset_no; i++)
1654 rset_delete (rset[i]);
1658 static RSET rpn_search_APT_or_list (ZebraHandle zh,
1659 Z_AttributesPlusTerm *zapt,
1660 const char *termz_org,
1661 oid_value attributeSet,
1663 int reg_type, int complete_flag,
1664 const char *rank_type,
1665 int num_bases, char **basenames)
1667 char term_dst[IT_MAX_WORD+1];
1668 RSET rset[60], result;
1669 int i, r, rset_no = 0;
1670 struct grep_info grep_info;
1671 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1672 const char *termp = termz;
1674 grep_info.term_no = 0;
1676 grep_info.isam_p_size = 0;
1677 grep_info.isam_p_buf = NULL;
1679 grep_info.reg_type = reg_type;
1683 logf (LOG_DEBUG, "APT_or_list termp=%s", termp);
1684 grep_info.isam_p_indx = 0;
1685 r = string_term (zh, zapt, &termp, attributeSet, stream, &grep_info,
1686 reg_type, complete_flag, num_bases, basenames,
1690 logf (LOG_DEBUG, "term: %s", term_dst);
1691 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1692 grep_info.isam_p_indx, term_dst,
1693 strlen(term_dst), rank_type);
1694 assert (rset[rset_no]);
1695 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1699 xfree(grep_info.term_no);
1701 xfree (grep_info.isam_p_buf);
1704 rset_null_parms parms;
1706 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1707 return rset_create (rset_kind_null, &parms);
1710 for (i = 1; i<rset_no; i++)
1712 rset_bool_parms bool_parms;
1714 bool_parms.rset_l = result;
1715 bool_parms.rset_r = rset[i];
1716 bool_parms.key_size = sizeof(struct it_key);
1717 bool_parms.cmp = key_compare_it;
1718 result = rset_create (rset_kind_or, &bool_parms);
1723 static RSET rpn_search_APT_and_list (ZebraHandle zh,
1724 Z_AttributesPlusTerm *zapt,
1725 const char *termz_org,
1726 oid_value attributeSet,
1728 int reg_type, int complete_flag,
1729 const char *rank_type,
1730 int num_bases, char **basenames)
1732 char term_dst[IT_MAX_WORD+1];
1733 RSET rset[60], result;
1734 int i, r, rset_no = 0;
1735 struct grep_info grep_info;
1736 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1737 const char *termp = termz;
1740 grep_info.term_no = 0;
1742 grep_info.isam_p_size = 0;
1743 grep_info.isam_p_buf = NULL;
1745 grep_info.reg_type = reg_type;
1749 logf (LOG_DEBUG, "APT_and_list termp=%s", termp);
1750 grep_info.isam_p_indx = 0;
1751 r = string_term (zh, zapt, &termp, attributeSet, stream, &grep_info,
1752 reg_type, complete_flag, num_bases, basenames,
1756 logf (LOG_DEBUG, "term: %s", term_dst);
1757 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1758 grep_info.isam_p_indx, term_dst,
1759 strlen(term_dst), rank_type);
1760 assert (rset[rset_no]);
1761 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1765 xfree(grep_info.term_no);
1767 xfree (grep_info.isam_p_buf);
1770 rset_null_parms parms;
1772 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1773 return rset_create (rset_kind_null, &parms);
1776 for (i = 1; i<rset_no; i++)
1778 rset_bool_parms bool_parms;
1780 bool_parms.rset_l = result;
1781 bool_parms.rset_r = rset[i];
1782 bool_parms.key_size = sizeof(struct it_key);
1783 bool_parms.cmp = key_compare_it;
1784 result = rset_create (rset_kind_and, &bool_parms);
1789 static int numeric_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1790 const char **term_sub,
1792 oid_value attributeSet,
1793 struct grep_info *grep_info,
1802 char *term_tmp = term_dict + strlen(term_dict);
1804 attr_init (&relation, zapt, 2);
1805 relation_value = attr_find (&relation, NULL);
1807 logf (LOG_DEBUG, "numeric relation value=%d", relation_value);
1809 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_tmp, 1,
1812 term_value = atoi (term_tmp);
1813 switch (relation_value)
1816 logf (LOG_DEBUG, "Relation <");
1817 gen_regular_rel (term_tmp, term_value-1, 1);
1820 logf (LOG_DEBUG, "Relation <=");
1821 gen_regular_rel (term_tmp, term_value, 1);
1824 logf (LOG_DEBUG, "Relation >=");
1825 gen_regular_rel (term_tmp, term_value, 0);
1828 logf (LOG_DEBUG, "Relation >");
1829 gen_regular_rel (term_tmp, term_value+1, 0);
1833 logf (LOG_DEBUG, "Relation =");
1834 sprintf (term_tmp, "(0*%d)", term_value);
1836 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_tmp);
1837 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info, max_pos,
1840 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1841 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1845 static int numeric_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1846 const char **term_sub,
1847 oid_value attributeSet, struct grep_info *grep_info,
1848 int reg_type, int complete_flag,
1849 int num_bases, char **basenames,
1852 char term_dict[2*IT_MAX_WORD+2];
1856 oid_value curAttributeSet = attributeSet;
1858 struct rpn_char_map_info rcmi;
1860 rpn_char_map_prepare (zh, reg_type, &rcmi);
1861 attr_init (&use, zapt, 1);
1862 use_value = attr_find (&use, &curAttributeSet);
1863 logf (LOG_DEBUG, "numeric_term, use value %d", use_value);
1865 if (use_value == -1)
1868 for (base_no = 0; base_no < num_bases; base_no++)
1871 data1_local_attribute *local_attr;
1872 int max_pos, prefix_len = 0;
1875 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
1877 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1878 curAttributeSet, use_value, r);
1885 if (zebraExplain_curDatabase (zh->zei, basenames[base_no]))
1887 zh->errCode = 109; /* Database unavailable */
1888 zh->errString = basenames[base_no];
1891 for (local_attr = attp.local_attributes; local_attr;
1892 local_attr = local_attr->next)
1898 ord = zebraExplain_lookupSU (zh->zei, attp.attset_ordinal,
1903 term_dict[prefix_len++] = '|';
1905 term_dict[prefix_len++] = '(';
1907 ord_len = key_SU_code (ord, ord_buf);
1908 for (i = 0; i<ord_len; i++)
1910 term_dict[prefix_len++] = 1;
1911 term_dict[prefix_len++] = ord_buf[i];
1919 term_dict[prefix_len++] = ')';
1920 term_dict[prefix_len++] = 1;
1921 term_dict[prefix_len++] = reg_type;
1922 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1923 term_dict[prefix_len] = '\0';
1924 if (!numeric_relation (zh, zapt, &termp, term_dict,
1925 attributeSet, grep_info, &max_pos, reg_type,
1930 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1934 static RSET rpn_search_APT_numeric (ZebraHandle zh,
1935 Z_AttributesPlusTerm *zapt,
1937 oid_value attributeSet,
1939 int reg_type, int complete_flag,
1940 const char *rank_type,
1941 int num_bases, char **basenames)
1943 char term_dst[IT_MAX_WORD+1];
1944 const char *termp = termz;
1945 RSET rset[60], result;
1946 int i, r, rset_no = 0;
1947 struct grep_info grep_info;
1950 grep_info.term_no = 0;
1952 grep_info.isam_p_size = 0;
1953 grep_info.isam_p_buf = NULL;
1955 grep_info.reg_type = reg_type;
1959 logf (LOG_DEBUG, "APT_numeric termp=%s", termp);
1960 grep_info.isam_p_indx = 0;
1961 r = numeric_term (zh, zapt, &termp, attributeSet, &grep_info,
1962 reg_type, complete_flag, num_bases, basenames,
1966 logf (LOG_DEBUG, "term: %s", term_dst);
1967 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1968 grep_info.isam_p_indx, term_dst,
1969 strlen(term_dst), rank_type);
1970 assert (rset[rset_no]);
1971 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1975 xfree(grep_info.term_no);
1977 xfree (grep_info.isam_p_buf);
1980 rset_null_parms parms;
1982 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1983 return rset_create (rset_kind_null, &parms);
1986 for (i = 1; i<rset_no; i++)
1988 rset_bool_parms bool_parms;
1990 bool_parms.rset_l = result;
1991 bool_parms.rset_r = rset[i];
1992 bool_parms.key_size = sizeof(struct it_key);
1993 bool_parms.cmp = key_compare_it;
1994 result = rset_create (rset_kind_and, &bool_parms);
1999 static RSET rpn_search_APT_local (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2001 oid_value attributeSet,
2003 const char *rank_type)
2008 rset_temp_parms parms;
2010 parms.rset_term = rset_term_create (termz, -1, rank_type);
2011 parms.key_size = sizeof (struct it_key);
2012 parms.temp_path = res_get (zh->res, "setTmpDir");
2013 result = rset_create (rset_kind_temp, &parms);
2014 rsfd = rset_open (result, RSETF_WRITE);
2016 key.sysno = atoi (termz);
2020 rset_write (result, rsfd, &key);
2021 rset_close (result, rsfd);
2025 static RSET rpn_sort_spec (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2026 oid_value attributeSet, NMEM stream,
2027 Z_SortKeySpecList *sort_sequence,
2028 const char *rank_type)
2030 rset_null_parms parms;
2032 int sort_relation_value;
2033 AttrType sort_relation_type;
2038 Z_AttributeElement *ae;
2042 attr_init (&sort_relation_type, zapt, 7);
2043 sort_relation_value = attr_find (&sort_relation_type, &attributeSet);
2045 attr_init (&use_type, zapt, 1);
2046 use_value = attr_find (&use_type, &attributeSet);
2048 if (!sort_sequence->specs)
2050 sort_sequence->num_specs = 10;
2051 sort_sequence->specs = (Z_SortKeySpec **)
2052 nmem_malloc (stream, sort_sequence->num_specs *
2053 sizeof(*sort_sequence->specs));
2054 for (i = 0; i<sort_sequence->num_specs; i++)
2055 sort_sequence->specs[i] = 0;
2057 if (zapt->term->which != Z_Term_general)
2060 i = atoi_n ((char *) zapt->term->u.general->buf,
2061 zapt->term->u.general->len);
2062 if (i >= sort_sequence->num_specs)
2065 oe.proto = PROTO_Z3950;
2066 oe.oclass = CLASS_ATTSET;
2067 oe.value = attributeSet;
2068 if (!oid_ent_to_oid (&oe, oid))
2071 sks = (Z_SortKeySpec *) nmem_malloc (stream, sizeof(*sks));
2072 sks->sortElement = (Z_SortElement *)
2073 nmem_malloc (stream, sizeof(*sks->sortElement));
2074 sks->sortElement->which = Z_SortElement_generic;
2075 sk = sks->sortElement->u.generic = (Z_SortKey *)
2076 nmem_malloc (stream, sizeof(*sk));
2077 sk->which = Z_SortKey_sortAttributes;
2078 sk->u.sortAttributes = (Z_SortAttributes *)
2079 nmem_malloc (stream, sizeof(*sk->u.sortAttributes));
2081 sk->u.sortAttributes->id = oid;
2082 sk->u.sortAttributes->list = (Z_AttributeList *)
2083 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list));
2084 sk->u.sortAttributes->list->num_attributes = 1;
2085 sk->u.sortAttributes->list->attributes = (Z_AttributeElement **)
2086 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list->attributes));
2087 ae = *sk->u.sortAttributes->list->attributes = (Z_AttributeElement *)
2088 nmem_malloc (stream, sizeof(**sk->u.sortAttributes->list->attributes));
2089 ae->attributeSet = 0;
2090 ae->attributeType = (int *)
2091 nmem_malloc (stream, sizeof(*ae->attributeType));
2092 *ae->attributeType = 1;
2093 ae->which = Z_AttributeValue_numeric;
2094 ae->value.numeric = (int *)
2095 nmem_malloc (stream, sizeof(*ae->value.numeric));
2096 *ae->value.numeric = use_value;
2098 sks->sortRelation = (int *)
2099 nmem_malloc (stream, sizeof(*sks->sortRelation));
2100 if (sort_relation_value == 1)
2101 *sks->sortRelation = Z_SortRelation_ascending;
2102 else if (sort_relation_value == 2)
2103 *sks->sortRelation = Z_SortRelation_descending;
2105 *sks->sortRelation = Z_SortRelation_ascending;
2107 sks->caseSensitivity = (int *)
2108 nmem_malloc (stream, sizeof(*sks->caseSensitivity));
2109 *sks->caseSensitivity = 0;
2112 sks->which = Z_SortKeySpec_null;
2113 sks->u.null = odr_nullval ();
2115 sks->missingValueAction = 0;
2118 sort_sequence->specs[i] = sks;
2120 parms.rset_term = rset_term_create ("", -1, rank_type);
2121 return rset_create (rset_kind_null, &parms);
2125 static RSET rpn_search_APT (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2126 oid_value attributeSet, NMEM stream,
2127 Z_SortKeySpecList *sort_sequence,
2128 int num_bases, char **basenames)
2131 char *search_type = NULL;
2132 char *rank_type = NULL;
2135 char termz[IT_MAX_WORD+1];
2137 zebra_maps_attr (zh->zebra_maps, zapt, ®_id, &search_type,
2138 &rank_type, &complete_flag, &sort_flag);
2140 logf (LOG_DEBUG, "reg_id=%c", reg_id);
2141 logf (LOG_DEBUG, "complete_flag=%d", complete_flag);
2142 logf (LOG_DEBUG, "search_type=%s", search_type);
2143 logf (LOG_DEBUG, "rank_type=%s", rank_type);
2145 if (zapt->term->which != Z_Term_general)
2150 trans_term (zh, zapt, termz);
2153 return rpn_sort_spec (zh, zapt, attributeSet, stream, sort_sequence,
2156 if (!strcmp (search_type, "phrase"))
2158 return rpn_search_APT_phrase (zh, zapt, termz, attributeSet, stream,
2159 reg_id, complete_flag, rank_type,
2160 num_bases, basenames);
2162 else if (!strcmp (search_type, "and-list"))
2164 return rpn_search_APT_and_list (zh, zapt, termz, attributeSet, stream,
2165 reg_id, complete_flag, rank_type,
2166 num_bases, basenames);
2168 else if (!strcmp (search_type, "or-list"))
2170 return rpn_search_APT_or_list (zh, zapt, termz, attributeSet, stream,
2171 reg_id, complete_flag, rank_type,
2172 num_bases, basenames);
2174 else if (!strcmp (search_type, "local"))
2176 return rpn_search_APT_local (zh, zapt, termz, attributeSet, stream,
2179 else if (!strcmp (search_type, "numeric"))
2181 return rpn_search_APT_numeric (zh, zapt, termz, attributeSet, stream,
2182 reg_id, complete_flag, rank_type,
2183 num_bases, basenames);
2189 static RSET rpn_search_structure (ZebraHandle zh, Z_RPNStructure *zs,
2190 oid_value attributeSet, NMEM stream,
2191 Z_SortKeySpecList *sort_sequence,
2192 int num_bases, char **basenames)
2195 if (zs->which == Z_RPNStructure_complex)
2197 Z_Operator *zop = zs->u.complex->roperator;
2198 rset_bool_parms bool_parms;
2200 bool_parms.rset_l = rpn_search_structure (zh, zs->u.complex->s1,
2201 attributeSet, stream,
2203 num_bases, basenames);
2204 if (bool_parms.rset_l == NULL)
2206 bool_parms.rset_r = rpn_search_structure (zh, zs->u.complex->s2,
2207 attributeSet, stream,
2209 num_bases, basenames);
2210 if (bool_parms.rset_r == NULL)
2212 rset_delete (bool_parms.rset_l);
2215 bool_parms.key_size = sizeof(struct it_key);
2216 bool_parms.cmp = key_compare_it;
2220 case Z_Operator_and:
2221 r = rset_create (rset_kind_and, &bool_parms);
2224 r = rset_create (rset_kind_or, &bool_parms);
2226 case Z_Operator_and_not:
2227 r = rset_create (rset_kind_not, &bool_parms);
2229 case Z_Operator_prox:
2231 if (zop->u.prox->which != Z_ProximityOperator_known)
2237 if (zop->u.prox->which != Z_ProxCode_known)
2245 if (*zop->u.prox->u.known != Z_ProxUnit_word)
2247 char *val = (char *) nmem_malloc (stream, 16);
2249 zh->errString = val;
2250 sprintf (val, "%d", *zop->u.prox->u.known);
2254 if (*zop->u.prox->proximityUnitCode != Z_ProxUnit_word)
2256 char *val = (char *) nmem_malloc (stream, 16);
2258 zh->errString = val;
2259 sprintf (val, "%d", *zop->u.prox->proximityUnitCode);
2263 r = rpn_proximity (zh, bool_parms.rset_l, bool_parms.rset_r,
2264 *zop->u.prox->ordered,
2265 (!zop->u.prox->exclusion ? 0 :
2266 *zop->u.prox->exclusion),
2267 *zop->u.prox->relationType,
2268 *zop->u.prox->distance);
2275 else if (zs->which == Z_RPNStructure_simple)
2277 if (zs->u.simple->which == Z_Operand_APT)
2279 logf (LOG_DEBUG, "rpn_search_APT");
2280 r = rpn_search_APT (zh, zs->u.simple->u.attributesPlusTerm,
2281 attributeSet, stream, sort_sequence,
2282 num_bases, basenames);
2284 else if (zs->u.simple->which == Z_Operand_resultSetId)
2286 logf (LOG_DEBUG, "rpn_search_ref");
2287 r = resultSetRef (zh, zs->u.simple->u.resultSetId);
2289 r = rset_create (rset_kind_null, NULL);
2306 RSET rpn_search (ZebraHandle zh, NMEM nmem,
2307 Z_RPNQuery *rpn, int num_bases, char **basenames,
2308 const char *setname,
2313 oid_value attributeSet;
2314 Z_SortKeySpecList *sort_sequence;
2318 zh->errString = NULL;
2321 sort_sequence = (Z_SortKeySpecList *)
2322 nmem_malloc (nmem, sizeof(*sort_sequence));
2323 sort_sequence->num_specs = 10;
2324 sort_sequence->specs = (Z_SortKeySpec **)
2325 nmem_malloc (nmem, sort_sequence->num_specs *
2326 sizeof(*sort_sequence->specs));
2327 for (i = 0; i<sort_sequence->num_specs; i++)
2328 sort_sequence->specs[i] = 0;
2330 attrset = oid_getentbyoid (rpn->attributeSetId);
2331 attributeSet = attrset->value;
2332 rset = rpn_search_structure (zh, rpn->RPNStructure, attributeSet,
2333 nmem, sort_sequence, num_bases, basenames);
2338 logf (LOG_DEBUG, "search error: %d", zh->errCode);
2340 for (i = 0; sort_sequence->specs[i]; i++)
2342 sort_sequence->num_specs = i;
2344 resultSetRank (zh, sset, rset);
2347 logf (LOG_DEBUG, "resultSetSortSingle in rpn_search");
2348 resultSetSortSingle (zh, nmem, sset, rset,
2349 sort_sequence, &sort_status);
2352 logf (LOG_DEBUG, "resultSetSortSingle status = %d", zh->errCode);
2358 struct scan_info_entry {
2364 struct scan_info_entry *list;
2370 static int scan_handle (char *name, const char *info, int pos, void *client)
2372 int len_prefix, idx;
2373 struct scan_info *scan_info = (struct scan_info *) client;
2375 len_prefix = strlen(scan_info->prefix);
2376 if (memcmp (name, scan_info->prefix, len_prefix))
2378 if (pos > 0) idx = scan_info->after - pos + scan_info->before;
2381 scan_info->list[idx].term = (char *)
2382 odr_malloc (scan_info->odr, strlen(name + len_prefix)+1);
2383 strcpy (scan_info->list[idx].term, name + len_prefix);
2384 assert (*info == sizeof(ISAMS_P));
2385 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAMS_P));
2389 static void scan_term_untrans (ZebraHandle zh, NMEM stream, int reg_type,
2390 char **dst, const char *src)
2392 char term_dst[1024];
2394 term_untrans (zh, reg_type, term_dst, src);
2396 *dst = (char *) nmem_malloc (stream, strlen(term_dst)+1);
2397 strcpy (*dst, term_dst);
2400 static void count_set (RSET r, int *count)
2408 logf (LOG_DEBUG, "count_set");
2411 rfd = rset_open (r, RSETF_READ);
2412 while (rset_read (r, rfd, &key, &term_index))
2414 if (key.sysno != psysno)
2421 rset_close (r, rfd);
2422 logf (LOG_DEBUG, "%d keys, %d records", kno, *count);
2425 void rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
2426 oid_value attributeset,
2427 int num_bases, char **basenames,
2428 int *position, int *num_entries, ZebraScanEntry **list,
2432 int pos = *position;
2433 int num = *num_entries;
2437 char termz[IT_MAX_WORD+20];
2440 struct scan_info *scan_info_array;
2441 ZebraScanEntry *glist;
2442 int ords[32], ord_no = 0;
2446 char *search_type = NULL;
2447 char *rank_type = NULL;
2452 if (attributeset == VAL_NONE)
2453 attributeset = VAL_BIB1;
2455 logf (LOG_DEBUG, "position = %d, num = %d", pos, num);
2457 attr_init (&use, zapt, 1);
2458 use_value = attr_find (&use, &attributeset);
2460 if (zebra_maps_attr (zh->zebra_maps, zapt, ®_id, &search_type,
2461 &rank_type, &complete_flag, &sort_flag))
2467 if (use_value == -1)
2469 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
2473 data1_local_attribute *local_attr;
2475 if ((r=att_getentbyatt (zh, &attp, attributeset, use_value)))
2477 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
2478 attributeset, use_value);
2486 if (zebraExplain_curDatabase (zh->zei, basenames[base_no]))
2488 zh->errString = basenames[base_no];
2489 zh->errCode = 109; /* Database unavailable */
2492 for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
2493 local_attr = local_attr->next)
2497 ord = zebraExplain_lookupSU (zh->zei, attp.attset_ordinal,
2500 ords[ord_no++] = ord;
2508 /* prepare dictionary scanning */
2511 scan_info_array = (struct scan_info *)
2512 odr_malloc (stream, ord_no * sizeof(*scan_info_array));
2513 for (i = 0; i < ord_no; i++)
2515 int j, prefix_len = 0;
2516 int before_tmp = before, after_tmp = after;
2517 struct scan_info *scan_info = scan_info_array + i;
2518 struct rpn_char_map_info rcmi;
2520 rpn_char_map_prepare (zh, reg_id, &rcmi);
2522 scan_info->before = before;
2523 scan_info->after = after;
2524 scan_info->odr = stream;
2526 scan_info->list = (struct scan_info_entry *)
2527 odr_malloc (stream, (before+after) * sizeof(*scan_info->list));
2528 for (j = 0; j<before+after; j++)
2529 scan_info->list[j].term = NULL;
2531 prefix_len += key_SU_code (ords[i], termz + prefix_len);
2532 termz[prefix_len++] = reg_id;
2533 termz[prefix_len] = 0;
2534 strcpy (scan_info->prefix, termz);
2536 trans_scan_term (zh, zapt, termz+prefix_len, reg_id);
2538 dict_scan (zh->dict, termz, &before_tmp, &after_tmp, scan_info,
2541 glist = (ZebraScanEntry *)
2542 odr_malloc (stream, (before+after)*sizeof(*glist));
2544 /* consider terms after main term */
2545 for (i = 0; i < ord_no; i++)
2549 for (i = 0; i<after; i++)
2552 const char *mterm = NULL;
2556 for (j = 0; j < ord_no; j++)
2558 if (ptr[j] < before+after &&
2559 (tst=scan_info_array[j].list[ptr[j]].term) &&
2560 (!mterm || strcmp (tst, mterm) < 0))
2568 scan_term_untrans (zh, stream->mem, reg_id,
2569 &glist[i+before].term, mterm);
2570 rset = rset_trunc (zh, &scan_info_array[j0].list[ptr[j0]].isam_p, 1,
2571 glist[i+before].term, strlen(glist[i+before].term),
2575 for (j = j0+1; j<ord_no; j++)
2577 if (ptr[j] < before+after &&
2578 (tst=scan_info_array[j].list[ptr[j]].term) &&
2579 !strcmp (tst, mterm))
2581 rset_bool_parms bool_parms;
2585 rset_trunc (zh, &scan_info_array[j].list[ptr[j]].isam_p, 1,
2586 glist[i+before].term,
2587 strlen(glist[i+before].term), NULL);
2589 bool_parms.key_size = sizeof(struct it_key);
2590 bool_parms.cmp = key_compare_it;
2591 bool_parms.rset_l = rset;
2592 bool_parms.rset_r = rset2;
2594 rset = rset_create (rset_kind_or, &bool_parms);
2599 count_set (rset, &glist[i+before].occurrences);
2604 *num_entries -= (after-i);
2608 /* consider terms before main term */
2609 for (i = 0; i<ord_no; i++)
2612 for (i = 0; i<before; i++)
2615 const char *mterm = NULL;
2619 for (j = 0; j <ord_no; j++)
2621 if (ptr[j] < before &&
2622 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2623 (!mterm || strcmp (tst, mterm) > 0))
2632 scan_term_untrans (zh, stream->mem, reg_id,
2633 &glist[before-1-i].term, mterm);
2636 (zh, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1,
2637 glist[before-1-i].term, strlen(glist[before-1-i].term),
2642 for (j = j0+1; j<ord_no; j++)
2644 if (ptr[j] < before &&
2645 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2646 !strcmp (tst, mterm))
2648 rset_bool_parms bool_parms;
2651 rset2 = rset_trunc (zh,
2652 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1,
2653 glist[before-1-i].term,
2654 strlen(glist[before-1-i].term), NULL);
2656 bool_parms.key_size = sizeof(struct it_key);
2657 bool_parms.cmp = key_compare_it;
2658 bool_parms.rset_l = rset;
2659 bool_parms.rset_r = rset2;
2661 rset = rset_create (rset_kind_or, &bool_parms);
2666 count_set (rset, &glist[before-1-i].occurrences);
2676 *list = glist + i; /* list is set to first 'real' entry */
2678 logf (LOG_DEBUG, "position = %d, num_entries = %d",
2679 *position, *num_entries);
2681 logf (LOG_DEBUG, "scan error: %d", zh->errCode);