2 * Copyright (C) 1995-2000, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.104 2000-04-05 09:49:35 adam
8 * On Unix, zebra/z'mbol uses automake.
10 * Revision 1.103 2000/03/20 19:08:36 adam
11 * Added remote record import using Z39.50 extended services and Segment
14 * Revision 1.102 2000/03/15 15:00:31 adam
15 * First work on threaded version.
17 * Revision 1.101 2000/03/02 14:35:03 adam
18 * Fixed proximity handling.
20 * Revision 1.100 1999/12/28 15:48:12 adam
23 * Revision 1.99 1999/12/23 09:03:32 adam
24 * Changed behaviour of trunc=105 so that * is regular .* and ! is regular .
26 * Revision 1.98 1999/11/30 13:48:04 adam
27 * Improved installation. Updated for inclusion of YAZ header files.
29 * Revision 1.97 1999/10/14 14:33:50 adam
30 * Added truncation 5=106.
32 * Revision 1.96 1999/09/23 10:05:05 adam
33 * Implemented structure=105 searching.
35 * Revision 1.95 1999/09/07 07:19:21 adam
36 * Work on character mapping. Implemented replace rules.
38 * Revision 1.94 1999/07/20 13:59:18 adam
39 * Fixed bug that occurred when phrases had 0 hits.
41 * Revision 1.93 1999/06/17 14:38:40 adam
42 * Bug fix: Scan SEGV'ed when getting unknown use attribute.
44 * Revision 1.92 1999/05/26 07:49:13 adam
47 * Revision 1.91 1999/02/02 14:51:13 adam
48 * Updated WIN32 code specific sections. Changed header.
50 * Revision 1.90 1998/11/16 16:03:43 adam
51 * Moved loggin utilities to Yaz. Was implemented in file zlogs.c.
53 * Revision 1.89 1998/11/16 10:11:55 adam
54 * Added addtional info for error 114 - unsupported use attribute.
56 * Revision 1.88 1998/10/18 07:54:52 adam
57 * Additional info added for diagnostics 114 (Unsupported use attribute) and
58 * 121 (Unsupported attribute set).
60 * Revision 1.87 1998/09/28 11:19:12 adam
61 * Fix for Compiled ASN.1.
63 * Revision 1.86 1998/09/22 10:48:20 adam
64 * Minor changes in search API.
66 * Revision 1.85 1998/09/22 10:03:43 adam
67 * Changed result sets to be persistent in the sense that they can
68 * be re-searched if needed.
69 * Fixed memory leak in rsm_or.
71 * Revision 1.84 1998/09/18 12:41:00 adam
72 * Fixed bug with numerical relations.
74 * Revision 1.83 1998/09/02 13:53:19 adam
75 * Extra parameter decode added to search routines to implement
78 * Revision 1.82 1998/06/26 11:16:40 quinn
79 * Added support (un-optimised) for left and left/right truncation
81 * Revision 1.81 1998/06/24 12:16:14 adam
82 * Support for relations on text operands. Open range support in
83 * DFA module (i.e. [-j], [g-]).
85 * Revision 1.80 1998/06/23 15:33:34 adam
86 * Added feature to specify sort criteria in query (type 7 specifies
89 * Revision 1.79 1998/06/22 11:35:09 adam
92 * Revision 1.78 1998/06/08 14:43:17 adam
93 * Added suport for EXPLAIN Proxy servers - added settings databasePath
94 * and explainDatabase to facilitate this. Increased maximum number
95 * of databases and attributes in one register.
97 * Revision 1.77 1998/05/20 10:12:22 adam
98 * Implemented automatic EXPLAIN database maintenance.
99 * Modified Zebra to work with ASN.1 compiled version of YAZ.
101 * Revision 1.76 1998/04/02 14:35:29 adam
102 * First version of Zebra that works with compiled ASN.1.
104 * Revision 1.75 1998/03/05 08:45:13 adam
105 * New result set model and modular ranking system. Moved towards
106 * descent server API. System information stored as "SGML" records.
108 * Revision 1.74 1998/02/10 12:03:06 adam
111 * Revision 1.73 1998/01/29 13:40:11 adam
112 * Better logging for scan service.
114 * Revision 1.72 1998/01/07 13:53:41 adam
115 * Queries using simple ranked operands returns right number of hits.
117 * Revision 1.71 1997/12/18 10:54:24 adam
118 * New method result set method rs_hits that returns the number of
119 * hits in result-set (if known). The ranked result set returns real
120 * number of hits but only when not combined with other operands.
122 * Revision 1.70 1997/10/31 12:34:43 adam
123 * Changed a few log statements.
125 * Revision 1.69 1997/10/29 12:05:02 adam
126 * Server produces diagnostic "Unsupported Attribute Set" when appropriate.
128 * Revision 1.68 1997/10/27 14:33:06 adam
129 * Moved towards generic character mapping depending on "structure"
130 * field in abstract syntax file. Fixed a few memory leaks. Fixed
131 * bug with negative integers when doing searches with relational
134 * Revision 1.67 1997/09/29 09:06:10 adam
135 * Removed one static var in order to make this module thread safe.
137 * Revision 1.66 1997/09/25 14:58:03 adam
140 * Revision 1.65 1997/09/22 12:39:06 adam
141 * Added get_pos method for the ranked result sets.
143 * Revision 1.64 1997/09/18 08:59:20 adam
144 * Extra generic handle for the character mapping routines.
146 * Revision 1.63 1997/09/17 12:19:18 adam
147 * Zebra version corresponds to YAZ version 1.4.
148 * Changed Zebra server so that it doesn't depend on global common_resource.
150 * Revision 1.62 1997/09/05 15:30:09 adam
151 * Changed prototype for chr_map_input - added const.
152 * Added support for C++, headers uses extern "C" for public definitions.
154 * Revision 1.61 1997/02/10 10:21:14 adam
155 * Bug fix: in search terms character (^) wasn't observed.
157 * Revision 1.60 1997/01/31 11:10:34 adam
158 * Bug fix: Leading and trailing white space weren't removed in scan tokens.
160 * Revision 1.59 1997/01/17 11:31:46 adam
161 * Bug fix: complete phrase search didn't work.
163 * Revision 1.58 1996/12/23 15:30:45 adam
164 * Work on truncation.
165 * Bug fix: result sets weren't deleted after server shut down.
167 * Revision 1.57 1996/11/11 13:38:02 adam
168 * Added proximity support in search.
170 * Revision 1.56 1996/11/08 11:10:32 adam
171 * Buffers used during file match got bigger.
172 * Compressed ISAM support everywhere.
173 * Bug fixes regarding masking characters in queries.
174 * Redesigned Regexp-2 queries.
176 * Revision 1.55 1996/11/04 14:07:44 adam
177 * Moved truncation code to trunc.c.
179 * Revision 1.54 1996/10/29 14:09:52 adam
180 * Use of cisam system - enabled if setting isamc is 1.
182 * Revision 1.53 1996/06/26 09:21:43 adam
183 * Bug fix: local attribute set wasn't obeyed in scan.
185 * Revision 1.52 1996/06/17 14:26:20 adam
186 * Function gen_regular_rel changed to handle negative numbers.
188 * Revision 1.51 1996/06/11 10:54:15 quinn
191 * Revision 1.50 1996/06/07 08:51:53 adam
192 * Bug fix: Character mapping was broken (introducued by last revision).
194 * Revision 1.49 1996/06/04 10:18:11 adam
195 * Search/scan uses character mapping module.
197 * Revision 1.48 1996/05/28 15:15:01 adam
198 * Bug fix: Didn't handle unknown database correctly.
200 * Revision 1.47 1996/05/15 18:36:28 adam
201 * Function trans_term transforms unsearchable characters to blanks.
203 * Revision 1.46 1996/05/15 11:57:56 adam
204 * Fixed bug introduced by set/field mapping in search operations.
206 * Revision 1.45 1996/05/14 11:34:00 adam
207 * Scan support in multiple registers/databases.
209 * Revision 1.44 1996/05/14 06:16:44 adam
210 * Compact use/set bytes used in search service.
212 * Revision 1.43 1996/05/09 09:54:43 adam
213 * Server supports maps from one logical attributes to a list of physical
215 * The extraction process doesn't make space consuming 'any' keys.
217 * Revision 1.42 1996/05/09 07:28:56 quinn
218 * Work towards phrases and multiple registers
220 * Revision 1.41 1996/03/20 09:36:43 adam
221 * Function dict_lookup_grep got extra parameter, init_pos, which marks
222 * from which position in pattern approximate pattern matching should occur.
223 * Approximate pattern matching is used in relevance=re-2.
225 * Revision 1.40 1996/02/02 13:44:44 adam
226 * The public dictionary functions simply use char instead of Dict_char
227 * to represent search strings. Dict_char is used internally only.
229 * Revision 1.39 1996/01/03 16:22:13 quinn
230 * operator->roperator
232 * Revision 1.38 1995/12/11 09:12:55 adam
233 * The rec_get function returns NULL if record doesn't exist - will
234 * happen in the server if the result set records have been deleted since
235 * the creation of the set (i.e. the search).
236 * The server saves a result temporarily if it is 'volatile', i.e. the
237 * set is register dependent.
239 * Revision 1.37 1995/12/06 15:05:28 adam
240 * More verbose in count_set.
242 * Revision 1.36 1995/12/06 12:41:27 adam
243 * New command 'stat' for the index program.
244 * Filenames can be read from stdin by specifying '-'.
245 * Bug fix/enhancement of the transformation from terms to regular
246 * expressons in the search engine.
248 * Revision 1.35 1995/11/27 09:29:00 adam
249 * Bug fixes regarding conversion to regular expressions.
251 * Revision 1.34 1995/11/16 17:00:56 adam
252 * Better logging of rpn query.
254 * Revision 1.33 1995/11/01 13:58:28 quinn
255 * Moving data1 to yaz/retrieval
257 * Revision 1.32 1995/10/27 14:00:11 adam
258 * Implemented detection of database availability.
260 * Revision 1.31 1995/10/17 18:02:10 adam
261 * New feature: databases. Implemented as prefix to words in dictionary.
263 * Revision 1.30 1995/10/16 09:32:38 adam
264 * More work on relational op.
266 * Revision 1.29 1995/10/13 16:01:49 adam
269 * Revision 1.28 1995/10/13 12:26:43 adam
270 * Optimization of truncation.
272 * Revision 1.27 1995/10/12 17:07:22 adam
275 * Revision 1.26 1995/10/12 12:40:54 adam
276 * Bug fixes in rpn_prox.
278 * Revision 1.25 1995/10/10 13:59:24 adam
279 * Function rset_open changed its wflag parameter to general flags.
281 * Revision 1.24 1995/10/09 16:18:37 adam
282 * Function dict_lookup_grep got extra client data parameter.
284 * Revision 1.23 1995/10/06 16:33:37 adam
285 * Use attribute mappings.
287 * Revision 1.22 1995/10/06 15:07:39 adam
288 * Structure 'local-number' handled.
290 * Revision 1.21 1995/10/06 13:52:06 adam
291 * Bug fixes. Handler may abort further scanning.
293 * Revision 1.20 1995/10/06 11:06:33 adam
294 * Scan entries include 'occurrences' now.
296 * Revision 1.19 1995/10/06 10:43:56 adam
297 * Scan added. 'occurrences' in scan entries not set yet.
299 * Revision 1.18 1995/10/04 16:57:20 adam
300 * Key input and merge sort in one pass.
302 * Revision 1.17 1995/10/04 12:55:17 adam
303 * Bug fix in ranked search. Use=Any keys inserted.
305 * Revision 1.16 1995/10/02 16:24:40 adam
306 * Use attribute actually used in search requests.
308 * Revision 1.15 1995/10/02 15:18:52 adam
309 * New member in recRetrieveCtrl: diagnostic.
311 * Revision 1.14 1995/09/28 12:10:32 adam
312 * Bug fixes. Field prefix used in queries.
314 * Revision 1.13 1995/09/18 14:17:50 adam
317 * Revision 1.12 1995/09/15 14:45:21 adam
319 * Work on truncation.
321 * Revision 1.11 1995/09/14 11:53:27 adam
322 * First work on regular expressions/truncations.
324 * Revision 1.10 1995/09/11 15:23:26 adam
325 * More work on relevance search.
327 * Revision 1.9 1995/09/11 13:09:35 adam
328 * More work on relevance feedback.
330 * Revision 1.8 1995/09/08 14:52:27 adam
331 * Minor changes. Dictionary is lower case now.
333 * Revision 1.7 1995/09/07 13:58:36 adam
334 * New parameter: result-set file descriptor (RSFD) to support multiple
335 * positions within the same result-set.
336 * Boolean operators: and, or, not implemented.
337 * Result-set references.
339 * Revision 1.6 1995/09/06 16:11:18 adam
340 * Option: only one word key per file.
342 * Revision 1.5 1995/09/06 10:33:04 adam
343 * More work on present. Some log messages removed.
345 * Revision 1.4 1995/09/05 15:28:40 adam
346 * More work on search engine.
348 * Revision 1.3 1995/09/04 15:20:22 adam
351 * Revision 1.2 1995/09/04 12:33:43 adam
352 * Various cleanup. YAZ util used instead.
354 * Revision 1.1 1995/09/04 09:10:40 adam
355 * More work on index add/del/update.
356 * Merge sort implemented.
357 * Initial work on z39 server.
376 struct rpn_char_map_info {
381 static const char **rpn_char_map_handler (void *vp, const char **from, int len)
383 struct rpn_char_map_info *p = (struct rpn_char_map_info *) vp;
384 return zebra_maps_input (p->zm, p->reg_type, from, len);
387 static void rpn_char_map_prepare (ZebraHandle zh, int reg_type,
388 struct rpn_char_map_info *map_info)
390 map_info->zm = zh->service->zebra_maps;
391 map_info->reg_type = reg_type;
392 dict_grep_cmap (zh->service->dict, map_info, rpn_char_map_handler);
399 Z_AttributesPlusTerm *zapt;
402 static int attr_find (AttrType *src, oid_value *attributeSetP)
407 num_attributes = src->zapt->attributes->num_attributes;
409 num_attributes = src->zapt->num_attributes;
411 while (src->major < num_attributes)
413 Z_AttributeElement *element;
416 element = src->zapt->attributes->attributes[src->major];
418 element = src->zapt->attributeList[src->major];
420 if (src->type == *element->attributeType)
422 switch (element->which)
424 case Z_AttributeValue_numeric:
426 if (element->attributeSet && attributeSetP)
430 attrset = oid_getentbyoid (element->attributeSet);
431 *attributeSetP = attrset->value;
433 return *element->value.numeric;
435 case Z_AttributeValue_complex:
436 if (src->minor >= element->value.complex->num_list ||
437 element->value.complex->list[src->minor]->which !=
438 Z_StringOrNumeric_numeric)
441 if (element->attributeSet && attributeSetP)
445 attrset = oid_getentbyoid (element->attributeSet);
446 *attributeSetP = attrset->value;
448 return *element->value.complex->list[src->minor-1]->u.numeric;
458 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
480 static void term_untrans (ZebraHandle zh, int reg_type,
481 char *dst, const char *src)
485 const char *cp = zebra_maps_output (zh->service->zebra_maps,
496 static void add_isam_p (const char *name, const char *info,
499 if (p->isam_p_indx == p->isam_p_size)
501 ISAMS_P *new_isam_p_buf;
505 p->isam_p_size = 2*p->isam_p_size + 100;
506 new_isam_p_buf = (ISAMS_P *) xmalloc (sizeof(*new_isam_p_buf) *
510 memcpy (new_isam_p_buf, p->isam_p_buf,
511 p->isam_p_indx * sizeof(*p->isam_p_buf));
512 xfree (p->isam_p_buf);
514 p->isam_p_buf = new_isam_p_buf;
517 new_term_no = (int *) xmalloc (sizeof(*new_term_no) *
521 memcpy (new_term_no, p->isam_p_buf,
522 p->isam_p_indx * sizeof(*p->term_no));
525 p->term_no = new_term_no;
528 assert (*info == sizeof(*p->isam_p_buf));
529 memcpy (p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
532 term_untrans (p->zh, p->reg_type, term_tmp, name+2);
533 logf (LOG_DEBUG, "grep: %s", term_tmp);
538 static int grep_handle (char *name, const char *info, void *p)
540 add_isam_p (name, info, (struct grep_info *) p);
544 static int term_pre (ZebraMaps zebra_maps, int reg_type, const char **src,
545 const char *ct1, const char *ct2)
547 const char *s1, *s0 = *src;
550 /* skip white space */
553 if (ct1 && strchr (ct1, *s0))
555 if (ct2 && strchr (ct2, *s0))
558 map = zebra_maps_input (zebra_maps, reg_type, &s1, strlen(s1));
559 if (**map != *CHR_SPACE)
567 /* term_100: handle term, where trunc=none (no operators at all) */
568 static int term_100 (ZebraMaps zebra_maps, int reg_type,
569 const char **src, char *dst, int space_split,
577 if (!term_pre (zebra_maps, reg_type, src, NULL, NULL))
583 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
584 if (space_split && **map == *CHR_SPACE)
588 if (!isalnum (*s1) && *s1 != '-')
600 /* term_101: handle term, where trunc=Process # */
601 static int term_101 (ZebraMaps zebra_maps, int reg_type,
602 const char **src, char *dst, int space_split,
610 if (!term_pre (zebra_maps, reg_type, src, "#", "#"))
619 dst_term[j++] = *s0++;
624 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
625 if (space_split && **map == *CHR_SPACE)
637 dst_term[j++] = '\0';
642 /* term_103: handle term, where trunc=re-2 (regular expressions) */
643 static int term_103 (ZebraMaps zebra_maps, int reg_type, const char **src,
644 char *dst, int *errors, int space_split,
652 if (!term_pre (zebra_maps, reg_type, src, "^\\()[].*+?|", "("))
655 if (errors && *s0 == '+' && s0[1] && s0[2] == '+' && s0[3] &&
658 *errors = s0[1] - '0';
665 if (strchr ("^\\()[].*+?|-", *s0))
673 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
674 if (**map == *CHR_SPACE)
691 /* term_103: handle term, where trunc=re-1 (regular expressions) */
692 static int term_102 (ZebraMaps zebra_maps, int reg_type, const char **src,
693 char *dst, int space_split, char *dst_term)
695 return term_103 (zebra_maps, reg_type, src, dst, NULL, space_split,
700 /* term_104: handle term, where trunc=Process # and ! */
701 static int term_104 (ZebraMaps zebra_maps, int reg_type,
702 const char **src, char *dst, int space_split,
710 if (!term_pre (zebra_maps, reg_type, src, "#!", "#!"))
719 dst_term[j++] = *s0++;
724 dst_term[j++] = *s0++;
728 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
729 if (space_split && **map == *CHR_SPACE)
741 dst_term[j++] = '\0';
746 /* term_105/106: handle term, where trunc=Process * and ! and right trunc */
747 static int term_105 (ZebraMaps zebra_maps, int reg_type,
748 const char **src, char *dst, int space_split,
749 char *dst_term, int right_truncate)
756 if (!term_pre (zebra_maps, reg_type, src, "*!", "*!"))
765 dst_term[j++] = *s0++;
770 dst_term[j++] = *s0++;
774 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
775 if (space_split && **map == *CHR_SPACE)
793 dst_term[j++] = '\0';
799 /* gen_regular_rel - generate regular expression from relation
800 * val: border value (inclusive)
801 * islt: 1 if <=; 0 if >=.
803 static void gen_regular_rel (char *dst, int val, int islt)
810 logf (LOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
814 strcpy (dst, "(-[0-9]+|(");
822 strcpy (dst, "([0-9]+|-(");
823 dst_p = strlen (dst);
833 dst_p = strlen (dst);
834 sprintf (numstr, "%d", val);
835 for (w = strlen(numstr); --w >= 0; pos++)
854 strcpy (dst + dst_p, numstr);
855 dst_p = strlen(dst) - pos - 1;
883 for (i = 0; i<pos; i++)
896 /* match everything less than 10^(pos-1) */
898 for (i=1; i<pos; i++)
899 strcat (dst, "[0-9]?");
903 /* match everything greater than 10^pos */
904 for (i = 0; i <= pos; i++)
905 strcat (dst, "[0-9]");
906 strcat (dst, "[0-9]*");
911 void string_rel_add_char (char **term_p, const char *src, int *indx)
913 if (src[*indx] == '\\')
914 *(*term_p)++ = src[(*indx)++];
915 *(*term_p)++ = src[(*indx)++];
919 * > abc ([b-].*|a[c-].*|ab[d-].*|abc.+)
920 * ([^-a].*|a[^-b].*ab[^-c].*|abc.+)
921 * >= abc ([b-].*|a[c-].*|ab[c-].*)
922 * ([^-a].*|a[^-b].*|ab[c-].*)
923 * < abc ([-0].*|a[-a].*|ab[-b].*)
924 * ([^a-].*|a[^b-].*|ab[^c-].*)
925 * <= abc ([-0].*|a[-a].*|ab[-b].*|abc)
926 * ([^a-].*|a[^b-].*|ab[^c-].*|abc)
928 static int string_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
929 const char **term_sub, char *term_dict,
930 oid_value attributeSet,
931 int reg_type, int space_split, char *term_dst)
936 char *term_tmp = term_dict + strlen(term_dict);
937 char term_component[256];
939 attr_init (&relation, zapt, 2);
940 relation_value = attr_find (&relation, NULL);
942 logf (LOG_DEBUG, "string relation value=%d", relation_value);
943 switch (relation_value)
946 if (!term_100 (zh->service->zebra_maps, reg_type,
947 term_sub, term_component,
948 space_split, term_dst))
950 logf (LOG_DEBUG, "Relation <");
953 for (i = 0; term_component[i]; )
960 string_rel_add_char (&term_tmp, term_component, &j);
965 string_rel_add_char (&term_tmp, term_component, &i);
976 if (!term_100 (zh->service->zebra_maps, reg_type,
977 term_sub, term_component,
978 space_split, term_dst))
980 logf (LOG_DEBUG, "Relation <=");
983 for (i = 0; term_component[i]; )
988 string_rel_add_char (&term_tmp, term_component, &j);
992 string_rel_add_char (&term_tmp, term_component, &i);
1001 for (i = 0; term_component[i]; )
1002 string_rel_add_char (&term_tmp, term_component, &i);
1007 if (!term_100 (zh->service->zebra_maps, reg_type,
1008 term_sub, term_component, space_split, term_dst))
1010 logf (LOG_DEBUG, "Relation >");
1013 for (i = 0; term_component[i];)
1018 string_rel_add_char (&term_tmp, term_component, &j);
1023 string_rel_add_char (&term_tmp, term_component, &i);
1031 for (i = 0; term_component[i];)
1032 string_rel_add_char (&term_tmp, term_component, &i);
1039 if (!term_100 (zh->service->zebra_maps, reg_type, term_sub,
1040 term_component, space_split, term_dst))
1042 logf (LOG_DEBUG, "Relation >=");
1045 for (i = 0; term_component[i];)
1052 string_rel_add_char (&term_tmp, term_component, &j);
1055 if (term_component[i+1])
1059 string_rel_add_char (&term_tmp, term_component, &i);
1063 string_rel_add_char (&term_tmp, term_component, &i);
1075 logf (LOG_DEBUG, "Relation =");
1076 if (!term_100 (zh->service->zebra_maps, reg_type, term_sub,
1077 term_component, space_split, term_dst))
1079 strcat (term_tmp, "(");
1080 strcat (term_tmp, term_component);
1081 strcat (term_tmp, ")");
1086 static int string_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1087 const char **term_sub,
1088 oid_value attributeSet, NMEM stream,
1089 struct grep_info *grep_info,
1090 int reg_type, int complete_flag,
1091 int num_bases, char **basenames,
1094 char term_dict[2*IT_MAX_WORD+4000];
1096 AttrType truncation;
1097 int truncation_value;
1100 oid_value curAttributeSet = attributeSet;
1102 struct rpn_char_map_info rcmi;
1103 int space_split = complete_flag ? 0 : 1;
1105 rpn_char_map_prepare (zh, reg_type, &rcmi);
1106 attr_init (&use, zapt, 1);
1107 use_value = attr_find (&use, &curAttributeSet);
1108 logf (LOG_DEBUG, "string_term, use value %d", use_value);
1109 attr_init (&truncation, zapt, 5);
1110 truncation_value = attr_find (&truncation, NULL);
1111 logf (LOG_DEBUG, "truncation value %d", truncation_value);
1113 if (use_value == -1)
1116 for (base_no = 0; base_no < num_bases; base_no++)
1119 data1_local_attribute *local_attr;
1120 int max_pos, prefix_len = 0;
1123 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
1125 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1126 curAttributeSet, use_value, r);
1130 sprintf (val_str, "%d", use_value);
1132 zh->errString = nmem_strdup (stream, val_str);
1137 struct oident oident;
1139 oident.proto = PROTO_Z3950;
1140 oident.oclass = CLASS_ATTSET;
1141 oident.value = curAttributeSet;
1142 oid_ent_to_oid (&oident, oid);
1145 zh->errString = nmem_strdup (stream, oident.desc);
1149 if (zebraExplain_curDatabase (zh->service->zei, basenames[base_no]))
1151 zh->errCode = 109; /* Database unavailable */
1152 zh->errString = basenames[base_no];
1155 for (local_attr = attp.local_attributes; local_attr;
1156 local_attr = local_attr->next)
1162 ord = zebraExplain_lookupSU (zh->service->zei, attp.attset_ordinal,
1167 term_dict[prefix_len++] = '|';
1169 term_dict[prefix_len++] = '(';
1171 ord_len = key_SU_code (ord, ord_buf);
1172 for (i = 0; i<ord_len; i++)
1174 term_dict[prefix_len++] = 1;
1175 term_dict[prefix_len++] = ord_buf[i];
1181 sprintf (val_str, "%d", use_value);
1183 zh->errString = nmem_strdup (stream, val_str);
1186 term_dict[prefix_len++] = ')';
1187 term_dict[prefix_len++] = 1;
1188 term_dict[prefix_len++] = reg_type;
1189 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1190 term_dict[prefix_len] = '\0';
1192 switch (truncation_value)
1194 case -1: /* not specified */
1195 case 100: /* do not truncate */
1196 if (!string_relation (zh, zapt, &termp, term_dict,
1198 reg_type, space_split, term_dst))
1200 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_dict+prefix_len);
1201 r = dict_lookup_grep (zh->service->dict, term_dict, 0,
1202 grep_info, &max_pos, 0, grep_handle);
1204 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1206 case 1: /* right truncation */
1207 term_dict[j++] = '(';
1208 if (!term_100 (zh->service->zebra_maps, reg_type,
1209 &termp, term_dict + j, space_split, term_dst))
1211 strcat (term_dict, ".*)");
1212 dict_lookup_grep (zh->service->dict, term_dict, 0, grep_info,
1213 &max_pos, 0, grep_handle);
1215 case 2: /* keft truncation */
1216 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1217 if (!term_100 (zh->service->zebra_maps, reg_type,
1218 &termp, term_dict + j, space_split, term_dst))
1220 strcat (term_dict, ")");
1221 dict_lookup_grep (zh->service->dict, term_dict, 0, grep_info,
1222 &max_pos, 0, grep_handle);
1224 case 3: /* left&right truncation */
1225 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1226 if (!term_100 (zh->service->zebra_maps, reg_type,
1227 &termp, term_dict + j, space_split, term_dst))
1229 strcat (term_dict, ".*)");
1230 dict_lookup_grep (zh->service->dict, term_dict, 0, grep_info,
1231 &max_pos, 0, grep_handle);
1235 case 101: /* process # in term */
1236 term_dict[j++] = '(';
1237 if (!term_101 (zh->service->zebra_maps, reg_type,
1238 &termp, term_dict + j, space_split, term_dst))
1240 strcat (term_dict, ")");
1241 r = dict_lookup_grep (zh->service->dict, term_dict, 0, grep_info,
1242 &max_pos, 0, grep_handle);
1244 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d", r);
1246 case 102: /* Regexp-1 */
1247 term_dict[j++] = '(';
1248 if (!term_102 (zh->service->zebra_maps, reg_type,
1249 &termp, term_dict + j, space_split, term_dst))
1251 strcat (term_dict, ")");
1252 logf (LOG_DEBUG, "Regexp-1 tolerance=%d", r);
1253 r = dict_lookup_grep (zh->service->dict, term_dict, 0, grep_info,
1254 &max_pos, 0, grep_handle);
1256 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
1259 case 103: /* Regexp-2 */
1261 term_dict[j++] = '(';
1262 if (!term_103 (zh->service->zebra_maps, reg_type,
1263 &termp, term_dict + j, &r, space_split, term_dst))
1265 strcat (term_dict, ")");
1266 logf (LOG_DEBUG, "Regexp-2 tolerance=%d", r);
1267 r = dict_lookup_grep (zh->service->dict, term_dict, r, grep_info,
1268 &max_pos, 2, grep_handle);
1270 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
1273 case 104: /* process # and ! in term */
1274 term_dict[j++] = '(';
1275 if (!term_104 (zh->service->zebra_maps, reg_type,
1276 &termp, term_dict + j, space_split, term_dst))
1278 strcat (term_dict, ")");
1279 r = dict_lookup_grep (zh->service->dict, term_dict, 0, grep_info,
1280 &max_pos, 0, grep_handle);
1282 logf (LOG_WARN, "dict_lookup_grep err, trunc=#/!: %d", r);
1284 case 105: /* process * and ! in term */
1285 term_dict[j++] = '(';
1286 if (!term_105 (zh->service->zebra_maps, reg_type,
1287 &termp, term_dict + j, space_split, term_dst, 1))
1289 strcat (term_dict, ")");
1290 r = dict_lookup_grep (zh->service->dict, term_dict, 0, grep_info,
1291 &max_pos, 0, grep_handle);
1293 logf (LOG_WARN, "dict_lookup_grep err, trunc=*/!: %d", r);
1295 case 106: /* process * and ! in term */
1296 term_dict[j++] = '(';
1297 if (!term_105 (zh->service->zebra_maps, reg_type,
1298 &termp, term_dict + j, space_split, term_dst, 0))
1300 strcat (term_dict, ")");
1301 r = dict_lookup_grep (zh->service->dict, term_dict, 0, grep_info,
1302 &max_pos, 0, grep_handle);
1304 logf (LOG_WARN, "dict_lookup_grep err, trunc=*/!: %d", r);
1309 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1313 static void trans_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1317 Z_Term *term = zapt->term;
1319 sizez = term->u.general->len;
1320 if (sizez > IT_MAX_WORD-1)
1321 sizez = IT_MAX_WORD-1;
1322 memcpy (termz, term->u.general->buf, sizez);
1323 termz[sizez] = '\0';
1326 static void trans_scan_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1327 char *termz, int reg_type)
1329 Z_Term *term = zapt->term;
1331 const char *cp = (const char *) term->u.general->buf;
1332 const char *cp_end = cp + term->u.general->len;
1335 const char *space_map = NULL;
1338 while ((len = (cp_end - cp)) > 0)
1340 map = zebra_maps_input (zh->service->zebra_maps, reg_type, &cp, len);
1341 if (**map == *CHR_SPACE)
1346 for (src = space_map; *src; src++)
1349 for (src = *map; *src; src++)
1356 static RSET rpn_prox (ZebraHandle zh, RSET *rset, int rset_no,
1357 int ordered, int exclusion, int relation, int distance)
1362 struct it_key **buf;
1364 char prox_term[1024];
1365 int length_prox_term = 0;
1366 int min_nn = 10000000;
1368 const char *flags = NULL;
1370 rsfd = (RSFD *) xmalloc (sizeof(*rsfd)*rset_no);
1371 more = (int *) xmalloc (sizeof(*more)*rset_no);
1372 buf = (struct it_key **) xmalloc (sizeof(*buf)*rset_no);
1375 for (i = 0; i<rset_no; i++)
1378 for (j = 0; j<rset[i]->no_rset_terms; j++)
1380 const char *nflags = rset[i]->rset_terms[j]->flags;
1381 char *term = rset[i]->rset_terms[j]->name;
1382 int lterm = strlen(term);
1383 if (lterm + length_prox_term < sizeof(prox_term)-1)
1385 if (length_prox_term)
1386 prox_term[length_prox_term++] = ' ';
1387 strcpy (prox_term + length_prox_term, term);
1388 length_prox_term += lterm;
1390 if (min_nn > rset[i]->rset_terms[j]->nn)
1391 min_nn = rset[i]->rset_terms[j]->nn;
1395 for (i = 0; i<rset_no; i++)
1400 for (i = 0; i<rset_no; i++)
1402 buf[i] = (struct it_key *) xmalloc (sizeof(**buf));
1403 rsfd[i] = rset_open (rset[i], RSETF_READ);
1404 if (!(more[i] = rset_read (rset[i], rsfd[i], buf[i], &term_index)))
1409 /* at least one is empty ... return null set */
1410 rset_null_parms parms;
1412 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1414 parms.rset_term->nn = 0;
1415 result = rset_create (rset_kind_null, &parms);
1417 else if (ordered && relation == 3 && exclusion == 0 && distance == 1)
1419 /* special proximity case = phrase search ... */
1420 rset_temp_parms parms;
1423 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1425 parms.rset_term->nn = min_nn;
1426 parms.key_size = sizeof (struct it_key);
1427 parms.temp_path = res_get (zh->service->res, "setTmpDir");
1428 result = rset_create (rset_kind_temp, &parms);
1429 rsfd_result = rset_open (result, RSETF_WRITE);
1433 for (i = 1; i<rset_no; i++)
1442 cmp = key_compare_it (buf[i], buf[i-1]);
1445 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1446 buf[i-1], &term_index);
1451 if (buf[i-1]->seqno+1 != buf[i]->seqno)
1453 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1454 buf[i-1], &term_index);
1460 more[i] = rset_read (rset[i], rsfd[i], buf[i],
1467 rset_write (result, rsfd_result, buf[0]);
1468 more[0] = rset_read (*rset, *rsfd, *buf, &term_index);
1471 rset_close (result, rsfd_result);
1473 else if (rset_no == 2)
1475 /* generic proximity case (two input sets only) ... */
1476 rset_temp_parms parms;
1479 logf (LOG_LOG, "generic prox, dist = %d, relation = %d, ordered =%d, exclusion=%d",
1480 distance, relation, ordered, exclusion);
1481 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1483 parms.rset_term->nn = min_nn;
1484 parms.key_size = sizeof (struct it_key);
1485 parms.temp_path = res_get (zh->service->res, "setTmpDir");
1486 result = rset_create (rset_kind_temp, &parms);
1487 rsfd_result = rset_open (result, RSETF_WRITE);
1489 while (more[0] && more[1])
1491 int cmp = key_compare_it (buf[0], buf[1]);
1493 more[0] = rset_read (rset[0], rsfd[0], buf[0], &term_index);
1495 more[1] = rset_read (rset[1], rsfd[1], buf[1], &term_index);
1498 int sysno = buf[0]->sysno;
1502 seqno[n++] = buf[0]->seqno;
1503 while ((more[0] = rset_read (rset[0], rsfd[0], buf[0],
1505 sysno == buf[0]->sysno)
1507 seqno[n++] = buf[0]->seqno;
1510 for (i = 0; i<n; i++)
1512 int diff = buf[1]->seqno - seqno[i];
1513 int excl = exclusion;
1514 if (!ordered && diff < 0)
1519 if (diff < distance && diff >= 0)
1523 if (diff <= distance && diff >= 0)
1527 if (diff == distance && diff >= 0)
1531 if (diff >= distance && diff >= 0)
1535 if (diff > distance && diff >= 0)
1539 if (diff != distance && diff >= 0)
1545 rset_write (result, rsfd_result, buf[1]);
1549 } while ((more[1] = rset_read (rset[1], rsfd[1], buf[1],
1551 sysno == buf[1]->sysno);
1554 rset_close (result, rsfd_result);
1558 rset_null_parms parms;
1560 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1562 parms.rset_term->nn = 0;
1563 result = rset_create (rset_kind_null, &parms);
1565 for (i = 0; i<rset_no; i++)
1568 rset_close (rset[i], rsfd[i]);
1578 char *normalize_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1579 const char *termz, NMEM stream, unsigned reg_id)
1582 AttrType truncation;
1583 int truncation_value;
1586 attr_init (&truncation, zapt, 5);
1587 truncation_value = attr_find (&truncation, NULL);
1589 switch (truncation_value)
1609 wrbuf = zebra_replace(zh->service->zebra_maps, reg_id, ex_list,
1610 termz, strlen(termz));
1612 return nmem_strdup(stream, termz);
1615 char *buf = (char*) nmem_malloc (stream, wrbuf_len(wrbuf)+1);
1616 memcpy (buf, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1617 buf[wrbuf_len(wrbuf)] = '\0';
1622 static RSET rpn_search_APT_phrase (ZebraHandle zh,
1623 Z_AttributesPlusTerm *zapt,
1624 const char *termz_org,
1625 oid_value attributeSet,
1627 int reg_type, int complete_flag,
1628 const char *rank_type,
1629 int num_bases, char **basenames)
1631 char term_dst[IT_MAX_WORD+1];
1632 RSET rset[60], result;
1633 int i, r, rset_no = 0;
1634 struct grep_info grep_info;
1635 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1636 const char *termp = termz;
1639 grep_info.term_no = 0;
1641 grep_info.isam_p_size = 0;
1642 grep_info.isam_p_buf = NULL;
1644 grep_info.reg_type = reg_type;
1648 logf (LOG_DEBUG, "APT_phrase termp=%s", termp);
1649 grep_info.isam_p_indx = 0;
1650 r = string_term (zh, zapt, &termp, attributeSet, stream, &grep_info,
1651 reg_type, complete_flag, num_bases, basenames,
1655 logf (LOG_DEBUG, "term: %s", term_dst);
1656 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1657 grep_info.isam_p_indx, term_dst,
1658 strlen(term_dst), rank_type);
1659 assert (rset[rset_no]);
1660 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1664 xfree(grep_info.term_no);
1666 xfree (grep_info.isam_p_buf);
1669 rset_null_parms parms;
1671 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1672 return rset_create (rset_kind_null, &parms);
1674 else if (rset_no == 1)
1676 result = rpn_prox (zh, rset, rset_no, 1, 0, 3, 1);
1677 for (i = 0; i<rset_no; i++)
1678 rset_delete (rset[i]);
1682 static RSET rpn_search_APT_or_list (ZebraHandle zh,
1683 Z_AttributesPlusTerm *zapt,
1684 const char *termz_org,
1685 oid_value attributeSet,
1687 int reg_type, int complete_flag,
1688 const char *rank_type,
1689 int num_bases, char **basenames)
1691 char term_dst[IT_MAX_WORD+1];
1692 RSET rset[60], result;
1693 int i, r, rset_no = 0;
1694 struct grep_info grep_info;
1695 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1696 const char *termp = termz;
1698 grep_info.term_no = 0;
1700 grep_info.isam_p_size = 0;
1701 grep_info.isam_p_buf = NULL;
1703 grep_info.reg_type = reg_type;
1707 logf (LOG_DEBUG, "APT_or_list termp=%s", termp);
1708 grep_info.isam_p_indx = 0;
1709 r = string_term (zh, zapt, &termp, attributeSet, stream, &grep_info,
1710 reg_type, complete_flag, num_bases, basenames,
1714 logf (LOG_DEBUG, "term: %s", term_dst);
1715 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1716 grep_info.isam_p_indx, term_dst,
1717 strlen(term_dst), rank_type);
1718 assert (rset[rset_no]);
1719 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1723 xfree(grep_info.term_no);
1725 xfree (grep_info.isam_p_buf);
1728 rset_null_parms parms;
1730 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1731 return rset_create (rset_kind_null, &parms);
1734 for (i = 1; i<rset_no; i++)
1736 rset_bool_parms bool_parms;
1738 bool_parms.rset_l = result;
1739 bool_parms.rset_r = rset[i];
1740 bool_parms.key_size = sizeof(struct it_key);
1741 bool_parms.cmp = key_compare_it;
1742 result = rset_create (rset_kind_or, &bool_parms);
1747 static RSET rpn_search_APT_and_list (ZebraHandle zh,
1748 Z_AttributesPlusTerm *zapt,
1749 const char *termz_org,
1750 oid_value attributeSet,
1752 int reg_type, int complete_flag,
1753 const char *rank_type,
1754 int num_bases, char **basenames)
1756 char term_dst[IT_MAX_WORD+1];
1757 RSET rset[60], result;
1758 int i, r, rset_no = 0;
1759 struct grep_info grep_info;
1760 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1761 const char *termp = termz;
1764 grep_info.term_no = 0;
1766 grep_info.isam_p_size = 0;
1767 grep_info.isam_p_buf = NULL;
1769 grep_info.reg_type = reg_type;
1773 logf (LOG_DEBUG, "APT_and_list termp=%s", termp);
1774 grep_info.isam_p_indx = 0;
1775 r = string_term (zh, zapt, &termp, attributeSet, stream, &grep_info,
1776 reg_type, complete_flag, num_bases, basenames,
1780 logf (LOG_DEBUG, "term: %s", term_dst);
1781 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1782 grep_info.isam_p_indx, term_dst,
1783 strlen(term_dst), rank_type);
1784 assert (rset[rset_no]);
1785 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1789 xfree(grep_info.term_no);
1791 xfree (grep_info.isam_p_buf);
1794 rset_null_parms parms;
1796 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1797 return rset_create (rset_kind_null, &parms);
1800 for (i = 1; i<rset_no; i++)
1802 rset_bool_parms bool_parms;
1804 bool_parms.rset_l = result;
1805 bool_parms.rset_r = rset[i];
1806 bool_parms.key_size = sizeof(struct it_key);
1807 bool_parms.cmp = key_compare_it;
1808 result = rset_create (rset_kind_and, &bool_parms);
1813 static int numeric_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1814 const char **term_sub,
1816 oid_value attributeSet,
1817 struct grep_info *grep_info,
1826 char *term_tmp = term_dict + strlen(term_dict);
1828 attr_init (&relation, zapt, 2);
1829 relation_value = attr_find (&relation, NULL);
1831 logf (LOG_DEBUG, "numeric relation value=%d", relation_value);
1833 if (!term_100 (zh->service->zebra_maps, reg_type, term_sub, term_tmp, 1,
1836 term_value = atoi (term_tmp);
1837 switch (relation_value)
1840 logf (LOG_DEBUG, "Relation <");
1841 gen_regular_rel (term_tmp, term_value-1, 1);
1844 logf (LOG_DEBUG, "Relation <=");
1845 gen_regular_rel (term_tmp, term_value, 1);
1848 logf (LOG_DEBUG, "Relation >=");
1849 gen_regular_rel (term_tmp, term_value, 0);
1852 logf (LOG_DEBUG, "Relation >");
1853 gen_regular_rel (term_tmp, term_value+1, 0);
1857 logf (LOG_DEBUG, "Relation =");
1858 sprintf (term_tmp, "(0*%d)", term_value);
1860 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_tmp);
1861 r = dict_lookup_grep (zh->service->dict, term_dict, 0, grep_info, max_pos,
1864 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1865 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1869 static int numeric_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1870 const char **term_sub,
1871 oid_value attributeSet, struct grep_info *grep_info,
1872 int reg_type, int complete_flag,
1873 int num_bases, char **basenames,
1876 char term_dict[2*IT_MAX_WORD+2];
1880 oid_value curAttributeSet = attributeSet;
1882 struct rpn_char_map_info rcmi;
1884 rpn_char_map_prepare (zh, reg_type, &rcmi);
1885 attr_init (&use, zapt, 1);
1886 use_value = attr_find (&use, &curAttributeSet);
1887 logf (LOG_DEBUG, "numeric_term, use value %d", use_value);
1889 if (use_value == -1)
1892 for (base_no = 0; base_no < num_bases; base_no++)
1895 data1_local_attribute *local_attr;
1896 int max_pos, prefix_len = 0;
1899 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
1901 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1902 curAttributeSet, use_value, r);
1909 if (zebraExplain_curDatabase (zh->service->zei, basenames[base_no]))
1911 zh->errCode = 109; /* Database unavailable */
1912 zh->errString = basenames[base_no];
1915 for (local_attr = attp.local_attributes; local_attr;
1916 local_attr = local_attr->next)
1922 ord = zebraExplain_lookupSU (zh->service->zei, attp.attset_ordinal,
1927 term_dict[prefix_len++] = '|';
1929 term_dict[prefix_len++] = '(';
1931 ord_len = key_SU_code (ord, ord_buf);
1932 for (i = 0; i<ord_len; i++)
1934 term_dict[prefix_len++] = 1;
1935 term_dict[prefix_len++] = ord_buf[i];
1943 term_dict[prefix_len++] = ')';
1944 term_dict[prefix_len++] = 1;
1945 term_dict[prefix_len++] = reg_type;
1946 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1947 term_dict[prefix_len] = '\0';
1948 if (!numeric_relation (zh, zapt, &termp, term_dict,
1949 attributeSet, grep_info, &max_pos, reg_type,
1954 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1958 static RSET rpn_search_APT_numeric (ZebraHandle zh,
1959 Z_AttributesPlusTerm *zapt,
1961 oid_value attributeSet,
1963 int reg_type, int complete_flag,
1964 const char *rank_type,
1965 int num_bases, char **basenames)
1967 char term_dst[IT_MAX_WORD+1];
1968 const char *termp = termz;
1969 RSET rset[60], result;
1970 int i, r, rset_no = 0;
1971 struct grep_info grep_info;
1974 grep_info.term_no = 0;
1976 grep_info.isam_p_size = 0;
1977 grep_info.isam_p_buf = NULL;
1979 grep_info.reg_type = reg_type;
1983 logf (LOG_DEBUG, "APT_numeric termp=%s", termp);
1984 grep_info.isam_p_indx = 0;
1985 r = numeric_term (zh, zapt, &termp, attributeSet, &grep_info,
1986 reg_type, complete_flag, num_bases, basenames,
1990 logf (LOG_DEBUG, "term: %s", term_dst);
1991 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1992 grep_info.isam_p_indx, term_dst,
1993 strlen(term_dst), rank_type);
1994 assert (rset[rset_no]);
1995 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1999 xfree(grep_info.term_no);
2001 xfree (grep_info.isam_p_buf);
2004 rset_null_parms parms;
2006 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
2007 return rset_create (rset_kind_null, &parms);
2010 for (i = 1; i<rset_no; i++)
2012 rset_bool_parms bool_parms;
2014 bool_parms.rset_l = result;
2015 bool_parms.rset_r = rset[i];
2016 bool_parms.key_size = sizeof(struct it_key);
2017 bool_parms.cmp = key_compare_it;
2018 result = rset_create (rset_kind_and, &bool_parms);
2023 static RSET rpn_search_APT_local (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2025 oid_value attributeSet,
2027 const char *rank_type)
2032 rset_temp_parms parms;
2034 parms.rset_term = rset_term_create (termz, -1, rank_type);
2035 parms.key_size = sizeof (struct it_key);
2036 parms.temp_path = res_get (zh->service->res, "setTmpDir");
2037 result = rset_create (rset_kind_temp, &parms);
2038 rsfd = rset_open (result, RSETF_WRITE);
2040 key.sysno = atoi (termz);
2044 rset_write (result, rsfd, &key);
2045 rset_close (result, rsfd);
2049 static RSET rpn_sort_spec (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2050 oid_value attributeSet, NMEM stream,
2051 Z_SortKeySpecList *sort_sequence,
2052 const char *rank_type)
2054 rset_null_parms parms;
2056 int sort_relation_value;
2057 AttrType sort_relation_type;
2062 Z_AttributeElement *ae;
2066 attr_init (&sort_relation_type, zapt, 7);
2067 sort_relation_value = attr_find (&sort_relation_type, &attributeSet);
2069 attr_init (&use_type, zapt, 1);
2070 use_value = attr_find (&use_type, &attributeSet);
2072 if (!sort_sequence->specs)
2074 sort_sequence->num_specs = 10;
2075 sort_sequence->specs = (Z_SortKeySpec **)
2076 nmem_malloc (stream, sort_sequence->num_specs *
2077 sizeof(*sort_sequence->specs));
2078 for (i = 0; i<sort_sequence->num_specs; i++)
2079 sort_sequence->specs[i] = 0;
2081 if (zapt->term->which != Z_Term_general)
2084 i = atoi_n ((char *) zapt->term->u.general->buf,
2085 zapt->term->u.general->len);
2086 if (i >= sort_sequence->num_specs)
2089 oe.proto = PROTO_Z3950;
2090 oe.oclass = CLASS_ATTSET;
2091 oe.value = attributeSet;
2092 if (!oid_ent_to_oid (&oe, oid))
2095 sks = (Z_SortKeySpec *) nmem_malloc (stream, sizeof(*sks));
2096 sks->sortElement = (Z_SortElement *)
2097 nmem_malloc (stream, sizeof(*sks->sortElement));
2098 sks->sortElement->which = Z_SortElement_generic;
2099 sk = sks->sortElement->u.generic = (Z_SortKey *)
2100 nmem_malloc (stream, sizeof(*sk));
2101 sk->which = Z_SortKey_sortAttributes;
2102 sk->u.sortAttributes = (Z_SortAttributes *)
2103 nmem_malloc (stream, sizeof(*sk->u.sortAttributes));
2105 sk->u.sortAttributes->id = oid;
2106 sk->u.sortAttributes->list = (Z_AttributeList *)
2107 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list));
2108 sk->u.sortAttributes->list->num_attributes = 1;
2109 sk->u.sortAttributes->list->attributes = (Z_AttributeElement **)
2110 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list->attributes));
2111 ae = *sk->u.sortAttributes->list->attributes = (Z_AttributeElement *)
2112 nmem_malloc (stream, sizeof(**sk->u.sortAttributes->list->attributes));
2113 ae->attributeSet = 0;
2114 ae->attributeType = (int *)
2115 nmem_malloc (stream, sizeof(*ae->attributeType));
2116 *ae->attributeType = 1;
2117 ae->which = Z_AttributeValue_numeric;
2118 ae->value.numeric = (int *)
2119 nmem_malloc (stream, sizeof(*ae->value.numeric));
2120 *ae->value.numeric = use_value;
2122 sks->sortRelation = (int *)
2123 nmem_malloc (stream, sizeof(*sks->sortRelation));
2124 if (sort_relation_value == 1)
2125 *sks->sortRelation = Z_SortRelation_ascending;
2126 else if (sort_relation_value == 2)
2127 *sks->sortRelation = Z_SortRelation_descending;
2129 *sks->sortRelation = Z_SortRelation_ascending;
2131 sks->caseSensitivity = (int *)
2132 nmem_malloc (stream, sizeof(*sks->caseSensitivity));
2133 *sks->caseSensitivity = 0;
2136 sks->which = Z_SortKeySpec_null;
2137 sks->u.null = odr_nullval ();
2139 sks->missingValueAction = 0;
2142 sort_sequence->specs[i] = sks;
2144 parms.rset_term = rset_term_create ("", -1, rank_type);
2145 return rset_create (rset_kind_null, &parms);
2149 static RSET rpn_search_APT (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2150 oid_value attributeSet, NMEM stream,
2151 Z_SortKeySpecList *sort_sequence,
2152 int num_bases, char **basenames)
2155 char *search_type = NULL;
2156 char *rank_type = NULL;
2159 char termz[IT_MAX_WORD+1];
2161 zebra_maps_attr (zh->service->zebra_maps, zapt, ®_id, &search_type,
2162 &rank_type, &complete_flag, &sort_flag);
2164 logf (LOG_DEBUG, "reg_id=%c", reg_id);
2165 logf (LOG_DEBUG, "complete_flag=%d", complete_flag);
2166 logf (LOG_DEBUG, "search_type=%s", search_type);
2167 logf (LOG_DEBUG, "rank_type=%s", rank_type);
2169 if (zapt->term->which != Z_Term_general)
2174 trans_term (zh, zapt, termz);
2177 return rpn_sort_spec (zh, zapt, attributeSet, stream, sort_sequence,
2180 if (!strcmp (search_type, "phrase"))
2182 return rpn_search_APT_phrase (zh, zapt, termz, attributeSet, stream,
2183 reg_id, complete_flag, rank_type,
2184 num_bases, basenames);
2186 else if (!strcmp (search_type, "and-list"))
2188 return rpn_search_APT_and_list (zh, zapt, termz, attributeSet, stream,
2189 reg_id, complete_flag, rank_type,
2190 num_bases, basenames);
2192 else if (!strcmp (search_type, "or-list"))
2194 return rpn_search_APT_or_list (zh, zapt, termz, attributeSet, stream,
2195 reg_id, complete_flag, rank_type,
2196 num_bases, basenames);
2198 else if (!strcmp (search_type, "local"))
2200 return rpn_search_APT_local (zh, zapt, termz, attributeSet, stream,
2203 else if (!strcmp (search_type, "numeric"))
2205 return rpn_search_APT_numeric (zh, zapt, termz, attributeSet, stream,
2206 reg_id, complete_flag, rank_type,
2207 num_bases, basenames);
2213 static RSET rpn_search_structure (ZebraHandle zh, Z_RPNStructure *zs,
2214 oid_value attributeSet, NMEM stream,
2215 Z_SortKeySpecList *sort_sequence,
2216 int num_bases, char **basenames)
2219 if (zs->which == Z_RPNStructure_complex)
2221 Z_Operator *zop = zs->u.complex->roperator;
2222 rset_bool_parms bool_parms;
2224 bool_parms.rset_l = rpn_search_structure (zh, zs->u.complex->s1,
2225 attributeSet, stream,
2227 num_bases, basenames);
2228 if (bool_parms.rset_l == NULL)
2230 bool_parms.rset_r = rpn_search_structure (zh, zs->u.complex->s2,
2231 attributeSet, stream,
2233 num_bases, basenames);
2234 if (bool_parms.rset_r == NULL)
2236 rset_delete (bool_parms.rset_l);
2239 bool_parms.key_size = sizeof(struct it_key);
2240 bool_parms.cmp = key_compare_it;
2244 case Z_Operator_and:
2245 r = rset_create (rset_kind_and, &bool_parms);
2248 r = rset_create (rset_kind_or, &bool_parms);
2250 case Z_Operator_and_not:
2251 r = rset_create (rset_kind_not, &bool_parms);
2253 case Z_Operator_prox:
2255 if (zop->u.prox->which != Z_ProximityOperator_known)
2261 if (zop->u.prox->which != Z_ProxCode_known)
2269 if (*zop->u.prox->u.known != Z_ProxUnit_word)
2271 char *val = (char *) nmem_malloc (stream, 16);
2273 zh->errString = val;
2274 sprintf (val, "%d", *zop->u.prox->u.known);
2278 if (*zop->u.prox->proximityUnitCode != Z_ProxUnit_word)
2280 char *val = (char *) nmem_malloc (stream, 16);
2282 zh->errString = val;
2283 sprintf (val, "%d", *zop->u.prox->proximityUnitCode);
2291 rsets[0] = bool_parms.rset_l;
2292 rsets[1] = bool_parms.rset_r;
2294 r = rpn_prox (zh, rsets, 2,
2295 *zop->u.prox->ordered,
2296 (!zop->u.prox->exclusion ? 0 :
2297 *zop->u.prox->exclusion),
2298 *zop->u.prox->relationType,
2299 *zop->u.prox->distance);
2300 rset_delete (rsets[0]);
2301 rset_delete (rsets[1]);
2309 else if (zs->which == Z_RPNStructure_simple)
2311 if (zs->u.simple->which == Z_Operand_APT)
2313 logf (LOG_DEBUG, "rpn_search_APT");
2314 r = rpn_search_APT (zh, zs->u.simple->u.attributesPlusTerm,
2315 attributeSet, stream, sort_sequence,
2316 num_bases, basenames);
2318 else if (zs->u.simple->which == Z_Operand_resultSetId)
2320 logf (LOG_DEBUG, "rpn_search_ref");
2321 r = resultSetRef (zh, zs->u.simple->u.resultSetId);
2323 r = rset_create (rset_kind_null, NULL);
2340 RSET rpn_search (ZebraHandle zh, NMEM nmem,
2341 Z_RPNQuery *rpn, int num_bases, char **basenames,
2342 const char *setname,
2347 oid_value attributeSet;
2348 Z_SortKeySpecList *sort_sequence;
2352 zh->errString = NULL;
2355 sort_sequence = (Z_SortKeySpecList *)
2356 nmem_malloc (nmem, sizeof(*sort_sequence));
2357 sort_sequence->num_specs = 10;
2358 sort_sequence->specs = (Z_SortKeySpec **)
2359 nmem_malloc (nmem, sort_sequence->num_specs *
2360 sizeof(*sort_sequence->specs));
2361 for (i = 0; i<sort_sequence->num_specs; i++)
2362 sort_sequence->specs[i] = 0;
2364 attrset = oid_getentbyoid (rpn->attributeSetId);
2365 attributeSet = attrset->value;
2366 rset = rpn_search_structure (zh, rpn->RPNStructure, attributeSet,
2367 nmem, sort_sequence, num_bases, basenames);
2372 logf (LOG_DEBUG, "search error: %d", zh->errCode);
2374 for (i = 0; sort_sequence->specs[i]; i++)
2376 sort_sequence->num_specs = i;
2378 resultSetRank (zh, sset, rset);
2381 logf (LOG_DEBUG, "resultSetSortSingle in rpn_search");
2382 resultSetSortSingle (zh, nmem, sset, rset,
2383 sort_sequence, &sort_status);
2386 logf (LOG_DEBUG, "resultSetSortSingle status = %d", zh->errCode);
2392 struct scan_info_entry {
2398 struct scan_info_entry *list;
2404 static int scan_handle (char *name, const char *info, int pos, void *client)
2406 int len_prefix, idx;
2407 struct scan_info *scan_info = (struct scan_info *) client;
2409 len_prefix = strlen(scan_info->prefix);
2410 if (memcmp (name, scan_info->prefix, len_prefix))
2412 if (pos > 0) idx = scan_info->after - pos + scan_info->before;
2415 scan_info->list[idx].term = (char *)
2416 odr_malloc (scan_info->odr, strlen(name + len_prefix)+1);
2417 strcpy (scan_info->list[idx].term, name + len_prefix);
2418 assert (*info == sizeof(ISAMS_P));
2419 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAMS_P));
2423 static void scan_term_untrans (ZebraHandle zh, NMEM stream, int reg_type,
2424 char **dst, const char *src)
2426 char term_dst[1024];
2428 term_untrans (zh, reg_type, term_dst, src);
2430 *dst = (char *) nmem_malloc (stream, strlen(term_dst)+1);
2431 strcpy (*dst, term_dst);
2434 static void count_set (RSET r, int *count)
2442 logf (LOG_DEBUG, "count_set");
2445 rfd = rset_open (r, RSETF_READ);
2446 while (rset_read (r, rfd, &key, &term_index))
2448 if (key.sysno != psysno)
2455 rset_close (r, rfd);
2456 logf (LOG_DEBUG, "%d keys, %d records", kno, *count);
2459 void rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
2460 oid_value attributeset,
2461 int num_bases, char **basenames,
2462 int *position, int *num_entries, ZebraScanEntry **list,
2466 int pos = *position;
2467 int num = *num_entries;
2471 char termz[IT_MAX_WORD+20];
2474 struct scan_info *scan_info_array;
2475 ZebraScanEntry *glist;
2476 int ords[32], ord_no = 0;
2480 char *search_type = NULL;
2481 char *rank_type = NULL;
2486 if (attributeset == VAL_NONE)
2487 attributeset = VAL_BIB1;
2489 logf (LOG_DEBUG, "position = %d, num = %d", pos, num);
2491 attr_init (&use, zapt, 1);
2492 use_value = attr_find (&use, &attributeset);
2494 if (zebra_maps_attr (zh->service->zebra_maps, zapt, ®_id, &search_type,
2495 &rank_type, &complete_flag, &sort_flag))
2501 if (use_value == -1)
2503 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
2507 data1_local_attribute *local_attr;
2509 if ((r=att_getentbyatt (zh, &attp, attributeset, use_value)))
2511 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
2512 attributeset, use_value);
2520 if (zebraExplain_curDatabase (zh->service->zei, basenames[base_no]))
2522 zh->errString = basenames[base_no];
2523 zh->errCode = 109; /* Database unavailable */
2526 for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
2527 local_attr = local_attr->next)
2531 ord = zebraExplain_lookupSU (zh->service->zei, attp.attset_ordinal,
2534 ords[ord_no++] = ord;
2542 /* prepare dictionary scanning */
2545 scan_info_array = (struct scan_info *)
2546 odr_malloc (stream, ord_no * sizeof(*scan_info_array));
2547 for (i = 0; i < ord_no; i++)
2549 int j, prefix_len = 0;
2550 int before_tmp = before, after_tmp = after;
2551 struct scan_info *scan_info = scan_info_array + i;
2552 struct rpn_char_map_info rcmi;
2554 rpn_char_map_prepare (zh, reg_id, &rcmi);
2556 scan_info->before = before;
2557 scan_info->after = after;
2558 scan_info->odr = stream;
2560 scan_info->list = (struct scan_info_entry *)
2561 odr_malloc (stream, (before+after) * sizeof(*scan_info->list));
2562 for (j = 0; j<before+after; j++)
2563 scan_info->list[j].term = NULL;
2565 prefix_len += key_SU_code (ords[i], termz + prefix_len);
2566 termz[prefix_len++] = reg_id;
2567 termz[prefix_len] = 0;
2568 strcpy (scan_info->prefix, termz);
2570 trans_scan_term (zh, zapt, termz+prefix_len, reg_id);
2572 dict_scan (zh->service->dict, termz, &before_tmp, &after_tmp,
2573 scan_info, scan_handle);
2575 glist = (ZebraScanEntry *)
2576 odr_malloc (stream, (before+after)*sizeof(*glist));
2578 /* consider terms after main term */
2579 for (i = 0; i < ord_no; i++)
2583 for (i = 0; i<after; i++)
2586 const char *mterm = NULL;
2590 for (j = 0; j < ord_no; j++)
2592 if (ptr[j] < before+after &&
2593 (tst=scan_info_array[j].list[ptr[j]].term) &&
2594 (!mterm || strcmp (tst, mterm) < 0))
2602 scan_term_untrans (zh, stream->mem, reg_id,
2603 &glist[i+before].term, mterm);
2604 rset = rset_trunc (zh, &scan_info_array[j0].list[ptr[j0]].isam_p, 1,
2605 glist[i+before].term, strlen(glist[i+before].term),
2609 for (j = j0+1; j<ord_no; j++)
2611 if (ptr[j] < before+after &&
2612 (tst=scan_info_array[j].list[ptr[j]].term) &&
2613 !strcmp (tst, mterm))
2615 rset_bool_parms bool_parms;
2619 rset_trunc (zh, &scan_info_array[j].list[ptr[j]].isam_p, 1,
2620 glist[i+before].term,
2621 strlen(glist[i+before].term), NULL);
2623 bool_parms.key_size = sizeof(struct it_key);
2624 bool_parms.cmp = key_compare_it;
2625 bool_parms.rset_l = rset;
2626 bool_parms.rset_r = rset2;
2628 rset = rset_create (rset_kind_or, &bool_parms);
2633 count_set (rset, &glist[i+before].occurrences);
2638 *num_entries -= (after-i);
2642 /* consider terms before main term */
2643 for (i = 0; i<ord_no; i++)
2646 for (i = 0; i<before; i++)
2649 const char *mterm = NULL;
2653 for (j = 0; j <ord_no; j++)
2655 if (ptr[j] < before &&
2656 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2657 (!mterm || strcmp (tst, mterm) > 0))
2666 scan_term_untrans (zh, stream->mem, reg_id,
2667 &glist[before-1-i].term, mterm);
2670 (zh, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1,
2671 glist[before-1-i].term, strlen(glist[before-1-i].term),
2676 for (j = j0+1; j<ord_no; j++)
2678 if (ptr[j] < before &&
2679 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2680 !strcmp (tst, mterm))
2682 rset_bool_parms bool_parms;
2685 rset2 = rset_trunc (zh,
2686 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1,
2687 glist[before-1-i].term,
2688 strlen(glist[before-1-i].term), NULL);
2690 bool_parms.key_size = sizeof(struct it_key);
2691 bool_parms.cmp = key_compare_it;
2692 bool_parms.rset_l = rset;
2693 bool_parms.rset_r = rset2;
2695 rset = rset_create (rset_kind_or, &bool_parms);
2700 count_set (rset, &glist[before-1-i].occurrences);
2710 *list = glist + i; /* list is set to first 'real' entry */
2712 logf (LOG_DEBUG, "position = %d, num_entries = %d",
2713 *position, *num_entries);
2715 logf (LOG_DEBUG, "scan error: %d", zh->errCode);