1 /* $Id: zrpn.c,v 1.170 2005-03-05 09:19:15 adam Exp $
2 Copyright (C) 1995-2005
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
34 #include <zebra_xpath.h>
39 /* maximum number of terms in an and/or/phrase item */
40 #define TERM_LIST_LENGTH_MAX 256
42 static const struct key_control it_ctrl =
44 sizeof(struct it_key),
45 2, /* we have sysnos and seqnos in this key, nothing more */
47 key_logdump_txt, /* FIXME - clean up these functions */
52 const struct key_control *key_it_ctrl = &it_ctrl;
54 struct rpn_char_map_info
65 Z_AttributesPlusTerm *zapt;
69 static int log_level_set = 0;
70 static int log_level_rpn = 0;
72 static const char **rpn_char_map_handler(void *vp, const char **from, int len)
74 struct rpn_char_map_info *p = (struct rpn_char_map_info *) vp;
75 const char **out = zebra_maps_input(p->zm, p->reg_type, from, len, 0);
79 const char *outp = *out;
80 yaz_log(YLOG_LOG, "---");
83 yaz_log(YLOG_LOG, "%02X", *outp);
91 static void rpn_char_map_prepare(struct zebra_register *reg, int reg_type,
92 struct rpn_char_map_info *map_info)
94 map_info->zm = reg->zebra_maps;
95 map_info->reg_type = reg_type;
96 dict_grep_cmap(reg->dict, map_info, rpn_char_map_handler);
99 static int attr_find_ex(AttrType *src, oid_value *attributeSetP,
100 const char **string_value)
104 num_attributes = src->zapt->attributes->num_attributes;
105 while (src->major < num_attributes)
107 Z_AttributeElement *element;
109 element = src->zapt->attributes->attributes[src->major];
110 if (src->type == *element->attributeType)
112 switch (element->which)
114 case Z_AttributeValue_numeric:
116 if (element->attributeSet && attributeSetP)
120 attrset = oid_getentbyoid(element->attributeSet);
121 *attributeSetP = attrset->value;
123 return *element->value.numeric;
125 case Z_AttributeValue_complex:
126 if (src->minor >= element->value.complex->num_list)
128 if (element->attributeSet && attributeSetP)
132 attrset = oid_getentbyoid(element->attributeSet);
133 *attributeSetP = attrset->value;
135 if (element->value.complex->list[src->minor]->which ==
136 Z_StringOrNumeric_numeric)
140 *element->value.complex->list[src->minor-1]->u.numeric;
142 else if (element->value.complex->list[src->minor]->which ==
143 Z_StringOrNumeric_string)
149 element->value.complex->list[src->minor-1]->u.string;
163 static int attr_find(AttrType *src, oid_value *attributeSetP)
165 return attr_find_ex(src, attributeSetP, 0);
168 static void attr_init(AttrType *src, Z_AttributesPlusTerm *zapt,
191 static void term_untrans(ZebraHandle zh, int reg_type,
192 char *dst, const char *src)
197 const char *cp = zebra_maps_output(zh->reg->zebra_maps,
199 if (!cp && len < IT_MAX_WORD-1)
202 while (*cp && len < IT_MAX_WORD-1)
208 static void add_isam_p(const char *name, const char *info,
213 log_level_rpn = yaz_log_module_level("rpn");
216 if (p->isam_p_indx == p->isam_p_size)
218 ISAMC_P *new_isam_p_buf;
222 p->isam_p_size = 2*p->isam_p_size + 100;
223 new_isam_p_buf = (ISAMC_P *) xmalloc(sizeof(*new_isam_p_buf) *
227 memcpy(new_isam_p_buf, p->isam_p_buf,
228 p->isam_p_indx * sizeof(*p->isam_p_buf));
229 xfree(p->isam_p_buf);
231 p->isam_p_buf = new_isam_p_buf;
234 new_term_no = (int *) xmalloc(sizeof(*new_term_no) * p->isam_p_size);
237 memcpy(new_term_no, p->isam_p_buf,
238 p->isam_p_indx * sizeof(*p->term_no));
241 p->term_no = new_term_no;
244 assert(*info == sizeof(*p->isam_p_buf));
245 memcpy(p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
252 char term_tmp[IT_MAX_WORD];
254 int len = key_SU_decode (&su_code, name);
256 term_untrans (p->zh, p->reg_type, term_tmp, name+len+1);
257 yaz_log(log_level_rpn, "grep: %d %c %s", su_code, name[len], term_tmp);
258 zebraExplain_lookup_ord (p->zh->reg->zei,
259 su_code, &db, &set, &use);
260 yaz_log(log_level_rpn, "grep: set=%d use=%d db=%s", set, use, db);
262 resultSetAddTerm(p->zh, p->termset, name[len], db,
269 static int grep_handle(char *name, const char *info, void *p)
271 add_isam_p(name, info, (struct grep_info *) p);
275 static int term_pre(ZebraMaps zebra_maps, int reg_type, const char **src,
276 const char *ct1, const char *ct2, int first)
278 const char *s1, *s0 = *src;
281 /* skip white space */
284 if (ct1 && strchr(ct1, *s0))
286 if (ct2 && strchr(ct2, *s0))
289 map = zebra_maps_input(zebra_maps, reg_type, &s1, strlen(s1), first);
290 if (**map != *CHR_SPACE)
298 #define REGEX_CHARS " []()|.*+?!"
300 /* term_100: handle term, where trunc = none(no operators at all) */
301 static int term_100(ZebraMaps zebra_maps, int reg_type,
302 const char **src, char *dst, int space_split,
310 const char *space_start = 0;
311 const char *space_end = 0;
313 if (!term_pre(zebra_maps, reg_type, src, NULL, NULL, !space_split))
319 map = zebra_maps_input(zebra_maps, reg_type, &s0, strlen(s0), 0);
322 if (**map == *CHR_SPACE)
325 else /* complete subfield only. */
327 if (**map == *CHR_SPACE)
328 { /* save space mapping for later .. */
333 else if (space_start)
334 { /* reload last space */
335 while (space_start < space_end)
337 if (strchr(REGEX_CHARS, *space_start))
339 dst_term[j++] = *space_start;
340 dst[i++] = *space_start++;
343 space_start = space_end = 0;
346 /* add non-space char */
349 if (strchr(REGEX_CHARS, *s1))
361 /* term_101: handle term, where trunc = Process # */
362 static int term_101(ZebraMaps zebra_maps, int reg_type,
363 const char **src, char *dst, int space_split,
371 if (!term_pre(zebra_maps, reg_type, src, "#", "#", !space_split))
380 dst_term[j++] = *s0++;
385 map = zebra_maps_input(zebra_maps, reg_type, &s0, strlen(s0), 0);
386 if (space_split && **map == *CHR_SPACE)
390 if (strchr(REGEX_CHARS, *s1))
398 dst_term[j++] = '\0';
403 /* term_103: handle term, where trunc = re-2 (regular expressions) */
404 static int term_103(ZebraMaps zebra_maps, int reg_type, const char **src,
405 char *dst, int *errors, int space_split,
413 if (!term_pre(zebra_maps, reg_type, src, "^\\()[].*+?|", "(", !space_split))
416 if (errors && *s0 == '+' && s0[1] && s0[2] == '+' && s0[3] &&
417 isdigit(((const unsigned char *)s0)[1]))
419 *errors = s0[1] - '0';
426 if (strchr("^\\()[].*+?|-", *s0))
434 map = zebra_maps_input(zebra_maps, reg_type, &s0, strlen(s0), 0);
435 if (**map == *CHR_SPACE)
439 if (strchr(REGEX_CHARS, *s1))
452 /* term_103: handle term, where trunc = re-1 (regular expressions) */
453 static int term_102 (ZebraMaps zebra_maps, int reg_type, const char **src,
454 char *dst, int space_split, char *dst_term)
456 return term_103(zebra_maps, reg_type, src, dst, NULL, space_split,
461 /* term_104: handle term, where trunc = Process # and ! */
462 static int term_104(ZebraMaps zebra_maps, int reg_type,
463 const char **src, char *dst, int space_split,
471 if (!term_pre(zebra_maps, reg_type, src, "?*#", "?*#", !space_split))
478 dst_term[j++] = *s0++;
479 if (*s0 >= '0' && *s0 <= '9')
482 while (*s0 >= '0' && *s0 <= '9')
484 limit = limit * 10 + (*s0 - '0');
485 dst_term[j++] = *s0++;
505 dst_term[j++] = *s0++;
510 dst_term[j++] = *s0++;
515 map = zebra_maps_input(zebra_maps, reg_type, &s0, strlen(s0), 0);
516 if (space_split && **map == *CHR_SPACE)
520 if (strchr(REGEX_CHARS, *s1))
528 dst_term[j++] = '\0';
533 /* term_105/106: handle term, where trunc = Process * and ! and right trunc */
534 static int term_105 (ZebraMaps zebra_maps, int reg_type,
535 const char **src, char *dst, int space_split,
536 char *dst_term, int right_truncate)
543 if (!term_pre(zebra_maps, reg_type, src, "*!", "*!", !space_split))
552 dst_term[j++] = *s0++;
557 dst_term[j++] = *s0++;
562 map = zebra_maps_input(zebra_maps, reg_type, &s0, strlen(s0), 0);
563 if (space_split && **map == *CHR_SPACE)
567 if (strchr(REGEX_CHARS, *s1))
581 dst_term[j++] = '\0';
587 /* gen_regular_rel - generate regular expression from relation
588 * val: border value (inclusive)
589 * islt: 1 if <=; 0 if >=.
591 static void gen_regular_rel(char *dst, int val, int islt)
598 yaz_log(YLOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
602 strcpy(dst, "(-[0-9]+|(");
610 strcpy(dst, "([0-9]+|-(");
622 sprintf(numstr, "%d", val);
623 for (w = strlen(numstr); --w >= 0; pos++)
642 strcpy(dst + dst_p, numstr);
643 dst_p = strlen(dst) - pos - 1;
671 for (i = 0; i<pos; i++)
684 /* match everything less than 10^(pos-1) */
686 for (i = 1; i<pos; i++)
687 strcat(dst, "[0-9]?");
691 /* match everything greater than 10^pos */
692 for (i = 0; i <= pos; i++)
693 strcat(dst, "[0-9]");
694 strcat(dst, "[0-9]*");
699 void string_rel_add_char(char **term_p, const char *src, int *indx)
701 if (src[*indx] == '\\')
702 *(*term_p)++ = src[(*indx)++];
703 *(*term_p)++ = src[(*indx)++];
707 * > abc ([b-].*|a[c-].*|ab[d-].*|abc.+)
708 * ([^-a].*|a[^-b].*ab[^-c].*|abc.+)
709 * >= abc ([b-].*|a[c-].*|ab[c-].*)
710 * ([^-a].*|a[^-b].*|ab[c-].*)
711 * < abc ([-0].*|a[-a].*|ab[-b].*)
712 * ([^a-].*|a[^b-].*|ab[^c-].*)
713 * <= abc ([-0].*|a[-a].*|ab[-b].*|abc)
714 * ([^a-].*|a[^b-].*|ab[^c-].*|abc)
716 static int string_relation(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
717 const char **term_sub, char *term_dict,
718 oid_value attributeSet,
719 int reg_type, int space_split, char *term_dst)
724 char *term_tmp = term_dict + strlen(term_dict);
725 char term_component[2*IT_MAX_WORD+20];
727 attr_init(&relation, zapt, 2);
728 relation_value = attr_find(&relation, NULL);
730 yaz_log(YLOG_DEBUG, "string relation value=%d", relation_value);
731 switch (relation_value)
734 if (!term_100 (zh->reg->zebra_maps, reg_type,
735 term_sub, term_component,
736 space_split, term_dst))
738 yaz_log(log_level_rpn, "Relation <");
741 for (i = 0; term_component[i]; )
748 string_rel_add_char (&term_tmp, term_component, &j);
753 string_rel_add_char (&term_tmp, term_component, &i);
760 if ((term_tmp - term_dict) > IT_MAX_WORD)
767 if (!term_100 (zh->reg->zebra_maps, reg_type,
768 term_sub, term_component,
769 space_split, term_dst))
771 yaz_log(log_level_rpn, "Relation <=");
774 for (i = 0; term_component[i]; )
779 string_rel_add_char (&term_tmp, term_component, &j);
783 string_rel_add_char (&term_tmp, term_component, &i);
792 if ((term_tmp - term_dict) > IT_MAX_WORD)
795 for (i = 0; term_component[i]; )
796 string_rel_add_char (&term_tmp, term_component, &i);
801 if (!term_100 (zh->reg->zebra_maps, reg_type,
802 term_sub, term_component, space_split, term_dst))
804 yaz_log(log_level_rpn, "Relation >");
807 for (i = 0; term_component[i];)
812 string_rel_add_char (&term_tmp, term_component, &j);
817 string_rel_add_char (&term_tmp, term_component, &i);
825 if ((term_tmp - term_dict) > IT_MAX_WORD)
828 for (i = 0; term_component[i];)
829 string_rel_add_char (&term_tmp, term_component, &i);
836 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub,
837 term_component, space_split, term_dst))
839 yaz_log(log_level_rpn, "Relation >=");
842 for (i = 0; term_component[i];)
849 string_rel_add_char (&term_tmp, term_component, &j);
852 if (term_component[i+1])
856 string_rel_add_char (&term_tmp, term_component, &i);
860 string_rel_add_char (&term_tmp, term_component, &i);
867 if ((term_tmp - term_dict) > IT_MAX_WORD)
875 yaz_log(log_level_rpn, "Relation =");
876 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub,
877 term_component, space_split, term_dst))
879 strcat(term_tmp, "(");
880 strcat(term_tmp, term_component);
881 strcat(term_tmp, ")");
886 static int string_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
887 const char **term_sub,
888 oid_value attributeSet, NMEM stream,
889 struct grep_info *grep_info,
890 int reg_type, int complete_flag,
891 int num_bases, char **basenames,
892 char *term_dst, int xpath_use);
894 static RSET term_trunc(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
895 const char **term_sub,
896 oid_value attributeSet, NMEM stream,
897 struct grep_info *grep_info,
898 int reg_type, int complete_flag,
899 int num_bases, char **basenames,
901 const char *rank_type, int xpath_use,
905 grep_info->isam_p_indx = 0;
906 r = string_term(zh, zapt, term_sub, attributeSet, stream, grep_info,
907 reg_type, complete_flag, num_bases, basenames,
908 term_dst, xpath_use);
911 yaz_log(log_level_rpn, "term: %s", term_dst);
912 return rset_trunc(zh, grep_info->isam_p_buf,
913 grep_info->isam_p_indx, term_dst,
914 strlen(term_dst), rank_type, 1 /* preserve pos */,
915 zapt->term->which, rset_nmem,
916 key_it_ctrl,key_it_ctrl->scope);
918 static char *nmem_strdup_i(NMEM nmem, int v)
921 sprintf (val_str, "%d", v);
922 return nmem_strdup(nmem, val_str);
925 static int string_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
926 const char **term_sub,
927 oid_value attributeSet, NMEM stream,
928 struct grep_info *grep_info,
929 int reg_type, int complete_flag,
930 int num_bases, char **basenames,
931 char *term_dst, int xpath_use)
933 char term_dict[2*IT_MAX_WORD+4000];
936 int truncation_value;
939 const char *use_string = 0;
940 oid_value curAttributeSet = attributeSet;
942 struct rpn_char_map_info rcmi;
943 int space_split = complete_flag ? 0 : 1;
945 int bases_ok = 0; /* no of databases with OK attribute */
946 int errCode = 0; /* err code (if any is not OK) */
947 char *errString = 0; /* addinfo */
949 rpn_char_map_prepare (zh->reg, reg_type, &rcmi);
950 attr_init (&use, zapt, 1);
951 use_value = attr_find_ex (&use, &curAttributeSet, &use_string);
952 yaz_log(log_level_rpn, "string_term, use value %d", use_value);
953 attr_init (&truncation, zapt, 5);
954 truncation_value = attr_find (&truncation, NULL);
955 yaz_log(log_level_rpn, "truncation value %d", truncation_value);
957 if (use_value == -1) /* no attribute - assumy "any" */
959 for (base_no = 0; base_no < num_bases; base_no++)
966 data1_local_attribute id_xpath_attr;
967 data1_local_attribute *local_attr;
968 int max_pos, prefix_len = 0;
972 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
974 zh->errCode = 109; /* Database unavailable */
975 zh->errString = basenames[base_no];
978 if (xpath_use > 0 && use_value == -2)
980 /* xpath mode and we have a string attribute */
981 attp.local_attributes = &id_xpath_attr;
982 attp.attset_ordinal = VAL_IDXPATH;
983 id_xpath_attr.next = 0;
985 use_value = xpath_use; /* xpath_use as use-attribute now */
986 id_xpath_attr.local = use_value;
988 else if (curAttributeSet == VAL_IDXPATH && use_value >= 0)
990 /* X-Path attribute, use numeric value directly */
991 attp.local_attributes = &id_xpath_attr;
992 attp.attset_ordinal = VAL_IDXPATH;
993 id_xpath_attr.next = 0;
994 id_xpath_attr.local = use_value;
996 else if (use_string &&
997 (ord = zebraExplain_lookup_attr_str(zh->reg->zei,
1000 /* we have a match for a raw string attribute */
1005 term_dict[prefix_len++] = '|';
1007 term_dict[prefix_len++] = '(';
1009 ord_len = key_SU_encode (ord, ord_buf);
1010 for (i = 0; i<ord_len; i++)
1012 term_dict[prefix_len++] = 1;
1013 term_dict[prefix_len++] = ord_buf[i];
1015 attp.local_attributes = 0; /* no more attributes */
1019 /* lookup in the .att files . Allow string as well */
1020 if ((r = att_getentbyatt (zh, &attp, curAttributeSet, use_value,
1023 yaz_log(YLOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1024 curAttributeSet, use_value, r);
1027 /* set was found, but value wasn't defined */
1030 errString = nmem_strdup(stream, use_string);
1032 errString = nmem_strdup_i (stream, use_value);
1037 struct oident oident;
1039 oident.proto = PROTO_Z3950;
1040 oident.oclass = CLASS_ATTSET;
1041 oident.value = curAttributeSet;
1042 oid_ent_to_oid (&oident, oid);
1045 errString = nmem_strdup (stream, oident.desc);
1050 for (local_attr = attp.local_attributes; local_attr;
1051 local_attr = local_attr->next)
1056 ord = zebraExplain_lookup_attr_su(zh->reg->zei,
1057 attp.attset_ordinal,
1062 term_dict[prefix_len++] = '|';
1064 term_dict[prefix_len++] = '(';
1066 ord_len = key_SU_encode (ord, ord_buf);
1067 for (i = 0; i<ord_len; i++)
1069 term_dict[prefix_len++] = 1;
1070 term_dict[prefix_len++] = ord_buf[i];
1079 errString = nmem_strdup_i(stream, use_value);
1085 bases_ok++; /* this has OK attributes */
1089 term_dict[prefix_len++] = ')';
1090 term_dict[prefix_len++] = 1;
1091 term_dict[prefix_len++] = reg_type;
1092 yaz_log(log_level_rpn, "reg_type = %d", term_dict[prefix_len-1]);
1093 term_dict[prefix_len] = '\0';
1095 switch (truncation_value)
1097 case -1: /* not specified */
1098 case 100: /* do not truncate */
1099 if (!string_relation (zh, zapt, &termp, term_dict,
1101 reg_type, space_split, term_dst))
1104 case 1: /* right truncation */
1105 term_dict[j++] = '(';
1106 if (!term_100(zh->reg->zebra_maps, reg_type,
1107 &termp, term_dict + j, space_split, term_dst))
1109 strcat(term_dict, ".*)");
1111 case 2: /* keft truncation */
1112 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1113 if (!term_100(zh->reg->zebra_maps, reg_type,
1114 &termp, term_dict + j, space_split, term_dst))
1116 strcat(term_dict, ")");
1118 case 3: /* left&right truncation */
1119 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1120 if (!term_100(zh->reg->zebra_maps, reg_type,
1121 &termp, term_dict + j, space_split, term_dst))
1123 strcat(term_dict, ".*)");
1125 case 101: /* process # in term */
1126 term_dict[j++] = '(';
1127 if (!term_101(zh->reg->zebra_maps, reg_type,
1128 &termp, term_dict + j, space_split, term_dst))
1130 strcat(term_dict, ")");
1132 case 102: /* Regexp-1 */
1133 term_dict[j++] = '(';
1134 if (!term_102(zh->reg->zebra_maps, reg_type,
1135 &termp, term_dict + j, space_split, term_dst))
1137 strcat(term_dict, ")");
1139 case 103: /* Regexp-2 */
1141 term_dict[j++] = '(';
1143 if (!term_103 (zh->reg->zebra_maps, reg_type,
1144 &termp, term_dict + j, ®ex_range,
1145 space_split, term_dst))
1147 strcat(term_dict, ")");
1148 case 104: /* process # and ! in term */
1149 term_dict[j++] = '(';
1150 if (!term_104 (zh->reg->zebra_maps, reg_type,
1151 &termp, term_dict + j, space_split, term_dst))
1153 strcat(term_dict, ")");
1155 case 105: /* process * and ! in term */
1156 term_dict[j++] = '(';
1157 if (!term_105 (zh->reg->zebra_maps, reg_type,
1158 &termp, term_dict + j, space_split, term_dst, 1))
1160 strcat(term_dict, ")");
1162 case 106: /* process * and ! in term */
1163 term_dict[j++] = '(';
1164 if (!term_105 (zh->reg->zebra_maps, reg_type,
1165 &termp, term_dict + j, space_split, term_dst, 0))
1167 strcat(term_dict, ")");
1171 zh->errString = nmem_strdup_i(stream, truncation_value);
1176 yaz_log(log_level_rpn, "dict_lookup_grep: %s", term_dict+prefix_len);
1177 r = dict_lookup_grep(zh->reg->dict, term_dict, regex_range,
1178 grep_info, &max_pos, init_pos,
1181 yaz_log(YLOG_WARN, "dict_lookup_grep fail %d", r);
1186 zh->errCode = errCode;
1187 zh->errString = errString;
1191 yaz_log(YLOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1196 /* convert APT search term to UTF8 */
1197 static int zapt_term_to_utf8 (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1201 Z_Term *term = zapt->term;
1203 switch (term->which)
1205 case Z_Term_general:
1206 if (zh->iconv_to_utf8 != 0)
1208 char *inbuf = term->u.general->buf;
1209 size_t inleft = term->u.general->len;
1210 char *outbuf = termz;
1211 size_t outleft = IT_MAX_WORD-1;
1214 ret = yaz_iconv(zh->iconv_to_utf8, &inbuf, &inleft,
1216 if (ret == (size_t)(-1))
1218 ret = yaz_iconv(zh->iconv_to_utf8, 0, 0, 0, 0);
1226 sizez = term->u.general->len;
1227 if (sizez > IT_MAX_WORD-1)
1228 sizez = IT_MAX_WORD-1;
1229 memcpy (termz, term->u.general->buf, sizez);
1230 termz[sizez] = '\0';
1233 case Z_Term_characterString:
1234 sizez = strlen(term->u.characterString);
1235 if (sizez > IT_MAX_WORD-1)
1236 sizez = IT_MAX_WORD-1;
1237 memcpy (termz, term->u.characterString, sizez);
1238 termz[sizez] = '\0';
1247 /* convert APT SCAN term to internal cmap */
1248 static int trans_scan_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1249 char *termz, int reg_type)
1251 char termz0[IT_MAX_WORD];
1253 if (zapt_term_to_utf8(zh, zapt, termz0))
1254 return -1; /* error */
1258 const char *cp = (const char *) termz0;
1259 const char *cp_end = cp + strlen(cp);
1262 const char *space_map = NULL;
1265 while ((len = (cp_end - cp)) > 0)
1267 map = zebra_maps_input (zh->reg->zebra_maps, reg_type, &cp, len, 0);
1268 if (**map == *CHR_SPACE)
1273 for (src = space_map; *src; src++)
1276 for (src = *map; *src; src++)
1285 char *normalize_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1286 const char *termz, NMEM stream, unsigned reg_id)
1289 AttrType truncation;
1290 int truncation_value;
1293 attr_init (&truncation, zapt, 5);
1294 truncation_value = attr_find (&truncation, NULL);
1296 switch (truncation_value)
1316 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, ex_list,
1317 termz, strlen(termz));
1319 return nmem_strdup(stream, termz);
1322 char *buf = (char*) nmem_malloc(stream, wrbuf_len(wrbuf)+1);
1323 memcpy (buf, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1324 buf[wrbuf_len(wrbuf)] = '\0';
1329 static void grep_info_delete (struct grep_info *grep_info)
1332 xfree(grep_info->term_no);
1334 xfree (grep_info->isam_p_buf);
1337 static int grep_info_prepare (ZebraHandle zh,
1338 Z_AttributesPlusTerm *zapt,
1339 struct grep_info *grep_info,
1344 int termset_value_numeric;
1345 const char *termset_value_string;
1348 grep_info->term_no = 0;
1350 grep_info->isam_p_size = 0;
1351 grep_info->isam_p_buf = NULL;
1353 grep_info->reg_type = reg_type;
1354 grep_info->termset = 0;
1358 attr_init (&termset, zapt, 8);
1359 termset_value_numeric =
1360 attr_find_ex (&termset, NULL, &termset_value_string);
1361 if (termset_value_numeric != -1)
1364 const char *termset_name = 0;
1365 if (termset_value_numeric != -2)
1368 sprintf (resname, "%d", termset_value_numeric);
1369 termset_name = resname;
1372 termset_name = termset_value_string;
1373 yaz_log(log_level_rpn, "creating termset set %s", termset_name);
1374 grep_info->termset = resultSetAdd (zh, termset_name, 1);
1375 if (!grep_info->termset)
1378 zh->errString = nmem_strdup (stream, termset_name);
1386 static RSET rpn_search_APT_phrase (ZebraHandle zh,
1387 Z_AttributesPlusTerm *zapt,
1388 const char *termz_org,
1389 oid_value attributeSet,
1391 int reg_type, int complete_flag,
1392 const char *rank_type, int xpath_use,
1393 int num_bases, char **basenames,
1396 char term_dst[IT_MAX_WORD+1];
1397 RSET rset[TERM_LIST_LENGTH_MAX], result;
1399 struct grep_info grep_info;
1400 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1401 const char *termp = termz;
1404 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1406 for (; rset_no < sizeof(rset)/sizeof(*rset); rset_no++)
1408 yaz_log(log_level_rpn, "APT_phrase termp=%s", termp);
1409 rset[rset_no] = term_trunc(zh, zapt, &termp, attributeSet,
1411 reg_type, complete_flag,
1412 num_bases, basenames,
1413 term_dst, rank_type,
1414 xpath_use,rset_nmem);
1418 grep_info_delete (&grep_info);
1420 return rsnull_create (rset_nmem,key_it_ctrl);
1421 else if (rset_no == 1)
1424 result = rsprox_create( rset_nmem, key_it_ctrl, key_it_ctrl->scope,
1426 1 /* ordered */, 0 /* exclusion */,
1427 3 /* relation */, 1 /* distance */);
1431 static RSET rpn_search_APT_or_list (ZebraHandle zh,
1432 Z_AttributesPlusTerm *zapt,
1433 const char *termz_org,
1434 oid_value attributeSet,
1436 int reg_type, int complete_flag,
1437 const char *rank_type,
1439 int num_bases, char **basenames,
1442 char term_dst[IT_MAX_WORD+1];
1443 RSET rset[TERM_LIST_LENGTH_MAX];
1445 struct grep_info grep_info;
1446 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1447 const char *termp = termz;
1449 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1451 for (; rset_no < sizeof(rset)/sizeof(*rset); rset_no++)
1453 yaz_log(log_level_rpn, "APT_or_list termp=%s", termp);
1454 rset[rset_no] = term_trunc(zh, zapt, &termp, attributeSet,
1456 reg_type, complete_flag,
1457 num_bases, basenames,
1458 term_dst, rank_type,
1459 xpath_use,rset_nmem);
1463 grep_info_delete (&grep_info);
1465 return rsnull_create (rset_nmem,key_it_ctrl);
1466 return rsmulti_or_create(rset_nmem, key_it_ctrl,key_it_ctrl->scope,
1470 static RSET rpn_search_APT_and_list (ZebraHandle zh,
1471 Z_AttributesPlusTerm *zapt,
1472 const char *termz_org,
1473 oid_value attributeSet,
1475 int reg_type, int complete_flag,
1476 const char *rank_type,
1478 int num_bases, char **basenames,
1481 char term_dst[IT_MAX_WORD+1];
1482 RSET rset[TERM_LIST_LENGTH_MAX];
1484 struct grep_info grep_info;
1485 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1486 const char *termp = termz;
1488 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1490 for (; rset_no < sizeof(rset)/sizeof(*rset); rset_no++)
1492 yaz_log(log_level_rpn, "APT_and_list termp=%s", termp);
1493 rset[rset_no] = term_trunc(zh, zapt, &termp, attributeSet,
1495 reg_type, complete_flag,
1496 num_bases, basenames,
1497 term_dst, rank_type,
1498 xpath_use, rset_nmem);
1502 grep_info_delete (&grep_info);
1504 return rsnull_create(rset_nmem,key_it_ctrl);
1506 return rsmulti_and_create(rset_nmem, key_it_ctrl, key_it_ctrl->scope,
1510 static int numeric_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1511 const char **term_sub,
1513 oid_value attributeSet,
1514 struct grep_info *grep_info,
1523 char *term_tmp = term_dict + strlen(term_dict);
1525 attr_init (&relation, zapt, 2);
1526 relation_value = attr_find (&relation, NULL);
1528 yaz_log(log_level_rpn, "numeric relation value=%d", relation_value);
1530 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub, term_tmp, 1,
1533 term_value = atoi (term_tmp);
1534 switch (relation_value)
1537 yaz_log(log_level_rpn, "Relation <");
1538 gen_regular_rel (term_tmp, term_value-1, 1);
1541 yaz_log(log_level_rpn, "Relation <=");
1542 gen_regular_rel (term_tmp, term_value, 1);
1545 yaz_log(log_level_rpn, "Relation >=");
1546 gen_regular_rel (term_tmp, term_value, 0);
1549 yaz_log(log_level_rpn, "Relation >");
1550 gen_regular_rel (term_tmp, term_value+1, 0);
1554 yaz_log(log_level_rpn, "Relation =");
1555 sprintf (term_tmp, "(0*%d)", term_value);
1557 yaz_log(log_level_rpn, "dict_lookup_grep: %s", term_tmp);
1558 r = dict_lookup_grep(zh->reg->dict, term_dict, 0, grep_info, max_pos,
1561 yaz_log(YLOG_WARN, "dict_lookup_grep fail, rel = gt: %d", r);
1562 yaz_log(log_level_rpn, "%d positions", grep_info->isam_p_indx);
1566 static int numeric_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1567 const char **term_sub,
1568 oid_value attributeSet, struct grep_info *grep_info,
1569 int reg_type, int complete_flag,
1570 int num_bases, char **basenames,
1571 char *term_dst, int xpath_use, NMEM stream)
1573 char term_dict[2*IT_MAX_WORD+2];
1577 const char *use_string = 0;
1578 oid_value curAttributeSet = attributeSet;
1580 struct rpn_char_map_info rcmi;
1582 int bases_ok = 0; /* no of databases with OK attribute */
1583 int errCode = 0; /* err code (if any is not OK) */
1584 char *errString = 0; /* addinfo */
1586 rpn_char_map_prepare (zh->reg, reg_type, &rcmi);
1587 attr_init (&use, zapt, 1);
1588 use_value = attr_find_ex (&use, &curAttributeSet, &use_string);
1590 if (use_value == -1)
1593 for (base_no = 0; base_no < num_bases; base_no++)
1596 data1_local_attribute id_xpath_attr;
1597 data1_local_attribute *local_attr;
1598 int max_pos, prefix_len = 0;
1601 if (use_value == -2) /* string attribute (assume IDXPATH/any) */
1603 use_value = xpath_use;
1604 attp.local_attributes = &id_xpath_attr;
1605 attp.attset_ordinal = VAL_IDXPATH;
1606 id_xpath_attr.next = 0;
1607 id_xpath_attr.local = use_value;
1609 else if (curAttributeSet == VAL_IDXPATH)
1611 attp.local_attributes = &id_xpath_attr;
1612 attp.attset_ordinal = VAL_IDXPATH;
1613 id_xpath_attr.next = 0;
1614 id_xpath_attr.local = use_value;
1618 if ((r = att_getentbyatt (zh, &attp, curAttributeSet, use_value,
1621 yaz_log(YLOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1622 curAttributeSet, use_value, r);
1625 errString = nmem_strdup_i(stream, use_value);
1633 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
1635 zh->errCode = 109; /* Database unavailable */
1636 zh->errString = basenames[base_no];
1639 for (local_attr = attp.local_attributes; local_attr;
1640 local_attr = local_attr->next)
1646 ord = zebraExplain_lookup_attr_su(zh->reg->zei,
1647 attp.attset_ordinal,
1652 term_dict[prefix_len++] = '|';
1654 term_dict[prefix_len++] = '(';
1656 ord_len = key_SU_encode (ord, ord_buf);
1657 for (i = 0; i<ord_len; i++)
1659 term_dict[prefix_len++] = 1;
1660 term_dict[prefix_len++] = ord_buf[i];
1666 errString = nmem_strdup_i(stream, use_value);
1670 term_dict[prefix_len++] = ')';
1671 term_dict[prefix_len++] = 1;
1672 term_dict[prefix_len++] = reg_type;
1673 yaz_log(YLOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1674 term_dict[prefix_len] = '\0';
1675 if (!numeric_relation (zh, zapt, &termp, term_dict,
1676 attributeSet, grep_info, &max_pos, reg_type,
1682 zh->errCode = errCode;
1683 zh->errString = errString;
1687 yaz_log(YLOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1691 static RSET rpn_search_APT_numeric (ZebraHandle zh,
1692 Z_AttributesPlusTerm *zapt,
1694 oid_value attributeSet,
1696 int reg_type, int complete_flag,
1697 const char *rank_type, int xpath_use,
1698 int num_bases, char **basenames,
1701 char term_dst[IT_MAX_WORD+1];
1702 const char *termp = termz;
1703 RSET rset[TERM_LIST_LENGTH_MAX];
1706 struct grep_info grep_info;
1708 yaz_log(log_level_rpn, "APT_numeric t='%s'",termz);
1709 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1711 for (; rset_no < sizeof(rset)/sizeof(*rset); rset_no++)
1713 yaz_log(YLOG_DEBUG, "APT_numeric termp=%s", termp);
1714 grep_info.isam_p_indx = 0;
1715 r = numeric_term(zh, zapt, &termp, attributeSet, &grep_info,
1716 reg_type, complete_flag, num_bases, basenames,
1717 term_dst, xpath_use,
1721 yaz_log(YLOG_DEBUG, "term: %s", term_dst);
1722 rset[rset_no] = rset_trunc(zh, grep_info.isam_p_buf,
1723 grep_info.isam_p_indx, term_dst,
1724 strlen(term_dst), rank_type,
1725 0 /* preserve position */,
1726 zapt->term->which, rset_nmem,
1727 key_it_ctrl,key_it_ctrl->scope);
1731 grep_info_delete (&grep_info);
1733 return rsnull_create(rset_nmem,key_it_ctrl);
1736 return rsmulti_and_create(rset_nmem,key_it_ctrl,key_it_ctrl->scope,
1740 static RSET rpn_search_APT_local (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1742 oid_value attributeSet,
1744 const char *rank_type, NMEM rset_nmem)
1750 result = rstemp_create( rset_nmem,key_it_ctrl,key_it_ctrl->scope,
1751 res_get (zh->res, "setTmpDir"),0 );
1752 rsfd = rset_open (result, RSETF_WRITE);
1760 rset_write (rsfd, &key);
1765 static RSET rpn_sort_spec (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1766 oid_value attributeSet, NMEM stream,
1767 Z_SortKeySpecList *sort_sequence,
1768 const char *rank_type)
1771 int sort_relation_value;
1772 AttrType sort_relation_type;
1777 Z_AttributeElement *ae;
1782 attr_init (&sort_relation_type, zapt, 7);
1783 sort_relation_value = attr_find (&sort_relation_type, &attributeSet);
1785 attr_init (&use_type, zapt, 1);
1786 use_value = attr_find (&use_type, &attributeSet);
1788 if (!sort_sequence->specs)
1790 sort_sequence->num_specs = 10;
1791 sort_sequence->specs = (Z_SortKeySpec **)
1792 nmem_malloc(stream, sort_sequence->num_specs *
1793 sizeof(*sort_sequence->specs));
1794 for (i = 0; i<sort_sequence->num_specs; i++)
1795 sort_sequence->specs[i] = 0;
1797 if (zapt->term->which != Z_Term_general)
1800 i = atoi_n ((char *) zapt->term->u.general->buf,
1801 zapt->term->u.general->len);
1802 if (i >= sort_sequence->num_specs)
1804 sprintf (termz, "%d", i);
1806 oe.proto = PROTO_Z3950;
1807 oe.oclass = CLASS_ATTSET;
1808 oe.value = attributeSet;
1809 if (!oid_ent_to_oid (&oe, oid))
1812 sks = (Z_SortKeySpec *) nmem_malloc(stream, sizeof(*sks));
1813 sks->sortElement = (Z_SortElement *)
1814 nmem_malloc(stream, sizeof(*sks->sortElement));
1815 sks->sortElement->which = Z_SortElement_generic;
1816 sk = sks->sortElement->u.generic = (Z_SortKey *)
1817 nmem_malloc(stream, sizeof(*sk));
1818 sk->which = Z_SortKey_sortAttributes;
1819 sk->u.sortAttributes = (Z_SortAttributes *)
1820 nmem_malloc(stream, sizeof(*sk->u.sortAttributes));
1822 sk->u.sortAttributes->id = oid;
1823 sk->u.sortAttributes->list = (Z_AttributeList *)
1824 nmem_malloc(stream, sizeof(*sk->u.sortAttributes->list));
1825 sk->u.sortAttributes->list->num_attributes = 1;
1826 sk->u.sortAttributes->list->attributes = (Z_AttributeElement **)
1827 nmem_malloc(stream, sizeof(*sk->u.sortAttributes->list->attributes));
1828 ae = *sk->u.sortAttributes->list->attributes = (Z_AttributeElement *)
1829 nmem_malloc(stream, sizeof(**sk->u.sortAttributes->list->attributes));
1830 ae->attributeSet = 0;
1831 ae->attributeType = (int *)
1832 nmem_malloc(stream, sizeof(*ae->attributeType));
1833 *ae->attributeType = 1;
1834 ae->which = Z_AttributeValue_numeric;
1835 ae->value.numeric = (int *)
1836 nmem_malloc(stream, sizeof(*ae->value.numeric));
1837 *ae->value.numeric = use_value;
1839 sks->sortRelation = (int *)
1840 nmem_malloc(stream, sizeof(*sks->sortRelation));
1841 if (sort_relation_value == 1)
1842 *sks->sortRelation = Z_SortKeySpec_ascending;
1843 else if (sort_relation_value == 2)
1844 *sks->sortRelation = Z_SortKeySpec_descending;
1846 *sks->sortRelation = Z_SortKeySpec_ascending;
1848 sks->caseSensitivity = (int *)
1849 nmem_malloc(stream, sizeof(*sks->caseSensitivity));
1850 *sks->caseSensitivity = 0;
1852 sks->which = Z_SortKeySpec_null;
1853 sks->u.null = odr_nullval ();
1854 sort_sequence->specs[i] = sks;
1855 return rsnull_create (NULL,key_it_ctrl);
1856 /* FIXME - nmem?? */
1860 static int parse_xpath(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1861 oid_value attributeSet,
1862 struct xpath_location_step *xpath, int max, NMEM mem)
1864 oid_value curAttributeSet = attributeSet;
1866 const char *use_string = 0;
1868 attr_init (&use, zapt, 1);
1869 attr_find_ex (&use, &curAttributeSet, &use_string);
1871 if (!use_string || *use_string != '/')
1874 return zebra_parse_xpath_str(use_string, xpath, max, mem);
1879 static RSET xpath_trunc(ZebraHandle zh, NMEM stream,
1880 int reg_type, const char *term, int use,
1881 oid_value curAttributeSet, NMEM rset_nmem)
1884 struct grep_info grep_info;
1885 char term_dict[2048];
1888 int ord = zebraExplain_lookup_attr_su(zh->reg->zei, curAttributeSet, use);
1889 int ord_len, i, r, max_pos;
1890 int term_type = Z_Term_characterString;
1891 const char *flags = "void";
1893 if (grep_info_prepare (zh, 0 /* zapt */, &grep_info, '0', stream))
1894 return rsnull_create (rset_nmem,key_it_ctrl);
1897 return rsnull_create (rset_nmem,key_it_ctrl);
1899 term_dict[prefix_len++] = '|';
1901 term_dict[prefix_len++] = '(';
1903 ord_len = key_SU_encode (ord, ord_buf);
1904 for (i = 0; i<ord_len; i++)
1906 term_dict[prefix_len++] = 1;
1907 term_dict[prefix_len++] = ord_buf[i];
1909 term_dict[prefix_len++] = ')';
1910 term_dict[prefix_len++] = 1;
1911 term_dict[prefix_len++] = reg_type;
1913 strcpy(term_dict+prefix_len, term);
1915 grep_info.isam_p_indx = 0;
1916 r = dict_lookup_grep(zh->reg->dict, term_dict, 0,
1917 &grep_info, &max_pos, 0, grep_handle);
1918 yaz_log (YLOG_LOG, "%s %d positions", term,
1919 grep_info.isam_p_indx);
1920 rset = rset_trunc(zh, grep_info.isam_p_buf,
1921 grep_info.isam_p_indx, term, strlen(term),
1922 flags, 1, term_type,rset_nmem,
1923 key_it_ctrl, key_it_ctrl->scope);
1924 grep_info_delete (&grep_info);
1928 static RSET rpn_search_xpath (ZebraHandle zh,
1929 oid_value attributeSet,
1930 int num_bases, char **basenames,
1931 NMEM stream, const char *rank_type, RSET rset,
1932 int xpath_len, struct xpath_location_step *xpath,
1935 oid_value curAttributeSet = attributeSet;
1942 yaz_log (YLOG_DEBUG, "xpath len=%d", xpath_len);
1943 for (i = 0; i<xpath_len; i++)
1945 yaz_log (log_level_rpn, "XPATH %d %s", i, xpath[i].part);
1949 curAttributeSet = VAL_IDXPATH;
1959 a[@attr = value]/b[@other = othervalue]
1961 /e/@a val range(e/,range(@a,freetext(w,1015,val),@a),e/)
1962 /a/b val range(b/a/,freetext(w,1016,val),b/a/)
1963 /a/b/@c val range(b/a/,range(@c,freetext(w,1016,val),@c),b/a/)
1964 /a/b[@c = y] val range(b/a/,freetext(w,1016,val),b/a/,@c = y)
1965 /a[@c = y]/b val range(a/,range(b/a/,freetext(w,1016,val),b/a/),a/,@c = y)
1966 /a[@c = x]/b[@c = y] range(a/,range(b/a/,freetext(w,1016,val),b/a/,@c = y),a/,@c = x)
1970 dict_grep_cmap (zh->reg->dict, 0, 0);
1972 for (base_no = 0; base_no < num_bases; base_no++)
1974 int level = xpath_len;
1977 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
1979 zh->errCode = 109; /* Database unavailable */
1980 zh->errString = basenames[base_no];
1983 while (--level >= 0)
1985 char xpath_rev[128];
1987 RSET rset_start_tag = 0, rset_end_tag = 0, rset_attr = 0;
1991 for (i = level; i >= 1; --i)
1993 const char *cp = xpath[i].part;
1999 memcpy (xpath_rev + len, "[^/]*", 5);
2002 else if (*cp == ' ')
2005 xpath_rev[len++] = 1;
2006 xpath_rev[len++] = ' ';
2010 xpath_rev[len++] = *cp;
2011 xpath_rev[len++] = '/';
2013 else if (i == 1) /* // case */
2015 xpath_rev[len++] = '.';
2016 xpath_rev[len++] = '*';
2021 if (xpath[level].predicate &&
2022 xpath[level].predicate->which == XPATH_PREDICATE_RELATION &&
2023 xpath[level].predicate->u.relation.name[0])
2025 WRBUF wbuf = wrbuf_alloc();
2026 wrbuf_puts(wbuf, xpath[level].predicate->u.relation.name+1);
2027 if (xpath[level].predicate->u.relation.value)
2029 const char *cp = xpath[level].predicate->u.relation.value;
2030 wrbuf_putc(wbuf, '=');
2034 if (strchr(REGEX_CHARS, *cp))
2035 wrbuf_putc(wbuf, '\\');
2036 wrbuf_putc(wbuf, *cp);
2040 wrbuf_puts(wbuf, "");
2041 rset_attr = xpath_trunc(
2042 zh, stream, '0', wrbuf_buf(wbuf), 3,
2043 curAttributeSet,rset_nmem);
2044 wrbuf_free(wbuf, 1);
2051 yaz_log (log_level_rpn, "xpath_rev (%d) = %s", level, xpath_rev);
2052 if (strlen(xpath_rev))
2054 rset_start_tag = xpath_trunc(zh, stream, '0',
2055 xpath_rev, 1, curAttributeSet, rset_nmem);
2057 rset_end_tag = xpath_trunc(zh, stream, '0',
2058 xpath_rev, 2, curAttributeSet, rset_nmem);
2060 rset = rsbetween_create(rset_nmem, key_it_ctrl,
2062 rset_start_tag, rset,
2063 rset_end_tag, rset_attr);
2074 static RSET rpn_search_APT (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2075 oid_value attributeSet, NMEM stream,
2076 Z_SortKeySpecList *sort_sequence,
2077 int num_bases, char **basenames,
2081 char *search_type = NULL;
2082 char rank_type[128];
2085 char termz[IT_MAX_WORD+1];
2089 struct xpath_location_step xpath[10];
2093 log_level_rpn = yaz_log_module_level("rpn");
2096 zebra_maps_attr (zh->reg->zebra_maps, zapt, ®_id, &search_type,
2097 rank_type, &complete_flag, &sort_flag);
2099 yaz_log(YLOG_DEBUG, "reg_id=%c", reg_id);
2100 yaz_log(YLOG_DEBUG, "complete_flag=%d", complete_flag);
2101 yaz_log(YLOG_DEBUG, "search_type=%s", search_type);
2102 yaz_log(YLOG_DEBUG, "rank_type=%s", rank_type);
2104 if (zapt_term_to_utf8(zh, zapt, termz))
2108 return rpn_sort_spec (zh, zapt, attributeSet, stream, sort_sequence,
2110 xpath_len = parse_xpath(zh, zapt, attributeSet, xpath, 10, stream);
2114 if (xpath[xpath_len-1].part[0] == '@')
2118 if (!strcmp (search_type, "phrase"))
2120 rset = rpn_search_APT_phrase (zh, zapt, termz, attributeSet, stream,
2121 reg_id, complete_flag, rank_type,
2123 num_bases, basenames, rset_nmem);
2125 else if (!strcmp (search_type, "and-list"))
2127 rset = rpn_search_APT_and_list (zh, zapt, termz, attributeSet, stream,
2128 reg_id, complete_flag, rank_type,
2130 num_bases, basenames, rset_nmem);
2132 else if (!strcmp (search_type, "or-list"))
2134 rset = rpn_search_APT_or_list (zh, zapt, termz, attributeSet, stream,
2135 reg_id, complete_flag, rank_type,
2137 num_bases, basenames, rset_nmem);
2139 else if (!strcmp (search_type, "local"))
2141 rset = rpn_search_APT_local (zh, zapt, termz, attributeSet, stream,
2142 rank_type, rset_nmem);
2144 else if (!strcmp (search_type, "numeric"))
2146 rset = rpn_search_APT_numeric (zh, zapt, termz, attributeSet, stream,
2147 reg_id, complete_flag, rank_type,
2149 num_bases, basenames, rset_nmem);
2151 else if (!strcmp (search_type, "always"))
2157 return rpn_search_xpath (zh, attributeSet, num_bases, basenames,
2158 stream, rank_type, rset,
2159 xpath_len, xpath, rset_nmem);
2162 static RSET rpn_search_structure (ZebraHandle zh, Z_RPNStructure *zs,
2163 oid_value attributeSet,
2164 NMEM stream, NMEM rset_nmem,
2165 Z_SortKeySpecList *sort_sequence,
2166 int num_bases, char **basenames)
2169 if (zs->which == Z_RPNStructure_complex)
2171 Z_Operator *zop = zs->u.complex->roperator;
2172 RSET rsets[2]; /* l and r argument */
2174 rsets[0]=rpn_search_structure (zh, zs->u.complex->s1,
2175 attributeSet, stream, rset_nmem,
2177 num_bases, basenames);
2178 if (rsets[0] == NULL)
2180 rsets[1]=rpn_search_structure (zh, zs->u.complex->s2,
2181 attributeSet, stream, rset_nmem,
2183 num_bases, basenames);
2184 if (rsets[1] == NULL)
2186 rset_delete (rsets[0]);
2192 case Z_Operator_and:
2193 r = rsmulti_and_create(rset_nmem, key_it_ctrl, key_it_ctrl->scope,
2197 r = rsmulti_or_create(rset_nmem, key_it_ctrl, key_it_ctrl->scope,
2200 case Z_Operator_and_not:
2201 r = rsbool_create_not(rset_nmem,key_it_ctrl, key_it_ctrl->scope,
2204 case Z_Operator_prox:
2205 if (zop->u.prox->which != Z_ProximityOperator_known)
2210 if (*zop->u.prox->u.known != Z_ProxUnit_word)
2212 char *val = (char *) nmem_malloc(stream, 16);
2214 zh->errString = val;
2215 sprintf (val, "%d", *zop->u.prox->u.known);
2220 /* new / old prox */
2221 r = rsprox_create(rset_nmem,key_it_ctrl,key_it_ctrl->scope,
2223 *zop->u.prox->ordered,
2224 (!zop->u.prox->exclusion ?
2225 0 : *zop->u.prox->exclusion),
2226 *zop->u.prox->relationType,
2227 *zop->u.prox->distance );
2235 else if (zs->which == Z_RPNStructure_simple)
2237 if (zs->u.simple->which == Z_Operand_APT)
2239 yaz_log(YLOG_DEBUG, "rpn_search_APT");
2240 r = rpn_search_APT (zh, zs->u.simple->u.attributesPlusTerm,
2241 attributeSet, stream, sort_sequence,
2242 num_bases, basenames,rset_nmem);
2244 else if (zs->u.simple->which == Z_Operand_resultSetId)
2246 yaz_log(YLOG_DEBUG, "rpn_search_ref");
2247 r = resultSetRef (zh, zs->u.simple->u.resultSetId);
2250 r = rsnull_create (rset_nmem,key_it_ctrl);
2253 nmem_strdup (stream, zs->u.simple->u.resultSetId);
2274 RSET rpn_search(ZebraHandle zh, NMEM nmem, NMEM rset_nmem,
2275 Z_RPNQuery *rpn, int num_bases, char **basenames,
2276 const char *setname,
2281 oid_value attributeSet;
2282 Z_SortKeySpecList *sort_sequence;
2286 zh->errString = NULL;
2289 sort_sequence = (Z_SortKeySpecList *)
2290 nmem_malloc(nmem, sizeof(*sort_sequence));
2291 sort_sequence->num_specs = 10; /* FIXME - Hard-coded number */
2292 sort_sequence->specs = (Z_SortKeySpec **)
2293 nmem_malloc(nmem, sort_sequence->num_specs *
2294 sizeof(*sort_sequence->specs));
2295 for (i = 0; i<sort_sequence->num_specs; i++)
2296 sort_sequence->specs[i] = 0;
2298 attrset = oid_getentbyoid (rpn->attributeSetId);
2299 attributeSet = attrset->value;
2300 rset = rpn_search_structure (zh, rpn->RPNStructure, attributeSet,
2302 sort_sequence, num_bases, basenames);
2307 yaz_log(YLOG_DEBUG, "search error: %d", zh->errCode);
2309 for (i = 0; sort_sequence->specs[i]; i++)
2311 sort_sequence->num_specs = i;
2313 resultSetRank (zh, sset, rset, rset_nmem);
2316 yaz_log(YLOG_DEBUG, "resultSetSortSingle in rpn_search");
2317 resultSetSortSingle (zh, nmem, sset, rset,
2318 sort_sequence, &sort_status);
2321 yaz_log(YLOG_DEBUG, "resultSetSortSingle status = %d", zh->errCode);
2327 struct scan_info_entry {
2333 struct scan_info_entry *list;
2339 static int scan_handle (char *name, const char *info, int pos, void *client)
2341 int len_prefix, idx;
2342 struct scan_info *scan_info = (struct scan_info *) client;
2344 len_prefix = strlen(scan_info->prefix);
2345 if (memcmp (name, scan_info->prefix, len_prefix))
2347 if (pos > 0) idx = scan_info->after - pos + scan_info->before;
2350 scan_info->list[idx].term = (char *)
2351 odr_malloc(scan_info->odr, strlen(name + len_prefix)+1);
2352 strcpy(scan_info->list[idx].term, name + len_prefix);
2353 assert (*info == sizeof(ISAMC_P));
2354 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAMC_P));
2358 static void scan_term_untrans (ZebraHandle zh, NMEM stream, int reg_type,
2359 char **dst, const char *src)
2361 char term_src[IT_MAX_WORD];
2362 char term_dst[IT_MAX_WORD];
2364 term_untrans (zh, reg_type, term_src, src);
2366 if (zh->iconv_from_utf8 != 0)
2369 char *inbuf = term_src;
2370 size_t inleft = strlen(term_src);
2371 char *outbuf = term_dst;
2372 size_t outleft = sizeof(term_dst)-1;
2375 ret = yaz_iconv (zh->iconv_from_utf8, &inbuf, &inleft,
2377 if (ret == (size_t)(-1))
2380 len = outbuf - term_dst;
2381 *dst = nmem_malloc(stream, len + 1);
2383 memcpy (*dst, term_dst, len);
2387 *dst = nmem_strdup(stream, term_src);
2390 static void count_set (RSET r, int *count)
2397 yaz_log(YLOG_DEBUG, "count_set");
2400 rfd = rset_open (r, RSETF_READ);
2401 while (rset_read (rfd, &key,0 /* never mind terms */))
2403 if (key.mem[0] != psysno)
2405 psysno = key.mem[0];
2411 yaz_log(YLOG_DEBUG, "%d keys, %d records", kno, *count);
2414 void rpn_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
2415 oid_value attributeset,
2416 int num_bases, char **basenames,
2417 int *position, int *num_entries, ZebraScanEntry **list,
2418 int *is_partial, RSET limit_set, int return_zero)
2421 int pos = *position;
2422 int num = *num_entries;
2426 char termz[IT_MAX_WORD+20];
2429 const char *use_string = 0;
2430 struct scan_info *scan_info_array;
2431 ZebraScanEntry *glist;
2432 int ords[32], ord_no = 0;
2435 int bases_ok = 0; /* no of databases with OK attribute */
2436 int errCode = 0; /* err code (if any is not OK) */
2437 char *errString = 0; /* addinfo */
2440 char *search_type = NULL;
2441 char rank_type[128];
2444 NMEM rset_nmem = NULL;
2448 if (attributeset == VAL_NONE)
2449 attributeset = VAL_BIB1;
2454 int termset_value_numeric;
2455 const char *termset_value_string;
2456 attr_init (&termset, zapt, 8);
2457 termset_value_numeric =
2458 attr_find_ex (&termset, NULL, &termset_value_string);
2459 if (termset_value_numeric != -1)
2462 const char *termset_name = 0;
2464 if (termset_value_numeric != -2)
2467 sprintf (resname, "%d", termset_value_numeric);
2468 termset_name = resname;
2471 termset_name = termset_value_string;
2473 limit_set = resultSetRef (zh, termset_name);
2477 yaz_log (YLOG_DEBUG, "position = %d, num = %d set=%d",
2478 pos, num, attributeset);
2480 attr_init (&use, zapt, 1);
2481 use_value = attr_find_ex (&use, &attributeset, &use_string);
2483 if (zebra_maps_attr (zh->reg->zebra_maps, zapt, ®_id, &search_type,
2484 rank_type, &complete_flag, &sort_flag))
2490 yaz_log (YLOG_DEBUG, "use_value = %d", use_value);
2492 if (use_value == -1)
2494 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
2498 data1_local_attribute *local_attr;
2500 if ((r = att_getentbyatt (zh, &attp, attributeset, use_value,
2503 yaz_log(YLOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
2504 attributeset, use_value);
2508 sprintf (val_str, "%d", use_value);
2510 errString = odr_strdup (stream, val_str);
2516 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
2518 zh->errString = basenames[base_no];
2519 zh->errCode = 109; /* Database unavailable */
2524 for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
2525 local_attr = local_attr->next)
2529 ord = zebraExplain_lookup_attr_su(zh->reg->zei,
2530 attp.attset_ordinal,
2533 ords[ord_no++] = ord;
2536 if (!bases_ok && errCode)
2538 zh->errCode = errCode;
2539 zh->errString = errString;
2547 /* prepare dictionary scanning */
2550 scan_info_array = (struct scan_info *)
2551 odr_malloc(stream, ord_no * sizeof(*scan_info_array));
2552 for (i = 0; i < ord_no; i++)
2554 int j, prefix_len = 0;
2555 int before_tmp = before, after_tmp = after;
2556 struct scan_info *scan_info = scan_info_array + i;
2557 struct rpn_char_map_info rcmi;
2559 rpn_char_map_prepare (zh->reg, reg_id, &rcmi);
2561 scan_info->before = before;
2562 scan_info->after = after;
2563 scan_info->odr = stream;
2565 scan_info->list = (struct scan_info_entry *)
2566 odr_malloc(stream, (before+after) * sizeof(*scan_info->list));
2567 for (j = 0; j<before+after; j++)
2568 scan_info->list[j].term = NULL;
2570 prefix_len += key_SU_encode (ords[i], termz + prefix_len);
2571 termz[prefix_len++] = reg_id;
2572 termz[prefix_len] = 0;
2573 strcpy(scan_info->prefix, termz);
2575 if (trans_scan_term(zh, zapt, termz+prefix_len, reg_id))
2578 dict_scan(zh->reg->dict, termz, &before_tmp, &after_tmp,
2579 scan_info, scan_handle);
2581 glist = (ZebraScanEntry *)
2582 odr_malloc(stream, (before+after)*sizeof(*glist));
2584 rset_nmem = nmem_create();
2586 /* consider terms after main term */
2587 for (i = 0; i < ord_no; i++)
2591 for (i = 0; i<after; i++)
2594 const char *mterm = NULL;
2598 for (j = 0; j < ord_no; j++)
2600 if (ptr[j] < before+after &&
2601 (tst = scan_info_array[j].list[ptr[j]].term) &&
2602 (!mterm || strcmp (tst, mterm) < 0))
2610 scan_term_untrans (zh, stream->mem, reg_id,
2611 &glist[i+before].term, mterm);
2612 rset = rset_trunc(zh, &scan_info_array[j0].list[ptr[j0]].isam_p, 1,
2613 glist[i+before].term, strlen(glist[i+before].term),
2614 NULL, 0, zapt->term->which, rset_nmem,
2615 key_it_ctrl,key_it_ctrl->scope);
2617 for (j = j0+1; j<ord_no; j++)
2619 if (ptr[j] < before+after &&
2620 (tst = scan_info_array[j].list[ptr[j]].term) &&
2621 !strcmp (tst, mterm))
2627 rset_trunc(zh, &scan_info_array[j].list[ptr[j]].isam_p, 1,
2628 glist[i+before].term,
2629 strlen(glist[i+before].term), NULL, 0,
2630 zapt->term->which,rset_nmem,
2631 key_it_ctrl, key_it_ctrl->scope);
2632 rset = rsmulti_or_create(rset_nmem, key_it_ctrl,
2633 2, key_it_ctrl->scope, rsets);
2641 rsets[1] = rset_dup(limit_set);
2643 rset = rsmulti_and_create(rset_nmem, key_it_ctrl,
2644 key_it_ctrl->scope, 2, rsets);
2646 count_set(rset, &glist[i+before].occurrences);
2651 *num_entries -= (after-i);
2655 /* consider terms before main term */
2656 for (i = 0; i<ord_no; i++)
2659 for (i = 0; i<before; i++)
2662 const char *mterm = NULL;
2666 for (j = 0; j <ord_no; j++)
2668 if (ptr[j] < before &&
2669 (tst = scan_info_array[j].list[before-1-ptr[j]].term) &&
2670 (!mterm || strcmp (tst, mterm) > 0))
2679 scan_term_untrans (zh, stream->mem, reg_id,
2680 &glist[before-1-i].term, mterm);
2683 (zh, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1,
2684 glist[before-1-i].term, strlen(glist[before-1-i].term),
2685 NULL, 0, zapt->term->which,rset_nmem,
2686 key_it_ctrl,key_it_ctrl->scope);
2690 for (j = j0+1; j<ord_no; j++)
2692 if (ptr[j] < before &&
2693 (tst = scan_info_array[j].list[before-1-ptr[j]].term) &&
2694 !strcmp (tst, mterm))
2699 rsets[1] = rset_trunc(
2701 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1,
2702 glist[before-1-i].term,
2703 strlen(glist[before-1-i].term), NULL, 0,
2704 zapt->term->which, rset_nmem,
2705 key_it_ctrl, key_it_ctrl->scope);
2706 rset = rsmulti_or_create(rset_nmem, key_it_ctrl,
2707 2, key_it_ctrl->scope, rsets);
2716 rsets[1] = rset_dup(limit_set);
2718 rset = rsmulti_and_create(rset_nmem, key_it_ctrl,
2719 key_it_ctrl->scope, 2, rsets);
2721 count_set (rset, &glist[before-1-i].occurrences);
2732 nmem_destroy(rset_nmem);
2733 *list = glist + i; /* list is set to first 'real' entry */
2735 yaz_log(YLOG_DEBUG, "position = %d, num_entries = %d",
2736 *position, *num_entries);
2738 yaz_log(YLOG_DEBUG, "scan error: %d", zh->errCode);