2 * Copyright (C) 1995-1998, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.80 1998-06-23 15:33:34 adam
8 * Added feature to specify sort criteria in query (type 7 specifies
11 * Revision 1.79 1998/06/22 11:35:09 adam
14 * Revision 1.78 1998/06/08 14:43:17 adam
15 * Added suport for EXPLAIN Proxy servers - added settings databasePath
16 * and explainDatabase to facilitate this. Increased maximum number
17 * of databases and attributes in one register.
19 * Revision 1.77 1998/05/20 10:12:22 adam
20 * Implemented automatic EXPLAIN database maintenance.
21 * Modified Zebra to work with ASN.1 compiled version of YAZ.
23 * Revision 1.76 1998/04/02 14:35:29 adam
24 * First version of Zebra that works with compiled ASN.1.
26 * Revision 1.75 1998/03/05 08:45:13 adam
27 * New result set model and modular ranking system. Moved towards
28 * descent server API. System information stored as "SGML" records.
30 * Revision 1.74 1998/02/10 12:03:06 adam
33 * Revision 1.73 1998/01/29 13:40:11 adam
34 * Better logging for scan service.
36 * Revision 1.72 1998/01/07 13:53:41 adam
37 * Queries using simple ranked operands returns right number of hits.
39 * Revision 1.71 1997/12/18 10:54:24 adam
40 * New method result set method rs_hits that returns the number of
41 * hits in result-set (if known). The ranked result set returns real
42 * number of hits but only when not combined with other operands.
44 * Revision 1.70 1997/10/31 12:34:43 adam
45 * Changed a few log statements.
47 * Revision 1.69 1997/10/29 12:05:02 adam
48 * Server produces diagnostic "Unsupported Attribute Set" when appropriate.
50 * Revision 1.68 1997/10/27 14:33:06 adam
51 * Moved towards generic character mapping depending on "structure"
52 * field in abstract syntax file. Fixed a few memory leaks. Fixed
53 * bug with negative integers when doing searches with relational
56 * Revision 1.67 1997/09/29 09:06:10 adam
57 * Removed one static var in order to make this module thread safe.
59 * Revision 1.66 1997/09/25 14:58:03 adam
62 * Revision 1.65 1997/09/22 12:39:06 adam
63 * Added get_pos method for the ranked result sets.
65 * Revision 1.64 1997/09/18 08:59:20 adam
66 * Extra generic handle for the character mapping routines.
68 * Revision 1.63 1997/09/17 12:19:18 adam
69 * Zebra version corresponds to YAZ version 1.4.
70 * Changed Zebra server so that it doesn't depend on global common_resource.
72 * Revision 1.62 1997/09/05 15:30:09 adam
73 * Changed prototype for chr_map_input - added const.
74 * Added support for C++, headers uses extern "C" for public definitions.
76 * Revision 1.61 1997/02/10 10:21:14 adam
77 * Bug fix: in search terms character (^) wasn't observed.
79 * Revision 1.60 1997/01/31 11:10:34 adam
80 * Bug fix: Leading and trailing white space weren't removed in scan tokens.
82 * Revision 1.59 1997/01/17 11:31:46 adam
83 * Bug fix: complete phrase search didn't work.
85 * Revision 1.58 1996/12/23 15:30:45 adam
87 * Bug fix: result sets weren't deleted after server shut down.
89 * Revision 1.57 1996/11/11 13:38:02 adam
90 * Added proximity support in search.
92 * Revision 1.56 1996/11/08 11:10:32 adam
93 * Buffers used during file match got bigger.
94 * Compressed ISAM support everywhere.
95 * Bug fixes regarding masking characters in queries.
96 * Redesigned Regexp-2 queries.
98 * Revision 1.55 1996/11/04 14:07:44 adam
99 * Moved truncation code to trunc.c.
101 * Revision 1.54 1996/10/29 14:09:52 adam
102 * Use of cisam system - enabled if setting isamc is 1.
104 * Revision 1.53 1996/06/26 09:21:43 adam
105 * Bug fix: local attribute set wasn't obeyed in scan.
107 * Revision 1.52 1996/06/17 14:26:20 adam
108 * Function gen_regular_rel changed to handle negative numbers.
110 * Revision 1.51 1996/06/11 10:54:15 quinn
113 * Revision 1.50 1996/06/07 08:51:53 adam
114 * Bug fix: Character mapping was broken (introducued by last revision).
116 * Revision 1.49 1996/06/04 10:18:11 adam
117 * Search/scan uses character mapping module.
119 * Revision 1.48 1996/05/28 15:15:01 adam
120 * Bug fix: Didn't handle unknown database correctly.
122 * Revision 1.47 1996/05/15 18:36:28 adam
123 * Function trans_term transforms unsearchable characters to blanks.
125 * Revision 1.46 1996/05/15 11:57:56 adam
126 * Fixed bug introduced by set/field mapping in search operations.
128 * Revision 1.45 1996/05/14 11:34:00 adam
129 * Scan support in multiple registers/databases.
131 * Revision 1.44 1996/05/14 06:16:44 adam
132 * Compact use/set bytes used in search service.
134 * Revision 1.43 1996/05/09 09:54:43 adam
135 * Server supports maps from one logical attributes to a list of physical
137 * The extraction process doesn't make space consuming 'any' keys.
139 * Revision 1.42 1996/05/09 07:28:56 quinn
140 * Work towards phrases and multiple registers
142 * Revision 1.41 1996/03/20 09:36:43 adam
143 * Function dict_lookup_grep got extra parameter, init_pos, which marks
144 * from which position in pattern approximate pattern matching should occur.
145 * Approximate pattern matching is used in relevance=re-2.
147 * Revision 1.40 1996/02/02 13:44:44 adam
148 * The public dictionary functions simply use char instead of Dict_char
149 * to represent search strings. Dict_char is used internally only.
151 * Revision 1.39 1996/01/03 16:22:13 quinn
152 * operator->roperator
154 * Revision 1.38 1995/12/11 09:12:55 adam
155 * The rec_get function returns NULL if record doesn't exist - will
156 * happen in the server if the result set records have been deleted since
157 * the creation of the set (i.e. the search).
158 * The server saves a result temporarily if it is 'volatile', i.e. the
159 * set is register dependent.
161 * Revision 1.37 1995/12/06 15:05:28 adam
162 * More verbose in count_set.
164 * Revision 1.36 1995/12/06 12:41:27 adam
165 * New command 'stat' for the index program.
166 * Filenames can be read from stdin by specifying '-'.
167 * Bug fix/enhancement of the transformation from terms to regular
168 * expressons in the search engine.
170 * Revision 1.35 1995/11/27 09:29:00 adam
171 * Bug fixes regarding conversion to regular expressions.
173 * Revision 1.34 1995/11/16 17:00:56 adam
174 * Better logging of rpn query.
176 * Revision 1.33 1995/11/01 13:58:28 quinn
177 * Moving data1 to yaz/retrieval
179 * Revision 1.32 1995/10/27 14:00:11 adam
180 * Implemented detection of database availability.
182 * Revision 1.31 1995/10/17 18:02:10 adam
183 * New feature: databases. Implemented as prefix to words in dictionary.
185 * Revision 1.30 1995/10/16 09:32:38 adam
186 * More work on relational op.
188 * Revision 1.29 1995/10/13 16:01:49 adam
191 * Revision 1.28 1995/10/13 12:26:43 adam
192 * Optimization of truncation.
194 * Revision 1.27 1995/10/12 17:07:22 adam
197 * Revision 1.26 1995/10/12 12:40:54 adam
198 * Bug fixes in rpn_prox.
200 * Revision 1.25 1995/10/10 13:59:24 adam
201 * Function rset_open changed its wflag parameter to general flags.
203 * Revision 1.24 1995/10/09 16:18:37 adam
204 * Function dict_lookup_grep got extra client data parameter.
206 * Revision 1.23 1995/10/06 16:33:37 adam
207 * Use attribute mappings.
209 * Revision 1.22 1995/10/06 15:07:39 adam
210 * Structure 'local-number' handled.
212 * Revision 1.21 1995/10/06 13:52:06 adam
213 * Bug fixes. Handler may abort further scanning.
215 * Revision 1.20 1995/10/06 11:06:33 adam
216 * Scan entries include 'occurrences' now.
218 * Revision 1.19 1995/10/06 10:43:56 adam
219 * Scan added. 'occurrences' in scan entries not set yet.
221 * Revision 1.18 1995/10/04 16:57:20 adam
222 * Key input and merge sort in one pass.
224 * Revision 1.17 1995/10/04 12:55:17 adam
225 * Bug fix in ranked search. Use=Any keys inserted.
227 * Revision 1.16 1995/10/02 16:24:40 adam
228 * Use attribute actually used in search requests.
230 * Revision 1.15 1995/10/02 15:18:52 adam
231 * New member in recRetrieveCtrl: diagnostic.
233 * Revision 1.14 1995/09/28 12:10:32 adam
234 * Bug fixes. Field prefix used in queries.
236 * Revision 1.13 1995/09/18 14:17:50 adam
239 * Revision 1.12 1995/09/15 14:45:21 adam
241 * Work on truncation.
243 * Revision 1.11 1995/09/14 11:53:27 adam
244 * First work on regular expressions/truncations.
246 * Revision 1.10 1995/09/11 15:23:26 adam
247 * More work on relevance search.
249 * Revision 1.9 1995/09/11 13:09:35 adam
250 * More work on relevance feedback.
252 * Revision 1.8 1995/09/08 14:52:27 adam
253 * Minor changes. Dictionary is lower case now.
255 * Revision 1.7 1995/09/07 13:58:36 adam
256 * New parameter: result-set file descriptor (RSFD) to support multiple
257 * positions within the same result-set.
258 * Boolean operators: and, or, not implemented.
259 * Result-set references.
261 * Revision 1.6 1995/09/06 16:11:18 adam
262 * Option: only one word key per file.
264 * Revision 1.5 1995/09/06 10:33:04 adam
265 * More work on present. Some log messages removed.
267 * Revision 1.4 1995/09/05 15:28:40 adam
268 * More work on search engine.
270 * Revision 1.3 1995/09/04 15:20:22 adam
273 * Revision 1.2 1995/09/04 12:33:43 adam
274 * Various cleanup. YAZ util used instead.
276 * Revision 1.1 1995/09/04 09:10:40 adam
277 * More work on index add/del/update.
278 * Merge sort implemented.
279 * Initial work on z39 server.
298 struct rpn_char_map_info {
303 static const char **rpn_char_map_handler (void *vp, const char **from, int len)
305 struct rpn_char_map_info *p = vp;
306 return zebra_maps_input (p->zm, p->reg_type, from, len);
309 static void rpn_char_map_prepare (ZebraHandle zh, int reg_type,
310 struct rpn_char_map_info *map_info)
312 map_info->zm = zh->zebra_maps;
313 map_info->reg_type = reg_type;
314 dict_grep_cmap (zh->dict, map_info, rpn_char_map_handler);
321 Z_AttributesPlusTerm *zapt;
324 static int attr_find (AttrType *src, oid_value *attributeSetP)
329 num_attributes = src->zapt->attributes->num_attributes;
331 num_attributes = src->zapt->num_attributes;
333 while (src->major < num_attributes)
335 Z_AttributeElement *element;
338 element = src->zapt->attributes->attributes[src->major];
340 element = src->zapt->attributeList[src->major];
342 if (src->type == *element->attributeType)
344 switch (element->which)
346 case Z_AttributeValue_numeric:
348 if (element->attributeSet && attributeSetP)
352 attrset = oid_getentbyoid (element->attributeSet);
353 *attributeSetP = attrset->value;
355 return *element->value.numeric;
357 case Z_AttributeValue_complex:
358 if (src->minor >= element->value.complex->num_list ||
359 element->value.complex->list[src->minor]->which !=
360 Z_StringOrNumeric_numeric)
363 if (element->attributeSet && attributeSetP)
367 attrset = oid_getentbyoid (element->attributeSet);
368 *attributeSetP = attrset->value;
370 return *element->value.complex->list[src->minor-1]->u.numeric;
380 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
402 static void term_untrans (ZebraHandle zh, int reg_type,
403 char *dst, const char *src)
407 const char *cp = zebra_maps_output (zh->zebra_maps, reg_type, &src);
414 static void add_isam_p (const char *name, const char *info,
417 if (p->isam_p_indx == p->isam_p_size)
419 ISAM_P *new_isam_p_buf;
423 p->isam_p_size = 2*p->isam_p_size + 100;
424 new_isam_p_buf = xmalloc (sizeof(*new_isam_p_buf) *
428 memcpy (new_isam_p_buf, p->isam_p_buf,
429 p->isam_p_indx * sizeof(*p->isam_p_buf));
430 xfree (p->isam_p_buf);
432 p->isam_p_buf = new_isam_p_buf;
435 new_term_no = xmalloc (sizeof(*new_term_no) *
439 memcpy (new_term_no, p->isam_p_buf,
440 p->isam_p_indx * sizeof(*p->term_no));
443 p->term_no = new_term_no;
446 assert (*info == sizeof(*p->isam_p_buf));
447 memcpy (p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
450 term_untrans (p->zh, p->reg_type, term_tmp, name+2);
451 logf (LOG_DEBUG, "grep: %s", term_tmp);
456 static int grep_handle (char *name, const char *info, void *p)
458 add_isam_p (name, info, p);
462 static int term_pre (ZebraMaps zebra_maps, int reg_type, const char **src,
463 const char *ct1, const char *ct2)
465 const char *s1, *s0 = *src;
468 /* skip white space */
471 if (ct1 && strchr (ct1, *s0))
473 if (ct2 && strchr (ct2, *s0))
476 map = zebra_maps_input (zebra_maps, reg_type, &s1, strlen(s1));
477 if (**map != *CHR_SPACE)
485 static int term_100 (ZebraMaps zebra_maps, int reg_type,
486 const char **src, char *dst, int space_split,
494 if (!term_pre (zebra_maps, reg_type, src, NULL, NULL))
500 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
501 if (space_split && **map == *CHR_SPACE)
505 if (!isalnum (*s1) && *s1 != '-')
517 static int term_101 (ZebraMaps zebra_maps, int reg_type,
518 const char **src, char *dst, int space_split,
526 if (!term_pre (zebra_maps, reg_type, src, "#", "#"))
535 dst_term[j++] = *s0++;
540 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
541 if (space_split && **map == *CHR_SPACE)
553 dst_term[j++] = '\0';
559 static int term_103 (ZebraMaps zebra_maps, int reg_type, const char **src,
560 char *dst, int *errors, int space_split,
568 if (!term_pre (zebra_maps, reg_type, src, "^\\()[].*+?|", "("))
571 if (errors && *s0 == '+' && s0[1] && s0[2] == '+' && s0[3] &&
574 *errors = s0[1] - '0';
581 if (strchr ("^\\()[].*+?|-", *s0))
589 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
590 if (**map == *CHR_SPACE)
607 static int term_102 (ZebraMaps zebra_maps, int reg_type, const char **src,
608 char *dst, int space_split, char *dst_term)
610 return term_103 (zebra_maps, reg_type, src, dst, NULL, space_split,
614 /* gen_regular_rel - generate regular expression from relation
615 * val: border value (inclusive)
616 * islt: 1 if <=; 0 if >=.
618 static void gen_regular_rel (char *dst, int val, int islt)
625 logf (LOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
629 strcpy (dst, "(-[0-9]+|(");
637 strcpy (dst, "([0-9]+|-(");
638 dst_p = strlen (dst);
648 dst_p = strlen (dst);
649 sprintf (numstr, "%d", val);
650 for (w = strlen(numstr); --w >= 0; pos++)
669 strcpy (dst + dst_p, numstr);
670 dst_p = strlen(dst) - pos - 1;
698 for (i = 0; i<pos; i++)
711 for (i=1; i<pos; i++)
712 strcat (dst, "[0-9]?");
716 for (i = 0; i <= pos; i++)
717 strcat (dst, "[0-9]");
718 strcat (dst, "[0-9]*");
723 static int string_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
724 const char **term_sub,
726 oid_value attributeSet,
727 struct grep_info *grep_info,
736 char *term_tmp = term_dict + strlen(term_dict);
738 attr_init (&relation, zapt, 2);
739 relation_value = attr_find (&relation, NULL);
741 logf (LOG_DEBUG, "string relation value=%d", relation_value);
742 switch (relation_value)
745 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_tmp, 1,
748 term_value = atoi (term_tmp);
749 logf (LOG_DEBUG, "Relation <");
750 gen_regular_rel (term_tmp, term_value-1, 1);
753 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_tmp, 1,
756 term_value = atoi (term_tmp);
757 logf (LOG_DEBUG, "Relation <=");
758 gen_regular_rel (term_tmp, term_value, 1);
761 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_tmp, 1,
764 term_value = atoi (term_tmp);
765 logf (LOG_DEBUG, "Relation >=");
766 gen_regular_rel (term_tmp, term_value, 0);
769 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_tmp, 1,
772 term_value = atoi (term_tmp);
773 logf (LOG_DEBUG, "Relation >");
774 gen_regular_rel (term_tmp, term_value+1, 0);
778 logf (LOG_DEBUG, "Relation =");
780 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_tmp+1, 1,
783 strcat (term_tmp, ")");
785 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_tmp);
786 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info, max_pos,
789 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
790 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
794 static int string_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
795 const char **term_sub,
796 oid_value attributeSet, struct grep_info *grep_info,
797 int reg_type, int complete_flag,
798 int num_bases, char **basenames,
801 char term_dict[2*IT_MAX_WORD+2];
804 int truncation_value;
807 oid_value curAttributeSet = attributeSet;
809 struct rpn_char_map_info rcmi;
810 int space_split = complete_flag ? 0 : 1;
812 rpn_char_map_prepare (zh, reg_type, &rcmi);
813 attr_init (&use, zapt, 1);
814 use_value = attr_find (&use, &curAttributeSet);
815 logf (LOG_DEBUG, "string_term, use value %d", use_value);
816 attr_init (&truncation, zapt, 5);
817 truncation_value = attr_find (&truncation, NULL);
818 logf (LOG_DEBUG, "truncation value %d", truncation_value);
823 for (base_no = 0; base_no < num_bases; base_no++)
826 data1_local_attribute *local_attr;
827 int max_pos, prefix_len = 0;
830 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
832 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
833 curAttributeSet, use_value, r);
840 if (zebraExplain_curDatabase (zh->zei, basenames[base_no]))
842 zh->errCode = 109; /* Database unavailable */
843 zh->errString = basenames[base_no];
846 for (local_attr = attp.local_attributes; local_attr;
847 local_attr = local_attr->next)
853 ord = zebraExplain_lookupSU (zh->zei, attp.attset_ordinal,
858 term_dict[prefix_len++] = '|';
860 term_dict[prefix_len++] = '(';
862 ord_len = key_SU_code (ord, ord_buf);
863 for (i = 0; i<ord_len; i++)
865 term_dict[prefix_len++] = 1;
866 term_dict[prefix_len++] = ord_buf[i];
874 term_dict[prefix_len++] = ')';
875 term_dict[prefix_len++] = 1;
876 term_dict[prefix_len++] = reg_type;
877 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
878 term_dict[prefix_len] = '\0';
880 switch (truncation_value)
882 case -1: /* not specified */
883 case 100: /* do not truncate */
884 if (!string_relation (zh, zapt, &termp, term_dict,
885 attributeSet, grep_info, &max_pos,
889 term_dict[j++] = '(';
890 if (!term_100 (zh->zebra_maps, reg_type,
891 &termp, term_dict + j, space_split, term_dst))
893 strcat (term_dict, ")");
894 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
895 &max_pos, 0, grep_handle);
897 logf (LOG_WARN, "dict_lookup_grep err, trunc=none:%d", r);
900 case 1: /* right truncation */
901 term_dict[j++] = '(';
902 if (!term_100 (zh->zebra_maps, reg_type,
903 &termp, term_dict + j, space_split, term_dst))
905 strcat (term_dict, ".*)");
906 dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
907 &max_pos, 0, grep_handle);
909 case 2: /* left truncation */
910 case 3: /* left&right truncation */
913 case 101: /* process # in term */
914 term_dict[j++] = '(';
915 if (!term_101 (zh->zebra_maps, reg_type,
916 &termp, term_dict + j, space_split, term_dst))
918 strcat (term_dict, ")");
919 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
920 &max_pos, 0, grep_handle);
922 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d", r);
924 case 102: /* Regexp-1 */
925 term_dict[j++] = '(';
926 if (!term_102 (zh->zebra_maps, reg_type,
927 &termp, term_dict + j, space_split, term_dst))
929 strcat (term_dict, ")");
930 logf (LOG_DEBUG, "Regexp-1 tolerance=%d", r);
931 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
932 &max_pos, 0, grep_handle);
934 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
937 case 103: /* Regexp-2 */
939 term_dict[j++] = '(';
940 if (!term_103 (zh->zebra_maps, reg_type,
941 &termp, term_dict + j, &r, space_split, term_dst))
943 strcat (term_dict, ")");
944 logf (LOG_DEBUG, "Regexp-2 tolerance=%d", r);
945 r = dict_lookup_grep (zh->dict, term_dict, r, grep_info,
946 &max_pos, 2, grep_handle);
948 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
954 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
958 static void trans_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
962 Z_Term *term = zapt->term;
964 sizez = term->u.general->len;
965 if (sizez > IT_MAX_WORD-1)
966 sizez = IT_MAX_WORD-1;
967 memcpy (termz, term->u.general->buf, sizez);
971 static void trans_scan_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
972 char *termz, int reg_type)
974 Z_Term *term = zapt->term;
976 const char *cp = (const char *) term->u.general->buf;
977 const char *cp_end = cp + term->u.general->len;
980 const char *space_map = NULL;
983 while ((len = (cp_end - cp)) > 0)
985 map = zebra_maps_input (zh->zebra_maps, reg_type, &cp, len);
986 if (**map == *CHR_SPACE)
991 for (src = space_map; *src; src++)
994 for (src = *map; *src; src++)
1001 static RSET rpn_proximity (ZebraHandle zh, RSET rset1, RSET rset2,
1003 int exclusion, int relation, int distance)
1008 struct it_key buf1, buf2;
1011 rset_temp_parms parms;
1014 rsfd1 = rset_open (rset1, RSETF_READ);
1015 more1 = rset_read (rset1, rsfd1, &buf1, &term_index);
1017 rsfd2 = rset_open (rset2, RSETF_READ);
1018 more2 = rset_read (rset2, rsfd2, &buf2, &term_index);
1020 parms.key_size = sizeof (struct it_key);
1021 parms.temp_path = res_get (zh->res, "setTmpDir");
1022 result = rset_create (rset_kind_temp, &parms);
1023 rsfd_result = rset_open (result, RSETF_WRITE);
1025 logf (LOG_DEBUG, "rpn_proximity excl=%d ord=%d rel=%d dis=%d",
1026 exclusion, ordered, relation, distance);
1027 while (more1 && more2)
1029 int cmp = key_compare_it (&buf1, &buf2);
1031 more1 = rset_read (rset1, rsfd1, &buf1, &term_index);
1033 more2 = rset_read (rset2, rsfd2, &buf2, &term_index);
1036 int sysno = buf1.sysno;
1040 seqno[n++] = buf1.seqno;
1041 while ((more1 = rset_read (rset1, rsfd1, &buf1, &term_index)) &&
1042 sysno == buf1.sysno)
1044 seqno[n++] = buf1.seqno;
1047 for (i = 0; i<n; i++)
1049 int diff = buf2.seqno - seqno[i];
1050 int excl = exclusion;
1051 if (!ordered && diff < 0)
1056 if (diff < distance)
1060 if (diff <= distance)
1064 if (diff == distance)
1068 if (diff >= distance)
1072 if (diff > distance)
1076 if (diff != distance)
1081 rset_write (result, rsfd_result, &buf2);
1083 } while ((more2 = rset_read (rset2, rsfd2, &buf2, &term_index)) &&
1084 sysno == buf2.sysno);
1087 rset_close (result, rsfd_result);
1088 rset_close (rset1, rsfd1);
1089 rset_close (rset2, rsfd2);
1093 static RSET rpn_prox (ZebraHandle zh, RSET *rset, int rset_no)
1098 struct it_key **buf;
1100 char prox_term[1024];
1101 int length_prox_term = 0;
1102 int min_nn = 10000000;
1104 const char *flags = NULL;
1106 rsfd = xmalloc (sizeof(*rsfd)*rset_no);
1107 more = xmalloc (sizeof(*more)*rset_no);
1108 buf = xmalloc (sizeof(*buf)*rset_no);
1110 for (i = 0; i<rset_no; i++)
1113 buf[i] = xmalloc (sizeof(**buf));
1114 rsfd[i] = rset_open (rset[i], RSETF_READ);
1115 if (!(more[i] = rset_read (rset[i], rsfd[i], buf[i], &term_index)))
1117 for (j = 0; j<rset[i]->no_rset_terms; j++)
1119 const char *nflags = rset[i]->rset_terms[j]->flags;
1120 char *term = rset[i]->rset_terms[j]->name;
1121 int lterm = strlen(term);
1122 if (length_prox_term)
1123 prox_term[length_prox_term++] = ' ';
1124 strcpy (prox_term + length_prox_term, term);
1125 length_prox_term += lterm;
1126 if (min_nn > rset[i]->rset_terms[j]->nn)
1127 min_nn = rset[i]->rset_terms[j]->nn;
1133 rset_null_parms parms;
1137 rset_close (rset[i], rsfd[i]);
1141 parms.rset_term = rset_term_create (prox_term, -1, flags);
1142 parms.rset_term->nn = 0;
1143 result = rset_create (rset_kind_null, &parms);
1147 rset_temp_parms parms;
1150 parms.rset_term = rset_term_create (prox_term, -1, flags);
1151 parms.rset_term->nn = min_nn;
1152 parms.key_size = sizeof (struct it_key);
1153 parms.temp_path = res_get (zh->res, "setTmpDir");
1154 result = rset_create (rset_kind_temp, &parms);
1155 rsfd_result = rset_open (result, RSETF_WRITE);
1159 for (i = 1; i<rset_no; i++)
1168 cmp = key_compare_it (buf[i], buf[i-1]);
1171 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1172 buf[i-1], &term_index);
1177 if (buf[i-1]->seqno+1 != buf[i]->seqno)
1179 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1180 buf[i-1], &term_index);
1186 more[i] = rset_read (rset[i], rsfd[i], buf[i],
1193 rset_write (result, rsfd_result, buf[0]);
1194 more[0] = rset_read (*rset, *rsfd, *buf, &term_index);
1198 for (i = 0; i<rset_no; i++)
1200 rset_close (rset[i], rsfd[i]);
1203 rset_close (result, rsfd_result);
1211 static RSET rpn_search_APT_phrase (ZebraHandle zh,
1212 Z_AttributesPlusTerm *zapt,
1214 oid_value attributeSet,
1215 int reg_type, int complete_flag,
1216 const char *rank_type,
1217 int num_bases, char **basenames)
1219 char term_dst[IT_MAX_WORD+1];
1220 const char *termp = termz;
1221 RSET rset[60], result;
1222 int i, r, rset_no = 0;
1223 struct grep_info grep_info;
1226 grep_info.term_no = 0;
1228 grep_info.isam_p_size = 0;
1229 grep_info.isam_p_buf = NULL;
1231 grep_info.reg_type = reg_type;
1235 logf (LOG_DEBUG, "APT_phrase termp=%s", termp);
1236 grep_info.isam_p_indx = 0;
1237 r = string_term (zh, zapt, &termp, attributeSet, &grep_info,
1238 reg_type, complete_flag, num_bases, basenames,
1242 logf (LOG_DEBUG, "term: %s", term_dst);
1243 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1244 grep_info.isam_p_indx, term_dst,
1245 strlen(term_dst), rank_type);
1246 assert (rset[rset_no]);
1247 if (++rset_no >= sizeof(rset)/sizeof(*rset))
1251 xfree(grep_info.term_no);
1253 xfree (grep_info.isam_p_buf);
1256 rset_null_parms parms;
1258 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1259 return rset_create (rset_kind_null, &parms);
1261 else if (rset_no == 1)
1263 result = rpn_prox (zh, rset, rset_no);
1264 for (i = 0; i<rset_no; i++)
1265 rset_delete (rset[i]);
1269 static RSET rpn_search_APT_or_list (ZebraHandle zh,
1270 Z_AttributesPlusTerm *zapt,
1272 oid_value attributeSet,
1273 int reg_type, int complete_flag,
1274 const char *rank_type,
1275 int num_bases, char **basenames)
1277 char term_dst[IT_MAX_WORD+1];
1278 const char *termp = termz;
1279 RSET rset[60], result;
1280 int i, r, rset_no = 0;
1281 struct grep_info grep_info;
1284 grep_info.term_no = 0;
1286 grep_info.isam_p_size = 0;
1287 grep_info.isam_p_buf = NULL;
1289 grep_info.reg_type = reg_type;
1293 logf (LOG_DEBUG, "APT_or_list termp=%s", termp);
1294 grep_info.isam_p_indx = 0;
1295 r = string_term (zh, zapt, &termp, attributeSet, &grep_info,
1296 reg_type, complete_flag, num_bases, basenames,
1300 logf (LOG_DEBUG, "term: %s", term_dst);
1301 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1302 grep_info.isam_p_indx, term_dst,
1303 strlen(term_dst), rank_type);
1304 assert (rset[rset_no]);
1305 if (++rset_no >= sizeof(rset)/sizeof(*rset))
1309 xfree(grep_info.term_no);
1311 xfree (grep_info.isam_p_buf);
1314 rset_null_parms parms;
1316 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1317 return rset_create (rset_kind_null, &parms);
1320 for (i = 1; i<rset_no; i++)
1322 rset_bool_parms bool_parms;
1324 bool_parms.rset_l = result;
1325 bool_parms.rset_r = rset[i];
1326 bool_parms.key_size = sizeof(struct it_key);
1327 bool_parms.cmp = key_compare_it;
1328 result = rset_create (rset_kind_or, &bool_parms);
1333 static RSET rpn_search_APT_and_list (ZebraHandle zh,
1334 Z_AttributesPlusTerm *zapt,
1336 oid_value attributeSet,
1337 int reg_type, int complete_flag,
1338 const char *rank_type,
1339 int num_bases, char **basenames)
1341 char term_dst[IT_MAX_WORD+1];
1342 const char *termp = termz;
1343 RSET rset[60], result;
1344 int i, r, rset_no = 0;
1345 struct grep_info grep_info;
1348 grep_info.term_no = 0;
1350 grep_info.isam_p_size = 0;
1351 grep_info.isam_p_buf = NULL;
1353 grep_info.reg_type = reg_type;
1357 logf (LOG_DEBUG, "APT_and_list termp=%s", termp);
1358 grep_info.isam_p_indx = 0;
1359 r = string_term (zh, zapt, &termp, attributeSet, &grep_info,
1360 reg_type, complete_flag, num_bases, basenames,
1364 logf (LOG_DEBUG, "term: %s", term_dst);
1365 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1366 grep_info.isam_p_indx, term_dst,
1367 strlen(term_dst), rank_type);
1368 assert (rset[rset_no]);
1369 if (++rset_no >= sizeof(rset)/sizeof(*rset))
1373 xfree(grep_info.term_no);
1375 xfree (grep_info.isam_p_buf);
1378 rset_null_parms parms;
1380 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1381 return rset_create (rset_kind_null, &parms);
1384 for (i = 1; i<rset_no; i++)
1386 rset_bool_parms bool_parms;
1388 bool_parms.rset_l = result;
1389 bool_parms.rset_r = rset[i];
1390 bool_parms.key_size = sizeof(struct it_key);
1391 bool_parms.cmp = key_compare_it;
1392 result = rset_create (rset_kind_and, &bool_parms);
1397 static int numeric_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1398 const char **term_sub,
1400 oid_value attributeSet,
1401 struct grep_info *grep_info,
1410 char *term_tmp = term_dict + strlen(term_dict);
1412 attr_init (&relation, zapt, 2);
1413 relation_value = attr_find (&relation, NULL);
1415 logf (LOG_DEBUG, "numeric relation value=%d", relation_value);
1417 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_tmp, 1,
1420 term_value = atoi (term_tmp);
1421 switch (relation_value)
1424 logf (LOG_DEBUG, "Relation <");
1425 gen_regular_rel (term_tmp, term_value-1, 1);
1428 logf (LOG_DEBUG, "Relation <=");
1429 gen_regular_rel (term_tmp, term_value, 1);
1432 logf (LOG_DEBUG, "Relation >=");
1433 gen_regular_rel (term_tmp, term_value, 0);
1436 logf (LOG_DEBUG, "Relation >");
1437 gen_regular_rel (term_tmp, term_value+1, 0);
1441 logf (LOG_DEBUG, "Relation =");
1442 sprintf (term_tmp, "(0*%d)", term_value);
1444 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_tmp);
1445 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info, max_pos,
1448 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1449 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1453 static int numeric_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1454 const char **term_sub,
1455 oid_value attributeSet, struct grep_info *grep_info,
1456 int reg_type, int complete_flag,
1457 int num_bases, char **basenames,
1460 char term_dict[2*IT_MAX_WORD+2];
1464 oid_value curAttributeSet = attributeSet;
1466 struct rpn_char_map_info rcmi;
1468 rpn_char_map_prepare (zh, reg_type, &rcmi);
1469 attr_init (&use, zapt, 1);
1470 use_value = attr_find (&use, &curAttributeSet);
1471 logf (LOG_DEBUG, "numeric_term, use value %d", use_value);
1473 if (use_value == -1)
1476 for (base_no = 0; base_no < num_bases; base_no++)
1479 data1_local_attribute *local_attr;
1480 int max_pos, prefix_len = 0;
1483 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
1485 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1486 curAttributeSet, use_value, r);
1493 if (zebraExplain_curDatabase (zh->zei, basenames[base_no]))
1495 zh->errCode = 109; /* Database unavailable */
1496 zh->errString = basenames[base_no];
1499 for (local_attr = attp.local_attributes; local_attr;
1500 local_attr = local_attr->next)
1506 ord = zebraExplain_lookupSU (zh->zei, attp.attset_ordinal,
1511 term_dict[prefix_len++] = '|';
1513 term_dict[prefix_len++] = '(';
1515 ord_len = key_SU_code (ord, ord_buf);
1516 for (i = 0; i<ord_len; i++)
1518 term_dict[prefix_len++] = 1;
1519 term_dict[prefix_len++] = ord_buf[i];
1527 term_dict[prefix_len++] = ')';
1528 term_dict[prefix_len++] = 1;
1529 term_dict[prefix_len++] = reg_type;
1530 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1531 term_dict[prefix_len] = '\0';
1532 if (!numeric_relation (zh, zapt, &termp, term_dict,
1533 attributeSet, grep_info, &max_pos, reg_type,
1538 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1542 static RSET rpn_search_APT_numeric (ZebraHandle zh,
1543 Z_AttributesPlusTerm *zapt,
1545 oid_value attributeSet,
1546 int reg_type, int complete_flag,
1547 const char *rank_type,
1548 int num_bases, char **basenames)
1550 char term_dst[IT_MAX_WORD+1];
1551 const char *termp = termz;
1552 RSET rset[60], result;
1553 int i, r, rset_no = 0;
1554 struct grep_info grep_info;
1557 grep_info.term_no = 0;
1559 grep_info.isam_p_size = 0;
1560 grep_info.isam_p_buf = NULL;
1562 grep_info.reg_type = reg_type;
1566 logf (LOG_DEBUG, "APT_numeric termp=%s", termp);
1567 grep_info.isam_p_indx = 0;
1568 r = numeric_term (zh, zapt, &termp, attributeSet, &grep_info,
1569 reg_type, complete_flag, num_bases, basenames,
1573 logf (LOG_DEBUG, "term: %s", term_dst);
1574 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1575 grep_info.isam_p_indx, term_dst,
1576 strlen(term_dst), rank_type);
1577 assert (rset[rset_no]);
1578 if (++rset_no >= sizeof(rset)/sizeof(*rset))
1582 xfree(grep_info.term_no);
1584 xfree (grep_info.isam_p_buf);
1587 rset_null_parms parms;
1589 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1590 return rset_create (rset_kind_null, &parms);
1593 for (i = 1; i<rset_no; i++)
1595 rset_bool_parms bool_parms;
1597 bool_parms.rset_l = result;
1598 bool_parms.rset_r = rset[i];
1599 bool_parms.key_size = sizeof(struct it_key);
1600 bool_parms.cmp = key_compare_it;
1601 result = rset_create (rset_kind_and, &bool_parms);
1606 static RSET rpn_search_APT_local (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1608 oid_value attributeSet,
1609 const char *rank_type)
1614 rset_temp_parms parms;
1616 parms.rset_term = rset_term_create (termz, -1, rank_type);
1617 parms.key_size = sizeof (struct it_key);
1618 parms.temp_path = res_get (zh->res, "setTmpDir");
1619 result = rset_create (rset_kind_temp, &parms);
1620 rsfd = rset_open (result, RSETF_WRITE);
1622 key.sysno = atoi (termz);
1626 rset_write (result, rsfd, &key);
1627 rset_close (result, rsfd);
1631 static RSET rpn_sort_spec (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1632 oid_value attributeSet, ODR stream,
1633 Z_SortKeySpecList *sort_sequence,
1634 const char *rank_type)
1636 rset_null_parms parms;
1638 int sort_relation_value;
1639 AttrType sort_relation_type;
1644 Z_AttributeElement *ae;
1648 attr_init (&sort_relation_type, zapt, 7);
1649 sort_relation_value = attr_find (&sort_relation_type, &attributeSet);
1651 attr_init (&use_type, zapt, 1);
1652 use_value = attr_find (&use_type, &attributeSet);
1654 if (!sort_sequence->specs)
1656 sort_sequence->num_specs = 10;
1657 sort_sequence->specs = odr_malloc (stream, sort_sequence->num_specs *
1658 sizeof(*sort_sequence->specs));
1659 for (i = 0; i<sort_sequence->num_specs; i++)
1660 sort_sequence->specs[i] = 0;
1662 if (zapt->term->which != Z_Term_general)
1665 i = atoi_n (zapt->term->u.general->buf, zapt->term->u.general->len);
1666 if (i >= sort_sequence->num_specs)
1669 oe.proto = PROTO_Z3950;
1670 oe.oclass = CLASS_ATTSET;
1671 oe.value = attributeSet;
1672 if (!oid_ent_to_oid (&oe, oid))
1675 sks = odr_malloc (stream, sizeof(*sks));
1676 sks->sortElement = odr_malloc (stream, sizeof(*sks->sortElement));
1677 sks->sortElement->which = Z_SortElement_generic;
1678 sk = sks->sortElement->u.generic = odr_malloc (stream, sizeof(*sk));
1679 sk->which = Z_SortKey_sortAttributes;
1680 sk->u.sortAttributes = odr_malloc (stream, sizeof(*sk->u.sortAttributes));
1682 sk->u.sortAttributes->id = oid;
1683 sk->u.sortAttributes->list =
1684 odr_malloc (stream, sizeof(*sk->u.sortAttributes->list));
1685 sk->u.sortAttributes->list->num_attributes = 1;
1686 sk->u.sortAttributes->list->attributes =
1687 odr_malloc (stream, sizeof(*sk->u.sortAttributes->list->attributes));
1688 ae = *sk->u.sortAttributes->list->attributes =
1689 odr_malloc (stream, sizeof(**sk->u.sortAttributes->list->attributes));
1690 ae->attributeSet = 0;
1691 ae->attributeType = odr_malloc (stream, sizeof(*ae->attributeType));
1692 *ae->attributeType = 1;
1693 ae->which = Z_AttributeValue_numeric;
1694 ae->value.numeric = odr_malloc (stream, sizeof(*ae->value.numeric));
1695 *ae->value.numeric = use_value;
1697 sks->sortRelation = odr_malloc (stream, sizeof(*sks->sortRelation));
1698 if (sort_relation_value == 1)
1699 *sks->sortRelation = Z_SortRelation_ascending;
1700 else if (sort_relation_value == 2)
1701 *sks->sortRelation = Z_SortRelation_descending;
1703 *sks->sortRelation = Z_SortRelation_ascending;
1705 sks->caseSensitivity = odr_malloc (stream, sizeof(*sks->caseSensitivity));
1706 *sks->caseSensitivity = 0;
1708 sks->missingValueAction = 0;
1710 sort_sequence->specs[i] = sks;
1712 parms.rset_term = rset_term_create ("", -1, rank_type);
1713 return rset_create (rset_kind_null, &parms);
1717 static RSET rpn_search_APT (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1718 oid_value attributeSet, ODR stream,
1719 Z_SortKeySpecList *sort_sequence,
1720 int num_bases, char **basenames)
1723 char *search_type = NULL;
1724 char *rank_type = NULL;
1727 char termz[IT_MAX_WORD+1];
1729 zebra_maps_attr (zh->zebra_maps, zapt, ®_id, &search_type,
1730 &rank_type, &complete_flag, &sort_flag);
1732 logf (LOG_DEBUG, "reg_id=%c", reg_id);
1733 logf (LOG_DEBUG, "complete_flag=%d", complete_flag);
1734 logf (LOG_DEBUG, "search_type=%s", search_type);
1735 logf (LOG_DEBUG, "rank_type=%s", rank_type);
1737 if (zapt->term->which != Z_Term_general)
1742 trans_term (zh, zapt, termz);
1745 return rpn_sort_spec (zh, zapt, attributeSet, stream, sort_sequence,
1748 if (!strcmp (search_type, "phrase"))
1750 return rpn_search_APT_phrase (zh, zapt, termz, attributeSet,
1751 reg_id, complete_flag, rank_type,
1752 num_bases, basenames);
1754 else if (!strcmp (search_type, "and-list"))
1756 return rpn_search_APT_and_list (zh, zapt, termz, attributeSet,
1757 reg_id, complete_flag, rank_type,
1758 num_bases, basenames);
1760 else if (!strcmp (search_type, "or-list"))
1762 return rpn_search_APT_or_list (zh, zapt, termz, attributeSet,
1763 reg_id, complete_flag, rank_type,
1764 num_bases, basenames);
1766 else if (!strcmp (search_type, "local"))
1768 return rpn_search_APT_local (zh, zapt, termz, attributeSet,
1771 else if (!strcmp (search_type, "numeric"))
1773 return rpn_search_APT_numeric (zh, zapt, termz, attributeSet,
1774 reg_id, complete_flag, rank_type,
1775 num_bases, basenames);
1781 static RSET rpn_search_structure (ZebraHandle zh, Z_RPNStructure *zs,
1782 oid_value attributeSet, ODR stream,
1783 Z_SortKeySpecList *sort_sequence,
1784 int num_bases, char **basenames)
1787 if (zs->which == Z_RPNStructure_complex)
1789 Z_Operator *zop = zs->u.complex->roperator;
1790 rset_bool_parms bool_parms;
1792 bool_parms.rset_l = rpn_search_structure (zh, zs->u.complex->s1,
1793 attributeSet, stream,
1795 num_bases, basenames);
1796 if (bool_parms.rset_l == NULL)
1798 bool_parms.rset_r = rpn_search_structure (zh, zs->u.complex->s2,
1799 attributeSet, stream,
1801 num_bases, basenames);
1802 if (bool_parms.rset_r == NULL)
1804 rset_delete (bool_parms.rset_l);
1807 bool_parms.key_size = sizeof(struct it_key);
1808 bool_parms.cmp = key_compare_it;
1812 case Z_Operator_and:
1813 r = rset_create (rset_kind_and, &bool_parms);
1816 r = rset_create (rset_kind_or, &bool_parms);
1818 case Z_Operator_and_not:
1819 r = rset_create (rset_kind_not, &bool_parms);
1821 case Z_Operator_prox:
1823 if (zop->u.prox->which != Z_ProximityOperator_known)
1829 if (zop->u.prox->which != Z_ProxCode_known)
1837 if (*zop->u.prox->u.known != Z_ProxUnit_word)
1839 char *val = odr_malloc (stream, 16);
1841 zh->errString = val;
1842 sprintf (val, "%d", *zop->u.prox->u.known);
1846 if (*zop->u.prox->proximityUnitCode != Z_ProxUnit_word)
1848 char *val = odr_malloc (stream, 16);
1850 zh->errString = val;
1851 sprintf (val, "%d", *zop->u.prox->proximityUnitCode);
1855 r = rpn_proximity (zh, bool_parms.rset_l, bool_parms.rset_r,
1856 *zop->u.prox->ordered,
1857 (!zop->u.prox->exclusion ? 0 :
1858 *zop->u.prox->exclusion),
1859 *zop->u.prox->relationType,
1860 *zop->u.prox->distance);
1867 else if (zs->which == Z_RPNStructure_simple)
1869 if (zs->u.simple->which == Z_Operand_APT)
1871 logf (LOG_DEBUG, "rpn_search_APT");
1872 r = rpn_search_APT (zh, zs->u.simple->u.attributesPlusTerm,
1873 attributeSet, stream, sort_sequence,
1874 num_bases, basenames);
1876 else if (zs->u.simple->which == Z_Operand_resultSetId)
1878 logf (LOG_DEBUG, "rpn_search_ref");
1879 r = resultSetRef (zh, zs->u.simple->u.resultSetId);
1881 r = rset_create (rset_kind_null, NULL);
1897 void rpn_search (ZebraHandle zh, ODR stream,
1898 Z_RPNQuery *rpn, int num_bases, char **basenames,
1899 const char *setname)
1903 oid_value attributeSet;
1904 Z_SortKeySpecList *sort_sequence;
1910 zh->errString = NULL;
1913 sort_sequence = odr_malloc (stream, sizeof(*sort_sequence));
1914 sort_sequence->num_specs = 10;
1915 sort_sequence->specs = odr_malloc (stream, sort_sequence->num_specs *
1916 sizeof(*sort_sequence->specs));
1917 for (i = 0; i<sort_sequence->num_specs; i++)
1918 sort_sequence->specs[i] = 0;
1920 attrset = oid_getentbyoid (rpn->attributeSetId);
1921 attributeSet = attrset->value;
1922 rset = rpn_search_structure (zh, rpn->RPNStructure, attributeSet, stream,
1924 num_bases, basenames);
1928 resultSetAdd (zh, setname, 1, rset, &zh->hits);
1930 logf (LOG_DEBUG, "search error: %d", zh->errCode);
1932 for (i = 0; sort_sequence->specs[i]; i++)
1934 sort_sequence->num_specs = i;
1936 resultSetSort (zh, stream, 1, &setname, setname, sort_sequence,
1940 struct scan_info_entry {
1946 struct scan_info_entry *list;
1952 static int scan_handle (char *name, const char *info, int pos, void *client)
1954 int len_prefix, idx;
1955 struct scan_info *scan_info = client;
1957 len_prefix = strlen(scan_info->prefix);
1958 if (memcmp (name, scan_info->prefix, len_prefix))
1961 idx = scan_info->after - pos + scan_info->before;
1964 scan_info->list[idx].term = odr_malloc (scan_info->odr,
1965 strlen(name + len_prefix)+1);
1966 strcpy (scan_info->list[idx].term, name + len_prefix);
1967 assert (*info == sizeof(ISAM_P));
1968 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAM_P));
1972 static void scan_term_untrans (ZebraHandle zh, ODR stream, int reg_type,
1973 char **dst, const char *src)
1975 char term_dst[1024];
1977 term_untrans (zh, reg_type, term_dst, src);
1979 *dst = odr_malloc (stream, strlen(term_dst)+1);
1980 strcpy (*dst, term_dst);
1983 static void count_set (RSET r, int *count)
1991 logf (LOG_DEBUG, "count_set");
1994 rfd = rset_open (r, RSETF_READ);
1995 while (rset_read (r, rfd, &key, &term_index))
1997 if (key.sysno != psysno)
2004 rset_close (r, rfd);
2005 logf (LOG_DEBUG, "%d keys, %d records", kno, *count);
2008 void rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
2009 oid_value attributeset,
2010 int num_bases, char **basenames,
2011 int *position, int *num_entries, ZebraScanEntry **list,
2015 int pos = *position;
2016 int num = *num_entries;
2020 char termz[IT_MAX_WORD+20];
2023 struct scan_info *scan_info_array;
2024 ZebraScanEntry *glist;
2025 int ords[32], ord_no = 0;
2029 char *search_type = NULL;
2030 char *rank_type = NULL;
2034 if (attributeset == VAL_NONE)
2035 attributeset = VAL_BIB1;
2037 zlog_scan (zapt, attributeset);
2038 logf (LOG_DEBUG, "position = %d, num = %d", pos, num);
2040 attr_init (&use, zapt, 1);
2041 use_value = attr_find (&use, &attributeset);
2043 if (zebra_maps_attr (zh->zebra_maps, zapt, ®_id, &search_type,
2044 &rank_type, &complete_flag, &sort_flag))
2050 if (use_value == -1)
2052 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
2056 data1_local_attribute *local_attr;
2058 if ((r=att_getentbyatt (zh, &attp, attributeset, use_value)))
2060 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
2061 attributeset, use_value);
2067 if (zebraExplain_curDatabase (zh->zei, basenames[base_no]))
2069 zh->errString = basenames[base_no];
2070 zh->errCode = 109; /* Database unavailable */
2073 for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
2074 local_attr = local_attr->next)
2078 ord = zebraExplain_lookupSU (zh->zei, attp.attset_ordinal,
2081 ords[ord_no++] = ord;
2089 /* prepare dictionary scanning */
2092 scan_info_array = odr_malloc (stream, ord_no * sizeof(*scan_info_array));
2093 for (i = 0; i < ord_no; i++)
2095 int j, prefix_len = 0;
2096 int before_tmp = before, after_tmp = after;
2097 struct scan_info *scan_info = scan_info_array + i;
2098 struct rpn_char_map_info rcmi;
2100 rpn_char_map_prepare (zh, reg_id, &rcmi);
2102 scan_info->before = before;
2103 scan_info->after = after;
2104 scan_info->odr = stream;
2106 scan_info->list = odr_malloc (stream, (before+after)*
2107 sizeof(*scan_info->list));
2108 for (j = 0; j<before+after; j++)
2109 scan_info->list[j].term = NULL;
2111 prefix_len += key_SU_code (ords[i], termz + prefix_len);
2112 termz[prefix_len++] = reg_id;
2113 termz[prefix_len] = 0;
2114 strcpy (scan_info->prefix, termz);
2116 trans_scan_term (zh, zapt, termz+prefix_len, reg_id);
2118 dict_scan (zh->dict, termz, &before_tmp, &after_tmp, scan_info,
2121 glist = odr_malloc (stream, (before+after)*sizeof(*glist));
2123 /* consider terms after main term */
2124 for (i = 0; i < ord_no; i++)
2128 for (i = 0; i<after; i++)
2131 const char *mterm = NULL;
2135 for (j = 0; j < ord_no; j++)
2137 if (ptr[j] < before+after &&
2138 (tst=scan_info_array[j].list[ptr[j]].term) &&
2139 (!mterm || strcmp (tst, mterm) < 0))
2147 scan_term_untrans (zh, stream, reg_id,
2148 &glist[i+before].term, mterm);
2149 rset = rset_trunc (zh, &scan_info_array[j0].list[ptr[j0]].isam_p, 1,
2150 glist[i+before].term, strlen(glist[i+before].term),
2154 for (j = j0+1; j<ord_no; j++)
2156 if (ptr[j] < before+after &&
2157 (tst=scan_info_array[j].list[ptr[j]].term) &&
2158 !strcmp (tst, mterm))
2160 rset_bool_parms bool_parms;
2164 rset_trunc (zh, &scan_info_array[j].list[ptr[j]].isam_p, 1,
2165 glist[i+before].term,
2166 strlen(glist[i+before].term), NULL);
2168 bool_parms.key_size = sizeof(struct it_key);
2169 bool_parms.cmp = key_compare_it;
2170 bool_parms.rset_l = rset;
2171 bool_parms.rset_r = rset2;
2173 rset = rset_create (rset_kind_or, &bool_parms);
2178 count_set (rset, &glist[i+before].occurrences);
2183 *num_entries -= (after-i);
2187 /* consider terms before main term */
2188 for (i = 0; i<ord_no; i++)
2191 for (i = 0; i<before; i++)
2194 const char *mterm = NULL;
2198 for (j = 0; j <ord_no; j++)
2200 if (ptr[j] < before &&
2201 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2202 (!mterm || strcmp (tst, mterm) > 0))
2211 scan_term_untrans (zh, stream, reg_id,
2212 &glist[before-1-i].term, mterm);
2215 (zh, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1,
2216 glist[before-1-i].term, strlen(glist[before-1-i].term),
2221 for (j = j0+1; j<ord_no; j++)
2223 if (ptr[j] < before &&
2224 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2225 !strcmp (tst, mterm))
2227 rset_bool_parms bool_parms;
2230 rset2 = rset_trunc (zh,
2231 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1,
2232 glist[before-1-i].term,
2233 strlen(glist[before-1-i].term), NULL);
2235 bool_parms.key_size = sizeof(struct it_key);
2236 bool_parms.cmp = key_compare_it;
2237 bool_parms.rset_l = rset;
2238 bool_parms.rset_r = rset2;
2240 rset = rset_create (rset_kind_or, &bool_parms);
2245 count_set (rset, &glist[before-1-i].occurrences);
2255 *list = glist + i; /* list is set to first 'real' entry */
2257 logf (LOG_DEBUG, "position = %d, num_entries = %d",
2258 *position, *num_entries);
2260 logf (LOG_DEBUG, "scan error: %d", zh->errCode);