2 * Copyright (C) 1995-1998, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.88 1998-10-18 07:54:52 adam
8 * Additional info added for diagnostics 114 (Unsupported use attribute) and
9 * 121 (Unsupported attribute set).
11 * Revision 1.87 1998/09/28 11:19:12 adam
12 * Fix for Compiled ASN.1.
14 * Revision 1.86 1998/09/22 10:48:20 adam
15 * Minor changes in search API.
17 * Revision 1.85 1998/09/22 10:03:43 adam
18 * Changed result sets to be persistent in the sense that they can
19 * be re-searched if needed.
20 * Fixed memory leak in rsm_or.
22 * Revision 1.84 1998/09/18 12:41:00 adam
23 * Fixed bug with numerical relations.
25 * Revision 1.83 1998/09/02 13:53:19 adam
26 * Extra parameter decode added to search routines to implement
29 * Revision 1.82 1998/06/26 11:16:40 quinn
30 * Added support (un-optimised) for left and left/right truncation
32 * Revision 1.81 1998/06/24 12:16:14 adam
33 * Support for relations on text operands. Open range support in
34 * DFA module (i.e. [-j], [g-]).
36 * Revision 1.80 1998/06/23 15:33:34 adam
37 * Added feature to specify sort criteria in query (type 7 specifies
40 * Revision 1.79 1998/06/22 11:35:09 adam
43 * Revision 1.78 1998/06/08 14:43:17 adam
44 * Added suport for EXPLAIN Proxy servers - added settings databasePath
45 * and explainDatabase to facilitate this. Increased maximum number
46 * of databases and attributes in one register.
48 * Revision 1.77 1998/05/20 10:12:22 adam
49 * Implemented automatic EXPLAIN database maintenance.
50 * Modified Zebra to work with ASN.1 compiled version of YAZ.
52 * Revision 1.76 1998/04/02 14:35:29 adam
53 * First version of Zebra that works with compiled ASN.1.
55 * Revision 1.75 1998/03/05 08:45:13 adam
56 * New result set model and modular ranking system. Moved towards
57 * descent server API. System information stored as "SGML" records.
59 * Revision 1.74 1998/02/10 12:03:06 adam
62 * Revision 1.73 1998/01/29 13:40:11 adam
63 * Better logging for scan service.
65 * Revision 1.72 1998/01/07 13:53:41 adam
66 * Queries using simple ranked operands returns right number of hits.
68 * Revision 1.71 1997/12/18 10:54:24 adam
69 * New method result set method rs_hits that returns the number of
70 * hits in result-set (if known). The ranked result set returns real
71 * number of hits but only when not combined with other operands.
73 * Revision 1.70 1997/10/31 12:34:43 adam
74 * Changed a few log statements.
76 * Revision 1.69 1997/10/29 12:05:02 adam
77 * Server produces diagnostic "Unsupported Attribute Set" when appropriate.
79 * Revision 1.68 1997/10/27 14:33:06 adam
80 * Moved towards generic character mapping depending on "structure"
81 * field in abstract syntax file. Fixed a few memory leaks. Fixed
82 * bug with negative integers when doing searches with relational
85 * Revision 1.67 1997/09/29 09:06:10 adam
86 * Removed one static var in order to make this module thread safe.
88 * Revision 1.66 1997/09/25 14:58:03 adam
91 * Revision 1.65 1997/09/22 12:39:06 adam
92 * Added get_pos method for the ranked result sets.
94 * Revision 1.64 1997/09/18 08:59:20 adam
95 * Extra generic handle for the character mapping routines.
97 * Revision 1.63 1997/09/17 12:19:18 adam
98 * Zebra version corresponds to YAZ version 1.4.
99 * Changed Zebra server so that it doesn't depend on global common_resource.
101 * Revision 1.62 1997/09/05 15:30:09 adam
102 * Changed prototype for chr_map_input - added const.
103 * Added support for C++, headers uses extern "C" for public definitions.
105 * Revision 1.61 1997/02/10 10:21:14 adam
106 * Bug fix: in search terms character (^) wasn't observed.
108 * Revision 1.60 1997/01/31 11:10:34 adam
109 * Bug fix: Leading and trailing white space weren't removed in scan tokens.
111 * Revision 1.59 1997/01/17 11:31:46 adam
112 * Bug fix: complete phrase search didn't work.
114 * Revision 1.58 1996/12/23 15:30:45 adam
115 * Work on truncation.
116 * Bug fix: result sets weren't deleted after server shut down.
118 * Revision 1.57 1996/11/11 13:38:02 adam
119 * Added proximity support in search.
121 * Revision 1.56 1996/11/08 11:10:32 adam
122 * Buffers used during file match got bigger.
123 * Compressed ISAM support everywhere.
124 * Bug fixes regarding masking characters in queries.
125 * Redesigned Regexp-2 queries.
127 * Revision 1.55 1996/11/04 14:07:44 adam
128 * Moved truncation code to trunc.c.
130 * Revision 1.54 1996/10/29 14:09:52 adam
131 * Use of cisam system - enabled if setting isamc is 1.
133 * Revision 1.53 1996/06/26 09:21:43 adam
134 * Bug fix: local attribute set wasn't obeyed in scan.
136 * Revision 1.52 1996/06/17 14:26:20 adam
137 * Function gen_regular_rel changed to handle negative numbers.
139 * Revision 1.51 1996/06/11 10:54:15 quinn
142 * Revision 1.50 1996/06/07 08:51:53 adam
143 * Bug fix: Character mapping was broken (introducued by last revision).
145 * Revision 1.49 1996/06/04 10:18:11 adam
146 * Search/scan uses character mapping module.
148 * Revision 1.48 1996/05/28 15:15:01 adam
149 * Bug fix: Didn't handle unknown database correctly.
151 * Revision 1.47 1996/05/15 18:36:28 adam
152 * Function trans_term transforms unsearchable characters to blanks.
154 * Revision 1.46 1996/05/15 11:57:56 adam
155 * Fixed bug introduced by set/field mapping in search operations.
157 * Revision 1.45 1996/05/14 11:34:00 adam
158 * Scan support in multiple registers/databases.
160 * Revision 1.44 1996/05/14 06:16:44 adam
161 * Compact use/set bytes used in search service.
163 * Revision 1.43 1996/05/09 09:54:43 adam
164 * Server supports maps from one logical attributes to a list of physical
166 * The extraction process doesn't make space consuming 'any' keys.
168 * Revision 1.42 1996/05/09 07:28:56 quinn
169 * Work towards phrases and multiple registers
171 * Revision 1.41 1996/03/20 09:36:43 adam
172 * Function dict_lookup_grep got extra parameter, init_pos, which marks
173 * from which position in pattern approximate pattern matching should occur.
174 * Approximate pattern matching is used in relevance=re-2.
176 * Revision 1.40 1996/02/02 13:44:44 adam
177 * The public dictionary functions simply use char instead of Dict_char
178 * to represent search strings. Dict_char is used internally only.
180 * Revision 1.39 1996/01/03 16:22:13 quinn
181 * operator->roperator
183 * Revision 1.38 1995/12/11 09:12:55 adam
184 * The rec_get function returns NULL if record doesn't exist - will
185 * happen in the server if the result set records have been deleted since
186 * the creation of the set (i.e. the search).
187 * The server saves a result temporarily if it is 'volatile', i.e. the
188 * set is register dependent.
190 * Revision 1.37 1995/12/06 15:05:28 adam
191 * More verbose in count_set.
193 * Revision 1.36 1995/12/06 12:41:27 adam
194 * New command 'stat' for the index program.
195 * Filenames can be read from stdin by specifying '-'.
196 * Bug fix/enhancement of the transformation from terms to regular
197 * expressons in the search engine.
199 * Revision 1.35 1995/11/27 09:29:00 adam
200 * Bug fixes regarding conversion to regular expressions.
202 * Revision 1.34 1995/11/16 17:00:56 adam
203 * Better logging of rpn query.
205 * Revision 1.33 1995/11/01 13:58:28 quinn
206 * Moving data1 to yaz/retrieval
208 * Revision 1.32 1995/10/27 14:00:11 adam
209 * Implemented detection of database availability.
211 * Revision 1.31 1995/10/17 18:02:10 adam
212 * New feature: databases. Implemented as prefix to words in dictionary.
214 * Revision 1.30 1995/10/16 09:32:38 adam
215 * More work on relational op.
217 * Revision 1.29 1995/10/13 16:01:49 adam
220 * Revision 1.28 1995/10/13 12:26:43 adam
221 * Optimization of truncation.
223 * Revision 1.27 1995/10/12 17:07:22 adam
226 * Revision 1.26 1995/10/12 12:40:54 adam
227 * Bug fixes in rpn_prox.
229 * Revision 1.25 1995/10/10 13:59:24 adam
230 * Function rset_open changed its wflag parameter to general flags.
232 * Revision 1.24 1995/10/09 16:18:37 adam
233 * Function dict_lookup_grep got extra client data parameter.
235 * Revision 1.23 1995/10/06 16:33:37 adam
236 * Use attribute mappings.
238 * Revision 1.22 1995/10/06 15:07:39 adam
239 * Structure 'local-number' handled.
241 * Revision 1.21 1995/10/06 13:52:06 adam
242 * Bug fixes. Handler may abort further scanning.
244 * Revision 1.20 1995/10/06 11:06:33 adam
245 * Scan entries include 'occurrences' now.
247 * Revision 1.19 1995/10/06 10:43:56 adam
248 * Scan added. 'occurrences' in scan entries not set yet.
250 * Revision 1.18 1995/10/04 16:57:20 adam
251 * Key input and merge sort in one pass.
253 * Revision 1.17 1995/10/04 12:55:17 adam
254 * Bug fix in ranked search. Use=Any keys inserted.
256 * Revision 1.16 1995/10/02 16:24:40 adam
257 * Use attribute actually used in search requests.
259 * Revision 1.15 1995/10/02 15:18:52 adam
260 * New member in recRetrieveCtrl: diagnostic.
262 * Revision 1.14 1995/09/28 12:10:32 adam
263 * Bug fixes. Field prefix used in queries.
265 * Revision 1.13 1995/09/18 14:17:50 adam
268 * Revision 1.12 1995/09/15 14:45:21 adam
270 * Work on truncation.
272 * Revision 1.11 1995/09/14 11:53:27 adam
273 * First work on regular expressions/truncations.
275 * Revision 1.10 1995/09/11 15:23:26 adam
276 * More work on relevance search.
278 * Revision 1.9 1995/09/11 13:09:35 adam
279 * More work on relevance feedback.
281 * Revision 1.8 1995/09/08 14:52:27 adam
282 * Minor changes. Dictionary is lower case now.
284 * Revision 1.7 1995/09/07 13:58:36 adam
285 * New parameter: result-set file descriptor (RSFD) to support multiple
286 * positions within the same result-set.
287 * Boolean operators: and, or, not implemented.
288 * Result-set references.
290 * Revision 1.6 1995/09/06 16:11:18 adam
291 * Option: only one word key per file.
293 * Revision 1.5 1995/09/06 10:33:04 adam
294 * More work on present. Some log messages removed.
296 * Revision 1.4 1995/09/05 15:28:40 adam
297 * More work on search engine.
299 * Revision 1.3 1995/09/04 15:20:22 adam
302 * Revision 1.2 1995/09/04 12:33:43 adam
303 * Various cleanup. YAZ util used instead.
305 * Revision 1.1 1995/09/04 09:10:40 adam
306 * More work on index add/del/update.
307 * Merge sort implemented.
308 * Initial work on z39 server.
327 struct rpn_char_map_info {
332 static const char **rpn_char_map_handler (void *vp, const char **from, int len)
334 struct rpn_char_map_info *p = vp;
335 return zebra_maps_input (p->zm, p->reg_type, from, len);
338 static void rpn_char_map_prepare (ZebraHandle zh, int reg_type,
339 struct rpn_char_map_info *map_info)
341 map_info->zm = zh->zebra_maps;
342 map_info->reg_type = reg_type;
343 dict_grep_cmap (zh->dict, map_info, rpn_char_map_handler);
350 Z_AttributesPlusTerm *zapt;
353 static int attr_find (AttrType *src, oid_value *attributeSetP)
358 num_attributes = src->zapt->attributes->num_attributes;
360 num_attributes = src->zapt->num_attributes;
362 while (src->major < num_attributes)
364 Z_AttributeElement *element;
367 element = src->zapt->attributes->attributes[src->major];
369 element = src->zapt->attributeList[src->major];
371 if (src->type == *element->attributeType)
373 switch (element->which)
375 case Z_AttributeValue_numeric:
377 if (element->attributeSet && attributeSetP)
381 attrset = oid_getentbyoid (element->attributeSet);
382 *attributeSetP = attrset->value;
384 return *element->value.numeric;
386 case Z_AttributeValue_complex:
387 if (src->minor >= element->value.complex->num_list ||
388 element->value.complex->list[src->minor]->which !=
389 Z_StringOrNumeric_numeric)
392 if (element->attributeSet && attributeSetP)
396 attrset = oid_getentbyoid (element->attributeSet);
397 *attributeSetP = attrset->value;
399 return *element->value.complex->list[src->minor-1]->u.numeric;
409 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
431 static void term_untrans (ZebraHandle zh, int reg_type,
432 char *dst, const char *src)
436 const char *cp = zebra_maps_output (zh->zebra_maps, reg_type, &src);
443 static void add_isam_p (const char *name, const char *info,
446 if (p->isam_p_indx == p->isam_p_size)
448 ISAM_P *new_isam_p_buf;
452 p->isam_p_size = 2*p->isam_p_size + 100;
453 new_isam_p_buf = xmalloc (sizeof(*new_isam_p_buf) *
457 memcpy (new_isam_p_buf, p->isam_p_buf,
458 p->isam_p_indx * sizeof(*p->isam_p_buf));
459 xfree (p->isam_p_buf);
461 p->isam_p_buf = new_isam_p_buf;
464 new_term_no = xmalloc (sizeof(*new_term_no) *
468 memcpy (new_term_no, p->isam_p_buf,
469 p->isam_p_indx * sizeof(*p->term_no));
472 p->term_no = new_term_no;
475 assert (*info == sizeof(*p->isam_p_buf));
476 memcpy (p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
479 term_untrans (p->zh, p->reg_type, term_tmp, name+2);
480 logf (LOG_DEBUG, "grep: %s", term_tmp);
485 static int grep_handle (char *name, const char *info, void *p)
487 add_isam_p (name, info, p);
491 static int term_pre (ZebraMaps zebra_maps, int reg_type, const char **src,
492 const char *ct1, const char *ct2)
494 const char *s1, *s0 = *src;
497 /* skip white space */
500 if (ct1 && strchr (ct1, *s0))
502 if (ct2 && strchr (ct2, *s0))
505 map = zebra_maps_input (zebra_maps, reg_type, &s1, strlen(s1));
506 if (**map != *CHR_SPACE)
514 static int term_100 (ZebraMaps zebra_maps, int reg_type,
515 const char **src, char *dst, int space_split,
523 if (!term_pre (zebra_maps, reg_type, src, NULL, NULL))
529 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
530 if (space_split && **map == *CHR_SPACE)
534 if (!isalnum (*s1) && *s1 != '-')
546 static int term_101 (ZebraMaps zebra_maps, int reg_type,
547 const char **src, char *dst, int space_split,
555 if (!term_pre (zebra_maps, reg_type, src, "#", "#"))
564 dst_term[j++] = *s0++;
569 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
570 if (space_split && **map == *CHR_SPACE)
582 dst_term[j++] = '\0';
588 static int term_103 (ZebraMaps zebra_maps, int reg_type, const char **src,
589 char *dst, int *errors, int space_split,
597 if (!term_pre (zebra_maps, reg_type, src, "^\\()[].*+?|", "("))
600 if (errors && *s0 == '+' && s0[1] && s0[2] == '+' && s0[3] &&
603 *errors = s0[1] - '0';
610 if (strchr ("^\\()[].*+?|-", *s0))
618 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
619 if (**map == *CHR_SPACE)
636 static int term_102 (ZebraMaps zebra_maps, int reg_type, const char **src,
637 char *dst, int space_split, char *dst_term)
639 return term_103 (zebra_maps, reg_type, src, dst, NULL, space_split,
643 /* gen_regular_rel - generate regular expression from relation
644 * val: border value (inclusive)
645 * islt: 1 if <=; 0 if >=.
647 static void gen_regular_rel (char *dst, int val, int islt)
654 logf (LOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
658 strcpy (dst, "(-[0-9]+|(");
666 strcpy (dst, "([0-9]+|-(");
667 dst_p = strlen (dst);
677 dst_p = strlen (dst);
678 sprintf (numstr, "%d", val);
679 for (w = strlen(numstr); --w >= 0; pos++)
698 strcpy (dst + dst_p, numstr);
699 dst_p = strlen(dst) - pos - 1;
727 for (i = 0; i<pos; i++)
740 for (i=1; i<pos; i++)
741 strcat (dst, "[0-9]?");
745 for (i = 0; i <= pos; i++)
746 strcat (dst, "[0-9]");
747 strcat (dst, "[0-9]*");
752 void string_rel_add_char (char **term_p, const char *src, int *indx)
754 if (src[*indx] == '\\')
755 *(*term_p)++ = src[(*indx)++];
756 *(*term_p)++ = src[(*indx)++];
760 * > abc ([b-].*|a[c-].*|ab[d-].*|abc.+)
761 * ([^-a].*|a[^-b].*ab[^-c].*|abc.+)
762 * >= abc ([b-].*|a[c-].*|ab[c-].*)
763 * ([^-a].*|a[^-b].*|ab[c-].*)
764 * < abc ([-0].*|a[-a].*|ab[-b].*)
765 * ([^a-].*|a[^b-].*|ab[^c-].*)
766 * <= abc ([-0].*|a[-a].*|ab[-b].*|abc)
767 * ([^a-].*|a[^b-].*|ab[^c-].*|abc)
769 static int string_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
770 const char **term_sub, char *term_dict,
771 oid_value attributeSet,
772 int reg_type, int space_split, char *term_dst)
777 char *term_tmp = term_dict + strlen(term_dict);
778 char term_component[256];
780 attr_init (&relation, zapt, 2);
781 relation_value = attr_find (&relation, NULL);
783 logf (LOG_DEBUG, "string relation value=%d", relation_value);
784 switch (relation_value)
787 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
788 space_split, term_dst))
790 logf (LOG_DEBUG, "Relation <");
793 for (i = 0; term_component[i]; )
800 string_rel_add_char (&term_tmp, term_component, &j);
805 string_rel_add_char (&term_tmp, term_component, &i);
816 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
817 space_split, term_dst))
819 logf (LOG_DEBUG, "Relation <=");
822 for (i = 0; term_component[i]; )
827 string_rel_add_char (&term_tmp, term_component, &j);
831 string_rel_add_char (&term_tmp, term_component, &i);
840 for (i = 0; term_component[i]; )
841 string_rel_add_char (&term_tmp, term_component, &i);
846 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
847 space_split, term_dst))
849 logf (LOG_DEBUG, "Relation >");
852 for (i = 0; term_component[i];)
857 string_rel_add_char (&term_tmp, term_component, &j);
862 string_rel_add_char (&term_tmp, term_component, &i);
870 for (i = 0; term_component[i];)
871 string_rel_add_char (&term_tmp, term_component, &i);
878 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
879 space_split, term_dst))
881 logf (LOG_DEBUG, "Relation >=");
884 for (i = 0; term_component[i];)
891 string_rel_add_char (&term_tmp, term_component, &j);
894 if (term_component[i+1])
898 string_rel_add_char (&term_tmp, term_component, &i);
902 string_rel_add_char (&term_tmp, term_component, &i);
914 logf (LOG_DEBUG, "Relation =");
915 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
916 space_split, term_dst))
918 strcat (term_tmp, "(");
919 strcat (term_tmp, term_component);
920 strcat (term_tmp, ")");
925 static int string_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
926 const char **term_sub,
927 oid_value attributeSet, NMEM stream,
928 struct grep_info *grep_info,
929 int reg_type, int complete_flag,
930 int num_bases, char **basenames,
933 char term_dict[2*IT_MAX_WORD+4000];
936 int truncation_value;
939 oid_value curAttributeSet = attributeSet;
941 struct rpn_char_map_info rcmi;
942 int space_split = complete_flag ? 0 : 1;
944 rpn_char_map_prepare (zh, reg_type, &rcmi);
945 attr_init (&use, zapt, 1);
946 use_value = attr_find (&use, &curAttributeSet);
947 logf (LOG_DEBUG, "string_term, use value %d", use_value);
948 attr_init (&truncation, zapt, 5);
949 truncation_value = attr_find (&truncation, NULL);
950 logf (LOG_DEBUG, "truncation value %d", truncation_value);
955 for (base_no = 0; base_no < num_bases; base_no++)
958 data1_local_attribute *local_attr;
959 int max_pos, prefix_len = 0;
962 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
964 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
965 curAttributeSet, use_value, r);
969 sprintf (val_str, "%d", use_value);
971 zh->errString = nmem_strdup (stream, val_str);
976 struct oident oident;
978 oident.proto = PROTO_Z3950;
979 oident.oclass = CLASS_ATTSET;
980 oident.value = curAttributeSet;
981 oid_ent_to_oid (&oident, oid);
984 zh->errString = nmem_strdup (stream, oident.desc);
988 if (zebraExplain_curDatabase (zh->zei, basenames[base_no]))
990 zh->errCode = 109; /* Database unavailable */
991 zh->errString = basenames[base_no];
994 for (local_attr = attp.local_attributes; local_attr;
995 local_attr = local_attr->next)
1001 ord = zebraExplain_lookupSU (zh->zei, attp.attset_ordinal,
1006 term_dict[prefix_len++] = '|';
1008 term_dict[prefix_len++] = '(';
1010 ord_len = key_SU_code (ord, ord_buf);
1011 for (i = 0; i<ord_len; i++)
1013 term_dict[prefix_len++] = 1;
1014 term_dict[prefix_len++] = ord_buf[i];
1022 term_dict[prefix_len++] = ')';
1023 term_dict[prefix_len++] = 1;
1024 term_dict[prefix_len++] = reg_type;
1025 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1026 term_dict[prefix_len] = '\0';
1028 switch (truncation_value)
1030 case -1: /* not specified */
1031 case 100: /* do not truncate */
1032 if (!string_relation (zh, zapt, &termp, term_dict,
1034 reg_type, space_split, term_dst))
1036 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_dict+prefix_len);
1037 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info, &max_pos,
1040 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1042 case 1: /* right truncation */
1043 term_dict[j++] = '(';
1044 if (!term_100 (zh->zebra_maps, reg_type,
1045 &termp, term_dict + j, space_split, term_dst))
1047 strcat (term_dict, ".*)");
1048 dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1049 &max_pos, 0, grep_handle);
1051 case 2: /* keft truncation */
1052 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1053 if (!term_100 (zh->zebra_maps, reg_type,
1054 &termp, term_dict + j, space_split, term_dst))
1056 strcat (term_dict, ")");
1057 dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1058 &max_pos, 0, grep_handle);
1060 case 3: /* left&right truncation */
1061 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1062 if (!term_100 (zh->zebra_maps, reg_type,
1063 &termp, term_dict + j, space_split, term_dst))
1065 strcat (term_dict, ".*)");
1066 dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1067 &max_pos, 0, grep_handle);
1071 case 101: /* process # in term */
1072 term_dict[j++] = '(';
1073 if (!term_101 (zh->zebra_maps, reg_type,
1074 &termp, term_dict + j, space_split, term_dst))
1076 strcat (term_dict, ")");
1077 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1078 &max_pos, 0, grep_handle);
1080 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d", r);
1082 case 102: /* Regexp-1 */
1083 term_dict[j++] = '(';
1084 if (!term_102 (zh->zebra_maps, reg_type,
1085 &termp, term_dict + j, space_split, term_dst))
1087 strcat (term_dict, ")");
1088 logf (LOG_DEBUG, "Regexp-1 tolerance=%d", r);
1089 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1090 &max_pos, 0, grep_handle);
1092 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
1095 case 103: /* Regexp-2 */
1097 term_dict[j++] = '(';
1098 if (!term_103 (zh->zebra_maps, reg_type,
1099 &termp, term_dict + j, &r, space_split, term_dst))
1101 strcat (term_dict, ")");
1102 logf (LOG_DEBUG, "Regexp-2 tolerance=%d", r);
1103 r = dict_lookup_grep (zh->dict, term_dict, r, grep_info,
1104 &max_pos, 2, grep_handle);
1106 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
1112 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1116 static void trans_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1120 Z_Term *term = zapt->term;
1122 sizez = term->u.general->len;
1123 if (sizez > IT_MAX_WORD-1)
1124 sizez = IT_MAX_WORD-1;
1125 memcpy (termz, term->u.general->buf, sizez);
1126 termz[sizez] = '\0';
1129 static void trans_scan_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1130 char *termz, int reg_type)
1132 Z_Term *term = zapt->term;
1134 const char *cp = (const char *) term->u.general->buf;
1135 const char *cp_end = cp + term->u.general->len;
1138 const char *space_map = NULL;
1141 while ((len = (cp_end - cp)) > 0)
1143 map = zebra_maps_input (zh->zebra_maps, reg_type, &cp, len);
1144 if (**map == *CHR_SPACE)
1149 for (src = space_map; *src; src++)
1152 for (src = *map; *src; src++)
1159 static RSET rpn_proximity (ZebraHandle zh, RSET rset1, RSET rset2,
1161 int exclusion, int relation, int distance)
1166 struct it_key buf1, buf2;
1169 rset_temp_parms parms;
1172 rsfd1 = rset_open (rset1, RSETF_READ);
1173 more1 = rset_read (rset1, rsfd1, &buf1, &term_index);
1175 rsfd2 = rset_open (rset2, RSETF_READ);
1176 more2 = rset_read (rset2, rsfd2, &buf2, &term_index);
1178 parms.key_size = sizeof (struct it_key);
1179 parms.temp_path = res_get (zh->res, "setTmpDir");
1180 result = rset_create (rset_kind_temp, &parms);
1181 rsfd_result = rset_open (result, RSETF_WRITE);
1183 logf (LOG_DEBUG, "rpn_proximity excl=%d ord=%d rel=%d dis=%d",
1184 exclusion, ordered, relation, distance);
1185 while (more1 && more2)
1187 int cmp = key_compare_it (&buf1, &buf2);
1189 more1 = rset_read (rset1, rsfd1, &buf1, &term_index);
1191 more2 = rset_read (rset2, rsfd2, &buf2, &term_index);
1194 int sysno = buf1.sysno;
1198 seqno[n++] = buf1.seqno;
1199 while ((more1 = rset_read (rset1, rsfd1, &buf1, &term_index)) &&
1200 sysno == buf1.sysno)
1202 seqno[n++] = buf1.seqno;
1205 for (i = 0; i<n; i++)
1207 int diff = buf2.seqno - seqno[i];
1208 int excl = exclusion;
1209 if (!ordered && diff < 0)
1214 if (diff < distance)
1218 if (diff <= distance)
1222 if (diff == distance)
1226 if (diff >= distance)
1230 if (diff > distance)
1234 if (diff != distance)
1239 rset_write (result, rsfd_result, &buf2);
1241 } while ((more2 = rset_read (rset2, rsfd2, &buf2, &term_index)) &&
1242 sysno == buf2.sysno);
1245 rset_close (result, rsfd_result);
1246 rset_close (rset1, rsfd1);
1247 rset_close (rset2, rsfd2);
1251 static RSET rpn_prox (ZebraHandle zh, RSET *rset, int rset_no)
1256 struct it_key **buf;
1258 char prox_term[1024];
1259 int length_prox_term = 0;
1260 int min_nn = 10000000;
1262 const char *flags = NULL;
1264 rsfd = xmalloc (sizeof(*rsfd)*rset_no);
1265 more = xmalloc (sizeof(*more)*rset_no);
1266 buf = xmalloc (sizeof(*buf)*rset_no);
1268 for (i = 0; i<rset_no; i++)
1271 buf[i] = xmalloc (sizeof(**buf));
1272 rsfd[i] = rset_open (rset[i], RSETF_READ);
1273 if (!(more[i] = rset_read (rset[i], rsfd[i], buf[i], &term_index)))
1275 for (j = 0; j<rset[i]->no_rset_terms; j++)
1277 const char *nflags = rset[i]->rset_terms[j]->flags;
1278 char *term = rset[i]->rset_terms[j]->name;
1279 int lterm = strlen(term);
1280 if (length_prox_term)
1281 prox_term[length_prox_term++] = ' ';
1282 strcpy (prox_term + length_prox_term, term);
1283 length_prox_term += lterm;
1284 if (min_nn > rset[i]->rset_terms[j]->nn)
1285 min_nn = rset[i]->rset_terms[j]->nn;
1291 rset_null_parms parms;
1295 rset_close (rset[i], rsfd[i]);
1299 parms.rset_term = rset_term_create (prox_term, -1, flags);
1300 parms.rset_term->nn = 0;
1301 result = rset_create (rset_kind_null, &parms);
1305 rset_temp_parms parms;
1308 parms.rset_term = rset_term_create (prox_term, -1, flags);
1309 parms.rset_term->nn = min_nn;
1310 parms.key_size = sizeof (struct it_key);
1311 parms.temp_path = res_get (zh->res, "setTmpDir");
1312 result = rset_create (rset_kind_temp, &parms);
1313 rsfd_result = rset_open (result, RSETF_WRITE);
1317 for (i = 1; i<rset_no; i++)
1326 cmp = key_compare_it (buf[i], buf[i-1]);
1329 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1330 buf[i-1], &term_index);
1335 if (buf[i-1]->seqno+1 != buf[i]->seqno)
1337 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1338 buf[i-1], &term_index);
1344 more[i] = rset_read (rset[i], rsfd[i], buf[i],
1351 rset_write (result, rsfd_result, buf[0]);
1352 more[0] = rset_read (*rset, *rsfd, *buf, &term_index);
1356 for (i = 0; i<rset_no; i++)
1358 rset_close (rset[i], rsfd[i]);
1361 rset_close (result, rsfd_result);
1369 static RSET rpn_search_APT_phrase (ZebraHandle zh,
1370 Z_AttributesPlusTerm *zapt,
1372 oid_value attributeSet,
1374 int reg_type, int complete_flag,
1375 const char *rank_type,
1376 int num_bases, char **basenames)
1378 char term_dst[IT_MAX_WORD+1];
1379 const char *termp = termz;
1380 RSET rset[60], result;
1381 int i, r, rset_no = 0;
1382 struct grep_info grep_info;
1385 grep_info.term_no = 0;
1387 grep_info.isam_p_size = 0;
1388 grep_info.isam_p_buf = NULL;
1390 grep_info.reg_type = reg_type;
1394 logf (LOG_DEBUG, "APT_phrase termp=%s", termp);
1395 grep_info.isam_p_indx = 0;
1396 r = string_term (zh, zapt, &termp, attributeSet, stream, &grep_info,
1397 reg_type, complete_flag, num_bases, basenames,
1401 logf (LOG_DEBUG, "term: %s", term_dst);
1402 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1403 grep_info.isam_p_indx, term_dst,
1404 strlen(term_dst), rank_type);
1405 assert (rset[rset_no]);
1406 if (++rset_no >= sizeof(rset)/sizeof(*rset))
1410 xfree(grep_info.term_no);
1412 xfree (grep_info.isam_p_buf);
1415 rset_null_parms parms;
1417 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1418 return rset_create (rset_kind_null, &parms);
1420 else if (rset_no == 1)
1422 result = rpn_prox (zh, rset, rset_no);
1423 for (i = 0; i<rset_no; i++)
1424 rset_delete (rset[i]);
1428 static RSET rpn_search_APT_or_list (ZebraHandle zh,
1429 Z_AttributesPlusTerm *zapt,
1431 oid_value attributeSet,
1433 int reg_type, int complete_flag,
1434 const char *rank_type,
1435 int num_bases, char **basenames)
1437 char term_dst[IT_MAX_WORD+1];
1438 const char *termp = termz;
1439 RSET rset[60], result;
1440 int i, r, rset_no = 0;
1441 struct grep_info grep_info;
1444 grep_info.term_no = 0;
1446 grep_info.isam_p_size = 0;
1447 grep_info.isam_p_buf = NULL;
1449 grep_info.reg_type = reg_type;
1453 logf (LOG_DEBUG, "APT_or_list termp=%s", termp);
1454 grep_info.isam_p_indx = 0;
1455 r = string_term (zh, zapt, &termp, attributeSet, stream, &grep_info,
1456 reg_type, complete_flag, num_bases, basenames,
1460 logf (LOG_DEBUG, "term: %s", term_dst);
1461 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1462 grep_info.isam_p_indx, term_dst,
1463 strlen(term_dst), rank_type);
1464 assert (rset[rset_no]);
1465 if (++rset_no >= sizeof(rset)/sizeof(*rset))
1469 xfree(grep_info.term_no);
1471 xfree (grep_info.isam_p_buf);
1474 rset_null_parms parms;
1476 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1477 return rset_create (rset_kind_null, &parms);
1480 for (i = 1; i<rset_no; i++)
1482 rset_bool_parms bool_parms;
1484 bool_parms.rset_l = result;
1485 bool_parms.rset_r = rset[i];
1486 bool_parms.key_size = sizeof(struct it_key);
1487 bool_parms.cmp = key_compare_it;
1488 result = rset_create (rset_kind_or, &bool_parms);
1493 static RSET rpn_search_APT_and_list (ZebraHandle zh,
1494 Z_AttributesPlusTerm *zapt,
1496 oid_value attributeSet,
1498 int reg_type, int complete_flag,
1499 const char *rank_type,
1500 int num_bases, char **basenames)
1502 char term_dst[IT_MAX_WORD+1];
1503 const char *termp = termz;
1504 RSET rset[60], result;
1505 int i, r, rset_no = 0;
1506 struct grep_info grep_info;
1509 grep_info.term_no = 0;
1511 grep_info.isam_p_size = 0;
1512 grep_info.isam_p_buf = NULL;
1514 grep_info.reg_type = reg_type;
1518 logf (LOG_DEBUG, "APT_and_list termp=%s", termp);
1519 grep_info.isam_p_indx = 0;
1520 r = string_term (zh, zapt, &termp, attributeSet, stream, &grep_info,
1521 reg_type, complete_flag, num_bases, basenames,
1525 logf (LOG_DEBUG, "term: %s", term_dst);
1526 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1527 grep_info.isam_p_indx, term_dst,
1528 strlen(term_dst), rank_type);
1529 assert (rset[rset_no]);
1530 if (++rset_no >= sizeof(rset)/sizeof(*rset))
1534 xfree(grep_info.term_no);
1536 xfree (grep_info.isam_p_buf);
1539 rset_null_parms parms;
1541 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1542 return rset_create (rset_kind_null, &parms);
1545 for (i = 1; i<rset_no; i++)
1547 rset_bool_parms bool_parms;
1549 bool_parms.rset_l = result;
1550 bool_parms.rset_r = rset[i];
1551 bool_parms.key_size = sizeof(struct it_key);
1552 bool_parms.cmp = key_compare_it;
1553 result = rset_create (rset_kind_and, &bool_parms);
1558 static int numeric_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1559 const char **term_sub,
1561 oid_value attributeSet,
1562 struct grep_info *grep_info,
1571 char *term_tmp = term_dict + strlen(term_dict);
1573 attr_init (&relation, zapt, 2);
1574 relation_value = attr_find (&relation, NULL);
1576 logf (LOG_DEBUG, "numeric relation value=%d", relation_value);
1578 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_tmp, 1,
1581 term_value = atoi (term_tmp);
1582 switch (relation_value)
1585 logf (LOG_DEBUG, "Relation <");
1586 gen_regular_rel (term_tmp, term_value-1, 1);
1589 logf (LOG_DEBUG, "Relation <=");
1590 gen_regular_rel (term_tmp, term_value, 1);
1593 logf (LOG_DEBUG, "Relation >=");
1594 gen_regular_rel (term_tmp, term_value, 0);
1597 logf (LOG_DEBUG, "Relation >");
1598 gen_regular_rel (term_tmp, term_value+1, 0);
1602 logf (LOG_DEBUG, "Relation =");
1603 sprintf (term_tmp, "(0*%d)", term_value);
1605 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_tmp);
1606 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info, max_pos,
1609 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1610 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1614 static int numeric_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1615 const char **term_sub,
1616 oid_value attributeSet, struct grep_info *grep_info,
1617 int reg_type, int complete_flag,
1618 int num_bases, char **basenames,
1621 char term_dict[2*IT_MAX_WORD+2];
1625 oid_value curAttributeSet = attributeSet;
1627 struct rpn_char_map_info rcmi;
1629 rpn_char_map_prepare (zh, reg_type, &rcmi);
1630 attr_init (&use, zapt, 1);
1631 use_value = attr_find (&use, &curAttributeSet);
1632 logf (LOG_DEBUG, "numeric_term, use value %d", use_value);
1634 if (use_value == -1)
1637 for (base_no = 0; base_no < num_bases; base_no++)
1640 data1_local_attribute *local_attr;
1641 int max_pos, prefix_len = 0;
1644 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
1646 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1647 curAttributeSet, use_value, r);
1654 if (zebraExplain_curDatabase (zh->zei, basenames[base_no]))
1656 zh->errCode = 109; /* Database unavailable */
1657 zh->errString = basenames[base_no];
1660 for (local_attr = attp.local_attributes; local_attr;
1661 local_attr = local_attr->next)
1667 ord = zebraExplain_lookupSU (zh->zei, attp.attset_ordinal,
1672 term_dict[prefix_len++] = '|';
1674 term_dict[prefix_len++] = '(';
1676 ord_len = key_SU_code (ord, ord_buf);
1677 for (i = 0; i<ord_len; i++)
1679 term_dict[prefix_len++] = 1;
1680 term_dict[prefix_len++] = ord_buf[i];
1688 term_dict[prefix_len++] = ')';
1689 term_dict[prefix_len++] = 1;
1690 term_dict[prefix_len++] = reg_type;
1691 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1692 term_dict[prefix_len] = '\0';
1693 if (!numeric_relation (zh, zapt, &termp, term_dict,
1694 attributeSet, grep_info, &max_pos, reg_type,
1699 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1703 static RSET rpn_search_APT_numeric (ZebraHandle zh,
1704 Z_AttributesPlusTerm *zapt,
1706 oid_value attributeSet,
1708 int reg_type, int complete_flag,
1709 const char *rank_type,
1710 int num_bases, char **basenames)
1712 char term_dst[IT_MAX_WORD+1];
1713 const char *termp = termz;
1714 RSET rset[60], result;
1715 int i, r, rset_no = 0;
1716 struct grep_info grep_info;
1719 grep_info.term_no = 0;
1721 grep_info.isam_p_size = 0;
1722 grep_info.isam_p_buf = NULL;
1724 grep_info.reg_type = reg_type;
1728 logf (LOG_DEBUG, "APT_numeric termp=%s", termp);
1729 grep_info.isam_p_indx = 0;
1730 r = numeric_term (zh, zapt, &termp, attributeSet, &grep_info,
1731 reg_type, complete_flag, num_bases, basenames,
1735 logf (LOG_DEBUG, "term: %s", term_dst);
1736 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1737 grep_info.isam_p_indx, term_dst,
1738 strlen(term_dst), rank_type);
1739 assert (rset[rset_no]);
1740 if (++rset_no >= sizeof(rset)/sizeof(*rset))
1744 xfree(grep_info.term_no);
1746 xfree (grep_info.isam_p_buf);
1749 rset_null_parms parms;
1751 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1752 return rset_create (rset_kind_null, &parms);
1755 for (i = 1; i<rset_no; i++)
1757 rset_bool_parms bool_parms;
1759 bool_parms.rset_l = result;
1760 bool_parms.rset_r = rset[i];
1761 bool_parms.key_size = sizeof(struct it_key);
1762 bool_parms.cmp = key_compare_it;
1763 result = rset_create (rset_kind_and, &bool_parms);
1768 static RSET rpn_search_APT_local (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1770 oid_value attributeSet,
1772 const char *rank_type)
1777 rset_temp_parms parms;
1779 parms.rset_term = rset_term_create (termz, -1, rank_type);
1780 parms.key_size = sizeof (struct it_key);
1781 parms.temp_path = res_get (zh->res, "setTmpDir");
1782 result = rset_create (rset_kind_temp, &parms);
1783 rsfd = rset_open (result, RSETF_WRITE);
1785 key.sysno = atoi (termz);
1789 rset_write (result, rsfd, &key);
1790 rset_close (result, rsfd);
1794 static RSET rpn_sort_spec (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1795 oid_value attributeSet, NMEM stream,
1796 Z_SortKeySpecList *sort_sequence,
1797 const char *rank_type)
1799 rset_null_parms parms;
1801 int sort_relation_value;
1802 AttrType sort_relation_type;
1807 Z_AttributeElement *ae;
1811 attr_init (&sort_relation_type, zapt, 7);
1812 sort_relation_value = attr_find (&sort_relation_type, &attributeSet);
1814 attr_init (&use_type, zapt, 1);
1815 use_value = attr_find (&use_type, &attributeSet);
1817 if (!sort_sequence->specs)
1819 sort_sequence->num_specs = 10;
1820 sort_sequence->specs = nmem_malloc (stream, sort_sequence->num_specs *
1821 sizeof(*sort_sequence->specs));
1822 for (i = 0; i<sort_sequence->num_specs; i++)
1823 sort_sequence->specs[i] = 0;
1825 if (zapt->term->which != Z_Term_general)
1828 i = atoi_n (zapt->term->u.general->buf, zapt->term->u.general->len);
1829 if (i >= sort_sequence->num_specs)
1832 oe.proto = PROTO_Z3950;
1833 oe.oclass = CLASS_ATTSET;
1834 oe.value = attributeSet;
1835 if (!oid_ent_to_oid (&oe, oid))
1838 sks = nmem_malloc (stream, sizeof(*sks));
1839 sks->sortElement = nmem_malloc (stream, sizeof(*sks->sortElement));
1840 sks->sortElement->which = Z_SortElement_generic;
1841 sk = sks->sortElement->u.generic = nmem_malloc (stream, sizeof(*sk));
1842 sk->which = Z_SortKey_sortAttributes;
1843 sk->u.sortAttributes = nmem_malloc (stream, sizeof(*sk->u.sortAttributes));
1845 sk->u.sortAttributes->id = oid;
1846 sk->u.sortAttributes->list =
1847 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list));
1848 sk->u.sortAttributes->list->num_attributes = 1;
1849 sk->u.sortAttributes->list->attributes =
1850 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list->attributes));
1851 ae = *sk->u.sortAttributes->list->attributes =
1852 nmem_malloc (stream, sizeof(**sk->u.sortAttributes->list->attributes));
1853 ae->attributeSet = 0;
1854 ae->attributeType = nmem_malloc (stream, sizeof(*ae->attributeType));
1855 *ae->attributeType = 1;
1856 ae->which = Z_AttributeValue_numeric;
1857 ae->value.numeric = nmem_malloc (stream, sizeof(*ae->value.numeric));
1858 *ae->value.numeric = use_value;
1860 sks->sortRelation = nmem_malloc (stream, sizeof(*sks->sortRelation));
1861 if (sort_relation_value == 1)
1862 *sks->sortRelation = Z_SortRelation_ascending;
1863 else if (sort_relation_value == 2)
1864 *sks->sortRelation = Z_SortRelation_descending;
1866 *sks->sortRelation = Z_SortRelation_ascending;
1868 sks->caseSensitivity = nmem_malloc (stream, sizeof(*sks->caseSensitivity));
1869 *sks->caseSensitivity = 0;
1872 sks->which = Z_SortKeySpec_null;
1873 sks->u.null = odr_nullval ();
1875 sks->missingValueAction = 0;
1878 sort_sequence->specs[i] = sks;
1880 parms.rset_term = rset_term_create ("", -1, rank_type);
1881 return rset_create (rset_kind_null, &parms);
1885 static RSET rpn_search_APT (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1886 oid_value attributeSet, NMEM stream,
1887 Z_SortKeySpecList *sort_sequence,
1888 int num_bases, char **basenames)
1891 char *search_type = NULL;
1892 char *rank_type = NULL;
1895 char termz[IT_MAX_WORD+1];
1897 zebra_maps_attr (zh->zebra_maps, zapt, ®_id, &search_type,
1898 &rank_type, &complete_flag, &sort_flag);
1900 logf (LOG_DEBUG, "reg_id=%c", reg_id);
1901 logf (LOG_DEBUG, "complete_flag=%d", complete_flag);
1902 logf (LOG_DEBUG, "search_type=%s", search_type);
1903 logf (LOG_DEBUG, "rank_type=%s", rank_type);
1905 if (zapt->term->which != Z_Term_general)
1910 trans_term (zh, zapt, termz);
1913 return rpn_sort_spec (zh, zapt, attributeSet, stream, sort_sequence,
1916 if (!strcmp (search_type, "phrase"))
1918 return rpn_search_APT_phrase (zh, zapt, termz, attributeSet, stream,
1919 reg_id, complete_flag, rank_type,
1920 num_bases, basenames);
1922 else if (!strcmp (search_type, "and-list"))
1924 return rpn_search_APT_and_list (zh, zapt, termz, attributeSet, stream,
1925 reg_id, complete_flag, rank_type,
1926 num_bases, basenames);
1928 else if (!strcmp (search_type, "or-list"))
1930 return rpn_search_APT_or_list (zh, zapt, termz, attributeSet, stream,
1931 reg_id, complete_flag, rank_type,
1932 num_bases, basenames);
1934 else if (!strcmp (search_type, "local"))
1936 return rpn_search_APT_local (zh, zapt, termz, attributeSet, stream,
1939 else if (!strcmp (search_type, "numeric"))
1941 return rpn_search_APT_numeric (zh, zapt, termz, attributeSet, stream,
1942 reg_id, complete_flag, rank_type,
1943 num_bases, basenames);
1949 static RSET rpn_search_structure (ZebraHandle zh, Z_RPNStructure *zs,
1950 oid_value attributeSet, NMEM stream,
1951 Z_SortKeySpecList *sort_sequence,
1952 int num_bases, char **basenames)
1955 if (zs->which == Z_RPNStructure_complex)
1957 Z_Operator *zop = zs->u.complex->roperator;
1958 rset_bool_parms bool_parms;
1960 bool_parms.rset_l = rpn_search_structure (zh, zs->u.complex->s1,
1961 attributeSet, stream,
1963 num_bases, basenames);
1964 if (bool_parms.rset_l == NULL)
1966 bool_parms.rset_r = rpn_search_structure (zh, zs->u.complex->s2,
1967 attributeSet, stream,
1969 num_bases, basenames);
1970 if (bool_parms.rset_r == NULL)
1972 rset_delete (bool_parms.rset_l);
1975 bool_parms.key_size = sizeof(struct it_key);
1976 bool_parms.cmp = key_compare_it;
1980 case Z_Operator_and:
1981 r = rset_create (rset_kind_and, &bool_parms);
1984 r = rset_create (rset_kind_or, &bool_parms);
1986 case Z_Operator_and_not:
1987 r = rset_create (rset_kind_not, &bool_parms);
1989 case Z_Operator_prox:
1991 if (zop->u.prox->which != Z_ProximityOperator_known)
1997 if (zop->u.prox->which != Z_ProxCode_known)
2005 if (*zop->u.prox->u.known != Z_ProxUnit_word)
2007 char *val = nmem_malloc (stream, 16);
2009 zh->errString = val;
2010 sprintf (val, "%d", *zop->u.prox->u.known);
2014 if (*zop->u.prox->proximityUnitCode != Z_ProxUnit_word)
2016 char *val = nmem_malloc (stream, 16);
2018 zh->errString = val;
2019 sprintf (val, "%d", *zop->u.prox->proximityUnitCode);
2023 r = rpn_proximity (zh, bool_parms.rset_l, bool_parms.rset_r,
2024 *zop->u.prox->ordered,
2025 (!zop->u.prox->exclusion ? 0 :
2026 *zop->u.prox->exclusion),
2027 *zop->u.prox->relationType,
2028 *zop->u.prox->distance);
2035 else if (zs->which == Z_RPNStructure_simple)
2037 if (zs->u.simple->which == Z_Operand_APT)
2039 logf (LOG_DEBUG, "rpn_search_APT");
2040 r = rpn_search_APT (zh, zs->u.simple->u.attributesPlusTerm,
2041 attributeSet, stream, sort_sequence,
2042 num_bases, basenames);
2044 else if (zs->u.simple->which == Z_Operand_resultSetId)
2046 logf (LOG_DEBUG, "rpn_search_ref");
2047 r = resultSetRef (zh, zs->u.simple->u.resultSetId);
2049 r = rset_create (rset_kind_null, NULL);
2066 RSET rpn_search (ZebraHandle zh, NMEM nmem,
2067 Z_RPNQuery *rpn, int num_bases, char **basenames,
2068 const char *setname,
2073 oid_value attributeSet;
2074 Z_SortKeySpecList *sort_sequence;
2078 zh->errString = NULL;
2081 sort_sequence = nmem_malloc (nmem, sizeof(*sort_sequence));
2082 sort_sequence->num_specs = 10;
2083 sort_sequence->specs = nmem_malloc (nmem, sort_sequence->num_specs *
2084 sizeof(*sort_sequence->specs));
2085 for (i = 0; i<sort_sequence->num_specs; i++)
2086 sort_sequence->specs[i] = 0;
2088 attrset = oid_getentbyoid (rpn->attributeSetId);
2089 attributeSet = attrset->value;
2090 rset = rpn_search_structure (zh, rpn->RPNStructure, attributeSet,
2091 nmem, sort_sequence, num_bases, basenames);
2096 logf (LOG_DEBUG, "search error: %d", zh->errCode);
2098 for (i = 0; sort_sequence->specs[i]; i++)
2100 sort_sequence->num_specs = i;
2102 resultSetRank (zh, sset, rset);
2105 logf (LOG_DEBUG, "resultSetSortSingle in rpn_search");
2106 resultSetSortSingle (zh, nmem, sset, rset,
2107 sort_sequence, &sort_status);
2110 logf (LOG_DEBUG, "resultSetSortSingle status = %d", zh->errCode);
2116 struct scan_info_entry {
2122 struct scan_info_entry *list;
2128 static int scan_handle (char *name, const char *info, int pos, void *client)
2130 int len_prefix, idx;
2131 struct scan_info *scan_info = client;
2133 len_prefix = strlen(scan_info->prefix);
2134 if (memcmp (name, scan_info->prefix, len_prefix))
2137 idx = scan_info->after - pos + scan_info->before;
2140 scan_info->list[idx].term = odr_malloc (scan_info->odr,
2141 strlen(name + len_prefix)+1);
2142 strcpy (scan_info->list[idx].term, name + len_prefix);
2143 assert (*info == sizeof(ISAM_P));
2144 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAM_P));
2148 static void scan_term_untrans (ZebraHandle zh, NMEM stream, int reg_type,
2149 char **dst, const char *src)
2151 char term_dst[1024];
2153 term_untrans (zh, reg_type, term_dst, src);
2155 *dst = nmem_malloc (stream, strlen(term_dst)+1);
2156 strcpy (*dst, term_dst);
2159 static void count_set (RSET r, int *count)
2167 logf (LOG_DEBUG, "count_set");
2170 rfd = rset_open (r, RSETF_READ);
2171 while (rset_read (r, rfd, &key, &term_index))
2173 if (key.sysno != psysno)
2180 rset_close (r, rfd);
2181 logf (LOG_DEBUG, "%d keys, %d records", kno, *count);
2184 void rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
2185 oid_value attributeset,
2186 int num_bases, char **basenames,
2187 int *position, int *num_entries, ZebraScanEntry **list,
2191 int pos = *position;
2192 int num = *num_entries;
2196 char termz[IT_MAX_WORD+20];
2199 struct scan_info *scan_info_array;
2200 ZebraScanEntry *glist;
2201 int ords[32], ord_no = 0;
2205 char *search_type = NULL;
2206 char *rank_type = NULL;
2210 if (attributeset == VAL_NONE)
2211 attributeset = VAL_BIB1;
2213 zlog_scan (zapt, attributeset);
2214 logf (LOG_DEBUG, "position = %d, num = %d", pos, num);
2216 attr_init (&use, zapt, 1);
2217 use_value = attr_find (&use, &attributeset);
2219 if (zebra_maps_attr (zh->zebra_maps, zapt, ®_id, &search_type,
2220 &rank_type, &complete_flag, &sort_flag))
2226 if (use_value == -1)
2228 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
2232 data1_local_attribute *local_attr;
2234 if ((r=att_getentbyatt (zh, &attp, attributeset, use_value)))
2236 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
2237 attributeset, use_value);
2243 if (zebraExplain_curDatabase (zh->zei, basenames[base_no]))
2245 zh->errString = basenames[base_no];
2246 zh->errCode = 109; /* Database unavailable */
2249 for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
2250 local_attr = local_attr->next)
2254 ord = zebraExplain_lookupSU (zh->zei, attp.attset_ordinal,
2257 ords[ord_no++] = ord;
2265 /* prepare dictionary scanning */
2268 scan_info_array = odr_malloc (stream, ord_no * sizeof(*scan_info_array));
2269 for (i = 0; i < ord_no; i++)
2271 int j, prefix_len = 0;
2272 int before_tmp = before, after_tmp = after;
2273 struct scan_info *scan_info = scan_info_array + i;
2274 struct rpn_char_map_info rcmi;
2276 rpn_char_map_prepare (zh, reg_id, &rcmi);
2278 scan_info->before = before;
2279 scan_info->after = after;
2280 scan_info->odr = stream;
2282 scan_info->list = odr_malloc (stream, (before+after)*
2283 sizeof(*scan_info->list));
2284 for (j = 0; j<before+after; j++)
2285 scan_info->list[j].term = NULL;
2287 prefix_len += key_SU_code (ords[i], termz + prefix_len);
2288 termz[prefix_len++] = reg_id;
2289 termz[prefix_len] = 0;
2290 strcpy (scan_info->prefix, termz);
2292 trans_scan_term (zh, zapt, termz+prefix_len, reg_id);
2294 dict_scan (zh->dict, termz, &before_tmp, &after_tmp, scan_info,
2297 glist = odr_malloc (stream, (before+after)*sizeof(*glist));
2299 /* consider terms after main term */
2300 for (i = 0; i < ord_no; i++)
2304 for (i = 0; i<after; i++)
2307 const char *mterm = NULL;
2311 for (j = 0; j < ord_no; j++)
2313 if (ptr[j] < before+after &&
2314 (tst=scan_info_array[j].list[ptr[j]].term) &&
2315 (!mterm || strcmp (tst, mterm) < 0))
2323 scan_term_untrans (zh, stream->mem, reg_id,
2324 &glist[i+before].term, mterm);
2325 rset = rset_trunc (zh, &scan_info_array[j0].list[ptr[j0]].isam_p, 1,
2326 glist[i+before].term, strlen(glist[i+before].term),
2330 for (j = j0+1; j<ord_no; j++)
2332 if (ptr[j] < before+after &&
2333 (tst=scan_info_array[j].list[ptr[j]].term) &&
2334 !strcmp (tst, mterm))
2336 rset_bool_parms bool_parms;
2340 rset_trunc (zh, &scan_info_array[j].list[ptr[j]].isam_p, 1,
2341 glist[i+before].term,
2342 strlen(glist[i+before].term), NULL);
2344 bool_parms.key_size = sizeof(struct it_key);
2345 bool_parms.cmp = key_compare_it;
2346 bool_parms.rset_l = rset;
2347 bool_parms.rset_r = rset2;
2349 rset = rset_create (rset_kind_or, &bool_parms);
2354 count_set (rset, &glist[i+before].occurrences);
2359 *num_entries -= (after-i);
2363 /* consider terms before main term */
2364 for (i = 0; i<ord_no; i++)
2367 for (i = 0; i<before; i++)
2370 const char *mterm = NULL;
2374 for (j = 0; j <ord_no; j++)
2376 if (ptr[j] < before &&
2377 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2378 (!mterm || strcmp (tst, mterm) > 0))
2387 scan_term_untrans (zh, stream->mem, reg_id,
2388 &glist[before-1-i].term, mterm);
2391 (zh, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1,
2392 glist[before-1-i].term, strlen(glist[before-1-i].term),
2397 for (j = j0+1; j<ord_no; j++)
2399 if (ptr[j] < before &&
2400 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2401 !strcmp (tst, mterm))
2403 rset_bool_parms bool_parms;
2406 rset2 = rset_trunc (zh,
2407 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1,
2408 glist[before-1-i].term,
2409 strlen(glist[before-1-i].term), NULL);
2411 bool_parms.key_size = sizeof(struct it_key);
2412 bool_parms.cmp = key_compare_it;
2413 bool_parms.rset_l = rset;
2414 bool_parms.rset_r = rset2;
2416 rset = rset_create (rset_kind_or, &bool_parms);
2421 count_set (rset, &glist[before-1-i].occurrences);
2431 *list = glist + i; /* list is set to first 'real' entry */
2433 logf (LOG_DEBUG, "position = %d, num_entries = %d",
2434 *position, *num_entries);
2436 logf (LOG_DEBUG, "scan error: %d", zh->errCode);