1 /* $Id: zrpn.c,v 1.169 2005-02-25 10:08:44 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
34 #include <zebra_xpath.h>
39 /* maximum number of terms in an and/or/phrase item */
40 #define TERM_LIST_LENGTH_MAX 256
42 static const struct key_control it_ctrl =
44 sizeof(struct it_key),
45 2, /* we have sysnos and seqnos in this key, nothing more */
47 key_logdump_txt, /* FIXME - clean up these functions */
52 const struct key_control *key_it_ctrl = &it_ctrl;
54 struct rpn_char_map_info
65 Z_AttributesPlusTerm *zapt;
69 static int log_level_set = 0;
70 static int log_level_rpn = 0;
72 static const char **rpn_char_map_handler(void *vp, const char **from, int len)
74 struct rpn_char_map_info *p = (struct rpn_char_map_info *) vp;
75 const char **out = zebra_maps_input(p->zm, p->reg_type, from, len, 0);
79 const char *outp = *out;
80 yaz_log(YLOG_LOG, "---");
83 yaz_log(YLOG_LOG, "%02X", *outp);
91 static void rpn_char_map_prepare(struct zebra_register *reg, int reg_type,
92 struct rpn_char_map_info *map_info)
94 map_info->zm = reg->zebra_maps;
95 map_info->reg_type = reg_type;
96 dict_grep_cmap(reg->dict, map_info, rpn_char_map_handler);
99 static int attr_find_ex(AttrType *src, oid_value *attributeSetP,
100 const char **string_value)
104 num_attributes = src->zapt->attributes->num_attributes;
105 while (src->major < num_attributes)
107 Z_AttributeElement *element;
109 element = src->zapt->attributes->attributes[src->major];
110 if (src->type == *element->attributeType)
112 switch (element->which)
114 case Z_AttributeValue_numeric:
116 if (element->attributeSet && attributeSetP)
120 attrset = oid_getentbyoid(element->attributeSet);
121 *attributeSetP = attrset->value;
123 return *element->value.numeric;
125 case Z_AttributeValue_complex:
126 if (src->minor >= element->value.complex->num_list)
128 if (element->attributeSet && attributeSetP)
132 attrset = oid_getentbyoid(element->attributeSet);
133 *attributeSetP = attrset->value;
135 if (element->value.complex->list[src->minor]->which ==
136 Z_StringOrNumeric_numeric)
140 *element->value.complex->list[src->minor-1]->u.numeric;
142 else if (element->value.complex->list[src->minor]->which ==
143 Z_StringOrNumeric_string)
149 element->value.complex->list[src->minor-1]->u.string;
163 static int attr_find(AttrType *src, oid_value *attributeSetP)
165 return attr_find_ex(src, attributeSetP, 0);
168 static void attr_init(AttrType *src, Z_AttributesPlusTerm *zapt,
191 static void term_untrans(ZebraHandle zh, int reg_type,
192 char *dst, const char *src)
197 const char *cp = zebra_maps_output(zh->reg->zebra_maps,
199 if (!cp && len < IT_MAX_WORD-1)
202 while (*cp && len < IT_MAX_WORD-1)
208 static void add_isam_p(const char *name, const char *info,
213 log_level_rpn = yaz_log_module_level("rpn");
216 if (p->isam_p_indx == p->isam_p_size)
218 ISAMC_P *new_isam_p_buf;
222 p->isam_p_size = 2*p->isam_p_size + 100;
223 new_isam_p_buf = (ISAMC_P *) xmalloc(sizeof(*new_isam_p_buf) *
227 memcpy(new_isam_p_buf, p->isam_p_buf,
228 p->isam_p_indx * sizeof(*p->isam_p_buf));
229 xfree(p->isam_p_buf);
231 p->isam_p_buf = new_isam_p_buf;
234 new_term_no = (int *) xmalloc(sizeof(*new_term_no) * p->isam_p_size);
237 memcpy(new_term_no, p->isam_p_buf,
238 p->isam_p_indx * sizeof(*p->term_no));
241 p->term_no = new_term_no;
244 assert(*info == sizeof(*p->isam_p_buf));
245 memcpy(p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
252 char term_tmp[IT_MAX_WORD];
254 int len = key_SU_decode (&su_code, name);
256 term_untrans (p->zh, p->reg_type, term_tmp, name+len+1);
257 yaz_log(log_level_rpn, "grep: %d %c %s", su_code, name[len], term_tmp);
258 zebraExplain_lookup_ord (p->zh->reg->zei,
259 su_code, &db, &set, &use);
260 yaz_log(log_level_rpn, "grep: set=%d use=%d db=%s", set, use, db);
262 resultSetAddTerm(p->zh, p->termset, name[len], db,
269 static int grep_handle(char *name, const char *info, void *p)
271 add_isam_p(name, info, (struct grep_info *) p);
275 static int term_pre(ZebraMaps zebra_maps, int reg_type, const char **src,
276 const char *ct1, const char *ct2, int first)
278 const char *s1, *s0 = *src;
281 /* skip white space */
284 if (ct1 && strchr(ct1, *s0))
286 if (ct2 && strchr(ct2, *s0))
289 map = zebra_maps_input(zebra_maps, reg_type, &s1, strlen(s1), first);
290 if (**map != *CHR_SPACE)
298 #define REGEX_CHARS " []()|.*+?!"
300 /* term_100: handle term, where trunc = none(no operators at all) */
301 static int term_100(ZebraMaps zebra_maps, int reg_type,
302 const char **src, char *dst, int space_split,
310 const char *space_start = 0;
311 const char *space_end = 0;
313 if (!term_pre(zebra_maps, reg_type, src, NULL, NULL, !space_split))
319 map = zebra_maps_input(zebra_maps, reg_type, &s0, strlen(s0), 0);
322 if (**map == *CHR_SPACE)
325 else /* complete subfield only. */
327 if (**map == *CHR_SPACE)
328 { /* save space mapping for later .. */
333 else if (space_start)
334 { /* reload last space */
335 while (space_start < space_end)
337 if (strchr(REGEX_CHARS, *space_start))
339 dst_term[j++] = *space_start;
340 dst[i++] = *space_start++;
343 space_start = space_end = 0;
346 /* add non-space char */
349 if (strchr(REGEX_CHARS, *s1))
361 /* term_101: handle term, where trunc = Process # */
362 static int term_101(ZebraMaps zebra_maps, int reg_type,
363 const char **src, char *dst, int space_split,
371 if (!term_pre(zebra_maps, reg_type, src, "#", "#", !space_split))
380 dst_term[j++] = *s0++;
385 map = zebra_maps_input(zebra_maps, reg_type, &s0, strlen(s0), 0);
386 if (space_split && **map == *CHR_SPACE)
390 if (strchr(REGEX_CHARS, *s1))
398 dst_term[j++] = '\0';
403 /* term_103: handle term, where trunc = re-2 (regular expressions) */
404 static int term_103(ZebraMaps zebra_maps, int reg_type, const char **src,
405 char *dst, int *errors, int space_split,
413 if (!term_pre(zebra_maps, reg_type, src, "^\\()[].*+?|", "(", !space_split))
416 if (errors && *s0 == '+' && s0[1] && s0[2] == '+' && s0[3] &&
417 isdigit(((const unsigned char *)s0)[1]))
419 *errors = s0[1] - '0';
426 if (strchr("^\\()[].*+?|-", *s0))
434 map = zebra_maps_input(zebra_maps, reg_type, &s0, strlen(s0), 0);
435 if (**map == *CHR_SPACE)
439 if (strchr(REGEX_CHARS, *s1))
452 /* term_103: handle term, where trunc = re-1 (regular expressions) */
453 static int term_102 (ZebraMaps zebra_maps, int reg_type, const char **src,
454 char *dst, int space_split, char *dst_term)
456 return term_103(zebra_maps, reg_type, src, dst, NULL, space_split,
461 /* term_104: handle term, where trunc = Process # and ! */
462 static int term_104(ZebraMaps zebra_maps, int reg_type,
463 const char **src, char *dst, int space_split,
471 if (!term_pre(zebra_maps, reg_type, src, "?*#", "?*#", !space_split))
478 dst_term[j++] = *s0++;
479 if (*s0 >= '0' && *s0 <= '9')
482 while (*s0 >= '0' && *s0 <= '9')
484 limit = limit * 10 + (*s0 - '0');
485 dst_term[j++] = *s0++;
505 dst_term[j++] = *s0++;
510 dst_term[j++] = *s0++;
515 map = zebra_maps_input(zebra_maps, reg_type, &s0, strlen(s0), 0);
516 if (space_split && **map == *CHR_SPACE)
520 if (strchr(REGEX_CHARS, *s1))
528 dst_term[j++] = '\0';
533 /* term_105/106: handle term, where trunc = Process * and ! and right trunc */
534 static int term_105 (ZebraMaps zebra_maps, int reg_type,
535 const char **src, char *dst, int space_split,
536 char *dst_term, int right_truncate)
543 if (!term_pre(zebra_maps, reg_type, src, "*!", "*!", !space_split))
552 dst_term[j++] = *s0++;
557 dst_term[j++] = *s0++;
562 map = zebra_maps_input(zebra_maps, reg_type, &s0, strlen(s0), 0);
563 if (space_split && **map == *CHR_SPACE)
567 if (strchr(REGEX_CHARS, *s1))
581 dst_term[j++] = '\0';
587 /* gen_regular_rel - generate regular expression from relation
588 * val: border value (inclusive)
589 * islt: 1 if <=; 0 if >=.
591 static void gen_regular_rel(char *dst, int val, int islt)
598 yaz_log(YLOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
602 strcpy(dst, "(-[0-9]+|(");
610 strcpy(dst, "([0-9]+|-(");
622 sprintf(numstr, "%d", val);
623 for (w = strlen(numstr); --w >= 0; pos++)
642 strcpy(dst + dst_p, numstr);
643 dst_p = strlen(dst) - pos - 1;
671 for (i = 0; i<pos; i++)
684 /* match everything less than 10^(pos-1) */
686 for (i = 1; i<pos; i++)
687 strcat(dst, "[0-9]?");
691 /* match everything greater than 10^pos */
692 for (i = 0; i <= pos; i++)
693 strcat(dst, "[0-9]");
694 strcat(dst, "[0-9]*");
699 void string_rel_add_char(char **term_p, const char *src, int *indx)
701 if (src[*indx] == '\\')
702 *(*term_p)++ = src[(*indx)++];
703 *(*term_p)++ = src[(*indx)++];
707 * > abc ([b-].*|a[c-].*|ab[d-].*|abc.+)
708 * ([^-a].*|a[^-b].*ab[^-c].*|abc.+)
709 * >= abc ([b-].*|a[c-].*|ab[c-].*)
710 * ([^-a].*|a[^-b].*|ab[c-].*)
711 * < abc ([-0].*|a[-a].*|ab[-b].*)
712 * ([^a-].*|a[^b-].*|ab[^c-].*)
713 * <= abc ([-0].*|a[-a].*|ab[-b].*|abc)
714 * ([^a-].*|a[^b-].*|ab[^c-].*|abc)
716 static int string_relation(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
717 const char **term_sub, char *term_dict,
718 oid_value attributeSet,
719 int reg_type, int space_split, char *term_dst)
724 char *term_tmp = term_dict + strlen(term_dict);
725 char term_component[2*IT_MAX_WORD+20];
727 attr_init(&relation, zapt, 2);
728 relation_value = attr_find(&relation, NULL);
730 yaz_log(YLOG_DEBUG, "string relation value=%d", relation_value);
731 switch (relation_value)
734 if (!term_100 (zh->reg->zebra_maps, reg_type,
735 term_sub, term_component,
736 space_split, term_dst))
738 yaz_log(log_level_rpn, "Relation <");
741 for (i = 0; term_component[i]; )
748 string_rel_add_char (&term_tmp, term_component, &j);
753 string_rel_add_char (&term_tmp, term_component, &i);
760 if ((term_tmp - term_dict) > IT_MAX_WORD)
767 if (!term_100 (zh->reg->zebra_maps, reg_type,
768 term_sub, term_component,
769 space_split, term_dst))
771 yaz_log(log_level_rpn, "Relation <=");
774 for (i = 0; term_component[i]; )
779 string_rel_add_char (&term_tmp, term_component, &j);
783 string_rel_add_char (&term_tmp, term_component, &i);
792 if ((term_tmp - term_dict) > IT_MAX_WORD)
795 for (i = 0; term_component[i]; )
796 string_rel_add_char (&term_tmp, term_component, &i);
801 if (!term_100 (zh->reg->zebra_maps, reg_type,
802 term_sub, term_component, space_split, term_dst))
804 yaz_log(log_level_rpn, "Relation >");
807 for (i = 0; term_component[i];)
812 string_rel_add_char (&term_tmp, term_component, &j);
817 string_rel_add_char (&term_tmp, term_component, &i);
825 if ((term_tmp - term_dict) > IT_MAX_WORD)
828 for (i = 0; term_component[i];)
829 string_rel_add_char (&term_tmp, term_component, &i);
836 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub,
837 term_component, space_split, term_dst))
839 yaz_log(log_level_rpn, "Relation >=");
842 for (i = 0; term_component[i];)
849 string_rel_add_char (&term_tmp, term_component, &j);
852 if (term_component[i+1])
856 string_rel_add_char (&term_tmp, term_component, &i);
860 string_rel_add_char (&term_tmp, term_component, &i);
867 if ((term_tmp - term_dict) > IT_MAX_WORD)
875 yaz_log(log_level_rpn, "Relation =");
876 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub,
877 term_component, space_split, term_dst))
879 strcat(term_tmp, "(");
880 strcat(term_tmp, term_component);
881 strcat(term_tmp, ")");
886 static int string_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
887 const char **term_sub,
888 oid_value attributeSet, NMEM stream,
889 struct grep_info *grep_info,
890 int reg_type, int complete_flag,
891 int num_bases, char **basenames,
892 char *term_dst, int xpath_use);
894 static RSET term_trunc(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
895 const char **term_sub,
896 oid_value attributeSet, NMEM stream,
897 struct grep_info *grep_info,
898 int reg_type, int complete_flag,
899 int num_bases, char **basenames,
901 const char *rank_type, int xpath_use,
905 grep_info->isam_p_indx = 0;
906 r = string_term(zh, zapt, term_sub, attributeSet, stream, grep_info,
907 reg_type, complete_flag, num_bases, basenames,
908 term_dst, xpath_use);
911 yaz_log(log_level_rpn, "term: %s", term_dst);
912 return rset_trunc(zh, grep_info->isam_p_buf,
913 grep_info->isam_p_indx, term_dst,
914 strlen(term_dst), rank_type, 1 /* preserve pos */,
915 zapt->term->which, rset_nmem,
916 key_it_ctrl,key_it_ctrl->scope);
918 static char *nmem_strdup_i(NMEM nmem, int v)
921 sprintf (val_str, "%d", v);
922 return nmem_strdup(nmem, val_str);
925 static int string_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
926 const char **term_sub,
927 oid_value attributeSet, NMEM stream,
928 struct grep_info *grep_info,
929 int reg_type, int complete_flag,
930 int num_bases, char **basenames,
931 char *term_dst, int xpath_use)
933 char term_dict[2*IT_MAX_WORD+4000];
936 int truncation_value;
939 const char *use_string = 0;
940 oid_value curAttributeSet = attributeSet;
942 struct rpn_char_map_info rcmi;
943 int space_split = complete_flag ? 0 : 1;
945 int bases_ok = 0; /* no of databases with OK attribute */
946 int errCode = 0; /* err code (if any is not OK) */
947 char *errString = 0; /* addinfo */
949 rpn_char_map_prepare (zh->reg, reg_type, &rcmi);
950 attr_init (&use, zapt, 1);
951 use_value = attr_find_ex (&use, &curAttributeSet, &use_string);
952 yaz_log(log_level_rpn, "string_term, use value %d", use_value);
953 attr_init (&truncation, zapt, 5);
954 truncation_value = attr_find (&truncation, NULL);
955 yaz_log(log_level_rpn, "truncation value %d", truncation_value);
957 if (use_value == -1) /* no attribute - assumy "any" */
959 for (base_no = 0; base_no < num_bases; base_no++)
965 data1_local_attribute id_xpath_attr;
966 data1_local_attribute *local_attr;
967 int max_pos, prefix_len = 0;
971 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
973 zh->errCode = 109; /* Database unavailable */
974 zh->errString = basenames[base_no];
977 if (xpath_use > 0 && use_value == -2)
979 use_value = xpath_use;
980 attp.local_attributes = &id_xpath_attr;
981 attp.attset_ordinal = VAL_IDXPATH;
982 id_xpath_attr.next = 0;
983 id_xpath_attr.local = use_value;
985 else if (curAttributeSet == VAL_IDXPATH)
987 attp.local_attributes = &id_xpath_attr;
988 attp.attset_ordinal = VAL_IDXPATH;
989 id_xpath_attr.next = 0;
990 id_xpath_attr.local = use_value;
994 if ((r = att_getentbyatt (zh, &attp, curAttributeSet, use_value,
997 yaz_log(YLOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
998 curAttributeSet, use_value, r);
1001 /* set was found, but value wasn't defined */
1004 errString = nmem_strdup(stream, use_string);
1006 errString = nmem_strdup_i (stream, use_value);
1011 struct oident oident;
1013 oident.proto = PROTO_Z3950;
1014 oident.oclass = CLASS_ATTSET;
1015 oident.value = curAttributeSet;
1016 oid_ent_to_oid (&oident, oid);
1019 errString = nmem_strdup (stream, oident.desc);
1024 for (local_attr = attp.local_attributes; local_attr;
1025 local_attr = local_attr->next)
1031 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
1036 term_dict[prefix_len++] = '|';
1038 term_dict[prefix_len++] = '(';
1040 ord_len = key_SU_encode (ord, ord_buf);
1041 for (i = 0; i<ord_len; i++)
1043 term_dict[prefix_len++] = 1;
1044 term_dict[prefix_len++] = ord_buf[i];
1053 errString = nmem_strdup_i(stream, use_value);
1059 bases_ok++; /* this has OK attributes */
1063 term_dict[prefix_len++] = ')';
1064 term_dict[prefix_len++] = 1;
1065 term_dict[prefix_len++] = reg_type;
1066 yaz_log(log_level_rpn, "reg_type = %d", term_dict[prefix_len-1]);
1067 term_dict[prefix_len] = '\0';
1069 switch (truncation_value)
1071 case -1: /* not specified */
1072 case 100: /* do not truncate */
1073 if (!string_relation (zh, zapt, &termp, term_dict,
1075 reg_type, space_split, term_dst))
1078 case 1: /* right truncation */
1079 term_dict[j++] = '(';
1080 if (!term_100(zh->reg->zebra_maps, reg_type,
1081 &termp, term_dict + j, space_split, term_dst))
1083 strcat(term_dict, ".*)");
1085 case 2: /* keft truncation */
1086 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1087 if (!term_100(zh->reg->zebra_maps, reg_type,
1088 &termp, term_dict + j, space_split, term_dst))
1090 strcat(term_dict, ")");
1092 case 3: /* left&right truncation */
1093 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1094 if (!term_100(zh->reg->zebra_maps, reg_type,
1095 &termp, term_dict + j, space_split, term_dst))
1097 strcat(term_dict, ".*)");
1099 case 101: /* process # in term */
1100 term_dict[j++] = '(';
1101 if (!term_101(zh->reg->zebra_maps, reg_type,
1102 &termp, term_dict + j, space_split, term_dst))
1104 strcat(term_dict, ")");
1106 case 102: /* Regexp-1 */
1107 term_dict[j++] = '(';
1108 if (!term_102(zh->reg->zebra_maps, reg_type,
1109 &termp, term_dict + j, space_split, term_dst))
1111 strcat(term_dict, ")");
1113 case 103: /* Regexp-2 */
1115 term_dict[j++] = '(';
1117 if (!term_103 (zh->reg->zebra_maps, reg_type,
1118 &termp, term_dict + j, ®ex_range,
1119 space_split, term_dst))
1121 strcat(term_dict, ")");
1122 case 104: /* process # and ! in term */
1123 term_dict[j++] = '(';
1124 if (!term_104 (zh->reg->zebra_maps, reg_type,
1125 &termp, term_dict + j, space_split, term_dst))
1127 strcat(term_dict, ")");
1129 case 105: /* process * and ! in term */
1130 term_dict[j++] = '(';
1131 if (!term_105 (zh->reg->zebra_maps, reg_type,
1132 &termp, term_dict + j, space_split, term_dst, 1))
1134 strcat(term_dict, ")");
1136 case 106: /* process * and ! in term */
1137 term_dict[j++] = '(';
1138 if (!term_105 (zh->reg->zebra_maps, reg_type,
1139 &termp, term_dict + j, space_split, term_dst, 0))
1141 strcat(term_dict, ")");
1145 zh->errString = nmem_strdup_i(stream, truncation_value);
1150 yaz_log(log_level_rpn, "dict_lookup_grep: %s", term_dict+prefix_len);
1151 r = dict_lookup_grep(zh->reg->dict, term_dict, regex_range,
1152 grep_info, &max_pos, init_pos,
1155 yaz_log(YLOG_WARN, "dict_lookup_grep fail %d", r);
1160 zh->errCode = errCode;
1161 zh->errString = errString;
1165 yaz_log(YLOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1170 /* convert APT search term to UTF8 */
1171 static int zapt_term_to_utf8 (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1175 Z_Term *term = zapt->term;
1177 switch (term->which)
1179 case Z_Term_general:
1180 if (zh->iconv_to_utf8 != 0)
1182 char *inbuf = term->u.general->buf;
1183 size_t inleft = term->u.general->len;
1184 char *outbuf = termz;
1185 size_t outleft = IT_MAX_WORD-1;
1188 ret = yaz_iconv(zh->iconv_to_utf8, &inbuf, &inleft,
1190 if (ret == (size_t)(-1))
1192 ret = yaz_iconv(zh->iconv_to_utf8, 0, 0, 0, 0);
1200 sizez = term->u.general->len;
1201 if (sizez > IT_MAX_WORD-1)
1202 sizez = IT_MAX_WORD-1;
1203 memcpy (termz, term->u.general->buf, sizez);
1204 termz[sizez] = '\0';
1207 case Z_Term_characterString:
1208 sizez = strlen(term->u.characterString);
1209 if (sizez > IT_MAX_WORD-1)
1210 sizez = IT_MAX_WORD-1;
1211 memcpy (termz, term->u.characterString, sizez);
1212 termz[sizez] = '\0';
1221 /* convert APT SCAN term to internal cmap */
1222 static int trans_scan_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1223 char *termz, int reg_type)
1225 char termz0[IT_MAX_WORD];
1227 if (zapt_term_to_utf8(zh, zapt, termz0))
1228 return -1; /* error */
1232 const char *cp = (const char *) termz0;
1233 const char *cp_end = cp + strlen(cp);
1236 const char *space_map = NULL;
1239 while ((len = (cp_end - cp)) > 0)
1241 map = zebra_maps_input (zh->reg->zebra_maps, reg_type, &cp, len, 0);
1242 if (**map == *CHR_SPACE)
1247 for (src = space_map; *src; src++)
1250 for (src = *map; *src; src++)
1259 char *normalize_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1260 const char *termz, NMEM stream, unsigned reg_id)
1263 AttrType truncation;
1264 int truncation_value;
1267 attr_init (&truncation, zapt, 5);
1268 truncation_value = attr_find (&truncation, NULL);
1270 switch (truncation_value)
1290 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, ex_list,
1291 termz, strlen(termz));
1293 return nmem_strdup(stream, termz);
1296 char *buf = (char*) nmem_malloc(stream, wrbuf_len(wrbuf)+1);
1297 memcpy (buf, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1298 buf[wrbuf_len(wrbuf)] = '\0';
1303 static void grep_info_delete (struct grep_info *grep_info)
1306 xfree(grep_info->term_no);
1308 xfree (grep_info->isam_p_buf);
1311 static int grep_info_prepare (ZebraHandle zh,
1312 Z_AttributesPlusTerm *zapt,
1313 struct grep_info *grep_info,
1318 int termset_value_numeric;
1319 const char *termset_value_string;
1322 grep_info->term_no = 0;
1324 grep_info->isam_p_size = 0;
1325 grep_info->isam_p_buf = NULL;
1327 grep_info->reg_type = reg_type;
1328 grep_info->termset = 0;
1332 attr_init (&termset, zapt, 8);
1333 termset_value_numeric =
1334 attr_find_ex (&termset, NULL, &termset_value_string);
1335 if (termset_value_numeric != -1)
1338 const char *termset_name = 0;
1339 if (termset_value_numeric != -2)
1342 sprintf (resname, "%d", termset_value_numeric);
1343 termset_name = resname;
1346 termset_name = termset_value_string;
1347 yaz_log(log_level_rpn, "creating termset set %s", termset_name);
1348 grep_info->termset = resultSetAdd (zh, termset_name, 1);
1349 if (!grep_info->termset)
1352 zh->errString = nmem_strdup (stream, termset_name);
1360 static RSET rpn_search_APT_phrase (ZebraHandle zh,
1361 Z_AttributesPlusTerm *zapt,
1362 const char *termz_org,
1363 oid_value attributeSet,
1365 int reg_type, int complete_flag,
1366 const char *rank_type, int xpath_use,
1367 int num_bases, char **basenames,
1370 char term_dst[IT_MAX_WORD+1];
1371 RSET rset[TERM_LIST_LENGTH_MAX], result;
1373 struct grep_info grep_info;
1374 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1375 const char *termp = termz;
1378 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1380 for (; rset_no < sizeof(rset)/sizeof(*rset); rset_no++)
1382 yaz_log(log_level_rpn, "APT_phrase termp=%s", termp);
1383 rset[rset_no] = term_trunc(zh, zapt, &termp, attributeSet,
1385 reg_type, complete_flag,
1386 num_bases, basenames,
1387 term_dst, rank_type,
1388 xpath_use,rset_nmem);
1392 grep_info_delete (&grep_info);
1394 return rsnull_create (rset_nmem,key_it_ctrl);
1395 else if (rset_no == 1)
1398 result = rsprox_create( rset_nmem, key_it_ctrl, key_it_ctrl->scope,
1400 1 /* ordered */, 0 /* exclusion */,
1401 3 /* relation */, 1 /* distance */);
1405 static RSET rpn_search_APT_or_list (ZebraHandle zh,
1406 Z_AttributesPlusTerm *zapt,
1407 const char *termz_org,
1408 oid_value attributeSet,
1410 int reg_type, int complete_flag,
1411 const char *rank_type,
1413 int num_bases, char **basenames,
1416 char term_dst[IT_MAX_WORD+1];
1417 RSET rset[TERM_LIST_LENGTH_MAX];
1419 struct grep_info grep_info;
1420 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1421 const char *termp = termz;
1423 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1425 for (; rset_no < sizeof(rset)/sizeof(*rset); rset_no++)
1427 yaz_log(log_level_rpn, "APT_or_list termp=%s", termp);
1428 rset[rset_no] = term_trunc(zh, zapt, &termp, attributeSet,
1430 reg_type, complete_flag,
1431 num_bases, basenames,
1432 term_dst, rank_type,
1433 xpath_use,rset_nmem);
1437 grep_info_delete (&grep_info);
1439 return rsnull_create (rset_nmem,key_it_ctrl);
1440 return rsmulti_or_create(rset_nmem, key_it_ctrl,key_it_ctrl->scope,
1444 static RSET rpn_search_APT_and_list (ZebraHandle zh,
1445 Z_AttributesPlusTerm *zapt,
1446 const char *termz_org,
1447 oid_value attributeSet,
1449 int reg_type, int complete_flag,
1450 const char *rank_type,
1452 int num_bases, char **basenames,
1455 char term_dst[IT_MAX_WORD+1];
1456 RSET rset[TERM_LIST_LENGTH_MAX];
1458 struct grep_info grep_info;
1459 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1460 const char *termp = termz;
1462 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1464 for (; rset_no < sizeof(rset)/sizeof(*rset); rset_no++)
1466 yaz_log(log_level_rpn, "APT_and_list termp=%s", termp);
1467 rset[rset_no] = term_trunc(zh, zapt, &termp, attributeSet,
1469 reg_type, complete_flag,
1470 num_bases, basenames,
1471 term_dst, rank_type,
1472 xpath_use, rset_nmem);
1476 grep_info_delete (&grep_info);
1478 return rsnull_create(rset_nmem,key_it_ctrl);
1480 return rsmulti_and_create(rset_nmem, key_it_ctrl, key_it_ctrl->scope,
1484 static int numeric_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1485 const char **term_sub,
1487 oid_value attributeSet,
1488 struct grep_info *grep_info,
1497 char *term_tmp = term_dict + strlen(term_dict);
1499 attr_init (&relation, zapt, 2);
1500 relation_value = attr_find (&relation, NULL);
1502 yaz_log(log_level_rpn, "numeric relation value=%d", relation_value);
1504 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub, term_tmp, 1,
1507 term_value = atoi (term_tmp);
1508 switch (relation_value)
1511 yaz_log(log_level_rpn, "Relation <");
1512 gen_regular_rel (term_tmp, term_value-1, 1);
1515 yaz_log(log_level_rpn, "Relation <=");
1516 gen_regular_rel (term_tmp, term_value, 1);
1519 yaz_log(log_level_rpn, "Relation >=");
1520 gen_regular_rel (term_tmp, term_value, 0);
1523 yaz_log(log_level_rpn, "Relation >");
1524 gen_regular_rel (term_tmp, term_value+1, 0);
1528 yaz_log(log_level_rpn, "Relation =");
1529 sprintf (term_tmp, "(0*%d)", term_value);
1531 yaz_log(log_level_rpn, "dict_lookup_grep: %s", term_tmp);
1532 r = dict_lookup_grep(zh->reg->dict, term_dict, 0, grep_info, max_pos,
1535 yaz_log(YLOG_WARN, "dict_lookup_grep fail, rel = gt: %d", r);
1536 yaz_log(log_level_rpn, "%d positions", grep_info->isam_p_indx);
1540 static int numeric_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1541 const char **term_sub,
1542 oid_value attributeSet, struct grep_info *grep_info,
1543 int reg_type, int complete_flag,
1544 int num_bases, char **basenames,
1545 char *term_dst, int xpath_use, NMEM stream)
1547 char term_dict[2*IT_MAX_WORD+2];
1551 const char *use_string = 0;
1552 oid_value curAttributeSet = attributeSet;
1554 struct rpn_char_map_info rcmi;
1556 int bases_ok = 0; /* no of databases with OK attribute */
1557 int errCode = 0; /* err code (if any is not OK) */
1558 char *errString = 0; /* addinfo */
1560 rpn_char_map_prepare (zh->reg, reg_type, &rcmi);
1561 attr_init (&use, zapt, 1);
1562 use_value = attr_find_ex (&use, &curAttributeSet, &use_string);
1564 if (use_value == -1)
1567 for (base_no = 0; base_no < num_bases; base_no++)
1570 data1_local_attribute id_xpath_attr;
1571 data1_local_attribute *local_attr;
1572 int max_pos, prefix_len = 0;
1575 if (use_value == -2) /* string attribute (assume IDXPATH/any) */
1577 use_value = xpath_use;
1578 attp.local_attributes = &id_xpath_attr;
1579 attp.attset_ordinal = VAL_IDXPATH;
1580 id_xpath_attr.next = 0;
1581 id_xpath_attr.local = use_value;
1583 else if (curAttributeSet == VAL_IDXPATH)
1585 attp.local_attributes = &id_xpath_attr;
1586 attp.attset_ordinal = VAL_IDXPATH;
1587 id_xpath_attr.next = 0;
1588 id_xpath_attr.local = use_value;
1592 if ((r = att_getentbyatt (zh, &attp, curAttributeSet, use_value,
1595 yaz_log(YLOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1596 curAttributeSet, use_value, r);
1599 errString = nmem_strdup_i(stream, use_value);
1607 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
1609 zh->errCode = 109; /* Database unavailable */
1610 zh->errString = basenames[base_no];
1613 for (local_attr = attp.local_attributes; local_attr;
1614 local_attr = local_attr->next)
1620 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
1625 term_dict[prefix_len++] = '|';
1627 term_dict[prefix_len++] = '(';
1629 ord_len = key_SU_encode (ord, ord_buf);
1630 for (i = 0; i<ord_len; i++)
1632 term_dict[prefix_len++] = 1;
1633 term_dict[prefix_len++] = ord_buf[i];
1639 errString = nmem_strdup_i(stream, use_value);
1643 term_dict[prefix_len++] = ')';
1644 term_dict[prefix_len++] = 1;
1645 term_dict[prefix_len++] = reg_type;
1646 yaz_log(YLOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1647 term_dict[prefix_len] = '\0';
1648 if (!numeric_relation (zh, zapt, &termp, term_dict,
1649 attributeSet, grep_info, &max_pos, reg_type,
1655 zh->errCode = errCode;
1656 zh->errString = errString;
1660 yaz_log(YLOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1664 static RSET rpn_search_APT_numeric (ZebraHandle zh,
1665 Z_AttributesPlusTerm *zapt,
1667 oid_value attributeSet,
1669 int reg_type, int complete_flag,
1670 const char *rank_type, int xpath_use,
1671 int num_bases, char **basenames,
1674 char term_dst[IT_MAX_WORD+1];
1675 const char *termp = termz;
1676 RSET rset[TERM_LIST_LENGTH_MAX];
1679 struct grep_info grep_info;
1681 yaz_log(log_level_rpn, "APT_numeric t='%s'",termz);
1682 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1684 for (; rset_no < sizeof(rset)/sizeof(*rset); rset_no++)
1686 yaz_log(YLOG_DEBUG, "APT_numeric termp=%s", termp);
1687 grep_info.isam_p_indx = 0;
1688 r = numeric_term(zh, zapt, &termp, attributeSet, &grep_info,
1689 reg_type, complete_flag, num_bases, basenames,
1690 term_dst, xpath_use,
1694 yaz_log(YLOG_DEBUG, "term: %s", term_dst);
1695 rset[rset_no] = rset_trunc(zh, grep_info.isam_p_buf,
1696 grep_info.isam_p_indx, term_dst,
1697 strlen(term_dst), rank_type,
1698 0 /* preserve position */,
1699 zapt->term->which, rset_nmem,
1700 key_it_ctrl,key_it_ctrl->scope);
1704 grep_info_delete (&grep_info);
1706 return rsnull_create(rset_nmem,key_it_ctrl);
1709 return rsmulti_and_create(rset_nmem,key_it_ctrl,key_it_ctrl->scope,
1713 static RSET rpn_search_APT_local (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1715 oid_value attributeSet,
1717 const char *rank_type, NMEM rset_nmem)
1723 result = rstemp_create( rset_nmem,key_it_ctrl,key_it_ctrl->scope,
1724 res_get (zh->res, "setTmpDir"),0 );
1725 rsfd = rset_open (result, RSETF_WRITE);
1733 rset_write (rsfd, &key);
1738 static RSET rpn_sort_spec (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1739 oid_value attributeSet, NMEM stream,
1740 Z_SortKeySpecList *sort_sequence,
1741 const char *rank_type)
1744 int sort_relation_value;
1745 AttrType sort_relation_type;
1750 Z_AttributeElement *ae;
1755 attr_init (&sort_relation_type, zapt, 7);
1756 sort_relation_value = attr_find (&sort_relation_type, &attributeSet);
1758 attr_init (&use_type, zapt, 1);
1759 use_value = attr_find (&use_type, &attributeSet);
1761 if (!sort_sequence->specs)
1763 sort_sequence->num_specs = 10;
1764 sort_sequence->specs = (Z_SortKeySpec **)
1765 nmem_malloc(stream, sort_sequence->num_specs *
1766 sizeof(*sort_sequence->specs));
1767 for (i = 0; i<sort_sequence->num_specs; i++)
1768 sort_sequence->specs[i] = 0;
1770 if (zapt->term->which != Z_Term_general)
1773 i = atoi_n ((char *) zapt->term->u.general->buf,
1774 zapt->term->u.general->len);
1775 if (i >= sort_sequence->num_specs)
1777 sprintf (termz, "%d", i);
1779 oe.proto = PROTO_Z3950;
1780 oe.oclass = CLASS_ATTSET;
1781 oe.value = attributeSet;
1782 if (!oid_ent_to_oid (&oe, oid))
1785 sks = (Z_SortKeySpec *) nmem_malloc(stream, sizeof(*sks));
1786 sks->sortElement = (Z_SortElement *)
1787 nmem_malloc(stream, sizeof(*sks->sortElement));
1788 sks->sortElement->which = Z_SortElement_generic;
1789 sk = sks->sortElement->u.generic = (Z_SortKey *)
1790 nmem_malloc(stream, sizeof(*sk));
1791 sk->which = Z_SortKey_sortAttributes;
1792 sk->u.sortAttributes = (Z_SortAttributes *)
1793 nmem_malloc(stream, sizeof(*sk->u.sortAttributes));
1795 sk->u.sortAttributes->id = oid;
1796 sk->u.sortAttributes->list = (Z_AttributeList *)
1797 nmem_malloc(stream, sizeof(*sk->u.sortAttributes->list));
1798 sk->u.sortAttributes->list->num_attributes = 1;
1799 sk->u.sortAttributes->list->attributes = (Z_AttributeElement **)
1800 nmem_malloc(stream, sizeof(*sk->u.sortAttributes->list->attributes));
1801 ae = *sk->u.sortAttributes->list->attributes = (Z_AttributeElement *)
1802 nmem_malloc(stream, sizeof(**sk->u.sortAttributes->list->attributes));
1803 ae->attributeSet = 0;
1804 ae->attributeType = (int *)
1805 nmem_malloc(stream, sizeof(*ae->attributeType));
1806 *ae->attributeType = 1;
1807 ae->which = Z_AttributeValue_numeric;
1808 ae->value.numeric = (int *)
1809 nmem_malloc(stream, sizeof(*ae->value.numeric));
1810 *ae->value.numeric = use_value;
1812 sks->sortRelation = (int *)
1813 nmem_malloc(stream, sizeof(*sks->sortRelation));
1814 if (sort_relation_value == 1)
1815 *sks->sortRelation = Z_SortKeySpec_ascending;
1816 else if (sort_relation_value == 2)
1817 *sks->sortRelation = Z_SortKeySpec_descending;
1819 *sks->sortRelation = Z_SortKeySpec_ascending;
1821 sks->caseSensitivity = (int *)
1822 nmem_malloc(stream, sizeof(*sks->caseSensitivity));
1823 *sks->caseSensitivity = 0;
1825 sks->which = Z_SortKeySpec_null;
1826 sks->u.null = odr_nullval ();
1827 sort_sequence->specs[i] = sks;
1828 return rsnull_create (NULL,key_it_ctrl);
1829 /* FIXME - nmem?? */
1833 static int parse_xpath(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1834 oid_value attributeSet,
1835 struct xpath_location_step *xpath, int max, NMEM mem)
1837 oid_value curAttributeSet = attributeSet;
1839 const char *use_string = 0;
1841 attr_init (&use, zapt, 1);
1842 attr_find_ex (&use, &curAttributeSet, &use_string);
1844 if (!use_string || *use_string != '/')
1847 return zebra_parse_xpath_str(use_string, xpath, max, mem);
1852 static RSET xpath_trunc(ZebraHandle zh, NMEM stream,
1853 int reg_type, const char *term, int use,
1854 oid_value curAttributeSet, NMEM rset_nmem)
1857 struct grep_info grep_info;
1858 char term_dict[2048];
1861 int ord = zebraExplain_lookupSU (zh->reg->zei, curAttributeSet, use);
1862 int ord_len, i, r, max_pos;
1863 int term_type = Z_Term_characterString;
1864 const char *flags = "void";
1866 if (grep_info_prepare (zh, 0 /* zapt */, &grep_info, '0', stream))
1867 return rsnull_create (rset_nmem,key_it_ctrl);
1870 return rsnull_create (rset_nmem,key_it_ctrl);
1872 term_dict[prefix_len++] = '|';
1874 term_dict[prefix_len++] = '(';
1876 ord_len = key_SU_encode (ord, ord_buf);
1877 for (i = 0; i<ord_len; i++)
1879 term_dict[prefix_len++] = 1;
1880 term_dict[prefix_len++] = ord_buf[i];
1882 term_dict[prefix_len++] = ')';
1883 term_dict[prefix_len++] = 1;
1884 term_dict[prefix_len++] = reg_type;
1886 strcpy(term_dict+prefix_len, term);
1888 grep_info.isam_p_indx = 0;
1889 r = dict_lookup_grep(zh->reg->dict, term_dict, 0,
1890 &grep_info, &max_pos, 0, grep_handle);
1891 yaz_log (YLOG_LOG, "%s %d positions", term,
1892 grep_info.isam_p_indx);
1893 rset = rset_trunc(zh, grep_info.isam_p_buf,
1894 grep_info.isam_p_indx, term, strlen(term),
1895 flags, 1, term_type,rset_nmem,
1896 key_it_ctrl, key_it_ctrl->scope);
1897 grep_info_delete (&grep_info);
1901 static RSET rpn_search_xpath (ZebraHandle zh,
1902 oid_value attributeSet,
1903 int num_bases, char **basenames,
1904 NMEM stream, const char *rank_type, RSET rset,
1905 int xpath_len, struct xpath_location_step *xpath,
1908 oid_value curAttributeSet = attributeSet;
1915 yaz_log (YLOG_DEBUG, "xpath len=%d", xpath_len);
1916 for (i = 0; i<xpath_len; i++)
1918 yaz_log (log_level_rpn, "XPATH %d %s", i, xpath[i].part);
1922 curAttributeSet = VAL_IDXPATH;
1932 a[@attr = value]/b[@other = othervalue]
1934 /e/@a val range(e/,range(@a,freetext(w,1015,val),@a),e/)
1935 /a/b val range(b/a/,freetext(w,1016,val),b/a/)
1936 /a/b/@c val range(b/a/,range(@c,freetext(w,1016,val),@c),b/a/)
1937 /a/b[@c = y] val range(b/a/,freetext(w,1016,val),b/a/,@c = y)
1938 /a[@c = y]/b val range(a/,range(b/a/,freetext(w,1016,val),b/a/),a/,@c = y)
1939 /a[@c = x]/b[@c = y] range(a/,range(b/a/,freetext(w,1016,val),b/a/,@c = y),a/,@c = x)
1943 dict_grep_cmap (zh->reg->dict, 0, 0);
1945 for (base_no = 0; base_no < num_bases; base_no++)
1947 int level = xpath_len;
1950 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
1952 zh->errCode = 109; /* Database unavailable */
1953 zh->errString = basenames[base_no];
1956 while (--level >= 0)
1958 char xpath_rev[128];
1960 RSET rset_start_tag = 0, rset_end_tag = 0, rset_attr = 0;
1964 for (i = level; i >= 1; --i)
1966 const char *cp = xpath[i].part;
1972 memcpy (xpath_rev + len, "[^/]*", 5);
1975 else if (*cp == ' ')
1978 xpath_rev[len++] = 1;
1979 xpath_rev[len++] = ' ';
1983 xpath_rev[len++] = *cp;
1984 xpath_rev[len++] = '/';
1986 else if (i == 1) /* // case */
1988 xpath_rev[len++] = '.';
1989 xpath_rev[len++] = '*';
1994 if (xpath[level].predicate &&
1995 xpath[level].predicate->which == XPATH_PREDICATE_RELATION &&
1996 xpath[level].predicate->u.relation.name[0])
1998 WRBUF wbuf = wrbuf_alloc();
1999 wrbuf_puts(wbuf, xpath[level].predicate->u.relation.name+1);
2000 if (xpath[level].predicate->u.relation.value)
2002 const char *cp = xpath[level].predicate->u.relation.value;
2003 wrbuf_putc(wbuf, '=');
2007 if (strchr(REGEX_CHARS, *cp))
2008 wrbuf_putc(wbuf, '\\');
2009 wrbuf_putc(wbuf, *cp);
2013 wrbuf_puts(wbuf, "");
2014 rset_attr = xpath_trunc(
2015 zh, stream, '0', wrbuf_buf(wbuf), 3,
2016 curAttributeSet,rset_nmem);
2017 wrbuf_free(wbuf, 1);
2024 yaz_log (log_level_rpn, "xpath_rev (%d) = %s", level, xpath_rev);
2025 if (strlen(xpath_rev))
2027 rset_start_tag = xpath_trunc(zh, stream, '0',
2028 xpath_rev, 1, curAttributeSet, rset_nmem);
2030 rset_end_tag = xpath_trunc(zh, stream, '0',
2031 xpath_rev, 2, curAttributeSet, rset_nmem);
2033 rset = rsbetween_create(rset_nmem, key_it_ctrl,
2035 rset_start_tag, rset,
2036 rset_end_tag, rset_attr);
2047 static RSET rpn_search_APT (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2048 oid_value attributeSet, NMEM stream,
2049 Z_SortKeySpecList *sort_sequence,
2050 int num_bases, char **basenames,
2054 char *search_type = NULL;
2055 char rank_type[128];
2058 char termz[IT_MAX_WORD+1];
2062 struct xpath_location_step xpath[10];
2066 log_level_rpn = yaz_log_module_level("rpn");
2069 zebra_maps_attr (zh->reg->zebra_maps, zapt, ®_id, &search_type,
2070 rank_type, &complete_flag, &sort_flag);
2072 yaz_log(YLOG_DEBUG, "reg_id=%c", reg_id);
2073 yaz_log(YLOG_DEBUG, "complete_flag=%d", complete_flag);
2074 yaz_log(YLOG_DEBUG, "search_type=%s", search_type);
2075 yaz_log(YLOG_DEBUG, "rank_type=%s", rank_type);
2077 if (zapt_term_to_utf8(zh, zapt, termz))
2081 return rpn_sort_spec (zh, zapt, attributeSet, stream, sort_sequence,
2083 xpath_len = parse_xpath(zh, zapt, attributeSet, xpath, 10, stream);
2087 if (xpath[xpath_len-1].part[0] == '@')
2091 if (!strcmp (search_type, "phrase"))
2093 rset = rpn_search_APT_phrase (zh, zapt, termz, attributeSet, stream,
2094 reg_id, complete_flag, rank_type,
2096 num_bases, basenames, rset_nmem);
2098 else if (!strcmp (search_type, "and-list"))
2100 rset = rpn_search_APT_and_list (zh, zapt, termz, attributeSet, stream,
2101 reg_id, complete_flag, rank_type,
2103 num_bases, basenames, rset_nmem);
2105 else if (!strcmp (search_type, "or-list"))
2107 rset = rpn_search_APT_or_list (zh, zapt, termz, attributeSet, stream,
2108 reg_id, complete_flag, rank_type,
2110 num_bases, basenames, rset_nmem);
2112 else if (!strcmp (search_type, "local"))
2114 rset = rpn_search_APT_local (zh, zapt, termz, attributeSet, stream,
2115 rank_type, rset_nmem);
2117 else if (!strcmp (search_type, "numeric"))
2119 rset = rpn_search_APT_numeric (zh, zapt, termz, attributeSet, stream,
2120 reg_id, complete_flag, rank_type,
2122 num_bases, basenames, rset_nmem);
2124 else if (!strcmp (search_type, "always"))
2130 return rpn_search_xpath (zh, attributeSet, num_bases, basenames,
2131 stream, rank_type, rset,
2132 xpath_len, xpath, rset_nmem);
2135 static RSET rpn_search_structure (ZebraHandle zh, Z_RPNStructure *zs,
2136 oid_value attributeSet,
2137 NMEM stream, NMEM rset_nmem,
2138 Z_SortKeySpecList *sort_sequence,
2139 int num_bases, char **basenames)
2142 if (zs->which == Z_RPNStructure_complex)
2144 Z_Operator *zop = zs->u.complex->roperator;
2145 RSET rsets[2]; /* l and r argument */
2147 rsets[0]=rpn_search_structure (zh, zs->u.complex->s1,
2148 attributeSet, stream, rset_nmem,
2150 num_bases, basenames);
2151 if (rsets[0] == NULL)
2153 rsets[1]=rpn_search_structure (zh, zs->u.complex->s2,
2154 attributeSet, stream, rset_nmem,
2156 num_bases, basenames);
2157 if (rsets[1] == NULL)
2159 rset_delete (rsets[0]);
2165 case Z_Operator_and:
2166 r = rsmulti_and_create(rset_nmem, key_it_ctrl, key_it_ctrl->scope,
2170 r = rsmulti_or_create(rset_nmem, key_it_ctrl, key_it_ctrl->scope,
2173 case Z_Operator_and_not:
2174 r = rsbool_create_not(rset_nmem,key_it_ctrl, key_it_ctrl->scope,
2177 case Z_Operator_prox:
2178 if (zop->u.prox->which != Z_ProximityOperator_known)
2183 if (*zop->u.prox->u.known != Z_ProxUnit_word)
2185 char *val = (char *) nmem_malloc(stream, 16);
2187 zh->errString = val;
2188 sprintf (val, "%d", *zop->u.prox->u.known);
2193 /* new / old prox */
2194 r = rsprox_create(rset_nmem,key_it_ctrl,key_it_ctrl->scope,
2196 *zop->u.prox->ordered,
2197 (!zop->u.prox->exclusion ?
2198 0 : *zop->u.prox->exclusion),
2199 *zop->u.prox->relationType,
2200 *zop->u.prox->distance );
2208 else if (zs->which == Z_RPNStructure_simple)
2210 if (zs->u.simple->which == Z_Operand_APT)
2212 yaz_log(YLOG_DEBUG, "rpn_search_APT");
2213 r = rpn_search_APT (zh, zs->u.simple->u.attributesPlusTerm,
2214 attributeSet, stream, sort_sequence,
2215 num_bases, basenames,rset_nmem);
2217 else if (zs->u.simple->which == Z_Operand_resultSetId)
2219 yaz_log(YLOG_DEBUG, "rpn_search_ref");
2220 r = resultSetRef (zh, zs->u.simple->u.resultSetId);
2223 r = rsnull_create (rset_nmem,key_it_ctrl);
2226 nmem_strdup (stream, zs->u.simple->u.resultSetId);
2247 RSET rpn_search(ZebraHandle zh, NMEM nmem, NMEM rset_nmem,
2248 Z_RPNQuery *rpn, int num_bases, char **basenames,
2249 const char *setname,
2254 oid_value attributeSet;
2255 Z_SortKeySpecList *sort_sequence;
2259 zh->errString = NULL;
2262 sort_sequence = (Z_SortKeySpecList *)
2263 nmem_malloc(nmem, sizeof(*sort_sequence));
2264 sort_sequence->num_specs = 10; /* FIXME - Hard-coded number */
2265 sort_sequence->specs = (Z_SortKeySpec **)
2266 nmem_malloc(nmem, sort_sequence->num_specs *
2267 sizeof(*sort_sequence->specs));
2268 for (i = 0; i<sort_sequence->num_specs; i++)
2269 sort_sequence->specs[i] = 0;
2271 attrset = oid_getentbyoid (rpn->attributeSetId);
2272 attributeSet = attrset->value;
2273 rset = rpn_search_structure (zh, rpn->RPNStructure, attributeSet,
2275 sort_sequence, num_bases, basenames);
2280 yaz_log(YLOG_DEBUG, "search error: %d", zh->errCode);
2282 for (i = 0; sort_sequence->specs[i]; i++)
2284 sort_sequence->num_specs = i;
2286 resultSetRank (zh, sset, rset, rset_nmem);
2289 yaz_log(YLOG_DEBUG, "resultSetSortSingle in rpn_search");
2290 resultSetSortSingle (zh, nmem, sset, rset,
2291 sort_sequence, &sort_status);
2294 yaz_log(YLOG_DEBUG, "resultSetSortSingle status = %d", zh->errCode);
2300 struct scan_info_entry {
2306 struct scan_info_entry *list;
2312 static int scan_handle (char *name, const char *info, int pos, void *client)
2314 int len_prefix, idx;
2315 struct scan_info *scan_info = (struct scan_info *) client;
2317 len_prefix = strlen(scan_info->prefix);
2318 if (memcmp (name, scan_info->prefix, len_prefix))
2320 if (pos > 0) idx = scan_info->after - pos + scan_info->before;
2323 scan_info->list[idx].term = (char *)
2324 odr_malloc(scan_info->odr, strlen(name + len_prefix)+1);
2325 strcpy(scan_info->list[idx].term, name + len_prefix);
2326 assert (*info == sizeof(ISAMC_P));
2327 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAMC_P));
2331 static void scan_term_untrans (ZebraHandle zh, NMEM stream, int reg_type,
2332 char **dst, const char *src)
2334 char term_src[IT_MAX_WORD];
2335 char term_dst[IT_MAX_WORD];
2337 term_untrans (zh, reg_type, term_src, src);
2339 if (zh->iconv_from_utf8 != 0)
2342 char *inbuf = term_src;
2343 size_t inleft = strlen(term_src);
2344 char *outbuf = term_dst;
2345 size_t outleft = sizeof(term_dst)-1;
2348 ret = yaz_iconv (zh->iconv_from_utf8, &inbuf, &inleft,
2350 if (ret == (size_t)(-1))
2353 len = outbuf - term_dst;
2354 *dst = nmem_malloc(stream, len + 1);
2356 memcpy (*dst, term_dst, len);
2360 *dst = nmem_strdup(stream, term_src);
2363 static void count_set (RSET r, int *count)
2370 yaz_log(YLOG_DEBUG, "count_set");
2373 rfd = rset_open (r, RSETF_READ);
2374 while (rset_read (rfd, &key,0 /* never mind terms */))
2376 if (key.mem[0] != psysno)
2378 psysno = key.mem[0];
2384 yaz_log(YLOG_DEBUG, "%d keys, %d records", kno, *count);
2387 void rpn_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
2388 oid_value attributeset,
2389 int num_bases, char **basenames,
2390 int *position, int *num_entries, ZebraScanEntry **list,
2391 int *is_partial, RSET limit_set, int return_zero)
2394 int pos = *position;
2395 int num = *num_entries;
2399 char termz[IT_MAX_WORD+20];
2402 const char *use_string = 0;
2403 struct scan_info *scan_info_array;
2404 ZebraScanEntry *glist;
2405 int ords[32], ord_no = 0;
2408 int bases_ok = 0; /* no of databases with OK attribute */
2409 int errCode = 0; /* err code (if any is not OK) */
2410 char *errString = 0; /* addinfo */
2413 char *search_type = NULL;
2414 char rank_type[128];
2417 NMEM rset_nmem = NULL;
2421 if (attributeset == VAL_NONE)
2422 attributeset = VAL_BIB1;
2427 int termset_value_numeric;
2428 const char *termset_value_string;
2429 attr_init (&termset, zapt, 8);
2430 termset_value_numeric =
2431 attr_find_ex (&termset, NULL, &termset_value_string);
2432 if (termset_value_numeric != -1)
2435 const char *termset_name = 0;
2437 if (termset_value_numeric != -2)
2440 sprintf (resname, "%d", termset_value_numeric);
2441 termset_name = resname;
2444 termset_name = termset_value_string;
2446 limit_set = resultSetRef (zh, termset_name);
2450 yaz_log (YLOG_DEBUG, "position = %d, num = %d set=%d",
2451 pos, num, attributeset);
2453 attr_init (&use, zapt, 1);
2454 use_value = attr_find_ex (&use, &attributeset, &use_string);
2456 if (zebra_maps_attr (zh->reg->zebra_maps, zapt, ®_id, &search_type,
2457 rank_type, &complete_flag, &sort_flag))
2463 yaz_log (YLOG_DEBUG, "use_value = %d", use_value);
2465 if (use_value == -1)
2467 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
2471 data1_local_attribute *local_attr;
2473 if ((r = att_getentbyatt (zh, &attp, attributeset, use_value,
2476 yaz_log(YLOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
2477 attributeset, use_value);
2481 sprintf (val_str, "%d", use_value);
2483 errString = odr_strdup (stream, val_str);
2489 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
2491 zh->errString = basenames[base_no];
2492 zh->errCode = 109; /* Database unavailable */
2497 for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
2498 local_attr = local_attr->next)
2502 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
2505 ords[ord_no++] = ord;
2508 if (!bases_ok && errCode)
2510 zh->errCode = errCode;
2511 zh->errString = errString;
2519 /* prepare dictionary scanning */
2522 scan_info_array = (struct scan_info *)
2523 odr_malloc(stream, ord_no * sizeof(*scan_info_array));
2524 for (i = 0; i < ord_no; i++)
2526 int j, prefix_len = 0;
2527 int before_tmp = before, after_tmp = after;
2528 struct scan_info *scan_info = scan_info_array + i;
2529 struct rpn_char_map_info rcmi;
2531 rpn_char_map_prepare (zh->reg, reg_id, &rcmi);
2533 scan_info->before = before;
2534 scan_info->after = after;
2535 scan_info->odr = stream;
2537 scan_info->list = (struct scan_info_entry *)
2538 odr_malloc(stream, (before+after) * sizeof(*scan_info->list));
2539 for (j = 0; j<before+after; j++)
2540 scan_info->list[j].term = NULL;
2542 prefix_len += key_SU_encode (ords[i], termz + prefix_len);
2543 termz[prefix_len++] = reg_id;
2544 termz[prefix_len] = 0;
2545 strcpy(scan_info->prefix, termz);
2547 if (trans_scan_term(zh, zapt, termz+prefix_len, reg_id))
2550 dict_scan(zh->reg->dict, termz, &before_tmp, &after_tmp,
2551 scan_info, scan_handle);
2553 glist = (ZebraScanEntry *)
2554 odr_malloc(stream, (before+after)*sizeof(*glist));
2556 rset_nmem = nmem_create();
2558 /* consider terms after main term */
2559 for (i = 0; i < ord_no; i++)
2563 for (i = 0; i<after; i++)
2566 const char *mterm = NULL;
2570 for (j = 0; j < ord_no; j++)
2572 if (ptr[j] < before+after &&
2573 (tst = scan_info_array[j].list[ptr[j]].term) &&
2574 (!mterm || strcmp (tst, mterm) < 0))
2582 scan_term_untrans (zh, stream->mem, reg_id,
2583 &glist[i+before].term, mterm);
2584 rset = rset_trunc(zh, &scan_info_array[j0].list[ptr[j0]].isam_p, 1,
2585 glist[i+before].term, strlen(glist[i+before].term),
2586 NULL, 0, zapt->term->which, rset_nmem,
2587 key_it_ctrl,key_it_ctrl->scope);
2589 for (j = j0+1; j<ord_no; j++)
2591 if (ptr[j] < before+after &&
2592 (tst = scan_info_array[j].list[ptr[j]].term) &&
2593 !strcmp (tst, mterm))
2599 rset_trunc(zh, &scan_info_array[j].list[ptr[j]].isam_p, 1,
2600 glist[i+before].term,
2601 strlen(glist[i+before].term), NULL, 0,
2602 zapt->term->which,rset_nmem,
2603 key_it_ctrl, key_it_ctrl->scope);
2604 rset = rsmulti_or_create(rset_nmem, key_it_ctrl,
2605 2, key_it_ctrl->scope, rsets);
2613 rsets[1] = rset_dup(limit_set);
2615 rset = rsmulti_and_create(rset_nmem, key_it_ctrl,
2616 key_it_ctrl->scope, 2, rsets);
2618 count_set(rset, &glist[i+before].occurrences);
2623 *num_entries -= (after-i);
2627 /* consider terms before main term */
2628 for (i = 0; i<ord_no; i++)
2631 for (i = 0; i<before; i++)
2634 const char *mterm = NULL;
2638 for (j = 0; j <ord_no; j++)
2640 if (ptr[j] < before &&
2641 (tst = scan_info_array[j].list[before-1-ptr[j]].term) &&
2642 (!mterm || strcmp (tst, mterm) > 0))
2651 scan_term_untrans (zh, stream->mem, reg_id,
2652 &glist[before-1-i].term, mterm);
2655 (zh, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1,
2656 glist[before-1-i].term, strlen(glist[before-1-i].term),
2657 NULL, 0, zapt->term->which,rset_nmem,
2658 key_it_ctrl,key_it_ctrl->scope);
2662 for (j = j0+1; j<ord_no; j++)
2664 if (ptr[j] < before &&
2665 (tst = scan_info_array[j].list[before-1-ptr[j]].term) &&
2666 !strcmp (tst, mterm))
2671 rsets[1] = rset_trunc(
2673 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1,
2674 glist[before-1-i].term,
2675 strlen(glist[before-1-i].term), NULL, 0,
2676 zapt->term->which, rset_nmem,
2677 key_it_ctrl, key_it_ctrl->scope);
2678 rset = rsmulti_or_create(rset_nmem, key_it_ctrl,
2679 2, key_it_ctrl->scope, rsets);
2688 rsets[1] = rset_dup(limit_set);
2690 rset = rsmulti_and_create(rset_nmem, key_it_ctrl,
2691 key_it_ctrl->scope, 2, rsets);
2693 count_set (rset, &glist[before-1-i].occurrences);
2704 nmem_destroy(rset_nmem);
2705 *list = glist + i; /* list is set to first 'real' entry */
2707 yaz_log(YLOG_DEBUG, "position = %d, num_entries = %d",
2708 *position, *num_entries);
2710 yaz_log(YLOG_DEBUG, "scan error: %d", zh->errCode);