1 /* $Id: zrpn.c,v 1.171 2005-03-11 17:56:34 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)
299 static void esc_str(char *out_buf, int out_size,
300 const char *in_buf, int in_size)
306 assert(out_size > 20);
308 for (k = 0; k<in_size; k++)
310 int c = in_buf[k] & 0xff;
312 if (c < 32 || c > 126)
316 sprintf(out_buf +strlen(out_buf), "%02X:%c ", c, pc);
317 if (strlen(out_buf) > out_size-20)
319 strcat(out_buf, "..");
325 #define REGEX_CHARS " []()|.*+?!"
327 /* term_100: handle term, where trunc = none(no operators at all) */
328 static int term_100(ZebraMaps zebra_maps, int reg_type,
329 const char **src, char *dst, int space_split,
337 const char *space_start = 0;
338 const char *space_end = 0;
340 if (!term_pre(zebra_maps, reg_type, src, NULL, NULL, !space_split))
347 map = zebra_maps_search(zebra_maps, reg_type, &s0, strlen(s0),
351 if (**map == *CHR_SPACE)
354 else /* complete subfield only. */
356 if (**map == *CHR_SPACE)
357 { /* save space mapping for later .. */
362 else if (space_start)
363 { /* reload last space */
364 while (space_start < space_end)
366 if (strchr(REGEX_CHARS, *space_start))
368 dst_term[j++] = *space_start;
369 dst[i++] = *space_start++;
372 space_start = space_end = 0;
375 /* add non-space char */
376 memcpy(dst_term+j, s1, s0 - s1);
382 if (strchr(REGEX_CHARS, *s1))
390 esc_str(tmpbuf, sizeof(tmpbuf), map[0], strlen(map[0]));
392 strcpy(dst + i, map[0]);
402 /* term_101: handle term, where trunc = Process # */
403 static int term_101(ZebraMaps zebra_maps, int reg_type,
404 const char **src, char *dst, int space_split,
412 if (!term_pre(zebra_maps, reg_type, src, "#", "#", !space_split))
421 dst_term[j++] = *s0++;
427 map = zebra_maps_search(zebra_maps, reg_type, &s0, strlen(s0),
429 if (space_split && **map == *CHR_SPACE)
432 /* add non-space char */
433 memcpy(dst_term+j, s1, s0 - s1);
439 if (strchr(REGEX_CHARS, *s1))
447 esc_str(tmpbuf, sizeof(tmpbuf), map[0], strlen(map[0]));
449 strcpy(dst + i, map[0]);
455 dst_term[j++] = '\0';
460 /* term_103: handle term, where trunc = re-2 (regular expressions) */
461 static int term_103(ZebraMaps zebra_maps, int reg_type, const char **src,
462 char *dst, int *errors, int space_split,
470 if (!term_pre(zebra_maps, reg_type, src, "^\\()[].*+?|", "(", !space_split))
473 if (errors && *s0 == '+' && s0[1] && s0[2] == '+' && s0[3] &&
474 isdigit(((const unsigned char *)s0)[1]))
476 *errors = s0[1] - '0';
483 if (strchr("^\\()[].*+?|-", *s0))
492 map = zebra_maps_search(zebra_maps, reg_type, &s0, strlen(s0),
494 if (space_split && **map == *CHR_SPACE)
497 /* add non-space char */
498 memcpy(dst_term+j, s1, s0 - s1);
504 if (strchr(REGEX_CHARS, *s1))
512 esc_str(tmpbuf, sizeof(tmpbuf), map[0], strlen(map[0]));
514 strcpy(dst + i, map[0]);
526 /* term_103: handle term, where trunc = re-1 (regular expressions) */
527 static int term_102 (ZebraMaps zebra_maps, int reg_type, const char **src,
528 char *dst, int space_split, char *dst_term)
530 return term_103(zebra_maps, reg_type, src, dst, NULL, space_split,
535 /* term_104: handle term, where trunc = Process # and ! */
536 static int term_104(ZebraMaps zebra_maps, int reg_type,
537 const char **src, char *dst, int space_split,
545 if (!term_pre(zebra_maps, reg_type, src, "?*#", "?*#", !space_split))
552 dst_term[j++] = *s0++;
553 if (*s0 >= '0' && *s0 <= '9')
556 while (*s0 >= '0' && *s0 <= '9')
558 limit = limit * 10 + (*s0 - '0');
559 dst_term[j++] = *s0++;
579 dst_term[j++] = *s0++;
584 dst_term[j++] = *s0++;
589 map = zebra_maps_input(zebra_maps, reg_type, &s0, strlen(s0), 0);
590 if (space_split && **map == *CHR_SPACE)
594 if (strchr(REGEX_CHARS, *s1))
602 dst_term[j++] = '\0';
607 /* term_105/106: handle term, where trunc = Process * and ! and right trunc */
608 static int term_105 (ZebraMaps zebra_maps, int reg_type,
609 const char **src, char *dst, int space_split,
610 char *dst_term, int right_truncate)
617 if (!term_pre(zebra_maps, reg_type, src, "*!", "*!", !space_split))
626 dst_term[j++] = *s0++;
631 dst_term[j++] = *s0++;
636 map = zebra_maps_input(zebra_maps, reg_type, &s0, strlen(s0), 0);
637 if (space_split && **map == *CHR_SPACE)
641 if (strchr(REGEX_CHARS, *s1))
655 dst_term[j++] = '\0';
661 /* gen_regular_rel - generate regular expression from relation
662 * val: border value (inclusive)
663 * islt: 1 if <=; 0 if >=.
665 static void gen_regular_rel(char *dst, int val, int islt)
672 yaz_log(YLOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
676 strcpy(dst, "(-[0-9]+|(");
684 strcpy(dst, "([0-9]+|-(");
696 sprintf(numstr, "%d", val);
697 for (w = strlen(numstr); --w >= 0; pos++)
716 strcpy(dst + dst_p, numstr);
717 dst_p = strlen(dst) - pos - 1;
745 for (i = 0; i<pos; i++)
758 /* match everything less than 10^(pos-1) */
760 for (i = 1; i<pos; i++)
761 strcat(dst, "[0-9]?");
765 /* match everything greater than 10^pos */
766 for (i = 0; i <= pos; i++)
767 strcat(dst, "[0-9]");
768 strcat(dst, "[0-9]*");
773 void string_rel_add_char(char **term_p, const char *src, int *indx)
775 if (src[*indx] == '\\')
776 *(*term_p)++ = src[(*indx)++];
777 *(*term_p)++ = src[(*indx)++];
781 * > abc ([b-].*|a[c-].*|ab[d-].*|abc.+)
782 * ([^-a].*|a[^-b].*ab[^-c].*|abc.+)
783 * >= abc ([b-].*|a[c-].*|ab[c-].*)
784 * ([^-a].*|a[^-b].*|ab[c-].*)
785 * < abc ([-0].*|a[-a].*|ab[-b].*)
786 * ([^a-].*|a[^b-].*|ab[^c-].*)
787 * <= abc ([-0].*|a[-a].*|ab[-b].*|abc)
788 * ([^a-].*|a[^b-].*|ab[^c-].*|abc)
790 static int string_relation(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
791 const char **term_sub, char *term_dict,
792 oid_value attributeSet,
793 int reg_type, int space_split, char *term_dst)
798 char *term_tmp = term_dict + strlen(term_dict);
799 char term_component[2*IT_MAX_WORD+20];
801 attr_init(&relation, zapt, 2);
802 relation_value = attr_find(&relation, NULL);
804 yaz_log(YLOG_DEBUG, "string relation value=%d", relation_value);
805 switch (relation_value)
808 if (!term_100 (zh->reg->zebra_maps, reg_type,
809 term_sub, term_component,
810 space_split, term_dst))
812 yaz_log(log_level_rpn, "Relation <");
815 for (i = 0; term_component[i]; )
822 string_rel_add_char (&term_tmp, term_component, &j);
827 string_rel_add_char (&term_tmp, term_component, &i);
834 if ((term_tmp - term_dict) > IT_MAX_WORD)
841 if (!term_100 (zh->reg->zebra_maps, reg_type,
842 term_sub, term_component,
843 space_split, term_dst))
845 yaz_log(log_level_rpn, "Relation <=");
848 for (i = 0; term_component[i]; )
853 string_rel_add_char (&term_tmp, term_component, &j);
857 string_rel_add_char (&term_tmp, term_component, &i);
866 if ((term_tmp - term_dict) > IT_MAX_WORD)
869 for (i = 0; term_component[i]; )
870 string_rel_add_char (&term_tmp, term_component, &i);
875 if (!term_100 (zh->reg->zebra_maps, reg_type,
876 term_sub, term_component, space_split, term_dst))
878 yaz_log(log_level_rpn, "Relation >");
881 for (i = 0; term_component[i];)
886 string_rel_add_char (&term_tmp, term_component, &j);
891 string_rel_add_char (&term_tmp, term_component, &i);
899 if ((term_tmp - term_dict) > IT_MAX_WORD)
902 for (i = 0; term_component[i];)
903 string_rel_add_char (&term_tmp, term_component, &i);
910 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub,
911 term_component, space_split, term_dst))
913 yaz_log(log_level_rpn, "Relation >=");
916 for (i = 0; term_component[i];)
923 string_rel_add_char (&term_tmp, term_component, &j);
926 if (term_component[i+1])
930 string_rel_add_char (&term_tmp, term_component, &i);
934 string_rel_add_char (&term_tmp, term_component, &i);
941 if ((term_tmp - term_dict) > IT_MAX_WORD)
949 yaz_log(log_level_rpn, "Relation =");
950 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub,
951 term_component, space_split, term_dst))
953 strcat(term_tmp, "(");
954 strcat(term_tmp, term_component);
955 strcat(term_tmp, ")");
960 static int string_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
961 const char **term_sub,
962 oid_value attributeSet, NMEM stream,
963 struct grep_info *grep_info,
964 int reg_type, int complete_flag,
965 int num_bases, char **basenames,
966 char *term_dst, int xpath_use);
968 static RSET term_trunc(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
969 const char **term_sub,
970 oid_value attributeSet, NMEM stream,
971 struct grep_info *grep_info,
972 int reg_type, int complete_flag,
973 int num_bases, char **basenames,
975 const char *rank_type, int xpath_use,
979 grep_info->isam_p_indx = 0;
980 r = string_term(zh, zapt, term_sub, attributeSet, stream, grep_info,
981 reg_type, complete_flag, num_bases, basenames,
982 term_dst, xpath_use);
985 yaz_log(log_level_rpn, "term: %s", term_dst);
986 return rset_trunc(zh, grep_info->isam_p_buf,
987 grep_info->isam_p_indx, term_dst,
988 strlen(term_dst), rank_type, 1 /* preserve pos */,
989 zapt->term->which, rset_nmem,
990 key_it_ctrl,key_it_ctrl->scope);
992 static char *nmem_strdup_i(NMEM nmem, int v)
995 sprintf (val_str, "%d", v);
996 return nmem_strdup(nmem, val_str);
999 static int string_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1000 const char **term_sub,
1001 oid_value attributeSet, NMEM stream,
1002 struct grep_info *grep_info,
1003 int reg_type, int complete_flag,
1004 int num_bases, char **basenames,
1005 char *term_dst, int xpath_use)
1007 char term_dict[2*IT_MAX_WORD+4000];
1009 AttrType truncation;
1010 int truncation_value;
1013 const char *use_string = 0;
1014 oid_value curAttributeSet = attributeSet;
1016 struct rpn_char_map_info rcmi;
1017 int space_split = complete_flag ? 0 : 1;
1019 int bases_ok = 0; /* no of databases with OK attribute */
1020 int errCode = 0; /* err code (if any is not OK) */
1021 char *errString = 0; /* addinfo */
1023 rpn_char_map_prepare (zh->reg, reg_type, &rcmi);
1024 attr_init (&use, zapt, 1);
1025 use_value = attr_find_ex (&use, &curAttributeSet, &use_string);
1026 yaz_log(log_level_rpn, "string_term, use value %d", use_value);
1027 attr_init (&truncation, zapt, 5);
1028 truncation_value = attr_find (&truncation, NULL);
1029 yaz_log(log_level_rpn, "truncation value %d", truncation_value);
1031 if (use_value == -1) /* no attribute - assumy "any" */
1033 for (base_no = 0; base_no < num_bases; base_no++)
1037 int regex_range = 0;
1040 data1_local_attribute id_xpath_attr;
1041 data1_local_attribute *local_attr;
1042 int max_pos, prefix_len = 0;
1046 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
1048 zh->errCode = 109; /* Database unavailable */
1049 zh->errString = basenames[base_no];
1052 if (xpath_use > 0 && use_value == -2)
1054 /* xpath mode and we have a string attribute */
1055 attp.local_attributes = &id_xpath_attr;
1056 attp.attset_ordinal = VAL_IDXPATH;
1057 id_xpath_attr.next = 0;
1059 use_value = xpath_use; /* xpath_use as use-attribute now */
1060 id_xpath_attr.local = use_value;
1062 else if (curAttributeSet == VAL_IDXPATH && use_value >= 0)
1064 /* X-Path attribute, use numeric value directly */
1065 attp.local_attributes = &id_xpath_attr;
1066 attp.attset_ordinal = VAL_IDXPATH;
1067 id_xpath_attr.next = 0;
1068 id_xpath_attr.local = use_value;
1070 else if (use_string &&
1071 (ord = zebraExplain_lookup_attr_str(zh->reg->zei,
1074 /* we have a match for a raw string attribute */
1079 term_dict[prefix_len++] = '|';
1081 term_dict[prefix_len++] = '(';
1083 ord_len = key_SU_encode (ord, ord_buf);
1084 for (i = 0; i<ord_len; i++)
1086 term_dict[prefix_len++] = 1;
1087 term_dict[prefix_len++] = ord_buf[i];
1089 attp.local_attributes = 0; /* no more attributes */
1093 /* lookup in the .att files . Allow string as well */
1094 if ((r = att_getentbyatt (zh, &attp, curAttributeSet, use_value,
1097 yaz_log(YLOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1098 curAttributeSet, use_value, r);
1101 /* set was found, but value wasn't defined */
1104 errString = nmem_strdup(stream, use_string);
1106 errString = nmem_strdup_i (stream, use_value);
1111 struct oident oident;
1113 oident.proto = PROTO_Z3950;
1114 oident.oclass = CLASS_ATTSET;
1115 oident.value = curAttributeSet;
1116 oid_ent_to_oid (&oident, oid);
1119 errString = nmem_strdup (stream, oident.desc);
1124 for (local_attr = attp.local_attributes; local_attr;
1125 local_attr = local_attr->next)
1130 ord = zebraExplain_lookup_attr_su(zh->reg->zei,
1131 attp.attset_ordinal,
1136 term_dict[prefix_len++] = '|';
1138 term_dict[prefix_len++] = '(';
1140 ord_len = key_SU_encode (ord, ord_buf);
1141 for (i = 0; i<ord_len; i++)
1143 term_dict[prefix_len++] = 1;
1144 term_dict[prefix_len++] = ord_buf[i];
1153 errString = nmem_strdup_i(stream, use_value);
1159 bases_ok++; /* this has OK attributes */
1163 term_dict[prefix_len++] = ')';
1164 term_dict[prefix_len++] = 1;
1165 term_dict[prefix_len++] = reg_type;
1166 yaz_log(log_level_rpn, "reg_type = %d", term_dict[prefix_len-1]);
1167 term_dict[prefix_len] = '\0';
1169 switch (truncation_value)
1171 case -1: /* not specified */
1172 case 100: /* do not truncate */
1173 if (!string_relation (zh, zapt, &termp, term_dict,
1175 reg_type, space_split, term_dst))
1178 case 1: /* right truncation */
1179 term_dict[j++] = '(';
1180 if (!term_100(zh->reg->zebra_maps, reg_type,
1181 &termp, term_dict + j, space_split, term_dst))
1183 strcat(term_dict, ".*)");
1185 case 2: /* keft truncation */
1186 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1187 if (!term_100(zh->reg->zebra_maps, reg_type,
1188 &termp, term_dict + j, space_split, term_dst))
1190 strcat(term_dict, ")");
1192 case 3: /* left&right truncation */
1193 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1194 if (!term_100(zh->reg->zebra_maps, reg_type,
1195 &termp, term_dict + j, space_split, term_dst))
1197 strcat(term_dict, ".*)");
1199 case 101: /* process # in term */
1200 term_dict[j++] = '(';
1201 if (!term_101(zh->reg->zebra_maps, reg_type,
1202 &termp, term_dict + j, space_split, term_dst))
1204 strcat(term_dict, ")");
1206 case 102: /* Regexp-1 */
1207 term_dict[j++] = '(';
1208 if (!term_102(zh->reg->zebra_maps, reg_type,
1209 &termp, term_dict + j, space_split, term_dst))
1211 strcat(term_dict, ")");
1213 case 103: /* Regexp-2 */
1215 term_dict[j++] = '(';
1217 if (!term_103 (zh->reg->zebra_maps, reg_type,
1218 &termp, term_dict + j, ®ex_range,
1219 space_split, term_dst))
1221 strcat(term_dict, ")");
1222 case 104: /* process # and ! in term */
1223 term_dict[j++] = '(';
1224 if (!term_104 (zh->reg->zebra_maps, reg_type,
1225 &termp, term_dict + j, space_split, term_dst))
1227 strcat(term_dict, ")");
1229 case 105: /* process * and ! in term */
1230 term_dict[j++] = '(';
1231 if (!term_105 (zh->reg->zebra_maps, reg_type,
1232 &termp, term_dict + j, space_split, term_dst, 1))
1234 strcat(term_dict, ")");
1236 case 106: /* process * and ! in term */
1237 term_dict[j++] = '(';
1238 if (!term_105 (zh->reg->zebra_maps, reg_type,
1239 &termp, term_dict + j, space_split, term_dst, 0))
1241 strcat(term_dict, ")");
1245 zh->errString = nmem_strdup_i(stream, truncation_value);
1251 const char *input = term_dict + prefix_len;
1252 esc_str(buf, sizeof(buf), input, strlen(input));
1256 yaz_log(log_level_rpn, "dict_lookup_grep: %s", term_dict+prefix_len);
1257 r = dict_lookup_grep(zh->reg->dict, term_dict, regex_range,
1258 grep_info, &max_pos, init_pos,
1261 yaz_log(YLOG_WARN, "dict_lookup_grep fail %d", r);
1266 zh->errCode = errCode;
1267 zh->errString = errString;
1271 yaz_log(YLOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1276 /* convert APT search term to UTF8 */
1277 static int zapt_term_to_utf8 (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1281 Z_Term *term = zapt->term;
1283 switch (term->which)
1285 case Z_Term_general:
1286 if (zh->iconv_to_utf8 != 0)
1288 char *inbuf = term->u.general->buf;
1289 size_t inleft = term->u.general->len;
1290 char *outbuf = termz;
1291 size_t outleft = IT_MAX_WORD-1;
1294 ret = yaz_iconv(zh->iconv_to_utf8, &inbuf, &inleft,
1296 if (ret == (size_t)(-1))
1298 ret = yaz_iconv(zh->iconv_to_utf8, 0, 0, 0, 0);
1306 sizez = term->u.general->len;
1307 if (sizez > IT_MAX_WORD-1)
1308 sizez = IT_MAX_WORD-1;
1309 memcpy (termz, term->u.general->buf, sizez);
1310 termz[sizez] = '\0';
1313 case Z_Term_characterString:
1314 sizez = strlen(term->u.characterString);
1315 if (sizez > IT_MAX_WORD-1)
1316 sizez = IT_MAX_WORD-1;
1317 memcpy (termz, term->u.characterString, sizez);
1318 termz[sizez] = '\0';
1327 /* convert APT SCAN term to internal cmap */
1328 static int trans_scan_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1329 char *termz, int reg_type)
1331 char termz0[IT_MAX_WORD];
1333 if (zapt_term_to_utf8(zh, zapt, termz0))
1334 return -1; /* error */
1338 const char *cp = (const char *) termz0;
1339 const char *cp_end = cp + strlen(cp);
1342 const char *space_map = NULL;
1345 while ((len = (cp_end - cp)) > 0)
1347 map = zebra_maps_input (zh->reg->zebra_maps, reg_type, &cp, len, 0);
1348 if (**map == *CHR_SPACE)
1353 for (src = space_map; *src; src++)
1356 for (src = *map; *src; src++)
1365 char *normalize_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1366 const char *termz, NMEM stream, unsigned reg_id)
1369 AttrType truncation;
1370 int truncation_value;
1373 attr_init (&truncation, zapt, 5);
1374 truncation_value = attr_find (&truncation, NULL);
1376 switch (truncation_value)
1396 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, ex_list,
1397 termz, strlen(termz));
1399 return nmem_strdup(stream, termz);
1402 char *buf = (char*) nmem_malloc(stream, wrbuf_len(wrbuf)+1);
1403 memcpy (buf, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1404 buf[wrbuf_len(wrbuf)] = '\0';
1409 static void grep_info_delete (struct grep_info *grep_info)
1412 xfree(grep_info->term_no);
1414 xfree (grep_info->isam_p_buf);
1417 static int grep_info_prepare (ZebraHandle zh,
1418 Z_AttributesPlusTerm *zapt,
1419 struct grep_info *grep_info,
1424 int termset_value_numeric;
1425 const char *termset_value_string;
1428 grep_info->term_no = 0;
1430 grep_info->isam_p_size = 0;
1431 grep_info->isam_p_buf = NULL;
1433 grep_info->reg_type = reg_type;
1434 grep_info->termset = 0;
1438 attr_init (&termset, zapt, 8);
1439 termset_value_numeric =
1440 attr_find_ex (&termset, NULL, &termset_value_string);
1441 if (termset_value_numeric != -1)
1444 const char *termset_name = 0;
1445 if (termset_value_numeric != -2)
1448 sprintf (resname, "%d", termset_value_numeric);
1449 termset_name = resname;
1452 termset_name = termset_value_string;
1453 yaz_log(log_level_rpn, "creating termset set %s", termset_name);
1454 grep_info->termset = resultSetAdd (zh, termset_name, 1);
1455 if (!grep_info->termset)
1458 zh->errString = nmem_strdup (stream, termset_name);
1466 static RSET rpn_search_APT_phrase (ZebraHandle zh,
1467 Z_AttributesPlusTerm *zapt,
1468 const char *termz_org,
1469 oid_value attributeSet,
1471 int reg_type, int complete_flag,
1472 const char *rank_type, int xpath_use,
1473 int num_bases, char **basenames,
1476 char term_dst[IT_MAX_WORD+1];
1477 RSET rset[TERM_LIST_LENGTH_MAX], result;
1479 struct grep_info grep_info;
1480 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1481 const char *termp = termz;
1484 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1486 for (; rset_no < sizeof(rset)/sizeof(*rset); rset_no++)
1488 yaz_log(log_level_rpn, "APT_phrase termp=%s", termp);
1489 rset[rset_no] = term_trunc(zh, zapt, &termp, attributeSet,
1491 reg_type, complete_flag,
1492 num_bases, basenames,
1493 term_dst, rank_type,
1494 xpath_use,rset_nmem);
1498 grep_info_delete (&grep_info);
1500 return rsnull_create (rset_nmem,key_it_ctrl);
1501 else if (rset_no == 1)
1504 result = rsprox_create( rset_nmem, key_it_ctrl, key_it_ctrl->scope,
1506 1 /* ordered */, 0 /* exclusion */,
1507 3 /* relation */, 1 /* distance */);
1511 static RSET rpn_search_APT_or_list (ZebraHandle zh,
1512 Z_AttributesPlusTerm *zapt,
1513 const char *termz_org,
1514 oid_value attributeSet,
1516 int reg_type, int complete_flag,
1517 const char *rank_type,
1519 int num_bases, char **basenames,
1522 char term_dst[IT_MAX_WORD+1];
1523 RSET rset[TERM_LIST_LENGTH_MAX];
1525 struct grep_info grep_info;
1526 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1527 const char *termp = termz;
1529 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1531 for (; rset_no < sizeof(rset)/sizeof(*rset); rset_no++)
1533 yaz_log(log_level_rpn, "APT_or_list termp=%s", termp);
1534 rset[rset_no] = term_trunc(zh, zapt, &termp, attributeSet,
1536 reg_type, complete_flag,
1537 num_bases, basenames,
1538 term_dst, rank_type,
1539 xpath_use,rset_nmem);
1543 grep_info_delete (&grep_info);
1545 return rsnull_create (rset_nmem,key_it_ctrl);
1546 return rsmulti_or_create(rset_nmem, key_it_ctrl,key_it_ctrl->scope,
1550 static RSET rpn_search_APT_and_list (ZebraHandle zh,
1551 Z_AttributesPlusTerm *zapt,
1552 const char *termz_org,
1553 oid_value attributeSet,
1555 int reg_type, int complete_flag,
1556 const char *rank_type,
1558 int num_bases, char **basenames,
1561 char term_dst[IT_MAX_WORD+1];
1562 RSET rset[TERM_LIST_LENGTH_MAX];
1564 struct grep_info grep_info;
1565 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1566 const char *termp = termz;
1568 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1570 for (; rset_no < sizeof(rset)/sizeof(*rset); rset_no++)
1572 yaz_log(log_level_rpn, "APT_and_list termp=%s", termp);
1573 rset[rset_no] = term_trunc(zh, zapt, &termp, attributeSet,
1575 reg_type, complete_flag,
1576 num_bases, basenames,
1577 term_dst, rank_type,
1578 xpath_use, rset_nmem);
1582 grep_info_delete (&grep_info);
1584 return rsnull_create(rset_nmem,key_it_ctrl);
1586 return rsmulti_and_create(rset_nmem, key_it_ctrl, key_it_ctrl->scope,
1590 static int numeric_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1591 const char **term_sub,
1593 oid_value attributeSet,
1594 struct grep_info *grep_info,
1603 char *term_tmp = term_dict + strlen(term_dict);
1605 attr_init (&relation, zapt, 2);
1606 relation_value = attr_find (&relation, NULL);
1608 yaz_log(log_level_rpn, "numeric relation value=%d", relation_value);
1610 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub, term_tmp, 1,
1613 term_value = atoi (term_tmp);
1614 switch (relation_value)
1617 yaz_log(log_level_rpn, "Relation <");
1618 gen_regular_rel (term_tmp, term_value-1, 1);
1621 yaz_log(log_level_rpn, "Relation <=");
1622 gen_regular_rel (term_tmp, term_value, 1);
1625 yaz_log(log_level_rpn, "Relation >=");
1626 gen_regular_rel (term_tmp, term_value, 0);
1629 yaz_log(log_level_rpn, "Relation >");
1630 gen_regular_rel (term_tmp, term_value+1, 0);
1634 yaz_log(log_level_rpn, "Relation =");
1635 sprintf (term_tmp, "(0*%d)", term_value);
1637 yaz_log(log_level_rpn, "dict_lookup_grep: %s", term_tmp);
1638 r = dict_lookup_grep(zh->reg->dict, term_dict, 0, grep_info, max_pos,
1641 yaz_log(YLOG_WARN, "dict_lookup_grep fail, rel = gt: %d", r);
1642 yaz_log(log_level_rpn, "%d positions", grep_info->isam_p_indx);
1646 static int numeric_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1647 const char **term_sub,
1648 oid_value attributeSet, struct grep_info *grep_info,
1649 int reg_type, int complete_flag,
1650 int num_bases, char **basenames,
1651 char *term_dst, int xpath_use, NMEM stream)
1653 char term_dict[2*IT_MAX_WORD+2];
1657 const char *use_string = 0;
1658 oid_value curAttributeSet = attributeSet;
1660 struct rpn_char_map_info rcmi;
1662 int bases_ok = 0; /* no of databases with OK attribute */
1663 int errCode = 0; /* err code (if any is not OK) */
1664 char *errString = 0; /* addinfo */
1666 rpn_char_map_prepare (zh->reg, reg_type, &rcmi);
1667 attr_init (&use, zapt, 1);
1668 use_value = attr_find_ex (&use, &curAttributeSet, &use_string);
1670 if (use_value == -1)
1673 for (base_no = 0; base_no < num_bases; base_no++)
1676 data1_local_attribute id_xpath_attr;
1677 data1_local_attribute *local_attr;
1678 int max_pos, prefix_len = 0;
1681 if (use_value == -2) /* string attribute (assume IDXPATH/any) */
1683 use_value = xpath_use;
1684 attp.local_attributes = &id_xpath_attr;
1685 attp.attset_ordinal = VAL_IDXPATH;
1686 id_xpath_attr.next = 0;
1687 id_xpath_attr.local = use_value;
1689 else if (curAttributeSet == VAL_IDXPATH)
1691 attp.local_attributes = &id_xpath_attr;
1692 attp.attset_ordinal = VAL_IDXPATH;
1693 id_xpath_attr.next = 0;
1694 id_xpath_attr.local = use_value;
1698 if ((r = att_getentbyatt (zh, &attp, curAttributeSet, use_value,
1701 yaz_log(YLOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1702 curAttributeSet, use_value, r);
1705 errString = nmem_strdup_i(stream, use_value);
1713 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
1715 zh->errCode = 109; /* Database unavailable */
1716 zh->errString = basenames[base_no];
1719 for (local_attr = attp.local_attributes; local_attr;
1720 local_attr = local_attr->next)
1726 ord = zebraExplain_lookup_attr_su(zh->reg->zei,
1727 attp.attset_ordinal,
1732 term_dict[prefix_len++] = '|';
1734 term_dict[prefix_len++] = '(';
1736 ord_len = key_SU_encode (ord, ord_buf);
1737 for (i = 0; i<ord_len; i++)
1739 term_dict[prefix_len++] = 1;
1740 term_dict[prefix_len++] = ord_buf[i];
1746 errString = nmem_strdup_i(stream, use_value);
1750 term_dict[prefix_len++] = ')';
1751 term_dict[prefix_len++] = 1;
1752 term_dict[prefix_len++] = reg_type;
1753 yaz_log(YLOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1754 term_dict[prefix_len] = '\0';
1755 if (!numeric_relation (zh, zapt, &termp, term_dict,
1756 attributeSet, grep_info, &max_pos, reg_type,
1762 zh->errCode = errCode;
1763 zh->errString = errString;
1767 yaz_log(YLOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1771 static RSET rpn_search_APT_numeric (ZebraHandle zh,
1772 Z_AttributesPlusTerm *zapt,
1774 oid_value attributeSet,
1776 int reg_type, int complete_flag,
1777 const char *rank_type, int xpath_use,
1778 int num_bases, char **basenames,
1781 char term_dst[IT_MAX_WORD+1];
1782 const char *termp = termz;
1783 RSET rset[TERM_LIST_LENGTH_MAX];
1786 struct grep_info grep_info;
1788 yaz_log(log_level_rpn, "APT_numeric t='%s'",termz);
1789 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1791 for (; rset_no < sizeof(rset)/sizeof(*rset); rset_no++)
1793 yaz_log(YLOG_DEBUG, "APT_numeric termp=%s", termp);
1794 grep_info.isam_p_indx = 0;
1795 r = numeric_term(zh, zapt, &termp, attributeSet, &grep_info,
1796 reg_type, complete_flag, num_bases, basenames,
1797 term_dst, xpath_use,
1801 yaz_log(YLOG_DEBUG, "term: %s", term_dst);
1802 rset[rset_no] = rset_trunc(zh, grep_info.isam_p_buf,
1803 grep_info.isam_p_indx, term_dst,
1804 strlen(term_dst), rank_type,
1805 0 /* preserve position */,
1806 zapt->term->which, rset_nmem,
1807 key_it_ctrl,key_it_ctrl->scope);
1811 grep_info_delete (&grep_info);
1813 return rsnull_create(rset_nmem,key_it_ctrl);
1816 return rsmulti_and_create(rset_nmem,key_it_ctrl,key_it_ctrl->scope,
1820 static RSET rpn_search_APT_local (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1822 oid_value attributeSet,
1824 const char *rank_type, NMEM rset_nmem)
1830 result = rstemp_create( rset_nmem,key_it_ctrl,key_it_ctrl->scope,
1831 res_get (zh->res, "setTmpDir"),0 );
1832 rsfd = rset_open (result, RSETF_WRITE);
1840 rset_write (rsfd, &key);
1845 static RSET rpn_sort_spec (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1846 oid_value attributeSet, NMEM stream,
1847 Z_SortKeySpecList *sort_sequence,
1848 const char *rank_type)
1851 int sort_relation_value;
1852 AttrType sort_relation_type;
1857 Z_AttributeElement *ae;
1862 attr_init (&sort_relation_type, zapt, 7);
1863 sort_relation_value = attr_find (&sort_relation_type, &attributeSet);
1865 attr_init (&use_type, zapt, 1);
1866 use_value = attr_find (&use_type, &attributeSet);
1868 if (!sort_sequence->specs)
1870 sort_sequence->num_specs = 10;
1871 sort_sequence->specs = (Z_SortKeySpec **)
1872 nmem_malloc(stream, sort_sequence->num_specs *
1873 sizeof(*sort_sequence->specs));
1874 for (i = 0; i<sort_sequence->num_specs; i++)
1875 sort_sequence->specs[i] = 0;
1877 if (zapt->term->which != Z_Term_general)
1880 i = atoi_n ((char *) zapt->term->u.general->buf,
1881 zapt->term->u.general->len);
1882 if (i >= sort_sequence->num_specs)
1884 sprintf (termz, "%d", i);
1886 oe.proto = PROTO_Z3950;
1887 oe.oclass = CLASS_ATTSET;
1888 oe.value = attributeSet;
1889 if (!oid_ent_to_oid (&oe, oid))
1892 sks = (Z_SortKeySpec *) nmem_malloc(stream, sizeof(*sks));
1893 sks->sortElement = (Z_SortElement *)
1894 nmem_malloc(stream, sizeof(*sks->sortElement));
1895 sks->sortElement->which = Z_SortElement_generic;
1896 sk = sks->sortElement->u.generic = (Z_SortKey *)
1897 nmem_malloc(stream, sizeof(*sk));
1898 sk->which = Z_SortKey_sortAttributes;
1899 sk->u.sortAttributes = (Z_SortAttributes *)
1900 nmem_malloc(stream, sizeof(*sk->u.sortAttributes));
1902 sk->u.sortAttributes->id = oid;
1903 sk->u.sortAttributes->list = (Z_AttributeList *)
1904 nmem_malloc(stream, sizeof(*sk->u.sortAttributes->list));
1905 sk->u.sortAttributes->list->num_attributes = 1;
1906 sk->u.sortAttributes->list->attributes = (Z_AttributeElement **)
1907 nmem_malloc(stream, sizeof(*sk->u.sortAttributes->list->attributes));
1908 ae = *sk->u.sortAttributes->list->attributes = (Z_AttributeElement *)
1909 nmem_malloc(stream, sizeof(**sk->u.sortAttributes->list->attributes));
1910 ae->attributeSet = 0;
1911 ae->attributeType = (int *)
1912 nmem_malloc(stream, sizeof(*ae->attributeType));
1913 *ae->attributeType = 1;
1914 ae->which = Z_AttributeValue_numeric;
1915 ae->value.numeric = (int *)
1916 nmem_malloc(stream, sizeof(*ae->value.numeric));
1917 *ae->value.numeric = use_value;
1919 sks->sortRelation = (int *)
1920 nmem_malloc(stream, sizeof(*sks->sortRelation));
1921 if (sort_relation_value == 1)
1922 *sks->sortRelation = Z_SortKeySpec_ascending;
1923 else if (sort_relation_value == 2)
1924 *sks->sortRelation = Z_SortKeySpec_descending;
1926 *sks->sortRelation = Z_SortKeySpec_ascending;
1928 sks->caseSensitivity = (int *)
1929 nmem_malloc(stream, sizeof(*sks->caseSensitivity));
1930 *sks->caseSensitivity = 0;
1932 sks->which = Z_SortKeySpec_null;
1933 sks->u.null = odr_nullval ();
1934 sort_sequence->specs[i] = sks;
1935 return rsnull_create (NULL,key_it_ctrl);
1936 /* FIXME - nmem?? */
1940 static int parse_xpath(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1941 oid_value attributeSet,
1942 struct xpath_location_step *xpath, int max, NMEM mem)
1944 oid_value curAttributeSet = attributeSet;
1946 const char *use_string = 0;
1948 attr_init (&use, zapt, 1);
1949 attr_find_ex (&use, &curAttributeSet, &use_string);
1951 if (!use_string || *use_string != '/')
1954 return zebra_parse_xpath_str(use_string, xpath, max, mem);
1959 static RSET xpath_trunc(ZebraHandle zh, NMEM stream,
1960 int reg_type, const char *term, int use,
1961 oid_value curAttributeSet, NMEM rset_nmem)
1964 struct grep_info grep_info;
1965 char term_dict[2048];
1968 int ord = zebraExplain_lookup_attr_su(zh->reg->zei, curAttributeSet, use);
1969 int ord_len, i, r, max_pos;
1970 int term_type = Z_Term_characterString;
1971 const char *flags = "void";
1973 if (grep_info_prepare (zh, 0 /* zapt */, &grep_info, '0', stream))
1974 return rsnull_create (rset_nmem,key_it_ctrl);
1977 return rsnull_create (rset_nmem,key_it_ctrl);
1979 term_dict[prefix_len++] = '|';
1981 term_dict[prefix_len++] = '(';
1983 ord_len = key_SU_encode (ord, ord_buf);
1984 for (i = 0; i<ord_len; i++)
1986 term_dict[prefix_len++] = 1;
1987 term_dict[prefix_len++] = ord_buf[i];
1989 term_dict[prefix_len++] = ')';
1990 term_dict[prefix_len++] = 1;
1991 term_dict[prefix_len++] = reg_type;
1993 strcpy(term_dict+prefix_len, term);
1995 grep_info.isam_p_indx = 0;
1996 r = dict_lookup_grep(zh->reg->dict, term_dict, 0,
1997 &grep_info, &max_pos, 0, grep_handle);
1998 yaz_log (YLOG_DEBUG, "%s %d positions", term,
1999 grep_info.isam_p_indx);
2000 rset = rset_trunc(zh, grep_info.isam_p_buf,
2001 grep_info.isam_p_indx, term, strlen(term),
2002 flags, 1, term_type,rset_nmem,
2003 key_it_ctrl, key_it_ctrl->scope);
2004 grep_info_delete (&grep_info);
2008 static RSET rpn_search_xpath (ZebraHandle zh,
2009 oid_value attributeSet,
2010 int num_bases, char **basenames,
2011 NMEM stream, const char *rank_type, RSET rset,
2012 int xpath_len, struct xpath_location_step *xpath,
2015 oid_value curAttributeSet = attributeSet;
2022 yaz_log (YLOG_DEBUG, "xpath len=%d", xpath_len);
2023 for (i = 0; i<xpath_len; i++)
2025 yaz_log (log_level_rpn, "XPATH %d %s", i, xpath[i].part);
2029 curAttributeSet = VAL_IDXPATH;
2039 a[@attr = value]/b[@other = othervalue]
2041 /e/@a val range(e/,range(@a,freetext(w,1015,val),@a),e/)
2042 /a/b val range(b/a/,freetext(w,1016,val),b/a/)
2043 /a/b/@c val range(b/a/,range(@c,freetext(w,1016,val),@c),b/a/)
2044 /a/b[@c = y] val range(b/a/,freetext(w,1016,val),b/a/,@c = y)
2045 /a[@c = y]/b val range(a/,range(b/a/,freetext(w,1016,val),b/a/),a/,@c = y)
2046 /a[@c = x]/b[@c = y] range(a/,range(b/a/,freetext(w,1016,val),b/a/,@c = y),a/,@c = x)
2050 dict_grep_cmap (zh->reg->dict, 0, 0);
2052 for (base_no = 0; base_no < num_bases; base_no++)
2054 int level = xpath_len;
2057 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
2059 zh->errCode = 109; /* Database unavailable */
2060 zh->errString = basenames[base_no];
2063 while (--level >= 0)
2065 char xpath_rev[128];
2067 RSET rset_start_tag = 0, rset_end_tag = 0, rset_attr = 0;
2071 for (i = level; i >= 1; --i)
2073 const char *cp = xpath[i].part;
2079 memcpy (xpath_rev + len, "[^/]*", 5);
2082 else if (*cp == ' ')
2085 xpath_rev[len++] = 1;
2086 xpath_rev[len++] = ' ';
2090 xpath_rev[len++] = *cp;
2091 xpath_rev[len++] = '/';
2093 else if (i == 1) /* // case */
2095 xpath_rev[len++] = '.';
2096 xpath_rev[len++] = '*';
2101 if (xpath[level].predicate &&
2102 xpath[level].predicate->which == XPATH_PREDICATE_RELATION &&
2103 xpath[level].predicate->u.relation.name[0])
2105 WRBUF wbuf = wrbuf_alloc();
2106 wrbuf_puts(wbuf, xpath[level].predicate->u.relation.name+1);
2107 if (xpath[level].predicate->u.relation.value)
2109 const char *cp = xpath[level].predicate->u.relation.value;
2110 wrbuf_putc(wbuf, '=');
2114 if (strchr(REGEX_CHARS, *cp))
2115 wrbuf_putc(wbuf, '\\');
2116 wrbuf_putc(wbuf, *cp);
2120 wrbuf_puts(wbuf, "");
2121 rset_attr = xpath_trunc(
2122 zh, stream, '0', wrbuf_buf(wbuf), 3,
2123 curAttributeSet,rset_nmem);
2124 wrbuf_free(wbuf, 1);
2131 yaz_log (log_level_rpn, "xpath_rev (%d) = %s", level, xpath_rev);
2132 if (strlen(xpath_rev))
2134 rset_start_tag = xpath_trunc(zh, stream, '0',
2135 xpath_rev, 1, curAttributeSet, rset_nmem);
2137 rset_end_tag = xpath_trunc(zh, stream, '0',
2138 xpath_rev, 2, curAttributeSet, rset_nmem);
2140 rset = rsbetween_create(rset_nmem, key_it_ctrl,
2142 rset_start_tag, rset,
2143 rset_end_tag, rset_attr);
2154 static RSET rpn_search_APT (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2155 oid_value attributeSet, NMEM stream,
2156 Z_SortKeySpecList *sort_sequence,
2157 int num_bases, char **basenames,
2161 char *search_type = NULL;
2162 char rank_type[128];
2165 char termz[IT_MAX_WORD+1];
2169 struct xpath_location_step xpath[10];
2173 log_level_rpn = yaz_log_module_level("rpn");
2176 zebra_maps_attr (zh->reg->zebra_maps, zapt, ®_id, &search_type,
2177 rank_type, &complete_flag, &sort_flag);
2179 yaz_log(YLOG_DEBUG, "reg_id=%c", reg_id);
2180 yaz_log(YLOG_DEBUG, "complete_flag=%d", complete_flag);
2181 yaz_log(YLOG_DEBUG, "search_type=%s", search_type);
2182 yaz_log(YLOG_DEBUG, "rank_type=%s", rank_type);
2184 if (zapt_term_to_utf8(zh, zapt, termz))
2188 return rpn_sort_spec (zh, zapt, attributeSet, stream, sort_sequence,
2190 xpath_len = parse_xpath(zh, zapt, attributeSet, xpath, 10, stream);
2194 if (xpath[xpath_len-1].part[0] == '@')
2198 if (!strcmp (search_type, "phrase"))
2200 rset = rpn_search_APT_phrase (zh, zapt, termz, attributeSet, stream,
2201 reg_id, complete_flag, rank_type,
2203 num_bases, basenames, rset_nmem);
2205 else if (!strcmp (search_type, "and-list"))
2207 rset = rpn_search_APT_and_list (zh, zapt, termz, attributeSet, stream,
2208 reg_id, complete_flag, rank_type,
2210 num_bases, basenames, rset_nmem);
2212 else if (!strcmp (search_type, "or-list"))
2214 rset = rpn_search_APT_or_list (zh, zapt, termz, attributeSet, stream,
2215 reg_id, complete_flag, rank_type,
2217 num_bases, basenames, rset_nmem);
2219 else if (!strcmp (search_type, "local"))
2221 rset = rpn_search_APT_local (zh, zapt, termz, attributeSet, stream,
2222 rank_type, rset_nmem);
2224 else if (!strcmp (search_type, "numeric"))
2226 rset = rpn_search_APT_numeric (zh, zapt, termz, attributeSet, stream,
2227 reg_id, complete_flag, rank_type,
2229 num_bases, basenames, rset_nmem);
2231 else if (!strcmp (search_type, "always"))
2237 return rpn_search_xpath (zh, attributeSet, num_bases, basenames,
2238 stream, rank_type, rset,
2239 xpath_len, xpath, rset_nmem);
2242 static RSET rpn_search_structure (ZebraHandle zh, Z_RPNStructure *zs,
2243 oid_value attributeSet,
2244 NMEM stream, NMEM rset_nmem,
2245 Z_SortKeySpecList *sort_sequence,
2246 int num_bases, char **basenames)
2249 if (zs->which == Z_RPNStructure_complex)
2251 Z_Operator *zop = zs->u.complex->roperator;
2252 RSET rsets[2]; /* l and r argument */
2254 rsets[0]=rpn_search_structure (zh, zs->u.complex->s1,
2255 attributeSet, stream, rset_nmem,
2257 num_bases, basenames);
2258 if (rsets[0] == NULL)
2260 rsets[1]=rpn_search_structure (zh, zs->u.complex->s2,
2261 attributeSet, stream, rset_nmem,
2263 num_bases, basenames);
2264 if (rsets[1] == NULL)
2266 rset_delete (rsets[0]);
2272 case Z_Operator_and:
2273 r = rsmulti_and_create(rset_nmem, key_it_ctrl, key_it_ctrl->scope,
2277 r = rsmulti_or_create(rset_nmem, key_it_ctrl, key_it_ctrl->scope,
2280 case Z_Operator_and_not:
2281 r = rsbool_create_not(rset_nmem,key_it_ctrl, key_it_ctrl->scope,
2284 case Z_Operator_prox:
2285 if (zop->u.prox->which != Z_ProximityOperator_known)
2290 if (*zop->u.prox->u.known != Z_ProxUnit_word)
2292 char *val = (char *) nmem_malloc(stream, 16);
2294 zh->errString = val;
2295 sprintf (val, "%d", *zop->u.prox->u.known);
2300 /* new / old prox */
2301 r = rsprox_create(rset_nmem,key_it_ctrl,key_it_ctrl->scope,
2303 *zop->u.prox->ordered,
2304 (!zop->u.prox->exclusion ?
2305 0 : *zop->u.prox->exclusion),
2306 *zop->u.prox->relationType,
2307 *zop->u.prox->distance );
2315 else if (zs->which == Z_RPNStructure_simple)
2317 if (zs->u.simple->which == Z_Operand_APT)
2319 yaz_log(YLOG_DEBUG, "rpn_search_APT");
2320 r = rpn_search_APT (zh, zs->u.simple->u.attributesPlusTerm,
2321 attributeSet, stream, sort_sequence,
2322 num_bases, basenames,rset_nmem);
2324 else if (zs->u.simple->which == Z_Operand_resultSetId)
2326 yaz_log(YLOG_DEBUG, "rpn_search_ref");
2327 r = resultSetRef (zh, zs->u.simple->u.resultSetId);
2330 r = rsnull_create (rset_nmem,key_it_ctrl);
2333 nmem_strdup (stream, zs->u.simple->u.resultSetId);
2354 RSET rpn_search(ZebraHandle zh, NMEM nmem, NMEM rset_nmem,
2355 Z_RPNQuery *rpn, int num_bases, char **basenames,
2356 const char *setname,
2361 oid_value attributeSet;
2362 Z_SortKeySpecList *sort_sequence;
2366 zh->errString = NULL;
2369 sort_sequence = (Z_SortKeySpecList *)
2370 nmem_malloc(nmem, sizeof(*sort_sequence));
2371 sort_sequence->num_specs = 10; /* FIXME - Hard-coded number */
2372 sort_sequence->specs = (Z_SortKeySpec **)
2373 nmem_malloc(nmem, sort_sequence->num_specs *
2374 sizeof(*sort_sequence->specs));
2375 for (i = 0; i<sort_sequence->num_specs; i++)
2376 sort_sequence->specs[i] = 0;
2378 attrset = oid_getentbyoid (rpn->attributeSetId);
2379 attributeSet = attrset->value;
2380 rset = rpn_search_structure (zh, rpn->RPNStructure, attributeSet,
2382 sort_sequence, num_bases, basenames);
2387 yaz_log(YLOG_DEBUG, "search error: %d", zh->errCode);
2389 for (i = 0; sort_sequence->specs[i]; i++)
2391 sort_sequence->num_specs = i;
2393 resultSetRank (zh, sset, rset, rset_nmem);
2396 yaz_log(YLOG_DEBUG, "resultSetSortSingle in rpn_search");
2397 resultSetSortSingle (zh, nmem, sset, rset,
2398 sort_sequence, &sort_status);
2401 yaz_log(YLOG_DEBUG, "resultSetSortSingle status = %d", zh->errCode);
2407 struct scan_info_entry {
2413 struct scan_info_entry *list;
2419 static int scan_handle (char *name, const char *info, int pos, void *client)
2421 int len_prefix, idx;
2422 struct scan_info *scan_info = (struct scan_info *) client;
2424 len_prefix = strlen(scan_info->prefix);
2425 if (memcmp (name, scan_info->prefix, len_prefix))
2427 if (pos > 0) idx = scan_info->after - pos + scan_info->before;
2430 scan_info->list[idx].term = (char *)
2431 odr_malloc(scan_info->odr, strlen(name + len_prefix)+1);
2432 strcpy(scan_info->list[idx].term, name + len_prefix);
2433 assert (*info == sizeof(ISAMC_P));
2434 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAMC_P));
2438 static void scan_term_untrans (ZebraHandle zh, NMEM stream, int reg_type,
2439 char **dst, const char *src)
2441 char term_src[IT_MAX_WORD];
2442 char term_dst[IT_MAX_WORD];
2444 term_untrans (zh, reg_type, term_src, src);
2446 if (zh->iconv_from_utf8 != 0)
2449 char *inbuf = term_src;
2450 size_t inleft = strlen(term_src);
2451 char *outbuf = term_dst;
2452 size_t outleft = sizeof(term_dst)-1;
2455 ret = yaz_iconv (zh->iconv_from_utf8, &inbuf, &inleft,
2457 if (ret == (size_t)(-1))
2460 len = outbuf - term_dst;
2461 *dst = nmem_malloc(stream, len + 1);
2463 memcpy (*dst, term_dst, len);
2467 *dst = nmem_strdup(stream, term_src);
2470 static void count_set (RSET r, int *count)
2477 yaz_log(YLOG_DEBUG, "count_set");
2480 rfd = rset_open (r, RSETF_READ);
2481 while (rset_read (rfd, &key,0 /* never mind terms */))
2483 if (key.mem[0] != psysno)
2485 psysno = key.mem[0];
2491 yaz_log(YLOG_DEBUG, "%d keys, %d records", kno, *count);
2494 void rpn_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
2495 oid_value attributeset,
2496 int num_bases, char **basenames,
2497 int *position, int *num_entries, ZebraScanEntry **list,
2498 int *is_partial, RSET limit_set, int return_zero)
2501 int pos = *position;
2502 int num = *num_entries;
2506 char termz[IT_MAX_WORD+20];
2509 const char *use_string = 0;
2510 struct scan_info *scan_info_array;
2511 ZebraScanEntry *glist;
2512 int ords[32], ord_no = 0;
2515 int bases_ok = 0; /* no of databases with OK attribute */
2516 int errCode = 0; /* err code (if any is not OK) */
2517 char *errString = 0; /* addinfo */
2520 char *search_type = NULL;
2521 char rank_type[128];
2524 NMEM rset_nmem = NULL;
2528 if (attributeset == VAL_NONE)
2529 attributeset = VAL_BIB1;
2534 int termset_value_numeric;
2535 const char *termset_value_string;
2536 attr_init (&termset, zapt, 8);
2537 termset_value_numeric =
2538 attr_find_ex (&termset, NULL, &termset_value_string);
2539 if (termset_value_numeric != -1)
2542 const char *termset_name = 0;
2544 if (termset_value_numeric != -2)
2547 sprintf (resname, "%d", termset_value_numeric);
2548 termset_name = resname;
2551 termset_name = termset_value_string;
2553 limit_set = resultSetRef (zh, termset_name);
2557 yaz_log (YLOG_DEBUG, "position = %d, num = %d set=%d",
2558 pos, num, attributeset);
2560 attr_init (&use, zapt, 1);
2561 use_value = attr_find_ex (&use, &attributeset, &use_string);
2563 if (zebra_maps_attr (zh->reg->zebra_maps, zapt, ®_id, &search_type,
2564 rank_type, &complete_flag, &sort_flag))
2570 yaz_log (YLOG_DEBUG, "use_value = %d", use_value);
2572 if (use_value == -1)
2574 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
2578 data1_local_attribute *local_attr;
2580 if ((r = att_getentbyatt (zh, &attp, attributeset, use_value,
2583 yaz_log(YLOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
2584 attributeset, use_value);
2588 sprintf (val_str, "%d", use_value);
2590 errString = odr_strdup (stream, val_str);
2596 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
2598 zh->errString = basenames[base_no];
2599 zh->errCode = 109; /* Database unavailable */
2604 for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
2605 local_attr = local_attr->next)
2609 ord = zebraExplain_lookup_attr_su(zh->reg->zei,
2610 attp.attset_ordinal,
2613 ords[ord_no++] = ord;
2616 if (!bases_ok && errCode)
2618 zh->errCode = errCode;
2619 zh->errString = errString;
2627 /* prepare dictionary scanning */
2630 scan_info_array = (struct scan_info *)
2631 odr_malloc(stream, ord_no * sizeof(*scan_info_array));
2632 for (i = 0; i < ord_no; i++)
2634 int j, prefix_len = 0;
2635 int before_tmp = before, after_tmp = after;
2636 struct scan_info *scan_info = scan_info_array + i;
2637 struct rpn_char_map_info rcmi;
2639 rpn_char_map_prepare (zh->reg, reg_id, &rcmi);
2641 scan_info->before = before;
2642 scan_info->after = after;
2643 scan_info->odr = stream;
2645 scan_info->list = (struct scan_info_entry *)
2646 odr_malloc(stream, (before+after) * sizeof(*scan_info->list));
2647 for (j = 0; j<before+after; j++)
2648 scan_info->list[j].term = NULL;
2650 prefix_len += key_SU_encode (ords[i], termz + prefix_len);
2651 termz[prefix_len++] = reg_id;
2652 termz[prefix_len] = 0;
2653 strcpy(scan_info->prefix, termz);
2655 if (trans_scan_term(zh, zapt, termz+prefix_len, reg_id))
2658 dict_scan(zh->reg->dict, termz, &before_tmp, &after_tmp,
2659 scan_info, scan_handle);
2661 glist = (ZebraScanEntry *)
2662 odr_malloc(stream, (before+after)*sizeof(*glist));
2664 rset_nmem = nmem_create();
2666 /* consider terms after main term */
2667 for (i = 0; i < ord_no; i++)
2671 for (i = 0; i<after; i++)
2674 const char *mterm = NULL;
2678 for (j = 0; j < ord_no; j++)
2680 if (ptr[j] < before+after &&
2681 (tst = scan_info_array[j].list[ptr[j]].term) &&
2682 (!mterm || strcmp (tst, mterm) < 0))
2690 scan_term_untrans (zh, stream->mem, reg_id,
2691 &glist[i+before].term, mterm);
2692 rset = rset_trunc(zh, &scan_info_array[j0].list[ptr[j0]].isam_p, 1,
2693 glist[i+before].term, strlen(glist[i+before].term),
2694 NULL, 0, zapt->term->which, rset_nmem,
2695 key_it_ctrl,key_it_ctrl->scope);
2697 for (j = j0+1; j<ord_no; j++)
2699 if (ptr[j] < before+after &&
2700 (tst = scan_info_array[j].list[ptr[j]].term) &&
2701 !strcmp (tst, mterm))
2707 rset_trunc(zh, &scan_info_array[j].list[ptr[j]].isam_p, 1,
2708 glist[i+before].term,
2709 strlen(glist[i+before].term), NULL, 0,
2710 zapt->term->which,rset_nmem,
2711 key_it_ctrl, key_it_ctrl->scope);
2712 rset = rsmulti_or_create(rset_nmem, key_it_ctrl,
2713 2, key_it_ctrl->scope, rsets);
2721 rsets[1] = rset_dup(limit_set);
2723 rset = rsmulti_and_create(rset_nmem, key_it_ctrl,
2724 key_it_ctrl->scope, 2, rsets);
2726 count_set(rset, &glist[i+before].occurrences);
2731 *num_entries -= (after-i);
2735 /* consider terms before main term */
2736 for (i = 0; i<ord_no; i++)
2739 for (i = 0; i<before; i++)
2742 const char *mterm = NULL;
2746 for (j = 0; j <ord_no; j++)
2748 if (ptr[j] < before &&
2749 (tst = scan_info_array[j].list[before-1-ptr[j]].term) &&
2750 (!mterm || strcmp (tst, mterm) > 0))
2759 scan_term_untrans (zh, stream->mem, reg_id,
2760 &glist[before-1-i].term, mterm);
2763 (zh, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1,
2764 glist[before-1-i].term, strlen(glist[before-1-i].term),
2765 NULL, 0, zapt->term->which,rset_nmem,
2766 key_it_ctrl,key_it_ctrl->scope);
2770 for (j = j0+1; j<ord_no; j++)
2772 if (ptr[j] < before &&
2773 (tst = scan_info_array[j].list[before-1-ptr[j]].term) &&
2774 !strcmp (tst, mterm))
2779 rsets[1] = rset_trunc(
2781 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1,
2782 glist[before-1-i].term,
2783 strlen(glist[before-1-i].term), NULL, 0,
2784 zapt->term->which, rset_nmem,
2785 key_it_ctrl, key_it_ctrl->scope);
2786 rset = rsmulti_or_create(rset_nmem, key_it_ctrl,
2787 2, key_it_ctrl->scope, rsets);
2796 rsets[1] = rset_dup(limit_set);
2798 rset = rsmulti_and_create(rset_nmem, key_it_ctrl,
2799 key_it_ctrl->scope, 2, rsets);
2801 count_set (rset, &glist[before-1-i].occurrences);
2812 nmem_destroy(rset_nmem);
2813 *list = glist + i; /* list is set to first 'real' entry */
2815 yaz_log(YLOG_DEBUG, "position = %d, num_entries = %d",
2816 *position, *num_entries);
2818 yaz_log(YLOG_DEBUG, "scan error: %d", zh->errCode);