1 /* $Id: zrpn.c,v 1.189 2005-05-04 10:48:39 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++;
575 map = zebra_maps_search(zebra_maps, reg_type, &s0, strlen(s0),
577 if (space_split && **map == *CHR_SPACE)
580 /* add non-space char */
581 memcpy(dst_term+j, s1, s0 - s1);
587 if (strchr(REGEX_CHARS, *s1))
595 esc_str(tmpbuf, sizeof(tmpbuf), map[0], strlen(map[0]));
597 strcpy(dst + i, map[0]);
603 dst_term[j++] = '\0';
608 /* term_105/106: handle term, where trunc = Process * and ! and right trunc */
609 static int term_105(ZebraMaps zebra_maps, int reg_type,
610 const char **src, char *dst, int space_split,
611 char *dst_term, int right_truncate)
618 if (!term_pre(zebra_maps, reg_type, src, "*!", "*!", !space_split))
627 dst_term[j++] = *s0++;
632 dst_term[j++] = *s0++;
638 map = zebra_maps_search(zebra_maps, reg_type, &s0, strlen(s0),
640 if (space_split && **map == *CHR_SPACE)
643 /* add non-space char */
644 memcpy(dst_term+j, s1, s0 - s1);
650 if (strchr(REGEX_CHARS, *s1))
658 esc_str(tmpbuf, sizeof(tmpbuf), map[0], strlen(map[0]));
660 strcpy(dst + i, map[0]);
672 dst_term[j++] = '\0';
678 /* gen_regular_rel - generate regular expression from relation
679 * val: border value (inclusive)
680 * islt: 1 if <=; 0 if >=.
682 static void gen_regular_rel(char *dst, int val, int islt)
689 yaz_log(YLOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
693 strcpy(dst, "(-[0-9]+|(");
701 strcpy(dst, "([0-9]+|-(");
713 sprintf(numstr, "%d", val);
714 for (w = strlen(numstr); --w >= 0; pos++)
733 strcpy(dst + dst_p, numstr);
734 dst_p = strlen(dst) - pos - 1;
762 for (i = 0; i<pos; i++)
775 /* match everything less than 10^(pos-1) */
777 for (i = 1; i<pos; i++)
778 strcat(dst, "[0-9]?");
782 /* match everything greater than 10^pos */
783 for (i = 0; i <= pos; i++)
784 strcat(dst, "[0-9]");
785 strcat(dst, "[0-9]*");
790 void string_rel_add_char(char **term_p, const char *src, int *indx)
792 if (src[*indx] == '\\')
793 *(*term_p)++ = src[(*indx)++];
794 *(*term_p)++ = src[(*indx)++];
798 * > abc ([b-].*|a[c-].*|ab[d-].*|abc.+)
799 * ([^-a].*|a[^-b].*ab[^-c].*|abc.+)
800 * >= abc ([b-].*|a[c-].*|ab[c-].*)
801 * ([^-a].*|a[^-b].*|ab[c-].*)
802 * < abc ([-0].*|a[-a].*|ab[-b].*)
803 * ([^a-].*|a[^b-].*|ab[^c-].*)
804 * <= abc ([-0].*|a[-a].*|ab[-b].*|abc)
805 * ([^a-].*|a[^b-].*|ab[^c-].*|abc)
807 static int string_relation(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
808 const char **term_sub, char *term_dict,
809 oid_value attributeSet,
810 int reg_type, int space_split, char *term_dst,
816 char *term_tmp = term_dict + strlen(term_dict);
817 char term_component[2*IT_MAX_WORD+20];
819 attr_init(&relation, zapt, 2);
820 relation_value = attr_find(&relation, NULL);
823 yaz_log(YLOG_DEBUG, "string relation value=%d", relation_value);
824 switch (relation_value)
827 if (!term_100(zh->reg->zebra_maps, reg_type,
828 term_sub, term_component,
829 space_split, term_dst))
831 yaz_log(log_level_rpn, "Relation <");
834 for (i = 0; term_component[i]; )
841 string_rel_add_char(&term_tmp, term_component, &j);
846 string_rel_add_char(&term_tmp, term_component, &i);
853 if ((term_tmp - term_dict) > IT_MAX_WORD)
860 if (!term_100(zh->reg->zebra_maps, reg_type,
861 term_sub, term_component,
862 space_split, term_dst))
864 yaz_log(log_level_rpn, "Relation <=");
867 for (i = 0; term_component[i]; )
872 string_rel_add_char(&term_tmp, term_component, &j);
876 string_rel_add_char(&term_tmp, term_component, &i);
885 if ((term_tmp - term_dict) > IT_MAX_WORD)
888 for (i = 0; term_component[i]; )
889 string_rel_add_char(&term_tmp, term_component, &i);
894 if (!term_100 (zh->reg->zebra_maps, reg_type,
895 term_sub, term_component, space_split, term_dst))
897 yaz_log(log_level_rpn, "Relation >");
900 for (i = 0; term_component[i];)
905 string_rel_add_char(&term_tmp, term_component, &j);
910 string_rel_add_char(&term_tmp, term_component, &i);
918 if ((term_tmp - term_dict) > IT_MAX_WORD)
921 for (i = 0; term_component[i];)
922 string_rel_add_char(&term_tmp, term_component, &i);
929 if (!term_100(zh->reg->zebra_maps, reg_type, term_sub,
930 term_component, space_split, term_dst))
932 yaz_log(log_level_rpn, "Relation >=");
935 for (i = 0; term_component[i];)
942 string_rel_add_char(&term_tmp, term_component, &j);
945 if (term_component[i+1])
949 string_rel_add_char(&term_tmp, term_component, &i);
953 string_rel_add_char(&term_tmp, term_component, &i);
960 if ((term_tmp - term_dict) > IT_MAX_WORD)
969 yaz_log(log_level_rpn, "Relation =");
970 if (!term_100(zh->reg->zebra_maps, reg_type, term_sub,
971 term_component, space_split, term_dst))
973 strcat(term_tmp, "(");
974 strcat(term_tmp, term_component);
975 strcat(term_tmp, ")");
984 static ZEBRA_RES string_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
985 const char **term_sub,
986 oid_value attributeSet, NMEM stream,
987 struct grep_info *grep_info,
988 int reg_type, int complete_flag,
989 int num_bases, char **basenames,
990 char *term_dst, int xpath_use);
992 static ZEBRA_RES term_trunc(ZebraHandle zh,
993 Z_AttributesPlusTerm *zapt,
994 const char **term_sub,
995 oid_value attributeSet, NMEM stream,
996 struct grep_info *grep_info,
997 int reg_type, int complete_flag,
998 int num_bases, char **basenames,
1000 const char *rank_type, int xpath_use,
1003 struct rset_key_control *kc)
1007 grep_info->isam_p_indx = 0;
1008 res = string_term(zh, zapt, term_sub, attributeSet, stream, grep_info,
1009 reg_type, complete_flag, num_bases, basenames,
1010 term_dst, xpath_use);
1011 if (res != ZEBRA_OK)
1013 if (!*term_sub) /* no more terms ? */
1015 yaz_log(log_level_rpn, "term: %s", term_dst);
1016 *rset = rset_trunc(zh, grep_info->isam_p_buf,
1017 grep_info->isam_p_indx, term_dst,
1018 strlen(term_dst), rank_type, 1 /* preserve pos */,
1019 zapt->term->which, rset_nmem,
1026 static char *nmem_strdup_i(NMEM nmem, int v)
1029 sprintf(val_str, "%d", v);
1030 return nmem_strdup(nmem, val_str);
1033 static ZEBRA_RES string_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1034 const char **term_sub,
1035 oid_value attributeSet, NMEM stream,
1036 struct grep_info *grep_info,
1037 int reg_type, int complete_flag,
1038 int num_bases, char **basenames,
1039 char *term_dst, int xpath_use)
1041 char term_dict[2*IT_MAX_WORD+4000];
1043 AttrType truncation;
1044 int truncation_value;
1047 const char *use_string = 0;
1048 oid_value curAttributeSet = attributeSet;
1050 struct rpn_char_map_info rcmi;
1051 int space_split = complete_flag ? 0 : 1;
1053 int bases_ok = 0; /* no of databases with OK attribute */
1054 int errCode = 0; /* err code (if any is not OK) */
1055 char *errString = 0; /* addinfo */
1057 rpn_char_map_prepare (zh->reg, reg_type, &rcmi);
1058 attr_init(&use, zapt, 1);
1059 use_value = attr_find_ex(&use, &curAttributeSet, &use_string);
1060 yaz_log(log_level_rpn, "string_term, use value %d", use_value);
1061 attr_init(&truncation, zapt, 5);
1062 truncation_value = attr_find(&truncation, NULL);
1063 yaz_log(log_level_rpn, "truncation value %d", truncation_value);
1065 if (use_value == -1) /* no attribute - assumy "any" */
1067 for (base_no = 0; base_no < num_bases; base_no++)
1071 int regex_range = 0;
1074 data1_local_attribute id_xpath_attr;
1075 data1_local_attribute *local_attr;
1076 int max_pos, prefix_len = 0;
1081 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
1083 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1084 zh->errString = basenames[base_no];
1087 if (xpath_use > 0 && use_value == -2)
1089 /* xpath mode and we have a string attribute */
1090 attp.local_attributes = &id_xpath_attr;
1091 attp.attset_ordinal = VAL_IDXPATH;
1092 id_xpath_attr.next = 0;
1094 use_value = xpath_use; /* xpath_use as use-attribute now */
1095 id_xpath_attr.local = use_value;
1097 else if (curAttributeSet == VAL_IDXPATH && use_value >= 0)
1099 /* X-Path attribute, use numeric value directly */
1100 attp.local_attributes = &id_xpath_attr;
1101 attp.attset_ordinal = VAL_IDXPATH;
1102 id_xpath_attr.next = 0;
1103 id_xpath_attr.local = use_value;
1105 else if (use_string &&
1106 (ord = zebraExplain_lookup_attr_str(zh->reg->zei,
1109 /* we have a match for a raw string attribute */
1114 term_dict[prefix_len++] = '|';
1116 term_dict[prefix_len++] = '(';
1118 ord_len = key_SU_encode (ord, ord_buf);
1119 for (i = 0; i<ord_len; i++)
1121 term_dict[prefix_len++] = 1;
1122 term_dict[prefix_len++] = ord_buf[i];
1124 attp.local_attributes = 0; /* no more attributes */
1128 /* lookup in the .att files . Allow string as well */
1129 if ((r = att_getentbyatt (zh, &attp, curAttributeSet, use_value,
1132 yaz_log(YLOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1133 curAttributeSet, use_value, r);
1136 /* set was found, but value wasn't defined */
1137 errCode = YAZ_BIB1_UNSUPP_USE_ATTRIBUTE;
1139 errString = nmem_strdup(stream, use_string);
1141 errString = nmem_strdup_i (stream, use_value);
1146 struct oident oident;
1148 oident.proto = PROTO_Z3950;
1149 oident.oclass = CLASS_ATTSET;
1150 oident.value = curAttributeSet;
1151 oid_ent_to_oid (&oident, oid);
1153 errCode = YAZ_BIB1_UNSUPP_ATTRIBUTE_SET;
1154 errString = nmem_strdup(stream, oident.desc);
1159 for (local_attr = attp.local_attributes; local_attr;
1160 local_attr = local_attr->next)
1165 ord = zebraExplain_lookup_attr_su(zh->reg->zei,
1166 attp.attset_ordinal,
1171 term_dict[prefix_len++] = '|';
1173 term_dict[prefix_len++] = '(';
1175 ord_len = key_SU_encode (ord, ord_buf);
1176 for (i = 0; i<ord_len; i++)
1178 term_dict[prefix_len++] = 1;
1179 term_dict[prefix_len++] = ord_buf[i];
1186 term_dict[prefix_len++] = ')';
1187 term_dict[prefix_len++] = 1;
1188 term_dict[prefix_len++] = reg_type;
1189 yaz_log(log_level_rpn, "reg_type = %d", term_dict[prefix_len-1]);
1190 term_dict[prefix_len] = '\0';
1192 switch (truncation_value)
1194 case -1: /* not specified */
1195 case 100: /* do not truncate */
1196 if (!string_relation (zh, zapt, &termp, term_dict,
1198 reg_type, space_split, term_dst,
1203 zh->errCode = relation_error;
1210 case 1: /* right truncation */
1211 term_dict[j++] = '(';
1212 if (!term_100(zh->reg->zebra_maps, reg_type,
1213 &termp, term_dict + j, space_split, term_dst))
1218 strcat(term_dict, ".*)");
1220 case 2: /* keft truncation */
1221 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1222 if (!term_100(zh->reg->zebra_maps, reg_type,
1223 &termp, term_dict + j, space_split, term_dst))
1228 strcat(term_dict, ")");
1230 case 3: /* left&right truncation */
1231 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1232 if (!term_100(zh->reg->zebra_maps, reg_type,
1233 &termp, term_dict + j, space_split, term_dst))
1238 strcat(term_dict, ".*)");
1240 case 101: /* process # in term */
1241 term_dict[j++] = '(';
1242 if (!term_101(zh->reg->zebra_maps, reg_type,
1243 &termp, term_dict + j, space_split, term_dst))
1248 strcat(term_dict, ")");
1250 case 102: /* Regexp-1 */
1251 term_dict[j++] = '(';
1252 if (!term_102(zh->reg->zebra_maps, reg_type,
1253 &termp, term_dict + j, space_split, term_dst))
1258 strcat(term_dict, ")");
1260 case 103: /* Regexp-2 */
1262 term_dict[j++] = '(';
1264 if (!term_103(zh->reg->zebra_maps, reg_type,
1265 &termp, term_dict + j, ®ex_range,
1266 space_split, term_dst))
1271 strcat(term_dict, ")");
1273 case 104: /* process # and ! in term */
1274 term_dict[j++] = '(';
1275 if (!term_104(zh->reg->zebra_maps, reg_type,
1276 &termp, term_dict + j, space_split, term_dst))
1281 strcat(term_dict, ")");
1283 case 105: /* process * and ! in term */
1284 term_dict[j++] = '(';
1285 if (!term_105(zh->reg->zebra_maps, reg_type,
1286 &termp, term_dict + j, space_split, term_dst, 1))
1291 strcat(term_dict, ")");
1293 case 106: /* process * and ! in term */
1294 term_dict[j++] = '(';
1295 if (!term_105(zh->reg->zebra_maps, reg_type,
1296 &termp, term_dict + j, space_split, term_dst, 0))
1301 strcat(term_dict, ")");
1304 zh->errCode = YAZ_BIB1_UNSUPP_TRUNCATION_ATTRIBUTE;
1305 zh->errString = nmem_strdup_i(stream, truncation_value);
1311 const char *input = term_dict + prefix_len;
1312 esc_str(buf, sizeof(buf), input, strlen(input));
1316 yaz_log(log_level_rpn, "dict_lookup_grep: %s", term_dict+prefix_len);
1317 r = dict_lookup_grep(zh->reg->dict, term_dict, regex_range,
1318 grep_info, &max_pos, init_pos,
1321 yaz_log(YLOG_WARN, "dict_lookup_grep fail %d", r);
1326 zh->errCode = errCode;
1327 zh->errString = errString;
1331 yaz_log(YLOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1336 /* convert APT search term to UTF8 */
1337 static ZEBRA_RES zapt_term_to_utf8(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1341 Z_Term *term = zapt->term;
1343 switch (term->which)
1345 case Z_Term_general:
1346 if (zh->iconv_to_utf8 != 0)
1348 char *inbuf = term->u.general->buf;
1349 size_t inleft = term->u.general->len;
1350 char *outbuf = termz;
1351 size_t outleft = IT_MAX_WORD-1;
1354 ret = yaz_iconv(zh->iconv_to_utf8, &inbuf, &inleft,
1356 if (ret == (size_t)(-1))
1358 ret = yaz_iconv(zh->iconv_to_utf8, 0, 0, 0, 0);
1360 YAZ_BIB1_QUERY_TERM_INCLUDES_CHARS_THAT_DO_NOT_TRANSLATE_INTO_;
1367 sizez = term->u.general->len;
1368 if (sizez > IT_MAX_WORD-1)
1369 sizez = IT_MAX_WORD-1;
1370 memcpy (termz, term->u.general->buf, sizez);
1371 termz[sizez] = '\0';
1374 case Z_Term_characterString:
1375 sizez = strlen(term->u.characterString);
1376 if (sizez > IT_MAX_WORD-1)
1377 sizez = IT_MAX_WORD-1;
1378 memcpy (termz, term->u.characterString, sizez);
1379 termz[sizez] = '\0';
1382 zh->errCode = YAZ_BIB1_UNSUPP_CODED_VALUE_FOR_TERM;
1388 /* convert APT SCAN term to internal cmap */
1389 static ZEBRA_RES trans_scan_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1390 char *termz, int reg_type)
1392 char termz0[IT_MAX_WORD];
1394 if (zapt_term_to_utf8(zh, zapt, termz0) == ZEBRA_FAIL)
1395 return ZEBRA_FAIL; /* error */
1399 const char *cp = (const char *) termz0;
1400 const char *cp_end = cp + strlen(cp);
1403 const char *space_map = NULL;
1406 while ((len = (cp_end - cp)) > 0)
1408 map = zebra_maps_input(zh->reg->zebra_maps, reg_type, &cp, len, 0);
1409 if (**map == *CHR_SPACE)
1414 for (src = space_map; *src; src++)
1417 for (src = *map; *src; src++)
1426 char *normalize_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1427 const char *termz, NMEM stream, unsigned reg_id)
1430 AttrType truncation;
1431 int truncation_value;
1434 attr_init(&truncation, zapt, 5);
1435 truncation_value = attr_find(&truncation, NULL);
1437 switch (truncation_value)
1457 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, ex_list,
1458 termz, strlen(termz));
1460 return nmem_strdup(stream, termz);
1463 char *buf = (char*) nmem_malloc(stream, wrbuf_len(wrbuf)+1);
1464 memcpy (buf, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1465 buf[wrbuf_len(wrbuf)] = '\0';
1470 static void grep_info_delete(struct grep_info *grep_info)
1473 xfree(grep_info->term_no);
1475 xfree(grep_info->isam_p_buf);
1478 static int grep_info_prepare(ZebraHandle zh,
1479 Z_AttributesPlusTerm *zapt,
1480 struct grep_info *grep_info,
1485 int termset_value_numeric;
1486 const char *termset_value_string;
1489 grep_info->term_no = 0;
1491 grep_info->isam_p_size = 0;
1492 grep_info->isam_p_buf = NULL;
1494 grep_info->reg_type = reg_type;
1495 grep_info->termset = 0;
1499 attr_init(&termset, zapt, 8);
1500 termset_value_numeric =
1501 attr_find_ex(&termset, NULL, &termset_value_string);
1502 if (termset_value_numeric != -1)
1505 const char *termset_name = 0;
1506 if (termset_value_numeric != -2)
1509 sprintf(resname, "%d", termset_value_numeric);
1510 termset_name = resname;
1513 termset_name = termset_value_string;
1514 yaz_log(log_level_rpn, "creating termset set %s", termset_name);
1515 grep_info->termset = resultSetAdd(zh, termset_name, 1);
1516 if (!grep_info->termset)
1518 zh->errCode = YAZ_BIB1_ILLEGAL_RESULT_SET_NAME;
1519 zh->errString = nmem_strdup(stream, termset_name);
1527 static ZEBRA_RES term_list_trunc(ZebraHandle zh,
1528 Z_AttributesPlusTerm *zapt,
1529 const char *termz_org,
1530 oid_value attributeSet,
1532 int reg_type, int complete_flag,
1533 const char *rank_type, int xpath_use,
1534 int num_bases, char **basenames,
1536 RSET **result_sets, int *num_result_sets,
1537 struct rset_key_control *kc)
1539 char term_dst[IT_MAX_WORD+1];
1540 struct grep_info grep_info;
1541 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1542 const char *termp = termz;
1545 *num_result_sets = 0;
1547 if (grep_info_prepare(zh, zapt, &grep_info, reg_type, stream))
1553 if (alloc_sets == *num_result_sets)
1556 RSET *rnew = (RSET *) nmem_malloc(stream, (alloc_sets+add) *
1559 memcpy(rnew, *result_sets, alloc_sets * sizeof(*rnew));
1560 alloc_sets = alloc_sets + add;
1561 *result_sets = rnew;
1563 res = term_trunc(zh, zapt, &termp, attributeSet,
1565 reg_type, complete_flag,
1566 num_bases, basenames,
1567 term_dst, rank_type,
1568 xpath_use, rset_nmem,
1569 &(*result_sets)[*num_result_sets],
1571 if (res != ZEBRA_OK)
1574 for (i = 0; i < *num_result_sets; i++)
1575 rset_delete((*result_sets)[i]);
1576 grep_info_delete (&grep_info);
1579 if ((*result_sets)[*num_result_sets] == 0)
1581 (*num_result_sets)++;
1583 grep_info_delete(&grep_info);
1587 static ZEBRA_RES rpn_search_APT_phrase(ZebraHandle zh,
1588 Z_AttributesPlusTerm *zapt,
1589 const char *termz_org,
1590 oid_value attributeSet,
1592 int reg_type, int complete_flag,
1593 const char *rank_type, int xpath_use,
1594 int num_bases, char **basenames,
1597 struct rset_key_control *kc)
1599 RSET *result_sets = 0;
1600 int num_result_sets = 0;
1602 term_list_trunc(zh, zapt, termz_org, attributeSet,
1603 stream, reg_type, complete_flag,
1604 rank_type, xpath_use,
1605 num_bases, basenames,
1607 &result_sets, &num_result_sets, kc);
1608 if (res != ZEBRA_OK)
1610 if (num_result_sets == 0)
1611 *rset = rsnull_create (rset_nmem, kc);
1612 else if (num_result_sets == 1)
1613 *rset = result_sets[0];
1615 *rset = rsprox_create(rset_nmem, kc, kc->scope,
1616 num_result_sets, result_sets,
1617 1 /* ordered */, 0 /* exclusion */,
1618 3 /* relation */, 1 /* distance */);
1624 static ZEBRA_RES rpn_search_APT_or_list(ZebraHandle zh,
1625 Z_AttributesPlusTerm *zapt,
1626 const char *termz_org,
1627 oid_value attributeSet,
1629 int reg_type, int complete_flag,
1630 const char *rank_type,
1632 int num_bases, char **basenames,
1635 struct rset_key_control *kc)
1637 RSET *result_sets = 0;
1638 int num_result_sets = 0;
1640 term_list_trunc(zh, zapt, termz_org, attributeSet,
1641 stream, reg_type, complete_flag,
1642 rank_type, xpath_use,
1643 num_bases, basenames,
1645 &result_sets, &num_result_sets, kc);
1646 if (res != ZEBRA_OK)
1648 if (num_result_sets == 0)
1649 *rset = rsnull_create (rset_nmem, kc);
1650 else if (num_result_sets == 1)
1651 *rset = result_sets[0];
1653 *rset = rsmulti_or_create(rset_nmem, kc, kc->scope,
1654 num_result_sets, result_sets);
1660 static ZEBRA_RES rpn_search_APT_and_list(ZebraHandle zh,
1661 Z_AttributesPlusTerm *zapt,
1662 const char *termz_org,
1663 oid_value attributeSet,
1665 int reg_type, int complete_flag,
1666 const char *rank_type,
1668 int num_bases, char **basenames,
1671 struct rset_key_control *kc)
1673 RSET *result_sets = 0;
1674 int num_result_sets = 0;
1676 term_list_trunc(zh, zapt, termz_org, attributeSet,
1677 stream, reg_type, complete_flag,
1678 rank_type, xpath_use,
1679 num_bases, basenames,
1681 &result_sets, &num_result_sets,
1683 if (res != ZEBRA_OK)
1685 if (num_result_sets == 0)
1686 *rset = rsnull_create (rset_nmem, kc);
1687 else if (num_result_sets == 1)
1688 *rset = result_sets[0];
1690 *rset = rsmulti_and_create(rset_nmem, kc, kc->scope,
1691 num_result_sets, result_sets);
1697 static int numeric_relation(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1698 const char **term_sub,
1700 oid_value attributeSet,
1701 struct grep_info *grep_info,
1711 char *term_tmp = term_dict + strlen(term_dict);
1714 attr_init(&relation, zapt, 2);
1715 relation_value = attr_find(&relation, NULL);
1717 yaz_log(log_level_rpn, "numeric relation value=%d", relation_value);
1719 if (!term_100(zh->reg->zebra_maps, reg_type, term_sub, term_tmp, 1,
1722 term_value = atoi (term_tmp);
1723 switch (relation_value)
1726 yaz_log(log_level_rpn, "Relation <");
1727 gen_regular_rel(term_tmp, term_value-1, 1);
1730 yaz_log(log_level_rpn, "Relation <=");
1731 gen_regular_rel(term_tmp, term_value, 1);
1734 yaz_log(log_level_rpn, "Relation >=");
1735 gen_regular_rel(term_tmp, term_value, 0);
1738 yaz_log(log_level_rpn, "Relation >");
1739 gen_regular_rel(term_tmp, term_value+1, 0);
1743 yaz_log(log_level_rpn, "Relation =");
1744 sprintf(term_tmp, "(0*%d)", term_value);
1750 yaz_log(log_level_rpn, "dict_lookup_grep: %s", term_tmp);
1751 r = dict_lookup_grep(zh->reg->dict, term_dict, 0, grep_info, max_pos,
1754 yaz_log(YLOG_WARN, "dict_lookup_grep fail, rel = gt: %d", r);
1755 yaz_log(log_level_rpn, "%d positions", grep_info->isam_p_indx);
1759 static ZEBRA_RES numeric_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1760 const char **term_sub,
1761 oid_value attributeSet,
1762 struct grep_info *grep_info,
1763 int reg_type, int complete_flag,
1764 int num_bases, char **basenames,
1765 char *term_dst, int xpath_use, NMEM stream)
1767 char term_dict[2*IT_MAX_WORD+2];
1771 const char *use_string = 0;
1772 oid_value curAttributeSet = attributeSet;
1774 struct rpn_char_map_info rcmi;
1776 int bases_ok = 0; /* no of databases with OK attribute */
1777 int errCode = 0; /* err code (if any is not OK) */
1778 char *errString = 0; /* addinfo */
1780 rpn_char_map_prepare (zh->reg, reg_type, &rcmi);
1781 attr_init(&use, zapt, 1);
1782 use_value = attr_find_ex(&use, &curAttributeSet, &use_string);
1784 if (use_value == -1)
1787 for (base_no = 0; base_no < num_bases; base_no++)
1790 data1_local_attribute id_xpath_attr;
1791 data1_local_attribute *local_attr;
1792 int max_pos, prefix_len = 0;
1793 int relation_error = 0;
1796 if (use_value == -2) /* string attribute (assume IDXPATH/any) */
1798 use_value = xpath_use;
1799 attp.local_attributes = &id_xpath_attr;
1800 attp.attset_ordinal = VAL_IDXPATH;
1801 id_xpath_attr.next = 0;
1802 id_xpath_attr.local = use_value;
1804 else if (curAttributeSet == VAL_IDXPATH)
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;
1813 if ((r = att_getentbyatt (zh, &attp, curAttributeSet, use_value,
1816 yaz_log(YLOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1817 curAttributeSet, use_value, r);
1820 errCode = YAZ_BIB1_UNSUPP_USE_ATTRIBUTE;
1822 errString = nmem_strdup(stream, use_string);
1824 errString = nmem_strdup_i (stream, use_value);
1827 errCode = YAZ_BIB1_UNSUPP_ATTRIBUTE_SET;
1831 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
1833 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1834 zh->errString = basenames[base_no];
1837 for (local_attr = attp.local_attributes; local_attr;
1838 local_attr = local_attr->next)
1844 ord = zebraExplain_lookup_attr_su(zh->reg->zei,
1845 attp.attset_ordinal,
1850 term_dict[prefix_len++] = '|';
1852 term_dict[prefix_len++] = '(';
1854 ord_len = key_SU_encode (ord, ord_buf);
1855 for (i = 0; i<ord_len; i++)
1857 term_dict[prefix_len++] = 1;
1858 term_dict[prefix_len++] = ord_buf[i];
1863 errCode = YAZ_BIB1_UNSUPP_USE_ATTRIBUTE;
1864 errString = nmem_strdup_i(stream, use_value);
1868 term_dict[prefix_len++] = ')';
1869 term_dict[prefix_len++] = 1;
1870 term_dict[prefix_len++] = reg_type;
1871 yaz_log(YLOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1872 term_dict[prefix_len] = '\0';
1873 if (!numeric_relation(zh, zapt, &termp, term_dict,
1874 attributeSet, grep_info, &max_pos, reg_type,
1875 term_dst, &relation_error))
1879 zh->errCode = relation_error;
1889 zh->errCode = errCode;
1890 zh->errString = errString;
1894 yaz_log(YLOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1898 static ZEBRA_RES rpn_search_APT_numeric(ZebraHandle zh,
1899 Z_AttributesPlusTerm *zapt,
1901 oid_value attributeSet,
1903 int reg_type, int complete_flag,
1904 const char *rank_type, int xpath_use,
1905 int num_bases, char **basenames,
1908 struct rset_key_control *kc)
1910 char term_dst[IT_MAX_WORD+1];
1911 const char *termp = termz;
1912 RSET *result_sets = 0;
1913 int num_result_sets = 0;
1915 struct grep_info grep_info;
1918 yaz_log(log_level_rpn, "APT_numeric t='%s'", termz);
1919 if (grep_info_prepare(zh, zapt, &grep_info, reg_type, stream))
1923 if (alloc_sets == num_result_sets)
1926 RSET *rnew = (RSET *) nmem_malloc(stream, (alloc_sets+add) *
1929 memcpy(rnew, result_sets, alloc_sets * sizeof(*rnew));
1930 alloc_sets = alloc_sets + add;
1933 yaz_log(YLOG_DEBUG, "APT_numeric termp=%s", termp);
1934 grep_info.isam_p_indx = 0;
1935 res = numeric_term(zh, zapt, &termp, attributeSet, &grep_info,
1936 reg_type, complete_flag, num_bases, basenames,
1937 term_dst, xpath_use,
1939 if (res == ZEBRA_FAIL || termp == 0)
1941 yaz_log(YLOG_DEBUG, "term: %s", term_dst);
1942 result_sets[num_result_sets] =
1943 rset_trunc(zh, grep_info.isam_p_buf,
1944 grep_info.isam_p_indx, term_dst,
1945 strlen(term_dst), rank_type,
1946 0 /* preserve position */,
1947 zapt->term->which, rset_nmem,
1949 if (!result_sets[num_result_sets])
1953 grep_info_delete(&grep_info);
1957 for (i = 0; i<num_result_sets; i++)
1958 rset_delete(result_sets[i]);
1961 if (num_result_sets == 0)
1962 *rset = rsnull_create(rset_nmem, kc);
1963 if (num_result_sets == 1)
1964 *rset = result_sets[0];
1966 *rset = rsmulti_and_create(rset_nmem, kc, kc->scope,
1967 num_result_sets, result_sets);
1973 static ZEBRA_RES rpn_search_APT_local(ZebraHandle zh,
1974 Z_AttributesPlusTerm *zapt,
1976 oid_value attributeSet,
1978 const char *rank_type, NMEM rset_nmem,
1980 struct rset_key_control *kc)
1985 *rset = rstemp_create(rset_nmem, kc, kc->scope,
1986 res_get (zh->res, "setTmpDir"),0 );
1987 rsfd = rset_open(*rset, RSETF_WRITE);
1995 rset_write (rsfd, &key);
2000 static ZEBRA_RES rpn_sort_spec(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2001 oid_value attributeSet, NMEM stream,
2002 Z_SortKeySpecList *sort_sequence,
2003 const char *rank_type,
2005 struct rset_key_control *kc)
2008 int sort_relation_value;
2009 AttrType sort_relation_type;
2016 attr_init(&sort_relation_type, zapt, 7);
2017 sort_relation_value = attr_find(&sort_relation_type, &attributeSet);
2019 if (!sort_sequence->specs)
2021 sort_sequence->num_specs = 10;
2022 sort_sequence->specs = (Z_SortKeySpec **)
2023 nmem_malloc(stream, sort_sequence->num_specs *
2024 sizeof(*sort_sequence->specs));
2025 for (i = 0; i<sort_sequence->num_specs; i++)
2026 sort_sequence->specs[i] = 0;
2028 if (zapt->term->which != Z_Term_general)
2031 i = atoi_n ((char *) zapt->term->u.general->buf,
2032 zapt->term->u.general->len);
2033 if (i >= sort_sequence->num_specs)
2035 sprintf(termz, "%d", i);
2037 oe.proto = PROTO_Z3950;
2038 oe.oclass = CLASS_ATTSET;
2039 oe.value = attributeSet;
2040 if (!oid_ent_to_oid (&oe, oid))
2043 sks = (Z_SortKeySpec *) nmem_malloc(stream, sizeof(*sks));
2044 sks->sortElement = (Z_SortElement *)
2045 nmem_malloc(stream, sizeof(*sks->sortElement));
2046 sks->sortElement->which = Z_SortElement_generic;
2047 sk = sks->sortElement->u.generic = (Z_SortKey *)
2048 nmem_malloc(stream, sizeof(*sk));
2049 sk->which = Z_SortKey_sortAttributes;
2050 sk->u.sortAttributes = (Z_SortAttributes *)
2051 nmem_malloc(stream, sizeof(*sk->u.sortAttributes));
2053 sk->u.sortAttributes->id = oid;
2054 sk->u.sortAttributes->list = zapt->attributes;
2056 sks->sortRelation = (int *)
2057 nmem_malloc(stream, sizeof(*sks->sortRelation));
2058 if (sort_relation_value == 1)
2059 *sks->sortRelation = Z_SortKeySpec_ascending;
2060 else if (sort_relation_value == 2)
2061 *sks->sortRelation = Z_SortKeySpec_descending;
2063 *sks->sortRelation = Z_SortKeySpec_ascending;
2065 sks->caseSensitivity = (int *)
2066 nmem_malloc(stream, sizeof(*sks->caseSensitivity));
2067 *sks->caseSensitivity = 0;
2069 sks->which = Z_SortKeySpec_null;
2070 sks->u.null = odr_nullval ();
2071 sort_sequence->specs[i] = sks;
2072 *rset = rsnull_create (NULL, kc);
2077 static int parse_xpath(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2078 oid_value attributeSet,
2079 struct xpath_location_step *xpath, int max, NMEM mem)
2081 oid_value curAttributeSet = attributeSet;
2083 const char *use_string = 0;
2085 attr_init(&use, zapt, 1);
2086 attr_find_ex(&use, &curAttributeSet, &use_string);
2088 if (!use_string || *use_string != '/')
2091 return zebra_parse_xpath_str(use_string, xpath, max, mem);
2096 static RSET xpath_trunc(ZebraHandle zh, NMEM stream,
2097 int reg_type, const char *term, int use,
2098 oid_value curAttributeSet, NMEM rset_nmem,
2099 struct rset_key_control *kc)
2102 struct grep_info grep_info;
2103 char term_dict[2048];
2106 int ord = zebraExplain_lookup_attr_su(zh->reg->zei, curAttributeSet, use);
2107 int ord_len, i, r, max_pos;
2108 int term_type = Z_Term_characterString;
2109 const char *flags = "void";
2111 if (grep_info_prepare(zh, 0 /* zapt */, &grep_info, '0', stream))
2112 return rsnull_create(rset_nmem, kc);
2115 return rsnull_create(rset_nmem, kc);
2117 term_dict[prefix_len++] = '|';
2119 term_dict[prefix_len++] = '(';
2121 ord_len = key_SU_encode (ord, ord_buf);
2122 for (i = 0; i<ord_len; i++)
2124 term_dict[prefix_len++] = 1;
2125 term_dict[prefix_len++] = ord_buf[i];
2127 term_dict[prefix_len++] = ')';
2128 term_dict[prefix_len++] = 1;
2129 term_dict[prefix_len++] = reg_type;
2131 strcpy(term_dict+prefix_len, term);
2133 grep_info.isam_p_indx = 0;
2134 r = dict_lookup_grep(zh->reg->dict, term_dict, 0,
2135 &grep_info, &max_pos, 0, grep_handle);
2136 yaz_log(YLOG_DEBUG, "%s %d positions", term,
2137 grep_info.isam_p_indx);
2138 rset = rset_trunc(zh, grep_info.isam_p_buf,
2139 grep_info.isam_p_indx, term, strlen(term),
2140 flags, 1, term_type,rset_nmem,
2142 grep_info_delete(&grep_info);
2146 static RSET rpn_search_xpath(ZebraHandle zh,
2147 oid_value attributeSet,
2148 int num_bases, char **basenames,
2149 NMEM stream, const char *rank_type, RSET rset,
2150 int xpath_len, struct xpath_location_step *xpath,
2152 struct rset_key_control *kc)
2154 oid_value curAttributeSet = attributeSet;
2161 yaz_log(YLOG_DEBUG, "xpath len=%d", xpath_len);
2162 for (i = 0; i<xpath_len; i++)
2164 yaz_log(log_level_rpn, "XPATH %d %s", i, xpath[i].part);
2168 curAttributeSet = VAL_IDXPATH;
2178 a[@attr = value]/b[@other = othervalue]
2180 /e/@a val range(e/,range(@a,freetext(w,1015,val),@a),e/)
2181 /a/b val range(b/a/,freetext(w,1016,val),b/a/)
2182 /a/b/@c val range(b/a/,range(@c,freetext(w,1016,val),@c),b/a/)
2183 /a/b[@c = y] val range(b/a/,freetext(w,1016,val),b/a/,@c = y)
2184 /a[@c = y]/b val range(a/,range(b/a/,freetext(w,1016,val),b/a/),a/,@c = y)
2185 /a[@c = x]/b[@c = y] range(a/,range(b/a/,freetext(w,1016,val),b/a/,@c = y),a/,@c = x)
2189 dict_grep_cmap (zh->reg->dict, 0, 0);
2191 for (base_no = 0; base_no < num_bases; base_no++)
2193 int level = xpath_len;
2196 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
2198 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
2199 zh->errString = basenames[base_no];
2202 while (--level >= 0)
2204 char xpath_rev[128];
2206 RSET rset_start_tag = 0, rset_end_tag = 0, rset_attr = 0;
2210 for (i = level; i >= 1; --i)
2212 const char *cp = xpath[i].part;
2218 memcpy (xpath_rev + len, "[^/]*", 5);
2221 else if (*cp == ' ')
2224 xpath_rev[len++] = 1;
2225 xpath_rev[len++] = ' ';
2229 xpath_rev[len++] = *cp;
2230 xpath_rev[len++] = '/';
2232 else if (i == 1) /* // case */
2234 xpath_rev[len++] = '.';
2235 xpath_rev[len++] = '*';
2240 if (xpath[level].predicate &&
2241 xpath[level].predicate->which == XPATH_PREDICATE_RELATION &&
2242 xpath[level].predicate->u.relation.name[0])
2244 WRBUF wbuf = wrbuf_alloc();
2245 wrbuf_puts(wbuf, xpath[level].predicate->u.relation.name+1);
2246 if (xpath[level].predicate->u.relation.value)
2248 const char *cp = xpath[level].predicate->u.relation.value;
2249 wrbuf_putc(wbuf, '=');
2253 if (strchr(REGEX_CHARS, *cp))
2254 wrbuf_putc(wbuf, '\\');
2255 wrbuf_putc(wbuf, *cp);
2259 wrbuf_puts(wbuf, "");
2260 rset_attr = xpath_trunc(
2261 zh, stream, '0', wrbuf_buf(wbuf), 3,
2262 curAttributeSet, rset_nmem, kc);
2263 wrbuf_free(wbuf, 1);
2270 yaz_log(log_level_rpn, "xpath_rev (%d) = %s", level, xpath_rev);
2271 if (strlen(xpath_rev))
2273 rset_start_tag = xpath_trunc(zh, stream, '0',
2274 xpath_rev, 1, curAttributeSet, rset_nmem, kc);
2276 rset_end_tag = xpath_trunc(zh, stream, '0',
2277 xpath_rev, 2, curAttributeSet, rset_nmem, kc);
2279 rset = rsbetween_create(rset_nmem, kc, kc->scope,
2280 rset_start_tag, rset,
2281 rset_end_tag, rset_attr);
2290 static ZEBRA_RES rpn_search_APT(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2291 oid_value attributeSet, NMEM stream,
2292 Z_SortKeySpecList *sort_sequence,
2293 int num_bases, char **basenames,
2296 struct rset_key_control *kc)
2298 ZEBRA_RES res = ZEBRA_OK;
2300 char *search_type = NULL;
2301 char rank_type[128];
2304 char termz[IT_MAX_WORD+1];
2307 struct xpath_location_step xpath[10];
2311 log_level_rpn = yaz_log_module_level("rpn");
2314 zebra_maps_attr(zh->reg->zebra_maps, zapt, ®_id, &search_type,
2315 rank_type, &complete_flag, &sort_flag);
2317 yaz_log(YLOG_DEBUG, "reg_id=%c", reg_id);
2318 yaz_log(YLOG_DEBUG, "complete_flag=%d", complete_flag);
2319 yaz_log(YLOG_DEBUG, "search_type=%s", search_type);
2320 yaz_log(YLOG_DEBUG, "rank_type=%s", rank_type);
2322 if (zapt_term_to_utf8(zh, zapt, termz) == ZEBRA_FAIL)
2326 return rpn_sort_spec(zh, zapt, attributeSet, stream, sort_sequence,
2327 rank_type, rset, kc);
2328 xpath_len = parse_xpath(zh, zapt, attributeSet, xpath, 10, stream);
2332 if (xpath[xpath_len-1].part[0] == '@')
2336 if (!strcmp(search_type, "phrase"))
2338 res = rpn_search_APT_phrase(zh, zapt, termz, attributeSet, stream,
2339 reg_id, complete_flag, rank_type,
2341 num_bases, basenames, rset_nmem,
2344 else if (!strcmp(search_type, "and-list"))
2346 res = rpn_search_APT_and_list(zh, zapt, termz, attributeSet, stream,
2347 reg_id, complete_flag, rank_type,
2349 num_bases, basenames, rset_nmem,
2352 else if (!strcmp(search_type, "or-list"))
2354 res = rpn_search_APT_or_list(zh, zapt, termz, attributeSet, stream,
2355 reg_id, complete_flag, rank_type,
2357 num_bases, basenames, rset_nmem,
2360 else if (!strcmp(search_type, "local"))
2362 res = rpn_search_APT_local(zh, zapt, termz, attributeSet, stream,
2363 rank_type, rset_nmem, rset, kc);
2365 else if (!strcmp(search_type, "numeric"))
2367 res = rpn_search_APT_numeric(zh, zapt, termz, attributeSet, stream,
2368 reg_id, complete_flag, rank_type,
2370 num_bases, basenames, rset_nmem,
2375 zh->errCode = YAZ_BIB1_UNSUPP_STRUCTURE_ATTRIBUTE;
2378 if (res != ZEBRA_OK)
2382 *rset = rpn_search_xpath(zh, attributeSet, num_bases, basenames,
2383 stream, rank_type, *rset,
2384 xpath_len, xpath, rset_nmem, kc);
2390 static ZEBRA_RES rpn_search_structure(ZebraHandle zh, Z_RPNStructure *zs,
2391 oid_value attributeSet,
2392 NMEM stream, NMEM rset_nmem,
2393 Z_SortKeySpecList *sort_sequence,
2394 int num_bases, char **basenames,
2395 RSET **result_sets, int *num_result_sets,
2396 Z_Operator *parent_op,
2397 struct rset_key_control *kc);
2399 ZEBRA_RES rpn_search_top(ZebraHandle zh, Z_RPNStructure *zs,
2400 oid_value attributeSet,
2401 NMEM stream, NMEM rset_nmem,
2402 Z_SortKeySpecList *sort_sequence,
2403 int num_bases, char **basenames,
2406 RSET *result_sets = 0;
2407 int num_result_sets = 0;
2409 struct rset_key_control *kc = zebra_key_control_create(zh);
2411 res = rpn_search_structure(zh, zs, attributeSet,
2414 num_bases, basenames,
2415 &result_sets, &num_result_sets,
2416 0 /* no parent op */,
2418 if (res != ZEBRA_OK)
2421 for (i = 0; i<num_result_sets; i++)
2422 rset_delete(result_sets[i]);
2426 assert(num_result_sets == 1);
2427 assert(result_sets);
2428 assert(*result_sets);
2429 *result_set = *result_sets;
2435 ZEBRA_RES rpn_search_structure(ZebraHandle zh, Z_RPNStructure *zs,
2436 oid_value attributeSet,
2437 NMEM stream, NMEM rset_nmem,
2438 Z_SortKeySpecList *sort_sequence,
2439 int num_bases, char **basenames,
2440 RSET **result_sets, int *num_result_sets,
2441 Z_Operator *parent_op,
2442 struct rset_key_control *kc)
2444 *num_result_sets = 0;
2445 if (zs->which == Z_RPNStructure_complex)
2448 Z_Operator *zop = zs->u.complex->roperator;
2449 RSET *result_sets_l = 0;
2450 int num_result_sets_l = 0;
2451 RSET *result_sets_r = 0;
2452 int num_result_sets_r = 0;
2454 res = rpn_search_structure(zh, zs->u.complex->s1,
2455 attributeSet, stream, rset_nmem,
2457 num_bases, basenames,
2458 &result_sets_l, &num_result_sets_l,
2460 if (res != ZEBRA_OK)
2463 for (i = 0; i<num_result_sets_l; i++)
2464 rset_delete(result_sets_l[i]);
2467 res = rpn_search_structure(zh, zs->u.complex->s2,
2468 attributeSet, stream, rset_nmem,
2470 num_bases, basenames,
2471 &result_sets_r, &num_result_sets_r,
2473 if (res != ZEBRA_OK)
2476 for (i = 0; i<num_result_sets_l; i++)
2477 rset_delete(result_sets_l[i]);
2478 for (i = 0; i<num_result_sets_r; i++)
2479 rset_delete(result_sets_r[i]);
2483 /* make a new list of result for all children */
2484 *num_result_sets = num_result_sets_l + num_result_sets_r;
2485 *result_sets = nmem_malloc(stream, *num_result_sets *
2486 sizeof(**result_sets));
2487 memcpy(*result_sets, result_sets_l,
2488 num_result_sets_l * sizeof(**result_sets));
2489 memcpy(*result_sets + num_result_sets_l, result_sets_r,
2490 num_result_sets_r * sizeof(**result_sets));
2492 if (!parent_op || parent_op->which != zop->which
2493 || (zop->which != Z_Operator_and &&
2494 zop->which != Z_Operator_or))
2496 /* parent node different from this one (or non-present) */
2497 /* we must combine result sets now */
2501 case Z_Operator_and:
2502 rset = rsmulti_and_create(rset_nmem, kc,
2504 *num_result_sets, *result_sets);
2507 rset = rsmulti_or_create(rset_nmem, kc,
2509 *num_result_sets, *result_sets);
2511 case Z_Operator_and_not:
2512 rset = rsbool_create_not(rset_nmem, kc,
2517 case Z_Operator_prox:
2518 if (zop->u.prox->which != Z_ProximityOperator_known)
2520 zh->errCode = YAZ_BIB1_UNSUPP_PROX_UNIT_CODE;
2523 if (*zop->u.prox->u.known != Z_ProxUnit_word)
2525 char *val = (char *) nmem_malloc(stream, 16);
2526 zh->errCode = YAZ_BIB1_UNSUPP_PROX_UNIT_CODE;
2527 zh->errString = val;
2528 sprintf(val, "%d", *zop->u.prox->u.known);
2533 rset = rsprox_create(rset_nmem, kc,
2535 *num_result_sets, *result_sets,
2536 *zop->u.prox->ordered,
2537 (!zop->u.prox->exclusion ?
2538 0 : *zop->u.prox->exclusion),
2539 *zop->u.prox->relationType,
2540 *zop->u.prox->distance );
2544 zh->errCode = YAZ_BIB1_OPERATOR_UNSUPP;
2547 *num_result_sets = 1;
2548 *result_sets = nmem_malloc(stream, *num_result_sets *
2549 sizeof(**result_sets));
2550 (*result_sets)[0] = rset;
2553 else if (zs->which == Z_RPNStructure_simple)
2558 if (zs->u.simple->which == Z_Operand_APT)
2560 yaz_log(YLOG_DEBUG, "rpn_search_APT");
2561 res = rpn_search_APT(zh, zs->u.simple->u.attributesPlusTerm,
2562 attributeSet, stream, sort_sequence,
2563 num_bases, basenames, rset_nmem, &rset,
2565 if (res != ZEBRA_OK)
2568 else if (zs->u.simple->which == Z_Operand_resultSetId)
2570 yaz_log(YLOG_DEBUG, "rpn_search_ref");
2571 rset = resultSetRef(zh, zs->u.simple->u.resultSetId);
2574 zh->errCode = YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST;
2576 nmem_strdup(stream, zs->u.simple->u.resultSetId);
2583 zh->errCode = YAZ_BIB1_UNSUPP_SEARCH;
2586 *num_result_sets = 1;
2587 *result_sets = nmem_malloc(stream, *num_result_sets *
2588 sizeof(**result_sets));
2589 (*result_sets)[0] = rset;
2593 zh->errCode = YAZ_BIB1_UNSUPP_SEARCH;
2599 struct scan_info_entry {
2605 struct scan_info_entry *list;
2611 static int scan_handle (char *name, const char *info, int pos, void *client)
2613 int len_prefix, idx;
2614 struct scan_info *scan_info = (struct scan_info *) client;
2616 len_prefix = strlen(scan_info->prefix);
2617 if (memcmp (name, scan_info->prefix, len_prefix))
2620 idx = scan_info->after - pos + scan_info->before;
2626 scan_info->list[idx].term = (char *)
2627 odr_malloc(scan_info->odr, strlen(name + len_prefix)+1);
2628 strcpy(scan_info->list[idx].term, name + len_prefix);
2629 assert (*info == sizeof(ISAM_P));
2630 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAM_P));
2634 static void scan_term_untrans (ZebraHandle zh, NMEM stream, int reg_type,
2635 char **dst, const char *src)
2637 char term_src[IT_MAX_WORD];
2638 char term_dst[IT_MAX_WORD];
2640 term_untrans (zh, reg_type, term_src, src);
2642 if (zh->iconv_from_utf8 != 0)
2645 char *inbuf = term_src;
2646 size_t inleft = strlen(term_src);
2647 char *outbuf = term_dst;
2648 size_t outleft = sizeof(term_dst)-1;
2651 ret = yaz_iconv (zh->iconv_from_utf8, &inbuf, &inleft,
2653 if (ret == (size_t)(-1))
2656 len = outbuf - term_dst;
2657 *dst = nmem_malloc(stream, len + 1);
2659 memcpy (*dst, term_dst, len);
2663 *dst = nmem_strdup(stream, term_src);
2666 static void count_set (RSET r, int *count)
2673 yaz_log(YLOG_DEBUG, "count_set");
2676 rfd = rset_open (r, RSETF_READ);
2677 while (rset_read (rfd, &key,0 /* never mind terms */))
2679 if (key.mem[0] != psysno)
2681 psysno = key.mem[0];
2687 yaz_log(YLOG_DEBUG, "%d keys, %d records", kno, *count);
2690 ZEBRA_RES rpn_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
2691 oid_value attributeset,
2692 int num_bases, char **basenames,
2693 int *position, int *num_entries, ZebraScanEntry **list,
2694 int *is_partial, RSET limit_set, int return_zero)
2697 int pos = *position;
2698 int num = *num_entries;
2702 char termz[IT_MAX_WORD+20];
2705 const char *use_string = 0;
2706 struct scan_info *scan_info_array;
2707 ZebraScanEntry *glist;
2708 int ords[32], ord_no = 0;
2711 int bases_ok = 0; /* no of databases with OK attribute */
2712 int errCode = 0; /* err code (if any is not OK) */
2713 char *errString = 0; /* addinfo */
2716 char *search_type = NULL;
2717 char rank_type[128];
2720 NMEM rset_nmem = NULL;
2721 struct rset_key_control *kc = 0;
2726 if (attributeset == VAL_NONE)
2727 attributeset = VAL_BIB1;
2732 int termset_value_numeric;
2733 const char *termset_value_string;
2734 attr_init(&termset, zapt, 8);
2735 termset_value_numeric =
2736 attr_find_ex(&termset, NULL, &termset_value_string);
2737 if (termset_value_numeric != -1)
2740 const char *termset_name = 0;
2742 if (termset_value_numeric != -2)
2745 sprintf(resname, "%d", termset_value_numeric);
2746 termset_name = resname;
2749 termset_name = termset_value_string;
2751 limit_set = resultSetRef (zh, termset_name);
2755 yaz_log(YLOG_DEBUG, "position = %d, num = %d set=%d",
2756 pos, num, attributeset);
2758 attr_init(&use, zapt, 1);
2759 use_value = attr_find_ex(&use, &attributeset, &use_string);
2761 if (zebra_maps_attr(zh->reg->zebra_maps, zapt, ®_id, &search_type,
2762 rank_type, &complete_flag, &sort_flag))
2765 zh->errCode = YAZ_BIB1_UNSUPP_ATTRIBUTE_TYPE;
2768 yaz_log(YLOG_DEBUG, "use_value = %d", use_value);
2770 if (use_value == -1)
2772 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
2774 data1_local_attribute *local_attr;
2778 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
2780 zh->errString = basenames[base_no];
2781 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
2787 (ord = zebraExplain_lookup_attr_str(zh->reg->zei,
2790 /* we have a match for a raw string attribute */
2792 ords[ord_no++] = ord;
2793 attp.local_attributes = 0; /* no more attributes */
2799 if ((r = att_getentbyatt (zh, &attp, attributeset, use_value,
2802 yaz_log(YLOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
2803 attributeset, use_value);
2806 errCode = YAZ_BIB1_UNSUPP_USE_ATTRIBUTE;
2808 errString = odr_strdup(stream, use_string);
2812 sprintf(val_str, "%d", use_value);
2813 errString = odr_strdup(stream, val_str);
2817 errCode = YAZ_BIB1_UNSUPP_ATTRIBUTE_SET;
2822 for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
2823 local_attr = local_attr->next)
2825 ord = zebraExplain_lookup_attr_su(zh->reg->zei,
2826 attp.attset_ordinal,
2829 ords[ord_no++] = ord;
2832 if (!bases_ok && errCode)
2834 zh->errCode = errCode;
2835 zh->errString = errString;
2844 /* prepare dictionary scanning */
2856 yaz_log(YLOG_DEBUG, "rpn_scan pos=%d num=%d before=%d "
2857 "after=%d before+after=%d",
2858 pos, num, before, after, before+after);
2859 scan_info_array = (struct scan_info *)
2860 odr_malloc(stream, ord_no * sizeof(*scan_info_array));
2861 for (i = 0; i < ord_no; i++)
2863 int j, prefix_len = 0;
2864 int before_tmp = before, after_tmp = after;
2865 struct scan_info *scan_info = scan_info_array + i;
2866 struct rpn_char_map_info rcmi;
2868 rpn_char_map_prepare (zh->reg, reg_id, &rcmi);
2870 scan_info->before = before;
2871 scan_info->after = after;
2872 scan_info->odr = stream;
2874 scan_info->list = (struct scan_info_entry *)
2875 odr_malloc(stream, (before+after) * sizeof(*scan_info->list));
2876 for (j = 0; j<before+after; j++)
2877 scan_info->list[j].term = NULL;
2879 prefix_len += key_SU_encode (ords[i], termz + prefix_len);
2880 termz[prefix_len++] = reg_id;
2881 termz[prefix_len] = 0;
2882 strcpy(scan_info->prefix, termz);
2884 if (trans_scan_term(zh, zapt, termz+prefix_len, reg_id) == ZEBRA_FAIL)
2887 dict_scan(zh->reg->dict, termz, &before_tmp, &after_tmp,
2888 scan_info, scan_handle);
2890 glist = (ZebraScanEntry *)
2891 odr_malloc(stream, (before+after)*sizeof(*glist));
2893 rset_nmem = nmem_create();
2894 kc = zebra_key_control_create(zh);
2896 /* consider terms after main term */
2897 for (i = 0; i < ord_no; i++)
2901 for (i = 0; i<after; i++)
2904 const char *mterm = NULL;
2907 int lo = i + pos-1; /* offset in result list */
2909 /* find: j0 is the first of the minimal values */
2910 for (j = 0; j < ord_no; j++)
2912 if (ptr[j] < before+after && ptr[j] >= 0 &&
2913 (tst = scan_info_array[j].list[ptr[j]].term) &&
2914 (!mterm || strcmp (tst, mterm) < 0))
2921 break; /* no value found, stop */
2923 /* get result set for first one , but only if it's within bounds */
2926 /* get result set for first term */
2927 scan_term_untrans(zh, stream->mem, reg_id,
2928 &glist[lo].term, mterm);
2929 rset = rset_trunc(zh, &scan_info_array[j0].list[ptr[j0]].isam_p, 1,
2930 glist[lo].term, strlen(glist[lo].term),
2931 NULL, 0, zapt->term->which, rset_nmem,
2934 ptr[j0]++; /* move index for this set .. */
2935 /* get result set for remaining scan terms */
2936 for (j = j0+1; j<ord_no; j++)
2938 if (ptr[j] < before+after && ptr[j] >= 0 &&
2939 (tst = scan_info_array[j].list[ptr[j]].term) &&
2940 !strcmp (tst, mterm))
2949 zh, &scan_info_array[j].list[ptr[j]].isam_p, 1,
2951 strlen(glist[lo].term), NULL, 0,
2952 zapt->term->which,rset_nmem,
2954 rset = rsmulti_or_create(rset_nmem, kc,
2955 2, kc->scope, rsets);
2962 /* merge with limit_set if given */
2967 rsets[1] = rset_dup(limit_set);
2969 rset = rsmulti_and_create(rset_nmem, kc,
2970 kc->scope, 2, rsets);
2973 count_set(rset, &glist[lo].occurrences);
2979 *num_entries -= (after-i);
2981 if (*num_entries < 0)
2984 nmem_destroy(rset_nmem);
2989 /* consider terms before main term */
2990 for (i = 0; i<ord_no; i++)
2993 for (i = 0; i<before; i++)
2996 const char *mterm = NULL;
2999 int lo = before-1-i; /* offset in result list */
3001 for (j = 0; j <ord_no; j++)
3003 if (ptr[j] < before && ptr[j] >= 0 &&
3004 (tst = scan_info_array[j].list[before-1-ptr[j]].term) &&
3005 (!mterm || strcmp (tst, mterm) > 0))
3014 scan_term_untrans (zh, stream->mem, reg_id,
3015 &glist[lo].term, mterm);
3018 (zh, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1,
3019 glist[lo].term, strlen(glist[lo].term),
3020 NULL, 0, zapt->term->which,rset_nmem,
3025 for (j = j0+1; j<ord_no; j++)
3027 if (ptr[j] < before && ptr[j] >= 0 &&
3028 (tst = scan_info_array[j].list[before-1-ptr[j]].term) &&
3029 !strcmp (tst, mterm))
3034 rsets[1] = rset_trunc(
3036 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1,
3038 strlen(glist[lo].term), NULL, 0,
3039 zapt->term->which, rset_nmem,
3041 rset = rsmulti_or_create(rset_nmem, kc,
3042 2, kc->scope, rsets);
3051 rsets[1] = rset_dup(limit_set);
3053 rset = rsmulti_and_create(rset_nmem, kc,
3054 kc->scope, 2, rsets);
3056 count_set (rset, &glist[lo].occurrences);
3060 nmem_destroy(rset_nmem);
3067 if (*num_entries <= 0)
3074 *list = glist + i; /* list is set to first 'real' entry */
3076 yaz_log(YLOG_DEBUG, "position = %d, num_entries = %d",
3077 *position, *num_entries);