2 * Copyright (C) 1995-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.97 1999-10-14 14:33:50 adam
8 * Added truncation 5=106.
10 * Revision 1.96 1999/09/23 10:05:05 adam
11 * Implemented structure=105 searching.
13 * Revision 1.95 1999/09/07 07:19:21 adam
14 * Work on character mapping. Implemented replace rules.
16 * Revision 1.94 1999/07/20 13:59:18 adam
17 * Fixed bug that occurred when phrases had 0 hits.
19 * Revision 1.93 1999/06/17 14:38:40 adam
20 * Bug fix: Scan SEGV'ed when getting unknown use attribute.
22 * Revision 1.92 1999/05/26 07:49:13 adam
25 * Revision 1.91 1999/02/02 14:51:13 adam
26 * Updated WIN32 code specific sections. Changed header.
28 * Revision 1.90 1998/11/16 16:03:43 adam
29 * Moved loggin utilities to Yaz. Was implemented in file zlogs.c.
31 * Revision 1.89 1998/11/16 10:11:55 adam
32 * Added addtional info for error 114 - unsupported use attribute.
34 * Revision 1.88 1998/10/18 07:54:52 adam
35 * Additional info added for diagnostics 114 (Unsupported use attribute) and
36 * 121 (Unsupported attribute set).
38 * Revision 1.87 1998/09/28 11:19:12 adam
39 * Fix for Compiled ASN.1.
41 * Revision 1.86 1998/09/22 10:48:20 adam
42 * Minor changes in search API.
44 * Revision 1.85 1998/09/22 10:03:43 adam
45 * Changed result sets to be persistent in the sense that they can
46 * be re-searched if needed.
47 * Fixed memory leak in rsm_or.
49 * Revision 1.84 1998/09/18 12:41:00 adam
50 * Fixed bug with numerical relations.
52 * Revision 1.83 1998/09/02 13:53:19 adam
53 * Extra parameter decode added to search routines to implement
56 * Revision 1.82 1998/06/26 11:16:40 quinn
57 * Added support (un-optimised) for left and left/right truncation
59 * Revision 1.81 1998/06/24 12:16:14 adam
60 * Support for relations on text operands. Open range support in
61 * DFA module (i.e. [-j], [g-]).
63 * Revision 1.80 1998/06/23 15:33:34 adam
64 * Added feature to specify sort criteria in query (type 7 specifies
67 * Revision 1.79 1998/06/22 11:35:09 adam
70 * Revision 1.78 1998/06/08 14:43:17 adam
71 * Added suport for EXPLAIN Proxy servers - added settings databasePath
72 * and explainDatabase to facilitate this. Increased maximum number
73 * of databases and attributes in one register.
75 * Revision 1.77 1998/05/20 10:12:22 adam
76 * Implemented automatic EXPLAIN database maintenance.
77 * Modified Zebra to work with ASN.1 compiled version of YAZ.
79 * Revision 1.76 1998/04/02 14:35:29 adam
80 * First version of Zebra that works with compiled ASN.1.
82 * Revision 1.75 1998/03/05 08:45:13 adam
83 * New result set model and modular ranking system. Moved towards
84 * descent server API. System information stored as "SGML" records.
86 * Revision 1.74 1998/02/10 12:03:06 adam
89 * Revision 1.73 1998/01/29 13:40:11 adam
90 * Better logging for scan service.
92 * Revision 1.72 1998/01/07 13:53:41 adam
93 * Queries using simple ranked operands returns right number of hits.
95 * Revision 1.71 1997/12/18 10:54:24 adam
96 * New method result set method rs_hits that returns the number of
97 * hits in result-set (if known). The ranked result set returns real
98 * number of hits but only when not combined with other operands.
100 * Revision 1.70 1997/10/31 12:34:43 adam
101 * Changed a few log statements.
103 * Revision 1.69 1997/10/29 12:05:02 adam
104 * Server produces diagnostic "Unsupported Attribute Set" when appropriate.
106 * Revision 1.68 1997/10/27 14:33:06 adam
107 * Moved towards generic character mapping depending on "structure"
108 * field in abstract syntax file. Fixed a few memory leaks. Fixed
109 * bug with negative integers when doing searches with relational
112 * Revision 1.67 1997/09/29 09:06:10 adam
113 * Removed one static var in order to make this module thread safe.
115 * Revision 1.66 1997/09/25 14:58:03 adam
118 * Revision 1.65 1997/09/22 12:39:06 adam
119 * Added get_pos method for the ranked result sets.
121 * Revision 1.64 1997/09/18 08:59:20 adam
122 * Extra generic handle for the character mapping routines.
124 * Revision 1.63 1997/09/17 12:19:18 adam
125 * Zebra version corresponds to YAZ version 1.4.
126 * Changed Zebra server so that it doesn't depend on global common_resource.
128 * Revision 1.62 1997/09/05 15:30:09 adam
129 * Changed prototype for chr_map_input - added const.
130 * Added support for C++, headers uses extern "C" for public definitions.
132 * Revision 1.61 1997/02/10 10:21:14 adam
133 * Bug fix: in search terms character (^) wasn't observed.
135 * Revision 1.60 1997/01/31 11:10:34 adam
136 * Bug fix: Leading and trailing white space weren't removed in scan tokens.
138 * Revision 1.59 1997/01/17 11:31:46 adam
139 * Bug fix: complete phrase search didn't work.
141 * Revision 1.58 1996/12/23 15:30:45 adam
142 * Work on truncation.
143 * Bug fix: result sets weren't deleted after server shut down.
145 * Revision 1.57 1996/11/11 13:38:02 adam
146 * Added proximity support in search.
148 * Revision 1.56 1996/11/08 11:10:32 adam
149 * Buffers used during file match got bigger.
150 * Compressed ISAM support everywhere.
151 * Bug fixes regarding masking characters in queries.
152 * Redesigned Regexp-2 queries.
154 * Revision 1.55 1996/11/04 14:07:44 adam
155 * Moved truncation code to trunc.c.
157 * Revision 1.54 1996/10/29 14:09:52 adam
158 * Use of cisam system - enabled if setting isamc is 1.
160 * Revision 1.53 1996/06/26 09:21:43 adam
161 * Bug fix: local attribute set wasn't obeyed in scan.
163 * Revision 1.52 1996/06/17 14:26:20 adam
164 * Function gen_regular_rel changed to handle negative numbers.
166 * Revision 1.51 1996/06/11 10:54:15 quinn
169 * Revision 1.50 1996/06/07 08:51:53 adam
170 * Bug fix: Character mapping was broken (introducued by last revision).
172 * Revision 1.49 1996/06/04 10:18:11 adam
173 * Search/scan uses character mapping module.
175 * Revision 1.48 1996/05/28 15:15:01 adam
176 * Bug fix: Didn't handle unknown database correctly.
178 * Revision 1.47 1996/05/15 18:36:28 adam
179 * Function trans_term transforms unsearchable characters to blanks.
181 * Revision 1.46 1996/05/15 11:57:56 adam
182 * Fixed bug introduced by set/field mapping in search operations.
184 * Revision 1.45 1996/05/14 11:34:00 adam
185 * Scan support in multiple registers/databases.
187 * Revision 1.44 1996/05/14 06:16:44 adam
188 * Compact use/set bytes used in search service.
190 * Revision 1.43 1996/05/09 09:54:43 adam
191 * Server supports maps from one logical attributes to a list of physical
193 * The extraction process doesn't make space consuming 'any' keys.
195 * Revision 1.42 1996/05/09 07:28:56 quinn
196 * Work towards phrases and multiple registers
198 * Revision 1.41 1996/03/20 09:36:43 adam
199 * Function dict_lookup_grep got extra parameter, init_pos, which marks
200 * from which position in pattern approximate pattern matching should occur.
201 * Approximate pattern matching is used in relevance=re-2.
203 * Revision 1.40 1996/02/02 13:44:44 adam
204 * The public dictionary functions simply use char instead of Dict_char
205 * to represent search strings. Dict_char is used internally only.
207 * Revision 1.39 1996/01/03 16:22:13 quinn
208 * operator->roperator
210 * Revision 1.38 1995/12/11 09:12:55 adam
211 * The rec_get function returns NULL if record doesn't exist - will
212 * happen in the server if the result set records have been deleted since
213 * the creation of the set (i.e. the search).
214 * The server saves a result temporarily if it is 'volatile', i.e. the
215 * set is register dependent.
217 * Revision 1.37 1995/12/06 15:05:28 adam
218 * More verbose in count_set.
220 * Revision 1.36 1995/12/06 12:41:27 adam
221 * New command 'stat' for the index program.
222 * Filenames can be read from stdin by specifying '-'.
223 * Bug fix/enhancement of the transformation from terms to regular
224 * expressons in the search engine.
226 * Revision 1.35 1995/11/27 09:29:00 adam
227 * Bug fixes regarding conversion to regular expressions.
229 * Revision 1.34 1995/11/16 17:00:56 adam
230 * Better logging of rpn query.
232 * Revision 1.33 1995/11/01 13:58:28 quinn
233 * Moving data1 to yaz/retrieval
235 * Revision 1.32 1995/10/27 14:00:11 adam
236 * Implemented detection of database availability.
238 * Revision 1.31 1995/10/17 18:02:10 adam
239 * New feature: databases. Implemented as prefix to words in dictionary.
241 * Revision 1.30 1995/10/16 09:32:38 adam
242 * More work on relational op.
244 * Revision 1.29 1995/10/13 16:01:49 adam
247 * Revision 1.28 1995/10/13 12:26:43 adam
248 * Optimization of truncation.
250 * Revision 1.27 1995/10/12 17:07:22 adam
253 * Revision 1.26 1995/10/12 12:40:54 adam
254 * Bug fixes in rpn_prox.
256 * Revision 1.25 1995/10/10 13:59:24 adam
257 * Function rset_open changed its wflag parameter to general flags.
259 * Revision 1.24 1995/10/09 16:18:37 adam
260 * Function dict_lookup_grep got extra client data parameter.
262 * Revision 1.23 1995/10/06 16:33:37 adam
263 * Use attribute mappings.
265 * Revision 1.22 1995/10/06 15:07:39 adam
266 * Structure 'local-number' handled.
268 * Revision 1.21 1995/10/06 13:52:06 adam
269 * Bug fixes. Handler may abort further scanning.
271 * Revision 1.20 1995/10/06 11:06:33 adam
272 * Scan entries include 'occurrences' now.
274 * Revision 1.19 1995/10/06 10:43:56 adam
275 * Scan added. 'occurrences' in scan entries not set yet.
277 * Revision 1.18 1995/10/04 16:57:20 adam
278 * Key input and merge sort in one pass.
280 * Revision 1.17 1995/10/04 12:55:17 adam
281 * Bug fix in ranked search. Use=Any keys inserted.
283 * Revision 1.16 1995/10/02 16:24:40 adam
284 * Use attribute actually used in search requests.
286 * Revision 1.15 1995/10/02 15:18:52 adam
287 * New member in recRetrieveCtrl: diagnostic.
289 * Revision 1.14 1995/09/28 12:10:32 adam
290 * Bug fixes. Field prefix used in queries.
292 * Revision 1.13 1995/09/18 14:17:50 adam
295 * Revision 1.12 1995/09/15 14:45:21 adam
297 * Work on truncation.
299 * Revision 1.11 1995/09/14 11:53:27 adam
300 * First work on regular expressions/truncations.
302 * Revision 1.10 1995/09/11 15:23:26 adam
303 * More work on relevance search.
305 * Revision 1.9 1995/09/11 13:09:35 adam
306 * More work on relevance feedback.
308 * Revision 1.8 1995/09/08 14:52:27 adam
309 * Minor changes. Dictionary is lower case now.
311 * Revision 1.7 1995/09/07 13:58:36 adam
312 * New parameter: result-set file descriptor (RSFD) to support multiple
313 * positions within the same result-set.
314 * Boolean operators: and, or, not implemented.
315 * Result-set references.
317 * Revision 1.6 1995/09/06 16:11:18 adam
318 * Option: only one word key per file.
320 * Revision 1.5 1995/09/06 10:33:04 adam
321 * More work on present. Some log messages removed.
323 * Revision 1.4 1995/09/05 15:28:40 adam
324 * More work on search engine.
326 * Revision 1.3 1995/09/04 15:20:22 adam
329 * Revision 1.2 1995/09/04 12:33:43 adam
330 * Various cleanup. YAZ util used instead.
332 * Revision 1.1 1995/09/04 09:10:40 adam
333 * More work on index add/del/update.
334 * Merge sort implemented.
335 * Initial work on z39 server.
354 struct rpn_char_map_info {
359 static const char **rpn_char_map_handler (void *vp, const char **from, int len)
361 struct rpn_char_map_info *p = (struct rpn_char_map_info *) vp;
362 return zebra_maps_input (p->zm, p->reg_type, from, len);
365 static void rpn_char_map_prepare (ZebraHandle zh, int reg_type,
366 struct rpn_char_map_info *map_info)
368 map_info->zm = zh->zebra_maps;
369 map_info->reg_type = reg_type;
370 dict_grep_cmap (zh->dict, map_info, rpn_char_map_handler);
377 Z_AttributesPlusTerm *zapt;
380 static int attr_find (AttrType *src, oid_value *attributeSetP)
385 num_attributes = src->zapt->attributes->num_attributes;
387 num_attributes = src->zapt->num_attributes;
389 while (src->major < num_attributes)
391 Z_AttributeElement *element;
394 element = src->zapt->attributes->attributes[src->major];
396 element = src->zapt->attributeList[src->major];
398 if (src->type == *element->attributeType)
400 switch (element->which)
402 case Z_AttributeValue_numeric:
404 if (element->attributeSet && attributeSetP)
408 attrset = oid_getentbyoid (element->attributeSet);
409 *attributeSetP = attrset->value;
411 return *element->value.numeric;
413 case Z_AttributeValue_complex:
414 if (src->minor >= element->value.complex->num_list ||
415 element->value.complex->list[src->minor]->which !=
416 Z_StringOrNumeric_numeric)
419 if (element->attributeSet && attributeSetP)
423 attrset = oid_getentbyoid (element->attributeSet);
424 *attributeSetP = attrset->value;
426 return *element->value.complex->list[src->minor-1]->u.numeric;
436 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
458 static void term_untrans (ZebraHandle zh, int reg_type,
459 char *dst, const char *src)
463 const char *cp = zebra_maps_output (zh->zebra_maps, reg_type, &src);
473 static void add_isam_p (const char *name, const char *info,
476 if (p->isam_p_indx == p->isam_p_size)
478 ISAM_P *new_isam_p_buf;
482 p->isam_p_size = 2*p->isam_p_size + 100;
483 new_isam_p_buf = (ISAM_P *) xmalloc (sizeof(*new_isam_p_buf) *
487 memcpy (new_isam_p_buf, p->isam_p_buf,
488 p->isam_p_indx * sizeof(*p->isam_p_buf));
489 xfree (p->isam_p_buf);
491 p->isam_p_buf = new_isam_p_buf;
494 new_term_no = (int *) xmalloc (sizeof(*new_term_no) *
498 memcpy (new_term_no, p->isam_p_buf,
499 p->isam_p_indx * sizeof(*p->term_no));
502 p->term_no = new_term_no;
505 assert (*info == sizeof(*p->isam_p_buf));
506 memcpy (p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
509 term_untrans (p->zh, p->reg_type, term_tmp, name+2);
510 logf (LOG_DEBUG, "grep: %s", term_tmp);
515 static int grep_handle (char *name, const char *info, void *p)
517 add_isam_p (name, info, (struct grep_info *) p);
521 static int term_pre (ZebraMaps zebra_maps, int reg_type, const char **src,
522 const char *ct1, const char *ct2)
524 const char *s1, *s0 = *src;
527 /* skip white space */
530 if (ct1 && strchr (ct1, *s0))
532 if (ct2 && strchr (ct2, *s0))
535 map = zebra_maps_input (zebra_maps, reg_type, &s1, strlen(s1));
536 if (**map != *CHR_SPACE)
544 /* term_100: handle term, where trunc=none (no operators at all) */
545 static int term_100 (ZebraMaps zebra_maps, int reg_type,
546 const char **src, char *dst, int space_split,
554 if (!term_pre (zebra_maps, reg_type, src, NULL, NULL))
560 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
561 if (space_split && **map == *CHR_SPACE)
565 if (!isalnum (*s1) && *s1 != '-')
577 /* term_101: handle term, where trunc=Process # */
578 static int term_101 (ZebraMaps zebra_maps, int reg_type,
579 const char **src, char *dst, int space_split,
587 if (!term_pre (zebra_maps, reg_type, src, "#", "#"))
596 dst_term[j++] = *s0++;
601 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
602 if (space_split && **map == *CHR_SPACE)
614 dst_term[j++] = '\0';
619 /* term_103: handle term, where trunc=re-2 (regular expressions) */
620 static int term_103 (ZebraMaps zebra_maps, int reg_type, const char **src,
621 char *dst, int *errors, int space_split,
629 if (!term_pre (zebra_maps, reg_type, src, "^\\()[].*+?|", "("))
632 if (errors && *s0 == '+' && s0[1] && s0[2] == '+' && s0[3] &&
635 *errors = s0[1] - '0';
642 if (strchr ("^\\()[].*+?|-", *s0))
650 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
651 if (**map == *CHR_SPACE)
668 /* term_103: handle term, where trunc=re-1 (regular expressions) */
669 static int term_102 (ZebraMaps zebra_maps, int reg_type, const char **src,
670 char *dst, int space_split, char *dst_term)
672 return term_103 (zebra_maps, reg_type, src, dst, NULL, space_split,
677 /* term_104: handle term, where trunc=Process # and ! */
678 static int term_104 (ZebraMaps zebra_maps, int reg_type,
679 const char **src, char *dst, int space_split,
687 if (!term_pre (zebra_maps, reg_type, src, "#!", "#!"))
696 dst_term[j++] = *s0++;
701 dst_term[j++] = *s0++;
705 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
706 if (space_split && **map == *CHR_SPACE)
718 dst_term[j++] = '\0';
723 /* term_105/106: handle term, where trunc=Process # and ! and right trunc */
724 static int term_105 (ZebraMaps zebra_maps, int reg_type,
725 const char **src, char *dst, int space_split,
726 char *dst_term, int right_truncate)
733 if (!term_pre (zebra_maps, reg_type, src, "*!", "*!"))
742 dst_term[j++] = *s0++;
747 dst_term[j++] = *s0++;
751 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
752 if (space_split && **map == *CHR_SPACE)
770 dst_term[j++] = '\0';
776 /* gen_regular_rel - generate regular expression from relation
777 * val: border value (inclusive)
778 * islt: 1 if <=; 0 if >=.
780 static void gen_regular_rel (char *dst, int val, int islt)
787 logf (LOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
791 strcpy (dst, "(-[0-9]+|(");
799 strcpy (dst, "([0-9]+|-(");
800 dst_p = strlen (dst);
810 dst_p = strlen (dst);
811 sprintf (numstr, "%d", val);
812 for (w = strlen(numstr); --w >= 0; pos++)
831 strcpy (dst + dst_p, numstr);
832 dst_p = strlen(dst) - pos - 1;
860 for (i = 0; i<pos; i++)
873 /* match everything less than 10^(pos-1) */
875 for (i=1; i<pos; i++)
876 strcat (dst, "[0-9]?");
880 /* match everything greater than 10^pos */
881 for (i = 0; i <= pos; i++)
882 strcat (dst, "[0-9]");
883 strcat (dst, "[0-9]*");
888 void string_rel_add_char (char **term_p, const char *src, int *indx)
890 if (src[*indx] == '\\')
891 *(*term_p)++ = src[(*indx)++];
892 *(*term_p)++ = src[(*indx)++];
896 * > abc ([b-].*|a[c-].*|ab[d-].*|abc.+)
897 * ([^-a].*|a[^-b].*ab[^-c].*|abc.+)
898 * >= abc ([b-].*|a[c-].*|ab[c-].*)
899 * ([^-a].*|a[^-b].*|ab[c-].*)
900 * < abc ([-0].*|a[-a].*|ab[-b].*)
901 * ([^a-].*|a[^b-].*|ab[^c-].*)
902 * <= abc ([-0].*|a[-a].*|ab[-b].*|abc)
903 * ([^a-].*|a[^b-].*|ab[^c-].*|abc)
905 static int string_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
906 const char **term_sub, char *term_dict,
907 oid_value attributeSet,
908 int reg_type, int space_split, char *term_dst)
913 char *term_tmp = term_dict + strlen(term_dict);
914 char term_component[256];
916 attr_init (&relation, zapt, 2);
917 relation_value = attr_find (&relation, NULL);
919 logf (LOG_DEBUG, "string relation value=%d", relation_value);
920 switch (relation_value)
923 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
924 space_split, term_dst))
926 logf (LOG_DEBUG, "Relation <");
929 for (i = 0; term_component[i]; )
936 string_rel_add_char (&term_tmp, term_component, &j);
941 string_rel_add_char (&term_tmp, term_component, &i);
952 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
953 space_split, term_dst))
955 logf (LOG_DEBUG, "Relation <=");
958 for (i = 0; term_component[i]; )
963 string_rel_add_char (&term_tmp, term_component, &j);
967 string_rel_add_char (&term_tmp, term_component, &i);
976 for (i = 0; term_component[i]; )
977 string_rel_add_char (&term_tmp, term_component, &i);
982 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
983 space_split, term_dst))
985 logf (LOG_DEBUG, "Relation >");
988 for (i = 0; term_component[i];)
993 string_rel_add_char (&term_tmp, term_component, &j);
998 string_rel_add_char (&term_tmp, term_component, &i);
1006 for (i = 0; term_component[i];)
1007 string_rel_add_char (&term_tmp, term_component, &i);
1014 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
1015 space_split, term_dst))
1017 logf (LOG_DEBUG, "Relation >=");
1020 for (i = 0; term_component[i];)
1027 string_rel_add_char (&term_tmp, term_component, &j);
1030 if (term_component[i+1])
1034 string_rel_add_char (&term_tmp, term_component, &i);
1038 string_rel_add_char (&term_tmp, term_component, &i);
1050 logf (LOG_DEBUG, "Relation =");
1051 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
1052 space_split, term_dst))
1054 strcat (term_tmp, "(");
1055 strcat (term_tmp, term_component);
1056 strcat (term_tmp, ")");
1061 static int string_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1062 const char **term_sub,
1063 oid_value attributeSet, NMEM stream,
1064 struct grep_info *grep_info,
1065 int reg_type, int complete_flag,
1066 int num_bases, char **basenames,
1069 char term_dict[2*IT_MAX_WORD+4000];
1071 AttrType truncation;
1072 int truncation_value;
1075 oid_value curAttributeSet = attributeSet;
1077 struct rpn_char_map_info rcmi;
1078 int space_split = complete_flag ? 0 : 1;
1080 rpn_char_map_prepare (zh, reg_type, &rcmi);
1081 attr_init (&use, zapt, 1);
1082 use_value = attr_find (&use, &curAttributeSet);
1083 logf (LOG_DEBUG, "string_term, use value %d", use_value);
1084 attr_init (&truncation, zapt, 5);
1085 truncation_value = attr_find (&truncation, NULL);
1086 logf (LOG_DEBUG, "truncation value %d", truncation_value);
1088 if (use_value == -1)
1091 for (base_no = 0; base_no < num_bases; base_no++)
1094 data1_local_attribute *local_attr;
1095 int max_pos, prefix_len = 0;
1098 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
1100 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1101 curAttributeSet, use_value, r);
1105 sprintf (val_str, "%d", use_value);
1107 zh->errString = nmem_strdup (stream, val_str);
1112 struct oident oident;
1114 oident.proto = PROTO_Z3950;
1115 oident.oclass = CLASS_ATTSET;
1116 oident.value = curAttributeSet;
1117 oid_ent_to_oid (&oident, oid);
1120 zh->errString = nmem_strdup (stream, oident.desc);
1124 if (zebraExplain_curDatabase (zh->zei, basenames[base_no]))
1126 zh->errCode = 109; /* Database unavailable */
1127 zh->errString = basenames[base_no];
1130 for (local_attr = attp.local_attributes; local_attr;
1131 local_attr = local_attr->next)
1137 ord = zebraExplain_lookupSU (zh->zei, attp.attset_ordinal,
1142 term_dict[prefix_len++] = '|';
1144 term_dict[prefix_len++] = '(';
1146 ord_len = key_SU_code (ord, ord_buf);
1147 for (i = 0; i<ord_len; i++)
1149 term_dict[prefix_len++] = 1;
1150 term_dict[prefix_len++] = ord_buf[i];
1156 sprintf (val_str, "%d", use_value);
1158 zh->errString = nmem_strdup (stream, val_str);
1161 term_dict[prefix_len++] = ')';
1162 term_dict[prefix_len++] = 1;
1163 term_dict[prefix_len++] = reg_type;
1164 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1165 term_dict[prefix_len] = '\0';
1167 switch (truncation_value)
1169 case -1: /* not specified */
1170 case 100: /* do not truncate */
1171 if (!string_relation (zh, zapt, &termp, term_dict,
1173 reg_type, space_split, term_dst))
1175 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_dict+prefix_len);
1176 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info, &max_pos,
1179 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1181 case 1: /* right truncation */
1182 term_dict[j++] = '(';
1183 if (!term_100 (zh->zebra_maps, reg_type,
1184 &termp, term_dict + j, space_split, term_dst))
1186 strcat (term_dict, ".*)");
1187 dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1188 &max_pos, 0, grep_handle);
1190 case 2: /* keft truncation */
1191 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1192 if (!term_100 (zh->zebra_maps, reg_type,
1193 &termp, term_dict + j, space_split, term_dst))
1195 strcat (term_dict, ")");
1196 dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1197 &max_pos, 0, grep_handle);
1199 case 3: /* left&right truncation */
1200 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1201 if (!term_100 (zh->zebra_maps, reg_type,
1202 &termp, term_dict + j, space_split, term_dst))
1204 strcat (term_dict, ".*)");
1205 dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1206 &max_pos, 0, grep_handle);
1210 case 101: /* process # in term */
1211 term_dict[j++] = '(';
1212 if (!term_101 (zh->zebra_maps, reg_type,
1213 &termp, term_dict + j, space_split, term_dst))
1215 strcat (term_dict, ")");
1216 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1217 &max_pos, 0, grep_handle);
1219 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d", r);
1221 case 102: /* Regexp-1 */
1222 term_dict[j++] = '(';
1223 if (!term_102 (zh->zebra_maps, reg_type,
1224 &termp, term_dict + j, space_split, term_dst))
1226 strcat (term_dict, ")");
1227 logf (LOG_DEBUG, "Regexp-1 tolerance=%d", r);
1228 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1229 &max_pos, 0, grep_handle);
1231 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
1234 case 103: /* Regexp-2 */
1236 term_dict[j++] = '(';
1237 if (!term_103 (zh->zebra_maps, reg_type,
1238 &termp, term_dict + j, &r, space_split, term_dst))
1240 strcat (term_dict, ")");
1241 logf (LOG_DEBUG, "Regexp-2 tolerance=%d", r);
1242 r = dict_lookup_grep (zh->dict, term_dict, r, grep_info,
1243 &max_pos, 2, grep_handle);
1245 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
1248 case 104: /* process # and ! in term */
1249 term_dict[j++] = '(';
1250 if (!term_104 (zh->zebra_maps, reg_type,
1251 &termp, term_dict + j, space_split, term_dst))
1253 strcat (term_dict, ")");
1254 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1255 &max_pos, 0, grep_handle);
1257 logf (LOG_WARN, "dict_lookup_grep err, trunc=#/!: %d", r);
1259 case 105: /* process * and ! in term */
1260 term_dict[j++] = '(';
1261 if (!term_105 (zh->zebra_maps, reg_type,
1262 &termp, term_dict + j, space_split, term_dst, 1))
1264 strcat (term_dict, ")");
1265 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1266 &max_pos, 0, grep_handle);
1268 logf (LOG_WARN, "dict_lookup_grep err, trunc=*/!: %d", r);
1270 case 106: /* process * and ! in term */
1271 term_dict[j++] = '(';
1272 if (!term_105 (zh->zebra_maps, reg_type,
1273 &termp, term_dict + j, space_split, term_dst, 0))
1275 strcat (term_dict, ")");
1276 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1277 &max_pos, 0, grep_handle);
1279 logf (LOG_WARN, "dict_lookup_grep err, trunc=*/!: %d", r);
1284 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1288 static void trans_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1292 Z_Term *term = zapt->term;
1294 sizez = term->u.general->len;
1295 if (sizez > IT_MAX_WORD-1)
1296 sizez = IT_MAX_WORD-1;
1297 memcpy (termz, term->u.general->buf, sizez);
1298 termz[sizez] = '\0';
1301 static void trans_scan_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1302 char *termz, int reg_type)
1304 Z_Term *term = zapt->term;
1306 const char *cp = (const char *) term->u.general->buf;
1307 const char *cp_end = cp + term->u.general->len;
1310 const char *space_map = NULL;
1313 while ((len = (cp_end - cp)) > 0)
1315 map = zebra_maps_input (zh->zebra_maps, reg_type, &cp, len);
1316 if (**map == *CHR_SPACE)
1321 for (src = space_map; *src; src++)
1324 for (src = *map; *src; src++)
1331 static RSET rpn_proximity (ZebraHandle zh, RSET rset1, RSET rset2,
1333 int exclusion, int relation, int distance)
1338 struct it_key buf1, buf2;
1341 rset_temp_parms parms;
1344 rsfd1 = rset_open (rset1, RSETF_READ);
1345 more1 = rset_read (rset1, rsfd1, &buf1, &term_index);
1347 rsfd2 = rset_open (rset2, RSETF_READ);
1348 more2 = rset_read (rset2, rsfd2, &buf2, &term_index);
1350 parms.key_size = sizeof (struct it_key);
1351 parms.temp_path = res_get (zh->res, "setTmpDir");
1352 result = rset_create (rset_kind_temp, &parms);
1353 rsfd_result = rset_open (result, RSETF_WRITE);
1355 logf (LOG_DEBUG, "rpn_proximity excl=%d ord=%d rel=%d dis=%d",
1356 exclusion, ordered, relation, distance);
1357 while (more1 && more2)
1359 int cmp = key_compare_it (&buf1, &buf2);
1361 more1 = rset_read (rset1, rsfd1, &buf1, &term_index);
1363 more2 = rset_read (rset2, rsfd2, &buf2, &term_index);
1366 int sysno = buf1.sysno;
1370 seqno[n++] = buf1.seqno;
1371 while ((more1 = rset_read (rset1, rsfd1, &buf1, &term_index)) &&
1372 sysno == buf1.sysno)
1374 seqno[n++] = buf1.seqno;
1377 for (i = 0; i<n; i++)
1379 int diff = buf2.seqno - seqno[i];
1380 int excl = exclusion;
1381 if (!ordered && diff < 0)
1386 if (diff < distance)
1390 if (diff <= distance)
1394 if (diff == distance)
1398 if (diff >= distance)
1402 if (diff > distance)
1406 if (diff != distance)
1411 rset_write (result, rsfd_result, &buf2);
1413 } while ((more2 = rset_read (rset2, rsfd2, &buf2, &term_index)) &&
1414 sysno == buf2.sysno);
1417 rset_close (result, rsfd_result);
1418 rset_close (rset1, rsfd1);
1419 rset_close (rset2, rsfd2);
1423 static RSET rpn_prox (ZebraHandle zh, RSET *rset, int rset_no)
1428 struct it_key **buf;
1430 char prox_term[1024];
1431 int length_prox_term = 0;
1432 int min_nn = 10000000;
1434 const char *flags = NULL;
1436 rsfd = (RSFD *) xmalloc (sizeof(*rsfd)*rset_no);
1437 more = (int *) xmalloc (sizeof(*more)*rset_no);
1438 buf = (struct it_key **) xmalloc (sizeof(*buf)*rset_no);
1441 for (i = 0; i<rset_no; i++)
1444 for (j = 0; j<rset[i]->no_rset_terms; j++)
1446 const char *nflags = rset[i]->rset_terms[j]->flags;
1447 char *term = rset[i]->rset_terms[j]->name;
1448 int lterm = strlen(term);
1449 if (lterm + length_prox_term < sizeof(prox_term)-1)
1451 if (length_prox_term)
1452 prox_term[length_prox_term++] = ' ';
1453 strcpy (prox_term + length_prox_term, term);
1454 length_prox_term += lterm;
1456 if (min_nn > rset[i]->rset_terms[j]->nn)
1457 min_nn = rset[i]->rset_terms[j]->nn;
1461 for (i = 0; i<rset_no; i++)
1463 buf[i] = (struct it_key *) xmalloc (sizeof(**buf));
1464 rsfd[i] = rset_open (rset[i], RSETF_READ);
1465 if (!(more[i] = rset_read (rset[i], rsfd[i], buf[i], &term_index)))
1470 rset_null_parms parms;
1474 rset_close (rset[i], rsfd[i]);
1478 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1480 parms.rset_term->nn = 0;
1481 result = rset_create (rset_kind_null, &parms);
1485 rset_temp_parms parms;
1488 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1490 parms.rset_term->nn = min_nn;
1491 parms.key_size = sizeof (struct it_key);
1492 parms.temp_path = res_get (zh->res, "setTmpDir");
1493 result = rset_create (rset_kind_temp, &parms);
1494 rsfd_result = rset_open (result, RSETF_WRITE);
1498 for (i = 1; i<rset_no; i++)
1507 cmp = key_compare_it (buf[i], buf[i-1]);
1510 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1511 buf[i-1], &term_index);
1516 if (buf[i-1]->seqno+1 != buf[i]->seqno)
1518 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1519 buf[i-1], &term_index);
1525 more[i] = rset_read (rset[i], rsfd[i], buf[i],
1532 rset_write (result, rsfd_result, buf[0]);
1533 more[0] = rset_read (*rset, *rsfd, *buf, &term_index);
1537 for (i = 0; i<rset_no; i++)
1539 rset_close (rset[i], rsfd[i]);
1542 rset_close (result, rsfd_result);
1551 char *normalize_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1552 const char *termz, NMEM stream, unsigned reg_id)
1555 AttrType truncation;
1556 int truncation_value;
1559 attr_init (&truncation, zapt, 5);
1560 truncation_value = attr_find (&truncation, NULL);
1562 switch (truncation_value)
1582 wrbuf = zebra_replace(zh->zebra_maps, reg_id, ex_list,
1583 termz, strlen(termz));
1585 return nmem_strdup(stream, termz);
1588 char *buf = (char*) nmem_malloc (stream, wrbuf_len(wrbuf)+1);
1589 memcpy (buf, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1590 buf[wrbuf_len(wrbuf)] = '\0';
1595 static RSET rpn_search_APT_phrase (ZebraHandle zh,
1596 Z_AttributesPlusTerm *zapt,
1597 const char *termz_org,
1598 oid_value attributeSet,
1600 int reg_type, int complete_flag,
1601 const char *rank_type,
1602 int num_bases, char **basenames)
1604 char term_dst[IT_MAX_WORD+1];
1605 RSET rset[60], result;
1606 int i, r, rset_no = 0;
1607 struct grep_info grep_info;
1608 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1609 const char *termp = termz;
1612 grep_info.term_no = 0;
1614 grep_info.isam_p_size = 0;
1615 grep_info.isam_p_buf = NULL;
1617 grep_info.reg_type = reg_type;
1621 logf (LOG_DEBUG, "APT_phrase termp=%s", termp);
1622 grep_info.isam_p_indx = 0;
1623 r = string_term (zh, zapt, &termp, attributeSet, stream, &grep_info,
1624 reg_type, complete_flag, num_bases, basenames,
1628 logf (LOG_DEBUG, "term: %s", term_dst);
1629 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1630 grep_info.isam_p_indx, term_dst,
1631 strlen(term_dst), rank_type);
1632 assert (rset[rset_no]);
1633 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1637 xfree(grep_info.term_no);
1639 xfree (grep_info.isam_p_buf);
1642 rset_null_parms parms;
1644 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1645 return rset_create (rset_kind_null, &parms);
1647 else if (rset_no == 1)
1649 result = rpn_prox (zh, rset, rset_no);
1650 for (i = 0; i<rset_no; i++)
1651 rset_delete (rset[i]);
1655 static RSET rpn_search_APT_or_list (ZebraHandle zh,
1656 Z_AttributesPlusTerm *zapt,
1657 const char *termz_org,
1658 oid_value attributeSet,
1660 int reg_type, int complete_flag,
1661 const char *rank_type,
1662 int num_bases, char **basenames)
1664 char term_dst[IT_MAX_WORD+1];
1665 RSET rset[60], result;
1666 int i, r, rset_no = 0;
1667 struct grep_info grep_info;
1668 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1669 const char *termp = termz;
1671 grep_info.term_no = 0;
1673 grep_info.isam_p_size = 0;
1674 grep_info.isam_p_buf = NULL;
1676 grep_info.reg_type = reg_type;
1680 logf (LOG_DEBUG, "APT_or_list termp=%s", termp);
1681 grep_info.isam_p_indx = 0;
1682 r = string_term (zh, zapt, &termp, attributeSet, stream, &grep_info,
1683 reg_type, complete_flag, num_bases, basenames,
1687 logf (LOG_DEBUG, "term: %s", term_dst);
1688 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1689 grep_info.isam_p_indx, term_dst,
1690 strlen(term_dst), rank_type);
1691 assert (rset[rset_no]);
1692 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1696 xfree(grep_info.term_no);
1698 xfree (grep_info.isam_p_buf);
1701 rset_null_parms parms;
1703 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1704 return rset_create (rset_kind_null, &parms);
1707 for (i = 1; i<rset_no; i++)
1709 rset_bool_parms bool_parms;
1711 bool_parms.rset_l = result;
1712 bool_parms.rset_r = rset[i];
1713 bool_parms.key_size = sizeof(struct it_key);
1714 bool_parms.cmp = key_compare_it;
1715 result = rset_create (rset_kind_or, &bool_parms);
1720 static RSET rpn_search_APT_and_list (ZebraHandle zh,
1721 Z_AttributesPlusTerm *zapt,
1722 const char *termz_org,
1723 oid_value attributeSet,
1725 int reg_type, int complete_flag,
1726 const char *rank_type,
1727 int num_bases, char **basenames)
1729 char term_dst[IT_MAX_WORD+1];
1730 RSET rset[60], result;
1731 int i, r, rset_no = 0;
1732 struct grep_info grep_info;
1733 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1734 const char *termp = termz;
1737 grep_info.term_no = 0;
1739 grep_info.isam_p_size = 0;
1740 grep_info.isam_p_buf = NULL;
1742 grep_info.reg_type = reg_type;
1746 logf (LOG_DEBUG, "APT_and_list termp=%s", termp);
1747 grep_info.isam_p_indx = 0;
1748 r = string_term (zh, zapt, &termp, attributeSet, stream, &grep_info,
1749 reg_type, complete_flag, num_bases, basenames,
1753 logf (LOG_DEBUG, "term: %s", term_dst);
1754 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1755 grep_info.isam_p_indx, term_dst,
1756 strlen(term_dst), rank_type);
1757 assert (rset[rset_no]);
1758 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1762 xfree(grep_info.term_no);
1764 xfree (grep_info.isam_p_buf);
1767 rset_null_parms parms;
1769 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1770 return rset_create (rset_kind_null, &parms);
1773 for (i = 1; i<rset_no; i++)
1775 rset_bool_parms bool_parms;
1777 bool_parms.rset_l = result;
1778 bool_parms.rset_r = rset[i];
1779 bool_parms.key_size = sizeof(struct it_key);
1780 bool_parms.cmp = key_compare_it;
1781 result = rset_create (rset_kind_and, &bool_parms);
1786 static int numeric_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1787 const char **term_sub,
1789 oid_value attributeSet,
1790 struct grep_info *grep_info,
1799 char *term_tmp = term_dict + strlen(term_dict);
1801 attr_init (&relation, zapt, 2);
1802 relation_value = attr_find (&relation, NULL);
1804 logf (LOG_DEBUG, "numeric relation value=%d", relation_value);
1806 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_tmp, 1,
1809 term_value = atoi (term_tmp);
1810 switch (relation_value)
1813 logf (LOG_DEBUG, "Relation <");
1814 gen_regular_rel (term_tmp, term_value-1, 1);
1817 logf (LOG_DEBUG, "Relation <=");
1818 gen_regular_rel (term_tmp, term_value, 1);
1821 logf (LOG_DEBUG, "Relation >=");
1822 gen_regular_rel (term_tmp, term_value, 0);
1825 logf (LOG_DEBUG, "Relation >");
1826 gen_regular_rel (term_tmp, term_value+1, 0);
1830 logf (LOG_DEBUG, "Relation =");
1831 sprintf (term_tmp, "(0*%d)", term_value);
1833 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_tmp);
1834 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info, max_pos,
1837 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1838 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1842 static int numeric_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1843 const char **term_sub,
1844 oid_value attributeSet, struct grep_info *grep_info,
1845 int reg_type, int complete_flag,
1846 int num_bases, char **basenames,
1849 char term_dict[2*IT_MAX_WORD+2];
1853 oid_value curAttributeSet = attributeSet;
1855 struct rpn_char_map_info rcmi;
1857 rpn_char_map_prepare (zh, reg_type, &rcmi);
1858 attr_init (&use, zapt, 1);
1859 use_value = attr_find (&use, &curAttributeSet);
1860 logf (LOG_DEBUG, "numeric_term, use value %d", use_value);
1862 if (use_value == -1)
1865 for (base_no = 0; base_no < num_bases; base_no++)
1868 data1_local_attribute *local_attr;
1869 int max_pos, prefix_len = 0;
1872 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
1874 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1875 curAttributeSet, use_value, r);
1882 if (zebraExplain_curDatabase (zh->zei, basenames[base_no]))
1884 zh->errCode = 109; /* Database unavailable */
1885 zh->errString = basenames[base_no];
1888 for (local_attr = attp.local_attributes; local_attr;
1889 local_attr = local_attr->next)
1895 ord = zebraExplain_lookupSU (zh->zei, attp.attset_ordinal,
1900 term_dict[prefix_len++] = '|';
1902 term_dict[prefix_len++] = '(';
1904 ord_len = key_SU_code (ord, ord_buf);
1905 for (i = 0; i<ord_len; i++)
1907 term_dict[prefix_len++] = 1;
1908 term_dict[prefix_len++] = ord_buf[i];
1916 term_dict[prefix_len++] = ')';
1917 term_dict[prefix_len++] = 1;
1918 term_dict[prefix_len++] = reg_type;
1919 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1920 term_dict[prefix_len] = '\0';
1921 if (!numeric_relation (zh, zapt, &termp, term_dict,
1922 attributeSet, grep_info, &max_pos, reg_type,
1927 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1931 static RSET rpn_search_APT_numeric (ZebraHandle zh,
1932 Z_AttributesPlusTerm *zapt,
1934 oid_value attributeSet,
1936 int reg_type, int complete_flag,
1937 const char *rank_type,
1938 int num_bases, char **basenames)
1940 char term_dst[IT_MAX_WORD+1];
1941 const char *termp = termz;
1942 RSET rset[60], result;
1943 int i, r, rset_no = 0;
1944 struct grep_info grep_info;
1947 grep_info.term_no = 0;
1949 grep_info.isam_p_size = 0;
1950 grep_info.isam_p_buf = NULL;
1952 grep_info.reg_type = reg_type;
1956 logf (LOG_DEBUG, "APT_numeric termp=%s", termp);
1957 grep_info.isam_p_indx = 0;
1958 r = numeric_term (zh, zapt, &termp, attributeSet, &grep_info,
1959 reg_type, complete_flag, num_bases, basenames,
1963 logf (LOG_DEBUG, "term: %s", term_dst);
1964 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1965 grep_info.isam_p_indx, term_dst,
1966 strlen(term_dst), rank_type);
1967 assert (rset[rset_no]);
1968 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1972 xfree(grep_info.term_no);
1974 xfree (grep_info.isam_p_buf);
1977 rset_null_parms parms;
1979 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1980 return rset_create (rset_kind_null, &parms);
1983 for (i = 1; i<rset_no; i++)
1985 rset_bool_parms bool_parms;
1987 bool_parms.rset_l = result;
1988 bool_parms.rset_r = rset[i];
1989 bool_parms.key_size = sizeof(struct it_key);
1990 bool_parms.cmp = key_compare_it;
1991 result = rset_create (rset_kind_and, &bool_parms);
1996 static RSET rpn_search_APT_local (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1998 oid_value attributeSet,
2000 const char *rank_type)
2005 rset_temp_parms parms;
2007 parms.rset_term = rset_term_create (termz, -1, rank_type);
2008 parms.key_size = sizeof (struct it_key);
2009 parms.temp_path = res_get (zh->res, "setTmpDir");
2010 result = rset_create (rset_kind_temp, &parms);
2011 rsfd = rset_open (result, RSETF_WRITE);
2013 key.sysno = atoi (termz);
2017 rset_write (result, rsfd, &key);
2018 rset_close (result, rsfd);
2022 static RSET rpn_sort_spec (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2023 oid_value attributeSet, NMEM stream,
2024 Z_SortKeySpecList *sort_sequence,
2025 const char *rank_type)
2027 rset_null_parms parms;
2029 int sort_relation_value;
2030 AttrType sort_relation_type;
2035 Z_AttributeElement *ae;
2039 attr_init (&sort_relation_type, zapt, 7);
2040 sort_relation_value = attr_find (&sort_relation_type, &attributeSet);
2042 attr_init (&use_type, zapt, 1);
2043 use_value = attr_find (&use_type, &attributeSet);
2045 if (!sort_sequence->specs)
2047 sort_sequence->num_specs = 10;
2048 sort_sequence->specs = (Z_SortKeySpec **)
2049 nmem_malloc (stream, sort_sequence->num_specs *
2050 sizeof(*sort_sequence->specs));
2051 for (i = 0; i<sort_sequence->num_specs; i++)
2052 sort_sequence->specs[i] = 0;
2054 if (zapt->term->which != Z_Term_general)
2057 i = atoi_n ((char *) zapt->term->u.general->buf,
2058 zapt->term->u.general->len);
2059 if (i >= sort_sequence->num_specs)
2062 oe.proto = PROTO_Z3950;
2063 oe.oclass = CLASS_ATTSET;
2064 oe.value = attributeSet;
2065 if (!oid_ent_to_oid (&oe, oid))
2068 sks = (Z_SortKeySpec *) nmem_malloc (stream, sizeof(*sks));
2069 sks->sortElement = (Z_SortElement *)
2070 nmem_malloc (stream, sizeof(*sks->sortElement));
2071 sks->sortElement->which = Z_SortElement_generic;
2072 sk = sks->sortElement->u.generic = (Z_SortKey *)
2073 nmem_malloc (stream, sizeof(*sk));
2074 sk->which = Z_SortKey_sortAttributes;
2075 sk->u.sortAttributes = (Z_SortAttributes *)
2076 nmem_malloc (stream, sizeof(*sk->u.sortAttributes));
2078 sk->u.sortAttributes->id = oid;
2079 sk->u.sortAttributes->list = (Z_AttributeList *)
2080 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list));
2081 sk->u.sortAttributes->list->num_attributes = 1;
2082 sk->u.sortAttributes->list->attributes = (Z_AttributeElement **)
2083 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list->attributes));
2084 ae = *sk->u.sortAttributes->list->attributes = (Z_AttributeElement *)
2085 nmem_malloc (stream, sizeof(**sk->u.sortAttributes->list->attributes));
2086 ae->attributeSet = 0;
2087 ae->attributeType = (int *)
2088 nmem_malloc (stream, sizeof(*ae->attributeType));
2089 *ae->attributeType = 1;
2090 ae->which = Z_AttributeValue_numeric;
2091 ae->value.numeric = (int *)
2092 nmem_malloc (stream, sizeof(*ae->value.numeric));
2093 *ae->value.numeric = use_value;
2095 sks->sortRelation = (int *)
2096 nmem_malloc (stream, sizeof(*sks->sortRelation));
2097 if (sort_relation_value == 1)
2098 *sks->sortRelation = Z_SortRelation_ascending;
2099 else if (sort_relation_value == 2)
2100 *sks->sortRelation = Z_SortRelation_descending;
2102 *sks->sortRelation = Z_SortRelation_ascending;
2104 sks->caseSensitivity = (int *)
2105 nmem_malloc (stream, sizeof(*sks->caseSensitivity));
2106 *sks->caseSensitivity = 0;
2109 sks->which = Z_SortKeySpec_null;
2110 sks->u.null = odr_nullval ();
2112 sks->missingValueAction = 0;
2115 sort_sequence->specs[i] = sks;
2117 parms.rset_term = rset_term_create ("", -1, rank_type);
2118 return rset_create (rset_kind_null, &parms);
2122 static RSET rpn_search_APT (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2123 oid_value attributeSet, NMEM stream,
2124 Z_SortKeySpecList *sort_sequence,
2125 int num_bases, char **basenames)
2128 char *search_type = NULL;
2129 char *rank_type = NULL;
2132 char termz[IT_MAX_WORD+1];
2134 zebra_maps_attr (zh->zebra_maps, zapt, ®_id, &search_type,
2135 &rank_type, &complete_flag, &sort_flag);
2137 logf (LOG_DEBUG, "reg_id=%c", reg_id);
2138 logf (LOG_DEBUG, "complete_flag=%d", complete_flag);
2139 logf (LOG_DEBUG, "search_type=%s", search_type);
2140 logf (LOG_DEBUG, "rank_type=%s", rank_type);
2142 if (zapt->term->which != Z_Term_general)
2147 trans_term (zh, zapt, termz);
2150 return rpn_sort_spec (zh, zapt, attributeSet, stream, sort_sequence,
2153 if (!strcmp (search_type, "phrase"))
2155 return rpn_search_APT_phrase (zh, zapt, termz, attributeSet, stream,
2156 reg_id, complete_flag, rank_type,
2157 num_bases, basenames);
2159 else if (!strcmp (search_type, "and-list"))
2161 return rpn_search_APT_and_list (zh, zapt, termz, attributeSet, stream,
2162 reg_id, complete_flag, rank_type,
2163 num_bases, basenames);
2165 else if (!strcmp (search_type, "or-list"))
2167 return rpn_search_APT_or_list (zh, zapt, termz, attributeSet, stream,
2168 reg_id, complete_flag, rank_type,
2169 num_bases, basenames);
2171 else if (!strcmp (search_type, "local"))
2173 return rpn_search_APT_local (zh, zapt, termz, attributeSet, stream,
2176 else if (!strcmp (search_type, "numeric"))
2178 return rpn_search_APT_numeric (zh, zapt, termz, attributeSet, stream,
2179 reg_id, complete_flag, rank_type,
2180 num_bases, basenames);
2186 static RSET rpn_search_structure (ZebraHandle zh, Z_RPNStructure *zs,
2187 oid_value attributeSet, NMEM stream,
2188 Z_SortKeySpecList *sort_sequence,
2189 int num_bases, char **basenames)
2192 if (zs->which == Z_RPNStructure_complex)
2194 Z_Operator *zop = zs->u.complex->roperator;
2195 rset_bool_parms bool_parms;
2197 bool_parms.rset_l = rpn_search_structure (zh, zs->u.complex->s1,
2198 attributeSet, stream,
2200 num_bases, basenames);
2201 if (bool_parms.rset_l == NULL)
2203 bool_parms.rset_r = rpn_search_structure (zh, zs->u.complex->s2,
2204 attributeSet, stream,
2206 num_bases, basenames);
2207 if (bool_parms.rset_r == NULL)
2209 rset_delete (bool_parms.rset_l);
2212 bool_parms.key_size = sizeof(struct it_key);
2213 bool_parms.cmp = key_compare_it;
2217 case Z_Operator_and:
2218 r = rset_create (rset_kind_and, &bool_parms);
2221 r = rset_create (rset_kind_or, &bool_parms);
2223 case Z_Operator_and_not:
2224 r = rset_create (rset_kind_not, &bool_parms);
2226 case Z_Operator_prox:
2228 if (zop->u.prox->which != Z_ProximityOperator_known)
2234 if (zop->u.prox->which != Z_ProxCode_known)
2242 if (*zop->u.prox->u.known != Z_ProxUnit_word)
2244 char *val = (char *) nmem_malloc (stream, 16);
2246 zh->errString = val;
2247 sprintf (val, "%d", *zop->u.prox->u.known);
2251 if (*zop->u.prox->proximityUnitCode != Z_ProxUnit_word)
2253 char *val = (char *) nmem_malloc (stream, 16);
2255 zh->errString = val;
2256 sprintf (val, "%d", *zop->u.prox->proximityUnitCode);
2260 r = rpn_proximity (zh, bool_parms.rset_l, bool_parms.rset_r,
2261 *zop->u.prox->ordered,
2262 (!zop->u.prox->exclusion ? 0 :
2263 *zop->u.prox->exclusion),
2264 *zop->u.prox->relationType,
2265 *zop->u.prox->distance);
2272 else if (zs->which == Z_RPNStructure_simple)
2274 if (zs->u.simple->which == Z_Operand_APT)
2276 logf (LOG_DEBUG, "rpn_search_APT");
2277 r = rpn_search_APT (zh, zs->u.simple->u.attributesPlusTerm,
2278 attributeSet, stream, sort_sequence,
2279 num_bases, basenames);
2281 else if (zs->u.simple->which == Z_Operand_resultSetId)
2283 logf (LOG_DEBUG, "rpn_search_ref");
2284 r = resultSetRef (zh, zs->u.simple->u.resultSetId);
2286 r = rset_create (rset_kind_null, NULL);
2303 RSET rpn_search (ZebraHandle zh, NMEM nmem,
2304 Z_RPNQuery *rpn, int num_bases, char **basenames,
2305 const char *setname,
2310 oid_value attributeSet;
2311 Z_SortKeySpecList *sort_sequence;
2315 zh->errString = NULL;
2318 sort_sequence = (Z_SortKeySpecList *)
2319 nmem_malloc (nmem, sizeof(*sort_sequence));
2320 sort_sequence->num_specs = 10;
2321 sort_sequence->specs = (Z_SortKeySpec **)
2322 nmem_malloc (nmem, sort_sequence->num_specs *
2323 sizeof(*sort_sequence->specs));
2324 for (i = 0; i<sort_sequence->num_specs; i++)
2325 sort_sequence->specs[i] = 0;
2327 attrset = oid_getentbyoid (rpn->attributeSetId);
2328 attributeSet = attrset->value;
2329 rset = rpn_search_structure (zh, rpn->RPNStructure, attributeSet,
2330 nmem, sort_sequence, num_bases, basenames);
2335 logf (LOG_DEBUG, "search error: %d", zh->errCode);
2337 for (i = 0; sort_sequence->specs[i]; i++)
2339 sort_sequence->num_specs = i;
2341 resultSetRank (zh, sset, rset);
2344 logf (LOG_DEBUG, "resultSetSortSingle in rpn_search");
2345 resultSetSortSingle (zh, nmem, sset, rset,
2346 sort_sequence, &sort_status);
2349 logf (LOG_DEBUG, "resultSetSortSingle status = %d", zh->errCode);
2355 struct scan_info_entry {
2361 struct scan_info_entry *list;
2367 static int scan_handle (char *name, const char *info, int pos, void *client)
2369 int len_prefix, idx;
2370 struct scan_info *scan_info = (struct scan_info *) client;
2372 len_prefix = strlen(scan_info->prefix);
2373 if (memcmp (name, scan_info->prefix, len_prefix))
2375 if (pos > 0) idx = scan_info->after - pos + scan_info->before;
2378 scan_info->list[idx].term = (char *)
2379 odr_malloc (scan_info->odr, strlen(name + len_prefix)+1);
2380 strcpy (scan_info->list[idx].term, name + len_prefix);
2381 assert (*info == sizeof(ISAM_P));
2382 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAM_P));
2386 static void scan_term_untrans (ZebraHandle zh, NMEM stream, int reg_type,
2387 char **dst, const char *src)
2389 char term_dst[1024];
2391 term_untrans (zh, reg_type, term_dst, src);
2393 *dst = (char *) nmem_malloc (stream, strlen(term_dst)+1);
2394 strcpy (*dst, term_dst);
2397 static void count_set (RSET r, int *count)
2405 logf (LOG_DEBUG, "count_set");
2408 rfd = rset_open (r, RSETF_READ);
2409 while (rset_read (r, rfd, &key, &term_index))
2411 if (key.sysno != psysno)
2418 rset_close (r, rfd);
2419 logf (LOG_DEBUG, "%d keys, %d records", kno, *count);
2422 void rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
2423 oid_value attributeset,
2424 int num_bases, char **basenames,
2425 int *position, int *num_entries, ZebraScanEntry **list,
2429 int pos = *position;
2430 int num = *num_entries;
2434 char termz[IT_MAX_WORD+20];
2437 struct scan_info *scan_info_array;
2438 ZebraScanEntry *glist;
2439 int ords[32], ord_no = 0;
2443 char *search_type = NULL;
2444 char *rank_type = NULL;
2449 if (attributeset == VAL_NONE)
2450 attributeset = VAL_BIB1;
2452 logf (LOG_DEBUG, "position = %d, num = %d", pos, num);
2454 attr_init (&use, zapt, 1);
2455 use_value = attr_find (&use, &attributeset);
2457 if (zebra_maps_attr (zh->zebra_maps, zapt, ®_id, &search_type,
2458 &rank_type, &complete_flag, &sort_flag))
2464 if (use_value == -1)
2466 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
2470 data1_local_attribute *local_attr;
2472 if ((r=att_getentbyatt (zh, &attp, attributeset, use_value)))
2474 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
2475 attributeset, use_value);
2483 if (zebraExplain_curDatabase (zh->zei, basenames[base_no]))
2485 zh->errString = basenames[base_no];
2486 zh->errCode = 109; /* Database unavailable */
2489 for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
2490 local_attr = local_attr->next)
2494 ord = zebraExplain_lookupSU (zh->zei, attp.attset_ordinal,
2497 ords[ord_no++] = ord;
2505 /* prepare dictionary scanning */
2508 scan_info_array = (struct scan_info *)
2509 odr_malloc (stream, ord_no * sizeof(*scan_info_array));
2510 for (i = 0; i < ord_no; i++)
2512 int j, prefix_len = 0;
2513 int before_tmp = before, after_tmp = after;
2514 struct scan_info *scan_info = scan_info_array + i;
2515 struct rpn_char_map_info rcmi;
2517 rpn_char_map_prepare (zh, reg_id, &rcmi);
2519 scan_info->before = before;
2520 scan_info->after = after;
2521 scan_info->odr = stream;
2523 scan_info->list = (struct scan_info_entry *)
2524 odr_malloc (stream, (before+after) * sizeof(*scan_info->list));
2525 for (j = 0; j<before+after; j++)
2526 scan_info->list[j].term = NULL;
2528 prefix_len += key_SU_code (ords[i], termz + prefix_len);
2529 termz[prefix_len++] = reg_id;
2530 termz[prefix_len] = 0;
2531 strcpy (scan_info->prefix, termz);
2533 trans_scan_term (zh, zapt, termz+prefix_len, reg_id);
2535 dict_scan (zh->dict, termz, &before_tmp, &after_tmp, scan_info,
2538 glist = (ZebraScanEntry *)
2539 odr_malloc (stream, (before+after)*sizeof(*glist));
2541 /* consider terms after main term */
2542 for (i = 0; i < ord_no; i++)
2546 for (i = 0; i<after; i++)
2549 const char *mterm = NULL;
2553 for (j = 0; j < ord_no; j++)
2555 if (ptr[j] < before+after &&
2556 (tst=scan_info_array[j].list[ptr[j]].term) &&
2557 (!mterm || strcmp (tst, mterm) < 0))
2565 scan_term_untrans (zh, stream->mem, reg_id,
2566 &glist[i+before].term, mterm);
2567 rset = rset_trunc (zh, &scan_info_array[j0].list[ptr[j0]].isam_p, 1,
2568 glist[i+before].term, strlen(glist[i+before].term),
2572 for (j = j0+1; j<ord_no; j++)
2574 if (ptr[j] < before+after &&
2575 (tst=scan_info_array[j].list[ptr[j]].term) &&
2576 !strcmp (tst, mterm))
2578 rset_bool_parms bool_parms;
2582 rset_trunc (zh, &scan_info_array[j].list[ptr[j]].isam_p, 1,
2583 glist[i+before].term,
2584 strlen(glist[i+before].term), NULL);
2586 bool_parms.key_size = sizeof(struct it_key);
2587 bool_parms.cmp = key_compare_it;
2588 bool_parms.rset_l = rset;
2589 bool_parms.rset_r = rset2;
2591 rset = rset_create (rset_kind_or, &bool_parms);
2596 count_set (rset, &glist[i+before].occurrences);
2601 *num_entries -= (after-i);
2605 /* consider terms before main term */
2606 for (i = 0; i<ord_no; i++)
2609 for (i = 0; i<before; i++)
2612 const char *mterm = NULL;
2616 for (j = 0; j <ord_no; j++)
2618 if (ptr[j] < before &&
2619 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2620 (!mterm || strcmp (tst, mterm) > 0))
2629 scan_term_untrans (zh, stream->mem, reg_id,
2630 &glist[before-1-i].term, mterm);
2633 (zh, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1,
2634 glist[before-1-i].term, strlen(glist[before-1-i].term),
2639 for (j = j0+1; j<ord_no; j++)
2641 if (ptr[j] < before &&
2642 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2643 !strcmp (tst, mterm))
2645 rset_bool_parms bool_parms;
2648 rset2 = rset_trunc (zh,
2649 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1,
2650 glist[before-1-i].term,
2651 strlen(glist[before-1-i].term), NULL);
2653 bool_parms.key_size = sizeof(struct it_key);
2654 bool_parms.cmp = key_compare_it;
2655 bool_parms.rset_l = rset;
2656 bool_parms.rset_r = rset2;
2658 rset = rset_create (rset_kind_or, &bool_parms);
2663 count_set (rset, &glist[before-1-i].occurrences);
2673 *list = glist + i; /* list is set to first 'real' entry */
2675 logf (LOG_DEBUG, "position = %d, num_entries = %d",
2676 *position, *num_entries);
2678 logf (LOG_DEBUG, "scan error: %d", zh->errCode);