1 /* $Id: zrpn.c,v 1.141.2.15 2006-08-14 10:39:01 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 this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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++;
569 map = zebra_maps_search(zebra_maps, reg_type, &s0, strlen(s0),
571 if (space_split && **map == *CHR_SPACE)
574 /* add non-space char */
575 memcpy(dst_term+j, s1, s0 - s1);
581 if (strchr(REGEX_CHARS, *s1))
589 esc_str(tmpbuf, sizeof(tmpbuf), map[0], strlen(map[0]));
591 strcpy(dst + i, map[0]);
597 dst_term[j++] = '\0';
602 /* term_105/106: handle term, where trunc=Process * and ! and right trunc */
603 static int term_105 (ZebraMaps zebra_maps, int reg_type,
604 const char **src, char *dst, int space_split,
605 char *dst_term, int right_truncate)
612 if (!term_pre(zebra_maps, reg_type, src, "*!", "*!", !space_split))
621 dst_term[j++] = *s0++;
626 dst_term[j++] = *s0++;
632 map = zebra_maps_search(zebra_maps, reg_type, &s0, strlen(s0),
634 if (space_split && **map == *CHR_SPACE)
637 /* add non-space char */
638 memcpy(dst_term+j, s1, s0 - s1);
644 if (strchr(REGEX_CHARS, *s1))
652 esc_str(tmpbuf, sizeof(tmpbuf), map[0], strlen(map[0]));
654 strcpy(dst + i, map[0]);
666 dst_term[j++] = '\0';
672 /* gen_regular_rel - generate regular expression from relation
673 * val: border value (inclusive)
674 * islt: 1 if <=; 0 if >=.
676 static void gen_regular_rel(char *dst, int val, int islt)
683 yaz_log(LOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
687 strcpy(dst, "(-[0-9]+|(");
695 strcpy(dst, "([0-9]+|-(");
707 sprintf(numstr, "%d", val);
708 for (w = strlen(numstr); --w >= 0; pos++)
727 strcpy(dst + dst_p, numstr);
728 dst_p = strlen(dst) - pos - 1;
756 for (i = 0; i<pos; i++)
769 /* match everything less than 10^(pos-1) */
771 for (i = 1; i<pos; i++)
772 strcat(dst, "[0-9]?");
776 /* match everything greater than 10^pos */
777 for (i = 0; i <= pos; i++)
778 strcat(dst, "[0-9]");
779 strcat(dst, "[0-9]*");
784 void string_rel_add_char(char **term_p, const char *src, int *indx)
786 if (src[*indx] == '\\')
787 *(*term_p)++ = src[(*indx)++];
788 *(*term_p)++ = src[(*indx)++];
792 * > abc ([b-].*|a[c-].*|ab[d-].*|abc.+)
793 * ([^-a].*|a[^-b].*ab[^-c].*|abc.+)
794 * >= abc ([b-].*|a[c-].*|ab[c-].*)
795 * ([^-a].*|a[^-b].*|ab[c-].*)
796 * < abc ([-0].*|a[-a].*|ab[-b].*)
797 * ([^a-].*|a[^b-].*|ab[^c-].*)
798 * <= abc ([-0].*|a[-a].*|ab[-b].*|abc)
799 * ([^a-].*|a[^b-].*|ab[^c-].*|abc)
801 static int string_relation(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
802 const char **term_sub, char *term_dict,
803 oid_value attributeSet,
804 int reg_type, int space_split, char *term_dst)
809 char *term_tmp = term_dict + strlen(term_dict);
810 char term_component[2*IT_MAX_WORD+20];
812 attr_init(&relation, zapt, 2);
813 relation_value = attr_find(&relation, NULL);
815 yaz_log(LOG_DEBUG, "string relation value=%d", relation_value);
816 switch (relation_value)
819 if (!term_100 (zh->reg->zebra_maps, reg_type,
820 term_sub, term_component,
821 space_split, term_dst))
823 yaz_log(LOG_DEBUG, "Relation <");
826 for (i = 0; term_component[i]; )
833 string_rel_add_char (&term_tmp, term_component, &j);
838 string_rel_add_char (&term_tmp, term_component, &i);
845 if ((term_tmp - term_dict) > IT_MAX_WORD)
852 if (!term_100 (zh->reg->zebra_maps, reg_type,
853 term_sub, term_component,
854 space_split, term_dst))
856 yaz_log(LOG_DEBUG, "Relation <=");
859 for (i = 0; term_component[i]; )
864 string_rel_add_char (&term_tmp, term_component, &j);
868 string_rel_add_char (&term_tmp, term_component, &i);
877 if ((term_tmp - term_dict) > IT_MAX_WORD)
880 for (i = 0; term_component[i]; )
881 string_rel_add_char (&term_tmp, term_component, &i);
886 if (!term_100 (zh->reg->zebra_maps, reg_type,
887 term_sub, term_component, space_split, term_dst))
889 yaz_log(LOG_DEBUG, "Relation >");
892 for (i = 0; term_component[i];)
897 string_rel_add_char (&term_tmp, term_component, &j);
902 string_rel_add_char (&term_tmp, term_component, &i);
910 if ((term_tmp - term_dict) > IT_MAX_WORD)
913 for (i = 0; term_component[i];)
914 string_rel_add_char (&term_tmp, term_component, &i);
921 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub,
922 term_component, space_split, term_dst))
924 yaz_log(LOG_DEBUG, "Relation >=");
927 for (i = 0; term_component[i];)
934 string_rel_add_char (&term_tmp, term_component, &j);
937 if (term_component[i+1])
941 string_rel_add_char (&term_tmp, term_component, &i);
945 string_rel_add_char (&term_tmp, term_component, &i);
952 if ((term_tmp - term_dict) > IT_MAX_WORD)
960 yaz_log(LOG_DEBUG, "Relation =");
961 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub,
962 term_component, space_split, term_dst))
964 strcat (term_tmp, "(");
965 strcat (term_tmp, term_component);
966 strcat (term_tmp, ")");
971 static int string_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
972 const char **term_sub,
973 oid_value attributeSet, NMEM stream,
974 struct grep_info *grep_info,
975 int reg_type, int complete_flag,
976 int num_bases, char **basenames,
977 char *term_dst, int xpath_use);
979 static RSET term_trunc (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
980 const char **term_sub,
981 oid_value attributeSet, NMEM stream,
982 struct grep_info *grep_info,
983 int reg_type, int complete_flag,
984 int num_bases, char **basenames,
986 const char *rank_type, int xpath_use)
989 grep_info->isam_p_indx = 0;
990 r = string_term (zh, zapt, term_sub, attributeSet, stream, grep_info,
991 reg_type, complete_flag, num_bases, basenames,
992 term_dst, xpath_use);
995 yaz_log(LOG_DEBUG, "term: %s", term_dst);
996 return rset_trunc (zh, grep_info->isam_p_buf,
997 grep_info->isam_p_indx, term_dst,
998 strlen(term_dst), rank_type, 1 /* preserve pos */,
1002 static char *nmem_strdup_i(NMEM nmem, int v)
1005 sprintf (val_str, "%d", v);
1006 return nmem_strdup (nmem, val_str);
1009 static int string_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1010 const char **term_sub,
1011 oid_value attributeSet, NMEM stream,
1012 struct grep_info *grep_info,
1013 int reg_type, int complete_flag,
1014 int num_bases, char **basenames,
1015 char *term_dst, int xpath_use)
1017 char term_dict[2*IT_MAX_WORD+4000];
1019 AttrType truncation;
1020 int truncation_value;
1023 const char *use_string = 0;
1024 oid_value curAttributeSet = attributeSet;
1026 struct rpn_char_map_info rcmi;
1027 int space_split = complete_flag ? 0 : 1;
1029 int bases_ok = 0; /* no of databases with OK attribute */
1030 int errCode = 0; /* err code (if any is not OK) */
1031 char *errString = 0; /* addinfo */
1033 rpn_char_map_prepare (zh->reg, reg_type, &rcmi);
1034 attr_init (&use, zapt, 1);
1035 use_value = attr_find_ex (&use, &curAttributeSet, &use_string);
1036 yaz_log(LOG_DEBUG, "string_term, use value %d", use_value);
1037 attr_init (&truncation, zapt, 5);
1038 truncation_value = attr_find (&truncation, NULL);
1039 yaz_log(LOG_DEBUG, "truncation value %d", truncation_value);
1041 if (use_value == -1) /* no attribute - assumy "any" */
1043 for (base_no = 0; base_no < num_bases; base_no++)
1046 int regex_range = 0;
1049 data1_local_attribute id_xpath_attr;
1050 data1_local_attribute *local_attr;
1051 int max_pos, prefix_len = 0;
1055 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
1057 zh->errCode = 109; /* Database unavailable */
1058 zh->errString = basenames[base_no];
1061 if (xpath_use > 0 && use_value == -2)
1063 use_value = xpath_use;
1064 attp.local_attributes = &id_xpath_attr;
1065 attp.attset_ordinal = VAL_IDXPATH;
1066 id_xpath_attr.next = 0;
1067 id_xpath_attr.local = use_value;
1069 else if (curAttributeSet == VAL_IDXPATH)
1071 attp.local_attributes = &id_xpath_attr;
1072 attp.attset_ordinal = VAL_IDXPATH;
1073 id_xpath_attr.next = 0;
1074 id_xpath_attr.local = use_value;
1078 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value,
1081 yaz_log(LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1082 curAttributeSet, use_value, r);
1085 /* set was found, but value wasn't defined */
1088 errString = nmem_strdup(stream, use_string);
1090 errString = nmem_strdup_i(stream, use_value);
1095 struct oident oident;
1097 oident.proto = PROTO_Z3950;
1098 oident.oclass = CLASS_ATTSET;
1099 oident.value = curAttributeSet;
1100 oid_ent_to_oid (&oident, oid);
1103 errString = nmem_strdup (stream, oident.desc);
1108 for (local_attr = attp.local_attributes; local_attr;
1109 local_attr = local_attr->next)
1115 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
1120 term_dict[prefix_len++] = '|';
1122 term_dict[prefix_len++] = '(';
1124 ord_len = key_SU_encode (ord, ord_buf);
1125 for (i = 0; i<ord_len; i++)
1127 term_dict[prefix_len++] = 1;
1128 term_dict[prefix_len++] = ord_buf[i];
1137 errString = nmem_strdup_i(stream, use_value);
1144 bases_ok++; /* this has OK attributes */
1147 term_dict[prefix_len++] = ')';
1148 term_dict[prefix_len++] = 1;
1149 term_dict[prefix_len++] = reg_type;
1150 yaz_log(LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1151 term_dict[prefix_len] = '\0';
1153 switch (truncation_value)
1155 case -1: /* not specified */
1156 case 100: /* do not truncate */
1157 if (!string_relation (zh, zapt, &termp, term_dict,
1159 reg_type, space_split, term_dst))
1162 case 1: /* right truncation */
1163 term_dict[j++] = '(';
1164 if (!term_100 (zh->reg->zebra_maps, reg_type,
1165 &termp, term_dict + j, space_split, term_dst))
1167 strcat (term_dict, ".*)");
1169 case 2: /* keft truncation */
1170 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1171 if (!term_100 (zh->reg->zebra_maps, reg_type,
1172 &termp, term_dict + j, space_split, term_dst))
1174 strcat (term_dict, ")");
1176 case 3: /* left&right truncation */
1177 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1178 if (!term_100 (zh->reg->zebra_maps, reg_type,
1179 &termp, term_dict + j, space_split, term_dst))
1181 strcat (term_dict, ".*)");
1183 case 101: /* process # in term */
1184 term_dict[j++] = '(';
1185 if (!term_101 (zh->reg->zebra_maps, reg_type,
1186 &termp, term_dict + j, space_split, term_dst))
1188 strcat (term_dict, ")");
1190 case 102: /* Regexp-1 */
1191 term_dict[j++] = '(';
1192 if (!term_102 (zh->reg->zebra_maps, reg_type,
1193 &termp, term_dict + j, space_split, term_dst))
1195 strcat (term_dict, ")");
1197 case 103: /* Regexp-2 */
1199 term_dict[j++] = '(';
1201 if (!term_103 (zh->reg->zebra_maps, reg_type,
1202 &termp, term_dict + j, ®ex_range,
1203 space_split, term_dst))
1205 strcat (term_dict, ")");
1207 case 104: /* process # and ! in term */
1208 term_dict[j++] = '(';
1209 if (!term_104 (zh->reg->zebra_maps, reg_type,
1210 &termp, term_dict + j, space_split, term_dst))
1212 strcat (term_dict, ")");
1214 case 105: /* process * and ! in term */
1215 term_dict[j++] = '(';
1216 if (!term_105 (zh->reg->zebra_maps, reg_type,
1217 &termp, term_dict + j, space_split, term_dst, 1))
1219 strcat (term_dict, ")");
1221 case 106: /* process * and ! in term */
1222 term_dict[j++] = '(';
1223 if (!term_105 (zh->reg->zebra_maps, reg_type,
1224 &termp, term_dict + j, space_split, term_dst, 0))
1226 strcat (term_dict, ")");
1230 zh->errString = nmem_strdup_i(stream, truncation_value);
1235 yaz_log(LOG_DEBUG, "dict_lookup_grep: %s", term_dict+prefix_len);
1236 r = dict_lookup_grep (zh->reg->dict, term_dict, regex_range,
1237 grep_info, &max_pos, init_pos,
1240 yaz_log(LOG_WARN, "dict_lookup_grep fail %d", r);
1245 zh->errCode = errCode;
1246 zh->errString = errString;
1250 yaz_log(LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1255 /* convert APT search term to UTF8 */
1256 static int zapt_term_to_utf8 (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1260 Z_Term *term = zapt->term;
1262 switch (term->which)
1264 case Z_Term_general:
1265 if (zh->iconv_to_utf8 != 0)
1267 char *inbuf = term->u.general->buf;
1268 size_t inleft = term->u.general->len;
1269 char *outbuf = termz;
1270 size_t outleft = IT_MAX_WORD-1;
1273 ret = yaz_iconv(zh->iconv_to_utf8, &inbuf, &inleft,
1275 if (ret == (size_t)(-1))
1277 ret = yaz_iconv(zh->iconv_to_utf8, 0, 0, 0, 0);
1285 sizez = term->u.general->len;
1286 if (sizez > IT_MAX_WORD-1)
1287 sizez = IT_MAX_WORD-1;
1288 memcpy (termz, term->u.general->buf, sizez);
1289 termz[sizez] = '\0';
1292 case Z_Term_characterString:
1293 sizez = strlen(term->u.characterString);
1294 if (sizez > IT_MAX_WORD-1)
1295 sizez = IT_MAX_WORD-1;
1296 memcpy (termz, term->u.characterString, sizez);
1297 termz[sizez] = '\0';
1306 /* convert APT SCAN term to internal cmap */
1307 static int trans_scan_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1308 char *termz, int reg_type)
1310 char termz0[IT_MAX_WORD];
1312 if (zapt_term_to_utf8(zh, zapt, termz0))
1313 return -1; /* error */
1317 const char *cp = (const char *) termz0;
1318 const char *cp_end = cp + strlen(cp);
1321 const char *space_map = NULL;
1324 while ((len = (cp_end - cp)) > 0)
1326 map = zebra_maps_input (zh->reg->zebra_maps, reg_type, &cp, len, 0);
1327 if (**map == *CHR_SPACE)
1332 for (src = space_map; *src; src++)
1335 for (src = *map; *src; src++)
1344 char *normalize_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1345 const char *termz, NMEM stream, unsigned reg_id)
1348 AttrType truncation;
1349 int truncation_value;
1352 attr_init (&truncation, zapt, 5);
1353 truncation_value = attr_find (&truncation, NULL);
1355 switch (truncation_value)
1375 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, ex_list,
1376 termz, strlen(termz));
1378 return nmem_strdup(stream, termz);
1381 char *buf = (char*) nmem_malloc (stream, wrbuf_len(wrbuf)+1);
1382 memcpy (buf, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1383 buf[wrbuf_len(wrbuf)] = '\0';
1388 static void grep_info_delete (struct grep_info *grep_info)
1391 xfree(grep_info->term_no);
1393 xfree (grep_info->isam_p_buf);
1396 static int grep_info_prepare (ZebraHandle zh,
1397 Z_AttributesPlusTerm *zapt,
1398 struct grep_info *grep_info,
1403 int termset_value_numeric;
1404 const char *termset_value_string;
1407 grep_info->term_no = 0;
1409 grep_info->isam_p_size = 0;
1410 grep_info->isam_p_buf = NULL;
1412 grep_info->reg_type = reg_type;
1413 grep_info->termset = 0;
1417 attr_init (&termset, zapt, 8);
1418 termset_value_numeric =
1419 attr_find_ex (&termset, NULL, &termset_value_string);
1420 if (termset_value_numeric != -1)
1423 const char *termset_name = 0;
1424 if (termset_value_numeric != -2)
1426 sprintf (resname, "%d", termset_value_numeric);
1427 termset_name = resname;
1430 termset_name = termset_value_string;
1431 yaz_log(LOG_LOG, "creating termset set %s", termset_name);
1432 grep_info->termset = resultSetAdd (zh, termset_name, 1);
1433 if (!grep_info->termset)
1436 zh->errString = nmem_strdup (stream, termset_name);
1444 static RSET rpn_search_APT_phrase (ZebraHandle zh,
1445 Z_AttributesPlusTerm *zapt,
1446 const char *termz_org,
1447 oid_value attributeSet,
1449 int reg_type, int complete_flag,
1450 const char *rank_type, int xpath_use,
1451 int num_bases, char **basenames)
1453 char term_dst[IT_MAX_WORD+1];
1454 RSET rset[60], result;
1456 struct grep_info grep_info;
1457 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1458 const char *termp = termz;
1461 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1465 yaz_log(LOG_DEBUG, "APT_phrase 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,
1474 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1477 grep_info_delete (&grep_info);
1480 rset_null_parms parms;
1482 parms.rset_term = rset_term_create (termz, -1, rank_type,
1484 return rset_create (rset_kind_null, &parms);
1486 else if (rset_no == 1)
1490 /* new / old prox */
1491 rset_prox_parms parms;
1494 parms.rset_no = rset_no;
1496 parms.exclusion = 0;
1499 parms.key_size = sizeof(struct it_key);
1500 parms.cmp = key_compare_it;
1501 parms.getseq = key_get_seq;
1502 parms.log_item = key_logdump_txt;
1503 result = rset_create(rset_kind_prox, &parms);
1508 static RSET rpn_search_APT_or_list (ZebraHandle zh,
1509 Z_AttributesPlusTerm *zapt,
1510 const char *termz_org,
1511 oid_value attributeSet,
1513 int reg_type, int complete_flag,
1514 const char *rank_type,
1516 int num_bases, char **basenames)
1518 char term_dst[IT_MAX_WORD+1];
1519 RSET rset[60], result;
1521 struct grep_info grep_info;
1522 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1523 const char *termp = termz;
1525 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1529 yaz_log(LOG_DEBUG, "APT_or_list termp=%s", termp);
1530 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1532 reg_type, complete_flag,
1533 num_bases, basenames,
1534 term_dst, rank_type,
1538 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1541 grep_info_delete (&grep_info);
1544 rset_null_parms parms;
1546 parms.rset_term = rset_term_create (termz, -1, rank_type,
1548 return rset_create (rset_kind_null, &parms);
1551 for (i = 1; i<rset_no; i++)
1553 rset_bool_parms bool_parms;
1555 bool_parms.rset_l = result;
1556 bool_parms.rset_r = rset[i];
1557 bool_parms.key_size = sizeof(struct it_key);
1558 bool_parms.cmp = key_compare_it;
1559 bool_parms.log_item = key_logdump_txt;
1560 result = rset_create (rset_kind_or, &bool_parms);
1565 static RSET rpn_search_APT_and_list (ZebraHandle zh,
1566 Z_AttributesPlusTerm *zapt,
1567 const char *termz_org,
1568 oid_value attributeSet,
1570 int reg_type, int complete_flag,
1571 const char *rank_type,
1573 int num_bases, char **basenames)
1575 char term_dst[IT_MAX_WORD+1];
1576 RSET rset[60], result;
1578 struct grep_info grep_info;
1579 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1580 const char *termp = termz;
1582 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1586 yaz_log(LOG_DEBUG, "APT_and_list termp=%s", termp);
1587 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1589 reg_type, complete_flag,
1590 num_bases, basenames,
1591 term_dst, rank_type,
1595 assert (rset[rset_no]);
1596 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1599 grep_info_delete (&grep_info);
1602 rset_null_parms parms;
1604 parms.rset_term = rset_term_create (termz, -1, rank_type,
1606 return rset_create (rset_kind_null, &parms);
1609 for (i = 1; i<rset_no; i++)
1611 rset_bool_parms bool_parms;
1613 bool_parms.rset_l = result;
1614 bool_parms.rset_r = rset[i];
1615 bool_parms.key_size = sizeof(struct it_key);
1616 bool_parms.cmp = key_compare_it;
1617 bool_parms.log_item = key_logdump_txt;
1618 result = rset_create (rset_kind_and, &bool_parms);
1623 static int numeric_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1624 const char **term_sub,
1626 oid_value attributeSet,
1627 struct grep_info *grep_info,
1636 char *term_tmp = term_dict + strlen(term_dict);
1638 attr_init (&relation, zapt, 2);
1639 relation_value = attr_find (&relation, NULL);
1641 yaz_log(LOG_DEBUG, "numeric relation value=%d", relation_value);
1643 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub, term_tmp, 1,
1646 term_value = atoi (term_tmp);
1647 switch (relation_value)
1650 yaz_log(LOG_DEBUG, "Relation <");
1651 gen_regular_rel (term_tmp, term_value-1, 1);
1654 yaz_log(LOG_DEBUG, "Relation <=");
1655 gen_regular_rel (term_tmp, term_value, 1);
1658 yaz_log(LOG_DEBUG, "Relation >=");
1659 gen_regular_rel (term_tmp, term_value, 0);
1662 yaz_log(LOG_DEBUG, "Relation >");
1663 gen_regular_rel (term_tmp, term_value+1, 0);
1667 yaz_log(LOG_DEBUG, "Relation =");
1668 sprintf (term_tmp, "(0*%d)", term_value);
1670 yaz_log(LOG_DEBUG, "dict_lookup_grep: %s", term_tmp);
1671 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info, max_pos,
1674 yaz_log(LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1675 yaz_log(LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1679 static int numeric_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1680 const char **term_sub,
1681 oid_value attributeSet, struct grep_info *grep_info,
1682 int reg_type, int complete_flag,
1683 int num_bases, char **basenames,
1684 char *term_dst, int xpath_use, NMEM stream)
1686 char term_dict[2*IT_MAX_WORD+2];
1690 const char *use_string = 0;
1691 oid_value curAttributeSet = attributeSet;
1693 struct rpn_char_map_info rcmi;
1695 int bases_ok = 0; /* no of databases with OK attribute */
1696 int errCode = 0; /* err code (if any is not OK) */
1697 char *errString = 0; /* addinfo */
1699 rpn_char_map_prepare (zh->reg, reg_type, &rcmi);
1700 attr_init (&use, zapt, 1);
1701 use_value = attr_find_ex (&use, &curAttributeSet, &use_string);
1703 if (use_value == -1)
1706 for (base_no = 0; base_no < num_bases; base_no++)
1709 data1_local_attribute id_xpath_attr;
1710 data1_local_attribute *local_attr;
1711 int max_pos, prefix_len = 0;
1714 if (use_value == -2) /* string attribute (assume IDXPATH/any) */
1716 use_value = xpath_use;
1717 attp.local_attributes = &id_xpath_attr;
1718 attp.attset_ordinal = VAL_IDXPATH;
1719 id_xpath_attr.next = 0;
1720 id_xpath_attr.local = use_value;
1722 else if (curAttributeSet == VAL_IDXPATH)
1724 attp.local_attributes = &id_xpath_attr;
1725 attp.attset_ordinal = VAL_IDXPATH;
1726 id_xpath_attr.next = 0;
1727 id_xpath_attr.local = use_value;
1731 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value,
1734 yaz_log(LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1735 curAttributeSet, use_value, r);
1738 errString = nmem_strdup_i (stream, use_value);
1746 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
1748 zh->errCode = 109; /* Database unavailable */
1749 zh->errString = basenames[base_no];
1752 for (local_attr = attp.local_attributes; local_attr;
1753 local_attr = local_attr->next)
1759 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
1764 term_dict[prefix_len++] = '|';
1766 term_dict[prefix_len++] = '(';
1768 ord_len = key_SU_encode (ord, ord_buf);
1769 for (i = 0; i<ord_len; i++)
1771 term_dict[prefix_len++] = 1;
1772 term_dict[prefix_len++] = ord_buf[i];
1778 sprintf (val_str, "%d", use_value);
1780 errString = nmem_strdup (stream, val_str);
1784 term_dict[prefix_len++] = ')';
1785 term_dict[prefix_len++] = 1;
1786 term_dict[prefix_len++] = reg_type;
1787 yaz_log(LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1788 term_dict[prefix_len] = '\0';
1789 if (!numeric_relation (zh, zapt, &termp, term_dict,
1790 attributeSet, grep_info, &max_pos, reg_type,
1796 zh->errCode = errCode;
1797 zh->errString = errString;
1801 yaz_log(LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1805 static RSET rpn_search_APT_numeric (ZebraHandle zh,
1806 Z_AttributesPlusTerm *zapt,
1808 oid_value attributeSet,
1810 int reg_type, int complete_flag,
1811 const char *rank_type, int xpath_use,
1812 int num_bases, char **basenames)
1814 char term_dst[IT_MAX_WORD+1];
1815 const char *termp = termz;
1816 RSET rset[60], result;
1817 int i, r, rset_no = 0;
1818 struct grep_info grep_info;
1820 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1824 yaz_log(LOG_DEBUG, "APT_numeric termp=%s", termp);
1825 grep_info.isam_p_indx = 0;
1826 r = numeric_term (zh, zapt, &termp, attributeSet, &grep_info,
1827 reg_type, complete_flag, num_bases, basenames,
1828 term_dst, xpath_use,
1832 yaz_log(LOG_DEBUG, "term: %s", term_dst);
1833 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1834 grep_info.isam_p_indx, term_dst,
1835 strlen(term_dst), rank_type,
1836 0 /* preserve position */,
1838 assert (rset[rset_no]);
1839 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1842 grep_info_delete (&grep_info);
1845 rset_null_parms parms;
1847 parms.rset_term = rset_term_create (term_dst, -1, rank_type,
1849 return rset_create (rset_kind_null, &parms);
1852 for (i = 1; i<rset_no; i++)
1854 rset_bool_parms bool_parms;
1856 bool_parms.rset_l = result;
1857 bool_parms.rset_r = rset[i];
1858 bool_parms.key_size = sizeof(struct it_key);
1859 bool_parms.cmp = key_compare_it;
1860 bool_parms.log_item = key_logdump_txt;
1861 result = rset_create (rset_kind_and, &bool_parms);
1866 static RSET rpn_search_APT_local (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1868 oid_value attributeSet,
1870 const char *rank_type)
1875 rset_temp_parms parms;
1877 parms.rset_term = rset_term_create (termz, -1, rank_type,
1879 parms.cmp = key_compare_it;
1880 parms.key_size = sizeof (struct it_key);
1881 parms.temp_path = res_get (zh->res, "setTmpDir");
1882 result = rset_create (rset_kind_temp, &parms);
1883 rsfd = rset_open (result, RSETF_WRITE);
1885 key.sysno = atoi (termz);
1889 rset_write (result, rsfd, &key);
1890 rset_close (result, rsfd);
1894 static RSET rpn_sort_spec (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1895 oid_value attributeSet, NMEM stream,
1896 Z_SortKeySpecList *sort_sequence,
1897 const char *rank_type)
1899 rset_null_parms parms;
1901 int sort_relation_value;
1902 AttrType sort_relation_type;
1909 attr_init (&sort_relation_type, zapt, 7);
1910 sort_relation_value = attr_find (&sort_relation_type, &attributeSet);
1912 if (!sort_sequence->specs)
1914 sort_sequence->num_specs = 10;
1915 sort_sequence->specs = (Z_SortKeySpec **)
1916 nmem_malloc (stream, sort_sequence->num_specs *
1917 sizeof(*sort_sequence->specs));
1918 for (i = 0; i<sort_sequence->num_specs; i++)
1919 sort_sequence->specs[i] = 0;
1921 if (zapt->term->which != Z_Term_general)
1924 i = atoi_n ((char *) zapt->term->u.general->buf,
1925 zapt->term->u.general->len);
1926 if (i >= sort_sequence->num_specs)
1928 sprintf (termz, "%d", i);
1930 oe.proto = PROTO_Z3950;
1931 oe.oclass = CLASS_ATTSET;
1932 oe.value = attributeSet;
1933 if (!oid_ent_to_oid (&oe, oid))
1936 sks = (Z_SortKeySpec *) nmem_malloc (stream, sizeof(*sks));
1937 sks->sortElement = (Z_SortElement *)
1938 nmem_malloc (stream, sizeof(*sks->sortElement));
1939 sks->sortElement->which = Z_SortElement_generic;
1940 sk = sks->sortElement->u.generic = (Z_SortKey *)
1941 nmem_malloc (stream, sizeof(*sk));
1942 sk->which = Z_SortKey_sortAttributes;
1943 sk->u.sortAttributes = (Z_SortAttributes *)
1944 nmem_malloc (stream, sizeof(*sk->u.sortAttributes));
1946 sk->u.sortAttributes->id = oid;
1947 sk->u.sortAttributes->list = zapt->attributes;
1949 sks->sortRelation = (int *)
1950 nmem_malloc (stream, sizeof(*sks->sortRelation));
1951 if (sort_relation_value == 1)
1952 *sks->sortRelation = Z_SortKeySpec_ascending;
1953 else if (sort_relation_value == 2)
1954 *sks->sortRelation = Z_SortKeySpec_descending;
1956 *sks->sortRelation = Z_SortKeySpec_ascending;
1958 sks->caseSensitivity = (int *)
1959 nmem_malloc (stream, sizeof(*sks->caseSensitivity));
1960 *sks->caseSensitivity = 0;
1962 sks->which = Z_SortKeySpec_null;
1963 sks->u.null = odr_nullval ();
1964 sort_sequence->specs[i] = sks;
1966 parms.rset_term = rset_term_create (termz, -1, rank_type,
1968 return rset_create (rset_kind_null, &parms);
1972 static int parse_xpath(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1973 oid_value attributeSet,
1974 struct xpath_location_step *xpath, int max, NMEM mem)
1976 oid_value curAttributeSet = attributeSet;
1978 const char *use_string = 0;
1980 attr_init (&use, zapt, 1);
1981 attr_find_ex (&use, &curAttributeSet, &use_string);
1983 if (!use_string || *use_string != '/')
1986 return zebra_parse_xpath_str(use_string, xpath, max, mem);
1991 static RSET xpath_trunc(ZebraHandle zh, NMEM stream,
1992 int reg_type, const char *term, int use,
1993 oid_value curAttributeSet)
1996 struct grep_info grep_info;
1997 char term_dict[2048];
2000 int ord = zebraExplain_lookupSU (zh->reg->zei, curAttributeSet, use);
2001 int ord_len, i, r, max_pos;
2002 int term_type = Z_Term_characterString;
2003 const char *flags = "void";
2005 if (grep_info_prepare (zh, 0 /* zapt */, &grep_info, '0', stream))
2007 rset_null_parms parms;
2009 parms.rset_term = rset_term_create (term, strlen(term),
2011 parms.rset_term->nn = 0;
2012 return rset_create (rset_kind_null, &parms);
2017 rset_null_parms parms;
2019 parms.rset_term = rset_term_create (term, strlen(term),
2021 parms.rset_term->nn = 0;
2022 return rset_create (rset_kind_null, &parms);
2025 term_dict[prefix_len++] = '|';
2027 term_dict[prefix_len++] = '(';
2029 ord_len = key_SU_encode (ord, ord_buf);
2030 for (i = 0; i<ord_len; i++)
2032 term_dict[prefix_len++] = 1;
2033 term_dict[prefix_len++] = ord_buf[i];
2035 term_dict[prefix_len++] = ')';
2036 term_dict[prefix_len++] = 1;
2037 term_dict[prefix_len++] = reg_type;
2039 strcpy (term_dict+prefix_len, term);
2041 grep_info.isam_p_indx = 0;
2042 r = dict_lookup_grep (zh->reg->dict, term_dict, 0,
2043 &grep_info, &max_pos, 0, grep_handle);
2044 yaz_log (LOG_LOG, "%s %d positions", term, grep_info.isam_p_indx);
2045 rset = rset_trunc (zh, grep_info.isam_p_buf,
2046 grep_info.isam_p_indx, term, strlen(term),
2047 flags, 1, term_type);
2048 grep_info_delete (&grep_info);
2052 static RSET rpn_search_xpath (ZebraHandle zh,
2053 oid_value attributeSet,
2054 int num_bases, char **basenames,
2055 NMEM stream, const char *rank_type, RSET rset,
2056 int xpath_len, struct xpath_location_step *xpath)
2058 oid_value curAttributeSet = attributeSet;
2065 yaz_log (LOG_DEBUG, "len=%d", xpath_len);
2066 for (i = 0; i<xpath_len; i++)
2068 yaz_log (LOG_DEBUG, "XPATH %d %s", i, xpath[i].part);
2071 curAttributeSet = VAL_IDXPATH;
2081 a[@attr=value]/b[@other=othervalue]
2083 /e/@a val range(e/,range(@a,freetext(w,1015,val),@a),e/)
2084 /a/b val range(b/a/,freetext(w,1016,val),b/a/)
2085 /a/b/@c val range(b/a/,range(@c,freetext(w,1016,val),@c),b/a/)
2086 /a/b[@c=y] val range(b/a/,freetext(w,1016,val),b/a/,@c=y)
2087 /a[@c=y]/b val range(a/,range(b/a/,freetext(w,1016,val),b/a/),a/,@c=y)
2088 /a[@c=x]/b[@c=y] range(a/,range(b/a/,freetext(w,1016,val),b/a/,@c=y),a/,@c=x)
2092 dict_grep_cmap (zh->reg->dict, 0, 0);
2094 for (base_no = 0; base_no < num_bases; base_no++)
2096 int level = xpath_len;
2099 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
2101 zh->errCode = 109; /* Database unavailable */
2102 zh->errString = basenames[base_no];
2105 while (--level >= 0)
2107 char xpath_rev[128];
2109 rset_between_parms parms;
2110 RSET rset_start_tag = 0, rset_end_tag = 0, rset_attr = 0;
2114 for (i = level; i >= 1; --i)
2116 const char *cp = xpath[i].part;
2122 memcpy (xpath_rev + len, "[^/]*", 5);
2125 else if (*cp == ' ')
2128 xpath_rev[len++] = 1;
2129 xpath_rev[len++] = ' ';
2133 xpath_rev[len++] = *cp;
2134 xpath_rev[len++] = '/';
2136 else if (i == 1) /* // case */
2138 xpath_rev[len++] = '.';
2139 xpath_rev[len++] = '*';
2144 if (xpath[level].predicate &&
2145 xpath[level].predicate->which == XPATH_PREDICATE_RELATION &&
2146 xpath[level].predicate->u.relation.name[0])
2148 WRBUF wbuf = wrbuf_alloc();
2149 wrbuf_puts(wbuf, xpath[level].predicate->u.relation.name+1);
2150 if (xpath[level].predicate->u.relation.value)
2152 const char *cp = xpath[level].predicate->u.relation.value;
2153 wrbuf_putc(wbuf, '=');
2157 if (strchr(REGEX_CHARS, *cp))
2158 wrbuf_putc(wbuf, '\\');
2159 wrbuf_putc(wbuf, *cp);
2163 wrbuf_puts(wbuf, "");
2164 rset_attr = xpath_trunc (
2165 zh, stream, '0', wrbuf_buf(wbuf), 3, curAttributeSet);
2166 wrbuf_free(wbuf, 1);
2173 yaz_log (LOG_DEBUG, "xpath_rev (%d) = %s", level, xpath_rev);
2174 if (strlen(xpath_rev))
2176 rset_start_tag = xpath_trunc(zh, stream,
2177 '0', xpath_rev, 1, curAttributeSet);
2179 rset_end_tag = xpath_trunc(zh, stream,
2180 '0', xpath_rev, 2, curAttributeSet);
2182 parms.key_size = sizeof(struct it_key);
2183 parms.cmp = key_compare_it;
2184 parms.rset_l = rset_start_tag;
2185 parms.rset_m = rset;
2186 parms.rset_r = rset_end_tag;
2187 parms.rset_attr = rset_attr;
2188 parms.printer = key_print_it;
2189 rset = rset_create (rset_kind_between, &parms);
2200 static RSET rpn_search_APT (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2201 oid_value attributeSet, NMEM stream,
2202 Z_SortKeySpecList *sort_sequence,
2203 int num_bases, char **basenames)
2206 char *search_type = NULL;
2207 char rank_type[128];
2210 char termz[IT_MAX_WORD+1];
2214 struct xpath_location_step xpath[10];
2216 zebra_maps_attr (zh->reg->zebra_maps, zapt, ®_id, &search_type,
2217 rank_type, &complete_flag, &sort_flag);
2219 yaz_log(LOG_DEBUG, "reg_id=%c", reg_id);
2220 yaz_log(LOG_DEBUG, "complete_flag=%d", complete_flag);
2221 yaz_log(LOG_DEBUG, "search_type=%s", search_type);
2222 yaz_log(LOG_DEBUG, "rank_type=%s", rank_type);
2224 if (zapt_term_to_utf8(zh, zapt, termz))
2228 return rpn_sort_spec (zh, zapt, attributeSet, stream, sort_sequence,
2230 xpath_len = parse_xpath(zh, zapt, attributeSet, xpath, 10, stream);
2234 if (xpath[xpath_len-1].part[0] == '@')
2238 if (!strcmp (search_type, "phrase"))
2240 rset = rpn_search_APT_phrase (zh, zapt, termz, attributeSet, stream,
2241 reg_id, complete_flag, rank_type,
2243 num_bases, basenames);
2245 else if (!strcmp (search_type, "and-list"))
2247 rset = rpn_search_APT_and_list (zh, zapt, termz, attributeSet, stream,
2248 reg_id, complete_flag, rank_type,
2250 num_bases, basenames);
2252 else if (!strcmp (search_type, "or-list"))
2254 rset = rpn_search_APT_or_list (zh, zapt, termz, attributeSet, stream,
2255 reg_id, complete_flag, rank_type,
2257 num_bases, basenames);
2259 else if (!strcmp (search_type, "local"))
2261 rset = rpn_search_APT_local (zh, zapt, termz, attributeSet, stream,
2264 else if (!strcmp (search_type, "numeric"))
2266 rset = rpn_search_APT_numeric (zh, zapt, termz, attributeSet, stream,
2267 reg_id, complete_flag, rank_type,
2269 num_bases, basenames);
2271 else if (!strcmp (search_type, "always"))
2277 return rpn_search_xpath (zh, attributeSet, num_bases, basenames,
2278 stream, rank_type, rset, xpath_len, xpath);
2281 static RSET rpn_search_structure (ZebraHandle zh, Z_RPNStructure *zs,
2282 oid_value attributeSet, NMEM stream,
2283 Z_SortKeySpecList *sort_sequence,
2284 int num_bases, char **basenames)
2287 if (zs->which == Z_RPNStructure_complex)
2289 Z_Operator *zop = zs->u.complex->roperator;
2290 rset_bool_parms bool_parms;
2292 bool_parms.rset_l = rpn_search_structure (zh, zs->u.complex->s1,
2293 attributeSet, stream,
2295 num_bases, basenames);
2296 if (bool_parms.rset_l == NULL)
2298 bool_parms.rset_r = rpn_search_structure (zh, zs->u.complex->s2,
2299 attributeSet, stream,
2301 num_bases, basenames);
2302 if (bool_parms.rset_r == NULL)
2304 rset_delete (bool_parms.rset_l);
2307 bool_parms.key_size = sizeof(struct it_key);
2308 bool_parms.cmp = key_compare_it;
2309 bool_parms.log_item = key_logdump_txt;
2313 case Z_Operator_and:
2314 if (res_get(zh->res, "rsetforward"))
2315 r = rset_create (rset_kind_and_forward, &bool_parms);
2317 r = rset_create (rset_kind_and, &bool_parms);
2320 r = rset_create (rset_kind_or, &bool_parms);
2322 case Z_Operator_and_not:
2323 r = rset_create (rset_kind_not, &bool_parms);
2325 case Z_Operator_prox:
2326 if (zop->u.prox->which != Z_ProximityOperator_known)
2331 if (*zop->u.prox->u.known != Z_ProxUnit_word)
2333 char *val = (char *) nmem_malloc (stream, 16);
2335 zh->errString = val;
2336 sprintf (val, "%d", *zop->u.prox->u.known);
2341 /* new / old prox */
2342 rset_prox_parms parms;
2345 twosets[0] = bool_parms.rset_l;
2346 twosets[1] = bool_parms.rset_r;
2347 parms.rset = twosets;
2349 parms.ordered = *zop->u.prox->ordered;
2350 parms.exclusion = (!zop->u.prox->exclusion ? 0 :
2351 *zop->u.prox->exclusion);
2352 parms.relation = *zop->u.prox->relationType;
2353 parms.distance = *zop->u.prox->distance;
2354 parms.key_size = sizeof(struct it_key);
2355 parms.cmp = key_compare_it;
2356 parms.getseq = key_get_seq;
2357 parms.log_item = key_logdump_txt;
2358 r = rset_create(rset_kind_prox, &parms);
2366 else if (zs->which == Z_RPNStructure_simple)
2368 if (zs->u.simple->which == Z_Operand_APT)
2370 yaz_log(LOG_DEBUG, "rpn_search_APT");
2371 r = rpn_search_APT (zh, zs->u.simple->u.attributesPlusTerm,
2372 attributeSet, stream, sort_sequence,
2373 num_bases, basenames);
2375 else if (zs->u.simple->which == Z_Operand_resultSetId)
2377 yaz_log(LOG_DEBUG, "rpn_search_ref");
2378 r = resultSetRef (zh, zs->u.simple->u.resultSetId);
2381 r = rset_create (rset_kind_null, NULL);
2384 nmem_strdup (stream, zs->u.simple->u.resultSetId);
2405 RSET rpn_search (ZebraHandle zh, NMEM nmem,
2406 Z_RPNQuery *rpn, int num_bases, char **basenames,
2407 const char *setname,
2412 oid_value attributeSet;
2413 Z_SortKeySpecList *sort_sequence;
2417 zh->errString = NULL;
2420 sort_sequence = (Z_SortKeySpecList *)
2421 nmem_malloc (nmem, sizeof(*sort_sequence));
2422 sort_sequence->num_specs = 10;
2423 sort_sequence->specs = (Z_SortKeySpec **)
2424 nmem_malloc (nmem, sort_sequence->num_specs *
2425 sizeof(*sort_sequence->specs));
2426 for (i = 0; i<sort_sequence->num_specs; i++)
2427 sort_sequence->specs[i] = 0;
2429 attrset = oid_getentbyoid (rpn->attributeSetId);
2430 attributeSet = attrset->value;
2431 rset = rpn_search_structure (zh, rpn->RPNStructure, attributeSet,
2432 nmem, sort_sequence, num_bases, basenames);
2437 yaz_log(LOG_DEBUG, "search error: %d", zh->errCode);
2439 for (i = 0; sort_sequence->specs[i]; i++)
2441 sort_sequence->num_specs = i;
2443 resultSetRank (zh, sset, rset);
2446 yaz_log(LOG_DEBUG, "resultSetSortSingle in rpn_search");
2447 resultSetSortSingle (zh, nmem, sset, rset,
2448 sort_sequence, &sort_status);
2451 yaz_log(LOG_DEBUG, "resultSetSortSingle status = %d", zh->errCode);
2457 struct scan_info_entry {
2463 struct scan_info_entry *list;
2469 static int scan_handle (char *name, const char *info, int pos, void *client)
2471 int len_prefix, idx;
2472 struct scan_info *scan_info = (struct scan_info *) client;
2474 len_prefix = strlen(scan_info->prefix);
2475 if (memcmp (name, scan_info->prefix, len_prefix))
2477 if (pos > 0) idx = scan_info->after - pos + scan_info->before;
2480 scan_info->list[idx].term = (char *)
2481 odr_malloc (scan_info->odr, strlen(name + len_prefix)+1);
2482 strcpy (scan_info->list[idx].term, name + len_prefix);
2483 assert (*info == sizeof(ISAMS_P));
2484 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAMS_P));
2488 static void scan_term_untrans (ZebraHandle zh, NMEM stream, int reg_type,
2489 char **dst, const char *src)
2491 char term_src[IT_MAX_WORD];
2492 char term_dst[IT_MAX_WORD];
2494 term_untrans (zh, reg_type, term_src, src);
2496 if (zh->iconv_from_utf8 != 0)
2499 char *inbuf = term_src;
2500 size_t inleft = strlen(term_src);
2501 char *outbuf = term_dst;
2502 size_t outleft = sizeof(term_dst)-1;
2505 ret = yaz_iconv (zh->iconv_from_utf8, &inbuf, &inleft,
2507 if (ret == (size_t)(-1))
2510 len = outbuf - term_dst;
2511 *dst = nmem_malloc (stream, len + 1);
2513 memcpy (*dst, term_dst, len);
2517 *dst = nmem_strdup (stream, term_src);
2520 static void count_set (RSET r, int *count)
2528 yaz_log(LOG_DEBUG, "count_set");
2531 rfd = rset_open (r, RSETF_READ);
2532 while (rset_read (r, rfd, &key, &term_index))
2534 if (key.sysno != psysno)
2541 rset_close (r, rfd);
2542 yaz_log(LOG_DEBUG, "%d keys, %d records", kno, *count);
2545 void rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
2546 oid_value attributeset,
2547 int num_bases, char **basenames,
2548 int *position, int *num_entries, ZebraScanEntry **list,
2549 int *is_partial, RSET limit_set, int return_zero)
2552 int pos = *position;
2553 int num = *num_entries;
2557 char termz[IT_MAX_WORD+20];
2560 const char *use_string = 0;
2561 struct scan_info *scan_info_array;
2562 ZebraScanEntry *glist;
2563 int ords[32], ord_no = 0;
2566 int bases_ok = 0; /* no of databases with OK attribute */
2567 int errCode = 0; /* err code (if any is not OK) */
2568 char *errString = 0; /* addinfo */
2571 char *search_type = NULL;
2572 char rank_type[128];
2578 if (attributeset == VAL_NONE)
2579 attributeset = VAL_BIB1;
2584 int termset_value_numeric;
2585 const char *termset_value_string;
2586 attr_init (&termset, zapt, 8);
2587 termset_value_numeric =
2588 attr_find_ex (&termset, NULL, &termset_value_string);
2589 if (termset_value_numeric != -1)
2592 const char *termset_name = 0;
2594 if (termset_value_numeric != -2)
2597 sprintf (resname, "%d", termset_value_numeric);
2598 termset_name = resname;
2601 termset_name = termset_value_string;
2603 limit_set = resultSetRef (zh, termset_name);
2607 yaz_log (LOG_DEBUG, "position = %d, num = %d set=%d",
2608 pos, num, attributeset);
2610 attr_init (&use, zapt, 1);
2611 use_value = attr_find_ex (&use, &attributeset, &use_string);
2613 if (zebra_maps_attr (zh->reg->zebra_maps, zapt, ®_id, &search_type,
2614 rank_type, &complete_flag, &sort_flag))
2620 yaz_log (LOG_DEBUG, "use_value = %d", use_value);
2622 if (use_value == -1)
2624 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
2628 data1_local_attribute *local_attr;
2630 if ((r=att_getentbyatt (zh, &attp, attributeset, use_value,
2633 yaz_log(LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
2634 attributeset, use_value);
2638 sprintf (val_str, "%d", use_value);
2640 errString = odr_strdup (stream, val_str);
2646 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
2648 zh->errString = basenames[base_no];
2649 zh->errCode = 109; /* Database unavailable */
2654 for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
2655 local_attr = local_attr->next)
2659 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
2662 ords[ord_no++] = ord;
2665 if (!bases_ok && errCode)
2667 zh->errCode = errCode;
2668 zh->errString = errString;
2676 /* prepare dictionary scanning */
2688 scan_info_array = (struct scan_info *)
2689 odr_malloc (stream, ord_no * sizeof(*scan_info_array));
2690 for (i = 0; i < ord_no; i++)
2692 int j, prefix_len = 0;
2693 int before_tmp = before, after_tmp = after;
2694 struct scan_info *scan_info = scan_info_array + i;
2695 struct rpn_char_map_info rcmi;
2697 rpn_char_map_prepare (zh->reg, reg_id, &rcmi);
2699 scan_info->before = before;
2700 scan_info->after = after;
2701 scan_info->odr = stream;
2703 scan_info->list = (struct scan_info_entry *)
2704 odr_malloc (stream, (before+after) * sizeof(*scan_info->list));
2705 for (j = 0; j<before+after; j++)
2706 scan_info->list[j].term = NULL;
2708 prefix_len += key_SU_encode (ords[i], termz + prefix_len);
2709 termz[prefix_len++] = reg_id;
2710 termz[prefix_len] = 0;
2711 strcpy (scan_info->prefix, termz);
2713 if (trans_scan_term (zh, zapt, termz+prefix_len, reg_id))
2716 dict_scan (zh->reg->dict, termz, &before_tmp, &after_tmp,
2717 scan_info, scan_handle);
2719 glist = (ZebraScanEntry *)
2720 odr_malloc (stream, (before+after)*sizeof(*glist));
2722 /* consider terms after main term */
2723 for (i = 0; i < ord_no; i++)
2727 for (i = 0; i<after; i++)
2730 const char *mterm = NULL;
2733 int lo = i + pos-1; /* offset in result list */
2735 for (j = 0; j < ord_no; j++)
2737 if (ptr[j] < before+after &&
2738 (tst=scan_info_array[j].list[ptr[j]].term) &&
2739 (!mterm || strcmp (tst, mterm) < 0))
2749 scan_term_untrans (zh, stream->mem, reg_id,
2750 &glist[lo].term, mterm);
2751 rset = rset_trunc(zh, &scan_info_array[j0].list[ptr[j0]].isam_p, 1,
2752 glist[lo].term, strlen(glist[lo].term),
2753 NULL, 0, zapt->term->which);
2756 for (j = j0+1; j<ord_no; j++)
2758 if (ptr[j] < before+after &&
2759 (tst=scan_info_array[j].list[ptr[j]].term) &&
2760 !strcmp (tst, mterm))
2764 rset_bool_parms bool_parms;
2769 &scan_info_array[j].list[ptr[j]].isam_p, 1,
2771 strlen(glist[lo].term), NULL, 0,
2774 bool_parms.key_size = sizeof(struct it_key);
2775 bool_parms.cmp = key_compare_it;
2776 bool_parms.log_item = key_logdump_txt;
2777 bool_parms.rset_l = rset;
2778 bool_parms.rset_r = rset2;
2780 rset = rset_create (rset_kind_or, &bool_parms);
2789 rset_bool_parms bool_parms;
2791 bool_parms.key_size = sizeof(struct it_key);
2792 bool_parms.cmp = key_compare_it;
2793 bool_parms.log_item = key_logdump_txt;
2794 bool_parms.rset_l = rset;
2795 bool_parms.rset_r = rset_dup(limit_set);
2797 rset = rset_create (rset_kind_and, &bool_parms);
2799 count_set (rset, &glist[lo].occurrences);
2805 *num_entries -= (after-i);
2807 if (*num_entries <= 0)
2814 /* consider terms before main term */
2815 for (i = 0; i<ord_no; i++)
2818 for (i = 0; i<before; i++)
2821 const char *mterm = NULL;
2825 for (j = 0; j <ord_no; j++)
2827 if (ptr[j] < before &&
2828 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2829 (!mterm || strcmp (tst, mterm) > 0))
2838 scan_term_untrans (zh, stream->mem, reg_id,
2839 &glist[before-1-i].term, mterm);
2842 (zh, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1,
2843 glist[before-1-i].term, strlen(glist[before-1-i].term),
2844 NULL, 0, zapt->term->which);
2848 for (j = j0+1; j<ord_no; j++)
2850 if (ptr[j] < before &&
2851 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2852 !strcmp (tst, mterm))
2854 rset_bool_parms bool_parms;
2857 rset2 = rset_trunc (zh,
2858 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1,
2859 glist[before-1-i].term,
2860 strlen(glist[before-1-i].term), NULL, 0,
2863 bool_parms.key_size = sizeof(struct it_key);
2864 bool_parms.cmp = key_compare_it;
2865 bool_parms.log_item = key_logdump_txt;
2866 bool_parms.rset_l = rset;
2867 bool_parms.rset_r = rset2;
2869 rset = rset_create (rset_kind_or, &bool_parms);
2876 rset_bool_parms bool_parms;
2878 bool_parms.key_size = sizeof(struct it_key);
2879 bool_parms.cmp = key_compare_it;
2880 bool_parms.log_item = key_logdump_txt;
2881 bool_parms.rset_l = rset;
2882 bool_parms.rset_r = rset_dup(limit_set);
2884 rset = rset_create (rset_kind_and, &bool_parms);
2886 count_set (rset, &glist[before-1-i].occurrences);
2895 if (*num_entries <= 0)
2901 *list = glist + i; /* list is set to first 'real' entry */
2903 yaz_log(LOG_DEBUG, "position = %d, num_entries = %d",
2904 *position, *num_entries);
2906 yaz_log(LOG_DEBUG, "scan error: %d", zh->errCode);