1 /* $Id: zrpn.c,v 1.141.2.9 2005-04-29 18:38:49 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>
40 #include <rsbetween.h>
43 struct rpn_char_map_info {
52 Z_AttributesPlusTerm *zapt;
56 static const char **rpn_char_map_handler(void *vp, const char **from, int len)
58 struct rpn_char_map_info *p = (struct rpn_char_map_info *) vp;
59 const char **out = zebra_maps_input(p->zm, p->reg_type, from, len, 0);
63 const char *outp = *out;
64 yaz_log(LOG_LOG, "---");
67 yaz_log(LOG_LOG, "%02X", *outp);
75 static void rpn_char_map_prepare(struct zebra_register *reg, int reg_type,
76 struct rpn_char_map_info *map_info)
78 map_info->zm = reg->zebra_maps;
79 map_info->reg_type = reg_type;
80 dict_grep_cmap(reg->dict, map_info, rpn_char_map_handler);
83 static int attr_find_ex(AttrType *src, oid_value *attributeSetP,
84 const char **string_value)
88 num_attributes = src->zapt->attributes->num_attributes;
89 while (src->major < num_attributes)
91 Z_AttributeElement *element;
93 element = src->zapt->attributes->attributes[src->major];
94 if (src->type == *element->attributeType)
96 switch (element->which)
98 case Z_AttributeValue_numeric:
100 if (element->attributeSet && attributeSetP)
104 attrset = oid_getentbyoid(element->attributeSet);
105 *attributeSetP = attrset->value;
107 return *element->value.numeric;
109 case Z_AttributeValue_complex:
110 if (src->minor >= element->value.complex->num_list)
112 if (element->attributeSet && attributeSetP)
116 attrset = oid_getentbyoid(element->attributeSet);
117 *attributeSetP = attrset->value;
119 if (element->value.complex->list[src->minor]->which ==
120 Z_StringOrNumeric_numeric)
124 *element->value.complex->list[src->minor-1]->u.numeric;
126 else if (element->value.complex->list[src->minor]->which ==
127 Z_StringOrNumeric_string)
133 element->value.complex->list[src->minor-1]->u.string;
147 static int attr_find(AttrType *src, oid_value *attributeSetP)
149 return attr_find_ex(src, attributeSetP, 0);
152 static void attr_init(AttrType *src, Z_AttributesPlusTerm *zapt,
175 static void term_untrans(ZebraHandle zh, int reg_type,
176 char *dst, const char *src)
181 const char *cp = zebra_maps_output(zh->reg->zebra_maps,
183 if (!cp && len < IT_MAX_WORD-1)
186 while (*cp && len < IT_MAX_WORD-1)
192 static void add_isam_p(const char *name, const char *info,
195 if (p->isam_p_indx == p->isam_p_size)
197 ISAMS_P *new_isam_p_buf;
201 p->isam_p_size = 2*p->isam_p_size + 100;
202 new_isam_p_buf = (ISAMS_P *) xmalloc(sizeof(*new_isam_p_buf) *
206 memcpy(new_isam_p_buf, p->isam_p_buf,
207 p->isam_p_indx * sizeof(*p->isam_p_buf));
208 xfree(p->isam_p_buf);
210 p->isam_p_buf = new_isam_p_buf;
213 new_term_no = (int *) xmalloc(sizeof(*new_term_no) *
217 memcpy(new_term_no, p->isam_p_buf,
218 p->isam_p_indx * sizeof(*p->term_no));
221 p->term_no = new_term_no;
224 assert(*info == sizeof(*p->isam_p_buf));
225 memcpy(p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
232 char term_tmp[IT_MAX_WORD];
234 int len = key_SU_decode(&su_code, name);
236 term_untrans(p->zh, p->reg_type, term_tmp, name+len+1);
237 yaz_log(LOG_LOG, "grep: %d %c %s", su_code, name[len], term_tmp);
238 zebraExplain_lookup_ord(p->zh->reg->zei,
239 su_code, &db, &set, &use);
240 yaz_log(LOG_LOG, "grep: set=%d use=%d db=%s", set, use, db);
242 resultSetAddTerm(p->zh, p->termset, name[len], db,
249 static int grep_handle(char *name, const char *info, void *p)
251 add_isam_p(name, info, (struct grep_info *) p);
255 static int term_pre(ZebraMaps zebra_maps, int reg_type, const char **src,
256 const char *ct1, const char *ct2, int first)
258 const char *s1, *s0 = *src;
261 /* skip white space */
264 if (ct1 && strchr(ct1, *s0))
266 if (ct2 && strchr(ct2, *s0))
269 map = zebra_maps_input(zebra_maps, reg_type, &s1, strlen(s1), first);
270 if (**map != *CHR_SPACE)
279 static void esc_str(char *out_buf, int out_size,
280 const char *in_buf, int in_size)
286 assert(out_size > 20);
288 for (k = 0; k<in_size; k++)
290 int c = in_buf[k] & 0xff;
292 if (c < 32 || c > 126)
296 sprintf(out_buf +strlen(out_buf), "%02X:%c ", c, pc);
297 if (strlen(out_buf) > out_size-20)
299 strcat(out_buf, "..");
305 #define REGEX_CHARS " []()|.*+?!"
307 /* term_100: handle term, where trunc = none(no operators at all) */
308 static int term_100(ZebraMaps zebra_maps, int reg_type,
309 const char **src, char *dst, int space_split,
317 const char *space_start = 0;
318 const char *space_end = 0;
320 if (!term_pre(zebra_maps, reg_type, src, NULL, NULL, !space_split))
327 map = zebra_maps_search(zebra_maps, reg_type, &s0, strlen(s0),
331 if (**map == *CHR_SPACE)
334 else /* complete subfield only. */
336 if (**map == *CHR_SPACE)
337 { /* save space mapping for later .. */
342 else if (space_start)
343 { /* reload last space */
344 while (space_start < space_end)
346 if (strchr(REGEX_CHARS, *space_start))
348 dst_term[j++] = *space_start;
349 dst[i++] = *space_start++;
352 space_start = space_end = 0;
355 /* add non-space char */
356 memcpy(dst_term+j, s1, s0 - s1);
362 if (strchr(REGEX_CHARS, *s1))
370 esc_str(tmpbuf, sizeof(tmpbuf), map[0], strlen(map[0]));
372 strcpy(dst + i, map[0]);
382 /* term_101: handle term, where trunc = Process # */
383 static int term_101(ZebraMaps zebra_maps, int reg_type,
384 const char **src, char *dst, int space_split,
392 if (!term_pre(zebra_maps, reg_type, src, "#", "#", !space_split))
401 dst_term[j++] = *s0++;
407 map = zebra_maps_search(zebra_maps, reg_type, &s0, strlen(s0),
409 if (space_split && **map == *CHR_SPACE)
412 /* add non-space char */
413 memcpy(dst_term+j, s1, s0 - s1);
419 if (strchr(REGEX_CHARS, *s1))
427 esc_str(tmpbuf, sizeof(tmpbuf), map[0], strlen(map[0]));
429 strcpy(dst + i, map[0]);
435 dst_term[j++] = '\0';
440 /* term_103: handle term, where trunc = re-2 (regular expressions) */
441 static int term_103(ZebraMaps zebra_maps, int reg_type, const char **src,
442 char *dst, int *errors, int space_split,
450 if (!term_pre(zebra_maps, reg_type, src, "^\\()[].*+?|", "(", !space_split))
453 if (errors && *s0 == '+' && s0[1] && s0[2] == '+' && s0[3] &&
454 isdigit(((const unsigned char *)s0)[1]))
456 *errors = s0[1] - '0';
463 if (strchr("^\\()[].*+?|-", *s0))
472 map = zebra_maps_search(zebra_maps, reg_type, &s0, strlen(s0),
474 if (space_split && **map == *CHR_SPACE)
477 /* add non-space char */
478 memcpy(dst_term+j, s1, s0 - s1);
484 if (strchr(REGEX_CHARS, *s1))
492 esc_str(tmpbuf, sizeof(tmpbuf), map[0], strlen(map[0]));
494 strcpy(dst + i, map[0]);
506 /* term_103: handle term, where trunc = re-1 (regular expressions) */
507 static int term_102 (ZebraMaps zebra_maps, int reg_type, const char **src,
508 char *dst, int space_split, char *dst_term)
510 return term_103(zebra_maps, reg_type, src, dst, NULL, space_split,
514 /* term_104: handle term, where trunc=Process # and ! */
515 static int term_104(ZebraMaps zebra_maps, int reg_type,
516 const char **src, char *dst, int space_split,
524 if (!term_pre(zebra_maps, reg_type, src, "?*#", "?*#", !space_split))
531 dst_term[j++] = *s0++;
532 if (*s0 >= '0' && *s0 <= '9')
535 while (*s0 >= '0' && *s0 <= '9')
537 limit = limit * 10 + (*s0 - '0');
538 dst_term[j++] = *s0++;
558 dst_term[j++] = *s0++;
563 dst_term[j++] = *s0++;
568 map = zebra_maps_input(zebra_maps, reg_type, &s0, strlen(s0), 0);
569 if (space_split && **map == *CHR_SPACE)
573 if (strchr(REGEX_CHARS, *s1))
581 dst_term[j++] = '\0';
586 /* term_105/106: handle term, where trunc=Process * and ! and right trunc */
587 static int term_105 (ZebraMaps zebra_maps, int reg_type,
588 const char **src, char *dst, int space_split,
589 char *dst_term, int right_truncate)
596 if (!term_pre(zebra_maps, reg_type, src, "*!", "*!", !space_split))
605 dst_term[j++] = *s0++;
610 dst_term[j++] = *s0++;
615 map = zebra_maps_input(zebra_maps, reg_type, &s0, strlen(s0), 0);
616 if (space_split && **map == *CHR_SPACE)
620 if (strchr(REGEX_CHARS, *s1))
634 dst_term[j++] = '\0';
640 /* gen_regular_rel - generate regular expression from relation
641 * val: border value (inclusive)
642 * islt: 1 if <=; 0 if >=.
644 static void gen_regular_rel(char *dst, int val, int islt)
651 yaz_log(LOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
655 strcpy(dst, "(-[0-9]+|(");
663 strcpy(dst, "([0-9]+|-(");
675 sprintf(numstr, "%d", val);
676 for (w = strlen(numstr); --w >= 0; pos++)
695 strcpy(dst + dst_p, numstr);
696 dst_p = strlen(dst) - pos - 1;
724 for (i = 0; i<pos; i++)
737 /* match everything less than 10^(pos-1) */
739 for (i = 1; i<pos; i++)
740 strcat(dst, "[0-9]?");
744 /* match everything greater than 10^pos */
745 for (i = 0; i <= pos; i++)
746 strcat(dst, "[0-9]");
747 strcat(dst, "[0-9]*");
752 void string_rel_add_char(char **term_p, const char *src, int *indx)
754 if (src[*indx] == '\\')
755 *(*term_p)++ = src[(*indx)++];
756 *(*term_p)++ = src[(*indx)++];
760 * > abc ([b-].*|a[c-].*|ab[d-].*|abc.+)
761 * ([^-a].*|a[^-b].*ab[^-c].*|abc.+)
762 * >= abc ([b-].*|a[c-].*|ab[c-].*)
763 * ([^-a].*|a[^-b].*|ab[c-].*)
764 * < abc ([-0].*|a[-a].*|ab[-b].*)
765 * ([^a-].*|a[^b-].*|ab[^c-].*)
766 * <= abc ([-0].*|a[-a].*|ab[-b].*|abc)
767 * ([^a-].*|a[^b-].*|ab[^c-].*|abc)
769 static int string_relation(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
770 const char **term_sub, char *term_dict,
771 oid_value attributeSet,
772 int reg_type, int space_split, char *term_dst)
777 char *term_tmp = term_dict + strlen(term_dict);
778 char term_component[2*IT_MAX_WORD+20];
780 attr_init(&relation, zapt, 2);
781 relation_value = attr_find(&relation, NULL);
783 yaz_log(LOG_DEBUG, "string relation value=%d", relation_value);
784 switch (relation_value)
787 if (!term_100 (zh->reg->zebra_maps, reg_type,
788 term_sub, term_component,
789 space_split, term_dst))
791 yaz_log(LOG_DEBUG, "Relation <");
794 for (i = 0; term_component[i]; )
801 string_rel_add_char (&term_tmp, term_component, &j);
806 string_rel_add_char (&term_tmp, term_component, &i);
813 if ((term_tmp - term_dict) > IT_MAX_WORD)
820 if (!term_100 (zh->reg->zebra_maps, reg_type,
821 term_sub, term_component,
822 space_split, term_dst))
824 yaz_log(LOG_DEBUG, "Relation <=");
827 for (i = 0; term_component[i]; )
832 string_rel_add_char (&term_tmp, term_component, &j);
836 string_rel_add_char (&term_tmp, term_component, &i);
845 if ((term_tmp - term_dict) > IT_MAX_WORD)
848 for (i = 0; term_component[i]; )
849 string_rel_add_char (&term_tmp, term_component, &i);
854 if (!term_100 (zh->reg->zebra_maps, reg_type,
855 term_sub, term_component, space_split, term_dst))
857 yaz_log(LOG_DEBUG, "Relation >");
860 for (i = 0; term_component[i];)
865 string_rel_add_char (&term_tmp, term_component, &j);
870 string_rel_add_char (&term_tmp, term_component, &i);
878 if ((term_tmp - term_dict) > IT_MAX_WORD)
881 for (i = 0; term_component[i];)
882 string_rel_add_char (&term_tmp, term_component, &i);
889 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub,
890 term_component, space_split, term_dst))
892 yaz_log(LOG_DEBUG, "Relation >=");
895 for (i = 0; term_component[i];)
902 string_rel_add_char (&term_tmp, term_component, &j);
905 if (term_component[i+1])
909 string_rel_add_char (&term_tmp, term_component, &i);
913 string_rel_add_char (&term_tmp, term_component, &i);
920 if ((term_tmp - term_dict) > IT_MAX_WORD)
928 yaz_log(LOG_DEBUG, "Relation =");
929 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub,
930 term_component, space_split, term_dst))
932 strcat (term_tmp, "(");
933 strcat (term_tmp, term_component);
934 strcat (term_tmp, ")");
939 static int string_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
940 const char **term_sub,
941 oid_value attributeSet, NMEM stream,
942 struct grep_info *grep_info,
943 int reg_type, int complete_flag,
944 int num_bases, char **basenames,
945 char *term_dst, int xpath_use);
947 static RSET term_trunc (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
948 const char **term_sub,
949 oid_value attributeSet, NMEM stream,
950 struct grep_info *grep_info,
951 int reg_type, int complete_flag,
952 int num_bases, char **basenames,
954 const char *rank_type, int xpath_use)
957 grep_info->isam_p_indx = 0;
958 r = string_term (zh, zapt, term_sub, attributeSet, stream, grep_info,
959 reg_type, complete_flag, num_bases, basenames,
960 term_dst, xpath_use);
963 yaz_log(LOG_DEBUG, "term: %s", term_dst);
964 return rset_trunc (zh, grep_info->isam_p_buf,
965 grep_info->isam_p_indx, term_dst,
966 strlen(term_dst), rank_type, 1 /* preserve pos */,
970 static char *nmem_strdup_i(NMEM nmem, int v)
973 sprintf (val_str, "%d", v);
974 return nmem_strdup (nmem, val_str);
977 static int string_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
978 const char **term_sub,
979 oid_value attributeSet, NMEM stream,
980 struct grep_info *grep_info,
981 int reg_type, int complete_flag,
982 int num_bases, char **basenames,
983 char *term_dst, int xpath_use)
985 char term_dict[2*IT_MAX_WORD+4000];
988 int truncation_value;
991 const char *use_string = 0;
992 oid_value curAttributeSet = attributeSet;
994 struct rpn_char_map_info rcmi;
995 int space_split = complete_flag ? 0 : 1;
997 int bases_ok = 0; /* no of databases with OK attribute */
998 int errCode = 0; /* err code (if any is not OK) */
999 char *errString = 0; /* addinfo */
1001 rpn_char_map_prepare (zh->reg, reg_type, &rcmi);
1002 attr_init (&use, zapt, 1);
1003 use_value = attr_find_ex (&use, &curAttributeSet, &use_string);
1004 yaz_log(LOG_DEBUG, "string_term, use value %d", use_value);
1005 attr_init (&truncation, zapt, 5);
1006 truncation_value = attr_find (&truncation, NULL);
1007 yaz_log(LOG_DEBUG, "truncation value %d", truncation_value);
1009 if (use_value == -1) /* no attribute - assumy "any" */
1011 for (base_no = 0; base_no < num_bases; base_no++)
1014 int regex_range = 0;
1017 data1_local_attribute id_xpath_attr;
1018 data1_local_attribute *local_attr;
1019 int max_pos, prefix_len = 0;
1023 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
1025 zh->errCode = 109; /* Database unavailable */
1026 zh->errString = basenames[base_no];
1029 if (xpath_use > 0 && use_value == -2)
1031 use_value = xpath_use;
1032 attp.local_attributes = &id_xpath_attr;
1033 attp.attset_ordinal = VAL_IDXPATH;
1034 id_xpath_attr.next = 0;
1035 id_xpath_attr.local = use_value;
1037 else if (curAttributeSet == VAL_IDXPATH)
1039 attp.local_attributes = &id_xpath_attr;
1040 attp.attset_ordinal = VAL_IDXPATH;
1041 id_xpath_attr.next = 0;
1042 id_xpath_attr.local = use_value;
1046 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value,
1049 yaz_log(LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1050 curAttributeSet, use_value, r);
1053 /* set was found, but value wasn't defined */
1056 errString = nmem_strdup(stream, use_string);
1058 errString = nmem_strdup_i(stream, use_value);
1063 struct oident oident;
1065 oident.proto = PROTO_Z3950;
1066 oident.oclass = CLASS_ATTSET;
1067 oident.value = curAttributeSet;
1068 oid_ent_to_oid (&oident, oid);
1071 errString = nmem_strdup (stream, oident.desc);
1076 for (local_attr = attp.local_attributes; local_attr;
1077 local_attr = local_attr->next)
1083 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
1088 term_dict[prefix_len++] = '|';
1090 term_dict[prefix_len++] = '(';
1092 ord_len = key_SU_encode (ord, ord_buf);
1093 for (i = 0; i<ord_len; i++)
1095 term_dict[prefix_len++] = 1;
1096 term_dict[prefix_len++] = ord_buf[i];
1105 errString = nmem_strdup_i(stream, use_value);
1112 bases_ok++; /* this has OK attributes */
1115 term_dict[prefix_len++] = ')';
1116 term_dict[prefix_len++] = 1;
1117 term_dict[prefix_len++] = reg_type;
1118 yaz_log(LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1119 term_dict[prefix_len] = '\0';
1121 switch (truncation_value)
1123 case -1: /* not specified */
1124 case 100: /* do not truncate */
1125 if (!string_relation (zh, zapt, &termp, term_dict,
1127 reg_type, space_split, term_dst))
1130 case 1: /* right truncation */
1131 term_dict[j++] = '(';
1132 if (!term_100 (zh->reg->zebra_maps, reg_type,
1133 &termp, term_dict + j, space_split, term_dst))
1135 strcat (term_dict, ".*)");
1137 case 2: /* keft truncation */
1138 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1139 if (!term_100 (zh->reg->zebra_maps, reg_type,
1140 &termp, term_dict + j, space_split, term_dst))
1142 strcat (term_dict, ")");
1144 case 3: /* left&right truncation */
1145 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1146 if (!term_100 (zh->reg->zebra_maps, reg_type,
1147 &termp, term_dict + j, space_split, term_dst))
1149 strcat (term_dict, ".*)");
1151 case 101: /* process # in term */
1152 term_dict[j++] = '(';
1153 if (!term_101 (zh->reg->zebra_maps, reg_type,
1154 &termp, term_dict + j, space_split, term_dst))
1156 strcat (term_dict, ")");
1158 case 102: /* Regexp-1 */
1159 term_dict[j++] = '(';
1160 if (!term_102 (zh->reg->zebra_maps, reg_type,
1161 &termp, term_dict + j, space_split, term_dst))
1163 strcat (term_dict, ")");
1165 case 103: /* Regexp-2 */
1167 term_dict[j++] = '(';
1169 if (!term_103 (zh->reg->zebra_maps, reg_type,
1170 &termp, term_dict + j, ®ex_range,
1171 space_split, term_dst))
1173 strcat (term_dict, ")");
1175 case 104: /* process # and ! in term */
1176 term_dict[j++] = '(';
1177 if (!term_104 (zh->reg->zebra_maps, reg_type,
1178 &termp, term_dict + j, space_split, term_dst))
1180 strcat (term_dict, ")");
1182 case 105: /* process * and ! in term */
1183 term_dict[j++] = '(';
1184 if (!term_105 (zh->reg->zebra_maps, reg_type,
1185 &termp, term_dict + j, space_split, term_dst, 1))
1187 strcat (term_dict, ")");
1189 case 106: /* process * and ! in term */
1190 term_dict[j++] = '(';
1191 if (!term_105 (zh->reg->zebra_maps, reg_type,
1192 &termp, term_dict + j, space_split, term_dst, 0))
1194 strcat (term_dict, ")");
1198 zh->errString = nmem_strdup_i(stream, truncation_value);
1203 yaz_log(LOG_DEBUG, "dict_lookup_grep: %s", term_dict+prefix_len);
1204 r = dict_lookup_grep (zh->reg->dict, term_dict, regex_range,
1205 grep_info, &max_pos, init_pos,
1208 yaz_log(LOG_WARN, "dict_lookup_grep fail %d", r);
1213 zh->errCode = errCode;
1214 zh->errString = errString;
1218 yaz_log(LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1223 /* convert APT search term to UTF8 */
1224 static int zapt_term_to_utf8 (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1228 Z_Term *term = zapt->term;
1230 switch (term->which)
1232 case Z_Term_general:
1233 if (zh->iconv_to_utf8 != 0)
1235 char *inbuf = term->u.general->buf;
1236 size_t inleft = term->u.general->len;
1237 char *outbuf = termz;
1238 size_t outleft = IT_MAX_WORD-1;
1241 ret = yaz_iconv(zh->iconv_to_utf8, &inbuf, &inleft,
1243 if (ret == (size_t)(-1))
1245 ret = yaz_iconv(zh->iconv_to_utf8, 0, 0, 0, 0);
1253 sizez = term->u.general->len;
1254 if (sizez > IT_MAX_WORD-1)
1255 sizez = IT_MAX_WORD-1;
1256 memcpy (termz, term->u.general->buf, sizez);
1257 termz[sizez] = '\0';
1260 case Z_Term_characterString:
1261 sizez = strlen(term->u.characterString);
1262 if (sizez > IT_MAX_WORD-1)
1263 sizez = IT_MAX_WORD-1;
1264 memcpy (termz, term->u.characterString, sizez);
1265 termz[sizez] = '\0';
1274 /* convert APT SCAN term to internal cmap */
1275 static int trans_scan_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1276 char *termz, int reg_type)
1278 char termz0[IT_MAX_WORD];
1280 if (zapt_term_to_utf8(zh, zapt, termz0))
1281 return -1; /* error */
1285 const char *cp = (const char *) termz0;
1286 const char *cp_end = cp + strlen(cp);
1289 const char *space_map = NULL;
1292 while ((len = (cp_end - cp)) > 0)
1294 map = zebra_maps_input (zh->reg->zebra_maps, reg_type, &cp, len, 0);
1295 if (**map == *CHR_SPACE)
1300 for (src = space_map; *src; src++)
1303 for (src = *map; *src; src++)
1312 char *normalize_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1313 const char *termz, NMEM stream, unsigned reg_id)
1316 AttrType truncation;
1317 int truncation_value;
1320 attr_init (&truncation, zapt, 5);
1321 truncation_value = attr_find (&truncation, NULL);
1323 switch (truncation_value)
1343 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, ex_list,
1344 termz, strlen(termz));
1346 return nmem_strdup(stream, termz);
1349 char *buf = (char*) nmem_malloc (stream, wrbuf_len(wrbuf)+1);
1350 memcpy (buf, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1351 buf[wrbuf_len(wrbuf)] = '\0';
1356 static void grep_info_delete (struct grep_info *grep_info)
1359 xfree(grep_info->term_no);
1361 xfree (grep_info->isam_p_buf);
1364 static int grep_info_prepare (ZebraHandle zh,
1365 Z_AttributesPlusTerm *zapt,
1366 struct grep_info *grep_info,
1371 int termset_value_numeric;
1372 const char *termset_value_string;
1375 grep_info->term_no = 0;
1377 grep_info->isam_p_size = 0;
1378 grep_info->isam_p_buf = NULL;
1380 grep_info->reg_type = reg_type;
1381 grep_info->termset = 0;
1385 attr_init (&termset, zapt, 8);
1386 termset_value_numeric =
1387 attr_find_ex (&termset, NULL, &termset_value_string);
1388 if (termset_value_numeric != -1)
1391 const char *termset_name = 0;
1392 if (termset_value_numeric != -2)
1394 sprintf (resname, "%d", termset_value_numeric);
1395 termset_name = resname;
1398 termset_name = termset_value_string;
1399 yaz_log(LOG_LOG, "creating termset set %s", termset_name);
1400 grep_info->termset = resultSetAdd (zh, termset_name, 1);
1401 if (!grep_info->termset)
1404 zh->errString = nmem_strdup (stream, termset_name);
1412 static RSET rpn_search_APT_phrase (ZebraHandle zh,
1413 Z_AttributesPlusTerm *zapt,
1414 const char *termz_org,
1415 oid_value attributeSet,
1417 int reg_type, int complete_flag,
1418 const char *rank_type, int xpath_use,
1419 int num_bases, char **basenames)
1421 char term_dst[IT_MAX_WORD+1];
1422 RSET rset[60], result;
1424 struct grep_info grep_info;
1425 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1426 const char *termp = termz;
1429 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1433 yaz_log(LOG_DEBUG, "APT_phrase termp=%s", termp);
1434 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1436 reg_type, complete_flag,
1437 num_bases, basenames,
1438 term_dst, rank_type,
1442 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1445 grep_info_delete (&grep_info);
1448 rset_null_parms parms;
1450 parms.rset_term = rset_term_create (termz, -1, rank_type,
1452 return rset_create (rset_kind_null, &parms);
1454 else if (rset_no == 1)
1458 /* new / old prox */
1459 rset_prox_parms parms;
1462 parms.rset_no = rset_no;
1464 parms.exclusion = 0;
1467 parms.key_size = sizeof(struct it_key);
1468 parms.cmp = key_compare_it;
1469 parms.getseq = key_get_seq;
1470 parms.log_item = key_logdump_txt;
1471 result = rset_create(rset_kind_prox, &parms);
1476 static RSET rpn_search_APT_or_list (ZebraHandle zh,
1477 Z_AttributesPlusTerm *zapt,
1478 const char *termz_org,
1479 oid_value attributeSet,
1481 int reg_type, int complete_flag,
1482 const char *rank_type,
1484 int num_bases, char **basenames)
1486 char term_dst[IT_MAX_WORD+1];
1487 RSET rset[60], result;
1489 struct grep_info grep_info;
1490 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1491 const char *termp = termz;
1493 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1497 yaz_log(LOG_DEBUG, "APT_or_list termp=%s", termp);
1498 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1500 reg_type, complete_flag,
1501 num_bases, basenames,
1502 term_dst, rank_type,
1506 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1509 grep_info_delete (&grep_info);
1512 rset_null_parms parms;
1514 parms.rset_term = rset_term_create (termz, -1, rank_type,
1516 return rset_create (rset_kind_null, &parms);
1519 for (i = 1; i<rset_no; i++)
1521 rset_bool_parms bool_parms;
1523 bool_parms.rset_l = result;
1524 bool_parms.rset_r = rset[i];
1525 bool_parms.key_size = sizeof(struct it_key);
1526 bool_parms.cmp = key_compare_it;
1527 bool_parms.log_item = key_logdump_txt;
1528 result = rset_create (rset_kind_or, &bool_parms);
1533 static RSET rpn_search_APT_and_list (ZebraHandle zh,
1534 Z_AttributesPlusTerm *zapt,
1535 const char *termz_org,
1536 oid_value attributeSet,
1538 int reg_type, int complete_flag,
1539 const char *rank_type,
1541 int num_bases, char **basenames)
1543 char term_dst[IT_MAX_WORD+1];
1544 RSET rset[60], result;
1546 struct grep_info grep_info;
1547 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1548 const char *termp = termz;
1550 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1554 yaz_log(LOG_DEBUG, "APT_and_list termp=%s", termp);
1555 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1557 reg_type, complete_flag,
1558 num_bases, basenames,
1559 term_dst, rank_type,
1563 assert (rset[rset_no]);
1564 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1567 grep_info_delete (&grep_info);
1570 rset_null_parms parms;
1572 parms.rset_term = rset_term_create (termz, -1, rank_type,
1574 return rset_create (rset_kind_null, &parms);
1577 for (i = 1; i<rset_no; i++)
1579 rset_bool_parms bool_parms;
1581 bool_parms.rset_l = result;
1582 bool_parms.rset_r = rset[i];
1583 bool_parms.key_size = sizeof(struct it_key);
1584 bool_parms.cmp = key_compare_it;
1585 bool_parms.log_item = key_logdump_txt;
1586 result = rset_create (rset_kind_and_forward, &bool_parms);
1591 static int numeric_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1592 const char **term_sub,
1594 oid_value attributeSet,
1595 struct grep_info *grep_info,
1604 char *term_tmp = term_dict + strlen(term_dict);
1606 attr_init (&relation, zapt, 2);
1607 relation_value = attr_find (&relation, NULL);
1609 yaz_log(LOG_DEBUG, "numeric relation value=%d", relation_value);
1611 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub, term_tmp, 1,
1614 term_value = atoi (term_tmp);
1615 switch (relation_value)
1618 yaz_log(LOG_DEBUG, "Relation <");
1619 gen_regular_rel (term_tmp, term_value-1, 1);
1622 yaz_log(LOG_DEBUG, "Relation <=");
1623 gen_regular_rel (term_tmp, term_value, 1);
1626 yaz_log(LOG_DEBUG, "Relation >=");
1627 gen_regular_rel (term_tmp, term_value, 0);
1630 yaz_log(LOG_DEBUG, "Relation >");
1631 gen_regular_rel (term_tmp, term_value+1, 0);
1635 yaz_log(LOG_DEBUG, "Relation =");
1636 sprintf (term_tmp, "(0*%d)", term_value);
1638 yaz_log(LOG_DEBUG, "dict_lookup_grep: %s", term_tmp);
1639 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info, max_pos,
1642 yaz_log(LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1643 yaz_log(LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1647 static int numeric_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1648 const char **term_sub,
1649 oid_value attributeSet, struct grep_info *grep_info,
1650 int reg_type, int complete_flag,
1651 int num_bases, char **basenames,
1652 char *term_dst, int xpath_use, NMEM stream)
1654 char term_dict[2*IT_MAX_WORD+2];
1658 const char *use_string = 0;
1659 oid_value curAttributeSet = attributeSet;
1661 struct rpn_char_map_info rcmi;
1663 int bases_ok = 0; /* no of databases with OK attribute */
1664 int errCode = 0; /* err code (if any is not OK) */
1665 char *errString = 0; /* addinfo */
1667 rpn_char_map_prepare (zh->reg, reg_type, &rcmi);
1668 attr_init (&use, zapt, 1);
1669 use_value = attr_find_ex (&use, &curAttributeSet, &use_string);
1671 if (use_value == -1)
1674 for (base_no = 0; base_no < num_bases; base_no++)
1677 data1_local_attribute id_xpath_attr;
1678 data1_local_attribute *local_attr;
1679 int max_pos, prefix_len = 0;
1682 if (use_value == -2) /* string attribute (assume IDXPATH/any) */
1684 use_value = xpath_use;
1685 attp.local_attributes = &id_xpath_attr;
1686 attp.attset_ordinal = VAL_IDXPATH;
1687 id_xpath_attr.next = 0;
1688 id_xpath_attr.local = use_value;
1690 else if (curAttributeSet == VAL_IDXPATH)
1692 attp.local_attributes = &id_xpath_attr;
1693 attp.attset_ordinal = VAL_IDXPATH;
1694 id_xpath_attr.next = 0;
1695 id_xpath_attr.local = use_value;
1699 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value,
1702 yaz_log(LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1703 curAttributeSet, use_value, r);
1706 errString = nmem_strdup_i (stream, use_value);
1714 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
1716 zh->errCode = 109; /* Database unavailable */
1717 zh->errString = basenames[base_no];
1720 for (local_attr = attp.local_attributes; local_attr;
1721 local_attr = local_attr->next)
1727 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
1732 term_dict[prefix_len++] = '|';
1734 term_dict[prefix_len++] = '(';
1736 ord_len = key_SU_encode (ord, ord_buf);
1737 for (i = 0; i<ord_len; i++)
1739 term_dict[prefix_len++] = 1;
1740 term_dict[prefix_len++] = ord_buf[i];
1746 sprintf (val_str, "%d", use_value);
1748 errString = nmem_strdup (stream, val_str);
1752 term_dict[prefix_len++] = ')';
1753 term_dict[prefix_len++] = 1;
1754 term_dict[prefix_len++] = reg_type;
1755 yaz_log(LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1756 term_dict[prefix_len] = '\0';
1757 if (!numeric_relation (zh, zapt, &termp, term_dict,
1758 attributeSet, grep_info, &max_pos, reg_type,
1764 zh->errCode = errCode;
1765 zh->errString = errString;
1769 yaz_log(LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1773 static RSET rpn_search_APT_numeric (ZebraHandle zh,
1774 Z_AttributesPlusTerm *zapt,
1776 oid_value attributeSet,
1778 int reg_type, int complete_flag,
1779 const char *rank_type, int xpath_use,
1780 int num_bases, char **basenames)
1782 char term_dst[IT_MAX_WORD+1];
1783 const char *termp = termz;
1784 RSET rset[60], result;
1785 int i, r, rset_no = 0;
1786 struct grep_info grep_info;
1788 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1792 yaz_log(LOG_DEBUG, "APT_numeric termp=%s", termp);
1793 grep_info.isam_p_indx = 0;
1794 r = numeric_term (zh, zapt, &termp, attributeSet, &grep_info,
1795 reg_type, complete_flag, num_bases, basenames,
1796 term_dst, xpath_use,
1800 yaz_log(LOG_DEBUG, "term: %s", term_dst);
1801 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1802 grep_info.isam_p_indx, term_dst,
1803 strlen(term_dst), rank_type,
1804 0 /* preserve position */,
1806 assert (rset[rset_no]);
1807 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1810 grep_info_delete (&grep_info);
1813 rset_null_parms parms;
1815 parms.rset_term = rset_term_create (term_dst, -1, rank_type,
1817 return rset_create (rset_kind_null, &parms);
1820 for (i = 1; i<rset_no; i++)
1822 rset_bool_parms bool_parms;
1824 bool_parms.rset_l = result;
1825 bool_parms.rset_r = rset[i];
1826 bool_parms.key_size = sizeof(struct it_key);
1827 bool_parms.cmp = key_compare_it;
1828 bool_parms.log_item = key_logdump_txt;
1829 result = rset_create (rset_kind_and_forward, &bool_parms);
1834 static RSET rpn_search_APT_local (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1836 oid_value attributeSet,
1838 const char *rank_type)
1843 rset_temp_parms parms;
1845 parms.rset_term = rset_term_create (termz, -1, rank_type,
1847 parms.cmp = key_compare_it;
1848 parms.key_size = sizeof (struct it_key);
1849 parms.temp_path = res_get (zh->res, "setTmpDir");
1850 result = rset_create (rset_kind_temp, &parms);
1851 rsfd = rset_open (result, RSETF_WRITE);
1853 key.sysno = atoi (termz);
1857 rset_write (result, rsfd, &key);
1858 rset_close (result, rsfd);
1862 static RSET rpn_sort_spec (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1863 oid_value attributeSet, NMEM stream,
1864 Z_SortKeySpecList *sort_sequence,
1865 const char *rank_type)
1867 rset_null_parms parms;
1869 int sort_relation_value;
1870 AttrType sort_relation_type;
1875 Z_AttributeElement *ae;
1880 attr_init (&sort_relation_type, zapt, 7);
1881 sort_relation_value = attr_find (&sort_relation_type, &attributeSet);
1883 attr_init (&use_type, zapt, 1);
1884 use_value = attr_find (&use_type, &attributeSet);
1886 if (!sort_sequence->specs)
1888 sort_sequence->num_specs = 10;
1889 sort_sequence->specs = (Z_SortKeySpec **)
1890 nmem_malloc (stream, sort_sequence->num_specs *
1891 sizeof(*sort_sequence->specs));
1892 for (i = 0; i<sort_sequence->num_specs; i++)
1893 sort_sequence->specs[i] = 0;
1895 if (zapt->term->which != Z_Term_general)
1898 i = atoi_n ((char *) zapt->term->u.general->buf,
1899 zapt->term->u.general->len);
1900 if (i >= sort_sequence->num_specs)
1902 sprintf (termz, "%d", i);
1904 oe.proto = PROTO_Z3950;
1905 oe.oclass = CLASS_ATTSET;
1906 oe.value = attributeSet;
1907 if (!oid_ent_to_oid (&oe, oid))
1910 sks = (Z_SortKeySpec *) nmem_malloc (stream, sizeof(*sks));
1911 sks->sortElement = (Z_SortElement *)
1912 nmem_malloc (stream, sizeof(*sks->sortElement));
1913 sks->sortElement->which = Z_SortElement_generic;
1914 sk = sks->sortElement->u.generic = (Z_SortKey *)
1915 nmem_malloc (stream, sizeof(*sk));
1916 sk->which = Z_SortKey_sortAttributes;
1917 sk->u.sortAttributes = (Z_SortAttributes *)
1918 nmem_malloc (stream, sizeof(*sk->u.sortAttributes));
1920 sk->u.sortAttributes->id = oid;
1921 sk->u.sortAttributes->list = (Z_AttributeList *)
1922 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list));
1923 sk->u.sortAttributes->list->num_attributes = 1;
1924 sk->u.sortAttributes->list->attributes = (Z_AttributeElement **)
1925 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list->attributes));
1926 ae = *sk->u.sortAttributes->list->attributes = (Z_AttributeElement *)
1927 nmem_malloc (stream, sizeof(**sk->u.sortAttributes->list->attributes));
1928 ae->attributeSet = 0;
1929 ae->attributeType = (int *)
1930 nmem_malloc (stream, sizeof(*ae->attributeType));
1931 *ae->attributeType = 1;
1932 ae->which = Z_AttributeValue_numeric;
1933 ae->value.numeric = (int *)
1934 nmem_malloc (stream, sizeof(*ae->value.numeric));
1935 *ae->value.numeric = use_value;
1937 sks->sortRelation = (int *)
1938 nmem_malloc (stream, sizeof(*sks->sortRelation));
1939 if (sort_relation_value == 1)
1940 *sks->sortRelation = Z_SortKeySpec_ascending;
1941 else if (sort_relation_value == 2)
1942 *sks->sortRelation = Z_SortKeySpec_descending;
1944 *sks->sortRelation = Z_SortKeySpec_ascending;
1946 sks->caseSensitivity = (int *)
1947 nmem_malloc (stream, sizeof(*sks->caseSensitivity));
1948 *sks->caseSensitivity = 0;
1950 sks->which = Z_SortKeySpec_null;
1951 sks->u.null = odr_nullval ();
1952 sort_sequence->specs[i] = sks;
1954 parms.rset_term = rset_term_create (termz, -1, rank_type,
1956 return rset_create (rset_kind_null, &parms);
1960 static int parse_xpath(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1961 oid_value attributeSet,
1962 struct xpath_location_step *xpath, int max, NMEM mem)
1964 oid_value curAttributeSet = attributeSet;
1966 const char *use_string = 0;
1968 attr_init (&use, zapt, 1);
1969 attr_find_ex (&use, &curAttributeSet, &use_string);
1971 if (!use_string || *use_string != '/')
1974 return zebra_parse_xpath_str(use_string, xpath, max, mem);
1979 static RSET xpath_trunc(ZebraHandle zh, NMEM stream,
1980 int reg_type, const char *term, int use,
1981 oid_value curAttributeSet)
1984 struct grep_info grep_info;
1985 char term_dict[2048];
1988 int ord = zebraExplain_lookupSU (zh->reg->zei, curAttributeSet, use);
1989 int ord_len, i, r, max_pos;
1990 int term_type = Z_Term_characterString;
1991 const char *flags = "void";
1993 if (grep_info_prepare (zh, 0 /* zapt */, &grep_info, '0', stream))
1995 rset_null_parms parms;
1997 parms.rset_term = rset_term_create (term, strlen(term),
1999 parms.rset_term->nn = 0;
2000 return rset_create (rset_kind_null, &parms);
2005 rset_null_parms parms;
2007 parms.rset_term = rset_term_create (term, strlen(term),
2009 parms.rset_term->nn = 0;
2010 return rset_create (rset_kind_null, &parms);
2013 term_dict[prefix_len++] = '|';
2015 term_dict[prefix_len++] = '(';
2017 ord_len = key_SU_encode (ord, ord_buf);
2018 for (i = 0; i<ord_len; i++)
2020 term_dict[prefix_len++] = 1;
2021 term_dict[prefix_len++] = ord_buf[i];
2023 term_dict[prefix_len++] = ')';
2024 term_dict[prefix_len++] = 1;
2025 term_dict[prefix_len++] = reg_type;
2027 strcpy (term_dict+prefix_len, term);
2029 grep_info.isam_p_indx = 0;
2030 r = dict_lookup_grep (zh->reg->dict, term_dict, 0,
2031 &grep_info, &max_pos, 0, grep_handle);
2032 yaz_log (LOG_LOG, "%s %d positions", term, grep_info.isam_p_indx);
2033 rset = rset_trunc (zh, grep_info.isam_p_buf,
2034 grep_info.isam_p_indx, term, strlen(term),
2035 flags, 1, term_type);
2036 grep_info_delete (&grep_info);
2040 static RSET rpn_search_xpath (ZebraHandle zh,
2041 oid_value attributeSet,
2042 int num_bases, char **basenames,
2043 NMEM stream, const char *rank_type, RSET rset,
2044 int xpath_len, struct xpath_location_step *xpath)
2046 oid_value curAttributeSet = attributeSet;
2053 yaz_log (LOG_DEBUG, "len=%d", xpath_len);
2054 for (i = 0; i<xpath_len; i++)
2056 yaz_log (LOG_DEBUG, "XPATH %d %s", i, xpath[i].part);
2059 curAttributeSet = VAL_IDXPATH;
2069 a[@attr=value]/b[@other=othervalue]
2071 /e/@a val range(e/,range(@a,freetext(w,1015,val),@a),e/)
2072 /a/b val range(b/a/,freetext(w,1016,val),b/a/)
2073 /a/b/@c val range(b/a/,range(@c,freetext(w,1016,val),@c),b/a/)
2074 /a/b[@c=y] val range(b/a/,freetext(w,1016,val),b/a/,@c=y)
2075 /a[@c=y]/b val range(a/,range(b/a/,freetext(w,1016,val),b/a/),a/,@c=y)
2076 /a[@c=x]/b[@c=y] range(a/,range(b/a/,freetext(w,1016,val),b/a/,@c=y),a/,@c=x)
2080 dict_grep_cmap (zh->reg->dict, 0, 0);
2082 for (base_no = 0; base_no < num_bases; base_no++)
2084 int level = xpath_len;
2087 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
2089 zh->errCode = 109; /* Database unavailable */
2090 zh->errString = basenames[base_no];
2093 while (--level >= 0)
2095 char xpath_rev[128];
2097 rset_between_parms parms;
2098 RSET rset_start_tag = 0, rset_end_tag = 0, rset_attr = 0;
2102 for (i = level; i >= 1; --i)
2104 const char *cp = xpath[i].part;
2110 memcpy (xpath_rev + len, "[^/]*", 5);
2113 else if (*cp == ' ')
2116 xpath_rev[len++] = 1;
2117 xpath_rev[len++] = ' ';
2121 xpath_rev[len++] = *cp;
2122 xpath_rev[len++] = '/';
2124 else if (i == 1) /* // case */
2126 xpath_rev[len++] = '.';
2127 xpath_rev[len++] = '*';
2132 if (xpath[level].predicate &&
2133 xpath[level].predicate->which == XPATH_PREDICATE_RELATION &&
2134 xpath[level].predicate->u.relation.name[0])
2136 WRBUF wbuf = wrbuf_alloc();
2137 wrbuf_puts(wbuf, xpath[level].predicate->u.relation.name+1);
2138 if (xpath[level].predicate->u.relation.value)
2140 const char *cp = xpath[level].predicate->u.relation.value;
2141 wrbuf_putc(wbuf, '=');
2145 if (strchr(REGEX_CHARS, *cp))
2146 wrbuf_putc(wbuf, '\\');
2147 wrbuf_putc(wbuf, *cp);
2151 wrbuf_puts(wbuf, "");
2152 rset_attr = xpath_trunc (
2153 zh, stream, '0', wrbuf_buf(wbuf), 3, curAttributeSet);
2154 wrbuf_free(wbuf, 1);
2161 yaz_log (LOG_DEBUG, "xpath_rev (%d) = %s", level, xpath_rev);
2162 if (strlen(xpath_rev))
2164 rset_start_tag = xpath_trunc(zh, stream,
2165 '0', xpath_rev, 1, curAttributeSet);
2167 rset_end_tag = xpath_trunc(zh, stream,
2168 '0', xpath_rev, 2, curAttributeSet);
2170 parms.key_size = sizeof(struct it_key);
2171 parms.cmp = key_compare_it;
2172 parms.rset_l = rset_start_tag;
2173 parms.rset_m = rset;
2174 parms.rset_r = rset_end_tag;
2175 parms.rset_attr = rset_attr;
2176 parms.printer = key_print_it;
2177 rset = rset_create (rset_kind_between, &parms);
2188 static RSET rpn_search_APT (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2189 oid_value attributeSet, NMEM stream,
2190 Z_SortKeySpecList *sort_sequence,
2191 int num_bases, char **basenames)
2194 char *search_type = NULL;
2195 char rank_type[128];
2198 char termz[IT_MAX_WORD+1];
2202 struct xpath_location_step xpath[10];
2204 zebra_maps_attr (zh->reg->zebra_maps, zapt, ®_id, &search_type,
2205 rank_type, &complete_flag, &sort_flag);
2207 yaz_log(LOG_DEBUG, "reg_id=%c", reg_id);
2208 yaz_log(LOG_DEBUG, "complete_flag=%d", complete_flag);
2209 yaz_log(LOG_DEBUG, "search_type=%s", search_type);
2210 yaz_log(LOG_DEBUG, "rank_type=%s", rank_type);
2212 if (zapt_term_to_utf8(zh, zapt, termz))
2216 return rpn_sort_spec (zh, zapt, attributeSet, stream, sort_sequence,
2218 xpath_len = parse_xpath(zh, zapt, attributeSet, xpath, 10, stream);
2222 if (xpath[xpath_len-1].part[0] == '@')
2226 if (!strcmp (search_type, "phrase"))
2228 rset = rpn_search_APT_phrase (zh, zapt, termz, attributeSet, stream,
2229 reg_id, complete_flag, rank_type,
2231 num_bases, basenames);
2233 else if (!strcmp (search_type, "and-list"))
2235 rset = rpn_search_APT_and_list (zh, zapt, termz, attributeSet, stream,
2236 reg_id, complete_flag, rank_type,
2238 num_bases, basenames);
2240 else if (!strcmp (search_type, "or-list"))
2242 rset = rpn_search_APT_or_list (zh, zapt, termz, attributeSet, stream,
2243 reg_id, complete_flag, rank_type,
2245 num_bases, basenames);
2247 else if (!strcmp (search_type, "local"))
2249 rset = rpn_search_APT_local (zh, zapt, termz, attributeSet, stream,
2252 else if (!strcmp (search_type, "numeric"))
2254 rset = rpn_search_APT_numeric (zh, zapt, termz, attributeSet, stream,
2255 reg_id, complete_flag, rank_type,
2257 num_bases, basenames);
2259 else if (!strcmp (search_type, "always"))
2265 return rpn_search_xpath (zh, attributeSet, num_bases, basenames,
2266 stream, rank_type, rset, xpath_len, xpath);
2269 static RSET rpn_search_structure (ZebraHandle zh, Z_RPNStructure *zs,
2270 oid_value attributeSet, NMEM stream,
2271 Z_SortKeySpecList *sort_sequence,
2272 int num_bases, char **basenames)
2275 if (zs->which == Z_RPNStructure_complex)
2277 Z_Operator *zop = zs->u.complex->roperator;
2278 rset_bool_parms bool_parms;
2280 bool_parms.rset_l = rpn_search_structure (zh, zs->u.complex->s1,
2281 attributeSet, stream,
2283 num_bases, basenames);
2284 if (bool_parms.rset_l == NULL)
2286 bool_parms.rset_r = rpn_search_structure (zh, zs->u.complex->s2,
2287 attributeSet, stream,
2289 num_bases, basenames);
2290 if (bool_parms.rset_r == NULL)
2292 rset_delete (bool_parms.rset_l);
2295 bool_parms.key_size = sizeof(struct it_key);
2296 bool_parms.cmp = key_compare_it;
2297 bool_parms.log_item = key_logdump_txt;
2301 case Z_Operator_and:
2302 if (res_get(zh->res, "rsetforward"))
2303 r = rset_create (rset_kind_and_forward, &bool_parms);
2305 r = rset_create (rset_kind_and, &bool_parms);
2308 r = rset_create (rset_kind_or, &bool_parms);
2310 case Z_Operator_and_not:
2311 r = rset_create (rset_kind_not, &bool_parms);
2313 case Z_Operator_prox:
2314 if (zop->u.prox->which != Z_ProximityOperator_known)
2319 if (*zop->u.prox->u.known != Z_ProxUnit_word)
2321 char *val = (char *) nmem_malloc (stream, 16);
2323 zh->errString = val;
2324 sprintf (val, "%d", *zop->u.prox->u.known);
2329 /* new / old prox */
2330 rset_prox_parms parms;
2333 twosets[0] = bool_parms.rset_l;
2334 twosets[1] = bool_parms.rset_r;
2335 parms.rset = twosets;
2337 parms.ordered = *zop->u.prox->ordered;
2338 parms.exclusion = (!zop->u.prox->exclusion ? 0 :
2339 *zop->u.prox->exclusion);
2340 parms.relation = *zop->u.prox->relationType;
2341 parms.distance = *zop->u.prox->distance;
2342 parms.key_size = sizeof(struct it_key);
2343 parms.cmp = key_compare_it;
2344 parms.getseq = key_get_seq;
2345 parms.log_item = key_logdump_txt;
2346 r = rset_create(rset_kind_prox, &parms);
2354 else if (zs->which == Z_RPNStructure_simple)
2356 if (zs->u.simple->which == Z_Operand_APT)
2358 yaz_log(LOG_DEBUG, "rpn_search_APT");
2359 r = rpn_search_APT (zh, zs->u.simple->u.attributesPlusTerm,
2360 attributeSet, stream, sort_sequence,
2361 num_bases, basenames);
2363 else if (zs->u.simple->which == Z_Operand_resultSetId)
2365 yaz_log(LOG_DEBUG, "rpn_search_ref");
2366 r = resultSetRef (zh, zs->u.simple->u.resultSetId);
2369 r = rset_create (rset_kind_null, NULL);
2372 nmem_strdup (stream, zs->u.simple->u.resultSetId);
2393 RSET rpn_search (ZebraHandle zh, NMEM nmem,
2394 Z_RPNQuery *rpn, int num_bases, char **basenames,
2395 const char *setname,
2400 oid_value attributeSet;
2401 Z_SortKeySpecList *sort_sequence;
2405 zh->errString = NULL;
2408 sort_sequence = (Z_SortKeySpecList *)
2409 nmem_malloc (nmem, sizeof(*sort_sequence));
2410 sort_sequence->num_specs = 10;
2411 sort_sequence->specs = (Z_SortKeySpec **)
2412 nmem_malloc (nmem, sort_sequence->num_specs *
2413 sizeof(*sort_sequence->specs));
2414 for (i = 0; i<sort_sequence->num_specs; i++)
2415 sort_sequence->specs[i] = 0;
2417 attrset = oid_getentbyoid (rpn->attributeSetId);
2418 attributeSet = attrset->value;
2419 rset = rpn_search_structure (zh, rpn->RPNStructure, attributeSet,
2420 nmem, sort_sequence, num_bases, basenames);
2425 yaz_log(LOG_DEBUG, "search error: %d", zh->errCode);
2427 for (i = 0; sort_sequence->specs[i]; i++)
2429 sort_sequence->num_specs = i;
2431 resultSetRank (zh, sset, rset);
2434 yaz_log(LOG_DEBUG, "resultSetSortSingle in rpn_search");
2435 resultSetSortSingle (zh, nmem, sset, rset,
2436 sort_sequence, &sort_status);
2439 yaz_log(LOG_DEBUG, "resultSetSortSingle status = %d", zh->errCode);
2445 struct scan_info_entry {
2451 struct scan_info_entry *list;
2457 static int scan_handle (char *name, const char *info, int pos, void *client)
2459 int len_prefix, idx;
2460 struct scan_info *scan_info = (struct scan_info *) client;
2462 len_prefix = strlen(scan_info->prefix);
2463 if (memcmp (name, scan_info->prefix, len_prefix))
2465 if (pos > 0) idx = scan_info->after - pos + scan_info->before;
2468 scan_info->list[idx].term = (char *)
2469 odr_malloc (scan_info->odr, strlen(name + len_prefix)+1);
2470 strcpy (scan_info->list[idx].term, name + len_prefix);
2471 assert (*info == sizeof(ISAMS_P));
2472 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAMS_P));
2476 static void scan_term_untrans (ZebraHandle zh, NMEM stream, int reg_type,
2477 char **dst, const char *src)
2479 char term_src[IT_MAX_WORD];
2480 char term_dst[IT_MAX_WORD];
2482 term_untrans (zh, reg_type, term_src, src);
2484 if (zh->iconv_from_utf8 != 0)
2487 char *inbuf = term_src;
2488 size_t inleft = strlen(term_src);
2489 char *outbuf = term_dst;
2490 size_t outleft = sizeof(term_dst)-1;
2493 ret = yaz_iconv (zh->iconv_from_utf8, &inbuf, &inleft,
2495 if (ret == (size_t)(-1))
2498 len = outbuf - term_dst;
2499 *dst = nmem_malloc (stream, len + 1);
2501 memcpy (*dst, term_dst, len);
2505 *dst = nmem_strdup (stream, term_src);
2508 static void count_set (RSET r, int *count)
2516 yaz_log(LOG_DEBUG, "count_set");
2519 rfd = rset_open (r, RSETF_READ);
2520 while (rset_read (r, rfd, &key, &term_index))
2522 if (key.sysno != psysno)
2529 rset_close (r, rfd);
2530 yaz_log(LOG_DEBUG, "%d keys, %d records", kno, *count);
2533 void rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
2534 oid_value attributeset,
2535 int num_bases, char **basenames,
2536 int *position, int *num_entries, ZebraScanEntry **list,
2537 int *is_partial, RSET limit_set, int return_zero)
2540 int pos = *position;
2541 int num = *num_entries;
2545 char termz[IT_MAX_WORD+20];
2548 const char *use_string = 0;
2549 struct scan_info *scan_info_array;
2550 ZebraScanEntry *glist;
2551 int ords[32], ord_no = 0;
2554 int bases_ok = 0; /* no of databases with OK attribute */
2555 int errCode = 0; /* err code (if any is not OK) */
2556 char *errString = 0; /* addinfo */
2559 char *search_type = NULL;
2560 char rank_type[128];
2566 if (attributeset == VAL_NONE)
2567 attributeset = VAL_BIB1;
2572 int termset_value_numeric;
2573 const char *termset_value_string;
2574 attr_init (&termset, zapt, 8);
2575 termset_value_numeric =
2576 attr_find_ex (&termset, NULL, &termset_value_string);
2577 if (termset_value_numeric != -1)
2580 const char *termset_name = 0;
2582 if (termset_value_numeric != -2)
2585 sprintf (resname, "%d", termset_value_numeric);
2586 termset_name = resname;
2589 termset_name = termset_value_string;
2591 limit_set = resultSetRef (zh, termset_name);
2595 yaz_log (LOG_DEBUG, "position = %d, num = %d set=%d",
2596 pos, num, attributeset);
2598 attr_init (&use, zapt, 1);
2599 use_value = attr_find_ex (&use, &attributeset, &use_string);
2601 if (zebra_maps_attr (zh->reg->zebra_maps, zapt, ®_id, &search_type,
2602 rank_type, &complete_flag, &sort_flag))
2608 yaz_log (LOG_DEBUG, "use_value = %d", use_value);
2610 if (use_value == -1)
2612 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
2616 data1_local_attribute *local_attr;
2618 if ((r=att_getentbyatt (zh, &attp, attributeset, use_value,
2621 yaz_log(LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
2622 attributeset, use_value);
2626 sprintf (val_str, "%d", use_value);
2628 errString = odr_strdup (stream, val_str);
2634 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
2636 zh->errString = basenames[base_no];
2637 zh->errCode = 109; /* Database unavailable */
2642 for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
2643 local_attr = local_attr->next)
2647 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
2650 ords[ord_no++] = ord;
2653 if (!bases_ok && errCode)
2655 zh->errCode = errCode;
2656 zh->errString = errString;
2664 /* prepare dictionary scanning */
2676 scan_info_array = (struct scan_info *)
2677 odr_malloc (stream, ord_no * sizeof(*scan_info_array));
2678 for (i = 0; i < ord_no; i++)
2680 int j, prefix_len = 0;
2681 int before_tmp = before, after_tmp = after;
2682 struct scan_info *scan_info = scan_info_array + i;
2683 struct rpn_char_map_info rcmi;
2685 rpn_char_map_prepare (zh->reg, reg_id, &rcmi);
2687 scan_info->before = before;
2688 scan_info->after = after;
2689 scan_info->odr = stream;
2691 scan_info->list = (struct scan_info_entry *)
2692 odr_malloc (stream, (before+after) * sizeof(*scan_info->list));
2693 for (j = 0; j<before+after; j++)
2694 scan_info->list[j].term = NULL;
2696 prefix_len += key_SU_encode (ords[i], termz + prefix_len);
2697 termz[prefix_len++] = reg_id;
2698 termz[prefix_len] = 0;
2699 strcpy (scan_info->prefix, termz);
2701 if (trans_scan_term (zh, zapt, termz+prefix_len, reg_id))
2704 dict_scan (zh->reg->dict, termz, &before_tmp, &after_tmp,
2705 scan_info, scan_handle);
2707 glist = (ZebraScanEntry *)
2708 odr_malloc (stream, (before+after)*sizeof(*glist));
2710 /* consider terms after main term */
2711 for (i = 0; i < ord_no; i++)
2715 for (i = 0; i<after; i++)
2718 const char *mterm = NULL;
2721 int lo = i + pos-1; /* offset in result list */
2723 for (j = 0; j < ord_no; j++)
2725 if (ptr[j] < before+after &&
2726 (tst=scan_info_array[j].list[ptr[j]].term) &&
2727 (!mterm || strcmp (tst, mterm) < 0))
2737 scan_term_untrans (zh, stream->mem, reg_id,
2738 &glist[lo].term, mterm);
2739 rset = rset_trunc(zh, &scan_info_array[j0].list[ptr[j0]].isam_p, 1,
2740 glist[lo].term, strlen(glist[lo].term),
2741 NULL, 0, zapt->term->which);
2744 for (j = j0+1; j<ord_no; j++)
2746 if (ptr[j] < before+after &&
2747 (tst=scan_info_array[j].list[ptr[j]].term) &&
2748 !strcmp (tst, mterm))
2752 rset_bool_parms bool_parms;
2757 &scan_info_array[j].list[ptr[j]].isam_p, 1,
2759 strlen(glist[lo].term), NULL, 0,
2762 bool_parms.key_size = sizeof(struct it_key);
2763 bool_parms.cmp = key_compare_it;
2764 bool_parms.log_item = key_logdump_txt;
2765 bool_parms.rset_l = rset;
2766 bool_parms.rset_r = rset2;
2768 rset = rset_create (rset_kind_or, &bool_parms);
2777 rset_bool_parms bool_parms;
2779 bool_parms.key_size = sizeof(struct it_key);
2780 bool_parms.cmp = key_compare_it;
2781 bool_parms.log_item = key_logdump_txt;
2782 bool_parms.rset_l = rset;
2783 bool_parms.rset_r = rset_dup(limit_set);
2785 rset = rset_create (rset_kind_and, &bool_parms);
2787 count_set (rset, &glist[lo].occurrences);
2793 *num_entries -= (after-i);
2797 /* consider terms before main term */
2798 for (i = 0; i<ord_no; i++)
2801 for (i = 0; i<before; i++)
2804 const char *mterm = NULL;
2808 for (j = 0; j <ord_no; j++)
2810 if (ptr[j] < before &&
2811 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2812 (!mterm || strcmp (tst, mterm) > 0))
2821 scan_term_untrans (zh, stream->mem, reg_id,
2822 &glist[before-1-i].term, mterm);
2825 (zh, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1,
2826 glist[before-1-i].term, strlen(glist[before-1-i].term),
2827 NULL, 0, zapt->term->which);
2831 for (j = j0+1; j<ord_no; j++)
2833 if (ptr[j] < before &&
2834 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2835 !strcmp (tst, mterm))
2837 rset_bool_parms bool_parms;
2840 rset2 = rset_trunc (zh,
2841 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1,
2842 glist[before-1-i].term,
2843 strlen(glist[before-1-i].term), NULL, 0,
2846 bool_parms.key_size = sizeof(struct it_key);
2847 bool_parms.cmp = key_compare_it;
2848 bool_parms.log_item = key_logdump_txt;
2849 bool_parms.rset_l = rset;
2850 bool_parms.rset_r = rset2;
2852 rset = rset_create (rset_kind_or, &bool_parms);
2859 rset_bool_parms bool_parms;
2861 bool_parms.key_size = sizeof(struct it_key);
2862 bool_parms.cmp = key_compare_it;
2863 bool_parms.log_item = key_logdump_txt;
2864 bool_parms.rset_l = rset;
2865 bool_parms.rset_r = rset_dup(limit_set);
2867 rset = rset_create (rset_kind_and, &bool_parms);
2869 count_set (rset, &glist[before-1-i].occurrences);
2879 *list = glist + i; /* list is set to first 'real' entry */
2881 yaz_log(LOG_DEBUG, "position = %d, num_entries = %d",
2882 *position, *num_entries);
2884 yaz_log(LOG_DEBUG, "scan error: %d", zh->errCode);