1 /* $Id: zrpn.c,v 1.186 2005-05-03 09:11:34 adam Exp $
2 Copyright (C) 1995-2005
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
32 #include <yaz/diagbib1.h>
34 #include <zebra_xpath.h>
39 struct rpn_char_map_info
50 Z_AttributesPlusTerm *zapt;
54 static int log_level_set = 0;
55 static int log_level_rpn = 0;
57 static const char **rpn_char_map_handler(void *vp, const char **from, int len)
59 struct rpn_char_map_info *p = (struct rpn_char_map_info *) vp;
60 const char **out = zebra_maps_input(p->zm, p->reg_type, from, len, 0);
64 const char *outp = *out;
65 yaz_log(YLOG_LOG, "---");
68 yaz_log(YLOG_LOG, "%02X", *outp);
76 static void rpn_char_map_prepare(struct zebra_register *reg, int reg_type,
77 struct rpn_char_map_info *map_info)
79 map_info->zm = reg->zebra_maps;
80 map_info->reg_type = reg_type;
81 dict_grep_cmap(reg->dict, map_info, rpn_char_map_handler);
84 static int attr_find_ex(AttrType *src, oid_value *attributeSetP,
85 const char **string_value)
89 num_attributes = src->zapt->attributes->num_attributes;
90 while (src->major < num_attributes)
92 Z_AttributeElement *element;
94 element = src->zapt->attributes->attributes[src->major];
95 if (src->type == *element->attributeType)
97 switch (element->which)
99 case Z_AttributeValue_numeric:
101 if (element->attributeSet && attributeSetP)
105 attrset = oid_getentbyoid(element->attributeSet);
106 *attributeSetP = attrset->value;
108 return *element->value.numeric;
110 case Z_AttributeValue_complex:
111 if (src->minor >= element->value.complex->num_list)
113 if (element->attributeSet && attributeSetP)
117 attrset = oid_getentbyoid(element->attributeSet);
118 *attributeSetP = attrset->value;
120 if (element->value.complex->list[src->minor]->which ==
121 Z_StringOrNumeric_numeric)
125 *element->value.complex->list[src->minor-1]->u.numeric;
127 else if (element->value.complex->list[src->minor]->which ==
128 Z_StringOrNumeric_string)
134 element->value.complex->list[src->minor-1]->u.string;
148 static int attr_find(AttrType *src, oid_value *attributeSetP)
150 return attr_find_ex(src, attributeSetP, 0);
153 static void attr_init(AttrType *src, Z_AttributesPlusTerm *zapt,
176 static void term_untrans(ZebraHandle zh, int reg_type,
177 char *dst, const char *src)
182 const char *cp = zebra_maps_output(zh->reg->zebra_maps,
184 if (!cp && len < IT_MAX_WORD-1)
187 while (*cp && len < IT_MAX_WORD-1)
193 static void add_isam_p(const char *name, const char *info,
198 log_level_rpn = yaz_log_module_level("rpn");
201 if (p->isam_p_indx == p->isam_p_size)
203 ISAM_P *new_isam_p_buf;
207 p->isam_p_size = 2*p->isam_p_size + 100;
208 new_isam_p_buf = (ISAM_P *) xmalloc(sizeof(*new_isam_p_buf) *
212 memcpy(new_isam_p_buf, p->isam_p_buf,
213 p->isam_p_indx * sizeof(*p->isam_p_buf));
214 xfree(p->isam_p_buf);
216 p->isam_p_buf = new_isam_p_buf;
219 new_term_no = (int *) xmalloc(sizeof(*new_term_no) * p->isam_p_size);
222 memcpy(new_term_no, p->isam_p_buf,
223 p->isam_p_indx * sizeof(*p->term_no));
226 p->term_no = new_term_no;
229 assert(*info == sizeof(*p->isam_p_buf));
230 memcpy(p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
237 char term_tmp[IT_MAX_WORD];
239 int len = key_SU_decode (&su_code, name);
241 term_untrans (p->zh, p->reg_type, term_tmp, name+len+1);
242 yaz_log(log_level_rpn, "grep: %d %c %s", su_code, name[len], term_tmp);
243 zebraExplain_lookup_ord (p->zh->reg->zei,
244 su_code, &db, &set, &use);
245 yaz_log(log_level_rpn, "grep: set=%d use=%d db=%s", set, use, db);
247 resultSetAddTerm(p->zh, p->termset, name[len], db,
254 static int grep_handle(char *name, const char *info, void *p)
256 add_isam_p(name, info, (struct grep_info *) p);
260 static int term_pre(ZebraMaps zebra_maps, int reg_type, const char **src,
261 const char *ct1, const char *ct2, int first)
263 const char *s1, *s0 = *src;
266 /* skip white space */
269 if (ct1 && strchr(ct1, *s0))
271 if (ct2 && strchr(ct2, *s0))
274 map = zebra_maps_input(zebra_maps, reg_type, &s1, strlen(s1), first);
275 if (**map != *CHR_SPACE)
284 static void esc_str(char *out_buf, int out_size,
285 const char *in_buf, int in_size)
291 assert(out_size > 20);
293 for (k = 0; k<in_size; k++)
295 int c = in_buf[k] & 0xff;
297 if (c < 32 || c > 126)
301 sprintf(out_buf +strlen(out_buf), "%02X:%c ", c, pc);
302 if (strlen(out_buf) > out_size-20)
304 strcat(out_buf, "..");
310 #define REGEX_CHARS " []()|.*+?!"
312 /* term_100: handle term, where trunc = none(no operators at all) */
313 static int term_100(ZebraMaps zebra_maps, int reg_type,
314 const char **src, char *dst, int space_split,
322 const char *space_start = 0;
323 const char *space_end = 0;
325 if (!term_pre(zebra_maps, reg_type, src, NULL, NULL, !space_split))
332 map = zebra_maps_search(zebra_maps, reg_type, &s0, strlen(s0),
336 if (**map == *CHR_SPACE)
339 else /* complete subfield only. */
341 if (**map == *CHR_SPACE)
342 { /* save space mapping for later .. */
347 else if (space_start)
348 { /* reload last space */
349 while (space_start < space_end)
351 if (strchr(REGEX_CHARS, *space_start))
353 dst_term[j++] = *space_start;
354 dst[i++] = *space_start++;
357 space_start = space_end = 0;
360 /* add non-space char */
361 memcpy(dst_term+j, s1, s0 - s1);
367 if (strchr(REGEX_CHARS, *s1))
375 esc_str(tmpbuf, sizeof(tmpbuf), map[0], strlen(map[0]));
377 strcpy(dst + i, map[0]);
387 /* term_101: handle term, where trunc = Process # */
388 static int term_101(ZebraMaps zebra_maps, int reg_type,
389 const char **src, char *dst, int space_split,
397 if (!term_pre(zebra_maps, reg_type, src, "#", "#", !space_split))
406 dst_term[j++] = *s0++;
412 map = zebra_maps_search(zebra_maps, reg_type, &s0, strlen(s0),
414 if (space_split && **map == *CHR_SPACE)
417 /* add non-space char */
418 memcpy(dst_term+j, s1, s0 - s1);
424 if (strchr(REGEX_CHARS, *s1))
432 esc_str(tmpbuf, sizeof(tmpbuf), map[0], strlen(map[0]));
434 strcpy(dst + i, map[0]);
440 dst_term[j++] = '\0';
445 /* term_103: handle term, where trunc = re-2 (regular expressions) */
446 static int term_103(ZebraMaps zebra_maps, int reg_type, const char **src,
447 char *dst, int *errors, int space_split,
455 if (!term_pre(zebra_maps, reg_type, src, "^\\()[].*+?|", "(", !space_split))
458 if (errors && *s0 == '+' && s0[1] && s0[2] == '+' && s0[3] &&
459 isdigit(((const unsigned char *)s0)[1]))
461 *errors = s0[1] - '0';
468 if (strchr("^\\()[].*+?|-", *s0))
477 map = zebra_maps_search(zebra_maps, reg_type, &s0, strlen(s0),
479 if (space_split && **map == *CHR_SPACE)
482 /* add non-space char */
483 memcpy(dst_term+j, s1, s0 - s1);
489 if (strchr(REGEX_CHARS, *s1))
497 esc_str(tmpbuf, sizeof(tmpbuf), map[0], strlen(map[0]));
499 strcpy(dst + i, map[0]);
511 /* term_103: handle term, where trunc = re-1 (regular expressions) */
512 static int term_102(ZebraMaps zebra_maps, int reg_type, const char **src,
513 char *dst, int space_split, char *dst_term)
515 return term_103(zebra_maps, reg_type, src, dst, NULL, space_split,
520 /* term_104: handle term, where trunc = Process # and ! */
521 static int term_104(ZebraMaps zebra_maps, int reg_type,
522 const char **src, char *dst, int space_split,
530 if (!term_pre(zebra_maps, reg_type, src, "?*#", "?*#", !space_split))
537 dst_term[j++] = *s0++;
538 if (*s0 >= '0' && *s0 <= '9')
541 while (*s0 >= '0' && *s0 <= '9')
543 limit = limit * 10 + (*s0 - '0');
544 dst_term[j++] = *s0++;
564 dst_term[j++] = *s0++;
569 dst_term[j++] = *s0++;
574 map = zebra_maps_input(zebra_maps, reg_type, &s0, strlen(s0), 0);
575 if (space_split && **map == *CHR_SPACE)
579 if (strchr(REGEX_CHARS, *s1))
587 dst_term[j++] = '\0';
592 /* term_105/106: handle term, where trunc = Process * and ! and right trunc */
593 static int term_105(ZebraMaps zebra_maps, int reg_type,
594 const char **src, char *dst, int space_split,
595 char *dst_term, int right_truncate)
602 if (!term_pre(zebra_maps, reg_type, src, "*!", "*!", !space_split))
611 dst_term[j++] = *s0++;
616 dst_term[j++] = *s0++;
621 map = zebra_maps_input(zebra_maps, reg_type, &s0, strlen(s0), 0);
622 if (space_split && **map == *CHR_SPACE)
626 if (strchr(REGEX_CHARS, *s1))
640 dst_term[j++] = '\0';
646 /* gen_regular_rel - generate regular expression from relation
647 * val: border value (inclusive)
648 * islt: 1 if <=; 0 if >=.
650 static void gen_regular_rel(char *dst, int val, int islt)
657 yaz_log(YLOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
661 strcpy(dst, "(-[0-9]+|(");
669 strcpy(dst, "([0-9]+|-(");
681 sprintf(numstr, "%d", val);
682 for (w = strlen(numstr); --w >= 0; pos++)
701 strcpy(dst + dst_p, numstr);
702 dst_p = strlen(dst) - pos - 1;
730 for (i = 0; i<pos; i++)
743 /* match everything less than 10^(pos-1) */
745 for (i = 1; i<pos; i++)
746 strcat(dst, "[0-9]?");
750 /* match everything greater than 10^pos */
751 for (i = 0; i <= pos; i++)
752 strcat(dst, "[0-9]");
753 strcat(dst, "[0-9]*");
758 void string_rel_add_char(char **term_p, const char *src, int *indx)
760 if (src[*indx] == '\\')
761 *(*term_p)++ = src[(*indx)++];
762 *(*term_p)++ = src[(*indx)++];
766 * > abc ([b-].*|a[c-].*|ab[d-].*|abc.+)
767 * ([^-a].*|a[^-b].*ab[^-c].*|abc.+)
768 * >= abc ([b-].*|a[c-].*|ab[c-].*)
769 * ([^-a].*|a[^-b].*|ab[c-].*)
770 * < abc ([-0].*|a[-a].*|ab[-b].*)
771 * ([^a-].*|a[^b-].*|ab[^c-].*)
772 * <= abc ([-0].*|a[-a].*|ab[-b].*|abc)
773 * ([^a-].*|a[^b-].*|ab[^c-].*|abc)
775 static int string_relation(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
776 const char **term_sub, char *term_dict,
777 oid_value attributeSet,
778 int reg_type, int space_split, char *term_dst,
784 char *term_tmp = term_dict + strlen(term_dict);
785 char term_component[2*IT_MAX_WORD+20];
787 attr_init(&relation, zapt, 2);
788 relation_value = attr_find(&relation, NULL);
791 yaz_log(YLOG_DEBUG, "string relation value=%d", relation_value);
792 switch (relation_value)
795 if (!term_100(zh->reg->zebra_maps, reg_type,
796 term_sub, term_component,
797 space_split, term_dst))
799 yaz_log(log_level_rpn, "Relation <");
802 for (i = 0; term_component[i]; )
809 string_rel_add_char(&term_tmp, term_component, &j);
814 string_rel_add_char(&term_tmp, term_component, &i);
821 if ((term_tmp - term_dict) > IT_MAX_WORD)
828 if (!term_100(zh->reg->zebra_maps, reg_type,
829 term_sub, term_component,
830 space_split, term_dst))
832 yaz_log(log_level_rpn, "Relation <=");
835 for (i = 0; term_component[i]; )
840 string_rel_add_char(&term_tmp, term_component, &j);
844 string_rel_add_char(&term_tmp, term_component, &i);
853 if ((term_tmp - term_dict) > IT_MAX_WORD)
856 for (i = 0; term_component[i]; )
857 string_rel_add_char(&term_tmp, term_component, &i);
862 if (!term_100 (zh->reg->zebra_maps, reg_type,
863 term_sub, term_component, space_split, term_dst))
865 yaz_log(log_level_rpn, "Relation >");
868 for (i = 0; term_component[i];)
873 string_rel_add_char(&term_tmp, term_component, &j);
878 string_rel_add_char(&term_tmp, term_component, &i);
886 if ((term_tmp - term_dict) > IT_MAX_WORD)
889 for (i = 0; term_component[i];)
890 string_rel_add_char(&term_tmp, term_component, &i);
897 if (!term_100(zh->reg->zebra_maps, reg_type, term_sub,
898 term_component, space_split, term_dst))
900 yaz_log(log_level_rpn, "Relation >=");
903 for (i = 0; term_component[i];)
910 string_rel_add_char(&term_tmp, term_component, &j);
913 if (term_component[i+1])
917 string_rel_add_char(&term_tmp, term_component, &i);
921 string_rel_add_char(&term_tmp, term_component, &i);
928 if ((term_tmp - term_dict) > IT_MAX_WORD)
937 yaz_log(log_level_rpn, "Relation =");
938 if (!term_100(zh->reg->zebra_maps, reg_type, term_sub,
939 term_component, space_split, term_dst))
941 strcat(term_tmp, "(");
942 strcat(term_tmp, term_component);
943 strcat(term_tmp, ")");
952 static ZEBRA_RES string_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
953 const char **term_sub,
954 oid_value attributeSet, NMEM stream,
955 struct grep_info *grep_info,
956 int reg_type, int complete_flag,
957 int num_bases, char **basenames,
958 char *term_dst, int xpath_use);
960 static ZEBRA_RES term_trunc(ZebraHandle zh,
961 Z_AttributesPlusTerm *zapt,
962 const char **term_sub,
963 oid_value attributeSet, NMEM stream,
964 struct grep_info *grep_info,
965 int reg_type, int complete_flag,
966 int num_bases, char **basenames,
968 const char *rank_type, int xpath_use,
971 struct rset_key_control *kc)
975 grep_info->isam_p_indx = 0;
976 res = string_term(zh, zapt, term_sub, attributeSet, stream, grep_info,
977 reg_type, complete_flag, num_bases, basenames,
978 term_dst, xpath_use);
981 if (!*term_sub) /* no more terms ? */
983 yaz_log(log_level_rpn, "term: %s", term_dst);
984 *rset = rset_trunc(zh, grep_info->isam_p_buf,
985 grep_info->isam_p_indx, term_dst,
986 strlen(term_dst), rank_type, 1 /* preserve pos */,
987 zapt->term->which, rset_nmem,
994 static char *nmem_strdup_i(NMEM nmem, int v)
997 sprintf(val_str, "%d", v);
998 return nmem_strdup(nmem, val_str);
1001 static ZEBRA_RES string_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1002 const char **term_sub,
1003 oid_value attributeSet, NMEM stream,
1004 struct grep_info *grep_info,
1005 int reg_type, int complete_flag,
1006 int num_bases, char **basenames,
1007 char *term_dst, int xpath_use)
1009 char term_dict[2*IT_MAX_WORD+4000];
1011 AttrType truncation;
1012 int truncation_value;
1015 const char *use_string = 0;
1016 oid_value curAttributeSet = attributeSet;
1018 struct rpn_char_map_info rcmi;
1019 int space_split = complete_flag ? 0 : 1;
1021 int bases_ok = 0; /* no of databases with OK attribute */
1022 int errCode = 0; /* err code (if any is not OK) */
1023 char *errString = 0; /* addinfo */
1025 rpn_char_map_prepare (zh->reg, reg_type, &rcmi);
1026 attr_init(&use, zapt, 1);
1027 use_value = attr_find_ex(&use, &curAttributeSet, &use_string);
1028 yaz_log(log_level_rpn, "string_term, use value %d", use_value);
1029 attr_init(&truncation, zapt, 5);
1030 truncation_value = attr_find(&truncation, NULL);
1031 yaz_log(log_level_rpn, "truncation value %d", truncation_value);
1033 if (use_value == -1) /* no attribute - assumy "any" */
1035 for (base_no = 0; base_no < num_bases; base_no++)
1039 int regex_range = 0;
1042 data1_local_attribute id_xpath_attr;
1043 data1_local_attribute *local_attr;
1044 int max_pos, prefix_len = 0;
1049 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
1051 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1052 zh->errString = basenames[base_no];
1055 if (xpath_use > 0 && use_value == -2)
1057 /* xpath mode and we have a string attribute */
1058 attp.local_attributes = &id_xpath_attr;
1059 attp.attset_ordinal = VAL_IDXPATH;
1060 id_xpath_attr.next = 0;
1062 use_value = xpath_use; /* xpath_use as use-attribute now */
1063 id_xpath_attr.local = use_value;
1065 else if (curAttributeSet == VAL_IDXPATH && use_value >= 0)
1067 /* X-Path attribute, use numeric value directly */
1068 attp.local_attributes = &id_xpath_attr;
1069 attp.attset_ordinal = VAL_IDXPATH;
1070 id_xpath_attr.next = 0;
1071 id_xpath_attr.local = use_value;
1073 else if (use_string &&
1074 (ord = zebraExplain_lookup_attr_str(zh->reg->zei,
1077 /* we have a match for a raw string attribute */
1082 term_dict[prefix_len++] = '|';
1084 term_dict[prefix_len++] = '(';
1086 ord_len = key_SU_encode (ord, ord_buf);
1087 for (i = 0; i<ord_len; i++)
1089 term_dict[prefix_len++] = 1;
1090 term_dict[prefix_len++] = ord_buf[i];
1092 attp.local_attributes = 0; /* no more attributes */
1096 /* lookup in the .att files . Allow string as well */
1097 if ((r = att_getentbyatt (zh, &attp, curAttributeSet, use_value,
1100 yaz_log(YLOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1101 curAttributeSet, use_value, r);
1104 /* set was found, but value wasn't defined */
1105 errCode = YAZ_BIB1_UNSUPP_USE_ATTRIBUTE;
1107 errString = nmem_strdup(stream, use_string);
1109 errString = nmem_strdup_i (stream, use_value);
1114 struct oident oident;
1116 oident.proto = PROTO_Z3950;
1117 oident.oclass = CLASS_ATTSET;
1118 oident.value = curAttributeSet;
1119 oid_ent_to_oid (&oident, oid);
1121 errCode = YAZ_BIB1_UNSUPP_ATTRIBUTE_SET;
1122 errString = nmem_strdup(stream, oident.desc);
1127 for (local_attr = attp.local_attributes; local_attr;
1128 local_attr = local_attr->next)
1133 ord = zebraExplain_lookup_attr_su(zh->reg->zei,
1134 attp.attset_ordinal,
1139 term_dict[prefix_len++] = '|';
1141 term_dict[prefix_len++] = '(';
1143 ord_len = key_SU_encode (ord, ord_buf);
1144 for (i = 0; i<ord_len; i++)
1146 term_dict[prefix_len++] = 1;
1147 term_dict[prefix_len++] = ord_buf[i];
1154 term_dict[prefix_len++] = ')';
1155 term_dict[prefix_len++] = 1;
1156 term_dict[prefix_len++] = reg_type;
1157 yaz_log(log_level_rpn, "reg_type = %d", term_dict[prefix_len-1]);
1158 term_dict[prefix_len] = '\0';
1160 switch (truncation_value)
1162 case -1: /* not specified */
1163 case 100: /* do not truncate */
1164 if (!string_relation (zh, zapt, &termp, term_dict,
1166 reg_type, space_split, term_dst,
1171 zh->errCode = relation_error;
1178 case 1: /* right truncation */
1179 term_dict[j++] = '(';
1180 if (!term_100(zh->reg->zebra_maps, reg_type,
1181 &termp, term_dict + j, space_split, term_dst))
1186 strcat(term_dict, ".*)");
1188 case 2: /* keft truncation */
1189 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1190 if (!term_100(zh->reg->zebra_maps, reg_type,
1191 &termp, term_dict + j, space_split, term_dst))
1196 strcat(term_dict, ")");
1198 case 3: /* left&right truncation */
1199 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1200 if (!term_100(zh->reg->zebra_maps, reg_type,
1201 &termp, term_dict + j, space_split, term_dst))
1206 strcat(term_dict, ".*)");
1208 case 101: /* process # in term */
1209 term_dict[j++] = '(';
1210 if (!term_101(zh->reg->zebra_maps, reg_type,
1211 &termp, term_dict + j, space_split, term_dst))
1216 strcat(term_dict, ")");
1218 case 102: /* Regexp-1 */
1219 term_dict[j++] = '(';
1220 if (!term_102(zh->reg->zebra_maps, reg_type,
1221 &termp, term_dict + j, space_split, term_dst))
1226 strcat(term_dict, ")");
1228 case 103: /* Regexp-2 */
1230 term_dict[j++] = '(';
1232 if (!term_103(zh->reg->zebra_maps, reg_type,
1233 &termp, term_dict + j, ®ex_range,
1234 space_split, term_dst))
1239 strcat(term_dict, ")");
1240 case 104: /* process # and ! in term */
1241 term_dict[j++] = '(';
1242 if (!term_104(zh->reg->zebra_maps, reg_type,
1243 &termp, term_dict + j, space_split, term_dst))
1248 strcat(term_dict, ")");
1250 case 105: /* process * and ! in term */
1251 term_dict[j++] = '(';
1252 if (!term_105(zh->reg->zebra_maps, reg_type,
1253 &termp, term_dict + j, space_split, term_dst, 1))
1258 strcat(term_dict, ")");
1260 case 106: /* process * and ! in term */
1261 term_dict[j++] = '(';
1262 if (!term_105(zh->reg->zebra_maps, reg_type,
1263 &termp, term_dict + j, space_split, term_dst, 0))
1268 strcat(term_dict, ")");
1271 zh->errCode = YAZ_BIB1_UNSUPP_TRUNCATION_ATTRIBUTE;
1272 zh->errString = nmem_strdup_i(stream, truncation_value);
1278 const char *input = term_dict + prefix_len;
1279 esc_str(buf, sizeof(buf), input, strlen(input));
1283 yaz_log(log_level_rpn, "dict_lookup_grep: %s", term_dict+prefix_len);
1284 r = dict_lookup_grep(zh->reg->dict, term_dict, regex_range,
1285 grep_info, &max_pos, init_pos,
1288 yaz_log(YLOG_WARN, "dict_lookup_grep fail %d", r);
1293 zh->errCode = errCode;
1294 zh->errString = errString;
1298 yaz_log(YLOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1303 /* convert APT search term to UTF8 */
1304 static ZEBRA_RES zapt_term_to_utf8(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1308 Z_Term *term = zapt->term;
1310 switch (term->which)
1312 case Z_Term_general:
1313 if (zh->iconv_to_utf8 != 0)
1315 char *inbuf = term->u.general->buf;
1316 size_t inleft = term->u.general->len;
1317 char *outbuf = termz;
1318 size_t outleft = IT_MAX_WORD-1;
1321 ret = yaz_iconv(zh->iconv_to_utf8, &inbuf, &inleft,
1323 if (ret == (size_t)(-1))
1325 ret = yaz_iconv(zh->iconv_to_utf8, 0, 0, 0, 0);
1327 YAZ_BIB1_QUERY_TERM_INCLUDES_CHARS_THAT_DO_NOT_TRANSLATE_INTO_;
1334 sizez = term->u.general->len;
1335 if (sizez > IT_MAX_WORD-1)
1336 sizez = IT_MAX_WORD-1;
1337 memcpy (termz, term->u.general->buf, sizez);
1338 termz[sizez] = '\0';
1341 case Z_Term_characterString:
1342 sizez = strlen(term->u.characterString);
1343 if (sizez > IT_MAX_WORD-1)
1344 sizez = IT_MAX_WORD-1;
1345 memcpy (termz, term->u.characterString, sizez);
1346 termz[sizez] = '\0';
1349 zh->errCode = YAZ_BIB1_UNSUPP_CODED_VALUE_FOR_TERM;
1355 /* convert APT SCAN term to internal cmap */
1356 static ZEBRA_RES trans_scan_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1357 char *termz, int reg_type)
1359 char termz0[IT_MAX_WORD];
1361 if (zapt_term_to_utf8(zh, zapt, termz0) == ZEBRA_FAIL)
1362 return ZEBRA_FAIL; /* error */
1366 const char *cp = (const char *) termz0;
1367 const char *cp_end = cp + strlen(cp);
1370 const char *space_map = NULL;
1373 while ((len = (cp_end - cp)) > 0)
1375 map = zebra_maps_input(zh->reg->zebra_maps, reg_type, &cp, len, 0);
1376 if (**map == *CHR_SPACE)
1381 for (src = space_map; *src; src++)
1384 for (src = *map; *src; src++)
1393 char *normalize_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1394 const char *termz, NMEM stream, unsigned reg_id)
1397 AttrType truncation;
1398 int truncation_value;
1401 attr_init(&truncation, zapt, 5);
1402 truncation_value = attr_find(&truncation, NULL);
1404 switch (truncation_value)
1424 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, ex_list,
1425 termz, strlen(termz));
1427 return nmem_strdup(stream, termz);
1430 char *buf = (char*) nmem_malloc(stream, wrbuf_len(wrbuf)+1);
1431 memcpy (buf, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1432 buf[wrbuf_len(wrbuf)] = '\0';
1437 static void grep_info_delete(struct grep_info *grep_info)
1440 xfree(grep_info->term_no);
1442 xfree(grep_info->isam_p_buf);
1445 static int grep_info_prepare(ZebraHandle zh,
1446 Z_AttributesPlusTerm *zapt,
1447 struct grep_info *grep_info,
1452 int termset_value_numeric;
1453 const char *termset_value_string;
1456 grep_info->term_no = 0;
1458 grep_info->isam_p_size = 0;
1459 grep_info->isam_p_buf = NULL;
1461 grep_info->reg_type = reg_type;
1462 grep_info->termset = 0;
1466 attr_init(&termset, zapt, 8);
1467 termset_value_numeric =
1468 attr_find_ex(&termset, NULL, &termset_value_string);
1469 if (termset_value_numeric != -1)
1472 const char *termset_name = 0;
1473 if (termset_value_numeric != -2)
1476 sprintf(resname, "%d", termset_value_numeric);
1477 termset_name = resname;
1480 termset_name = termset_value_string;
1481 yaz_log(log_level_rpn, "creating termset set %s", termset_name);
1482 grep_info->termset = resultSetAdd(zh, termset_name, 1);
1483 if (!grep_info->termset)
1485 zh->errCode = YAZ_BIB1_ILLEGAL_RESULT_SET_NAME;
1486 zh->errString = nmem_strdup(stream, termset_name);
1494 static ZEBRA_RES term_list_trunc(ZebraHandle zh,
1495 Z_AttributesPlusTerm *zapt,
1496 const char *termz_org,
1497 oid_value attributeSet,
1499 int reg_type, int complete_flag,
1500 const char *rank_type, int xpath_use,
1501 int num_bases, char **basenames,
1503 RSET **result_sets, int *num_result_sets,
1504 struct rset_key_control *kc)
1506 char term_dst[IT_MAX_WORD+1];
1507 struct grep_info grep_info;
1508 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1509 const char *termp = termz;
1512 *num_result_sets = 0;
1514 if (grep_info_prepare(zh, zapt, &grep_info, reg_type, stream))
1520 if (alloc_sets == *num_result_sets)
1523 RSET *rnew = (RSET *) nmem_malloc(stream, (alloc_sets+add) *
1526 memcpy(rnew, *result_sets, alloc_sets * sizeof(*rnew));
1527 alloc_sets = alloc_sets + add;
1528 *result_sets = rnew;
1530 res = term_trunc(zh, zapt, &termp, attributeSet,
1532 reg_type, complete_flag,
1533 num_bases, basenames,
1534 term_dst, rank_type,
1535 xpath_use, rset_nmem,
1536 &(*result_sets)[*num_result_sets],
1538 if (res != ZEBRA_OK)
1541 for (i = 0; i < *num_result_sets; i++)
1542 rset_delete((*result_sets)[i]);
1543 grep_info_delete (&grep_info);
1546 if ((*result_sets)[*num_result_sets] == 0)
1548 (*num_result_sets)++;
1550 grep_info_delete(&grep_info);
1554 static ZEBRA_RES rpn_search_APT_phrase(ZebraHandle zh,
1555 Z_AttributesPlusTerm *zapt,
1556 const char *termz_org,
1557 oid_value attributeSet,
1559 int reg_type, int complete_flag,
1560 const char *rank_type, int xpath_use,
1561 int num_bases, char **basenames,
1564 struct rset_key_control *kc)
1566 RSET *result_sets = 0;
1567 int num_result_sets = 0;
1569 term_list_trunc(zh, zapt, termz_org, attributeSet,
1570 stream, reg_type, complete_flag,
1571 rank_type, xpath_use,
1572 num_bases, basenames,
1574 &result_sets, &num_result_sets, kc);
1575 if (res != ZEBRA_OK)
1577 if (num_result_sets == 0)
1578 *rset = rsnull_create (rset_nmem, kc);
1579 else if (num_result_sets == 1)
1580 *rset = result_sets[0];
1582 *rset = rsprox_create(rset_nmem, kc, kc->scope,
1583 num_result_sets, result_sets,
1584 1 /* ordered */, 0 /* exclusion */,
1585 3 /* relation */, 1 /* distance */);
1591 static ZEBRA_RES rpn_search_APT_or_list(ZebraHandle zh,
1592 Z_AttributesPlusTerm *zapt,
1593 const char *termz_org,
1594 oid_value attributeSet,
1596 int reg_type, int complete_flag,
1597 const char *rank_type,
1599 int num_bases, char **basenames,
1602 struct rset_key_control *kc)
1604 RSET *result_sets = 0;
1605 int num_result_sets = 0;
1607 term_list_trunc(zh, zapt, termz_org, attributeSet,
1608 stream, reg_type, complete_flag,
1609 rank_type, xpath_use,
1610 num_bases, basenames,
1612 &result_sets, &num_result_sets, kc);
1613 if (res != ZEBRA_OK)
1615 if (num_result_sets == 0)
1616 *rset = rsnull_create (rset_nmem, kc);
1617 else if (num_result_sets == 1)
1618 *rset = result_sets[0];
1620 *rset = rsmulti_or_create(rset_nmem, kc, kc->scope,
1621 num_result_sets, result_sets);
1627 static ZEBRA_RES rpn_search_APT_and_list(ZebraHandle zh,
1628 Z_AttributesPlusTerm *zapt,
1629 const char *termz_org,
1630 oid_value attributeSet,
1632 int reg_type, int complete_flag,
1633 const char *rank_type,
1635 int num_bases, char **basenames,
1638 struct rset_key_control *kc)
1640 RSET *result_sets = 0;
1641 int num_result_sets = 0;
1643 term_list_trunc(zh, zapt, termz_org, attributeSet,
1644 stream, reg_type, complete_flag,
1645 rank_type, xpath_use,
1646 num_bases, basenames,
1648 &result_sets, &num_result_sets,
1650 if (res != ZEBRA_OK)
1652 if (num_result_sets == 0)
1653 *rset = rsnull_create (rset_nmem, kc);
1654 else if (num_result_sets == 1)
1655 *rset = result_sets[0];
1657 *rset = rsmulti_and_create(rset_nmem, kc, kc->scope,
1658 num_result_sets, result_sets);
1664 static int numeric_relation(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1665 const char **term_sub,
1667 oid_value attributeSet,
1668 struct grep_info *grep_info,
1678 char *term_tmp = term_dict + strlen(term_dict);
1681 attr_init(&relation, zapt, 2);
1682 relation_value = attr_find(&relation, NULL);
1684 yaz_log(log_level_rpn, "numeric relation value=%d", relation_value);
1686 if (!term_100(zh->reg->zebra_maps, reg_type, term_sub, term_tmp, 1,
1689 term_value = atoi (term_tmp);
1690 switch (relation_value)
1693 yaz_log(log_level_rpn, "Relation <");
1694 gen_regular_rel(term_tmp, term_value-1, 1);
1697 yaz_log(log_level_rpn, "Relation <=");
1698 gen_regular_rel(term_tmp, term_value, 1);
1701 yaz_log(log_level_rpn, "Relation >=");
1702 gen_regular_rel(term_tmp, term_value, 0);
1705 yaz_log(log_level_rpn, "Relation >");
1706 gen_regular_rel(term_tmp, term_value+1, 0);
1710 yaz_log(log_level_rpn, "Relation =");
1711 sprintf(term_tmp, "(0*%d)", term_value);
1717 yaz_log(log_level_rpn, "dict_lookup_grep: %s", term_tmp);
1718 r = dict_lookup_grep(zh->reg->dict, term_dict, 0, grep_info, max_pos,
1721 yaz_log(YLOG_WARN, "dict_lookup_grep fail, rel = gt: %d", r);
1722 yaz_log(log_level_rpn, "%d positions", grep_info->isam_p_indx);
1726 static ZEBRA_RES numeric_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1727 const char **term_sub,
1728 oid_value attributeSet,
1729 struct grep_info *grep_info,
1730 int reg_type, int complete_flag,
1731 int num_bases, char **basenames,
1732 char *term_dst, int xpath_use, NMEM stream)
1734 char term_dict[2*IT_MAX_WORD+2];
1738 const char *use_string = 0;
1739 oid_value curAttributeSet = attributeSet;
1741 struct rpn_char_map_info rcmi;
1743 int bases_ok = 0; /* no of databases with OK attribute */
1744 int errCode = 0; /* err code (if any is not OK) */
1745 char *errString = 0; /* addinfo */
1747 rpn_char_map_prepare (zh->reg, reg_type, &rcmi);
1748 attr_init(&use, zapt, 1);
1749 use_value = attr_find_ex(&use, &curAttributeSet, &use_string);
1751 if (use_value == -1)
1754 for (base_no = 0; base_no < num_bases; base_no++)
1757 data1_local_attribute id_xpath_attr;
1758 data1_local_attribute *local_attr;
1759 int max_pos, prefix_len = 0;
1760 int relation_error = 0;
1763 if (use_value == -2) /* string attribute (assume IDXPATH/any) */
1765 use_value = xpath_use;
1766 attp.local_attributes = &id_xpath_attr;
1767 attp.attset_ordinal = VAL_IDXPATH;
1768 id_xpath_attr.next = 0;
1769 id_xpath_attr.local = use_value;
1771 else if (curAttributeSet == VAL_IDXPATH)
1773 attp.local_attributes = &id_xpath_attr;
1774 attp.attset_ordinal = VAL_IDXPATH;
1775 id_xpath_attr.next = 0;
1776 id_xpath_attr.local = use_value;
1780 if ((r = att_getentbyatt (zh, &attp, curAttributeSet, use_value,
1783 yaz_log(YLOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1784 curAttributeSet, use_value, r);
1787 errCode = YAZ_BIB1_UNSUPP_USE_ATTRIBUTE;
1789 errString = nmem_strdup(stream, use_string);
1791 errString = nmem_strdup_i (stream, use_value);
1794 errCode = YAZ_BIB1_UNSUPP_ATTRIBUTE_SET;
1798 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
1800 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1801 zh->errString = basenames[base_no];
1804 for (local_attr = attp.local_attributes; local_attr;
1805 local_attr = local_attr->next)
1811 ord = zebraExplain_lookup_attr_su(zh->reg->zei,
1812 attp.attset_ordinal,
1817 term_dict[prefix_len++] = '|';
1819 term_dict[prefix_len++] = '(';
1821 ord_len = key_SU_encode (ord, ord_buf);
1822 for (i = 0; i<ord_len; i++)
1824 term_dict[prefix_len++] = 1;
1825 term_dict[prefix_len++] = ord_buf[i];
1830 errCode = YAZ_BIB1_UNSUPP_USE_ATTRIBUTE;
1831 errString = nmem_strdup_i(stream, use_value);
1835 term_dict[prefix_len++] = ')';
1836 term_dict[prefix_len++] = 1;
1837 term_dict[prefix_len++] = reg_type;
1838 yaz_log(YLOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1839 term_dict[prefix_len] = '\0';
1840 if (!numeric_relation(zh, zapt, &termp, term_dict,
1841 attributeSet, grep_info, &max_pos, reg_type,
1842 term_dst, &relation_error))
1846 zh->errCode = relation_error;
1856 zh->errCode = errCode;
1857 zh->errString = errString;
1861 yaz_log(YLOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1865 static ZEBRA_RES rpn_search_APT_numeric(ZebraHandle zh,
1866 Z_AttributesPlusTerm *zapt,
1868 oid_value attributeSet,
1870 int reg_type, int complete_flag,
1871 const char *rank_type, int xpath_use,
1872 int num_bases, char **basenames,
1875 struct rset_key_control *kc)
1877 char term_dst[IT_MAX_WORD+1];
1878 const char *termp = termz;
1879 RSET *result_sets = 0;
1880 int num_result_sets = 0;
1882 struct grep_info grep_info;
1885 yaz_log(log_level_rpn, "APT_numeric t='%s'", termz);
1886 if (grep_info_prepare(zh, zapt, &grep_info, reg_type, stream))
1890 if (alloc_sets == num_result_sets)
1893 RSET *rnew = (RSET *) nmem_malloc(stream, (alloc_sets+add) *
1896 memcpy(rnew, result_sets, alloc_sets * sizeof(*rnew));
1897 alloc_sets = alloc_sets + add;
1900 yaz_log(YLOG_DEBUG, "APT_numeric termp=%s", termp);
1901 grep_info.isam_p_indx = 0;
1902 res = numeric_term(zh, zapt, &termp, attributeSet, &grep_info,
1903 reg_type, complete_flag, num_bases, basenames,
1904 term_dst, xpath_use,
1906 if (res == ZEBRA_FAIL || termp == 0)
1908 yaz_log(YLOG_DEBUG, "term: %s", term_dst);
1909 result_sets[num_result_sets] =
1910 rset_trunc(zh, grep_info.isam_p_buf,
1911 grep_info.isam_p_indx, term_dst,
1912 strlen(term_dst), rank_type,
1913 0 /* preserve position */,
1914 zapt->term->which, rset_nmem,
1916 if (!result_sets[num_result_sets])
1920 grep_info_delete(&grep_info);
1924 for (i = 0; i<num_result_sets; i++)
1925 rset_delete(result_sets[i]);
1928 if (num_result_sets == 0)
1929 *rset = rsnull_create(rset_nmem, kc);
1930 if (num_result_sets == 1)
1931 *rset = result_sets[0];
1933 *rset = rsmulti_and_create(rset_nmem, kc, kc->scope,
1934 num_result_sets, result_sets);
1940 static ZEBRA_RES rpn_search_APT_local(ZebraHandle zh,
1941 Z_AttributesPlusTerm *zapt,
1943 oid_value attributeSet,
1945 const char *rank_type, NMEM rset_nmem,
1947 struct rset_key_control *kc)
1952 *rset = rstemp_create(rset_nmem, kc, kc->scope,
1953 res_get (zh->res, "setTmpDir"),0 );
1954 rsfd = rset_open(*rset, RSETF_WRITE);
1962 rset_write (rsfd, &key);
1967 static ZEBRA_RES rpn_sort_spec(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1968 oid_value attributeSet, NMEM stream,
1969 Z_SortKeySpecList *sort_sequence,
1970 const char *rank_type,
1972 struct rset_key_control *kc)
1975 int sort_relation_value;
1976 AttrType sort_relation_type;
1981 Z_AttributeElement *ae;
1986 attr_init(&sort_relation_type, zapt, 7);
1987 sort_relation_value = attr_find(&sort_relation_type, &attributeSet);
1989 attr_init(&use_type, zapt, 1);
1990 use_value = attr_find(&use_type, &attributeSet);
1992 if (!sort_sequence->specs)
1994 sort_sequence->num_specs = 10;
1995 sort_sequence->specs = (Z_SortKeySpec **)
1996 nmem_malloc(stream, sort_sequence->num_specs *
1997 sizeof(*sort_sequence->specs));
1998 for (i = 0; i<sort_sequence->num_specs; i++)
1999 sort_sequence->specs[i] = 0;
2001 if (zapt->term->which != Z_Term_general)
2004 i = atoi_n ((char *) zapt->term->u.general->buf,
2005 zapt->term->u.general->len);
2006 if (i >= sort_sequence->num_specs)
2008 sprintf(termz, "%d", i);
2010 oe.proto = PROTO_Z3950;
2011 oe.oclass = CLASS_ATTSET;
2012 oe.value = attributeSet;
2013 if (!oid_ent_to_oid (&oe, oid))
2016 sks = (Z_SortKeySpec *) nmem_malloc(stream, sizeof(*sks));
2017 sks->sortElement = (Z_SortElement *)
2018 nmem_malloc(stream, sizeof(*sks->sortElement));
2019 sks->sortElement->which = Z_SortElement_generic;
2020 sk = sks->sortElement->u.generic = (Z_SortKey *)
2021 nmem_malloc(stream, sizeof(*sk));
2022 sk->which = Z_SortKey_sortAttributes;
2023 sk->u.sortAttributes = (Z_SortAttributes *)
2024 nmem_malloc(stream, sizeof(*sk->u.sortAttributes));
2026 sk->u.sortAttributes->id = oid;
2027 sk->u.sortAttributes->list = (Z_AttributeList *)
2028 nmem_malloc(stream, sizeof(*sk->u.sortAttributes->list));
2029 sk->u.sortAttributes->list->num_attributes = 1;
2030 sk->u.sortAttributes->list->attributes = (Z_AttributeElement **)
2031 nmem_malloc(stream, sizeof(*sk->u.sortAttributes->list->attributes));
2032 ae = *sk->u.sortAttributes->list->attributes = (Z_AttributeElement *)
2033 nmem_malloc(stream, sizeof(**sk->u.sortAttributes->list->attributes));
2034 ae->attributeSet = 0;
2035 ae->attributeType = (int *)
2036 nmem_malloc(stream, sizeof(*ae->attributeType));
2037 *ae->attributeType = 1;
2038 ae->which = Z_AttributeValue_numeric;
2039 ae->value.numeric = (int *)
2040 nmem_malloc(stream, sizeof(*ae->value.numeric));
2041 *ae->value.numeric = use_value;
2043 sks->sortRelation = (int *)
2044 nmem_malloc(stream, sizeof(*sks->sortRelation));
2045 if (sort_relation_value == 1)
2046 *sks->sortRelation = Z_SortKeySpec_ascending;
2047 else if (sort_relation_value == 2)
2048 *sks->sortRelation = Z_SortKeySpec_descending;
2050 *sks->sortRelation = Z_SortKeySpec_ascending;
2052 sks->caseSensitivity = (int *)
2053 nmem_malloc(stream, sizeof(*sks->caseSensitivity));
2054 *sks->caseSensitivity = 0;
2056 sks->which = Z_SortKeySpec_null;
2057 sks->u.null = odr_nullval ();
2058 sort_sequence->specs[i] = sks;
2059 *rset = rsnull_create (NULL, kc);
2064 static int parse_xpath(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2065 oid_value attributeSet,
2066 struct xpath_location_step *xpath, int max, NMEM mem)
2068 oid_value curAttributeSet = attributeSet;
2070 const char *use_string = 0;
2072 attr_init(&use, zapt, 1);
2073 attr_find_ex(&use, &curAttributeSet, &use_string);
2075 if (!use_string || *use_string != '/')
2078 return zebra_parse_xpath_str(use_string, xpath, max, mem);
2083 static RSET xpath_trunc(ZebraHandle zh, NMEM stream,
2084 int reg_type, const char *term, int use,
2085 oid_value curAttributeSet, NMEM rset_nmem,
2086 struct rset_key_control *kc)
2089 struct grep_info grep_info;
2090 char term_dict[2048];
2093 int ord = zebraExplain_lookup_attr_su(zh->reg->zei, curAttributeSet, use);
2094 int ord_len, i, r, max_pos;
2095 int term_type = Z_Term_characterString;
2096 const char *flags = "void";
2098 if (grep_info_prepare(zh, 0 /* zapt */, &grep_info, '0', stream))
2099 return rsnull_create(rset_nmem, kc);
2102 return rsnull_create(rset_nmem, kc);
2104 term_dict[prefix_len++] = '|';
2106 term_dict[prefix_len++] = '(';
2108 ord_len = key_SU_encode (ord, ord_buf);
2109 for (i = 0; i<ord_len; i++)
2111 term_dict[prefix_len++] = 1;
2112 term_dict[prefix_len++] = ord_buf[i];
2114 term_dict[prefix_len++] = ')';
2115 term_dict[prefix_len++] = 1;
2116 term_dict[prefix_len++] = reg_type;
2118 strcpy(term_dict+prefix_len, term);
2120 grep_info.isam_p_indx = 0;
2121 r = dict_lookup_grep(zh->reg->dict, term_dict, 0,
2122 &grep_info, &max_pos, 0, grep_handle);
2123 yaz_log(YLOG_DEBUG, "%s %d positions", term,
2124 grep_info.isam_p_indx);
2125 rset = rset_trunc(zh, grep_info.isam_p_buf,
2126 grep_info.isam_p_indx, term, strlen(term),
2127 flags, 1, term_type,rset_nmem,
2129 grep_info_delete(&grep_info);
2133 static RSET rpn_search_xpath(ZebraHandle zh,
2134 oid_value attributeSet,
2135 int num_bases, char **basenames,
2136 NMEM stream, const char *rank_type, RSET rset,
2137 int xpath_len, struct xpath_location_step *xpath,
2139 struct rset_key_control *kc)
2141 oid_value curAttributeSet = attributeSet;
2148 yaz_log(YLOG_DEBUG, "xpath len=%d", xpath_len);
2149 for (i = 0; i<xpath_len; i++)
2151 yaz_log(log_level_rpn, "XPATH %d %s", i, xpath[i].part);
2155 curAttributeSet = VAL_IDXPATH;
2165 a[@attr = value]/b[@other = othervalue]
2167 /e/@a val range(e/,range(@a,freetext(w,1015,val),@a),e/)
2168 /a/b val range(b/a/,freetext(w,1016,val),b/a/)
2169 /a/b/@c val range(b/a/,range(@c,freetext(w,1016,val),@c),b/a/)
2170 /a/b[@c = y] val range(b/a/,freetext(w,1016,val),b/a/,@c = y)
2171 /a[@c = y]/b val range(a/,range(b/a/,freetext(w,1016,val),b/a/),a/,@c = y)
2172 /a[@c = x]/b[@c = y] range(a/,range(b/a/,freetext(w,1016,val),b/a/,@c = y),a/,@c = x)
2176 dict_grep_cmap (zh->reg->dict, 0, 0);
2178 for (base_no = 0; base_no < num_bases; base_no++)
2180 int level = xpath_len;
2183 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
2185 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
2186 zh->errString = basenames[base_no];
2189 while (--level >= 0)
2191 char xpath_rev[128];
2193 RSET rset_start_tag = 0, rset_end_tag = 0, rset_attr = 0;
2197 for (i = level; i >= 1; --i)
2199 const char *cp = xpath[i].part;
2205 memcpy (xpath_rev + len, "[^/]*", 5);
2208 else if (*cp == ' ')
2211 xpath_rev[len++] = 1;
2212 xpath_rev[len++] = ' ';
2216 xpath_rev[len++] = *cp;
2217 xpath_rev[len++] = '/';
2219 else if (i == 1) /* // case */
2221 xpath_rev[len++] = '.';
2222 xpath_rev[len++] = '*';
2227 if (xpath[level].predicate &&
2228 xpath[level].predicate->which == XPATH_PREDICATE_RELATION &&
2229 xpath[level].predicate->u.relation.name[0])
2231 WRBUF wbuf = wrbuf_alloc();
2232 wrbuf_puts(wbuf, xpath[level].predicate->u.relation.name+1);
2233 if (xpath[level].predicate->u.relation.value)
2235 const char *cp = xpath[level].predicate->u.relation.value;
2236 wrbuf_putc(wbuf, '=');
2240 if (strchr(REGEX_CHARS, *cp))
2241 wrbuf_putc(wbuf, '\\');
2242 wrbuf_putc(wbuf, *cp);
2246 wrbuf_puts(wbuf, "");
2247 rset_attr = xpath_trunc(
2248 zh, stream, '0', wrbuf_buf(wbuf), 3,
2249 curAttributeSet, rset_nmem, kc);
2250 wrbuf_free(wbuf, 1);
2257 yaz_log(log_level_rpn, "xpath_rev (%d) = %s", level, xpath_rev);
2258 if (strlen(xpath_rev))
2260 rset_start_tag = xpath_trunc(zh, stream, '0',
2261 xpath_rev, 1, curAttributeSet, rset_nmem, kc);
2263 rset_end_tag = xpath_trunc(zh, stream, '0',
2264 xpath_rev, 2, curAttributeSet, rset_nmem, kc);
2266 rset = rsbetween_create(rset_nmem, kc, kc->scope,
2267 rset_start_tag, rset,
2268 rset_end_tag, rset_attr);
2277 static ZEBRA_RES rpn_search_APT(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2278 oid_value attributeSet, NMEM stream,
2279 Z_SortKeySpecList *sort_sequence,
2280 int num_bases, char **basenames,
2283 struct rset_key_control *kc)
2285 ZEBRA_RES res = ZEBRA_OK;
2287 char *search_type = NULL;
2288 char rank_type[128];
2291 char termz[IT_MAX_WORD+1];
2294 struct xpath_location_step xpath[10];
2298 log_level_rpn = yaz_log_module_level("rpn");
2301 zebra_maps_attr(zh->reg->zebra_maps, zapt, ®_id, &search_type,
2302 rank_type, &complete_flag, &sort_flag);
2304 yaz_log(YLOG_DEBUG, "reg_id=%c", reg_id);
2305 yaz_log(YLOG_DEBUG, "complete_flag=%d", complete_flag);
2306 yaz_log(YLOG_DEBUG, "search_type=%s", search_type);
2307 yaz_log(YLOG_DEBUG, "rank_type=%s", rank_type);
2309 if (zapt_term_to_utf8(zh, zapt, termz) == ZEBRA_FAIL)
2313 return rpn_sort_spec(zh, zapt, attributeSet, stream, sort_sequence,
2314 rank_type, rset, kc);
2315 xpath_len = parse_xpath(zh, zapt, attributeSet, xpath, 10, stream);
2319 if (xpath[xpath_len-1].part[0] == '@')
2323 if (!strcmp(search_type, "phrase"))
2325 res = rpn_search_APT_phrase(zh, zapt, termz, attributeSet, stream,
2326 reg_id, complete_flag, rank_type,
2328 num_bases, basenames, rset_nmem,
2331 else if (!strcmp(search_type, "and-list"))
2333 res = rpn_search_APT_and_list(zh, zapt, termz, attributeSet, stream,
2334 reg_id, complete_flag, rank_type,
2336 num_bases, basenames, rset_nmem,
2339 else if (!strcmp(search_type, "or-list"))
2341 res = rpn_search_APT_or_list(zh, zapt, termz, attributeSet, stream,
2342 reg_id, complete_flag, rank_type,
2344 num_bases, basenames, rset_nmem,
2347 else if (!strcmp(search_type, "local"))
2349 res = rpn_search_APT_local(zh, zapt, termz, attributeSet, stream,
2350 rank_type, rset_nmem, rset, kc);
2352 else if (!strcmp(search_type, "numeric"))
2354 res = rpn_search_APT_numeric(zh, zapt, termz, attributeSet, stream,
2355 reg_id, complete_flag, rank_type,
2357 num_bases, basenames, rset_nmem,
2362 zh->errCode = YAZ_BIB1_UNSUPP_STRUCTURE_ATTRIBUTE;
2365 if (res != ZEBRA_OK)
2369 *rset = rpn_search_xpath(zh, attributeSet, num_bases, basenames,
2370 stream, rank_type, *rset,
2371 xpath_len, xpath, rset_nmem, kc);
2377 static ZEBRA_RES rpn_search_structure(ZebraHandle zh, Z_RPNStructure *zs,
2378 oid_value attributeSet,
2379 NMEM stream, NMEM rset_nmem,
2380 Z_SortKeySpecList *sort_sequence,
2381 int num_bases, char **basenames,
2382 RSET **result_sets, int *num_result_sets,
2383 Z_Operator *parent_op,
2384 struct rset_key_control *kc);
2386 ZEBRA_RES rpn_search_top(ZebraHandle zh, Z_RPNStructure *zs,
2387 oid_value attributeSet,
2388 NMEM stream, NMEM rset_nmem,
2389 Z_SortKeySpecList *sort_sequence,
2390 int num_bases, char **basenames,
2393 RSET *result_sets = 0;
2394 int num_result_sets = 0;
2396 struct rset_key_control *kc = zebra_key_control_create(zh);
2398 res = rpn_search_structure(zh, zs, attributeSet,
2401 num_bases, basenames,
2402 &result_sets, &num_result_sets,
2403 0 /* no parent op */,
2405 if (res != ZEBRA_OK)
2408 for (i = 0; i<num_result_sets; i++)
2409 rset_delete(result_sets[i]);
2413 assert(num_result_sets == 1);
2414 assert(result_sets);
2415 assert(*result_sets);
2416 *result_set = *result_sets;
2422 ZEBRA_RES rpn_search_structure(ZebraHandle zh, Z_RPNStructure *zs,
2423 oid_value attributeSet,
2424 NMEM stream, NMEM rset_nmem,
2425 Z_SortKeySpecList *sort_sequence,
2426 int num_bases, char **basenames,
2427 RSET **result_sets, int *num_result_sets,
2428 Z_Operator *parent_op,
2429 struct rset_key_control *kc)
2431 *num_result_sets = 0;
2432 if (zs->which == Z_RPNStructure_complex)
2435 Z_Operator *zop = zs->u.complex->roperator;
2436 RSET *result_sets_l = 0;
2437 int num_result_sets_l = 0;
2438 RSET *result_sets_r = 0;
2439 int num_result_sets_r = 0;
2441 res = rpn_search_structure(zh, zs->u.complex->s1,
2442 attributeSet, stream, rset_nmem,
2444 num_bases, basenames,
2445 &result_sets_l, &num_result_sets_l,
2447 if (res != ZEBRA_OK)
2450 for (i = 0; i<num_result_sets_l; i++)
2451 rset_delete(result_sets_l[i]);
2454 res = rpn_search_structure(zh, zs->u.complex->s2,
2455 attributeSet, stream, rset_nmem,
2457 num_bases, basenames,
2458 &result_sets_r, &num_result_sets_r,
2460 if (res != ZEBRA_OK)
2463 for (i = 0; i<num_result_sets_l; i++)
2464 rset_delete(result_sets_l[i]);
2465 for (i = 0; i<num_result_sets_r; i++)
2466 rset_delete(result_sets_r[i]);
2470 /* make a new list of result for all children */
2471 *num_result_sets = num_result_sets_l + num_result_sets_r;
2472 *result_sets = nmem_malloc(stream, *num_result_sets *
2473 sizeof(**result_sets));
2474 memcpy(*result_sets, result_sets_l,
2475 num_result_sets_l * sizeof(**result_sets));
2476 memcpy(*result_sets + num_result_sets_l, result_sets_r,
2477 num_result_sets_r * sizeof(**result_sets));
2479 if (!parent_op || parent_op->which != zop->which
2480 || (zop->which != Z_Operator_and &&
2481 zop->which != Z_Operator_or))
2483 /* parent node different from this one (or non-present) */
2484 /* we must combine result sets now */
2488 case Z_Operator_and:
2489 rset = rsmulti_and_create(rset_nmem, kc,
2491 *num_result_sets, *result_sets);
2494 rset = rsmulti_or_create(rset_nmem, kc,
2496 *num_result_sets, *result_sets);
2498 case Z_Operator_and_not:
2499 rset = rsbool_create_not(rset_nmem, kc,
2504 case Z_Operator_prox:
2505 if (zop->u.prox->which != Z_ProximityOperator_known)
2507 zh->errCode = YAZ_BIB1_UNSUPP_PROX_UNIT_CODE;
2510 if (*zop->u.prox->u.known != Z_ProxUnit_word)
2512 char *val = (char *) nmem_malloc(stream, 16);
2513 zh->errCode = YAZ_BIB1_UNSUPP_PROX_UNIT_CODE;
2514 zh->errString = val;
2515 sprintf(val, "%d", *zop->u.prox->u.known);
2520 rset = rsprox_create(rset_nmem, kc,
2522 *num_result_sets, *result_sets,
2523 *zop->u.prox->ordered,
2524 (!zop->u.prox->exclusion ?
2525 0 : *zop->u.prox->exclusion),
2526 *zop->u.prox->relationType,
2527 *zop->u.prox->distance );
2531 zh->errCode = YAZ_BIB1_OPERATOR_UNSUPP;
2534 *num_result_sets = 1;
2535 *result_sets = nmem_malloc(stream, *num_result_sets *
2536 sizeof(**result_sets));
2537 (*result_sets)[0] = rset;
2540 else if (zs->which == Z_RPNStructure_simple)
2545 if (zs->u.simple->which == Z_Operand_APT)
2547 yaz_log(YLOG_DEBUG, "rpn_search_APT");
2548 res = rpn_search_APT(zh, zs->u.simple->u.attributesPlusTerm,
2549 attributeSet, stream, sort_sequence,
2550 num_bases, basenames, rset_nmem, &rset,
2552 if (res != ZEBRA_OK)
2555 else if (zs->u.simple->which == Z_Operand_resultSetId)
2557 yaz_log(YLOG_DEBUG, "rpn_search_ref");
2558 rset = resultSetRef(zh, zs->u.simple->u.resultSetId);
2561 zh->errCode = YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST;
2563 nmem_strdup(stream, zs->u.simple->u.resultSetId);
2570 zh->errCode = YAZ_BIB1_UNSUPP_SEARCH;
2573 *num_result_sets = 1;
2574 *result_sets = nmem_malloc(stream, *num_result_sets *
2575 sizeof(**result_sets));
2576 (*result_sets)[0] = rset;
2580 zh->errCode = YAZ_BIB1_UNSUPP_SEARCH;
2586 struct scan_info_entry {
2592 struct scan_info_entry *list;
2598 static int scan_handle (char *name, const char *info, int pos, void *client)
2600 int len_prefix, idx;
2601 struct scan_info *scan_info = (struct scan_info *) client;
2603 len_prefix = strlen(scan_info->prefix);
2604 if (memcmp (name, scan_info->prefix, len_prefix))
2607 idx = scan_info->after - pos + scan_info->before;
2613 scan_info->list[idx].term = (char *)
2614 odr_malloc(scan_info->odr, strlen(name + len_prefix)+1);
2615 strcpy(scan_info->list[idx].term, name + len_prefix);
2616 assert (*info == sizeof(ISAM_P));
2617 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAM_P));
2621 static void scan_term_untrans (ZebraHandle zh, NMEM stream, int reg_type,
2622 char **dst, const char *src)
2624 char term_src[IT_MAX_WORD];
2625 char term_dst[IT_MAX_WORD];
2627 term_untrans (zh, reg_type, term_src, src);
2629 if (zh->iconv_from_utf8 != 0)
2632 char *inbuf = term_src;
2633 size_t inleft = strlen(term_src);
2634 char *outbuf = term_dst;
2635 size_t outleft = sizeof(term_dst)-1;
2638 ret = yaz_iconv (zh->iconv_from_utf8, &inbuf, &inleft,
2640 if (ret == (size_t)(-1))
2643 len = outbuf - term_dst;
2644 *dst = nmem_malloc(stream, len + 1);
2646 memcpy (*dst, term_dst, len);
2650 *dst = nmem_strdup(stream, term_src);
2653 static void count_set (RSET r, int *count)
2660 yaz_log(YLOG_DEBUG, "count_set");
2663 rfd = rset_open (r, RSETF_READ);
2664 while (rset_read (rfd, &key,0 /* never mind terms */))
2666 if (key.mem[0] != psysno)
2668 psysno = key.mem[0];
2674 yaz_log(YLOG_DEBUG, "%d keys, %d records", kno, *count);
2677 ZEBRA_RES rpn_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
2678 oid_value attributeset,
2679 int num_bases, char **basenames,
2680 int *position, int *num_entries, ZebraScanEntry **list,
2681 int *is_partial, RSET limit_set, int return_zero)
2684 int pos = *position;
2685 int num = *num_entries;
2689 char termz[IT_MAX_WORD+20];
2692 const char *use_string = 0;
2693 struct scan_info *scan_info_array;
2694 ZebraScanEntry *glist;
2695 int ords[32], ord_no = 0;
2698 int bases_ok = 0; /* no of databases with OK attribute */
2699 int errCode = 0; /* err code (if any is not OK) */
2700 char *errString = 0; /* addinfo */
2703 char *search_type = NULL;
2704 char rank_type[128];
2707 NMEM rset_nmem = NULL;
2708 struct rset_key_control *kc = 0;
2713 if (attributeset == VAL_NONE)
2714 attributeset = VAL_BIB1;
2719 int termset_value_numeric;
2720 const char *termset_value_string;
2721 attr_init(&termset, zapt, 8);
2722 termset_value_numeric =
2723 attr_find_ex(&termset, NULL, &termset_value_string);
2724 if (termset_value_numeric != -1)
2727 const char *termset_name = 0;
2729 if (termset_value_numeric != -2)
2732 sprintf(resname, "%d", termset_value_numeric);
2733 termset_name = resname;
2736 termset_name = termset_value_string;
2738 limit_set = resultSetRef (zh, termset_name);
2742 yaz_log(YLOG_DEBUG, "position = %d, num = %d set=%d",
2743 pos, num, attributeset);
2745 attr_init(&use, zapt, 1);
2746 use_value = attr_find_ex(&use, &attributeset, &use_string);
2748 if (zebra_maps_attr(zh->reg->zebra_maps, zapt, ®_id, &search_type,
2749 rank_type, &complete_flag, &sort_flag))
2752 zh->errCode = YAZ_BIB1_UNSUPP_ATTRIBUTE_TYPE;
2755 yaz_log(YLOG_DEBUG, "use_value = %d", use_value);
2757 if (use_value == -1)
2759 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
2761 data1_local_attribute *local_attr;
2765 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
2767 zh->errString = basenames[base_no];
2768 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
2774 (ord = zebraExplain_lookup_attr_str(zh->reg->zei,
2777 /* we have a match for a raw string attribute */
2779 ords[ord_no++] = ord;
2780 attp.local_attributes = 0; /* no more attributes */
2786 if ((r = att_getentbyatt (zh, &attp, attributeset, use_value,
2789 yaz_log(YLOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
2790 attributeset, use_value);
2793 errCode = YAZ_BIB1_UNSUPP_USE_ATTRIBUTE;
2795 errString = odr_strdup(stream, use_string);
2799 sprintf(val_str, "%d", use_value);
2800 errString = odr_strdup(stream, val_str);
2804 errCode = YAZ_BIB1_UNSUPP_ATTRIBUTE_SET;
2809 for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
2810 local_attr = local_attr->next)
2812 ord = zebraExplain_lookup_attr_su(zh->reg->zei,
2813 attp.attset_ordinal,
2816 ords[ord_no++] = ord;
2819 if (!bases_ok && errCode)
2821 zh->errCode = errCode;
2822 zh->errString = errString;
2831 /* prepare dictionary scanning */
2843 yaz_log(YLOG_DEBUG, "rpn_scan pos=%d num=%d before=%d "
2844 "after=%d before+after=%d",
2845 pos, num, before, after, before+after);
2846 scan_info_array = (struct scan_info *)
2847 odr_malloc(stream, ord_no * sizeof(*scan_info_array));
2848 for (i = 0; i < ord_no; i++)
2850 int j, prefix_len = 0;
2851 int before_tmp = before, after_tmp = after;
2852 struct scan_info *scan_info = scan_info_array + i;
2853 struct rpn_char_map_info rcmi;
2855 rpn_char_map_prepare (zh->reg, reg_id, &rcmi);
2857 scan_info->before = before;
2858 scan_info->after = after;
2859 scan_info->odr = stream;
2861 scan_info->list = (struct scan_info_entry *)
2862 odr_malloc(stream, (before+after) * sizeof(*scan_info->list));
2863 for (j = 0; j<before+after; j++)
2864 scan_info->list[j].term = NULL;
2866 prefix_len += key_SU_encode (ords[i], termz + prefix_len);
2867 termz[prefix_len++] = reg_id;
2868 termz[prefix_len] = 0;
2869 strcpy(scan_info->prefix, termz);
2871 if (trans_scan_term(zh, zapt, termz+prefix_len, reg_id) == ZEBRA_FAIL)
2874 dict_scan(zh->reg->dict, termz, &before_tmp, &after_tmp,
2875 scan_info, scan_handle);
2877 glist = (ZebraScanEntry *)
2878 odr_malloc(stream, (before+after)*sizeof(*glist));
2880 rset_nmem = nmem_create();
2881 kc = zebra_key_control_create(zh);
2883 /* consider terms after main term */
2884 for (i = 0; i < ord_no; i++)
2888 for (i = 0; i<after; i++)
2891 const char *mterm = NULL;
2894 int lo = i + pos-1; /* offset in result list */
2896 /* find: j0 is the first of the minimal values */
2897 for (j = 0; j < ord_no; j++)
2899 if (ptr[j] < before+after && ptr[j] >= 0 &&
2900 (tst = scan_info_array[j].list[ptr[j]].term) &&
2901 (!mterm || strcmp (tst, mterm) < 0))
2908 break; /* no value found, stop */
2910 /* get result set for first one , but only if it's within bounds */
2913 /* get result set for first term */
2914 scan_term_untrans(zh, stream->mem, reg_id,
2915 &glist[lo].term, mterm);
2916 rset = rset_trunc(zh, &scan_info_array[j0].list[ptr[j0]].isam_p, 1,
2917 glist[lo].term, strlen(glist[lo].term),
2918 NULL, 0, zapt->term->which, rset_nmem,
2921 ptr[j0]++; /* move index for this set .. */
2922 /* get result set for remaining scan terms */
2923 for (j = j0+1; j<ord_no; j++)
2925 if (ptr[j] < before+after && ptr[j] >= 0 &&
2926 (tst = scan_info_array[j].list[ptr[j]].term) &&
2927 !strcmp (tst, mterm))
2936 zh, &scan_info_array[j].list[ptr[j]].isam_p, 1,
2938 strlen(glist[lo].term), NULL, 0,
2939 zapt->term->which,rset_nmem,
2941 rset = rsmulti_or_create(rset_nmem, kc,
2942 2, kc->scope, rsets);
2949 /* merge with limit_set if given */
2954 rsets[1] = rset_dup(limit_set);
2956 rset = rsmulti_and_create(rset_nmem, kc,
2957 kc->scope, 2, rsets);
2960 count_set(rset, &glist[lo].occurrences);
2966 *num_entries -= (after-i);
2968 if (*num_entries < 0)
2971 nmem_destroy(rset_nmem);
2976 /* consider terms before main term */
2977 for (i = 0; i<ord_no; i++)
2980 for (i = 0; i<before; i++)
2983 const char *mterm = NULL;
2986 int lo = before-1-i; /* offset in result list */
2988 for (j = 0; j <ord_no; j++)
2990 if (ptr[j] < before && ptr[j] >= 0 &&
2991 (tst = scan_info_array[j].list[before-1-ptr[j]].term) &&
2992 (!mterm || strcmp (tst, mterm) > 0))
3001 scan_term_untrans (zh, stream->mem, reg_id,
3002 &glist[lo].term, mterm);
3005 (zh, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1,
3006 glist[lo].term, strlen(glist[lo].term),
3007 NULL, 0, zapt->term->which,rset_nmem,
3012 for (j = j0+1; j<ord_no; j++)
3014 if (ptr[j] < before && ptr[j] >= 0 &&
3015 (tst = scan_info_array[j].list[before-1-ptr[j]].term) &&
3016 !strcmp (tst, mterm))
3021 rsets[1] = rset_trunc(
3023 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1,
3025 strlen(glist[lo].term), NULL, 0,
3026 zapt->term->which, rset_nmem,
3028 rset = rsmulti_or_create(rset_nmem, kc,
3029 2, kc->scope, rsets);
3038 rsets[1] = rset_dup(limit_set);
3040 rset = rsmulti_and_create(rset_nmem, kc,
3041 kc->scope, 2, rsets);
3043 count_set (rset, &glist[lo].occurrences);
3047 nmem_destroy(rset_nmem);
3054 if (*num_entries <= 0)
3061 *list = glist + i; /* list is set to first 'real' entry */
3063 yaz_log(YLOG_DEBUG, "position = %d, num_entries = %d",
3064 *position, *num_entries);