1 /* $Id: zrpn.c,v 1.141.2.2 2004-11-15 21:53:45 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
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)
278 #define REGEX_CHARS " []()|.*+?!"
280 /* term_100: handle term, where trunc=none(no operators at all) */
281 static int term_100(ZebraMaps zebra_maps, int reg_type,
282 const char **src, char *dst, int space_split,
290 const char *space_start = 0;
291 const char *space_end = 0;
293 if (!term_pre(zebra_maps, reg_type, src, NULL, NULL, !space_split))
299 map = zebra_maps_input(zebra_maps, reg_type, &s0, strlen(s0), 0);
302 if (**map == *CHR_SPACE)
305 else /* complete subfield only. */
307 if (**map == *CHR_SPACE)
308 { /* save space mapping for later .. */
313 else if (space_start)
314 { /* reload last space */
315 while (space_start < space_end)
317 if (strchr(REGEX_CHARS, *space_start))
319 dst_term[j++] = *space_start;
320 dst[i++] = *space_start++;
323 space_start = space_end = 0;
326 /* add non-space char */
329 if (strchr(REGEX_CHARS, *s1))
341 /* term_101: handle term, where trunc=Process # */
342 static int term_101(ZebraMaps zebra_maps, int reg_type,
343 const char **src, char *dst, int space_split,
351 if (!term_pre(zebra_maps, reg_type, src, "#", "#", !space_split))
360 dst_term[j++] = *s0++;
365 map = zebra_maps_input(zebra_maps, reg_type, &s0, strlen(s0), 0);
366 if (space_split && **map == *CHR_SPACE)
370 if (strchr(REGEX_CHARS, *s1))
378 dst_term[j++] = '\0';
383 /* term_103: handle term, where trunc=re-2 (regular expressions) */
384 static int term_103(ZebraMaps zebra_maps, int reg_type, const char **src,
385 char *dst, int *errors, int space_split,
393 if (!term_pre(zebra_maps, reg_type, src, "^\\()[].*+?|", "(", !space_split))
396 if (errors && *s0 == '+' && s0[1] && s0[2] == '+' && s0[3] &&
399 *errors = s0[1] - '0';
406 if (strchr("^\\()[].*+?|-", *s0))
414 map = zebra_maps_input(zebra_maps, reg_type, &s0, strlen(s0), 0);
415 if (**map == *CHR_SPACE)
419 if (strchr(REGEX_CHARS, *s1))
432 /* term_103: handle term, where trunc=re-1 (regular expressions) */
433 static int term_102 (ZebraMaps zebra_maps, int reg_type, const char **src,
434 char *dst, int space_split, char *dst_term)
436 return term_103(zebra_maps, reg_type, src, dst, NULL, space_split,
441 /* term_104: handle term, where trunc=Process # and ! */
442 static int term_104(ZebraMaps zebra_maps, int reg_type,
443 const char **src, char *dst, int space_split,
451 if (!term_pre(zebra_maps, reg_type, src, "?*#", "?*#", !space_split))
458 dst_term[j++] = *s0++;
459 if (*s0 >= '0' && *s0 <= '9')
462 while (*s0 >= '0' && *s0 <= '9')
464 limit = limit * 10 + (*s0 - '0');
465 dst_term[j++] = *s0++;
485 dst_term[j++] = *s0++;
490 dst_term[j++] = *s0++;
494 map = zebra_maps_input(zebra_maps, reg_type, &s0, strlen(s0), 0);
495 if (space_split && **map == *CHR_SPACE)
499 if (strchr(REGEX_CHARS, *s1))
507 dst_term[j++] = '\0';
512 /* term_105/106: handle term, where trunc=Process * and ! and right trunc */
513 static int term_105 (ZebraMaps zebra_maps, int reg_type,
514 const char **src, char *dst, int space_split,
515 char *dst_term, int right_truncate)
522 if (!term_pre(zebra_maps, reg_type, src, "*!", "*!", !space_split))
531 dst_term[j++] = *s0++;
536 dst_term[j++] = *s0++;
540 map = zebra_maps_input(zebra_maps, reg_type, &s0, strlen(s0), 0);
541 if (space_split && **map == *CHR_SPACE)
545 if (strchr(REGEX_CHARS, *s1))
559 dst_term[j++] = '\0';
565 /* gen_regular_rel - generate regular expression from relation
566 * val: border value (inclusive)
567 * islt: 1 if <=; 0 if >=.
569 static void gen_regular_rel(char *dst, int val, int islt)
576 yaz_log(LOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
580 strcpy(dst, "(-[0-9]+|(");
588 strcpy(dst, "([0-9]+|-(");
600 sprintf(numstr, "%d", val);
601 for (w = strlen(numstr); --w >= 0; pos++)
620 strcpy(dst + dst_p, numstr);
621 dst_p = strlen(dst) - pos - 1;
649 for (i = 0; i<pos; i++)
662 /* match everything less than 10^(pos-1) */
664 for (i = 1; i<pos; i++)
665 strcat(dst, "[0-9]?");
669 /* match everything greater than 10^pos */
670 for (i = 0; i <= pos; i++)
671 strcat(dst, "[0-9]");
672 strcat(dst, "[0-9]*");
677 void string_rel_add_char(char **term_p, const char *src, int *indx)
679 if (src[*indx] == '\\')
680 *(*term_p)++ = src[(*indx)++];
681 *(*term_p)++ = src[(*indx)++];
685 * > abc ([b-].*|a[c-].*|ab[d-].*|abc.+)
686 * ([^-a].*|a[^-b].*ab[^-c].*|abc.+)
687 * >= abc ([b-].*|a[c-].*|ab[c-].*)
688 * ([^-a].*|a[^-b].*|ab[c-].*)
689 * < abc ([-0].*|a[-a].*|ab[-b].*)
690 * ([^a-].*|a[^b-].*|ab[^c-].*)
691 * <= abc ([-0].*|a[-a].*|ab[-b].*|abc)
692 * ([^a-].*|a[^b-].*|ab[^c-].*|abc)
694 static int string_relation(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
695 const char **term_sub, char *term_dict,
696 oid_value attributeSet,
697 int reg_type, int space_split, char *term_dst)
702 char *term_tmp = term_dict + strlen(term_dict);
703 char term_component[2*IT_MAX_WORD+20];
705 attr_init(&relation, zapt, 2);
706 relation_value = attr_find(&relation, NULL);
708 yaz_log(LOG_DEBUG, "string relation value=%d", relation_value);
709 switch (relation_value)
712 if (!term_100 (zh->reg->zebra_maps, reg_type,
713 term_sub, term_component,
714 space_split, term_dst))
716 yaz_log(LOG_DEBUG, "Relation <");
719 for (i = 0; term_component[i]; )
726 string_rel_add_char (&term_tmp, term_component, &j);
731 string_rel_add_char (&term_tmp, term_component, &i);
738 if ((term_tmp - term_dict) > IT_MAX_WORD)
745 if (!term_100 (zh->reg->zebra_maps, reg_type,
746 term_sub, term_component,
747 space_split, term_dst))
749 yaz_log(LOG_DEBUG, "Relation <=");
752 for (i = 0; term_component[i]; )
757 string_rel_add_char (&term_tmp, term_component, &j);
761 string_rel_add_char (&term_tmp, term_component, &i);
770 if ((term_tmp - term_dict) > IT_MAX_WORD)
773 for (i = 0; term_component[i]; )
774 string_rel_add_char (&term_tmp, term_component, &i);
779 if (!term_100 (zh->reg->zebra_maps, reg_type,
780 term_sub, term_component, space_split, term_dst))
782 yaz_log(LOG_DEBUG, "Relation >");
785 for (i = 0; term_component[i];)
790 string_rel_add_char (&term_tmp, term_component, &j);
795 string_rel_add_char (&term_tmp, term_component, &i);
803 if ((term_tmp - term_dict) > IT_MAX_WORD)
806 for (i = 0; term_component[i];)
807 string_rel_add_char (&term_tmp, term_component, &i);
814 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub,
815 term_component, space_split, term_dst))
817 yaz_log(LOG_DEBUG, "Relation >=");
820 for (i = 0; term_component[i];)
827 string_rel_add_char (&term_tmp, term_component, &j);
830 if (term_component[i+1])
834 string_rel_add_char (&term_tmp, term_component, &i);
838 string_rel_add_char (&term_tmp, term_component, &i);
845 if ((term_tmp - term_dict) > IT_MAX_WORD)
853 yaz_log(LOG_DEBUG, "Relation =");
854 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub,
855 term_component, space_split, term_dst))
857 strcat (term_tmp, "(");
858 strcat (term_tmp, term_component);
859 strcat (term_tmp, ")");
864 static int string_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
865 const char **term_sub,
866 oid_value attributeSet, NMEM stream,
867 struct grep_info *grep_info,
868 int reg_type, int complete_flag,
869 int num_bases, char **basenames,
870 char *term_dst, int xpath_use);
872 static RSET term_trunc (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
873 const char **term_sub,
874 oid_value attributeSet, NMEM stream,
875 struct grep_info *grep_info,
876 int reg_type, int complete_flag,
877 int num_bases, char **basenames,
879 const char *rank_type, int xpath_use)
882 grep_info->isam_p_indx = 0;
883 r = string_term (zh, zapt, term_sub, attributeSet, stream, grep_info,
884 reg_type, complete_flag, num_bases, basenames,
885 term_dst, xpath_use);
888 yaz_log(LOG_DEBUG, "term: %s", term_dst);
889 return rset_trunc (zh, grep_info->isam_p_buf,
890 grep_info->isam_p_indx, term_dst,
891 strlen(term_dst), rank_type, 1 /* preserve pos */,
895 static char *nmem_strdup_i(NMEM nmem, int v)
898 sprintf (val_str, "%d", v);
899 return nmem_strdup (nmem, val_str);
902 static int string_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
903 const char **term_sub,
904 oid_value attributeSet, NMEM stream,
905 struct grep_info *grep_info,
906 int reg_type, int complete_flag,
907 int num_bases, char **basenames,
908 char *term_dst, int xpath_use)
910 char term_dict[2*IT_MAX_WORD+4000];
913 int truncation_value;
916 const char *use_string = 0;
917 oid_value curAttributeSet = attributeSet;
919 struct rpn_char_map_info rcmi;
920 int space_split = complete_flag ? 0 : 1;
922 int bases_ok = 0; /* no of databases with OK attribute */
923 int errCode = 0; /* err code (if any is not OK) */
924 char *errString = 0; /* addinfo */
926 rpn_char_map_prepare (zh->reg, reg_type, &rcmi);
927 attr_init (&use, zapt, 1);
928 use_value = attr_find_ex (&use, &curAttributeSet, &use_string);
929 yaz_log(LOG_DEBUG, "string_term, use value %d", use_value);
930 attr_init (&truncation, zapt, 5);
931 truncation_value = attr_find (&truncation, NULL);
932 yaz_log(LOG_DEBUG, "truncation value %d", truncation_value);
934 if (use_value == -1) /* no attribute - assumy "any" */
936 for (base_no = 0; base_no < num_bases; base_no++)
941 data1_local_attribute id_xpath_attr;
942 data1_local_attribute *local_attr;
943 int max_pos, prefix_len = 0;
947 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
949 zh->errCode = 109; /* Database unavailable */
950 zh->errString = basenames[base_no];
953 if (xpath_use > 0 && use_value == -2)
955 use_value = xpath_use;
956 attp.local_attributes = &id_xpath_attr;
957 attp.attset_ordinal = VAL_IDXPATH;
958 id_xpath_attr.next = 0;
959 id_xpath_attr.local = use_value;
961 else if (curAttributeSet == VAL_IDXPATH)
963 attp.local_attributes = &id_xpath_attr;
964 attp.attset_ordinal = VAL_IDXPATH;
965 id_xpath_attr.next = 0;
966 id_xpath_attr.local = use_value;
970 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value,
973 yaz_log(LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
974 curAttributeSet, use_value, r);
977 /* set was found, but value wasn't defined */
980 errString = nmem_strdup(stream, use_string);
982 errString = nmem_strdup_i(stream, use_value);
987 struct oident oident;
989 oident.proto = PROTO_Z3950;
990 oident.oclass = CLASS_ATTSET;
991 oident.value = curAttributeSet;
992 oid_ent_to_oid (&oident, oid);
995 errString = nmem_strdup (stream, oident.desc);
1000 for (local_attr = attp.local_attributes; local_attr;
1001 local_attr = local_attr->next)
1007 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
1012 term_dict[prefix_len++] = '|';
1014 term_dict[prefix_len++] = '(';
1016 ord_len = key_SU_encode (ord, ord_buf);
1017 for (i = 0; i<ord_len; i++)
1019 term_dict[prefix_len++] = 1;
1020 term_dict[prefix_len++] = ord_buf[i];
1029 errString = nmem_strdup_i(stream, use_value);
1036 bases_ok++; /* this has OK attributes */
1039 term_dict[prefix_len++] = ')';
1040 term_dict[prefix_len++] = 1;
1041 term_dict[prefix_len++] = reg_type;
1042 yaz_log(LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1043 term_dict[prefix_len] = '\0';
1045 switch (truncation_value)
1047 case -1: /* not specified */
1048 case 100: /* do not truncate */
1049 if (!string_relation (zh, zapt, &termp, term_dict,
1051 reg_type, space_split, term_dst))
1054 case 1: /* right truncation */
1055 term_dict[j++] = '(';
1056 if (!term_100 (zh->reg->zebra_maps, reg_type,
1057 &termp, term_dict + j, space_split, term_dst))
1059 strcat (term_dict, ".*)");
1061 case 2: /* keft truncation */
1062 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1063 if (!term_100 (zh->reg->zebra_maps, reg_type,
1064 &termp, term_dict + j, space_split, term_dst))
1066 strcat (term_dict, ")");
1068 case 3: /* left&right truncation */
1069 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1070 if (!term_100 (zh->reg->zebra_maps, reg_type,
1071 &termp, term_dict + j, space_split, term_dst))
1073 strcat (term_dict, ".*)");
1075 case 101: /* process # in term */
1076 term_dict[j++] = '(';
1077 if (!term_101 (zh->reg->zebra_maps, reg_type,
1078 &termp, term_dict + j, space_split, term_dst))
1080 strcat (term_dict, ")");
1082 case 102: /* Regexp-1 */
1083 term_dict[j++] = '(';
1084 if (!term_102 (zh->reg->zebra_maps, reg_type,
1085 &termp, term_dict + j, space_split, term_dst))
1087 strcat (term_dict, ")");
1089 case 103: /* Regexp-2 */
1091 term_dict[j++] = '(';
1092 if (!term_103 (zh->reg->zebra_maps, reg_type,
1093 &termp, term_dict + j, &r, space_split, term_dst))
1095 strcat (term_dict, ")");
1098 case 104: /* process # and ! in term */
1099 term_dict[j++] = '(';
1100 if (!term_104 (zh->reg->zebra_maps, reg_type,
1101 &termp, term_dict + j, space_split, term_dst))
1103 strcat (term_dict, ")");
1105 case 105: /* process * and ! in term */
1106 term_dict[j++] = '(';
1107 if (!term_105 (zh->reg->zebra_maps, reg_type,
1108 &termp, term_dict + j, space_split, term_dst, 1))
1110 strcat (term_dict, ")");
1112 case 106: /* process * and ! in term */
1113 term_dict[j++] = '(';
1114 if (!term_105 (zh->reg->zebra_maps, reg_type,
1115 &termp, term_dict + j, space_split, term_dst, 0))
1117 strcat (term_dict, ")");
1121 zh->errString = nmem_strdup_i(stream, truncation_value);
1126 yaz_log(LOG_DEBUG, "dict_lookup_grep: %s", term_dict+prefix_len);
1127 r = dict_lookup_grep (zh->reg->dict, term_dict, 0,
1128 grep_info, &max_pos, regex_range,
1131 yaz_log(LOG_WARN, "dict_lookup_grep fail %d", r);
1136 zh->errCode = errCode;
1137 zh->errString = errString;
1141 yaz_log(LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1146 /* convert APT search term to UTF8 */
1147 static int zapt_term_to_utf8 (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1151 Z_Term *term = zapt->term;
1153 switch (term->which)
1155 case Z_Term_general:
1156 if (zh->iconv_to_utf8 != 0)
1158 char *inbuf = term->u.general->buf;
1159 size_t inleft = term->u.general->len;
1160 char *outbuf = termz;
1161 size_t outleft = IT_MAX_WORD-1;
1164 ret = yaz_iconv(zh->iconv_to_utf8, &inbuf, &inleft,
1166 if (ret == (size_t)(-1))
1168 ret = yaz_iconv(zh->iconv_to_utf8, 0, 0, 0, 0);
1176 sizez = term->u.general->len;
1177 if (sizez > IT_MAX_WORD-1)
1178 sizez = IT_MAX_WORD-1;
1179 memcpy (termz, term->u.general->buf, sizez);
1180 termz[sizez] = '\0';
1183 case Z_Term_characterString:
1184 sizez = strlen(term->u.characterString);
1185 if (sizez > IT_MAX_WORD-1)
1186 sizez = IT_MAX_WORD-1;
1187 memcpy (termz, term->u.characterString, sizez);
1188 termz[sizez] = '\0';
1197 /* convert APT SCAN term to internal cmap */
1198 static int trans_scan_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1199 char *termz, int reg_type)
1201 char termz0[IT_MAX_WORD];
1203 if (zapt_term_to_utf8(zh, zapt, termz0))
1204 return -1; /* error */
1208 const char *cp = (const char *) termz0;
1209 const char *cp_end = cp + strlen(cp);
1212 const char *space_map = NULL;
1215 while ((len = (cp_end - cp)) > 0)
1217 map = zebra_maps_input (zh->reg->zebra_maps, reg_type, &cp, len, 0);
1218 if (**map == *CHR_SPACE)
1223 for (src = space_map; *src; src++)
1226 for (src = *map; *src; src++)
1235 char *normalize_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1236 const char *termz, NMEM stream, unsigned reg_id)
1239 AttrType truncation;
1240 int truncation_value;
1243 attr_init (&truncation, zapt, 5);
1244 truncation_value = attr_find (&truncation, NULL);
1246 switch (truncation_value)
1266 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, ex_list,
1267 termz, strlen(termz));
1269 return nmem_strdup(stream, termz);
1272 char *buf = (char*) nmem_malloc (stream, wrbuf_len(wrbuf)+1);
1273 memcpy (buf, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1274 buf[wrbuf_len(wrbuf)] = '\0';
1279 static void grep_info_delete (struct grep_info *grep_info)
1282 xfree(grep_info->term_no);
1284 xfree (grep_info->isam_p_buf);
1287 static int grep_info_prepare (ZebraHandle zh,
1288 Z_AttributesPlusTerm *zapt,
1289 struct grep_info *grep_info,
1294 int termset_value_numeric;
1295 const char *termset_value_string;
1298 grep_info->term_no = 0;
1300 grep_info->isam_p_size = 0;
1301 grep_info->isam_p_buf = NULL;
1303 grep_info->reg_type = reg_type;
1304 grep_info->termset = 0;
1308 attr_init (&termset, zapt, 8);
1309 termset_value_numeric =
1310 attr_find_ex (&termset, NULL, &termset_value_string);
1311 if (termset_value_numeric != -1)
1314 const char *termset_name = 0;
1315 if (termset_value_numeric != -2)
1317 sprintf (resname, "%d", termset_value_numeric);
1318 termset_name = resname;
1321 termset_name = termset_value_string;
1322 yaz_log(LOG_LOG, "creating termset set %s", termset_name);
1323 grep_info->termset = resultSetAdd (zh, termset_name, 1);
1324 if (!grep_info->termset)
1327 zh->errString = nmem_strdup (stream, termset_name);
1335 static RSET rpn_search_APT_phrase (ZebraHandle zh,
1336 Z_AttributesPlusTerm *zapt,
1337 const char *termz_org,
1338 oid_value attributeSet,
1340 int reg_type, int complete_flag,
1341 const char *rank_type, int xpath_use,
1342 int num_bases, char **basenames)
1344 char term_dst[IT_MAX_WORD+1];
1345 RSET rset[60], result;
1347 struct grep_info grep_info;
1348 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1349 const char *termp = termz;
1352 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1356 yaz_log(LOG_DEBUG, "APT_phrase termp=%s", termp);
1357 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1359 reg_type, complete_flag,
1360 num_bases, basenames,
1361 term_dst, rank_type,
1365 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1368 grep_info_delete (&grep_info);
1371 rset_null_parms parms;
1373 parms.rset_term = rset_term_create (termz, -1, rank_type,
1375 return rset_create (rset_kind_null, &parms);
1377 else if (rset_no == 1)
1381 /* new / old prox */
1382 rset_prox_parms parms;
1385 parms.rset_no = rset_no;
1387 parms.exclusion = 0;
1390 parms.key_size = sizeof(struct it_key);
1391 parms.cmp = key_compare_it;
1392 parms.getseq = key_get_seq;
1393 parms.log_item = key_logdump_txt;
1394 result = rset_create(rset_kind_prox, &parms);
1399 static RSET rpn_search_APT_or_list (ZebraHandle zh,
1400 Z_AttributesPlusTerm *zapt,
1401 const char *termz_org,
1402 oid_value attributeSet,
1404 int reg_type, int complete_flag,
1405 const char *rank_type,
1407 int num_bases, char **basenames)
1409 char term_dst[IT_MAX_WORD+1];
1410 RSET rset[60], result;
1412 struct grep_info grep_info;
1413 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1414 const char *termp = termz;
1416 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1420 yaz_log(LOG_DEBUG, "APT_or_list termp=%s", termp);
1421 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1423 reg_type, complete_flag,
1424 num_bases, basenames,
1425 term_dst, rank_type,
1429 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1432 grep_info_delete (&grep_info);
1435 rset_null_parms parms;
1437 parms.rset_term = rset_term_create (termz, -1, rank_type,
1439 return rset_create (rset_kind_null, &parms);
1442 for (i = 1; i<rset_no; i++)
1444 rset_bool_parms bool_parms;
1446 bool_parms.rset_l = result;
1447 bool_parms.rset_r = rset[i];
1448 bool_parms.key_size = sizeof(struct it_key);
1449 bool_parms.cmp = key_compare_it;
1450 bool_parms.log_item = key_logdump_txt;
1451 result = rset_create (rset_kind_or, &bool_parms);
1456 static RSET rpn_search_APT_and_list (ZebraHandle zh,
1457 Z_AttributesPlusTerm *zapt,
1458 const char *termz_org,
1459 oid_value attributeSet,
1461 int reg_type, int complete_flag,
1462 const char *rank_type,
1464 int num_bases, char **basenames)
1466 char term_dst[IT_MAX_WORD+1];
1467 RSET rset[60], result;
1469 struct grep_info grep_info;
1470 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1471 const char *termp = termz;
1473 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1477 yaz_log(LOG_DEBUG, "APT_and_list termp=%s", termp);
1478 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1480 reg_type, complete_flag,
1481 num_bases, basenames,
1482 term_dst, rank_type,
1486 assert (rset[rset_no]);
1487 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1490 grep_info_delete (&grep_info);
1493 rset_null_parms parms;
1495 parms.rset_term = rset_term_create (termz, -1, rank_type,
1497 return rset_create (rset_kind_null, &parms);
1500 for (i = 1; i<rset_no; i++)
1502 rset_bool_parms bool_parms;
1504 bool_parms.rset_l = result;
1505 bool_parms.rset_r = rset[i];
1506 bool_parms.key_size = sizeof(struct it_key);
1507 bool_parms.cmp = key_compare_it;
1508 bool_parms.log_item = key_logdump_txt;
1509 result = rset_create (rset_kind_and, &bool_parms);
1514 static int numeric_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1515 const char **term_sub,
1517 oid_value attributeSet,
1518 struct grep_info *grep_info,
1527 char *term_tmp = term_dict + strlen(term_dict);
1529 attr_init (&relation, zapt, 2);
1530 relation_value = attr_find (&relation, NULL);
1532 yaz_log(LOG_DEBUG, "numeric relation value=%d", relation_value);
1534 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub, term_tmp, 1,
1537 term_value = atoi (term_tmp);
1538 switch (relation_value)
1541 yaz_log(LOG_DEBUG, "Relation <");
1542 gen_regular_rel (term_tmp, term_value-1, 1);
1545 yaz_log(LOG_DEBUG, "Relation <=");
1546 gen_regular_rel (term_tmp, term_value, 1);
1549 yaz_log(LOG_DEBUG, "Relation >=");
1550 gen_regular_rel (term_tmp, term_value, 0);
1553 yaz_log(LOG_DEBUG, "Relation >");
1554 gen_regular_rel (term_tmp, term_value+1, 0);
1558 yaz_log(LOG_DEBUG, "Relation =");
1559 sprintf (term_tmp, "(0*%d)", term_value);
1561 yaz_log(LOG_DEBUG, "dict_lookup_grep: %s", term_tmp);
1562 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info, max_pos,
1565 yaz_log(LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1566 yaz_log(LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1570 static int numeric_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1571 const char **term_sub,
1572 oid_value attributeSet, struct grep_info *grep_info,
1573 int reg_type, int complete_flag,
1574 int num_bases, char **basenames,
1575 char *term_dst, int xpath_use, NMEM stream)
1577 char term_dict[2*IT_MAX_WORD+2];
1581 const char *use_string = 0;
1582 oid_value curAttributeSet = attributeSet;
1584 struct rpn_char_map_info rcmi;
1586 int bases_ok = 0; /* no of databases with OK attribute */
1587 int errCode = 0; /* err code (if any is not OK) */
1588 char *errString = 0; /* addinfo */
1590 rpn_char_map_prepare (zh->reg, reg_type, &rcmi);
1591 attr_init (&use, zapt, 1);
1592 use_value = attr_find_ex (&use, &curAttributeSet, &use_string);
1594 if (use_value == -1)
1597 for (base_no = 0; base_no < num_bases; base_no++)
1600 data1_local_attribute id_xpath_attr;
1601 data1_local_attribute *local_attr;
1602 int max_pos, prefix_len = 0;
1605 if (use_value == -2) /* string attribute (assume IDXPATH/any) */
1607 use_value = xpath_use;
1608 attp.local_attributes = &id_xpath_attr;
1609 attp.attset_ordinal = VAL_IDXPATH;
1610 id_xpath_attr.next = 0;
1611 id_xpath_attr.local = use_value;
1613 else if (curAttributeSet == VAL_IDXPATH)
1615 attp.local_attributes = &id_xpath_attr;
1616 attp.attset_ordinal = VAL_IDXPATH;
1617 id_xpath_attr.next = 0;
1618 id_xpath_attr.local = use_value;
1622 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value,
1625 yaz_log(LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1626 curAttributeSet, use_value, r);
1629 errString = nmem_strdup_i (stream, use_value);
1637 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
1639 zh->errCode = 109; /* Database unavailable */
1640 zh->errString = basenames[base_no];
1643 for (local_attr = attp.local_attributes; local_attr;
1644 local_attr = local_attr->next)
1650 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
1655 term_dict[prefix_len++] = '|';
1657 term_dict[prefix_len++] = '(';
1659 ord_len = key_SU_encode (ord, ord_buf);
1660 for (i = 0; i<ord_len; i++)
1662 term_dict[prefix_len++] = 1;
1663 term_dict[prefix_len++] = ord_buf[i];
1669 sprintf (val_str, "%d", use_value);
1671 errString = nmem_strdup (stream, val_str);
1675 term_dict[prefix_len++] = ')';
1676 term_dict[prefix_len++] = 1;
1677 term_dict[prefix_len++] = reg_type;
1678 yaz_log(LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1679 term_dict[prefix_len] = '\0';
1680 if (!numeric_relation (zh, zapt, &termp, term_dict,
1681 attributeSet, grep_info, &max_pos, reg_type,
1687 zh->errCode = errCode;
1688 zh->errString = errString;
1692 yaz_log(LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1696 static RSET rpn_search_APT_numeric (ZebraHandle zh,
1697 Z_AttributesPlusTerm *zapt,
1699 oid_value attributeSet,
1701 int reg_type, int complete_flag,
1702 const char *rank_type, int xpath_use,
1703 int num_bases, char **basenames)
1705 char term_dst[IT_MAX_WORD+1];
1706 const char *termp = termz;
1707 RSET rset[60], result;
1708 int i, r, rset_no = 0;
1709 struct grep_info grep_info;
1711 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1715 yaz_log(LOG_DEBUG, "APT_numeric termp=%s", termp);
1716 grep_info.isam_p_indx = 0;
1717 r = numeric_term (zh, zapt, &termp, attributeSet, &grep_info,
1718 reg_type, complete_flag, num_bases, basenames,
1719 term_dst, xpath_use,
1723 yaz_log(LOG_DEBUG, "term: %s", term_dst);
1724 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1725 grep_info.isam_p_indx, term_dst,
1726 strlen(term_dst), rank_type,
1727 0 /* preserve position */,
1729 assert (rset[rset_no]);
1730 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1733 grep_info_delete (&grep_info);
1736 rset_null_parms parms;
1738 parms.rset_term = rset_term_create (term_dst, -1, rank_type,
1740 return rset_create (rset_kind_null, &parms);
1743 for (i = 1; i<rset_no; i++)
1745 rset_bool_parms bool_parms;
1747 bool_parms.rset_l = result;
1748 bool_parms.rset_r = rset[i];
1749 bool_parms.key_size = sizeof(struct it_key);
1750 bool_parms.cmp = key_compare_it;
1751 bool_parms.log_item = key_logdump_txt;
1752 result = rset_create (rset_kind_and, &bool_parms);
1757 static RSET rpn_search_APT_local (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1759 oid_value attributeSet,
1761 const char *rank_type)
1766 rset_temp_parms parms;
1768 parms.rset_term = rset_term_create (termz, -1, rank_type,
1770 parms.cmp = key_compare_it;
1771 parms.key_size = sizeof (struct it_key);
1772 parms.temp_path = res_get (zh->res, "setTmpDir");
1773 result = rset_create (rset_kind_temp, &parms);
1774 rsfd = rset_open (result, RSETF_WRITE);
1776 key.sysno = atoi (termz);
1780 rset_write (result, rsfd, &key);
1781 rset_close (result, rsfd);
1785 static RSET rpn_sort_spec (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1786 oid_value attributeSet, NMEM stream,
1787 Z_SortKeySpecList *sort_sequence,
1788 const char *rank_type)
1790 rset_null_parms parms;
1792 int sort_relation_value;
1793 AttrType sort_relation_type;
1798 Z_AttributeElement *ae;
1803 attr_init (&sort_relation_type, zapt, 7);
1804 sort_relation_value = attr_find (&sort_relation_type, &attributeSet);
1806 attr_init (&use_type, zapt, 1);
1807 use_value = attr_find (&use_type, &attributeSet);
1809 if (!sort_sequence->specs)
1811 sort_sequence->num_specs = 10;
1812 sort_sequence->specs = (Z_SortKeySpec **)
1813 nmem_malloc (stream, sort_sequence->num_specs *
1814 sizeof(*sort_sequence->specs));
1815 for (i = 0; i<sort_sequence->num_specs; i++)
1816 sort_sequence->specs[i] = 0;
1818 if (zapt->term->which != Z_Term_general)
1821 i = atoi_n ((char *) zapt->term->u.general->buf,
1822 zapt->term->u.general->len);
1823 if (i >= sort_sequence->num_specs)
1825 sprintf (termz, "%d", i);
1827 oe.proto = PROTO_Z3950;
1828 oe.oclass = CLASS_ATTSET;
1829 oe.value = attributeSet;
1830 if (!oid_ent_to_oid (&oe, oid))
1833 sks = (Z_SortKeySpec *) nmem_malloc (stream, sizeof(*sks));
1834 sks->sortElement = (Z_SortElement *)
1835 nmem_malloc (stream, sizeof(*sks->sortElement));
1836 sks->sortElement->which = Z_SortElement_generic;
1837 sk = sks->sortElement->u.generic = (Z_SortKey *)
1838 nmem_malloc (stream, sizeof(*sk));
1839 sk->which = Z_SortKey_sortAttributes;
1840 sk->u.sortAttributes = (Z_SortAttributes *)
1841 nmem_malloc (stream, sizeof(*sk->u.sortAttributes));
1843 sk->u.sortAttributes->id = oid;
1844 sk->u.sortAttributes->list = (Z_AttributeList *)
1845 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list));
1846 sk->u.sortAttributes->list->num_attributes = 1;
1847 sk->u.sortAttributes->list->attributes = (Z_AttributeElement **)
1848 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list->attributes));
1849 ae = *sk->u.sortAttributes->list->attributes = (Z_AttributeElement *)
1850 nmem_malloc (stream, sizeof(**sk->u.sortAttributes->list->attributes));
1851 ae->attributeSet = 0;
1852 ae->attributeType = (int *)
1853 nmem_malloc (stream, sizeof(*ae->attributeType));
1854 *ae->attributeType = 1;
1855 ae->which = Z_AttributeValue_numeric;
1856 ae->value.numeric = (int *)
1857 nmem_malloc (stream, sizeof(*ae->value.numeric));
1858 *ae->value.numeric = use_value;
1860 sks->sortRelation = (int *)
1861 nmem_malloc (stream, sizeof(*sks->sortRelation));
1862 if (sort_relation_value == 1)
1863 *sks->sortRelation = Z_SortKeySpec_ascending;
1864 else if (sort_relation_value == 2)
1865 *sks->sortRelation = Z_SortKeySpec_descending;
1867 *sks->sortRelation = Z_SortKeySpec_ascending;
1869 sks->caseSensitivity = (int *)
1870 nmem_malloc (stream, sizeof(*sks->caseSensitivity));
1871 *sks->caseSensitivity = 0;
1873 sks->which = Z_SortKeySpec_null;
1874 sks->u.null = odr_nullval ();
1875 sort_sequence->specs[i] = sks;
1877 parms.rset_term = rset_term_create (termz, -1, rank_type,
1879 return rset_create (rset_kind_null, &parms);
1883 static int parse_xpath(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1884 oid_value attributeSet,
1885 struct xpath_location_step *xpath, int max, NMEM mem)
1887 oid_value curAttributeSet = attributeSet;
1889 const char *use_string = 0;
1891 attr_init (&use, zapt, 1);
1892 attr_find_ex (&use, &curAttributeSet, &use_string);
1894 if (!use_string || *use_string != '/')
1897 return zebra_parse_xpath_str(use_string, xpath, max, mem);
1902 static RSET xpath_trunc(ZebraHandle zh, NMEM stream,
1903 int reg_type, const char *term, int use,
1904 oid_value curAttributeSet)
1907 struct grep_info grep_info;
1908 char term_dict[2048];
1911 int ord = zebraExplain_lookupSU (zh->reg->zei, curAttributeSet, use);
1912 int ord_len, i, r, max_pos;
1913 int term_type = Z_Term_characterString;
1914 const char *flags = "void";
1916 if (grep_info_prepare (zh, 0 /* zapt */, &grep_info, '0', stream))
1918 rset_null_parms parms;
1920 parms.rset_term = rset_term_create (term, strlen(term),
1922 parms.rset_term->nn = 0;
1923 return rset_create (rset_kind_null, &parms);
1928 rset_null_parms parms;
1930 parms.rset_term = rset_term_create (term, strlen(term),
1932 parms.rset_term->nn = 0;
1933 return rset_create (rset_kind_null, &parms);
1936 term_dict[prefix_len++] = '|';
1938 term_dict[prefix_len++] = '(';
1940 ord_len = key_SU_encode (ord, ord_buf);
1941 for (i = 0; i<ord_len; i++)
1943 term_dict[prefix_len++] = 1;
1944 term_dict[prefix_len++] = ord_buf[i];
1946 term_dict[prefix_len++] = ')';
1947 term_dict[prefix_len++] = 1;
1948 term_dict[prefix_len++] = reg_type;
1950 strcpy (term_dict+prefix_len, term);
1952 grep_info.isam_p_indx = 0;
1953 r = dict_lookup_grep (zh->reg->dict, term_dict, 0,
1954 &grep_info, &max_pos, 0, grep_handle);
1955 yaz_log (LOG_LOG, "%s %d positions", term, grep_info.isam_p_indx);
1956 rset = rset_trunc (zh, grep_info.isam_p_buf,
1957 grep_info.isam_p_indx, term, strlen(term),
1958 flags, 1, term_type);
1959 grep_info_delete (&grep_info);
1963 static RSET rpn_search_xpath (ZebraHandle zh,
1964 oid_value attributeSet,
1965 int num_bases, char **basenames,
1966 NMEM stream, const char *rank_type, RSET rset,
1967 int xpath_len, struct xpath_location_step *xpath)
1969 oid_value curAttributeSet = attributeSet;
1976 yaz_log (LOG_DEBUG, "len=%d", xpath_len);
1977 for (i = 0; i<xpath_len; i++)
1979 yaz_log (LOG_DEBUG, "XPATH %d %s", i, xpath[i].part);
1982 curAttributeSet = VAL_IDXPATH;
1992 a[@attr=value]/b[@other=othervalue]
1994 /e/@a val range(e/,range(@a,freetext(w,1015,val),@a),e/)
1995 /a/b val range(b/a/,freetext(w,1016,val),b/a/)
1996 /a/b/@c val range(b/a/,range(@c,freetext(w,1016,val),@c),b/a/)
1997 /a/b[@c=y] val range(b/a/,freetext(w,1016,val),b/a/,@c=y)
1998 /a[@c=y]/b val range(a/,range(b/a/,freetext(w,1016,val),b/a/),a/,@c=y)
1999 /a[@c=x]/b[@c=y] range(a/,range(b/a/,freetext(w,1016,val),b/a/,@c=y),a/,@c=x)
2003 dict_grep_cmap (zh->reg->dict, 0, 0);
2005 for (base_no = 0; base_no < num_bases; base_no++)
2007 int level = xpath_len;
2010 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
2012 zh->errCode = 109; /* Database unavailable */
2013 zh->errString = basenames[base_no];
2016 while (--level >= 0)
2018 char xpath_rev[128];
2020 rset_between_parms parms;
2021 RSET rset_start_tag = 0, rset_end_tag = 0, rset_attr = 0;
2025 for (i = level; i >= 1; --i)
2027 const char *cp = xpath[i].part;
2033 memcpy (xpath_rev + len, "[^/]*", 5);
2036 else if (*cp == ' ')
2039 xpath_rev[len++] = 1;
2040 xpath_rev[len++] = ' ';
2044 xpath_rev[len++] = *cp;
2045 xpath_rev[len++] = '/';
2047 else if (i == 1) /* // case */
2049 xpath_rev[len++] = '.';
2050 xpath_rev[len++] = '*';
2055 if (xpath[level].predicate &&
2056 xpath[level].predicate->which == XPATH_PREDICATE_RELATION &&
2057 xpath[level].predicate->u.relation.name[0])
2059 WRBUF wbuf = wrbuf_alloc();
2060 wrbuf_puts(wbuf, xpath[level].predicate->u.relation.name+1);
2061 if (xpath[level].predicate->u.relation.value)
2063 const char *cp = xpath[level].predicate->u.relation.value;
2064 wrbuf_putc(wbuf, '=');
2068 if (strchr(REGEX_CHARS, *cp))
2069 wrbuf_putc(wbuf, '\\');
2070 wrbuf_putc(wbuf, *cp);
2074 wrbuf_puts(wbuf, "");
2075 rset_attr = xpath_trunc (
2076 zh, stream, '0', wrbuf_buf(wbuf), 3, curAttributeSet);
2077 wrbuf_free(wbuf, 1);
2084 yaz_log (LOG_DEBUG, "xpath_rev (%d) = %s", level, xpath_rev);
2085 if (strlen(xpath_rev))
2087 rset_start_tag = xpath_trunc(zh, stream,
2088 '0', xpath_rev, 1, curAttributeSet);
2090 rset_end_tag = xpath_trunc(zh, stream,
2091 '0', xpath_rev, 2, curAttributeSet);
2093 parms.key_size = sizeof(struct it_key);
2094 parms.cmp = key_compare_it;
2095 parms.rset_l = rset_start_tag;
2096 parms.rset_m = rset;
2097 parms.rset_r = rset_end_tag;
2098 parms.rset_attr = rset_attr;
2099 parms.printer = key_print_it;
2100 rset = rset_create (rset_kind_between, &parms);
2111 static RSET rpn_search_APT (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2112 oid_value attributeSet, NMEM stream,
2113 Z_SortKeySpecList *sort_sequence,
2114 int num_bases, char **basenames)
2117 char *search_type = NULL;
2118 char rank_type[128];
2121 char termz[IT_MAX_WORD+1];
2125 struct xpath_location_step xpath[10];
2127 zebra_maps_attr (zh->reg->zebra_maps, zapt, ®_id, &search_type,
2128 rank_type, &complete_flag, &sort_flag);
2130 yaz_log(LOG_DEBUG, "reg_id=%c", reg_id);
2131 yaz_log(LOG_DEBUG, "complete_flag=%d", complete_flag);
2132 yaz_log(LOG_DEBUG, "search_type=%s", search_type);
2133 yaz_log(LOG_DEBUG, "rank_type=%s", rank_type);
2135 if (zapt_term_to_utf8(zh, zapt, termz))
2139 return rpn_sort_spec (zh, zapt, attributeSet, stream, sort_sequence,
2141 xpath_len = parse_xpath(zh, zapt, attributeSet, xpath, 10, stream);
2145 if (xpath[xpath_len-1].part[0] == '@')
2149 if (!strcmp (search_type, "phrase"))
2151 rset = rpn_search_APT_phrase (zh, zapt, termz, attributeSet, stream,
2152 reg_id, complete_flag, rank_type,
2154 num_bases, basenames);
2156 else if (!strcmp (search_type, "and-list"))
2158 rset = rpn_search_APT_and_list (zh, zapt, termz, attributeSet, stream,
2159 reg_id, complete_flag, rank_type,
2161 num_bases, basenames);
2163 else if (!strcmp (search_type, "or-list"))
2165 rset = rpn_search_APT_or_list (zh, zapt, termz, attributeSet, stream,
2166 reg_id, complete_flag, rank_type,
2168 num_bases, basenames);
2170 else if (!strcmp (search_type, "local"))
2172 rset = rpn_search_APT_local (zh, zapt, termz, attributeSet, stream,
2175 else if (!strcmp (search_type, "numeric"))
2177 rset = rpn_search_APT_numeric (zh, zapt, termz, attributeSet, stream,
2178 reg_id, complete_flag, rank_type,
2180 num_bases, basenames);
2182 else if (!strcmp (search_type, "always"))
2188 return rpn_search_xpath (zh, attributeSet, num_bases, basenames,
2189 stream, rank_type, rset, xpath_len, xpath);
2192 static RSET rpn_search_structure (ZebraHandle zh, Z_RPNStructure *zs,
2193 oid_value attributeSet, NMEM stream,
2194 Z_SortKeySpecList *sort_sequence,
2195 int num_bases, char **basenames)
2198 if (zs->which == Z_RPNStructure_complex)
2200 Z_Operator *zop = zs->u.complex->roperator;
2201 rset_bool_parms bool_parms;
2203 bool_parms.rset_l = rpn_search_structure (zh, zs->u.complex->s1,
2204 attributeSet, stream,
2206 num_bases, basenames);
2207 if (bool_parms.rset_l == NULL)
2209 bool_parms.rset_r = rpn_search_structure (zh, zs->u.complex->s2,
2210 attributeSet, stream,
2212 num_bases, basenames);
2213 if (bool_parms.rset_r == NULL)
2215 rset_delete (bool_parms.rset_l);
2218 bool_parms.key_size = sizeof(struct it_key);
2219 bool_parms.cmp = key_compare_it;
2220 bool_parms.log_item = key_logdump_txt;
2224 case Z_Operator_and:
2225 r = rset_create (rset_kind_and, &bool_parms);
2228 r = rset_create (rset_kind_or, &bool_parms);
2230 case Z_Operator_and_not:
2231 r = rset_create (rset_kind_not, &bool_parms);
2233 case Z_Operator_prox:
2234 if (zop->u.prox->which != Z_ProximityOperator_known)
2239 if (*zop->u.prox->u.known != Z_ProxUnit_word)
2241 char *val = (char *) nmem_malloc (stream, 16);
2243 zh->errString = val;
2244 sprintf (val, "%d", *zop->u.prox->u.known);
2249 /* new / old prox */
2250 rset_prox_parms parms;
2253 twosets[0] = bool_parms.rset_l;
2254 twosets[1] = bool_parms.rset_r;
2255 parms.rset = twosets;
2257 parms.ordered = *zop->u.prox->ordered;
2258 parms.exclusion = (!zop->u.prox->exclusion ? 0 :
2259 *zop->u.prox->exclusion);
2260 parms.relation = *zop->u.prox->relationType;
2261 parms.distance = *zop->u.prox->distance;
2262 parms.key_size = sizeof(struct it_key);
2263 parms.cmp = key_compare_it;
2264 parms.getseq = key_get_seq;
2265 parms.log_item = key_logdump_txt;
2266 r = rset_create(rset_kind_prox, &parms);
2274 else if (zs->which == Z_RPNStructure_simple)
2276 if (zs->u.simple->which == Z_Operand_APT)
2278 yaz_log(LOG_DEBUG, "rpn_search_APT");
2279 r = rpn_search_APT (zh, zs->u.simple->u.attributesPlusTerm,
2280 attributeSet, stream, sort_sequence,
2281 num_bases, basenames);
2283 else if (zs->u.simple->which == Z_Operand_resultSetId)
2285 yaz_log(LOG_DEBUG, "rpn_search_ref");
2286 r = resultSetRef (zh, zs->u.simple->u.resultSetId);
2289 r = rset_create (rset_kind_null, NULL);
2292 nmem_strdup (stream, zs->u.simple->u.resultSetId);
2313 RSET rpn_search (ZebraHandle zh, NMEM nmem,
2314 Z_RPNQuery *rpn, int num_bases, char **basenames,
2315 const char *setname,
2320 oid_value attributeSet;
2321 Z_SortKeySpecList *sort_sequence;
2325 zh->errString = NULL;
2328 sort_sequence = (Z_SortKeySpecList *)
2329 nmem_malloc (nmem, sizeof(*sort_sequence));
2330 sort_sequence->num_specs = 10;
2331 sort_sequence->specs = (Z_SortKeySpec **)
2332 nmem_malloc (nmem, sort_sequence->num_specs *
2333 sizeof(*sort_sequence->specs));
2334 for (i = 0; i<sort_sequence->num_specs; i++)
2335 sort_sequence->specs[i] = 0;
2337 attrset = oid_getentbyoid (rpn->attributeSetId);
2338 attributeSet = attrset->value;
2339 rset = rpn_search_structure (zh, rpn->RPNStructure, attributeSet,
2340 nmem, sort_sequence, num_bases, basenames);
2345 yaz_log(LOG_DEBUG, "search error: %d", zh->errCode);
2347 for (i = 0; sort_sequence->specs[i]; i++)
2349 sort_sequence->num_specs = i;
2351 resultSetRank (zh, sset, rset);
2354 yaz_log(LOG_DEBUG, "resultSetSortSingle in rpn_search");
2355 resultSetSortSingle (zh, nmem, sset, rset,
2356 sort_sequence, &sort_status);
2359 yaz_log(LOG_DEBUG, "resultSetSortSingle status = %d", zh->errCode);
2365 struct scan_info_entry {
2371 struct scan_info_entry *list;
2377 static int scan_handle (char *name, const char *info, int pos, void *client)
2379 int len_prefix, idx;
2380 struct scan_info *scan_info = (struct scan_info *) client;
2382 len_prefix = strlen(scan_info->prefix);
2383 if (memcmp (name, scan_info->prefix, len_prefix))
2385 if (pos > 0) idx = scan_info->after - pos + scan_info->before;
2388 scan_info->list[idx].term = (char *)
2389 odr_malloc (scan_info->odr, strlen(name + len_prefix)+1);
2390 strcpy (scan_info->list[idx].term, name + len_prefix);
2391 assert (*info == sizeof(ISAMS_P));
2392 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAMS_P));
2396 static void scan_term_untrans (ZebraHandle zh, NMEM stream, int reg_type,
2397 char **dst, const char *src)
2399 char term_src[IT_MAX_WORD];
2400 char term_dst[IT_MAX_WORD];
2402 term_untrans (zh, reg_type, term_src, src);
2404 if (zh->iconv_from_utf8 != 0)
2407 char *inbuf = term_src;
2408 size_t inleft = strlen(term_src);
2409 char *outbuf = term_dst;
2410 size_t outleft = sizeof(term_dst)-1;
2413 ret = yaz_iconv (zh->iconv_from_utf8, &inbuf, &inleft,
2415 if (ret == (size_t)(-1))
2418 len = outbuf - term_dst;
2419 *dst = nmem_malloc (stream, len + 1);
2421 memcpy (*dst, term_dst, len);
2425 *dst = nmem_strdup (stream, term_src);
2428 static void count_set (RSET r, int *count)
2436 yaz_log(LOG_DEBUG, "count_set");
2439 rfd = rset_open (r, RSETF_READ);
2440 while (rset_read (r, rfd, &key, &term_index))
2442 if (key.sysno != psysno)
2449 rset_close (r, rfd);
2450 yaz_log(LOG_DEBUG, "%d keys, %d records", kno, *count);
2453 void rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
2454 oid_value attributeset,
2455 int num_bases, char **basenames,
2456 int *position, int *num_entries, ZebraScanEntry **list,
2457 int *is_partial, RSET limit_set, int return_zero)
2460 int pos = *position;
2461 int num = *num_entries;
2465 char termz[IT_MAX_WORD+20];
2468 const char *use_string = 0;
2469 struct scan_info *scan_info_array;
2470 ZebraScanEntry *glist;
2471 int ords[32], ord_no = 0;
2474 int bases_ok = 0; /* no of databases with OK attribute */
2475 int errCode = 0; /* err code (if any is not OK) */
2476 char *errString = 0; /* addinfo */
2479 char *search_type = NULL;
2480 char rank_type[128];
2486 if (attributeset == VAL_NONE)
2487 attributeset = VAL_BIB1;
2492 int termset_value_numeric;
2493 const char *termset_value_string;
2494 attr_init (&termset, zapt, 8);
2495 termset_value_numeric =
2496 attr_find_ex (&termset, NULL, &termset_value_string);
2497 if (termset_value_numeric != -1)
2500 const char *termset_name = 0;
2502 if (termset_value_numeric != -2)
2505 sprintf (resname, "%d", termset_value_numeric);
2506 termset_name = resname;
2509 termset_name = termset_value_string;
2511 limit_set = resultSetRef (zh, termset_name);
2515 yaz_log (LOG_DEBUG, "position = %d, num = %d set=%d",
2516 pos, num, attributeset);
2518 attr_init (&use, zapt, 1);
2519 use_value = attr_find_ex (&use, &attributeset, &use_string);
2521 if (zebra_maps_attr (zh->reg->zebra_maps, zapt, ®_id, &search_type,
2522 rank_type, &complete_flag, &sort_flag))
2528 yaz_log (LOG_DEBUG, "use_value = %d", use_value);
2530 if (use_value == -1)
2532 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
2536 data1_local_attribute *local_attr;
2538 if ((r=att_getentbyatt (zh, &attp, attributeset, use_value,
2541 yaz_log(LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
2542 attributeset, use_value);
2546 sprintf (val_str, "%d", use_value);
2548 errString = odr_strdup (stream, val_str);
2554 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
2556 zh->errString = basenames[base_no];
2557 zh->errCode = 109; /* Database unavailable */
2562 for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
2563 local_attr = local_attr->next)
2567 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
2570 ords[ord_no++] = ord;
2573 if (!bases_ok && errCode)
2575 zh->errCode = errCode;
2576 zh->errString = errString;
2582 sprintf (val_str, "%d", use_value);
2584 zh->errString = odr_strdup (stream, val_str);
2589 /* prepare dictionary scanning */
2592 scan_info_array = (struct scan_info *)
2593 odr_malloc (stream, ord_no * sizeof(*scan_info_array));
2594 for (i = 0; i < ord_no; i++)
2596 int j, prefix_len = 0;
2597 int before_tmp = before, after_tmp = after;
2598 struct scan_info *scan_info = scan_info_array + i;
2599 struct rpn_char_map_info rcmi;
2601 rpn_char_map_prepare (zh->reg, reg_id, &rcmi);
2603 scan_info->before = before;
2604 scan_info->after = after;
2605 scan_info->odr = stream;
2607 scan_info->list = (struct scan_info_entry *)
2608 odr_malloc (stream, (before+after) * sizeof(*scan_info->list));
2609 for (j = 0; j<before+after; j++)
2610 scan_info->list[j].term = NULL;
2612 prefix_len += key_SU_encode (ords[i], termz + prefix_len);
2613 termz[prefix_len++] = reg_id;
2614 termz[prefix_len] = 0;
2615 strcpy (scan_info->prefix, termz);
2617 if (trans_scan_term (zh, zapt, termz+prefix_len, reg_id))
2620 dict_scan (zh->reg->dict, termz, &before_tmp, &after_tmp,
2621 scan_info, scan_handle);
2623 glist = (ZebraScanEntry *)
2624 odr_malloc (stream, (before+after)*sizeof(*glist));
2626 /* consider terms after main term */
2627 for (i = 0; i < ord_no; i++)
2631 for (i = 0; i<after; i++)
2634 const char *mterm = NULL;
2638 for (j = 0; j < ord_no; j++)
2640 if (ptr[j] < before+after &&
2641 (tst=scan_info_array[j].list[ptr[j]].term) &&
2642 (!mterm || strcmp (tst, mterm) < 0))
2650 scan_term_untrans (zh, stream->mem, reg_id,
2651 &glist[i+before].term, mterm);
2652 rset = rset_trunc (zh, &scan_info_array[j0].list[ptr[j0]].isam_p, 1,
2653 glist[i+before].term, strlen(glist[i+before].term),
2654 NULL, 0, zapt->term->which);
2657 for (j = j0+1; j<ord_no; j++)
2659 if (ptr[j] < before+after &&
2660 (tst=scan_info_array[j].list[ptr[j]].term) &&
2661 !strcmp (tst, mterm))
2663 rset_bool_parms bool_parms;
2667 rset_trunc (zh, &scan_info_array[j].list[ptr[j]].isam_p, 1,
2668 glist[i+before].term,
2669 strlen(glist[i+before].term), NULL, 0,
2672 bool_parms.key_size = sizeof(struct it_key);
2673 bool_parms.cmp = key_compare_it;
2674 bool_parms.log_item = key_logdump_txt;
2675 bool_parms.rset_l = rset;
2676 bool_parms.rset_r = rset2;
2678 rset = rset_create (rset_kind_or, &bool_parms);
2685 rset_bool_parms bool_parms;
2687 bool_parms.key_size = sizeof(struct it_key);
2688 bool_parms.cmp = key_compare_it;
2689 bool_parms.log_item = key_logdump_txt;
2690 bool_parms.rset_l = rset;
2691 bool_parms.rset_r = rset_dup(limit_set);
2693 rset = rset_create (rset_kind_and, &bool_parms);
2695 count_set (rset, &glist[i+before].occurrences);
2700 *num_entries -= (after-i);
2704 /* consider terms before main term */
2705 for (i = 0; i<ord_no; i++)
2708 for (i = 0; i<before; i++)
2711 const char *mterm = NULL;
2715 for (j = 0; j <ord_no; j++)
2717 if (ptr[j] < before &&
2718 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2719 (!mterm || strcmp (tst, mterm) > 0))
2728 scan_term_untrans (zh, stream->mem, reg_id,
2729 &glist[before-1-i].term, mterm);
2732 (zh, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1,
2733 glist[before-1-i].term, strlen(glist[before-1-i].term),
2734 NULL, 0, zapt->term->which);
2738 for (j = j0+1; j<ord_no; j++)
2740 if (ptr[j] < before &&
2741 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2742 !strcmp (tst, mterm))
2744 rset_bool_parms bool_parms;
2747 rset2 = rset_trunc (zh,
2748 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1,
2749 glist[before-1-i].term,
2750 strlen(glist[before-1-i].term), NULL, 0,
2753 bool_parms.key_size = sizeof(struct it_key);
2754 bool_parms.cmp = key_compare_it;
2755 bool_parms.log_item = key_logdump_txt;
2756 bool_parms.rset_l = rset;
2757 bool_parms.rset_r = rset2;
2759 rset = rset_create (rset_kind_or, &bool_parms);
2766 rset_bool_parms bool_parms;
2768 bool_parms.key_size = sizeof(struct it_key);
2769 bool_parms.cmp = key_compare_it;
2770 bool_parms.log_item = key_logdump_txt;
2771 bool_parms.rset_l = rset;
2772 bool_parms.rset_r = rset_dup(limit_set);
2774 rset = rset_create (rset_kind_and, &bool_parms);
2776 count_set (rset, &glist[before-1-i].occurrences);
2786 *list = glist + i; /* list is set to first 'real' entry */
2788 yaz_log(LOG_DEBUG, "position = %d, num_entries = %d",
2789 *position, *num_entries);
2791 yaz_log(LOG_DEBUG, "scan error: %d", zh->errCode);