1 /* $Id: zrpn.c,v 1.181 2005-04-29 10:37:12 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 static const struct key_control it_ctrl =
41 sizeof(struct it_key),
42 2, /* we have sysnos and seqnos in this key, nothing more */
44 key_logdump_txt, /* FIXME - clean up these functions */
49 const struct key_control *key_it_ctrl = &it_ctrl;
51 struct rpn_char_map_info
62 Z_AttributesPlusTerm *zapt;
66 static int log_level_set = 0;
67 static int log_level_rpn = 0;
69 static const char **rpn_char_map_handler(void *vp, const char **from, int len)
71 struct rpn_char_map_info *p = (struct rpn_char_map_info *) vp;
72 const char **out = zebra_maps_input(p->zm, p->reg_type, from, len, 0);
76 const char *outp = *out;
77 yaz_log(YLOG_LOG, "---");
80 yaz_log(YLOG_LOG, "%02X", *outp);
88 static void rpn_char_map_prepare(struct zebra_register *reg, int reg_type,
89 struct rpn_char_map_info *map_info)
91 map_info->zm = reg->zebra_maps;
92 map_info->reg_type = reg_type;
93 dict_grep_cmap(reg->dict, map_info, rpn_char_map_handler);
96 static int attr_find_ex(AttrType *src, oid_value *attributeSetP,
97 const char **string_value)
101 num_attributes = src->zapt->attributes->num_attributes;
102 while (src->major < num_attributes)
104 Z_AttributeElement *element;
106 element = src->zapt->attributes->attributes[src->major];
107 if (src->type == *element->attributeType)
109 switch (element->which)
111 case Z_AttributeValue_numeric:
113 if (element->attributeSet && attributeSetP)
117 attrset = oid_getentbyoid(element->attributeSet);
118 *attributeSetP = attrset->value;
120 return *element->value.numeric;
122 case Z_AttributeValue_complex:
123 if (src->minor >= element->value.complex->num_list)
125 if (element->attributeSet && attributeSetP)
129 attrset = oid_getentbyoid(element->attributeSet);
130 *attributeSetP = attrset->value;
132 if (element->value.complex->list[src->minor]->which ==
133 Z_StringOrNumeric_numeric)
137 *element->value.complex->list[src->minor-1]->u.numeric;
139 else if (element->value.complex->list[src->minor]->which ==
140 Z_StringOrNumeric_string)
146 element->value.complex->list[src->minor-1]->u.string;
160 static int attr_find(AttrType *src, oid_value *attributeSetP)
162 return attr_find_ex(src, attributeSetP, 0);
165 static void attr_init(AttrType *src, Z_AttributesPlusTerm *zapt,
188 static void term_untrans(ZebraHandle zh, int reg_type,
189 char *dst, const char *src)
194 const char *cp = zebra_maps_output(zh->reg->zebra_maps,
196 if (!cp && len < IT_MAX_WORD-1)
199 while (*cp && len < IT_MAX_WORD-1)
205 static void add_isam_p(const char *name, const char *info,
210 log_level_rpn = yaz_log_module_level("rpn");
213 if (p->isam_p_indx == p->isam_p_size)
215 ISAM_P *new_isam_p_buf;
219 p->isam_p_size = 2*p->isam_p_size + 100;
220 new_isam_p_buf = (ISAM_P *) xmalloc(sizeof(*new_isam_p_buf) *
224 memcpy(new_isam_p_buf, p->isam_p_buf,
225 p->isam_p_indx * sizeof(*p->isam_p_buf));
226 xfree(p->isam_p_buf);
228 p->isam_p_buf = new_isam_p_buf;
231 new_term_no = (int *) xmalloc(sizeof(*new_term_no) * p->isam_p_size);
234 memcpy(new_term_no, p->isam_p_buf,
235 p->isam_p_indx * sizeof(*p->term_no));
238 p->term_no = new_term_no;
241 assert(*info == sizeof(*p->isam_p_buf));
242 memcpy(p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
249 char term_tmp[IT_MAX_WORD];
251 int len = key_SU_decode (&su_code, name);
253 term_untrans (p->zh, p->reg_type, term_tmp, name+len+1);
254 yaz_log(log_level_rpn, "grep: %d %c %s", su_code, name[len], term_tmp);
255 zebraExplain_lookup_ord (p->zh->reg->zei,
256 su_code, &db, &set, &use);
257 yaz_log(log_level_rpn, "grep: set=%d use=%d db=%s", set, use, db);
259 resultSetAddTerm(p->zh, p->termset, name[len], db,
266 static int grep_handle(char *name, const char *info, void *p)
268 add_isam_p(name, info, (struct grep_info *) p);
272 static int term_pre(ZebraMaps zebra_maps, int reg_type, const char **src,
273 const char *ct1, const char *ct2, int first)
275 const char *s1, *s0 = *src;
278 /* skip white space */
281 if (ct1 && strchr(ct1, *s0))
283 if (ct2 && strchr(ct2, *s0))
286 map = zebra_maps_input(zebra_maps, reg_type, &s1, strlen(s1), first);
287 if (**map != *CHR_SPACE)
296 static void esc_str(char *out_buf, int out_size,
297 const char *in_buf, int in_size)
303 assert(out_size > 20);
305 for (k = 0; k<in_size; k++)
307 int c = in_buf[k] & 0xff;
309 if (c < 32 || c > 126)
313 sprintf(out_buf +strlen(out_buf), "%02X:%c ", c, pc);
314 if (strlen(out_buf) > out_size-20)
316 strcat(out_buf, "..");
322 #define REGEX_CHARS " []()|.*+?!"
324 /* term_100: handle term, where trunc = none(no operators at all) */
325 static int term_100(ZebraMaps zebra_maps, int reg_type,
326 const char **src, char *dst, int space_split,
334 const char *space_start = 0;
335 const char *space_end = 0;
337 if (!term_pre(zebra_maps, reg_type, src, NULL, NULL, !space_split))
344 map = zebra_maps_search(zebra_maps, reg_type, &s0, strlen(s0),
348 if (**map == *CHR_SPACE)
351 else /* complete subfield only. */
353 if (**map == *CHR_SPACE)
354 { /* save space mapping for later .. */
359 else if (space_start)
360 { /* reload last space */
361 while (space_start < space_end)
363 if (strchr(REGEX_CHARS, *space_start))
365 dst_term[j++] = *space_start;
366 dst[i++] = *space_start++;
369 space_start = space_end = 0;
372 /* add non-space char */
373 memcpy(dst_term+j, s1, s0 - s1);
379 if (strchr(REGEX_CHARS, *s1))
387 esc_str(tmpbuf, sizeof(tmpbuf), map[0], strlen(map[0]));
389 strcpy(dst + i, map[0]);
399 /* term_101: handle term, where trunc = Process # */
400 static int term_101(ZebraMaps zebra_maps, int reg_type,
401 const char **src, char *dst, int space_split,
409 if (!term_pre(zebra_maps, reg_type, src, "#", "#", !space_split))
418 dst_term[j++] = *s0++;
424 map = zebra_maps_search(zebra_maps, reg_type, &s0, strlen(s0),
426 if (space_split && **map == *CHR_SPACE)
429 /* add non-space char */
430 memcpy(dst_term+j, s1, s0 - s1);
436 if (strchr(REGEX_CHARS, *s1))
444 esc_str(tmpbuf, sizeof(tmpbuf), map[0], strlen(map[0]));
446 strcpy(dst + i, map[0]);
452 dst_term[j++] = '\0';
457 /* term_103: handle term, where trunc = re-2 (regular expressions) */
458 static int term_103(ZebraMaps zebra_maps, int reg_type, const char **src,
459 char *dst, int *errors, int space_split,
467 if (!term_pre(zebra_maps, reg_type, src, "^\\()[].*+?|", "(", !space_split))
470 if (errors && *s0 == '+' && s0[1] && s0[2] == '+' && s0[3] &&
471 isdigit(((const unsigned char *)s0)[1]))
473 *errors = s0[1] - '0';
480 if (strchr("^\\()[].*+?|-", *s0))
489 map = zebra_maps_search(zebra_maps, reg_type, &s0, strlen(s0),
491 if (space_split && **map == *CHR_SPACE)
494 /* add non-space char */
495 memcpy(dst_term+j, s1, s0 - s1);
501 if (strchr(REGEX_CHARS, *s1))
509 esc_str(tmpbuf, sizeof(tmpbuf), map[0], strlen(map[0]));
511 strcpy(dst + i, map[0]);
523 /* term_103: handle term, where trunc = re-1 (regular expressions) */
524 static int term_102(ZebraMaps zebra_maps, int reg_type, const char **src,
525 char *dst, int space_split, char *dst_term)
527 return term_103(zebra_maps, reg_type, src, dst, NULL, space_split,
532 /* term_104: handle term, where trunc = Process # and ! */
533 static int term_104(ZebraMaps zebra_maps, int reg_type,
534 const char **src, char *dst, int space_split,
542 if (!term_pre(zebra_maps, reg_type, src, "?*#", "?*#", !space_split))
549 dst_term[j++] = *s0++;
550 if (*s0 >= '0' && *s0 <= '9')
553 while (*s0 >= '0' && *s0 <= '9')
555 limit = limit * 10 + (*s0 - '0');
556 dst_term[j++] = *s0++;
576 dst_term[j++] = *s0++;
581 dst_term[j++] = *s0++;
586 map = zebra_maps_input(zebra_maps, reg_type, &s0, strlen(s0), 0);
587 if (space_split && **map == *CHR_SPACE)
591 if (strchr(REGEX_CHARS, *s1))
599 dst_term[j++] = '\0';
604 /* term_105/106: handle term, where trunc = Process * and ! and right trunc */
605 static int term_105(ZebraMaps zebra_maps, int reg_type,
606 const char **src, char *dst, int space_split,
607 char *dst_term, int right_truncate)
614 if (!term_pre(zebra_maps, reg_type, src, "*!", "*!", !space_split))
623 dst_term[j++] = *s0++;
628 dst_term[j++] = *s0++;
633 map = zebra_maps_input(zebra_maps, reg_type, &s0, strlen(s0), 0);
634 if (space_split && **map == *CHR_SPACE)
638 if (strchr(REGEX_CHARS, *s1))
652 dst_term[j++] = '\0';
658 /* gen_regular_rel - generate regular expression from relation
659 * val: border value (inclusive)
660 * islt: 1 if <=; 0 if >=.
662 static void gen_regular_rel(char *dst, int val, int islt)
669 yaz_log(YLOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
673 strcpy(dst, "(-[0-9]+|(");
681 strcpy(dst, "([0-9]+|-(");
693 sprintf(numstr, "%d", val);
694 for (w = strlen(numstr); --w >= 0; pos++)
713 strcpy(dst + dst_p, numstr);
714 dst_p = strlen(dst) - pos - 1;
742 for (i = 0; i<pos; i++)
755 /* match everything less than 10^(pos-1) */
757 for (i = 1; i<pos; i++)
758 strcat(dst, "[0-9]?");
762 /* match everything greater than 10^pos */
763 for (i = 0; i <= pos; i++)
764 strcat(dst, "[0-9]");
765 strcat(dst, "[0-9]*");
770 void string_rel_add_char(char **term_p, const char *src, int *indx)
772 if (src[*indx] == '\\')
773 *(*term_p)++ = src[(*indx)++];
774 *(*term_p)++ = src[(*indx)++];
778 * > abc ([b-].*|a[c-].*|ab[d-].*|abc.+)
779 * ([^-a].*|a[^-b].*ab[^-c].*|abc.+)
780 * >= abc ([b-].*|a[c-].*|ab[c-].*)
781 * ([^-a].*|a[^-b].*|ab[c-].*)
782 * < abc ([-0].*|a[-a].*|ab[-b].*)
783 * ([^a-].*|a[^b-].*|ab[^c-].*)
784 * <= abc ([-0].*|a[-a].*|ab[-b].*|abc)
785 * ([^a-].*|a[^b-].*|ab[^c-].*|abc)
787 static int string_relation(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
788 const char **term_sub, char *term_dict,
789 oid_value attributeSet,
790 int reg_type, int space_split, char *term_dst,
796 char *term_tmp = term_dict + strlen(term_dict);
797 char term_component[2*IT_MAX_WORD+20];
799 attr_init(&relation, zapt, 2);
800 relation_value = attr_find(&relation, NULL);
803 yaz_log(YLOG_DEBUG, "string relation value=%d", relation_value);
804 switch (relation_value)
807 if (!term_100(zh->reg->zebra_maps, reg_type,
808 term_sub, term_component,
809 space_split, term_dst))
811 yaz_log(log_level_rpn, "Relation <");
814 for (i = 0; term_component[i]; )
821 string_rel_add_char(&term_tmp, term_component, &j);
826 string_rel_add_char(&term_tmp, term_component, &i);
833 if ((term_tmp - term_dict) > IT_MAX_WORD)
840 if (!term_100(zh->reg->zebra_maps, reg_type,
841 term_sub, term_component,
842 space_split, term_dst))
844 yaz_log(log_level_rpn, "Relation <=");
847 for (i = 0; term_component[i]; )
852 string_rel_add_char(&term_tmp, term_component, &j);
856 string_rel_add_char(&term_tmp, term_component, &i);
865 if ((term_tmp - term_dict) > IT_MAX_WORD)
868 for (i = 0; term_component[i]; )
869 string_rel_add_char(&term_tmp, term_component, &i);
874 if (!term_100 (zh->reg->zebra_maps, reg_type,
875 term_sub, term_component, space_split, term_dst))
877 yaz_log(log_level_rpn, "Relation >");
880 for (i = 0; term_component[i];)
885 string_rel_add_char(&term_tmp, term_component, &j);
890 string_rel_add_char(&term_tmp, term_component, &i);
898 if ((term_tmp - term_dict) > IT_MAX_WORD)
901 for (i = 0; term_component[i];)
902 string_rel_add_char(&term_tmp, term_component, &i);
909 if (!term_100(zh->reg->zebra_maps, reg_type, term_sub,
910 term_component, space_split, term_dst))
912 yaz_log(log_level_rpn, "Relation >=");
915 for (i = 0; term_component[i];)
922 string_rel_add_char(&term_tmp, term_component, &j);
925 if (term_component[i+1])
929 string_rel_add_char(&term_tmp, term_component, &i);
933 string_rel_add_char(&term_tmp, term_component, &i);
940 if ((term_tmp - term_dict) > IT_MAX_WORD)
949 yaz_log(log_level_rpn, "Relation =");
950 if (!term_100(zh->reg->zebra_maps, reg_type, term_sub,
951 term_component, space_split, term_dst))
953 strcat(term_tmp, "(");
954 strcat(term_tmp, term_component);
955 strcat(term_tmp, ")");
964 static ZEBRA_RES string_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
965 const char **term_sub,
966 oid_value attributeSet, NMEM stream,
967 struct grep_info *grep_info,
968 int reg_type, int complete_flag,
969 int num_bases, char **basenames,
970 char *term_dst, int xpath_use);
972 static ZEBRA_RES term_trunc(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
973 const char **term_sub,
974 oid_value attributeSet, NMEM stream,
975 struct grep_info *grep_info,
976 int reg_type, int complete_flag,
977 int num_bases, char **basenames,
979 const char *rank_type, int xpath_use,
985 grep_info->isam_p_indx = 0;
986 res = string_term(zh, zapt, term_sub, attributeSet, stream, grep_info,
987 reg_type, complete_flag, num_bases, basenames,
988 term_dst, xpath_use);
991 if (!*term_sub) /* no more terms ? */
993 yaz_log(log_level_rpn, "term: %s", term_dst);
994 *rset = rset_trunc(zh, grep_info->isam_p_buf,
995 grep_info->isam_p_indx, term_dst,
996 strlen(term_dst), rank_type, 1 /* preserve pos */,
997 zapt->term->which, rset_nmem,
998 key_it_ctrl, key_it_ctrl->scope);
1004 static char *nmem_strdup_i(NMEM nmem, int v)
1007 sprintf(val_str, "%d", v);
1008 return nmem_strdup(nmem, val_str);
1011 static ZEBRA_RES string_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1012 const char **term_sub,
1013 oid_value attributeSet, NMEM stream,
1014 struct grep_info *grep_info,
1015 int reg_type, int complete_flag,
1016 int num_bases, char **basenames,
1017 char *term_dst, int xpath_use)
1019 char term_dict[2*IT_MAX_WORD+4000];
1021 AttrType truncation;
1022 int truncation_value;
1025 const char *use_string = 0;
1026 oid_value curAttributeSet = attributeSet;
1028 struct rpn_char_map_info rcmi;
1029 int space_split = complete_flag ? 0 : 1;
1031 int bases_ok = 0; /* no of databases with OK attribute */
1032 int errCode = 0; /* err code (if any is not OK) */
1033 char *errString = 0; /* addinfo */
1035 rpn_char_map_prepare (zh->reg, reg_type, &rcmi);
1036 attr_init(&use, zapt, 1);
1037 use_value = attr_find_ex(&use, &curAttributeSet, &use_string);
1038 yaz_log(log_level_rpn, "string_term, use value %d", use_value);
1039 attr_init(&truncation, zapt, 5);
1040 truncation_value = attr_find(&truncation, NULL);
1041 yaz_log(log_level_rpn, "truncation value %d", truncation_value);
1043 if (use_value == -1) /* no attribute - assumy "any" */
1045 for (base_no = 0; base_no < num_bases; base_no++)
1049 int regex_range = 0;
1052 data1_local_attribute id_xpath_attr;
1053 data1_local_attribute *local_attr;
1054 int max_pos, prefix_len = 0;
1059 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
1061 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1062 zh->errString = basenames[base_no];
1065 if (xpath_use > 0 && use_value == -2)
1067 /* xpath mode and we have a string attribute */
1068 attp.local_attributes = &id_xpath_attr;
1069 attp.attset_ordinal = VAL_IDXPATH;
1070 id_xpath_attr.next = 0;
1072 use_value = xpath_use; /* xpath_use as use-attribute now */
1073 id_xpath_attr.local = use_value;
1075 else if (curAttributeSet == VAL_IDXPATH && use_value >= 0)
1077 /* X-Path attribute, use numeric value directly */
1078 attp.local_attributes = &id_xpath_attr;
1079 attp.attset_ordinal = VAL_IDXPATH;
1080 id_xpath_attr.next = 0;
1081 id_xpath_attr.local = use_value;
1083 else if (use_string &&
1084 (ord = zebraExplain_lookup_attr_str(zh->reg->zei,
1087 /* we have a match for a raw string attribute */
1092 term_dict[prefix_len++] = '|';
1094 term_dict[prefix_len++] = '(';
1096 ord_len = key_SU_encode (ord, ord_buf);
1097 for (i = 0; i<ord_len; i++)
1099 term_dict[prefix_len++] = 1;
1100 term_dict[prefix_len++] = ord_buf[i];
1102 attp.local_attributes = 0; /* no more attributes */
1106 /* lookup in the .att files . Allow string as well */
1107 if ((r = att_getentbyatt (zh, &attp, curAttributeSet, use_value,
1110 yaz_log(YLOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1111 curAttributeSet, use_value, r);
1114 /* set was found, but value wasn't defined */
1115 errCode = YAZ_BIB1_UNSUPP_USE_ATTRIBUTE;
1117 errString = nmem_strdup(stream, use_string);
1119 errString = nmem_strdup_i (stream, use_value);
1124 struct oident oident;
1126 oident.proto = PROTO_Z3950;
1127 oident.oclass = CLASS_ATTSET;
1128 oident.value = curAttributeSet;
1129 oid_ent_to_oid (&oident, oid);
1131 errCode = YAZ_BIB1_UNSUPP_ATTRIBUTE_SET;
1132 errString = nmem_strdup(stream, oident.desc);
1137 for (local_attr = attp.local_attributes; local_attr;
1138 local_attr = local_attr->next)
1143 ord = zebraExplain_lookup_attr_su(zh->reg->zei,
1144 attp.attset_ordinal,
1149 term_dict[prefix_len++] = '|';
1151 term_dict[prefix_len++] = '(';
1153 ord_len = key_SU_encode (ord, ord_buf);
1154 for (i = 0; i<ord_len; i++)
1156 term_dict[prefix_len++] = 1;
1157 term_dict[prefix_len++] = ord_buf[i];
1164 term_dict[prefix_len++] = ')';
1165 term_dict[prefix_len++] = 1;
1166 term_dict[prefix_len++] = reg_type;
1167 yaz_log(log_level_rpn, "reg_type = %d", term_dict[prefix_len-1]);
1168 term_dict[prefix_len] = '\0';
1170 switch (truncation_value)
1172 case -1: /* not specified */
1173 case 100: /* do not truncate */
1174 if (!string_relation (zh, zapt, &termp, term_dict,
1176 reg_type, space_split, term_dst,
1181 zh->errCode = relation_error;
1188 case 1: /* right truncation */
1189 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 2: /* keft 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 3: /* left&right truncation */
1209 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1210 if (!term_100(zh->reg->zebra_maps, reg_type,
1211 &termp, term_dict + j, space_split, term_dst))
1216 strcat(term_dict, ".*)");
1218 case 101: /* process # in term */
1219 term_dict[j++] = '(';
1220 if (!term_101(zh->reg->zebra_maps, reg_type,
1221 &termp, term_dict + j, space_split, term_dst))
1226 strcat(term_dict, ")");
1228 case 102: /* Regexp-1 */
1229 term_dict[j++] = '(';
1230 if (!term_102(zh->reg->zebra_maps, reg_type,
1231 &termp, term_dict + j, space_split, term_dst))
1236 strcat(term_dict, ")");
1238 case 103: /* Regexp-2 */
1240 term_dict[j++] = '(';
1242 if (!term_103(zh->reg->zebra_maps, reg_type,
1243 &termp, term_dict + j, ®ex_range,
1244 space_split, term_dst))
1249 strcat(term_dict, ")");
1250 case 104: /* process # and ! in term */
1251 term_dict[j++] = '(';
1252 if (!term_104(zh->reg->zebra_maps, reg_type,
1253 &termp, term_dict + j, space_split, term_dst))
1258 strcat(term_dict, ")");
1260 case 105: /* 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, 1))
1268 strcat(term_dict, ")");
1270 case 106: /* process * and ! in term */
1271 term_dict[j++] = '(';
1272 if (!term_105(zh->reg->zebra_maps, reg_type,
1273 &termp, term_dict + j, space_split, term_dst, 0))
1278 strcat(term_dict, ")");
1281 zh->errCode = YAZ_BIB1_UNSUPP_TRUNCATION_ATTRIBUTE;
1282 zh->errString = nmem_strdup_i(stream, truncation_value);
1288 const char *input = term_dict + prefix_len;
1289 esc_str(buf, sizeof(buf), input, strlen(input));
1293 yaz_log(log_level_rpn, "dict_lookup_grep: %s", term_dict+prefix_len);
1294 r = dict_lookup_grep(zh->reg->dict, term_dict, regex_range,
1295 grep_info, &max_pos, init_pos,
1298 yaz_log(YLOG_WARN, "dict_lookup_grep fail %d", r);
1303 zh->errCode = errCode;
1304 zh->errString = errString;
1308 yaz_log(YLOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1313 /* convert APT search term to UTF8 */
1314 static ZEBRA_RES zapt_term_to_utf8(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1318 Z_Term *term = zapt->term;
1320 switch (term->which)
1322 case Z_Term_general:
1323 if (zh->iconv_to_utf8 != 0)
1325 char *inbuf = term->u.general->buf;
1326 size_t inleft = term->u.general->len;
1327 char *outbuf = termz;
1328 size_t outleft = IT_MAX_WORD-1;
1331 ret = yaz_iconv(zh->iconv_to_utf8, &inbuf, &inleft,
1333 if (ret == (size_t)(-1))
1335 ret = yaz_iconv(zh->iconv_to_utf8, 0, 0, 0, 0);
1337 YAZ_BIB1_QUERY_TERM_INCLUDES_CHARS_THAT_DO_NOT_TRANSLATE_INTO_;
1344 sizez = term->u.general->len;
1345 if (sizez > IT_MAX_WORD-1)
1346 sizez = IT_MAX_WORD-1;
1347 memcpy (termz, term->u.general->buf, sizez);
1348 termz[sizez] = '\0';
1351 case Z_Term_characterString:
1352 sizez = strlen(term->u.characterString);
1353 if (sizez > IT_MAX_WORD-1)
1354 sizez = IT_MAX_WORD-1;
1355 memcpy (termz, term->u.characterString, sizez);
1356 termz[sizez] = '\0';
1359 zh->errCode = YAZ_BIB1_UNSUPP_CODED_VALUE_FOR_TERM;
1365 /* convert APT SCAN term to internal cmap */
1366 static ZEBRA_RES trans_scan_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1367 char *termz, int reg_type)
1369 char termz0[IT_MAX_WORD];
1371 if (zapt_term_to_utf8(zh, zapt, termz0) == ZEBRA_FAIL)
1372 return ZEBRA_FAIL; /* error */
1376 const char *cp = (const char *) termz0;
1377 const char *cp_end = cp + strlen(cp);
1380 const char *space_map = NULL;
1383 while ((len = (cp_end - cp)) > 0)
1385 map = zebra_maps_input(zh->reg->zebra_maps, reg_type, &cp, len, 0);
1386 if (**map == *CHR_SPACE)
1391 for (src = space_map; *src; src++)
1394 for (src = *map; *src; src++)
1403 char *normalize_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1404 const char *termz, NMEM stream, unsigned reg_id)
1407 AttrType truncation;
1408 int truncation_value;
1411 attr_init(&truncation, zapt, 5);
1412 truncation_value = attr_find(&truncation, NULL);
1414 switch (truncation_value)
1434 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, ex_list,
1435 termz, strlen(termz));
1437 return nmem_strdup(stream, termz);
1440 char *buf = (char*) nmem_malloc(stream, wrbuf_len(wrbuf)+1);
1441 memcpy (buf, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1442 buf[wrbuf_len(wrbuf)] = '\0';
1447 static void grep_info_delete(struct grep_info *grep_info)
1450 xfree(grep_info->term_no);
1452 xfree(grep_info->isam_p_buf);
1455 static int grep_info_prepare(ZebraHandle zh,
1456 Z_AttributesPlusTerm *zapt,
1457 struct grep_info *grep_info,
1462 int termset_value_numeric;
1463 const char *termset_value_string;
1466 grep_info->term_no = 0;
1468 grep_info->isam_p_size = 0;
1469 grep_info->isam_p_buf = NULL;
1471 grep_info->reg_type = reg_type;
1472 grep_info->termset = 0;
1476 attr_init(&termset, zapt, 8);
1477 termset_value_numeric =
1478 attr_find_ex(&termset, NULL, &termset_value_string);
1479 if (termset_value_numeric != -1)
1482 const char *termset_name = 0;
1483 if (termset_value_numeric != -2)
1486 sprintf(resname, "%d", termset_value_numeric);
1487 termset_name = resname;
1490 termset_name = termset_value_string;
1491 yaz_log(log_level_rpn, "creating termset set %s", termset_name);
1492 grep_info->termset = resultSetAdd(zh, termset_name, 1);
1493 if (!grep_info->termset)
1495 zh->errCode = YAZ_BIB1_ILLEGAL_RESULT_SET_NAME;
1496 zh->errString = nmem_strdup(stream, termset_name);
1504 static ZEBRA_RES term_list_trunc(ZebraHandle zh,
1505 Z_AttributesPlusTerm *zapt,
1506 const char *termz_org,
1507 oid_value attributeSet,
1509 int reg_type, int complete_flag,
1510 const char *rank_type, int xpath_use,
1511 int num_bases, char **basenames,
1513 RSET **result_sets, int *num_result_sets)
1515 char term_dst[IT_MAX_WORD+1];
1516 struct grep_info grep_info;
1517 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1518 const char *termp = termz;
1521 *num_result_sets = 0;
1523 if (grep_info_prepare(zh, zapt, &grep_info, reg_type, stream))
1529 if (alloc_sets == *num_result_sets)
1532 RSET *rnew = (RSET *) nmem_malloc(stream, (alloc_sets+add) *
1535 memcpy(rnew, *result_sets, alloc_sets * sizeof(*rnew));
1536 alloc_sets = alloc_sets + add;
1537 *result_sets = rnew;
1539 res = term_trunc(zh, zapt, &termp, attributeSet,
1541 reg_type, complete_flag,
1542 num_bases, basenames,
1543 term_dst, rank_type,
1544 xpath_use, rset_nmem,
1545 &(*result_sets)[*num_result_sets]);
1546 if (res != ZEBRA_OK)
1549 for (i = 0; i < *num_result_sets; i++)
1550 rset_delete((*result_sets)[i]);
1551 grep_info_delete (&grep_info);
1554 if ((*result_sets)[*num_result_sets] == 0)
1556 (*num_result_sets)++;
1558 grep_info_delete(&grep_info);
1562 static ZEBRA_RES rpn_search_APT_phrase(ZebraHandle zh,
1563 Z_AttributesPlusTerm *zapt,
1564 const char *termz_org,
1565 oid_value attributeSet,
1567 int reg_type, int complete_flag,
1568 const char *rank_type, int xpath_use,
1569 int num_bases, char **basenames,
1573 RSET *result_sets = 0;
1574 int num_result_sets = 0;
1576 term_list_trunc(zh, zapt, termz_org, attributeSet,
1577 stream, reg_type, complete_flag,
1578 rank_type, xpath_use,
1579 num_bases, basenames,
1581 &result_sets, &num_result_sets);
1582 if (res != ZEBRA_OK)
1584 if (num_result_sets == 0)
1585 *rset = rsnull_create (rset_nmem, key_it_ctrl);
1586 else if (num_result_sets == 1)
1587 *rset = result_sets[0];
1589 *rset = rsprox_create(rset_nmem, key_it_ctrl, key_it_ctrl->scope,
1590 num_result_sets, result_sets,
1591 1 /* ordered */, 0 /* exclusion */,
1592 3 /* relation */, 1 /* distance */);
1598 static ZEBRA_RES rpn_search_APT_or_list(ZebraHandle zh,
1599 Z_AttributesPlusTerm *zapt,
1600 const char *termz_org,
1601 oid_value attributeSet,
1603 int reg_type, int complete_flag,
1604 const char *rank_type,
1606 int num_bases, char **basenames,
1610 RSET *result_sets = 0;
1611 int num_result_sets = 0;
1613 term_list_trunc(zh, zapt, termz_org, attributeSet,
1614 stream, reg_type, complete_flag,
1615 rank_type, xpath_use,
1616 num_bases, basenames,
1618 &result_sets, &num_result_sets);
1619 if (res != ZEBRA_OK)
1621 if (num_result_sets == 0)
1622 *rset = rsnull_create (rset_nmem, key_it_ctrl);
1623 else if (num_result_sets == 1)
1624 *rset = result_sets[0];
1626 *rset = rsmulti_or_create(rset_nmem, key_it_ctrl, key_it_ctrl->scope,
1627 num_result_sets, result_sets);
1633 static ZEBRA_RES rpn_search_APT_and_list(ZebraHandle zh,
1634 Z_AttributesPlusTerm *zapt,
1635 const char *termz_org,
1636 oid_value attributeSet,
1638 int reg_type, int complete_flag,
1639 const char *rank_type,
1641 int num_bases, char **basenames,
1645 RSET *result_sets = 0;
1646 int num_result_sets = 0;
1648 term_list_trunc(zh, zapt, termz_org, attributeSet,
1649 stream, reg_type, complete_flag,
1650 rank_type, xpath_use,
1651 num_bases, basenames,
1653 &result_sets, &num_result_sets);
1654 if (res != ZEBRA_OK)
1656 if (num_result_sets == 0)
1657 *rset = rsnull_create (rset_nmem, key_it_ctrl);
1658 else if (num_result_sets == 1)
1659 *rset = result_sets[0];
1661 *rset = rsmulti_and_create(rset_nmem, key_it_ctrl, key_it_ctrl->scope,
1662 num_result_sets, result_sets);
1668 static int numeric_relation(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1669 const char **term_sub,
1671 oid_value attributeSet,
1672 struct grep_info *grep_info,
1682 char *term_tmp = term_dict + strlen(term_dict);
1685 attr_init(&relation, zapt, 2);
1686 relation_value = attr_find(&relation, NULL);
1688 yaz_log(log_level_rpn, "numeric relation value=%d", relation_value);
1690 if (!term_100(zh->reg->zebra_maps, reg_type, term_sub, term_tmp, 1,
1693 term_value = atoi (term_tmp);
1694 switch (relation_value)
1697 yaz_log(log_level_rpn, "Relation <");
1698 gen_regular_rel(term_tmp, term_value-1, 1);
1701 yaz_log(log_level_rpn, "Relation <=");
1702 gen_regular_rel(term_tmp, term_value, 1);
1705 yaz_log(log_level_rpn, "Relation >=");
1706 gen_regular_rel(term_tmp, term_value, 0);
1709 yaz_log(log_level_rpn, "Relation >");
1710 gen_regular_rel(term_tmp, term_value+1, 0);
1714 yaz_log(log_level_rpn, "Relation =");
1715 sprintf(term_tmp, "(0*%d)", term_value);
1721 yaz_log(log_level_rpn, "dict_lookup_grep: %s", term_tmp);
1722 r = dict_lookup_grep(zh->reg->dict, term_dict, 0, grep_info, max_pos,
1725 yaz_log(YLOG_WARN, "dict_lookup_grep fail, rel = gt: %d", r);
1726 yaz_log(log_level_rpn, "%d positions", grep_info->isam_p_indx);
1730 static ZEBRA_RES numeric_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1731 const char **term_sub,
1732 oid_value attributeSet,
1733 struct grep_info *grep_info,
1734 int reg_type, int complete_flag,
1735 int num_bases, char **basenames,
1736 char *term_dst, int xpath_use, NMEM stream)
1738 char term_dict[2*IT_MAX_WORD+2];
1742 const char *use_string = 0;
1743 oid_value curAttributeSet = attributeSet;
1745 struct rpn_char_map_info rcmi;
1747 int bases_ok = 0; /* no of databases with OK attribute */
1748 int errCode = 0; /* err code (if any is not OK) */
1749 char *errString = 0; /* addinfo */
1751 rpn_char_map_prepare (zh->reg, reg_type, &rcmi);
1752 attr_init(&use, zapt, 1);
1753 use_value = attr_find_ex(&use, &curAttributeSet, &use_string);
1755 if (use_value == -1)
1758 for (base_no = 0; base_no < num_bases; base_no++)
1761 data1_local_attribute id_xpath_attr;
1762 data1_local_attribute *local_attr;
1763 int max_pos, prefix_len = 0;
1764 int relation_error = 0;
1767 if (use_value == -2) /* string attribute (assume IDXPATH/any) */
1769 use_value = xpath_use;
1770 attp.local_attributes = &id_xpath_attr;
1771 attp.attset_ordinal = VAL_IDXPATH;
1772 id_xpath_attr.next = 0;
1773 id_xpath_attr.local = use_value;
1775 else if (curAttributeSet == VAL_IDXPATH)
1777 attp.local_attributes = &id_xpath_attr;
1778 attp.attset_ordinal = VAL_IDXPATH;
1779 id_xpath_attr.next = 0;
1780 id_xpath_attr.local = use_value;
1784 if ((r = att_getentbyatt (zh, &attp, curAttributeSet, use_value,
1787 yaz_log(YLOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1788 curAttributeSet, use_value, r);
1791 errCode = YAZ_BIB1_UNSUPP_USE_ATTRIBUTE;
1793 errString = nmem_strdup(stream, use_string);
1795 errString = nmem_strdup_i (stream, use_value);
1798 errCode = YAZ_BIB1_UNSUPP_ATTRIBUTE_SET;
1802 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
1804 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1805 zh->errString = basenames[base_no];
1808 for (local_attr = attp.local_attributes; local_attr;
1809 local_attr = local_attr->next)
1815 ord = zebraExplain_lookup_attr_su(zh->reg->zei,
1816 attp.attset_ordinal,
1821 term_dict[prefix_len++] = '|';
1823 term_dict[prefix_len++] = '(';
1825 ord_len = key_SU_encode (ord, ord_buf);
1826 for (i = 0; i<ord_len; i++)
1828 term_dict[prefix_len++] = 1;
1829 term_dict[prefix_len++] = ord_buf[i];
1834 errCode = YAZ_BIB1_UNSUPP_USE_ATTRIBUTE;
1835 errString = nmem_strdup_i(stream, use_value);
1839 term_dict[prefix_len++] = ')';
1840 term_dict[prefix_len++] = 1;
1841 term_dict[prefix_len++] = reg_type;
1842 yaz_log(YLOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1843 term_dict[prefix_len] = '\0';
1844 if (!numeric_relation(zh, zapt, &termp, term_dict,
1845 attributeSet, grep_info, &max_pos, reg_type,
1846 term_dst, &relation_error))
1850 zh->errCode = relation_error;
1860 zh->errCode = errCode;
1861 zh->errString = errString;
1865 yaz_log(YLOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1869 static ZEBRA_RES rpn_search_APT_numeric(ZebraHandle zh,
1870 Z_AttributesPlusTerm *zapt,
1872 oid_value attributeSet,
1874 int reg_type, int complete_flag,
1875 const char *rank_type, int xpath_use,
1876 int num_bases, char **basenames,
1880 char term_dst[IT_MAX_WORD+1];
1881 const char *termp = termz;
1882 RSET *result_sets = 0;
1883 int num_result_sets = 0;
1885 struct grep_info grep_info;
1888 yaz_log(log_level_rpn, "APT_numeric t='%s'", termz);
1889 if (grep_info_prepare(zh, zapt, &grep_info, reg_type, stream))
1893 if (alloc_sets == num_result_sets)
1896 RSET *rnew = (RSET *) nmem_malloc(stream, (alloc_sets+add) *
1899 memcpy(rnew, result_sets, alloc_sets * sizeof(*rnew));
1900 alloc_sets = alloc_sets + add;
1903 yaz_log(YLOG_DEBUG, "APT_numeric termp=%s", termp);
1904 grep_info.isam_p_indx = 0;
1905 res = numeric_term(zh, zapt, &termp, attributeSet, &grep_info,
1906 reg_type, complete_flag, num_bases, basenames,
1907 term_dst, xpath_use,
1909 if (res == ZEBRA_FAIL || termp == 0)
1911 yaz_log(YLOG_DEBUG, "term: %s", term_dst);
1912 result_sets[num_result_sets] =
1913 rset_trunc(zh, grep_info.isam_p_buf,
1914 grep_info.isam_p_indx, term_dst,
1915 strlen(term_dst), rank_type,
1916 0 /* preserve position */,
1917 zapt->term->which, rset_nmem,
1918 key_it_ctrl,key_it_ctrl->scope);
1919 if (!result_sets[num_result_sets])
1923 grep_info_delete(&grep_info);
1927 for (i = 0; i<num_result_sets; i++)
1928 rset_delete(result_sets[i]);
1931 if (num_result_sets == 0)
1932 *rset = rsnull_create(rset_nmem, key_it_ctrl);
1933 if (num_result_sets == 1)
1934 *rset = result_sets[0];
1936 *rset = rsmulti_and_create(rset_nmem, key_it_ctrl, key_it_ctrl->scope,
1937 num_result_sets, result_sets);
1943 static ZEBRA_RES rpn_search_APT_local(ZebraHandle zh,
1944 Z_AttributesPlusTerm *zapt,
1946 oid_value attributeSet,
1948 const char *rank_type, NMEM rset_nmem,
1954 *rset = rstemp_create(rset_nmem,key_it_ctrl,key_it_ctrl->scope,
1955 res_get (zh->res, "setTmpDir"),0 );
1956 rsfd = rset_open(*rset, RSETF_WRITE);
1964 rset_write (rsfd, &key);
1969 static ZEBRA_RES rpn_sort_spec(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1970 oid_value attributeSet, NMEM stream,
1971 Z_SortKeySpecList *sort_sequence,
1972 const char *rank_type,
1976 int sort_relation_value;
1977 AttrType sort_relation_type;
1982 Z_AttributeElement *ae;
1987 attr_init(&sort_relation_type, zapt, 7);
1988 sort_relation_value = attr_find(&sort_relation_type, &attributeSet);
1990 attr_init(&use_type, zapt, 1);
1991 use_value = attr_find(&use_type, &attributeSet);
1993 if (!sort_sequence->specs)
1995 sort_sequence->num_specs = 10;
1996 sort_sequence->specs = (Z_SortKeySpec **)
1997 nmem_malloc(stream, sort_sequence->num_specs *
1998 sizeof(*sort_sequence->specs));
1999 for (i = 0; i<sort_sequence->num_specs; i++)
2000 sort_sequence->specs[i] = 0;
2002 if (zapt->term->which != Z_Term_general)
2005 i = atoi_n ((char *) zapt->term->u.general->buf,
2006 zapt->term->u.general->len);
2007 if (i >= sort_sequence->num_specs)
2009 sprintf(termz, "%d", i);
2011 oe.proto = PROTO_Z3950;
2012 oe.oclass = CLASS_ATTSET;
2013 oe.value = attributeSet;
2014 if (!oid_ent_to_oid (&oe, oid))
2017 sks = (Z_SortKeySpec *) nmem_malloc(stream, sizeof(*sks));
2018 sks->sortElement = (Z_SortElement *)
2019 nmem_malloc(stream, sizeof(*sks->sortElement));
2020 sks->sortElement->which = Z_SortElement_generic;
2021 sk = sks->sortElement->u.generic = (Z_SortKey *)
2022 nmem_malloc(stream, sizeof(*sk));
2023 sk->which = Z_SortKey_sortAttributes;
2024 sk->u.sortAttributes = (Z_SortAttributes *)
2025 nmem_malloc(stream, sizeof(*sk->u.sortAttributes));
2027 sk->u.sortAttributes->id = oid;
2028 sk->u.sortAttributes->list = (Z_AttributeList *)
2029 nmem_malloc(stream, sizeof(*sk->u.sortAttributes->list));
2030 sk->u.sortAttributes->list->num_attributes = 1;
2031 sk->u.sortAttributes->list->attributes = (Z_AttributeElement **)
2032 nmem_malloc(stream, sizeof(*sk->u.sortAttributes->list->attributes));
2033 ae = *sk->u.sortAttributes->list->attributes = (Z_AttributeElement *)
2034 nmem_malloc(stream, sizeof(**sk->u.sortAttributes->list->attributes));
2035 ae->attributeSet = 0;
2036 ae->attributeType = (int *)
2037 nmem_malloc(stream, sizeof(*ae->attributeType));
2038 *ae->attributeType = 1;
2039 ae->which = Z_AttributeValue_numeric;
2040 ae->value.numeric = (int *)
2041 nmem_malloc(stream, sizeof(*ae->value.numeric));
2042 *ae->value.numeric = use_value;
2044 sks->sortRelation = (int *)
2045 nmem_malloc(stream, sizeof(*sks->sortRelation));
2046 if (sort_relation_value == 1)
2047 *sks->sortRelation = Z_SortKeySpec_ascending;
2048 else if (sort_relation_value == 2)
2049 *sks->sortRelation = Z_SortKeySpec_descending;
2051 *sks->sortRelation = Z_SortKeySpec_ascending;
2053 sks->caseSensitivity = (int *)
2054 nmem_malloc(stream, sizeof(*sks->caseSensitivity));
2055 *sks->caseSensitivity = 0;
2057 sks->which = Z_SortKeySpec_null;
2058 sks->u.null = odr_nullval ();
2059 sort_sequence->specs[i] = sks;
2060 *rset = rsnull_create (NULL, key_it_ctrl);
2065 static int parse_xpath(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2066 oid_value attributeSet,
2067 struct xpath_location_step *xpath, int max, NMEM mem)
2069 oid_value curAttributeSet = attributeSet;
2071 const char *use_string = 0;
2073 attr_init(&use, zapt, 1);
2074 attr_find_ex(&use, &curAttributeSet, &use_string);
2076 if (!use_string || *use_string != '/')
2079 return zebra_parse_xpath_str(use_string, xpath, max, mem);
2084 static RSET xpath_trunc(ZebraHandle zh, NMEM stream,
2085 int reg_type, const char *term, int use,
2086 oid_value curAttributeSet, NMEM rset_nmem)
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,key_it_ctrl);
2102 return rsnull_create (rset_nmem,key_it_ctrl);
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,
2128 key_it_ctrl, key_it_ctrl->scope);
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,
2140 oid_value curAttributeSet = attributeSet;
2147 yaz_log(YLOG_DEBUG, "xpath len=%d", xpath_len);
2148 for (i = 0; i<xpath_len; i++)
2150 yaz_log(log_level_rpn, "XPATH %d %s", i, xpath[i].part);
2154 curAttributeSet = VAL_IDXPATH;
2164 a[@attr = value]/b[@other = othervalue]
2166 /e/@a val range(e/,range(@a,freetext(w,1015,val),@a),e/)
2167 /a/b val range(b/a/,freetext(w,1016,val),b/a/)
2168 /a/b/@c val range(b/a/,range(@c,freetext(w,1016,val),@c),b/a/)
2169 /a/b[@c = y] val range(b/a/,freetext(w,1016,val),b/a/,@c = y)
2170 /a[@c = y]/b val range(a/,range(b/a/,freetext(w,1016,val),b/a/),a/,@c = y)
2171 /a[@c = x]/b[@c = y] range(a/,range(b/a/,freetext(w,1016,val),b/a/,@c = y),a/,@c = x)
2175 dict_grep_cmap (zh->reg->dict, 0, 0);
2177 for (base_no = 0; base_no < num_bases; base_no++)
2179 int level = xpath_len;
2182 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
2184 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
2185 zh->errString = basenames[base_no];
2188 while (--level >= 0)
2190 char xpath_rev[128];
2192 RSET rset_start_tag = 0, rset_end_tag = 0, rset_attr = 0;
2196 for (i = level; i >= 1; --i)
2198 const char *cp = xpath[i].part;
2204 memcpy (xpath_rev + len, "[^/]*", 5);
2207 else if (*cp == ' ')
2210 xpath_rev[len++] = 1;
2211 xpath_rev[len++] = ' ';
2215 xpath_rev[len++] = *cp;
2216 xpath_rev[len++] = '/';
2218 else if (i == 1) /* // case */
2220 xpath_rev[len++] = '.';
2221 xpath_rev[len++] = '*';
2226 if (xpath[level].predicate &&
2227 xpath[level].predicate->which == XPATH_PREDICATE_RELATION &&
2228 xpath[level].predicate->u.relation.name[0])
2230 WRBUF wbuf = wrbuf_alloc();
2231 wrbuf_puts(wbuf, xpath[level].predicate->u.relation.name+1);
2232 if (xpath[level].predicate->u.relation.value)
2234 const char *cp = xpath[level].predicate->u.relation.value;
2235 wrbuf_putc(wbuf, '=');
2239 if (strchr(REGEX_CHARS, *cp))
2240 wrbuf_putc(wbuf, '\\');
2241 wrbuf_putc(wbuf, *cp);
2245 wrbuf_puts(wbuf, "");
2246 rset_attr = xpath_trunc(
2247 zh, stream, '0', wrbuf_buf(wbuf), 3,
2248 curAttributeSet,rset_nmem);
2249 wrbuf_free(wbuf, 1);
2256 yaz_log(log_level_rpn, "xpath_rev (%d) = %s", level, xpath_rev);
2257 if (strlen(xpath_rev))
2259 rset_start_tag = xpath_trunc(zh, stream, '0',
2260 xpath_rev, 1, curAttributeSet, rset_nmem);
2262 rset_end_tag = xpath_trunc(zh, stream, '0',
2263 xpath_rev, 2, curAttributeSet, rset_nmem);
2265 rset = rsbetween_create(rset_nmem, key_it_ctrl,
2267 rset_start_tag, rset,
2268 rset_end_tag, rset_attr);
2279 static ZEBRA_RES rpn_search_APT(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2280 oid_value attributeSet, NMEM stream,
2281 Z_SortKeySpecList *sort_sequence,
2282 int num_bases, char **basenames,
2286 ZEBRA_RES res = ZEBRA_OK;
2288 char *search_type = NULL;
2289 char rank_type[128];
2292 char termz[IT_MAX_WORD+1];
2295 struct xpath_location_step xpath[10];
2299 log_level_rpn = yaz_log_module_level("rpn");
2302 zebra_maps_attr(zh->reg->zebra_maps, zapt, ®_id, &search_type,
2303 rank_type, &complete_flag, &sort_flag);
2305 yaz_log(YLOG_DEBUG, "reg_id=%c", reg_id);
2306 yaz_log(YLOG_DEBUG, "complete_flag=%d", complete_flag);
2307 yaz_log(YLOG_DEBUG, "search_type=%s", search_type);
2308 yaz_log(YLOG_DEBUG, "rank_type=%s", rank_type);
2310 if (zapt_term_to_utf8(zh, zapt, termz) == ZEBRA_FAIL)
2314 return rpn_sort_spec(zh, zapt, attributeSet, stream, sort_sequence,
2316 xpath_len = parse_xpath(zh, zapt, attributeSet, xpath, 10, stream);
2320 if (xpath[xpath_len-1].part[0] == '@')
2324 if (!strcmp(search_type, "phrase"))
2326 res = rpn_search_APT_phrase(zh, zapt, termz, attributeSet, stream,
2327 reg_id, complete_flag, rank_type,
2329 num_bases, basenames, rset_nmem,
2332 else if (!strcmp(search_type, "and-list"))
2334 res = rpn_search_APT_and_list(zh, zapt, termz, attributeSet, stream,
2335 reg_id, complete_flag, rank_type,
2337 num_bases, basenames, rset_nmem,
2340 else if (!strcmp(search_type, "or-list"))
2342 res = rpn_search_APT_or_list(zh, zapt, termz, attributeSet, stream,
2343 reg_id, complete_flag, rank_type,
2345 num_bases, basenames, rset_nmem,
2348 else if (!strcmp(search_type, "local"))
2350 res = rpn_search_APT_local(zh, zapt, termz, attributeSet, stream,
2351 rank_type, rset_nmem, rset);
2353 else if (!strcmp(search_type, "numeric"))
2355 res = rpn_search_APT_numeric(zh, zapt, termz, attributeSet, stream,
2356 reg_id, complete_flag, rank_type,
2358 num_bases, basenames, rset_nmem, rset);
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);
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);
2385 ZEBRA_RES rpn_search_top(ZebraHandle zh, Z_RPNStructure *zs,
2386 oid_value attributeSet,
2387 NMEM stream, NMEM rset_nmem,
2388 Z_SortKeySpecList *sort_sequence,
2389 int num_bases, char **basenames,
2392 RSET *result_sets = 0;
2393 int num_result_sets = 0;
2394 ZEBRA_RES res = rpn_search_structure(zh, zs, attributeSet,
2397 num_bases, basenames,
2398 &result_sets, &num_result_sets,
2400 if (res != ZEBRA_OK)
2403 for (i = 0; i<num_result_sets; i++)
2404 rset_delete(result_sets[i]);
2408 assert(num_result_sets == 1);
2409 assert(result_sets);
2410 assert(*result_sets);
2411 *result_set = *result_sets;
2415 ZEBRA_RES rpn_search_structure(ZebraHandle zh, Z_RPNStructure *zs,
2416 oid_value attributeSet,
2417 NMEM stream, NMEM rset_nmem,
2418 Z_SortKeySpecList *sort_sequence,
2419 int num_bases, char **basenames,
2420 RSET **result_sets, int *num_result_sets,
2421 Z_Operator *parent_op)
2423 *num_result_sets = 0;
2424 if (zs->which == Z_RPNStructure_complex)
2427 Z_Operator *zop = zs->u.complex->roperator;
2428 RSET *result_sets_l = 0;
2429 int num_result_sets_l = 0;
2430 RSET *result_sets_r = 0;
2431 int num_result_sets_r = 0;
2433 res = rpn_search_structure(zh, zs->u.complex->s1,
2434 attributeSet, stream, rset_nmem,
2436 num_bases, basenames,
2437 &result_sets_l, &num_result_sets_l,
2439 if (res != ZEBRA_OK)
2442 for (i = 0; i<num_result_sets_l; i++)
2443 rset_delete(result_sets_l[i]);
2446 res = rpn_search_structure(zh, zs->u.complex->s2,
2447 attributeSet, stream, rset_nmem,
2449 num_bases, basenames,
2450 &result_sets_r, &num_result_sets_r,
2452 if (res != ZEBRA_OK)
2455 for (i = 0; i<num_result_sets_l; i++)
2456 rset_delete(result_sets_l[i]);
2457 for (i = 0; i<num_result_sets_r; i++)
2458 rset_delete(result_sets_r[i]);
2462 /* make a new list of result for all children */
2463 *num_result_sets = num_result_sets_l + num_result_sets_r;
2464 *result_sets = nmem_malloc(stream, *num_result_sets *
2465 sizeof(**result_sets));
2466 memcpy(*result_sets, result_sets_l,
2467 num_result_sets_l * sizeof(**result_sets));
2468 memcpy(*result_sets + num_result_sets_l, result_sets_r,
2469 num_result_sets_r * sizeof(**result_sets));
2471 if (!parent_op || parent_op->which != zop->which
2472 || (zop->which != Z_Operator_and &&
2473 zop->which != Z_Operator_or))
2475 /* parent node different from this one (or non-present) */
2476 /* we must combine result sets now */
2480 case Z_Operator_and:
2481 rset = rsmulti_and_create(rset_nmem, key_it_ctrl,
2483 *num_result_sets, *result_sets);
2486 rset = rsmulti_or_create(rset_nmem, key_it_ctrl,
2488 *num_result_sets, *result_sets);
2490 case Z_Operator_and_not:
2491 rset = rsbool_create_not(rset_nmem, key_it_ctrl,
2496 case Z_Operator_prox:
2497 if (zop->u.prox->which != Z_ProximityOperator_known)
2499 zh->errCode = YAZ_BIB1_UNSUPP_PROX_UNIT_CODE;
2502 if (*zop->u.prox->u.known != Z_ProxUnit_word)
2504 char *val = (char *) nmem_malloc(stream, 16);
2505 zh->errCode = YAZ_BIB1_UNSUPP_PROX_UNIT_CODE;
2506 zh->errString = val;
2507 sprintf(val, "%d", *zop->u.prox->u.known);
2512 rset = rsprox_create(rset_nmem, key_it_ctrl,
2514 *num_result_sets, *result_sets,
2515 *zop->u.prox->ordered,
2516 (!zop->u.prox->exclusion ?
2517 0 : *zop->u.prox->exclusion),
2518 *zop->u.prox->relationType,
2519 *zop->u.prox->distance );
2523 zh->errCode = YAZ_BIB1_OPERATOR_UNSUPP;
2526 *num_result_sets = 1;
2527 *result_sets = nmem_malloc(stream, *num_result_sets *
2528 sizeof(**result_sets));
2529 (*result_sets)[0] = rset;
2532 else if (zs->which == Z_RPNStructure_simple)
2537 if (zs->u.simple->which == Z_Operand_APT)
2539 yaz_log(YLOG_DEBUG, "rpn_search_APT");
2540 res = rpn_search_APT(zh, zs->u.simple->u.attributesPlusTerm,
2541 attributeSet, stream, sort_sequence,
2542 num_bases, basenames, rset_nmem, &rset);
2543 if (res != ZEBRA_OK)
2546 else if (zs->u.simple->which == Z_Operand_resultSetId)
2548 yaz_log(YLOG_DEBUG, "rpn_search_ref");
2549 rset = resultSetRef(zh, zs->u.simple->u.resultSetId);
2552 zh->errCode = YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST;
2554 nmem_strdup(stream, zs->u.simple->u.resultSetId);
2561 zh->errCode = YAZ_BIB1_UNSUPP_SEARCH;
2564 *num_result_sets = 1;
2565 *result_sets = nmem_malloc(stream, *num_result_sets *
2566 sizeof(**result_sets));
2567 (*result_sets)[0] = rset;
2571 zh->errCode = YAZ_BIB1_UNSUPP_SEARCH;
2577 struct scan_info_entry {
2583 struct scan_info_entry *list;
2589 static int scan_handle (char *name, const char *info, int pos, void *client)
2591 int len_prefix, idx;
2592 struct scan_info *scan_info = (struct scan_info *) client;
2594 len_prefix = strlen(scan_info->prefix);
2595 if (memcmp (name, scan_info->prefix, len_prefix))
2598 idx = scan_info->after - pos + scan_info->before;
2603 scan_info->list[idx].term = (char *)
2604 odr_malloc(scan_info->odr, strlen(name + len_prefix)+1);
2605 strcpy(scan_info->list[idx].term, name + len_prefix);
2606 assert (*info == sizeof(ISAM_P));
2607 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAM_P));
2611 static void scan_term_untrans (ZebraHandle zh, NMEM stream, int reg_type,
2612 char **dst, const char *src)
2614 char term_src[IT_MAX_WORD];
2615 char term_dst[IT_MAX_WORD];
2617 term_untrans (zh, reg_type, term_src, src);
2619 if (zh->iconv_from_utf8 != 0)
2622 char *inbuf = term_src;
2623 size_t inleft = strlen(term_src);
2624 char *outbuf = term_dst;
2625 size_t outleft = sizeof(term_dst)-1;
2628 ret = yaz_iconv (zh->iconv_from_utf8, &inbuf, &inleft,
2630 if (ret == (size_t)(-1))
2633 len = outbuf - term_dst;
2634 *dst = nmem_malloc(stream, len + 1);
2636 memcpy (*dst, term_dst, len);
2640 *dst = nmem_strdup(stream, term_src);
2643 static void count_set (RSET r, int *count)
2650 yaz_log(YLOG_DEBUG, "count_set");
2653 rfd = rset_open (r, RSETF_READ);
2654 while (rset_read (rfd, &key,0 /* never mind terms */))
2656 if (key.mem[0] != psysno)
2658 psysno = key.mem[0];
2664 yaz_log(YLOG_DEBUG, "%d keys, %d records", kno, *count);
2667 ZEBRA_RES rpn_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
2668 oid_value attributeset,
2669 int num_bases, char **basenames,
2670 int *position, int *num_entries, ZebraScanEntry **list,
2671 int *is_partial, RSET limit_set, int return_zero)
2674 int pos = *position;
2675 int num = *num_entries;
2679 char termz[IT_MAX_WORD+20];
2682 const char *use_string = 0;
2683 struct scan_info *scan_info_array;
2684 ZebraScanEntry *glist;
2685 int ords[32], ord_no = 0;
2688 int bases_ok = 0; /* no of databases with OK attribute */
2689 int errCode = 0; /* err code (if any is not OK) */
2690 char *errString = 0; /* addinfo */
2693 char *search_type = NULL;
2694 char rank_type[128];
2697 NMEM rset_nmem = NULL;
2702 if (attributeset == VAL_NONE)
2703 attributeset = VAL_BIB1;
2708 int termset_value_numeric;
2709 const char *termset_value_string;
2710 attr_init(&termset, zapt, 8);
2711 termset_value_numeric =
2712 attr_find_ex(&termset, NULL, &termset_value_string);
2713 if (termset_value_numeric != -1)
2716 const char *termset_name = 0;
2718 if (termset_value_numeric != -2)
2721 sprintf(resname, "%d", termset_value_numeric);
2722 termset_name = resname;
2725 termset_name = termset_value_string;
2727 limit_set = resultSetRef (zh, termset_name);
2731 yaz_log(YLOG_DEBUG, "position = %d, num = %d set=%d",
2732 pos, num, attributeset);
2734 attr_init(&use, zapt, 1);
2735 use_value = attr_find_ex(&use, &attributeset, &use_string);
2737 if (zebra_maps_attr(zh->reg->zebra_maps, zapt, ®_id, &search_type,
2738 rank_type, &complete_flag, &sort_flag))
2741 zh->errCode = YAZ_BIB1_UNSUPP_ATTRIBUTE_TYPE;
2744 yaz_log(YLOG_DEBUG, "use_value = %d", use_value);
2746 if (use_value == -1)
2748 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
2750 data1_local_attribute *local_attr;
2754 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
2756 zh->errString = basenames[base_no];
2757 zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
2763 (ord = zebraExplain_lookup_attr_str(zh->reg->zei,
2766 /* we have a match for a raw string attribute */
2768 ords[ord_no++] = ord;
2769 attp.local_attributes = 0; /* no more attributes */
2775 if ((r = att_getentbyatt (zh, &attp, attributeset, use_value,
2778 yaz_log(YLOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
2779 attributeset, use_value);
2782 errCode = YAZ_BIB1_UNSUPP_USE_ATTRIBUTE;
2784 errString = odr_strdup(stream, use_string);
2788 sprintf(val_str, "%d", use_value);
2789 errString = odr_strdup(stream, val_str);
2793 errCode = YAZ_BIB1_UNSUPP_ATTRIBUTE_SET;
2798 for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
2799 local_attr = local_attr->next)
2801 ord = zebraExplain_lookup_attr_su(zh->reg->zei,
2802 attp.attset_ordinal,
2805 ords[ord_no++] = ord;
2808 if (!bases_ok && errCode)
2810 zh->errCode = errCode;
2811 zh->errString = errString;
2820 /* prepare dictionary scanning */
2823 zh->errCode = YAZ_BIB1_SCAN_UNSUPP_VALUE_OF_POSITION_IN_RESPONSE;
2834 yaz_log(YLOG_DEBUG, "rpn_scan pos=%d num=%d before=%d "
2835 "after=%d before+after=%d",
2836 pos, num, before, after, before+after);
2837 scan_info_array = (struct scan_info *)
2838 odr_malloc(stream, ord_no * sizeof(*scan_info_array));
2839 for (i = 0; i < ord_no; i++)
2841 int j, prefix_len = 0;
2842 int before_tmp = before, after_tmp = after;
2843 struct scan_info *scan_info = scan_info_array + i;
2844 struct rpn_char_map_info rcmi;
2846 rpn_char_map_prepare (zh->reg, reg_id, &rcmi);
2848 scan_info->before = before;
2849 scan_info->after = after;
2850 scan_info->odr = stream;
2852 scan_info->list = (struct scan_info_entry *)
2853 odr_malloc(stream, (before+after) * sizeof(*scan_info->list));
2854 for (j = 0; j<before+after; j++)
2855 scan_info->list[j].term = NULL;
2857 prefix_len += key_SU_encode (ords[i], termz + prefix_len);
2858 termz[prefix_len++] = reg_id;
2859 termz[prefix_len] = 0;
2860 strcpy(scan_info->prefix, termz);
2862 if (trans_scan_term(zh, zapt, termz+prefix_len, reg_id) == ZEBRA_FAIL)
2865 dict_scan(zh->reg->dict, termz, &before_tmp, &after_tmp,
2866 scan_info, scan_handle);
2868 glist = (ZebraScanEntry *)
2869 odr_malloc(stream, (before+after)*sizeof(*glist));
2871 rset_nmem = nmem_create();
2873 /* consider terms after main term */
2874 for (i = 0; i < ord_no; i++)
2878 for (i = 0; i<after; i++)
2881 const char *mterm = NULL;
2885 for (j = 0; j < ord_no; j++)
2887 if (ptr[j] < before+after && ptr[j] >= 0 &&
2888 (tst = scan_info_array[j].list[ptr[j]].term) &&
2889 (!mterm || strcmp (tst, mterm) < 0))
2897 scan_term_untrans(zh, stream->mem, reg_id,
2898 &glist[i+before].term, mterm);
2899 rset = rset_trunc(zh, &scan_info_array[j0].list[ptr[j0]].isam_p, 1,
2900 glist[i+before].term, strlen(glist[i+before].term),
2901 NULL, 0, zapt->term->which, rset_nmem,
2902 key_it_ctrl,key_it_ctrl->scope);
2904 for (j = j0+1; j<ord_no; j++)
2906 if (ptr[j] < before+after && ptr[j] >= 0 &&
2907 (tst = scan_info_array[j].list[ptr[j]].term) &&
2908 !strcmp (tst, mterm))
2914 rset_trunc(zh, &scan_info_array[j].list[ptr[j]].isam_p, 1,
2915 glist[i+before].term,
2916 strlen(glist[i+before].term), NULL, 0,
2917 zapt->term->which,rset_nmem,
2918 key_it_ctrl, key_it_ctrl->scope);
2919 rset = rsmulti_or_create(rset_nmem, key_it_ctrl,
2920 2, key_it_ctrl->scope, rsets);
2928 rsets[1] = rset_dup(limit_set);
2930 rset = rsmulti_and_create(rset_nmem, key_it_ctrl,
2931 key_it_ctrl->scope, 2, rsets);
2933 count_set(rset, &glist[i+before].occurrences);
2938 *num_entries -= (after-i);
2941 /* consider terms before main term */
2942 for (i = 0; i<ord_no; i++)
2945 for (i = 0; i<before; i++)
2948 const char *mterm = NULL;
2952 for (j = 0; j <ord_no; j++)
2954 if (ptr[j] < before && ptr[j] >= 0 &&
2955 (tst = scan_info_array[j].list[before-1-ptr[j]].term) &&
2956 (!mterm || strcmp (tst, mterm) > 0))
2965 scan_term_untrans (zh, stream->mem, reg_id,
2966 &glist[before-1-i].term, mterm);
2969 (zh, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1,
2970 glist[before-1-i].term, strlen(glist[before-1-i].term),
2971 NULL, 0, zapt->term->which,rset_nmem,
2972 key_it_ctrl,key_it_ctrl->scope);
2976 for (j = j0+1; j<ord_no; j++)
2978 if (ptr[j] < before && ptr[j] >= 0 &&
2979 (tst = scan_info_array[j].list[before-1-ptr[j]].term) &&
2980 !strcmp (tst, mterm))
2985 rsets[1] = rset_trunc(
2987 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1,
2988 glist[before-1-i].term,
2989 strlen(glist[before-1-i].term), NULL, 0,
2990 zapt->term->which, rset_nmem,
2991 key_it_ctrl, key_it_ctrl->scope);
2992 rset = rsmulti_or_create(rset_nmem, key_it_ctrl,
2993 2, key_it_ctrl->scope, rsets);
3002 rsets[1] = rset_dup(limit_set);
3004 rset = rsmulti_and_create(rset_nmem, key_it_ctrl,
3005 key_it_ctrl->scope, 2, rsets);
3007 count_set (rset, &glist[before-1-i].occurrences);
3018 nmem_destroy(rset_nmem);
3019 *list = glist + i; /* list is set to first 'real' entry */
3021 yaz_log(YLOG_DEBUG, "position = %d, num_entries = %d",
3022 *position, *num_entries);