2 * Copyright (C) 1995-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.94 1999-07-20 13:59:18 adam
8 * Fixed bug that occurred when phrases had 0 hits.
10 * Revision 1.93 1999/06/17 14:38:40 adam
11 * Bug fix: Scan SEGV'ed when getting unknown use attribute.
13 * Revision 1.92 1999/05/26 07:49:13 adam
16 * Revision 1.91 1999/02/02 14:51:13 adam
17 * Updated WIN32 code specific sections. Changed header.
19 * Revision 1.90 1998/11/16 16:03:43 adam
20 * Moved loggin utilities to Yaz. Was implemented in file zlogs.c.
22 * Revision 1.89 1998/11/16 10:11:55 adam
23 * Added addtional info for error 114 - unsupported use attribute.
25 * Revision 1.88 1998/10/18 07:54:52 adam
26 * Additional info added for diagnostics 114 (Unsupported use attribute) and
27 * 121 (Unsupported attribute set).
29 * Revision 1.87 1998/09/28 11:19:12 adam
30 * Fix for Compiled ASN.1.
32 * Revision 1.86 1998/09/22 10:48:20 adam
33 * Minor changes in search API.
35 * Revision 1.85 1998/09/22 10:03:43 adam
36 * Changed result sets to be persistent in the sense that they can
37 * be re-searched if needed.
38 * Fixed memory leak in rsm_or.
40 * Revision 1.84 1998/09/18 12:41:00 adam
41 * Fixed bug with numerical relations.
43 * Revision 1.83 1998/09/02 13:53:19 adam
44 * Extra parameter decode added to search routines to implement
47 * Revision 1.82 1998/06/26 11:16:40 quinn
48 * Added support (un-optimised) for left and left/right truncation
50 * Revision 1.81 1998/06/24 12:16:14 adam
51 * Support for relations on text operands. Open range support in
52 * DFA module (i.e. [-j], [g-]).
54 * Revision 1.80 1998/06/23 15:33:34 adam
55 * Added feature to specify sort criteria in query (type 7 specifies
58 * Revision 1.79 1998/06/22 11:35:09 adam
61 * Revision 1.78 1998/06/08 14:43:17 adam
62 * Added suport for EXPLAIN Proxy servers - added settings databasePath
63 * and explainDatabase to facilitate this. Increased maximum number
64 * of databases and attributes in one register.
66 * Revision 1.77 1998/05/20 10:12:22 adam
67 * Implemented automatic EXPLAIN database maintenance.
68 * Modified Zebra to work with ASN.1 compiled version of YAZ.
70 * Revision 1.76 1998/04/02 14:35:29 adam
71 * First version of Zebra that works with compiled ASN.1.
73 * Revision 1.75 1998/03/05 08:45:13 adam
74 * New result set model and modular ranking system. Moved towards
75 * descent server API. System information stored as "SGML" records.
77 * Revision 1.74 1998/02/10 12:03:06 adam
80 * Revision 1.73 1998/01/29 13:40:11 adam
81 * Better logging for scan service.
83 * Revision 1.72 1998/01/07 13:53:41 adam
84 * Queries using simple ranked operands returns right number of hits.
86 * Revision 1.71 1997/12/18 10:54:24 adam
87 * New method result set method rs_hits that returns the number of
88 * hits in result-set (if known). The ranked result set returns real
89 * number of hits but only when not combined with other operands.
91 * Revision 1.70 1997/10/31 12:34:43 adam
92 * Changed a few log statements.
94 * Revision 1.69 1997/10/29 12:05:02 adam
95 * Server produces diagnostic "Unsupported Attribute Set" when appropriate.
97 * Revision 1.68 1997/10/27 14:33:06 adam
98 * Moved towards generic character mapping depending on "structure"
99 * field in abstract syntax file. Fixed a few memory leaks. Fixed
100 * bug with negative integers when doing searches with relational
103 * Revision 1.67 1997/09/29 09:06:10 adam
104 * Removed one static var in order to make this module thread safe.
106 * Revision 1.66 1997/09/25 14:58:03 adam
109 * Revision 1.65 1997/09/22 12:39:06 adam
110 * Added get_pos method for the ranked result sets.
112 * Revision 1.64 1997/09/18 08:59:20 adam
113 * Extra generic handle for the character mapping routines.
115 * Revision 1.63 1997/09/17 12:19:18 adam
116 * Zebra version corresponds to YAZ version 1.4.
117 * Changed Zebra server so that it doesn't depend on global common_resource.
119 * Revision 1.62 1997/09/05 15:30:09 adam
120 * Changed prototype for chr_map_input - added const.
121 * Added support for C++, headers uses extern "C" for public definitions.
123 * Revision 1.61 1997/02/10 10:21:14 adam
124 * Bug fix: in search terms character (^) wasn't observed.
126 * Revision 1.60 1997/01/31 11:10:34 adam
127 * Bug fix: Leading and trailing white space weren't removed in scan tokens.
129 * Revision 1.59 1997/01/17 11:31:46 adam
130 * Bug fix: complete phrase search didn't work.
132 * Revision 1.58 1996/12/23 15:30:45 adam
133 * Work on truncation.
134 * Bug fix: result sets weren't deleted after server shut down.
136 * Revision 1.57 1996/11/11 13:38:02 adam
137 * Added proximity support in search.
139 * Revision 1.56 1996/11/08 11:10:32 adam
140 * Buffers used during file match got bigger.
141 * Compressed ISAM support everywhere.
142 * Bug fixes regarding masking characters in queries.
143 * Redesigned Regexp-2 queries.
145 * Revision 1.55 1996/11/04 14:07:44 adam
146 * Moved truncation code to trunc.c.
148 * Revision 1.54 1996/10/29 14:09:52 adam
149 * Use of cisam system - enabled if setting isamc is 1.
151 * Revision 1.53 1996/06/26 09:21:43 adam
152 * Bug fix: local attribute set wasn't obeyed in scan.
154 * Revision 1.52 1996/06/17 14:26:20 adam
155 * Function gen_regular_rel changed to handle negative numbers.
157 * Revision 1.51 1996/06/11 10:54:15 quinn
160 * Revision 1.50 1996/06/07 08:51:53 adam
161 * Bug fix: Character mapping was broken (introducued by last revision).
163 * Revision 1.49 1996/06/04 10:18:11 adam
164 * Search/scan uses character mapping module.
166 * Revision 1.48 1996/05/28 15:15:01 adam
167 * Bug fix: Didn't handle unknown database correctly.
169 * Revision 1.47 1996/05/15 18:36:28 adam
170 * Function trans_term transforms unsearchable characters to blanks.
172 * Revision 1.46 1996/05/15 11:57:56 adam
173 * Fixed bug introduced by set/field mapping in search operations.
175 * Revision 1.45 1996/05/14 11:34:00 adam
176 * Scan support in multiple registers/databases.
178 * Revision 1.44 1996/05/14 06:16:44 adam
179 * Compact use/set bytes used in search service.
181 * Revision 1.43 1996/05/09 09:54:43 adam
182 * Server supports maps from one logical attributes to a list of physical
184 * The extraction process doesn't make space consuming 'any' keys.
186 * Revision 1.42 1996/05/09 07:28:56 quinn
187 * Work towards phrases and multiple registers
189 * Revision 1.41 1996/03/20 09:36:43 adam
190 * Function dict_lookup_grep got extra parameter, init_pos, which marks
191 * from which position in pattern approximate pattern matching should occur.
192 * Approximate pattern matching is used in relevance=re-2.
194 * Revision 1.40 1996/02/02 13:44:44 adam
195 * The public dictionary functions simply use char instead of Dict_char
196 * to represent search strings. Dict_char is used internally only.
198 * Revision 1.39 1996/01/03 16:22:13 quinn
199 * operator->roperator
201 * Revision 1.38 1995/12/11 09:12:55 adam
202 * The rec_get function returns NULL if record doesn't exist - will
203 * happen in the server if the result set records have been deleted since
204 * the creation of the set (i.e. the search).
205 * The server saves a result temporarily if it is 'volatile', i.e. the
206 * set is register dependent.
208 * Revision 1.37 1995/12/06 15:05:28 adam
209 * More verbose in count_set.
211 * Revision 1.36 1995/12/06 12:41:27 adam
212 * New command 'stat' for the index program.
213 * Filenames can be read from stdin by specifying '-'.
214 * Bug fix/enhancement of the transformation from terms to regular
215 * expressons in the search engine.
217 * Revision 1.35 1995/11/27 09:29:00 adam
218 * Bug fixes regarding conversion to regular expressions.
220 * Revision 1.34 1995/11/16 17:00:56 adam
221 * Better logging of rpn query.
223 * Revision 1.33 1995/11/01 13:58:28 quinn
224 * Moving data1 to yaz/retrieval
226 * Revision 1.32 1995/10/27 14:00:11 adam
227 * Implemented detection of database availability.
229 * Revision 1.31 1995/10/17 18:02:10 adam
230 * New feature: databases. Implemented as prefix to words in dictionary.
232 * Revision 1.30 1995/10/16 09:32:38 adam
233 * More work on relational op.
235 * Revision 1.29 1995/10/13 16:01:49 adam
238 * Revision 1.28 1995/10/13 12:26:43 adam
239 * Optimization of truncation.
241 * Revision 1.27 1995/10/12 17:07:22 adam
244 * Revision 1.26 1995/10/12 12:40:54 adam
245 * Bug fixes in rpn_prox.
247 * Revision 1.25 1995/10/10 13:59:24 adam
248 * Function rset_open changed its wflag parameter to general flags.
250 * Revision 1.24 1995/10/09 16:18:37 adam
251 * Function dict_lookup_grep got extra client data parameter.
253 * Revision 1.23 1995/10/06 16:33:37 adam
254 * Use attribute mappings.
256 * Revision 1.22 1995/10/06 15:07:39 adam
257 * Structure 'local-number' handled.
259 * Revision 1.21 1995/10/06 13:52:06 adam
260 * Bug fixes. Handler may abort further scanning.
262 * Revision 1.20 1995/10/06 11:06:33 adam
263 * Scan entries include 'occurrences' now.
265 * Revision 1.19 1995/10/06 10:43:56 adam
266 * Scan added. 'occurrences' in scan entries not set yet.
268 * Revision 1.18 1995/10/04 16:57:20 adam
269 * Key input and merge sort in one pass.
271 * Revision 1.17 1995/10/04 12:55:17 adam
272 * Bug fix in ranked search. Use=Any keys inserted.
274 * Revision 1.16 1995/10/02 16:24:40 adam
275 * Use attribute actually used in search requests.
277 * Revision 1.15 1995/10/02 15:18:52 adam
278 * New member in recRetrieveCtrl: diagnostic.
280 * Revision 1.14 1995/09/28 12:10:32 adam
281 * Bug fixes. Field prefix used in queries.
283 * Revision 1.13 1995/09/18 14:17:50 adam
286 * Revision 1.12 1995/09/15 14:45:21 adam
288 * Work on truncation.
290 * Revision 1.11 1995/09/14 11:53:27 adam
291 * First work on regular expressions/truncations.
293 * Revision 1.10 1995/09/11 15:23:26 adam
294 * More work on relevance search.
296 * Revision 1.9 1995/09/11 13:09:35 adam
297 * More work on relevance feedback.
299 * Revision 1.8 1995/09/08 14:52:27 adam
300 * Minor changes. Dictionary is lower case now.
302 * Revision 1.7 1995/09/07 13:58:36 adam
303 * New parameter: result-set file descriptor (RSFD) to support multiple
304 * positions within the same result-set.
305 * Boolean operators: and, or, not implemented.
306 * Result-set references.
308 * Revision 1.6 1995/09/06 16:11:18 adam
309 * Option: only one word key per file.
311 * Revision 1.5 1995/09/06 10:33:04 adam
312 * More work on present. Some log messages removed.
314 * Revision 1.4 1995/09/05 15:28:40 adam
315 * More work on search engine.
317 * Revision 1.3 1995/09/04 15:20:22 adam
320 * Revision 1.2 1995/09/04 12:33:43 adam
321 * Various cleanup. YAZ util used instead.
323 * Revision 1.1 1995/09/04 09:10:40 adam
324 * More work on index add/del/update.
325 * Merge sort implemented.
326 * Initial work on z39 server.
345 struct rpn_char_map_info {
350 static const char **rpn_char_map_handler (void *vp, const char **from, int len)
352 struct rpn_char_map_info *p = (struct rpn_char_map_info *) vp;
353 return zebra_maps_input (p->zm, p->reg_type, from, len);
356 static void rpn_char_map_prepare (ZebraHandle zh, int reg_type,
357 struct rpn_char_map_info *map_info)
359 map_info->zm = zh->zebra_maps;
360 map_info->reg_type = reg_type;
361 dict_grep_cmap (zh->dict, map_info, rpn_char_map_handler);
368 Z_AttributesPlusTerm *zapt;
371 static int attr_find (AttrType *src, oid_value *attributeSetP)
376 num_attributes = src->zapt->attributes->num_attributes;
378 num_attributes = src->zapt->num_attributes;
380 while (src->major < num_attributes)
382 Z_AttributeElement *element;
385 element = src->zapt->attributes->attributes[src->major];
387 element = src->zapt->attributeList[src->major];
389 if (src->type == *element->attributeType)
391 switch (element->which)
393 case Z_AttributeValue_numeric:
395 if (element->attributeSet && attributeSetP)
399 attrset = oid_getentbyoid (element->attributeSet);
400 *attributeSetP = attrset->value;
402 return *element->value.numeric;
404 case Z_AttributeValue_complex:
405 if (src->minor >= element->value.complex->num_list ||
406 element->value.complex->list[src->minor]->which !=
407 Z_StringOrNumeric_numeric)
410 if (element->attributeSet && attributeSetP)
414 attrset = oid_getentbyoid (element->attributeSet);
415 *attributeSetP = attrset->value;
417 return *element->value.complex->list[src->minor-1]->u.numeric;
427 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
449 static void term_untrans (ZebraHandle zh, int reg_type,
450 char *dst, const char *src)
454 const char *cp = zebra_maps_output (zh->zebra_maps, reg_type, &src);
461 static void add_isam_p (const char *name, const char *info,
464 if (p->isam_p_indx == p->isam_p_size)
466 ISAM_P *new_isam_p_buf;
470 p->isam_p_size = 2*p->isam_p_size + 100;
471 new_isam_p_buf = (ISAM_P *) xmalloc (sizeof(*new_isam_p_buf) *
475 memcpy (new_isam_p_buf, p->isam_p_buf,
476 p->isam_p_indx * sizeof(*p->isam_p_buf));
477 xfree (p->isam_p_buf);
479 p->isam_p_buf = new_isam_p_buf;
482 new_term_no = (int *) xmalloc (sizeof(*new_term_no) *
486 memcpy (new_term_no, p->isam_p_buf,
487 p->isam_p_indx * sizeof(*p->term_no));
490 p->term_no = new_term_no;
493 assert (*info == sizeof(*p->isam_p_buf));
494 memcpy (p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
497 term_untrans (p->zh, p->reg_type, term_tmp, name+2);
498 logf (LOG_DEBUG, "grep: %s", term_tmp);
503 static int grep_handle (char *name, const char *info, void *p)
505 add_isam_p (name, info, (struct grep_info *) p);
509 static int term_pre (ZebraMaps zebra_maps, int reg_type, const char **src,
510 const char *ct1, const char *ct2)
512 const char *s1, *s0 = *src;
515 /* skip white space */
518 if (ct1 && strchr (ct1, *s0))
520 if (ct2 && strchr (ct2, *s0))
523 map = zebra_maps_input (zebra_maps, reg_type, &s1, strlen(s1));
524 if (**map != *CHR_SPACE)
532 /* term_100: handle term, where trunc=none (no operators at all) */
533 static int term_100 (ZebraMaps zebra_maps, int reg_type,
534 const char **src, char *dst, int space_split,
542 if (!term_pre (zebra_maps, reg_type, src, NULL, NULL))
548 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
549 if (space_split && **map == *CHR_SPACE)
553 if (!isalnum (*s1) && *s1 != '-')
565 /* term_101: handle term, where trunc=Process # */
566 static int term_101 (ZebraMaps zebra_maps, int reg_type,
567 const char **src, char *dst, int space_split,
575 if (!term_pre (zebra_maps, reg_type, src, "#", "#"))
584 dst_term[j++] = *s0++;
589 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
590 if (space_split && **map == *CHR_SPACE)
602 dst_term[j++] = '\0';
607 /* term_103: handle term, where trunc=re-2 (regular expressions) */
608 static int term_103 (ZebraMaps zebra_maps, int reg_type, const char **src,
609 char *dst, int *errors, int space_split,
617 if (!term_pre (zebra_maps, reg_type, src, "^\\()[].*+?|", "("))
620 if (errors && *s0 == '+' && s0[1] && s0[2] == '+' && s0[3] &&
623 *errors = s0[1] - '0';
630 if (strchr ("^\\()[].*+?|-", *s0))
638 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
639 if (**map == *CHR_SPACE)
656 /* term_103: handle term, where trunc=re-1 (regular expressions) */
657 static int term_102 (ZebraMaps zebra_maps, int reg_type, const char **src,
658 char *dst, int space_split, char *dst_term)
660 return term_103 (zebra_maps, reg_type, src, dst, NULL, space_split,
664 /* gen_regular_rel - generate regular expression from relation
665 * val: border value (inclusive)
666 * islt: 1 if <=; 0 if >=.
668 static void gen_regular_rel (char *dst, int val, int islt)
675 logf (LOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
679 strcpy (dst, "(-[0-9]+|(");
687 strcpy (dst, "([0-9]+|-(");
688 dst_p = strlen (dst);
698 dst_p = strlen (dst);
699 sprintf (numstr, "%d", val);
700 for (w = strlen(numstr); --w >= 0; pos++)
719 strcpy (dst + dst_p, numstr);
720 dst_p = strlen(dst) - pos - 1;
748 for (i = 0; i<pos; i++)
761 for (i=1; i<pos; i++)
762 strcat (dst, "[0-9]?");
766 for (i = 0; i <= pos; i++)
767 strcat (dst, "[0-9]");
768 strcat (dst, "[0-9]*");
773 void string_rel_add_char (char **term_p, const char *src, int *indx)
775 if (src[*indx] == '\\')
776 *(*term_p)++ = src[(*indx)++];
777 *(*term_p)++ = src[(*indx)++];
781 * > abc ([b-].*|a[c-].*|ab[d-].*|abc.+)
782 * ([^-a].*|a[^-b].*ab[^-c].*|abc.+)
783 * >= abc ([b-].*|a[c-].*|ab[c-].*)
784 * ([^-a].*|a[^-b].*|ab[c-].*)
785 * < abc ([-0].*|a[-a].*|ab[-b].*)
786 * ([^a-].*|a[^b-].*|ab[^c-].*)
787 * <= abc ([-0].*|a[-a].*|ab[-b].*|abc)
788 * ([^a-].*|a[^b-].*|ab[^c-].*|abc)
790 static int string_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
791 const char **term_sub, char *term_dict,
792 oid_value attributeSet,
793 int reg_type, int space_split, char *term_dst)
798 char *term_tmp = term_dict + strlen(term_dict);
799 char term_component[256];
801 attr_init (&relation, zapt, 2);
802 relation_value = attr_find (&relation, NULL);
804 logf (LOG_DEBUG, "string relation value=%d", relation_value);
805 switch (relation_value)
808 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
809 space_split, term_dst))
811 logf (LOG_DEBUG, "Relation <");
814 for (i = 0; term_component[i]; )
821 string_rel_add_char (&term_tmp, term_component, &j);
826 string_rel_add_char (&term_tmp, term_component, &i);
837 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
838 space_split, term_dst))
840 logf (LOG_DEBUG, "Relation <=");
843 for (i = 0; term_component[i]; )
848 string_rel_add_char (&term_tmp, term_component, &j);
852 string_rel_add_char (&term_tmp, term_component, &i);
861 for (i = 0; term_component[i]; )
862 string_rel_add_char (&term_tmp, term_component, &i);
867 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
868 space_split, term_dst))
870 logf (LOG_DEBUG, "Relation >");
873 for (i = 0; term_component[i];)
878 string_rel_add_char (&term_tmp, term_component, &j);
883 string_rel_add_char (&term_tmp, term_component, &i);
891 for (i = 0; term_component[i];)
892 string_rel_add_char (&term_tmp, term_component, &i);
899 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
900 space_split, term_dst))
902 logf (LOG_DEBUG, "Relation >=");
905 for (i = 0; term_component[i];)
912 string_rel_add_char (&term_tmp, term_component, &j);
915 if (term_component[i+1])
919 string_rel_add_char (&term_tmp, term_component, &i);
923 string_rel_add_char (&term_tmp, term_component, &i);
935 logf (LOG_DEBUG, "Relation =");
936 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_component,
937 space_split, term_dst))
939 strcat (term_tmp, "(");
940 strcat (term_tmp, term_component);
941 strcat (term_tmp, ")");
946 static int string_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
947 const char **term_sub,
948 oid_value attributeSet, NMEM stream,
949 struct grep_info *grep_info,
950 int reg_type, int complete_flag,
951 int num_bases, char **basenames,
954 char term_dict[2*IT_MAX_WORD+4000];
957 int truncation_value;
960 oid_value curAttributeSet = attributeSet;
962 struct rpn_char_map_info rcmi;
963 int space_split = complete_flag ? 0 : 1;
965 rpn_char_map_prepare (zh, reg_type, &rcmi);
966 attr_init (&use, zapt, 1);
967 use_value = attr_find (&use, &curAttributeSet);
968 logf (LOG_DEBUG, "string_term, use value %d", use_value);
969 attr_init (&truncation, zapt, 5);
970 truncation_value = attr_find (&truncation, NULL);
971 logf (LOG_DEBUG, "truncation value %d", truncation_value);
976 for (base_no = 0; base_no < num_bases; base_no++)
979 data1_local_attribute *local_attr;
980 int max_pos, prefix_len = 0;
983 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
985 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
986 curAttributeSet, use_value, r);
990 sprintf (val_str, "%d", use_value);
992 zh->errString = nmem_strdup (stream, val_str);
997 struct oident oident;
999 oident.proto = PROTO_Z3950;
1000 oident.oclass = CLASS_ATTSET;
1001 oident.value = curAttributeSet;
1002 oid_ent_to_oid (&oident, oid);
1005 zh->errString = nmem_strdup (stream, oident.desc);
1009 if (zebraExplain_curDatabase (zh->zei, basenames[base_no]))
1011 zh->errCode = 109; /* Database unavailable */
1012 zh->errString = basenames[base_no];
1015 for (local_attr = attp.local_attributes; local_attr;
1016 local_attr = local_attr->next)
1022 ord = zebraExplain_lookupSU (zh->zei, attp.attset_ordinal,
1027 term_dict[prefix_len++] = '|';
1029 term_dict[prefix_len++] = '(';
1031 ord_len = key_SU_code (ord, ord_buf);
1032 for (i = 0; i<ord_len; i++)
1034 term_dict[prefix_len++] = 1;
1035 term_dict[prefix_len++] = ord_buf[i];
1041 sprintf (val_str, "%d", use_value);
1043 zh->errString = nmem_strdup (stream, val_str);
1046 term_dict[prefix_len++] = ')';
1047 term_dict[prefix_len++] = 1;
1048 term_dict[prefix_len++] = reg_type;
1049 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1050 term_dict[prefix_len] = '\0';
1052 switch (truncation_value)
1054 case -1: /* not specified */
1055 case 100: /* do not truncate */
1056 if (!string_relation (zh, zapt, &termp, term_dict,
1058 reg_type, space_split, term_dst))
1060 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_dict+prefix_len);
1061 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info, &max_pos,
1064 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1066 case 1: /* right truncation */
1067 term_dict[j++] = '(';
1068 if (!term_100 (zh->zebra_maps, reg_type,
1069 &termp, term_dict + j, space_split, term_dst))
1071 strcat (term_dict, ".*)");
1072 dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1073 &max_pos, 0, grep_handle);
1075 case 2: /* keft truncation */
1076 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1077 if (!term_100 (zh->zebra_maps, reg_type,
1078 &termp, term_dict + j, space_split, term_dst))
1080 strcat (term_dict, ")");
1081 dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1082 &max_pos, 0, grep_handle);
1084 case 3: /* left&right truncation */
1085 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1086 if (!term_100 (zh->zebra_maps, reg_type,
1087 &termp, term_dict + j, space_split, term_dst))
1089 strcat (term_dict, ".*)");
1090 dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1091 &max_pos, 0, grep_handle);
1095 case 101: /* process # in term */
1096 term_dict[j++] = '(';
1097 if (!term_101 (zh->zebra_maps, reg_type,
1098 &termp, term_dict + j, space_split, term_dst))
1100 strcat (term_dict, ")");
1101 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1102 &max_pos, 0, grep_handle);
1104 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d", r);
1106 case 102: /* Regexp-1 */
1107 term_dict[j++] = '(';
1108 if (!term_102 (zh->zebra_maps, reg_type,
1109 &termp, term_dict + j, space_split, term_dst))
1111 strcat (term_dict, ")");
1112 logf (LOG_DEBUG, "Regexp-1 tolerance=%d", r);
1113 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
1114 &max_pos, 0, grep_handle);
1116 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
1119 case 103: /* Regexp-2 */
1121 term_dict[j++] = '(';
1122 if (!term_103 (zh->zebra_maps, reg_type,
1123 &termp, term_dict + j, &r, space_split, term_dst))
1125 strcat (term_dict, ")");
1126 logf (LOG_DEBUG, "Regexp-2 tolerance=%d", r);
1127 r = dict_lookup_grep (zh->dict, term_dict, r, grep_info,
1128 &max_pos, 2, grep_handle);
1130 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
1136 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1140 static void trans_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1144 Z_Term *term = zapt->term;
1146 sizez = term->u.general->len;
1147 if (sizez > IT_MAX_WORD-1)
1148 sizez = IT_MAX_WORD-1;
1149 memcpy (termz, term->u.general->buf, sizez);
1150 termz[sizez] = '\0';
1153 static void trans_scan_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1154 char *termz, int reg_type)
1156 Z_Term *term = zapt->term;
1158 const char *cp = (const char *) term->u.general->buf;
1159 const char *cp_end = cp + term->u.general->len;
1162 const char *space_map = NULL;
1165 while ((len = (cp_end - cp)) > 0)
1167 map = zebra_maps_input (zh->zebra_maps, reg_type, &cp, len);
1168 if (**map == *CHR_SPACE)
1173 for (src = space_map; *src; src++)
1176 for (src = *map; *src; src++)
1183 static RSET rpn_proximity (ZebraHandle zh, RSET rset1, RSET rset2,
1185 int exclusion, int relation, int distance)
1190 struct it_key buf1, buf2;
1193 rset_temp_parms parms;
1196 rsfd1 = rset_open (rset1, RSETF_READ);
1197 more1 = rset_read (rset1, rsfd1, &buf1, &term_index);
1199 rsfd2 = rset_open (rset2, RSETF_READ);
1200 more2 = rset_read (rset2, rsfd2, &buf2, &term_index);
1202 parms.key_size = sizeof (struct it_key);
1203 parms.temp_path = res_get (zh->res, "setTmpDir");
1204 result = rset_create (rset_kind_temp, &parms);
1205 rsfd_result = rset_open (result, RSETF_WRITE);
1207 logf (LOG_DEBUG, "rpn_proximity excl=%d ord=%d rel=%d dis=%d",
1208 exclusion, ordered, relation, distance);
1209 while (more1 && more2)
1211 int cmp = key_compare_it (&buf1, &buf2);
1213 more1 = rset_read (rset1, rsfd1, &buf1, &term_index);
1215 more2 = rset_read (rset2, rsfd2, &buf2, &term_index);
1218 int sysno = buf1.sysno;
1222 seqno[n++] = buf1.seqno;
1223 while ((more1 = rset_read (rset1, rsfd1, &buf1, &term_index)) &&
1224 sysno == buf1.sysno)
1226 seqno[n++] = buf1.seqno;
1229 for (i = 0; i<n; i++)
1231 int diff = buf2.seqno - seqno[i];
1232 int excl = exclusion;
1233 if (!ordered && diff < 0)
1238 if (diff < distance)
1242 if (diff <= distance)
1246 if (diff == distance)
1250 if (diff >= distance)
1254 if (diff > distance)
1258 if (diff != distance)
1263 rset_write (result, rsfd_result, &buf2);
1265 } while ((more2 = rset_read (rset2, rsfd2, &buf2, &term_index)) &&
1266 sysno == buf2.sysno);
1269 rset_close (result, rsfd_result);
1270 rset_close (rset1, rsfd1);
1271 rset_close (rset2, rsfd2);
1275 static RSET rpn_prox (ZebraHandle zh, RSET *rset, int rset_no)
1280 struct it_key **buf;
1282 char prox_term[1024];
1283 int length_prox_term = 0;
1284 int min_nn = 10000000;
1286 const char *flags = NULL;
1288 rsfd = (RSFD *) xmalloc (sizeof(*rsfd)*rset_no);
1289 more = (int *) xmalloc (sizeof(*more)*rset_no);
1290 buf = (struct it_key **) xmalloc (sizeof(*buf)*rset_no);
1293 for (i = 0; i<rset_no; i++)
1296 for (j = 0; j<rset[i]->no_rset_terms; j++)
1298 const char *nflags = rset[i]->rset_terms[j]->flags;
1299 char *term = rset[i]->rset_terms[j]->name;
1300 int lterm = strlen(term);
1301 if (lterm + length_prox_term < sizeof(prox_term)-1)
1303 if (length_prox_term)
1304 prox_term[length_prox_term++] = ' ';
1305 strcpy (prox_term + length_prox_term, term);
1306 length_prox_term += lterm;
1308 if (min_nn > rset[i]->rset_terms[j]->nn)
1309 min_nn = rset[i]->rset_terms[j]->nn;
1313 for (i = 0; i<rset_no; i++)
1315 buf[i] = (struct it_key *) xmalloc (sizeof(**buf));
1316 rsfd[i] = rset_open (rset[i], RSETF_READ);
1317 if (!(more[i] = rset_read (rset[i], rsfd[i], buf[i], &term_index)))
1322 rset_null_parms parms;
1326 rset_close (rset[i], rsfd[i]);
1330 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1332 parms.rset_term->nn = 0;
1333 result = rset_create (rset_kind_null, &parms);
1337 rset_temp_parms parms;
1340 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1342 parms.rset_term->nn = min_nn;
1343 parms.key_size = sizeof (struct it_key);
1344 parms.temp_path = res_get (zh->res, "setTmpDir");
1345 result = rset_create (rset_kind_temp, &parms);
1346 rsfd_result = rset_open (result, RSETF_WRITE);
1350 for (i = 1; i<rset_no; i++)
1359 cmp = key_compare_it (buf[i], buf[i-1]);
1362 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1363 buf[i-1], &term_index);
1368 if (buf[i-1]->seqno+1 != buf[i]->seqno)
1370 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1371 buf[i-1], &term_index);
1377 more[i] = rset_read (rset[i], rsfd[i], buf[i],
1384 rset_write (result, rsfd_result, buf[0]);
1385 more[0] = rset_read (*rset, *rsfd, *buf, &term_index);
1389 for (i = 0; i<rset_no; i++)
1391 rset_close (rset[i], rsfd[i]);
1394 rset_close (result, rsfd_result);
1402 static RSET rpn_search_APT_phrase (ZebraHandle zh,
1403 Z_AttributesPlusTerm *zapt,
1405 oid_value attributeSet,
1407 int reg_type, int complete_flag,
1408 const char *rank_type,
1409 int num_bases, char **basenames)
1411 char term_dst[IT_MAX_WORD+1];
1412 const char *termp = termz;
1413 RSET rset[60], result;
1414 int i, r, rset_no = 0;
1415 struct grep_info grep_info;
1418 grep_info.term_no = 0;
1420 grep_info.isam_p_size = 0;
1421 grep_info.isam_p_buf = NULL;
1423 grep_info.reg_type = reg_type;
1427 logf (LOG_DEBUG, "APT_phrase termp=%s", termp);
1428 grep_info.isam_p_indx = 0;
1429 r = string_term (zh, zapt, &termp, attributeSet, stream, &grep_info,
1430 reg_type, complete_flag, num_bases, basenames,
1434 logf (LOG_DEBUG, "term: %s", term_dst);
1435 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1436 grep_info.isam_p_indx, term_dst,
1437 strlen(term_dst), rank_type);
1438 assert (rset[rset_no]);
1439 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1443 xfree(grep_info.term_no);
1445 xfree (grep_info.isam_p_buf);
1448 rset_null_parms parms;
1450 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1451 return rset_create (rset_kind_null, &parms);
1453 else if (rset_no == 1)
1455 result = rpn_prox (zh, rset, rset_no);
1456 for (i = 0; i<rset_no; i++)
1457 rset_delete (rset[i]);
1461 static RSET rpn_search_APT_or_list (ZebraHandle zh,
1462 Z_AttributesPlusTerm *zapt,
1464 oid_value attributeSet,
1466 int reg_type, int complete_flag,
1467 const char *rank_type,
1468 int num_bases, char **basenames)
1470 char term_dst[IT_MAX_WORD+1];
1471 const char *termp = termz;
1472 RSET rset[60], result;
1473 int i, r, rset_no = 0;
1474 struct grep_info grep_info;
1477 grep_info.term_no = 0;
1479 grep_info.isam_p_size = 0;
1480 grep_info.isam_p_buf = NULL;
1482 grep_info.reg_type = reg_type;
1486 logf (LOG_DEBUG, "APT_or_list termp=%s", termp);
1487 grep_info.isam_p_indx = 0;
1488 r = string_term (zh, zapt, &termp, attributeSet, stream, &grep_info,
1489 reg_type, complete_flag, num_bases, basenames,
1493 logf (LOG_DEBUG, "term: %s", term_dst);
1494 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1495 grep_info.isam_p_indx, term_dst,
1496 strlen(term_dst), rank_type);
1497 assert (rset[rset_no]);
1498 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1502 xfree(grep_info.term_no);
1504 xfree (grep_info.isam_p_buf);
1507 rset_null_parms parms;
1509 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1510 return rset_create (rset_kind_null, &parms);
1513 for (i = 1; i<rset_no; i++)
1515 rset_bool_parms bool_parms;
1517 bool_parms.rset_l = result;
1518 bool_parms.rset_r = rset[i];
1519 bool_parms.key_size = sizeof(struct it_key);
1520 bool_parms.cmp = key_compare_it;
1521 result = rset_create (rset_kind_or, &bool_parms);
1526 static RSET rpn_search_APT_and_list (ZebraHandle zh,
1527 Z_AttributesPlusTerm *zapt,
1529 oid_value attributeSet,
1531 int reg_type, int complete_flag,
1532 const char *rank_type,
1533 int num_bases, char **basenames)
1535 char term_dst[IT_MAX_WORD+1];
1536 const char *termp = termz;
1537 RSET rset[60], result;
1538 int i, r, rset_no = 0;
1539 struct grep_info grep_info;
1542 grep_info.term_no = 0;
1544 grep_info.isam_p_size = 0;
1545 grep_info.isam_p_buf = NULL;
1547 grep_info.reg_type = reg_type;
1551 logf (LOG_DEBUG, "APT_and_list termp=%s", termp);
1552 grep_info.isam_p_indx = 0;
1553 r = string_term (zh, zapt, &termp, attributeSet, stream, &grep_info,
1554 reg_type, complete_flag, num_bases, basenames,
1558 logf (LOG_DEBUG, "term: %s", term_dst);
1559 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1560 grep_info.isam_p_indx, term_dst,
1561 strlen(term_dst), rank_type);
1562 assert (rset[rset_no]);
1563 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1567 xfree(grep_info.term_no);
1569 xfree (grep_info.isam_p_buf);
1572 rset_null_parms parms;
1574 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1575 return rset_create (rset_kind_null, &parms);
1578 for (i = 1; i<rset_no; i++)
1580 rset_bool_parms bool_parms;
1582 bool_parms.rset_l = result;
1583 bool_parms.rset_r = rset[i];
1584 bool_parms.key_size = sizeof(struct it_key);
1585 bool_parms.cmp = key_compare_it;
1586 result = rset_create (rset_kind_and, &bool_parms);
1591 static int numeric_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1592 const char **term_sub,
1594 oid_value attributeSet,
1595 struct grep_info *grep_info,
1604 char *term_tmp = term_dict + strlen(term_dict);
1606 attr_init (&relation, zapt, 2);
1607 relation_value = attr_find (&relation, NULL);
1609 logf (LOG_DEBUG, "numeric relation value=%d", relation_value);
1611 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_tmp, 1,
1614 term_value = atoi (term_tmp);
1615 switch (relation_value)
1618 logf (LOG_DEBUG, "Relation <");
1619 gen_regular_rel (term_tmp, term_value-1, 1);
1622 logf (LOG_DEBUG, "Relation <=");
1623 gen_regular_rel (term_tmp, term_value, 1);
1626 logf (LOG_DEBUG, "Relation >=");
1627 gen_regular_rel (term_tmp, term_value, 0);
1630 logf (LOG_DEBUG, "Relation >");
1631 gen_regular_rel (term_tmp, term_value+1, 0);
1635 logf (LOG_DEBUG, "Relation =");
1636 sprintf (term_tmp, "(0*%d)", term_value);
1638 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_tmp);
1639 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info, max_pos,
1642 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1643 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1647 static int numeric_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1648 const char **term_sub,
1649 oid_value attributeSet, struct grep_info *grep_info,
1650 int reg_type, int complete_flag,
1651 int num_bases, char **basenames,
1654 char term_dict[2*IT_MAX_WORD+2];
1658 oid_value curAttributeSet = attributeSet;
1660 struct rpn_char_map_info rcmi;
1662 rpn_char_map_prepare (zh, reg_type, &rcmi);
1663 attr_init (&use, zapt, 1);
1664 use_value = attr_find (&use, &curAttributeSet);
1665 logf (LOG_DEBUG, "numeric_term, use value %d", use_value);
1667 if (use_value == -1)
1670 for (base_no = 0; base_no < num_bases; base_no++)
1673 data1_local_attribute *local_attr;
1674 int max_pos, prefix_len = 0;
1677 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
1679 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1680 curAttributeSet, use_value, r);
1687 if (zebraExplain_curDatabase (zh->zei, basenames[base_no]))
1689 zh->errCode = 109; /* Database unavailable */
1690 zh->errString = basenames[base_no];
1693 for (local_attr = attp.local_attributes; local_attr;
1694 local_attr = local_attr->next)
1700 ord = zebraExplain_lookupSU (zh->zei, attp.attset_ordinal,
1705 term_dict[prefix_len++] = '|';
1707 term_dict[prefix_len++] = '(';
1709 ord_len = key_SU_code (ord, ord_buf);
1710 for (i = 0; i<ord_len; i++)
1712 term_dict[prefix_len++] = 1;
1713 term_dict[prefix_len++] = ord_buf[i];
1721 term_dict[prefix_len++] = ')';
1722 term_dict[prefix_len++] = 1;
1723 term_dict[prefix_len++] = reg_type;
1724 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1725 term_dict[prefix_len] = '\0';
1726 if (!numeric_relation (zh, zapt, &termp, term_dict,
1727 attributeSet, grep_info, &max_pos, reg_type,
1732 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1736 static RSET rpn_search_APT_numeric (ZebraHandle zh,
1737 Z_AttributesPlusTerm *zapt,
1739 oid_value attributeSet,
1741 int reg_type, int complete_flag,
1742 const char *rank_type,
1743 int num_bases, char **basenames)
1745 char term_dst[IT_MAX_WORD+1];
1746 const char *termp = termz;
1747 RSET rset[60], result;
1748 int i, r, rset_no = 0;
1749 struct grep_info grep_info;
1752 grep_info.term_no = 0;
1754 grep_info.isam_p_size = 0;
1755 grep_info.isam_p_buf = NULL;
1757 grep_info.reg_type = reg_type;
1761 logf (LOG_DEBUG, "APT_numeric termp=%s", termp);
1762 grep_info.isam_p_indx = 0;
1763 r = numeric_term (zh, zapt, &termp, attributeSet, &grep_info,
1764 reg_type, complete_flag, num_bases, basenames,
1768 logf (LOG_DEBUG, "term: %s", term_dst);
1769 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1770 grep_info.isam_p_indx, term_dst,
1771 strlen(term_dst), rank_type);
1772 assert (rset[rset_no]);
1773 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1777 xfree(grep_info.term_no);
1779 xfree (grep_info.isam_p_buf);
1782 rset_null_parms parms;
1784 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1785 return rset_create (rset_kind_null, &parms);
1788 for (i = 1; i<rset_no; i++)
1790 rset_bool_parms bool_parms;
1792 bool_parms.rset_l = result;
1793 bool_parms.rset_r = rset[i];
1794 bool_parms.key_size = sizeof(struct it_key);
1795 bool_parms.cmp = key_compare_it;
1796 result = rset_create (rset_kind_and, &bool_parms);
1801 static RSET rpn_search_APT_local (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1803 oid_value attributeSet,
1805 const char *rank_type)
1810 rset_temp_parms parms;
1812 parms.rset_term = rset_term_create (termz, -1, rank_type);
1813 parms.key_size = sizeof (struct it_key);
1814 parms.temp_path = res_get (zh->res, "setTmpDir");
1815 result = rset_create (rset_kind_temp, &parms);
1816 rsfd = rset_open (result, RSETF_WRITE);
1818 key.sysno = atoi (termz);
1822 rset_write (result, rsfd, &key);
1823 rset_close (result, rsfd);
1827 static RSET rpn_sort_spec (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1828 oid_value attributeSet, NMEM stream,
1829 Z_SortKeySpecList *sort_sequence,
1830 const char *rank_type)
1832 rset_null_parms parms;
1834 int sort_relation_value;
1835 AttrType sort_relation_type;
1840 Z_AttributeElement *ae;
1844 attr_init (&sort_relation_type, zapt, 7);
1845 sort_relation_value = attr_find (&sort_relation_type, &attributeSet);
1847 attr_init (&use_type, zapt, 1);
1848 use_value = attr_find (&use_type, &attributeSet);
1850 if (!sort_sequence->specs)
1852 sort_sequence->num_specs = 10;
1853 sort_sequence->specs = (Z_SortKeySpec **)
1854 nmem_malloc (stream, sort_sequence->num_specs *
1855 sizeof(*sort_sequence->specs));
1856 for (i = 0; i<sort_sequence->num_specs; i++)
1857 sort_sequence->specs[i] = 0;
1859 if (zapt->term->which != Z_Term_general)
1862 i = atoi_n ((char *) zapt->term->u.general->buf,
1863 zapt->term->u.general->len);
1864 if (i >= sort_sequence->num_specs)
1867 oe.proto = PROTO_Z3950;
1868 oe.oclass = CLASS_ATTSET;
1869 oe.value = attributeSet;
1870 if (!oid_ent_to_oid (&oe, oid))
1873 sks = (Z_SortKeySpec *) nmem_malloc (stream, sizeof(*sks));
1874 sks->sortElement = (Z_SortElement *)
1875 nmem_malloc (stream, sizeof(*sks->sortElement));
1876 sks->sortElement->which = Z_SortElement_generic;
1877 sk = sks->sortElement->u.generic = (Z_SortKey *)
1878 nmem_malloc (stream, sizeof(*sk));
1879 sk->which = Z_SortKey_sortAttributes;
1880 sk->u.sortAttributes = (Z_SortAttributes *)
1881 nmem_malloc (stream, sizeof(*sk->u.sortAttributes));
1883 sk->u.sortAttributes->id = oid;
1884 sk->u.sortAttributes->list = (Z_AttributeList *)
1885 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list));
1886 sk->u.sortAttributes->list->num_attributes = 1;
1887 sk->u.sortAttributes->list->attributes = (Z_AttributeElement **)
1888 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list->attributes));
1889 ae = *sk->u.sortAttributes->list->attributes = (Z_AttributeElement *)
1890 nmem_malloc (stream, sizeof(**sk->u.sortAttributes->list->attributes));
1891 ae->attributeSet = 0;
1892 ae->attributeType = (int *)
1893 nmem_malloc (stream, sizeof(*ae->attributeType));
1894 *ae->attributeType = 1;
1895 ae->which = Z_AttributeValue_numeric;
1896 ae->value.numeric = (int *)
1897 nmem_malloc (stream, sizeof(*ae->value.numeric));
1898 *ae->value.numeric = use_value;
1900 sks->sortRelation = (int *)
1901 nmem_malloc (stream, sizeof(*sks->sortRelation));
1902 if (sort_relation_value == 1)
1903 *sks->sortRelation = Z_SortRelation_ascending;
1904 else if (sort_relation_value == 2)
1905 *sks->sortRelation = Z_SortRelation_descending;
1907 *sks->sortRelation = Z_SortRelation_ascending;
1909 sks->caseSensitivity = (int *)
1910 nmem_malloc (stream, sizeof(*sks->caseSensitivity));
1911 *sks->caseSensitivity = 0;
1914 sks->which = Z_SortKeySpec_null;
1915 sks->u.null = odr_nullval ();
1917 sks->missingValueAction = 0;
1920 sort_sequence->specs[i] = sks;
1922 parms.rset_term = rset_term_create ("", -1, rank_type);
1923 return rset_create (rset_kind_null, &parms);
1927 static RSET rpn_search_APT (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1928 oid_value attributeSet, NMEM stream,
1929 Z_SortKeySpecList *sort_sequence,
1930 int num_bases, char **basenames)
1933 char *search_type = NULL;
1934 char *rank_type = NULL;
1937 char termz[IT_MAX_WORD+1];
1939 zebra_maps_attr (zh->zebra_maps, zapt, ®_id, &search_type,
1940 &rank_type, &complete_flag, &sort_flag);
1942 logf (LOG_DEBUG, "reg_id=%c", reg_id);
1943 logf (LOG_DEBUG, "complete_flag=%d", complete_flag);
1944 logf (LOG_DEBUG, "search_type=%s", search_type);
1945 logf (LOG_DEBUG, "rank_type=%s", rank_type);
1947 if (zapt->term->which != Z_Term_general)
1952 trans_term (zh, zapt, termz);
1955 return rpn_sort_spec (zh, zapt, attributeSet, stream, sort_sequence,
1958 if (!strcmp (search_type, "phrase"))
1960 return rpn_search_APT_phrase (zh, zapt, termz, attributeSet, stream,
1961 reg_id, complete_flag, rank_type,
1962 num_bases, basenames);
1964 else if (!strcmp (search_type, "and-list"))
1966 return rpn_search_APT_and_list (zh, zapt, termz, attributeSet, stream,
1967 reg_id, complete_flag, rank_type,
1968 num_bases, basenames);
1970 else if (!strcmp (search_type, "or-list"))
1972 return rpn_search_APT_or_list (zh, zapt, termz, attributeSet, stream,
1973 reg_id, complete_flag, rank_type,
1974 num_bases, basenames);
1976 else if (!strcmp (search_type, "local"))
1978 return rpn_search_APT_local (zh, zapt, termz, attributeSet, stream,
1981 else if (!strcmp (search_type, "numeric"))
1983 return rpn_search_APT_numeric (zh, zapt, termz, attributeSet, stream,
1984 reg_id, complete_flag, rank_type,
1985 num_bases, basenames);
1991 static RSET rpn_search_structure (ZebraHandle zh, Z_RPNStructure *zs,
1992 oid_value attributeSet, NMEM stream,
1993 Z_SortKeySpecList *sort_sequence,
1994 int num_bases, char **basenames)
1997 if (zs->which == Z_RPNStructure_complex)
1999 Z_Operator *zop = zs->u.complex->roperator;
2000 rset_bool_parms bool_parms;
2002 bool_parms.rset_l = rpn_search_structure (zh, zs->u.complex->s1,
2003 attributeSet, stream,
2005 num_bases, basenames);
2006 if (bool_parms.rset_l == NULL)
2008 bool_parms.rset_r = rpn_search_structure (zh, zs->u.complex->s2,
2009 attributeSet, stream,
2011 num_bases, basenames);
2012 if (bool_parms.rset_r == NULL)
2014 rset_delete (bool_parms.rset_l);
2017 bool_parms.key_size = sizeof(struct it_key);
2018 bool_parms.cmp = key_compare_it;
2022 case Z_Operator_and:
2023 r = rset_create (rset_kind_and, &bool_parms);
2026 r = rset_create (rset_kind_or, &bool_parms);
2028 case Z_Operator_and_not:
2029 r = rset_create (rset_kind_not, &bool_parms);
2031 case Z_Operator_prox:
2033 if (zop->u.prox->which != Z_ProximityOperator_known)
2039 if (zop->u.prox->which != Z_ProxCode_known)
2047 if (*zop->u.prox->u.known != Z_ProxUnit_word)
2049 char *val = (char *) nmem_malloc (stream, 16);
2051 zh->errString = val;
2052 sprintf (val, "%d", *zop->u.prox->u.known);
2056 if (*zop->u.prox->proximityUnitCode != Z_ProxUnit_word)
2058 char *val = (char *) nmem_malloc (stream, 16);
2060 zh->errString = val;
2061 sprintf (val, "%d", *zop->u.prox->proximityUnitCode);
2065 r = rpn_proximity (zh, bool_parms.rset_l, bool_parms.rset_r,
2066 *zop->u.prox->ordered,
2067 (!zop->u.prox->exclusion ? 0 :
2068 *zop->u.prox->exclusion),
2069 *zop->u.prox->relationType,
2070 *zop->u.prox->distance);
2077 else if (zs->which == Z_RPNStructure_simple)
2079 if (zs->u.simple->which == Z_Operand_APT)
2081 logf (LOG_DEBUG, "rpn_search_APT");
2082 r = rpn_search_APT (zh, zs->u.simple->u.attributesPlusTerm,
2083 attributeSet, stream, sort_sequence,
2084 num_bases, basenames);
2086 else if (zs->u.simple->which == Z_Operand_resultSetId)
2088 logf (LOG_DEBUG, "rpn_search_ref");
2089 r = resultSetRef (zh, zs->u.simple->u.resultSetId);
2091 r = rset_create (rset_kind_null, NULL);
2108 RSET rpn_search (ZebraHandle zh, NMEM nmem,
2109 Z_RPNQuery *rpn, int num_bases, char **basenames,
2110 const char *setname,
2115 oid_value attributeSet;
2116 Z_SortKeySpecList *sort_sequence;
2120 zh->errString = NULL;
2123 sort_sequence = (Z_SortKeySpecList *)
2124 nmem_malloc (nmem, sizeof(*sort_sequence));
2125 sort_sequence->num_specs = 10;
2126 sort_sequence->specs = (Z_SortKeySpec **)
2127 nmem_malloc (nmem, sort_sequence->num_specs *
2128 sizeof(*sort_sequence->specs));
2129 for (i = 0; i<sort_sequence->num_specs; i++)
2130 sort_sequence->specs[i] = 0;
2132 attrset = oid_getentbyoid (rpn->attributeSetId);
2133 attributeSet = attrset->value;
2134 rset = rpn_search_structure (zh, rpn->RPNStructure, attributeSet,
2135 nmem, sort_sequence, num_bases, basenames);
2140 logf (LOG_DEBUG, "search error: %d", zh->errCode);
2142 for (i = 0; sort_sequence->specs[i]; i++)
2144 sort_sequence->num_specs = i;
2146 resultSetRank (zh, sset, rset);
2149 logf (LOG_DEBUG, "resultSetSortSingle in rpn_search");
2150 resultSetSortSingle (zh, nmem, sset, rset,
2151 sort_sequence, &sort_status);
2154 logf (LOG_DEBUG, "resultSetSortSingle status = %d", zh->errCode);
2160 struct scan_info_entry {
2166 struct scan_info_entry *list;
2172 static int scan_handle (char *name, const char *info, int pos, void *client)
2174 int len_prefix, idx;
2175 struct scan_info *scan_info = (struct scan_info *) client;
2177 len_prefix = strlen(scan_info->prefix);
2178 if (memcmp (name, scan_info->prefix, len_prefix))
2180 if (pos > 0) idx = scan_info->after - pos + scan_info->before;
2183 scan_info->list[idx].term = (char *)
2184 odr_malloc (scan_info->odr, strlen(name + len_prefix)+1);
2185 strcpy (scan_info->list[idx].term, name + len_prefix);
2186 assert (*info == sizeof(ISAM_P));
2187 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAM_P));
2191 static void scan_term_untrans (ZebraHandle zh, NMEM stream, int reg_type,
2192 char **dst, const char *src)
2194 char term_dst[1024];
2196 term_untrans (zh, reg_type, term_dst, src);
2198 *dst = (char *) nmem_malloc (stream, strlen(term_dst)+1);
2199 strcpy (*dst, term_dst);
2202 static void count_set (RSET r, int *count)
2210 logf (LOG_DEBUG, "count_set");
2213 rfd = rset_open (r, RSETF_READ);
2214 while (rset_read (r, rfd, &key, &term_index))
2216 if (key.sysno != psysno)
2223 rset_close (r, rfd);
2224 logf (LOG_DEBUG, "%d keys, %d records", kno, *count);
2227 void rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
2228 oid_value attributeset,
2229 int num_bases, char **basenames,
2230 int *position, int *num_entries, ZebraScanEntry **list,
2234 int pos = *position;
2235 int num = *num_entries;
2239 char termz[IT_MAX_WORD+20];
2242 struct scan_info *scan_info_array;
2243 ZebraScanEntry *glist;
2244 int ords[32], ord_no = 0;
2248 char *search_type = NULL;
2249 char *rank_type = NULL;
2254 if (attributeset == VAL_NONE)
2255 attributeset = VAL_BIB1;
2257 logf (LOG_DEBUG, "position = %d, num = %d", pos, num);
2259 attr_init (&use, zapt, 1);
2260 use_value = attr_find (&use, &attributeset);
2262 if (zebra_maps_attr (zh->zebra_maps, zapt, ®_id, &search_type,
2263 &rank_type, &complete_flag, &sort_flag))
2269 if (use_value == -1)
2271 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
2275 data1_local_attribute *local_attr;
2277 if ((r=att_getentbyatt (zh, &attp, attributeset, use_value)))
2279 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
2280 attributeset, use_value);
2288 if (zebraExplain_curDatabase (zh->zei, basenames[base_no]))
2290 zh->errString = basenames[base_no];
2291 zh->errCode = 109; /* Database unavailable */
2294 for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
2295 local_attr = local_attr->next)
2299 ord = zebraExplain_lookupSU (zh->zei, attp.attset_ordinal,
2302 ords[ord_no++] = ord;
2310 /* prepare dictionary scanning */
2313 scan_info_array = (struct scan_info *)
2314 odr_malloc (stream, ord_no * sizeof(*scan_info_array));
2315 for (i = 0; i < ord_no; i++)
2317 int j, prefix_len = 0;
2318 int before_tmp = before, after_tmp = after;
2319 struct scan_info *scan_info = scan_info_array + i;
2320 struct rpn_char_map_info rcmi;
2322 rpn_char_map_prepare (zh, reg_id, &rcmi);
2324 scan_info->before = before;
2325 scan_info->after = after;
2326 scan_info->odr = stream;
2328 scan_info->list = (struct scan_info_entry *)
2329 odr_malloc (stream, (before+after) * sizeof(*scan_info->list));
2330 for (j = 0; j<before+after; j++)
2331 scan_info->list[j].term = NULL;
2333 prefix_len += key_SU_code (ords[i], termz + prefix_len);
2334 termz[prefix_len++] = reg_id;
2335 termz[prefix_len] = 0;
2336 strcpy (scan_info->prefix, termz);
2338 trans_scan_term (zh, zapt, termz+prefix_len, reg_id);
2340 dict_scan (zh->dict, termz, &before_tmp, &after_tmp, scan_info,
2343 glist = (ZebraScanEntry *)
2344 odr_malloc (stream, (before+after)*sizeof(*glist));
2346 /* consider terms after main term */
2347 for (i = 0; i < ord_no; i++)
2351 for (i = 0; i<after; i++)
2354 const char *mterm = NULL;
2358 for (j = 0; j < ord_no; j++)
2360 if (ptr[j] < before+after &&
2361 (tst=scan_info_array[j].list[ptr[j]].term) &&
2362 (!mterm || strcmp (tst, mterm) < 0))
2370 scan_term_untrans (zh, stream->mem, reg_id,
2371 &glist[i+before].term, mterm);
2372 rset = rset_trunc (zh, &scan_info_array[j0].list[ptr[j0]].isam_p, 1,
2373 glist[i+before].term, strlen(glist[i+before].term),
2377 for (j = j0+1; j<ord_no; j++)
2379 if (ptr[j] < before+after &&
2380 (tst=scan_info_array[j].list[ptr[j]].term) &&
2381 !strcmp (tst, mterm))
2383 rset_bool_parms bool_parms;
2387 rset_trunc (zh, &scan_info_array[j].list[ptr[j]].isam_p, 1,
2388 glist[i+before].term,
2389 strlen(glist[i+before].term), NULL);
2391 bool_parms.key_size = sizeof(struct it_key);
2392 bool_parms.cmp = key_compare_it;
2393 bool_parms.rset_l = rset;
2394 bool_parms.rset_r = rset2;
2396 rset = rset_create (rset_kind_or, &bool_parms);
2401 count_set (rset, &glist[i+before].occurrences);
2406 *num_entries -= (after-i);
2410 /* consider terms before main term */
2411 for (i = 0; i<ord_no; i++)
2414 for (i = 0; i<before; i++)
2417 const char *mterm = NULL;
2421 for (j = 0; j <ord_no; j++)
2423 if (ptr[j] < before &&
2424 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2425 (!mterm || strcmp (tst, mterm) > 0))
2434 scan_term_untrans (zh, stream->mem, reg_id,
2435 &glist[before-1-i].term, mterm);
2438 (zh, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1,
2439 glist[before-1-i].term, strlen(glist[before-1-i].term),
2444 for (j = j0+1; j<ord_no; j++)
2446 if (ptr[j] < before &&
2447 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2448 !strcmp (tst, mterm))
2450 rset_bool_parms bool_parms;
2453 rset2 = rset_trunc (zh,
2454 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1,
2455 glist[before-1-i].term,
2456 strlen(glist[before-1-i].term), NULL);
2458 bool_parms.key_size = sizeof(struct it_key);
2459 bool_parms.cmp = key_compare_it;
2460 bool_parms.rset_l = rset;
2461 bool_parms.rset_r = rset2;
2463 rset = rset_create (rset_kind_or, &bool_parms);
2468 count_set (rset, &glist[before-1-i].occurrences);
2478 *list = glist + i; /* list is set to first 'real' entry */
2480 logf (LOG_DEBUG, "position = %d, num_entries = %d",
2481 *position, *num_entries);
2483 logf (LOG_DEBUG, "scan error: %d", zh->errCode);