2 * Copyright (C) 1995-2001, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.108 2001-11-14 22:06:27 adam
8 * Rank-weight may be controlled via query.
10 * Revision 1.107 2001/10/15 19:53:43 adam
11 * POSIX thread updates. First work on term sets.
13 * Revision 1.106 2001/04/11 07:58:13 adam
14 * Bug fix: multiple space mapped to one space when using complete subfield.
16 * Revision 1.105 2000/11/08 13:46:59 adam
17 * Fixed scan: server could break if bad attribute/database was selected.
18 * Work on remote update.
20 * Revision 1.104 2000/04/05 09:49:35 adam
21 * On Unix, zebra/z'mbol uses automake.
23 * Revision 1.103 2000/03/20 19:08:36 adam
24 * Added remote record import using Z39.50 extended services and Segment
27 * Revision 1.102 2000/03/15 15:00:31 adam
28 * First work on threaded version.
30 * Revision 1.101 2000/03/02 14:35:03 adam
31 * Fixed proximity handling.
33 * Revision 1.100 1999/12/28 15:48:12 adam
36 * Revision 1.99 1999/12/23 09:03:32 adam
37 * Changed behaviour of trunc=105 so that * is regular .* and ! is regular .
39 * Revision 1.98 1999/11/30 13:48:04 adam
40 * Improved installation. Updated for inclusion of YAZ header files.
42 * Revision 1.97 1999/10/14 14:33:50 adam
43 * Added truncation 5=106.
45 * Revision 1.96 1999/09/23 10:05:05 adam
46 * Implemented structure=105 searching.
48 * Revision 1.95 1999/09/07 07:19:21 adam
49 * Work on character mapping. Implemented replace rules.
51 * Revision 1.94 1999/07/20 13:59:18 adam
52 * Fixed bug that occurred when phrases had 0 hits.
54 * Revision 1.93 1999/06/17 14:38:40 adam
55 * Bug fix: Scan SEGV'ed when getting unknown use attribute.
57 * Revision 1.92 1999/05/26 07:49:13 adam
60 * Revision 1.91 1999/02/02 14:51:13 adam
61 * Updated WIN32 code specific sections. Changed header.
63 * Revision 1.90 1998/11/16 16:03:43 adam
64 * Moved loggin utilities to Yaz. Was implemented in file zlogs.c.
66 * Revision 1.89 1998/11/16 10:11:55 adam
67 * Added addtional info for error 114 - unsupported use attribute.
69 * Revision 1.88 1998/10/18 07:54:52 adam
70 * Additional info added for diagnostics 114 (Unsupported use attribute) and
71 * 121 (Unsupported attribute set).
73 * Revision 1.87 1998/09/28 11:19:12 adam
74 * Fix for Compiled ASN.1.
76 * Revision 1.86 1998/09/22 10:48:20 adam
77 * Minor changes in search API.
79 * Revision 1.85 1998/09/22 10:03:43 adam
80 * Changed result sets to be persistent in the sense that they can
81 * be re-searched if needed.
82 * Fixed memory leak in rsm_or.
84 * Revision 1.84 1998/09/18 12:41:00 adam
85 * Fixed bug with numerical relations.
87 * Revision 1.83 1998/09/02 13:53:19 adam
88 * Extra parameter decode added to search routines to implement
91 * Revision 1.82 1998/06/26 11:16:40 quinn
92 * Added support (un-optimised) for left and left/right truncation
94 * Revision 1.81 1998/06/24 12:16:14 adam
95 * Support for relations on text operands. Open range support in
96 * DFA module (i.e. [-j], [g-]).
98 * Revision 1.80 1998/06/23 15:33:34 adam
99 * Added feature to specify sort criteria in query (type 7 specifies
102 * Revision 1.79 1998/06/22 11:35:09 adam
105 * Revision 1.78 1998/06/08 14:43:17 adam
106 * Added suport for EXPLAIN Proxy servers - added settings databasePath
107 * and explainDatabase to facilitate this. Increased maximum number
108 * of databases and attributes in one register.
110 * Revision 1.77 1998/05/20 10:12:22 adam
111 * Implemented automatic EXPLAIN database maintenance.
112 * Modified Zebra to work with ASN.1 compiled version of YAZ.
114 * Revision 1.76 1998/04/02 14:35:29 adam
115 * First version of Zebra that works with compiled ASN.1.
117 * Revision 1.75 1998/03/05 08:45:13 adam
118 * New result set model and modular ranking system. Moved towards
119 * descent server API. System information stored as "SGML" records.
121 * Revision 1.74 1998/02/10 12:03:06 adam
124 * Revision 1.73 1998/01/29 13:40:11 adam
125 * Better logging for scan service.
127 * Revision 1.72 1998/01/07 13:53:41 adam
128 * Queries using simple ranked operands returns right number of hits.
130 * Revision 1.71 1997/12/18 10:54:24 adam
131 * New method result set method rs_hits that returns the number of
132 * hits in result-set (if known). The ranked result set returns real
133 * number of hits but only when not combined with other operands.
135 * Revision 1.70 1997/10/31 12:34:43 adam
136 * Changed a few log statements.
138 * Revision 1.69 1997/10/29 12:05:02 adam
139 * Server produces diagnostic "Unsupported Attribute Set" when appropriate.
141 * Revision 1.68 1997/10/27 14:33:06 adam
142 * Moved towards generic character mapping depending on "structure"
143 * field in abstract syntax file. Fixed a few memory leaks. Fixed
144 * bug with negative integers when doing searches with relational
147 * Revision 1.67 1997/09/29 09:06:10 adam
148 * Removed one static var in order to make this module thread safe.
150 * Revision 1.66 1997/09/25 14:58:03 adam
153 * Revision 1.65 1997/09/22 12:39:06 adam
154 * Added get_pos method for the ranked result sets.
156 * Revision 1.64 1997/09/18 08:59:20 adam
157 * Extra generic handle for the character mapping routines.
159 * Revision 1.63 1997/09/17 12:19:18 adam
160 * Zebra version corresponds to YAZ version 1.4.
161 * Changed Zebra server so that it doesn't depend on global common_resource.
163 * Revision 1.62 1997/09/05 15:30:09 adam
164 * Changed prototype for chr_map_input - added const.
165 * Added support for C++, headers uses extern "C" for public definitions.
167 * Revision 1.61 1997/02/10 10:21:14 adam
168 * Bug fix: in search terms character (^) wasn't observed.
170 * Revision 1.60 1997/01/31 11:10:34 adam
171 * Bug fix: Leading and trailing white space weren't removed in scan tokens.
173 * Revision 1.59 1997/01/17 11:31:46 adam
174 * Bug fix: complete phrase search didn't work.
176 * Revision 1.58 1996/12/23 15:30:45 adam
177 * Work on truncation.
178 * Bug fix: result sets weren't deleted after server shut down.
180 * Revision 1.57 1996/11/11 13:38:02 adam
181 * Added proximity support in search.
183 * Revision 1.56 1996/11/08 11:10:32 adam
184 * Buffers used during file match got bigger.
185 * Compressed ISAM support everywhere.
186 * Bug fixes regarding masking characters in queries.
187 * Redesigned Regexp-2 queries.
189 * Revision 1.55 1996/11/04 14:07:44 adam
190 * Moved truncation code to trunc.c.
192 * Revision 1.54 1996/10/29 14:09:52 adam
193 * Use of cisam system - enabled if setting isamc is 1.
195 * Revision 1.53 1996/06/26 09:21:43 adam
196 * Bug fix: local attribute set wasn't obeyed in scan.
198 * Revision 1.52 1996/06/17 14:26:20 adam
199 * Function gen_regular_rel changed to handle negative numbers.
201 * Revision 1.51 1996/06/11 10:54:15 quinn
204 * Revision 1.50 1996/06/07 08:51:53 adam
205 * Bug fix: Character mapping was broken (introducued by last revision).
207 * Revision 1.49 1996/06/04 10:18:11 adam
208 * Search/scan uses character mapping module.
210 * Revision 1.48 1996/05/28 15:15:01 adam
211 * Bug fix: Didn't handle unknown database correctly.
213 * Revision 1.47 1996/05/15 18:36:28 adam
214 * Function trans_term transforms unsearchable characters to blanks.
216 * Revision 1.46 1996/05/15 11:57:56 adam
217 * Fixed bug introduced by set/field mapping in search operations.
219 * Revision 1.45 1996/05/14 11:34:00 adam
220 * Scan support in multiple registers/databases.
222 * Revision 1.44 1996/05/14 06:16:44 adam
223 * Compact use/set bytes used in search service.
225 * Revision 1.43 1996/05/09 09:54:43 adam
226 * Server supports maps from one logical attributes to a list of physical
228 * The extraction process doesn't make space consuming 'any' keys.
230 * Revision 1.42 1996/05/09 07:28:56 quinn
231 * Work towards phrases and multiple registers
233 * Revision 1.41 1996/03/20 09:36:43 adam
234 * Function dict_lookup_grep got extra parameter, init_pos, which marks
235 * from which position in pattern approximate pattern matching should occur.
236 * Approximate pattern matching is used in relevance=re-2.
238 * Revision 1.40 1996/02/02 13:44:44 adam
239 * The public dictionary functions simply use char instead of Dict_char
240 * to represent search strings. Dict_char is used internally only.
242 * Revision 1.39 1996/01/03 16:22:13 quinn
243 * operator->roperator
245 * Revision 1.38 1995/12/11 09:12:55 adam
246 * The rec_get function returns NULL if record doesn't exist - will
247 * happen in the server if the result set records have been deleted since
248 * the creation of the set (i.e. the search).
249 * The server saves a result temporarily if it is 'volatile', i.e. the
250 * set is register dependent.
252 * Revision 1.37 1995/12/06 15:05:28 adam
253 * More verbose in count_set.
255 * Revision 1.36 1995/12/06 12:41:27 adam
256 * New command 'stat' for the index program.
257 * Filenames can be read from stdin by specifying '-'.
258 * Bug fix/enhancement of the transformation from terms to regular
259 * expressons in the search engine.
261 * Revision 1.35 1995/11/27 09:29:00 adam
262 * Bug fixes regarding conversion to regular expressions.
264 * Revision 1.34 1995/11/16 17:00:56 adam
265 * Better logging of rpn query.
267 * Revision 1.33 1995/11/01 13:58:28 quinn
268 * Moving data1 to yaz/retrieval
270 * Revision 1.32 1995/10/27 14:00:11 adam
271 * Implemented detection of database availability.
273 * Revision 1.31 1995/10/17 18:02:10 adam
274 * New feature: databases. Implemented as prefix to words in dictionary.
276 * Revision 1.30 1995/10/16 09:32:38 adam
277 * More work on relational op.
279 * Revision 1.29 1995/10/13 16:01:49 adam
282 * Revision 1.28 1995/10/13 12:26:43 adam
283 * Optimization of truncation.
285 * Revision 1.27 1995/10/12 17:07:22 adam
288 * Revision 1.26 1995/10/12 12:40:54 adam
289 * Bug fixes in rpn_prox.
291 * Revision 1.25 1995/10/10 13:59:24 adam
292 * Function rset_open changed its wflag parameter to general flags.
294 * Revision 1.24 1995/10/09 16:18:37 adam
295 * Function dict_lookup_grep got extra client data parameter.
297 * Revision 1.23 1995/10/06 16:33:37 adam
298 * Use attribute mappings.
300 * Revision 1.22 1995/10/06 15:07:39 adam
301 * Structure 'local-number' handled.
303 * Revision 1.21 1995/10/06 13:52:06 adam
304 * Bug fixes. Handler may abort further scanning.
306 * Revision 1.20 1995/10/06 11:06:33 adam
307 * Scan entries include 'occurrences' now.
309 * Revision 1.19 1995/10/06 10:43:56 adam
310 * Scan added. 'occurrences' in scan entries not set yet.
312 * Revision 1.18 1995/10/04 16:57:20 adam
313 * Key input and merge sort in one pass.
315 * Revision 1.17 1995/10/04 12:55:17 adam
316 * Bug fix in ranked search. Use=Any keys inserted.
318 * Revision 1.16 1995/10/02 16:24:40 adam
319 * Use attribute actually used in search requests.
321 * Revision 1.15 1995/10/02 15:18:52 adam
322 * New member in recRetrieveCtrl: diagnostic.
324 * Revision 1.14 1995/09/28 12:10:32 adam
325 * Bug fixes. Field prefix used in queries.
327 * Revision 1.13 1995/09/18 14:17:50 adam
330 * Revision 1.12 1995/09/15 14:45:21 adam
332 * Work on truncation.
334 * Revision 1.11 1995/09/14 11:53:27 adam
335 * First work on regular expressions/truncations.
337 * Revision 1.10 1995/09/11 15:23:26 adam
338 * More work on relevance search.
340 * Revision 1.9 1995/09/11 13:09:35 adam
341 * More work on relevance feedback.
343 * Revision 1.8 1995/09/08 14:52:27 adam
344 * Minor changes. Dictionary is lower case now.
346 * Revision 1.7 1995/09/07 13:58:36 adam
347 * New parameter: result-set file descriptor (RSFD) to support multiple
348 * positions within the same result-set.
349 * Boolean operators: and, or, not implemented.
350 * Result-set references.
352 * Revision 1.6 1995/09/06 16:11:18 adam
353 * Option: only one word key per file.
355 * Revision 1.5 1995/09/06 10:33:04 adam
356 * More work on present. Some log messages removed.
358 * Revision 1.4 1995/09/05 15:28:40 adam
359 * More work on search engine.
361 * Revision 1.3 1995/09/04 15:20:22 adam
364 * Revision 1.2 1995/09/04 12:33:43 adam
365 * Various cleanup. YAZ util used instead.
367 * Revision 1.1 1995/09/04 09:10:40 adam
368 * More work on index add/del/update.
369 * Merge sort implemented.
370 * Initial work on z39 server.
389 struct rpn_char_map_info {
394 static const char **rpn_char_map_handler (void *vp, const char **from, int len)
396 struct rpn_char_map_info *p = (struct rpn_char_map_info *) vp;
397 return zebra_maps_input (p->zm, p->reg_type, from, len);
400 static void rpn_char_map_prepare (ZebraHandle zh, int reg_type,
401 struct rpn_char_map_info *map_info)
403 map_info->zm = zh->service->zebra_maps;
404 map_info->reg_type = reg_type;
405 dict_grep_cmap (zh->service->dict, map_info, rpn_char_map_handler);
412 Z_AttributesPlusTerm *zapt;
415 static int attr_find_ex (AttrType *src, oid_value *attributeSetP,
416 const char **string_value)
421 num_attributes = src->zapt->attributes->num_attributes;
423 num_attributes = src->zapt->num_attributes;
425 while (src->major < num_attributes)
427 Z_AttributeElement *element;
430 element = src->zapt->attributes->attributes[src->major];
432 element = src->zapt->attributeList[src->major];
434 if (src->type == *element->attributeType)
436 switch (element->which)
438 case Z_AttributeValue_numeric:
440 if (element->attributeSet && attributeSetP)
444 attrset = oid_getentbyoid (element->attributeSet);
445 *attributeSetP = attrset->value;
447 return *element->value.numeric;
449 case Z_AttributeValue_complex:
450 if (src->minor >= element->value.complex->num_list)
452 if (element->value.complex->list[src->minor]->which ==
453 Z_StringOrNumeric_numeric)
456 if (element->attributeSet && attributeSetP)
460 attrset = oid_getentbyoid (element->attributeSet);
461 *attributeSetP = attrset->value;
464 *element->value.complex->list[src->minor-1]->u.numeric;
466 else if (element->value.complex->list[src->minor]->which ==
467 Z_StringOrNumeric_string)
473 element->value.complex->list[src->minor-1]->u.string;
487 static int attr_find (AttrType *src, oid_value *attributeSetP)
489 return attr_find_ex (src, attributeSetP, 0);
492 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
515 static void term_untrans (ZebraHandle zh, int reg_type,
516 char *dst, const char *src)
520 const char *cp = zebra_maps_output (zh->service->zebra_maps,
531 static void add_isam_p (const char *name, const char *info,
534 if (p->isam_p_indx == p->isam_p_size)
536 ISAMS_P *new_isam_p_buf;
540 p->isam_p_size = 2*p->isam_p_size + 100;
541 new_isam_p_buf = (ISAMS_P *) xmalloc (sizeof(*new_isam_p_buf) *
545 memcpy (new_isam_p_buf, p->isam_p_buf,
546 p->isam_p_indx * sizeof(*p->isam_p_buf));
547 xfree (p->isam_p_buf);
549 p->isam_p_buf = new_isam_p_buf;
552 new_term_no = (int *) xmalloc (sizeof(*new_term_no) *
556 memcpy (new_term_no, p->isam_p_buf,
557 p->isam_p_indx * sizeof(*p->term_no));
560 p->term_no = new_term_no;
563 assert (*info == sizeof(*p->isam_p_buf));
564 memcpy (p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
573 int len = key_SU_decode (&su_code, name);
575 term_untrans (p->zh, p->reg_type, term_tmp, name+len+1);
576 logf (LOG_LOG, "grep: %d %c %s", su_code, name[len], term_tmp);
577 zebraExplain_lookup_ord (p->zh->service->zei,
578 su_code, &db, &set, &use);
579 logf (LOG_LOG, "grep: set=%d use=%d db=%s", set, use, db);
581 resultSetAddTerm (p->zh, p->termset, name[len], db,
588 static int grep_handle (char *name, const char *info, void *p)
590 add_isam_p (name, info, (struct grep_info *) p);
594 static int term_pre (ZebraMaps zebra_maps, int reg_type, const char **src,
595 const char *ct1, const char *ct2)
597 const char *s1, *s0 = *src;
600 /* skip white space */
603 if (ct1 && strchr (ct1, *s0))
605 if (ct2 && strchr (ct2, *s0))
608 map = zebra_maps_input (zebra_maps, reg_type, &s1, strlen(s1));
609 if (**map != *CHR_SPACE)
617 /* term_100: handle term, where trunc=none (no operators at all) */
618 static int term_100 (ZebraMaps zebra_maps, int reg_type,
619 const char **src, char *dst, int space_split,
627 const char *space_start = 0;
628 const char *space_end = 0;
630 if (!term_pre (zebra_maps, reg_type, src, NULL, NULL))
636 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
639 if (**map == *CHR_SPACE)
642 else /* complete subfield only. */
644 if (**map == *CHR_SPACE)
645 { /* save space mapping for later .. */
650 else if (space_start)
651 { /* reload last space */
652 while (space_start < space_end)
654 if (!isalnum (*space_start) && *space_start != '-')
656 dst_term[j++] = *space_start;
657 dst[i++] = *space_start++;
660 space_start = space_end = 0;
663 /* add non-space char */
666 if (!isalnum (*s1) && *s1 != '-')
678 /* term_101: handle term, where trunc=Process # */
679 static int term_101 (ZebraMaps zebra_maps, int reg_type,
680 const char **src, char *dst, int space_split,
688 if (!term_pre (zebra_maps, reg_type, src, "#", "#"))
697 dst_term[j++] = *s0++;
702 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
703 if (space_split && **map == *CHR_SPACE)
715 dst_term[j++] = '\0';
720 /* term_103: handle term, where trunc=re-2 (regular expressions) */
721 static int term_103 (ZebraMaps zebra_maps, int reg_type, const char **src,
722 char *dst, int *errors, int space_split,
730 if (!term_pre (zebra_maps, reg_type, src, "^\\()[].*+?|", "("))
733 if (errors && *s0 == '+' && s0[1] && s0[2] == '+' && s0[3] &&
736 *errors = s0[1] - '0';
743 if (strchr ("^\\()[].*+?|-", *s0))
751 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
752 if (**map == *CHR_SPACE)
769 /* term_103: handle term, where trunc=re-1 (regular expressions) */
770 static int term_102 (ZebraMaps zebra_maps, int reg_type, const char **src,
771 char *dst, int space_split, char *dst_term)
773 return term_103 (zebra_maps, reg_type, src, dst, NULL, space_split,
778 /* term_104: handle term, where trunc=Process # and ! */
779 static int term_104 (ZebraMaps zebra_maps, int reg_type,
780 const char **src, char *dst, int space_split,
788 if (!term_pre (zebra_maps, reg_type, src, "#!", "#!"))
797 dst_term[j++] = *s0++;
802 dst_term[j++] = *s0++;
806 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
807 if (space_split && **map == *CHR_SPACE)
819 dst_term[j++] = '\0';
824 /* term_105/106: handle term, where trunc=Process * and ! and right trunc */
825 static int term_105 (ZebraMaps zebra_maps, int reg_type,
826 const char **src, char *dst, int space_split,
827 char *dst_term, int right_truncate)
834 if (!term_pre (zebra_maps, reg_type, src, "*!", "*!"))
843 dst_term[j++] = *s0++;
848 dst_term[j++] = *s0++;
852 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
853 if (space_split && **map == *CHR_SPACE)
871 dst_term[j++] = '\0';
877 /* gen_regular_rel - generate regular expression from relation
878 * val: border value (inclusive)
879 * islt: 1 if <=; 0 if >=.
881 static void gen_regular_rel (char *dst, int val, int islt)
888 logf (LOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
892 strcpy (dst, "(-[0-9]+|(");
900 strcpy (dst, "([0-9]+|-(");
901 dst_p = strlen (dst);
911 dst_p = strlen (dst);
912 sprintf (numstr, "%d", val);
913 for (w = strlen(numstr); --w >= 0; pos++)
932 strcpy (dst + dst_p, numstr);
933 dst_p = strlen(dst) - pos - 1;
961 for (i = 0; i<pos; i++)
974 /* match everything less than 10^(pos-1) */
976 for (i=1; i<pos; i++)
977 strcat (dst, "[0-9]?");
981 /* match everything greater than 10^pos */
982 for (i = 0; i <= pos; i++)
983 strcat (dst, "[0-9]");
984 strcat (dst, "[0-9]*");
989 void string_rel_add_char (char **term_p, const char *src, int *indx)
991 if (src[*indx] == '\\')
992 *(*term_p)++ = src[(*indx)++];
993 *(*term_p)++ = src[(*indx)++];
997 * > abc ([b-].*|a[c-].*|ab[d-].*|abc.+)
998 * ([^-a].*|a[^-b].*ab[^-c].*|abc.+)
999 * >= abc ([b-].*|a[c-].*|ab[c-].*)
1000 * ([^-a].*|a[^-b].*|ab[c-].*)
1001 * < abc ([-0].*|a[-a].*|ab[-b].*)
1002 * ([^a-].*|a[^b-].*|ab[^c-].*)
1003 * <= abc ([-0].*|a[-a].*|ab[-b].*|abc)
1004 * ([^a-].*|a[^b-].*|ab[^c-].*|abc)
1006 static int string_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1007 const char **term_sub, char *term_dict,
1008 oid_value attributeSet,
1009 int reg_type, int space_split, char *term_dst)
1014 char *term_tmp = term_dict + strlen(term_dict);
1015 char term_component[256];
1017 attr_init (&relation, zapt, 2);
1018 relation_value = attr_find (&relation, NULL);
1020 logf (LOG_DEBUG, "string relation value=%d", relation_value);
1021 switch (relation_value)
1024 if (!term_100 (zh->service->zebra_maps, reg_type,
1025 term_sub, term_component,
1026 space_split, term_dst))
1028 logf (LOG_DEBUG, "Relation <");
1031 for (i = 0; term_component[i]; )
1038 string_rel_add_char (&term_tmp, term_component, &j);
1043 string_rel_add_char (&term_tmp, term_component, &i);
1054 if (!term_100 (zh->service->zebra_maps, reg_type,
1055 term_sub, term_component,
1056 space_split, term_dst))
1058 logf (LOG_DEBUG, "Relation <=");
1061 for (i = 0; term_component[i]; )
1066 string_rel_add_char (&term_tmp, term_component, &j);
1070 string_rel_add_char (&term_tmp, term_component, &i);
1079 for (i = 0; term_component[i]; )
1080 string_rel_add_char (&term_tmp, term_component, &i);
1085 if (!term_100 (zh->service->zebra_maps, reg_type,
1086 term_sub, term_component, space_split, term_dst))
1088 logf (LOG_DEBUG, "Relation >");
1091 for (i = 0; term_component[i];)
1096 string_rel_add_char (&term_tmp, term_component, &j);
1101 string_rel_add_char (&term_tmp, term_component, &i);
1109 for (i = 0; term_component[i];)
1110 string_rel_add_char (&term_tmp, term_component, &i);
1117 if (!term_100 (zh->service->zebra_maps, reg_type, term_sub,
1118 term_component, space_split, term_dst))
1120 logf (LOG_DEBUG, "Relation >=");
1123 for (i = 0; term_component[i];)
1130 string_rel_add_char (&term_tmp, term_component, &j);
1133 if (term_component[i+1])
1137 string_rel_add_char (&term_tmp, term_component, &i);
1141 string_rel_add_char (&term_tmp, term_component, &i);
1153 logf (LOG_DEBUG, "Relation =");
1154 if (!term_100 (zh->service->zebra_maps, reg_type, term_sub,
1155 term_component, space_split, term_dst))
1157 strcat (term_tmp, "(");
1158 strcat (term_tmp, term_component);
1159 strcat (term_tmp, ")");
1164 static int string_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1165 const char **term_sub,
1166 oid_value attributeSet, NMEM stream,
1167 struct grep_info *grep_info,
1168 int reg_type, int complete_flag,
1169 int num_bases, char **basenames,
1172 char term_dict[2*IT_MAX_WORD+4000];
1174 AttrType truncation;
1175 int truncation_value;
1178 oid_value curAttributeSet = attributeSet;
1180 struct rpn_char_map_info rcmi;
1181 int space_split = complete_flag ? 0 : 1;
1183 rpn_char_map_prepare (zh, reg_type, &rcmi);
1184 attr_init (&use, zapt, 1);
1185 use_value = attr_find (&use, &curAttributeSet);
1186 logf (LOG_DEBUG, "string_term, use value %d", use_value);
1187 attr_init (&truncation, zapt, 5);
1188 truncation_value = attr_find (&truncation, NULL);
1189 logf (LOG_DEBUG, "truncation value %d", truncation_value);
1191 if (use_value == -1)
1194 for (base_no = 0; base_no < num_bases; base_no++)
1197 data1_local_attribute *local_attr;
1198 int max_pos, prefix_len = 0;
1201 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
1203 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1204 curAttributeSet, use_value, r);
1208 sprintf (val_str, "%d", use_value);
1210 zh->errString = nmem_strdup (stream, val_str);
1215 struct oident oident;
1217 oident.proto = PROTO_Z3950;
1218 oident.oclass = CLASS_ATTSET;
1219 oident.value = curAttributeSet;
1220 oid_ent_to_oid (&oident, oid);
1223 zh->errString = nmem_strdup (stream, oident.desc);
1227 if (zebraExplain_curDatabase (zh->service->zei, basenames[base_no]))
1229 zh->errCode = 109; /* Database unavailable */
1230 zh->errString = basenames[base_no];
1233 for (local_attr = attp.local_attributes; local_attr;
1234 local_attr = local_attr->next)
1240 ord = zebraExplain_lookupSU (zh->service->zei, attp.attset_ordinal,
1245 term_dict[prefix_len++] = '|';
1247 term_dict[prefix_len++] = '(';
1249 ord_len = key_SU_encode (ord, ord_buf);
1250 for (i = 0; i<ord_len; i++)
1252 term_dict[prefix_len++] = 1;
1253 term_dict[prefix_len++] = ord_buf[i];
1259 sprintf (val_str, "%d", use_value);
1261 zh->errString = nmem_strdup (stream, val_str);
1264 term_dict[prefix_len++] = ')';
1265 term_dict[prefix_len++] = 1;
1266 term_dict[prefix_len++] = reg_type;
1267 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1268 term_dict[prefix_len] = '\0';
1270 switch (truncation_value)
1272 case -1: /* not specified */
1273 case 100: /* do not truncate */
1274 if (!string_relation (zh, zapt, &termp, term_dict,
1276 reg_type, space_split, term_dst))
1278 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_dict+prefix_len);
1279 r = dict_lookup_grep (zh->service->dict, term_dict, 0,
1280 grep_info, &max_pos, 0, grep_handle);
1282 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1284 case 1: /* right truncation */
1285 term_dict[j++] = '(';
1286 if (!term_100 (zh->service->zebra_maps, reg_type,
1287 &termp, term_dict + j, space_split, term_dst))
1289 strcat (term_dict, ".*)");
1290 dict_lookup_grep (zh->service->dict, term_dict, 0, grep_info,
1291 &max_pos, 0, grep_handle);
1293 case 2: /* keft truncation */
1294 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1295 if (!term_100 (zh->service->zebra_maps, reg_type,
1296 &termp, term_dict + j, space_split, term_dst))
1298 strcat (term_dict, ")");
1299 dict_lookup_grep (zh->service->dict, term_dict, 0, grep_info,
1300 &max_pos, 0, grep_handle);
1302 case 3: /* left&right truncation */
1303 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1304 if (!term_100 (zh->service->zebra_maps, reg_type,
1305 &termp, term_dict + j, space_split, term_dst))
1307 strcat (term_dict, ".*)");
1308 dict_lookup_grep (zh->service->dict, term_dict, 0, grep_info,
1309 &max_pos, 0, grep_handle);
1313 case 101: /* process # in term */
1314 term_dict[j++] = '(';
1315 if (!term_101 (zh->service->zebra_maps, reg_type,
1316 &termp, term_dict + j, space_split, term_dst))
1318 strcat (term_dict, ")");
1319 r = dict_lookup_grep (zh->service->dict, term_dict, 0, grep_info,
1320 &max_pos, 0, grep_handle);
1322 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d", r);
1324 case 102: /* Regexp-1 */
1325 term_dict[j++] = '(';
1326 if (!term_102 (zh->service->zebra_maps, reg_type,
1327 &termp, term_dict + j, space_split, term_dst))
1329 strcat (term_dict, ")");
1330 logf (LOG_DEBUG, "Regexp-1 tolerance=%d", r);
1331 r = dict_lookup_grep (zh->service->dict, term_dict, 0, grep_info,
1332 &max_pos, 0, grep_handle);
1334 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
1337 case 103: /* Regexp-2 */
1339 term_dict[j++] = '(';
1340 if (!term_103 (zh->service->zebra_maps, reg_type,
1341 &termp, term_dict + j, &r, space_split, term_dst))
1343 strcat (term_dict, ")");
1344 logf (LOG_DEBUG, "Regexp-2 tolerance=%d", r);
1345 r = dict_lookup_grep (zh->service->dict, term_dict, r, grep_info,
1346 &max_pos, 2, grep_handle);
1348 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
1351 case 104: /* process # and ! in term */
1352 term_dict[j++] = '(';
1353 if (!term_104 (zh->service->zebra_maps, reg_type,
1354 &termp, term_dict + j, space_split, term_dst))
1356 strcat (term_dict, ")");
1357 r = dict_lookup_grep (zh->service->dict, term_dict, 0, grep_info,
1358 &max_pos, 0, grep_handle);
1360 logf (LOG_WARN, "dict_lookup_grep err, trunc=#/!: %d", r);
1362 case 105: /* process * and ! in term */
1363 term_dict[j++] = '(';
1364 if (!term_105 (zh->service->zebra_maps, reg_type,
1365 &termp, term_dict + j, space_split, term_dst, 1))
1367 strcat (term_dict, ")");
1368 r = dict_lookup_grep (zh->service->dict, term_dict, 0, grep_info,
1369 &max_pos, 0, grep_handle);
1371 logf (LOG_WARN, "dict_lookup_grep err, trunc=*/!: %d", r);
1373 case 106: /* process * and ! in term */
1374 term_dict[j++] = '(';
1375 if (!term_105 (zh->service->zebra_maps, reg_type,
1376 &termp, term_dict + j, space_split, term_dst, 0))
1378 strcat (term_dict, ")");
1379 r = dict_lookup_grep (zh->service->dict, term_dict, 0, grep_info,
1380 &max_pos, 0, grep_handle);
1382 logf (LOG_WARN, "dict_lookup_grep err, trunc=*/!: %d", r);
1387 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1391 static void trans_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1395 Z_Term *term = zapt->term;
1397 sizez = term->u.general->len;
1398 if (sizez > IT_MAX_WORD-1)
1399 sizez = IT_MAX_WORD-1;
1400 memcpy (termz, term->u.general->buf, sizez);
1401 termz[sizez] = '\0';
1404 static void trans_scan_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1405 char *termz, int reg_type)
1407 Z_Term *term = zapt->term;
1409 const char *cp = (const char *) term->u.general->buf;
1410 const char *cp_end = cp + term->u.general->len;
1413 const char *space_map = NULL;
1416 while ((len = (cp_end - cp)) > 0)
1418 map = zebra_maps_input (zh->service->zebra_maps, reg_type, &cp, len);
1419 if (**map == *CHR_SPACE)
1424 for (src = space_map; *src; src++)
1427 for (src = *map; *src; src++)
1434 static RSET rpn_prox (ZebraHandle zh, RSET *rset, int rset_no,
1435 int ordered, int exclusion, int relation, int distance)
1440 struct it_key **buf;
1442 char prox_term[1024];
1443 int length_prox_term = 0;
1444 int min_nn = 10000000;
1446 const char *flags = NULL;
1448 rsfd = (RSFD *) xmalloc (sizeof(*rsfd)*rset_no);
1449 more = (int *) xmalloc (sizeof(*more)*rset_no);
1450 buf = (struct it_key **) xmalloc (sizeof(*buf)*rset_no);
1453 for (i = 0; i<rset_no; i++)
1456 for (j = 0; j<rset[i]->no_rset_terms; j++)
1458 const char *nflags = rset[i]->rset_terms[j]->flags;
1459 char *term = rset[i]->rset_terms[j]->name;
1460 int lterm = strlen(term);
1461 if (lterm + length_prox_term < sizeof(prox_term)-1)
1463 if (length_prox_term)
1464 prox_term[length_prox_term++] = ' ';
1465 strcpy (prox_term + length_prox_term, term);
1466 length_prox_term += lterm;
1468 if (min_nn > rset[i]->rset_terms[j]->nn)
1469 min_nn = rset[i]->rset_terms[j]->nn;
1473 for (i = 0; i<rset_no; i++)
1478 for (i = 0; i<rset_no; i++)
1480 buf[i] = (struct it_key *) xmalloc (sizeof(**buf));
1481 rsfd[i] = rset_open (rset[i], RSETF_READ);
1482 if (!(more[i] = rset_read (rset[i], rsfd[i], buf[i], &term_index)))
1487 /* at least one is empty ... return null set */
1488 rset_null_parms parms;
1490 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1492 parms.rset_term->nn = 0;
1493 result = rset_create (rset_kind_null, &parms);
1495 else if (ordered && relation == 3 && exclusion == 0 && distance == 1)
1497 /* special proximity case = phrase search ... */
1498 rset_temp_parms parms;
1501 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1503 parms.rset_term->nn = min_nn;
1504 parms.key_size = sizeof (struct it_key);
1505 parms.temp_path = res_get (zh->service->res, "setTmpDir");
1506 result = rset_create (rset_kind_temp, &parms);
1507 rsfd_result = rset_open (result, RSETF_WRITE);
1511 for (i = 1; i<rset_no; i++)
1520 cmp = key_compare_it (buf[i], buf[i-1]);
1523 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1524 buf[i-1], &term_index);
1529 if (buf[i-1]->seqno+1 != buf[i]->seqno)
1531 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1532 buf[i-1], &term_index);
1538 more[i] = rset_read (rset[i], rsfd[i], buf[i],
1545 rset_write (result, rsfd_result, buf[0]);
1546 more[0] = rset_read (*rset, *rsfd, *buf, &term_index);
1549 rset_close (result, rsfd_result);
1551 else if (rset_no == 2)
1553 /* generic proximity case (two input sets only) ... */
1554 rset_temp_parms parms;
1557 logf (LOG_LOG, "generic prox, dist = %d, relation = %d, ordered =%d, exclusion=%d",
1558 distance, relation, ordered, exclusion);
1559 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1561 parms.rset_term->nn = min_nn;
1562 parms.key_size = sizeof (struct it_key);
1563 parms.temp_path = res_get (zh->service->res, "setTmpDir");
1564 result = rset_create (rset_kind_temp, &parms);
1565 rsfd_result = rset_open (result, RSETF_WRITE);
1567 while (more[0] && more[1])
1569 int cmp = key_compare_it (buf[0], buf[1]);
1571 more[0] = rset_read (rset[0], rsfd[0], buf[0], &term_index);
1573 more[1] = rset_read (rset[1], rsfd[1], buf[1], &term_index);
1576 int sysno = buf[0]->sysno;
1580 seqno[n++] = buf[0]->seqno;
1581 while ((more[0] = rset_read (rset[0], rsfd[0], buf[0],
1583 sysno == buf[0]->sysno)
1585 seqno[n++] = buf[0]->seqno;
1588 for (i = 0; i<n; i++)
1590 int diff = buf[1]->seqno - seqno[i];
1591 int excl = exclusion;
1592 if (!ordered && diff < 0)
1597 if (diff < distance && diff >= 0)
1601 if (diff <= distance && diff >= 0)
1605 if (diff == distance && diff >= 0)
1609 if (diff >= distance && diff >= 0)
1613 if (diff > distance && diff >= 0)
1617 if (diff != distance && diff >= 0)
1623 rset_write (result, rsfd_result, buf[1]);
1627 } while ((more[1] = rset_read (rset[1], rsfd[1], buf[1],
1629 sysno == buf[1]->sysno);
1632 rset_close (result, rsfd_result);
1636 rset_null_parms parms;
1638 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1640 parms.rset_term->nn = 0;
1641 result = rset_create (rset_kind_null, &parms);
1643 for (i = 0; i<rset_no; i++)
1646 rset_close (rset[i], rsfd[i]);
1656 char *normalize_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1657 const char *termz, NMEM stream, unsigned reg_id)
1660 AttrType truncation;
1661 int truncation_value;
1664 attr_init (&truncation, zapt, 5);
1665 truncation_value = attr_find (&truncation, NULL);
1667 switch (truncation_value)
1687 wrbuf = zebra_replace(zh->service->zebra_maps, reg_id, ex_list,
1688 termz, strlen(termz));
1690 return nmem_strdup(stream, termz);
1693 char *buf = (char*) nmem_malloc (stream, wrbuf_len(wrbuf)+1);
1694 memcpy (buf, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1695 buf[wrbuf_len(wrbuf)] = '\0';
1700 static int grep_info_prepare (ZebraHandle zh,
1701 Z_AttributesPlusTerm *zapt,
1702 struct grep_info *grep_info,
1707 int termset_value_numeric;
1708 const char *termset_value_string;
1711 grep_info->term_no = 0;
1713 grep_info->isam_p_size = 0;
1714 grep_info->isam_p_buf = NULL;
1716 grep_info->reg_type = reg_type;
1717 grep_info->termset = 0;
1719 attr_init (&termset, zapt, 8);
1720 termset_value_numeric =
1721 attr_find_ex (&termset, NULL, &termset_value_string);
1722 if (termset_value_numeric != -1)
1725 const char *termset_name = 0;
1726 if (termset_value_numeric != -2)
1729 sprintf (resname, "%d", termset_value_numeric);
1730 termset_name = resname;
1733 termset_name = termset_value_string;
1734 logf (LOG_LOG, "creating termset set %s", termset_name);
1735 grep_info->termset = resultSetAdd (zh, termset_name, 1);
1736 if (!grep_info->termset)
1739 zh->errString = nmem_strdup (stream, termset_name);
1747 static RSET rpn_search_APT_phrase (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;
1763 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1767 logf (LOG_DEBUG, "APT_phrase termp=%s", termp);
1768 grep_info.isam_p_indx = 0;
1769 r = string_term (zh, zapt, &termp, attributeSet, stream, &grep_info,
1770 reg_type, complete_flag, num_bases, basenames,
1774 logf (LOG_DEBUG, "term: %s", term_dst);
1775 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1776 grep_info.isam_p_indx, term_dst,
1777 strlen(term_dst), rank_type);
1778 assert (rset[rset_no]);
1779 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1783 xfree(grep_info.term_no);
1785 xfree (grep_info.isam_p_buf);
1788 rset_null_parms parms;
1790 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1791 return rset_create (rset_kind_null, &parms);
1793 else if (rset_no == 1)
1795 result = rpn_prox (zh, rset, rset_no, 1, 0, 3, 1);
1796 for (i = 0; i<rset_no; i++)
1797 rset_delete (rset[i]);
1801 static RSET rpn_search_APT_or_list (ZebraHandle zh,
1802 Z_AttributesPlusTerm *zapt,
1803 const char *termz_org,
1804 oid_value attributeSet,
1806 int reg_type, int complete_flag,
1807 const char *rank_type,
1808 int num_bases, char **basenames)
1810 char term_dst[IT_MAX_WORD+1];
1811 RSET rset[60], result;
1812 int i, r, rset_no = 0;
1813 struct grep_info grep_info;
1814 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1815 const char *termp = termz;
1817 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1821 logf (LOG_DEBUG, "APT_or_list termp=%s", termp);
1822 grep_info.isam_p_indx = 0;
1823 r = string_term (zh, zapt, &termp, attributeSet, stream, &grep_info,
1824 reg_type, complete_flag, num_bases, basenames,
1828 logf (LOG_DEBUG, "term: %s", term_dst);
1829 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1830 grep_info.isam_p_indx, term_dst,
1831 strlen(term_dst), rank_type);
1832 assert (rset[rset_no]);
1833 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1837 xfree(grep_info.term_no);
1839 xfree (grep_info.isam_p_buf);
1842 rset_null_parms parms;
1844 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1845 return rset_create (rset_kind_null, &parms);
1848 for (i = 1; i<rset_no; i++)
1850 rset_bool_parms bool_parms;
1852 bool_parms.rset_l = result;
1853 bool_parms.rset_r = rset[i];
1854 bool_parms.key_size = sizeof(struct it_key);
1855 bool_parms.cmp = key_compare_it;
1856 result = rset_create (rset_kind_or, &bool_parms);
1861 static RSET rpn_search_APT_and_list (ZebraHandle zh,
1862 Z_AttributesPlusTerm *zapt,
1863 const char *termz_org,
1864 oid_value attributeSet,
1866 int reg_type, int complete_flag,
1867 const char *rank_type,
1868 int num_bases, char **basenames)
1870 char term_dst[IT_MAX_WORD+1];
1871 RSET rset[60], result;
1872 int i, r, rset_no = 0;
1873 struct grep_info grep_info;
1874 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1875 const char *termp = termz;
1877 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1881 logf (LOG_DEBUG, "APT_and_list termp=%s", termp);
1882 grep_info.isam_p_indx = 0;
1883 r = string_term (zh, zapt, &termp, attributeSet, stream, &grep_info,
1884 reg_type, complete_flag, num_bases, basenames,
1888 logf (LOG_DEBUG, "term: %s", term_dst);
1889 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1890 grep_info.isam_p_indx, term_dst,
1891 strlen(term_dst), rank_type);
1892 assert (rset[rset_no]);
1893 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1897 xfree(grep_info.term_no);
1899 xfree (grep_info.isam_p_buf);
1902 rset_null_parms parms;
1904 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1905 return rset_create (rset_kind_null, &parms);
1908 for (i = 1; i<rset_no; i++)
1910 rset_bool_parms bool_parms;
1912 bool_parms.rset_l = result;
1913 bool_parms.rset_r = rset[i];
1914 bool_parms.key_size = sizeof(struct it_key);
1915 bool_parms.cmp = key_compare_it;
1916 result = rset_create (rset_kind_and, &bool_parms);
1921 static int numeric_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1922 const char **term_sub,
1924 oid_value attributeSet,
1925 struct grep_info *grep_info,
1934 char *term_tmp = term_dict + strlen(term_dict);
1936 attr_init (&relation, zapt, 2);
1937 relation_value = attr_find (&relation, NULL);
1939 logf (LOG_DEBUG, "numeric relation value=%d", relation_value);
1941 if (!term_100 (zh->service->zebra_maps, reg_type, term_sub, term_tmp, 1,
1944 term_value = atoi (term_tmp);
1945 switch (relation_value)
1948 logf (LOG_DEBUG, "Relation <");
1949 gen_regular_rel (term_tmp, term_value-1, 1);
1952 logf (LOG_DEBUG, "Relation <=");
1953 gen_regular_rel (term_tmp, term_value, 1);
1956 logf (LOG_DEBUG, "Relation >=");
1957 gen_regular_rel (term_tmp, term_value, 0);
1960 logf (LOG_DEBUG, "Relation >");
1961 gen_regular_rel (term_tmp, term_value+1, 0);
1965 logf (LOG_DEBUG, "Relation =");
1966 sprintf (term_tmp, "(0*%d)", term_value);
1968 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_tmp);
1969 r = dict_lookup_grep (zh->service->dict, term_dict, 0, grep_info, max_pos,
1972 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1973 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1977 static int numeric_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1978 const char **term_sub,
1979 oid_value attributeSet, struct grep_info *grep_info,
1980 int reg_type, int complete_flag,
1981 int num_bases, char **basenames,
1984 char term_dict[2*IT_MAX_WORD+2];
1988 oid_value curAttributeSet = attributeSet;
1990 struct rpn_char_map_info rcmi;
1992 rpn_char_map_prepare (zh, reg_type, &rcmi);
1993 attr_init (&use, zapt, 1);
1994 use_value = attr_find (&use, &curAttributeSet);
1995 logf (LOG_DEBUG, "numeric_term, use value %d", use_value);
1997 if (use_value == -1)
2000 for (base_no = 0; base_no < num_bases; base_no++)
2003 data1_local_attribute *local_attr;
2004 int max_pos, prefix_len = 0;
2007 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
2009 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
2010 curAttributeSet, use_value, r);
2017 if (zebraExplain_curDatabase (zh->service->zei, basenames[base_no]))
2019 zh->errCode = 109; /* Database unavailable */
2020 zh->errString = basenames[base_no];
2023 for (local_attr = attp.local_attributes; local_attr;
2024 local_attr = local_attr->next)
2030 ord = zebraExplain_lookupSU (zh->service->zei, attp.attset_ordinal,
2035 term_dict[prefix_len++] = '|';
2037 term_dict[prefix_len++] = '(';
2039 ord_len = key_SU_encode (ord, ord_buf);
2040 for (i = 0; i<ord_len; i++)
2042 term_dict[prefix_len++] = 1;
2043 term_dict[prefix_len++] = ord_buf[i];
2051 term_dict[prefix_len++] = ')';
2052 term_dict[prefix_len++] = 1;
2053 term_dict[prefix_len++] = reg_type;
2054 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
2055 term_dict[prefix_len] = '\0';
2056 if (!numeric_relation (zh, zapt, &termp, term_dict,
2057 attributeSet, grep_info, &max_pos, reg_type,
2062 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
2066 static RSET rpn_search_APT_numeric (ZebraHandle zh,
2067 Z_AttributesPlusTerm *zapt,
2069 oid_value attributeSet,
2071 int reg_type, int complete_flag,
2072 const char *rank_type,
2073 int num_bases, char **basenames)
2075 char term_dst[IT_MAX_WORD+1];
2076 const char *termp = termz;
2077 RSET rset[60], result;
2078 int i, r, rset_no = 0;
2079 struct grep_info grep_info;
2081 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
2085 logf (LOG_DEBUG, "APT_numeric termp=%s", termp);
2086 grep_info.isam_p_indx = 0;
2087 r = numeric_term (zh, zapt, &termp, attributeSet, &grep_info,
2088 reg_type, complete_flag, num_bases, basenames,
2092 logf (LOG_DEBUG, "term: %s", term_dst);
2093 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
2094 grep_info.isam_p_indx, term_dst,
2095 strlen(term_dst), rank_type);
2096 assert (rset[rset_no]);
2097 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
2101 xfree(grep_info.term_no);
2103 xfree (grep_info.isam_p_buf);
2106 rset_null_parms parms;
2108 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
2109 return rset_create (rset_kind_null, &parms);
2112 for (i = 1; i<rset_no; i++)
2114 rset_bool_parms bool_parms;
2116 bool_parms.rset_l = result;
2117 bool_parms.rset_r = rset[i];
2118 bool_parms.key_size = sizeof(struct it_key);
2119 bool_parms.cmp = key_compare_it;
2120 result = rset_create (rset_kind_and, &bool_parms);
2125 static RSET rpn_search_APT_local (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2127 oid_value attributeSet,
2129 const char *rank_type)
2134 rset_temp_parms parms;
2136 parms.rset_term = rset_term_create (termz, -1, rank_type);
2137 parms.key_size = sizeof (struct it_key);
2138 parms.temp_path = res_get (zh->service->res, "setTmpDir");
2139 result = rset_create (rset_kind_temp, &parms);
2140 rsfd = rset_open (result, RSETF_WRITE);
2142 key.sysno = atoi (termz);
2146 rset_write (result, rsfd, &key);
2147 rset_close (result, rsfd);
2151 static RSET rpn_sort_spec (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2152 oid_value attributeSet, NMEM stream,
2153 Z_SortKeySpecList *sort_sequence,
2154 const char *rank_type)
2156 rset_null_parms parms;
2158 int sort_relation_value;
2159 AttrType sort_relation_type;
2164 Z_AttributeElement *ae;
2168 attr_init (&sort_relation_type, zapt, 7);
2169 sort_relation_value = attr_find (&sort_relation_type, &attributeSet);
2171 attr_init (&use_type, zapt, 1);
2172 use_value = attr_find (&use_type, &attributeSet);
2174 if (!sort_sequence->specs)
2176 sort_sequence->num_specs = 10;
2177 sort_sequence->specs = (Z_SortKeySpec **)
2178 nmem_malloc (stream, sort_sequence->num_specs *
2179 sizeof(*sort_sequence->specs));
2180 for (i = 0; i<sort_sequence->num_specs; i++)
2181 sort_sequence->specs[i] = 0;
2183 if (zapt->term->which != Z_Term_general)
2186 i = atoi_n ((char *) zapt->term->u.general->buf,
2187 zapt->term->u.general->len);
2188 if (i >= sort_sequence->num_specs)
2191 oe.proto = PROTO_Z3950;
2192 oe.oclass = CLASS_ATTSET;
2193 oe.value = attributeSet;
2194 if (!oid_ent_to_oid (&oe, oid))
2197 sks = (Z_SortKeySpec *) nmem_malloc (stream, sizeof(*sks));
2198 sks->sortElement = (Z_SortElement *)
2199 nmem_malloc (stream, sizeof(*sks->sortElement));
2200 sks->sortElement->which = Z_SortElement_generic;
2201 sk = sks->sortElement->u.generic = (Z_SortKey *)
2202 nmem_malloc (stream, sizeof(*sk));
2203 sk->which = Z_SortKey_sortAttributes;
2204 sk->u.sortAttributes = (Z_SortAttributes *)
2205 nmem_malloc (stream, sizeof(*sk->u.sortAttributes));
2207 sk->u.sortAttributes->id = oid;
2208 sk->u.sortAttributes->list = (Z_AttributeList *)
2209 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list));
2210 sk->u.sortAttributes->list->num_attributes = 1;
2211 sk->u.sortAttributes->list->attributes = (Z_AttributeElement **)
2212 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list->attributes));
2213 ae = *sk->u.sortAttributes->list->attributes = (Z_AttributeElement *)
2214 nmem_malloc (stream, sizeof(**sk->u.sortAttributes->list->attributes));
2215 ae->attributeSet = 0;
2216 ae->attributeType = (int *)
2217 nmem_malloc (stream, sizeof(*ae->attributeType));
2218 *ae->attributeType = 1;
2219 ae->which = Z_AttributeValue_numeric;
2220 ae->value.numeric = (int *)
2221 nmem_malloc (stream, sizeof(*ae->value.numeric));
2222 *ae->value.numeric = use_value;
2224 sks->sortRelation = (int *)
2225 nmem_malloc (stream, sizeof(*sks->sortRelation));
2226 if (sort_relation_value == 1)
2227 *sks->sortRelation = Z_SortRelation_ascending;
2228 else if (sort_relation_value == 2)
2229 *sks->sortRelation = Z_SortRelation_descending;
2231 *sks->sortRelation = Z_SortRelation_ascending;
2233 sks->caseSensitivity = (int *)
2234 nmem_malloc (stream, sizeof(*sks->caseSensitivity));
2235 *sks->caseSensitivity = 0;
2238 sks->which = Z_SortKeySpec_null;
2239 sks->u.null = odr_nullval ();
2241 sks->missingValueAction = 0;
2244 sort_sequence->specs[i] = sks;
2246 parms.rset_term = rset_term_create ("", -1, rank_type);
2247 return rset_create (rset_kind_null, &parms);
2251 static RSET rpn_search_APT (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2252 oid_value attributeSet, NMEM stream,
2253 Z_SortKeySpecList *sort_sequence,
2254 int num_bases, char **basenames)
2257 char *search_type = NULL;
2258 char rank_type[128];
2261 char termz[IT_MAX_WORD+1];
2263 zebra_maps_attr (zh->service->zebra_maps, zapt, ®_id, &search_type,
2264 rank_type, &complete_flag, &sort_flag);
2266 logf (LOG_DEBUG, "reg_id=%c", reg_id);
2267 logf (LOG_DEBUG, "complete_flag=%d", complete_flag);
2268 logf (LOG_DEBUG, "search_type=%s", search_type);
2269 logf (LOG_DEBUG, "rank_type=%s", rank_type);
2271 if (zapt->term->which != Z_Term_general)
2276 trans_term (zh, zapt, termz);
2279 return rpn_sort_spec (zh, zapt, attributeSet, stream, sort_sequence,
2282 if (!strcmp (search_type, "phrase"))
2284 return rpn_search_APT_phrase (zh, zapt, termz, attributeSet, stream,
2285 reg_id, complete_flag, rank_type,
2286 num_bases, basenames);
2288 else if (!strcmp (search_type, "and-list"))
2290 return rpn_search_APT_and_list (zh, zapt, termz, attributeSet, stream,
2291 reg_id, complete_flag, rank_type,
2292 num_bases, basenames);
2294 else if (!strcmp (search_type, "or-list"))
2296 return rpn_search_APT_or_list (zh, zapt, termz, attributeSet, stream,
2297 reg_id, complete_flag, rank_type,
2298 num_bases, basenames);
2300 else if (!strcmp (search_type, "local"))
2302 return rpn_search_APT_local (zh, zapt, termz, attributeSet, stream,
2305 else if (!strcmp (search_type, "numeric"))
2307 return rpn_search_APT_numeric (zh, zapt, termz, attributeSet, stream,
2308 reg_id, complete_flag, rank_type,
2309 num_bases, basenames);
2315 static RSET rpn_search_structure (ZebraHandle zh, Z_RPNStructure *zs,
2316 oid_value attributeSet, NMEM stream,
2317 Z_SortKeySpecList *sort_sequence,
2318 int num_bases, char **basenames)
2321 if (zs->which == Z_RPNStructure_complex)
2323 Z_Operator *zop = zs->u.complex->roperator;
2324 rset_bool_parms bool_parms;
2326 bool_parms.rset_l = rpn_search_structure (zh, zs->u.complex->s1,
2327 attributeSet, stream,
2329 num_bases, basenames);
2330 if (bool_parms.rset_l == NULL)
2332 bool_parms.rset_r = rpn_search_structure (zh, zs->u.complex->s2,
2333 attributeSet, stream,
2335 num_bases, basenames);
2336 if (bool_parms.rset_r == NULL)
2338 rset_delete (bool_parms.rset_l);
2341 bool_parms.key_size = sizeof(struct it_key);
2342 bool_parms.cmp = key_compare_it;
2346 case Z_Operator_and:
2347 r = rset_create (rset_kind_and, &bool_parms);
2350 r = rset_create (rset_kind_or, &bool_parms);
2352 case Z_Operator_and_not:
2353 r = rset_create (rset_kind_not, &bool_parms);
2355 case Z_Operator_prox:
2357 if (zop->u.prox->which != Z_ProximityOperator_known)
2363 if (zop->u.prox->which != Z_ProxCode_known)
2371 if (*zop->u.prox->u.known != Z_ProxUnit_word)
2373 char *val = (char *) nmem_malloc (stream, 16);
2375 zh->errString = val;
2376 sprintf (val, "%d", *zop->u.prox->u.known);
2380 if (*zop->u.prox->proximityUnitCode != Z_ProxUnit_word)
2382 char *val = (char *) nmem_malloc (stream, 16);
2384 zh->errString = val;
2385 sprintf (val, "%d", *zop->u.prox->proximityUnitCode);
2393 rsets[0] = bool_parms.rset_l;
2394 rsets[1] = bool_parms.rset_r;
2396 r = rpn_prox (zh, rsets, 2,
2397 *zop->u.prox->ordered,
2398 (!zop->u.prox->exclusion ? 0 :
2399 *zop->u.prox->exclusion),
2400 *zop->u.prox->relationType,
2401 *zop->u.prox->distance);
2402 rset_delete (rsets[0]);
2403 rset_delete (rsets[1]);
2411 else if (zs->which == Z_RPNStructure_simple)
2413 if (zs->u.simple->which == Z_Operand_APT)
2415 logf (LOG_DEBUG, "rpn_search_APT");
2416 r = rpn_search_APT (zh, zs->u.simple->u.attributesPlusTerm,
2417 attributeSet, stream, sort_sequence,
2418 num_bases, basenames);
2420 else if (zs->u.simple->which == Z_Operand_resultSetId)
2422 logf (LOG_DEBUG, "rpn_search_ref");
2423 r = resultSetRef (zh, zs->u.simple->u.resultSetId);
2426 r = rset_create (rset_kind_null, NULL);
2429 nmem_strdup (stream, zs->u.simple->u.resultSetId);
2448 RSET rpn_search (ZebraHandle zh, NMEM nmem,
2449 Z_RPNQuery *rpn, int num_bases, char **basenames,
2450 const char *setname,
2455 oid_value attributeSet;
2456 Z_SortKeySpecList *sort_sequence;
2460 zh->errString = NULL;
2463 sort_sequence = (Z_SortKeySpecList *)
2464 nmem_malloc (nmem, sizeof(*sort_sequence));
2465 sort_sequence->num_specs = 10;
2466 sort_sequence->specs = (Z_SortKeySpec **)
2467 nmem_malloc (nmem, sort_sequence->num_specs *
2468 sizeof(*sort_sequence->specs));
2469 for (i = 0; i<sort_sequence->num_specs; i++)
2470 sort_sequence->specs[i] = 0;
2472 attrset = oid_getentbyoid (rpn->attributeSetId);
2473 attributeSet = attrset->value;
2474 rset = rpn_search_structure (zh, rpn->RPNStructure, attributeSet,
2475 nmem, sort_sequence, num_bases, basenames);
2480 logf (LOG_DEBUG, "search error: %d", zh->errCode);
2482 for (i = 0; sort_sequence->specs[i]; i++)
2484 sort_sequence->num_specs = i;
2486 resultSetRank (zh, sset, rset);
2489 logf (LOG_DEBUG, "resultSetSortSingle in rpn_search");
2490 resultSetSortSingle (zh, nmem, sset, rset,
2491 sort_sequence, &sort_status);
2494 logf (LOG_DEBUG, "resultSetSortSingle status = %d", zh->errCode);
2500 struct scan_info_entry {
2506 struct scan_info_entry *list;
2512 static int scan_handle (char *name, const char *info, int pos, void *client)
2514 int len_prefix, idx;
2515 struct scan_info *scan_info = (struct scan_info *) client;
2517 len_prefix = strlen(scan_info->prefix);
2518 if (memcmp (name, scan_info->prefix, len_prefix))
2520 if (pos > 0) idx = scan_info->after - pos + scan_info->before;
2523 scan_info->list[idx].term = (char *)
2524 odr_malloc (scan_info->odr, strlen(name + len_prefix)+1);
2525 strcpy (scan_info->list[idx].term, name + len_prefix);
2526 assert (*info == sizeof(ISAMS_P));
2527 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAMS_P));
2531 static void scan_term_untrans (ZebraHandle zh, NMEM stream, int reg_type,
2532 char **dst, const char *src)
2534 char term_dst[1024];
2536 term_untrans (zh, reg_type, term_dst, src);
2538 *dst = (char *) nmem_malloc (stream, strlen(term_dst)+1);
2539 strcpy (*dst, term_dst);
2542 static void count_set (RSET r, int *count)
2550 logf (LOG_DEBUG, "count_set");
2553 rfd = rset_open (r, RSETF_READ);
2554 while (rset_read (r, rfd, &key, &term_index))
2556 if (key.sysno != psysno)
2563 rset_close (r, rfd);
2564 logf (LOG_DEBUG, "%d keys, %d records", kno, *count);
2567 void rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
2568 oid_value attributeset,
2569 int num_bases, char **basenames,
2570 int *position, int *num_entries, ZebraScanEntry **list,
2574 int pos = *position;
2575 int num = *num_entries;
2579 char termz[IT_MAX_WORD+20];
2582 struct scan_info *scan_info_array;
2583 ZebraScanEntry *glist;
2584 int ords[32], ord_no = 0;
2588 char *search_type = NULL;
2589 char rank_type[128];
2594 if (attributeset == VAL_NONE)
2595 attributeset = VAL_BIB1;
2597 logf (LOG_DEBUG, "position = %d, num = %d", pos, num);
2599 attr_init (&use, zapt, 1);
2600 use_value = attr_find (&use, &attributeset);
2602 if (zebra_maps_attr (zh->service->zebra_maps, zapt, ®_id, &search_type,
2603 rank_type, &complete_flag, &sort_flag))
2610 if (use_value == -1)
2612 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
2616 data1_local_attribute *local_attr;
2618 if ((r=att_getentbyatt (zh, &attp, attributeset, use_value)))
2620 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
2621 attributeset, use_value);
2629 if (zebraExplain_curDatabase (zh->service->zei, basenames[base_no]))
2631 zh->errString = basenames[base_no];
2632 zh->errCode = 109; /* Database unavailable */
2636 for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
2637 local_attr = local_attr->next)
2641 ord = zebraExplain_lookupSU (zh->service->zei, attp.attset_ordinal,
2644 ords[ord_no++] = ord;
2653 /* prepare dictionary scanning */
2656 scan_info_array = (struct scan_info *)
2657 odr_malloc (stream, ord_no * sizeof(*scan_info_array));
2658 for (i = 0; i < ord_no; i++)
2660 int j, prefix_len = 0;
2661 int before_tmp = before, after_tmp = after;
2662 struct scan_info *scan_info = scan_info_array + i;
2663 struct rpn_char_map_info rcmi;
2665 rpn_char_map_prepare (zh, reg_id, &rcmi);
2667 scan_info->before = before;
2668 scan_info->after = after;
2669 scan_info->odr = stream;
2671 scan_info->list = (struct scan_info_entry *)
2672 odr_malloc (stream, (before+after) * sizeof(*scan_info->list));
2673 for (j = 0; j<before+after; j++)
2674 scan_info->list[j].term = NULL;
2676 prefix_len += key_SU_encode (ords[i], termz + prefix_len);
2677 termz[prefix_len++] = reg_id;
2678 termz[prefix_len] = 0;
2679 strcpy (scan_info->prefix, termz);
2681 trans_scan_term (zh, zapt, termz+prefix_len, reg_id);
2683 dict_scan (zh->service->dict, termz, &before_tmp, &after_tmp,
2684 scan_info, scan_handle);
2686 glist = (ZebraScanEntry *)
2687 odr_malloc (stream, (before+after)*sizeof(*glist));
2689 /* consider terms after main term */
2690 for (i = 0; i < ord_no; i++)
2694 for (i = 0; i<after; i++)
2697 const char *mterm = NULL;
2701 for (j = 0; j < ord_no; j++)
2703 if (ptr[j] < before+after &&
2704 (tst=scan_info_array[j].list[ptr[j]].term) &&
2705 (!mterm || strcmp (tst, mterm) < 0))
2713 scan_term_untrans (zh, stream->mem, reg_id,
2714 &glist[i+before].term, mterm);
2715 rset = rset_trunc (zh, &scan_info_array[j0].list[ptr[j0]].isam_p, 1,
2716 glist[i+before].term, strlen(glist[i+before].term),
2720 for (j = j0+1; j<ord_no; j++)
2722 if (ptr[j] < before+after &&
2723 (tst=scan_info_array[j].list[ptr[j]].term) &&
2724 !strcmp (tst, mterm))
2726 rset_bool_parms bool_parms;
2730 rset_trunc (zh, &scan_info_array[j].list[ptr[j]].isam_p, 1,
2731 glist[i+before].term,
2732 strlen(glist[i+before].term), NULL);
2734 bool_parms.key_size = sizeof(struct it_key);
2735 bool_parms.cmp = key_compare_it;
2736 bool_parms.rset_l = rset;
2737 bool_parms.rset_r = rset2;
2739 rset = rset_create (rset_kind_or, &bool_parms);
2744 count_set (rset, &glist[i+before].occurrences);
2749 *num_entries -= (after-i);
2753 /* consider terms before main term */
2754 for (i = 0; i<ord_no; i++)
2757 for (i = 0; i<before; i++)
2760 const char *mterm = NULL;
2764 for (j = 0; j <ord_no; j++)
2766 if (ptr[j] < before &&
2767 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2768 (!mterm || strcmp (tst, mterm) > 0))
2777 scan_term_untrans (zh, stream->mem, reg_id,
2778 &glist[before-1-i].term, mterm);
2781 (zh, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1,
2782 glist[before-1-i].term, strlen(glist[before-1-i].term),
2787 for (j = j0+1; j<ord_no; j++)
2789 if (ptr[j] < before &&
2790 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2791 !strcmp (tst, mterm))
2793 rset_bool_parms bool_parms;
2796 rset2 = rset_trunc (zh,
2797 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1,
2798 glist[before-1-i].term,
2799 strlen(glist[before-1-i].term), NULL);
2801 bool_parms.key_size = sizeof(struct it_key);
2802 bool_parms.cmp = key_compare_it;
2803 bool_parms.rset_l = rset;
2804 bool_parms.rset_r = rset2;
2806 rset = rset_create (rset_kind_or, &bool_parms);
2811 count_set (rset, &glist[before-1-i].occurrences);
2821 *list = glist + i; /* list is set to first 'real' entry */
2823 logf (LOG_DEBUG, "position = %d, num_entries = %d",
2824 *position, *num_entries);
2826 logf (LOG_DEBUG, "scan error: %d", zh->errCode);