2 * Copyright (C) 1995-2000, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.105 2000-11-08 13:46:59 adam
8 * Fixed scan: server could break if bad attribute/database was selected.
9 * Work on remote update.
11 * Revision 1.104 2000/04/05 09:49:35 adam
12 * On Unix, zebra/z'mbol uses automake.
14 * Revision 1.103 2000/03/20 19:08:36 adam
15 * Added remote record import using Z39.50 extended services and Segment
18 * Revision 1.102 2000/03/15 15:00:31 adam
19 * First work on threaded version.
21 * Revision 1.101 2000/03/02 14:35:03 adam
22 * Fixed proximity handling.
24 * Revision 1.100 1999/12/28 15:48:12 adam
27 * Revision 1.99 1999/12/23 09:03:32 adam
28 * Changed behaviour of trunc=105 so that * is regular .* and ! is regular .
30 * Revision 1.98 1999/11/30 13:48:04 adam
31 * Improved installation. Updated for inclusion of YAZ header files.
33 * Revision 1.97 1999/10/14 14:33:50 adam
34 * Added truncation 5=106.
36 * Revision 1.96 1999/09/23 10:05:05 adam
37 * Implemented structure=105 searching.
39 * Revision 1.95 1999/09/07 07:19:21 adam
40 * Work on character mapping. Implemented replace rules.
42 * Revision 1.94 1999/07/20 13:59:18 adam
43 * Fixed bug that occurred when phrases had 0 hits.
45 * Revision 1.93 1999/06/17 14:38:40 adam
46 * Bug fix: Scan SEGV'ed when getting unknown use attribute.
48 * Revision 1.92 1999/05/26 07:49:13 adam
51 * Revision 1.91 1999/02/02 14:51:13 adam
52 * Updated WIN32 code specific sections. Changed header.
54 * Revision 1.90 1998/11/16 16:03:43 adam
55 * Moved loggin utilities to Yaz. Was implemented in file zlogs.c.
57 * Revision 1.89 1998/11/16 10:11:55 adam
58 * Added addtional info for error 114 - unsupported use attribute.
60 * Revision 1.88 1998/10/18 07:54:52 adam
61 * Additional info added for diagnostics 114 (Unsupported use attribute) and
62 * 121 (Unsupported attribute set).
64 * Revision 1.87 1998/09/28 11:19:12 adam
65 * Fix for Compiled ASN.1.
67 * Revision 1.86 1998/09/22 10:48:20 adam
68 * Minor changes in search API.
70 * Revision 1.85 1998/09/22 10:03:43 adam
71 * Changed result sets to be persistent in the sense that they can
72 * be re-searched if needed.
73 * Fixed memory leak in rsm_or.
75 * Revision 1.84 1998/09/18 12:41:00 adam
76 * Fixed bug with numerical relations.
78 * Revision 1.83 1998/09/02 13:53:19 adam
79 * Extra parameter decode added to search routines to implement
82 * Revision 1.82 1998/06/26 11:16:40 quinn
83 * Added support (un-optimised) for left and left/right truncation
85 * Revision 1.81 1998/06/24 12:16:14 adam
86 * Support for relations on text operands. Open range support in
87 * DFA module (i.e. [-j], [g-]).
89 * Revision 1.80 1998/06/23 15:33:34 adam
90 * Added feature to specify sort criteria in query (type 7 specifies
93 * Revision 1.79 1998/06/22 11:35:09 adam
96 * Revision 1.78 1998/06/08 14:43:17 adam
97 * Added suport for EXPLAIN Proxy servers - added settings databasePath
98 * and explainDatabase to facilitate this. Increased maximum number
99 * of databases and attributes in one register.
101 * Revision 1.77 1998/05/20 10:12:22 adam
102 * Implemented automatic EXPLAIN database maintenance.
103 * Modified Zebra to work with ASN.1 compiled version of YAZ.
105 * Revision 1.76 1998/04/02 14:35:29 adam
106 * First version of Zebra that works with compiled ASN.1.
108 * Revision 1.75 1998/03/05 08:45:13 adam
109 * New result set model and modular ranking system. Moved towards
110 * descent server API. System information stored as "SGML" records.
112 * Revision 1.74 1998/02/10 12:03:06 adam
115 * Revision 1.73 1998/01/29 13:40:11 adam
116 * Better logging for scan service.
118 * Revision 1.72 1998/01/07 13:53:41 adam
119 * Queries using simple ranked operands returns right number of hits.
121 * Revision 1.71 1997/12/18 10:54:24 adam
122 * New method result set method rs_hits that returns the number of
123 * hits in result-set (if known). The ranked result set returns real
124 * number of hits but only when not combined with other operands.
126 * Revision 1.70 1997/10/31 12:34:43 adam
127 * Changed a few log statements.
129 * Revision 1.69 1997/10/29 12:05:02 adam
130 * Server produces diagnostic "Unsupported Attribute Set" when appropriate.
132 * Revision 1.68 1997/10/27 14:33:06 adam
133 * Moved towards generic character mapping depending on "structure"
134 * field in abstract syntax file. Fixed a few memory leaks. Fixed
135 * bug with negative integers when doing searches with relational
138 * Revision 1.67 1997/09/29 09:06:10 adam
139 * Removed one static var in order to make this module thread safe.
141 * Revision 1.66 1997/09/25 14:58:03 adam
144 * Revision 1.65 1997/09/22 12:39:06 adam
145 * Added get_pos method for the ranked result sets.
147 * Revision 1.64 1997/09/18 08:59:20 adam
148 * Extra generic handle for the character mapping routines.
150 * Revision 1.63 1997/09/17 12:19:18 adam
151 * Zebra version corresponds to YAZ version 1.4.
152 * Changed Zebra server so that it doesn't depend on global common_resource.
154 * Revision 1.62 1997/09/05 15:30:09 adam
155 * Changed prototype for chr_map_input - added const.
156 * Added support for C++, headers uses extern "C" for public definitions.
158 * Revision 1.61 1997/02/10 10:21:14 adam
159 * Bug fix: in search terms character (^) wasn't observed.
161 * Revision 1.60 1997/01/31 11:10:34 adam
162 * Bug fix: Leading and trailing white space weren't removed in scan tokens.
164 * Revision 1.59 1997/01/17 11:31:46 adam
165 * Bug fix: complete phrase search didn't work.
167 * Revision 1.58 1996/12/23 15:30:45 adam
168 * Work on truncation.
169 * Bug fix: result sets weren't deleted after server shut down.
171 * Revision 1.57 1996/11/11 13:38:02 adam
172 * Added proximity support in search.
174 * Revision 1.56 1996/11/08 11:10:32 adam
175 * Buffers used during file match got bigger.
176 * Compressed ISAM support everywhere.
177 * Bug fixes regarding masking characters in queries.
178 * Redesigned Regexp-2 queries.
180 * Revision 1.55 1996/11/04 14:07:44 adam
181 * Moved truncation code to trunc.c.
183 * Revision 1.54 1996/10/29 14:09:52 adam
184 * Use of cisam system - enabled if setting isamc is 1.
186 * Revision 1.53 1996/06/26 09:21:43 adam
187 * Bug fix: local attribute set wasn't obeyed in scan.
189 * Revision 1.52 1996/06/17 14:26:20 adam
190 * Function gen_regular_rel changed to handle negative numbers.
192 * Revision 1.51 1996/06/11 10:54:15 quinn
195 * Revision 1.50 1996/06/07 08:51:53 adam
196 * Bug fix: Character mapping was broken (introducued by last revision).
198 * Revision 1.49 1996/06/04 10:18:11 adam
199 * Search/scan uses character mapping module.
201 * Revision 1.48 1996/05/28 15:15:01 adam
202 * Bug fix: Didn't handle unknown database correctly.
204 * Revision 1.47 1996/05/15 18:36:28 adam
205 * Function trans_term transforms unsearchable characters to blanks.
207 * Revision 1.46 1996/05/15 11:57:56 adam
208 * Fixed bug introduced by set/field mapping in search operations.
210 * Revision 1.45 1996/05/14 11:34:00 adam
211 * Scan support in multiple registers/databases.
213 * Revision 1.44 1996/05/14 06:16:44 adam
214 * Compact use/set bytes used in search service.
216 * Revision 1.43 1996/05/09 09:54:43 adam
217 * Server supports maps from one logical attributes to a list of physical
219 * The extraction process doesn't make space consuming 'any' keys.
221 * Revision 1.42 1996/05/09 07:28:56 quinn
222 * Work towards phrases and multiple registers
224 * Revision 1.41 1996/03/20 09:36:43 adam
225 * Function dict_lookup_grep got extra parameter, init_pos, which marks
226 * from which position in pattern approximate pattern matching should occur.
227 * Approximate pattern matching is used in relevance=re-2.
229 * Revision 1.40 1996/02/02 13:44:44 adam
230 * The public dictionary functions simply use char instead of Dict_char
231 * to represent search strings. Dict_char is used internally only.
233 * Revision 1.39 1996/01/03 16:22:13 quinn
234 * operator->roperator
236 * Revision 1.38 1995/12/11 09:12:55 adam
237 * The rec_get function returns NULL if record doesn't exist - will
238 * happen in the server if the result set records have been deleted since
239 * the creation of the set (i.e. the search).
240 * The server saves a result temporarily if it is 'volatile', i.e. the
241 * set is register dependent.
243 * Revision 1.37 1995/12/06 15:05:28 adam
244 * More verbose in count_set.
246 * Revision 1.36 1995/12/06 12:41:27 adam
247 * New command 'stat' for the index program.
248 * Filenames can be read from stdin by specifying '-'.
249 * Bug fix/enhancement of the transformation from terms to regular
250 * expressons in the search engine.
252 * Revision 1.35 1995/11/27 09:29:00 adam
253 * Bug fixes regarding conversion to regular expressions.
255 * Revision 1.34 1995/11/16 17:00:56 adam
256 * Better logging of rpn query.
258 * Revision 1.33 1995/11/01 13:58:28 quinn
259 * Moving data1 to yaz/retrieval
261 * Revision 1.32 1995/10/27 14:00:11 adam
262 * Implemented detection of database availability.
264 * Revision 1.31 1995/10/17 18:02:10 adam
265 * New feature: databases. Implemented as prefix to words in dictionary.
267 * Revision 1.30 1995/10/16 09:32:38 adam
268 * More work on relational op.
270 * Revision 1.29 1995/10/13 16:01:49 adam
273 * Revision 1.28 1995/10/13 12:26:43 adam
274 * Optimization of truncation.
276 * Revision 1.27 1995/10/12 17:07:22 adam
279 * Revision 1.26 1995/10/12 12:40:54 adam
280 * Bug fixes in rpn_prox.
282 * Revision 1.25 1995/10/10 13:59:24 adam
283 * Function rset_open changed its wflag parameter to general flags.
285 * Revision 1.24 1995/10/09 16:18:37 adam
286 * Function dict_lookup_grep got extra client data parameter.
288 * Revision 1.23 1995/10/06 16:33:37 adam
289 * Use attribute mappings.
291 * Revision 1.22 1995/10/06 15:07:39 adam
292 * Structure 'local-number' handled.
294 * Revision 1.21 1995/10/06 13:52:06 adam
295 * Bug fixes. Handler may abort further scanning.
297 * Revision 1.20 1995/10/06 11:06:33 adam
298 * Scan entries include 'occurrences' now.
300 * Revision 1.19 1995/10/06 10:43:56 adam
301 * Scan added. 'occurrences' in scan entries not set yet.
303 * Revision 1.18 1995/10/04 16:57:20 adam
304 * Key input and merge sort in one pass.
306 * Revision 1.17 1995/10/04 12:55:17 adam
307 * Bug fix in ranked search. Use=Any keys inserted.
309 * Revision 1.16 1995/10/02 16:24:40 adam
310 * Use attribute actually used in search requests.
312 * Revision 1.15 1995/10/02 15:18:52 adam
313 * New member in recRetrieveCtrl: diagnostic.
315 * Revision 1.14 1995/09/28 12:10:32 adam
316 * Bug fixes. Field prefix used in queries.
318 * Revision 1.13 1995/09/18 14:17:50 adam
321 * Revision 1.12 1995/09/15 14:45:21 adam
323 * Work on truncation.
325 * Revision 1.11 1995/09/14 11:53:27 adam
326 * First work on regular expressions/truncations.
328 * Revision 1.10 1995/09/11 15:23:26 adam
329 * More work on relevance search.
331 * Revision 1.9 1995/09/11 13:09:35 adam
332 * More work on relevance feedback.
334 * Revision 1.8 1995/09/08 14:52:27 adam
335 * Minor changes. Dictionary is lower case now.
337 * Revision 1.7 1995/09/07 13:58:36 adam
338 * New parameter: result-set file descriptor (RSFD) to support multiple
339 * positions within the same result-set.
340 * Boolean operators: and, or, not implemented.
341 * Result-set references.
343 * Revision 1.6 1995/09/06 16:11:18 adam
344 * Option: only one word key per file.
346 * Revision 1.5 1995/09/06 10:33:04 adam
347 * More work on present. Some log messages removed.
349 * Revision 1.4 1995/09/05 15:28:40 adam
350 * More work on search engine.
352 * Revision 1.3 1995/09/04 15:20:22 adam
355 * Revision 1.2 1995/09/04 12:33:43 adam
356 * Various cleanup. YAZ util used instead.
358 * Revision 1.1 1995/09/04 09:10:40 adam
359 * More work on index add/del/update.
360 * Merge sort implemented.
361 * Initial work on z39 server.
380 struct rpn_char_map_info {
385 static const char **rpn_char_map_handler (void *vp, const char **from, int len)
387 struct rpn_char_map_info *p = (struct rpn_char_map_info *) vp;
388 return zebra_maps_input (p->zm, p->reg_type, from, len);
391 static void rpn_char_map_prepare (ZebraHandle zh, int reg_type,
392 struct rpn_char_map_info *map_info)
394 map_info->zm = zh->service->zebra_maps;
395 map_info->reg_type = reg_type;
396 dict_grep_cmap (zh->service->dict, map_info, rpn_char_map_handler);
403 Z_AttributesPlusTerm *zapt;
406 static int attr_find (AttrType *src, oid_value *attributeSetP)
411 num_attributes = src->zapt->attributes->num_attributes;
413 num_attributes = src->zapt->num_attributes;
415 while (src->major < num_attributes)
417 Z_AttributeElement *element;
420 element = src->zapt->attributes->attributes[src->major];
422 element = src->zapt->attributeList[src->major];
424 if (src->type == *element->attributeType)
426 switch (element->which)
428 case Z_AttributeValue_numeric:
430 if (element->attributeSet && attributeSetP)
434 attrset = oid_getentbyoid (element->attributeSet);
435 *attributeSetP = attrset->value;
437 return *element->value.numeric;
439 case Z_AttributeValue_complex:
440 if (src->minor >= element->value.complex->num_list ||
441 element->value.complex->list[src->minor]->which !=
442 Z_StringOrNumeric_numeric)
445 if (element->attributeSet && attributeSetP)
449 attrset = oid_getentbyoid (element->attributeSet);
450 *attributeSetP = attrset->value;
452 return *element->value.complex->list[src->minor-1]->u.numeric;
462 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
484 static void term_untrans (ZebraHandle zh, int reg_type,
485 char *dst, const char *src)
489 const char *cp = zebra_maps_output (zh->service->zebra_maps,
500 static void add_isam_p (const char *name, const char *info,
503 if (p->isam_p_indx == p->isam_p_size)
505 ISAMS_P *new_isam_p_buf;
509 p->isam_p_size = 2*p->isam_p_size + 100;
510 new_isam_p_buf = (ISAMS_P *) xmalloc (sizeof(*new_isam_p_buf) *
514 memcpy (new_isam_p_buf, p->isam_p_buf,
515 p->isam_p_indx * sizeof(*p->isam_p_buf));
516 xfree (p->isam_p_buf);
518 p->isam_p_buf = new_isam_p_buf;
521 new_term_no = (int *) xmalloc (sizeof(*new_term_no) *
525 memcpy (new_term_no, p->isam_p_buf,
526 p->isam_p_indx * sizeof(*p->term_no));
529 p->term_no = new_term_no;
532 assert (*info == sizeof(*p->isam_p_buf));
533 memcpy (p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
536 term_untrans (p->zh, p->reg_type, term_tmp, name+2);
537 logf (LOG_DEBUG, "grep: %s", term_tmp);
542 static int grep_handle (char *name, const char *info, void *p)
544 add_isam_p (name, info, (struct grep_info *) p);
548 static int term_pre (ZebraMaps zebra_maps, int reg_type, const char **src,
549 const char *ct1, const char *ct2)
551 const char *s1, *s0 = *src;
554 /* skip white space */
557 if (ct1 && strchr (ct1, *s0))
559 if (ct2 && strchr (ct2, *s0))
562 map = zebra_maps_input (zebra_maps, reg_type, &s1, strlen(s1));
563 if (**map != *CHR_SPACE)
571 /* term_100: handle term, where trunc=none (no operators at all) */
572 static int term_100 (ZebraMaps zebra_maps, int reg_type,
573 const char **src, char *dst, int space_split,
581 if (!term_pre (zebra_maps, reg_type, src, NULL, NULL))
587 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
588 if (space_split && **map == *CHR_SPACE)
592 if (!isalnum (*s1) && *s1 != '-')
604 /* term_101: handle term, where trunc=Process # */
605 static int term_101 (ZebraMaps zebra_maps, int reg_type,
606 const char **src, char *dst, int space_split,
614 if (!term_pre (zebra_maps, reg_type, src, "#", "#"))
623 dst_term[j++] = *s0++;
628 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
629 if (space_split && **map == *CHR_SPACE)
641 dst_term[j++] = '\0';
646 /* term_103: handle term, where trunc=re-2 (regular expressions) */
647 static int term_103 (ZebraMaps zebra_maps, int reg_type, const char **src,
648 char *dst, int *errors, int space_split,
656 if (!term_pre (zebra_maps, reg_type, src, "^\\()[].*+?|", "("))
659 if (errors && *s0 == '+' && s0[1] && s0[2] == '+' && s0[3] &&
662 *errors = s0[1] - '0';
669 if (strchr ("^\\()[].*+?|-", *s0))
677 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
678 if (**map == *CHR_SPACE)
695 /* term_103: handle term, where trunc=re-1 (regular expressions) */
696 static int term_102 (ZebraMaps zebra_maps, int reg_type, const char **src,
697 char *dst, int space_split, char *dst_term)
699 return term_103 (zebra_maps, reg_type, src, dst, NULL, space_split,
704 /* term_104: handle term, where trunc=Process # and ! */
705 static int term_104 (ZebraMaps zebra_maps, int reg_type,
706 const char **src, char *dst, int space_split,
714 if (!term_pre (zebra_maps, reg_type, src, "#!", "#!"))
723 dst_term[j++] = *s0++;
728 dst_term[j++] = *s0++;
732 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
733 if (space_split && **map == *CHR_SPACE)
745 dst_term[j++] = '\0';
750 /* term_105/106: handle term, where trunc=Process * and ! and right trunc */
751 static int term_105 (ZebraMaps zebra_maps, int reg_type,
752 const char **src, char *dst, int space_split,
753 char *dst_term, int right_truncate)
760 if (!term_pre (zebra_maps, reg_type, src, "*!", "*!"))
769 dst_term[j++] = *s0++;
774 dst_term[j++] = *s0++;
778 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
779 if (space_split && **map == *CHR_SPACE)
797 dst_term[j++] = '\0';
803 /* gen_regular_rel - generate regular expression from relation
804 * val: border value (inclusive)
805 * islt: 1 if <=; 0 if >=.
807 static void gen_regular_rel (char *dst, int val, int islt)
814 logf (LOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
818 strcpy (dst, "(-[0-9]+|(");
826 strcpy (dst, "([0-9]+|-(");
827 dst_p = strlen (dst);
837 dst_p = strlen (dst);
838 sprintf (numstr, "%d", val);
839 for (w = strlen(numstr); --w >= 0; pos++)
858 strcpy (dst + dst_p, numstr);
859 dst_p = strlen(dst) - pos - 1;
887 for (i = 0; i<pos; i++)
900 /* match everything less than 10^(pos-1) */
902 for (i=1; i<pos; i++)
903 strcat (dst, "[0-9]?");
907 /* match everything greater than 10^pos */
908 for (i = 0; i <= pos; i++)
909 strcat (dst, "[0-9]");
910 strcat (dst, "[0-9]*");
915 void string_rel_add_char (char **term_p, const char *src, int *indx)
917 if (src[*indx] == '\\')
918 *(*term_p)++ = src[(*indx)++];
919 *(*term_p)++ = src[(*indx)++];
923 * > abc ([b-].*|a[c-].*|ab[d-].*|abc.+)
924 * ([^-a].*|a[^-b].*ab[^-c].*|abc.+)
925 * >= abc ([b-].*|a[c-].*|ab[c-].*)
926 * ([^-a].*|a[^-b].*|ab[c-].*)
927 * < abc ([-0].*|a[-a].*|ab[-b].*)
928 * ([^a-].*|a[^b-].*|ab[^c-].*)
929 * <= abc ([-0].*|a[-a].*|ab[-b].*|abc)
930 * ([^a-].*|a[^b-].*|ab[^c-].*|abc)
932 static int string_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
933 const char **term_sub, char *term_dict,
934 oid_value attributeSet,
935 int reg_type, int space_split, char *term_dst)
940 char *term_tmp = term_dict + strlen(term_dict);
941 char term_component[256];
943 attr_init (&relation, zapt, 2);
944 relation_value = attr_find (&relation, NULL);
946 logf (LOG_DEBUG, "string relation value=%d", relation_value);
947 switch (relation_value)
950 if (!term_100 (zh->service->zebra_maps, reg_type,
951 term_sub, term_component,
952 space_split, term_dst))
954 logf (LOG_DEBUG, "Relation <");
957 for (i = 0; term_component[i]; )
964 string_rel_add_char (&term_tmp, term_component, &j);
969 string_rel_add_char (&term_tmp, term_component, &i);
980 if (!term_100 (zh->service->zebra_maps, reg_type,
981 term_sub, term_component,
982 space_split, term_dst))
984 logf (LOG_DEBUG, "Relation <=");
987 for (i = 0; term_component[i]; )
992 string_rel_add_char (&term_tmp, term_component, &j);
996 string_rel_add_char (&term_tmp, term_component, &i);
1005 for (i = 0; term_component[i]; )
1006 string_rel_add_char (&term_tmp, term_component, &i);
1011 if (!term_100 (zh->service->zebra_maps, reg_type,
1012 term_sub, term_component, space_split, term_dst))
1014 logf (LOG_DEBUG, "Relation >");
1017 for (i = 0; term_component[i];)
1022 string_rel_add_char (&term_tmp, term_component, &j);
1027 string_rel_add_char (&term_tmp, term_component, &i);
1035 for (i = 0; term_component[i];)
1036 string_rel_add_char (&term_tmp, term_component, &i);
1043 if (!term_100 (zh->service->zebra_maps, reg_type, term_sub,
1044 term_component, space_split, term_dst))
1046 logf (LOG_DEBUG, "Relation >=");
1049 for (i = 0; term_component[i];)
1056 string_rel_add_char (&term_tmp, term_component, &j);
1059 if (term_component[i+1])
1063 string_rel_add_char (&term_tmp, term_component, &i);
1067 string_rel_add_char (&term_tmp, term_component, &i);
1079 logf (LOG_DEBUG, "Relation =");
1080 if (!term_100 (zh->service->zebra_maps, reg_type, term_sub,
1081 term_component, space_split, term_dst))
1083 strcat (term_tmp, "(");
1084 strcat (term_tmp, term_component);
1085 strcat (term_tmp, ")");
1090 static int string_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1091 const char **term_sub,
1092 oid_value attributeSet, NMEM stream,
1093 struct grep_info *grep_info,
1094 int reg_type, int complete_flag,
1095 int num_bases, char **basenames,
1098 char term_dict[2*IT_MAX_WORD+4000];
1100 AttrType truncation;
1101 int truncation_value;
1104 oid_value curAttributeSet = attributeSet;
1106 struct rpn_char_map_info rcmi;
1107 int space_split = complete_flag ? 0 : 1;
1109 rpn_char_map_prepare (zh, reg_type, &rcmi);
1110 attr_init (&use, zapt, 1);
1111 use_value = attr_find (&use, &curAttributeSet);
1112 logf (LOG_DEBUG, "string_term, use value %d", use_value);
1113 attr_init (&truncation, zapt, 5);
1114 truncation_value = attr_find (&truncation, NULL);
1115 logf (LOG_DEBUG, "truncation value %d", truncation_value);
1117 if (use_value == -1)
1120 for (base_no = 0; base_no < num_bases; base_no++)
1123 data1_local_attribute *local_attr;
1124 int max_pos, prefix_len = 0;
1127 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
1129 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1130 curAttributeSet, use_value, r);
1134 sprintf (val_str, "%d", use_value);
1136 zh->errString = nmem_strdup (stream, val_str);
1141 struct oident oident;
1143 oident.proto = PROTO_Z3950;
1144 oident.oclass = CLASS_ATTSET;
1145 oident.value = curAttributeSet;
1146 oid_ent_to_oid (&oident, oid);
1149 zh->errString = nmem_strdup (stream, oident.desc);
1153 if (zebraExplain_curDatabase (zh->service->zei, basenames[base_no]))
1155 zh->errCode = 109; /* Database unavailable */
1156 zh->errString = basenames[base_no];
1159 for (local_attr = attp.local_attributes; local_attr;
1160 local_attr = local_attr->next)
1166 ord = zebraExplain_lookupSU (zh->service->zei, attp.attset_ordinal,
1171 term_dict[prefix_len++] = '|';
1173 term_dict[prefix_len++] = '(';
1175 ord_len = key_SU_code (ord, ord_buf);
1176 for (i = 0; i<ord_len; i++)
1178 term_dict[prefix_len++] = 1;
1179 term_dict[prefix_len++] = ord_buf[i];
1185 sprintf (val_str, "%d", use_value);
1187 zh->errString = nmem_strdup (stream, val_str);
1190 term_dict[prefix_len++] = ')';
1191 term_dict[prefix_len++] = 1;
1192 term_dict[prefix_len++] = reg_type;
1193 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1194 term_dict[prefix_len] = '\0';
1196 switch (truncation_value)
1198 case -1: /* not specified */
1199 case 100: /* do not truncate */
1200 if (!string_relation (zh, zapt, &termp, term_dict,
1202 reg_type, space_split, term_dst))
1204 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_dict+prefix_len);
1205 r = dict_lookup_grep (zh->service->dict, term_dict, 0,
1206 grep_info, &max_pos, 0, grep_handle);
1208 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1210 case 1: /* right truncation */
1211 term_dict[j++] = '(';
1212 if (!term_100 (zh->service->zebra_maps, reg_type,
1213 &termp, term_dict + j, space_split, term_dst))
1215 strcat (term_dict, ".*)");
1216 dict_lookup_grep (zh->service->dict, term_dict, 0, grep_info,
1217 &max_pos, 0, grep_handle);
1219 case 2: /* keft truncation */
1220 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1221 if (!term_100 (zh->service->zebra_maps, reg_type,
1222 &termp, term_dict + j, space_split, term_dst))
1224 strcat (term_dict, ")");
1225 dict_lookup_grep (zh->service->dict, term_dict, 0, grep_info,
1226 &max_pos, 0, grep_handle);
1228 case 3: /* left&right truncation */
1229 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1230 if (!term_100 (zh->service->zebra_maps, reg_type,
1231 &termp, term_dict + j, space_split, term_dst))
1233 strcat (term_dict, ".*)");
1234 dict_lookup_grep (zh->service->dict, term_dict, 0, grep_info,
1235 &max_pos, 0, grep_handle);
1239 case 101: /* process # in term */
1240 term_dict[j++] = '(';
1241 if (!term_101 (zh->service->zebra_maps, reg_type,
1242 &termp, term_dict + j, space_split, term_dst))
1244 strcat (term_dict, ")");
1245 r = dict_lookup_grep (zh->service->dict, term_dict, 0, grep_info,
1246 &max_pos, 0, grep_handle);
1248 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d", r);
1250 case 102: /* Regexp-1 */
1251 term_dict[j++] = '(';
1252 if (!term_102 (zh->service->zebra_maps, reg_type,
1253 &termp, term_dict + j, space_split, term_dst))
1255 strcat (term_dict, ")");
1256 logf (LOG_DEBUG, "Regexp-1 tolerance=%d", r);
1257 r = dict_lookup_grep (zh->service->dict, term_dict, 0, grep_info,
1258 &max_pos, 0, grep_handle);
1260 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
1263 case 103: /* Regexp-2 */
1265 term_dict[j++] = '(';
1266 if (!term_103 (zh->service->zebra_maps, reg_type,
1267 &termp, term_dict + j, &r, space_split, term_dst))
1269 strcat (term_dict, ")");
1270 logf (LOG_DEBUG, "Regexp-2 tolerance=%d", r);
1271 r = dict_lookup_grep (zh->service->dict, term_dict, r, grep_info,
1272 &max_pos, 2, grep_handle);
1274 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
1277 case 104: /* process # and ! in term */
1278 term_dict[j++] = '(';
1279 if (!term_104 (zh->service->zebra_maps, reg_type,
1280 &termp, term_dict + j, space_split, term_dst))
1282 strcat (term_dict, ")");
1283 r = dict_lookup_grep (zh->service->dict, term_dict, 0, grep_info,
1284 &max_pos, 0, grep_handle);
1286 logf (LOG_WARN, "dict_lookup_grep err, trunc=#/!: %d", r);
1288 case 105: /* process * and ! in term */
1289 term_dict[j++] = '(';
1290 if (!term_105 (zh->service->zebra_maps, reg_type,
1291 &termp, term_dict + j, space_split, term_dst, 1))
1293 strcat (term_dict, ")");
1294 r = dict_lookup_grep (zh->service->dict, term_dict, 0, grep_info,
1295 &max_pos, 0, grep_handle);
1297 logf (LOG_WARN, "dict_lookup_grep err, trunc=*/!: %d", r);
1299 case 106: /* process * and ! in term */
1300 term_dict[j++] = '(';
1301 if (!term_105 (zh->service->zebra_maps, reg_type,
1302 &termp, term_dict + j, space_split, term_dst, 0))
1304 strcat (term_dict, ")");
1305 r = dict_lookup_grep (zh->service->dict, term_dict, 0, grep_info,
1306 &max_pos, 0, grep_handle);
1308 logf (LOG_WARN, "dict_lookup_grep err, trunc=*/!: %d", r);
1313 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1317 static void trans_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1321 Z_Term *term = zapt->term;
1323 sizez = term->u.general->len;
1324 if (sizez > IT_MAX_WORD-1)
1325 sizez = IT_MAX_WORD-1;
1326 memcpy (termz, term->u.general->buf, sizez);
1327 termz[sizez] = '\0';
1330 static void trans_scan_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1331 char *termz, int reg_type)
1333 Z_Term *term = zapt->term;
1335 const char *cp = (const char *) term->u.general->buf;
1336 const char *cp_end = cp + term->u.general->len;
1339 const char *space_map = NULL;
1342 while ((len = (cp_end - cp)) > 0)
1344 map = zebra_maps_input (zh->service->zebra_maps, reg_type, &cp, len);
1345 if (**map == *CHR_SPACE)
1350 for (src = space_map; *src; src++)
1353 for (src = *map; *src; src++)
1360 static RSET rpn_prox (ZebraHandle zh, RSET *rset, int rset_no,
1361 int ordered, int exclusion, int relation, int distance)
1366 struct it_key **buf;
1368 char prox_term[1024];
1369 int length_prox_term = 0;
1370 int min_nn = 10000000;
1372 const char *flags = NULL;
1374 rsfd = (RSFD *) xmalloc (sizeof(*rsfd)*rset_no);
1375 more = (int *) xmalloc (sizeof(*more)*rset_no);
1376 buf = (struct it_key **) xmalloc (sizeof(*buf)*rset_no);
1379 for (i = 0; i<rset_no; i++)
1382 for (j = 0; j<rset[i]->no_rset_terms; j++)
1384 const char *nflags = rset[i]->rset_terms[j]->flags;
1385 char *term = rset[i]->rset_terms[j]->name;
1386 int lterm = strlen(term);
1387 if (lterm + length_prox_term < sizeof(prox_term)-1)
1389 if (length_prox_term)
1390 prox_term[length_prox_term++] = ' ';
1391 strcpy (prox_term + length_prox_term, term);
1392 length_prox_term += lterm;
1394 if (min_nn > rset[i]->rset_terms[j]->nn)
1395 min_nn = rset[i]->rset_terms[j]->nn;
1399 for (i = 0; i<rset_no; i++)
1404 for (i = 0; i<rset_no; i++)
1406 buf[i] = (struct it_key *) xmalloc (sizeof(**buf));
1407 rsfd[i] = rset_open (rset[i], RSETF_READ);
1408 if (!(more[i] = rset_read (rset[i], rsfd[i], buf[i], &term_index)))
1413 /* at least one is empty ... return null set */
1414 rset_null_parms parms;
1416 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1418 parms.rset_term->nn = 0;
1419 result = rset_create (rset_kind_null, &parms);
1421 else if (ordered && relation == 3 && exclusion == 0 && distance == 1)
1423 /* special proximity case = phrase search ... */
1424 rset_temp_parms parms;
1427 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1429 parms.rset_term->nn = min_nn;
1430 parms.key_size = sizeof (struct it_key);
1431 parms.temp_path = res_get (zh->service->res, "setTmpDir");
1432 result = rset_create (rset_kind_temp, &parms);
1433 rsfd_result = rset_open (result, RSETF_WRITE);
1437 for (i = 1; i<rset_no; i++)
1446 cmp = key_compare_it (buf[i], buf[i-1]);
1449 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1450 buf[i-1], &term_index);
1455 if (buf[i-1]->seqno+1 != buf[i]->seqno)
1457 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1458 buf[i-1], &term_index);
1464 more[i] = rset_read (rset[i], rsfd[i], buf[i],
1471 rset_write (result, rsfd_result, buf[0]);
1472 more[0] = rset_read (*rset, *rsfd, *buf, &term_index);
1475 rset_close (result, rsfd_result);
1477 else if (rset_no == 2)
1479 /* generic proximity case (two input sets only) ... */
1480 rset_temp_parms parms;
1483 logf (LOG_LOG, "generic prox, dist = %d, relation = %d, ordered =%d, exclusion=%d",
1484 distance, relation, ordered, exclusion);
1485 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1487 parms.rset_term->nn = min_nn;
1488 parms.key_size = sizeof (struct it_key);
1489 parms.temp_path = res_get (zh->service->res, "setTmpDir");
1490 result = rset_create (rset_kind_temp, &parms);
1491 rsfd_result = rset_open (result, RSETF_WRITE);
1493 while (more[0] && more[1])
1495 int cmp = key_compare_it (buf[0], buf[1]);
1497 more[0] = rset_read (rset[0], rsfd[0], buf[0], &term_index);
1499 more[1] = rset_read (rset[1], rsfd[1], buf[1], &term_index);
1502 int sysno = buf[0]->sysno;
1506 seqno[n++] = buf[0]->seqno;
1507 while ((more[0] = rset_read (rset[0], rsfd[0], buf[0],
1509 sysno == buf[0]->sysno)
1511 seqno[n++] = buf[0]->seqno;
1514 for (i = 0; i<n; i++)
1516 int diff = buf[1]->seqno - seqno[i];
1517 int excl = exclusion;
1518 if (!ordered && 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)
1543 if (diff != distance && diff >= 0)
1549 rset_write (result, rsfd_result, buf[1]);
1553 } while ((more[1] = rset_read (rset[1], rsfd[1], buf[1],
1555 sysno == buf[1]->sysno);
1558 rset_close (result, rsfd_result);
1562 rset_null_parms parms;
1564 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1566 parms.rset_term->nn = 0;
1567 result = rset_create (rset_kind_null, &parms);
1569 for (i = 0; i<rset_no; i++)
1572 rset_close (rset[i], rsfd[i]);
1582 char *normalize_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1583 const char *termz, NMEM stream, unsigned reg_id)
1586 AttrType truncation;
1587 int truncation_value;
1590 attr_init (&truncation, zapt, 5);
1591 truncation_value = attr_find (&truncation, NULL);
1593 switch (truncation_value)
1613 wrbuf = zebra_replace(zh->service->zebra_maps, reg_id, ex_list,
1614 termz, strlen(termz));
1616 return nmem_strdup(stream, termz);
1619 char *buf = (char*) nmem_malloc (stream, wrbuf_len(wrbuf)+1);
1620 memcpy (buf, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1621 buf[wrbuf_len(wrbuf)] = '\0';
1626 static RSET rpn_search_APT_phrase (ZebraHandle zh,
1627 Z_AttributesPlusTerm *zapt,
1628 const char *termz_org,
1629 oid_value attributeSet,
1631 int reg_type, int complete_flag,
1632 const char *rank_type,
1633 int num_bases, char **basenames)
1635 char term_dst[IT_MAX_WORD+1];
1636 RSET rset[60], result;
1637 int i, r, rset_no = 0;
1638 struct grep_info grep_info;
1639 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1640 const char *termp = termz;
1643 grep_info.term_no = 0;
1645 grep_info.isam_p_size = 0;
1646 grep_info.isam_p_buf = NULL;
1648 grep_info.reg_type = reg_type;
1652 logf (LOG_DEBUG, "APT_phrase termp=%s", termp);
1653 grep_info.isam_p_indx = 0;
1654 r = string_term (zh, zapt, &termp, attributeSet, stream, &grep_info,
1655 reg_type, complete_flag, num_bases, basenames,
1659 logf (LOG_DEBUG, "term: %s", term_dst);
1660 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1661 grep_info.isam_p_indx, term_dst,
1662 strlen(term_dst), rank_type);
1663 assert (rset[rset_no]);
1664 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1668 xfree(grep_info.term_no);
1670 xfree (grep_info.isam_p_buf);
1673 rset_null_parms parms;
1675 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1676 return rset_create (rset_kind_null, &parms);
1678 else if (rset_no == 1)
1680 result = rpn_prox (zh, rset, rset_no, 1, 0, 3, 1);
1681 for (i = 0; i<rset_no; i++)
1682 rset_delete (rset[i]);
1686 static RSET rpn_search_APT_or_list (ZebraHandle zh,
1687 Z_AttributesPlusTerm *zapt,
1688 const char *termz_org,
1689 oid_value attributeSet,
1691 int reg_type, int complete_flag,
1692 const char *rank_type,
1693 int num_bases, char **basenames)
1695 char term_dst[IT_MAX_WORD+1];
1696 RSET rset[60], result;
1697 int i, r, rset_no = 0;
1698 struct grep_info grep_info;
1699 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1700 const char *termp = termz;
1702 grep_info.term_no = 0;
1704 grep_info.isam_p_size = 0;
1705 grep_info.isam_p_buf = NULL;
1707 grep_info.reg_type = reg_type;
1711 logf (LOG_DEBUG, "APT_or_list termp=%s", termp);
1712 grep_info.isam_p_indx = 0;
1713 r = string_term (zh, zapt, &termp, attributeSet, stream, &grep_info,
1714 reg_type, complete_flag, num_bases, basenames,
1718 logf (LOG_DEBUG, "term: %s", term_dst);
1719 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1720 grep_info.isam_p_indx, term_dst,
1721 strlen(term_dst), rank_type);
1722 assert (rset[rset_no]);
1723 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1727 xfree(grep_info.term_no);
1729 xfree (grep_info.isam_p_buf);
1732 rset_null_parms parms;
1734 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1735 return rset_create (rset_kind_null, &parms);
1738 for (i = 1; i<rset_no; i++)
1740 rset_bool_parms bool_parms;
1742 bool_parms.rset_l = result;
1743 bool_parms.rset_r = rset[i];
1744 bool_parms.key_size = sizeof(struct it_key);
1745 bool_parms.cmp = key_compare_it;
1746 result = rset_create (rset_kind_or, &bool_parms);
1751 static RSET rpn_search_APT_and_list (ZebraHandle zh,
1752 Z_AttributesPlusTerm *zapt,
1753 const char *termz_org,
1754 oid_value attributeSet,
1756 int reg_type, int complete_flag,
1757 const char *rank_type,
1758 int num_bases, char **basenames)
1760 char term_dst[IT_MAX_WORD+1];
1761 RSET rset[60], result;
1762 int i, r, rset_no = 0;
1763 struct grep_info grep_info;
1764 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1765 const char *termp = termz;
1768 grep_info.term_no = 0;
1770 grep_info.isam_p_size = 0;
1771 grep_info.isam_p_buf = NULL;
1773 grep_info.reg_type = reg_type;
1777 logf (LOG_DEBUG, "APT_and_list termp=%s", termp);
1778 grep_info.isam_p_indx = 0;
1779 r = string_term (zh, zapt, &termp, attributeSet, stream, &grep_info,
1780 reg_type, complete_flag, num_bases, basenames,
1784 logf (LOG_DEBUG, "term: %s", term_dst);
1785 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1786 grep_info.isam_p_indx, term_dst,
1787 strlen(term_dst), rank_type);
1788 assert (rset[rset_no]);
1789 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1793 xfree(grep_info.term_no);
1795 xfree (grep_info.isam_p_buf);
1798 rset_null_parms parms;
1800 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1801 return rset_create (rset_kind_null, &parms);
1804 for (i = 1; i<rset_no; i++)
1806 rset_bool_parms bool_parms;
1808 bool_parms.rset_l = result;
1809 bool_parms.rset_r = rset[i];
1810 bool_parms.key_size = sizeof(struct it_key);
1811 bool_parms.cmp = key_compare_it;
1812 result = rset_create (rset_kind_and, &bool_parms);
1817 static int numeric_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1818 const char **term_sub,
1820 oid_value attributeSet,
1821 struct grep_info *grep_info,
1830 char *term_tmp = term_dict + strlen(term_dict);
1832 attr_init (&relation, zapt, 2);
1833 relation_value = attr_find (&relation, NULL);
1835 logf (LOG_DEBUG, "numeric relation value=%d", relation_value);
1837 if (!term_100 (zh->service->zebra_maps, reg_type, term_sub, term_tmp, 1,
1840 term_value = atoi (term_tmp);
1841 switch (relation_value)
1844 logf (LOG_DEBUG, "Relation <");
1845 gen_regular_rel (term_tmp, term_value-1, 1);
1848 logf (LOG_DEBUG, "Relation <=");
1849 gen_regular_rel (term_tmp, term_value, 1);
1852 logf (LOG_DEBUG, "Relation >=");
1853 gen_regular_rel (term_tmp, term_value, 0);
1856 logf (LOG_DEBUG, "Relation >");
1857 gen_regular_rel (term_tmp, term_value+1, 0);
1861 logf (LOG_DEBUG, "Relation =");
1862 sprintf (term_tmp, "(0*%d)", term_value);
1864 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_tmp);
1865 r = dict_lookup_grep (zh->service->dict, term_dict, 0, grep_info, max_pos,
1868 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1869 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1873 static int numeric_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1874 const char **term_sub,
1875 oid_value attributeSet, struct grep_info *grep_info,
1876 int reg_type, int complete_flag,
1877 int num_bases, char **basenames,
1880 char term_dict[2*IT_MAX_WORD+2];
1884 oid_value curAttributeSet = attributeSet;
1886 struct rpn_char_map_info rcmi;
1888 rpn_char_map_prepare (zh, reg_type, &rcmi);
1889 attr_init (&use, zapt, 1);
1890 use_value = attr_find (&use, &curAttributeSet);
1891 logf (LOG_DEBUG, "numeric_term, use value %d", use_value);
1893 if (use_value == -1)
1896 for (base_no = 0; base_no < num_bases; base_no++)
1899 data1_local_attribute *local_attr;
1900 int max_pos, prefix_len = 0;
1903 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
1905 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1906 curAttributeSet, use_value, r);
1913 if (zebraExplain_curDatabase (zh->service->zei, basenames[base_no]))
1915 zh->errCode = 109; /* Database unavailable */
1916 zh->errString = basenames[base_no];
1919 for (local_attr = attp.local_attributes; local_attr;
1920 local_attr = local_attr->next)
1926 ord = zebraExplain_lookupSU (zh->service->zei, attp.attset_ordinal,
1931 term_dict[prefix_len++] = '|';
1933 term_dict[prefix_len++] = '(';
1935 ord_len = key_SU_code (ord, ord_buf);
1936 for (i = 0; i<ord_len; i++)
1938 term_dict[prefix_len++] = 1;
1939 term_dict[prefix_len++] = ord_buf[i];
1947 term_dict[prefix_len++] = ')';
1948 term_dict[prefix_len++] = 1;
1949 term_dict[prefix_len++] = reg_type;
1950 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1951 term_dict[prefix_len] = '\0';
1952 if (!numeric_relation (zh, zapt, &termp, term_dict,
1953 attributeSet, grep_info, &max_pos, reg_type,
1958 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1962 static RSET rpn_search_APT_numeric (ZebraHandle zh,
1963 Z_AttributesPlusTerm *zapt,
1965 oid_value attributeSet,
1967 int reg_type, int complete_flag,
1968 const char *rank_type,
1969 int num_bases, char **basenames)
1971 char term_dst[IT_MAX_WORD+1];
1972 const char *termp = termz;
1973 RSET rset[60], result;
1974 int i, r, rset_no = 0;
1975 struct grep_info grep_info;
1978 grep_info.term_no = 0;
1980 grep_info.isam_p_size = 0;
1981 grep_info.isam_p_buf = NULL;
1983 grep_info.reg_type = reg_type;
1987 logf (LOG_DEBUG, "APT_numeric termp=%s", termp);
1988 grep_info.isam_p_indx = 0;
1989 r = numeric_term (zh, zapt, &termp, attributeSet, &grep_info,
1990 reg_type, complete_flag, num_bases, basenames,
1994 logf (LOG_DEBUG, "term: %s", term_dst);
1995 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1996 grep_info.isam_p_indx, term_dst,
1997 strlen(term_dst), rank_type);
1998 assert (rset[rset_no]);
1999 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
2003 xfree(grep_info.term_no);
2005 xfree (grep_info.isam_p_buf);
2008 rset_null_parms parms;
2010 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
2011 return rset_create (rset_kind_null, &parms);
2014 for (i = 1; i<rset_no; i++)
2016 rset_bool_parms bool_parms;
2018 bool_parms.rset_l = result;
2019 bool_parms.rset_r = rset[i];
2020 bool_parms.key_size = sizeof(struct it_key);
2021 bool_parms.cmp = key_compare_it;
2022 result = rset_create (rset_kind_and, &bool_parms);
2027 static RSET rpn_search_APT_local (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2029 oid_value attributeSet,
2031 const char *rank_type)
2036 rset_temp_parms parms;
2038 parms.rset_term = rset_term_create (termz, -1, rank_type);
2039 parms.key_size = sizeof (struct it_key);
2040 parms.temp_path = res_get (zh->service->res, "setTmpDir");
2041 result = rset_create (rset_kind_temp, &parms);
2042 rsfd = rset_open (result, RSETF_WRITE);
2044 key.sysno = atoi (termz);
2048 rset_write (result, rsfd, &key);
2049 rset_close (result, rsfd);
2053 static RSET rpn_sort_spec (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2054 oid_value attributeSet, NMEM stream,
2055 Z_SortKeySpecList *sort_sequence,
2056 const char *rank_type)
2058 rset_null_parms parms;
2060 int sort_relation_value;
2061 AttrType sort_relation_type;
2066 Z_AttributeElement *ae;
2070 attr_init (&sort_relation_type, zapt, 7);
2071 sort_relation_value = attr_find (&sort_relation_type, &attributeSet);
2073 attr_init (&use_type, zapt, 1);
2074 use_value = attr_find (&use_type, &attributeSet);
2076 if (!sort_sequence->specs)
2078 sort_sequence->num_specs = 10;
2079 sort_sequence->specs = (Z_SortKeySpec **)
2080 nmem_malloc (stream, sort_sequence->num_specs *
2081 sizeof(*sort_sequence->specs));
2082 for (i = 0; i<sort_sequence->num_specs; i++)
2083 sort_sequence->specs[i] = 0;
2085 if (zapt->term->which != Z_Term_general)
2088 i = atoi_n ((char *) zapt->term->u.general->buf,
2089 zapt->term->u.general->len);
2090 if (i >= sort_sequence->num_specs)
2093 oe.proto = PROTO_Z3950;
2094 oe.oclass = CLASS_ATTSET;
2095 oe.value = attributeSet;
2096 if (!oid_ent_to_oid (&oe, oid))
2099 sks = (Z_SortKeySpec *) nmem_malloc (stream, sizeof(*sks));
2100 sks->sortElement = (Z_SortElement *)
2101 nmem_malloc (stream, sizeof(*sks->sortElement));
2102 sks->sortElement->which = Z_SortElement_generic;
2103 sk = sks->sortElement->u.generic = (Z_SortKey *)
2104 nmem_malloc (stream, sizeof(*sk));
2105 sk->which = Z_SortKey_sortAttributes;
2106 sk->u.sortAttributes = (Z_SortAttributes *)
2107 nmem_malloc (stream, sizeof(*sk->u.sortAttributes));
2109 sk->u.sortAttributes->id = oid;
2110 sk->u.sortAttributes->list = (Z_AttributeList *)
2111 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list));
2112 sk->u.sortAttributes->list->num_attributes = 1;
2113 sk->u.sortAttributes->list->attributes = (Z_AttributeElement **)
2114 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list->attributes));
2115 ae = *sk->u.sortAttributes->list->attributes = (Z_AttributeElement *)
2116 nmem_malloc (stream, sizeof(**sk->u.sortAttributes->list->attributes));
2117 ae->attributeSet = 0;
2118 ae->attributeType = (int *)
2119 nmem_malloc (stream, sizeof(*ae->attributeType));
2120 *ae->attributeType = 1;
2121 ae->which = Z_AttributeValue_numeric;
2122 ae->value.numeric = (int *)
2123 nmem_malloc (stream, sizeof(*ae->value.numeric));
2124 *ae->value.numeric = use_value;
2126 sks->sortRelation = (int *)
2127 nmem_malloc (stream, sizeof(*sks->sortRelation));
2128 if (sort_relation_value == 1)
2129 *sks->sortRelation = Z_SortRelation_ascending;
2130 else if (sort_relation_value == 2)
2131 *sks->sortRelation = Z_SortRelation_descending;
2133 *sks->sortRelation = Z_SortRelation_ascending;
2135 sks->caseSensitivity = (int *)
2136 nmem_malloc (stream, sizeof(*sks->caseSensitivity));
2137 *sks->caseSensitivity = 0;
2140 sks->which = Z_SortKeySpec_null;
2141 sks->u.null = odr_nullval ();
2143 sks->missingValueAction = 0;
2146 sort_sequence->specs[i] = sks;
2148 parms.rset_term = rset_term_create ("", -1, rank_type);
2149 return rset_create (rset_kind_null, &parms);
2153 static RSET rpn_search_APT (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2154 oid_value attributeSet, NMEM stream,
2155 Z_SortKeySpecList *sort_sequence,
2156 int num_bases, char **basenames)
2159 char *search_type = NULL;
2160 char *rank_type = NULL;
2163 char termz[IT_MAX_WORD+1];
2165 zebra_maps_attr (zh->service->zebra_maps, zapt, ®_id, &search_type,
2166 &rank_type, &complete_flag, &sort_flag);
2168 logf (LOG_DEBUG, "reg_id=%c", reg_id);
2169 logf (LOG_DEBUG, "complete_flag=%d", complete_flag);
2170 logf (LOG_DEBUG, "search_type=%s", search_type);
2171 logf (LOG_DEBUG, "rank_type=%s", rank_type);
2173 if (zapt->term->which != Z_Term_general)
2178 trans_term (zh, zapt, termz);
2181 return rpn_sort_spec (zh, zapt, attributeSet, stream, sort_sequence,
2184 if (!strcmp (search_type, "phrase"))
2186 return rpn_search_APT_phrase (zh, zapt, termz, attributeSet, stream,
2187 reg_id, complete_flag, rank_type,
2188 num_bases, basenames);
2190 else if (!strcmp (search_type, "and-list"))
2192 return rpn_search_APT_and_list (zh, zapt, termz, attributeSet, stream,
2193 reg_id, complete_flag, rank_type,
2194 num_bases, basenames);
2196 else if (!strcmp (search_type, "or-list"))
2198 return rpn_search_APT_or_list (zh, zapt, termz, attributeSet, stream,
2199 reg_id, complete_flag, rank_type,
2200 num_bases, basenames);
2202 else if (!strcmp (search_type, "local"))
2204 return rpn_search_APT_local (zh, zapt, termz, attributeSet, stream,
2207 else if (!strcmp (search_type, "numeric"))
2209 return rpn_search_APT_numeric (zh, zapt, termz, attributeSet, stream,
2210 reg_id, complete_flag, rank_type,
2211 num_bases, basenames);
2217 static RSET rpn_search_structure (ZebraHandle zh, Z_RPNStructure *zs,
2218 oid_value attributeSet, NMEM stream,
2219 Z_SortKeySpecList *sort_sequence,
2220 int num_bases, char **basenames)
2223 if (zs->which == Z_RPNStructure_complex)
2225 Z_Operator *zop = zs->u.complex->roperator;
2226 rset_bool_parms bool_parms;
2228 bool_parms.rset_l = rpn_search_structure (zh, zs->u.complex->s1,
2229 attributeSet, stream,
2231 num_bases, basenames);
2232 if (bool_parms.rset_l == NULL)
2234 bool_parms.rset_r = rpn_search_structure (zh, zs->u.complex->s2,
2235 attributeSet, stream,
2237 num_bases, basenames);
2238 if (bool_parms.rset_r == NULL)
2240 rset_delete (bool_parms.rset_l);
2243 bool_parms.key_size = sizeof(struct it_key);
2244 bool_parms.cmp = key_compare_it;
2248 case Z_Operator_and:
2249 r = rset_create (rset_kind_and, &bool_parms);
2252 r = rset_create (rset_kind_or, &bool_parms);
2254 case Z_Operator_and_not:
2255 r = rset_create (rset_kind_not, &bool_parms);
2257 case Z_Operator_prox:
2259 if (zop->u.prox->which != Z_ProximityOperator_known)
2265 if (zop->u.prox->which != Z_ProxCode_known)
2273 if (*zop->u.prox->u.known != Z_ProxUnit_word)
2275 char *val = (char *) nmem_malloc (stream, 16);
2277 zh->errString = val;
2278 sprintf (val, "%d", *zop->u.prox->u.known);
2282 if (*zop->u.prox->proximityUnitCode != Z_ProxUnit_word)
2284 char *val = (char *) nmem_malloc (stream, 16);
2286 zh->errString = val;
2287 sprintf (val, "%d", *zop->u.prox->proximityUnitCode);
2295 rsets[0] = bool_parms.rset_l;
2296 rsets[1] = bool_parms.rset_r;
2298 r = rpn_prox (zh, rsets, 2,
2299 *zop->u.prox->ordered,
2300 (!zop->u.prox->exclusion ? 0 :
2301 *zop->u.prox->exclusion),
2302 *zop->u.prox->relationType,
2303 *zop->u.prox->distance);
2304 rset_delete (rsets[0]);
2305 rset_delete (rsets[1]);
2313 else if (zs->which == Z_RPNStructure_simple)
2315 if (zs->u.simple->which == Z_Operand_APT)
2317 logf (LOG_DEBUG, "rpn_search_APT");
2318 r = rpn_search_APT (zh, zs->u.simple->u.attributesPlusTerm,
2319 attributeSet, stream, sort_sequence,
2320 num_bases, basenames);
2322 else if (zs->u.simple->which == Z_Operand_resultSetId)
2324 logf (LOG_DEBUG, "rpn_search_ref");
2325 r = resultSetRef (zh, zs->u.simple->u.resultSetId);
2327 r = rset_create (rset_kind_null, NULL);
2344 RSET rpn_search (ZebraHandle zh, NMEM nmem,
2345 Z_RPNQuery *rpn, int num_bases, char **basenames,
2346 const char *setname,
2351 oid_value attributeSet;
2352 Z_SortKeySpecList *sort_sequence;
2356 zh->errString = NULL;
2359 sort_sequence = (Z_SortKeySpecList *)
2360 nmem_malloc (nmem, sizeof(*sort_sequence));
2361 sort_sequence->num_specs = 10;
2362 sort_sequence->specs = (Z_SortKeySpec **)
2363 nmem_malloc (nmem, sort_sequence->num_specs *
2364 sizeof(*sort_sequence->specs));
2365 for (i = 0; i<sort_sequence->num_specs; i++)
2366 sort_sequence->specs[i] = 0;
2368 attrset = oid_getentbyoid (rpn->attributeSetId);
2369 attributeSet = attrset->value;
2370 rset = rpn_search_structure (zh, rpn->RPNStructure, attributeSet,
2371 nmem, sort_sequence, num_bases, basenames);
2376 logf (LOG_DEBUG, "search error: %d", zh->errCode);
2378 for (i = 0; sort_sequence->specs[i]; i++)
2380 sort_sequence->num_specs = i;
2382 resultSetRank (zh, sset, rset);
2385 logf (LOG_DEBUG, "resultSetSortSingle in rpn_search");
2386 resultSetSortSingle (zh, nmem, sset, rset,
2387 sort_sequence, &sort_status);
2390 logf (LOG_DEBUG, "resultSetSortSingle status = %d", zh->errCode);
2396 struct scan_info_entry {
2402 struct scan_info_entry *list;
2408 static int scan_handle (char *name, const char *info, int pos, void *client)
2410 int len_prefix, idx;
2411 struct scan_info *scan_info = (struct scan_info *) client;
2413 len_prefix = strlen(scan_info->prefix);
2414 if (memcmp (name, scan_info->prefix, len_prefix))
2416 if (pos > 0) idx = scan_info->after - pos + scan_info->before;
2419 scan_info->list[idx].term = (char *)
2420 odr_malloc (scan_info->odr, strlen(name + len_prefix)+1);
2421 strcpy (scan_info->list[idx].term, name + len_prefix);
2422 assert (*info == sizeof(ISAMS_P));
2423 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAMS_P));
2427 static void scan_term_untrans (ZebraHandle zh, NMEM stream, int reg_type,
2428 char **dst, const char *src)
2430 char term_dst[1024];
2432 term_untrans (zh, reg_type, term_dst, src);
2434 *dst = (char *) nmem_malloc (stream, strlen(term_dst)+1);
2435 strcpy (*dst, term_dst);
2438 static void count_set (RSET r, int *count)
2446 logf (LOG_DEBUG, "count_set");
2449 rfd = rset_open (r, RSETF_READ);
2450 while (rset_read (r, rfd, &key, &term_index))
2452 if (key.sysno != psysno)
2459 rset_close (r, rfd);
2460 logf (LOG_DEBUG, "%d keys, %d records", kno, *count);
2463 void rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
2464 oid_value attributeset,
2465 int num_bases, char **basenames,
2466 int *position, int *num_entries, ZebraScanEntry **list,
2470 int pos = *position;
2471 int num = *num_entries;
2475 char termz[IT_MAX_WORD+20];
2478 struct scan_info *scan_info_array;
2479 ZebraScanEntry *glist;
2480 int ords[32], ord_no = 0;
2484 char *search_type = NULL;
2485 char *rank_type = NULL;
2490 if (attributeset == VAL_NONE)
2491 attributeset = VAL_BIB1;
2493 logf (LOG_DEBUG, "position = %d, num = %d", pos, num);
2495 attr_init (&use, zapt, 1);
2496 use_value = attr_find (&use, &attributeset);
2498 if (zebra_maps_attr (zh->service->zebra_maps, zapt, ®_id, &search_type,
2499 &rank_type, &complete_flag, &sort_flag))
2506 if (use_value == -1)
2508 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
2512 data1_local_attribute *local_attr;
2514 if ((r=att_getentbyatt (zh, &attp, attributeset, use_value)))
2516 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
2517 attributeset, use_value);
2525 if (zebraExplain_curDatabase (zh->service->zei, basenames[base_no]))
2527 zh->errString = basenames[base_no];
2528 zh->errCode = 109; /* Database unavailable */
2532 for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
2533 local_attr = local_attr->next)
2537 ord = zebraExplain_lookupSU (zh->service->zei, attp.attset_ordinal,
2540 ords[ord_no++] = ord;
2549 /* prepare dictionary scanning */
2552 scan_info_array = (struct scan_info *)
2553 odr_malloc (stream, ord_no * sizeof(*scan_info_array));
2554 for (i = 0; i < ord_no; i++)
2556 int j, prefix_len = 0;
2557 int before_tmp = before, after_tmp = after;
2558 struct scan_info *scan_info = scan_info_array + i;
2559 struct rpn_char_map_info rcmi;
2561 rpn_char_map_prepare (zh, reg_id, &rcmi);
2563 scan_info->before = before;
2564 scan_info->after = after;
2565 scan_info->odr = stream;
2567 scan_info->list = (struct scan_info_entry *)
2568 odr_malloc (stream, (before+after) * sizeof(*scan_info->list));
2569 for (j = 0; j<before+after; j++)
2570 scan_info->list[j].term = NULL;
2572 prefix_len += key_SU_code (ords[i], termz + prefix_len);
2573 termz[prefix_len++] = reg_id;
2574 termz[prefix_len] = 0;
2575 strcpy (scan_info->prefix, termz);
2577 trans_scan_term (zh, zapt, termz+prefix_len, reg_id);
2579 dict_scan (zh->service->dict, termz, &before_tmp, &after_tmp,
2580 scan_info, scan_handle);
2582 glist = (ZebraScanEntry *)
2583 odr_malloc (stream, (before+after)*sizeof(*glist));
2585 /* consider terms after main term */
2586 for (i = 0; i < ord_no; i++)
2590 for (i = 0; i<after; i++)
2593 const char *mterm = NULL;
2597 for (j = 0; j < ord_no; j++)
2599 if (ptr[j] < before+after &&
2600 (tst=scan_info_array[j].list[ptr[j]].term) &&
2601 (!mterm || strcmp (tst, mterm) < 0))
2609 scan_term_untrans (zh, stream->mem, reg_id,
2610 &glist[i+before].term, mterm);
2611 rset = rset_trunc (zh, &scan_info_array[j0].list[ptr[j0]].isam_p, 1,
2612 glist[i+before].term, strlen(glist[i+before].term),
2616 for (j = j0+1; j<ord_no; j++)
2618 if (ptr[j] < before+after &&
2619 (tst=scan_info_array[j].list[ptr[j]].term) &&
2620 !strcmp (tst, mterm))
2622 rset_bool_parms bool_parms;
2626 rset_trunc (zh, &scan_info_array[j].list[ptr[j]].isam_p, 1,
2627 glist[i+before].term,
2628 strlen(glist[i+before].term), NULL);
2630 bool_parms.key_size = sizeof(struct it_key);
2631 bool_parms.cmp = key_compare_it;
2632 bool_parms.rset_l = rset;
2633 bool_parms.rset_r = rset2;
2635 rset = rset_create (rset_kind_or, &bool_parms);
2640 count_set (rset, &glist[i+before].occurrences);
2645 *num_entries -= (after-i);
2649 /* consider terms before main term */
2650 for (i = 0; i<ord_no; i++)
2653 for (i = 0; i<before; i++)
2656 const char *mterm = NULL;
2660 for (j = 0; j <ord_no; j++)
2662 if (ptr[j] < before &&
2663 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2664 (!mterm || strcmp (tst, mterm) > 0))
2673 scan_term_untrans (zh, stream->mem, reg_id,
2674 &glist[before-1-i].term, mterm);
2677 (zh, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1,
2678 glist[before-1-i].term, strlen(glist[before-1-i].term),
2683 for (j = j0+1; j<ord_no; j++)
2685 if (ptr[j] < before &&
2686 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2687 !strcmp (tst, mterm))
2689 rset_bool_parms bool_parms;
2692 rset2 = rset_trunc (zh,
2693 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1,
2694 glist[before-1-i].term,
2695 strlen(glist[before-1-i].term), NULL);
2697 bool_parms.key_size = sizeof(struct it_key);
2698 bool_parms.cmp = key_compare_it;
2699 bool_parms.rset_l = rset;
2700 bool_parms.rset_r = rset2;
2702 rset = rset_create (rset_kind_or, &bool_parms);
2707 count_set (rset, &glist[before-1-i].occurrences);
2717 *list = glist + i; /* list is set to first 'real' entry */
2719 logf (LOG_DEBUG, "position = %d, num_entries = %d",
2720 *position, *num_entries);
2722 logf (LOG_DEBUG, "scan error: %d", zh->errCode);