2 * Copyright (C) 1995-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.93 1999-06-17 14:38:40 adam
8 * Bug fix: Scan SEGV'ed when getting unknown use attribute.
10 * Revision 1.92 1999/05/26 07:49:13 adam
13 * Revision 1.91 1999/02/02 14:51:13 adam
14 * Updated WIN32 code specific sections. Changed header.
16 * Revision 1.90 1998/11/16 16:03:43 adam
17 * Moved loggin utilities to Yaz. Was implemented in file zlogs.c.
19 * Revision 1.89 1998/11/16 10:11:55 adam
20 * Added addtional info for error 114 - unsupported use attribute.
22 * Revision 1.88 1998/10/18 07:54:52 adam
23 * Additional info added for diagnostics 114 (Unsupported use attribute) and
24 * 121 (Unsupported attribute set).
26 * Revision 1.87 1998/09/28 11:19:12 adam
27 * Fix for Compiled ASN.1.
29 * Revision 1.86 1998/09/22 10:48:20 adam
30 * Minor changes in search API.
32 * Revision 1.85 1998/09/22 10:03:43 adam
33 * Changed result sets to be persistent in the sense that they can
34 * be re-searched if needed.
35 * Fixed memory leak in rsm_or.
37 * Revision 1.84 1998/09/18 12:41:00 adam
38 * Fixed bug with numerical relations.
40 * Revision 1.83 1998/09/02 13:53:19 adam
41 * Extra parameter decode added to search routines to implement
44 * Revision 1.82 1998/06/26 11:16:40 quinn
45 * Added support (un-optimised) for left and left/right truncation
47 * Revision 1.81 1998/06/24 12:16:14 adam
48 * Support for relations on text operands. Open range support in
49 * DFA module (i.e. [-j], [g-]).
51 * Revision 1.80 1998/06/23 15:33:34 adam
52 * Added feature to specify sort criteria in query (type 7 specifies
55 * Revision 1.79 1998/06/22 11:35:09 adam
58 * Revision 1.78 1998/06/08 14:43:17 adam
59 * Added suport for EXPLAIN Proxy servers - added settings databasePath
60 * and explainDatabase to facilitate this. Increased maximum number
61 * of databases and attributes in one register.
63 * Revision 1.77 1998/05/20 10:12:22 adam
64 * Implemented automatic EXPLAIN database maintenance.
65 * Modified Zebra to work with ASN.1 compiled version of YAZ.
67 * Revision 1.76 1998/04/02 14:35:29 adam
68 * First version of Zebra that works with compiled ASN.1.
70 * Revision 1.75 1998/03/05 08:45:13 adam
71 * New result set model and modular ranking system. Moved towards
72 * descent server API. System information stored as "SGML" records.
74 * Revision 1.74 1998/02/10 12:03:06 adam
77 * Revision 1.73 1998/01/29 13:40:11 adam
78 * Better logging for scan service.
80 * Revision 1.72 1998/01/07 13:53:41 adam
81 * Queries using simple ranked operands returns right number of hits.
83 * Revision 1.71 1997/12/18 10:54:24 adam
84 * New method result set method rs_hits that returns the number of
85 * hits in result-set (if known). The ranked result set returns real
86 * number of hits but only when not combined with other operands.
88 * Revision 1.70 1997/10/31 12:34:43 adam
89 * Changed a few log statements.
91 * Revision 1.69 1997/10/29 12:05:02 adam
92 * Server produces diagnostic "Unsupported Attribute Set" when appropriate.
94 * Revision 1.68 1997/10/27 14:33:06 adam
95 * Moved towards generic character mapping depending on "structure"
96 * field in abstract syntax file. Fixed a few memory leaks. Fixed
97 * bug with negative integers when doing searches with relational
100 * Revision 1.67 1997/09/29 09:06:10 adam
101 * Removed one static var in order to make this module thread safe.
103 * Revision 1.66 1997/09/25 14:58:03 adam
106 * Revision 1.65 1997/09/22 12:39:06 adam
107 * Added get_pos method for the ranked result sets.
109 * Revision 1.64 1997/09/18 08:59:20 adam
110 * Extra generic handle for the character mapping routines.
112 * Revision 1.63 1997/09/17 12:19:18 adam
113 * Zebra version corresponds to YAZ version 1.4.
114 * Changed Zebra server so that it doesn't depend on global common_resource.
116 * Revision 1.62 1997/09/05 15:30:09 adam
117 * Changed prototype for chr_map_input - added const.
118 * Added support for C++, headers uses extern "C" for public definitions.
120 * Revision 1.61 1997/02/10 10:21:14 adam
121 * Bug fix: in search terms character (^) wasn't observed.
123 * Revision 1.60 1997/01/31 11:10:34 adam
124 * Bug fix: Leading and trailing white space weren't removed in scan tokens.
126 * Revision 1.59 1997/01/17 11:31:46 adam
127 * Bug fix: complete phrase search didn't work.
129 * Revision 1.58 1996/12/23 15:30:45 adam
130 * Work on truncation.
131 * Bug fix: result sets weren't deleted after server shut down.
133 * Revision 1.57 1996/11/11 13:38:02 adam
134 * Added proximity support in search.
136 * Revision 1.56 1996/11/08 11:10:32 adam
137 * Buffers used during file match got bigger.
138 * Compressed ISAM support everywhere.
139 * Bug fixes regarding masking characters in queries.
140 * Redesigned Regexp-2 queries.
142 * Revision 1.55 1996/11/04 14:07:44 adam
143 * Moved truncation code to trunc.c.
145 * Revision 1.54 1996/10/29 14:09:52 adam
146 * Use of cisam system - enabled if setting isamc is 1.
148 * Revision 1.53 1996/06/26 09:21:43 adam
149 * Bug fix: local attribute set wasn't obeyed in scan.
151 * Revision 1.52 1996/06/17 14:26:20 adam
152 * Function gen_regular_rel changed to handle negative numbers.
154 * Revision 1.51 1996/06/11 10:54:15 quinn
157 * Revision 1.50 1996/06/07 08:51:53 adam
158 * Bug fix: Character mapping was broken (introducued by last revision).
160 * Revision 1.49 1996/06/04 10:18:11 adam
161 * Search/scan uses character mapping module.
163 * Revision 1.48 1996/05/28 15:15:01 adam
164 * Bug fix: Didn't handle unknown database correctly.
166 * Revision 1.47 1996/05/15 18:36:28 adam
167 * Function trans_term transforms unsearchable characters to blanks.
169 * Revision 1.46 1996/05/15 11:57:56 adam
170 * Fixed bug introduced by set/field mapping in search operations.
172 * Revision 1.45 1996/05/14 11:34:00 adam
173 * Scan support in multiple registers/databases.
175 * Revision 1.44 1996/05/14 06:16:44 adam
176 * Compact use/set bytes used in search service.
178 * Revision 1.43 1996/05/09 09:54:43 adam
179 * Server supports maps from one logical attributes to a list of physical
181 * The extraction process doesn't make space consuming 'any' keys.
183 * Revision 1.42 1996/05/09 07:28:56 quinn
184 * Work towards phrases and multiple registers
186 * Revision 1.41 1996/03/20 09:36:43 adam
187 * Function dict_lookup_grep got extra parameter, init_pos, which marks
188 * from which position in pattern approximate pattern matching should occur.
189 * Approximate pattern matching is used in relevance=re-2.
191 * Revision 1.40 1996/02/02 13:44:44 adam
192 * The public dictionary functions simply use char instead of Dict_char
193 * to represent search strings. Dict_char is used internally only.
195 * Revision 1.39 1996/01/03 16:22:13 quinn
196 * operator->roperator
198 * Revision 1.38 1995/12/11 09:12:55 adam
199 * The rec_get function returns NULL if record doesn't exist - will
200 * happen in the server if the result set records have been deleted since
201 * the creation of the set (i.e. the search).
202 * The server saves a result temporarily if it is 'volatile', i.e. the
203 * set is register dependent.
205 * Revision 1.37 1995/12/06 15:05:28 adam
206 * More verbose in count_set.
208 * Revision 1.36 1995/12/06 12:41:27 adam
209 * New command 'stat' for the index program.
210 * Filenames can be read from stdin by specifying '-'.
211 * Bug fix/enhancement of the transformation from terms to regular
212 * expressons in the search engine.
214 * Revision 1.35 1995/11/27 09:29:00 adam
215 * Bug fixes regarding conversion to regular expressions.
217 * Revision 1.34 1995/11/16 17:00:56 adam
218 * Better logging of rpn query.
220 * Revision 1.33 1995/11/01 13:58:28 quinn
221 * Moving data1 to yaz/retrieval
223 * Revision 1.32 1995/10/27 14:00:11 adam
224 * Implemented detection of database availability.
226 * Revision 1.31 1995/10/17 18:02:10 adam
227 * New feature: databases. Implemented as prefix to words in dictionary.
229 * Revision 1.30 1995/10/16 09:32:38 adam
230 * More work on relational op.
232 * Revision 1.29 1995/10/13 16:01:49 adam
235 * Revision 1.28 1995/10/13 12:26:43 adam
236 * Optimization of truncation.
238 * Revision 1.27 1995/10/12 17:07:22 adam
241 * Revision 1.26 1995/10/12 12:40:54 adam
242 * Bug fixes in rpn_prox.
244 * Revision 1.25 1995/10/10 13:59:24 adam
245 * Function rset_open changed its wflag parameter to general flags.
247 * Revision 1.24 1995/10/09 16:18:37 adam
248 * Function dict_lookup_grep got extra client data parameter.
250 * Revision 1.23 1995/10/06 16:33:37 adam
251 * Use attribute mappings.
253 * Revision 1.22 1995/10/06 15:07:39 adam
254 * Structure 'local-number' handled.
256 * Revision 1.21 1995/10/06 13:52:06 adam
257 * Bug fixes. Handler may abort further scanning.
259 * Revision 1.20 1995/10/06 11:06:33 adam
260 * Scan entries include 'occurrences' now.
262 * Revision 1.19 1995/10/06 10:43:56 adam
263 * Scan added. 'occurrences' in scan entries not set yet.
265 * Revision 1.18 1995/10/04 16:57:20 adam
266 * Key input and merge sort in one pass.
268 * Revision 1.17 1995/10/04 12:55:17 adam
269 * Bug fix in ranked search. Use=Any keys inserted.
271 * Revision 1.16 1995/10/02 16:24:40 adam
272 * Use attribute actually used in search requests.
274 * Revision 1.15 1995/10/02 15:18:52 adam
275 * New member in recRetrieveCtrl: diagnostic.
277 * Revision 1.14 1995/09/28 12:10:32 adam
278 * Bug fixes. Field prefix used in queries.
280 * Revision 1.13 1995/09/18 14:17:50 adam
283 * Revision 1.12 1995/09/15 14:45:21 adam
285 * Work on truncation.
287 * Revision 1.11 1995/09/14 11:53:27 adam
288 * First work on regular expressions/truncations.
290 * Revision 1.10 1995/09/11 15:23:26 adam
291 * More work on relevance search.
293 * Revision 1.9 1995/09/11 13:09:35 adam
294 * More work on relevance feedback.
296 * Revision 1.8 1995/09/08 14:52:27 adam
297 * Minor changes. Dictionary is lower case now.
299 * Revision 1.7 1995/09/07 13:58:36 adam
300 * New parameter: result-set file descriptor (RSFD) to support multiple
301 * positions within the same result-set.
302 * Boolean operators: and, or, not implemented.
303 * Result-set references.
305 * Revision 1.6 1995/09/06 16:11:18 adam
306 * Option: only one word key per file.
308 * Revision 1.5 1995/09/06 10:33:04 adam
309 * More work on present. Some log messages removed.
311 * Revision 1.4 1995/09/05 15:28:40 adam
312 * More work on search engine.
314 * Revision 1.3 1995/09/04 15:20:22 adam
317 * Revision 1.2 1995/09/04 12:33:43 adam
318 * Various cleanup. YAZ util used instead.
320 * Revision 1.1 1995/09/04 09:10:40 adam
321 * More work on index add/del/update.
322 * Merge sort implemented.
323 * Initial work on z39 server.
342 struct rpn_char_map_info {
347 static const char **rpn_char_map_handler (void *vp, const char **from, int len)
349 struct rpn_char_map_info *p = (struct rpn_char_map_info *) vp;
350 return zebra_maps_input (p->zm, p->reg_type, from, len);
353 static void rpn_char_map_prepare (ZebraHandle zh, int reg_type,
354 struct rpn_char_map_info *map_info)
356 map_info->zm = zh->zebra_maps;
357 map_info->reg_type = reg_type;
358 dict_grep_cmap (zh->dict, map_info, rpn_char_map_handler);
365 Z_AttributesPlusTerm *zapt;
368 static int attr_find (AttrType *src, oid_value *attributeSetP)
373 num_attributes = src->zapt->attributes->num_attributes;
375 num_attributes = src->zapt->num_attributes;
377 while (src->major < num_attributes)
379 Z_AttributeElement *element;
382 element = src->zapt->attributes->attributes[src->major];
384 element = src->zapt->attributeList[src->major];
386 if (src->type == *element->attributeType)
388 switch (element->which)
390 case Z_AttributeValue_numeric:
392 if (element->attributeSet && attributeSetP)
396 attrset = oid_getentbyoid (element->attributeSet);
397 *attributeSetP = attrset->value;
399 return *element->value.numeric;
401 case Z_AttributeValue_complex:
402 if (src->minor >= element->value.complex->num_list ||
403 element->value.complex->list[src->minor]->which !=
404 Z_StringOrNumeric_numeric)
407 if (element->attributeSet && attributeSetP)
411 attrset = oid_getentbyoid (element->attributeSet);
412 *attributeSetP = attrset->value;
414 return *element->value.complex->list[src->minor-1]->u.numeric;
424 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
446 static void term_untrans (ZebraHandle zh, int reg_type,
447 char *dst, const char *src)
451 const char *cp = zebra_maps_output (zh->zebra_maps, reg_type, &src);
458 static void add_isam_p (const char *name, const char *info,
461 if (p->isam_p_indx == p->isam_p_size)
463 ISAM_P *new_isam_p_buf;
467 p->isam_p_size = 2*p->isam_p_size + 100;
468 new_isam_p_buf = (ISAM_P *) xmalloc (sizeof(*new_isam_p_buf) *
472 memcpy (new_isam_p_buf, p->isam_p_buf,
473 p->isam_p_indx * sizeof(*p->isam_p_buf));
474 xfree (p->isam_p_buf);
476 p->isam_p_buf = new_isam_p_buf;
479 new_term_no = (int *) xmalloc (sizeof(*new_term_no) *
483 memcpy (new_term_no, p->isam_p_buf,
484 p->isam_p_indx * sizeof(*p->term_no));
487 p->term_no = new_term_no;
490 assert (*info == sizeof(*p->isam_p_buf));
491 memcpy (p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
494 term_untrans (p->zh, p->reg_type, term_tmp, name+2);
495 logf (LOG_DEBUG, "grep: %s", term_tmp);
500 static int grep_handle (char *name, const char *info, void *p)
502 add_isam_p (name, info, (struct grep_info *) p);
506 static int term_pre (ZebraMaps zebra_maps, int reg_type, const char **src,
507 const char *ct1, const char *ct2)
509 const char *s1, *s0 = *src;
512 /* skip white space */
515 if (ct1 && strchr (ct1, *s0))
517 if (ct2 && strchr (ct2, *s0))
520 map = zebra_maps_input (zebra_maps, reg_type, &s1, strlen(s1));
521 if (**map != *CHR_SPACE)
529 static int term_100 (ZebraMaps zebra_maps, int reg_type,
530 const char **src, char *dst, int space_split,
538 if (!term_pre (zebra_maps, reg_type, src, NULL, NULL))
544 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
545 if (space_split && **map == *CHR_SPACE)
549 if (!isalnum (*s1) && *s1 != '-')
561 static int term_101 (ZebraMaps zebra_maps, int reg_type,
562 const char **src, char *dst, int space_split,
570 if (!term_pre (zebra_maps, reg_type, src, "#", "#"))
579 dst_term[j++] = *s0++;
584 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
585 if (space_split && **map == *CHR_SPACE)
597 dst_term[j++] = '\0';
603 static int term_103 (ZebraMaps zebra_maps, int reg_type, const char **src,
604 char *dst, int *errors, int space_split,
612 if (!term_pre (zebra_maps, reg_type, src, "^\\()[].*+?|", "("))
615 if (errors && *s0 == '+' && s0[1] && s0[2] == '+' && s0[3] &&
618 *errors = s0[1] - '0';
625 if (strchr ("^\\()[].*+?|-", *s0))
633 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
634 if (**map == *CHR_SPACE)
651 static int term_102 (ZebraMaps zebra_maps, int reg_type, const char **src,
652 char *dst, int space_split, char *dst_term)
654 return term_103 (zebra_maps, reg_type, src, dst, NULL, space_split,
658 /* gen_regular_rel - generate regular expression from relation
659 * val: border value (inclusive)
660 * islt: 1 if <=; 0 if >=.
662 static void gen_regular_rel (char *dst, int val, int islt)
669 logf (LOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
673 strcpy (dst, "(-[0-9]+|(");
681 strcpy (dst, "([0-9]+|-(");
682 dst_p = strlen (dst);
692 dst_p = strlen (dst);
693 sprintf (numstr, "%d", val);
694 for (w = strlen(numstr); --w >= 0; pos++)
713 strcpy (dst + dst_p, numstr);
714 dst_p = strlen(dst) - pos - 1;
742 for (i = 0; i<pos; i++)
755 for (i=1; i<pos; i++)
756 strcat (dst, "[0-9]?");
760 for (i = 0; i <= pos; i++)
761 strcat (dst, "[0-9]");
762 strcat (dst, "[0-9]*");
767 void string_rel_add_char (char **term_p, const char *src, int *indx)
769 if (src[*indx] == '\\')
770 *(*term_p)++ = src[(*indx)++];
771 *(*term_p)++ = src[(*indx)++];
775 * > abc ([b-].*|a[c-].*|ab[d-].*|abc.+)
776 * ([^-a].*|a[^-b].*ab[^-c].*|abc.+)
777 * >= abc ([b-].*|a[c-].*|ab[c-].*)
778 * ([^-a].*|a[^-b].*|ab[c-].*)
779 * < abc ([-0].*|a[-a].*|ab[-b].*)
780 * ([^a-].*|a[^b-].*|ab[^c-].*)
781 * <= abc ([-0].*|a[-a].*|ab[-b].*|abc)
782 * ([^a-].*|a[^b-].*|ab[^c-].*|abc)
784 static int string_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
785 const char **term_sub, char *term_dict,
786 oid_value attributeSet,
787 int reg_type, int space_split, char *term_dst)
792 char *term_tmp = term_dict + strlen(term_dict);
793 char term_component[256];
795 attr_init (&relation, zapt, 2);
796 relation_value = attr_find (&relation, NULL);
798 logf (LOG_DEBUG, "string relation value=%d", relation_value);
799 switch (relation_value)
802 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
803 space_split, term_dst))
805 logf (LOG_DEBUG, "Relation <");
808 for (i = 0; term_component[i]; )
815 string_rel_add_char (&term_tmp, term_component, &j);
820 string_rel_add_char (&term_tmp, term_component, &i);
831 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
832 space_split, term_dst))
834 logf (LOG_DEBUG, "Relation <=");
837 for (i = 0; term_component[i]; )
842 string_rel_add_char (&term_tmp, term_component, &j);
846 string_rel_add_char (&term_tmp, term_component, &i);
855 for (i = 0; term_component[i]; )
856 string_rel_add_char (&term_tmp, term_component, &i);
861 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
862 space_split, term_dst))
864 logf (LOG_DEBUG, "Relation >");
867 for (i = 0; term_component[i];)
872 string_rel_add_char (&term_tmp, term_component, &j);
877 string_rel_add_char (&term_tmp, term_component, &i);
885 for (i = 0; term_component[i];)
886 string_rel_add_char (&term_tmp, term_component, &i);
893 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
894 space_split, term_dst))
896 logf (LOG_DEBUG, "Relation >=");
899 for (i = 0; term_component[i];)
906 string_rel_add_char (&term_tmp, term_component, &j);
909 if (term_component[i+1])
913 string_rel_add_char (&term_tmp, term_component, &i);
917 string_rel_add_char (&term_tmp, term_component, &i);
929 logf (LOG_DEBUG, "Relation =");
930 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
931 space_split, term_dst))
933 strcat (term_tmp, "(");
934 strcat (term_tmp, term_component);
935 strcat (term_tmp, ")");
940 static int string_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
941 const char **term_sub,
942 oid_value attributeSet, NMEM stream,
943 struct grep_info *grep_info,
944 int reg_type, int complete_flag,
945 int num_bases, char **basenames,
948 char term_dict[2*IT_MAX_WORD+4000];
951 int truncation_value;
954 oid_value curAttributeSet = attributeSet;
956 struct rpn_char_map_info rcmi;
957 int space_split = complete_flag ? 0 : 1;
959 rpn_char_map_prepare (zh, reg_type, &rcmi);
960 attr_init (&use, zapt, 1);
961 use_value = attr_find (&use, &curAttributeSet);
962 logf (LOG_DEBUG, "string_term, use value %d", use_value);
963 attr_init (&truncation, zapt, 5);
964 truncation_value = attr_find (&truncation, NULL);
965 logf (LOG_DEBUG, "truncation value %d", truncation_value);
970 for (base_no = 0; base_no < num_bases; base_no++)
973 data1_local_attribute *local_attr;
974 int max_pos, prefix_len = 0;
977 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
979 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
980 curAttributeSet, use_value, r);
984 sprintf (val_str, "%d", use_value);
986 zh->errString = nmem_strdup (stream, val_str);
991 struct oident oident;
993 oident.proto = PROTO_Z3950;
994 oident.oclass = CLASS_ATTSET;
995 oident.value = curAttributeSet;
996 oid_ent_to_oid (&oident, oid);
999 zh->errString = nmem_strdup (stream, oident.desc);
1003 if (zebraExplain_curDatabase (zh->zei, basenames[base_no]))
1005 zh->errCode = 109; /* Database unavailable */
1006 zh->errString = basenames[base_no];
1009 for (local_attr = attp.local_attributes; local_attr;
1010 local_attr = local_attr->next)
1016 ord = zebraExplain_lookupSU (zh->zei, attp.attset_ordinal,
1021 term_dict[prefix_len++] = '|';
1023 term_dict[prefix_len++] = '(';
1025 ord_len = key_SU_code (ord, ord_buf);
1026 for (i = 0; i<ord_len; i++)
1028 term_dict[prefix_len++] = 1;
1029 term_dict[prefix_len++] = ord_buf[i];
1035 sprintf (val_str, "%d", use_value);
1037 zh->errString = nmem_strdup (stream, val_str);
1040 term_dict[prefix_len++] = ')';
1041 term_dict[prefix_len++] = 1;
1042 term_dict[prefix_len++] = reg_type;
1043 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1044 term_dict[prefix_len] = '\0';
1046 switch (truncation_value)
1048 case -1: /* not specified */
1049 case 100: /* do not truncate */
1050 if (!string_relation (zh, zapt, &termp, term_dict,
1052 reg_type, space_split, term_dst))
1054 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_dict+prefix_len);
1055 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info, &max_pos,
1058 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1060 case 1: /* right truncation */
1061 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);
1069 case 2: /* keft truncation */
1070 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1071 if (!term_100 (zh->zebra_maps, reg_type,
1072 &termp, term_dict + j, space_split, term_dst))
1074 strcat (term_dict, ")");
1075 dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1076 &max_pos, 0, grep_handle);
1078 case 3: /* left&right truncation */
1079 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1080 if (!term_100 (zh->zebra_maps, reg_type,
1081 &termp, term_dict + j, space_split, term_dst))
1083 strcat (term_dict, ".*)");
1084 dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1085 &max_pos, 0, grep_handle);
1089 case 101: /* process # in term */
1090 term_dict[j++] = '(';
1091 if (!term_101 (zh->zebra_maps, reg_type,
1092 &termp, term_dict + j, space_split, term_dst))
1094 strcat (term_dict, ")");
1095 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1096 &max_pos, 0, grep_handle);
1098 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d", r);
1100 case 102: /* Regexp-1 */
1101 term_dict[j++] = '(';
1102 if (!term_102 (zh->zebra_maps, reg_type,
1103 &termp, term_dict + j, space_split, term_dst))
1105 strcat (term_dict, ")");
1106 logf (LOG_DEBUG, "Regexp-1 tolerance=%d", r);
1107 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1108 &max_pos, 0, grep_handle);
1110 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
1113 case 103: /* Regexp-2 */
1115 term_dict[j++] = '(';
1116 if (!term_103 (zh->zebra_maps, reg_type,
1117 &termp, term_dict + j, &r, space_split, term_dst))
1119 strcat (term_dict, ")");
1120 logf (LOG_DEBUG, "Regexp-2 tolerance=%d", r);
1121 r = dict_lookup_grep (zh->dict, term_dict, r, grep_info,
1122 &max_pos, 2, grep_handle);
1124 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
1130 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1134 static void trans_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1138 Z_Term *term = zapt->term;
1140 sizez = term->u.general->len;
1141 if (sizez > IT_MAX_WORD-1)
1142 sizez = IT_MAX_WORD-1;
1143 memcpy (termz, term->u.general->buf, sizez);
1144 termz[sizez] = '\0';
1147 static void trans_scan_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1148 char *termz, int reg_type)
1150 Z_Term *term = zapt->term;
1152 const char *cp = (const char *) term->u.general->buf;
1153 const char *cp_end = cp + term->u.general->len;
1156 const char *space_map = NULL;
1159 while ((len = (cp_end - cp)) > 0)
1161 map = zebra_maps_input (zh->zebra_maps, reg_type, &cp, len);
1162 if (**map == *CHR_SPACE)
1167 for (src = space_map; *src; src++)
1170 for (src = *map; *src; src++)
1177 static RSET rpn_proximity (ZebraHandle zh, RSET rset1, RSET rset2,
1179 int exclusion, int relation, int distance)
1184 struct it_key buf1, buf2;
1187 rset_temp_parms parms;
1190 rsfd1 = rset_open (rset1, RSETF_READ);
1191 more1 = rset_read (rset1, rsfd1, &buf1, &term_index);
1193 rsfd2 = rset_open (rset2, RSETF_READ);
1194 more2 = rset_read (rset2, rsfd2, &buf2, &term_index);
1196 parms.key_size = sizeof (struct it_key);
1197 parms.temp_path = res_get (zh->res, "setTmpDir");
1198 result = rset_create (rset_kind_temp, &parms);
1199 rsfd_result = rset_open (result, RSETF_WRITE);
1201 logf (LOG_DEBUG, "rpn_proximity excl=%d ord=%d rel=%d dis=%d",
1202 exclusion, ordered, relation, distance);
1203 while (more1 && more2)
1205 int cmp = key_compare_it (&buf1, &buf2);
1207 more1 = rset_read (rset1, rsfd1, &buf1, &term_index);
1209 more2 = rset_read (rset2, rsfd2, &buf2, &term_index);
1212 int sysno = buf1.sysno;
1216 seqno[n++] = buf1.seqno;
1217 while ((more1 = rset_read (rset1, rsfd1, &buf1, &term_index)) &&
1218 sysno == buf1.sysno)
1220 seqno[n++] = buf1.seqno;
1223 for (i = 0; i<n; i++)
1225 int diff = buf2.seqno - seqno[i];
1226 int excl = exclusion;
1227 if (!ordered && diff < 0)
1232 if (diff < distance)
1236 if (diff <= distance)
1240 if (diff == distance)
1244 if (diff >= distance)
1248 if (diff > distance)
1252 if (diff != distance)
1257 rset_write (result, rsfd_result, &buf2);
1259 } while ((more2 = rset_read (rset2, rsfd2, &buf2, &term_index)) &&
1260 sysno == buf2.sysno);
1263 rset_close (result, rsfd_result);
1264 rset_close (rset1, rsfd1);
1265 rset_close (rset2, rsfd2);
1269 static RSET rpn_prox (ZebraHandle zh, RSET *rset, int rset_no)
1274 struct it_key **buf;
1276 char prox_term[1024];
1277 int length_prox_term = 0;
1278 int min_nn = 10000000;
1280 const char *flags = NULL;
1282 rsfd = (RSFD *) xmalloc (sizeof(*rsfd)*rset_no);
1283 more = (int *) xmalloc (sizeof(*more)*rset_no);
1284 buf = (struct it_key **) xmalloc (sizeof(*buf)*rset_no);
1286 for (i = 0; i<rset_no; i++)
1289 buf[i] = (struct it_key *) xmalloc (sizeof(**buf));
1290 rsfd[i] = rset_open (rset[i], RSETF_READ);
1291 if (!(more[i] = rset_read (rset[i], rsfd[i], buf[i], &term_index)))
1293 for (j = 0; j<rset[i]->no_rset_terms; j++)
1295 const char *nflags = rset[i]->rset_terms[j]->flags;
1296 char *term = rset[i]->rset_terms[j]->name;
1297 int lterm = strlen(term);
1298 if (length_prox_term)
1299 prox_term[length_prox_term++] = ' ';
1300 strcpy (prox_term + length_prox_term, term);
1301 length_prox_term += lterm;
1302 if (min_nn > rset[i]->rset_terms[j]->nn)
1303 min_nn = rset[i]->rset_terms[j]->nn;
1309 rset_null_parms parms;
1313 rset_close (rset[i], rsfd[i]);
1317 parms.rset_term = rset_term_create (prox_term, -1, flags);
1318 parms.rset_term->nn = 0;
1319 result = rset_create (rset_kind_null, &parms);
1323 rset_temp_parms parms;
1326 parms.rset_term = rset_term_create (prox_term, -1, flags);
1327 parms.rset_term->nn = min_nn;
1328 parms.key_size = sizeof (struct it_key);
1329 parms.temp_path = res_get (zh->res, "setTmpDir");
1330 result = rset_create (rset_kind_temp, &parms);
1331 rsfd_result = rset_open (result, RSETF_WRITE);
1335 for (i = 1; i<rset_no; i++)
1344 cmp = key_compare_it (buf[i], buf[i-1]);
1347 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1348 buf[i-1], &term_index);
1353 if (buf[i-1]->seqno+1 != buf[i]->seqno)
1355 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1356 buf[i-1], &term_index);
1362 more[i] = rset_read (rset[i], rsfd[i], buf[i],
1369 rset_write (result, rsfd_result, buf[0]);
1370 more[0] = rset_read (*rset, *rsfd, *buf, &term_index);
1374 for (i = 0; i<rset_no; i++)
1376 rset_close (rset[i], rsfd[i]);
1379 rset_close (result, rsfd_result);
1387 static RSET rpn_search_APT_phrase (ZebraHandle zh,
1388 Z_AttributesPlusTerm *zapt,
1390 oid_value attributeSet,
1392 int reg_type, int complete_flag,
1393 const char *rank_type,
1394 int num_bases, char **basenames)
1396 char term_dst[IT_MAX_WORD+1];
1397 const char *termp = termz;
1398 RSET rset[60], result;
1399 int i, r, rset_no = 0;
1400 struct grep_info grep_info;
1403 grep_info.term_no = 0;
1405 grep_info.isam_p_size = 0;
1406 grep_info.isam_p_buf = NULL;
1408 grep_info.reg_type = reg_type;
1412 logf (LOG_DEBUG, "APT_phrase termp=%s", termp);
1413 grep_info.isam_p_indx = 0;
1414 r = string_term (zh, zapt, &termp, attributeSet, stream, &grep_info,
1415 reg_type, complete_flag, num_bases, basenames,
1419 logf (LOG_DEBUG, "term: %s", term_dst);
1420 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1421 grep_info.isam_p_indx, term_dst,
1422 strlen(term_dst), rank_type);
1423 assert (rset[rset_no]);
1424 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1428 xfree(grep_info.term_no);
1430 xfree (grep_info.isam_p_buf);
1433 rset_null_parms parms;
1435 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1436 return rset_create (rset_kind_null, &parms);
1438 else if (rset_no == 1)
1440 result = rpn_prox (zh, rset, rset_no);
1441 for (i = 0; i<rset_no; i++)
1442 rset_delete (rset[i]);
1446 static RSET rpn_search_APT_or_list (ZebraHandle zh,
1447 Z_AttributesPlusTerm *zapt,
1449 oid_value attributeSet,
1451 int reg_type, int complete_flag,
1452 const char *rank_type,
1453 int num_bases, char **basenames)
1455 char term_dst[IT_MAX_WORD+1];
1456 const char *termp = termz;
1457 RSET rset[60], result;
1458 int i, r, rset_no = 0;
1459 struct grep_info grep_info;
1462 grep_info.term_no = 0;
1464 grep_info.isam_p_size = 0;
1465 grep_info.isam_p_buf = NULL;
1467 grep_info.reg_type = reg_type;
1471 logf (LOG_DEBUG, "APT_or_list termp=%s", termp);
1472 grep_info.isam_p_indx = 0;
1473 r = string_term (zh, zapt, &termp, attributeSet, stream, &grep_info,
1474 reg_type, complete_flag, num_bases, basenames,
1478 logf (LOG_DEBUG, "term: %s", term_dst);
1479 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1480 grep_info.isam_p_indx, term_dst,
1481 strlen(term_dst), rank_type);
1482 assert (rset[rset_no]);
1483 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1487 xfree(grep_info.term_no);
1489 xfree (grep_info.isam_p_buf);
1492 rset_null_parms parms;
1494 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1495 return rset_create (rset_kind_null, &parms);
1498 for (i = 1; i<rset_no; i++)
1500 rset_bool_parms bool_parms;
1502 bool_parms.rset_l = result;
1503 bool_parms.rset_r = rset[i];
1504 bool_parms.key_size = sizeof(struct it_key);
1505 bool_parms.cmp = key_compare_it;
1506 result = rset_create (rset_kind_or, &bool_parms);
1511 static RSET rpn_search_APT_and_list (ZebraHandle zh,
1512 Z_AttributesPlusTerm *zapt,
1514 oid_value attributeSet,
1516 int reg_type, int complete_flag,
1517 const char *rank_type,
1518 int num_bases, char **basenames)
1520 char term_dst[IT_MAX_WORD+1];
1521 const char *termp = termz;
1522 RSET rset[60], result;
1523 int i, r, rset_no = 0;
1524 struct grep_info grep_info;
1527 grep_info.term_no = 0;
1529 grep_info.isam_p_size = 0;
1530 grep_info.isam_p_buf = NULL;
1532 grep_info.reg_type = reg_type;
1536 logf (LOG_DEBUG, "APT_and_list termp=%s", termp);
1537 grep_info.isam_p_indx = 0;
1538 r = string_term (zh, zapt, &termp, attributeSet, stream, &grep_info,
1539 reg_type, complete_flag, num_bases, basenames,
1543 logf (LOG_DEBUG, "term: %s", term_dst);
1544 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1545 grep_info.isam_p_indx, term_dst,
1546 strlen(term_dst), rank_type);
1547 assert (rset[rset_no]);
1548 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1552 xfree(grep_info.term_no);
1554 xfree (grep_info.isam_p_buf);
1557 rset_null_parms parms;
1559 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1560 return rset_create (rset_kind_null, &parms);
1563 for (i = 1; i<rset_no; i++)
1565 rset_bool_parms bool_parms;
1567 bool_parms.rset_l = result;
1568 bool_parms.rset_r = rset[i];
1569 bool_parms.key_size = sizeof(struct it_key);
1570 bool_parms.cmp = key_compare_it;
1571 result = rset_create (rset_kind_and, &bool_parms);
1576 static int numeric_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1577 const char **term_sub,
1579 oid_value attributeSet,
1580 struct grep_info *grep_info,
1589 char *term_tmp = term_dict + strlen(term_dict);
1591 attr_init (&relation, zapt, 2);
1592 relation_value = attr_find (&relation, NULL);
1594 logf (LOG_DEBUG, "numeric relation value=%d", relation_value);
1596 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_tmp, 1,
1599 term_value = atoi (term_tmp);
1600 switch (relation_value)
1603 logf (LOG_DEBUG, "Relation <");
1604 gen_regular_rel (term_tmp, term_value-1, 1);
1607 logf (LOG_DEBUG, "Relation <=");
1608 gen_regular_rel (term_tmp, term_value, 1);
1611 logf (LOG_DEBUG, "Relation >=");
1612 gen_regular_rel (term_tmp, term_value, 0);
1615 logf (LOG_DEBUG, "Relation >");
1616 gen_regular_rel (term_tmp, term_value+1, 0);
1620 logf (LOG_DEBUG, "Relation =");
1621 sprintf (term_tmp, "(0*%d)", term_value);
1623 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_tmp);
1624 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info, max_pos,
1627 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1628 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1632 static int numeric_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1633 const char **term_sub,
1634 oid_value attributeSet, struct grep_info *grep_info,
1635 int reg_type, int complete_flag,
1636 int num_bases, char **basenames,
1639 char term_dict[2*IT_MAX_WORD+2];
1643 oid_value curAttributeSet = attributeSet;
1645 struct rpn_char_map_info rcmi;
1647 rpn_char_map_prepare (zh, reg_type, &rcmi);
1648 attr_init (&use, zapt, 1);
1649 use_value = attr_find (&use, &curAttributeSet);
1650 logf (LOG_DEBUG, "numeric_term, use value %d", use_value);
1652 if (use_value == -1)
1655 for (base_no = 0; base_no < num_bases; base_no++)
1658 data1_local_attribute *local_attr;
1659 int max_pos, prefix_len = 0;
1662 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
1664 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1665 curAttributeSet, use_value, r);
1672 if (zebraExplain_curDatabase (zh->zei, basenames[base_no]))
1674 zh->errCode = 109; /* Database unavailable */
1675 zh->errString = basenames[base_no];
1678 for (local_attr = attp.local_attributes; local_attr;
1679 local_attr = local_attr->next)
1685 ord = zebraExplain_lookupSU (zh->zei, attp.attset_ordinal,
1690 term_dict[prefix_len++] = '|';
1692 term_dict[prefix_len++] = '(';
1694 ord_len = key_SU_code (ord, ord_buf);
1695 for (i = 0; i<ord_len; i++)
1697 term_dict[prefix_len++] = 1;
1698 term_dict[prefix_len++] = ord_buf[i];
1706 term_dict[prefix_len++] = ')';
1707 term_dict[prefix_len++] = 1;
1708 term_dict[prefix_len++] = reg_type;
1709 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1710 term_dict[prefix_len] = '\0';
1711 if (!numeric_relation (zh, zapt, &termp, term_dict,
1712 attributeSet, grep_info, &max_pos, reg_type,
1717 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1721 static RSET rpn_search_APT_numeric (ZebraHandle zh,
1722 Z_AttributesPlusTerm *zapt,
1724 oid_value attributeSet,
1726 int reg_type, int complete_flag,
1727 const char *rank_type,
1728 int num_bases, char **basenames)
1730 char term_dst[IT_MAX_WORD+1];
1731 const char *termp = termz;
1732 RSET rset[60], result;
1733 int i, r, rset_no = 0;
1734 struct grep_info grep_info;
1737 grep_info.term_no = 0;
1739 grep_info.isam_p_size = 0;
1740 grep_info.isam_p_buf = NULL;
1742 grep_info.reg_type = reg_type;
1746 logf (LOG_DEBUG, "APT_numeric termp=%s", termp);
1747 grep_info.isam_p_indx = 0;
1748 r = numeric_term (zh, zapt, &termp, attributeSet, &grep_info,
1749 reg_type, complete_flag, num_bases, basenames,
1753 logf (LOG_DEBUG, "term: %s", term_dst);
1754 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1755 grep_info.isam_p_indx, term_dst,
1756 strlen(term_dst), rank_type);
1757 assert (rset[rset_no]);
1758 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1762 xfree(grep_info.term_no);
1764 xfree (grep_info.isam_p_buf);
1767 rset_null_parms parms;
1769 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1770 return rset_create (rset_kind_null, &parms);
1773 for (i = 1; i<rset_no; i++)
1775 rset_bool_parms bool_parms;
1777 bool_parms.rset_l = result;
1778 bool_parms.rset_r = rset[i];
1779 bool_parms.key_size = sizeof(struct it_key);
1780 bool_parms.cmp = key_compare_it;
1781 result = rset_create (rset_kind_and, &bool_parms);
1786 static RSET rpn_search_APT_local (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1788 oid_value attributeSet,
1790 const char *rank_type)
1795 rset_temp_parms parms;
1797 parms.rset_term = rset_term_create (termz, -1, rank_type);
1798 parms.key_size = sizeof (struct it_key);
1799 parms.temp_path = res_get (zh->res, "setTmpDir");
1800 result = rset_create (rset_kind_temp, &parms);
1801 rsfd = rset_open (result, RSETF_WRITE);
1803 key.sysno = atoi (termz);
1807 rset_write (result, rsfd, &key);
1808 rset_close (result, rsfd);
1812 static RSET rpn_sort_spec (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1813 oid_value attributeSet, NMEM stream,
1814 Z_SortKeySpecList *sort_sequence,
1815 const char *rank_type)
1817 rset_null_parms parms;
1819 int sort_relation_value;
1820 AttrType sort_relation_type;
1825 Z_AttributeElement *ae;
1829 attr_init (&sort_relation_type, zapt, 7);
1830 sort_relation_value = attr_find (&sort_relation_type, &attributeSet);
1832 attr_init (&use_type, zapt, 1);
1833 use_value = attr_find (&use_type, &attributeSet);
1835 if (!sort_sequence->specs)
1837 sort_sequence->num_specs = 10;
1838 sort_sequence->specs = (Z_SortKeySpec **)
1839 nmem_malloc (stream, sort_sequence->num_specs *
1840 sizeof(*sort_sequence->specs));
1841 for (i = 0; i<sort_sequence->num_specs; i++)
1842 sort_sequence->specs[i] = 0;
1844 if (zapt->term->which != Z_Term_general)
1847 i = atoi_n ((char *) zapt->term->u.general->buf,
1848 zapt->term->u.general->len);
1849 if (i >= sort_sequence->num_specs)
1852 oe.proto = PROTO_Z3950;
1853 oe.oclass = CLASS_ATTSET;
1854 oe.value = attributeSet;
1855 if (!oid_ent_to_oid (&oe, oid))
1858 sks = (Z_SortKeySpec *) nmem_malloc (stream, sizeof(*sks));
1859 sks->sortElement = (Z_SortElement *)
1860 nmem_malloc (stream, sizeof(*sks->sortElement));
1861 sks->sortElement->which = Z_SortElement_generic;
1862 sk = sks->sortElement->u.generic = (Z_SortKey *)
1863 nmem_malloc (stream, sizeof(*sk));
1864 sk->which = Z_SortKey_sortAttributes;
1865 sk->u.sortAttributes = (Z_SortAttributes *)
1866 nmem_malloc (stream, sizeof(*sk->u.sortAttributes));
1868 sk->u.sortAttributes->id = oid;
1869 sk->u.sortAttributes->list = (Z_AttributeList *)
1870 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list));
1871 sk->u.sortAttributes->list->num_attributes = 1;
1872 sk->u.sortAttributes->list->attributes = (Z_AttributeElement **)
1873 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list->attributes));
1874 ae = *sk->u.sortAttributes->list->attributes = (Z_AttributeElement *)
1875 nmem_malloc (stream, sizeof(**sk->u.sortAttributes->list->attributes));
1876 ae->attributeSet = 0;
1877 ae->attributeType = (int *)
1878 nmem_malloc (stream, sizeof(*ae->attributeType));
1879 *ae->attributeType = 1;
1880 ae->which = Z_AttributeValue_numeric;
1881 ae->value.numeric = (int *)
1882 nmem_malloc (stream, sizeof(*ae->value.numeric));
1883 *ae->value.numeric = use_value;
1885 sks->sortRelation = (int *)
1886 nmem_malloc (stream, sizeof(*sks->sortRelation));
1887 if (sort_relation_value == 1)
1888 *sks->sortRelation = Z_SortRelation_ascending;
1889 else if (sort_relation_value == 2)
1890 *sks->sortRelation = Z_SortRelation_descending;
1892 *sks->sortRelation = Z_SortRelation_ascending;
1894 sks->caseSensitivity = (int *)
1895 nmem_malloc (stream, sizeof(*sks->caseSensitivity));
1896 *sks->caseSensitivity = 0;
1899 sks->which = Z_SortKeySpec_null;
1900 sks->u.null = odr_nullval ();
1902 sks->missingValueAction = 0;
1905 sort_sequence->specs[i] = sks;
1907 parms.rset_term = rset_term_create ("", -1, rank_type);
1908 return rset_create (rset_kind_null, &parms);
1912 static RSET rpn_search_APT (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1913 oid_value attributeSet, NMEM stream,
1914 Z_SortKeySpecList *sort_sequence,
1915 int num_bases, char **basenames)
1918 char *search_type = NULL;
1919 char *rank_type = NULL;
1922 char termz[IT_MAX_WORD+1];
1924 zebra_maps_attr (zh->zebra_maps, zapt, ®_id, &search_type,
1925 &rank_type, &complete_flag, &sort_flag);
1927 logf (LOG_DEBUG, "reg_id=%c", reg_id);
1928 logf (LOG_DEBUG, "complete_flag=%d", complete_flag);
1929 logf (LOG_DEBUG, "search_type=%s", search_type);
1930 logf (LOG_DEBUG, "rank_type=%s", rank_type);
1932 if (zapt->term->which != Z_Term_general)
1937 trans_term (zh, zapt, termz);
1940 return rpn_sort_spec (zh, zapt, attributeSet, stream, sort_sequence,
1943 if (!strcmp (search_type, "phrase"))
1945 return rpn_search_APT_phrase (zh, zapt, termz, attributeSet, stream,
1946 reg_id, complete_flag, rank_type,
1947 num_bases, basenames);
1949 else if (!strcmp (search_type, "and-list"))
1951 return rpn_search_APT_and_list (zh, zapt, termz, attributeSet, stream,
1952 reg_id, complete_flag, rank_type,
1953 num_bases, basenames);
1955 else if (!strcmp (search_type, "or-list"))
1957 return rpn_search_APT_or_list (zh, zapt, termz, attributeSet, stream,
1958 reg_id, complete_flag, rank_type,
1959 num_bases, basenames);
1961 else if (!strcmp (search_type, "local"))
1963 return rpn_search_APT_local (zh, zapt, termz, attributeSet, stream,
1966 else if (!strcmp (search_type, "numeric"))
1968 return rpn_search_APT_numeric (zh, zapt, termz, attributeSet, stream,
1969 reg_id, complete_flag, rank_type,
1970 num_bases, basenames);
1976 static RSET rpn_search_structure (ZebraHandle zh, Z_RPNStructure *zs,
1977 oid_value attributeSet, NMEM stream,
1978 Z_SortKeySpecList *sort_sequence,
1979 int num_bases, char **basenames)
1982 if (zs->which == Z_RPNStructure_complex)
1984 Z_Operator *zop = zs->u.complex->roperator;
1985 rset_bool_parms bool_parms;
1987 bool_parms.rset_l = rpn_search_structure (zh, zs->u.complex->s1,
1988 attributeSet, stream,
1990 num_bases, basenames);
1991 if (bool_parms.rset_l == NULL)
1993 bool_parms.rset_r = rpn_search_structure (zh, zs->u.complex->s2,
1994 attributeSet, stream,
1996 num_bases, basenames);
1997 if (bool_parms.rset_r == NULL)
1999 rset_delete (bool_parms.rset_l);
2002 bool_parms.key_size = sizeof(struct it_key);
2003 bool_parms.cmp = key_compare_it;
2007 case Z_Operator_and:
2008 r = rset_create (rset_kind_and, &bool_parms);
2011 r = rset_create (rset_kind_or, &bool_parms);
2013 case Z_Operator_and_not:
2014 r = rset_create (rset_kind_not, &bool_parms);
2016 case Z_Operator_prox:
2018 if (zop->u.prox->which != Z_ProximityOperator_known)
2024 if (zop->u.prox->which != Z_ProxCode_known)
2032 if (*zop->u.prox->u.known != Z_ProxUnit_word)
2034 char *val = (char *) nmem_malloc (stream, 16);
2036 zh->errString = val;
2037 sprintf (val, "%d", *zop->u.prox->u.known);
2041 if (*zop->u.prox->proximityUnitCode != Z_ProxUnit_word)
2043 char *val = (char *) nmem_malloc (stream, 16);
2045 zh->errString = val;
2046 sprintf (val, "%d", *zop->u.prox->proximityUnitCode);
2050 r = rpn_proximity (zh, bool_parms.rset_l, bool_parms.rset_r,
2051 *zop->u.prox->ordered,
2052 (!zop->u.prox->exclusion ? 0 :
2053 *zop->u.prox->exclusion),
2054 *zop->u.prox->relationType,
2055 *zop->u.prox->distance);
2062 else if (zs->which == Z_RPNStructure_simple)
2064 if (zs->u.simple->which == Z_Operand_APT)
2066 logf (LOG_DEBUG, "rpn_search_APT");
2067 r = rpn_search_APT (zh, zs->u.simple->u.attributesPlusTerm,
2068 attributeSet, stream, sort_sequence,
2069 num_bases, basenames);
2071 else if (zs->u.simple->which == Z_Operand_resultSetId)
2073 logf (LOG_DEBUG, "rpn_search_ref");
2074 r = resultSetRef (zh, zs->u.simple->u.resultSetId);
2076 r = rset_create (rset_kind_null, NULL);
2093 RSET rpn_search (ZebraHandle zh, NMEM nmem,
2094 Z_RPNQuery *rpn, int num_bases, char **basenames,
2095 const char *setname,
2100 oid_value attributeSet;
2101 Z_SortKeySpecList *sort_sequence;
2105 zh->errString = NULL;
2108 sort_sequence = (Z_SortKeySpecList *)
2109 nmem_malloc (nmem, sizeof(*sort_sequence));
2110 sort_sequence->num_specs = 10;
2111 sort_sequence->specs = (Z_SortKeySpec **)
2112 nmem_malloc (nmem, sort_sequence->num_specs *
2113 sizeof(*sort_sequence->specs));
2114 for (i = 0; i<sort_sequence->num_specs; i++)
2115 sort_sequence->specs[i] = 0;
2117 attrset = oid_getentbyoid (rpn->attributeSetId);
2118 attributeSet = attrset->value;
2119 rset = rpn_search_structure (zh, rpn->RPNStructure, attributeSet,
2120 nmem, sort_sequence, num_bases, basenames);
2125 logf (LOG_DEBUG, "search error: %d", zh->errCode);
2127 for (i = 0; sort_sequence->specs[i]; i++)
2129 sort_sequence->num_specs = i;
2131 resultSetRank (zh, sset, rset);
2134 logf (LOG_DEBUG, "resultSetSortSingle in rpn_search");
2135 resultSetSortSingle (zh, nmem, sset, rset,
2136 sort_sequence, &sort_status);
2139 logf (LOG_DEBUG, "resultSetSortSingle status = %d", zh->errCode);
2145 struct scan_info_entry {
2151 struct scan_info_entry *list;
2157 static int scan_handle (char *name, const char *info, int pos, void *client)
2159 int len_prefix, idx;
2160 struct scan_info *scan_info = (struct scan_info *) client;
2162 len_prefix = strlen(scan_info->prefix);
2163 if (memcmp (name, scan_info->prefix, len_prefix))
2166 idx = scan_info->after - pos + scan_info->before;
2169 scan_info->list[idx].term = (char *)
2170 odr_malloc (scan_info->odr, strlen(name + len_prefix)+1);
2171 strcpy (scan_info->list[idx].term, name + len_prefix);
2172 assert (*info == sizeof(ISAM_P));
2173 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAM_P));
2177 static void scan_term_untrans (ZebraHandle zh, NMEM stream, int reg_type,
2178 char **dst, const char *src)
2180 char term_dst[1024];
2182 term_untrans (zh, reg_type, term_dst, src);
2184 *dst = (char *) nmem_malloc (stream, strlen(term_dst)+1);
2185 strcpy (*dst, term_dst);
2188 static void count_set (RSET r, int *count)
2196 logf (LOG_DEBUG, "count_set");
2199 rfd = rset_open (r, RSETF_READ);
2200 while (rset_read (r, rfd, &key, &term_index))
2202 if (key.sysno != psysno)
2209 rset_close (r, rfd);
2210 logf (LOG_DEBUG, "%d keys, %d records", kno, *count);
2213 void rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
2214 oid_value attributeset,
2215 int num_bases, char **basenames,
2216 int *position, int *num_entries, ZebraScanEntry **list,
2220 int pos = *position;
2221 int num = *num_entries;
2225 char termz[IT_MAX_WORD+20];
2228 struct scan_info *scan_info_array;
2229 ZebraScanEntry *glist;
2230 int ords[32], ord_no = 0;
2234 char *search_type = NULL;
2235 char *rank_type = NULL;
2240 if (attributeset == VAL_NONE)
2241 attributeset = VAL_BIB1;
2243 logf (LOG_DEBUG, "position = %d, num = %d", pos, num);
2245 attr_init (&use, zapt, 1);
2246 use_value = attr_find (&use, &attributeset);
2248 if (zebra_maps_attr (zh->zebra_maps, zapt, ®_id, &search_type,
2249 &rank_type, &complete_flag, &sort_flag))
2255 if (use_value == -1)
2257 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
2261 data1_local_attribute *local_attr;
2263 if ((r=att_getentbyatt (zh, &attp, attributeset, use_value)))
2265 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
2266 attributeset, use_value);
2274 if (zebraExplain_curDatabase (zh->zei, basenames[base_no]))
2276 zh->errString = basenames[base_no];
2277 zh->errCode = 109; /* Database unavailable */
2280 for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
2281 local_attr = local_attr->next)
2285 ord = zebraExplain_lookupSU (zh->zei, attp.attset_ordinal,
2288 ords[ord_no++] = ord;
2296 /* prepare dictionary scanning */
2299 scan_info_array = (struct scan_info *)
2300 odr_malloc (stream, ord_no * sizeof(*scan_info_array));
2301 for (i = 0; i < ord_no; i++)
2303 int j, prefix_len = 0;
2304 int before_tmp = before, after_tmp = after;
2305 struct scan_info *scan_info = scan_info_array + i;
2306 struct rpn_char_map_info rcmi;
2308 rpn_char_map_prepare (zh, reg_id, &rcmi);
2310 scan_info->before = before;
2311 scan_info->after = after;
2312 scan_info->odr = stream;
2314 scan_info->list = (struct scan_info_entry *)
2315 odr_malloc (stream, (before+after) * sizeof(*scan_info->list));
2316 for (j = 0; j<before+after; j++)
2317 scan_info->list[j].term = NULL;
2319 prefix_len += key_SU_code (ords[i], termz + prefix_len);
2320 termz[prefix_len++] = reg_id;
2321 termz[prefix_len] = 0;
2322 strcpy (scan_info->prefix, termz);
2324 trans_scan_term (zh, zapt, termz+prefix_len, reg_id);
2326 dict_scan (zh->dict, termz, &before_tmp, &after_tmp, scan_info,
2329 glist = (ZebraScanEntry *)
2330 odr_malloc (stream, (before+after)*sizeof(*glist));
2332 /* consider terms after main term */
2333 for (i = 0; i < ord_no; i++)
2337 for (i = 0; i<after; i++)
2340 const char *mterm = NULL;
2344 for (j = 0; j < ord_no; j++)
2346 if (ptr[j] < before+after &&
2347 (tst=scan_info_array[j].list[ptr[j]].term) &&
2348 (!mterm || strcmp (tst, mterm) < 0))
2356 scan_term_untrans (zh, stream->mem, reg_id,
2357 &glist[i+before].term, mterm);
2358 rset = rset_trunc (zh, &scan_info_array[j0].list[ptr[j0]].isam_p, 1,
2359 glist[i+before].term, strlen(glist[i+before].term),
2363 for (j = j0+1; j<ord_no; j++)
2365 if (ptr[j] < before+after &&
2366 (tst=scan_info_array[j].list[ptr[j]].term) &&
2367 !strcmp (tst, mterm))
2369 rset_bool_parms bool_parms;
2373 rset_trunc (zh, &scan_info_array[j].list[ptr[j]].isam_p, 1,
2374 glist[i+before].term,
2375 strlen(glist[i+before].term), NULL);
2377 bool_parms.key_size = sizeof(struct it_key);
2378 bool_parms.cmp = key_compare_it;
2379 bool_parms.rset_l = rset;
2380 bool_parms.rset_r = rset2;
2382 rset = rset_create (rset_kind_or, &bool_parms);
2387 count_set (rset, &glist[i+before].occurrences);
2392 *num_entries -= (after-i);
2396 /* consider terms before main term */
2397 for (i = 0; i<ord_no; i++)
2400 for (i = 0; i<before; i++)
2403 const char *mterm = NULL;
2407 for (j = 0; j <ord_no; j++)
2409 if (ptr[j] < before &&
2410 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2411 (!mterm || strcmp (tst, mterm) > 0))
2420 scan_term_untrans (zh, stream->mem, reg_id,
2421 &glist[before-1-i].term, mterm);
2424 (zh, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1,
2425 glist[before-1-i].term, strlen(glist[before-1-i].term),
2430 for (j = j0+1; j<ord_no; j++)
2432 if (ptr[j] < before &&
2433 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2434 !strcmp (tst, mterm))
2436 rset_bool_parms bool_parms;
2439 rset2 = rset_trunc (zh,
2440 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1,
2441 glist[before-1-i].term,
2442 strlen(glist[before-1-i].term), NULL);
2444 bool_parms.key_size = sizeof(struct it_key);
2445 bool_parms.cmp = key_compare_it;
2446 bool_parms.rset_l = rset;
2447 bool_parms.rset_r = rset2;
2449 rset = rset_create (rset_kind_or, &bool_parms);
2454 count_set (rset, &glist[before-1-i].occurrences);
2464 *list = glist + i; /* list is set to first 'real' entry */
2466 logf (LOG_DEBUG, "position = %d, num_entries = %d",
2467 *position, *num_entries);
2469 logf (LOG_DEBUG, "scan error: %d", zh->errCode);