2 * Copyright (C) 1995-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.95 1999-09-07 07:19:21 adam
8 * Work on character mapping. Implemented replace rules.
10 * Revision 1.94 1999/07/20 13:59:18 adam
11 * Fixed bug that occurred when phrases had 0 hits.
13 * Revision 1.93 1999/06/17 14:38:40 adam
14 * Bug fix: Scan SEGV'ed when getting unknown use attribute.
16 * Revision 1.92 1999/05/26 07:49:13 adam
19 * Revision 1.91 1999/02/02 14:51:13 adam
20 * Updated WIN32 code specific sections. Changed header.
22 * Revision 1.90 1998/11/16 16:03:43 adam
23 * Moved loggin utilities to Yaz. Was implemented in file zlogs.c.
25 * Revision 1.89 1998/11/16 10:11:55 adam
26 * Added addtional info for error 114 - unsupported use attribute.
28 * Revision 1.88 1998/10/18 07:54:52 adam
29 * Additional info added for diagnostics 114 (Unsupported use attribute) and
30 * 121 (Unsupported attribute set).
32 * Revision 1.87 1998/09/28 11:19:12 adam
33 * Fix for Compiled ASN.1.
35 * Revision 1.86 1998/09/22 10:48:20 adam
36 * Minor changes in search API.
38 * Revision 1.85 1998/09/22 10:03:43 adam
39 * Changed result sets to be persistent in the sense that they can
40 * be re-searched if needed.
41 * Fixed memory leak in rsm_or.
43 * Revision 1.84 1998/09/18 12:41:00 adam
44 * Fixed bug with numerical relations.
46 * Revision 1.83 1998/09/02 13:53:19 adam
47 * Extra parameter decode added to search routines to implement
50 * Revision 1.82 1998/06/26 11:16:40 quinn
51 * Added support (un-optimised) for left and left/right truncation
53 * Revision 1.81 1998/06/24 12:16:14 adam
54 * Support for relations on text operands. Open range support in
55 * DFA module (i.e. [-j], [g-]).
57 * Revision 1.80 1998/06/23 15:33:34 adam
58 * Added feature to specify sort criteria in query (type 7 specifies
61 * Revision 1.79 1998/06/22 11:35:09 adam
64 * Revision 1.78 1998/06/08 14:43:17 adam
65 * Added suport for EXPLAIN Proxy servers - added settings databasePath
66 * and explainDatabase to facilitate this. Increased maximum number
67 * of databases and attributes in one register.
69 * Revision 1.77 1998/05/20 10:12:22 adam
70 * Implemented automatic EXPLAIN database maintenance.
71 * Modified Zebra to work with ASN.1 compiled version of YAZ.
73 * Revision 1.76 1998/04/02 14:35:29 adam
74 * First version of Zebra that works with compiled ASN.1.
76 * Revision 1.75 1998/03/05 08:45:13 adam
77 * New result set model and modular ranking system. Moved towards
78 * descent server API. System information stored as "SGML" records.
80 * Revision 1.74 1998/02/10 12:03:06 adam
83 * Revision 1.73 1998/01/29 13:40:11 adam
84 * Better logging for scan service.
86 * Revision 1.72 1998/01/07 13:53:41 adam
87 * Queries using simple ranked operands returns right number of hits.
89 * Revision 1.71 1997/12/18 10:54:24 adam
90 * New method result set method rs_hits that returns the number of
91 * hits in result-set (if known). The ranked result set returns real
92 * number of hits but only when not combined with other operands.
94 * Revision 1.70 1997/10/31 12:34:43 adam
95 * Changed a few log statements.
97 * Revision 1.69 1997/10/29 12:05:02 adam
98 * Server produces diagnostic "Unsupported Attribute Set" when appropriate.
100 * Revision 1.68 1997/10/27 14:33:06 adam
101 * Moved towards generic character mapping depending on "structure"
102 * field in abstract syntax file. Fixed a few memory leaks. Fixed
103 * bug with negative integers when doing searches with relational
106 * Revision 1.67 1997/09/29 09:06:10 adam
107 * Removed one static var in order to make this module thread safe.
109 * Revision 1.66 1997/09/25 14:58:03 adam
112 * Revision 1.65 1997/09/22 12:39:06 adam
113 * Added get_pos method for the ranked result sets.
115 * Revision 1.64 1997/09/18 08:59:20 adam
116 * Extra generic handle for the character mapping routines.
118 * Revision 1.63 1997/09/17 12:19:18 adam
119 * Zebra version corresponds to YAZ version 1.4.
120 * Changed Zebra server so that it doesn't depend on global common_resource.
122 * Revision 1.62 1997/09/05 15:30:09 adam
123 * Changed prototype for chr_map_input - added const.
124 * Added support for C++, headers uses extern "C" for public definitions.
126 * Revision 1.61 1997/02/10 10:21:14 adam
127 * Bug fix: in search terms character (^) wasn't observed.
129 * Revision 1.60 1997/01/31 11:10:34 adam
130 * Bug fix: Leading and trailing white space weren't removed in scan tokens.
132 * Revision 1.59 1997/01/17 11:31:46 adam
133 * Bug fix: complete phrase search didn't work.
135 * Revision 1.58 1996/12/23 15:30:45 adam
136 * Work on truncation.
137 * Bug fix: result sets weren't deleted after server shut down.
139 * Revision 1.57 1996/11/11 13:38:02 adam
140 * Added proximity support in search.
142 * Revision 1.56 1996/11/08 11:10:32 adam
143 * Buffers used during file match got bigger.
144 * Compressed ISAM support everywhere.
145 * Bug fixes regarding masking characters in queries.
146 * Redesigned Regexp-2 queries.
148 * Revision 1.55 1996/11/04 14:07:44 adam
149 * Moved truncation code to trunc.c.
151 * Revision 1.54 1996/10/29 14:09:52 adam
152 * Use of cisam system - enabled if setting isamc is 1.
154 * Revision 1.53 1996/06/26 09:21:43 adam
155 * Bug fix: local attribute set wasn't obeyed in scan.
157 * Revision 1.52 1996/06/17 14:26:20 adam
158 * Function gen_regular_rel changed to handle negative numbers.
160 * Revision 1.51 1996/06/11 10:54:15 quinn
163 * Revision 1.50 1996/06/07 08:51:53 adam
164 * Bug fix: Character mapping was broken (introducued by last revision).
166 * Revision 1.49 1996/06/04 10:18:11 adam
167 * Search/scan uses character mapping module.
169 * Revision 1.48 1996/05/28 15:15:01 adam
170 * Bug fix: Didn't handle unknown database correctly.
172 * Revision 1.47 1996/05/15 18:36:28 adam
173 * Function trans_term transforms unsearchable characters to blanks.
175 * Revision 1.46 1996/05/15 11:57:56 adam
176 * Fixed bug introduced by set/field mapping in search operations.
178 * Revision 1.45 1996/05/14 11:34:00 adam
179 * Scan support in multiple registers/databases.
181 * Revision 1.44 1996/05/14 06:16:44 adam
182 * Compact use/set bytes used in search service.
184 * Revision 1.43 1996/05/09 09:54:43 adam
185 * Server supports maps from one logical attributes to a list of physical
187 * The extraction process doesn't make space consuming 'any' keys.
189 * Revision 1.42 1996/05/09 07:28:56 quinn
190 * Work towards phrases and multiple registers
192 * Revision 1.41 1996/03/20 09:36:43 adam
193 * Function dict_lookup_grep got extra parameter, init_pos, which marks
194 * from which position in pattern approximate pattern matching should occur.
195 * Approximate pattern matching is used in relevance=re-2.
197 * Revision 1.40 1996/02/02 13:44:44 adam
198 * The public dictionary functions simply use char instead of Dict_char
199 * to represent search strings. Dict_char is used internally only.
201 * Revision 1.39 1996/01/03 16:22:13 quinn
202 * operator->roperator
204 * Revision 1.38 1995/12/11 09:12:55 adam
205 * The rec_get function returns NULL if record doesn't exist - will
206 * happen in the server if the result set records have been deleted since
207 * the creation of the set (i.e. the search).
208 * The server saves a result temporarily if it is 'volatile', i.e. the
209 * set is register dependent.
211 * Revision 1.37 1995/12/06 15:05:28 adam
212 * More verbose in count_set.
214 * Revision 1.36 1995/12/06 12:41:27 adam
215 * New command 'stat' for the index program.
216 * Filenames can be read from stdin by specifying '-'.
217 * Bug fix/enhancement of the transformation from terms to regular
218 * expressons in the search engine.
220 * Revision 1.35 1995/11/27 09:29:00 adam
221 * Bug fixes regarding conversion to regular expressions.
223 * Revision 1.34 1995/11/16 17:00:56 adam
224 * Better logging of rpn query.
226 * Revision 1.33 1995/11/01 13:58:28 quinn
227 * Moving data1 to yaz/retrieval
229 * Revision 1.32 1995/10/27 14:00:11 adam
230 * Implemented detection of database availability.
232 * Revision 1.31 1995/10/17 18:02:10 adam
233 * New feature: databases. Implemented as prefix to words in dictionary.
235 * Revision 1.30 1995/10/16 09:32:38 adam
236 * More work on relational op.
238 * Revision 1.29 1995/10/13 16:01:49 adam
241 * Revision 1.28 1995/10/13 12:26:43 adam
242 * Optimization of truncation.
244 * Revision 1.27 1995/10/12 17:07:22 adam
247 * Revision 1.26 1995/10/12 12:40:54 adam
248 * Bug fixes in rpn_prox.
250 * Revision 1.25 1995/10/10 13:59:24 adam
251 * Function rset_open changed its wflag parameter to general flags.
253 * Revision 1.24 1995/10/09 16:18:37 adam
254 * Function dict_lookup_grep got extra client data parameter.
256 * Revision 1.23 1995/10/06 16:33:37 adam
257 * Use attribute mappings.
259 * Revision 1.22 1995/10/06 15:07:39 adam
260 * Structure 'local-number' handled.
262 * Revision 1.21 1995/10/06 13:52:06 adam
263 * Bug fixes. Handler may abort further scanning.
265 * Revision 1.20 1995/10/06 11:06:33 adam
266 * Scan entries include 'occurrences' now.
268 * Revision 1.19 1995/10/06 10:43:56 adam
269 * Scan added. 'occurrences' in scan entries not set yet.
271 * Revision 1.18 1995/10/04 16:57:20 adam
272 * Key input and merge sort in one pass.
274 * Revision 1.17 1995/10/04 12:55:17 adam
275 * Bug fix in ranked search. Use=Any keys inserted.
277 * Revision 1.16 1995/10/02 16:24:40 adam
278 * Use attribute actually used in search requests.
280 * Revision 1.15 1995/10/02 15:18:52 adam
281 * New member in recRetrieveCtrl: diagnostic.
283 * Revision 1.14 1995/09/28 12:10:32 adam
284 * Bug fixes. Field prefix used in queries.
286 * Revision 1.13 1995/09/18 14:17:50 adam
289 * Revision 1.12 1995/09/15 14:45:21 adam
291 * Work on truncation.
293 * Revision 1.11 1995/09/14 11:53:27 adam
294 * First work on regular expressions/truncations.
296 * Revision 1.10 1995/09/11 15:23:26 adam
297 * More work on relevance search.
299 * Revision 1.9 1995/09/11 13:09:35 adam
300 * More work on relevance feedback.
302 * Revision 1.8 1995/09/08 14:52:27 adam
303 * Minor changes. Dictionary is lower case now.
305 * Revision 1.7 1995/09/07 13:58:36 adam
306 * New parameter: result-set file descriptor (RSFD) to support multiple
307 * positions within the same result-set.
308 * Boolean operators: and, or, not implemented.
309 * Result-set references.
311 * Revision 1.6 1995/09/06 16:11:18 adam
312 * Option: only one word key per file.
314 * Revision 1.5 1995/09/06 10:33:04 adam
315 * More work on present. Some log messages removed.
317 * Revision 1.4 1995/09/05 15:28:40 adam
318 * More work on search engine.
320 * Revision 1.3 1995/09/04 15:20:22 adam
323 * Revision 1.2 1995/09/04 12:33:43 adam
324 * Various cleanup. YAZ util used instead.
326 * Revision 1.1 1995/09/04 09:10:40 adam
327 * More work on index add/del/update.
328 * Merge sort implemented.
329 * Initial work on z39 server.
348 struct rpn_char_map_info {
353 static const char **rpn_char_map_handler (void *vp, const char **from, int len)
355 struct rpn_char_map_info *p = (struct rpn_char_map_info *) vp;
356 return zebra_maps_input (p->zm, p->reg_type, from, len);
359 static void rpn_char_map_prepare (ZebraHandle zh, int reg_type,
360 struct rpn_char_map_info *map_info)
362 map_info->zm = zh->zebra_maps;
363 map_info->reg_type = reg_type;
364 dict_grep_cmap (zh->dict, map_info, rpn_char_map_handler);
371 Z_AttributesPlusTerm *zapt;
374 static int attr_find (AttrType *src, oid_value *attributeSetP)
379 num_attributes = src->zapt->attributes->num_attributes;
381 num_attributes = src->zapt->num_attributes;
383 while (src->major < num_attributes)
385 Z_AttributeElement *element;
388 element = src->zapt->attributes->attributes[src->major];
390 element = src->zapt->attributeList[src->major];
392 if (src->type == *element->attributeType)
394 switch (element->which)
396 case Z_AttributeValue_numeric:
398 if (element->attributeSet && attributeSetP)
402 attrset = oid_getentbyoid (element->attributeSet);
403 *attributeSetP = attrset->value;
405 return *element->value.numeric;
407 case Z_AttributeValue_complex:
408 if (src->minor >= element->value.complex->num_list ||
409 element->value.complex->list[src->minor]->which !=
410 Z_StringOrNumeric_numeric)
413 if (element->attributeSet && attributeSetP)
417 attrset = oid_getentbyoid (element->attributeSet);
418 *attributeSetP = attrset->value;
420 return *element->value.complex->list[src->minor-1]->u.numeric;
430 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
452 static void term_untrans (ZebraHandle zh, int reg_type,
453 char *dst, const char *src)
457 const char *cp = zebra_maps_output (zh->zebra_maps, reg_type, &src);
467 static void add_isam_p (const char *name, const char *info,
470 if (p->isam_p_indx == p->isam_p_size)
472 ISAM_P *new_isam_p_buf;
476 p->isam_p_size = 2*p->isam_p_size + 100;
477 new_isam_p_buf = (ISAM_P *) xmalloc (sizeof(*new_isam_p_buf) *
481 memcpy (new_isam_p_buf, p->isam_p_buf,
482 p->isam_p_indx * sizeof(*p->isam_p_buf));
483 xfree (p->isam_p_buf);
485 p->isam_p_buf = new_isam_p_buf;
488 new_term_no = (int *) xmalloc (sizeof(*new_term_no) *
492 memcpy (new_term_no, p->isam_p_buf,
493 p->isam_p_indx * sizeof(*p->term_no));
496 p->term_no = new_term_no;
499 assert (*info == sizeof(*p->isam_p_buf));
500 memcpy (p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
503 term_untrans (p->zh, p->reg_type, term_tmp, name+2);
504 logf (LOG_DEBUG, "grep: %s", term_tmp);
509 static int grep_handle (char *name, const char *info, void *p)
511 add_isam_p (name, info, (struct grep_info *) p);
515 static int term_pre (ZebraMaps zebra_maps, int reg_type, const char **src,
516 const char *ct1, const char *ct2)
518 const char *s1, *s0 = *src;
521 /* skip white space */
524 if (ct1 && strchr (ct1, *s0))
526 if (ct2 && strchr (ct2, *s0))
529 map = zebra_maps_input (zebra_maps, reg_type, &s1, strlen(s1));
530 if (**map != *CHR_SPACE)
538 /* term_100: handle term, where trunc=none (no operators at all) */
539 static int term_100 (ZebraMaps zebra_maps, int reg_type,
540 const char **src, char *dst, int space_split,
548 if (!term_pre (zebra_maps, reg_type, src, NULL, NULL))
554 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
555 if (space_split && **map == *CHR_SPACE)
559 if (!isalnum (*s1) && *s1 != '-')
571 /* term_101: handle term, where trunc=Process # */
572 static int term_101 (ZebraMaps zebra_maps, int reg_type,
573 const char **src, char *dst, int space_split,
581 if (!term_pre (zebra_maps, reg_type, src, "#", "#"))
590 dst_term[j++] = *s0++;
595 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
596 if (space_split && **map == *CHR_SPACE)
608 dst_term[j++] = '\0';
613 /* term_103: handle term, where trunc=re-2 (regular expressions) */
614 static int term_103 (ZebraMaps zebra_maps, int reg_type, const char **src,
615 char *dst, int *errors, int space_split,
623 if (!term_pre (zebra_maps, reg_type, src, "^\\()[].*+?|", "("))
626 if (errors && *s0 == '+' && s0[1] && s0[2] == '+' && s0[3] &&
629 *errors = s0[1] - '0';
636 if (strchr ("^\\()[].*+?|-", *s0))
644 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
645 if (**map == *CHR_SPACE)
662 /* term_103: handle term, where trunc=re-1 (regular expressions) */
663 static int term_102 (ZebraMaps zebra_maps, int reg_type, const char **src,
664 char *dst, int space_split, char *dst_term)
666 return term_103 (zebra_maps, reg_type, src, dst, NULL, space_split,
671 /* term_104: handle term, where trunc=Process # and ! */
672 static int term_104 (ZebraMaps zebra_maps, int reg_type,
673 const char **src, char *dst, int space_split,
681 if (!term_pre (zebra_maps, reg_type, src, "#!", "#!"))
690 dst_term[j++] = *s0++;
695 dst_term[j++] = *s0++;
699 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
700 if (space_split && **map == *CHR_SPACE)
712 dst_term[j++] = '\0';
718 /* gen_regular_rel - generate regular expression from relation
719 * val: border value (inclusive)
720 * islt: 1 if <=; 0 if >=.
722 static void gen_regular_rel (char *dst, int val, int islt)
729 logf (LOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
733 strcpy (dst, "(-[0-9]+|(");
741 strcpy (dst, "([0-9]+|-(");
742 dst_p = strlen (dst);
752 dst_p = strlen (dst);
753 sprintf (numstr, "%d", val);
754 for (w = strlen(numstr); --w >= 0; pos++)
773 strcpy (dst + dst_p, numstr);
774 dst_p = strlen(dst) - pos - 1;
802 for (i = 0; i<pos; i++)
815 /* match everything less than 10^(pos-1) */
817 for (i=1; i<pos; i++)
818 strcat (dst, "[0-9]?");
822 /* match everything greater than 10^pos */
823 for (i = 0; i <= pos; i++)
824 strcat (dst, "[0-9]");
825 strcat (dst, "[0-9]*");
830 void string_rel_add_char (char **term_p, const char *src, int *indx)
832 if (src[*indx] == '\\')
833 *(*term_p)++ = src[(*indx)++];
834 *(*term_p)++ = src[(*indx)++];
838 * > abc ([b-].*|a[c-].*|ab[d-].*|abc.+)
839 * ([^-a].*|a[^-b].*ab[^-c].*|abc.+)
840 * >= abc ([b-].*|a[c-].*|ab[c-].*)
841 * ([^-a].*|a[^-b].*|ab[c-].*)
842 * < abc ([-0].*|a[-a].*|ab[-b].*)
843 * ([^a-].*|a[^b-].*|ab[^c-].*)
844 * <= abc ([-0].*|a[-a].*|ab[-b].*|abc)
845 * ([^a-].*|a[^b-].*|ab[^c-].*|abc)
847 static int string_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
848 const char **term_sub, char *term_dict,
849 oid_value attributeSet,
850 int reg_type, int space_split, char *term_dst)
855 char *term_tmp = term_dict + strlen(term_dict);
856 char term_component[256];
858 attr_init (&relation, zapt, 2);
859 relation_value = attr_find (&relation, NULL);
861 logf (LOG_DEBUG, "string relation value=%d", relation_value);
862 switch (relation_value)
865 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
866 space_split, term_dst))
868 logf (LOG_DEBUG, "Relation <");
871 for (i = 0; term_component[i]; )
878 string_rel_add_char (&term_tmp, term_component, &j);
883 string_rel_add_char (&term_tmp, term_component, &i);
894 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
895 space_split, term_dst))
897 logf (LOG_DEBUG, "Relation <=");
900 for (i = 0; term_component[i]; )
905 string_rel_add_char (&term_tmp, term_component, &j);
909 string_rel_add_char (&term_tmp, term_component, &i);
918 for (i = 0; term_component[i]; )
919 string_rel_add_char (&term_tmp, term_component, &i);
924 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
925 space_split, term_dst))
927 logf (LOG_DEBUG, "Relation >");
930 for (i = 0; term_component[i];)
935 string_rel_add_char (&term_tmp, term_component, &j);
940 string_rel_add_char (&term_tmp, term_component, &i);
948 for (i = 0; term_component[i];)
949 string_rel_add_char (&term_tmp, term_component, &i);
956 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
957 space_split, term_dst))
959 logf (LOG_DEBUG, "Relation >=");
962 for (i = 0; term_component[i];)
969 string_rel_add_char (&term_tmp, term_component, &j);
972 if (term_component[i+1])
976 string_rel_add_char (&term_tmp, term_component, &i);
980 string_rel_add_char (&term_tmp, term_component, &i);
992 logf (LOG_DEBUG, "Relation =");
993 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
994 space_split, term_dst))
996 strcat (term_tmp, "(");
997 strcat (term_tmp, term_component);
998 strcat (term_tmp, ")");
1003 static int string_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1004 const char **term_sub,
1005 oid_value attributeSet, NMEM stream,
1006 struct grep_info *grep_info,
1007 int reg_type, int complete_flag,
1008 int num_bases, char **basenames,
1011 char term_dict[2*IT_MAX_WORD+4000];
1013 AttrType truncation;
1014 int truncation_value;
1017 oid_value curAttributeSet = attributeSet;
1019 struct rpn_char_map_info rcmi;
1020 int space_split = complete_flag ? 0 : 1;
1022 rpn_char_map_prepare (zh, reg_type, &rcmi);
1023 attr_init (&use, zapt, 1);
1024 use_value = attr_find (&use, &curAttributeSet);
1025 logf (LOG_DEBUG, "string_term, use value %d", use_value);
1026 attr_init (&truncation, zapt, 5);
1027 truncation_value = attr_find (&truncation, NULL);
1028 logf (LOG_DEBUG, "truncation value %d", truncation_value);
1030 if (use_value == -1)
1033 for (base_no = 0; base_no < num_bases; base_no++)
1036 data1_local_attribute *local_attr;
1037 int max_pos, prefix_len = 0;
1040 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
1042 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1043 curAttributeSet, use_value, r);
1047 sprintf (val_str, "%d", use_value);
1049 zh->errString = nmem_strdup (stream, val_str);
1054 struct oident oident;
1056 oident.proto = PROTO_Z3950;
1057 oident.oclass = CLASS_ATTSET;
1058 oident.value = curAttributeSet;
1059 oid_ent_to_oid (&oident, oid);
1062 zh->errString = nmem_strdup (stream, oident.desc);
1066 if (zebraExplain_curDatabase (zh->zei, basenames[base_no]))
1068 zh->errCode = 109; /* Database unavailable */
1069 zh->errString = basenames[base_no];
1072 for (local_attr = attp.local_attributes; local_attr;
1073 local_attr = local_attr->next)
1079 ord = zebraExplain_lookupSU (zh->zei, attp.attset_ordinal,
1084 term_dict[prefix_len++] = '|';
1086 term_dict[prefix_len++] = '(';
1088 ord_len = key_SU_code (ord, ord_buf);
1089 for (i = 0; i<ord_len; i++)
1091 term_dict[prefix_len++] = 1;
1092 term_dict[prefix_len++] = ord_buf[i];
1098 sprintf (val_str, "%d", use_value);
1100 zh->errString = nmem_strdup (stream, val_str);
1103 term_dict[prefix_len++] = ')';
1104 term_dict[prefix_len++] = 1;
1105 term_dict[prefix_len++] = reg_type;
1106 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1107 term_dict[prefix_len] = '\0';
1109 switch (truncation_value)
1111 case -1: /* not specified */
1112 case 100: /* do not truncate */
1113 if (!string_relation (zh, zapt, &termp, term_dict,
1115 reg_type, space_split, term_dst))
1117 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_dict+prefix_len);
1118 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info, &max_pos,
1121 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1123 case 1: /* right truncation */
1124 term_dict[j++] = '(';
1125 if (!term_100 (zh->zebra_maps, reg_type,
1126 &termp, term_dict + j, space_split, term_dst))
1128 strcat (term_dict, ".*)");
1129 dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1130 &max_pos, 0, grep_handle);
1132 case 2: /* keft truncation */
1133 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1134 if (!term_100 (zh->zebra_maps, reg_type,
1135 &termp, term_dict + j, space_split, term_dst))
1137 strcat (term_dict, ")");
1138 dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1139 &max_pos, 0, grep_handle);
1141 case 3: /* left&right truncation */
1142 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1143 if (!term_100 (zh->zebra_maps, reg_type,
1144 &termp, term_dict + j, space_split, term_dst))
1146 strcat (term_dict, ".*)");
1147 dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1148 &max_pos, 0, grep_handle);
1152 case 101: /* process # in term */
1153 term_dict[j++] = '(';
1154 if (!term_101 (zh->zebra_maps, reg_type,
1155 &termp, term_dict + j, space_split, term_dst))
1157 strcat (term_dict, ")");
1158 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1159 &max_pos, 0, grep_handle);
1161 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d", r);
1163 case 102: /* Regexp-1 */
1164 term_dict[j++] = '(';
1165 if (!term_102 (zh->zebra_maps, reg_type,
1166 &termp, term_dict + j, space_split, term_dst))
1168 strcat (term_dict, ")");
1169 logf (LOG_DEBUG, "Regexp-1 tolerance=%d", r);
1170 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1171 &max_pos, 0, grep_handle);
1173 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
1176 case 103: /* Regexp-2 */
1178 term_dict[j++] = '(';
1179 if (!term_103 (zh->zebra_maps, reg_type,
1180 &termp, term_dict + j, &r, space_split, term_dst))
1182 strcat (term_dict, ")");
1183 logf (LOG_DEBUG, "Regexp-2 tolerance=%d", r);
1184 r = dict_lookup_grep (zh->dict, term_dict, r, grep_info,
1185 &max_pos, 2, grep_handle);
1187 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
1190 case 104: /* process # and ! in term */
1191 term_dict[j++] = '(';
1192 if (!term_104 (zh->zebra_maps, reg_type,
1193 &termp, term_dict + j, space_split, term_dst))
1195 strcat (term_dict, ")");
1196 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1197 &max_pos, 0, grep_handle);
1199 logf (LOG_WARN, "dict_lookup_grep err, trunc=#/!: %d", r);
1204 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1208 static void trans_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1212 Z_Term *term = zapt->term;
1214 sizez = term->u.general->len;
1215 if (sizez > IT_MAX_WORD-1)
1216 sizez = IT_MAX_WORD-1;
1217 memcpy (termz, term->u.general->buf, sizez);
1218 termz[sizez] = '\0';
1221 static void trans_scan_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1222 char *termz, int reg_type)
1224 Z_Term *term = zapt->term;
1226 const char *cp = (const char *) term->u.general->buf;
1227 const char *cp_end = cp + term->u.general->len;
1230 const char *space_map = NULL;
1233 while ((len = (cp_end - cp)) > 0)
1235 map = zebra_maps_input (zh->zebra_maps, reg_type, &cp, len);
1236 if (**map == *CHR_SPACE)
1241 for (src = space_map; *src; src++)
1244 for (src = *map; *src; src++)
1251 static RSET rpn_proximity (ZebraHandle zh, RSET rset1, RSET rset2,
1253 int exclusion, int relation, int distance)
1258 struct it_key buf1, buf2;
1261 rset_temp_parms parms;
1264 rsfd1 = rset_open (rset1, RSETF_READ);
1265 more1 = rset_read (rset1, rsfd1, &buf1, &term_index);
1267 rsfd2 = rset_open (rset2, RSETF_READ);
1268 more2 = rset_read (rset2, rsfd2, &buf2, &term_index);
1270 parms.key_size = sizeof (struct it_key);
1271 parms.temp_path = res_get (zh->res, "setTmpDir");
1272 result = rset_create (rset_kind_temp, &parms);
1273 rsfd_result = rset_open (result, RSETF_WRITE);
1275 logf (LOG_DEBUG, "rpn_proximity excl=%d ord=%d rel=%d dis=%d",
1276 exclusion, ordered, relation, distance);
1277 while (more1 && more2)
1279 int cmp = key_compare_it (&buf1, &buf2);
1281 more1 = rset_read (rset1, rsfd1, &buf1, &term_index);
1283 more2 = rset_read (rset2, rsfd2, &buf2, &term_index);
1286 int sysno = buf1.sysno;
1290 seqno[n++] = buf1.seqno;
1291 while ((more1 = rset_read (rset1, rsfd1, &buf1, &term_index)) &&
1292 sysno == buf1.sysno)
1294 seqno[n++] = buf1.seqno;
1297 for (i = 0; i<n; i++)
1299 int diff = buf2.seqno - seqno[i];
1300 int excl = exclusion;
1301 if (!ordered && diff < 0)
1306 if (diff < distance)
1310 if (diff <= distance)
1314 if (diff == distance)
1318 if (diff >= distance)
1322 if (diff > distance)
1326 if (diff != distance)
1331 rset_write (result, rsfd_result, &buf2);
1333 } while ((more2 = rset_read (rset2, rsfd2, &buf2, &term_index)) &&
1334 sysno == buf2.sysno);
1337 rset_close (result, rsfd_result);
1338 rset_close (rset1, rsfd1);
1339 rset_close (rset2, rsfd2);
1343 static RSET rpn_prox (ZebraHandle zh, RSET *rset, int rset_no)
1348 struct it_key **buf;
1350 char prox_term[1024];
1351 int length_prox_term = 0;
1352 int min_nn = 10000000;
1354 const char *flags = NULL;
1356 rsfd = (RSFD *) xmalloc (sizeof(*rsfd)*rset_no);
1357 more = (int *) xmalloc (sizeof(*more)*rset_no);
1358 buf = (struct it_key **) xmalloc (sizeof(*buf)*rset_no);
1361 for (i = 0; i<rset_no; i++)
1364 for (j = 0; j<rset[i]->no_rset_terms; j++)
1366 const char *nflags = rset[i]->rset_terms[j]->flags;
1367 char *term = rset[i]->rset_terms[j]->name;
1368 int lterm = strlen(term);
1369 if (lterm + length_prox_term < sizeof(prox_term)-1)
1371 if (length_prox_term)
1372 prox_term[length_prox_term++] = ' ';
1373 strcpy (prox_term + length_prox_term, term);
1374 length_prox_term += lterm;
1376 if (min_nn > rset[i]->rset_terms[j]->nn)
1377 min_nn = rset[i]->rset_terms[j]->nn;
1381 for (i = 0; i<rset_no; i++)
1383 buf[i] = (struct it_key *) xmalloc (sizeof(**buf));
1384 rsfd[i] = rset_open (rset[i], RSETF_READ);
1385 if (!(more[i] = rset_read (rset[i], rsfd[i], buf[i], &term_index)))
1390 rset_null_parms parms;
1394 rset_close (rset[i], rsfd[i]);
1398 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1400 parms.rset_term->nn = 0;
1401 result = rset_create (rset_kind_null, &parms);
1405 rset_temp_parms parms;
1408 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1410 parms.rset_term->nn = min_nn;
1411 parms.key_size = sizeof (struct it_key);
1412 parms.temp_path = res_get (zh->res, "setTmpDir");
1413 result = rset_create (rset_kind_temp, &parms);
1414 rsfd_result = rset_open (result, RSETF_WRITE);
1418 for (i = 1; i<rset_no; i++)
1427 cmp = key_compare_it (buf[i], buf[i-1]);
1430 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1431 buf[i-1], &term_index);
1436 if (buf[i-1]->seqno+1 != buf[i]->seqno)
1438 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1439 buf[i-1], &term_index);
1445 more[i] = rset_read (rset[i], rsfd[i], buf[i],
1452 rset_write (result, rsfd_result, buf[0]);
1453 more[0] = rset_read (*rset, *rsfd, *buf, &term_index);
1457 for (i = 0; i<rset_no; i++)
1459 rset_close (rset[i], rsfd[i]);
1462 rset_close (result, rsfd_result);
1471 char *normalize_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1472 const char *termz, NMEM stream, unsigned reg_id)
1475 AttrType truncation;
1476 int truncation_value;
1479 attr_init (&truncation, zapt, 5);
1480 truncation_value = attr_find (&truncation, NULL);
1482 switch (truncation_value)
1499 wrbuf = zebra_replace(zh->zebra_maps, reg_id, ex_list,
1500 termz, strlen(termz));
1502 return nmem_strdup(stream, termz);
1505 char *buf = (char*) nmem_malloc (stream, wrbuf_len(wrbuf)+1);
1506 memcpy (buf, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1507 buf[wrbuf_len(wrbuf)] = '\0';
1512 static RSET rpn_search_APT_phrase (ZebraHandle zh,
1513 Z_AttributesPlusTerm *zapt,
1514 const char *termz_org,
1515 oid_value attributeSet,
1517 int reg_type, int complete_flag,
1518 const char *rank_type,
1519 int num_bases, char **basenames)
1521 char term_dst[IT_MAX_WORD+1];
1522 RSET rset[60], result;
1523 int i, r, rset_no = 0;
1524 struct grep_info grep_info;
1525 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1526 const char *termp = termz;
1529 grep_info.term_no = 0;
1531 grep_info.isam_p_size = 0;
1532 grep_info.isam_p_buf = NULL;
1534 grep_info.reg_type = reg_type;
1538 logf (LOG_DEBUG, "APT_phrase termp=%s", termp);
1539 grep_info.isam_p_indx = 0;
1540 r = string_term (zh, zapt, &termp, attributeSet, stream, &grep_info,
1541 reg_type, complete_flag, num_bases, basenames,
1545 logf (LOG_DEBUG, "term: %s", term_dst);
1546 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1547 grep_info.isam_p_indx, term_dst,
1548 strlen(term_dst), rank_type);
1549 assert (rset[rset_no]);
1550 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1554 xfree(grep_info.term_no);
1556 xfree (grep_info.isam_p_buf);
1559 rset_null_parms parms;
1561 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1562 return rset_create (rset_kind_null, &parms);
1564 else if (rset_no == 1)
1566 result = rpn_prox (zh, rset, rset_no);
1567 for (i = 0; i<rset_no; i++)
1568 rset_delete (rset[i]);
1572 static RSET rpn_search_APT_or_list (ZebraHandle zh,
1573 Z_AttributesPlusTerm *zapt,
1574 const char *termz_org,
1575 oid_value attributeSet,
1577 int reg_type, int complete_flag,
1578 const char *rank_type,
1579 int num_bases, char **basenames)
1581 char term_dst[IT_MAX_WORD+1];
1582 RSET rset[60], result;
1583 int i, r, rset_no = 0;
1584 struct grep_info grep_info;
1585 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1586 const char *termp = termz;
1588 grep_info.term_no = 0;
1590 grep_info.isam_p_size = 0;
1591 grep_info.isam_p_buf = NULL;
1593 grep_info.reg_type = reg_type;
1597 logf (LOG_DEBUG, "APT_or_list termp=%s", termp);
1598 grep_info.isam_p_indx = 0;
1599 r = string_term (zh, zapt, &termp, attributeSet, stream, &grep_info,
1600 reg_type, complete_flag, num_bases, basenames,
1604 logf (LOG_DEBUG, "term: %s", term_dst);
1605 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1606 grep_info.isam_p_indx, term_dst,
1607 strlen(term_dst), rank_type);
1608 assert (rset[rset_no]);
1609 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1613 xfree(grep_info.term_no);
1615 xfree (grep_info.isam_p_buf);
1618 rset_null_parms parms;
1620 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1621 return rset_create (rset_kind_null, &parms);
1624 for (i = 1; i<rset_no; i++)
1626 rset_bool_parms bool_parms;
1628 bool_parms.rset_l = result;
1629 bool_parms.rset_r = rset[i];
1630 bool_parms.key_size = sizeof(struct it_key);
1631 bool_parms.cmp = key_compare_it;
1632 result = rset_create (rset_kind_or, &bool_parms);
1637 static RSET rpn_search_APT_and_list (ZebraHandle zh,
1638 Z_AttributesPlusTerm *zapt,
1639 const char *termz_org,
1640 oid_value attributeSet,
1642 int reg_type, int complete_flag,
1643 const char *rank_type,
1644 int num_bases, char **basenames)
1646 char term_dst[IT_MAX_WORD+1];
1647 RSET rset[60], result;
1648 int i, r, rset_no = 0;
1649 struct grep_info grep_info;
1650 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1651 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_and_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_and, &bool_parms);
1703 static int numeric_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1704 const char **term_sub,
1706 oid_value attributeSet,
1707 struct grep_info *grep_info,
1716 char *term_tmp = term_dict + strlen(term_dict);
1718 attr_init (&relation, zapt, 2);
1719 relation_value = attr_find (&relation, NULL);
1721 logf (LOG_DEBUG, "numeric relation value=%d", relation_value);
1723 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_tmp, 1,
1726 term_value = atoi (term_tmp);
1727 switch (relation_value)
1730 logf (LOG_DEBUG, "Relation <");
1731 gen_regular_rel (term_tmp, term_value-1, 1);
1734 logf (LOG_DEBUG, "Relation <=");
1735 gen_regular_rel (term_tmp, term_value, 1);
1738 logf (LOG_DEBUG, "Relation >=");
1739 gen_regular_rel (term_tmp, term_value, 0);
1742 logf (LOG_DEBUG, "Relation >");
1743 gen_regular_rel (term_tmp, term_value+1, 0);
1747 logf (LOG_DEBUG, "Relation =");
1748 sprintf (term_tmp, "(0*%d)", term_value);
1750 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_tmp);
1751 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info, max_pos,
1754 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1755 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1759 static int numeric_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1760 const char **term_sub,
1761 oid_value attributeSet, struct grep_info *grep_info,
1762 int reg_type, int complete_flag,
1763 int num_bases, char **basenames,
1766 char term_dict[2*IT_MAX_WORD+2];
1770 oid_value curAttributeSet = attributeSet;
1772 struct rpn_char_map_info rcmi;
1774 rpn_char_map_prepare (zh, reg_type, &rcmi);
1775 attr_init (&use, zapt, 1);
1776 use_value = attr_find (&use, &curAttributeSet);
1777 logf (LOG_DEBUG, "numeric_term, use value %d", use_value);
1779 if (use_value == -1)
1782 for (base_no = 0; base_no < num_bases; base_no++)
1785 data1_local_attribute *local_attr;
1786 int max_pos, prefix_len = 0;
1789 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
1791 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1792 curAttributeSet, use_value, r);
1799 if (zebraExplain_curDatabase (zh->zei, basenames[base_no]))
1801 zh->errCode = 109; /* Database unavailable */
1802 zh->errString = basenames[base_no];
1805 for (local_attr = attp.local_attributes; local_attr;
1806 local_attr = local_attr->next)
1812 ord = zebraExplain_lookupSU (zh->zei, attp.attset_ordinal,
1817 term_dict[prefix_len++] = '|';
1819 term_dict[prefix_len++] = '(';
1821 ord_len = key_SU_code (ord, ord_buf);
1822 for (i = 0; i<ord_len; i++)
1824 term_dict[prefix_len++] = 1;
1825 term_dict[prefix_len++] = ord_buf[i];
1833 term_dict[prefix_len++] = ')';
1834 term_dict[prefix_len++] = 1;
1835 term_dict[prefix_len++] = reg_type;
1836 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1837 term_dict[prefix_len] = '\0';
1838 if (!numeric_relation (zh, zapt, &termp, term_dict,
1839 attributeSet, grep_info, &max_pos, reg_type,
1844 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1848 static RSET rpn_search_APT_numeric (ZebraHandle zh,
1849 Z_AttributesPlusTerm *zapt,
1851 oid_value attributeSet,
1853 int reg_type, int complete_flag,
1854 const char *rank_type,
1855 int num_bases, char **basenames)
1857 char term_dst[IT_MAX_WORD+1];
1858 const char *termp = termz;
1859 RSET rset[60], result;
1860 int i, r, rset_no = 0;
1861 struct grep_info grep_info;
1864 grep_info.term_no = 0;
1866 grep_info.isam_p_size = 0;
1867 grep_info.isam_p_buf = NULL;
1869 grep_info.reg_type = reg_type;
1873 logf (LOG_DEBUG, "APT_numeric termp=%s", termp);
1874 grep_info.isam_p_indx = 0;
1875 r = numeric_term (zh, zapt, &termp, attributeSet, &grep_info,
1876 reg_type, complete_flag, num_bases, basenames,
1880 logf (LOG_DEBUG, "term: %s", term_dst);
1881 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1882 grep_info.isam_p_indx, term_dst,
1883 strlen(term_dst), rank_type);
1884 assert (rset[rset_no]);
1885 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1889 xfree(grep_info.term_no);
1891 xfree (grep_info.isam_p_buf);
1894 rset_null_parms parms;
1896 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1897 return rset_create (rset_kind_null, &parms);
1900 for (i = 1; i<rset_no; i++)
1902 rset_bool_parms bool_parms;
1904 bool_parms.rset_l = result;
1905 bool_parms.rset_r = rset[i];
1906 bool_parms.key_size = sizeof(struct it_key);
1907 bool_parms.cmp = key_compare_it;
1908 result = rset_create (rset_kind_and, &bool_parms);
1913 static RSET rpn_search_APT_local (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1915 oid_value attributeSet,
1917 const char *rank_type)
1922 rset_temp_parms parms;
1924 parms.rset_term = rset_term_create (termz, -1, rank_type);
1925 parms.key_size = sizeof (struct it_key);
1926 parms.temp_path = res_get (zh->res, "setTmpDir");
1927 result = rset_create (rset_kind_temp, &parms);
1928 rsfd = rset_open (result, RSETF_WRITE);
1930 key.sysno = atoi (termz);
1934 rset_write (result, rsfd, &key);
1935 rset_close (result, rsfd);
1939 static RSET rpn_sort_spec (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1940 oid_value attributeSet, NMEM stream,
1941 Z_SortKeySpecList *sort_sequence,
1942 const char *rank_type)
1944 rset_null_parms parms;
1946 int sort_relation_value;
1947 AttrType sort_relation_type;
1952 Z_AttributeElement *ae;
1956 attr_init (&sort_relation_type, zapt, 7);
1957 sort_relation_value = attr_find (&sort_relation_type, &attributeSet);
1959 attr_init (&use_type, zapt, 1);
1960 use_value = attr_find (&use_type, &attributeSet);
1962 if (!sort_sequence->specs)
1964 sort_sequence->num_specs = 10;
1965 sort_sequence->specs = (Z_SortKeySpec **)
1966 nmem_malloc (stream, sort_sequence->num_specs *
1967 sizeof(*sort_sequence->specs));
1968 for (i = 0; i<sort_sequence->num_specs; i++)
1969 sort_sequence->specs[i] = 0;
1971 if (zapt->term->which != Z_Term_general)
1974 i = atoi_n ((char *) zapt->term->u.general->buf,
1975 zapt->term->u.general->len);
1976 if (i >= sort_sequence->num_specs)
1979 oe.proto = PROTO_Z3950;
1980 oe.oclass = CLASS_ATTSET;
1981 oe.value = attributeSet;
1982 if (!oid_ent_to_oid (&oe, oid))
1985 sks = (Z_SortKeySpec *) nmem_malloc (stream, sizeof(*sks));
1986 sks->sortElement = (Z_SortElement *)
1987 nmem_malloc (stream, sizeof(*sks->sortElement));
1988 sks->sortElement->which = Z_SortElement_generic;
1989 sk = sks->sortElement->u.generic = (Z_SortKey *)
1990 nmem_malloc (stream, sizeof(*sk));
1991 sk->which = Z_SortKey_sortAttributes;
1992 sk->u.sortAttributes = (Z_SortAttributes *)
1993 nmem_malloc (stream, sizeof(*sk->u.sortAttributes));
1995 sk->u.sortAttributes->id = oid;
1996 sk->u.sortAttributes->list = (Z_AttributeList *)
1997 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list));
1998 sk->u.sortAttributes->list->num_attributes = 1;
1999 sk->u.sortAttributes->list->attributes = (Z_AttributeElement **)
2000 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list->attributes));
2001 ae = *sk->u.sortAttributes->list->attributes = (Z_AttributeElement *)
2002 nmem_malloc (stream, sizeof(**sk->u.sortAttributes->list->attributes));
2003 ae->attributeSet = 0;
2004 ae->attributeType = (int *)
2005 nmem_malloc (stream, sizeof(*ae->attributeType));
2006 *ae->attributeType = 1;
2007 ae->which = Z_AttributeValue_numeric;
2008 ae->value.numeric = (int *)
2009 nmem_malloc (stream, sizeof(*ae->value.numeric));
2010 *ae->value.numeric = use_value;
2012 sks->sortRelation = (int *)
2013 nmem_malloc (stream, sizeof(*sks->sortRelation));
2014 if (sort_relation_value == 1)
2015 *sks->sortRelation = Z_SortRelation_ascending;
2016 else if (sort_relation_value == 2)
2017 *sks->sortRelation = Z_SortRelation_descending;
2019 *sks->sortRelation = Z_SortRelation_ascending;
2021 sks->caseSensitivity = (int *)
2022 nmem_malloc (stream, sizeof(*sks->caseSensitivity));
2023 *sks->caseSensitivity = 0;
2026 sks->which = Z_SortKeySpec_null;
2027 sks->u.null = odr_nullval ();
2029 sks->missingValueAction = 0;
2032 sort_sequence->specs[i] = sks;
2034 parms.rset_term = rset_term_create ("", -1, rank_type);
2035 return rset_create (rset_kind_null, &parms);
2039 static RSET rpn_search_APT (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2040 oid_value attributeSet, NMEM stream,
2041 Z_SortKeySpecList *sort_sequence,
2042 int num_bases, char **basenames)
2045 char *search_type = NULL;
2046 char *rank_type = NULL;
2049 char termz[IT_MAX_WORD+1];
2051 zebra_maps_attr (zh->zebra_maps, zapt, ®_id, &search_type,
2052 &rank_type, &complete_flag, &sort_flag);
2054 logf (LOG_DEBUG, "reg_id=%c", reg_id);
2055 logf (LOG_DEBUG, "complete_flag=%d", complete_flag);
2056 logf (LOG_DEBUG, "search_type=%s", search_type);
2057 logf (LOG_DEBUG, "rank_type=%s", rank_type);
2059 if (zapt->term->which != Z_Term_general)
2064 trans_term (zh, zapt, termz);
2067 return rpn_sort_spec (zh, zapt, attributeSet, stream, sort_sequence,
2070 if (!strcmp (search_type, "phrase"))
2072 return rpn_search_APT_phrase (zh, zapt, termz, attributeSet, stream,
2073 reg_id, complete_flag, rank_type,
2074 num_bases, basenames);
2076 else if (!strcmp (search_type, "and-list"))
2078 return rpn_search_APT_and_list (zh, zapt, termz, attributeSet, stream,
2079 reg_id, complete_flag, rank_type,
2080 num_bases, basenames);
2082 else if (!strcmp (search_type, "or-list"))
2084 return rpn_search_APT_or_list (zh, zapt, termz, attributeSet, stream,
2085 reg_id, complete_flag, rank_type,
2086 num_bases, basenames);
2088 else if (!strcmp (search_type, "local"))
2090 return rpn_search_APT_local (zh, zapt, termz, attributeSet, stream,
2093 else if (!strcmp (search_type, "numeric"))
2095 return rpn_search_APT_numeric (zh, zapt, termz, attributeSet, stream,
2096 reg_id, complete_flag, rank_type,
2097 num_bases, basenames);
2103 static RSET rpn_search_structure (ZebraHandle zh, Z_RPNStructure *zs,
2104 oid_value attributeSet, NMEM stream,
2105 Z_SortKeySpecList *sort_sequence,
2106 int num_bases, char **basenames)
2109 if (zs->which == Z_RPNStructure_complex)
2111 Z_Operator *zop = zs->u.complex->roperator;
2112 rset_bool_parms bool_parms;
2114 bool_parms.rset_l = rpn_search_structure (zh, zs->u.complex->s1,
2115 attributeSet, stream,
2117 num_bases, basenames);
2118 if (bool_parms.rset_l == NULL)
2120 bool_parms.rset_r = rpn_search_structure (zh, zs->u.complex->s2,
2121 attributeSet, stream,
2123 num_bases, basenames);
2124 if (bool_parms.rset_r == NULL)
2126 rset_delete (bool_parms.rset_l);
2129 bool_parms.key_size = sizeof(struct it_key);
2130 bool_parms.cmp = key_compare_it;
2134 case Z_Operator_and:
2135 r = rset_create (rset_kind_and, &bool_parms);
2138 r = rset_create (rset_kind_or, &bool_parms);
2140 case Z_Operator_and_not:
2141 r = rset_create (rset_kind_not, &bool_parms);
2143 case Z_Operator_prox:
2145 if (zop->u.prox->which != Z_ProximityOperator_known)
2151 if (zop->u.prox->which != Z_ProxCode_known)
2159 if (*zop->u.prox->u.known != Z_ProxUnit_word)
2161 char *val = (char *) nmem_malloc (stream, 16);
2163 zh->errString = val;
2164 sprintf (val, "%d", *zop->u.prox->u.known);
2168 if (*zop->u.prox->proximityUnitCode != Z_ProxUnit_word)
2170 char *val = (char *) nmem_malloc (stream, 16);
2172 zh->errString = val;
2173 sprintf (val, "%d", *zop->u.prox->proximityUnitCode);
2177 r = rpn_proximity (zh, bool_parms.rset_l, bool_parms.rset_r,
2178 *zop->u.prox->ordered,
2179 (!zop->u.prox->exclusion ? 0 :
2180 *zop->u.prox->exclusion),
2181 *zop->u.prox->relationType,
2182 *zop->u.prox->distance);
2189 else if (zs->which == Z_RPNStructure_simple)
2191 if (zs->u.simple->which == Z_Operand_APT)
2193 logf (LOG_DEBUG, "rpn_search_APT");
2194 r = rpn_search_APT (zh, zs->u.simple->u.attributesPlusTerm,
2195 attributeSet, stream, sort_sequence,
2196 num_bases, basenames);
2198 else if (zs->u.simple->which == Z_Operand_resultSetId)
2200 logf (LOG_DEBUG, "rpn_search_ref");
2201 r = resultSetRef (zh, zs->u.simple->u.resultSetId);
2203 r = rset_create (rset_kind_null, NULL);
2220 RSET rpn_search (ZebraHandle zh, NMEM nmem,
2221 Z_RPNQuery *rpn, int num_bases, char **basenames,
2222 const char *setname,
2227 oid_value attributeSet;
2228 Z_SortKeySpecList *sort_sequence;
2232 zh->errString = NULL;
2235 sort_sequence = (Z_SortKeySpecList *)
2236 nmem_malloc (nmem, sizeof(*sort_sequence));
2237 sort_sequence->num_specs = 10;
2238 sort_sequence->specs = (Z_SortKeySpec **)
2239 nmem_malloc (nmem, sort_sequence->num_specs *
2240 sizeof(*sort_sequence->specs));
2241 for (i = 0; i<sort_sequence->num_specs; i++)
2242 sort_sequence->specs[i] = 0;
2244 attrset = oid_getentbyoid (rpn->attributeSetId);
2245 attributeSet = attrset->value;
2246 rset = rpn_search_structure (zh, rpn->RPNStructure, attributeSet,
2247 nmem, sort_sequence, num_bases, basenames);
2252 logf (LOG_DEBUG, "search error: %d", zh->errCode);
2254 for (i = 0; sort_sequence->specs[i]; i++)
2256 sort_sequence->num_specs = i;
2258 resultSetRank (zh, sset, rset);
2261 logf (LOG_DEBUG, "resultSetSortSingle in rpn_search");
2262 resultSetSortSingle (zh, nmem, sset, rset,
2263 sort_sequence, &sort_status);
2266 logf (LOG_DEBUG, "resultSetSortSingle status = %d", zh->errCode);
2272 struct scan_info_entry {
2278 struct scan_info_entry *list;
2284 static int scan_handle (char *name, const char *info, int pos, void *client)
2286 int len_prefix, idx;
2287 struct scan_info *scan_info = (struct scan_info *) client;
2289 len_prefix = strlen(scan_info->prefix);
2290 if (memcmp (name, scan_info->prefix, len_prefix))
2292 if (pos > 0) idx = scan_info->after - pos + scan_info->before;
2295 scan_info->list[idx].term = (char *)
2296 odr_malloc (scan_info->odr, strlen(name + len_prefix)+1);
2297 strcpy (scan_info->list[idx].term, name + len_prefix);
2298 assert (*info == sizeof(ISAM_P));
2299 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAM_P));
2303 static void scan_term_untrans (ZebraHandle zh, NMEM stream, int reg_type,
2304 char **dst, const char *src)
2306 char term_dst[1024];
2308 term_untrans (zh, reg_type, term_dst, src);
2310 *dst = (char *) nmem_malloc (stream, strlen(term_dst)+1);
2311 strcpy (*dst, term_dst);
2314 static void count_set (RSET r, int *count)
2322 logf (LOG_DEBUG, "count_set");
2325 rfd = rset_open (r, RSETF_READ);
2326 while (rset_read (r, rfd, &key, &term_index))
2328 if (key.sysno != psysno)
2335 rset_close (r, rfd);
2336 logf (LOG_DEBUG, "%d keys, %d records", kno, *count);
2339 void rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
2340 oid_value attributeset,
2341 int num_bases, char **basenames,
2342 int *position, int *num_entries, ZebraScanEntry **list,
2346 int pos = *position;
2347 int num = *num_entries;
2351 char termz[IT_MAX_WORD+20];
2354 struct scan_info *scan_info_array;
2355 ZebraScanEntry *glist;
2356 int ords[32], ord_no = 0;
2360 char *search_type = NULL;
2361 char *rank_type = NULL;
2366 if (attributeset == VAL_NONE)
2367 attributeset = VAL_BIB1;
2369 logf (LOG_DEBUG, "position = %d, num = %d", pos, num);
2371 attr_init (&use, zapt, 1);
2372 use_value = attr_find (&use, &attributeset);
2374 if (zebra_maps_attr (zh->zebra_maps, zapt, ®_id, &search_type,
2375 &rank_type, &complete_flag, &sort_flag))
2381 if (use_value == -1)
2383 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
2387 data1_local_attribute *local_attr;
2389 if ((r=att_getentbyatt (zh, &attp, attributeset, use_value)))
2391 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
2392 attributeset, use_value);
2400 if (zebraExplain_curDatabase (zh->zei, basenames[base_no]))
2402 zh->errString = basenames[base_no];
2403 zh->errCode = 109; /* Database unavailable */
2406 for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
2407 local_attr = local_attr->next)
2411 ord = zebraExplain_lookupSU (zh->zei, attp.attset_ordinal,
2414 ords[ord_no++] = ord;
2422 /* prepare dictionary scanning */
2425 scan_info_array = (struct scan_info *)
2426 odr_malloc (stream, ord_no * sizeof(*scan_info_array));
2427 for (i = 0; i < ord_no; i++)
2429 int j, prefix_len = 0;
2430 int before_tmp = before, after_tmp = after;
2431 struct scan_info *scan_info = scan_info_array + i;
2432 struct rpn_char_map_info rcmi;
2434 rpn_char_map_prepare (zh, reg_id, &rcmi);
2436 scan_info->before = before;
2437 scan_info->after = after;
2438 scan_info->odr = stream;
2440 scan_info->list = (struct scan_info_entry *)
2441 odr_malloc (stream, (before+after) * sizeof(*scan_info->list));
2442 for (j = 0; j<before+after; j++)
2443 scan_info->list[j].term = NULL;
2445 prefix_len += key_SU_code (ords[i], termz + prefix_len);
2446 termz[prefix_len++] = reg_id;
2447 termz[prefix_len] = 0;
2448 strcpy (scan_info->prefix, termz);
2450 trans_scan_term (zh, zapt, termz+prefix_len, reg_id);
2452 dict_scan (zh->dict, termz, &before_tmp, &after_tmp, scan_info,
2455 glist = (ZebraScanEntry *)
2456 odr_malloc (stream, (before+after)*sizeof(*glist));
2458 /* consider terms after main term */
2459 for (i = 0; i < ord_no; i++)
2463 for (i = 0; i<after; i++)
2466 const char *mterm = NULL;
2470 for (j = 0; j < ord_no; j++)
2472 if (ptr[j] < before+after &&
2473 (tst=scan_info_array[j].list[ptr[j]].term) &&
2474 (!mterm || strcmp (tst, mterm) < 0))
2482 scan_term_untrans (zh, stream->mem, reg_id,
2483 &glist[i+before].term, mterm);
2484 rset = rset_trunc (zh, &scan_info_array[j0].list[ptr[j0]].isam_p, 1,
2485 glist[i+before].term, strlen(glist[i+before].term),
2489 for (j = j0+1; j<ord_no; j++)
2491 if (ptr[j] < before+after &&
2492 (tst=scan_info_array[j].list[ptr[j]].term) &&
2493 !strcmp (tst, mterm))
2495 rset_bool_parms bool_parms;
2499 rset_trunc (zh, &scan_info_array[j].list[ptr[j]].isam_p, 1,
2500 glist[i+before].term,
2501 strlen(glist[i+before].term), NULL);
2503 bool_parms.key_size = sizeof(struct it_key);
2504 bool_parms.cmp = key_compare_it;
2505 bool_parms.rset_l = rset;
2506 bool_parms.rset_r = rset2;
2508 rset = rset_create (rset_kind_or, &bool_parms);
2513 count_set (rset, &glist[i+before].occurrences);
2518 *num_entries -= (after-i);
2522 /* consider terms before main term */
2523 for (i = 0; i<ord_no; i++)
2526 for (i = 0; i<before; i++)
2529 const char *mterm = NULL;
2533 for (j = 0; j <ord_no; j++)
2535 if (ptr[j] < before &&
2536 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2537 (!mterm || strcmp (tst, mterm) > 0))
2546 scan_term_untrans (zh, stream->mem, reg_id,
2547 &glist[before-1-i].term, mterm);
2550 (zh, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1,
2551 glist[before-1-i].term, strlen(glist[before-1-i].term),
2556 for (j = j0+1; j<ord_no; j++)
2558 if (ptr[j] < before &&
2559 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2560 !strcmp (tst, mterm))
2562 rset_bool_parms bool_parms;
2565 rset2 = rset_trunc (zh,
2566 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1,
2567 glist[before-1-i].term,
2568 strlen(glist[before-1-i].term), NULL);
2570 bool_parms.key_size = sizeof(struct it_key);
2571 bool_parms.cmp = key_compare_it;
2572 bool_parms.rset_l = rset;
2573 bool_parms.rset_r = rset2;
2575 rset = rset_create (rset_kind_or, &bool_parms);
2580 count_set (rset, &glist[before-1-i].occurrences);
2590 *list = glist + i; /* list is set to first 'real' entry */
2592 logf (LOG_DEBUG, "position = %d, num_entries = %d",
2593 *position, *num_entries);
2595 logf (LOG_DEBUG, "scan error: %d", zh->errCode);