2 * Copyright (C) 1995-1998, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.75 1998-03-05 08:45:13 adam
8 * New result set model and modular ranking system. Moved towards
9 * descent server API. System information stored as "SGML" records.
11 * Revision 1.74 1998/02/10 12:03:06 adam
14 * Revision 1.73 1998/01/29 13:40:11 adam
15 * Better logging for scan service.
17 * Revision 1.72 1998/01/07 13:53:41 adam
18 * Queries using simple ranked operands returns right number of hits.
20 * Revision 1.71 1997/12/18 10:54:24 adam
21 * New method result set method rs_hits that returns the number of
22 * hits in result-set (if known). The ranked result set returns real
23 * number of hits but only when not combined with other operands.
25 * Revision 1.70 1997/10/31 12:34:43 adam
26 * Changed a few log statements.
28 * Revision 1.69 1997/10/29 12:05:02 adam
29 * Server produces diagnostic "Unsupported Attribute Set" when appropriate.
31 * Revision 1.68 1997/10/27 14:33:06 adam
32 * Moved towards generic character mapping depending on "structure"
33 * field in abstract syntax file. Fixed a few memory leaks. Fixed
34 * bug with negative integers when doing searches with relational
37 * Revision 1.67 1997/09/29 09:06:10 adam
38 * Removed one static var in order to make this module thread safe.
40 * Revision 1.66 1997/09/25 14:58:03 adam
43 * Revision 1.65 1997/09/22 12:39:06 adam
44 * Added get_pos method for the ranked result sets.
46 * Revision 1.64 1997/09/18 08:59:20 adam
47 * Extra generic handle for the character mapping routines.
49 * Revision 1.63 1997/09/17 12:19:18 adam
50 * Zebra version corresponds to YAZ version 1.4.
51 * Changed Zebra server so that it doesn't depend on global common_resource.
53 * Revision 1.62 1997/09/05 15:30:09 adam
54 * Changed prototype for chr_map_input - added const.
55 * Added support for C++, headers uses extern "C" for public definitions.
57 * Revision 1.61 1997/02/10 10:21:14 adam
58 * Bug fix: in search terms character (^) wasn't observed.
60 * Revision 1.60 1997/01/31 11:10:34 adam
61 * Bug fix: Leading and trailing white space weren't removed in scan tokens.
63 * Revision 1.59 1997/01/17 11:31:46 adam
64 * Bug fix: complete phrase search didn't work.
66 * Revision 1.58 1996/12/23 15:30:45 adam
68 * Bug fix: result sets weren't deleted after server shut down.
70 * Revision 1.57 1996/11/11 13:38:02 adam
71 * Added proximity support in search.
73 * Revision 1.56 1996/11/08 11:10:32 adam
74 * Buffers used during file match got bigger.
75 * Compressed ISAM support everywhere.
76 * Bug fixes regarding masking characters in queries.
77 * Redesigned Regexp-2 queries.
79 * Revision 1.55 1996/11/04 14:07:44 adam
80 * Moved truncation code to trunc.c.
82 * Revision 1.54 1996/10/29 14:09:52 adam
83 * Use of cisam system - enabled if setting isamc is 1.
85 * Revision 1.53 1996/06/26 09:21:43 adam
86 * Bug fix: local attribute set wasn't obeyed in scan.
88 * Revision 1.52 1996/06/17 14:26:20 adam
89 * Function gen_regular_rel changed to handle negative numbers.
91 * Revision 1.51 1996/06/11 10:54:15 quinn
94 * Revision 1.50 1996/06/07 08:51:53 adam
95 * Bug fix: Character mapping was broken (introducued by last revision).
97 * Revision 1.49 1996/06/04 10:18:11 adam
98 * Search/scan uses character mapping module.
100 * Revision 1.48 1996/05/28 15:15:01 adam
101 * Bug fix: Didn't handle unknown database correctly.
103 * Revision 1.47 1996/05/15 18:36:28 adam
104 * Function trans_term transforms unsearchable characters to blanks.
106 * Revision 1.46 1996/05/15 11:57:56 adam
107 * Fixed bug introduced by set/field mapping in search operations.
109 * Revision 1.45 1996/05/14 11:34:00 adam
110 * Scan support in multiple registers/databases.
112 * Revision 1.44 1996/05/14 06:16:44 adam
113 * Compact use/set bytes used in search service.
115 * Revision 1.43 1996/05/09 09:54:43 adam
116 * Server supports maps from one logical attributes to a list of physical
118 * The extraction process doesn't make space consuming 'any' keys.
120 * Revision 1.42 1996/05/09 07:28:56 quinn
121 * Work towards phrases and multiple registers
123 * Revision 1.41 1996/03/20 09:36:43 adam
124 * Function dict_lookup_grep got extra parameter, init_pos, which marks
125 * from which position in pattern approximate pattern matching should occur.
126 * Approximate pattern matching is used in relevance=re-2.
128 * Revision 1.40 1996/02/02 13:44:44 adam
129 * The public dictionary functions simply use char instead of Dict_char
130 * to represent search strings. Dict_char is used internally only.
132 * Revision 1.39 1996/01/03 16:22:13 quinn
133 * operator->roperator
135 * Revision 1.38 1995/12/11 09:12:55 adam
136 * The rec_get function returns NULL if record doesn't exist - will
137 * happen in the server if the result set records have been deleted since
138 * the creation of the set (i.e. the search).
139 * The server saves a result temporarily if it is 'volatile', i.e. the
140 * set is register dependent.
142 * Revision 1.37 1995/12/06 15:05:28 adam
143 * More verbose in count_set.
145 * Revision 1.36 1995/12/06 12:41:27 adam
146 * New command 'stat' for the index program.
147 * Filenames can be read from stdin by specifying '-'.
148 * Bug fix/enhancement of the transformation from terms to regular
149 * expressons in the search engine.
151 * Revision 1.35 1995/11/27 09:29:00 adam
152 * Bug fixes regarding conversion to regular expressions.
154 * Revision 1.34 1995/11/16 17:00:56 adam
155 * Better logging of rpn query.
157 * Revision 1.33 1995/11/01 13:58:28 quinn
158 * Moving data1 to yaz/retrieval
160 * Revision 1.32 1995/10/27 14:00:11 adam
161 * Implemented detection of database availability.
163 * Revision 1.31 1995/10/17 18:02:10 adam
164 * New feature: databases. Implemented as prefix to words in dictionary.
166 * Revision 1.30 1995/10/16 09:32:38 adam
167 * More work on relational op.
169 * Revision 1.29 1995/10/13 16:01:49 adam
172 * Revision 1.28 1995/10/13 12:26:43 adam
173 * Optimization of truncation.
175 * Revision 1.27 1995/10/12 17:07:22 adam
178 * Revision 1.26 1995/10/12 12:40:54 adam
179 * Bug fixes in rpn_prox.
181 * Revision 1.25 1995/10/10 13:59:24 adam
182 * Function rset_open changed its wflag parameter to general flags.
184 * Revision 1.24 1995/10/09 16:18:37 adam
185 * Function dict_lookup_grep got extra client data parameter.
187 * Revision 1.23 1995/10/06 16:33:37 adam
188 * Use attribute mappings.
190 * Revision 1.22 1995/10/06 15:07:39 adam
191 * Structure 'local-number' handled.
193 * Revision 1.21 1995/10/06 13:52:06 adam
194 * Bug fixes. Handler may abort further scanning.
196 * Revision 1.20 1995/10/06 11:06:33 adam
197 * Scan entries include 'occurrences' now.
199 * Revision 1.19 1995/10/06 10:43:56 adam
200 * Scan added. 'occurrences' in scan entries not set yet.
202 * Revision 1.18 1995/10/04 16:57:20 adam
203 * Key input and merge sort in one pass.
205 * Revision 1.17 1995/10/04 12:55:17 adam
206 * Bug fix in ranked search. Use=Any keys inserted.
208 * Revision 1.16 1995/10/02 16:24:40 adam
209 * Use attribute actually used in search requests.
211 * Revision 1.15 1995/10/02 15:18:52 adam
212 * New member in recRetrieveCtrl: diagnostic.
214 * Revision 1.14 1995/09/28 12:10:32 adam
215 * Bug fixes. Field prefix used in queries.
217 * Revision 1.13 1995/09/18 14:17:50 adam
220 * Revision 1.12 1995/09/15 14:45:21 adam
222 * Work on truncation.
224 * Revision 1.11 1995/09/14 11:53:27 adam
225 * First work on regular expressions/truncations.
227 * Revision 1.10 1995/09/11 15:23:26 adam
228 * More work on relevance search.
230 * Revision 1.9 1995/09/11 13:09:35 adam
231 * More work on relevance feedback.
233 * Revision 1.8 1995/09/08 14:52:27 adam
234 * Minor changes. Dictionary is lower case now.
236 * Revision 1.7 1995/09/07 13:58:36 adam
237 * New parameter: result-set file descriptor (RSFD) to support multiple
238 * positions within the same result-set.
239 * Boolean operators: and, or, not implemented.
240 * Result-set references.
242 * Revision 1.6 1995/09/06 16:11:18 adam
243 * Option: only one word key per file.
245 * Revision 1.5 1995/09/06 10:33:04 adam
246 * More work on present. Some log messages removed.
248 * Revision 1.4 1995/09/05 15:28:40 adam
249 * More work on search engine.
251 * Revision 1.3 1995/09/04 15:20:22 adam
254 * Revision 1.2 1995/09/04 12:33:43 adam
255 * Various cleanup. YAZ util used instead.
257 * Revision 1.1 1995/09/04 09:10:40 adam
258 * More work on index add/del/update.
259 * Merge sort implemented.
260 * Initial work on z39 server.
279 struct rpn_char_map_info {
284 static const char **rpn_char_map_handler (void *vp, const char **from, int len)
286 struct rpn_char_map_info *p = vp;
287 return zebra_maps_input (p->zm, p->reg_type, from, len);
290 static void rpn_char_map_prepare (ZebraHandle zh, int reg_type,
291 struct rpn_char_map_info *map_info)
293 map_info->zm = zh->zebra_maps;
294 map_info->reg_type = reg_type;
295 dict_grep_cmap (zh->dict, map_info, rpn_char_map_handler);
302 Z_AttributesPlusTerm *zapt;
305 static int attr_find (AttrType *src, oid_value *attributeSetP)
307 while (src->major < src->zapt->num_attributes)
309 Z_AttributeElement *element;
311 element = src->zapt->attributeList[src->major];
312 if (src->type == *element->attributeType)
314 switch (element->which)
316 case Z_AttributeValue_numeric:
318 if (element->attributeSet && attributeSetP)
322 attrset = oid_getentbyoid (element->attributeSet);
323 *attributeSetP = attrset->value;
325 return *element->value.numeric;
327 case Z_AttributeValue_complex:
328 if (src->minor >= element->value.complex->num_list ||
329 element->value.complex->list[src->minor]->which !=
330 Z_StringOrNumeric_numeric)
333 if (element->attributeSet && attributeSetP)
337 attrset = oid_getentbyoid (element->attributeSet);
338 *attributeSetP = attrset->value;
340 return *element->value.complex->list[src->minor-1]->u.numeric;
350 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
372 static void term_untrans (ZebraHandle zh, int reg_type,
373 char *dst, const char *src)
377 const char *cp = zebra_maps_output (zh->zebra_maps, reg_type, &src);
384 static void add_isam_p (const char *name, const char *info,
388 if (p->isam_p_indx == p->isam_p_size)
390 ISAM_P *new_isam_p_buf;
394 p->isam_p_size = 2*p->isam_p_size + 100;
395 new_isam_p_buf = xmalloc (sizeof(*new_isam_p_buf) *
399 memcpy (new_isam_p_buf, p->isam_p_buf,
400 p->isam_p_indx * sizeof(*p->isam_p_buf));
401 xfree (p->isam_p_buf);
403 p->isam_p_buf = new_isam_p_buf;
406 new_term_no = xmalloc (sizeof(*new_term_no) *
410 memcpy (new_term_no, p->isam_p_buf,
411 p->isam_p_indx * sizeof(*p->term_no));
414 p->term_no = new_term_no;
417 assert (*info == sizeof(*p->isam_p_buf));
418 memcpy (p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
420 term_untrans (p->zh, p->reg_type, term_tmp, name+2);
421 logf (LOG_DEBUG, "grep: %s", term_tmp);
426 static int grep_handle (char *name, const char *info, void *p)
428 add_isam_p (name, info, p);
432 static int term_pre (ZebraMaps zebra_maps, int reg_type, const char **src,
433 const char *ct1, const char *ct2)
435 const char *s1, *s0 = *src;
438 /* skip white space */
441 if (ct1 && strchr (ct1, *s0))
443 if (ct2 && strchr (ct2, *s0))
446 map = zebra_maps_input (zebra_maps, reg_type, &s1, strlen(s1));
447 if (**map != *CHR_SPACE)
455 static int term_100 (ZebraMaps zebra_maps, int reg_type,
456 const char **src, char *dst, int space_split,
464 if (!term_pre (zebra_maps, reg_type, src, NULL, NULL))
470 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
471 if (space_split && **map == *CHR_SPACE)
475 if (!isalnum (*s1) && *s1 != '-')
487 static int term_101 (ZebraMaps zebra_maps, int reg_type,
488 const char **src, char *dst, int space_split,
496 if (!term_pre (zebra_maps, reg_type, src, "#", "#"))
505 dst_term[j++] = *s0++;
510 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
511 if (space_split && **map == *CHR_SPACE)
523 dst_term[j++] = '\0';
529 static int term_103 (ZebraMaps zebra_maps, int reg_type, const char **src,
530 char *dst, int *errors, int space_split,
538 if (!term_pre (zebra_maps, reg_type, src, "^\\()[].*+?|", "("))
541 if (errors && *s0 == '+' && s0[1] && s0[2] == '+' && s0[3] &&
544 *errors = s0[1] - '0';
551 if (strchr ("^\\()[].*+?|-", *s0))
559 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
560 if (**map == *CHR_SPACE)
577 static int term_102 (ZebraMaps zebra_maps, int reg_type, const char **src,
578 char *dst, int space_split, char *dst_term)
580 return term_103 (zebra_maps, reg_type, src, dst, NULL, space_split,
584 /* gen_regular_rel - generate regular expression from relation
585 * val: border value (inclusive)
586 * islt: 1 if <=; 0 if >=.
588 static void gen_regular_rel (char *dst, int val, int islt)
595 logf (LOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
599 strcpy (dst, "(-[0-9]+|(");
607 strcpy (dst, "([0-9]+|-(");
608 dst_p = strlen (dst);
618 dst_p = strlen (dst);
619 sprintf (numstr, "%d", val);
620 for (w = strlen(numstr); --w >= 0; pos++)
639 strcpy (dst + dst_p, numstr);
640 dst_p = strlen(dst) - pos - 1;
668 for (i = 0; i<pos; i++)
681 for (i=1; i<pos; i++)
682 strcat (dst, "[0-9]?");
686 for (i = 0; i <= pos; i++)
687 strcat (dst, "[0-9]");
688 strcat (dst, "[0-9]*");
693 static int relational_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
694 const char **term_sub,
696 oid_value attributeSet,
697 struct grep_info *grep_info,
706 char *term_tmp = term_dict + strlen(term_dict);
708 attr_init (&relation, zapt, 2);
709 relation_value = attr_find (&relation, NULL);
711 logf (LOG_DEBUG, "relation value=%d", relation_value);
712 switch (relation_value)
715 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_tmp, 1,
718 term_value = atoi (term_tmp);
719 logf (LOG_DEBUG, "Relation <");
720 gen_regular_rel (term_tmp, term_value-1, 1);
723 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_tmp, 1,
726 term_value = atoi (term_tmp);
727 logf (LOG_DEBUG, "Relation <=");
728 gen_regular_rel (term_tmp, term_value, 1);
731 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_tmp, 1,
734 term_value = atoi (term_tmp);
735 logf (LOG_DEBUG, "Relation >=");
736 gen_regular_rel (term_tmp, term_value, 0);
739 if (!term_100 (zh->zebra_maps, reg_type, term_sub, term_tmp, 1,
742 term_value = atoi (term_tmp);
743 logf (LOG_DEBUG, "Relation >");
744 gen_regular_rel (term_tmp, term_value+1, 0);
749 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_tmp);
750 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info, max_pos,
753 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
754 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
758 static int field_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
759 const char **term_sub,
760 oid_value attributeSet, struct grep_info *grep_info,
761 int reg_type, int complete_flag,
762 int num_bases, char **basenames,
765 char term_dict[2*IT_MAX_WORD+2];
768 int truncation_value;
771 oid_value curAttributeSet = attributeSet;
773 struct rpn_char_map_info rcmi;
774 int space_split = complete_flag ? 0 : 1;
776 rpn_char_map_prepare (zh, reg_type, &rcmi);
777 attr_init (&use, zapt, 1);
778 use_value = attr_find (&use, &curAttributeSet);
779 logf (LOG_DEBUG, "field_term, use value %d", use_value);
780 attr_init (&truncation, zapt, 5);
781 truncation_value = attr_find (&truncation, NULL);
782 logf (LOG_DEBUG, "truncation value %d", truncation_value);
787 for (base_no = 0; base_no < num_bases; base_no++)
790 data1_local_attribute *local_attr;
791 int max_pos, prefix_len = 0;
794 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
796 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
797 curAttributeSet, use_value, r);
804 if (zebraExplain_curDatabase (zh->zei, basenames[base_no]))
806 zh->errCode = 109; /* Database unavailable */
807 zh->errString = basenames[base_no];
810 for (local_attr = attp.local_attributes; local_attr;
811 local_attr = local_attr->next)
815 ord = zebraExplain_lookupSU (zh->zei, attp.attset_ordinal,
820 term_dict[prefix_len++] = '|';
822 term_dict[prefix_len++] = '(';
823 term_dict[prefix_len++] = 1;
824 term_dict[prefix_len++] = ord;
831 term_dict[prefix_len++] = ')';
832 term_dict[prefix_len++] = 1;
833 term_dict[prefix_len++] = reg_type;
834 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
835 term_dict[prefix_len] = '\0';
836 if (!relational_term (zh, zapt, &termp, term_dict,
837 attributeSet, grep_info, &max_pos, reg_type,
841 switch (truncation_value)
843 case -1: /* not specified */
844 case 100: /* do not truncate */
845 term_dict[j++] = '(';
846 if (!term_100 (zh->zebra_maps, reg_type,
847 &termp, term_dict + j, space_split, term_dst))
849 strcat (term_dict, ")");
850 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
851 &max_pos, 0, grep_handle);
853 logf (LOG_WARN, "dict_lookup_grep err, trunc=none:%d", r);
855 case 1: /* right truncation */
856 term_dict[j++] = '(';
857 if (!term_100 (zh->zebra_maps, reg_type,
858 &termp, term_dict + j, space_split, term_dst))
860 strcat (term_dict, ".*)");
861 dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
862 &max_pos, 0, grep_handle);
864 case 2: /* left truncation */
865 case 3: /* left&right truncation */
868 case 101: /* process # in term */
869 term_dict[j++] = '(';
870 if (!term_101 (zh->zebra_maps, reg_type,
871 &termp, term_dict + j, space_split, term_dst))
873 strcat (term_dict, ")");
874 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
875 &max_pos, 0, grep_handle);
877 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d", r);
879 case 102: /* Regexp-1 */
880 term_dict[j++] = '(';
881 if (!term_102 (zh->zebra_maps, reg_type,
882 &termp, term_dict + j, space_split, term_dst))
884 strcat (term_dict, ")");
885 logf (LOG_DEBUG, "Regexp-1 tolerance=%d", r);
886 r = dict_lookup_grep (zh->dict, term_dict, 0, grep_info,
887 &max_pos, 0, grep_handle);
889 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
892 case 103: /* Regexp-2 */
894 term_dict[j++] = '(';
895 if (!term_103 (zh->zebra_maps, reg_type,
896 &termp, term_dict + j, &r, space_split, term_dst))
898 strcat (term_dict, ")");
899 logf (LOG_DEBUG, "Regexp-2 tolerance=%d", r);
900 r = dict_lookup_grep (zh->dict, term_dict, r, grep_info,
901 &max_pos, 2, grep_handle);
903 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
910 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
914 static void trans_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
918 Z_Term *term = zapt->term;
920 sizez = term->u.general->len;
921 if (sizez > IT_MAX_WORD-1)
922 sizez = IT_MAX_WORD-1;
923 memcpy (termz, term->u.general->buf, sizez);
927 static void trans_scan_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
928 char *termz, int reg_type)
930 Z_Term *term = zapt->term;
932 const char *cp = (const char *) term->u.general->buf;
933 const char *cp_end = cp + term->u.general->len;
936 const char *space_map = NULL;
939 while ((len = (cp_end - cp)) > 0)
941 map = zebra_maps_input (zh->zebra_maps, reg_type, &cp, len);
942 if (**map == *CHR_SPACE)
947 for (src = space_map; *src; src++)
950 for (src = *map; *src; src++)
957 static RSET rpn_proximity (ZebraHandle zh, RSET rset1, RSET rset2,
959 int exclusion, int relation, int distance)
964 struct it_key buf1, buf2;
967 rset_temp_parms parms;
970 rsfd1 = rset_open (rset1, RSETF_READ);
971 more1 = rset_read (rset1, rsfd1, &buf1, &term_index);
973 rsfd2 = rset_open (rset2, RSETF_READ);
974 more2 = rset_read (rset2, rsfd2, &buf2, &term_index);
976 parms.key_size = sizeof (struct it_key);
977 parms.temp_path = res_get (zh->res, "setTmpDir");
978 result = rset_create (rset_kind_temp, &parms);
979 rsfd_result = rset_open (result, RSETF_WRITE);
981 logf (LOG_DEBUG, "rpn_proximity excl=%d ord=%d rel=%d dis=%d",
982 exclusion, ordered, relation, distance);
983 while (more1 && more2)
985 int cmp = key_compare_it (&buf1, &buf2);
987 more1 = rset_read (rset1, rsfd1, &buf1, &term_index);
989 more2 = rset_read (rset2, rsfd2, &buf2, &term_index);
992 int sysno = buf1.sysno;
996 seqno[n++] = buf1.seqno;
997 while ((more1 = rset_read (rset1, rsfd1, &buf1, &term_index)) &&
1000 seqno[n++] = buf1.seqno;
1003 for (i = 0; i<n; i++)
1005 int diff = buf2.seqno - seqno[i];
1006 int excl = exclusion;
1007 if (!ordered && diff < 0)
1012 if (diff < distance)
1016 if (diff <= distance)
1020 if (diff == distance)
1024 if (diff >= distance)
1028 if (diff > distance)
1032 if (diff != distance)
1037 rset_write (result, rsfd_result, &buf2);
1039 } while ((more2 = rset_read (rset2, rsfd2, &buf2, &term_index)) &&
1040 sysno == buf2.sysno);
1043 rset_close (result, rsfd_result);
1044 rset_close (rset1, rsfd1);
1045 rset_close (rset2, rsfd2);
1049 static RSET rpn_prox (ZebraHandle zh, RSET *rset, int rset_no)
1054 struct it_key **buf;
1056 char prox_term[1024];
1057 int length_prox_term = 0;
1058 int min_nn = 10000000;
1060 const char *flags = NULL;
1062 rsfd = xmalloc (sizeof(*rsfd)*rset_no);
1063 more = xmalloc (sizeof(*more)*rset_no);
1064 buf = xmalloc (sizeof(*buf)*rset_no);
1066 for (i = 0; i<rset_no; i++)
1069 buf[i] = xmalloc (sizeof(**buf));
1070 rsfd[i] = rset_open (rset[i], RSETF_READ);
1071 if (!(more[i] = rset_read (rset[i], rsfd[i], buf[i], &term_index)))
1073 for (j = 0; j<rset[i]->no_rset_terms; j++)
1075 const char *nflags = rset[i]->rset_terms[j]->flags;
1076 char *term = rset[i]->rset_terms[j]->name;
1077 int lterm = strlen(term);
1078 if (length_prox_term)
1079 prox_term[length_prox_term++] = ' ';
1080 strcpy (prox_term + length_prox_term, term);
1081 length_prox_term += lterm;
1082 if (min_nn > rset[i]->rset_terms[j]->nn)
1083 min_nn = rset[i]->rset_terms[j]->nn;
1089 rset_null_parms parms;
1093 rset_close (rset[i], rsfd[i]);
1097 parms.rset_term = rset_term_create (prox_term, -1, flags);
1098 parms.rset_term->nn = 0;
1099 result = rset_create (rset_kind_null, &parms);
1103 rset_temp_parms parms;
1106 parms.rset_term = rset_term_create (prox_term, -1, flags);
1107 parms.rset_term->nn = min_nn;
1108 parms.key_size = sizeof (struct it_key);
1109 parms.temp_path = res_get (zh->res, "setTmpDir");
1110 result = rset_create (rset_kind_temp, &parms);
1111 rsfd_result = rset_open (result, RSETF_WRITE);
1115 for (i = 1; i<rset_no; i++)
1124 cmp = key_compare_it (buf[i], buf[i-1]);
1127 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1128 buf[i-1], &term_index);
1133 if (buf[i-1]->seqno+1 != buf[i]->seqno)
1135 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1136 buf[i-1], &term_index);
1142 more[i] = rset_read (rset[i], rsfd[i], buf[i],
1149 rset_write (result, rsfd_result, buf[0]);
1150 more[0] = rset_read (*rset, *rsfd, *buf, &term_index);
1154 for (i = 0; i<rset_no; i++)
1156 rset_close (rset[i], rsfd[i]);
1159 rset_close (result, rsfd_result);
1167 static RSET rpn_search_APT_phrase (ZebraHandle zh,
1168 Z_AttributesPlusTerm *zapt,
1170 oid_value attributeSet,
1171 int reg_type, int complete_flag,
1172 const char *rank_type,
1173 int num_bases, char **basenames)
1175 char term_dst[IT_MAX_WORD+1];
1176 const char *termp = termz;
1177 RSET rset[60], result;
1178 int i, r, rset_no = 0;
1179 struct grep_info grep_info;
1182 grep_info.term_no = 0;
1184 grep_info.isam_p_size = 0;
1185 grep_info.isam_p_buf = NULL;
1187 grep_info.reg_type = reg_type;
1191 logf (LOG_DEBUG, "APT_phrase termp=%s", termp);
1192 grep_info.isam_p_indx = 0;
1193 r = field_term (zh, zapt, &termp, attributeSet, &grep_info,
1194 reg_type, complete_flag, num_bases, basenames,
1198 logf (LOG_DEBUG, "term: %s", term_dst);
1199 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1200 grep_info.isam_p_indx, term_dst,
1201 strlen(term_dst), rank_type);
1202 assert (rset[rset_no]);
1203 if (++rset_no >= sizeof(rset)/sizeof(*rset))
1207 xfree(grep_info.term_no);
1209 xfree (grep_info.isam_p_buf);
1212 rset_null_parms parms;
1214 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1215 return rset_create (rset_kind_null, &parms);
1217 else if (rset_no == 1)
1219 result = rpn_prox (zh, rset, rset_no);
1220 for (i = 0; i<rset_no; i++)
1221 rset_delete (rset[i]);
1225 static RSET rpn_search_APT_or_list (ZebraHandle zh,
1226 Z_AttributesPlusTerm *zapt,
1228 oid_value attributeSet,
1229 int reg_type, int complete_flag,
1230 const char *rank_type,
1231 int num_bases, char **basenames)
1233 char term_dst[IT_MAX_WORD+1];
1234 const char *termp = termz;
1235 RSET rset[60], result;
1236 int i, r, rset_no = 0;
1237 struct grep_info grep_info;
1240 grep_info.term_no = 0;
1242 grep_info.isam_p_size = 0;
1243 grep_info.isam_p_buf = NULL;
1245 grep_info.reg_type = reg_type;
1249 logf (LOG_DEBUG, "APT_or_list termp=%s", termp);
1250 grep_info.isam_p_indx = 0;
1251 r = field_term (zh, zapt, &termp, attributeSet, &grep_info,
1252 reg_type, complete_flag, num_bases, basenames,
1256 logf (LOG_DEBUG, "term: %s", term_dst);
1257 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1258 grep_info.isam_p_indx, term_dst,
1259 strlen(term_dst), rank_type);
1260 assert (rset[rset_no]);
1261 if (++rset_no >= sizeof(rset)/sizeof(*rset))
1265 xfree(grep_info.term_no);
1267 xfree (grep_info.isam_p_buf);
1270 rset_null_parms parms;
1272 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1273 return rset_create (rset_kind_null, &parms);
1276 for (i = 1; i<rset_no; i++)
1278 rset_bool_parms bool_parms;
1280 bool_parms.rset_l = result;
1281 bool_parms.rset_r = rset[i];
1282 bool_parms.key_size = sizeof(struct it_key);
1283 bool_parms.cmp = key_compare_it;
1284 result = rset_create (rset_kind_or, &bool_parms);
1289 static RSET rpn_search_APT_and_list (ZebraHandle zh,
1290 Z_AttributesPlusTerm *zapt,
1292 oid_value attributeSet,
1293 int reg_type, int complete_flag,
1294 const char *rank_type,
1295 int num_bases, char **basenames)
1297 char term_dst[IT_MAX_WORD+1];
1298 const char *termp = termz;
1299 RSET rset[60], result;
1300 int i, r, rset_no = 0;
1301 struct grep_info grep_info;
1304 grep_info.term_no = 0;
1306 grep_info.isam_p_size = 0;
1307 grep_info.isam_p_buf = NULL;
1309 grep_info.reg_type = reg_type;
1313 logf (LOG_DEBUG, "APT_and_list termp=%s", termp);
1314 grep_info.isam_p_indx = 0;
1315 r = field_term (zh, zapt, &termp, attributeSet, &grep_info,
1316 reg_type, complete_flag, num_bases, basenames,
1320 logf (LOG_DEBUG, "term: %s", term_dst);
1321 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1322 grep_info.isam_p_indx, term_dst,
1323 strlen(term_dst), rank_type);
1324 assert (rset[rset_no]);
1325 if (++rset_no >= sizeof(rset)/sizeof(*rset))
1329 xfree(grep_info.term_no);
1331 xfree (grep_info.isam_p_buf);
1334 rset_null_parms parms;
1336 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1337 return rset_create (rset_kind_null, &parms);
1340 for (i = 1; i<rset_no; i++)
1342 rset_bool_parms bool_parms;
1344 bool_parms.rset_l = result;
1345 bool_parms.rset_r = rset[i];
1346 bool_parms.key_size = sizeof(struct it_key);
1347 bool_parms.cmp = key_compare_it;
1348 result = rset_create (rset_kind_and, &bool_parms);
1354 static RSET rpn_search_APT_local (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1356 oid_value attributeSet,
1357 const char *rank_type)
1362 rset_temp_parms parms;
1364 parms.rset_term = rset_term_create (termz, -1, rank_type);
1365 parms.key_size = sizeof (struct it_key);
1366 parms.temp_path = res_get (zh->res, "setTmpDir");
1367 result = rset_create (rset_kind_temp, &parms);
1368 rsfd = rset_open (result, RSETF_WRITE);
1370 key.sysno = atoi (termz);
1374 rset_write (result, rsfd, &key);
1375 rset_close (result, rsfd);
1379 static RSET rpn_search_APT (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1380 oid_value attributeSet,
1381 int num_bases, char **basenames)
1384 char *search_type = NULL;
1385 char *rank_type = NULL;
1387 char termz[IT_MAX_WORD+1];
1389 zebra_maps_attr (zh->zebra_maps, zapt, ®_id, &search_type,
1390 &rank_type, &complete_flag);
1392 logf (LOG_DEBUG, "reg_id=%c", reg_id);
1393 logf (LOG_DEBUG, "complete_flag=%d", complete_flag);
1394 logf (LOG_DEBUG, "search_type=%s", search_type);
1395 logf (LOG_DEBUG, "rank_type=%s", rank_type);
1397 if (zapt->term->which != Z_Term_general)
1402 trans_term (zh, zapt, termz);
1404 if (!strcmp (search_type, "phrase"))
1406 return rpn_search_APT_phrase (zh, zapt, termz, attributeSet,
1407 reg_id, complete_flag, rank_type,
1408 num_bases, basenames);
1410 else if (!strcmp (search_type, "and-list"))
1412 return rpn_search_APT_and_list (zh, zapt, termz, attributeSet,
1413 reg_id, complete_flag, rank_type,
1414 num_bases, basenames);
1416 else if (!strcmp (search_type, "or-list"))
1418 return rpn_search_APT_or_list (zh, zapt, termz, attributeSet,
1419 reg_id, complete_flag, rank_type,
1420 num_bases, basenames);
1422 else if (!strcmp (search_type, "local"))
1424 return rpn_search_APT_local (zh, zapt, termz, attributeSet,
1431 static RSET rpn_search_structure (ZebraHandle zh, Z_RPNStructure *zs,
1432 oid_value attributeSet, ODR stream,
1433 int num_bases, char **basenames)
1436 if (zs->which == Z_RPNStructure_complex)
1438 Z_Operator *zop = zs->u.complex->roperator;
1439 rset_bool_parms bool_parms;
1441 bool_parms.rset_l = rpn_search_structure (zh, zs->u.complex->s1,
1442 attributeSet, stream,
1443 num_bases, basenames);
1444 if (bool_parms.rset_l == NULL)
1446 bool_parms.rset_r = rpn_search_structure (zh, zs->u.complex->s2,
1447 attributeSet, stream,
1448 num_bases, basenames);
1449 if (bool_parms.rset_r == NULL)
1451 rset_delete (bool_parms.rset_l);
1454 bool_parms.key_size = sizeof(struct it_key);
1455 bool_parms.cmp = key_compare_it;
1459 case Z_Operator_and:
1460 r = rset_create (rset_kind_and, &bool_parms);
1463 r = rset_create (rset_kind_or, &bool_parms);
1465 case Z_Operator_and_not:
1466 r = rset_create (rset_kind_not, &bool_parms);
1468 case Z_Operator_prox:
1469 if (zop->u.prox->which != Z_ProxCode_known)
1474 if (*zop->u.prox->proximityUnitCode != Z_ProxUnit_word)
1476 char *val = odr_malloc (stream, 16);
1478 zh->errString = val;
1479 sprintf (val, "%d", *zop->u.prox->proximityUnitCode);
1482 r = rpn_proximity (zh, bool_parms.rset_l, bool_parms.rset_r,
1483 *zop->u.prox->ordered,
1484 (!zop->u.prox->exclusion ? 0 :
1485 *zop->u.prox->exclusion),
1486 *zop->u.prox->relationType,
1487 *zop->u.prox->distance);
1494 else if (zs->which == Z_RPNStructure_simple)
1496 if (zs->u.simple->which == Z_Operand_APT)
1498 logf (LOG_DEBUG, "rpn_search_APT");
1499 r = rpn_search_APT (zh, zs->u.simple->u.attributesPlusTerm,
1500 attributeSet, num_bases, basenames);
1502 else if (zs->u.simple->which == Z_Operand_resultSetId)
1504 logf (LOG_DEBUG, "rpn_search_ref");
1505 r = resultSetRef (zh, zs->u.simple->u.resultSetId);
1507 r = rset_create (rset_kind_null, NULL);
1523 void rpn_search (ZebraHandle zh, ODR stream,
1524 Z_RPNQuery *rpn, int num_bases, char **basenames,
1525 const char *setname)
1529 oid_value attributeSet;
1534 zh->errString = NULL;
1537 attrset = oid_getentbyoid (rpn->attributeSetId);
1538 attributeSet = attrset->value;
1539 rset = rpn_search_structure (zh, rpn->RPNStructure, attributeSet, stream,
1540 num_bases, basenames);
1544 resultSetAdd (zh, setname, 1, rset, &zh->hits);
1546 logf (LOG_DEBUG, "search error: %d", zh->errCode);
1549 struct scan_info_entry {
1555 struct scan_info_entry *list;
1561 static int scan_handle (char *name, const char *info, int pos, void *client)
1563 int len_prefix, idx;
1564 struct scan_info *scan_info = client;
1566 len_prefix = strlen(scan_info->prefix);
1567 if (memcmp (name, scan_info->prefix, len_prefix))
1570 idx = scan_info->after - pos + scan_info->before;
1573 scan_info->list[idx].term = odr_malloc (scan_info->odr,
1574 strlen(name + len_prefix)+1);
1575 strcpy (scan_info->list[idx].term, name + len_prefix);
1576 assert (*info == sizeof(ISAM_P));
1577 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAM_P));
1581 static void scan_term_untrans (ZebraHandle zh, ODR stream, int reg_type,
1582 char **dstp, const char *src)
1584 char term_dst[1024], **dst;
1586 term_untrans (zh, reg_type, term_dst, src);
1588 *dst = odr_malloc (stream, strlen(term_dst)+1);
1589 strcpy (*dst, term_dst);
1592 static void count_set (RSET r, int *count)
1600 logf (LOG_DEBUG, "count_set");
1603 rfd = rset_open (r, RSETF_READ);
1604 while (rset_read (r, rfd, &key, &term_index))
1606 if (key.sysno != psysno)
1613 rset_close (r, rfd);
1614 logf (LOG_DEBUG, "%d keys, %d records", kno, *count);
1617 void rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
1618 oid_value attributeset,
1619 int num_bases, char **basenames,
1620 int *position, int *num_entries, ZebraScanEntry **list,
1624 int pos = *position;
1625 int num = *num_entries;
1629 char termz[IT_MAX_WORD+20];
1632 struct scan_info *scan_info_array;
1633 ZebraScanEntry *glist;
1634 int ords[32], ord_no = 0;
1638 char *search_type = NULL;
1639 char *rank_type = NULL;
1642 if (attributeset == VAL_NONE)
1643 attributeset = VAL_BIB1;
1645 zlog_scan (zapt, attributeset);
1646 logf (LOG_DEBUG, "position = %d, num = %d", pos, num);
1648 attr_init (&use, zapt, 1);
1649 use_value = attr_find (&use, &attributeset);
1651 if (zebra_maps_attr (zh->zebra_maps, zapt, ®_id, &search_type,
1652 &rank_type, &complete_flag))
1658 if (use_value == -1)
1660 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
1664 data1_local_attribute *local_attr;
1666 if ((r=att_getentbyatt (zh, &attp, attributeset, use_value)))
1668 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
1669 attributeset, use_value);
1675 if (zebraExplain_curDatabase (zh->zei, basenames[base_no]))
1677 zh->errString = basenames[base_no];
1678 zh->errCode = 109; /* Database unavailable */
1681 for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
1682 local_attr = local_attr->next)
1686 ord = zebraExplain_lookupSU (zh->zei, attp.attset_ordinal,
1689 ords[ord_no++] = ord;
1699 scan_info_array = odr_malloc (stream, ord_no * sizeof(*scan_info_array));
1700 for (i = 0; i < ord_no; i++)
1702 int j, prefix_len = 0;
1703 int before_tmp = before, after_tmp = after;
1704 struct scan_info *scan_info = scan_info_array + i;
1705 struct rpn_char_map_info rcmi;
1707 rpn_char_map_prepare (zh, reg_id, &rcmi);
1709 scan_info->before = before;
1710 scan_info->after = after;
1711 scan_info->odr = stream;
1713 scan_info->list = odr_malloc (stream, (before+after)*
1714 sizeof(*scan_info->list));
1715 for (j = 0; j<before+after; j++)
1716 scan_info->list[j].term = NULL;
1717 termz[prefix_len++] = ords[i];
1718 termz[prefix_len++] = reg_id;
1719 termz[prefix_len] = 0;
1720 strcpy (scan_info->prefix, termz);
1722 trans_scan_term (zh, zapt, termz+prefix_len, reg_id);
1724 dict_scan (zh->dict, termz, &before_tmp, &after_tmp, scan_info,
1727 glist = odr_malloc (stream, (before+after)*sizeof(*glist));
1728 for (i = 0; i < ord_no; i++)
1732 for (i = 0; i<after; i++)
1735 const char *mterm = NULL;
1739 for (j = 0; j < ord_no; j++)
1741 if (ptr[j] < before+after &&
1742 (tst=scan_info_array[j].list[ptr[j]].term) &&
1743 (!mterm || strcmp (tst, mterm) < 0))
1751 scan_term_untrans (zh, stream, reg_id,
1752 &glist[i+before].term, mterm);
1753 rset = rset_trunc (zh, &scan_info_array[j0].list[ptr[j0]].isam_p, 1,
1754 glist[i+before].term, strlen(glist[i+before].term),
1758 for (j = j0+1; j<ord_no; j++)
1760 if (ptr[j] < before+after &&
1761 (tst=scan_info_array[j].list[ptr[j]].term) &&
1762 !strcmp (tst, mterm))
1764 rset_bool_parms bool_parms;
1768 rset_trunc (zh, &scan_info_array[j].list[ptr[j]].isam_p, 1,
1769 glist[i+before].term,
1770 strlen(glist[i+before].term), NULL);
1772 bool_parms.key_size = sizeof(struct it_key);
1773 bool_parms.cmp = key_compare_it;
1774 bool_parms.rset_l = rset;
1775 bool_parms.rset_r = rset2;
1777 rset = rset_create (rset_kind_or, &bool_parms);
1782 count_set (rset, &glist[i+before].occurrences);
1787 *num_entries -= (after-i);
1791 for (i = 0; i<ord_no; i++)
1794 for (i = 0; i<before; i++)
1797 const char *mterm = NULL;
1801 for (j = 0; j <ord_no; j++)
1803 if (ptr[j] < before &&
1804 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
1805 (!mterm || strcmp (tst, mterm) > 0))
1814 scan_term_untrans (zh, stream, reg_id,
1815 &glist[before-1-i].term, mterm);
1818 (zh, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1,
1819 glist[before-1-i].term, strlen(glist[before-1-i].term),
1824 for (j = j0+1; j<ord_no; j++)
1826 if (ptr[j] < before &&
1827 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
1828 !strcmp (tst, mterm))
1830 rset_bool_parms bool_parms;
1833 rset2 = rset_trunc (zh,
1834 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1,
1835 glist[before-1-i].term,
1836 strlen(glist[before-1-i].term), NULL);
1838 bool_parms.key_size = sizeof(struct it_key);
1839 bool_parms.cmp = key_compare_it;
1840 bool_parms.rset_l = rset;
1841 bool_parms.rset_r = rset2;
1843 rset = rset_create (rset_kind_or, &bool_parms);
1848 count_set (rset, &glist[before-1-i].occurrences);
1858 *list = glist + i; /* list is set to first 'real' entry */
1860 logf (LOG_DEBUG, "position = %d, num_entries = %d",
1861 *position, *num_entries);
1863 logf (LOG_DEBUG, "scan error: %d", zh->errCode);