1 /* $Id: zrpn.c,v 1.129 2003-02-27 11:29:13 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
34 #include <zebra_xpath.h>
40 #include <rsbetween.h>
42 struct rpn_char_map_info {
51 Z_AttributesPlusTerm *zapt;
55 static const char **rpn_char_map_handler (void *vp, const char **from, int len)
57 struct rpn_char_map_info *p = (struct rpn_char_map_info *) vp;
58 const char **out = zebra_maps_input (p->zm, p->reg_type, from, len);
62 const char *outp = *out;
63 yaz_log (LOG_LOG, "---");
66 yaz_log (LOG_LOG, "%02X", *outp);
74 static void rpn_char_map_prepare (struct zebra_register *reg, int reg_type,
75 struct rpn_char_map_info *map_info)
77 map_info->zm = reg->zebra_maps;
78 map_info->reg_type = reg_type;
79 dict_grep_cmap (reg->dict, map_info, rpn_char_map_handler);
82 static int attr_find_ex (AttrType *src, oid_value *attributeSetP,
83 const char **string_value)
87 num_attributes = src->zapt->attributes->num_attributes;
88 while (src->major < num_attributes)
90 Z_AttributeElement *element;
92 element = src->zapt->attributes->attributes[src->major];
93 if (src->type == *element->attributeType)
95 switch (element->which)
97 case Z_AttributeValue_numeric:
99 if (element->attributeSet && attributeSetP)
103 attrset = oid_getentbyoid (element->attributeSet);
104 *attributeSetP = attrset->value;
106 return *element->value.numeric;
108 case Z_AttributeValue_complex:
109 if (src->minor >= element->value.complex->num_list)
111 if (element->attributeSet && attributeSetP)
115 attrset = oid_getentbyoid (element->attributeSet);
116 *attributeSetP = attrset->value;
118 if (element->value.complex->list[src->minor]->which ==
119 Z_StringOrNumeric_numeric)
123 *element->value.complex->list[src->minor-1]->u.numeric;
125 else if (element->value.complex->list[src->minor]->which ==
126 Z_StringOrNumeric_string)
132 element->value.complex->list[src->minor-1]->u.string;
146 static int attr_find (AttrType *src, oid_value *attributeSetP)
148 return attr_find_ex (src, attributeSetP, 0);
151 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
174 static void term_untrans (ZebraHandle zh, int reg_type,
175 char *dst, const char *src)
180 const char *cp = zebra_maps_output (zh->reg->zebra_maps,
182 if (!cp && len < IT_MAX_WORD-1)
185 while (*cp && len < IT_MAX_WORD-1)
191 static void add_isam_p (const char *name, const char *info,
194 if (p->isam_p_indx == p->isam_p_size)
196 ISAMS_P *new_isam_p_buf;
200 p->isam_p_size = 2*p->isam_p_size + 100;
201 new_isam_p_buf = (ISAMS_P *) xmalloc (sizeof(*new_isam_p_buf) *
205 memcpy (new_isam_p_buf, p->isam_p_buf,
206 p->isam_p_indx * sizeof(*p->isam_p_buf));
207 xfree (p->isam_p_buf);
209 p->isam_p_buf = new_isam_p_buf;
212 new_term_no = (int *) xmalloc (sizeof(*new_term_no) *
216 memcpy (new_term_no, p->isam_p_buf,
217 p->isam_p_indx * sizeof(*p->term_no));
220 p->term_no = new_term_no;
223 assert (*info == sizeof(*p->isam_p_buf));
224 memcpy (p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
231 char term_tmp[IT_MAX_WORD];
233 int len = key_SU_decode (&su_code, name);
235 term_untrans (p->zh, p->reg_type, term_tmp, name+len+1);
236 logf (LOG_LOG, "grep: %d %c %s", su_code, name[len], term_tmp);
237 zebraExplain_lookup_ord (p->zh->reg->zei,
238 su_code, &db, &set, &use);
239 logf (LOG_LOG, "grep: set=%d use=%d db=%s", set, use, db);
241 resultSetAddTerm (p->zh, p->termset, name[len], db,
248 static int grep_handle (char *name, const char *info, void *p)
250 add_isam_p (name, info, (struct grep_info *) p);
254 static int term_pre (ZebraMaps zebra_maps, int reg_type, const char **src,
255 const char *ct1, const char *ct2)
257 const char *s1, *s0 = *src;
260 /* skip white space */
263 if (ct1 && strchr (ct1, *s0))
265 if (ct2 && strchr (ct2, *s0))
268 map = zebra_maps_input (zebra_maps, reg_type, &s1, strlen(s1));
269 if (**map != *CHR_SPACE)
277 #define REGEX_CHARS " []()|.*+?!"
279 /* term_100: handle term, where trunc=none (no operators at all) */
280 static int term_100 (ZebraMaps zebra_maps, int reg_type,
281 const char **src, char *dst, int space_split,
289 const char *space_start = 0;
290 const char *space_end = 0;
292 if (!term_pre (zebra_maps, reg_type, src, NULL, NULL))
298 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
301 if (**map == *CHR_SPACE)
304 else /* complete subfield only. */
306 if (**map == *CHR_SPACE)
307 { /* save space mapping for later .. */
312 else if (space_start)
313 { /* reload last space */
314 while (space_start < space_end)
316 if (strchr (REGEX_CHARS, *space_start))
318 dst_term[j++] = *space_start;
319 dst[i++] = *space_start++;
322 space_start = space_end = 0;
325 /* add non-space char */
328 if (strchr(REGEX_CHARS, *s1))
340 /* term_101: handle term, where trunc=Process # */
341 static int term_101 (ZebraMaps zebra_maps, int reg_type,
342 const char **src, char *dst, int space_split,
350 if (!term_pre (zebra_maps, reg_type, src, "#", "#"))
359 dst_term[j++] = *s0++;
364 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
365 if (space_split && **map == *CHR_SPACE)
369 if (strchr(REGEX_CHARS, *s1))
377 dst_term[j++] = '\0';
382 /* term_103: handle term, where trunc=re-2 (regular expressions) */
383 static int term_103 (ZebraMaps zebra_maps, int reg_type, const char **src,
384 char *dst, int *errors, int space_split,
392 if (!term_pre (zebra_maps, reg_type, src, "^\\()[].*+?|", "("))
395 if (errors && *s0 == '+' && s0[1] && s0[2] == '+' && s0[3] &&
398 *errors = s0[1] - '0';
405 if (strchr ("^\\()[].*+?|-", *s0))
413 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
414 if (**map == *CHR_SPACE)
418 if (strchr(REGEX_CHARS, *s1))
431 /* term_103: handle term, where trunc=re-1 (regular expressions) */
432 static int term_102 (ZebraMaps zebra_maps, int reg_type, const char **src,
433 char *dst, int space_split, char *dst_term)
435 return term_103 (zebra_maps, reg_type, src, dst, NULL, space_split,
440 /* term_104: handle term, where trunc=Process # and ! */
441 static int term_104 (ZebraMaps zebra_maps, int reg_type,
442 const char **src, char *dst, int space_split,
450 if (!term_pre (zebra_maps, reg_type, src, "?*#", "?*#"))
457 dst_term[j++] = *s0++;
458 if (*s0 >= '0' && *s0 <= '9')
461 while (*s0 >= '0' && *s0 <= '9')
463 limit = limit * 10 + (*s0 - '0');
464 dst_term[j++] = *s0++;
484 dst_term[j++] = *s0++;
489 dst_term[j++] = *s0++;
493 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
494 if (space_split && **map == *CHR_SPACE)
498 if (strchr(REGEX_CHARS, *s1))
506 dst_term[j++] = '\0';
511 /* term_105/106: handle term, where trunc=Process * and ! and right trunc */
512 static int term_105 (ZebraMaps zebra_maps, int reg_type,
513 const char **src, char *dst, int space_split,
514 char *dst_term, int right_truncate)
521 if (!term_pre (zebra_maps, reg_type, src, "*!", "*!"))
530 dst_term[j++] = *s0++;
535 dst_term[j++] = *s0++;
539 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
540 if (space_split && **map == *CHR_SPACE)
544 if (strchr(REGEX_CHARS, *s1))
558 dst_term[j++] = '\0';
564 /* gen_regular_rel - generate regular expression from relation
565 * val: border value (inclusive)
566 * islt: 1 if <=; 0 if >=.
568 static void gen_regular_rel (char *dst, int val, int islt)
575 logf (LOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
579 strcpy (dst, "(-[0-9]+|(");
587 strcpy (dst, "([0-9]+|-(");
588 dst_p = strlen (dst);
598 dst_p = strlen (dst);
599 sprintf (numstr, "%d", val);
600 for (w = strlen(numstr); --w >= 0; pos++)
619 strcpy (dst + dst_p, numstr);
620 dst_p = strlen(dst) - pos - 1;
648 for (i = 0; i<pos; i++)
661 /* match everything less than 10^(pos-1) */
663 for (i=1; i<pos; i++)
664 strcat (dst, "[0-9]?");
668 /* match everything greater than 10^pos */
669 for (i = 0; i <= pos; i++)
670 strcat (dst, "[0-9]");
671 strcat (dst, "[0-9]*");
676 void string_rel_add_char (char **term_p, const char *src, int *indx)
678 if (src[*indx] == '\\')
679 *(*term_p)++ = src[(*indx)++];
680 *(*term_p)++ = src[(*indx)++];
684 * > abc ([b-].*|a[c-].*|ab[d-].*|abc.+)
685 * ([^-a].*|a[^-b].*ab[^-c].*|abc.+)
686 * >= abc ([b-].*|a[c-].*|ab[c-].*)
687 * ([^-a].*|a[^-b].*|ab[c-].*)
688 * < abc ([-0].*|a[-a].*|ab[-b].*)
689 * ([^a-].*|a[^b-].*|ab[^c-].*)
690 * <= abc ([-0].*|a[-a].*|ab[-b].*|abc)
691 * ([^a-].*|a[^b-].*|ab[^c-].*|abc)
693 static int string_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
694 const char **term_sub, char *term_dict,
695 oid_value attributeSet,
696 int reg_type, int space_split, char *term_dst)
701 char *term_tmp = term_dict + strlen(term_dict);
702 char term_component[256];
704 attr_init (&relation, zapt, 2);
705 relation_value = attr_find (&relation, NULL);
707 logf (LOG_DEBUG, "string relation value=%d", relation_value);
708 switch (relation_value)
711 if (!term_100 (zh->reg->zebra_maps, reg_type,
712 term_sub, term_component,
713 space_split, term_dst))
715 logf (LOG_DEBUG, "Relation <");
718 for (i = 0; term_component[i]; )
725 string_rel_add_char (&term_tmp, term_component, &j);
730 string_rel_add_char (&term_tmp, term_component, &i);
741 if (!term_100 (zh->reg->zebra_maps, reg_type,
742 term_sub, term_component,
743 space_split, term_dst))
745 logf (LOG_DEBUG, "Relation <=");
748 for (i = 0; term_component[i]; )
753 string_rel_add_char (&term_tmp, term_component, &j);
757 string_rel_add_char (&term_tmp, term_component, &i);
766 for (i = 0; term_component[i]; )
767 string_rel_add_char (&term_tmp, term_component, &i);
772 if (!term_100 (zh->reg->zebra_maps, reg_type,
773 term_sub, term_component, space_split, term_dst))
775 logf (LOG_DEBUG, "Relation >");
778 for (i = 0; term_component[i];)
783 string_rel_add_char (&term_tmp, term_component, &j);
788 string_rel_add_char (&term_tmp, term_component, &i);
796 for (i = 0; term_component[i];)
797 string_rel_add_char (&term_tmp, term_component, &i);
804 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub,
805 term_component, space_split, term_dst))
807 logf (LOG_DEBUG, "Relation >=");
810 for (i = 0; term_component[i];)
817 string_rel_add_char (&term_tmp, term_component, &j);
820 if (term_component[i+1])
824 string_rel_add_char (&term_tmp, term_component, &i);
828 string_rel_add_char (&term_tmp, term_component, &i);
840 logf (LOG_DEBUG, "Relation =");
841 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub,
842 term_component, space_split, term_dst))
844 strcat (term_tmp, "(");
845 strcat (term_tmp, term_component);
846 strcat (term_tmp, ")");
851 static int string_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
852 const char **term_sub,
853 oid_value attributeSet, NMEM stream,
854 struct grep_info *grep_info,
855 int reg_type, int complete_flag,
856 int num_bases, char **basenames,
857 char *term_dst, int xpath_use);
859 static RSET term_trunc (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
860 const char **term_sub,
861 oid_value attributeSet, NMEM stream,
862 struct grep_info *grep_info,
863 int reg_type, int complete_flag,
864 int num_bases, char **basenames,
866 const char *rank_type, int xpath_use)
869 grep_info->isam_p_indx = 0;
870 r = string_term (zh, zapt, term_sub, attributeSet, stream, grep_info,
871 reg_type, complete_flag, num_bases, basenames,
872 term_dst, xpath_use);
875 logf (LOG_DEBUG, "term: %s", term_dst);
876 return rset_trunc (zh, grep_info->isam_p_buf,
877 grep_info->isam_p_indx, term_dst,
878 strlen(term_dst), rank_type, 1 /* preserve pos */,
883 static int string_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
884 const char **term_sub,
885 oid_value attributeSet, NMEM stream,
886 struct grep_info *grep_info,
887 int reg_type, int complete_flag,
888 int num_bases, char **basenames,
889 char *term_dst, int xpath_use)
891 char term_dict[2*IT_MAX_WORD+4000];
894 int truncation_value;
897 const char *use_string = 0;
898 oid_value curAttributeSet = attributeSet;
900 struct rpn_char_map_info rcmi;
901 int space_split = complete_flag ? 0 : 1;
903 rpn_char_map_prepare (zh->reg, reg_type, &rcmi);
904 attr_init (&use, zapt, 1);
905 use_value = attr_find_ex (&use, &curAttributeSet, &use_string);
906 logf (LOG_DEBUG, "string_term, use value %d", use_value);
907 attr_init (&truncation, zapt, 5);
908 truncation_value = attr_find (&truncation, NULL);
909 logf (LOG_DEBUG, "truncation value %d", truncation_value);
911 if (use_value == -1) /* no attribute - assumy "any" */
913 for (base_no = 0; base_no < num_bases; base_no++)
916 data1_local_attribute id_xpath_attr;
917 data1_local_attribute *local_attr;
918 int max_pos, prefix_len = 0;
922 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
924 zh->errCode = 109; /* Database unavailable */
925 zh->errString = basenames[base_no];
928 if (use_value == -2) /* string attribute (assume IDXPATH/any) */
930 use_value = xpath_use;
931 attp.local_attributes = &id_xpath_attr;
932 attp.attset_ordinal = VAL_IDXPATH;
933 id_xpath_attr.next = 0;
934 id_xpath_attr.local = use_value;
936 else if (curAttributeSet == VAL_IDXPATH)
938 attp.local_attributes = &id_xpath_attr;
939 attp.attset_ordinal = VAL_IDXPATH;
940 id_xpath_attr.next = 0;
941 id_xpath_attr.local = use_value;
945 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
947 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
948 curAttributeSet, use_value, r);
951 /* set was found, but value wasn't defined */
953 sprintf (val_str, "%d", use_value);
955 zh->errString = nmem_strdup (stream, val_str);
960 struct oident oident;
962 oident.proto = PROTO_Z3950;
963 oident.oclass = CLASS_ATTSET;
964 oident.value = curAttributeSet;
965 oid_ent_to_oid (&oident, oid);
968 zh->errString = nmem_strdup (stream, oident.desc);
973 for (local_attr = attp.local_attributes; local_attr;
974 local_attr = local_attr->next)
980 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
985 term_dict[prefix_len++] = '|';
987 term_dict[prefix_len++] = '(';
989 ord_len = key_SU_encode (ord, ord_buf);
990 for (i = 0; i<ord_len; i++)
992 term_dict[prefix_len++] = 1;
993 term_dict[prefix_len++] = ord_buf[i];
999 sprintf (val_str, "%d", use_value);
1001 zh->errString = nmem_strdup (stream, val_str);
1004 term_dict[prefix_len++] = ')';
1005 term_dict[prefix_len++] = 1;
1006 term_dict[prefix_len++] = reg_type;
1007 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1008 term_dict[prefix_len] = '\0';
1010 switch (truncation_value)
1012 case -1: /* not specified */
1013 case 100: /* do not truncate */
1014 if (!string_relation (zh, zapt, &termp, term_dict,
1016 reg_type, space_split, term_dst))
1018 logf (LOG_LOG, "dict_lookup_grep: %s", term_dict+prefix_len);
1019 r = dict_lookup_grep (zh->reg->dict, term_dict, 0,
1020 grep_info, &max_pos, 0, grep_handle);
1022 logf (LOG_WARN, "dict_lookup_grep fail %d", r);
1024 case 1: /* right truncation */
1025 term_dict[j++] = '(';
1026 if (!term_100 (zh->reg->zebra_maps, reg_type,
1027 &termp, term_dict + j, space_split, term_dst))
1029 strcat (term_dict, ".*)");
1030 dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1031 &max_pos, 0, grep_handle);
1033 case 2: /* keft truncation */
1034 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1035 if (!term_100 (zh->reg->zebra_maps, reg_type,
1036 &termp, term_dict + j, space_split, term_dst))
1038 strcat (term_dict, ")");
1039 dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1040 &max_pos, 0, grep_handle);
1042 case 3: /* left&right truncation */
1043 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1044 if (!term_100 (zh->reg->zebra_maps, reg_type,
1045 &termp, term_dict + j, space_split, term_dst))
1047 strcat (term_dict, ".*)");
1048 dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1049 &max_pos, 0, grep_handle);
1053 case 101: /* process # in term */
1054 term_dict[j++] = '(';
1055 if (!term_101 (zh->reg->zebra_maps, reg_type,
1056 &termp, term_dict + j, space_split, term_dst))
1058 strcat (term_dict, ")");
1059 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1060 &max_pos, 0, grep_handle);
1062 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d", r);
1064 case 102: /* Regexp-1 */
1065 term_dict[j++] = '(';
1066 if (!term_102 (zh->reg->zebra_maps, reg_type,
1067 &termp, term_dict + j, space_split, term_dst))
1069 strcat (term_dict, ")");
1070 logf (LOG_DEBUG, "Regexp-1 tolerance=%d", r);
1071 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1072 &max_pos, 0, grep_handle);
1074 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
1077 case 103: /* Regexp-2 */
1079 term_dict[j++] = '(';
1080 if (!term_103 (zh->reg->zebra_maps, reg_type,
1081 &termp, term_dict + j, &r, space_split, term_dst))
1083 strcat (term_dict, ")");
1084 logf (LOG_DEBUG, "Regexp-2 tolerance=%d", r);
1085 r = dict_lookup_grep (zh->reg->dict, term_dict, r, grep_info,
1086 &max_pos, 2, grep_handle);
1088 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
1091 case 104: /* process # and ! in term */
1092 term_dict[j++] = '(';
1093 if (!term_104 (zh->reg->zebra_maps, reg_type,
1094 &termp, term_dict + j, space_split, term_dst))
1096 strcat (term_dict, ")");
1097 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1098 &max_pos, 0, grep_handle);
1100 logf (LOG_WARN, "dict_lookup_grep err, trunc=#/!: %d", r);
1102 case 105: /* process * and ! in term */
1103 term_dict[j++] = '(';
1104 if (!term_105 (zh->reg->zebra_maps, reg_type,
1105 &termp, term_dict + j, space_split, term_dst, 1))
1107 strcat (term_dict, ")");
1108 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1109 &max_pos, 0, grep_handle);
1111 logf (LOG_WARN, "dict_lookup_grep err, trunc=*/!: %d", r);
1113 case 106: /* process * and ! in term */
1114 term_dict[j++] = '(';
1115 if (!term_105 (zh->reg->zebra_maps, reg_type,
1116 &termp, term_dict + j, space_split, term_dst, 0))
1118 strcat (term_dict, ")");
1119 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1120 &max_pos, 0, grep_handle);
1122 logf (LOG_WARN, "dict_lookup_grep err, trunc=*/!: %d", r);
1127 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1132 /* convert APT search term to UTF8 */
1133 static int zapt_term_to_utf8 (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1137 Z_Term *term = zapt->term;
1139 switch (term->which)
1141 case Z_Term_general:
1142 if (zh->iconv_to_utf8 != 0)
1144 char *inbuf = term->u.general->buf;
1145 size_t inleft = term->u.general->len;
1146 char *outbuf = termz;
1147 size_t outleft = IT_MAX_WORD-1;
1150 ret = yaz_iconv(zh->iconv_to_utf8, &inbuf, &inleft,
1152 if (ret == (size_t)(-1))
1154 ret = yaz_iconv(zh->iconv_to_utf8, 0, 0, 0, 0);
1162 sizez = term->u.general->len;
1163 if (sizez > IT_MAX_WORD-1)
1164 sizez = IT_MAX_WORD-1;
1165 memcpy (termz, term->u.general->buf, sizez);
1166 termz[sizez] = '\0';
1169 case Z_Term_characterString:
1170 sizez = strlen(term->u.characterString);
1171 if (sizez > IT_MAX_WORD-1)
1172 sizez = IT_MAX_WORD-1;
1173 memcpy (termz, term->u.characterString, sizez);
1174 termz[sizez] = '\0';
1183 /* convert APT SCAN term to internal cmap */
1184 static int trans_scan_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1185 char *termz, int reg_type)
1187 char termz0[IT_MAX_WORD];
1189 if (zapt_term_to_utf8(zh, zapt, termz0))
1190 return -1; /* error */
1194 const char *cp = (const char *) termz0;
1195 const char *cp_end = cp + strlen(cp);
1198 const char *space_map = NULL;
1201 while ((len = (cp_end - cp)) > 0)
1203 map = zebra_maps_input (zh->reg->zebra_maps, reg_type, &cp, len);
1204 if (**map == *CHR_SPACE)
1209 for (src = space_map; *src; src++)
1212 for (src = *map; *src; src++)
1221 static RSET rpn_prox (ZebraHandle zh, RSET *rset, int rset_no,
1222 int ordered, int exclusion, int relation, int distance)
1227 struct it_key **buf;
1229 char prox_term[1024];
1230 int length_prox_term = 0;
1231 int min_nn = 10000000;
1233 int term_type = Z_Term_characterString;
1234 const char *flags = NULL;
1236 rsfd = (RSFD *) xmalloc (sizeof(*rsfd)*rset_no);
1237 more = (int *) xmalloc (sizeof(*more)*rset_no);
1238 buf = (struct it_key **) xmalloc (sizeof(*buf)*rset_no);
1241 for (i = 0; i<rset_no; i++)
1244 for (j = 0; j<rset[i]->no_rset_terms; j++)
1246 const char *nflags = rset[i]->rset_terms[j]->flags;
1247 char *term = rset[i]->rset_terms[j]->name;
1248 int lterm = strlen(term);
1249 if (lterm + length_prox_term < sizeof(prox_term)-1)
1251 if (length_prox_term)
1252 prox_term[length_prox_term++] = ' ';
1253 strcpy (prox_term + length_prox_term, term);
1254 length_prox_term += lterm;
1256 if (min_nn > rset[i]->rset_terms[j]->nn)
1257 min_nn = rset[i]->rset_terms[j]->nn;
1259 term_type = rset[i]->rset_terms[j]->type;
1261 /* only if all term types are of type characterString .. */
1262 /* the resulting term is of that type */
1263 if (term_type != Z_Term_characterString)
1264 term_type = Z_Term_general;
1267 for (i = 0; i<rset_no; i++)
1272 for (i = 0; i<rset_no; i++)
1274 buf[i] = (struct it_key *) xmalloc (sizeof(**buf));
1275 rsfd[i] = rset_open (rset[i], RSETF_READ);
1276 if (!(more[i] = rset_read (rset[i], rsfd[i], buf[i], &term_index)))
1281 /* at least one is empty ... return null set */
1282 rset_null_parms parms;
1284 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1286 parms.rset_term->nn = 0;
1287 result = rset_create (rset_kind_null, &parms);
1289 else if (ordered && relation == 3 && exclusion == 0 && distance == 1)
1291 /* special proximity case = phrase search ... */
1292 rset_temp_parms parms;
1295 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1297 parms.rset_term->nn = min_nn;
1298 parms.cmp = key_compare_it;
1299 parms.key_size = sizeof (struct it_key);
1300 parms.temp_path = res_get (zh->res, "setTmpDir");
1301 result = rset_create (rset_kind_temp, &parms);
1302 rsfd_result = rset_open (result, RSETF_WRITE);
1306 for (i = 1; i<rset_no; i++)
1315 cmp = key_compare_it (buf[i], buf[i-1]);
1318 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1319 buf[i-1], &term_index);
1324 if (buf[i-1]->seqno+1 != buf[i]->seqno)
1326 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1327 buf[i-1], &term_index);
1333 more[i] = rset_read (rset[i], rsfd[i], buf[i],
1340 rset_write (result, rsfd_result, buf[0]);
1341 more[0] = rset_read (*rset, *rsfd, *buf, &term_index);
1344 rset_close (result, rsfd_result);
1346 else if (rset_no == 2)
1348 /* generic proximity case (two input sets only) ... */
1349 rset_temp_parms parms;
1352 logf (LOG_LOG, "generic prox, dist = %d, relation = %d, ordered =%d, exclusion=%d",
1353 distance, relation, ordered, exclusion);
1354 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1356 parms.rset_term->nn = min_nn;
1357 parms.cmp = key_compare_it;
1358 parms.key_size = sizeof (struct it_key);
1359 parms.temp_path = res_get (zh->res, "setTmpDir");
1360 result = rset_create (rset_kind_temp, &parms);
1361 rsfd_result = rset_open (result, RSETF_WRITE);
1363 while (more[0] && more[1])
1365 int cmp = key_compare_it (buf[0], buf[1]);
1367 more[0] = rset_read (rset[0], rsfd[0], buf[0], &term_index);
1369 more[1] = rset_read (rset[1], rsfd[1], buf[1], &term_index);
1372 int sysno = buf[0]->sysno;
1376 seqno[n++] = buf[0]->seqno;
1377 while ((more[0] = rset_read (rset[0], rsfd[0], buf[0],
1379 sysno == buf[0]->sysno)
1381 seqno[n++] = buf[0]->seqno;
1384 for (i = 0; i<n; i++)
1386 int diff = buf[1]->seqno - seqno[i];
1387 int excl = exclusion;
1388 if (!ordered && diff < 0)
1393 if (diff < distance && diff >= 0)
1397 if (diff <= distance && diff >= 0)
1401 if (diff == distance && diff >= 0)
1405 if (diff >= distance && diff >= 0)
1409 if (diff > distance && diff >= 0)
1413 if (diff != distance && diff >= 0)
1419 rset_write (result, rsfd_result, buf[1]);
1423 } while ((more[1] = rset_read (rset[1], rsfd[1], buf[1],
1425 sysno == buf[1]->sysno);
1428 rset_close (result, rsfd_result);
1432 rset_null_parms parms;
1434 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1436 parms.rset_term->nn = 0;
1437 result = rset_create (rset_kind_null, &parms);
1439 for (i = 0; i<rset_no; i++)
1442 rset_close (rset[i], rsfd[i]);
1452 char *normalize_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1453 const char *termz, NMEM stream, unsigned reg_id)
1456 AttrType truncation;
1457 int truncation_value;
1460 attr_init (&truncation, zapt, 5);
1461 truncation_value = attr_find (&truncation, NULL);
1463 switch (truncation_value)
1483 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, ex_list,
1484 termz, strlen(termz));
1486 return nmem_strdup(stream, termz);
1489 char *buf = (char*) nmem_malloc (stream, wrbuf_len(wrbuf)+1);
1490 memcpy (buf, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1491 buf[wrbuf_len(wrbuf)] = '\0';
1496 static void grep_info_delete (struct grep_info *grep_info)
1499 xfree(grep_info->term_no);
1501 xfree (grep_info->isam_p_buf);
1504 static int grep_info_prepare (ZebraHandle zh,
1505 Z_AttributesPlusTerm *zapt,
1506 struct grep_info *grep_info,
1511 int termset_value_numeric;
1512 const char *termset_value_string;
1515 grep_info->term_no = 0;
1517 grep_info->isam_p_size = 0;
1518 grep_info->isam_p_buf = NULL;
1520 grep_info->reg_type = reg_type;
1521 grep_info->termset = 0;
1525 attr_init (&termset, zapt, 8);
1526 termset_value_numeric =
1527 attr_find_ex (&termset, NULL, &termset_value_string);
1528 if (termset_value_numeric != -1)
1531 const char *termset_name = 0;
1532 if (termset_value_numeric != -2)
1535 sprintf (resname, "%d", termset_value_numeric);
1536 termset_name = resname;
1539 termset_name = termset_value_string;
1540 logf (LOG_LOG, "creating termset set %s", termset_name);
1541 grep_info->termset = resultSetAdd (zh, termset_name, 1);
1542 if (!grep_info->termset)
1545 zh->errString = nmem_strdup (stream, termset_name);
1553 static RSET rpn_search_APT_phrase (ZebraHandle zh,
1554 Z_AttributesPlusTerm *zapt,
1555 const char *termz_org,
1556 oid_value attributeSet,
1558 int reg_type, int complete_flag,
1559 const char *rank_type, int xpath_use,
1560 int num_bases, char **basenames)
1562 char term_dst[IT_MAX_WORD+1];
1563 RSET rset[60], result;
1565 struct grep_info grep_info;
1566 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1567 const char *termp = termz;
1570 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1574 logf (LOG_DEBUG, "APT_phrase termp=%s", termp);
1575 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1577 reg_type, complete_flag,
1578 num_bases, basenames,
1579 term_dst, rank_type,
1583 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1586 grep_info_delete (&grep_info);
1589 rset_null_parms parms;
1591 parms.rset_term = rset_term_create (termz, -1, rank_type,
1593 return rset_create (rset_kind_null, &parms);
1595 else if (rset_no == 1)
1597 result = rpn_prox (zh, rset, rset_no, 1, 0, 3, 1);
1598 for (i = 0; i<rset_no; i++)
1599 rset_delete (rset[i]);
1603 static RSET rpn_search_APT_or_list (ZebraHandle zh,
1604 Z_AttributesPlusTerm *zapt,
1605 const char *termz_org,
1606 oid_value attributeSet,
1608 int reg_type, int complete_flag,
1609 const char *rank_type,
1611 int num_bases, char **basenames)
1613 char term_dst[IT_MAX_WORD+1];
1614 RSET rset[60], result;
1616 struct grep_info grep_info;
1617 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1618 const char *termp = termz;
1620 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1624 logf (LOG_DEBUG, "APT_or_list termp=%s", termp);
1625 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1627 reg_type, complete_flag,
1628 num_bases, basenames,
1629 term_dst, rank_type,
1633 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1636 grep_info_delete (&grep_info);
1639 rset_null_parms parms;
1641 parms.rset_term = rset_term_create (termz, -1, rank_type,
1643 return rset_create (rset_kind_null, &parms);
1646 for (i = 1; i<rset_no; i++)
1648 rset_bool_parms bool_parms;
1650 bool_parms.rset_l = result;
1651 bool_parms.rset_r = rset[i];
1652 bool_parms.key_size = sizeof(struct it_key);
1653 bool_parms.cmp = key_compare_it;
1654 result = rset_create (rset_kind_or, &bool_parms);
1659 static RSET rpn_search_APT_and_list (ZebraHandle zh,
1660 Z_AttributesPlusTerm *zapt,
1661 const char *termz_org,
1662 oid_value attributeSet,
1664 int reg_type, int complete_flag,
1665 const char *rank_type,
1667 int num_bases, char **basenames)
1669 char term_dst[IT_MAX_WORD+1];
1670 RSET rset[60], result;
1672 struct grep_info grep_info;
1673 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1674 const char *termp = termz;
1676 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1680 logf (LOG_DEBUG, "APT_and_list termp=%s", termp);
1681 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1683 reg_type, complete_flag,
1684 num_bases, basenames,
1685 term_dst, rank_type,
1689 assert (rset[rset_no]);
1690 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1693 grep_info_delete (&grep_info);
1696 rset_null_parms parms;
1698 parms.rset_term = rset_term_create (termz, -1, rank_type,
1700 return rset_create (rset_kind_null, &parms);
1703 for (i = 1; i<rset_no; i++)
1705 rset_bool_parms bool_parms;
1707 bool_parms.rset_l = result;
1708 bool_parms.rset_r = rset[i];
1709 bool_parms.key_size = sizeof(struct it_key);
1710 bool_parms.cmp = key_compare_it;
1711 result = rset_create (rset_kind_and, &bool_parms);
1716 static int numeric_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1717 const char **term_sub,
1719 oid_value attributeSet,
1720 struct grep_info *grep_info,
1729 char *term_tmp = term_dict + strlen(term_dict);
1731 attr_init (&relation, zapt, 2);
1732 relation_value = attr_find (&relation, NULL);
1734 logf (LOG_DEBUG, "numeric relation value=%d", relation_value);
1736 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub, term_tmp, 1,
1739 term_value = atoi (term_tmp);
1740 switch (relation_value)
1743 logf (LOG_DEBUG, "Relation <");
1744 gen_regular_rel (term_tmp, term_value-1, 1);
1747 logf (LOG_DEBUG, "Relation <=");
1748 gen_regular_rel (term_tmp, term_value, 1);
1751 logf (LOG_DEBUG, "Relation >=");
1752 gen_regular_rel (term_tmp, term_value, 0);
1755 logf (LOG_DEBUG, "Relation >");
1756 gen_regular_rel (term_tmp, term_value+1, 0);
1760 logf (LOG_DEBUG, "Relation =");
1761 sprintf (term_tmp, "(0*%d)", term_value);
1763 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_tmp);
1764 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info, max_pos,
1767 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1768 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1772 static int numeric_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1773 const char **term_sub,
1774 oid_value attributeSet, struct grep_info *grep_info,
1775 int reg_type, int complete_flag,
1776 int num_bases, char **basenames,
1777 char *term_dst, int xpath_use, NMEM stream)
1779 char term_dict[2*IT_MAX_WORD+2];
1783 const char *use_string = 0;
1784 oid_value curAttributeSet = attributeSet;
1786 struct rpn_char_map_info rcmi;
1788 rpn_char_map_prepare (zh->reg, reg_type, &rcmi);
1789 attr_init (&use, zapt, 1);
1790 use_value = attr_find_ex (&use, &curAttributeSet, &use_string);
1792 if (use_value == -1)
1795 for (base_no = 0; base_no < num_bases; base_no++)
1798 data1_local_attribute id_xpath_attr;
1799 data1_local_attribute *local_attr;
1800 int max_pos, prefix_len = 0;
1803 if (use_value == -2) /* string attribute (assume IDXPATH/any) */
1805 use_value = xpath_use;
1806 attp.local_attributes = &id_xpath_attr;
1807 attp.attset_ordinal = VAL_IDXPATH;
1808 id_xpath_attr.next = 0;
1809 id_xpath_attr.local = use_value;
1811 else if (curAttributeSet == VAL_IDXPATH)
1813 attp.local_attributes = &id_xpath_attr;
1814 attp.attset_ordinal = VAL_IDXPATH;
1815 id_xpath_attr.next = 0;
1816 id_xpath_attr.local = use_value;
1820 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
1822 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1823 curAttributeSet, use_value, r);
1827 sprintf (val_str, "%d", use_value);
1828 zh->errString = nmem_strdup (stream, val_str);
1836 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
1838 zh->errCode = 109; /* Database unavailable */
1839 zh->errString = basenames[base_no];
1842 for (local_attr = attp.local_attributes; local_attr;
1843 local_attr = local_attr->next)
1849 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
1854 term_dict[prefix_len++] = '|';
1856 term_dict[prefix_len++] = '(';
1858 ord_len = key_SU_encode (ord, ord_buf);
1859 for (i = 0; i<ord_len; i++)
1861 term_dict[prefix_len++] = 1;
1862 term_dict[prefix_len++] = ord_buf[i];
1868 sprintf (val_str, "%d", use_value);
1870 zh->errString = nmem_strdup (stream, val_str);
1873 term_dict[prefix_len++] = ')';
1874 term_dict[prefix_len++] = 1;
1875 term_dict[prefix_len++] = reg_type;
1876 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1877 term_dict[prefix_len] = '\0';
1878 if (!numeric_relation (zh, zapt, &termp, term_dict,
1879 attributeSet, grep_info, &max_pos, reg_type,
1884 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1888 static RSET rpn_search_APT_numeric (ZebraHandle zh,
1889 Z_AttributesPlusTerm *zapt,
1891 oid_value attributeSet,
1893 int reg_type, int complete_flag,
1894 const char *rank_type, int xpath_use,
1895 int num_bases, char **basenames)
1897 char term_dst[IT_MAX_WORD+1];
1898 const char *termp = termz;
1899 RSET rset[60], result;
1900 int i, r, rset_no = 0;
1901 struct grep_info grep_info;
1903 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1907 logf (LOG_DEBUG, "APT_numeric termp=%s", termp);
1908 grep_info.isam_p_indx = 0;
1909 r = numeric_term (zh, zapt, &termp, attributeSet, &grep_info,
1910 reg_type, complete_flag, num_bases, basenames,
1911 term_dst, xpath_use,
1915 logf (LOG_DEBUG, "term: %s", term_dst);
1916 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1917 grep_info.isam_p_indx, term_dst,
1918 strlen(term_dst), rank_type,
1919 0 /* preserve position */,
1921 assert (rset[rset_no]);
1922 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1925 grep_info_delete (&grep_info);
1928 rset_null_parms parms;
1930 parms.rset_term = rset_term_create (term_dst, -1, rank_type,
1932 return rset_create (rset_kind_null, &parms);
1935 for (i = 1; i<rset_no; i++)
1937 rset_bool_parms bool_parms;
1939 bool_parms.rset_l = result;
1940 bool_parms.rset_r = rset[i];
1941 bool_parms.key_size = sizeof(struct it_key);
1942 bool_parms.cmp = key_compare_it;
1943 result = rset_create (rset_kind_and, &bool_parms);
1948 static RSET rpn_search_APT_local (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1950 oid_value attributeSet,
1952 const char *rank_type)
1957 rset_temp_parms parms;
1959 parms.rset_term = rset_term_create (termz, -1, rank_type,
1961 parms.cmp = key_compare_it;
1962 parms.key_size = sizeof (struct it_key);
1963 parms.temp_path = res_get (zh->res, "setTmpDir");
1964 result = rset_create (rset_kind_temp, &parms);
1965 rsfd = rset_open (result, RSETF_WRITE);
1967 key.sysno = atoi (termz);
1971 rset_write (result, rsfd, &key);
1972 rset_close (result, rsfd);
1976 static RSET rpn_sort_spec (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1977 oid_value attributeSet, NMEM stream,
1978 Z_SortKeySpecList *sort_sequence,
1979 const char *rank_type)
1981 rset_null_parms parms;
1983 int sort_relation_value;
1984 AttrType sort_relation_type;
1989 Z_AttributeElement *ae;
1994 attr_init (&sort_relation_type, zapt, 7);
1995 sort_relation_value = attr_find (&sort_relation_type, &attributeSet);
1997 attr_init (&use_type, zapt, 1);
1998 use_value = attr_find (&use_type, &attributeSet);
2000 if (!sort_sequence->specs)
2002 sort_sequence->num_specs = 10;
2003 sort_sequence->specs = (Z_SortKeySpec **)
2004 nmem_malloc (stream, sort_sequence->num_specs *
2005 sizeof(*sort_sequence->specs));
2006 for (i = 0; i<sort_sequence->num_specs; i++)
2007 sort_sequence->specs[i] = 0;
2009 if (zapt->term->which != Z_Term_general)
2012 i = atoi_n ((char *) zapt->term->u.general->buf,
2013 zapt->term->u.general->len);
2014 if (i >= sort_sequence->num_specs)
2016 sprintf (termz, "%d", i);
2018 oe.proto = PROTO_Z3950;
2019 oe.oclass = CLASS_ATTSET;
2020 oe.value = attributeSet;
2021 if (!oid_ent_to_oid (&oe, oid))
2024 sks = (Z_SortKeySpec *) nmem_malloc (stream, sizeof(*sks));
2025 sks->sortElement = (Z_SortElement *)
2026 nmem_malloc (stream, sizeof(*sks->sortElement));
2027 sks->sortElement->which = Z_SortElement_generic;
2028 sk = sks->sortElement->u.generic = (Z_SortKey *)
2029 nmem_malloc (stream, sizeof(*sk));
2030 sk->which = Z_SortKey_sortAttributes;
2031 sk->u.sortAttributes = (Z_SortAttributes *)
2032 nmem_malloc (stream, sizeof(*sk->u.sortAttributes));
2034 sk->u.sortAttributes->id = oid;
2035 sk->u.sortAttributes->list = (Z_AttributeList *)
2036 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list));
2037 sk->u.sortAttributes->list->num_attributes = 1;
2038 sk->u.sortAttributes->list->attributes = (Z_AttributeElement **)
2039 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list->attributes));
2040 ae = *sk->u.sortAttributes->list->attributes = (Z_AttributeElement *)
2041 nmem_malloc (stream, sizeof(**sk->u.sortAttributes->list->attributes));
2042 ae->attributeSet = 0;
2043 ae->attributeType = (int *)
2044 nmem_malloc (stream, sizeof(*ae->attributeType));
2045 *ae->attributeType = 1;
2046 ae->which = Z_AttributeValue_numeric;
2047 ae->value.numeric = (int *)
2048 nmem_malloc (stream, sizeof(*ae->value.numeric));
2049 *ae->value.numeric = use_value;
2051 sks->sortRelation = (int *)
2052 nmem_malloc (stream, sizeof(*sks->sortRelation));
2053 if (sort_relation_value == 1)
2054 *sks->sortRelation = Z_SortRelation_ascending;
2055 else if (sort_relation_value == 2)
2056 *sks->sortRelation = Z_SortRelation_descending;
2058 *sks->sortRelation = Z_SortRelation_ascending;
2060 sks->caseSensitivity = (int *)
2061 nmem_malloc (stream, sizeof(*sks->caseSensitivity));
2062 *sks->caseSensitivity = 0;
2064 sks->which = Z_SortKeySpec_null;
2065 sks->u.null = odr_nullval ();
2066 sort_sequence->specs[i] = sks;
2068 parms.rset_term = rset_term_create (termz, -1, rank_type,
2070 return rset_create (rset_kind_null, &parms);
2073 /* pop - moved to xpath.c */
2076 struct xpath_predicate {
2079 #define XPATH_PREDICATE_RELATION 1
2085 #define XPATH_PREDICATE_BOOLEAN 2
2088 struct xpath_predicate *left;
2089 struct xpath_predicate *right;
2094 struct xpath_location_step {
2096 struct xpath_predicate *predicate;
2101 static int parse_xpath(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2102 oid_value attributeSet,
2103 struct xpath_location_step *xpath, NMEM mem)
2105 oid_value curAttributeSet = attributeSet;
2107 const char *use_string = 0;
2110 attr_init (&use, zapt, 1);
2111 attr_find_ex (&use, &curAttributeSet, &use_string);
2113 if (!use_string || *use_string != '/')
2116 return (parse_xpath_str(use_string, xpath, mem));
2121 static RSET xpath_trunc(ZebraHandle zh, NMEM stream,
2122 int reg_type, const char *term, int use,
2123 oid_value curAttributeSet)
2126 struct grep_info grep_info;
2127 char term_dict[2048];
2130 int ord = zebraExplain_lookupSU (zh->reg->zei, curAttributeSet, use);
2131 int ord_len, i, r, max_pos;
2132 int term_type = Z_Term_characterString;
2133 const char *flags = "void";
2135 if (grep_info_prepare (zh, 0 /* zapt */, &grep_info, '0', stream))
2137 rset_null_parms parms;
2139 parms.rset_term = rset_term_create (term, strlen(term),
2141 parms.rset_term->nn = 0;
2142 return rset_create (rset_kind_null, &parms);
2147 rset_null_parms parms;
2149 parms.rset_term = rset_term_create (term, strlen(term),
2151 parms.rset_term->nn = 0;
2152 return rset_create (rset_kind_null, &parms);
2155 term_dict[prefix_len++] = '|';
2157 term_dict[prefix_len++] = '(';
2159 ord_len = key_SU_encode (ord, ord_buf);
2160 for (i = 0; i<ord_len; i++)
2162 term_dict[prefix_len++] = 1;
2163 term_dict[prefix_len++] = ord_buf[i];
2165 term_dict[prefix_len++] = ')';
2166 term_dict[prefix_len++] = 1;
2167 term_dict[prefix_len++] = reg_type;
2169 strcpy (term_dict+prefix_len, term);
2171 grep_info.isam_p_indx = 0;
2172 r = dict_lookup_grep (zh->reg->dict, term_dict, 0,
2173 &grep_info, &max_pos, 0, grep_handle);
2174 yaz_log (LOG_LOG, "%s %d positions", term,
2175 grep_info.isam_p_indx);
2176 rset = rset_trunc (zh, grep_info.isam_p_buf,
2177 grep_info.isam_p_indx, term, strlen(term),
2178 flags, 1, term_type);
2179 grep_info_delete (&grep_info);
2183 static RSET rpn_search_xpath (ZebraHandle zh,
2184 oid_value attributeSet,
2185 int num_bases, char **basenames,
2186 NMEM stream, const char *rank_type, RSET rset,
2187 int xpath_len, struct xpath_location_step *xpath)
2189 oid_value curAttributeSet = attributeSet;
2196 yaz_log (LOG_LOG, "len=%d", xpath_len);
2197 for (i = 0; i<xpath_len; i++)
2199 yaz_log (LOG_LOG, "XPATH %d %s", i, xpath[i].part);
2203 curAttributeSet = VAL_IDXPATH;
2213 a[@attr=value]/b[@other=othervalue]
2215 /e/@a val range(e/,range(@a,freetext(w,1015,val),@a),e/)
2216 /a/b val range(b/a/,freetext(w,1016,val),b/a/)
2217 /a/b/@c val range(b/a/,range(@c,freetext(w,1016,val),@c),b/a/)
2218 /a/b[@c=y] val range(b/a/,freetext(w,1016,val),b/a/,@c=y)
2219 /a[@c=y]/b val range(a/,range(b/a/,freetext(w,1016,val),b/a/),a/,@c=y)
2220 /a[@c=x]/b[@c=y] range(a/,range(b/a/,freetext(w,1016,val),b/a/,@c=y),a/,@c=x)
2224 dict_grep_cmap (zh->reg->dict, 0, 0);
2226 for (base_no = 0; base_no < num_bases; base_no++)
2228 int level = xpath_len;
2231 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
2233 zh->errCode = 109; /* Database unavailable */
2234 zh->errString = basenames[base_no];
2237 while (--level >= 0)
2239 char xpath_rev[128];
2241 rset_between_parms parms;
2242 RSET rset_start_tag = 0, rset_end_tag = 0, rset_attr = 0;
2246 for (i = level; i >= 1; --i)
2248 const char *cp = xpath[i].part;
2254 memcpy (xpath_rev + len, "[^/]*", 5);
2257 else if (*cp == ' ')
2260 xpath_rev[len++] = 1;
2261 xpath_rev[len++] = ' ';
2265 xpath_rev[len++] = *cp;
2266 xpath_rev[len++] = '/';
2268 else if (i == 1) /* // case */
2270 xpath_rev[len++] = '.';
2271 xpath_rev[len++] = '*';
2276 if (xpath[level].predicate &&
2277 xpath[level].predicate->which == XPATH_PREDICATE_RELATION &&
2278 xpath[level].predicate->u.relation.name[0])
2280 char predicate_str[128];
2282 strcpy (predicate_str,
2283 xpath[level].predicate->u.relation.name+1);
2284 if (xpath[level].predicate->u.relation.value)
2286 strcat (predicate_str, "=");
2287 strcat (predicate_str,
2288 xpath[level].predicate->u.relation.value);
2290 rset_attr = xpath_trunc (
2291 zh, stream, '0', predicate_str, 3, curAttributeSet);
2298 yaz_log (LOG_LOG, "xpath_rev (%d) = %s", level, xpath_rev);
2299 if (strlen(xpath_rev))
2301 rset_start_tag = xpath_trunc(zh, stream,
2302 '0', xpath_rev, 1, curAttributeSet);
2304 rset_end_tag = xpath_trunc(zh, stream,
2305 '0', xpath_rev, 2, curAttributeSet);
2307 parms.key_size = sizeof(struct it_key);
2308 parms.cmp = key_compare_it;
2309 parms.rset_l = rset_start_tag;
2310 parms.rset_m = rset;
2311 parms.rset_r = rset_end_tag;
2312 parms.rset_attr = rset_attr;
2313 parms.printer = key_print_it;
2314 rset = rset_create (rset_kind_between, &parms);
2325 static RSET rpn_search_APT (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2326 oid_value attributeSet, NMEM stream,
2327 Z_SortKeySpecList *sort_sequence,
2328 int num_bases, char **basenames)
2331 char *search_type = NULL;
2332 char rank_type[128];
2335 char termz[IT_MAX_WORD+1];
2339 struct xpath_location_step xpath[10];
2341 zebra_maps_attr (zh->reg->zebra_maps, zapt, ®_id, &search_type,
2342 rank_type, &complete_flag, &sort_flag);
2344 logf (LOG_DEBUG, "reg_id=%c", reg_id);
2345 logf (LOG_DEBUG, "complete_flag=%d", complete_flag);
2346 logf (LOG_DEBUG, "search_type=%s", search_type);
2347 logf (LOG_DEBUG, "rank_type=%s", rank_type);
2349 if (zapt_term_to_utf8(zh, zapt, termz))
2353 return rpn_sort_spec (zh, zapt, attributeSet, stream, sort_sequence,
2355 xpath_len = parse_xpath(zh, zapt, attributeSet, xpath, stream);
2359 if (xpath[xpath_len-1].part[0] == '@')
2363 if (!strcmp (search_type, "phrase"))
2365 rset = rpn_search_APT_phrase (zh, zapt, termz, attributeSet, stream,
2366 reg_id, complete_flag, rank_type,
2368 num_bases, basenames);
2370 else if (!strcmp (search_type, "and-list"))
2372 rset = rpn_search_APT_and_list (zh, zapt, termz, attributeSet, stream,
2373 reg_id, complete_flag, rank_type,
2375 num_bases, basenames);
2377 else if (!strcmp (search_type, "or-list"))
2379 rset = rpn_search_APT_or_list (zh, zapt, termz, attributeSet, stream,
2380 reg_id, complete_flag, rank_type,
2382 num_bases, basenames);
2384 else if (!strcmp (search_type, "local"))
2386 rset = rpn_search_APT_local (zh, zapt, termz, attributeSet, stream,
2389 else if (!strcmp (search_type, "numeric"))
2391 rset = rpn_search_APT_numeric (zh, zapt, termz, attributeSet, stream,
2392 reg_id, complete_flag, rank_type,
2394 num_bases, basenames);
2396 else if (!strcmp (search_type, "always"))
2402 return rpn_search_xpath (zh, attributeSet, num_bases, basenames,
2403 stream, rank_type, rset, xpath_len, xpath);
2406 static RSET rpn_search_structure (ZebraHandle zh, Z_RPNStructure *zs,
2407 oid_value attributeSet, NMEM stream,
2408 Z_SortKeySpecList *sort_sequence,
2409 int num_bases, char **basenames)
2412 if (zs->which == Z_RPNStructure_complex)
2414 Z_Operator *zop = zs->u.complex->roperator;
2415 rset_bool_parms bool_parms;
2417 bool_parms.rset_l = rpn_search_structure (zh, zs->u.complex->s1,
2418 attributeSet, stream,
2420 num_bases, basenames);
2421 if (bool_parms.rset_l == NULL)
2423 bool_parms.rset_r = rpn_search_structure (zh, zs->u.complex->s2,
2424 attributeSet, stream,
2426 num_bases, basenames);
2427 if (bool_parms.rset_r == NULL)
2429 rset_delete (bool_parms.rset_l);
2432 bool_parms.key_size = sizeof(struct it_key);
2433 bool_parms.cmp = key_compare_it;
2437 case Z_Operator_and:
2438 r = rset_create (rset_kind_and, &bool_parms);
2441 r = rset_create (rset_kind_or, &bool_parms);
2443 case Z_Operator_and_not:
2444 r = rset_create (rset_kind_not, &bool_parms);
2446 case Z_Operator_prox:
2447 if (zop->u.prox->which != Z_ProximityOperator_known)
2452 if (*zop->u.prox->u.known != Z_ProxUnit_word)
2454 char *val = (char *) nmem_malloc (stream, 16);
2456 zh->errString = val;
2457 sprintf (val, "%d", *zop->u.prox->u.known);
2464 rsets[0] = bool_parms.rset_l;
2465 rsets[1] = bool_parms.rset_r;
2467 r = rpn_prox (zh, rsets, 2,
2468 *zop->u.prox->ordered,
2469 (!zop->u.prox->exclusion ? 0 :
2470 *zop->u.prox->exclusion),
2471 *zop->u.prox->relationType,
2472 *zop->u.prox->distance);
2473 rset_delete (rsets[0]);
2474 rset_delete (rsets[1]);
2482 else if (zs->which == Z_RPNStructure_simple)
2484 if (zs->u.simple->which == Z_Operand_APT)
2486 logf (LOG_DEBUG, "rpn_search_APT");
2487 r = rpn_search_APT (zh, zs->u.simple->u.attributesPlusTerm,
2488 attributeSet, stream, sort_sequence,
2489 num_bases, basenames);
2491 else if (zs->u.simple->which == Z_Operand_resultSetId)
2493 logf (LOG_DEBUG, "rpn_search_ref");
2494 r = resultSetRef (zh, zs->u.simple->u.resultSetId);
2497 r = rset_create (rset_kind_null, NULL);
2500 nmem_strdup (stream, zs->u.simple->u.resultSetId);
2519 RSET rpn_search (ZebraHandle zh, NMEM nmem,
2520 Z_RPNQuery *rpn, int num_bases, char **basenames,
2521 const char *setname,
2526 oid_value attributeSet;
2527 Z_SortKeySpecList *sort_sequence;
2531 zh->errString = NULL;
2534 sort_sequence = (Z_SortKeySpecList *)
2535 nmem_malloc (nmem, sizeof(*sort_sequence));
2536 sort_sequence->num_specs = 10;
2537 sort_sequence->specs = (Z_SortKeySpec **)
2538 nmem_malloc (nmem, sort_sequence->num_specs *
2539 sizeof(*sort_sequence->specs));
2540 for (i = 0; i<sort_sequence->num_specs; i++)
2541 sort_sequence->specs[i] = 0;
2543 attrset = oid_getentbyoid (rpn->attributeSetId);
2544 attributeSet = attrset->value;
2545 rset = rpn_search_structure (zh, rpn->RPNStructure, attributeSet,
2546 nmem, sort_sequence, num_bases, basenames);
2551 logf (LOG_DEBUG, "search error: %d", zh->errCode);
2553 for (i = 0; sort_sequence->specs[i]; i++)
2555 sort_sequence->num_specs = i;
2557 resultSetRank (zh, sset, rset);
2560 logf (LOG_DEBUG, "resultSetSortSingle in rpn_search");
2561 resultSetSortSingle (zh, nmem, sset, rset,
2562 sort_sequence, &sort_status);
2565 logf (LOG_DEBUG, "resultSetSortSingle status = %d", zh->errCode);
2571 struct scan_info_entry {
2577 struct scan_info_entry *list;
2583 static int scan_handle (char *name, const char *info, int pos, void *client)
2585 int len_prefix, idx;
2586 struct scan_info *scan_info = (struct scan_info *) client;
2588 len_prefix = strlen(scan_info->prefix);
2589 if (memcmp (name, scan_info->prefix, len_prefix))
2591 if (pos > 0) idx = scan_info->after - pos + scan_info->before;
2594 scan_info->list[idx].term = (char *)
2595 odr_malloc (scan_info->odr, strlen(name + len_prefix)+1);
2596 strcpy (scan_info->list[idx].term, name + len_prefix);
2597 assert (*info == sizeof(ISAMS_P));
2598 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAMS_P));
2602 static void scan_term_untrans (ZebraHandle zh, NMEM stream, int reg_type,
2603 char **dst, const char *src)
2605 char term_src[IT_MAX_WORD];
2606 char term_dst[IT_MAX_WORD];
2608 term_untrans (zh, reg_type, term_src, src);
2610 if (zh->iconv_from_utf8 != 0)
2613 char *inbuf = term_src;
2614 size_t inleft = strlen(term_src);
2615 char *outbuf = term_dst;
2616 size_t outleft = sizeof(term_dst)-1;
2619 ret = yaz_iconv (zh->iconv_from_utf8, &inbuf, &inleft,
2621 if (ret == (size_t)(-1))
2624 len = outbuf - term_dst;
2625 *dst = nmem_malloc (stream, len + 1);
2627 memcpy (*dst, term_dst, len);
2631 *dst = nmem_strdup (stream, term_src);
2634 static void count_set (RSET r, int *count)
2642 logf (LOG_DEBUG, "count_set");
2645 rfd = rset_open (r, RSETF_READ);
2646 while (rset_read (r, rfd, &key, &term_index))
2648 if (key.sysno != psysno)
2655 rset_close (r, rfd);
2656 logf (LOG_DEBUG, "%d keys, %d records", kno, *count);
2659 void rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
2660 oid_value attributeset,
2661 int num_bases, char **basenames,
2662 int *position, int *num_entries, ZebraScanEntry **list,
2666 int pos = *position;
2667 int num = *num_entries;
2671 char termz[IT_MAX_WORD+20];
2674 struct scan_info *scan_info_array;
2675 ZebraScanEntry *glist;
2676 int ords[32], ord_no = 0;
2680 char *search_type = NULL;
2681 char rank_type[128];
2686 if (attributeset == VAL_NONE)
2687 attributeset = VAL_BIB1;
2689 yaz_log (LOG_DEBUG, "position = %d, num = %d set=%d",
2690 pos, num, attributeset);
2692 attr_init (&use, zapt, 1);
2693 use_value = attr_find (&use, &attributeset);
2695 if (zebra_maps_attr (zh->reg->zebra_maps, zapt, ®_id, &search_type,
2696 rank_type, &complete_flag, &sort_flag))
2702 yaz_log (LOG_DEBUG, "use_value = %d", use_value);
2704 if (use_value == -1)
2706 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
2710 data1_local_attribute *local_attr;
2712 if ((r=att_getentbyatt (zh, &attp, attributeset, use_value)))
2714 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
2715 attributeset, use_value);
2719 sprintf (val_str, "%d", use_value);
2721 zh->errString = odr_strdup (stream, val_str);
2728 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
2730 zh->errString = basenames[base_no];
2731 zh->errCode = 109; /* Database unavailable */
2735 for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
2736 local_attr = local_attr->next)
2740 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
2743 ords[ord_no++] = ord;
2752 /* prepare dictionary scanning */
2755 scan_info_array = (struct scan_info *)
2756 odr_malloc (stream, ord_no * sizeof(*scan_info_array));
2757 for (i = 0; i < ord_no; i++)
2759 int j, prefix_len = 0;
2760 int before_tmp = before, after_tmp = after;
2761 struct scan_info *scan_info = scan_info_array + i;
2762 struct rpn_char_map_info rcmi;
2764 rpn_char_map_prepare (zh->reg, reg_id, &rcmi);
2766 scan_info->before = before;
2767 scan_info->after = after;
2768 scan_info->odr = stream;
2770 scan_info->list = (struct scan_info_entry *)
2771 odr_malloc (stream, (before+after) * sizeof(*scan_info->list));
2772 for (j = 0; j<before+after; j++)
2773 scan_info->list[j].term = NULL;
2775 prefix_len += key_SU_encode (ords[i], termz + prefix_len);
2776 termz[prefix_len++] = reg_id;
2777 termz[prefix_len] = 0;
2778 strcpy (scan_info->prefix, termz);
2780 if (trans_scan_term (zh, zapt, termz+prefix_len, reg_id))
2783 dict_scan (zh->reg->dict, termz, &before_tmp, &after_tmp,
2784 scan_info, scan_handle);
2786 glist = (ZebraScanEntry *)
2787 odr_malloc (stream, (before+after)*sizeof(*glist));
2789 /* consider terms after main term */
2790 for (i = 0; i < ord_no; i++)
2794 for (i = 0; i<after; i++)
2797 const char *mterm = NULL;
2801 for (j = 0; j < ord_no; j++)
2803 if (ptr[j] < before+after &&
2804 (tst=scan_info_array[j].list[ptr[j]].term) &&
2805 (!mterm || strcmp (tst, mterm) < 0))
2813 scan_term_untrans (zh, stream->mem, reg_id,
2814 &glist[i+before].term, mterm);
2815 rset = rset_trunc (zh, &scan_info_array[j0].list[ptr[j0]].isam_p, 1,
2816 glist[i+before].term, strlen(glist[i+before].term),
2817 NULL, 0, zapt->term->which);
2820 for (j = j0+1; j<ord_no; j++)
2822 if (ptr[j] < before+after &&
2823 (tst=scan_info_array[j].list[ptr[j]].term) &&
2824 !strcmp (tst, mterm))
2826 rset_bool_parms bool_parms;
2830 rset_trunc (zh, &scan_info_array[j].list[ptr[j]].isam_p, 1,
2831 glist[i+before].term,
2832 strlen(glist[i+before].term), NULL, 0,
2835 bool_parms.key_size = sizeof(struct it_key);
2836 bool_parms.cmp = key_compare_it;
2837 bool_parms.rset_l = rset;
2838 bool_parms.rset_r = rset2;
2840 rset = rset_create (rset_kind_or, &bool_parms);
2845 count_set (rset, &glist[i+before].occurrences);
2850 *num_entries -= (after-i);
2854 /* consider terms before main term */
2855 for (i = 0; i<ord_no; i++)
2858 for (i = 0; i<before; i++)
2861 const char *mterm = NULL;
2865 for (j = 0; j <ord_no; j++)
2867 if (ptr[j] < before &&
2868 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2869 (!mterm || strcmp (tst, mterm) > 0))
2878 scan_term_untrans (zh, stream->mem, reg_id,
2879 &glist[before-1-i].term, mterm);
2882 (zh, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1,
2883 glist[before-1-i].term, strlen(glist[before-1-i].term),
2884 NULL, 0, zapt->term->which);
2888 for (j = j0+1; j<ord_no; j++)
2890 if (ptr[j] < before &&
2891 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2892 !strcmp (tst, mterm))
2894 rset_bool_parms bool_parms;
2897 rset2 = rset_trunc (zh,
2898 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1,
2899 glist[before-1-i].term,
2900 strlen(glist[before-1-i].term), NULL, 0,
2903 bool_parms.key_size = sizeof(struct it_key);
2904 bool_parms.cmp = key_compare_it;
2905 bool_parms.rset_l = rset;
2906 bool_parms.rset_r = rset2;
2908 rset = rset_create (rset_kind_or, &bool_parms);
2913 count_set (rset, &glist[before-1-i].occurrences);
2923 *list = glist + i; /* list is set to first 'real' entry */
2925 logf (LOG_DEBUG, "position = %d, num_entries = %d",
2926 *position, *num_entries);
2928 logf (LOG_DEBUG, "scan error: %d", zh->errCode);