2 * Copyright (C) 1995-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.96 1999-09-23 10:05:05 adam
8 * Implemented structure=105 searching.
10 * Revision 1.95 1999/09/07 07:19:21 adam
11 * Work on character mapping. Implemented replace rules.
13 * Revision 1.94 1999/07/20 13:59:18 adam
14 * Fixed bug that occurred when phrases had 0 hits.
16 * Revision 1.93 1999/06/17 14:38:40 adam
17 * Bug fix: Scan SEGV'ed when getting unknown use attribute.
19 * Revision 1.92 1999/05/26 07:49:13 adam
22 * Revision 1.91 1999/02/02 14:51:13 adam
23 * Updated WIN32 code specific sections. Changed header.
25 * Revision 1.90 1998/11/16 16:03:43 adam
26 * Moved loggin utilities to Yaz. Was implemented in file zlogs.c.
28 * Revision 1.89 1998/11/16 10:11:55 adam
29 * Added addtional info for error 114 - unsupported use attribute.
31 * Revision 1.88 1998/10/18 07:54:52 adam
32 * Additional info added for diagnostics 114 (Unsupported use attribute) and
33 * 121 (Unsupported attribute set).
35 * Revision 1.87 1998/09/28 11:19:12 adam
36 * Fix for Compiled ASN.1.
38 * Revision 1.86 1998/09/22 10:48:20 adam
39 * Minor changes in search API.
41 * Revision 1.85 1998/09/22 10:03:43 adam
42 * Changed result sets to be persistent in the sense that they can
43 * be re-searched if needed.
44 * Fixed memory leak in rsm_or.
46 * Revision 1.84 1998/09/18 12:41:00 adam
47 * Fixed bug with numerical relations.
49 * Revision 1.83 1998/09/02 13:53:19 adam
50 * Extra parameter decode added to search routines to implement
53 * Revision 1.82 1998/06/26 11:16:40 quinn
54 * Added support (un-optimised) for left and left/right truncation
56 * Revision 1.81 1998/06/24 12:16:14 adam
57 * Support for relations on text operands. Open range support in
58 * DFA module (i.e. [-j], [g-]).
60 * Revision 1.80 1998/06/23 15:33:34 adam
61 * Added feature to specify sort criteria in query (type 7 specifies
64 * Revision 1.79 1998/06/22 11:35:09 adam
67 * Revision 1.78 1998/06/08 14:43:17 adam
68 * Added suport for EXPLAIN Proxy servers - added settings databasePath
69 * and explainDatabase to facilitate this. Increased maximum number
70 * of databases and attributes in one register.
72 * Revision 1.77 1998/05/20 10:12:22 adam
73 * Implemented automatic EXPLAIN database maintenance.
74 * Modified Zebra to work with ASN.1 compiled version of YAZ.
76 * Revision 1.76 1998/04/02 14:35:29 adam
77 * First version of Zebra that works with compiled ASN.1.
79 * Revision 1.75 1998/03/05 08:45:13 adam
80 * New result set model and modular ranking system. Moved towards
81 * descent server API. System information stored as "SGML" records.
83 * Revision 1.74 1998/02/10 12:03:06 adam
86 * Revision 1.73 1998/01/29 13:40:11 adam
87 * Better logging for scan service.
89 * Revision 1.72 1998/01/07 13:53:41 adam
90 * Queries using simple ranked operands returns right number of hits.
92 * Revision 1.71 1997/12/18 10:54:24 adam
93 * New method result set method rs_hits that returns the number of
94 * hits in result-set (if known). The ranked result set returns real
95 * number of hits but only when not combined with other operands.
97 * Revision 1.70 1997/10/31 12:34:43 adam
98 * Changed a few log statements.
100 * Revision 1.69 1997/10/29 12:05:02 adam
101 * Server produces diagnostic "Unsupported Attribute Set" when appropriate.
103 * Revision 1.68 1997/10/27 14:33:06 adam
104 * Moved towards generic character mapping depending on "structure"
105 * field in abstract syntax file. Fixed a few memory leaks. Fixed
106 * bug with negative integers when doing searches with relational
109 * Revision 1.67 1997/09/29 09:06:10 adam
110 * Removed one static var in order to make this module thread safe.
112 * Revision 1.66 1997/09/25 14:58:03 adam
115 * Revision 1.65 1997/09/22 12:39:06 adam
116 * Added get_pos method for the ranked result sets.
118 * Revision 1.64 1997/09/18 08:59:20 adam
119 * Extra generic handle for the character mapping routines.
121 * Revision 1.63 1997/09/17 12:19:18 adam
122 * Zebra version corresponds to YAZ version 1.4.
123 * Changed Zebra server so that it doesn't depend on global common_resource.
125 * Revision 1.62 1997/09/05 15:30:09 adam
126 * Changed prototype for chr_map_input - added const.
127 * Added support for C++, headers uses extern "C" for public definitions.
129 * Revision 1.61 1997/02/10 10:21:14 adam
130 * Bug fix: in search terms character (^) wasn't observed.
132 * Revision 1.60 1997/01/31 11:10:34 adam
133 * Bug fix: Leading and trailing white space weren't removed in scan tokens.
135 * Revision 1.59 1997/01/17 11:31:46 adam
136 * Bug fix: complete phrase search didn't work.
138 * Revision 1.58 1996/12/23 15:30:45 adam
139 * Work on truncation.
140 * Bug fix: result sets weren't deleted after server shut down.
142 * Revision 1.57 1996/11/11 13:38:02 adam
143 * Added proximity support in search.
145 * Revision 1.56 1996/11/08 11:10:32 adam
146 * Buffers used during file match got bigger.
147 * Compressed ISAM support everywhere.
148 * Bug fixes regarding masking characters in queries.
149 * Redesigned Regexp-2 queries.
151 * Revision 1.55 1996/11/04 14:07:44 adam
152 * Moved truncation code to trunc.c.
154 * Revision 1.54 1996/10/29 14:09:52 adam
155 * Use of cisam system - enabled if setting isamc is 1.
157 * Revision 1.53 1996/06/26 09:21:43 adam
158 * Bug fix: local attribute set wasn't obeyed in scan.
160 * Revision 1.52 1996/06/17 14:26:20 adam
161 * Function gen_regular_rel changed to handle negative numbers.
163 * Revision 1.51 1996/06/11 10:54:15 quinn
166 * Revision 1.50 1996/06/07 08:51:53 adam
167 * Bug fix: Character mapping was broken (introducued by last revision).
169 * Revision 1.49 1996/06/04 10:18:11 adam
170 * Search/scan uses character mapping module.
172 * Revision 1.48 1996/05/28 15:15:01 adam
173 * Bug fix: Didn't handle unknown database correctly.
175 * Revision 1.47 1996/05/15 18:36:28 adam
176 * Function trans_term transforms unsearchable characters to blanks.
178 * Revision 1.46 1996/05/15 11:57:56 adam
179 * Fixed bug introduced by set/field mapping in search operations.
181 * Revision 1.45 1996/05/14 11:34:00 adam
182 * Scan support in multiple registers/databases.
184 * Revision 1.44 1996/05/14 06:16:44 adam
185 * Compact use/set bytes used in search service.
187 * Revision 1.43 1996/05/09 09:54:43 adam
188 * Server supports maps from one logical attributes to a list of physical
190 * The extraction process doesn't make space consuming 'any' keys.
192 * Revision 1.42 1996/05/09 07:28:56 quinn
193 * Work towards phrases and multiple registers
195 * Revision 1.41 1996/03/20 09:36:43 adam
196 * Function dict_lookup_grep got extra parameter, init_pos, which marks
197 * from which position in pattern approximate pattern matching should occur.
198 * Approximate pattern matching is used in relevance=re-2.
200 * Revision 1.40 1996/02/02 13:44:44 adam
201 * The public dictionary functions simply use char instead of Dict_char
202 * to represent search strings. Dict_char is used internally only.
204 * Revision 1.39 1996/01/03 16:22:13 quinn
205 * operator->roperator
207 * Revision 1.38 1995/12/11 09:12:55 adam
208 * The rec_get function returns NULL if record doesn't exist - will
209 * happen in the server if the result set records have been deleted since
210 * the creation of the set (i.e. the search).
211 * The server saves a result temporarily if it is 'volatile', i.e. the
212 * set is register dependent.
214 * Revision 1.37 1995/12/06 15:05:28 adam
215 * More verbose in count_set.
217 * Revision 1.36 1995/12/06 12:41:27 adam
218 * New command 'stat' for the index program.
219 * Filenames can be read from stdin by specifying '-'.
220 * Bug fix/enhancement of the transformation from terms to regular
221 * expressons in the search engine.
223 * Revision 1.35 1995/11/27 09:29:00 adam
224 * Bug fixes regarding conversion to regular expressions.
226 * Revision 1.34 1995/11/16 17:00:56 adam
227 * Better logging of rpn query.
229 * Revision 1.33 1995/11/01 13:58:28 quinn
230 * Moving data1 to yaz/retrieval
232 * Revision 1.32 1995/10/27 14:00:11 adam
233 * Implemented detection of database availability.
235 * Revision 1.31 1995/10/17 18:02:10 adam
236 * New feature: databases. Implemented as prefix to words in dictionary.
238 * Revision 1.30 1995/10/16 09:32:38 adam
239 * More work on relational op.
241 * Revision 1.29 1995/10/13 16:01:49 adam
244 * Revision 1.28 1995/10/13 12:26:43 adam
245 * Optimization of truncation.
247 * Revision 1.27 1995/10/12 17:07:22 adam
250 * Revision 1.26 1995/10/12 12:40:54 adam
251 * Bug fixes in rpn_prox.
253 * Revision 1.25 1995/10/10 13:59:24 adam
254 * Function rset_open changed its wflag parameter to general flags.
256 * Revision 1.24 1995/10/09 16:18:37 adam
257 * Function dict_lookup_grep got extra client data parameter.
259 * Revision 1.23 1995/10/06 16:33:37 adam
260 * Use attribute mappings.
262 * Revision 1.22 1995/10/06 15:07:39 adam
263 * Structure 'local-number' handled.
265 * Revision 1.21 1995/10/06 13:52:06 adam
266 * Bug fixes. Handler may abort further scanning.
268 * Revision 1.20 1995/10/06 11:06:33 adam
269 * Scan entries include 'occurrences' now.
271 * Revision 1.19 1995/10/06 10:43:56 adam
272 * Scan added. 'occurrences' in scan entries not set yet.
274 * Revision 1.18 1995/10/04 16:57:20 adam
275 * Key input and merge sort in one pass.
277 * Revision 1.17 1995/10/04 12:55:17 adam
278 * Bug fix in ranked search. Use=Any keys inserted.
280 * Revision 1.16 1995/10/02 16:24:40 adam
281 * Use attribute actually used in search requests.
283 * Revision 1.15 1995/10/02 15:18:52 adam
284 * New member in recRetrieveCtrl: diagnostic.
286 * Revision 1.14 1995/09/28 12:10:32 adam
287 * Bug fixes. Field prefix used in queries.
289 * Revision 1.13 1995/09/18 14:17:50 adam
292 * Revision 1.12 1995/09/15 14:45:21 adam
294 * Work on truncation.
296 * Revision 1.11 1995/09/14 11:53:27 adam
297 * First work on regular expressions/truncations.
299 * Revision 1.10 1995/09/11 15:23:26 adam
300 * More work on relevance search.
302 * Revision 1.9 1995/09/11 13:09:35 adam
303 * More work on relevance feedback.
305 * Revision 1.8 1995/09/08 14:52:27 adam
306 * Minor changes. Dictionary is lower case now.
308 * Revision 1.7 1995/09/07 13:58:36 adam
309 * New parameter: result-set file descriptor (RSFD) to support multiple
310 * positions within the same result-set.
311 * Boolean operators: and, or, not implemented.
312 * Result-set references.
314 * Revision 1.6 1995/09/06 16:11:18 adam
315 * Option: only one word key per file.
317 * Revision 1.5 1995/09/06 10:33:04 adam
318 * More work on present. Some log messages removed.
320 * Revision 1.4 1995/09/05 15:28:40 adam
321 * More work on search engine.
323 * Revision 1.3 1995/09/04 15:20:22 adam
326 * Revision 1.2 1995/09/04 12:33:43 adam
327 * Various cleanup. YAZ util used instead.
329 * Revision 1.1 1995/09/04 09:10:40 adam
330 * More work on index add/del/update.
331 * Merge sort implemented.
332 * Initial work on z39 server.
351 struct rpn_char_map_info {
356 static const char **rpn_char_map_handler (void *vp, const char **from, int len)
358 struct rpn_char_map_info *p = (struct rpn_char_map_info *) vp;
359 return zebra_maps_input (p->zm, p->reg_type, from, len);
362 static void rpn_char_map_prepare (ZebraHandle zh, int reg_type,
363 struct rpn_char_map_info *map_info)
365 map_info->zm = zh->zebra_maps;
366 map_info->reg_type = reg_type;
367 dict_grep_cmap (zh->dict, map_info, rpn_char_map_handler);
374 Z_AttributesPlusTerm *zapt;
377 static int attr_find (AttrType *src, oid_value *attributeSetP)
382 num_attributes = src->zapt->attributes->num_attributes;
384 num_attributes = src->zapt->num_attributes;
386 while (src->major < num_attributes)
388 Z_AttributeElement *element;
391 element = src->zapt->attributes->attributes[src->major];
393 element = src->zapt->attributeList[src->major];
395 if (src->type == *element->attributeType)
397 switch (element->which)
399 case Z_AttributeValue_numeric:
401 if (element->attributeSet && attributeSetP)
405 attrset = oid_getentbyoid (element->attributeSet);
406 *attributeSetP = attrset->value;
408 return *element->value.numeric;
410 case Z_AttributeValue_complex:
411 if (src->minor >= element->value.complex->num_list ||
412 element->value.complex->list[src->minor]->which !=
413 Z_StringOrNumeric_numeric)
416 if (element->attributeSet && attributeSetP)
420 attrset = oid_getentbyoid (element->attributeSet);
421 *attributeSetP = attrset->value;
423 return *element->value.complex->list[src->minor-1]->u.numeric;
433 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
455 static void term_untrans (ZebraHandle zh, int reg_type,
456 char *dst, const char *src)
460 const char *cp = zebra_maps_output (zh->zebra_maps, reg_type, &src);
470 static void add_isam_p (const char *name, const char *info,
473 if (p->isam_p_indx == p->isam_p_size)
475 ISAM_P *new_isam_p_buf;
479 p->isam_p_size = 2*p->isam_p_size + 100;
480 new_isam_p_buf = (ISAM_P *) xmalloc (sizeof(*new_isam_p_buf) *
484 memcpy (new_isam_p_buf, p->isam_p_buf,
485 p->isam_p_indx * sizeof(*p->isam_p_buf));
486 xfree (p->isam_p_buf);
488 p->isam_p_buf = new_isam_p_buf;
491 new_term_no = (int *) xmalloc (sizeof(*new_term_no) *
495 memcpy (new_term_no, p->isam_p_buf,
496 p->isam_p_indx * sizeof(*p->term_no));
499 p->term_no = new_term_no;
502 assert (*info == sizeof(*p->isam_p_buf));
503 memcpy (p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
506 term_untrans (p->zh, p->reg_type, term_tmp, name+2);
507 logf (LOG_DEBUG, "grep: %s", term_tmp);
512 static int grep_handle (char *name, const char *info, void *p)
514 add_isam_p (name, info, (struct grep_info *) p);
518 static int term_pre (ZebraMaps zebra_maps, int reg_type, const char **src,
519 const char *ct1, const char *ct2)
521 const char *s1, *s0 = *src;
524 /* skip white space */
527 if (ct1 && strchr (ct1, *s0))
529 if (ct2 && strchr (ct2, *s0))
532 map = zebra_maps_input (zebra_maps, reg_type, &s1, strlen(s1));
533 if (**map != *CHR_SPACE)
541 /* term_100: handle term, where trunc=none (no operators at all) */
542 static int term_100 (ZebraMaps zebra_maps, int reg_type,
543 const char **src, char *dst, int space_split,
551 if (!term_pre (zebra_maps, reg_type, src, NULL, NULL))
557 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
558 if (space_split && **map == *CHR_SPACE)
562 if (!isalnum (*s1) && *s1 != '-')
574 /* term_101: handle term, where trunc=Process # */
575 static int term_101 (ZebraMaps zebra_maps, int reg_type,
576 const char **src, char *dst, int space_split,
584 if (!term_pre (zebra_maps, reg_type, src, "#", "#"))
593 dst_term[j++] = *s0++;
598 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
599 if (space_split && **map == *CHR_SPACE)
611 dst_term[j++] = '\0';
616 /* term_103: handle term, where trunc=re-2 (regular expressions) */
617 static int term_103 (ZebraMaps zebra_maps, int reg_type, const char **src,
618 char *dst, int *errors, int space_split,
626 if (!term_pre (zebra_maps, reg_type, src, "^\\()[].*+?|", "("))
629 if (errors && *s0 == '+' && s0[1] && s0[2] == '+' && s0[3] &&
632 *errors = s0[1] - '0';
639 if (strchr ("^\\()[].*+?|-", *s0))
647 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
648 if (**map == *CHR_SPACE)
665 /* term_103: handle term, where trunc=re-1 (regular expressions) */
666 static int term_102 (ZebraMaps zebra_maps, int reg_type, const char **src,
667 char *dst, int space_split, char *dst_term)
669 return term_103 (zebra_maps, reg_type, src, dst, NULL, space_split,
674 /* term_104: handle term, where trunc=Process # and ! */
675 static int term_104 (ZebraMaps zebra_maps, int reg_type,
676 const char **src, char *dst, int space_split,
684 if (!term_pre (zebra_maps, reg_type, src, "#!", "#!"))
693 dst_term[j++] = *s0++;
698 dst_term[j++] = *s0++;
702 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
703 if (space_split && **map == *CHR_SPACE)
715 dst_term[j++] = '\0';
720 /* term_105: handle term, where trunc=Process # and ! and right trunc */
721 static int term_105 (ZebraMaps zebra_maps, int reg_type,
722 const char **src, char *dst, int space_split,
730 if (!term_pre (zebra_maps, reg_type, src, "*!", "*!"))
739 dst_term[j++] = *s0++;
744 dst_term[j++] = *s0++;
748 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
749 if (space_split && **map == *CHR_SPACE)
764 dst_term[j++] = '\0';
770 /* gen_regular_rel - generate regular expression from relation
771 * val: border value (inclusive)
772 * islt: 1 if <=; 0 if >=.
774 static void gen_regular_rel (char *dst, int val, int islt)
781 logf (LOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
785 strcpy (dst, "(-[0-9]+|(");
793 strcpy (dst, "([0-9]+|-(");
794 dst_p = strlen (dst);
804 dst_p = strlen (dst);
805 sprintf (numstr, "%d", val);
806 for (w = strlen(numstr); --w >= 0; pos++)
825 strcpy (dst + dst_p, numstr);
826 dst_p = strlen(dst) - pos - 1;
854 for (i = 0; i<pos; i++)
867 /* match everything less than 10^(pos-1) */
869 for (i=1; i<pos; i++)
870 strcat (dst, "[0-9]?");
874 /* match everything greater than 10^pos */
875 for (i = 0; i <= pos; i++)
876 strcat (dst, "[0-9]");
877 strcat (dst, "[0-9]*");
882 void string_rel_add_char (char **term_p, const char *src, int *indx)
884 if (src[*indx] == '\\')
885 *(*term_p)++ = src[(*indx)++];
886 *(*term_p)++ = src[(*indx)++];
890 * > abc ([b-].*|a[c-].*|ab[d-].*|abc.+)
891 * ([^-a].*|a[^-b].*ab[^-c].*|abc.+)
892 * >= abc ([b-].*|a[c-].*|ab[c-].*)
893 * ([^-a].*|a[^-b].*|ab[c-].*)
894 * < abc ([-0].*|a[-a].*|ab[-b].*)
895 * ([^a-].*|a[^b-].*|ab[^c-].*)
896 * <= abc ([-0].*|a[-a].*|ab[-b].*|abc)
897 * ([^a-].*|a[^b-].*|ab[^c-].*|abc)
899 static int string_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
900 const char **term_sub, char *term_dict,
901 oid_value attributeSet,
902 int reg_type, int space_split, char *term_dst)
907 char *term_tmp = term_dict + strlen(term_dict);
908 char term_component[256];
910 attr_init (&relation, zapt, 2);
911 relation_value = attr_find (&relation, NULL);
913 logf (LOG_DEBUG, "string relation value=%d", relation_value);
914 switch (relation_value)
917 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
918 space_split, term_dst))
920 logf (LOG_DEBUG, "Relation <");
923 for (i = 0; term_component[i]; )
930 string_rel_add_char (&term_tmp, term_component, &j);
935 string_rel_add_char (&term_tmp, term_component, &i);
946 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
947 space_split, term_dst))
949 logf (LOG_DEBUG, "Relation <=");
952 for (i = 0; term_component[i]; )
957 string_rel_add_char (&term_tmp, term_component, &j);
961 string_rel_add_char (&term_tmp, term_component, &i);
970 for (i = 0; term_component[i]; )
971 string_rel_add_char (&term_tmp, term_component, &i);
976 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
977 space_split, term_dst))
979 logf (LOG_DEBUG, "Relation >");
982 for (i = 0; term_component[i];)
987 string_rel_add_char (&term_tmp, term_component, &j);
992 string_rel_add_char (&term_tmp, term_component, &i);
1000 for (i = 0; term_component[i];)
1001 string_rel_add_char (&term_tmp, term_component, &i);
1008 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
1009 space_split, term_dst))
1011 logf (LOG_DEBUG, "Relation >=");
1014 for (i = 0; term_component[i];)
1021 string_rel_add_char (&term_tmp, term_component, &j);
1024 if (term_component[i+1])
1028 string_rel_add_char (&term_tmp, term_component, &i);
1032 string_rel_add_char (&term_tmp, term_component, &i);
1044 logf (LOG_DEBUG, "Relation =");
1045 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
1046 space_split, term_dst))
1048 strcat (term_tmp, "(");
1049 strcat (term_tmp, term_component);
1050 strcat (term_tmp, ")");
1055 static int string_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1056 const char **term_sub,
1057 oid_value attributeSet, NMEM stream,
1058 struct grep_info *grep_info,
1059 int reg_type, int complete_flag,
1060 int num_bases, char **basenames,
1063 char term_dict[2*IT_MAX_WORD+4000];
1065 AttrType truncation;
1066 int truncation_value;
1069 oid_value curAttributeSet = attributeSet;
1071 struct rpn_char_map_info rcmi;
1072 int space_split = complete_flag ? 0 : 1;
1074 rpn_char_map_prepare (zh, reg_type, &rcmi);
1075 attr_init (&use, zapt, 1);
1076 use_value = attr_find (&use, &curAttributeSet);
1077 logf (LOG_DEBUG, "string_term, use value %d", use_value);
1078 attr_init (&truncation, zapt, 5);
1079 truncation_value = attr_find (&truncation, NULL);
1080 logf (LOG_DEBUG, "truncation value %d", truncation_value);
1082 if (use_value == -1)
1085 for (base_no = 0; base_no < num_bases; base_no++)
1088 data1_local_attribute *local_attr;
1089 int max_pos, prefix_len = 0;
1092 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
1094 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1095 curAttributeSet, use_value, r);
1099 sprintf (val_str, "%d", use_value);
1101 zh->errString = nmem_strdup (stream, val_str);
1106 struct oident oident;
1108 oident.proto = PROTO_Z3950;
1109 oident.oclass = CLASS_ATTSET;
1110 oident.value = curAttributeSet;
1111 oid_ent_to_oid (&oident, oid);
1114 zh->errString = nmem_strdup (stream, oident.desc);
1118 if (zebraExplain_curDatabase (zh->zei, basenames[base_no]))
1120 zh->errCode = 109; /* Database unavailable */
1121 zh->errString = basenames[base_no];
1124 for (local_attr = attp.local_attributes; local_attr;
1125 local_attr = local_attr->next)
1131 ord = zebraExplain_lookupSU (zh->zei, attp.attset_ordinal,
1136 term_dict[prefix_len++] = '|';
1138 term_dict[prefix_len++] = '(';
1140 ord_len = key_SU_code (ord, ord_buf);
1141 for (i = 0; i<ord_len; i++)
1143 term_dict[prefix_len++] = 1;
1144 term_dict[prefix_len++] = ord_buf[i];
1150 sprintf (val_str, "%d", use_value);
1152 zh->errString = nmem_strdup (stream, val_str);
1155 term_dict[prefix_len++] = ')';
1156 term_dict[prefix_len++] = 1;
1157 term_dict[prefix_len++] = reg_type;
1158 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1159 term_dict[prefix_len] = '\0';
1161 switch (truncation_value)
1163 case -1: /* not specified */
1164 case 100: /* do not truncate */
1165 if (!string_relation (zh, zapt, &termp, term_dict,
1167 reg_type, space_split, term_dst))
1169 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_dict+prefix_len);
1170 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info, &max_pos,
1173 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1175 case 1: /* right truncation */
1176 term_dict[j++] = '(';
1177 if (!term_100 (zh->zebra_maps, reg_type,
1178 &termp, term_dict + j, space_split, term_dst))
1180 strcat (term_dict, ".*)");
1181 dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1182 &max_pos, 0, grep_handle);
1184 case 2: /* keft truncation */
1185 term_dict[j++] = '('; term_dict[j++] = '.'; 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 3: /* left&right 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);
1204 case 101: /* process # in term */
1205 term_dict[j++] = '(';
1206 if (!term_101 (zh->zebra_maps, reg_type,
1207 &termp, term_dict + j, space_split, term_dst))
1209 strcat (term_dict, ")");
1210 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1211 &max_pos, 0, grep_handle);
1213 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d", r);
1215 case 102: /* Regexp-1 */
1216 term_dict[j++] = '(';
1217 if (!term_102 (zh->zebra_maps, reg_type,
1218 &termp, term_dict + j, space_split, term_dst))
1220 strcat (term_dict, ")");
1221 logf (LOG_DEBUG, "Regexp-1 tolerance=%d", r);
1222 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1223 &max_pos, 0, grep_handle);
1225 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
1228 case 103: /* Regexp-2 */
1230 term_dict[j++] = '(';
1231 if (!term_103 (zh->zebra_maps, reg_type,
1232 &termp, term_dict + j, &r, space_split, term_dst))
1234 strcat (term_dict, ")");
1235 logf (LOG_DEBUG, "Regexp-2 tolerance=%d", r);
1236 r = dict_lookup_grep (zh->dict, term_dict, r, grep_info,
1237 &max_pos, 2, grep_handle);
1239 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
1242 case 104: /* process # and ! in term */
1243 term_dict[j++] = '(';
1244 if (!term_104 (zh->zebra_maps, reg_type,
1245 &termp, term_dict + j, space_split, term_dst))
1247 strcat (term_dict, ")");
1248 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1249 &max_pos, 0, grep_handle);
1251 logf (LOG_WARN, "dict_lookup_grep err, trunc=#/!: %d", r);
1253 case 105: /* process * and ! in term */
1254 term_dict[j++] = '(';
1255 if (!term_105 (zh->zebra_maps, reg_type,
1256 &termp, term_dict + j, space_split, term_dst))
1258 strcat (term_dict, ")");
1259 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1260 &max_pos, 0, grep_handle);
1262 logf (LOG_WARN, "dict_lookup_grep err, trunc=*/!: %d", r);
1267 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1271 static void trans_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1275 Z_Term *term = zapt->term;
1277 sizez = term->u.general->len;
1278 if (sizez > IT_MAX_WORD-1)
1279 sizez = IT_MAX_WORD-1;
1280 memcpy (termz, term->u.general->buf, sizez);
1281 termz[sizez] = '\0';
1284 static void trans_scan_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1285 char *termz, int reg_type)
1287 Z_Term *term = zapt->term;
1289 const char *cp = (const char *) term->u.general->buf;
1290 const char *cp_end = cp + term->u.general->len;
1293 const char *space_map = NULL;
1296 while ((len = (cp_end - cp)) > 0)
1298 map = zebra_maps_input (zh->zebra_maps, reg_type, &cp, len);
1299 if (**map == *CHR_SPACE)
1304 for (src = space_map; *src; src++)
1307 for (src = *map; *src; src++)
1314 static RSET rpn_proximity (ZebraHandle zh, RSET rset1, RSET rset2,
1316 int exclusion, int relation, int distance)
1321 struct it_key buf1, buf2;
1324 rset_temp_parms parms;
1327 rsfd1 = rset_open (rset1, RSETF_READ);
1328 more1 = rset_read (rset1, rsfd1, &buf1, &term_index);
1330 rsfd2 = rset_open (rset2, RSETF_READ);
1331 more2 = rset_read (rset2, rsfd2, &buf2, &term_index);
1333 parms.key_size = sizeof (struct it_key);
1334 parms.temp_path = res_get (zh->res, "setTmpDir");
1335 result = rset_create (rset_kind_temp, &parms);
1336 rsfd_result = rset_open (result, RSETF_WRITE);
1338 logf (LOG_DEBUG, "rpn_proximity excl=%d ord=%d rel=%d dis=%d",
1339 exclusion, ordered, relation, distance);
1340 while (more1 && more2)
1342 int cmp = key_compare_it (&buf1, &buf2);
1344 more1 = rset_read (rset1, rsfd1, &buf1, &term_index);
1346 more2 = rset_read (rset2, rsfd2, &buf2, &term_index);
1349 int sysno = buf1.sysno;
1353 seqno[n++] = buf1.seqno;
1354 while ((more1 = rset_read (rset1, rsfd1, &buf1, &term_index)) &&
1355 sysno == buf1.sysno)
1357 seqno[n++] = buf1.seqno;
1360 for (i = 0; i<n; i++)
1362 int diff = buf2.seqno - seqno[i];
1363 int excl = exclusion;
1364 if (!ordered && diff < 0)
1369 if (diff < distance)
1373 if (diff <= distance)
1377 if (diff == distance)
1381 if (diff >= distance)
1385 if (diff > distance)
1389 if (diff != distance)
1394 rset_write (result, rsfd_result, &buf2);
1396 } while ((more2 = rset_read (rset2, rsfd2, &buf2, &term_index)) &&
1397 sysno == buf2.sysno);
1400 rset_close (result, rsfd_result);
1401 rset_close (rset1, rsfd1);
1402 rset_close (rset2, rsfd2);
1406 static RSET rpn_prox (ZebraHandle zh, RSET *rset, int rset_no)
1411 struct it_key **buf;
1413 char prox_term[1024];
1414 int length_prox_term = 0;
1415 int min_nn = 10000000;
1417 const char *flags = NULL;
1419 rsfd = (RSFD *) xmalloc (sizeof(*rsfd)*rset_no);
1420 more = (int *) xmalloc (sizeof(*more)*rset_no);
1421 buf = (struct it_key **) xmalloc (sizeof(*buf)*rset_no);
1424 for (i = 0; i<rset_no; i++)
1427 for (j = 0; j<rset[i]->no_rset_terms; j++)
1429 const char *nflags = rset[i]->rset_terms[j]->flags;
1430 char *term = rset[i]->rset_terms[j]->name;
1431 int lterm = strlen(term);
1432 if (lterm + length_prox_term < sizeof(prox_term)-1)
1434 if (length_prox_term)
1435 prox_term[length_prox_term++] = ' ';
1436 strcpy (prox_term + length_prox_term, term);
1437 length_prox_term += lterm;
1439 if (min_nn > rset[i]->rset_terms[j]->nn)
1440 min_nn = rset[i]->rset_terms[j]->nn;
1444 for (i = 0; i<rset_no; i++)
1446 buf[i] = (struct it_key *) xmalloc (sizeof(**buf));
1447 rsfd[i] = rset_open (rset[i], RSETF_READ);
1448 if (!(more[i] = rset_read (rset[i], rsfd[i], buf[i], &term_index)))
1453 rset_null_parms parms;
1457 rset_close (rset[i], rsfd[i]);
1461 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1463 parms.rset_term->nn = 0;
1464 result = rset_create (rset_kind_null, &parms);
1468 rset_temp_parms parms;
1471 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1473 parms.rset_term->nn = min_nn;
1474 parms.key_size = sizeof (struct it_key);
1475 parms.temp_path = res_get (zh->res, "setTmpDir");
1476 result = rset_create (rset_kind_temp, &parms);
1477 rsfd_result = rset_open (result, RSETF_WRITE);
1481 for (i = 1; i<rset_no; i++)
1490 cmp = key_compare_it (buf[i], buf[i-1]);
1493 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1494 buf[i-1], &term_index);
1499 if (buf[i-1]->seqno+1 != buf[i]->seqno)
1501 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1502 buf[i-1], &term_index);
1508 more[i] = rset_read (rset[i], rsfd[i], buf[i],
1515 rset_write (result, rsfd_result, buf[0]);
1516 more[0] = rset_read (*rset, *rsfd, *buf, &term_index);
1520 for (i = 0; i<rset_no; i++)
1522 rset_close (rset[i], rsfd[i]);
1525 rset_close (result, rsfd_result);
1534 char *normalize_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1535 const char *termz, NMEM stream, unsigned reg_id)
1538 AttrType truncation;
1539 int truncation_value;
1542 attr_init (&truncation, zapt, 5);
1543 truncation_value = attr_find (&truncation, NULL);
1545 switch (truncation_value)
1565 wrbuf = zebra_replace(zh->zebra_maps, reg_id, ex_list,
1566 termz, strlen(termz));
1568 return nmem_strdup(stream, termz);
1571 char *buf = (char*) nmem_malloc (stream, wrbuf_len(wrbuf)+1);
1572 memcpy (buf, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1573 buf[wrbuf_len(wrbuf)] = '\0';
1578 static RSET rpn_search_APT_phrase (ZebraHandle zh,
1579 Z_AttributesPlusTerm *zapt,
1580 const char *termz_org,
1581 oid_value attributeSet,
1583 int reg_type, int complete_flag,
1584 const char *rank_type,
1585 int num_bases, char **basenames)
1587 char term_dst[IT_MAX_WORD+1];
1588 RSET rset[60], result;
1589 int i, r, rset_no = 0;
1590 struct grep_info grep_info;
1591 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1592 const char *termp = termz;
1595 grep_info.term_no = 0;
1597 grep_info.isam_p_size = 0;
1598 grep_info.isam_p_buf = NULL;
1600 grep_info.reg_type = reg_type;
1604 logf (LOG_DEBUG, "APT_phrase termp=%s", termp);
1605 grep_info.isam_p_indx = 0;
1606 r = string_term (zh, zapt, &termp, attributeSet, stream, &grep_info,
1607 reg_type, complete_flag, num_bases, basenames,
1611 logf (LOG_DEBUG, "term: %s", term_dst);
1612 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1613 grep_info.isam_p_indx, term_dst,
1614 strlen(term_dst), rank_type);
1615 assert (rset[rset_no]);
1616 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1620 xfree(grep_info.term_no);
1622 xfree (grep_info.isam_p_buf);
1625 rset_null_parms parms;
1627 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1628 return rset_create (rset_kind_null, &parms);
1630 else if (rset_no == 1)
1632 result = rpn_prox (zh, rset, rset_no);
1633 for (i = 0; i<rset_no; i++)
1634 rset_delete (rset[i]);
1638 static RSET rpn_search_APT_or_list (ZebraHandle zh,
1639 Z_AttributesPlusTerm *zapt,
1640 const char *termz_org,
1641 oid_value attributeSet,
1643 int reg_type, int complete_flag,
1644 const char *rank_type,
1645 int num_bases, char **basenames)
1647 char term_dst[IT_MAX_WORD+1];
1648 RSET rset[60], result;
1649 int i, r, rset_no = 0;
1650 struct grep_info grep_info;
1651 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1652 const char *termp = termz;
1654 grep_info.term_no = 0;
1656 grep_info.isam_p_size = 0;
1657 grep_info.isam_p_buf = NULL;
1659 grep_info.reg_type = reg_type;
1663 logf (LOG_DEBUG, "APT_or_list termp=%s", termp);
1664 grep_info.isam_p_indx = 0;
1665 r = string_term (zh, zapt, &termp, attributeSet, stream, &grep_info,
1666 reg_type, complete_flag, num_bases, basenames,
1670 logf (LOG_DEBUG, "term: %s", term_dst);
1671 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1672 grep_info.isam_p_indx, term_dst,
1673 strlen(term_dst), rank_type);
1674 assert (rset[rset_no]);
1675 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1679 xfree(grep_info.term_no);
1681 xfree (grep_info.isam_p_buf);
1684 rset_null_parms parms;
1686 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1687 return rset_create (rset_kind_null, &parms);
1690 for (i = 1; i<rset_no; i++)
1692 rset_bool_parms bool_parms;
1694 bool_parms.rset_l = result;
1695 bool_parms.rset_r = rset[i];
1696 bool_parms.key_size = sizeof(struct it_key);
1697 bool_parms.cmp = key_compare_it;
1698 result = rset_create (rset_kind_or, &bool_parms);
1703 static RSET rpn_search_APT_and_list (ZebraHandle zh,
1704 Z_AttributesPlusTerm *zapt,
1705 const char *termz_org,
1706 oid_value attributeSet,
1708 int reg_type, int complete_flag,
1709 const char *rank_type,
1710 int num_bases, char **basenames)
1712 char term_dst[IT_MAX_WORD+1];
1713 RSET rset[60], result;
1714 int i, r, rset_no = 0;
1715 struct grep_info grep_info;
1716 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1717 const char *termp = termz;
1720 grep_info.term_no = 0;
1722 grep_info.isam_p_size = 0;
1723 grep_info.isam_p_buf = NULL;
1725 grep_info.reg_type = reg_type;
1729 logf (LOG_DEBUG, "APT_and_list termp=%s", termp);
1730 grep_info.isam_p_indx = 0;
1731 r = string_term (zh, zapt, &termp, attributeSet, stream, &grep_info,
1732 reg_type, complete_flag, num_bases, basenames,
1736 logf (LOG_DEBUG, "term: %s", term_dst);
1737 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1738 grep_info.isam_p_indx, term_dst,
1739 strlen(term_dst), rank_type);
1740 assert (rset[rset_no]);
1741 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1745 xfree(grep_info.term_no);
1747 xfree (grep_info.isam_p_buf);
1750 rset_null_parms parms;
1752 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1753 return rset_create (rset_kind_null, &parms);
1756 for (i = 1; i<rset_no; i++)
1758 rset_bool_parms bool_parms;
1760 bool_parms.rset_l = result;
1761 bool_parms.rset_r = rset[i];
1762 bool_parms.key_size = sizeof(struct it_key);
1763 bool_parms.cmp = key_compare_it;
1764 result = rset_create (rset_kind_and, &bool_parms);
1769 static int numeric_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1770 const char **term_sub,
1772 oid_value attributeSet,
1773 struct grep_info *grep_info,
1782 char *term_tmp = term_dict + strlen(term_dict);
1784 attr_init (&relation, zapt, 2);
1785 relation_value = attr_find (&relation, NULL);
1787 logf (LOG_DEBUG, "numeric relation value=%d", relation_value);
1789 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_tmp, 1,
1792 term_value = atoi (term_tmp);
1793 switch (relation_value)
1796 logf (LOG_DEBUG, "Relation <");
1797 gen_regular_rel (term_tmp, term_value-1, 1);
1800 logf (LOG_DEBUG, "Relation <=");
1801 gen_regular_rel (term_tmp, term_value, 1);
1804 logf (LOG_DEBUG, "Relation >=");
1805 gen_regular_rel (term_tmp, term_value, 0);
1808 logf (LOG_DEBUG, "Relation >");
1809 gen_regular_rel (term_tmp, term_value+1, 0);
1813 logf (LOG_DEBUG, "Relation =");
1814 sprintf (term_tmp, "(0*%d)", term_value);
1816 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_tmp);
1817 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info, max_pos,
1820 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1821 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1825 static int numeric_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1826 const char **term_sub,
1827 oid_value attributeSet, struct grep_info *grep_info,
1828 int reg_type, int complete_flag,
1829 int num_bases, char **basenames,
1832 char term_dict[2*IT_MAX_WORD+2];
1836 oid_value curAttributeSet = attributeSet;
1838 struct rpn_char_map_info rcmi;
1840 rpn_char_map_prepare (zh, reg_type, &rcmi);
1841 attr_init (&use, zapt, 1);
1842 use_value = attr_find (&use, &curAttributeSet);
1843 logf (LOG_DEBUG, "numeric_term, use value %d", use_value);
1845 if (use_value == -1)
1848 for (base_no = 0; base_no < num_bases; base_no++)
1851 data1_local_attribute *local_attr;
1852 int max_pos, prefix_len = 0;
1855 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
1857 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1858 curAttributeSet, use_value, r);
1865 if (zebraExplain_curDatabase (zh->zei, basenames[base_no]))
1867 zh->errCode = 109; /* Database unavailable */
1868 zh->errString = basenames[base_no];
1871 for (local_attr = attp.local_attributes; local_attr;
1872 local_attr = local_attr->next)
1878 ord = zebraExplain_lookupSU (zh->zei, attp.attset_ordinal,
1883 term_dict[prefix_len++] = '|';
1885 term_dict[prefix_len++] = '(';
1887 ord_len = key_SU_code (ord, ord_buf);
1888 for (i = 0; i<ord_len; i++)
1890 term_dict[prefix_len++] = 1;
1891 term_dict[prefix_len++] = ord_buf[i];
1899 term_dict[prefix_len++] = ')';
1900 term_dict[prefix_len++] = 1;
1901 term_dict[prefix_len++] = reg_type;
1902 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1903 term_dict[prefix_len] = '\0';
1904 if (!numeric_relation (zh, zapt, &termp, term_dict,
1905 attributeSet, grep_info, &max_pos, reg_type,
1910 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1914 static RSET rpn_search_APT_numeric (ZebraHandle zh,
1915 Z_AttributesPlusTerm *zapt,
1917 oid_value attributeSet,
1919 int reg_type, int complete_flag,
1920 const char *rank_type,
1921 int num_bases, char **basenames)
1923 char term_dst[IT_MAX_WORD+1];
1924 const char *termp = termz;
1925 RSET rset[60], result;
1926 int i, r, rset_no = 0;
1927 struct grep_info grep_info;
1930 grep_info.term_no = 0;
1932 grep_info.isam_p_size = 0;
1933 grep_info.isam_p_buf = NULL;
1935 grep_info.reg_type = reg_type;
1939 logf (LOG_DEBUG, "APT_numeric termp=%s", termp);
1940 grep_info.isam_p_indx = 0;
1941 r = numeric_term (zh, zapt, &termp, attributeSet, &grep_info,
1942 reg_type, complete_flag, num_bases, basenames,
1946 logf (LOG_DEBUG, "term: %s", term_dst);
1947 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1948 grep_info.isam_p_indx, term_dst,
1949 strlen(term_dst), rank_type);
1950 assert (rset[rset_no]);
1951 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1955 xfree(grep_info.term_no);
1957 xfree (grep_info.isam_p_buf);
1960 rset_null_parms parms;
1962 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1963 return rset_create (rset_kind_null, &parms);
1966 for (i = 1; i<rset_no; i++)
1968 rset_bool_parms bool_parms;
1970 bool_parms.rset_l = result;
1971 bool_parms.rset_r = rset[i];
1972 bool_parms.key_size = sizeof(struct it_key);
1973 bool_parms.cmp = key_compare_it;
1974 result = rset_create (rset_kind_and, &bool_parms);
1979 static RSET rpn_search_APT_local (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1981 oid_value attributeSet,
1983 const char *rank_type)
1988 rset_temp_parms parms;
1990 parms.rset_term = rset_term_create (termz, -1, rank_type);
1991 parms.key_size = sizeof (struct it_key);
1992 parms.temp_path = res_get (zh->res, "setTmpDir");
1993 result = rset_create (rset_kind_temp, &parms);
1994 rsfd = rset_open (result, RSETF_WRITE);
1996 key.sysno = atoi (termz);
2000 rset_write (result, rsfd, &key);
2001 rset_close (result, rsfd);
2005 static RSET rpn_sort_spec (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2006 oid_value attributeSet, NMEM stream,
2007 Z_SortKeySpecList *sort_sequence,
2008 const char *rank_type)
2010 rset_null_parms parms;
2012 int sort_relation_value;
2013 AttrType sort_relation_type;
2018 Z_AttributeElement *ae;
2022 attr_init (&sort_relation_type, zapt, 7);
2023 sort_relation_value = attr_find (&sort_relation_type, &attributeSet);
2025 attr_init (&use_type, zapt, 1);
2026 use_value = attr_find (&use_type, &attributeSet);
2028 if (!sort_sequence->specs)
2030 sort_sequence->num_specs = 10;
2031 sort_sequence->specs = (Z_SortKeySpec **)
2032 nmem_malloc (stream, sort_sequence->num_specs *
2033 sizeof(*sort_sequence->specs));
2034 for (i = 0; i<sort_sequence->num_specs; i++)
2035 sort_sequence->specs[i] = 0;
2037 if (zapt->term->which != Z_Term_general)
2040 i = atoi_n ((char *) zapt->term->u.general->buf,
2041 zapt->term->u.general->len);
2042 if (i >= sort_sequence->num_specs)
2045 oe.proto = PROTO_Z3950;
2046 oe.oclass = CLASS_ATTSET;
2047 oe.value = attributeSet;
2048 if (!oid_ent_to_oid (&oe, oid))
2051 sks = (Z_SortKeySpec *) nmem_malloc (stream, sizeof(*sks));
2052 sks->sortElement = (Z_SortElement *)
2053 nmem_malloc (stream, sizeof(*sks->sortElement));
2054 sks->sortElement->which = Z_SortElement_generic;
2055 sk = sks->sortElement->u.generic = (Z_SortKey *)
2056 nmem_malloc (stream, sizeof(*sk));
2057 sk->which = Z_SortKey_sortAttributes;
2058 sk->u.sortAttributes = (Z_SortAttributes *)
2059 nmem_malloc (stream, sizeof(*sk->u.sortAttributes));
2061 sk->u.sortAttributes->id = oid;
2062 sk->u.sortAttributes->list = (Z_AttributeList *)
2063 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list));
2064 sk->u.sortAttributes->list->num_attributes = 1;
2065 sk->u.sortAttributes->list->attributes = (Z_AttributeElement **)
2066 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list->attributes));
2067 ae = *sk->u.sortAttributes->list->attributes = (Z_AttributeElement *)
2068 nmem_malloc (stream, sizeof(**sk->u.sortAttributes->list->attributes));
2069 ae->attributeSet = 0;
2070 ae->attributeType = (int *)
2071 nmem_malloc (stream, sizeof(*ae->attributeType));
2072 *ae->attributeType = 1;
2073 ae->which = Z_AttributeValue_numeric;
2074 ae->value.numeric = (int *)
2075 nmem_malloc (stream, sizeof(*ae->value.numeric));
2076 *ae->value.numeric = use_value;
2078 sks->sortRelation = (int *)
2079 nmem_malloc (stream, sizeof(*sks->sortRelation));
2080 if (sort_relation_value == 1)
2081 *sks->sortRelation = Z_SortRelation_ascending;
2082 else if (sort_relation_value == 2)
2083 *sks->sortRelation = Z_SortRelation_descending;
2085 *sks->sortRelation = Z_SortRelation_ascending;
2087 sks->caseSensitivity = (int *)
2088 nmem_malloc (stream, sizeof(*sks->caseSensitivity));
2089 *sks->caseSensitivity = 0;
2092 sks->which = Z_SortKeySpec_null;
2093 sks->u.null = odr_nullval ();
2095 sks->missingValueAction = 0;
2098 sort_sequence->specs[i] = sks;
2100 parms.rset_term = rset_term_create ("", -1, rank_type);
2101 return rset_create (rset_kind_null, &parms);
2105 static RSET rpn_search_APT (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2106 oid_value attributeSet, NMEM stream,
2107 Z_SortKeySpecList *sort_sequence,
2108 int num_bases, char **basenames)
2111 char *search_type = NULL;
2112 char *rank_type = NULL;
2115 char termz[IT_MAX_WORD+1];
2117 zebra_maps_attr (zh->zebra_maps, zapt, ®_id, &search_type,
2118 &rank_type, &complete_flag, &sort_flag);
2120 logf (LOG_DEBUG, "reg_id=%c", reg_id);
2121 logf (LOG_DEBUG, "complete_flag=%d", complete_flag);
2122 logf (LOG_DEBUG, "search_type=%s", search_type);
2123 logf (LOG_DEBUG, "rank_type=%s", rank_type);
2125 if (zapt->term->which != Z_Term_general)
2130 trans_term (zh, zapt, termz);
2133 return rpn_sort_spec (zh, zapt, attributeSet, stream, sort_sequence,
2136 if (!strcmp (search_type, "phrase"))
2138 return rpn_search_APT_phrase (zh, zapt, termz, attributeSet, stream,
2139 reg_id, complete_flag, rank_type,
2140 num_bases, basenames);
2142 else if (!strcmp (search_type, "and-list"))
2144 return rpn_search_APT_and_list (zh, zapt, termz, attributeSet, stream,
2145 reg_id, complete_flag, rank_type,
2146 num_bases, basenames);
2148 else if (!strcmp (search_type, "or-list"))
2150 return rpn_search_APT_or_list (zh, zapt, termz, attributeSet, stream,
2151 reg_id, complete_flag, rank_type,
2152 num_bases, basenames);
2154 else if (!strcmp (search_type, "local"))
2156 return rpn_search_APT_local (zh, zapt, termz, attributeSet, stream,
2159 else if (!strcmp (search_type, "numeric"))
2161 return rpn_search_APT_numeric (zh, zapt, termz, attributeSet, stream,
2162 reg_id, complete_flag, rank_type,
2163 num_bases, basenames);
2169 static RSET rpn_search_structure (ZebraHandle zh, Z_RPNStructure *zs,
2170 oid_value attributeSet, NMEM stream,
2171 Z_SortKeySpecList *sort_sequence,
2172 int num_bases, char **basenames)
2175 if (zs->which == Z_RPNStructure_complex)
2177 Z_Operator *zop = zs->u.complex->roperator;
2178 rset_bool_parms bool_parms;
2180 bool_parms.rset_l = rpn_search_structure (zh, zs->u.complex->s1,
2181 attributeSet, stream,
2183 num_bases, basenames);
2184 if (bool_parms.rset_l == NULL)
2186 bool_parms.rset_r = rpn_search_structure (zh, zs->u.complex->s2,
2187 attributeSet, stream,
2189 num_bases, basenames);
2190 if (bool_parms.rset_r == NULL)
2192 rset_delete (bool_parms.rset_l);
2195 bool_parms.key_size = sizeof(struct it_key);
2196 bool_parms.cmp = key_compare_it;
2200 case Z_Operator_and:
2201 r = rset_create (rset_kind_and, &bool_parms);
2204 r = rset_create (rset_kind_or, &bool_parms);
2206 case Z_Operator_and_not:
2207 r = rset_create (rset_kind_not, &bool_parms);
2209 case Z_Operator_prox:
2211 if (zop->u.prox->which != Z_ProximityOperator_known)
2217 if (zop->u.prox->which != Z_ProxCode_known)
2225 if (*zop->u.prox->u.known != Z_ProxUnit_word)
2227 char *val = (char *) nmem_malloc (stream, 16);
2229 zh->errString = val;
2230 sprintf (val, "%d", *zop->u.prox->u.known);
2234 if (*zop->u.prox->proximityUnitCode != Z_ProxUnit_word)
2236 char *val = (char *) nmem_malloc (stream, 16);
2238 zh->errString = val;
2239 sprintf (val, "%d", *zop->u.prox->proximityUnitCode);
2243 r = rpn_proximity (zh, bool_parms.rset_l, bool_parms.rset_r,
2244 *zop->u.prox->ordered,
2245 (!zop->u.prox->exclusion ? 0 :
2246 *zop->u.prox->exclusion),
2247 *zop->u.prox->relationType,
2248 *zop->u.prox->distance);
2255 else if (zs->which == Z_RPNStructure_simple)
2257 if (zs->u.simple->which == Z_Operand_APT)
2259 logf (LOG_DEBUG, "rpn_search_APT");
2260 r = rpn_search_APT (zh, zs->u.simple->u.attributesPlusTerm,
2261 attributeSet, stream, sort_sequence,
2262 num_bases, basenames);
2264 else if (zs->u.simple->which == Z_Operand_resultSetId)
2266 logf (LOG_DEBUG, "rpn_search_ref");
2267 r = resultSetRef (zh, zs->u.simple->u.resultSetId);
2269 r = rset_create (rset_kind_null, NULL);
2286 RSET rpn_search (ZebraHandle zh, NMEM nmem,
2287 Z_RPNQuery *rpn, int num_bases, char **basenames,
2288 const char *setname,
2293 oid_value attributeSet;
2294 Z_SortKeySpecList *sort_sequence;
2298 zh->errString = NULL;
2301 sort_sequence = (Z_SortKeySpecList *)
2302 nmem_malloc (nmem, sizeof(*sort_sequence));
2303 sort_sequence->num_specs = 10;
2304 sort_sequence->specs = (Z_SortKeySpec **)
2305 nmem_malloc (nmem, sort_sequence->num_specs *
2306 sizeof(*sort_sequence->specs));
2307 for (i = 0; i<sort_sequence->num_specs; i++)
2308 sort_sequence->specs[i] = 0;
2310 attrset = oid_getentbyoid (rpn->attributeSetId);
2311 attributeSet = attrset->value;
2312 rset = rpn_search_structure (zh, rpn->RPNStructure, attributeSet,
2313 nmem, sort_sequence, num_bases, basenames);
2318 logf (LOG_DEBUG, "search error: %d", zh->errCode);
2320 for (i = 0; sort_sequence->specs[i]; i++)
2322 sort_sequence->num_specs = i;
2324 resultSetRank (zh, sset, rset);
2327 logf (LOG_DEBUG, "resultSetSortSingle in rpn_search");
2328 resultSetSortSingle (zh, nmem, sset, rset,
2329 sort_sequence, &sort_status);
2332 logf (LOG_DEBUG, "resultSetSortSingle status = %d", zh->errCode);
2338 struct scan_info_entry {
2344 struct scan_info_entry *list;
2350 static int scan_handle (char *name, const char *info, int pos, void *client)
2352 int len_prefix, idx;
2353 struct scan_info *scan_info = (struct scan_info *) client;
2355 len_prefix = strlen(scan_info->prefix);
2356 if (memcmp (name, scan_info->prefix, len_prefix))
2358 if (pos > 0) idx = scan_info->after - pos + scan_info->before;
2361 scan_info->list[idx].term = (char *)
2362 odr_malloc (scan_info->odr, strlen(name + len_prefix)+1);
2363 strcpy (scan_info->list[idx].term, name + len_prefix);
2364 assert (*info == sizeof(ISAM_P));
2365 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAM_P));
2369 static void scan_term_untrans (ZebraHandle zh, NMEM stream, int reg_type,
2370 char **dst, const char *src)
2372 char term_dst[1024];
2374 term_untrans (zh, reg_type, term_dst, src);
2376 *dst = (char *) nmem_malloc (stream, strlen(term_dst)+1);
2377 strcpy (*dst, term_dst);
2380 static void count_set (RSET r, int *count)
2388 logf (LOG_DEBUG, "count_set");
2391 rfd = rset_open (r, RSETF_READ);
2392 while (rset_read (r, rfd, &key, &term_index))
2394 if (key.sysno != psysno)
2401 rset_close (r, rfd);
2402 logf (LOG_DEBUG, "%d keys, %d records", kno, *count);
2405 void rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
2406 oid_value attributeset,
2407 int num_bases, char **basenames,
2408 int *position, int *num_entries, ZebraScanEntry **list,
2412 int pos = *position;
2413 int num = *num_entries;
2417 char termz[IT_MAX_WORD+20];
2420 struct scan_info *scan_info_array;
2421 ZebraScanEntry *glist;
2422 int ords[32], ord_no = 0;
2426 char *search_type = NULL;
2427 char *rank_type = NULL;
2432 if (attributeset == VAL_NONE)
2433 attributeset = VAL_BIB1;
2435 logf (LOG_DEBUG, "position = %d, num = %d", pos, num);
2437 attr_init (&use, zapt, 1);
2438 use_value = attr_find (&use, &attributeset);
2440 if (zebra_maps_attr (zh->zebra_maps, zapt, ®_id, &search_type,
2441 &rank_type, &complete_flag, &sort_flag))
2447 if (use_value == -1)
2449 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
2453 data1_local_attribute *local_attr;
2455 if ((r=att_getentbyatt (zh, &attp, attributeset, use_value)))
2457 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
2458 attributeset, use_value);
2466 if (zebraExplain_curDatabase (zh->zei, basenames[base_no]))
2468 zh->errString = basenames[base_no];
2469 zh->errCode = 109; /* Database unavailable */
2472 for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
2473 local_attr = local_attr->next)
2477 ord = zebraExplain_lookupSU (zh->zei, attp.attset_ordinal,
2480 ords[ord_no++] = ord;
2488 /* prepare dictionary scanning */
2491 scan_info_array = (struct scan_info *)
2492 odr_malloc (stream, ord_no * sizeof(*scan_info_array));
2493 for (i = 0; i < ord_no; i++)
2495 int j, prefix_len = 0;
2496 int before_tmp = before, after_tmp = after;
2497 struct scan_info *scan_info = scan_info_array + i;
2498 struct rpn_char_map_info rcmi;
2500 rpn_char_map_prepare (zh, reg_id, &rcmi);
2502 scan_info->before = before;
2503 scan_info->after = after;
2504 scan_info->odr = stream;
2506 scan_info->list = (struct scan_info_entry *)
2507 odr_malloc (stream, (before+after) * sizeof(*scan_info->list));
2508 for (j = 0; j<before+after; j++)
2509 scan_info->list[j].term = NULL;
2511 prefix_len += key_SU_code (ords[i], termz + prefix_len);
2512 termz[prefix_len++] = reg_id;
2513 termz[prefix_len] = 0;
2514 strcpy (scan_info->prefix, termz);
2516 trans_scan_term (zh, zapt, termz+prefix_len, reg_id);
2518 dict_scan (zh->dict, termz, &before_tmp, &after_tmp, scan_info,
2521 glist = (ZebraScanEntry *)
2522 odr_malloc (stream, (before+after)*sizeof(*glist));
2524 /* consider terms after main term */
2525 for (i = 0; i < ord_no; i++)
2529 for (i = 0; i<after; i++)
2532 const char *mterm = NULL;
2536 for (j = 0; j < ord_no; j++)
2538 if (ptr[j] < before+after &&
2539 (tst=scan_info_array[j].list[ptr[j]].term) &&
2540 (!mterm || strcmp (tst, mterm) < 0))
2548 scan_term_untrans (zh, stream->mem, reg_id,
2549 &glist[i+before].term, mterm);
2550 rset = rset_trunc (zh, &scan_info_array[j0].list[ptr[j0]].isam_p, 1,
2551 glist[i+before].term, strlen(glist[i+before].term),
2555 for (j = j0+1; j<ord_no; j++)
2557 if (ptr[j] < before+after &&
2558 (tst=scan_info_array[j].list[ptr[j]].term) &&
2559 !strcmp (tst, mterm))
2561 rset_bool_parms bool_parms;
2565 rset_trunc (zh, &scan_info_array[j].list[ptr[j]].isam_p, 1,
2566 glist[i+before].term,
2567 strlen(glist[i+before].term), NULL);
2569 bool_parms.key_size = sizeof(struct it_key);
2570 bool_parms.cmp = key_compare_it;
2571 bool_parms.rset_l = rset;
2572 bool_parms.rset_r = rset2;
2574 rset = rset_create (rset_kind_or, &bool_parms);
2579 count_set (rset, &glist[i+before].occurrences);
2584 *num_entries -= (after-i);
2588 /* consider terms before main term */
2589 for (i = 0; i<ord_no; i++)
2592 for (i = 0; i<before; i++)
2595 const char *mterm = NULL;
2599 for (j = 0; j <ord_no; j++)
2601 if (ptr[j] < before &&
2602 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2603 (!mterm || strcmp (tst, mterm) > 0))
2612 scan_term_untrans (zh, stream->mem, reg_id,
2613 &glist[before-1-i].term, mterm);
2616 (zh, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1,
2617 glist[before-1-i].term, strlen(glist[before-1-i].term),
2622 for (j = j0+1; j<ord_no; j++)
2624 if (ptr[j] < before &&
2625 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2626 !strcmp (tst, mterm))
2628 rset_bool_parms bool_parms;
2631 rset2 = rset_trunc (zh,
2632 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1,
2633 glist[before-1-i].term,
2634 strlen(glist[before-1-i].term), NULL);
2636 bool_parms.key_size = sizeof(struct it_key);
2637 bool_parms.cmp = key_compare_it;
2638 bool_parms.rset_l = rset;
2639 bool_parms.rset_r = rset2;
2641 rset = rset_create (rset_kind_or, &bool_parms);
2646 count_set (rset, &glist[before-1-i].occurrences);
2656 *list = glist + i; /* list is set to first 'real' entry */
2658 logf (LOG_DEBUG, "position = %d, num_entries = %d",
2659 *position, *num_entries);
2661 logf (LOG_DEBUG, "scan error: %d", zh->errCode);