1 /* $Id: zrpn.c,v 1.153 2004-09-15 08:13:51 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
34 #include <zebra_xpath.h>
40 static const struct key_control it_ctrl = {
41 sizeof(struct it_key),
42 2, /* we have sysnos and seqnos in this key, nothing more */
44 key_logdump_txt, /* FIXME - clean up these functions */
49 const struct key_control *key_it_ctrl = &it_ctrl;
51 struct rpn_char_map_info {
60 Z_AttributesPlusTerm *zapt;
64 static const char **rpn_char_map_handler (void *vp, const char **from, int len)
66 struct rpn_char_map_info *p = (struct rpn_char_map_info *) vp;
67 const char **out = zebra_maps_input (p->zm, p->reg_type, from, len, 0);
71 const char *outp = *out;
72 yaz_log (LOG_LOG, "---");
75 yaz_log (LOG_LOG, "%02X", *outp);
83 static void rpn_char_map_prepare (struct zebra_register *reg, int reg_type,
84 struct rpn_char_map_info *map_info)
86 map_info->zm = reg->zebra_maps;
87 map_info->reg_type = reg_type;
88 dict_grep_cmap (reg->dict, map_info, rpn_char_map_handler);
91 static int attr_find_ex (AttrType *src, oid_value *attributeSetP,
92 const char **string_value)
96 num_attributes = src->zapt->attributes->num_attributes;
97 while (src->major < num_attributes)
99 Z_AttributeElement *element;
101 element = src->zapt->attributes->attributes[src->major];
102 if (src->type == *element->attributeType)
104 switch (element->which)
106 case Z_AttributeValue_numeric:
108 if (element->attributeSet && attributeSetP)
112 attrset = oid_getentbyoid (element->attributeSet);
113 *attributeSetP = attrset->value;
115 return *element->value.numeric;
117 case Z_AttributeValue_complex:
118 if (src->minor >= element->value.complex->num_list)
120 if (element->attributeSet && attributeSetP)
124 attrset = oid_getentbyoid (element->attributeSet);
125 *attributeSetP = attrset->value;
127 if (element->value.complex->list[src->minor]->which ==
128 Z_StringOrNumeric_numeric)
132 *element->value.complex->list[src->minor-1]->u.numeric;
134 else if (element->value.complex->list[src->minor]->which ==
135 Z_StringOrNumeric_string)
141 element->value.complex->list[src->minor-1]->u.string;
155 static int attr_find (AttrType *src, oid_value *attributeSetP)
157 return attr_find_ex (src, attributeSetP, 0);
160 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
183 static void term_untrans (ZebraHandle zh, int reg_type,
184 char *dst, const char *src)
189 const char *cp = zebra_maps_output (zh->reg->zebra_maps,
191 if (!cp && len < IT_MAX_WORD-1)
194 while (*cp && len < IT_MAX_WORD-1)
200 static void add_isam_p (const char *name, const char *info,
203 if (p->isam_p_indx == p->isam_p_size)
205 ISAMC_P *new_isam_p_buf;
209 p->isam_p_size = 2*p->isam_p_size + 100;
210 new_isam_p_buf = (ISAMC_P *) xmalloc (sizeof(*new_isam_p_buf) *
214 memcpy (new_isam_p_buf, p->isam_p_buf,
215 p->isam_p_indx * sizeof(*p->isam_p_buf));
216 xfree (p->isam_p_buf);
218 p->isam_p_buf = new_isam_p_buf;
221 new_term_no = (int *) xmalloc (sizeof(*new_term_no) *
225 memcpy (new_term_no, p->isam_p_buf,
226 p->isam_p_indx * sizeof(*p->term_no));
229 p->term_no = new_term_no;
232 assert (*info == sizeof(*p->isam_p_buf));
233 memcpy (p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
240 char term_tmp[IT_MAX_WORD];
242 int len = key_SU_decode (&su_code, name);
244 term_untrans (p->zh, p->reg_type, term_tmp, name+len+1);
245 logf (LOG_LOG, "grep: %d %c %s", su_code, name[len], term_tmp);
246 zebraExplain_lookup_ord (p->zh->reg->zei,
247 su_code, &db, &set, &use);
248 logf (LOG_LOG, "grep: set=%d use=%d db=%s", set, use, db);
250 resultSetAddTerm (p->zh, p->termset, name[len], db,
257 static int grep_handle (char *name, const char *info, void *p)
259 add_isam_p (name, info, (struct grep_info *) p);
263 static int term_pre (ZebraMaps zebra_maps, int reg_type, const char **src,
264 const char *ct1, const char *ct2, int first)
266 const char *s1, *s0 = *src;
269 /* skip white space */
272 if (ct1 && strchr (ct1, *s0))
274 if (ct2 && strchr (ct2, *s0))
277 map = zebra_maps_input (zebra_maps, reg_type, &s1, strlen(s1), first);
278 if (**map != *CHR_SPACE)
286 #define REGEX_CHARS " []()|.*+?!"
288 /* term_100: handle term, where trunc=none (no operators at all) */
289 static int term_100 (ZebraMaps zebra_maps, int reg_type,
290 const char **src, char *dst, int space_split,
298 const char *space_start = 0;
299 const char *space_end = 0;
301 if (!term_pre (zebra_maps, reg_type, src, NULL, NULL, !space_split))
307 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0), 0);
310 if (**map == *CHR_SPACE)
313 else /* complete subfield only. */
315 if (**map == *CHR_SPACE)
316 { /* save space mapping for later .. */
321 else if (space_start)
322 { /* reload last space */
323 while (space_start < space_end)
325 if (strchr (REGEX_CHARS, *space_start))
327 dst_term[j++] = *space_start;
328 dst[i++] = *space_start++;
331 space_start = space_end = 0;
334 /* add non-space char */
337 if (strchr(REGEX_CHARS, *s1))
349 /* term_101: handle term, where trunc=Process # */
350 static int term_101 (ZebraMaps zebra_maps, int reg_type,
351 const char **src, char *dst, int space_split,
359 if (!term_pre (zebra_maps, reg_type, src, "#", "#", !space_split))
368 dst_term[j++] = *s0++;
373 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0), 0);
374 if (space_split && **map == *CHR_SPACE)
378 if (strchr(REGEX_CHARS, *s1))
386 dst_term[j++] = '\0';
391 /* term_103: handle term, where trunc=re-2 (regular expressions) */
392 static int term_103 (ZebraMaps zebra_maps, int reg_type, const char **src,
393 char *dst, int *errors, int space_split,
401 if (!term_pre (zebra_maps, reg_type, src, "^\\()[].*+?|", "(", !space_split))
404 if (errors && *s0 == '+' && s0[1] && s0[2] == '+' && s0[3] &&
407 *errors = s0[1] - '0';
414 if (strchr ("^\\()[].*+?|-", *s0))
422 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0), 0);
423 if (**map == *CHR_SPACE)
427 if (strchr(REGEX_CHARS, *s1))
440 /* term_103: handle term, where trunc=re-1 (regular expressions) */
441 static int term_102 (ZebraMaps zebra_maps, int reg_type, const char **src,
442 char *dst, int space_split, char *dst_term)
444 return term_103 (zebra_maps, reg_type, src, dst, NULL, space_split,
449 /* term_104: handle term, where trunc=Process # and ! */
450 static int term_104 (ZebraMaps zebra_maps, int reg_type,
451 const char **src, char *dst, int space_split,
459 if (!term_pre (zebra_maps, reg_type, src, "?*#", "?*#", !space_split))
466 dst_term[j++] = *s0++;
467 if (*s0 >= '0' && *s0 <= '9')
470 while (*s0 >= '0' && *s0 <= '9')
472 limit = limit * 10 + (*s0 - '0');
473 dst_term[j++] = *s0++;
493 dst_term[j++] = *s0++;
498 dst_term[j++] = *s0++;
502 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0), 0);
503 if (space_split && **map == *CHR_SPACE)
507 if (strchr(REGEX_CHARS, *s1))
515 dst_term[j++] = '\0';
520 /* term_105/106: handle term, where trunc=Process * and ! and right trunc */
521 static int term_105 (ZebraMaps zebra_maps, int reg_type,
522 const char **src, char *dst, int space_split,
523 char *dst_term, int right_truncate)
530 if (!term_pre (zebra_maps, reg_type, src, "*!", "*!", !space_split))
539 dst_term[j++] = *s0++;
544 dst_term[j++] = *s0++;
548 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0), 0);
549 if (space_split && **map == *CHR_SPACE)
553 if (strchr(REGEX_CHARS, *s1))
567 dst_term[j++] = '\0';
573 /* gen_regular_rel - generate regular expression from relation
574 * val: border value (inclusive)
575 * islt: 1 if <=; 0 if >=.
577 static void gen_regular_rel (char *dst, int val, int islt)
584 logf (LOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
588 strcpy (dst, "(-[0-9]+|(");
596 strcpy (dst, "([0-9]+|-(");
597 dst_p = strlen (dst);
607 dst_p = strlen (dst);
608 sprintf (numstr, "%d", val);
609 for (w = strlen(numstr); --w >= 0; pos++)
628 strcpy (dst + dst_p, numstr);
629 dst_p = strlen(dst) - pos - 1;
657 for (i = 0; i<pos; i++)
670 /* match everything less than 10^(pos-1) */
672 for (i=1; i<pos; i++)
673 strcat (dst, "[0-9]?");
677 /* match everything greater than 10^pos */
678 for (i = 0; i <= pos; i++)
679 strcat (dst, "[0-9]");
680 strcat (dst, "[0-9]*");
685 void string_rel_add_char (char **term_p, const char *src, int *indx)
687 if (src[*indx] == '\\')
688 *(*term_p)++ = src[(*indx)++];
689 *(*term_p)++ = src[(*indx)++];
693 * > abc ([b-].*|a[c-].*|ab[d-].*|abc.+)
694 * ([^-a].*|a[^-b].*ab[^-c].*|abc.+)
695 * >= abc ([b-].*|a[c-].*|ab[c-].*)
696 * ([^-a].*|a[^-b].*|ab[c-].*)
697 * < abc ([-0].*|a[-a].*|ab[-b].*)
698 * ([^a-].*|a[^b-].*|ab[^c-].*)
699 * <= abc ([-0].*|a[-a].*|ab[-b].*|abc)
700 * ([^a-].*|a[^b-].*|ab[^c-].*|abc)
702 static int string_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
703 const char **term_sub, char *term_dict,
704 oid_value attributeSet,
705 int reg_type, int space_split, char *term_dst)
710 char *term_tmp = term_dict + strlen(term_dict);
711 char term_component[2*IT_MAX_WORD+20];
713 attr_init (&relation, zapt, 2);
714 relation_value = attr_find (&relation, NULL);
716 logf (LOG_DEBUG, "string relation value=%d", relation_value);
717 switch (relation_value)
720 if (!term_100 (zh->reg->zebra_maps, reg_type,
721 term_sub, term_component,
722 space_split, term_dst))
724 logf (LOG_DEBUG, "Relation <");
727 for (i = 0; term_component[i]; )
734 string_rel_add_char (&term_tmp, term_component, &j);
739 string_rel_add_char (&term_tmp, term_component, &i);
746 if ((term_tmp - term_dict) > IT_MAX_WORD)
753 if (!term_100 (zh->reg->zebra_maps, reg_type,
754 term_sub, term_component,
755 space_split, term_dst))
757 logf (LOG_DEBUG, "Relation <=");
760 for (i = 0; term_component[i]; )
765 string_rel_add_char (&term_tmp, term_component, &j);
769 string_rel_add_char (&term_tmp, term_component, &i);
778 if ((term_tmp - term_dict) > IT_MAX_WORD)
781 for (i = 0; term_component[i]; )
782 string_rel_add_char (&term_tmp, term_component, &i);
787 if (!term_100 (zh->reg->zebra_maps, reg_type,
788 term_sub, term_component, space_split, term_dst))
790 logf (LOG_DEBUG, "Relation >");
793 for (i = 0; term_component[i];)
798 string_rel_add_char (&term_tmp, term_component, &j);
803 string_rel_add_char (&term_tmp, term_component, &i);
811 if ((term_tmp - term_dict) > IT_MAX_WORD)
814 for (i = 0; term_component[i];)
815 string_rel_add_char (&term_tmp, term_component, &i);
822 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub,
823 term_component, space_split, term_dst))
825 logf (LOG_DEBUG, "Relation >=");
828 for (i = 0; term_component[i];)
835 string_rel_add_char (&term_tmp, term_component, &j);
838 if (term_component[i+1])
842 string_rel_add_char (&term_tmp, term_component, &i);
846 string_rel_add_char (&term_tmp, term_component, &i);
853 if ((term_tmp - term_dict) > IT_MAX_WORD)
861 logf (LOG_DEBUG, "Relation =");
862 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub,
863 term_component, space_split, term_dst))
865 strcat (term_tmp, "(");
866 strcat (term_tmp, term_component);
867 strcat (term_tmp, ")");
872 static int string_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
873 const char **term_sub,
874 oid_value attributeSet, NMEM stream,
875 struct grep_info *grep_info,
876 int reg_type, int complete_flag,
877 int num_bases, char **basenames,
878 char *term_dst, int xpath_use);
880 static RSET term_trunc (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
881 const char **term_sub,
882 oid_value attributeSet, NMEM stream,
883 struct grep_info *grep_info,
884 int reg_type, int complete_flag,
885 int num_bases, char **basenames,
887 const char *rank_type, int xpath_use,
891 grep_info->isam_p_indx = 0;
892 r = string_term (zh, zapt, term_sub, attributeSet, stream, grep_info,
893 reg_type, complete_flag, num_bases, basenames,
894 term_dst, xpath_use);
897 logf (LOG_DEBUG, "term: %s", term_dst);
898 return rset_trunc (zh, grep_info->isam_p_buf,
899 grep_info->isam_p_indx, term_dst,
900 strlen(term_dst), rank_type, 1 /* preserve pos */,
901 zapt->term->which, rset_nmem,
902 key_it_ctrl,key_it_ctrl->scope);
906 static int string_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
907 const char **term_sub,
908 oid_value attributeSet, NMEM stream,
909 struct grep_info *grep_info,
910 int reg_type, int complete_flag,
911 int num_bases, char **basenames,
912 char *term_dst, int xpath_use)
914 char term_dict[2*IT_MAX_WORD+4000];
917 int truncation_value;
920 const char *use_string = 0;
921 oid_value curAttributeSet = attributeSet;
923 struct rpn_char_map_info rcmi;
924 int space_split = complete_flag ? 0 : 1;
926 int bases_ok = 0; /* no of databases with OK attribute */
927 int errCode = 0; /* err code (if any is not OK) */
928 char *errString = 0; /* addinfo */
930 rpn_char_map_prepare (zh->reg, reg_type, &rcmi);
931 attr_init (&use, zapt, 1);
932 use_value = attr_find_ex (&use, &curAttributeSet, &use_string);
933 logf (LOG_DEBUG, "string_term, use value %d", use_value);
934 attr_init (&truncation, zapt, 5);
935 truncation_value = attr_find (&truncation, NULL);
936 logf (LOG_DEBUG, "truncation value %d", truncation_value);
938 if (use_value == -1) /* no attribute - assumy "any" */
940 for (base_no = 0; base_no < num_bases; base_no++)
943 data1_local_attribute id_xpath_attr;
944 data1_local_attribute *local_attr;
945 int max_pos, prefix_len = 0;
949 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
951 zh->errCode = 109; /* Database unavailable */
952 zh->errString = basenames[base_no];
955 if (xpath_use > 0 && use_value == -2)
957 use_value = xpath_use;
958 attp.local_attributes = &id_xpath_attr;
959 attp.attset_ordinal = VAL_IDXPATH;
960 id_xpath_attr.next = 0;
961 id_xpath_attr.local = use_value;
963 else if (curAttributeSet == VAL_IDXPATH)
965 attp.local_attributes = &id_xpath_attr;
966 attp.attset_ordinal = VAL_IDXPATH;
967 id_xpath_attr.next = 0;
968 id_xpath_attr.local = use_value;
972 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value,
975 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
976 curAttributeSet, use_value, r);
979 /* set was found, but value wasn't defined */
982 errString = nmem_strdup(stream, use_string);
986 sprintf (val_str, "%d", use_value);
987 errString = nmem_strdup (stream, val_str);
993 struct oident oident;
995 oident.proto = PROTO_Z3950;
996 oident.oclass = CLASS_ATTSET;
997 oident.value = curAttributeSet;
998 oid_ent_to_oid (&oident, oid);
1001 errString = nmem_strdup (stream, oident.desc);
1006 for (local_attr = attp.local_attributes; local_attr;
1007 local_attr = local_attr->next)
1013 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
1018 term_dict[prefix_len++] = '|';
1020 term_dict[prefix_len++] = '(';
1022 ord_len = key_SU_encode (ord, ord_buf);
1023 for (i = 0; i<ord_len; i++)
1025 term_dict[prefix_len++] = 1;
1026 term_dict[prefix_len++] = ord_buf[i];
1035 sprintf (val_str, "%d", use_value);
1037 errString = nmem_strdup (stream, val_str);
1041 bases_ok++; /* this has OK attributes */
1043 term_dict[prefix_len++] = ')';
1044 term_dict[prefix_len++] = 1;
1045 term_dict[prefix_len++] = reg_type;
1046 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1047 term_dict[prefix_len] = '\0';
1049 switch (truncation_value)
1051 case -1: /* not specified */
1052 case 100: /* do not truncate */
1053 if (!string_relation (zh, zapt, &termp, term_dict,
1055 reg_type, space_split, term_dst))
1057 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_dict+prefix_len);
1058 r = dict_lookup_grep (zh->reg->dict, term_dict, 0,
1059 grep_info, &max_pos, 0, grep_handle);
1061 logf (LOG_WARN, "dict_lookup_grep fail %d", r);
1063 case 1: /* right truncation */
1064 term_dict[j++] = '(';
1065 if (!term_100 (zh->reg->zebra_maps, reg_type,
1066 &termp, term_dict + j, space_split, term_dst))
1068 strcat (term_dict, ".*)");
1069 dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1070 &max_pos, 0, grep_handle);
1072 case 2: /* keft truncation */
1073 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1074 if (!term_100 (zh->reg->zebra_maps, reg_type,
1075 &termp, term_dict + j, space_split, term_dst))
1077 strcat (term_dict, ")");
1078 dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1079 &max_pos, 0, grep_handle);
1081 case 3: /* left&right truncation */
1082 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1083 if (!term_100 (zh->reg->zebra_maps, reg_type,
1084 &termp, term_dict + j, space_split, term_dst))
1086 strcat (term_dict, ".*)");
1087 dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1088 &max_pos, 0, grep_handle);
1092 case 101: /* process # in term */
1093 term_dict[j++] = '(';
1094 if (!term_101 (zh->reg->zebra_maps, reg_type,
1095 &termp, term_dict + j, space_split, term_dst))
1097 strcat (term_dict, ")");
1098 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1099 &max_pos, 0, grep_handle);
1101 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d", r);
1103 case 102: /* Regexp-1 */
1104 term_dict[j++] = '(';
1105 if (!term_102 (zh->reg->zebra_maps, reg_type,
1106 &termp, term_dict + j, space_split, term_dst))
1108 strcat (term_dict, ")");
1109 logf (LOG_DEBUG, "Regexp-1 tolerance=%d", r);
1110 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1111 &max_pos, 0, grep_handle);
1113 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
1116 case 103: /* Regexp-2 */
1118 term_dict[j++] = '(';
1119 if (!term_103 (zh->reg->zebra_maps, reg_type,
1120 &termp, term_dict + j, &r, space_split, term_dst))
1122 strcat (term_dict, ")");
1123 logf (LOG_DEBUG, "Regexp-2 tolerance=%d", r);
1124 r = dict_lookup_grep (zh->reg->dict, term_dict, r, grep_info,
1125 &max_pos, 2, grep_handle);
1127 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
1130 case 104: /* process # and ! in term */
1131 term_dict[j++] = '(';
1132 if (!term_104 (zh->reg->zebra_maps, reg_type,
1133 &termp, term_dict + j, space_split, term_dst))
1135 strcat (term_dict, ")");
1136 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1137 &max_pos, 0, grep_handle);
1139 logf (LOG_WARN, "dict_lookup_grep err, trunc=#/!: %d", r);
1141 case 105: /* process * and ! in term */
1142 term_dict[j++] = '(';
1143 if (!term_105 (zh->reg->zebra_maps, reg_type,
1144 &termp, term_dict + j, space_split, term_dst, 1))
1146 strcat (term_dict, ")");
1147 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1148 &max_pos, 0, grep_handle);
1150 logf (LOG_WARN, "dict_lookup_grep err, trunc=*/!: %d", r);
1152 case 106: /* process * and ! in term */
1153 term_dict[j++] = '(';
1154 if (!term_105 (zh->reg->zebra_maps, reg_type,
1155 &termp, term_dict + j, space_split, term_dst, 0))
1157 strcat (term_dict, ")");
1158 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1159 &max_pos, 0, grep_handle);
1161 logf (LOG_WARN, "dict_lookup_grep err, trunc=*/!: %d", r);
1167 zh->errCode = errCode;
1168 zh->errString = errString;
1172 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1177 /* convert APT search term to UTF8 */
1178 static int zapt_term_to_utf8 (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1182 Z_Term *term = zapt->term;
1184 switch (term->which)
1186 case Z_Term_general:
1187 if (zh->iconv_to_utf8 != 0)
1189 char *inbuf = term->u.general->buf;
1190 size_t inleft = term->u.general->len;
1191 char *outbuf = termz;
1192 size_t outleft = IT_MAX_WORD-1;
1195 ret = yaz_iconv(zh->iconv_to_utf8, &inbuf, &inleft,
1197 if (ret == (size_t)(-1))
1199 ret = yaz_iconv(zh->iconv_to_utf8, 0, 0, 0, 0);
1207 sizez = term->u.general->len;
1208 if (sizez > IT_MAX_WORD-1)
1209 sizez = IT_MAX_WORD-1;
1210 memcpy (termz, term->u.general->buf, sizez);
1211 termz[sizez] = '\0';
1214 case Z_Term_characterString:
1215 sizez = strlen(term->u.characterString);
1216 if (sizez > IT_MAX_WORD-1)
1217 sizez = IT_MAX_WORD-1;
1218 memcpy (termz, term->u.characterString, sizez);
1219 termz[sizez] = '\0';
1228 /* convert APT SCAN term to internal cmap */
1229 static int trans_scan_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1230 char *termz, int reg_type)
1232 char termz0[IT_MAX_WORD];
1234 if (zapt_term_to_utf8(zh, zapt, termz0))
1235 return -1; /* error */
1239 const char *cp = (const char *) termz0;
1240 const char *cp_end = cp + strlen(cp);
1243 const char *space_map = NULL;
1246 while ((len = (cp_end - cp)) > 0)
1248 map = zebra_maps_input (zh->reg->zebra_maps, reg_type, &cp, len, 0);
1249 if (**map == *CHR_SPACE)
1254 for (src = space_map; *src; src++)
1257 for (src = *map; *src; src++)
1266 char *normalize_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1267 const char *termz, NMEM stream, unsigned reg_id)
1270 AttrType truncation;
1271 int truncation_value;
1274 attr_init (&truncation, zapt, 5);
1275 truncation_value = attr_find (&truncation, NULL);
1277 switch (truncation_value)
1297 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, ex_list,
1298 termz, strlen(termz));
1300 return nmem_strdup(stream, termz);
1303 char *buf = (char*) nmem_malloc (stream, wrbuf_len(wrbuf)+1);
1304 memcpy (buf, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1305 buf[wrbuf_len(wrbuf)] = '\0';
1310 static void grep_info_delete (struct grep_info *grep_info)
1313 xfree(grep_info->term_no);
1315 xfree (grep_info->isam_p_buf);
1318 static int grep_info_prepare (ZebraHandle zh,
1319 Z_AttributesPlusTerm *zapt,
1320 struct grep_info *grep_info,
1325 int termset_value_numeric;
1326 const char *termset_value_string;
1329 grep_info->term_no = 0;
1331 grep_info->isam_p_size = 0;
1332 grep_info->isam_p_buf = NULL;
1334 grep_info->reg_type = reg_type;
1335 grep_info->termset = 0;
1339 attr_init (&termset, zapt, 8);
1340 termset_value_numeric =
1341 attr_find_ex (&termset, NULL, &termset_value_string);
1342 if (termset_value_numeric != -1)
1345 const char *termset_name = 0;
1346 if (termset_value_numeric != -2)
1349 sprintf (resname, "%d", termset_value_numeric);
1350 termset_name = resname;
1353 termset_name = termset_value_string;
1354 logf (LOG_LOG, "creating termset set %s", termset_name);
1355 grep_info->termset = resultSetAdd (zh, termset_name, 1);
1356 if (!grep_info->termset)
1359 zh->errString = nmem_strdup (stream, termset_name);
1367 static RSET rpn_search_APT_phrase (ZebraHandle zh,
1368 Z_AttributesPlusTerm *zapt,
1369 const char *termz_org,
1370 oid_value attributeSet,
1372 int reg_type, int complete_flag,
1373 const char *rank_type, int xpath_use,
1374 int num_bases, char **basenames,
1377 char term_dst[IT_MAX_WORD+1];
1378 RSET rset[60], result;
1380 struct grep_info grep_info;
1381 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1382 const char *termp = termz;
1385 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1389 logf (LOG_DEBUG, "APT_phrase termp=%s", termp);
1390 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1392 reg_type, complete_flag,
1393 num_bases, basenames,
1394 term_dst, rank_type,
1395 xpath_use,rset_nmem);
1398 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1401 grep_info_delete (&grep_info);
1403 return rsnull_create (rset_nmem,key_it_ctrl);
1404 else if (rset_no == 1)
1407 result = rsprox_create( rset_nmem, key_it_ctrl, key_it_ctrl->scope,
1409 1 /* ordered */, 0 /* exclusion */,
1410 3 /* relation */, 1 /* distance */);
1414 static RSET rpn_search_APT_or_list (ZebraHandle zh,
1415 Z_AttributesPlusTerm *zapt,
1416 const char *termz_org,
1417 oid_value attributeSet,
1419 int reg_type, int complete_flag,
1420 const char *rank_type,
1422 int num_bases, char **basenames,
1425 char term_dst[IT_MAX_WORD+1];
1428 struct grep_info grep_info;
1429 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1430 const char *termp = termz;
1432 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1436 logf (LOG_DEBUG, "APT_or_list termp=%s", termp);
1437 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1439 reg_type, complete_flag,
1440 num_bases, basenames,
1441 term_dst, rank_type,
1442 xpath_use,rset_nmem);
1445 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1448 grep_info_delete (&grep_info);
1450 return rsnull_create (rset_nmem,key_it_ctrl);
1451 return rsmultior_create(rset_nmem, key_it_ctrl,key_it_ctrl->scope,
1455 static RSET rpn_search_APT_and_list (ZebraHandle zh,
1456 Z_AttributesPlusTerm *zapt,
1457 const char *termz_org,
1458 oid_value attributeSet,
1460 int reg_type, int complete_flag,
1461 const char *rank_type,
1463 int num_bases, char **basenames,
1466 char term_dst[IT_MAX_WORD+1];
1467 RSET rset[60], result;
1469 struct grep_info grep_info;
1470 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1471 const char *termp = termz;
1473 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1477 logf (LOG_DEBUG, "APT_and_list termp=%s", termp);
1478 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1480 reg_type, complete_flag,
1481 num_bases, basenames,
1482 term_dst, rank_type,
1483 xpath_use, rset_nmem);
1486 assert (rset[rset_no]);
1487 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1490 grep_info_delete (&grep_info);
1492 return rsnull_create (rset_nmem,key_it_ctrl);
1494 /* FIXME - Use a proper rsmultiand */
1495 for (i = 1; i<rset_no; i++)
1496 result= rsbool_create_and(rset_nmem,key_it_ctrl,key_it_ctrl->scope,
1501 static int numeric_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1502 const char **term_sub,
1504 oid_value attributeSet,
1505 struct grep_info *grep_info,
1514 char *term_tmp = term_dict + strlen(term_dict);
1516 attr_init (&relation, zapt, 2);
1517 relation_value = attr_find (&relation, NULL);
1519 logf (LOG_DEBUG, "numeric relation value=%d", relation_value);
1521 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub, term_tmp, 1,
1524 term_value = atoi (term_tmp);
1525 switch (relation_value)
1528 logf (LOG_DEBUG, "Relation <");
1529 gen_regular_rel (term_tmp, term_value-1, 1);
1532 logf (LOG_DEBUG, "Relation <=");
1533 gen_regular_rel (term_tmp, term_value, 1);
1536 logf (LOG_DEBUG, "Relation >=");
1537 gen_regular_rel (term_tmp, term_value, 0);
1540 logf (LOG_DEBUG, "Relation >");
1541 gen_regular_rel (term_tmp, term_value+1, 0);
1545 logf (LOG_DEBUG, "Relation =");
1546 sprintf (term_tmp, "(0*%d)", term_value);
1548 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_tmp);
1549 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info, max_pos,
1552 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1553 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1557 static int numeric_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1558 const char **term_sub,
1559 oid_value attributeSet, struct grep_info *grep_info,
1560 int reg_type, int complete_flag,
1561 int num_bases, char **basenames,
1562 char *term_dst, int xpath_use, NMEM stream)
1564 char term_dict[2*IT_MAX_WORD+2];
1568 const char *use_string = 0;
1569 oid_value curAttributeSet = attributeSet;
1571 struct rpn_char_map_info rcmi;
1573 int bases_ok = 0; /* no of databases with OK attribute */
1574 int errCode = 0; /* err code (if any is not OK) */
1575 char *errString = 0; /* addinfo */
1577 rpn_char_map_prepare (zh->reg, reg_type, &rcmi);
1578 attr_init (&use, zapt, 1);
1579 use_value = attr_find_ex (&use, &curAttributeSet, &use_string);
1581 if (use_value == -1)
1584 for (base_no = 0; base_no < num_bases; base_no++)
1587 data1_local_attribute id_xpath_attr;
1588 data1_local_attribute *local_attr;
1589 int max_pos, prefix_len = 0;
1592 if (use_value == -2) /* string attribute (assume IDXPATH/any) */
1594 use_value = xpath_use;
1595 attp.local_attributes = &id_xpath_attr;
1596 attp.attset_ordinal = VAL_IDXPATH;
1597 id_xpath_attr.next = 0;
1598 id_xpath_attr.local = use_value;
1600 else if (curAttributeSet == VAL_IDXPATH)
1602 attp.local_attributes = &id_xpath_attr;
1603 attp.attset_ordinal = VAL_IDXPATH;
1604 id_xpath_attr.next = 0;
1605 id_xpath_attr.local = use_value;
1609 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value,
1612 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1613 curAttributeSet, use_value, r);
1617 sprintf (val_str, "%d", use_value);
1618 errString = nmem_strdup (stream, val_str);
1626 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
1628 zh->errCode = 109; /* Database unavailable */
1629 zh->errString = basenames[base_no];
1632 for (local_attr = attp.local_attributes; local_attr;
1633 local_attr = local_attr->next)
1639 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
1644 term_dict[prefix_len++] = '|';
1646 term_dict[prefix_len++] = '(';
1648 ord_len = key_SU_encode (ord, ord_buf);
1649 for (i = 0; i<ord_len; i++)
1651 term_dict[prefix_len++] = 1;
1652 term_dict[prefix_len++] = ord_buf[i];
1658 sprintf (val_str, "%d", use_value);
1660 errString = nmem_strdup (stream, val_str);
1664 term_dict[prefix_len++] = ')';
1665 term_dict[prefix_len++] = 1;
1666 term_dict[prefix_len++] = reg_type;
1667 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1668 term_dict[prefix_len] = '\0';
1669 if (!numeric_relation (zh, zapt, &termp, term_dict,
1670 attributeSet, grep_info, &max_pos, reg_type,
1676 zh->errCode = errCode;
1677 zh->errString = errString;
1681 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1685 static RSET rpn_search_APT_numeric (ZebraHandle zh,
1686 Z_AttributesPlusTerm *zapt,
1688 oid_value attributeSet,
1690 int reg_type, int complete_flag,
1691 const char *rank_type, int xpath_use,
1692 int num_bases, char **basenames,
1695 char term_dst[IT_MAX_WORD+1];
1696 const char *termp = termz;
1697 RSET rset[60], result;
1698 int i, r, rset_no = 0;
1699 struct grep_info grep_info;
1701 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1705 logf (LOG_DEBUG, "APT_numeric termp=%s", termp);
1706 grep_info.isam_p_indx = 0;
1707 r = numeric_term (zh, zapt, &termp, attributeSet, &grep_info,
1708 reg_type, complete_flag, num_bases, basenames,
1709 term_dst, xpath_use,
1713 logf (LOG_DEBUG, "term: %s", term_dst);
1714 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1715 grep_info.isam_p_indx, term_dst,
1716 strlen(term_dst), rank_type,
1717 0 /* preserve position */,
1718 zapt->term->which, rset_nmem,
1719 key_it_ctrl,key_it_ctrl->scope);
1720 assert (rset[rset_no]);
1721 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1724 grep_info_delete (&grep_info);
1726 return rsnull_create (rset_nmem,key_it_ctrl);
1728 for (i = 1; i<rset_no; i++)
1730 /* FIXME - Use a proper multi-and */
1731 result= rsbool_create_and(rset_nmem,key_it_ctrl,key_it_ctrl->scope,
1737 static RSET rpn_search_APT_local (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1739 oid_value attributeSet,
1741 const char *rank_type, NMEM rset_nmem)
1748 rset_temp_parms parms;
1750 parms.cmp = key_compare_it;
1751 parms.key_size = sizeof (struct it_key);
1752 parms.temp_path = res_get (zh->res, "setTmpDir");
1753 result = rset_create (rset_kind_temp, &parms);
1755 result = rstemp_create( rset_nmem,key_it_ctrl,key_it_ctrl->scope,
1756 res_get (zh->res, "setTmpDir") );
1757 rsfd = rset_open (result, RSETF_WRITE);
1765 rset_write (rsfd, &key);
1770 static RSET rpn_sort_spec (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1771 oid_value attributeSet, NMEM stream,
1772 Z_SortKeySpecList *sort_sequence,
1773 const char *rank_type)
1776 int sort_relation_value;
1777 AttrType sort_relation_type;
1782 Z_AttributeElement *ae;
1787 attr_init (&sort_relation_type, zapt, 7);
1788 sort_relation_value = attr_find (&sort_relation_type, &attributeSet);
1790 attr_init (&use_type, zapt, 1);
1791 use_value = attr_find (&use_type, &attributeSet);
1793 if (!sort_sequence->specs)
1795 sort_sequence->num_specs = 10;
1796 sort_sequence->specs = (Z_SortKeySpec **)
1797 nmem_malloc (stream, sort_sequence->num_specs *
1798 sizeof(*sort_sequence->specs));
1799 for (i = 0; i<sort_sequence->num_specs; i++)
1800 sort_sequence->specs[i] = 0;
1802 if (zapt->term->which != Z_Term_general)
1805 i = atoi_n ((char *) zapt->term->u.general->buf,
1806 zapt->term->u.general->len);
1807 if (i >= sort_sequence->num_specs)
1809 sprintf (termz, "%d", i);
1811 oe.proto = PROTO_Z3950;
1812 oe.oclass = CLASS_ATTSET;
1813 oe.value = attributeSet;
1814 if (!oid_ent_to_oid (&oe, oid))
1817 sks = (Z_SortKeySpec *) nmem_malloc (stream, sizeof(*sks));
1818 sks->sortElement = (Z_SortElement *)
1819 nmem_malloc (stream, sizeof(*sks->sortElement));
1820 sks->sortElement->which = Z_SortElement_generic;
1821 sk = sks->sortElement->u.generic = (Z_SortKey *)
1822 nmem_malloc (stream, sizeof(*sk));
1823 sk->which = Z_SortKey_sortAttributes;
1824 sk->u.sortAttributes = (Z_SortAttributes *)
1825 nmem_malloc (stream, sizeof(*sk->u.sortAttributes));
1827 sk->u.sortAttributes->id = oid;
1828 sk->u.sortAttributes->list = (Z_AttributeList *)
1829 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list));
1830 sk->u.sortAttributes->list->num_attributes = 1;
1831 sk->u.sortAttributes->list->attributes = (Z_AttributeElement **)
1832 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list->attributes));
1833 ae = *sk->u.sortAttributes->list->attributes = (Z_AttributeElement *)
1834 nmem_malloc (stream, sizeof(**sk->u.sortAttributes->list->attributes));
1835 ae->attributeSet = 0;
1836 ae->attributeType = (int *)
1837 nmem_malloc (stream, sizeof(*ae->attributeType));
1838 *ae->attributeType = 1;
1839 ae->which = Z_AttributeValue_numeric;
1840 ae->value.numeric = (int *)
1841 nmem_malloc (stream, sizeof(*ae->value.numeric));
1842 *ae->value.numeric = use_value;
1844 sks->sortRelation = (int *)
1845 nmem_malloc (stream, sizeof(*sks->sortRelation));
1846 if (sort_relation_value == 1)
1847 *sks->sortRelation = Z_SortKeySpec_ascending;
1848 else if (sort_relation_value == 2)
1849 *sks->sortRelation = Z_SortKeySpec_descending;
1851 *sks->sortRelation = Z_SortKeySpec_ascending;
1853 sks->caseSensitivity = (int *)
1854 nmem_malloc (stream, sizeof(*sks->caseSensitivity));
1855 *sks->caseSensitivity = 0;
1857 sks->which = Z_SortKeySpec_null;
1858 sks->u.null = odr_nullval ();
1859 sort_sequence->specs[i] = sks;
1860 return rsnull_create (NULL,key_it_ctrl);
1861 /* FIXME - nmem?? */
1865 static int parse_xpath(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1866 oid_value attributeSet,
1867 struct xpath_location_step *xpath, int max, NMEM mem)
1869 oid_value curAttributeSet = attributeSet;
1871 const char *use_string = 0;
1873 attr_init (&use, zapt, 1);
1874 attr_find_ex (&use, &curAttributeSet, &use_string);
1876 if (!use_string || *use_string != '/')
1879 return zebra_parse_xpath_str(use_string, xpath, max, mem);
1884 static RSET xpath_trunc(ZebraHandle zh, NMEM stream,
1885 int reg_type, const char *term, int use,
1886 oid_value curAttributeSet, NMEM rset_nmem)
1889 struct grep_info grep_info;
1890 char term_dict[2048];
1893 int ord = zebraExplain_lookupSU (zh->reg->zei, curAttributeSet, use);
1894 int ord_len, i, r, max_pos;
1895 int term_type = Z_Term_characterString;
1896 const char *flags = "void";
1898 if (grep_info_prepare (zh, 0 /* zapt */, &grep_info, '0', stream))
1899 return rsnull_create (rset_nmem,key_it_ctrl);
1902 return rsnull_create (rset_nmem,key_it_ctrl);
1904 term_dict[prefix_len++] = '|';
1906 term_dict[prefix_len++] = '(';
1908 ord_len = key_SU_encode (ord, ord_buf);
1909 for (i = 0; i<ord_len; i++)
1911 term_dict[prefix_len++] = 1;
1912 term_dict[prefix_len++] = ord_buf[i];
1914 term_dict[prefix_len++] = ')';
1915 term_dict[prefix_len++] = 1;
1916 term_dict[prefix_len++] = reg_type;
1918 strcpy (term_dict+prefix_len, term);
1920 grep_info.isam_p_indx = 0;
1921 r = dict_lookup_grep (zh->reg->dict, term_dict, 0,
1922 &grep_info, &max_pos, 0, grep_handle);
1923 yaz_log (LOG_LOG, "%s %d positions", term,
1924 grep_info.isam_p_indx);
1925 rset = rset_trunc (zh, grep_info.isam_p_buf,
1926 grep_info.isam_p_indx, term, strlen(term),
1927 flags, 1, term_type,rset_nmem,
1928 key_it_ctrl, key_it_ctrl->scope);
1929 grep_info_delete (&grep_info);
1933 static RSET rpn_search_xpath (ZebraHandle zh,
1934 oid_value attributeSet,
1935 int num_bases, char **basenames,
1936 NMEM stream, const char *rank_type, RSET rset,
1937 int xpath_len, struct xpath_location_step *xpath,
1940 oid_value curAttributeSet = attributeSet;
1947 yaz_log (LOG_LOG, "len=%d", xpath_len);
1948 for (i = 0; i<xpath_len; i++)
1950 yaz_log (LOG_LOG, "XPATH %d %s", i, xpath[i].part);
1954 curAttributeSet = VAL_IDXPATH;
1964 a[@attr=value]/b[@other=othervalue]
1966 /e/@a val range(e/,range(@a,freetext(w,1015,val),@a),e/)
1967 /a/b val range(b/a/,freetext(w,1016,val),b/a/)
1968 /a/b/@c val range(b/a/,range(@c,freetext(w,1016,val),@c),b/a/)
1969 /a/b[@c=y] val range(b/a/,freetext(w,1016,val),b/a/,@c=y)
1970 /a[@c=y]/b val range(a/,range(b/a/,freetext(w,1016,val),b/a/),a/,@c=y)
1971 /a[@c=x]/b[@c=y] range(a/,range(b/a/,freetext(w,1016,val),b/a/,@c=y),a/,@c=x)
1975 dict_grep_cmap (zh->reg->dict, 0, 0);
1977 for (base_no = 0; base_no < num_bases; base_no++)
1979 int level = xpath_len;
1982 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
1984 zh->errCode = 109; /* Database unavailable */
1985 zh->errString = basenames[base_no];
1988 while (--level >= 0)
1990 char xpath_rev[128];
1992 RSET rset_start_tag = 0, rset_end_tag = 0, rset_attr = 0;
1996 for (i = level; i >= 1; --i)
1998 const char *cp = xpath[i].part;
2004 memcpy (xpath_rev + len, "[^/]*", 5);
2007 else if (*cp == ' ')
2010 xpath_rev[len++] = 1;
2011 xpath_rev[len++] = ' ';
2015 xpath_rev[len++] = *cp;
2016 xpath_rev[len++] = '/';
2018 else if (i == 1) /* // case */
2020 xpath_rev[len++] = '.';
2021 xpath_rev[len++] = '*';
2026 if (xpath[level].predicate &&
2027 xpath[level].predicate->which == XPATH_PREDICATE_RELATION &&
2028 xpath[level].predicate->u.relation.name[0])
2030 WRBUF wbuf = wrbuf_alloc();
2031 wrbuf_puts(wbuf, xpath[level].predicate->u.relation.name+1);
2032 if (xpath[level].predicate->u.relation.value)
2034 const char *cp = xpath[level].predicate->u.relation.value;
2035 wrbuf_putc(wbuf, '=');
2039 if (strchr(REGEX_CHARS, *cp))
2040 wrbuf_putc(wbuf, '\\');
2041 wrbuf_putc(wbuf, *cp);
2045 wrbuf_puts(wbuf, "");
2046 rset_attr = xpath_trunc (
2047 zh, stream, '0', wrbuf_buf(wbuf), 3,
2048 curAttributeSet,rset_nmem);
2049 wrbuf_free(wbuf, 1);
2056 yaz_log (LOG_LOG, "xpath_rev (%d) = %s", level, xpath_rev);
2057 if (strlen(xpath_rev))
2059 rset_start_tag = xpath_trunc(zh, stream, '0',
2060 xpath_rev, 1, curAttributeSet, rset_nmem);
2062 rset_end_tag = xpath_trunc(zh, stream, '0',
2063 xpath_rev, 2, curAttributeSet, rset_nmem);
2066 parms.key_size = sizeof(struct it_key);
2067 parms.cmp = key_compare_it;
2068 parms.rset_l = rset_start_tag;
2069 parms.rset_m = rset;
2070 parms.rset_r = rset_end_tag;
2071 parms.rset_attr = rset_attr;
2072 parms.printer = key_print_it;
2073 rset = rset_create (rset_kind_between, &parms);
2075 rset=rsbetween_create( rset_nmem,key_it_ctrl,key_it_ctrl->scope,
2076 rset_start_tag, rset, rset_end_tag, rset_attr);
2087 static RSET rpn_search_APT (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2088 oid_value attributeSet, NMEM stream,
2089 Z_SortKeySpecList *sort_sequence,
2090 int num_bases, char **basenames,
2094 char *search_type = NULL;
2095 char rank_type[128];
2098 char termz[IT_MAX_WORD+1];
2102 struct xpath_location_step xpath[10];
2104 zebra_maps_attr (zh->reg->zebra_maps, zapt, ®_id, &search_type,
2105 rank_type, &complete_flag, &sort_flag);
2107 logf (LOG_DEBUG, "reg_id=%c", reg_id);
2108 logf (LOG_DEBUG, "complete_flag=%d", complete_flag);
2109 logf (LOG_DEBUG, "search_type=%s", search_type);
2110 logf (LOG_DEBUG, "rank_type=%s", rank_type);
2112 if (zapt_term_to_utf8(zh, zapt, termz))
2116 return rpn_sort_spec (zh, zapt, attributeSet, stream, sort_sequence,
2118 xpath_len = parse_xpath(zh, zapt, attributeSet, xpath, 10, stream);
2122 if (xpath[xpath_len-1].part[0] == '@')
2126 if (!strcmp (search_type, "phrase"))
2128 rset = rpn_search_APT_phrase (zh, zapt, termz, attributeSet, stream,
2129 reg_id, complete_flag, rank_type,
2131 num_bases, basenames, rset_nmem);
2133 else if (!strcmp (search_type, "and-list"))
2135 rset = rpn_search_APT_and_list (zh, zapt, termz, attributeSet, stream,
2136 reg_id, complete_flag, rank_type,
2138 num_bases, basenames, rset_nmem);
2140 else if (!strcmp (search_type, "or-list"))
2142 rset = rpn_search_APT_or_list (zh, zapt, termz, attributeSet, stream,
2143 reg_id, complete_flag, rank_type,
2145 num_bases, basenames, rset_nmem);
2147 else if (!strcmp (search_type, "local"))
2149 rset = rpn_search_APT_local (zh, zapt, termz, attributeSet, stream,
2150 rank_type, rset_nmem);
2152 else if (!strcmp (search_type, "numeric"))
2154 rset = rpn_search_APT_numeric (zh, zapt, termz, attributeSet, stream,
2155 reg_id, complete_flag, rank_type,
2157 num_bases, basenames, rset_nmem);
2159 else if (!strcmp (search_type, "always"))
2165 return rpn_search_xpath (zh, attributeSet, num_bases, basenames,
2166 stream, rank_type, rset,
2167 xpath_len, xpath, rset_nmem);
2170 static RSET rpn_search_structure (ZebraHandle zh, Z_RPNStructure *zs,
2171 oid_value attributeSet,
2172 NMEM stream, NMEM rset_nmem,
2173 Z_SortKeySpecList *sort_sequence,
2174 int num_bases, char **basenames)
2177 if (zs->which == Z_RPNStructure_complex)
2179 Z_Operator *zop = zs->u.complex->roperator;
2183 rset_l = rpn_search_structure (zh, zs->u.complex->s1,
2184 attributeSet, stream, rset_nmem,
2186 num_bases, basenames);
2189 rset_r = rpn_search_structure (zh, zs->u.complex->s2,
2190 attributeSet, stream, rset_nmem,
2192 num_bases, basenames);
2195 rset_delete (rset_l);
2201 case Z_Operator_and:
2202 r = rsbool_create_and(rset_nmem,key_it_ctrl, key_it_ctrl->scope,
2206 r = rsbool_create_or(rset_nmem,key_it_ctrl, key_it_ctrl->scope,
2209 case Z_Operator_and_not:
2210 r = rsbool_create_not(rset_nmem,key_it_ctrl, key_it_ctrl->scope,
2213 case Z_Operator_prox:
2214 if (zop->u.prox->which != Z_ProximityOperator_known)
2219 if (*zop->u.prox->u.known != Z_ProxUnit_word)
2221 char *val = (char *) nmem_malloc (stream, 16);
2223 zh->errString = val;
2224 sprintf (val, "%d", *zop->u.prox->u.known);
2229 /* new / old prox */
2232 twosets[0] = rset_l;
2233 twosets[1] = rset_r;
2234 r=rsprox_create(rset_nmem,key_it_ctrl,key_it_ctrl->scope,
2236 *zop->u.prox->ordered,
2237 (!zop->u.prox->exclusion ?
2238 0 : *zop->u.prox->exclusion),
2239 *zop->u.prox->relationType,
2240 *zop->u.prox->distance );
2248 else if (zs->which == Z_RPNStructure_simple)
2250 if (zs->u.simple->which == Z_Operand_APT)
2252 logf (LOG_DEBUG, "rpn_search_APT");
2253 r = rpn_search_APT (zh, zs->u.simple->u.attributesPlusTerm,
2254 attributeSet, stream, sort_sequence,
2255 num_bases, basenames,rset_nmem);
2257 else if (zs->u.simple->which == Z_Operand_resultSetId)
2259 logf (LOG_DEBUG, "rpn_search_ref");
2260 r = resultSetRef (zh, zs->u.simple->u.resultSetId);
2263 r = rsnull_create (rset_nmem,key_it_ctrl);
2266 nmem_strdup (stream, zs->u.simple->u.resultSetId);
2287 RSET rpn_search (ZebraHandle zh, NMEM nmem, NMEM rset_nmem,
2288 Z_RPNQuery *rpn, int num_bases, char **basenames,
2289 const char *setname,
2294 oid_value attributeSet;
2295 Z_SortKeySpecList *sort_sequence;
2299 zh->errString = NULL;
2302 sort_sequence = (Z_SortKeySpecList *)
2303 nmem_malloc (nmem, sizeof(*sort_sequence));
2304 sort_sequence->num_specs = 10;
2305 sort_sequence->specs = (Z_SortKeySpec **)
2306 nmem_malloc (nmem, sort_sequence->num_specs *
2307 sizeof(*sort_sequence->specs));
2308 for (i = 0; i<sort_sequence->num_specs; i++)
2309 sort_sequence->specs[i] = 0;
2311 attrset = oid_getentbyoid (rpn->attributeSetId);
2312 attributeSet = attrset->value;
2313 rset = rpn_search_structure (zh, rpn->RPNStructure, attributeSet,
2315 sort_sequence, num_bases, basenames);
2320 logf (LOG_DEBUG, "search error: %d", zh->errCode);
2322 for (i = 0; sort_sequence->specs[i]; i++)
2324 sort_sequence->num_specs = i;
2326 resultSetRank (zh, sset, rset);
2329 logf (LOG_DEBUG, "resultSetSortSingle in rpn_search");
2330 resultSetSortSingle (zh, nmem, sset, rset,
2331 sort_sequence, &sort_status);
2334 logf (LOG_DEBUG, "resultSetSortSingle status = %d", zh->errCode);
2340 struct scan_info_entry {
2346 struct scan_info_entry *list;
2352 static int scan_handle (char *name, const char *info, int pos, void *client)
2354 int len_prefix, idx;
2355 struct scan_info *scan_info = (struct scan_info *) client;
2357 len_prefix = strlen(scan_info->prefix);
2358 if (memcmp (name, scan_info->prefix, len_prefix))
2360 if (pos > 0) idx = scan_info->after - pos + scan_info->before;
2363 scan_info->list[idx].term = (char *)
2364 odr_malloc (scan_info->odr, strlen(name + len_prefix)+1);
2365 strcpy (scan_info->list[idx].term, name + len_prefix);
2366 assert (*info == sizeof(ISAMC_P));
2367 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAMC_P));
2371 static void scan_term_untrans (ZebraHandle zh, NMEM stream, int reg_type,
2372 char **dst, const char *src)
2374 char term_src[IT_MAX_WORD];
2375 char term_dst[IT_MAX_WORD];
2377 term_untrans (zh, reg_type, term_src, src);
2379 if (zh->iconv_from_utf8 != 0)
2382 char *inbuf = term_src;
2383 size_t inleft = strlen(term_src);
2384 char *outbuf = term_dst;
2385 size_t outleft = sizeof(term_dst)-1;
2388 ret = yaz_iconv (zh->iconv_from_utf8, &inbuf, &inleft,
2390 if (ret == (size_t)(-1))
2393 len = outbuf - term_dst;
2394 *dst = nmem_malloc (stream, len + 1);
2396 memcpy (*dst, term_dst, len);
2400 *dst = nmem_strdup (stream, term_src);
2403 static void count_set (RSET r, int *count)
2410 logf (LOG_DEBUG, "count_set");
2413 rfd = rset_open (r, RSETF_READ);
2414 while (rset_read (rfd, &key))
2416 if (key.mem[0] != psysno)
2418 psysno = key.mem[0];
2424 logf (LOG_DEBUG, "%d keys, %d records", kno, *count);
2427 void rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
2428 oid_value attributeset,
2429 int num_bases, char **basenames,
2430 int *position, int *num_entries, ZebraScanEntry **list,
2431 int *is_partial, RSET limit_set, int return_zero)
2434 int pos = *position;
2435 int num = *num_entries;
2439 char termz[IT_MAX_WORD+20];
2442 const char *use_string = 0;
2443 struct scan_info *scan_info_array;
2444 ZebraScanEntry *glist;
2445 int ords[32], ord_no = 0;
2448 int bases_ok = 0; /* no of databases with OK attribute */
2449 int errCode = 0; /* err code (if any is not OK) */
2450 char *errString = 0; /* addinfo */
2453 char *search_type = NULL;
2454 char rank_type[128];
2457 NMEM rset_nmem=NULL;
2461 if (attributeset == VAL_NONE)
2462 attributeset = VAL_BIB1;
2467 int termset_value_numeric;
2468 const char *termset_value_string;
2469 attr_init (&termset, zapt, 8);
2470 termset_value_numeric =
2471 attr_find_ex (&termset, NULL, &termset_value_string);
2472 if (termset_value_numeric != -1)
2475 const char *termset_name = 0;
2477 if (termset_value_numeric != -2)
2480 sprintf (resname, "%d", termset_value_numeric);
2481 termset_name = resname;
2484 termset_name = termset_value_string;
2486 limit_set = resultSetRef (zh, termset_name);
2490 yaz_log (LOG_DEBUG, "position = %d, num = %d set=%d",
2491 pos, num, attributeset);
2493 attr_init (&use, zapt, 1);
2494 use_value = attr_find_ex (&use, &attributeset, &use_string);
2496 if (zebra_maps_attr (zh->reg->zebra_maps, zapt, ®_id, &search_type,
2497 rank_type, &complete_flag, &sort_flag))
2503 yaz_log (LOG_DEBUG, "use_value = %d", use_value);
2505 if (use_value == -1)
2507 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
2511 data1_local_attribute *local_attr;
2513 if ((r=att_getentbyatt (zh, &attp, attributeset, use_value,
2516 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
2517 attributeset, use_value);
2521 sprintf (val_str, "%d", use_value);
2523 errString = odr_strdup (stream, val_str);
2529 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
2531 zh->errString = basenames[base_no];
2532 zh->errCode = 109; /* Database unavailable */
2537 for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
2538 local_attr = local_attr->next)
2542 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
2545 ords[ord_no++] = ord;
2548 if (!bases_ok && errCode)
2550 zh->errCode = errCode;
2551 zh->errString = errString;
2557 sprintf (val_str, "%d", use_value);
2559 zh->errString = odr_strdup (stream, val_str);
2564 /* prepare dictionary scanning */
2567 scan_info_array = (struct scan_info *)
2568 odr_malloc (stream, ord_no * sizeof(*scan_info_array));
2569 for (i = 0; i < ord_no; i++)
2571 int j, prefix_len = 0;
2572 int before_tmp = before, after_tmp = after;
2573 struct scan_info *scan_info = scan_info_array + i;
2574 struct rpn_char_map_info rcmi;
2576 rpn_char_map_prepare (zh->reg, reg_id, &rcmi);
2578 scan_info->before = before;
2579 scan_info->after = after;
2580 scan_info->odr = stream;
2582 scan_info->list = (struct scan_info_entry *)
2583 odr_malloc (stream, (before+after) * sizeof(*scan_info->list));
2584 for (j = 0; j<before+after; j++)
2585 scan_info->list[j].term = NULL;
2587 prefix_len += key_SU_encode (ords[i], termz + prefix_len);
2588 termz[prefix_len++] = reg_id;
2589 termz[prefix_len] = 0;
2590 strcpy (scan_info->prefix, termz);
2592 if (trans_scan_term (zh, zapt, termz+prefix_len, reg_id))
2595 dict_scan (zh->reg->dict, termz, &before_tmp, &after_tmp,
2596 scan_info, scan_handle);
2598 glist = (ZebraScanEntry *)
2599 odr_malloc (stream, (before+after)*sizeof(*glist));
2601 rset_nmem=nmem_create();
2603 /* consider terms after main term */
2604 for (i = 0; i < ord_no; i++)
2608 for (i = 0; i<after; i++)
2611 const char *mterm = NULL;
2615 for (j = 0; j < ord_no; j++)
2617 if (ptr[j] < before+after &&
2618 (tst=scan_info_array[j].list[ptr[j]].term) &&
2619 (!mterm || strcmp (tst, mterm) < 0))
2627 scan_term_untrans (zh, stream->mem, reg_id,
2628 &glist[i+before].term, mterm);
2629 rset = rset_trunc (zh, &scan_info_array[j0].list[ptr[j0]].isam_p, 1,
2630 glist[i+before].term, strlen(glist[i+before].term),
2631 NULL, 0, zapt->term->which, rset_nmem,
2632 key_it_ctrl,key_it_ctrl->scope);
2634 for (j = j0+1; j<ord_no; j++)
2636 if (ptr[j] < before+after &&
2637 (tst=scan_info_array[j].list[ptr[j]].term) &&
2638 !strcmp (tst, mterm))
2643 rset_trunc (zh, &scan_info_array[j].list[ptr[j]].isam_p, 1,
2644 glist[i+before].term,
2645 strlen(glist[i+before].term), NULL, 0,
2646 zapt->term->which,rset_nmem,
2647 key_it_ctrl, key_it_ctrl->scope);
2648 rset = rsbool_create_or(rset_nmem,key_it_ctrl,
2649 key_it_ctrl->scope, rset, rset2);
2650 /* FIXME - Use a proper multi-or */
2656 rset = rsbool_create_and(rset_nmem,key_it_ctrl,key_it_ctrl->scope,
2657 rset, rset_dup(limit_set));
2658 count_set (rset, &glist[i+before].occurrences);
2663 *num_entries -= (after-i);
2667 /* consider terms before main term */
2668 for (i = 0; i<ord_no; i++)
2671 for (i = 0; i<before; i++)
2674 const char *mterm = NULL;
2678 for (j = 0; j <ord_no; j++)
2680 if (ptr[j] < before &&
2681 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2682 (!mterm || strcmp (tst, mterm) > 0))
2691 scan_term_untrans (zh, stream->mem, reg_id,
2692 &glist[before-1-i].term, mterm);
2695 (zh, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1,
2696 glist[before-1-i].term, strlen(glist[before-1-i].term),
2697 NULL, 0, zapt->term->which,rset_nmem,
2698 key_it_ctrl,key_it_ctrl->scope);
2702 for (j = j0+1; j<ord_no; j++)
2704 if (ptr[j] < before &&
2705 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2706 !strcmp (tst, mterm))
2710 rset2 = rset_trunc (zh,
2711 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1,
2712 glist[before-1-i].term,
2713 strlen(glist[before-1-i].term), NULL, 0,
2714 zapt->term->which, rset_nmem,
2715 key_it_ctrl, key_it_ctrl->scope);
2716 rset = rsbool_create_and(rset_nmem,key_it_ctrl,
2717 key_it_ctrl->scope, rset, rset2);
2718 /* FIXME - multi-and ?? */
2723 rset = rsbool_create_and(rset_nmem,key_it_ctrl,key_it_ctrl->scope,
2724 rset, rset_dup(limit_set));
2725 count_set (rset, &glist[before-1-i].occurrences);
2736 nmem_destroy(rset_nmem);
2737 *list = glist + i; /* list is set to first 'real' entry */
2739 logf (LOG_DEBUG, "position = %d, num_entries = %d",
2740 *position, *num_entries);
2742 logf (LOG_DEBUG, "scan error: %d", zh->errCode);