1 /* $Id: zrpn.c,v 1.167 2005-01-15 19:38:29 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) *
238 memcpy(new_term_no, p->isam_p_buf,
239 p->isam_p_indx * sizeof(*p->term_no));
242 p->term_no = new_term_no;
245 assert(*info == sizeof(*p->isam_p_buf));
246 memcpy(p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
253 char term_tmp[IT_MAX_WORD];
255 int len = key_SU_decode (&su_code, name);
257 term_untrans (p->zh, p->reg_type, term_tmp, name+len+1);
258 yaz_log(log_level_rpn, "grep: %d %c %s", su_code, name[len], term_tmp);
259 zebraExplain_lookup_ord (p->zh->reg->zei,
260 su_code, &db, &set, &use);
261 yaz_log(log_level_rpn, "grep: set=%d use=%d db=%s", set, use, db);
263 resultSetAddTerm(p->zh, p->termset, name[len], db,
270 static int grep_handle(char *name, const char *info, void *p)
272 add_isam_p(name, info, (struct grep_info *) p);
276 static int term_pre(ZebraMaps zebra_maps, int reg_type, const char **src,
277 const char *ct1, const char *ct2, int first)
279 const char *s1, *s0 = *src;
282 /* skip white space */
285 if (ct1 && strchr(ct1, *s0))
287 if (ct2 && strchr(ct2, *s0))
290 map = zebra_maps_input(zebra_maps, reg_type, &s1, strlen(s1), first);
291 if (**map != *CHR_SPACE)
299 #define REGEX_CHARS " []()|.*+?!"
301 /* term_100: handle term, where trunc=none(no operators at all) */
302 static int term_100(ZebraMaps zebra_maps, int reg_type,
303 const char **src, char *dst, int space_split,
311 const char *space_start = 0;
312 const char *space_end = 0;
314 if (!term_pre(zebra_maps, reg_type, src, NULL, NULL, !space_split))
320 map = zebra_maps_input(zebra_maps, reg_type, &s0, strlen(s0), 0);
323 if (**map == *CHR_SPACE)
326 else /* complete subfield only. */
328 if (**map == *CHR_SPACE)
329 { /* save space mapping for later .. */
334 else if (space_start)
335 { /* reload last space */
336 while (space_start < space_end)
338 if (strchr(REGEX_CHARS, *space_start))
340 dst_term[j++] = *space_start;
341 dst[i++] = *space_start++;
344 space_start = space_end = 0;
347 /* add non-space char */
350 if (strchr(REGEX_CHARS, *s1))
362 /* term_101: handle term, where trunc=Process # */
363 static int term_101(ZebraMaps zebra_maps, int reg_type,
364 const char **src, char *dst, int space_split,
372 if (!term_pre(zebra_maps, reg_type, src, "#", "#", !space_split))
381 dst_term[j++] = *s0++;
386 map = zebra_maps_input(zebra_maps, reg_type, &s0, strlen(s0), 0);
387 if (space_split && **map == *CHR_SPACE)
391 if (strchr(REGEX_CHARS, *s1))
399 dst_term[j++] = '\0';
404 /* term_103: handle term, where trunc=re-2 (regular expressions) */
405 static int term_103(ZebraMaps zebra_maps, int reg_type, const char **src,
406 char *dst, int *errors, int space_split,
414 if (!term_pre(zebra_maps, reg_type, src, "^\\()[].*+?|", "(", !space_split))
417 if (errors && *s0 == '+' && s0[1] && s0[2] == '+' && s0[3] &&
418 isdigit(((const unsigned char *)s0)[1]))
420 *errors = s0[1] - '0';
427 if (strchr("^\\()[].*+?|-", *s0))
435 map = zebra_maps_input(zebra_maps, reg_type, &s0, strlen(s0), 0);
436 if (**map == *CHR_SPACE)
440 if (strchr(REGEX_CHARS, *s1))
453 /* term_103: handle term, where trunc=re-1 (regular expressions) */
454 static int term_102 (ZebraMaps zebra_maps, int reg_type, const char **src,
455 char *dst, int space_split, char *dst_term)
457 return term_103(zebra_maps, reg_type, src, dst, NULL, space_split,
462 /* term_104: handle term, where trunc=Process # and ! */
463 static int term_104(ZebraMaps zebra_maps, int reg_type,
464 const char **src, char *dst, int space_split,
472 if (!term_pre(zebra_maps, reg_type, src, "?*#", "?*#", !space_split))
479 dst_term[j++] = *s0++;
480 if (*s0 >= '0' && *s0 <= '9')
483 while (*s0 >= '0' && *s0 <= '9')
485 limit = limit * 10 + (*s0 - '0');
486 dst_term[j++] = *s0++;
506 dst_term[j++] = *s0++;
511 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++;
561 map = zebra_maps_input(zebra_maps, reg_type, &s0, strlen(s0), 0);
562 if (space_split && **map == *CHR_SPACE)
566 if (strchr(REGEX_CHARS, *s1))
580 dst_term[j++] = '\0';
586 /* gen_regular_rel - generate regular expression from relation
587 * val: border value (inclusive)
588 * islt: 1 if <=; 0 if >=.
590 static void gen_regular_rel(char *dst, int val, int islt)
597 yaz_log(YLOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
601 strcpy(dst, "(-[0-9]+|(");
609 strcpy(dst, "([0-9]+|-(");
621 sprintf(numstr, "%d", val);
622 for (w = strlen(numstr); --w >= 0; pos++)
641 strcpy(dst + dst_p, numstr);
642 dst_p = strlen(dst) - pos - 1;
670 for (i = 0; i<pos; i++)
683 /* match everything less than 10^(pos-1) */
685 for (i=1; i<pos; i++)
686 strcat(dst, "[0-9]?");
690 /* match everything greater than 10^pos */
691 for (i = 0; i <= pos; i++)
692 strcat(dst, "[0-9]");
693 strcat(dst, "[0-9]*");
698 void string_rel_add_char(char **term_p, const char *src, int *indx)
700 if (src[*indx] == '\\')
701 *(*term_p)++ = src[(*indx)++];
702 *(*term_p)++ = src[(*indx)++];
706 * > abc ([b-].*|a[c-].*|ab[d-].*|abc.+)
707 * ([^-a].*|a[^-b].*ab[^-c].*|abc.+)
708 * >= abc ([b-].*|a[c-].*|ab[c-].*)
709 * ([^-a].*|a[^-b].*|ab[c-].*)
710 * < abc ([-0].*|a[-a].*|ab[-b].*)
711 * ([^a-].*|a[^b-].*|ab[^c-].*)
712 * <= abc ([-0].*|a[-a].*|ab[-b].*|abc)
713 * ([^a-].*|a[^b-].*|ab[^c-].*|abc)
715 static int string_relation(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
716 const char **term_sub, char *term_dict,
717 oid_value attributeSet,
718 int reg_type, int space_split, char *term_dst)
723 char *term_tmp = term_dict + strlen(term_dict);
724 char term_component[2*IT_MAX_WORD+20];
726 attr_init(&relation, zapt, 2);
727 relation_value = attr_find(&relation, NULL);
729 yaz_log(YLOG_DEBUG, "string relation value=%d", relation_value);
730 switch (relation_value)
733 if (!term_100 (zh->reg->zebra_maps, reg_type,
734 term_sub, term_component,
735 space_split, term_dst))
737 yaz_log(log_level_rpn, "Relation <");
740 for (i = 0; term_component[i]; )
747 string_rel_add_char (&term_tmp, term_component, &j);
752 string_rel_add_char (&term_tmp, term_component, &i);
759 if ((term_tmp - term_dict) > IT_MAX_WORD)
766 if (!term_100 (zh->reg->zebra_maps, reg_type,
767 term_sub, term_component,
768 space_split, term_dst))
770 yaz_log(log_level_rpn, "Relation <=");
773 for (i = 0; term_component[i]; )
778 string_rel_add_char (&term_tmp, term_component, &j);
782 string_rel_add_char (&term_tmp, term_component, &i);
791 if ((term_tmp - term_dict) > IT_MAX_WORD)
794 for (i = 0; term_component[i]; )
795 string_rel_add_char (&term_tmp, term_component, &i);
800 if (!term_100 (zh->reg->zebra_maps, reg_type,
801 term_sub, term_component, space_split, term_dst))
803 yaz_log(log_level_rpn, "Relation >");
806 for (i = 0; term_component[i];)
811 string_rel_add_char (&term_tmp, term_component, &j);
816 string_rel_add_char (&term_tmp, term_component, &i);
824 if ((term_tmp - term_dict) > IT_MAX_WORD)
827 for (i = 0; term_component[i];)
828 string_rel_add_char (&term_tmp, term_component, &i);
835 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub,
836 term_component, space_split, term_dst))
838 yaz_log(log_level_rpn, "Relation >=");
841 for (i = 0; term_component[i];)
848 string_rel_add_char (&term_tmp, term_component, &j);
851 if (term_component[i+1])
855 string_rel_add_char (&term_tmp, term_component, &i);
859 string_rel_add_char (&term_tmp, term_component, &i);
866 if ((term_tmp - term_dict) > IT_MAX_WORD)
874 yaz_log(log_level_rpn, "Relation =");
875 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub,
876 term_component, space_split, term_dst))
878 strcat(term_tmp, "(");
879 strcat(term_tmp, term_component);
880 strcat(term_tmp, ")");
885 static int string_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
886 const char **term_sub,
887 oid_value attributeSet, NMEM stream,
888 struct grep_info *grep_info,
889 int reg_type, int complete_flag,
890 int num_bases, char **basenames,
891 char *term_dst, int xpath_use);
893 static RSET term_trunc(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
894 const char **term_sub,
895 oid_value attributeSet, NMEM stream,
896 struct grep_info *grep_info,
897 int reg_type, int complete_flag,
898 int num_bases, char **basenames,
900 const char *rank_type, int xpath_use,
904 grep_info->isam_p_indx = 0;
905 r = string_term(zh, zapt, term_sub, attributeSet, stream, grep_info,
906 reg_type, complete_flag, num_bases, basenames,
907 term_dst, xpath_use);
910 yaz_log(log_level_rpn, "term: %s", term_dst);
911 return rset_trunc(zh, grep_info->isam_p_buf,
912 grep_info->isam_p_indx, term_dst,
913 strlen(term_dst), rank_type, 1 /* preserve pos */,
914 zapt->term->which, rset_nmem,
915 key_it_ctrl,key_it_ctrl->scope);
917 static char *nmem_strdup_i(NMEM nmem, int v)
920 sprintf (val_str, "%d", v);
921 return nmem_strdup(nmem, val_str);
924 static int string_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
925 const char **term_sub,
926 oid_value attributeSet, NMEM stream,
927 struct grep_info *grep_info,
928 int reg_type, int complete_flag,
929 int num_bases, char **basenames,
930 char *term_dst, int xpath_use)
932 char term_dict[2*IT_MAX_WORD+4000];
935 int truncation_value;
938 const char *use_string = 0;
939 oid_value curAttributeSet = attributeSet;
941 struct rpn_char_map_info rcmi;
942 int space_split = complete_flag ? 0 : 1;
944 int bases_ok = 0; /* no of databases with OK attribute */
945 int errCode = 0; /* err code (if any is not OK) */
946 char *errString = 0; /* addinfo */
948 rpn_char_map_prepare (zh->reg, reg_type, &rcmi);
949 attr_init (&use, zapt, 1);
950 use_value = attr_find_ex (&use, &curAttributeSet, &use_string);
951 yaz_log(log_level_rpn, "string_term, use value %d", use_value);
952 attr_init (&truncation, zapt, 5);
953 truncation_value = attr_find (&truncation, NULL);
954 yaz_log(log_level_rpn, "truncation value %d", truncation_value);
956 if (use_value == -1) /* no attribute - assumy "any" */
958 for (base_no = 0; base_no < num_bases; base_no++)
964 data1_local_attribute id_xpath_attr;
965 data1_local_attribute *local_attr;
966 int max_pos, prefix_len = 0;
970 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
972 zh->errCode = 109; /* Database unavailable */
973 zh->errString = basenames[base_no];
976 if (xpath_use > 0 && use_value == -2)
978 use_value = xpath_use;
979 attp.local_attributes = &id_xpath_attr;
980 attp.attset_ordinal = VAL_IDXPATH;
981 id_xpath_attr.next = 0;
982 id_xpath_attr.local = use_value;
984 else if (curAttributeSet == VAL_IDXPATH)
986 attp.local_attributes = &id_xpath_attr;
987 attp.attset_ordinal = VAL_IDXPATH;
988 id_xpath_attr.next = 0;
989 id_xpath_attr.local = use_value;
993 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value,
996 yaz_log(YLOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
997 curAttributeSet, use_value, r);
1000 /* set was found, but value wasn't defined */
1003 errString = nmem_strdup(stream, use_string);
1005 errString = nmem_strdup_i (stream, use_value);
1010 struct oident oident;
1012 oident.proto = PROTO_Z3950;
1013 oident.oclass = CLASS_ATTSET;
1014 oident.value = curAttributeSet;
1015 oid_ent_to_oid (&oident, oid);
1018 errString = nmem_strdup (stream, oident.desc);
1023 for (local_attr = attp.local_attributes; local_attr;
1024 local_attr = local_attr->next)
1030 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
1035 term_dict[prefix_len++] = '|';
1037 term_dict[prefix_len++] = '(';
1039 ord_len = key_SU_encode (ord, ord_buf);
1040 for (i = 0; i<ord_len; i++)
1042 term_dict[prefix_len++] = 1;
1043 term_dict[prefix_len++] = ord_buf[i];
1052 errString = nmem_strdup_i(stream, use_value);
1058 bases_ok++; /* this has OK attributes */
1062 term_dict[prefix_len++] = ')';
1063 term_dict[prefix_len++] = 1;
1064 term_dict[prefix_len++] = reg_type;
1065 yaz_log(log_level_rpn, "reg_type = %d", term_dict[prefix_len-1]);
1066 term_dict[prefix_len] = '\0';
1068 switch (truncation_value)
1070 case -1: /* not specified */
1071 case 100: /* do not truncate */
1072 if (!string_relation (zh, zapt, &termp, term_dict,
1074 reg_type, space_split, term_dst))
1077 case 1: /* right truncation */
1078 term_dict[j++] = '(';
1079 if (!term_100(zh->reg->zebra_maps, reg_type,
1080 &termp, term_dict + j, space_split, term_dst))
1082 strcat(term_dict, ".*)");
1084 case 2: /* keft truncation */
1085 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1086 if (!term_100(zh->reg->zebra_maps, reg_type,
1087 &termp, term_dict + j, space_split, term_dst))
1089 strcat(term_dict, ")");
1091 case 3: /* left&right truncation */
1092 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1093 if (!term_100(zh->reg->zebra_maps, reg_type,
1094 &termp, term_dict + j, space_split, term_dst))
1096 strcat(term_dict, ".*)");
1098 case 101: /* process # in term */
1099 term_dict[j++] = '(';
1100 if (!term_101(zh->reg->zebra_maps, reg_type,
1101 &termp, term_dict + j, space_split, term_dst))
1103 strcat(term_dict, ")");
1105 case 102: /* Regexp-1 */
1106 term_dict[j++] = '(';
1107 if (!term_102(zh->reg->zebra_maps, reg_type,
1108 &termp, term_dict + j, space_split, term_dst))
1110 strcat(term_dict, ")");
1112 case 103: /* Regexp-2 */
1114 term_dict[j++] = '(';
1116 if (!term_103 (zh->reg->zebra_maps, reg_type,
1117 &termp, term_dict + j, ®ex_range,
1118 space_split, term_dst))
1120 strcat(term_dict, ")");
1121 case 104: /* process # and ! in term */
1122 term_dict[j++] = '(';
1123 if (!term_104 (zh->reg->zebra_maps, reg_type,
1124 &termp, term_dict + j, space_split, term_dst))
1126 strcat(term_dict, ")");
1128 case 105: /* process * and ! in term */
1129 term_dict[j++] = '(';
1130 if (!term_105 (zh->reg->zebra_maps, reg_type,
1131 &termp, term_dict + j, space_split, term_dst, 1))
1133 strcat(term_dict, ")");
1135 case 106: /* process * and ! in term */
1136 term_dict[j++] = '(';
1137 if (!term_105 (zh->reg->zebra_maps, reg_type,
1138 &termp, term_dict + j, space_split, term_dst, 0))
1140 strcat(term_dict, ")");
1144 zh->errString = nmem_strdup_i(stream, truncation_value);
1149 yaz_log(log_level_rpn, "dict_lookup_grep: %s", term_dict+prefix_len);
1150 r = dict_lookup_grep(zh->reg->dict, term_dict, regex_range,
1151 grep_info, &max_pos, init_pos,
1154 yaz_log(YLOG_WARN, "dict_lookup_grep fail %d", r);
1159 zh->errCode = errCode;
1160 zh->errString = errString;
1164 yaz_log(YLOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1169 /* convert APT search term to UTF8 */
1170 static int zapt_term_to_utf8 (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1174 Z_Term *term = zapt->term;
1176 switch (term->which)
1178 case Z_Term_general:
1179 if (zh->iconv_to_utf8 != 0)
1181 char *inbuf = term->u.general->buf;
1182 size_t inleft = term->u.general->len;
1183 char *outbuf = termz;
1184 size_t outleft = IT_MAX_WORD-1;
1187 ret = yaz_iconv(zh->iconv_to_utf8, &inbuf, &inleft,
1189 if (ret == (size_t)(-1))
1191 ret = yaz_iconv(zh->iconv_to_utf8, 0, 0, 0, 0);
1199 sizez = term->u.general->len;
1200 if (sizez > IT_MAX_WORD-1)
1201 sizez = IT_MAX_WORD-1;
1202 memcpy (termz, term->u.general->buf, sizez);
1203 termz[sizez] = '\0';
1206 case Z_Term_characterString:
1207 sizez = strlen(term->u.characterString);
1208 if (sizez > IT_MAX_WORD-1)
1209 sizez = IT_MAX_WORD-1;
1210 memcpy (termz, term->u.characterString, sizez);
1211 termz[sizez] = '\0';
1220 /* convert APT SCAN term to internal cmap */
1221 static int trans_scan_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1222 char *termz, int reg_type)
1224 char termz0[IT_MAX_WORD];
1226 if (zapt_term_to_utf8(zh, zapt, termz0))
1227 return -1; /* error */
1231 const char *cp = (const char *) termz0;
1232 const char *cp_end = cp + strlen(cp);
1235 const char *space_map = NULL;
1238 while ((len = (cp_end - cp)) > 0)
1240 map = zebra_maps_input (zh->reg->zebra_maps, reg_type, &cp, len, 0);
1241 if (**map == *CHR_SPACE)
1246 for (src = space_map; *src; src++)
1249 for (src = *map; *src; src++)
1258 char *normalize_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1259 const char *termz, NMEM stream, unsigned reg_id)
1262 AttrType truncation;
1263 int truncation_value;
1266 attr_init (&truncation, zapt, 5);
1267 truncation_value = attr_find (&truncation, NULL);
1269 switch (truncation_value)
1289 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, ex_list,
1290 termz, strlen(termz));
1292 return nmem_strdup(stream, termz);
1295 char *buf = (char*) nmem_malloc(stream, wrbuf_len(wrbuf)+1);
1296 memcpy (buf, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1297 buf[wrbuf_len(wrbuf)] = '\0';
1302 static void grep_info_delete (struct grep_info *grep_info)
1305 xfree(grep_info->term_no);
1307 xfree (grep_info->isam_p_buf);
1310 static int grep_info_prepare (ZebraHandle zh,
1311 Z_AttributesPlusTerm *zapt,
1312 struct grep_info *grep_info,
1317 int termset_value_numeric;
1318 const char *termset_value_string;
1321 grep_info->term_no = 0;
1323 grep_info->isam_p_size = 0;
1324 grep_info->isam_p_buf = NULL;
1326 grep_info->reg_type = reg_type;
1327 grep_info->termset = 0;
1331 attr_init (&termset, zapt, 8);
1332 termset_value_numeric =
1333 attr_find_ex (&termset, NULL, &termset_value_string);
1334 if (termset_value_numeric != -1)
1337 const char *termset_name = 0;
1338 if (termset_value_numeric != -2)
1341 sprintf (resname, "%d", termset_value_numeric);
1342 termset_name = resname;
1345 termset_name = termset_value_string;
1346 yaz_log(log_level_rpn, "creating termset set %s", termset_name);
1347 grep_info->termset = resultSetAdd (zh, termset_name, 1);
1348 if (!grep_info->termset)
1351 zh->errString = nmem_strdup (stream, termset_name);
1359 static RSET rpn_search_APT_phrase (ZebraHandle zh,
1360 Z_AttributesPlusTerm *zapt,
1361 const char *termz_org,
1362 oid_value attributeSet,
1364 int reg_type, int complete_flag,
1365 const char *rank_type, int xpath_use,
1366 int num_bases, char **basenames,
1369 char term_dst[IT_MAX_WORD+1];
1370 RSET rset[TERM_LIST_LENGTH_MAX], result;
1372 struct grep_info grep_info;
1373 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1374 const char *termp = termz;
1377 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1379 for (; rset_no < sizeof(rset)/sizeof(*rset); rset_no++)
1381 yaz_log(log_level_rpn, "APT_phrase termp=%s", termp);
1382 rset[rset_no] = term_trunc(zh, zapt, &termp, attributeSet,
1384 reg_type, complete_flag,
1385 num_bases, basenames,
1386 term_dst, rank_type,
1387 xpath_use,rset_nmem);
1391 grep_info_delete (&grep_info);
1393 return rsnull_create (rset_nmem,key_it_ctrl);
1394 else if (rset_no == 1)
1397 result = rsprox_create( rset_nmem, key_it_ctrl, key_it_ctrl->scope,
1399 1 /* ordered */, 0 /* exclusion */,
1400 3 /* relation */, 1 /* distance */);
1404 static RSET rpn_search_APT_or_list (ZebraHandle zh,
1405 Z_AttributesPlusTerm *zapt,
1406 const char *termz_org,
1407 oid_value attributeSet,
1409 int reg_type, int complete_flag,
1410 const char *rank_type,
1412 int num_bases, char **basenames,
1415 char term_dst[IT_MAX_WORD+1];
1416 RSET rset[TERM_LIST_LENGTH_MAX];
1418 struct grep_info grep_info;
1419 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1420 const char *termp = termz;
1422 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1424 for (; rset_no < sizeof(rset)/sizeof(*rset); rset_no++)
1426 yaz_log(log_level_rpn, "APT_or_list termp=%s", termp);
1427 rset[rset_no] = term_trunc(zh, zapt, &termp, attributeSet,
1429 reg_type, complete_flag,
1430 num_bases, basenames,
1431 term_dst, rank_type,
1432 xpath_use,rset_nmem);
1436 grep_info_delete (&grep_info);
1438 return rsnull_create (rset_nmem,key_it_ctrl);
1439 return rsmultior_create(rset_nmem, key_it_ctrl,key_it_ctrl->scope,
1443 static RSET rpn_search_APT_and_list (ZebraHandle zh,
1444 Z_AttributesPlusTerm *zapt,
1445 const char *termz_org,
1446 oid_value attributeSet,
1448 int reg_type, int complete_flag,
1449 const char *rank_type,
1451 int num_bases, char **basenames,
1454 char term_dst[IT_MAX_WORD+1];
1455 RSET rset[TERM_LIST_LENGTH_MAX];
1457 struct grep_info grep_info;
1458 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1459 const char *termp = termz;
1461 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1463 for (; rset_no < sizeof(rset)/sizeof(*rset); rset_no++)
1465 yaz_log(log_level_rpn, "APT_and_list termp=%s", termp);
1466 rset[rset_no] = term_trunc(zh, zapt, &termp, attributeSet,
1468 reg_type, complete_flag,
1469 num_bases, basenames,
1470 term_dst, rank_type,
1471 xpath_use, rset_nmem);
1475 grep_info_delete (&grep_info);
1477 return rsnull_create (rset_nmem,key_it_ctrl);
1479 return rsmultiand_create( rset_nmem, key_it_ctrl, key_it_ctrl->scope,
1483 static int numeric_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1484 const char **term_sub,
1486 oid_value attributeSet,
1487 struct grep_info *grep_info,
1496 char *term_tmp = term_dict + strlen(term_dict);
1498 attr_init (&relation, zapt, 2);
1499 relation_value = attr_find (&relation, NULL);
1501 yaz_log(log_level_rpn, "numeric relation value=%d", relation_value);
1503 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub, term_tmp, 1,
1506 term_value = atoi (term_tmp);
1507 switch (relation_value)
1510 yaz_log(log_level_rpn, "Relation <");
1511 gen_regular_rel (term_tmp, term_value-1, 1);
1514 yaz_log(log_level_rpn, "Relation <=");
1515 gen_regular_rel (term_tmp, term_value, 1);
1518 yaz_log(log_level_rpn, "Relation >=");
1519 gen_regular_rel (term_tmp, term_value, 0);
1522 yaz_log(log_level_rpn, "Relation >");
1523 gen_regular_rel (term_tmp, term_value+1, 0);
1527 yaz_log(log_level_rpn, "Relation =");
1528 sprintf (term_tmp, "(0*%d)", term_value);
1530 yaz_log(log_level_rpn, "dict_lookup_grep: %s", term_tmp);
1531 r = dict_lookup_grep(zh->reg->dict, term_dict, 0, grep_info, max_pos,
1534 yaz_log(YLOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1535 yaz_log(log_level_rpn, "%d positions", grep_info->isam_p_indx);
1539 static int numeric_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1540 const char **term_sub,
1541 oid_value attributeSet, struct grep_info *grep_info,
1542 int reg_type, int complete_flag,
1543 int num_bases, char **basenames,
1544 char *term_dst, int xpath_use, NMEM stream)
1546 char term_dict[2*IT_MAX_WORD+2];
1550 const char *use_string = 0;
1551 oid_value curAttributeSet = attributeSet;
1553 struct rpn_char_map_info rcmi;
1555 int bases_ok = 0; /* no of databases with OK attribute */
1556 int errCode = 0; /* err code (if any is not OK) */
1557 char *errString = 0; /* addinfo */
1559 rpn_char_map_prepare (zh->reg, reg_type, &rcmi);
1560 attr_init (&use, zapt, 1);
1561 use_value = attr_find_ex (&use, &curAttributeSet, &use_string);
1563 if (use_value == -1)
1566 for (base_no = 0; base_no < num_bases; base_no++)
1569 data1_local_attribute id_xpath_attr;
1570 data1_local_attribute *local_attr;
1571 int max_pos, prefix_len = 0;
1574 if (use_value == -2) /* string attribute (assume IDXPATH/any) */
1576 use_value = xpath_use;
1577 attp.local_attributes = &id_xpath_attr;
1578 attp.attset_ordinal = VAL_IDXPATH;
1579 id_xpath_attr.next = 0;
1580 id_xpath_attr.local = use_value;
1582 else if (curAttributeSet == VAL_IDXPATH)
1584 attp.local_attributes = &id_xpath_attr;
1585 attp.attset_ordinal = VAL_IDXPATH;
1586 id_xpath_attr.next = 0;
1587 id_xpath_attr.local = use_value;
1591 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value,
1594 yaz_log(YLOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1595 curAttributeSet, use_value, r);
1598 errString = nmem_strdup_i(stream, use_value);
1606 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
1608 zh->errCode = 109; /* Database unavailable */
1609 zh->errString = basenames[base_no];
1612 for (local_attr = attp.local_attributes; local_attr;
1613 local_attr = local_attr->next)
1619 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
1624 term_dict[prefix_len++] = '|';
1626 term_dict[prefix_len++] = '(';
1628 ord_len = key_SU_encode (ord, ord_buf);
1629 for (i = 0; i<ord_len; i++)
1631 term_dict[prefix_len++] = 1;
1632 term_dict[prefix_len++] = ord_buf[i];
1638 errString = nmem_strdup_i(stream, use_value);
1642 term_dict[prefix_len++] = ')';
1643 term_dict[prefix_len++] = 1;
1644 term_dict[prefix_len++] = reg_type;
1645 yaz_log(YLOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1646 term_dict[prefix_len] = '\0';
1647 if (!numeric_relation (zh, zapt, &termp, term_dict,
1648 attributeSet, grep_info, &max_pos, reg_type,
1654 zh->errCode = errCode;
1655 zh->errString = errString;
1659 yaz_log(YLOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1663 static RSET rpn_search_APT_numeric (ZebraHandle zh,
1664 Z_AttributesPlusTerm *zapt,
1666 oid_value attributeSet,
1668 int reg_type, int complete_flag,
1669 const char *rank_type, int xpath_use,
1670 int num_bases, char **basenames,
1673 char term_dst[IT_MAX_WORD+1];
1674 const char *termp = termz;
1675 RSET rset[TERM_LIST_LENGTH_MAX];
1678 struct grep_info grep_info;
1680 yaz_log(log_level_rpn, "APT_numeric t='%s'",termz);
1681 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1683 for (; rset_no < sizeof(rset)/sizeof(*rset); rset_no++)
1685 yaz_log(YLOG_DEBUG, "APT_numeric termp=%s", termp);
1686 grep_info.isam_p_indx = 0;
1687 r = numeric_term(zh, zapt, &termp, attributeSet, &grep_info,
1688 reg_type, complete_flag, num_bases, basenames,
1689 term_dst, xpath_use,
1693 yaz_log(YLOG_DEBUG, "term: %s", term_dst);
1694 rset[rset_no] = rset_trunc(zh, grep_info.isam_p_buf,
1695 grep_info.isam_p_indx, term_dst,
1696 strlen(term_dst), rank_type,
1697 0 /* preserve position */,
1698 zapt->term->which, rset_nmem,
1699 key_it_ctrl,key_it_ctrl->scope);
1703 grep_info_delete (&grep_info);
1705 return rsnull_create (rset_nmem,key_it_ctrl);
1708 return rsmultiand_create(rset_nmem,key_it_ctrl,key_it_ctrl->scope,
1712 static RSET rpn_search_APT_local (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1714 oid_value attributeSet,
1716 const char *rank_type, NMEM rset_nmem)
1722 result = rstemp_create( rset_nmem,key_it_ctrl,key_it_ctrl->scope,
1723 res_get (zh->res, "setTmpDir"),0 );
1724 rsfd = rset_open (result, RSETF_WRITE);
1732 rset_write (rsfd, &key);
1737 static RSET rpn_sort_spec (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1738 oid_value attributeSet, NMEM stream,
1739 Z_SortKeySpecList *sort_sequence,
1740 const char *rank_type)
1743 int sort_relation_value;
1744 AttrType sort_relation_type;
1749 Z_AttributeElement *ae;
1754 attr_init (&sort_relation_type, zapt, 7);
1755 sort_relation_value = attr_find (&sort_relation_type, &attributeSet);
1757 attr_init (&use_type, zapt, 1);
1758 use_value = attr_find (&use_type, &attributeSet);
1760 if (!sort_sequence->specs)
1762 sort_sequence->num_specs = 10;
1763 sort_sequence->specs = (Z_SortKeySpec **)
1764 nmem_malloc(stream, sort_sequence->num_specs *
1765 sizeof(*sort_sequence->specs));
1766 for (i = 0; i<sort_sequence->num_specs; i++)
1767 sort_sequence->specs[i] = 0;
1769 if (zapt->term->which != Z_Term_general)
1772 i = atoi_n ((char *) zapt->term->u.general->buf,
1773 zapt->term->u.general->len);
1774 if (i >= sort_sequence->num_specs)
1776 sprintf (termz, "%d", i);
1778 oe.proto = PROTO_Z3950;
1779 oe.oclass = CLASS_ATTSET;
1780 oe.value = attributeSet;
1781 if (!oid_ent_to_oid (&oe, oid))
1784 sks = (Z_SortKeySpec *) nmem_malloc(stream, sizeof(*sks));
1785 sks->sortElement = (Z_SortElement *)
1786 nmem_malloc(stream, sizeof(*sks->sortElement));
1787 sks->sortElement->which = Z_SortElement_generic;
1788 sk = sks->sortElement->u.generic = (Z_SortKey *)
1789 nmem_malloc(stream, sizeof(*sk));
1790 sk->which = Z_SortKey_sortAttributes;
1791 sk->u.sortAttributes = (Z_SortAttributes *)
1792 nmem_malloc(stream, sizeof(*sk->u.sortAttributes));
1794 sk->u.sortAttributes->id = oid;
1795 sk->u.sortAttributes->list = (Z_AttributeList *)
1796 nmem_malloc(stream, sizeof(*sk->u.sortAttributes->list));
1797 sk->u.sortAttributes->list->num_attributes = 1;
1798 sk->u.sortAttributes->list->attributes = (Z_AttributeElement **)
1799 nmem_malloc(stream, sizeof(*sk->u.sortAttributes->list->attributes));
1800 ae = *sk->u.sortAttributes->list->attributes = (Z_AttributeElement *)
1801 nmem_malloc(stream, sizeof(**sk->u.sortAttributes->list->attributes));
1802 ae->attributeSet = 0;
1803 ae->attributeType = (int *)
1804 nmem_malloc(stream, sizeof(*ae->attributeType));
1805 *ae->attributeType = 1;
1806 ae->which = Z_AttributeValue_numeric;
1807 ae->value.numeric = (int *)
1808 nmem_malloc(stream, sizeof(*ae->value.numeric));
1809 *ae->value.numeric = use_value;
1811 sks->sortRelation = (int *)
1812 nmem_malloc(stream, sizeof(*sks->sortRelation));
1813 if (sort_relation_value == 1)
1814 *sks->sortRelation = Z_SortKeySpec_ascending;
1815 else if (sort_relation_value == 2)
1816 *sks->sortRelation = Z_SortKeySpec_descending;
1818 *sks->sortRelation = Z_SortKeySpec_ascending;
1820 sks->caseSensitivity = (int *)
1821 nmem_malloc(stream, sizeof(*sks->caseSensitivity));
1822 *sks->caseSensitivity = 0;
1824 sks->which = Z_SortKeySpec_null;
1825 sks->u.null = odr_nullval ();
1826 sort_sequence->specs[i] = sks;
1827 return rsnull_create (NULL,key_it_ctrl);
1828 /* FIXME - nmem?? */
1832 static int parse_xpath(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1833 oid_value attributeSet,
1834 struct xpath_location_step *xpath, int max, NMEM mem)
1836 oid_value curAttributeSet = attributeSet;
1838 const char *use_string = 0;
1840 attr_init (&use, zapt, 1);
1841 attr_find_ex (&use, &curAttributeSet, &use_string);
1843 if (!use_string || *use_string != '/')
1846 return zebra_parse_xpath_str(use_string, xpath, max, mem);
1851 static RSET xpath_trunc(ZebraHandle zh, NMEM stream,
1852 int reg_type, const char *term, int use,
1853 oid_value curAttributeSet, NMEM rset_nmem)
1856 struct grep_info grep_info;
1857 char term_dict[2048];
1860 int ord = zebraExplain_lookupSU (zh->reg->zei, curAttributeSet, use);
1861 int ord_len, i, r, max_pos;
1862 int term_type = Z_Term_characterString;
1863 const char *flags = "void";
1865 if (grep_info_prepare (zh, 0 /* zapt */, &grep_info, '0', stream))
1866 return rsnull_create (rset_nmem,key_it_ctrl);
1869 return rsnull_create (rset_nmem,key_it_ctrl);
1871 term_dict[prefix_len++] = '|';
1873 term_dict[prefix_len++] = '(';
1875 ord_len = key_SU_encode (ord, ord_buf);
1876 for (i = 0; i<ord_len; i++)
1878 term_dict[prefix_len++] = 1;
1879 term_dict[prefix_len++] = ord_buf[i];
1881 term_dict[prefix_len++] = ')';
1882 term_dict[prefix_len++] = 1;
1883 term_dict[prefix_len++] = reg_type;
1885 strcpy(term_dict+prefix_len, term);
1887 grep_info.isam_p_indx = 0;
1888 r = dict_lookup_grep(zh->reg->dict, term_dict, 0,
1889 &grep_info, &max_pos, 0, grep_handle);
1890 yaz_log (YLOG_LOG, "%s %d positions", term,
1891 grep_info.isam_p_indx);
1892 rset = rset_trunc(zh, grep_info.isam_p_buf,
1893 grep_info.isam_p_indx, term, strlen(term),
1894 flags, 1, term_type,rset_nmem,
1895 key_it_ctrl, key_it_ctrl->scope);
1896 grep_info_delete (&grep_info);
1900 static RSET rpn_search_xpath (ZebraHandle zh,
1901 oid_value attributeSet,
1902 int num_bases, char **basenames,
1903 NMEM stream, const char *rank_type, RSET rset,
1904 int xpath_len, struct xpath_location_step *xpath,
1907 oid_value curAttributeSet = attributeSet;
1914 yaz_log (YLOG_DEBUG, "xpath len=%d", xpath_len);
1915 for (i = 0; i<xpath_len; i++)
1917 yaz_log (log_level_rpn, "XPATH %d %s", i, xpath[i].part);
1921 curAttributeSet = VAL_IDXPATH;
1931 a[@attr=value]/b[@other=othervalue]
1933 /e/@a val range(e/,range(@a,freetext(w,1015,val),@a),e/)
1934 /a/b val range(b/a/,freetext(w,1016,val),b/a/)
1935 /a/b/@c val range(b/a/,range(@c,freetext(w,1016,val),@c),b/a/)
1936 /a/b[@c=y] val range(b/a/,freetext(w,1016,val),b/a/,@c=y)
1937 /a[@c=y]/b val range(a/,range(b/a/,freetext(w,1016,val),b/a/),a/,@c=y)
1938 /a[@c=x]/b[@c=y] range(a/,range(b/a/,freetext(w,1016,val),b/a/,@c=y),a/,@c=x)
1942 dict_grep_cmap (zh->reg->dict, 0, 0);
1944 for (base_no = 0; base_no < num_bases; base_no++)
1946 int level = xpath_len;
1949 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
1951 zh->errCode = 109; /* Database unavailable */
1952 zh->errString = basenames[base_no];
1955 while (--level >= 0)
1957 char xpath_rev[128];
1959 RSET rset_start_tag = 0, rset_end_tag = 0, rset_attr = 0;
1963 for (i = level; i >= 1; --i)
1965 const char *cp = xpath[i].part;
1971 memcpy (xpath_rev + len, "[^/]*", 5);
1974 else if (*cp == ' ')
1977 xpath_rev[len++] = 1;
1978 xpath_rev[len++] = ' ';
1982 xpath_rev[len++] = *cp;
1983 xpath_rev[len++] = '/';
1985 else if (i == 1) /* // case */
1987 xpath_rev[len++] = '.';
1988 xpath_rev[len++] = '*';
1993 if (xpath[level].predicate &&
1994 xpath[level].predicate->which == XPATH_PREDICATE_RELATION &&
1995 xpath[level].predicate->u.relation.name[0])
1997 WRBUF wbuf = wrbuf_alloc();
1998 wrbuf_puts(wbuf, xpath[level].predicate->u.relation.name+1);
1999 if (xpath[level].predicate->u.relation.value)
2001 const char *cp = xpath[level].predicate->u.relation.value;
2002 wrbuf_putc(wbuf, '=');
2006 if (strchr(REGEX_CHARS, *cp))
2007 wrbuf_putc(wbuf, '\\');
2008 wrbuf_putc(wbuf, *cp);
2012 wrbuf_puts(wbuf, "");
2013 rset_attr = xpath_trunc(
2014 zh, stream, '0', wrbuf_buf(wbuf), 3,
2015 curAttributeSet,rset_nmem);
2016 wrbuf_free(wbuf, 1);
2023 yaz_log (log_level_rpn, "xpath_rev (%d) = %s", level, xpath_rev);
2024 if (strlen(xpath_rev))
2026 rset_start_tag = xpath_trunc(zh, stream, '0',
2027 xpath_rev, 1, curAttributeSet, rset_nmem);
2029 rset_end_tag = xpath_trunc(zh, stream, '0',
2030 xpath_rev, 2, curAttributeSet, rset_nmem);
2033 parms.key_size = sizeof(struct it_key);
2034 parms.cmp = key_compare_it;
2035 parms.rset_l = rset_start_tag;
2036 parms.rset_m = rset;
2037 parms.rset_r = rset_end_tag;
2038 parms.rset_attr = rset_attr;
2039 parms.printer = key_print_it;
2040 rset = rset_create (rset_kind_between, &parms);
2042 rset=rsbetween_create( rset_nmem,key_it_ctrl,key_it_ctrl->scope,
2043 rset_start_tag, rset, rset_end_tag, rset_attr);
2054 static RSET rpn_search_APT (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2055 oid_value attributeSet, NMEM stream,
2056 Z_SortKeySpecList *sort_sequence,
2057 int num_bases, char **basenames,
2061 char *search_type = NULL;
2062 char rank_type[128];
2065 char termz[IT_MAX_WORD+1];
2069 struct xpath_location_step xpath[10];
2073 log_level_rpn = yaz_log_module_level("rpn");
2076 zebra_maps_attr (zh->reg->zebra_maps, zapt, ®_id, &search_type,
2077 rank_type, &complete_flag, &sort_flag);
2079 yaz_log(YLOG_DEBUG, "reg_id=%c", reg_id);
2080 yaz_log(YLOG_DEBUG, "complete_flag=%d", complete_flag);
2081 yaz_log(YLOG_DEBUG, "search_type=%s", search_type);
2082 yaz_log(YLOG_DEBUG, "rank_type=%s", rank_type);
2084 if (zapt_term_to_utf8(zh, zapt, termz))
2088 return rpn_sort_spec (zh, zapt, attributeSet, stream, sort_sequence,
2090 xpath_len = parse_xpath(zh, zapt, attributeSet, xpath, 10, stream);
2094 if (xpath[xpath_len-1].part[0] == '@')
2098 if (!strcmp (search_type, "phrase"))
2100 rset = rpn_search_APT_phrase (zh, zapt, termz, attributeSet, stream,
2101 reg_id, complete_flag, rank_type,
2103 num_bases, basenames, rset_nmem);
2105 else if (!strcmp (search_type, "and-list"))
2107 rset = rpn_search_APT_and_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, "or-list"))
2114 rset = rpn_search_APT_or_list (zh, zapt, termz, attributeSet, stream,
2115 reg_id, complete_flag, rank_type,
2117 num_bases, basenames, rset_nmem);
2119 else if (!strcmp (search_type, "local"))
2121 rset = rpn_search_APT_local (zh, zapt, termz, attributeSet, stream,
2122 rank_type, rset_nmem);
2124 else if (!strcmp (search_type, "numeric"))
2126 rset = rpn_search_APT_numeric (zh, zapt, termz, attributeSet, stream,
2127 reg_id, complete_flag, rank_type,
2129 num_bases, basenames, rset_nmem);
2131 else if (!strcmp (search_type, "always"))
2137 return rpn_search_xpath (zh, attributeSet, num_bases, basenames,
2138 stream, rank_type, rset,
2139 xpath_len, xpath, rset_nmem);
2142 static RSET rpn_search_structure (ZebraHandle zh, Z_RPNStructure *zs,
2143 oid_value attributeSet,
2144 NMEM stream, NMEM rset_nmem,
2145 Z_SortKeySpecList *sort_sequence,
2146 int num_bases, char **basenames)
2149 if (zs->which == Z_RPNStructure_complex)
2151 Z_Operator *zop = zs->u.complex->roperator;
2152 RSET rsets[2]; /* l and r argument */
2154 rsets[0]=rpn_search_structure (zh, zs->u.complex->s1,
2155 attributeSet, stream, rset_nmem,
2157 num_bases, basenames);
2158 if (rsets[0] == NULL)
2160 rsets[1]=rpn_search_structure (zh, zs->u.complex->s2,
2161 attributeSet, stream, rset_nmem,
2163 num_bases, basenames);
2164 if (rsets[1] == NULL)
2166 rset_delete (rsets[0]);
2172 case Z_Operator_and:
2173 r=rsmultiand_create(rset_nmem, key_it_ctrl, key_it_ctrl->scope,
2177 r=rsmultior_create(rset_nmem, key_it_ctrl, key_it_ctrl->scope,
2180 case Z_Operator_and_not:
2181 r = rsbool_create_not(rset_nmem,key_it_ctrl, key_it_ctrl->scope,
2184 case Z_Operator_prox:
2185 if (zop->u.prox->which != Z_ProximityOperator_known)
2190 if (*zop->u.prox->u.known != Z_ProxUnit_word)
2192 char *val = (char *) nmem_malloc(stream, 16);
2194 zh->errString = val;
2195 sprintf (val, "%d", *zop->u.prox->u.known);
2200 /* new / old prox */
2201 r=rsprox_create(rset_nmem,key_it_ctrl,key_it_ctrl->scope,
2203 *zop->u.prox->ordered,
2204 (!zop->u.prox->exclusion ?
2205 0 : *zop->u.prox->exclusion),
2206 *zop->u.prox->relationType,
2207 *zop->u.prox->distance );
2215 else if (zs->which == Z_RPNStructure_simple)
2217 if (zs->u.simple->which == Z_Operand_APT)
2219 yaz_log(YLOG_DEBUG, "rpn_search_APT");
2220 r = rpn_search_APT (zh, zs->u.simple->u.attributesPlusTerm,
2221 attributeSet, stream, sort_sequence,
2222 num_bases, basenames,rset_nmem);
2224 else if (zs->u.simple->which == Z_Operand_resultSetId)
2226 yaz_log(YLOG_DEBUG, "rpn_search_ref");
2227 r = resultSetRef (zh, zs->u.simple->u.resultSetId);
2230 r = rsnull_create (rset_nmem,key_it_ctrl);
2233 nmem_strdup (stream, zs->u.simple->u.resultSetId);
2254 RSET rpn_search(ZebraHandle zh, NMEM nmem, NMEM rset_nmem,
2255 Z_RPNQuery *rpn, int num_bases, char **basenames,
2256 const char *setname,
2261 oid_value attributeSet;
2262 Z_SortKeySpecList *sort_sequence;
2266 zh->errString = NULL;
2269 sort_sequence = (Z_SortKeySpecList *)
2270 nmem_malloc(nmem, sizeof(*sort_sequence));
2271 sort_sequence->num_specs = 10; /* FIXME - Hard-coded number */
2272 sort_sequence->specs = (Z_SortKeySpec **)
2273 nmem_malloc(nmem, sort_sequence->num_specs *
2274 sizeof(*sort_sequence->specs));
2275 for (i = 0; i<sort_sequence->num_specs; i++)
2276 sort_sequence->specs[i] = 0;
2278 attrset = oid_getentbyoid (rpn->attributeSetId);
2279 attributeSet = attrset->value;
2280 rset = rpn_search_structure (zh, rpn->RPNStructure, attributeSet,
2282 sort_sequence, num_bases, basenames);
2287 yaz_log(YLOG_DEBUG, "search error: %d", zh->errCode);
2289 for (i = 0; sort_sequence->specs[i]; i++)
2291 sort_sequence->num_specs = i;
2293 resultSetRank (zh, sset, rset, rset_nmem);
2296 yaz_log(YLOG_DEBUG, "resultSetSortSingle in rpn_search");
2297 resultSetSortSingle (zh, nmem, sset, rset,
2298 sort_sequence, &sort_status);
2301 yaz_log(YLOG_DEBUG, "resultSetSortSingle status = %d", zh->errCode);
2307 struct scan_info_entry {
2313 struct scan_info_entry *list;
2319 static int scan_handle (char *name, const char *info, int pos, void *client)
2321 int len_prefix, idx;
2322 struct scan_info *scan_info = (struct scan_info *) client;
2324 len_prefix = strlen(scan_info->prefix);
2325 if (memcmp (name, scan_info->prefix, len_prefix))
2327 if (pos > 0) idx = scan_info->after - pos + scan_info->before;
2330 scan_info->list[idx].term = (char *)
2331 odr_malloc(scan_info->odr, strlen(name + len_prefix)+1);
2332 strcpy(scan_info->list[idx].term, name + len_prefix);
2333 assert (*info == sizeof(ISAMC_P));
2334 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAMC_P));
2338 static void scan_term_untrans (ZebraHandle zh, NMEM stream, int reg_type,
2339 char **dst, const char *src)
2341 char term_src[IT_MAX_WORD];
2342 char term_dst[IT_MAX_WORD];
2344 term_untrans (zh, reg_type, term_src, src);
2346 if (zh->iconv_from_utf8 != 0)
2349 char *inbuf = term_src;
2350 size_t inleft = strlen(term_src);
2351 char *outbuf = term_dst;
2352 size_t outleft = sizeof(term_dst)-1;
2355 ret = yaz_iconv (zh->iconv_from_utf8, &inbuf, &inleft,
2357 if (ret == (size_t)(-1))
2360 len = outbuf - term_dst;
2361 *dst = nmem_malloc(stream, len + 1);
2363 memcpy (*dst, term_dst, len);
2367 *dst = nmem_strdup(stream, term_src);
2370 static void count_set (RSET r, int *count)
2377 yaz_log(YLOG_DEBUG, "count_set");
2380 rfd = rset_open (r, RSETF_READ);
2381 while (rset_read (rfd, &key,0 /* never mind terms */))
2383 if (key.mem[0] != psysno)
2385 psysno = key.mem[0];
2391 yaz_log(YLOG_DEBUG, "%d keys, %d records", kno, *count);
2394 void rpn_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
2395 oid_value attributeset,
2396 int num_bases, char **basenames,
2397 int *position, int *num_entries, ZebraScanEntry **list,
2398 int *is_partial, RSET limit_set, int return_zero)
2401 int pos = *position;
2402 int num = *num_entries;
2406 char termz[IT_MAX_WORD+20];
2409 const char *use_string = 0;
2410 struct scan_info *scan_info_array;
2411 ZebraScanEntry *glist;
2412 int ords[32], ord_no = 0;
2415 int bases_ok = 0; /* no of databases with OK attribute */
2416 int errCode = 0; /* err code (if any is not OK) */
2417 char *errString = 0; /* addinfo */
2420 char *search_type = NULL;
2421 char rank_type[128];
2424 NMEM rset_nmem=NULL;
2428 if (attributeset == VAL_NONE)
2429 attributeset = VAL_BIB1;
2434 int termset_value_numeric;
2435 const char *termset_value_string;
2436 attr_init (&termset, zapt, 8);
2437 termset_value_numeric =
2438 attr_find_ex (&termset, NULL, &termset_value_string);
2439 if (termset_value_numeric != -1)
2442 const char *termset_name = 0;
2444 if (termset_value_numeric != -2)
2447 sprintf (resname, "%d", termset_value_numeric);
2448 termset_name = resname;
2451 termset_name = termset_value_string;
2453 limit_set = resultSetRef (zh, termset_name);
2457 yaz_log (YLOG_DEBUG, "position = %d, num = %d set=%d",
2458 pos, num, attributeset);
2460 attr_init (&use, zapt, 1);
2461 use_value = attr_find_ex (&use, &attributeset, &use_string);
2463 if (zebra_maps_attr (zh->reg->zebra_maps, zapt, ®_id, &search_type,
2464 rank_type, &complete_flag, &sort_flag))
2470 yaz_log (YLOG_DEBUG, "use_value = %d", use_value);
2472 if (use_value == -1)
2474 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
2478 data1_local_attribute *local_attr;
2480 if ((r=att_getentbyatt (zh, &attp, attributeset, use_value,
2483 yaz_log(YLOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
2484 attributeset, use_value);
2488 sprintf (val_str, "%d", use_value);
2490 errString = odr_strdup (stream, val_str);
2496 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
2498 zh->errString = basenames[base_no];
2499 zh->errCode = 109; /* Database unavailable */
2504 for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
2505 local_attr = local_attr->next)
2509 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
2512 ords[ord_no++] = ord;
2515 if (!bases_ok && errCode)
2517 zh->errCode = errCode;
2518 zh->errString = errString;
2526 /* prepare dictionary scanning */
2529 scan_info_array = (struct scan_info *)
2530 odr_malloc(stream, ord_no * sizeof(*scan_info_array));
2531 for (i = 0; i < ord_no; i++)
2533 int j, prefix_len = 0;
2534 int before_tmp = before, after_tmp = after;
2535 struct scan_info *scan_info = scan_info_array + i;
2536 struct rpn_char_map_info rcmi;
2538 rpn_char_map_prepare (zh->reg, reg_id, &rcmi);
2540 scan_info->before = before;
2541 scan_info->after = after;
2542 scan_info->odr = stream;
2544 scan_info->list = (struct scan_info_entry *)
2545 odr_malloc(stream, (before+after) * sizeof(*scan_info->list));
2546 for (j = 0; j<before+after; j++)
2547 scan_info->list[j].term = NULL;
2549 prefix_len += key_SU_encode (ords[i], termz + prefix_len);
2550 termz[prefix_len++] = reg_id;
2551 termz[prefix_len] = 0;
2552 strcpy(scan_info->prefix, termz);
2554 if (trans_scan_term(zh, zapt, termz+prefix_len, reg_id))
2557 dict_scan(zh->reg->dict, termz, &before_tmp, &after_tmp,
2558 scan_info, scan_handle);
2560 glist = (ZebraScanEntry *)
2561 odr_malloc(stream, (before+after)*sizeof(*glist));
2563 rset_nmem = nmem_create();
2565 /* consider terms after main term */
2566 for (i = 0; i < ord_no; i++)
2570 for (i = 0; i<after; i++)
2573 const char *mterm = NULL;
2577 for (j = 0; j < ord_no; j++)
2579 if (ptr[j] < before+after &&
2580 (tst=scan_info_array[j].list[ptr[j]].term) &&
2581 (!mterm || strcmp (tst, mterm) < 0))
2589 scan_term_untrans (zh, stream->mem, reg_id,
2590 &glist[i+before].term, mterm);
2591 rset = rset_trunc(zh, &scan_info_array[j0].list[ptr[j0]].isam_p, 1,
2592 glist[i+before].term, strlen(glist[i+before].term),
2593 NULL, 0, zapt->term->which, rset_nmem,
2594 key_it_ctrl,key_it_ctrl->scope);
2596 for (j = j0+1; j<ord_no; j++)
2598 if (ptr[j] < before+after &&
2599 (tst=scan_info_array[j].list[ptr[j]].term) &&
2600 !strcmp (tst, mterm))
2605 rset_trunc(zh, &scan_info_array[j].list[ptr[j]].isam_p, 1,
2606 glist[i+before].term,
2607 strlen(glist[i+before].term), NULL, 0,
2608 zapt->term->which,rset_nmem,
2609 key_it_ctrl, key_it_ctrl->scope);
2610 rset = rsbool_create_or(rset_nmem,key_it_ctrl,
2611 key_it_ctrl->scope, rset, rset2);
2612 /* FIXME - Use a proper multi-or */
2618 rset = rsbool_create_and(rset_nmem,key_it_ctrl,key_it_ctrl->scope,
2619 rset, rset_dup(limit_set));
2620 count_set (rset, &glist[i+before].occurrences);
2625 *num_entries -= (after-i);
2629 /* consider terms before main term */
2630 for (i = 0; i<ord_no; i++)
2633 for (i = 0; i<before; i++)
2636 const char *mterm = NULL;
2640 for (j = 0; j <ord_no; j++)
2642 if (ptr[j] < before &&
2643 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2644 (!mterm || strcmp (tst, mterm) > 0))
2653 scan_term_untrans (zh, stream->mem, reg_id,
2654 &glist[before-1-i].term, mterm);
2657 (zh, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1,
2658 glist[before-1-i].term, strlen(glist[before-1-i].term),
2659 NULL, 0, zapt->term->which,rset_nmem,
2660 key_it_ctrl,key_it_ctrl->scope);
2664 for (j = j0+1; j<ord_no; j++)
2666 if (ptr[j] < before &&
2667 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2668 !strcmp (tst, mterm))
2674 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1,
2675 glist[before-1-i].term,
2676 strlen(glist[before-1-i].term), NULL, 0,
2677 zapt->term->which, rset_nmem,
2678 key_it_ctrl, key_it_ctrl->scope);
2679 rset = rsbool_create_and(rset_nmem,key_it_ctrl,
2680 key_it_ctrl->scope, rset, rset2);
2681 /* FIXME - multi-and ?? */
2686 rset = rsbool_create_and(rset_nmem,key_it_ctrl,key_it_ctrl->scope,
2687 rset, rset_dup(limit_set));
2688 count_set (rset, &glist[before-1-i].occurrences);
2699 nmem_destroy(rset_nmem);
2700 *list = glist + i; /* list is set to first 'real' entry */
2702 yaz_log(YLOG_DEBUG, "position = %d, num_entries = %d",
2703 *position, *num_entries);
2705 yaz_log(YLOG_DEBUG, "scan error: %d", zh->errCode);