2 * Copyright (C) 1995-2001, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.107 2001-10-15 19:53:43 adam
8 * POSIX thread updates. First work on term sets.
10 * Revision 1.106 2001/04/11 07:58:13 adam
11 * Bug fix: multiple space mapped to one space when using complete subfield.
13 * Revision 1.105 2000/11/08 13:46:59 adam
14 * Fixed scan: server could break if bad attribute/database was selected.
15 * Work on remote update.
17 * Revision 1.104 2000/04/05 09:49:35 adam
18 * On Unix, zebra/z'mbol uses automake.
20 * Revision 1.103 2000/03/20 19:08:36 adam
21 * Added remote record import using Z39.50 extended services and Segment
24 * Revision 1.102 2000/03/15 15:00:31 adam
25 * First work on threaded version.
27 * Revision 1.101 2000/03/02 14:35:03 adam
28 * Fixed proximity handling.
30 * Revision 1.100 1999/12/28 15:48:12 adam
33 * Revision 1.99 1999/12/23 09:03:32 adam
34 * Changed behaviour of trunc=105 so that * is regular .* and ! is regular .
36 * Revision 1.98 1999/11/30 13:48:04 adam
37 * Improved installation. Updated for inclusion of YAZ header files.
39 * Revision 1.97 1999/10/14 14:33:50 adam
40 * Added truncation 5=106.
42 * Revision 1.96 1999/09/23 10:05:05 adam
43 * Implemented structure=105 searching.
45 * Revision 1.95 1999/09/07 07:19:21 adam
46 * Work on character mapping. Implemented replace rules.
48 * Revision 1.94 1999/07/20 13:59:18 adam
49 * Fixed bug that occurred when phrases had 0 hits.
51 * Revision 1.93 1999/06/17 14:38:40 adam
52 * Bug fix: Scan SEGV'ed when getting unknown use attribute.
54 * Revision 1.92 1999/05/26 07:49:13 adam
57 * Revision 1.91 1999/02/02 14:51:13 adam
58 * Updated WIN32 code specific sections. Changed header.
60 * Revision 1.90 1998/11/16 16:03:43 adam
61 * Moved loggin utilities to Yaz. Was implemented in file zlogs.c.
63 * Revision 1.89 1998/11/16 10:11:55 adam
64 * Added addtional info for error 114 - unsupported use attribute.
66 * Revision 1.88 1998/10/18 07:54:52 adam
67 * Additional info added for diagnostics 114 (Unsupported use attribute) and
68 * 121 (Unsupported attribute set).
70 * Revision 1.87 1998/09/28 11:19:12 adam
71 * Fix for Compiled ASN.1.
73 * Revision 1.86 1998/09/22 10:48:20 adam
74 * Minor changes in search API.
76 * Revision 1.85 1998/09/22 10:03:43 adam
77 * Changed result sets to be persistent in the sense that they can
78 * be re-searched if needed.
79 * Fixed memory leak in rsm_or.
81 * Revision 1.84 1998/09/18 12:41:00 adam
82 * Fixed bug with numerical relations.
84 * Revision 1.83 1998/09/02 13:53:19 adam
85 * Extra parameter decode added to search routines to implement
88 * Revision 1.82 1998/06/26 11:16:40 quinn
89 * Added support (un-optimised) for left and left/right truncation
91 * Revision 1.81 1998/06/24 12:16:14 adam
92 * Support for relations on text operands. Open range support in
93 * DFA module (i.e. [-j], [g-]).
95 * Revision 1.80 1998/06/23 15:33:34 adam
96 * Added feature to specify sort criteria in query (type 7 specifies
99 * Revision 1.79 1998/06/22 11:35:09 adam
102 * Revision 1.78 1998/06/08 14:43:17 adam
103 * Added suport for EXPLAIN Proxy servers - added settings databasePath
104 * and explainDatabase to facilitate this. Increased maximum number
105 * of databases and attributes in one register.
107 * Revision 1.77 1998/05/20 10:12:22 adam
108 * Implemented automatic EXPLAIN database maintenance.
109 * Modified Zebra to work with ASN.1 compiled version of YAZ.
111 * Revision 1.76 1998/04/02 14:35:29 adam
112 * First version of Zebra that works with compiled ASN.1.
114 * Revision 1.75 1998/03/05 08:45:13 adam
115 * New result set model and modular ranking system. Moved towards
116 * descent server API. System information stored as "SGML" records.
118 * Revision 1.74 1998/02/10 12:03:06 adam
121 * Revision 1.73 1998/01/29 13:40:11 adam
122 * Better logging for scan service.
124 * Revision 1.72 1998/01/07 13:53:41 adam
125 * Queries using simple ranked operands returns right number of hits.
127 * Revision 1.71 1997/12/18 10:54:24 adam
128 * New method result set method rs_hits that returns the number of
129 * hits in result-set (if known). The ranked result set returns real
130 * number of hits but only when not combined with other operands.
132 * Revision 1.70 1997/10/31 12:34:43 adam
133 * Changed a few log statements.
135 * Revision 1.69 1997/10/29 12:05:02 adam
136 * Server produces diagnostic "Unsupported Attribute Set" when appropriate.
138 * Revision 1.68 1997/10/27 14:33:06 adam
139 * Moved towards generic character mapping depending on "structure"
140 * field in abstract syntax file. Fixed a few memory leaks. Fixed
141 * bug with negative integers when doing searches with relational
144 * Revision 1.67 1997/09/29 09:06:10 adam
145 * Removed one static var in order to make this module thread safe.
147 * Revision 1.66 1997/09/25 14:58:03 adam
150 * Revision 1.65 1997/09/22 12:39:06 adam
151 * Added get_pos method for the ranked result sets.
153 * Revision 1.64 1997/09/18 08:59:20 adam
154 * Extra generic handle for the character mapping routines.
156 * Revision 1.63 1997/09/17 12:19:18 adam
157 * Zebra version corresponds to YAZ version 1.4.
158 * Changed Zebra server so that it doesn't depend on global common_resource.
160 * Revision 1.62 1997/09/05 15:30:09 adam
161 * Changed prototype for chr_map_input - added const.
162 * Added support for C++, headers uses extern "C" for public definitions.
164 * Revision 1.61 1997/02/10 10:21:14 adam
165 * Bug fix: in search terms character (^) wasn't observed.
167 * Revision 1.60 1997/01/31 11:10:34 adam
168 * Bug fix: Leading and trailing white space weren't removed in scan tokens.
170 * Revision 1.59 1997/01/17 11:31:46 adam
171 * Bug fix: complete phrase search didn't work.
173 * Revision 1.58 1996/12/23 15:30:45 adam
174 * Work on truncation.
175 * Bug fix: result sets weren't deleted after server shut down.
177 * Revision 1.57 1996/11/11 13:38:02 adam
178 * Added proximity support in search.
180 * Revision 1.56 1996/11/08 11:10:32 adam
181 * Buffers used during file match got bigger.
182 * Compressed ISAM support everywhere.
183 * Bug fixes regarding masking characters in queries.
184 * Redesigned Regexp-2 queries.
186 * Revision 1.55 1996/11/04 14:07:44 adam
187 * Moved truncation code to trunc.c.
189 * Revision 1.54 1996/10/29 14:09:52 adam
190 * Use of cisam system - enabled if setting isamc is 1.
192 * Revision 1.53 1996/06/26 09:21:43 adam
193 * Bug fix: local attribute set wasn't obeyed in scan.
195 * Revision 1.52 1996/06/17 14:26:20 adam
196 * Function gen_regular_rel changed to handle negative numbers.
198 * Revision 1.51 1996/06/11 10:54:15 quinn
201 * Revision 1.50 1996/06/07 08:51:53 adam
202 * Bug fix: Character mapping was broken (introducued by last revision).
204 * Revision 1.49 1996/06/04 10:18:11 adam
205 * Search/scan uses character mapping module.
207 * Revision 1.48 1996/05/28 15:15:01 adam
208 * Bug fix: Didn't handle unknown database correctly.
210 * Revision 1.47 1996/05/15 18:36:28 adam
211 * Function trans_term transforms unsearchable characters to blanks.
213 * Revision 1.46 1996/05/15 11:57:56 adam
214 * Fixed bug introduced by set/field mapping in search operations.
216 * Revision 1.45 1996/05/14 11:34:00 adam
217 * Scan support in multiple registers/databases.
219 * Revision 1.44 1996/05/14 06:16:44 adam
220 * Compact use/set bytes used in search service.
222 * Revision 1.43 1996/05/09 09:54:43 adam
223 * Server supports maps from one logical attributes to a list of physical
225 * The extraction process doesn't make space consuming 'any' keys.
227 * Revision 1.42 1996/05/09 07:28:56 quinn
228 * Work towards phrases and multiple registers
230 * Revision 1.41 1996/03/20 09:36:43 adam
231 * Function dict_lookup_grep got extra parameter, init_pos, which marks
232 * from which position in pattern approximate pattern matching should occur.
233 * Approximate pattern matching is used in relevance=re-2.
235 * Revision 1.40 1996/02/02 13:44:44 adam
236 * The public dictionary functions simply use char instead of Dict_char
237 * to represent search strings. Dict_char is used internally only.
239 * Revision 1.39 1996/01/03 16:22:13 quinn
240 * operator->roperator
242 * Revision 1.38 1995/12/11 09:12:55 adam
243 * The rec_get function returns NULL if record doesn't exist - will
244 * happen in the server if the result set records have been deleted since
245 * the creation of the set (i.e. the search).
246 * The server saves a result temporarily if it is 'volatile', i.e. the
247 * set is register dependent.
249 * Revision 1.37 1995/12/06 15:05:28 adam
250 * More verbose in count_set.
252 * Revision 1.36 1995/12/06 12:41:27 adam
253 * New command 'stat' for the index program.
254 * Filenames can be read from stdin by specifying '-'.
255 * Bug fix/enhancement of the transformation from terms to regular
256 * expressons in the search engine.
258 * Revision 1.35 1995/11/27 09:29:00 adam
259 * Bug fixes regarding conversion to regular expressions.
261 * Revision 1.34 1995/11/16 17:00:56 adam
262 * Better logging of rpn query.
264 * Revision 1.33 1995/11/01 13:58:28 quinn
265 * Moving data1 to yaz/retrieval
267 * Revision 1.32 1995/10/27 14:00:11 adam
268 * Implemented detection of database availability.
270 * Revision 1.31 1995/10/17 18:02:10 adam
271 * New feature: databases. Implemented as prefix to words in dictionary.
273 * Revision 1.30 1995/10/16 09:32:38 adam
274 * More work on relational op.
276 * Revision 1.29 1995/10/13 16:01:49 adam
279 * Revision 1.28 1995/10/13 12:26:43 adam
280 * Optimization of truncation.
282 * Revision 1.27 1995/10/12 17:07:22 adam
285 * Revision 1.26 1995/10/12 12:40:54 adam
286 * Bug fixes in rpn_prox.
288 * Revision 1.25 1995/10/10 13:59:24 adam
289 * Function rset_open changed its wflag parameter to general flags.
291 * Revision 1.24 1995/10/09 16:18:37 adam
292 * Function dict_lookup_grep got extra client data parameter.
294 * Revision 1.23 1995/10/06 16:33:37 adam
295 * Use attribute mappings.
297 * Revision 1.22 1995/10/06 15:07:39 adam
298 * Structure 'local-number' handled.
300 * Revision 1.21 1995/10/06 13:52:06 adam
301 * Bug fixes. Handler may abort further scanning.
303 * Revision 1.20 1995/10/06 11:06:33 adam
304 * Scan entries include 'occurrences' now.
306 * Revision 1.19 1995/10/06 10:43:56 adam
307 * Scan added. 'occurrences' in scan entries not set yet.
309 * Revision 1.18 1995/10/04 16:57:20 adam
310 * Key input and merge sort in one pass.
312 * Revision 1.17 1995/10/04 12:55:17 adam
313 * Bug fix in ranked search. Use=Any keys inserted.
315 * Revision 1.16 1995/10/02 16:24:40 adam
316 * Use attribute actually used in search requests.
318 * Revision 1.15 1995/10/02 15:18:52 adam
319 * New member in recRetrieveCtrl: diagnostic.
321 * Revision 1.14 1995/09/28 12:10:32 adam
322 * Bug fixes. Field prefix used in queries.
324 * Revision 1.13 1995/09/18 14:17:50 adam
327 * Revision 1.12 1995/09/15 14:45:21 adam
329 * Work on truncation.
331 * Revision 1.11 1995/09/14 11:53:27 adam
332 * First work on regular expressions/truncations.
334 * Revision 1.10 1995/09/11 15:23:26 adam
335 * More work on relevance search.
337 * Revision 1.9 1995/09/11 13:09:35 adam
338 * More work on relevance feedback.
340 * Revision 1.8 1995/09/08 14:52:27 adam
341 * Minor changes. Dictionary is lower case now.
343 * Revision 1.7 1995/09/07 13:58:36 adam
344 * New parameter: result-set file descriptor (RSFD) to support multiple
345 * positions within the same result-set.
346 * Boolean operators: and, or, not implemented.
347 * Result-set references.
349 * Revision 1.6 1995/09/06 16:11:18 adam
350 * Option: only one word key per file.
352 * Revision 1.5 1995/09/06 10:33:04 adam
353 * More work on present. Some log messages removed.
355 * Revision 1.4 1995/09/05 15:28:40 adam
356 * More work on search engine.
358 * Revision 1.3 1995/09/04 15:20:22 adam
361 * Revision 1.2 1995/09/04 12:33:43 adam
362 * Various cleanup. YAZ util used instead.
364 * Revision 1.1 1995/09/04 09:10:40 adam
365 * More work on index add/del/update.
366 * Merge sort implemented.
367 * Initial work on z39 server.
386 struct rpn_char_map_info {
391 static const char **rpn_char_map_handler (void *vp, const char **from, int len)
393 struct rpn_char_map_info *p = (struct rpn_char_map_info *) vp;
394 return zebra_maps_input (p->zm, p->reg_type, from, len);
397 static void rpn_char_map_prepare (ZebraHandle zh, int reg_type,
398 struct rpn_char_map_info *map_info)
400 map_info->zm = zh->service->zebra_maps;
401 map_info->reg_type = reg_type;
402 dict_grep_cmap (zh->service->dict, map_info, rpn_char_map_handler);
409 Z_AttributesPlusTerm *zapt;
412 static int attr_find_ex (AttrType *src, oid_value *attributeSetP,
413 const char **string_value)
418 num_attributes = src->zapt->attributes->num_attributes;
420 num_attributes = src->zapt->num_attributes;
422 while (src->major < num_attributes)
424 Z_AttributeElement *element;
427 element = src->zapt->attributes->attributes[src->major];
429 element = src->zapt->attributeList[src->major];
431 if (src->type == *element->attributeType)
433 switch (element->which)
435 case Z_AttributeValue_numeric:
437 if (element->attributeSet && attributeSetP)
441 attrset = oid_getentbyoid (element->attributeSet);
442 *attributeSetP = attrset->value;
444 return *element->value.numeric;
446 case Z_AttributeValue_complex:
447 if (src->minor >= element->value.complex->num_list)
449 if (element->value.complex->list[src->minor]->which ==
450 Z_StringOrNumeric_numeric)
453 if (element->attributeSet && attributeSetP)
457 attrset = oid_getentbyoid (element->attributeSet);
458 *attributeSetP = attrset->value;
461 *element->value.complex->list[src->minor-1]->u.numeric;
463 else if (element->value.complex->list[src->minor]->which ==
464 Z_StringOrNumeric_string)
470 element->value.complex->list[src->minor-1]->u.string;
484 static int attr_find (AttrType *src, oid_value *attributeSetP)
486 return attr_find_ex (src, attributeSetP, 0);
489 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
512 static void term_untrans (ZebraHandle zh, int reg_type,
513 char *dst, const char *src)
517 const char *cp = zebra_maps_output (zh->service->zebra_maps,
528 static void add_isam_p (const char *name, const char *info,
531 if (p->isam_p_indx == p->isam_p_size)
533 ISAMS_P *new_isam_p_buf;
537 p->isam_p_size = 2*p->isam_p_size + 100;
538 new_isam_p_buf = (ISAMS_P *) xmalloc (sizeof(*new_isam_p_buf) *
542 memcpy (new_isam_p_buf, p->isam_p_buf,
543 p->isam_p_indx * sizeof(*p->isam_p_buf));
544 xfree (p->isam_p_buf);
546 p->isam_p_buf = new_isam_p_buf;
549 new_term_no = (int *) xmalloc (sizeof(*new_term_no) *
553 memcpy (new_term_no, p->isam_p_buf,
554 p->isam_p_indx * sizeof(*p->term_no));
557 p->term_no = new_term_no;
560 assert (*info == sizeof(*p->isam_p_buf));
561 memcpy (p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
570 int len = key_SU_decode (&su_code, name);
572 term_untrans (p->zh, p->reg_type, term_tmp, name+len+1);
573 logf (LOG_LOG, "grep: %d %c %s", su_code, name[len], term_tmp);
574 zebraExplain_lookup_ord (p->zh->service->zei,
575 su_code, &db, &set, &use);
576 logf (LOG_LOG, "grep: set=%d use=%d db=%s", set, use, db);
578 resultSetAddTerm (p->zh, p->termset, name[len], db,
585 static int grep_handle (char *name, const char *info, void *p)
587 add_isam_p (name, info, (struct grep_info *) p);
591 static int term_pre (ZebraMaps zebra_maps, int reg_type, const char **src,
592 const char *ct1, const char *ct2)
594 const char *s1, *s0 = *src;
597 /* skip white space */
600 if (ct1 && strchr (ct1, *s0))
602 if (ct2 && strchr (ct2, *s0))
605 map = zebra_maps_input (zebra_maps, reg_type, &s1, strlen(s1));
606 if (**map != *CHR_SPACE)
614 /* term_100: handle term, where trunc=none (no operators at all) */
615 static int term_100 (ZebraMaps zebra_maps, int reg_type,
616 const char **src, char *dst, int space_split,
624 const char *space_start = 0;
625 const char *space_end = 0;
627 if (!term_pre (zebra_maps, reg_type, src, NULL, NULL))
633 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
636 if (**map == *CHR_SPACE)
639 else /* complete subfield only. */
641 if (**map == *CHR_SPACE)
642 { /* save space mapping for later .. */
647 else if (space_start)
648 { /* reload last space */
649 while (space_start < space_end)
651 if (!isalnum (*space_start) && *space_start != '-')
653 dst_term[j++] = *space_start;
654 dst[i++] = *space_start++;
657 space_start = space_end = 0;
660 /* add non-space char */
663 if (!isalnum (*s1) && *s1 != '-')
675 /* term_101: handle term, where trunc=Process # */
676 static int term_101 (ZebraMaps zebra_maps, int reg_type,
677 const char **src, char *dst, int space_split,
685 if (!term_pre (zebra_maps, reg_type, src, "#", "#"))
694 dst_term[j++] = *s0++;
699 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
700 if (space_split && **map == *CHR_SPACE)
712 dst_term[j++] = '\0';
717 /* term_103: handle term, where trunc=re-2 (regular expressions) */
718 static int term_103 (ZebraMaps zebra_maps, int reg_type, const char **src,
719 char *dst, int *errors, int space_split,
727 if (!term_pre (zebra_maps, reg_type, src, "^\\()[].*+?|", "("))
730 if (errors && *s0 == '+' && s0[1] && s0[2] == '+' && s0[3] &&
733 *errors = s0[1] - '0';
740 if (strchr ("^\\()[].*+?|-", *s0))
748 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
749 if (**map == *CHR_SPACE)
766 /* term_103: handle term, where trunc=re-1 (regular expressions) */
767 static int term_102 (ZebraMaps zebra_maps, int reg_type, const char **src,
768 char *dst, int space_split, char *dst_term)
770 return term_103 (zebra_maps, reg_type, src, dst, NULL, space_split,
775 /* term_104: handle term, where trunc=Process # and ! */
776 static int term_104 (ZebraMaps zebra_maps, int reg_type,
777 const char **src, char *dst, int space_split,
785 if (!term_pre (zebra_maps, reg_type, src, "#!", "#!"))
794 dst_term[j++] = *s0++;
799 dst_term[j++] = *s0++;
803 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
804 if (space_split && **map == *CHR_SPACE)
816 dst_term[j++] = '\0';
821 /* term_105/106: handle term, where trunc=Process * and ! and right trunc */
822 static int term_105 (ZebraMaps zebra_maps, int reg_type,
823 const char **src, char *dst, int space_split,
824 char *dst_term, int right_truncate)
831 if (!term_pre (zebra_maps, reg_type, src, "*!", "*!"))
840 dst_term[j++] = *s0++;
845 dst_term[j++] = *s0++;
849 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
850 if (space_split && **map == *CHR_SPACE)
868 dst_term[j++] = '\0';
874 /* gen_regular_rel - generate regular expression from relation
875 * val: border value (inclusive)
876 * islt: 1 if <=; 0 if >=.
878 static void gen_regular_rel (char *dst, int val, int islt)
885 logf (LOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
889 strcpy (dst, "(-[0-9]+|(");
897 strcpy (dst, "([0-9]+|-(");
898 dst_p = strlen (dst);
908 dst_p = strlen (dst);
909 sprintf (numstr, "%d", val);
910 for (w = strlen(numstr); --w >= 0; pos++)
929 strcpy (dst + dst_p, numstr);
930 dst_p = strlen(dst) - pos - 1;
958 for (i = 0; i<pos; i++)
971 /* match everything less than 10^(pos-1) */
973 for (i=1; i<pos; i++)
974 strcat (dst, "[0-9]?");
978 /* match everything greater than 10^pos */
979 for (i = 0; i <= pos; i++)
980 strcat (dst, "[0-9]");
981 strcat (dst, "[0-9]*");
986 void string_rel_add_char (char **term_p, const char *src, int *indx)
988 if (src[*indx] == '\\')
989 *(*term_p)++ = src[(*indx)++];
990 *(*term_p)++ = src[(*indx)++];
994 * > abc ([b-].*|a[c-].*|ab[d-].*|abc.+)
995 * ([^-a].*|a[^-b].*ab[^-c].*|abc.+)
996 * >= abc ([b-].*|a[c-].*|ab[c-].*)
997 * ([^-a].*|a[^-b].*|ab[c-].*)
998 * < abc ([-0].*|a[-a].*|ab[-b].*)
999 * ([^a-].*|a[^b-].*|ab[^c-].*)
1000 * <= abc ([-0].*|a[-a].*|ab[-b].*|abc)
1001 * ([^a-].*|a[^b-].*|ab[^c-].*|abc)
1003 static int string_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1004 const char **term_sub, char *term_dict,
1005 oid_value attributeSet,
1006 int reg_type, int space_split, char *term_dst)
1011 char *term_tmp = term_dict + strlen(term_dict);
1012 char term_component[256];
1014 attr_init (&relation, zapt, 2);
1015 relation_value = attr_find (&relation, NULL);
1017 logf (LOG_DEBUG, "string relation value=%d", relation_value);
1018 switch (relation_value)
1021 if (!term_100 (zh->service->zebra_maps, reg_type,
1022 term_sub, term_component,
1023 space_split, term_dst))
1025 logf (LOG_DEBUG, "Relation <");
1028 for (i = 0; term_component[i]; )
1035 string_rel_add_char (&term_tmp, term_component, &j);
1040 string_rel_add_char (&term_tmp, term_component, &i);
1051 if (!term_100 (zh->service->zebra_maps, reg_type,
1052 term_sub, term_component,
1053 space_split, term_dst))
1055 logf (LOG_DEBUG, "Relation <=");
1058 for (i = 0; term_component[i]; )
1063 string_rel_add_char (&term_tmp, term_component, &j);
1067 string_rel_add_char (&term_tmp, term_component, &i);
1076 for (i = 0; term_component[i]; )
1077 string_rel_add_char (&term_tmp, term_component, &i);
1082 if (!term_100 (zh->service->zebra_maps, reg_type,
1083 term_sub, term_component, space_split, term_dst))
1085 logf (LOG_DEBUG, "Relation >");
1088 for (i = 0; term_component[i];)
1093 string_rel_add_char (&term_tmp, term_component, &j);
1098 string_rel_add_char (&term_tmp, term_component, &i);
1106 for (i = 0; term_component[i];)
1107 string_rel_add_char (&term_tmp, term_component, &i);
1114 if (!term_100 (zh->service->zebra_maps, reg_type, term_sub,
1115 term_component, space_split, term_dst))
1117 logf (LOG_DEBUG, "Relation >=");
1120 for (i = 0; term_component[i];)
1127 string_rel_add_char (&term_tmp, term_component, &j);
1130 if (term_component[i+1])
1134 string_rel_add_char (&term_tmp, term_component, &i);
1138 string_rel_add_char (&term_tmp, term_component, &i);
1150 logf (LOG_DEBUG, "Relation =");
1151 if (!term_100 (zh->service->zebra_maps, reg_type, term_sub,
1152 term_component, space_split, term_dst))
1154 strcat (term_tmp, "(");
1155 strcat (term_tmp, term_component);
1156 strcat (term_tmp, ")");
1161 static int string_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1162 const char **term_sub,
1163 oid_value attributeSet, NMEM stream,
1164 struct grep_info *grep_info,
1165 int reg_type, int complete_flag,
1166 int num_bases, char **basenames,
1169 char term_dict[2*IT_MAX_WORD+4000];
1171 AttrType truncation;
1172 int truncation_value;
1175 oid_value curAttributeSet = attributeSet;
1177 struct rpn_char_map_info rcmi;
1178 int space_split = complete_flag ? 0 : 1;
1180 rpn_char_map_prepare (zh, reg_type, &rcmi);
1181 attr_init (&use, zapt, 1);
1182 use_value = attr_find (&use, &curAttributeSet);
1183 logf (LOG_DEBUG, "string_term, use value %d", use_value);
1184 attr_init (&truncation, zapt, 5);
1185 truncation_value = attr_find (&truncation, NULL);
1186 logf (LOG_DEBUG, "truncation value %d", truncation_value);
1188 if (use_value == -1)
1191 for (base_no = 0; base_no < num_bases; base_no++)
1194 data1_local_attribute *local_attr;
1195 int max_pos, prefix_len = 0;
1198 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
1200 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1201 curAttributeSet, use_value, r);
1205 sprintf (val_str, "%d", use_value);
1207 zh->errString = nmem_strdup (stream, val_str);
1212 struct oident oident;
1214 oident.proto = PROTO_Z3950;
1215 oident.oclass = CLASS_ATTSET;
1216 oident.value = curAttributeSet;
1217 oid_ent_to_oid (&oident, oid);
1220 zh->errString = nmem_strdup (stream, oident.desc);
1224 if (zebraExplain_curDatabase (zh->service->zei, basenames[base_no]))
1226 zh->errCode = 109; /* Database unavailable */
1227 zh->errString = basenames[base_no];
1230 for (local_attr = attp.local_attributes; local_attr;
1231 local_attr = local_attr->next)
1237 ord = zebraExplain_lookupSU (zh->service->zei, attp.attset_ordinal,
1242 term_dict[prefix_len++] = '|';
1244 term_dict[prefix_len++] = '(';
1246 ord_len = key_SU_encode (ord, ord_buf);
1247 for (i = 0; i<ord_len; i++)
1249 term_dict[prefix_len++] = 1;
1250 term_dict[prefix_len++] = ord_buf[i];
1256 sprintf (val_str, "%d", use_value);
1258 zh->errString = nmem_strdup (stream, val_str);
1261 term_dict[prefix_len++] = ')';
1262 term_dict[prefix_len++] = 1;
1263 term_dict[prefix_len++] = reg_type;
1264 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1265 term_dict[prefix_len] = '\0';
1267 switch (truncation_value)
1269 case -1: /* not specified */
1270 case 100: /* do not truncate */
1271 if (!string_relation (zh, zapt, &termp, term_dict,
1273 reg_type, space_split, term_dst))
1275 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_dict+prefix_len);
1276 r = dict_lookup_grep (zh->service->dict, term_dict, 0,
1277 grep_info, &max_pos, 0, grep_handle);
1279 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1281 case 1: /* right truncation */
1282 term_dict[j++] = '(';
1283 if (!term_100 (zh->service->zebra_maps, reg_type,
1284 &termp, term_dict + j, space_split, term_dst))
1286 strcat (term_dict, ".*)");
1287 dict_lookup_grep (zh->service->dict, term_dict, 0, grep_info,
1288 &max_pos, 0, grep_handle);
1290 case 2: /* keft truncation */
1291 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1292 if (!term_100 (zh->service->zebra_maps, reg_type,
1293 &termp, term_dict + j, space_split, term_dst))
1295 strcat (term_dict, ")");
1296 dict_lookup_grep (zh->service->dict, term_dict, 0, grep_info,
1297 &max_pos, 0, grep_handle);
1299 case 3: /* left&right truncation */
1300 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1301 if (!term_100 (zh->service->zebra_maps, reg_type,
1302 &termp, term_dict + j, space_split, term_dst))
1304 strcat (term_dict, ".*)");
1305 dict_lookup_grep (zh->service->dict, term_dict, 0, grep_info,
1306 &max_pos, 0, grep_handle);
1310 case 101: /* process # in term */
1311 term_dict[j++] = '(';
1312 if (!term_101 (zh->service->zebra_maps, reg_type,
1313 &termp, term_dict + j, space_split, term_dst))
1315 strcat (term_dict, ")");
1316 r = dict_lookup_grep (zh->service->dict, term_dict, 0, grep_info,
1317 &max_pos, 0, grep_handle);
1319 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d", r);
1321 case 102: /* Regexp-1 */
1322 term_dict[j++] = '(';
1323 if (!term_102 (zh->service->zebra_maps, reg_type,
1324 &termp, term_dict + j, space_split, term_dst))
1326 strcat (term_dict, ")");
1327 logf (LOG_DEBUG, "Regexp-1 tolerance=%d", r);
1328 r = dict_lookup_grep (zh->service->dict, term_dict, 0, grep_info,
1329 &max_pos, 0, grep_handle);
1331 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
1334 case 103: /* Regexp-2 */
1336 term_dict[j++] = '(';
1337 if (!term_103 (zh->service->zebra_maps, reg_type,
1338 &termp, term_dict + j, &r, space_split, term_dst))
1340 strcat (term_dict, ")");
1341 logf (LOG_DEBUG, "Regexp-2 tolerance=%d", r);
1342 r = dict_lookup_grep (zh->service->dict, term_dict, r, grep_info,
1343 &max_pos, 2, grep_handle);
1345 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
1348 case 104: /* process # and ! in term */
1349 term_dict[j++] = '(';
1350 if (!term_104 (zh->service->zebra_maps, reg_type,
1351 &termp, term_dict + j, space_split, term_dst))
1353 strcat (term_dict, ")");
1354 r = dict_lookup_grep (zh->service->dict, term_dict, 0, grep_info,
1355 &max_pos, 0, grep_handle);
1357 logf (LOG_WARN, "dict_lookup_grep err, trunc=#/!: %d", r);
1359 case 105: /* process * and ! in term */
1360 term_dict[j++] = '(';
1361 if (!term_105 (zh->service->zebra_maps, reg_type,
1362 &termp, term_dict + j, space_split, term_dst, 1))
1364 strcat (term_dict, ")");
1365 r = dict_lookup_grep (zh->service->dict, term_dict, 0, grep_info,
1366 &max_pos, 0, grep_handle);
1368 logf (LOG_WARN, "dict_lookup_grep err, trunc=*/!: %d", r);
1370 case 106: /* process * and ! in term */
1371 term_dict[j++] = '(';
1372 if (!term_105 (zh->service->zebra_maps, reg_type,
1373 &termp, term_dict + j, space_split, term_dst, 0))
1375 strcat (term_dict, ")");
1376 r = dict_lookup_grep (zh->service->dict, term_dict, 0, grep_info,
1377 &max_pos, 0, grep_handle);
1379 logf (LOG_WARN, "dict_lookup_grep err, trunc=*/!: %d", r);
1384 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1388 static void trans_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1392 Z_Term *term = zapt->term;
1394 sizez = term->u.general->len;
1395 if (sizez > IT_MAX_WORD-1)
1396 sizez = IT_MAX_WORD-1;
1397 memcpy (termz, term->u.general->buf, sizez);
1398 termz[sizez] = '\0';
1401 static void trans_scan_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1402 char *termz, int reg_type)
1404 Z_Term *term = zapt->term;
1406 const char *cp = (const char *) term->u.general->buf;
1407 const char *cp_end = cp + term->u.general->len;
1410 const char *space_map = NULL;
1413 while ((len = (cp_end - cp)) > 0)
1415 map = zebra_maps_input (zh->service->zebra_maps, reg_type, &cp, len);
1416 if (**map == *CHR_SPACE)
1421 for (src = space_map; *src; src++)
1424 for (src = *map; *src; src++)
1431 static RSET rpn_prox (ZebraHandle zh, RSET *rset, int rset_no,
1432 int ordered, int exclusion, int relation, int distance)
1437 struct it_key **buf;
1439 char prox_term[1024];
1440 int length_prox_term = 0;
1441 int min_nn = 10000000;
1443 const char *flags = NULL;
1445 rsfd = (RSFD *) xmalloc (sizeof(*rsfd)*rset_no);
1446 more = (int *) xmalloc (sizeof(*more)*rset_no);
1447 buf = (struct it_key **) xmalloc (sizeof(*buf)*rset_no);
1450 for (i = 0; i<rset_no; i++)
1453 for (j = 0; j<rset[i]->no_rset_terms; j++)
1455 const char *nflags = rset[i]->rset_terms[j]->flags;
1456 char *term = rset[i]->rset_terms[j]->name;
1457 int lterm = strlen(term);
1458 if (lterm + length_prox_term < sizeof(prox_term)-1)
1460 if (length_prox_term)
1461 prox_term[length_prox_term++] = ' ';
1462 strcpy (prox_term + length_prox_term, term);
1463 length_prox_term += lterm;
1465 if (min_nn > rset[i]->rset_terms[j]->nn)
1466 min_nn = rset[i]->rset_terms[j]->nn;
1470 for (i = 0; i<rset_no; i++)
1475 for (i = 0; i<rset_no; i++)
1477 buf[i] = (struct it_key *) xmalloc (sizeof(**buf));
1478 rsfd[i] = rset_open (rset[i], RSETF_READ);
1479 if (!(more[i] = rset_read (rset[i], rsfd[i], buf[i], &term_index)))
1484 /* at least one is empty ... return null set */
1485 rset_null_parms parms;
1487 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1489 parms.rset_term->nn = 0;
1490 result = rset_create (rset_kind_null, &parms);
1492 else if (ordered && relation == 3 && exclusion == 0 && distance == 1)
1494 /* special proximity case = phrase search ... */
1495 rset_temp_parms parms;
1498 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1500 parms.rset_term->nn = min_nn;
1501 parms.key_size = sizeof (struct it_key);
1502 parms.temp_path = res_get (zh->service->res, "setTmpDir");
1503 result = rset_create (rset_kind_temp, &parms);
1504 rsfd_result = rset_open (result, RSETF_WRITE);
1508 for (i = 1; i<rset_no; i++)
1517 cmp = key_compare_it (buf[i], buf[i-1]);
1520 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1521 buf[i-1], &term_index);
1526 if (buf[i-1]->seqno+1 != buf[i]->seqno)
1528 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1529 buf[i-1], &term_index);
1535 more[i] = rset_read (rset[i], rsfd[i], buf[i],
1542 rset_write (result, rsfd_result, buf[0]);
1543 more[0] = rset_read (*rset, *rsfd, *buf, &term_index);
1546 rset_close (result, rsfd_result);
1548 else if (rset_no == 2)
1550 /* generic proximity case (two input sets only) ... */
1551 rset_temp_parms parms;
1554 logf (LOG_LOG, "generic prox, dist = %d, relation = %d, ordered =%d, exclusion=%d",
1555 distance, relation, ordered, exclusion);
1556 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1558 parms.rset_term->nn = min_nn;
1559 parms.key_size = sizeof (struct it_key);
1560 parms.temp_path = res_get (zh->service->res, "setTmpDir");
1561 result = rset_create (rset_kind_temp, &parms);
1562 rsfd_result = rset_open (result, RSETF_WRITE);
1564 while (more[0] && more[1])
1566 int cmp = key_compare_it (buf[0], buf[1]);
1568 more[0] = rset_read (rset[0], rsfd[0], buf[0], &term_index);
1570 more[1] = rset_read (rset[1], rsfd[1], buf[1], &term_index);
1573 int sysno = buf[0]->sysno;
1577 seqno[n++] = buf[0]->seqno;
1578 while ((more[0] = rset_read (rset[0], rsfd[0], buf[0],
1580 sysno == buf[0]->sysno)
1582 seqno[n++] = buf[0]->seqno;
1585 for (i = 0; i<n; i++)
1587 int diff = buf[1]->seqno - seqno[i];
1588 int excl = exclusion;
1589 if (!ordered && diff < 0)
1594 if (diff < distance && diff >= 0)
1598 if (diff <= distance && diff >= 0)
1602 if (diff == distance && diff >= 0)
1606 if (diff >= distance && diff >= 0)
1610 if (diff > distance && diff >= 0)
1614 if (diff != distance && diff >= 0)
1620 rset_write (result, rsfd_result, buf[1]);
1624 } while ((more[1] = rset_read (rset[1], rsfd[1], buf[1],
1626 sysno == buf[1]->sysno);
1629 rset_close (result, rsfd_result);
1633 rset_null_parms parms;
1635 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1637 parms.rset_term->nn = 0;
1638 result = rset_create (rset_kind_null, &parms);
1640 for (i = 0; i<rset_no; i++)
1643 rset_close (rset[i], rsfd[i]);
1653 char *normalize_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1654 const char *termz, NMEM stream, unsigned reg_id)
1657 AttrType truncation;
1658 int truncation_value;
1661 attr_init (&truncation, zapt, 5);
1662 truncation_value = attr_find (&truncation, NULL);
1664 switch (truncation_value)
1684 wrbuf = zebra_replace(zh->service->zebra_maps, reg_id, ex_list,
1685 termz, strlen(termz));
1687 return nmem_strdup(stream, termz);
1690 char *buf = (char*) nmem_malloc (stream, wrbuf_len(wrbuf)+1);
1691 memcpy (buf, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1692 buf[wrbuf_len(wrbuf)] = '\0';
1697 static int grep_info_prepare (ZebraHandle zh,
1698 Z_AttributesPlusTerm *zapt,
1699 struct grep_info *grep_info,
1704 int termset_value_numeric;
1705 const char *termset_value_string;
1708 grep_info->term_no = 0;
1710 grep_info->isam_p_size = 0;
1711 grep_info->isam_p_buf = NULL;
1713 grep_info->reg_type = reg_type;
1714 grep_info->termset = 0;
1716 attr_init (&termset, zapt, 8);
1717 termset_value_numeric =
1718 attr_find_ex (&termset, NULL, &termset_value_string);
1719 if (termset_value_numeric != -1)
1722 const char *termset_name = 0;
1723 if (termset_value_numeric != -2)
1726 sprintf (resname, "%d", termset_value_numeric);
1727 termset_name = resname;
1730 termset_name = termset_value_string;
1731 logf (LOG_LOG, "creating termset set %s", termset_name);
1732 grep_info->termset = resultSetAdd (zh, termset_name, 1);
1733 if (!grep_info->termset)
1736 zh->errString = nmem_strdup (stream, termset_name);
1744 static RSET rpn_search_APT_phrase (ZebraHandle zh,
1745 Z_AttributesPlusTerm *zapt,
1746 const char *termz_org,
1747 oid_value attributeSet,
1749 int reg_type, int complete_flag,
1750 const char *rank_type,
1751 int num_bases, char **basenames)
1753 char term_dst[IT_MAX_WORD+1];
1754 RSET rset[60], result;
1755 int i, r, rset_no = 0;
1756 struct grep_info grep_info;
1757 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1758 const char *termp = termz;
1760 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1764 logf (LOG_DEBUG, "APT_phrase termp=%s", termp);
1765 grep_info.isam_p_indx = 0;
1766 r = string_term (zh, zapt, &termp, attributeSet, stream, &grep_info,
1767 reg_type, complete_flag, num_bases, basenames,
1771 logf (LOG_DEBUG, "term: %s", term_dst);
1772 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1773 grep_info.isam_p_indx, term_dst,
1774 strlen(term_dst), rank_type);
1775 assert (rset[rset_no]);
1776 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1780 xfree(grep_info.term_no);
1782 xfree (grep_info.isam_p_buf);
1785 rset_null_parms parms;
1787 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1788 return rset_create (rset_kind_null, &parms);
1790 else if (rset_no == 1)
1792 result = rpn_prox (zh, rset, rset_no, 1, 0, 3, 1);
1793 for (i = 0; i<rset_no; i++)
1794 rset_delete (rset[i]);
1798 static RSET rpn_search_APT_or_list (ZebraHandle zh,
1799 Z_AttributesPlusTerm *zapt,
1800 const char *termz_org,
1801 oid_value attributeSet,
1803 int reg_type, int complete_flag,
1804 const char *rank_type,
1805 int num_bases, char **basenames)
1807 char term_dst[IT_MAX_WORD+1];
1808 RSET rset[60], result;
1809 int i, r, rset_no = 0;
1810 struct grep_info grep_info;
1811 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1812 const char *termp = termz;
1814 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1818 logf (LOG_DEBUG, "APT_or_list termp=%s", termp);
1819 grep_info.isam_p_indx = 0;
1820 r = string_term (zh, zapt, &termp, attributeSet, stream, &grep_info,
1821 reg_type, complete_flag, num_bases, basenames,
1825 logf (LOG_DEBUG, "term: %s", term_dst);
1826 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1827 grep_info.isam_p_indx, term_dst,
1828 strlen(term_dst), rank_type);
1829 assert (rset[rset_no]);
1830 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1834 xfree(grep_info.term_no);
1836 xfree (grep_info.isam_p_buf);
1839 rset_null_parms parms;
1841 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1842 return rset_create (rset_kind_null, &parms);
1845 for (i = 1; i<rset_no; i++)
1847 rset_bool_parms bool_parms;
1849 bool_parms.rset_l = result;
1850 bool_parms.rset_r = rset[i];
1851 bool_parms.key_size = sizeof(struct it_key);
1852 bool_parms.cmp = key_compare_it;
1853 result = rset_create (rset_kind_or, &bool_parms);
1858 static RSET rpn_search_APT_and_list (ZebraHandle zh,
1859 Z_AttributesPlusTerm *zapt,
1860 const char *termz_org,
1861 oid_value attributeSet,
1863 int reg_type, int complete_flag,
1864 const char *rank_type,
1865 int num_bases, char **basenames)
1867 char term_dst[IT_MAX_WORD+1];
1868 RSET rset[60], result;
1869 int i, r, rset_no = 0;
1870 struct grep_info grep_info;
1871 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1872 const char *termp = termz;
1874 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1878 logf (LOG_DEBUG, "APT_and_list termp=%s", termp);
1879 grep_info.isam_p_indx = 0;
1880 r = string_term (zh, zapt, &termp, attributeSet, stream, &grep_info,
1881 reg_type, complete_flag, num_bases, basenames,
1885 logf (LOG_DEBUG, "term: %s", term_dst);
1886 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1887 grep_info.isam_p_indx, term_dst,
1888 strlen(term_dst), rank_type);
1889 assert (rset[rset_no]);
1890 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1894 xfree(grep_info.term_no);
1896 xfree (grep_info.isam_p_buf);
1899 rset_null_parms parms;
1901 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1902 return rset_create (rset_kind_null, &parms);
1905 for (i = 1; i<rset_no; i++)
1907 rset_bool_parms bool_parms;
1909 bool_parms.rset_l = result;
1910 bool_parms.rset_r = rset[i];
1911 bool_parms.key_size = sizeof(struct it_key);
1912 bool_parms.cmp = key_compare_it;
1913 result = rset_create (rset_kind_and, &bool_parms);
1918 static int numeric_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1919 const char **term_sub,
1921 oid_value attributeSet,
1922 struct grep_info *grep_info,
1931 char *term_tmp = term_dict + strlen(term_dict);
1933 attr_init (&relation, zapt, 2);
1934 relation_value = attr_find (&relation, NULL);
1936 logf (LOG_DEBUG, "numeric relation value=%d", relation_value);
1938 if (!term_100 (zh->service->zebra_maps, reg_type, term_sub, term_tmp, 1,
1941 term_value = atoi (term_tmp);
1942 switch (relation_value)
1945 logf (LOG_DEBUG, "Relation <");
1946 gen_regular_rel (term_tmp, term_value-1, 1);
1949 logf (LOG_DEBUG, "Relation <=");
1950 gen_regular_rel (term_tmp, term_value, 1);
1953 logf (LOG_DEBUG, "Relation >=");
1954 gen_regular_rel (term_tmp, term_value, 0);
1957 logf (LOG_DEBUG, "Relation >");
1958 gen_regular_rel (term_tmp, term_value+1, 0);
1962 logf (LOG_DEBUG, "Relation =");
1963 sprintf (term_tmp, "(0*%d)", term_value);
1965 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_tmp);
1966 r = dict_lookup_grep (zh->service->dict, term_dict, 0, grep_info, max_pos,
1969 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1970 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1974 static int numeric_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1975 const char **term_sub,
1976 oid_value attributeSet, struct grep_info *grep_info,
1977 int reg_type, int complete_flag,
1978 int num_bases, char **basenames,
1981 char term_dict[2*IT_MAX_WORD+2];
1985 oid_value curAttributeSet = attributeSet;
1987 struct rpn_char_map_info rcmi;
1989 rpn_char_map_prepare (zh, reg_type, &rcmi);
1990 attr_init (&use, zapt, 1);
1991 use_value = attr_find (&use, &curAttributeSet);
1992 logf (LOG_DEBUG, "numeric_term, use value %d", use_value);
1994 if (use_value == -1)
1997 for (base_no = 0; base_no < num_bases; base_no++)
2000 data1_local_attribute *local_attr;
2001 int max_pos, prefix_len = 0;
2004 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
2006 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
2007 curAttributeSet, use_value, r);
2014 if (zebraExplain_curDatabase (zh->service->zei, basenames[base_no]))
2016 zh->errCode = 109; /* Database unavailable */
2017 zh->errString = basenames[base_no];
2020 for (local_attr = attp.local_attributes; local_attr;
2021 local_attr = local_attr->next)
2027 ord = zebraExplain_lookupSU (zh->service->zei, attp.attset_ordinal,
2032 term_dict[prefix_len++] = '|';
2034 term_dict[prefix_len++] = '(';
2036 ord_len = key_SU_encode (ord, ord_buf);
2037 for (i = 0; i<ord_len; i++)
2039 term_dict[prefix_len++] = 1;
2040 term_dict[prefix_len++] = ord_buf[i];
2048 term_dict[prefix_len++] = ')';
2049 term_dict[prefix_len++] = 1;
2050 term_dict[prefix_len++] = reg_type;
2051 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
2052 term_dict[prefix_len] = '\0';
2053 if (!numeric_relation (zh, zapt, &termp, term_dict,
2054 attributeSet, grep_info, &max_pos, reg_type,
2059 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
2063 static RSET rpn_search_APT_numeric (ZebraHandle zh,
2064 Z_AttributesPlusTerm *zapt,
2066 oid_value attributeSet,
2068 int reg_type, int complete_flag,
2069 const char *rank_type,
2070 int num_bases, char **basenames)
2072 char term_dst[IT_MAX_WORD+1];
2073 const char *termp = termz;
2074 RSET rset[60], result;
2075 int i, r, rset_no = 0;
2076 struct grep_info grep_info;
2078 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
2082 logf (LOG_DEBUG, "APT_numeric termp=%s", termp);
2083 grep_info.isam_p_indx = 0;
2084 r = numeric_term (zh, zapt, &termp, attributeSet, &grep_info,
2085 reg_type, complete_flag, num_bases, basenames,
2089 logf (LOG_DEBUG, "term: %s", term_dst);
2090 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
2091 grep_info.isam_p_indx, term_dst,
2092 strlen(term_dst), rank_type);
2093 assert (rset[rset_no]);
2094 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
2098 xfree(grep_info.term_no);
2100 xfree (grep_info.isam_p_buf);
2103 rset_null_parms parms;
2105 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
2106 return rset_create (rset_kind_null, &parms);
2109 for (i = 1; i<rset_no; i++)
2111 rset_bool_parms bool_parms;
2113 bool_parms.rset_l = result;
2114 bool_parms.rset_r = rset[i];
2115 bool_parms.key_size = sizeof(struct it_key);
2116 bool_parms.cmp = key_compare_it;
2117 result = rset_create (rset_kind_and, &bool_parms);
2122 static RSET rpn_search_APT_local (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2124 oid_value attributeSet,
2126 const char *rank_type)
2131 rset_temp_parms parms;
2133 parms.rset_term = rset_term_create (termz, -1, rank_type);
2134 parms.key_size = sizeof (struct it_key);
2135 parms.temp_path = res_get (zh->service->res, "setTmpDir");
2136 result = rset_create (rset_kind_temp, &parms);
2137 rsfd = rset_open (result, RSETF_WRITE);
2139 key.sysno = atoi (termz);
2143 rset_write (result, rsfd, &key);
2144 rset_close (result, rsfd);
2148 static RSET rpn_sort_spec (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2149 oid_value attributeSet, NMEM stream,
2150 Z_SortKeySpecList *sort_sequence,
2151 const char *rank_type)
2153 rset_null_parms parms;
2155 int sort_relation_value;
2156 AttrType sort_relation_type;
2161 Z_AttributeElement *ae;
2165 attr_init (&sort_relation_type, zapt, 7);
2166 sort_relation_value = attr_find (&sort_relation_type, &attributeSet);
2168 attr_init (&use_type, zapt, 1);
2169 use_value = attr_find (&use_type, &attributeSet);
2171 if (!sort_sequence->specs)
2173 sort_sequence->num_specs = 10;
2174 sort_sequence->specs = (Z_SortKeySpec **)
2175 nmem_malloc (stream, sort_sequence->num_specs *
2176 sizeof(*sort_sequence->specs));
2177 for (i = 0; i<sort_sequence->num_specs; i++)
2178 sort_sequence->specs[i] = 0;
2180 if (zapt->term->which != Z_Term_general)
2183 i = atoi_n ((char *) zapt->term->u.general->buf,
2184 zapt->term->u.general->len);
2185 if (i >= sort_sequence->num_specs)
2188 oe.proto = PROTO_Z3950;
2189 oe.oclass = CLASS_ATTSET;
2190 oe.value = attributeSet;
2191 if (!oid_ent_to_oid (&oe, oid))
2194 sks = (Z_SortKeySpec *) nmem_malloc (stream, sizeof(*sks));
2195 sks->sortElement = (Z_SortElement *)
2196 nmem_malloc (stream, sizeof(*sks->sortElement));
2197 sks->sortElement->which = Z_SortElement_generic;
2198 sk = sks->sortElement->u.generic = (Z_SortKey *)
2199 nmem_malloc (stream, sizeof(*sk));
2200 sk->which = Z_SortKey_sortAttributes;
2201 sk->u.sortAttributes = (Z_SortAttributes *)
2202 nmem_malloc (stream, sizeof(*sk->u.sortAttributes));
2204 sk->u.sortAttributes->id = oid;
2205 sk->u.sortAttributes->list = (Z_AttributeList *)
2206 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list));
2207 sk->u.sortAttributes->list->num_attributes = 1;
2208 sk->u.sortAttributes->list->attributes = (Z_AttributeElement **)
2209 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list->attributes));
2210 ae = *sk->u.sortAttributes->list->attributes = (Z_AttributeElement *)
2211 nmem_malloc (stream, sizeof(**sk->u.sortAttributes->list->attributes));
2212 ae->attributeSet = 0;
2213 ae->attributeType = (int *)
2214 nmem_malloc (stream, sizeof(*ae->attributeType));
2215 *ae->attributeType = 1;
2216 ae->which = Z_AttributeValue_numeric;
2217 ae->value.numeric = (int *)
2218 nmem_malloc (stream, sizeof(*ae->value.numeric));
2219 *ae->value.numeric = use_value;
2221 sks->sortRelation = (int *)
2222 nmem_malloc (stream, sizeof(*sks->sortRelation));
2223 if (sort_relation_value == 1)
2224 *sks->sortRelation = Z_SortRelation_ascending;
2225 else if (sort_relation_value == 2)
2226 *sks->sortRelation = Z_SortRelation_descending;
2228 *sks->sortRelation = Z_SortRelation_ascending;
2230 sks->caseSensitivity = (int *)
2231 nmem_malloc (stream, sizeof(*sks->caseSensitivity));
2232 *sks->caseSensitivity = 0;
2235 sks->which = Z_SortKeySpec_null;
2236 sks->u.null = odr_nullval ();
2238 sks->missingValueAction = 0;
2241 sort_sequence->specs[i] = sks;
2243 parms.rset_term = rset_term_create ("", -1, rank_type);
2244 return rset_create (rset_kind_null, &parms);
2248 static RSET rpn_search_APT (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2249 oid_value attributeSet, NMEM stream,
2250 Z_SortKeySpecList *sort_sequence,
2251 int num_bases, char **basenames)
2254 char *search_type = NULL;
2255 char *rank_type = NULL;
2258 char termz[IT_MAX_WORD+1];
2260 zebra_maps_attr (zh->service->zebra_maps, zapt, ®_id, &search_type,
2261 &rank_type, &complete_flag, &sort_flag);
2263 logf (LOG_DEBUG, "reg_id=%c", reg_id);
2264 logf (LOG_DEBUG, "complete_flag=%d", complete_flag);
2265 logf (LOG_DEBUG, "search_type=%s", search_type);
2266 logf (LOG_DEBUG, "rank_type=%s", rank_type);
2268 if (zapt->term->which != Z_Term_general)
2273 trans_term (zh, zapt, termz);
2276 return rpn_sort_spec (zh, zapt, attributeSet, stream, sort_sequence,
2279 if (!strcmp (search_type, "phrase"))
2281 return rpn_search_APT_phrase (zh, zapt, termz, attributeSet, stream,
2282 reg_id, complete_flag, rank_type,
2283 num_bases, basenames);
2285 else if (!strcmp (search_type, "and-list"))
2287 return rpn_search_APT_and_list (zh, zapt, termz, attributeSet, stream,
2288 reg_id, complete_flag, rank_type,
2289 num_bases, basenames);
2291 else if (!strcmp (search_type, "or-list"))
2293 return rpn_search_APT_or_list (zh, zapt, termz, attributeSet, stream,
2294 reg_id, complete_flag, rank_type,
2295 num_bases, basenames);
2297 else if (!strcmp (search_type, "local"))
2299 return rpn_search_APT_local (zh, zapt, termz, attributeSet, stream,
2302 else if (!strcmp (search_type, "numeric"))
2304 return rpn_search_APT_numeric (zh, zapt, termz, attributeSet, stream,
2305 reg_id, complete_flag, rank_type,
2306 num_bases, basenames);
2312 static RSET rpn_search_structure (ZebraHandle zh, Z_RPNStructure *zs,
2313 oid_value attributeSet, NMEM stream,
2314 Z_SortKeySpecList *sort_sequence,
2315 int num_bases, char **basenames)
2318 if (zs->which == Z_RPNStructure_complex)
2320 Z_Operator *zop = zs->u.complex->roperator;
2321 rset_bool_parms bool_parms;
2323 bool_parms.rset_l = rpn_search_structure (zh, zs->u.complex->s1,
2324 attributeSet, stream,
2326 num_bases, basenames);
2327 if (bool_parms.rset_l == NULL)
2329 bool_parms.rset_r = rpn_search_structure (zh, zs->u.complex->s2,
2330 attributeSet, stream,
2332 num_bases, basenames);
2333 if (bool_parms.rset_r == NULL)
2335 rset_delete (bool_parms.rset_l);
2338 bool_parms.key_size = sizeof(struct it_key);
2339 bool_parms.cmp = key_compare_it;
2343 case Z_Operator_and:
2344 r = rset_create (rset_kind_and, &bool_parms);
2347 r = rset_create (rset_kind_or, &bool_parms);
2349 case Z_Operator_and_not:
2350 r = rset_create (rset_kind_not, &bool_parms);
2352 case Z_Operator_prox:
2354 if (zop->u.prox->which != Z_ProximityOperator_known)
2360 if (zop->u.prox->which != Z_ProxCode_known)
2368 if (*zop->u.prox->u.known != Z_ProxUnit_word)
2370 char *val = (char *) nmem_malloc (stream, 16);
2372 zh->errString = val;
2373 sprintf (val, "%d", *zop->u.prox->u.known);
2377 if (*zop->u.prox->proximityUnitCode != Z_ProxUnit_word)
2379 char *val = (char *) nmem_malloc (stream, 16);
2381 zh->errString = val;
2382 sprintf (val, "%d", *zop->u.prox->proximityUnitCode);
2390 rsets[0] = bool_parms.rset_l;
2391 rsets[1] = bool_parms.rset_r;
2393 r = rpn_prox (zh, rsets, 2,
2394 *zop->u.prox->ordered,
2395 (!zop->u.prox->exclusion ? 0 :
2396 *zop->u.prox->exclusion),
2397 *zop->u.prox->relationType,
2398 *zop->u.prox->distance);
2399 rset_delete (rsets[0]);
2400 rset_delete (rsets[1]);
2408 else if (zs->which == Z_RPNStructure_simple)
2410 if (zs->u.simple->which == Z_Operand_APT)
2412 logf (LOG_DEBUG, "rpn_search_APT");
2413 r = rpn_search_APT (zh, zs->u.simple->u.attributesPlusTerm,
2414 attributeSet, stream, sort_sequence,
2415 num_bases, basenames);
2417 else if (zs->u.simple->which == Z_Operand_resultSetId)
2419 logf (LOG_DEBUG, "rpn_search_ref");
2420 r = resultSetRef (zh, zs->u.simple->u.resultSetId);
2423 r = rset_create (rset_kind_null, NULL);
2426 nmem_strdup (stream, zs->u.simple->u.resultSetId);
2445 RSET rpn_search (ZebraHandle zh, NMEM nmem,
2446 Z_RPNQuery *rpn, int num_bases, char **basenames,
2447 const char *setname,
2452 oid_value attributeSet;
2453 Z_SortKeySpecList *sort_sequence;
2457 zh->errString = NULL;
2460 sort_sequence = (Z_SortKeySpecList *)
2461 nmem_malloc (nmem, sizeof(*sort_sequence));
2462 sort_sequence->num_specs = 10;
2463 sort_sequence->specs = (Z_SortKeySpec **)
2464 nmem_malloc (nmem, sort_sequence->num_specs *
2465 sizeof(*sort_sequence->specs));
2466 for (i = 0; i<sort_sequence->num_specs; i++)
2467 sort_sequence->specs[i] = 0;
2469 attrset = oid_getentbyoid (rpn->attributeSetId);
2470 attributeSet = attrset->value;
2471 rset = rpn_search_structure (zh, rpn->RPNStructure, attributeSet,
2472 nmem, sort_sequence, num_bases, basenames);
2477 logf (LOG_DEBUG, "search error: %d", zh->errCode);
2479 for (i = 0; sort_sequence->specs[i]; i++)
2481 sort_sequence->num_specs = i;
2483 resultSetRank (zh, sset, rset);
2486 logf (LOG_DEBUG, "resultSetSortSingle in rpn_search");
2487 resultSetSortSingle (zh, nmem, sset, rset,
2488 sort_sequence, &sort_status);
2491 logf (LOG_DEBUG, "resultSetSortSingle status = %d", zh->errCode);
2497 struct scan_info_entry {
2503 struct scan_info_entry *list;
2509 static int scan_handle (char *name, const char *info, int pos, void *client)
2511 int len_prefix, idx;
2512 struct scan_info *scan_info = (struct scan_info *) client;
2514 len_prefix = strlen(scan_info->prefix);
2515 if (memcmp (name, scan_info->prefix, len_prefix))
2517 if (pos > 0) idx = scan_info->after - pos + scan_info->before;
2520 scan_info->list[idx].term = (char *)
2521 odr_malloc (scan_info->odr, strlen(name + len_prefix)+1);
2522 strcpy (scan_info->list[idx].term, name + len_prefix);
2523 assert (*info == sizeof(ISAMS_P));
2524 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAMS_P));
2528 static void scan_term_untrans (ZebraHandle zh, NMEM stream, int reg_type,
2529 char **dst, const char *src)
2531 char term_dst[1024];
2533 term_untrans (zh, reg_type, term_dst, src);
2535 *dst = (char *) nmem_malloc (stream, strlen(term_dst)+1);
2536 strcpy (*dst, term_dst);
2539 static void count_set (RSET r, int *count)
2547 logf (LOG_DEBUG, "count_set");
2550 rfd = rset_open (r, RSETF_READ);
2551 while (rset_read (r, rfd, &key, &term_index))
2553 if (key.sysno != psysno)
2560 rset_close (r, rfd);
2561 logf (LOG_DEBUG, "%d keys, %d records", kno, *count);
2564 void rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
2565 oid_value attributeset,
2566 int num_bases, char **basenames,
2567 int *position, int *num_entries, ZebraScanEntry **list,
2571 int pos = *position;
2572 int num = *num_entries;
2576 char termz[IT_MAX_WORD+20];
2579 struct scan_info *scan_info_array;
2580 ZebraScanEntry *glist;
2581 int ords[32], ord_no = 0;
2585 char *search_type = NULL;
2586 char *rank_type = NULL;
2591 if (attributeset == VAL_NONE)
2592 attributeset = VAL_BIB1;
2594 logf (LOG_DEBUG, "position = %d, num = %d", pos, num);
2596 attr_init (&use, zapt, 1);
2597 use_value = attr_find (&use, &attributeset);
2599 if (zebra_maps_attr (zh->service->zebra_maps, zapt, ®_id, &search_type,
2600 &rank_type, &complete_flag, &sort_flag))
2607 if (use_value == -1)
2609 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
2613 data1_local_attribute *local_attr;
2615 if ((r=att_getentbyatt (zh, &attp, attributeset, use_value)))
2617 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
2618 attributeset, use_value);
2626 if (zebraExplain_curDatabase (zh->service->zei, basenames[base_no]))
2628 zh->errString = basenames[base_no];
2629 zh->errCode = 109; /* Database unavailable */
2633 for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
2634 local_attr = local_attr->next)
2638 ord = zebraExplain_lookupSU (zh->service->zei, attp.attset_ordinal,
2641 ords[ord_no++] = ord;
2650 /* prepare dictionary scanning */
2653 scan_info_array = (struct scan_info *)
2654 odr_malloc (stream, ord_no * sizeof(*scan_info_array));
2655 for (i = 0; i < ord_no; i++)
2657 int j, prefix_len = 0;
2658 int before_tmp = before, after_tmp = after;
2659 struct scan_info *scan_info = scan_info_array + i;
2660 struct rpn_char_map_info rcmi;
2662 rpn_char_map_prepare (zh, reg_id, &rcmi);
2664 scan_info->before = before;
2665 scan_info->after = after;
2666 scan_info->odr = stream;
2668 scan_info->list = (struct scan_info_entry *)
2669 odr_malloc (stream, (before+after) * sizeof(*scan_info->list));
2670 for (j = 0; j<before+after; j++)
2671 scan_info->list[j].term = NULL;
2673 prefix_len += key_SU_encode (ords[i], termz + prefix_len);
2674 termz[prefix_len++] = reg_id;
2675 termz[prefix_len] = 0;
2676 strcpy (scan_info->prefix, termz);
2678 trans_scan_term (zh, zapt, termz+prefix_len, reg_id);
2680 dict_scan (zh->service->dict, termz, &before_tmp, &after_tmp,
2681 scan_info, scan_handle);
2683 glist = (ZebraScanEntry *)
2684 odr_malloc (stream, (before+after)*sizeof(*glist));
2686 /* consider terms after main term */
2687 for (i = 0; i < ord_no; i++)
2691 for (i = 0; i<after; i++)
2694 const char *mterm = NULL;
2698 for (j = 0; j < ord_no; j++)
2700 if (ptr[j] < before+after &&
2701 (tst=scan_info_array[j].list[ptr[j]].term) &&
2702 (!mterm || strcmp (tst, mterm) < 0))
2710 scan_term_untrans (zh, stream->mem, reg_id,
2711 &glist[i+before].term, mterm);
2712 rset = rset_trunc (zh, &scan_info_array[j0].list[ptr[j0]].isam_p, 1,
2713 glist[i+before].term, strlen(glist[i+before].term),
2717 for (j = j0+1; j<ord_no; j++)
2719 if (ptr[j] < before+after &&
2720 (tst=scan_info_array[j].list[ptr[j]].term) &&
2721 !strcmp (tst, mterm))
2723 rset_bool_parms bool_parms;
2727 rset_trunc (zh, &scan_info_array[j].list[ptr[j]].isam_p, 1,
2728 glist[i+before].term,
2729 strlen(glist[i+before].term), NULL);
2731 bool_parms.key_size = sizeof(struct it_key);
2732 bool_parms.cmp = key_compare_it;
2733 bool_parms.rset_l = rset;
2734 bool_parms.rset_r = rset2;
2736 rset = rset_create (rset_kind_or, &bool_parms);
2741 count_set (rset, &glist[i+before].occurrences);
2746 *num_entries -= (after-i);
2750 /* consider terms before main term */
2751 for (i = 0; i<ord_no; i++)
2754 for (i = 0; i<before; i++)
2757 const char *mterm = NULL;
2761 for (j = 0; j <ord_no; j++)
2763 if (ptr[j] < before &&
2764 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2765 (!mterm || strcmp (tst, mterm) > 0))
2774 scan_term_untrans (zh, stream->mem, reg_id,
2775 &glist[before-1-i].term, mterm);
2778 (zh, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1,
2779 glist[before-1-i].term, strlen(glist[before-1-i].term),
2784 for (j = j0+1; j<ord_no; j++)
2786 if (ptr[j] < before &&
2787 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2788 !strcmp (tst, mterm))
2790 rset_bool_parms bool_parms;
2793 rset2 = rset_trunc (zh,
2794 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1,
2795 glist[before-1-i].term,
2796 strlen(glist[before-1-i].term), NULL);
2798 bool_parms.key_size = sizeof(struct it_key);
2799 bool_parms.cmp = key_compare_it;
2800 bool_parms.rset_l = rset;
2801 bool_parms.rset_r = rset2;
2803 rset = rset_create (rset_kind_or, &bool_parms);
2808 count_set (rset, &glist[before-1-i].occurrences);
2818 *list = glist + i; /* list is set to first 'real' entry */
2820 logf (LOG_DEBUG, "position = %d, num_entries = %d",
2821 *position, *num_entries);
2823 logf (LOG_DEBUG, "scan error: %d", zh->errCode);