1 /* $Id: zrpn.c,v 1.164 2004-12-10 12:37:07 heikki 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 static const struct key_control it_ctrl =
42 sizeof(struct it_key),
43 2, /* we have sysnos and seqnos in this key, nothing more */
45 key_logdump_txt, /* FIXME - clean up these functions */
50 const struct key_control *key_it_ctrl = &it_ctrl;
52 struct rpn_char_map_info
63 Z_AttributesPlusTerm *zapt;
67 static int log_level_set=0;
68 static int log_level_rpn=0;
70 static const char **rpn_char_map_handler(void *vp, const char **from, int len)
72 struct rpn_char_map_info *p = (struct rpn_char_map_info *) vp;
73 const char **out = zebra_maps_input(p->zm, p->reg_type, from, len, 0);
77 const char *outp = *out;
78 yaz_log(YLOG_LOG, "---");
81 yaz_log(YLOG_LOG, "%02X", *outp);
89 static void rpn_char_map_prepare(struct zebra_register *reg, int reg_type,
90 struct rpn_char_map_info *map_info)
92 map_info->zm = reg->zebra_maps;
93 map_info->reg_type = reg_type;
94 dict_grep_cmap(reg->dict, map_info, rpn_char_map_handler);
97 static int attr_find_ex(AttrType *src, oid_value *attributeSetP,
98 const char **string_value)
102 num_attributes = src->zapt->attributes->num_attributes;
103 while (src->major < num_attributes)
105 Z_AttributeElement *element;
107 element = src->zapt->attributes->attributes[src->major];
108 if (src->type == *element->attributeType)
110 switch (element->which)
112 case Z_AttributeValue_numeric:
114 if (element->attributeSet && attributeSetP)
118 attrset = oid_getentbyoid(element->attributeSet);
119 *attributeSetP = attrset->value;
121 return *element->value.numeric;
123 case Z_AttributeValue_complex:
124 if (src->minor >= element->value.complex->num_list)
126 if (element->attributeSet && attributeSetP)
130 attrset = oid_getentbyoid(element->attributeSet);
131 *attributeSetP = attrset->value;
133 if (element->value.complex->list[src->minor]->which ==
134 Z_StringOrNumeric_numeric)
138 *element->value.complex->list[src->minor-1]->u.numeric;
140 else if (element->value.complex->list[src->minor]->which ==
141 Z_StringOrNumeric_string)
147 element->value.complex->list[src->minor-1]->u.string;
161 static int attr_find(AttrType *src, oid_value *attributeSetP)
163 return attr_find_ex(src, attributeSetP, 0);
166 static void attr_init(AttrType *src, Z_AttributesPlusTerm *zapt,
189 static void term_untrans(ZebraHandle zh, int reg_type,
190 char *dst, const char *src)
195 const char *cp = zebra_maps_output(zh->reg->zebra_maps,
197 if (!cp && len < IT_MAX_WORD-1)
200 while (*cp && len < IT_MAX_WORD-1)
206 static void add_isam_p(const char *name, const char *info,
211 log_level_rpn = yaz_log_module_level("rpn");
214 if (p->isam_p_indx == p->isam_p_size)
216 ISAMC_P *new_isam_p_buf;
220 p->isam_p_size = 2*p->isam_p_size + 100;
221 new_isam_p_buf = (ISAMC_P *) xmalloc(sizeof(*new_isam_p_buf) *
225 memcpy(new_isam_p_buf, p->isam_p_buf,
226 p->isam_p_indx * sizeof(*p->isam_p_buf));
227 xfree(p->isam_p_buf);
229 p->isam_p_buf = new_isam_p_buf;
232 new_term_no = (int *) xmalloc(sizeof(*new_term_no) *
236 memcpy(new_term_no, p->isam_p_buf,
237 p->isam_p_indx * sizeof(*p->term_no));
240 p->term_no = new_term_no;
243 assert(*info == sizeof(*p->isam_p_buf));
244 memcpy(p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
251 char term_tmp[IT_MAX_WORD];
253 int len = key_SU_decode (&su_code, name);
255 term_untrans (p->zh, p->reg_type, term_tmp, name+len+1);
256 yaz_log(log_level_rpn, "grep: %d %c %s", su_code, name[len], term_tmp);
257 zebraExplain_lookup_ord (p->zh->reg->zei,
258 su_code, &db, &set, &use);
259 yaz_log(log_level_rpn, "grep: set=%d use=%d db=%s", set, use, db);
261 resultSetAddTerm(p->zh, p->termset, name[len], db,
268 static int grep_handle(char *name, const char *info, void *p)
270 add_isam_p(name, info, (struct grep_info *) p);
274 static int term_pre(ZebraMaps zebra_maps, int reg_type, const char **src,
275 const char *ct1, const char *ct2, int first)
277 const char *s1, *s0 = *src;
280 /* skip white space */
283 if (ct1 && strchr(ct1, *s0))
285 if (ct2 && strchr(ct2, *s0))
288 map = zebra_maps_input(zebra_maps, reg_type, &s1, strlen(s1), first);
289 if (**map != *CHR_SPACE)
297 #define REGEX_CHARS " []()|.*+?!"
299 /* term_100: handle term, where trunc=none(no operators at all) */
300 static int term_100(ZebraMaps zebra_maps, int reg_type,
301 const char **src, char *dst, int space_split,
309 const char *space_start = 0;
310 const char *space_end = 0;
312 if (!term_pre(zebra_maps, reg_type, src, NULL, NULL, !space_split))
318 map = zebra_maps_input(zebra_maps, reg_type, &s0, strlen(s0), 0);
321 if (**map == *CHR_SPACE)
324 else /* complete subfield only. */
326 if (**map == *CHR_SPACE)
327 { /* save space mapping for later .. */
332 else if (space_start)
333 { /* reload last space */
334 while (space_start < space_end)
336 if (strchr(REGEX_CHARS, *space_start))
338 dst_term[j++] = *space_start;
339 dst[i++] = *space_start++;
342 space_start = space_end = 0;
345 /* add non-space char */
348 if (strchr(REGEX_CHARS, *s1))
360 /* term_101: handle term, where trunc=Process # */
361 static int term_101(ZebraMaps zebra_maps, int reg_type,
362 const char **src, char *dst, int space_split,
370 if (!term_pre(zebra_maps, reg_type, src, "#", "#", !space_split))
379 dst_term[j++] = *s0++;
384 map = zebra_maps_input(zebra_maps, reg_type, &s0, strlen(s0), 0);
385 if (space_split && **map == *CHR_SPACE)
389 if (strchr(REGEX_CHARS, *s1))
397 dst_term[j++] = '\0';
402 /* term_103: handle term, where trunc=re-2 (regular expressions) */
403 static int term_103(ZebraMaps zebra_maps, int reg_type, const char **src,
404 char *dst, int *errors, int space_split,
412 if (!term_pre(zebra_maps, reg_type, src, "^\\()[].*+?|", "(", !space_split))
415 if (errors && *s0 == '+' && s0[1] && s0[2] == '+' && s0[3] &&
418 *errors = s0[1] - '0';
425 if (strchr("^\\()[].*+?|-", *s0))
433 map = zebra_maps_input(zebra_maps, reg_type, &s0, strlen(s0), 0);
434 if (**map == *CHR_SPACE)
438 if (strchr(REGEX_CHARS, *s1))
451 /* term_103: handle term, where trunc=re-1 (regular expressions) */
452 static int term_102 (ZebraMaps zebra_maps, int reg_type, const char **src,
453 char *dst, int space_split, char *dst_term)
455 return term_103(zebra_maps, reg_type, src, dst, NULL, space_split,
460 /* term_104: handle term, where trunc=Process # and ! */
461 static int term_104(ZebraMaps zebra_maps, int reg_type,
462 const char **src, char *dst, int space_split,
470 if (!term_pre(zebra_maps, reg_type, src, "?*#", "?*#", !space_split))
477 dst_term[j++] = *s0++;
478 if (*s0 >= '0' && *s0 <= '9')
481 while (*s0 >= '0' && *s0 <= '9')
483 limit = limit * 10 + (*s0 - '0');
484 dst_term[j++] = *s0++;
504 dst_term[j++] = *s0++;
509 dst_term[j++] = *s0++;
513 map = zebra_maps_input(zebra_maps, reg_type, &s0, strlen(s0), 0);
514 if (space_split && **map == *CHR_SPACE)
518 if (strchr(REGEX_CHARS, *s1))
526 dst_term[j++] = '\0';
531 /* term_105/106: handle term, where trunc=Process * and ! and right trunc */
532 static int term_105 (ZebraMaps zebra_maps, int reg_type,
533 const char **src, char *dst, int space_split,
534 char *dst_term, int right_truncate)
541 if (!term_pre(zebra_maps, reg_type, src, "*!", "*!", !space_split))
550 dst_term[j++] = *s0++;
555 dst_term[j++] = *s0++;
559 map = zebra_maps_input(zebra_maps, reg_type, &s0, strlen(s0), 0);
560 if (space_split && **map == *CHR_SPACE)
564 if (strchr(REGEX_CHARS, *s1))
578 dst_term[j++] = '\0';
584 /* gen_regular_rel - generate regular expression from relation
585 * val: border value (inclusive)
586 * islt: 1 if <=; 0 if >=.
588 static void gen_regular_rel(char *dst, int val, int islt)
595 yaz_log(YLOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
599 strcpy(dst, "(-[0-9]+|(");
607 strcpy(dst, "([0-9]+|-(");
619 sprintf(numstr, "%d", val);
620 for (w = strlen(numstr); --w >= 0; pos++)
639 strcpy(dst + dst_p, numstr);
640 dst_p = strlen(dst) - pos - 1;
668 for (i = 0; i<pos; i++)
681 /* match everything less than 10^(pos-1) */
683 for (i=1; i<pos; i++)
684 strcat(dst, "[0-9]?");
688 /* match everything greater than 10^pos */
689 for (i = 0; i <= pos; i++)
690 strcat(dst, "[0-9]");
691 strcat(dst, "[0-9]*");
696 void string_rel_add_char(char **term_p, const char *src, int *indx)
698 if (src[*indx] == '\\')
699 *(*term_p)++ = src[(*indx)++];
700 *(*term_p)++ = src[(*indx)++];
704 * > abc ([b-].*|a[c-].*|ab[d-].*|abc.+)
705 * ([^-a].*|a[^-b].*ab[^-c].*|abc.+)
706 * >= abc ([b-].*|a[c-].*|ab[c-].*)
707 * ([^-a].*|a[^-b].*|ab[c-].*)
708 * < abc ([-0].*|a[-a].*|ab[-b].*)
709 * ([^a-].*|a[^b-].*|ab[^c-].*)
710 * <= abc ([-0].*|a[-a].*|ab[-b].*|abc)
711 * ([^a-].*|a[^b-].*|ab[^c-].*|abc)
713 static int string_relation(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
714 const char **term_sub, char *term_dict,
715 oid_value attributeSet,
716 int reg_type, int space_split, char *term_dst)
721 char *term_tmp = term_dict + strlen(term_dict);
722 char term_component[2*IT_MAX_WORD+20];
724 attr_init(&relation, zapt, 2);
725 relation_value = attr_find(&relation, NULL);
727 yaz_log(YLOG_DEBUG, "string relation value=%d", relation_value);
728 switch (relation_value)
731 if (!term_100 (zh->reg->zebra_maps, reg_type,
732 term_sub, term_component,
733 space_split, term_dst))
735 yaz_log(log_level_rpn, "Relation <");
738 for (i = 0; term_component[i]; )
745 string_rel_add_char (&term_tmp, term_component, &j);
750 string_rel_add_char (&term_tmp, term_component, &i);
757 if ((term_tmp - term_dict) > IT_MAX_WORD)
764 if (!term_100 (zh->reg->zebra_maps, reg_type,
765 term_sub, term_component,
766 space_split, term_dst))
768 yaz_log(log_level_rpn, "Relation <=");
771 for (i = 0; term_component[i]; )
776 string_rel_add_char (&term_tmp, term_component, &j);
780 string_rel_add_char (&term_tmp, term_component, &i);
789 if ((term_tmp - term_dict) > IT_MAX_WORD)
792 for (i = 0; term_component[i]; )
793 string_rel_add_char (&term_tmp, term_component, &i);
798 if (!term_100 (zh->reg->zebra_maps, reg_type,
799 term_sub, term_component, space_split, term_dst))
801 yaz_log(log_level_rpn, "Relation >");
804 for (i = 0; term_component[i];)
809 string_rel_add_char (&term_tmp, term_component, &j);
814 string_rel_add_char (&term_tmp, term_component, &i);
822 if ((term_tmp - term_dict) > IT_MAX_WORD)
825 for (i = 0; term_component[i];)
826 string_rel_add_char (&term_tmp, term_component, &i);
833 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub,
834 term_component, space_split, term_dst))
836 yaz_log(log_level_rpn, "Relation >=");
839 for (i = 0; term_component[i];)
846 string_rel_add_char (&term_tmp, term_component, &j);
849 if (term_component[i+1])
853 string_rel_add_char (&term_tmp, term_component, &i);
857 string_rel_add_char (&term_tmp, term_component, &i);
864 if ((term_tmp - term_dict) > IT_MAX_WORD)
872 yaz_log(log_level_rpn, "Relation =");
873 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub,
874 term_component, space_split, term_dst))
876 strcat(term_tmp, "(");
877 strcat(term_tmp, term_component);
878 strcat(term_tmp, ")");
883 static int string_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
884 const char **term_sub,
885 oid_value attributeSet, NMEM stream,
886 struct grep_info *grep_info,
887 int reg_type, int complete_flag,
888 int num_bases, char **basenames,
889 char *term_dst, int xpath_use);
891 static RSET term_trunc(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
892 const char **term_sub,
893 oid_value attributeSet, NMEM stream,
894 struct grep_info *grep_info,
895 int reg_type, int complete_flag,
896 int num_bases, char **basenames,
898 const char *rank_type, int xpath_use,
902 grep_info->isam_p_indx = 0;
903 r = string_term(zh, zapt, term_sub, attributeSet, stream, grep_info,
904 reg_type, complete_flag, num_bases, basenames,
905 term_dst, xpath_use);
908 yaz_log(log_level_rpn, "term: %s", term_dst);
909 return rset_trunc(zh, grep_info->isam_p_buf,
910 grep_info->isam_p_indx, term_dst,
911 strlen(term_dst), rank_type, 1 /* preserve pos */,
912 zapt->term->which, rset_nmem,
913 key_it_ctrl,key_it_ctrl->scope);
915 static char *nmem_strdup_i(NMEM nmem, int v)
918 sprintf (val_str, "%d", v);
919 return nmem_strdup(nmem, val_str);
922 static int string_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
923 const char **term_sub,
924 oid_value attributeSet, NMEM stream,
925 struct grep_info *grep_info,
926 int reg_type, int complete_flag,
927 int num_bases, char **basenames,
928 char *term_dst, int xpath_use)
930 char term_dict[2*IT_MAX_WORD+4000];
933 int truncation_value;
936 const char *use_string = 0;
937 oid_value curAttributeSet = attributeSet;
939 struct rpn_char_map_info rcmi;
940 int space_split = complete_flag ? 0 : 1;
942 int bases_ok = 0; /* no of databases with OK attribute */
943 int errCode = 0; /* err code (if any is not OK) */
944 char *errString = 0; /* addinfo */
946 rpn_char_map_prepare (zh->reg, reg_type, &rcmi);
947 attr_init (&use, zapt, 1);
948 use_value = attr_find_ex (&use, &curAttributeSet, &use_string);
949 yaz_log(log_level_rpn, "string_term, use value %d", use_value);
950 attr_init (&truncation, zapt, 5);
951 truncation_value = attr_find (&truncation, NULL);
952 yaz_log(log_level_rpn, "truncation value %d", truncation_value);
954 if (use_value == -1) /* no attribute - assumy "any" */
956 for (base_no = 0; base_no < num_bases; base_no++)
962 data1_local_attribute id_xpath_attr;
963 data1_local_attribute *local_attr;
964 int max_pos, prefix_len = 0;
968 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
970 zh->errCode = 109; /* Database unavailable */
971 zh->errString = basenames[base_no];
974 if (xpath_use > 0 && use_value == -2)
976 use_value = xpath_use;
977 attp.local_attributes = &id_xpath_attr;
978 attp.attset_ordinal = VAL_IDXPATH;
979 id_xpath_attr.next = 0;
980 id_xpath_attr.local = use_value;
982 else if (curAttributeSet == VAL_IDXPATH)
984 attp.local_attributes = &id_xpath_attr;
985 attp.attset_ordinal = VAL_IDXPATH;
986 id_xpath_attr.next = 0;
987 id_xpath_attr.local = use_value;
991 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value,
994 yaz_log(YLOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
995 curAttributeSet, use_value, r);
998 /* set was found, but value wasn't defined */
1001 errString = nmem_strdup(stream, use_string);
1003 errString = nmem_strdup_i (stream, use_value);
1008 struct oident oident;
1010 oident.proto = PROTO_Z3950;
1011 oident.oclass = CLASS_ATTSET;
1012 oident.value = curAttributeSet;
1013 oid_ent_to_oid (&oident, oid);
1016 errString = nmem_strdup (stream, oident.desc);
1021 for (local_attr = attp.local_attributes; local_attr;
1022 local_attr = local_attr->next)
1028 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
1033 term_dict[prefix_len++] = '|';
1035 term_dict[prefix_len++] = '(';
1037 ord_len = key_SU_encode (ord, ord_buf);
1038 for (i = 0; i<ord_len; i++)
1040 term_dict[prefix_len++] = 1;
1041 term_dict[prefix_len++] = ord_buf[i];
1050 errString = nmem_strdup_i(stream, use_value);
1056 bases_ok++; /* this has OK attributes */
1060 term_dict[prefix_len++] = ')';
1061 term_dict[prefix_len++] = 1;
1062 term_dict[prefix_len++] = reg_type;
1063 yaz_log(log_level_rpn, "reg_type = %d", term_dict[prefix_len-1]);
1064 term_dict[prefix_len] = '\0';
1066 switch (truncation_value)
1068 case -1: /* not specified */
1069 case 100: /* do not truncate */
1070 if (!string_relation (zh, zapt, &termp, term_dict,
1072 reg_type, space_split, term_dst))
1075 case 1: /* right truncation */
1076 term_dict[j++] = '(';
1077 if (!term_100(zh->reg->zebra_maps, reg_type,
1078 &termp, term_dict + j, space_split, term_dst))
1080 strcat(term_dict, ".*)");
1082 case 2: /* keft truncation */
1083 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1084 if (!term_100(zh->reg->zebra_maps, reg_type,
1085 &termp, term_dict + j, space_split, term_dst))
1087 strcat(term_dict, ")");
1089 case 3: /* left&right truncation */
1090 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1091 if (!term_100(zh->reg->zebra_maps, reg_type,
1092 &termp, term_dict + j, space_split, term_dst))
1094 strcat(term_dict, ".*)");
1096 case 101: /* process # in term */
1097 term_dict[j++] = '(';
1098 if (!term_101(zh->reg->zebra_maps, reg_type,
1099 &termp, term_dict + j, space_split, term_dst))
1101 strcat(term_dict, ")");
1103 case 102: /* Regexp-1 */
1104 term_dict[j++] = '(';
1105 if (!term_102(zh->reg->zebra_maps, reg_type,
1106 &termp, term_dict + j, space_split, term_dst))
1108 strcat(term_dict, ")");
1110 case 103: /* Regexp-2 */
1112 term_dict[j++] = '(';
1114 if (!term_103 (zh->reg->zebra_maps, reg_type,
1115 &termp, term_dict + j, ®ex_range,
1116 space_split, term_dst))
1118 strcat(term_dict, ")");
1119 case 104: /* process # and ! in term */
1120 term_dict[j++] = '(';
1121 if (!term_104 (zh->reg->zebra_maps, reg_type,
1122 &termp, term_dict + j, space_split, term_dst))
1124 strcat(term_dict, ")");
1126 case 105: /* process * and ! in term */
1127 term_dict[j++] = '(';
1128 if (!term_105 (zh->reg->zebra_maps, reg_type,
1129 &termp, term_dict + j, space_split, term_dst, 1))
1131 strcat(term_dict, ")");
1133 case 106: /* process * and ! in term */
1134 term_dict[j++] = '(';
1135 if (!term_105 (zh->reg->zebra_maps, reg_type,
1136 &termp, term_dict + j, space_split, term_dst, 0))
1138 strcat(term_dict, ")");
1142 zh->errString = nmem_strdup_i(stream, truncation_value);
1147 yaz_log(log_level_rpn, "dict_lookup_grep: %s", term_dict+prefix_len);
1148 r = dict_lookup_grep(zh->reg->dict, term_dict, regex_range,
1149 grep_info, &max_pos, init_pos,
1152 yaz_log(YLOG_WARN, "dict_lookup_grep fail %d", r);
1157 zh->errCode = errCode;
1158 zh->errString = errString;
1162 yaz_log(YLOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1167 /* convert APT search term to UTF8 */
1168 static int zapt_term_to_utf8 (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1172 Z_Term *term = zapt->term;
1174 switch (term->which)
1176 case Z_Term_general:
1177 if (zh->iconv_to_utf8 != 0)
1179 char *inbuf = term->u.general->buf;
1180 size_t inleft = term->u.general->len;
1181 char *outbuf = termz;
1182 size_t outleft = IT_MAX_WORD-1;
1185 ret = yaz_iconv(zh->iconv_to_utf8, &inbuf, &inleft,
1187 if (ret == (size_t)(-1))
1189 ret = yaz_iconv(zh->iconv_to_utf8, 0, 0, 0, 0);
1197 sizez = term->u.general->len;
1198 if (sizez > IT_MAX_WORD-1)
1199 sizez = IT_MAX_WORD-1;
1200 memcpy (termz, term->u.general->buf, sizez);
1201 termz[sizez] = '\0';
1204 case Z_Term_characterString:
1205 sizez = strlen(term->u.characterString);
1206 if (sizez > IT_MAX_WORD-1)
1207 sizez = IT_MAX_WORD-1;
1208 memcpy (termz, term->u.characterString, sizez);
1209 termz[sizez] = '\0';
1218 /* convert APT SCAN term to internal cmap */
1219 static int trans_scan_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1220 char *termz, int reg_type)
1222 char termz0[IT_MAX_WORD];
1224 if (zapt_term_to_utf8(zh, zapt, termz0))
1225 return -1; /* error */
1229 const char *cp = (const char *) termz0;
1230 const char *cp_end = cp + strlen(cp);
1233 const char *space_map = NULL;
1236 while ((len = (cp_end - cp)) > 0)
1238 map = zebra_maps_input (zh->reg->zebra_maps, reg_type, &cp, len, 0);
1239 if (**map == *CHR_SPACE)
1244 for (src = space_map; *src; src++)
1247 for (src = *map; *src; src++)
1256 char *normalize_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1257 const char *termz, NMEM stream, unsigned reg_id)
1260 AttrType truncation;
1261 int truncation_value;
1264 attr_init (&truncation, zapt, 5);
1265 truncation_value = attr_find (&truncation, NULL);
1267 switch (truncation_value)
1287 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, ex_list,
1288 termz, strlen(termz));
1290 return nmem_strdup(stream, termz);
1293 char *buf = (char*) nmem_malloc(stream, wrbuf_len(wrbuf)+1);
1294 memcpy (buf, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1295 buf[wrbuf_len(wrbuf)] = '\0';
1300 static void grep_info_delete (struct grep_info *grep_info)
1303 xfree(grep_info->term_no);
1305 xfree (grep_info->isam_p_buf);
1308 static int grep_info_prepare (ZebraHandle zh,
1309 Z_AttributesPlusTerm *zapt,
1310 struct grep_info *grep_info,
1315 int termset_value_numeric;
1316 const char *termset_value_string;
1319 grep_info->term_no = 0;
1321 grep_info->isam_p_size = 0;
1322 grep_info->isam_p_buf = NULL;
1324 grep_info->reg_type = reg_type;
1325 grep_info->termset = 0;
1329 attr_init (&termset, zapt, 8);
1330 termset_value_numeric =
1331 attr_find_ex (&termset, NULL, &termset_value_string);
1332 if (termset_value_numeric != -1)
1335 const char *termset_name = 0;
1336 if (termset_value_numeric != -2)
1339 sprintf (resname, "%d", termset_value_numeric);
1340 termset_name = resname;
1343 termset_name = termset_value_string;
1344 yaz_log(log_level_rpn, "creating termset set %s", termset_name);
1345 grep_info->termset = resultSetAdd (zh, termset_name, 1);
1346 if (!grep_info->termset)
1349 zh->errString = nmem_strdup (stream, termset_name);
1357 static RSET rpn_search_APT_phrase (ZebraHandle zh,
1358 Z_AttributesPlusTerm *zapt,
1359 const char *termz_org,
1360 oid_value attributeSet,
1362 int reg_type, int complete_flag,
1363 const char *rank_type, int xpath_use,
1364 int num_bases, char **basenames,
1367 char term_dst[IT_MAX_WORD+1];
1368 RSET rset[60], result;
1370 struct grep_info grep_info;
1371 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1372 const char *termp = termz;
1375 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1379 yaz_log(log_level_rpn, "APT_phrase termp=%s", termp);
1380 rset[rset_no] = term_trunc(zh, zapt, &termp, attributeSet,
1382 reg_type, complete_flag,
1383 num_bases, basenames,
1384 term_dst, rank_type,
1385 xpath_use,rset_nmem);
1388 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1391 grep_info_delete (&grep_info);
1393 return rsnull_create (rset_nmem,key_it_ctrl);
1394 else if (rset_no == 1)
1397 result = rsprox_create( rset_nmem, key_it_ctrl, key_it_ctrl->scope,
1399 1 /* ordered */, 0 /* exclusion */,
1400 3 /* relation */, 1 /* distance */);
1404 static RSET rpn_search_APT_or_list (ZebraHandle zh,
1405 Z_AttributesPlusTerm *zapt,
1406 const char *termz_org,
1407 oid_value attributeSet,
1409 int reg_type, int complete_flag,
1410 const char *rank_type,
1412 int num_bases, char **basenames,
1415 char term_dst[IT_MAX_WORD+1];
1418 struct grep_info grep_info;
1419 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1420 const char *termp = termz;
1422 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1426 yaz_log(log_level_rpn, "APT_or_list termp=%s", termp);
1427 rset[rset_no] = term_trunc(zh, zapt, &termp, attributeSet,
1429 reg_type, complete_flag,
1430 num_bases, basenames,
1431 term_dst, rank_type,
1432 xpath_use,rset_nmem);
1435 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1438 grep_info_delete (&grep_info);
1440 return rsnull_create (rset_nmem,key_it_ctrl);
1441 return rsmultior_create(rset_nmem, key_it_ctrl,key_it_ctrl->scope,
1445 static RSET rpn_search_APT_and_list (ZebraHandle zh,
1446 Z_AttributesPlusTerm *zapt,
1447 const char *termz_org,
1448 oid_value attributeSet,
1450 int reg_type, int complete_flag,
1451 const char *rank_type,
1453 int num_bases, char **basenames,
1456 char term_dst[IT_MAX_WORD+1];
1457 RSET rset[60]; /* FIXME - bug 160 - should be dynamic somehow */
1459 struct grep_info grep_info;
1460 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1461 const char *termp = termz;
1463 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1467 yaz_log(log_level_rpn, "APT_and_list termp=%s", termp);
1468 rset[rset_no] = term_trunc(zh, zapt, &termp, attributeSet,
1470 reg_type, complete_flag,
1471 num_bases, basenames,
1472 term_dst, rank_type,
1473 xpath_use, rset_nmem);
1476 assert (rset[rset_no]);
1477 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1480 grep_info_delete (&grep_info);
1482 return rsnull_create (rset_nmem,key_it_ctrl);
1484 return rsmultiand_create( rset_nmem, key_it_ctrl, key_it_ctrl->scope,
1488 static int numeric_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1489 const char **term_sub,
1491 oid_value attributeSet,
1492 struct grep_info *grep_info,
1501 char *term_tmp = term_dict + strlen(term_dict);
1503 attr_init (&relation, zapt, 2);
1504 relation_value = attr_find (&relation, NULL);
1506 yaz_log(log_level_rpn, "numeric relation value=%d", relation_value);
1508 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub, term_tmp, 1,
1511 term_value = atoi (term_tmp);
1512 switch (relation_value)
1515 yaz_log(log_level_rpn, "Relation <");
1516 gen_regular_rel (term_tmp, term_value-1, 1);
1519 yaz_log(log_level_rpn, "Relation <=");
1520 gen_regular_rel (term_tmp, term_value, 1);
1523 yaz_log(log_level_rpn, "Relation >=");
1524 gen_regular_rel (term_tmp, term_value, 0);
1527 yaz_log(log_level_rpn, "Relation >");
1528 gen_regular_rel (term_tmp, term_value+1, 0);
1532 yaz_log(log_level_rpn, "Relation =");
1533 sprintf (term_tmp, "(0*%d)", term_value);
1535 yaz_log(log_level_rpn, "dict_lookup_grep: %s", term_tmp);
1536 r = dict_lookup_grep(zh->reg->dict, term_dict, 0, grep_info, max_pos,
1539 yaz_log(YLOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1540 yaz_log(log_level_rpn, "%d positions", grep_info->isam_p_indx);
1544 static int numeric_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1545 const char **term_sub,
1546 oid_value attributeSet, struct grep_info *grep_info,
1547 int reg_type, int complete_flag,
1548 int num_bases, char **basenames,
1549 char *term_dst, int xpath_use, NMEM stream)
1551 char term_dict[2*IT_MAX_WORD+2];
1555 const char *use_string = 0;
1556 oid_value curAttributeSet = attributeSet;
1558 struct rpn_char_map_info rcmi;
1560 int bases_ok = 0; /* no of databases with OK attribute */
1561 int errCode = 0; /* err code (if any is not OK) */
1562 char *errString = 0; /* addinfo */
1564 rpn_char_map_prepare (zh->reg, reg_type, &rcmi);
1565 attr_init (&use, zapt, 1);
1566 use_value = attr_find_ex (&use, &curAttributeSet, &use_string);
1568 if (use_value == -1)
1571 for (base_no = 0; base_no < num_bases; base_no++)
1574 data1_local_attribute id_xpath_attr;
1575 data1_local_attribute *local_attr;
1576 int max_pos, prefix_len = 0;
1579 if (use_value == -2) /* string attribute (assume IDXPATH/any) */
1581 use_value = xpath_use;
1582 attp.local_attributes = &id_xpath_attr;
1583 attp.attset_ordinal = VAL_IDXPATH;
1584 id_xpath_attr.next = 0;
1585 id_xpath_attr.local = use_value;
1587 else if (curAttributeSet == VAL_IDXPATH)
1589 attp.local_attributes = &id_xpath_attr;
1590 attp.attset_ordinal = VAL_IDXPATH;
1591 id_xpath_attr.next = 0;
1592 id_xpath_attr.local = use_value;
1596 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value,
1599 yaz_log(YLOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1600 curAttributeSet, use_value, r);
1603 errString = nmem_strdup_i(stream, use_value);
1611 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
1613 zh->errCode = 109; /* Database unavailable */
1614 zh->errString = basenames[base_no];
1617 for (local_attr = attp.local_attributes; local_attr;
1618 local_attr = local_attr->next)
1624 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
1629 term_dict[prefix_len++] = '|';
1631 term_dict[prefix_len++] = '(';
1633 ord_len = key_SU_encode (ord, ord_buf);
1634 for (i = 0; i<ord_len; i++)
1636 term_dict[prefix_len++] = 1;
1637 term_dict[prefix_len++] = ord_buf[i];
1643 errString = nmem_strdup_i(stream, use_value);
1647 term_dict[prefix_len++] = ')';
1648 term_dict[prefix_len++] = 1;
1649 term_dict[prefix_len++] = reg_type;
1650 yaz_log(YLOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1651 term_dict[prefix_len] = '\0';
1652 if (!numeric_relation (zh, zapt, &termp, term_dict,
1653 attributeSet, grep_info, &max_pos, reg_type,
1659 zh->errCode = errCode;
1660 zh->errString = errString;
1664 yaz_log(YLOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1668 static RSET rpn_search_APT_numeric (ZebraHandle zh,
1669 Z_AttributesPlusTerm *zapt,
1671 oid_value attributeSet,
1673 int reg_type, int complete_flag,
1674 const char *rank_type, int xpath_use,
1675 int num_bases, char **basenames,
1678 char term_dst[IT_MAX_WORD+1];
1679 const char *termp = termz;
1680 RSET rset[60]; /* FIXME - hard-coded magic number */
1682 struct grep_info grep_info;
1684 yaz_log(log_level_rpn, "APT_numeric t='%s'",termz);
1685 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1689 yaz_log(YLOG_DEBUG, "APT_numeric termp=%s", termp);
1690 grep_info.isam_p_indx = 0;
1691 r = numeric_term(zh, zapt, &termp, attributeSet, &grep_info,
1692 reg_type, complete_flag, num_bases, basenames,
1693 term_dst, xpath_use,
1697 yaz_log(YLOG_DEBUG, "term: %s", term_dst);
1698 rset[rset_no] = rset_trunc(zh, grep_info.isam_p_buf,
1699 grep_info.isam_p_indx, term_dst,
1700 strlen(term_dst), rank_type,
1701 0 /* preserve position */,
1702 zapt->term->which, rset_nmem,
1703 key_it_ctrl,key_it_ctrl->scope);
1704 assert (rset[rset_no]);
1705 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1708 grep_info_delete (&grep_info);
1710 return rsnull_create (rset_nmem,key_it_ctrl);
1713 return rsmultiand_create(rset_nmem,key_it_ctrl,key_it_ctrl->scope,
1717 static RSET rpn_search_APT_local (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1719 oid_value attributeSet,
1721 const char *rank_type, NMEM rset_nmem)
1727 result = rstemp_create( rset_nmem,key_it_ctrl,key_it_ctrl->scope,
1728 res_get (zh->res, "setTmpDir"),0 );
1729 rsfd = rset_open (result, RSETF_WRITE);
1737 rset_write (rsfd, &key);
1742 static RSET rpn_sort_spec (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1743 oid_value attributeSet, NMEM stream,
1744 Z_SortKeySpecList *sort_sequence,
1745 const char *rank_type)
1748 int sort_relation_value;
1749 AttrType sort_relation_type;
1754 Z_AttributeElement *ae;
1759 attr_init (&sort_relation_type, zapt, 7);
1760 sort_relation_value = attr_find (&sort_relation_type, &attributeSet);
1762 attr_init (&use_type, zapt, 1);
1763 use_value = attr_find (&use_type, &attributeSet);
1765 if (!sort_sequence->specs)
1767 sort_sequence->num_specs = 10;
1768 sort_sequence->specs = (Z_SortKeySpec **)
1769 nmem_malloc(stream, sort_sequence->num_specs *
1770 sizeof(*sort_sequence->specs));
1771 for (i = 0; i<sort_sequence->num_specs; i++)
1772 sort_sequence->specs[i] = 0;
1774 if (zapt->term->which != Z_Term_general)
1777 i = atoi_n ((char *) zapt->term->u.general->buf,
1778 zapt->term->u.general->len);
1779 if (i >= sort_sequence->num_specs)
1781 sprintf (termz, "%d", i);
1783 oe.proto = PROTO_Z3950;
1784 oe.oclass = CLASS_ATTSET;
1785 oe.value = attributeSet;
1786 if (!oid_ent_to_oid (&oe, oid))
1789 sks = (Z_SortKeySpec *) nmem_malloc(stream, sizeof(*sks));
1790 sks->sortElement = (Z_SortElement *)
1791 nmem_malloc(stream, sizeof(*sks->sortElement));
1792 sks->sortElement->which = Z_SortElement_generic;
1793 sk = sks->sortElement->u.generic = (Z_SortKey *)
1794 nmem_malloc(stream, sizeof(*sk));
1795 sk->which = Z_SortKey_sortAttributes;
1796 sk->u.sortAttributes = (Z_SortAttributes *)
1797 nmem_malloc(stream, sizeof(*sk->u.sortAttributes));
1799 sk->u.sortAttributes->id = oid;
1800 sk->u.sortAttributes->list = (Z_AttributeList *)
1801 nmem_malloc(stream, sizeof(*sk->u.sortAttributes->list));
1802 sk->u.sortAttributes->list->num_attributes = 1;
1803 sk->u.sortAttributes->list->attributes = (Z_AttributeElement **)
1804 nmem_malloc(stream, sizeof(*sk->u.sortAttributes->list->attributes));
1805 ae = *sk->u.sortAttributes->list->attributes = (Z_AttributeElement *)
1806 nmem_malloc(stream, sizeof(**sk->u.sortAttributes->list->attributes));
1807 ae->attributeSet = 0;
1808 ae->attributeType = (int *)
1809 nmem_malloc(stream, sizeof(*ae->attributeType));
1810 *ae->attributeType = 1;
1811 ae->which = Z_AttributeValue_numeric;
1812 ae->value.numeric = (int *)
1813 nmem_malloc(stream, sizeof(*ae->value.numeric));
1814 *ae->value.numeric = use_value;
1816 sks->sortRelation = (int *)
1817 nmem_malloc(stream, sizeof(*sks->sortRelation));
1818 if (sort_relation_value == 1)
1819 *sks->sortRelation = Z_SortKeySpec_ascending;
1820 else if (sort_relation_value == 2)
1821 *sks->sortRelation = Z_SortKeySpec_descending;
1823 *sks->sortRelation = Z_SortKeySpec_ascending;
1825 sks->caseSensitivity = (int *)
1826 nmem_malloc(stream, sizeof(*sks->caseSensitivity));
1827 *sks->caseSensitivity = 0;
1829 sks->which = Z_SortKeySpec_null;
1830 sks->u.null = odr_nullval ();
1831 sort_sequence->specs[i] = sks;
1832 return rsnull_create (NULL,key_it_ctrl);
1833 /* FIXME - nmem?? */
1837 static int parse_xpath(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1838 oid_value attributeSet,
1839 struct xpath_location_step *xpath, int max, NMEM mem)
1841 oid_value curAttributeSet = attributeSet;
1843 const char *use_string = 0;
1845 attr_init (&use, zapt, 1);
1846 attr_find_ex (&use, &curAttributeSet, &use_string);
1848 if (!use_string || *use_string != '/')
1851 return zebra_parse_xpath_str(use_string, xpath, max, mem);
1856 static RSET xpath_trunc(ZebraHandle zh, NMEM stream,
1857 int reg_type, const char *term, int use,
1858 oid_value curAttributeSet, NMEM rset_nmem)
1861 struct grep_info grep_info;
1862 char term_dict[2048];
1865 int ord = zebraExplain_lookupSU (zh->reg->zei, curAttributeSet, use);
1866 int ord_len, i, r, max_pos;
1867 int term_type = Z_Term_characterString;
1868 const char *flags = "void";
1870 if (grep_info_prepare (zh, 0 /* zapt */, &grep_info, '0', stream))
1871 return rsnull_create (rset_nmem,key_it_ctrl);
1874 return rsnull_create (rset_nmem,key_it_ctrl);
1876 term_dict[prefix_len++] = '|';
1878 term_dict[prefix_len++] = '(';
1880 ord_len = key_SU_encode (ord, ord_buf);
1881 for (i = 0; i<ord_len; i++)
1883 term_dict[prefix_len++] = 1;
1884 term_dict[prefix_len++] = ord_buf[i];
1886 term_dict[prefix_len++] = ')';
1887 term_dict[prefix_len++] = 1;
1888 term_dict[prefix_len++] = reg_type;
1890 strcpy(term_dict+prefix_len, term);
1892 grep_info.isam_p_indx = 0;
1893 r = dict_lookup_grep(zh->reg->dict, term_dict, 0,
1894 &grep_info, &max_pos, 0, grep_handle);
1895 yaz_log (YLOG_LOG, "%s %d positions", term,
1896 grep_info.isam_p_indx);
1897 rset = rset_trunc(zh, grep_info.isam_p_buf,
1898 grep_info.isam_p_indx, term, strlen(term),
1899 flags, 1, term_type,rset_nmem,
1900 key_it_ctrl, key_it_ctrl->scope);
1901 grep_info_delete (&grep_info);
1905 static RSET rpn_search_xpath (ZebraHandle zh,
1906 oid_value attributeSet,
1907 int num_bases, char **basenames,
1908 NMEM stream, const char *rank_type, RSET rset,
1909 int xpath_len, struct xpath_location_step *xpath,
1912 oid_value curAttributeSet = attributeSet;
1919 yaz_log (YLOG_DEBUG, "xpath len=%d", xpath_len);
1920 for (i = 0; i<xpath_len; i++)
1922 yaz_log (log_level_rpn, "XPATH %d %s", i, xpath[i].part);
1926 curAttributeSet = VAL_IDXPATH;
1936 a[@attr=value]/b[@other=othervalue]
1938 /e/@a val range(e/,range(@a,freetext(w,1015,val),@a),e/)
1939 /a/b val range(b/a/,freetext(w,1016,val),b/a/)
1940 /a/b/@c val range(b/a/,range(@c,freetext(w,1016,val),@c),b/a/)
1941 /a/b[@c=y] val range(b/a/,freetext(w,1016,val),b/a/,@c=y)
1942 /a[@c=y]/b val range(a/,range(b/a/,freetext(w,1016,val),b/a/),a/,@c=y)
1943 /a[@c=x]/b[@c=y] range(a/,range(b/a/,freetext(w,1016,val),b/a/,@c=y),a/,@c=x)
1947 dict_grep_cmap (zh->reg->dict, 0, 0);
1949 for (base_no = 0; base_no < num_bases; base_no++)
1951 int level = xpath_len;
1954 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
1956 zh->errCode = 109; /* Database unavailable */
1957 zh->errString = basenames[base_no];
1960 while (--level >= 0)
1962 char xpath_rev[128];
1964 RSET rset_start_tag = 0, rset_end_tag = 0, rset_attr = 0;
1968 for (i = level; i >= 1; --i)
1970 const char *cp = xpath[i].part;
1976 memcpy (xpath_rev + len, "[^/]*", 5);
1979 else if (*cp == ' ')
1982 xpath_rev[len++] = 1;
1983 xpath_rev[len++] = ' ';
1987 xpath_rev[len++] = *cp;
1988 xpath_rev[len++] = '/';
1990 else if (i == 1) /* // case */
1992 xpath_rev[len++] = '.';
1993 xpath_rev[len++] = '*';
1998 if (xpath[level].predicate &&
1999 xpath[level].predicate->which == XPATH_PREDICATE_RELATION &&
2000 xpath[level].predicate->u.relation.name[0])
2002 WRBUF wbuf = wrbuf_alloc();
2003 wrbuf_puts(wbuf, xpath[level].predicate->u.relation.name+1);
2004 if (xpath[level].predicate->u.relation.value)
2006 const char *cp = xpath[level].predicate->u.relation.value;
2007 wrbuf_putc(wbuf, '=');
2011 if (strchr(REGEX_CHARS, *cp))
2012 wrbuf_putc(wbuf, '\\');
2013 wrbuf_putc(wbuf, *cp);
2017 wrbuf_puts(wbuf, "");
2018 rset_attr = xpath_trunc(
2019 zh, stream, '0', wrbuf_buf(wbuf), 3,
2020 curAttributeSet,rset_nmem);
2021 wrbuf_free(wbuf, 1);
2028 yaz_log (log_level_rpn, "xpath_rev (%d) = %s", level, xpath_rev);
2029 if (strlen(xpath_rev))
2031 rset_start_tag = xpath_trunc(zh, stream, '0',
2032 xpath_rev, 1, curAttributeSet, rset_nmem);
2034 rset_end_tag = xpath_trunc(zh, stream, '0',
2035 xpath_rev, 2, curAttributeSet, rset_nmem);
2038 parms.key_size = sizeof(struct it_key);
2039 parms.cmp = key_compare_it;
2040 parms.rset_l = rset_start_tag;
2041 parms.rset_m = rset;
2042 parms.rset_r = rset_end_tag;
2043 parms.rset_attr = rset_attr;
2044 parms.printer = key_print_it;
2045 rset = rset_create (rset_kind_between, &parms);
2047 rset=rsbetween_create( rset_nmem,key_it_ctrl,key_it_ctrl->scope,
2048 rset_start_tag, rset, rset_end_tag, rset_attr);
2059 static RSET rpn_search_APT (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2060 oid_value attributeSet, NMEM stream,
2061 Z_SortKeySpecList *sort_sequence,
2062 int num_bases, char **basenames,
2066 char *search_type = NULL;
2067 char rank_type[128];
2070 char termz[IT_MAX_WORD+1];
2074 struct xpath_location_step xpath[10];
2078 log_level_rpn = yaz_log_module_level("rpn");
2081 zebra_maps_attr (zh->reg->zebra_maps, zapt, ®_id, &search_type,
2082 rank_type, &complete_flag, &sort_flag);
2084 yaz_log(YLOG_DEBUG, "reg_id=%c", reg_id);
2085 yaz_log(YLOG_DEBUG, "complete_flag=%d", complete_flag);
2086 yaz_log(YLOG_DEBUG, "search_type=%s", search_type);
2087 yaz_log(YLOG_DEBUG, "rank_type=%s", rank_type);
2089 if (zapt_term_to_utf8(zh, zapt, termz))
2093 return rpn_sort_spec (zh, zapt, attributeSet, stream, sort_sequence,
2095 xpath_len = parse_xpath(zh, zapt, attributeSet, xpath, 10, stream);
2099 if (xpath[xpath_len-1].part[0] == '@')
2103 if (!strcmp (search_type, "phrase"))
2105 rset = rpn_search_APT_phrase (zh, zapt, termz, attributeSet, stream,
2106 reg_id, complete_flag, rank_type,
2108 num_bases, basenames, rset_nmem);
2110 else if (!strcmp (search_type, "and-list"))
2112 rset = rpn_search_APT_and_list (zh, zapt, termz, attributeSet, stream,
2113 reg_id, complete_flag, rank_type,
2115 num_bases, basenames, rset_nmem);
2117 else if (!strcmp (search_type, "or-list"))
2119 rset = rpn_search_APT_or_list (zh, zapt, termz, attributeSet, stream,
2120 reg_id, complete_flag, rank_type,
2122 num_bases, basenames, rset_nmem);
2124 else if (!strcmp (search_type, "local"))
2126 rset = rpn_search_APT_local (zh, zapt, termz, attributeSet, stream,
2127 rank_type, rset_nmem);
2129 else if (!strcmp (search_type, "numeric"))
2131 rset = rpn_search_APT_numeric (zh, zapt, termz, attributeSet, stream,
2132 reg_id, complete_flag, rank_type,
2134 num_bases, basenames, rset_nmem);
2136 else if (!strcmp (search_type, "always"))
2142 return rpn_search_xpath (zh, attributeSet, num_bases, basenames,
2143 stream, rank_type, rset,
2144 xpath_len, xpath, rset_nmem);
2147 static RSET rpn_search_structure (ZebraHandle zh, Z_RPNStructure *zs,
2148 oid_value attributeSet,
2149 NMEM stream, NMEM rset_nmem,
2150 Z_SortKeySpecList *sort_sequence,
2151 int num_bases, char **basenames)
2154 if (zs->which == Z_RPNStructure_complex)
2156 Z_Operator *zop = zs->u.complex->roperator;
2157 RSET rsets[2]; /* l and r argument */
2159 rsets[0]=rpn_search_structure (zh, zs->u.complex->s1,
2160 attributeSet, stream, rset_nmem,
2162 num_bases, basenames);
2163 if (rsets[0] == NULL)
2165 rsets[1]=rpn_search_structure (zh, zs->u.complex->s2,
2166 attributeSet, stream, rset_nmem,
2168 num_bases, basenames);
2169 if (rsets[1] == NULL)
2171 rset_delete (rsets[0]);
2177 case Z_Operator_and:
2178 r=rsmultiand_create(rset_nmem, key_it_ctrl, key_it_ctrl->scope,
2182 r=rsmultior_create(rset_nmem, key_it_ctrl, key_it_ctrl->scope,
2185 case Z_Operator_and_not:
2186 r = rsbool_create_not(rset_nmem,key_it_ctrl, key_it_ctrl->scope,
2189 case Z_Operator_prox:
2190 if (zop->u.prox->which != Z_ProximityOperator_known)
2195 if (*zop->u.prox->u.known != Z_ProxUnit_word)
2197 char *val = (char *) nmem_malloc(stream, 16);
2199 zh->errString = val;
2200 sprintf (val, "%d", *zop->u.prox->u.known);
2205 /* new / old prox */
2206 r=rsprox_create(rset_nmem,key_it_ctrl,key_it_ctrl->scope,
2208 *zop->u.prox->ordered,
2209 (!zop->u.prox->exclusion ?
2210 0 : *zop->u.prox->exclusion),
2211 *zop->u.prox->relationType,
2212 *zop->u.prox->distance );
2220 else if (zs->which == Z_RPNStructure_simple)
2222 if (zs->u.simple->which == Z_Operand_APT)
2224 yaz_log(YLOG_DEBUG, "rpn_search_APT");
2225 r = rpn_search_APT (zh, zs->u.simple->u.attributesPlusTerm,
2226 attributeSet, stream, sort_sequence,
2227 num_bases, basenames,rset_nmem);
2229 else if (zs->u.simple->which == Z_Operand_resultSetId)
2231 yaz_log(YLOG_DEBUG, "rpn_search_ref");
2232 r = resultSetRef (zh, zs->u.simple->u.resultSetId);
2235 r = rsnull_create (rset_nmem,key_it_ctrl);
2238 nmem_strdup (stream, zs->u.simple->u.resultSetId);
2259 RSET rpn_search(ZebraHandle zh, NMEM nmem, NMEM rset_nmem,
2260 Z_RPNQuery *rpn, int num_bases, char **basenames,
2261 const char *setname,
2266 oid_value attributeSet;
2267 Z_SortKeySpecList *sort_sequence;
2271 zh->errString = NULL;
2274 sort_sequence = (Z_SortKeySpecList *)
2275 nmem_malloc(nmem, sizeof(*sort_sequence));
2276 sort_sequence->num_specs = 10; /* FIXME - Hard-coded number */
2277 sort_sequence->specs = (Z_SortKeySpec **)
2278 nmem_malloc(nmem, sort_sequence->num_specs *
2279 sizeof(*sort_sequence->specs));
2280 for (i = 0; i<sort_sequence->num_specs; i++)
2281 sort_sequence->specs[i] = 0;
2283 attrset = oid_getentbyoid (rpn->attributeSetId);
2284 attributeSet = attrset->value;
2285 rset = rpn_search_structure (zh, rpn->RPNStructure, attributeSet,
2287 sort_sequence, num_bases, basenames);
2292 yaz_log(YLOG_DEBUG, "search error: %d", zh->errCode);
2294 for (i = 0; sort_sequence->specs[i]; i++)
2296 sort_sequence->num_specs = i;
2298 resultSetRank (zh, sset, rset, rset_nmem);
2301 yaz_log(YLOG_DEBUG, "resultSetSortSingle in rpn_search");
2302 resultSetSortSingle (zh, nmem, sset, rset,
2303 sort_sequence, &sort_status);
2306 yaz_log(YLOG_DEBUG, "resultSetSortSingle status = %d", zh->errCode);
2312 struct scan_info_entry {
2318 struct scan_info_entry *list;
2324 static int scan_handle (char *name, const char *info, int pos, void *client)
2326 int len_prefix, idx;
2327 struct scan_info *scan_info = (struct scan_info *) client;
2329 len_prefix = strlen(scan_info->prefix);
2330 if (memcmp (name, scan_info->prefix, len_prefix))
2332 if (pos > 0) idx = scan_info->after - pos + scan_info->before;
2335 scan_info->list[idx].term = (char *)
2336 odr_malloc(scan_info->odr, strlen(name + len_prefix)+1);
2337 strcpy(scan_info->list[idx].term, name + len_prefix);
2338 assert (*info == sizeof(ISAMC_P));
2339 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAMC_P));
2343 static void scan_term_untrans (ZebraHandle zh, NMEM stream, int reg_type,
2344 char **dst, const char *src)
2346 char term_src[IT_MAX_WORD];
2347 char term_dst[IT_MAX_WORD];
2349 term_untrans (zh, reg_type, term_src, src);
2351 if (zh->iconv_from_utf8 != 0)
2354 char *inbuf = term_src;
2355 size_t inleft = strlen(term_src);
2356 char *outbuf = term_dst;
2357 size_t outleft = sizeof(term_dst)-1;
2360 ret = yaz_iconv (zh->iconv_from_utf8, &inbuf, &inleft,
2362 if (ret == (size_t)(-1))
2365 len = outbuf - term_dst;
2366 *dst = nmem_malloc(stream, len + 1);
2368 memcpy (*dst, term_dst, len);
2372 *dst = nmem_strdup(stream, term_src);
2375 static void count_set (RSET r, int *count)
2382 yaz_log(YLOG_DEBUG, "count_set");
2385 rfd = rset_open (r, RSETF_READ);
2386 while (rset_read (rfd, &key,0 /* never mind terms */))
2388 if (key.mem[0] != psysno)
2390 psysno = key.mem[0];
2396 yaz_log(YLOG_DEBUG, "%d keys, %d records", kno, *count);
2399 void rpn_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
2400 oid_value attributeset,
2401 int num_bases, char **basenames,
2402 int *position, int *num_entries, ZebraScanEntry **list,
2403 int *is_partial, RSET limit_set, int return_zero)
2406 int pos = *position;
2407 int num = *num_entries;
2411 char termz[IT_MAX_WORD+20];
2414 const char *use_string = 0;
2415 struct scan_info *scan_info_array;
2416 ZebraScanEntry *glist;
2417 int ords[32], ord_no = 0;
2420 int bases_ok = 0; /* no of databases with OK attribute */
2421 int errCode = 0; /* err code (if any is not OK) */
2422 char *errString = 0; /* addinfo */
2425 char *search_type = NULL;
2426 char rank_type[128];
2429 NMEM rset_nmem=NULL;
2433 if (attributeset == VAL_NONE)
2434 attributeset = VAL_BIB1;
2439 int termset_value_numeric;
2440 const char *termset_value_string;
2441 attr_init (&termset, zapt, 8);
2442 termset_value_numeric =
2443 attr_find_ex (&termset, NULL, &termset_value_string);
2444 if (termset_value_numeric != -1)
2447 const char *termset_name = 0;
2449 if (termset_value_numeric != -2)
2452 sprintf (resname, "%d", termset_value_numeric);
2453 termset_name = resname;
2456 termset_name = termset_value_string;
2458 limit_set = resultSetRef (zh, termset_name);
2462 yaz_log (YLOG_DEBUG, "position = %d, num = %d set=%d",
2463 pos, num, attributeset);
2465 attr_init (&use, zapt, 1);
2466 use_value = attr_find_ex (&use, &attributeset, &use_string);
2468 if (zebra_maps_attr (zh->reg->zebra_maps, zapt, ®_id, &search_type,
2469 rank_type, &complete_flag, &sort_flag))
2475 yaz_log (YLOG_DEBUG, "use_value = %d", use_value);
2477 if (use_value == -1)
2479 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
2483 data1_local_attribute *local_attr;
2485 if ((r=att_getentbyatt (zh, &attp, attributeset, use_value,
2488 yaz_log(YLOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
2489 attributeset, use_value);
2493 sprintf (val_str, "%d", use_value);
2495 errString = odr_strdup (stream, val_str);
2501 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
2503 zh->errString = basenames[base_no];
2504 zh->errCode = 109; /* Database unavailable */
2509 for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
2510 local_attr = local_attr->next)
2514 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
2517 ords[ord_no++] = ord;
2520 if (!bases_ok && errCode)
2522 zh->errCode = errCode;
2523 zh->errString = errString;
2531 /* prepare dictionary scanning */
2534 scan_info_array = (struct scan_info *)
2535 odr_malloc(stream, ord_no * sizeof(*scan_info_array));
2536 for (i = 0; i < ord_no; i++)
2538 int j, prefix_len = 0;
2539 int before_tmp = before, after_tmp = after;
2540 struct scan_info *scan_info = scan_info_array + i;
2541 struct rpn_char_map_info rcmi;
2543 rpn_char_map_prepare (zh->reg, reg_id, &rcmi);
2545 scan_info->before = before;
2546 scan_info->after = after;
2547 scan_info->odr = stream;
2549 scan_info->list = (struct scan_info_entry *)
2550 odr_malloc(stream, (before+after) * sizeof(*scan_info->list));
2551 for (j = 0; j<before+after; j++)
2552 scan_info->list[j].term = NULL;
2554 prefix_len += key_SU_encode (ords[i], termz + prefix_len);
2555 termz[prefix_len++] = reg_id;
2556 termz[prefix_len] = 0;
2557 strcpy(scan_info->prefix, termz);
2559 if (trans_scan_term(zh, zapt, termz+prefix_len, reg_id))
2562 dict_scan(zh->reg->dict, termz, &before_tmp, &after_tmp,
2563 scan_info, scan_handle);
2565 glist = (ZebraScanEntry *)
2566 odr_malloc(stream, (before+after)*sizeof(*glist));
2568 rset_nmem = nmem_create();
2570 /* consider terms after main term */
2571 for (i = 0; i < ord_no; i++)
2575 for (i = 0; i<after; i++)
2578 const char *mterm = NULL;
2582 for (j = 0; j < ord_no; j++)
2584 if (ptr[j] < before+after &&
2585 (tst=scan_info_array[j].list[ptr[j]].term) &&
2586 (!mterm || strcmp (tst, mterm) < 0))
2594 scan_term_untrans (zh, stream->mem, reg_id,
2595 &glist[i+before].term, mterm);
2596 rset = rset_trunc(zh, &scan_info_array[j0].list[ptr[j0]].isam_p, 1,
2597 glist[i+before].term, strlen(glist[i+before].term),
2598 NULL, 0, zapt->term->which, rset_nmem,
2599 key_it_ctrl,key_it_ctrl->scope);
2601 for (j = j0+1; j<ord_no; j++)
2603 if (ptr[j] < before+after &&
2604 (tst=scan_info_array[j].list[ptr[j]].term) &&
2605 !strcmp (tst, mterm))
2610 rset_trunc(zh, &scan_info_array[j].list[ptr[j]].isam_p, 1,
2611 glist[i+before].term,
2612 strlen(glist[i+before].term), NULL, 0,
2613 zapt->term->which,rset_nmem,
2614 key_it_ctrl, key_it_ctrl->scope);
2615 rset = rsbool_create_or(rset_nmem,key_it_ctrl,
2616 key_it_ctrl->scope, rset, rset2);
2617 /* FIXME - Use a proper multi-or */
2623 rset = rsbool_create_and(rset_nmem,key_it_ctrl,key_it_ctrl->scope,
2624 rset, rset_dup(limit_set));
2625 count_set (rset, &glist[i+before].occurrences);
2630 *num_entries -= (after-i);
2634 /* consider terms before main term */
2635 for (i = 0; i<ord_no; i++)
2638 for (i = 0; i<before; i++)
2641 const char *mterm = NULL;
2645 for (j = 0; j <ord_no; j++)
2647 if (ptr[j] < before &&
2648 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2649 (!mterm || strcmp (tst, mterm) > 0))
2658 scan_term_untrans (zh, stream->mem, reg_id,
2659 &glist[before-1-i].term, mterm);
2662 (zh, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1,
2663 glist[before-1-i].term, strlen(glist[before-1-i].term),
2664 NULL, 0, zapt->term->which,rset_nmem,
2665 key_it_ctrl,key_it_ctrl->scope);
2669 for (j = j0+1; j<ord_no; j++)
2671 if (ptr[j] < before &&
2672 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2673 !strcmp (tst, mterm))
2679 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1,
2680 glist[before-1-i].term,
2681 strlen(glist[before-1-i].term), NULL, 0,
2682 zapt->term->which, rset_nmem,
2683 key_it_ctrl, key_it_ctrl->scope);
2684 rset = rsbool_create_and(rset_nmem,key_it_ctrl,
2685 key_it_ctrl->scope, rset, rset2);
2686 /* FIXME - multi-and ?? */
2691 rset = rsbool_create_and(rset_nmem,key_it_ctrl,key_it_ctrl->scope,
2692 rset, rset_dup(limit_set));
2693 count_set (rset, &glist[before-1-i].occurrences);
2704 nmem_destroy(rset_nmem);
2705 *list = glist + i; /* list is set to first 'real' entry */
2707 yaz_log(YLOG_DEBUG, "position = %d, num_entries = %d",
2708 *position, *num_entries);
2710 yaz_log(YLOG_DEBUG, "scan error: %d", zh->errCode);