1 /* $Id: zrpn.c,v 1.141 2004-08-03 12:15:44 heikki Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
34 #include <zebra_xpath.h>
40 #include <rsbetween.h>
43 struct rpn_char_map_info {
52 Z_AttributesPlusTerm *zapt;
56 static const char **rpn_char_map_handler (void *vp, const char **from, int len)
58 struct rpn_char_map_info *p = (struct rpn_char_map_info *) vp;
59 const char **out = zebra_maps_input (p->zm, p->reg_type, from, len);
63 const char *outp = *out;
64 yaz_log (LOG_LOG, "---");
67 yaz_log (LOG_LOG, "%02X", *outp);
75 static void rpn_char_map_prepare (struct zebra_register *reg, int reg_type,
76 struct rpn_char_map_info *map_info)
78 map_info->zm = reg->zebra_maps;
79 map_info->reg_type = reg_type;
80 dict_grep_cmap (reg->dict, map_info, rpn_char_map_handler);
83 static int attr_find_ex (AttrType *src, oid_value *attributeSetP,
84 const char **string_value)
88 num_attributes = src->zapt->attributes->num_attributes;
89 while (src->major < num_attributes)
91 Z_AttributeElement *element;
93 element = src->zapt->attributes->attributes[src->major];
94 if (src->type == *element->attributeType)
96 switch (element->which)
98 case Z_AttributeValue_numeric:
100 if (element->attributeSet && attributeSetP)
104 attrset = oid_getentbyoid (element->attributeSet);
105 *attributeSetP = attrset->value;
107 return *element->value.numeric;
109 case Z_AttributeValue_complex:
110 if (src->minor >= element->value.complex->num_list)
112 if (element->attributeSet && attributeSetP)
116 attrset = oid_getentbyoid (element->attributeSet);
117 *attributeSetP = attrset->value;
119 if (element->value.complex->list[src->minor]->which ==
120 Z_StringOrNumeric_numeric)
124 *element->value.complex->list[src->minor-1]->u.numeric;
126 else if (element->value.complex->list[src->minor]->which ==
127 Z_StringOrNumeric_string)
133 element->value.complex->list[src->minor-1]->u.string;
147 static int attr_find (AttrType *src, oid_value *attributeSetP)
149 return attr_find_ex (src, attributeSetP, 0);
152 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
175 static void term_untrans (ZebraHandle zh, int reg_type,
176 char *dst, const char *src)
181 const char *cp = zebra_maps_output (zh->reg->zebra_maps,
183 if (!cp && len < IT_MAX_WORD-1)
186 while (*cp && len < IT_MAX_WORD-1)
192 static void add_isam_p (const char *name, const char *info,
195 if (p->isam_p_indx == p->isam_p_size)
197 ISAMS_P *new_isam_p_buf;
201 p->isam_p_size = 2*p->isam_p_size + 100;
202 new_isam_p_buf = (ISAMS_P *) xmalloc (sizeof(*new_isam_p_buf) *
206 memcpy (new_isam_p_buf, p->isam_p_buf,
207 p->isam_p_indx * sizeof(*p->isam_p_buf));
208 xfree (p->isam_p_buf);
210 p->isam_p_buf = new_isam_p_buf;
213 new_term_no = (int *) xmalloc (sizeof(*new_term_no) *
217 memcpy (new_term_no, p->isam_p_buf,
218 p->isam_p_indx * sizeof(*p->term_no));
221 p->term_no = new_term_no;
224 assert (*info == sizeof(*p->isam_p_buf));
225 memcpy (p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
232 char term_tmp[IT_MAX_WORD];
234 int len = key_SU_decode (&su_code, name);
236 term_untrans (p->zh, p->reg_type, term_tmp, name+len+1);
237 logf (LOG_LOG, "grep: %d %c %s", su_code, name[len], term_tmp);
238 zebraExplain_lookup_ord (p->zh->reg->zei,
239 su_code, &db, &set, &use);
240 logf (LOG_LOG, "grep: set=%d use=%d db=%s", set, use, db);
242 resultSetAddTerm (p->zh, p->termset, name[len], db,
249 static int grep_handle (char *name, const char *info, void *p)
251 add_isam_p (name, info, (struct grep_info *) p);
255 static int term_pre (ZebraMaps zebra_maps, int reg_type, const char **src,
256 const char *ct1, const char *ct2)
258 const char *s1, *s0 = *src;
261 /* skip white space */
264 if (ct1 && strchr (ct1, *s0))
266 if (ct2 && strchr (ct2, *s0))
269 map = zebra_maps_input (zebra_maps, reg_type, &s1, strlen(s1));
270 if (**map != *CHR_SPACE)
278 #define REGEX_CHARS " []()|.*+?!"
280 /* term_100: handle term, where trunc=none (no operators at all) */
281 static int term_100 (ZebraMaps zebra_maps, int reg_type,
282 const char **src, char *dst, int space_split,
290 const char *space_start = 0;
291 const char *space_end = 0;
293 if (!term_pre (zebra_maps, reg_type, src, NULL, NULL))
299 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
302 if (**map == *CHR_SPACE)
305 else /* complete subfield only. */
307 if (**map == *CHR_SPACE)
308 { /* save space mapping for later .. */
313 else if (space_start)
314 { /* reload last space */
315 while (space_start < space_end)
317 if (strchr (REGEX_CHARS, *space_start))
319 dst_term[j++] = *space_start;
320 dst[i++] = *space_start++;
323 space_start = space_end = 0;
326 /* add non-space char */
329 if (strchr(REGEX_CHARS, *s1))
341 /* term_101: handle term, where trunc=Process # */
342 static int term_101 (ZebraMaps zebra_maps, int reg_type,
343 const char **src, char *dst, int space_split,
351 if (!term_pre (zebra_maps, reg_type, src, "#", "#"))
360 dst_term[j++] = *s0++;
365 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
366 if (space_split && **map == *CHR_SPACE)
370 if (strchr(REGEX_CHARS, *s1))
378 dst_term[j++] = '\0';
383 /* term_103: handle term, where trunc=re-2 (regular expressions) */
384 static int term_103 (ZebraMaps zebra_maps, int reg_type, const char **src,
385 char *dst, int *errors, int space_split,
393 if (!term_pre (zebra_maps, reg_type, src, "^\\()[].*+?|", "("))
396 if (errors && *s0 == '+' && s0[1] && s0[2] == '+' && s0[3] &&
399 *errors = s0[1] - '0';
406 if (strchr ("^\\()[].*+?|-", *s0))
414 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
415 if (**map == *CHR_SPACE)
419 if (strchr(REGEX_CHARS, *s1))
432 /* term_103: handle term, where trunc=re-1 (regular expressions) */
433 static int term_102 (ZebraMaps zebra_maps, int reg_type, const char **src,
434 char *dst, int space_split, char *dst_term)
436 return term_103 (zebra_maps, reg_type, src, dst, NULL, space_split,
441 /* term_104: handle term, where trunc=Process # and ! */
442 static int term_104 (ZebraMaps zebra_maps, int reg_type,
443 const char **src, char *dst, int space_split,
451 if (!term_pre (zebra_maps, reg_type, src, "?*#", "?*#"))
458 dst_term[j++] = *s0++;
459 if (*s0 >= '0' && *s0 <= '9')
462 while (*s0 >= '0' && *s0 <= '9')
464 limit = limit * 10 + (*s0 - '0');
465 dst_term[j++] = *s0++;
485 dst_term[j++] = *s0++;
490 dst_term[j++] = *s0++;
494 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
495 if (space_split && **map == *CHR_SPACE)
499 if (strchr(REGEX_CHARS, *s1))
507 dst_term[j++] = '\0';
512 /* term_105/106: handle term, where trunc=Process * and ! and right trunc */
513 static int term_105 (ZebraMaps zebra_maps, int reg_type,
514 const char **src, char *dst, int space_split,
515 char *dst_term, int right_truncate)
522 if (!term_pre (zebra_maps, reg_type, src, "*!", "*!"))
531 dst_term[j++] = *s0++;
536 dst_term[j++] = *s0++;
540 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
541 if (space_split && **map == *CHR_SPACE)
545 if (strchr(REGEX_CHARS, *s1))
559 dst_term[j++] = '\0';
565 /* gen_regular_rel - generate regular expression from relation
566 * val: border value (inclusive)
567 * islt: 1 if <=; 0 if >=.
569 static void gen_regular_rel (char *dst, int val, int islt)
576 logf (LOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
580 strcpy (dst, "(-[0-9]+|(");
588 strcpy (dst, "([0-9]+|-(");
589 dst_p = strlen (dst);
599 dst_p = strlen (dst);
600 sprintf (numstr, "%d", val);
601 for (w = strlen(numstr); --w >= 0; pos++)
620 strcpy (dst + dst_p, numstr);
621 dst_p = strlen(dst) - pos - 1;
649 for (i = 0; i<pos; i++)
662 /* match everything less than 10^(pos-1) */
664 for (i=1; i<pos; i++)
665 strcat (dst, "[0-9]?");
669 /* match everything greater than 10^pos */
670 for (i = 0; i <= pos; i++)
671 strcat (dst, "[0-9]");
672 strcat (dst, "[0-9]*");
677 void string_rel_add_char (char **term_p, const char *src, int *indx)
679 if (src[*indx] == '\\')
680 *(*term_p)++ = src[(*indx)++];
681 *(*term_p)++ = src[(*indx)++];
685 * > abc ([b-].*|a[c-].*|ab[d-].*|abc.+)
686 * ([^-a].*|a[^-b].*ab[^-c].*|abc.+)
687 * >= abc ([b-].*|a[c-].*|ab[c-].*)
688 * ([^-a].*|a[^-b].*|ab[c-].*)
689 * < abc ([-0].*|a[-a].*|ab[-b].*)
690 * ([^a-].*|a[^b-].*|ab[^c-].*)
691 * <= abc ([-0].*|a[-a].*|ab[-b].*|abc)
692 * ([^a-].*|a[^b-].*|ab[^c-].*|abc)
694 static int string_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
695 const char **term_sub, char *term_dict,
696 oid_value attributeSet,
697 int reg_type, int space_split, char *term_dst)
702 char *term_tmp = term_dict + strlen(term_dict);
703 char term_component[2*IT_MAX_WORD+20];
705 attr_init (&relation, zapt, 2);
706 relation_value = attr_find (&relation, NULL);
708 logf (LOG_DEBUG, "string relation value=%d", relation_value);
709 switch (relation_value)
712 if (!term_100 (zh->reg->zebra_maps, reg_type,
713 term_sub, term_component,
714 space_split, term_dst))
716 logf (LOG_DEBUG, "Relation <");
719 for (i = 0; term_component[i]; )
726 string_rel_add_char (&term_tmp, term_component, &j);
731 string_rel_add_char (&term_tmp, term_component, &i);
738 if ((term_tmp - term_dict) > IT_MAX_WORD)
745 if (!term_100 (zh->reg->zebra_maps, reg_type,
746 term_sub, term_component,
747 space_split, term_dst))
749 logf (LOG_DEBUG, "Relation <=");
752 for (i = 0; term_component[i]; )
757 string_rel_add_char (&term_tmp, term_component, &j);
761 string_rel_add_char (&term_tmp, term_component, &i);
770 if ((term_tmp - term_dict) > IT_MAX_WORD)
773 for (i = 0; term_component[i]; )
774 string_rel_add_char (&term_tmp, term_component, &i);
779 if (!term_100 (zh->reg->zebra_maps, reg_type,
780 term_sub, term_component, space_split, term_dst))
782 logf (LOG_DEBUG, "Relation >");
785 for (i = 0; term_component[i];)
790 string_rel_add_char (&term_tmp, term_component, &j);
795 string_rel_add_char (&term_tmp, term_component, &i);
803 if ((term_tmp - term_dict) > IT_MAX_WORD)
806 for (i = 0; term_component[i];)
807 string_rel_add_char (&term_tmp, term_component, &i);
814 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub,
815 term_component, space_split, term_dst))
817 logf (LOG_DEBUG, "Relation >=");
820 for (i = 0; term_component[i];)
827 string_rel_add_char (&term_tmp, term_component, &j);
830 if (term_component[i+1])
834 string_rel_add_char (&term_tmp, term_component, &i);
838 string_rel_add_char (&term_tmp, term_component, &i);
845 if ((term_tmp - term_dict) > IT_MAX_WORD)
853 logf (LOG_DEBUG, "Relation =");
854 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub,
855 term_component, space_split, term_dst))
857 strcat (term_tmp, "(");
858 strcat (term_tmp, term_component);
859 strcat (term_tmp, ")");
864 static int string_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
865 const char **term_sub,
866 oid_value attributeSet, NMEM stream,
867 struct grep_info *grep_info,
868 int reg_type, int complete_flag,
869 int num_bases, char **basenames,
870 char *term_dst, int xpath_use);
872 static RSET term_trunc (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
873 const char **term_sub,
874 oid_value attributeSet, NMEM stream,
875 struct grep_info *grep_info,
876 int reg_type, int complete_flag,
877 int num_bases, char **basenames,
879 const char *rank_type, int xpath_use)
882 grep_info->isam_p_indx = 0;
883 r = string_term (zh, zapt, term_sub, attributeSet, stream, grep_info,
884 reg_type, complete_flag, num_bases, basenames,
885 term_dst, xpath_use);
888 logf (LOG_DEBUG, "term: %s", term_dst);
889 return rset_trunc (zh, grep_info->isam_p_buf,
890 grep_info->isam_p_indx, term_dst,
891 strlen(term_dst), rank_type, 1 /* preserve pos */,
896 static int string_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
897 const char **term_sub,
898 oid_value attributeSet, NMEM stream,
899 struct grep_info *grep_info,
900 int reg_type, int complete_flag,
901 int num_bases, char **basenames,
902 char *term_dst, int xpath_use)
904 char term_dict[2*IT_MAX_WORD+4000];
907 int truncation_value;
910 const char *use_string = 0;
911 oid_value curAttributeSet = attributeSet;
913 struct rpn_char_map_info rcmi;
914 int space_split = complete_flag ? 0 : 1;
916 int bases_ok = 0; /* no of databases with OK attribute */
917 int errCode = 0; /* err code (if any is not OK) */
918 char *errString = 0; /* addinfo */
920 rpn_char_map_prepare (zh->reg, reg_type, &rcmi);
921 attr_init (&use, zapt, 1);
922 use_value = attr_find_ex (&use, &curAttributeSet, &use_string);
923 logf (LOG_DEBUG, "string_term, use value %d", use_value);
924 attr_init (&truncation, zapt, 5);
925 truncation_value = attr_find (&truncation, NULL);
926 logf (LOG_DEBUG, "truncation value %d", truncation_value);
928 if (use_value == -1) /* no attribute - assumy "any" */
930 for (base_no = 0; base_no < num_bases; base_no++)
933 data1_local_attribute id_xpath_attr;
934 data1_local_attribute *local_attr;
935 int max_pos, prefix_len = 0;
939 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
941 zh->errCode = 109; /* Database unavailable */
942 zh->errString = basenames[base_no];
945 if (xpath_use > 0 && use_value == -2)
947 use_value = xpath_use;
948 attp.local_attributes = &id_xpath_attr;
949 attp.attset_ordinal = VAL_IDXPATH;
950 id_xpath_attr.next = 0;
951 id_xpath_attr.local = use_value;
953 else if (curAttributeSet == VAL_IDXPATH)
955 attp.local_attributes = &id_xpath_attr;
956 attp.attset_ordinal = VAL_IDXPATH;
957 id_xpath_attr.next = 0;
958 id_xpath_attr.local = use_value;
962 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value,
965 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
966 curAttributeSet, use_value, r);
969 /* set was found, but value wasn't defined */
972 errString = nmem_strdup(stream, use_string);
976 sprintf (val_str, "%d", use_value);
977 errString = nmem_strdup (stream, val_str);
983 struct oident oident;
985 oident.proto = PROTO_Z3950;
986 oident.oclass = CLASS_ATTSET;
987 oident.value = curAttributeSet;
988 oid_ent_to_oid (&oident, oid);
991 errString = nmem_strdup (stream, oident.desc);
996 for (local_attr = attp.local_attributes; local_attr;
997 local_attr = local_attr->next)
1003 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
1008 term_dict[prefix_len++] = '|';
1010 term_dict[prefix_len++] = '(';
1012 ord_len = key_SU_encode (ord, ord_buf);
1013 for (i = 0; i<ord_len; i++)
1015 term_dict[prefix_len++] = 1;
1016 term_dict[prefix_len++] = ord_buf[i];
1025 sprintf (val_str, "%d", use_value);
1027 errString = nmem_strdup (stream, val_str);
1031 bases_ok++; /* this has OK attributes */
1033 term_dict[prefix_len++] = ')';
1034 term_dict[prefix_len++] = 1;
1035 term_dict[prefix_len++] = reg_type;
1036 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1037 term_dict[prefix_len] = '\0';
1039 switch (truncation_value)
1041 case -1: /* not specified */
1042 case 100: /* do not truncate */
1043 if (!string_relation (zh, zapt, &termp, term_dict,
1045 reg_type, space_split, term_dst))
1047 logf (LOG_LOG, "dict_lookup_grep: %s", term_dict+prefix_len);
1048 r = dict_lookup_grep (zh->reg->dict, term_dict, 0,
1049 grep_info, &max_pos, 0, grep_handle);
1051 logf (LOG_WARN, "dict_lookup_grep fail %d", r);
1053 case 1: /* right truncation */
1054 term_dict[j++] = '(';
1055 if (!term_100 (zh->reg->zebra_maps, reg_type,
1056 &termp, term_dict + j, space_split, term_dst))
1058 strcat (term_dict, ".*)");
1059 dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1060 &max_pos, 0, grep_handle);
1062 case 2: /* keft truncation */
1063 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1064 if (!term_100 (zh->reg->zebra_maps, reg_type,
1065 &termp, term_dict + j, space_split, term_dst))
1067 strcat (term_dict, ")");
1068 dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1069 &max_pos, 0, grep_handle);
1071 case 3: /* left&right truncation */
1072 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1073 if (!term_100 (zh->reg->zebra_maps, reg_type,
1074 &termp, term_dict + j, space_split, term_dst))
1076 strcat (term_dict, ".*)");
1077 dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1078 &max_pos, 0, grep_handle);
1082 case 101: /* process # in term */
1083 term_dict[j++] = '(';
1084 if (!term_101 (zh->reg->zebra_maps, reg_type,
1085 &termp, term_dict + j, space_split, term_dst))
1087 strcat (term_dict, ")");
1088 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1089 &max_pos, 0, grep_handle);
1091 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d", r);
1093 case 102: /* Regexp-1 */
1094 term_dict[j++] = '(';
1095 if (!term_102 (zh->reg->zebra_maps, reg_type,
1096 &termp, term_dict + j, space_split, term_dst))
1098 strcat (term_dict, ")");
1099 logf (LOG_DEBUG, "Regexp-1 tolerance=%d", r);
1100 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1101 &max_pos, 0, grep_handle);
1103 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
1106 case 103: /* Regexp-2 */
1108 term_dict[j++] = '(';
1109 if (!term_103 (zh->reg->zebra_maps, reg_type,
1110 &termp, term_dict + j, &r, space_split, term_dst))
1112 strcat (term_dict, ")");
1113 logf (LOG_DEBUG, "Regexp-2 tolerance=%d", r);
1114 r = dict_lookup_grep (zh->reg->dict, term_dict, r, grep_info,
1115 &max_pos, 2, grep_handle);
1117 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
1120 case 104: /* process # and ! in term */
1121 term_dict[j++] = '(';
1122 if (!term_104 (zh->reg->zebra_maps, reg_type,
1123 &termp, term_dict + j, space_split, term_dst))
1125 strcat (term_dict, ")");
1126 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1127 &max_pos, 0, grep_handle);
1129 logf (LOG_WARN, "dict_lookup_grep err, trunc=#/!: %d", r);
1131 case 105: /* process * and ! in term */
1132 term_dict[j++] = '(';
1133 if (!term_105 (zh->reg->zebra_maps, reg_type,
1134 &termp, term_dict + j, space_split, term_dst, 1))
1136 strcat (term_dict, ")");
1137 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1138 &max_pos, 0, grep_handle);
1140 logf (LOG_WARN, "dict_lookup_grep err, trunc=*/!: %d", r);
1142 case 106: /* process * and ! in term */
1143 term_dict[j++] = '(';
1144 if (!term_105 (zh->reg->zebra_maps, reg_type,
1145 &termp, term_dict + j, space_split, term_dst, 0))
1147 strcat (term_dict, ")");
1148 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1149 &max_pos, 0, grep_handle);
1151 logf (LOG_WARN, "dict_lookup_grep err, trunc=*/!: %d", r);
1157 zh->errCode = errCode;
1158 zh->errString = errString;
1162 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1167 /* convert APT search term to UTF8 */
1168 static int zapt_term_to_utf8 (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1172 Z_Term *term = zapt->term;
1174 switch (term->which)
1176 case Z_Term_general:
1177 if (zh->iconv_to_utf8 != 0)
1179 char *inbuf = term->u.general->buf;
1180 size_t inleft = term->u.general->len;
1181 char *outbuf = termz;
1182 size_t outleft = IT_MAX_WORD-1;
1185 ret = yaz_iconv(zh->iconv_to_utf8, &inbuf, &inleft,
1187 if (ret == (size_t)(-1))
1189 ret = yaz_iconv(zh->iconv_to_utf8, 0, 0, 0, 0);
1197 sizez = term->u.general->len;
1198 if (sizez > IT_MAX_WORD-1)
1199 sizez = IT_MAX_WORD-1;
1200 memcpy (termz, term->u.general->buf, sizez);
1201 termz[sizez] = '\0';
1204 case Z_Term_characterString:
1205 sizez = strlen(term->u.characterString);
1206 if (sizez > IT_MAX_WORD-1)
1207 sizez = IT_MAX_WORD-1;
1208 memcpy (termz, term->u.characterString, sizez);
1209 termz[sizez] = '\0';
1218 /* convert APT SCAN term to internal cmap */
1219 static int trans_scan_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1220 char *termz, int reg_type)
1222 char termz0[IT_MAX_WORD];
1224 if (zapt_term_to_utf8(zh, zapt, termz0))
1225 return -1; /* error */
1229 const char *cp = (const char *) termz0;
1230 const char *cp_end = cp + strlen(cp);
1233 const char *space_map = NULL;
1236 while ((len = (cp_end - cp)) > 0)
1238 map = zebra_maps_input (zh->reg->zebra_maps, reg_type, &cp, len);
1239 if (**map == *CHR_SPACE)
1244 for (src = space_map; *src; src++)
1247 for (src = *map; *src; src++)
1256 char *normalize_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1257 const char *termz, NMEM stream, unsigned reg_id)
1260 AttrType truncation;
1261 int truncation_value;
1264 attr_init (&truncation, zapt, 5);
1265 truncation_value = attr_find (&truncation, NULL);
1267 switch (truncation_value)
1287 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, ex_list,
1288 termz, strlen(termz));
1290 return nmem_strdup(stream, termz);
1293 char *buf = (char*) nmem_malloc (stream, wrbuf_len(wrbuf)+1);
1294 memcpy (buf, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1295 buf[wrbuf_len(wrbuf)] = '\0';
1300 static void grep_info_delete (struct grep_info *grep_info)
1303 xfree(grep_info->term_no);
1305 xfree (grep_info->isam_p_buf);
1308 static int grep_info_prepare (ZebraHandle zh,
1309 Z_AttributesPlusTerm *zapt,
1310 struct grep_info *grep_info,
1315 int termset_value_numeric;
1316 const char *termset_value_string;
1319 grep_info->term_no = 0;
1321 grep_info->isam_p_size = 0;
1322 grep_info->isam_p_buf = NULL;
1324 grep_info->reg_type = reg_type;
1325 grep_info->termset = 0;
1329 attr_init (&termset, zapt, 8);
1330 termset_value_numeric =
1331 attr_find_ex (&termset, NULL, &termset_value_string);
1332 if (termset_value_numeric != -1)
1335 const char *termset_name = 0;
1336 if (termset_value_numeric != -2)
1339 sprintf (resname, "%d", termset_value_numeric);
1340 termset_name = resname;
1343 termset_name = termset_value_string;
1344 logf (LOG_LOG, "creating termset set %s", termset_name);
1345 grep_info->termset = resultSetAdd (zh, termset_name, 1);
1346 if (!grep_info->termset)
1349 zh->errString = nmem_strdup (stream, termset_name);
1357 static RSET rpn_search_APT_phrase (ZebraHandle zh,
1358 Z_AttributesPlusTerm *zapt,
1359 const char *termz_org,
1360 oid_value attributeSet,
1362 int reg_type, int complete_flag,
1363 const char *rank_type, int xpath_use,
1364 int num_bases, char **basenames)
1366 char term_dst[IT_MAX_WORD+1];
1367 RSET rset[60], result;
1369 struct grep_info grep_info;
1370 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1371 const char *termp = termz;
1374 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1378 logf (LOG_DEBUG, "APT_phrase termp=%s", termp);
1379 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1381 reg_type, complete_flag,
1382 num_bases, basenames,
1383 term_dst, rank_type,
1387 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1390 grep_info_delete (&grep_info);
1393 rset_null_parms parms;
1395 parms.rset_term = rset_term_create (termz, -1, rank_type,
1397 return rset_create (rset_kind_null, &parms);
1399 else if (rset_no == 1)
1403 /* new / old prox */
1404 rset_prox_parms parms;
1407 parms.rset_no = rset_no;
1409 parms.exclusion = 0;
1412 parms.key_size = sizeof(struct it_key);
1413 parms.cmp = key_compare_it;
1414 parms.getseq = key_get_seq;
1415 parms.log_item = key_logdump_txt;
1416 result = rset_create(rset_kind_prox, &parms);
1421 static RSET rpn_search_APT_or_list (ZebraHandle zh,
1422 Z_AttributesPlusTerm *zapt,
1423 const char *termz_org,
1424 oid_value attributeSet,
1426 int reg_type, int complete_flag,
1427 const char *rank_type,
1429 int num_bases, char **basenames)
1431 char term_dst[IT_MAX_WORD+1];
1432 RSET rset[60], result;
1434 struct grep_info grep_info;
1435 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1436 const char *termp = termz;
1438 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1442 logf (LOG_DEBUG, "APT_or_list termp=%s", termp);
1443 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1445 reg_type, complete_flag,
1446 num_bases, basenames,
1447 term_dst, rank_type,
1451 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1454 grep_info_delete (&grep_info);
1457 rset_null_parms parms;
1459 parms.rset_term = rset_term_create (termz, -1, rank_type,
1461 return rset_create (rset_kind_null, &parms);
1464 for (i = 1; i<rset_no; i++)
1466 rset_bool_parms bool_parms;
1468 bool_parms.rset_l = result;
1469 bool_parms.rset_r = rset[i];
1470 bool_parms.key_size = sizeof(struct it_key);
1471 bool_parms.cmp = key_compare_it;
1472 bool_parms.log_item = key_logdump_txt;
1473 result = rset_create (rset_kind_or, &bool_parms);
1478 static RSET rpn_search_APT_and_list (ZebraHandle zh,
1479 Z_AttributesPlusTerm *zapt,
1480 const char *termz_org,
1481 oid_value attributeSet,
1483 int reg_type, int complete_flag,
1484 const char *rank_type,
1486 int num_bases, char **basenames)
1488 char term_dst[IT_MAX_WORD+1];
1489 RSET rset[60], result;
1491 struct grep_info grep_info;
1492 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1493 const char *termp = termz;
1495 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1499 logf (LOG_DEBUG, "APT_and_list termp=%s", termp);
1500 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1502 reg_type, complete_flag,
1503 num_bases, basenames,
1504 term_dst, rank_type,
1508 assert (rset[rset_no]);
1509 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1512 grep_info_delete (&grep_info);
1515 rset_null_parms parms;
1517 parms.rset_term = rset_term_create (termz, -1, rank_type,
1519 return rset_create (rset_kind_null, &parms);
1522 for (i = 1; i<rset_no; i++)
1524 rset_bool_parms bool_parms;
1526 bool_parms.rset_l = result;
1527 bool_parms.rset_r = rset[i];
1528 bool_parms.key_size = sizeof(struct it_key);
1529 bool_parms.cmp = key_compare_it;
1530 bool_parms.log_item = key_logdump_txt;
1531 result = rset_create (rset_kind_and, &bool_parms);
1536 static int numeric_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1537 const char **term_sub,
1539 oid_value attributeSet,
1540 struct grep_info *grep_info,
1549 char *term_tmp = term_dict + strlen(term_dict);
1551 attr_init (&relation, zapt, 2);
1552 relation_value = attr_find (&relation, NULL);
1554 logf (LOG_DEBUG, "numeric relation value=%d", relation_value);
1556 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub, term_tmp, 1,
1559 term_value = atoi (term_tmp);
1560 switch (relation_value)
1563 logf (LOG_DEBUG, "Relation <");
1564 gen_regular_rel (term_tmp, term_value-1, 1);
1567 logf (LOG_DEBUG, "Relation <=");
1568 gen_regular_rel (term_tmp, term_value, 1);
1571 logf (LOG_DEBUG, "Relation >=");
1572 gen_regular_rel (term_tmp, term_value, 0);
1575 logf (LOG_DEBUG, "Relation >");
1576 gen_regular_rel (term_tmp, term_value+1, 0);
1580 logf (LOG_DEBUG, "Relation =");
1581 sprintf (term_tmp, "(0*%d)", term_value);
1583 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_tmp);
1584 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info, max_pos,
1587 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1588 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1592 static int numeric_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1593 const char **term_sub,
1594 oid_value attributeSet, struct grep_info *grep_info,
1595 int reg_type, int complete_flag,
1596 int num_bases, char **basenames,
1597 char *term_dst, int xpath_use, NMEM stream)
1599 char term_dict[2*IT_MAX_WORD+2];
1603 const char *use_string = 0;
1604 oid_value curAttributeSet = attributeSet;
1606 struct rpn_char_map_info rcmi;
1608 int bases_ok = 0; /* no of databases with OK attribute */
1609 int errCode = 0; /* err code (if any is not OK) */
1610 char *errString = 0; /* addinfo */
1612 rpn_char_map_prepare (zh->reg, reg_type, &rcmi);
1613 attr_init (&use, zapt, 1);
1614 use_value = attr_find_ex (&use, &curAttributeSet, &use_string);
1616 if (use_value == -1)
1619 for (base_no = 0; base_no < num_bases; base_no++)
1622 data1_local_attribute id_xpath_attr;
1623 data1_local_attribute *local_attr;
1624 int max_pos, prefix_len = 0;
1627 if (use_value == -2) /* string attribute (assume IDXPATH/any) */
1629 use_value = xpath_use;
1630 attp.local_attributes = &id_xpath_attr;
1631 attp.attset_ordinal = VAL_IDXPATH;
1632 id_xpath_attr.next = 0;
1633 id_xpath_attr.local = use_value;
1635 else if (curAttributeSet == VAL_IDXPATH)
1637 attp.local_attributes = &id_xpath_attr;
1638 attp.attset_ordinal = VAL_IDXPATH;
1639 id_xpath_attr.next = 0;
1640 id_xpath_attr.local = use_value;
1644 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value,
1647 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1648 curAttributeSet, use_value, r);
1652 sprintf (val_str, "%d", use_value);
1653 errString = nmem_strdup (stream, val_str);
1661 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
1663 zh->errCode = 109; /* Database unavailable */
1664 zh->errString = basenames[base_no];
1667 for (local_attr = attp.local_attributes; local_attr;
1668 local_attr = local_attr->next)
1674 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
1679 term_dict[prefix_len++] = '|';
1681 term_dict[prefix_len++] = '(';
1683 ord_len = key_SU_encode (ord, ord_buf);
1684 for (i = 0; i<ord_len; i++)
1686 term_dict[prefix_len++] = 1;
1687 term_dict[prefix_len++] = ord_buf[i];
1693 sprintf (val_str, "%d", use_value);
1695 errString = nmem_strdup (stream, val_str);
1699 term_dict[prefix_len++] = ')';
1700 term_dict[prefix_len++] = 1;
1701 term_dict[prefix_len++] = reg_type;
1702 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1703 term_dict[prefix_len] = '\0';
1704 if (!numeric_relation (zh, zapt, &termp, term_dict,
1705 attributeSet, grep_info, &max_pos, reg_type,
1711 zh->errCode = errCode;
1712 zh->errString = errString;
1716 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1720 static RSET rpn_search_APT_numeric (ZebraHandle zh,
1721 Z_AttributesPlusTerm *zapt,
1723 oid_value attributeSet,
1725 int reg_type, int complete_flag,
1726 const char *rank_type, int xpath_use,
1727 int num_bases, char **basenames)
1729 char term_dst[IT_MAX_WORD+1];
1730 const char *termp = termz;
1731 RSET rset[60], result;
1732 int i, r, rset_no = 0;
1733 struct grep_info grep_info;
1735 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1739 logf (LOG_DEBUG, "APT_numeric termp=%s", termp);
1740 grep_info.isam_p_indx = 0;
1741 r = numeric_term (zh, zapt, &termp, attributeSet, &grep_info,
1742 reg_type, complete_flag, num_bases, basenames,
1743 term_dst, xpath_use,
1747 logf (LOG_DEBUG, "term: %s", term_dst);
1748 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1749 grep_info.isam_p_indx, term_dst,
1750 strlen(term_dst), rank_type,
1751 0 /* preserve position */,
1753 assert (rset[rset_no]);
1754 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1757 grep_info_delete (&grep_info);
1760 rset_null_parms parms;
1762 parms.rset_term = rset_term_create (term_dst, -1, rank_type,
1764 return rset_create (rset_kind_null, &parms);
1767 for (i = 1; i<rset_no; i++)
1769 rset_bool_parms bool_parms;
1771 bool_parms.rset_l = result;
1772 bool_parms.rset_r = rset[i];
1773 bool_parms.key_size = sizeof(struct it_key);
1774 bool_parms.cmp = key_compare_it;
1775 bool_parms.log_item = key_logdump_txt;
1776 result = rset_create (rset_kind_and, &bool_parms);
1781 static RSET rpn_search_APT_local (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1783 oid_value attributeSet,
1785 const char *rank_type)
1790 rset_temp_parms parms;
1792 parms.rset_term = rset_term_create (termz, -1, rank_type,
1794 parms.cmp = key_compare_it;
1795 parms.key_size = sizeof (struct it_key);
1796 parms.temp_path = res_get (zh->res, "setTmpDir");
1797 result = rset_create (rset_kind_temp, &parms);
1798 rsfd = rset_open (result, RSETF_WRITE);
1800 key.sysno = atoi (termz);
1804 rset_write (result, rsfd, &key);
1805 rset_close (result, rsfd);
1809 static RSET rpn_sort_spec (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1810 oid_value attributeSet, NMEM stream,
1811 Z_SortKeySpecList *sort_sequence,
1812 const char *rank_type)
1814 rset_null_parms parms;
1816 int sort_relation_value;
1817 AttrType sort_relation_type;
1822 Z_AttributeElement *ae;
1827 attr_init (&sort_relation_type, zapt, 7);
1828 sort_relation_value = attr_find (&sort_relation_type, &attributeSet);
1830 attr_init (&use_type, zapt, 1);
1831 use_value = attr_find (&use_type, &attributeSet);
1833 if (!sort_sequence->specs)
1835 sort_sequence->num_specs = 10;
1836 sort_sequence->specs = (Z_SortKeySpec **)
1837 nmem_malloc (stream, sort_sequence->num_specs *
1838 sizeof(*sort_sequence->specs));
1839 for (i = 0; i<sort_sequence->num_specs; i++)
1840 sort_sequence->specs[i] = 0;
1842 if (zapt->term->which != Z_Term_general)
1845 i = atoi_n ((char *) zapt->term->u.general->buf,
1846 zapt->term->u.general->len);
1847 if (i >= sort_sequence->num_specs)
1849 sprintf (termz, "%d", i);
1851 oe.proto = PROTO_Z3950;
1852 oe.oclass = CLASS_ATTSET;
1853 oe.value = attributeSet;
1854 if (!oid_ent_to_oid (&oe, oid))
1857 sks = (Z_SortKeySpec *) nmem_malloc (stream, sizeof(*sks));
1858 sks->sortElement = (Z_SortElement *)
1859 nmem_malloc (stream, sizeof(*sks->sortElement));
1860 sks->sortElement->which = Z_SortElement_generic;
1861 sk = sks->sortElement->u.generic = (Z_SortKey *)
1862 nmem_malloc (stream, sizeof(*sk));
1863 sk->which = Z_SortKey_sortAttributes;
1864 sk->u.sortAttributes = (Z_SortAttributes *)
1865 nmem_malloc (stream, sizeof(*sk->u.sortAttributes));
1867 sk->u.sortAttributes->id = oid;
1868 sk->u.sortAttributes->list = (Z_AttributeList *)
1869 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list));
1870 sk->u.sortAttributes->list->num_attributes = 1;
1871 sk->u.sortAttributes->list->attributes = (Z_AttributeElement **)
1872 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list->attributes));
1873 ae = *sk->u.sortAttributes->list->attributes = (Z_AttributeElement *)
1874 nmem_malloc (stream, sizeof(**sk->u.sortAttributes->list->attributes));
1875 ae->attributeSet = 0;
1876 ae->attributeType = (int *)
1877 nmem_malloc (stream, sizeof(*ae->attributeType));
1878 *ae->attributeType = 1;
1879 ae->which = Z_AttributeValue_numeric;
1880 ae->value.numeric = (int *)
1881 nmem_malloc (stream, sizeof(*ae->value.numeric));
1882 *ae->value.numeric = use_value;
1884 sks->sortRelation = (int *)
1885 nmem_malloc (stream, sizeof(*sks->sortRelation));
1886 if (sort_relation_value == 1)
1887 *sks->sortRelation = Z_SortKeySpec_ascending;
1888 else if (sort_relation_value == 2)
1889 *sks->sortRelation = Z_SortKeySpec_descending;
1891 *sks->sortRelation = Z_SortKeySpec_ascending;
1893 sks->caseSensitivity = (int *)
1894 nmem_malloc (stream, sizeof(*sks->caseSensitivity));
1895 *sks->caseSensitivity = 0;
1897 sks->which = Z_SortKeySpec_null;
1898 sks->u.null = odr_nullval ();
1899 sort_sequence->specs[i] = sks;
1901 parms.rset_term = rset_term_create (termz, -1, rank_type,
1903 return rset_create (rset_kind_null, &parms);
1907 static int parse_xpath(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1908 oid_value attributeSet,
1909 struct xpath_location_step *xpath, int max, NMEM mem)
1911 oid_value curAttributeSet = attributeSet;
1913 const char *use_string = 0;
1915 attr_init (&use, zapt, 1);
1916 attr_find_ex (&use, &curAttributeSet, &use_string);
1918 if (!use_string || *use_string != '/')
1921 return zebra_parse_xpath_str(use_string, xpath, max, mem);
1926 static RSET xpath_trunc(ZebraHandle zh, NMEM stream,
1927 int reg_type, const char *term, int use,
1928 oid_value curAttributeSet)
1931 struct grep_info grep_info;
1932 char term_dict[2048];
1935 int ord = zebraExplain_lookupSU (zh->reg->zei, curAttributeSet, use);
1936 int ord_len, i, r, max_pos;
1937 int term_type = Z_Term_characterString;
1938 const char *flags = "void";
1940 if (grep_info_prepare (zh, 0 /* zapt */, &grep_info, '0', stream))
1942 rset_null_parms parms;
1944 parms.rset_term = rset_term_create (term, strlen(term),
1946 parms.rset_term->nn = 0;
1947 return rset_create (rset_kind_null, &parms);
1952 rset_null_parms parms;
1954 parms.rset_term = rset_term_create (term, strlen(term),
1956 parms.rset_term->nn = 0;
1957 return rset_create (rset_kind_null, &parms);
1960 term_dict[prefix_len++] = '|';
1962 term_dict[prefix_len++] = '(';
1964 ord_len = key_SU_encode (ord, ord_buf);
1965 for (i = 0; i<ord_len; i++)
1967 term_dict[prefix_len++] = 1;
1968 term_dict[prefix_len++] = ord_buf[i];
1970 term_dict[prefix_len++] = ')';
1971 term_dict[prefix_len++] = 1;
1972 term_dict[prefix_len++] = reg_type;
1974 strcpy (term_dict+prefix_len, term);
1976 grep_info.isam_p_indx = 0;
1977 r = dict_lookup_grep (zh->reg->dict, term_dict, 0,
1978 &grep_info, &max_pos, 0, grep_handle);
1979 yaz_log (LOG_LOG, "%s %d positions", term,
1980 grep_info.isam_p_indx);
1981 rset = rset_trunc (zh, grep_info.isam_p_buf,
1982 grep_info.isam_p_indx, term, strlen(term),
1983 flags, 1, term_type);
1984 grep_info_delete (&grep_info);
1988 static RSET rpn_search_xpath (ZebraHandle zh,
1989 oid_value attributeSet,
1990 int num_bases, char **basenames,
1991 NMEM stream, const char *rank_type, RSET rset,
1992 int xpath_len, struct xpath_location_step *xpath)
1994 oid_value curAttributeSet = attributeSet;
2001 yaz_log (LOG_LOG, "len=%d", xpath_len);
2002 for (i = 0; i<xpath_len; i++)
2004 yaz_log (LOG_LOG, "XPATH %d %s", i, xpath[i].part);
2008 curAttributeSet = VAL_IDXPATH;
2018 a[@attr=value]/b[@other=othervalue]
2020 /e/@a val range(e/,range(@a,freetext(w,1015,val),@a),e/)
2021 /a/b val range(b/a/,freetext(w,1016,val),b/a/)
2022 /a/b/@c val range(b/a/,range(@c,freetext(w,1016,val),@c),b/a/)
2023 /a/b[@c=y] val range(b/a/,freetext(w,1016,val),b/a/,@c=y)
2024 /a[@c=y]/b val range(a/,range(b/a/,freetext(w,1016,val),b/a/),a/,@c=y)
2025 /a[@c=x]/b[@c=y] range(a/,range(b/a/,freetext(w,1016,val),b/a/,@c=y),a/,@c=x)
2029 dict_grep_cmap (zh->reg->dict, 0, 0);
2031 for (base_no = 0; base_no < num_bases; base_no++)
2033 int level = xpath_len;
2036 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
2038 zh->errCode = 109; /* Database unavailable */
2039 zh->errString = basenames[base_no];
2042 while (--level >= 0)
2044 char xpath_rev[128];
2046 rset_between_parms parms;
2047 RSET rset_start_tag = 0, rset_end_tag = 0, rset_attr = 0;
2051 for (i = level; i >= 1; --i)
2053 const char *cp = xpath[i].part;
2059 memcpy (xpath_rev + len, "[^/]*", 5);
2062 else if (*cp == ' ')
2065 xpath_rev[len++] = 1;
2066 xpath_rev[len++] = ' ';
2070 xpath_rev[len++] = *cp;
2071 xpath_rev[len++] = '/';
2073 else if (i == 1) /* // case */
2075 xpath_rev[len++] = '.';
2076 xpath_rev[len++] = '*';
2081 if (xpath[level].predicate &&
2082 xpath[level].predicate->which == XPATH_PREDICATE_RELATION &&
2083 xpath[level].predicate->u.relation.name[0])
2085 WRBUF wbuf = wrbuf_alloc();
2086 wrbuf_puts(wbuf, xpath[level].predicate->u.relation.name+1);
2087 if (xpath[level].predicate->u.relation.value)
2089 const char *cp = xpath[level].predicate->u.relation.value;
2090 wrbuf_putc(wbuf, '=');
2094 if (strchr(REGEX_CHARS, *cp))
2095 wrbuf_putc(wbuf, '\\');
2096 wrbuf_putc(wbuf, *cp);
2100 wrbuf_puts(wbuf, "");
2101 rset_attr = xpath_trunc (
2102 zh, stream, '0', wrbuf_buf(wbuf), 3, curAttributeSet);
2103 wrbuf_free(wbuf, 1);
2110 yaz_log (LOG_LOG, "xpath_rev (%d) = %s", level, xpath_rev);
2111 if (strlen(xpath_rev))
2113 rset_start_tag = xpath_trunc(zh, stream,
2114 '0', xpath_rev, 1, curAttributeSet);
2116 rset_end_tag = xpath_trunc(zh, stream,
2117 '0', xpath_rev, 2, curAttributeSet);
2119 parms.key_size = sizeof(struct it_key);
2120 parms.cmp = key_compare_it;
2121 parms.rset_l = rset_start_tag;
2122 parms.rset_m = rset;
2123 parms.rset_r = rset_end_tag;
2124 parms.rset_attr = rset_attr;
2125 parms.printer = key_print_it;
2126 rset = rset_create (rset_kind_between, &parms);
2137 static RSET rpn_search_APT (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2138 oid_value attributeSet, NMEM stream,
2139 Z_SortKeySpecList *sort_sequence,
2140 int num_bases, char **basenames)
2143 char *search_type = NULL;
2144 char rank_type[128];
2147 char termz[IT_MAX_WORD+1];
2151 struct xpath_location_step xpath[10];
2153 zebra_maps_attr (zh->reg->zebra_maps, zapt, ®_id, &search_type,
2154 rank_type, &complete_flag, &sort_flag);
2156 logf (LOG_DEBUG, "reg_id=%c", reg_id);
2157 logf (LOG_DEBUG, "complete_flag=%d", complete_flag);
2158 logf (LOG_DEBUG, "search_type=%s", search_type);
2159 logf (LOG_DEBUG, "rank_type=%s", rank_type);
2161 if (zapt_term_to_utf8(zh, zapt, termz))
2165 return rpn_sort_spec (zh, zapt, attributeSet, stream, sort_sequence,
2167 xpath_len = parse_xpath(zh, zapt, attributeSet, xpath, 10, stream);
2171 if (xpath[xpath_len-1].part[0] == '@')
2175 if (!strcmp (search_type, "phrase"))
2177 rset = rpn_search_APT_phrase (zh, zapt, termz, attributeSet, stream,
2178 reg_id, complete_flag, rank_type,
2180 num_bases, basenames);
2182 else if (!strcmp (search_type, "and-list"))
2184 rset = rpn_search_APT_and_list (zh, zapt, termz, attributeSet, stream,
2185 reg_id, complete_flag, rank_type,
2187 num_bases, basenames);
2189 else if (!strcmp (search_type, "or-list"))
2191 rset = rpn_search_APT_or_list (zh, zapt, termz, attributeSet, stream,
2192 reg_id, complete_flag, rank_type,
2194 num_bases, basenames);
2196 else if (!strcmp (search_type, "local"))
2198 rset = rpn_search_APT_local (zh, zapt, termz, attributeSet, stream,
2201 else if (!strcmp (search_type, "numeric"))
2203 rset = rpn_search_APT_numeric (zh, zapt, termz, attributeSet, stream,
2204 reg_id, complete_flag, rank_type,
2206 num_bases, basenames);
2208 else if (!strcmp (search_type, "always"))
2214 return rpn_search_xpath (zh, attributeSet, num_bases, basenames,
2215 stream, rank_type, rset, xpath_len, xpath);
2218 static RSET rpn_search_structure (ZebraHandle zh, Z_RPNStructure *zs,
2219 oid_value attributeSet, NMEM stream,
2220 Z_SortKeySpecList *sort_sequence,
2221 int num_bases, char **basenames)
2224 if (zs->which == Z_RPNStructure_complex)
2226 Z_Operator *zop = zs->u.complex->roperator;
2227 rset_bool_parms bool_parms;
2229 bool_parms.rset_l = rpn_search_structure (zh, zs->u.complex->s1,
2230 attributeSet, stream,
2232 num_bases, basenames);
2233 if (bool_parms.rset_l == NULL)
2235 bool_parms.rset_r = rpn_search_structure (zh, zs->u.complex->s2,
2236 attributeSet, stream,
2238 num_bases, basenames);
2239 if (bool_parms.rset_r == NULL)
2241 rset_delete (bool_parms.rset_l);
2244 bool_parms.key_size = sizeof(struct it_key);
2245 bool_parms.cmp = key_compare_it;
2246 bool_parms.log_item = key_logdump_txt;
2250 case Z_Operator_and:
2251 r = rset_create (rset_kind_and, &bool_parms);
2254 r = rset_create (rset_kind_or, &bool_parms);
2256 case Z_Operator_and_not:
2257 r = rset_create (rset_kind_not, &bool_parms);
2259 case Z_Operator_prox:
2260 if (zop->u.prox->which != Z_ProximityOperator_known)
2265 if (*zop->u.prox->u.known != Z_ProxUnit_word)
2267 char *val = (char *) nmem_malloc (stream, 16);
2269 zh->errString = val;
2270 sprintf (val, "%d", *zop->u.prox->u.known);
2275 /* new / old prox */
2276 rset_prox_parms parms;
2279 twosets[0] = bool_parms.rset_l;
2280 twosets[1] = bool_parms.rset_r;
2281 parms.rset = twosets;
2283 parms.ordered = *zop->u.prox->ordered;
2284 parms.exclusion = (!zop->u.prox->exclusion ? 0 :
2285 *zop->u.prox->exclusion);
2286 parms.relation = *zop->u.prox->relationType;
2287 parms.distance = *zop->u.prox->distance;
2288 parms.key_size = sizeof(struct it_key);
2289 parms.cmp = key_compare_it;
2290 parms.getseq = key_get_seq;
2291 parms.log_item = key_logdump_txt;
2292 r = rset_create(rset_kind_prox, &parms);
2300 else if (zs->which == Z_RPNStructure_simple)
2302 if (zs->u.simple->which == Z_Operand_APT)
2304 logf (LOG_DEBUG, "rpn_search_APT");
2305 r = rpn_search_APT (zh, zs->u.simple->u.attributesPlusTerm,
2306 attributeSet, stream, sort_sequence,
2307 num_bases, basenames);
2309 else if (zs->u.simple->which == Z_Operand_resultSetId)
2311 logf (LOG_DEBUG, "rpn_search_ref");
2312 r = resultSetRef (zh, zs->u.simple->u.resultSetId);
2315 r = rset_create (rset_kind_null, NULL);
2318 nmem_strdup (stream, zs->u.simple->u.resultSetId);
2339 RSET rpn_search (ZebraHandle zh, NMEM nmem,
2340 Z_RPNQuery *rpn, int num_bases, char **basenames,
2341 const char *setname,
2346 oid_value attributeSet;
2347 Z_SortKeySpecList *sort_sequence;
2351 zh->errString = NULL;
2354 sort_sequence = (Z_SortKeySpecList *)
2355 nmem_malloc (nmem, sizeof(*sort_sequence));
2356 sort_sequence->num_specs = 10;
2357 sort_sequence->specs = (Z_SortKeySpec **)
2358 nmem_malloc (nmem, sort_sequence->num_specs *
2359 sizeof(*sort_sequence->specs));
2360 for (i = 0; i<sort_sequence->num_specs; i++)
2361 sort_sequence->specs[i] = 0;
2363 attrset = oid_getentbyoid (rpn->attributeSetId);
2364 attributeSet = attrset->value;
2365 rset = rpn_search_structure (zh, rpn->RPNStructure, attributeSet,
2366 nmem, sort_sequence, num_bases, basenames);
2371 logf (LOG_DEBUG, "search error: %d", zh->errCode);
2373 for (i = 0; sort_sequence->specs[i]; i++)
2375 sort_sequence->num_specs = i;
2377 resultSetRank (zh, sset, rset);
2380 logf (LOG_DEBUG, "resultSetSortSingle in rpn_search");
2381 resultSetSortSingle (zh, nmem, sset, rset,
2382 sort_sequence, &sort_status);
2385 logf (LOG_DEBUG, "resultSetSortSingle status = %d", zh->errCode);
2391 struct scan_info_entry {
2397 struct scan_info_entry *list;
2403 static int scan_handle (char *name, const char *info, int pos, void *client)
2405 int len_prefix, idx;
2406 struct scan_info *scan_info = (struct scan_info *) client;
2408 len_prefix = strlen(scan_info->prefix);
2409 if (memcmp (name, scan_info->prefix, len_prefix))
2411 if (pos > 0) idx = scan_info->after - pos + scan_info->before;
2414 scan_info->list[idx].term = (char *)
2415 odr_malloc (scan_info->odr, strlen(name + len_prefix)+1);
2416 strcpy (scan_info->list[idx].term, name + len_prefix);
2417 assert (*info == sizeof(ISAMS_P));
2418 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAMS_P));
2422 static void scan_term_untrans (ZebraHandle zh, NMEM stream, int reg_type,
2423 char **dst, const char *src)
2425 char term_src[IT_MAX_WORD];
2426 char term_dst[IT_MAX_WORD];
2428 term_untrans (zh, reg_type, term_src, src);
2430 if (zh->iconv_from_utf8 != 0)
2433 char *inbuf = term_src;
2434 size_t inleft = strlen(term_src);
2435 char *outbuf = term_dst;
2436 size_t outleft = sizeof(term_dst)-1;
2439 ret = yaz_iconv (zh->iconv_from_utf8, &inbuf, &inleft,
2441 if (ret == (size_t)(-1))
2444 len = outbuf - term_dst;
2445 *dst = nmem_malloc (stream, len + 1);
2447 memcpy (*dst, term_dst, len);
2451 *dst = nmem_strdup (stream, term_src);
2454 static void count_set (RSET r, int *count)
2462 logf (LOG_DEBUG, "count_set");
2465 rfd = rset_open (r, RSETF_READ);
2466 while (rset_read (r, rfd, &key, &term_index))
2468 if (key.sysno != psysno)
2475 rset_close (r, rfd);
2476 logf (LOG_DEBUG, "%d keys, %d records", kno, *count);
2479 void rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
2480 oid_value attributeset,
2481 int num_bases, char **basenames,
2482 int *position, int *num_entries, ZebraScanEntry **list,
2483 int *is_partial, RSET limit_set, int return_zero)
2486 int pos = *position;
2487 int num = *num_entries;
2491 char termz[IT_MAX_WORD+20];
2494 const char *use_string = 0;
2495 struct scan_info *scan_info_array;
2496 ZebraScanEntry *glist;
2497 int ords[32], ord_no = 0;
2500 int bases_ok = 0; /* no of databases with OK attribute */
2501 int errCode = 0; /* err code (if any is not OK) */
2502 char *errString = 0; /* addinfo */
2505 char *search_type = NULL;
2506 char rank_type[128];
2512 if (attributeset == VAL_NONE)
2513 attributeset = VAL_BIB1;
2518 int termset_value_numeric;
2519 const char *termset_value_string;
2520 attr_init (&termset, zapt, 8);
2521 termset_value_numeric =
2522 attr_find_ex (&termset, NULL, &termset_value_string);
2523 if (termset_value_numeric != -1)
2526 const char *termset_name = 0;
2528 if (termset_value_numeric != -2)
2531 sprintf (resname, "%d", termset_value_numeric);
2532 termset_name = resname;
2535 termset_name = termset_value_string;
2537 limit_set = resultSetRef (zh, termset_name);
2541 yaz_log (LOG_DEBUG, "position = %d, num = %d set=%d",
2542 pos, num, attributeset);
2544 attr_init (&use, zapt, 1);
2545 use_value = attr_find_ex (&use, &attributeset, &use_string);
2547 if (zebra_maps_attr (zh->reg->zebra_maps, zapt, ®_id, &search_type,
2548 rank_type, &complete_flag, &sort_flag))
2554 yaz_log (LOG_DEBUG, "use_value = %d", use_value);
2556 if (use_value == -1)
2558 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
2562 data1_local_attribute *local_attr;
2564 if ((r=att_getentbyatt (zh, &attp, attributeset, use_value,
2567 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
2568 attributeset, use_value);
2572 sprintf (val_str, "%d", use_value);
2574 errString = odr_strdup (stream, val_str);
2580 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
2582 zh->errString = basenames[base_no];
2583 zh->errCode = 109; /* Database unavailable */
2588 for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
2589 local_attr = local_attr->next)
2593 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
2596 ords[ord_no++] = ord;
2599 if (!bases_ok && errCode)
2601 zh->errCode = errCode;
2602 zh->errString = errString;
2608 sprintf (val_str, "%d", use_value);
2610 zh->errString = odr_strdup (stream, val_str);
2615 /* prepare dictionary scanning */
2618 scan_info_array = (struct scan_info *)
2619 odr_malloc (stream, ord_no * sizeof(*scan_info_array));
2620 for (i = 0; i < ord_no; i++)
2622 int j, prefix_len = 0;
2623 int before_tmp = before, after_tmp = after;
2624 struct scan_info *scan_info = scan_info_array + i;
2625 struct rpn_char_map_info rcmi;
2627 rpn_char_map_prepare (zh->reg, reg_id, &rcmi);
2629 scan_info->before = before;
2630 scan_info->after = after;
2631 scan_info->odr = stream;
2633 scan_info->list = (struct scan_info_entry *)
2634 odr_malloc (stream, (before+after) * sizeof(*scan_info->list));
2635 for (j = 0; j<before+after; j++)
2636 scan_info->list[j].term = NULL;
2638 prefix_len += key_SU_encode (ords[i], termz + prefix_len);
2639 termz[prefix_len++] = reg_id;
2640 termz[prefix_len] = 0;
2641 strcpy (scan_info->prefix, termz);
2643 if (trans_scan_term (zh, zapt, termz+prefix_len, reg_id))
2646 dict_scan (zh->reg->dict, termz, &before_tmp, &after_tmp,
2647 scan_info, scan_handle);
2649 glist = (ZebraScanEntry *)
2650 odr_malloc (stream, (before+after)*sizeof(*glist));
2652 /* consider terms after main term */
2653 for (i = 0; i < ord_no; i++)
2657 for (i = 0; i<after; i++)
2660 const char *mterm = NULL;
2664 for (j = 0; j < ord_no; j++)
2666 if (ptr[j] < before+after &&
2667 (tst=scan_info_array[j].list[ptr[j]].term) &&
2668 (!mterm || strcmp (tst, mterm) < 0))
2676 scan_term_untrans (zh, stream->mem, reg_id,
2677 &glist[i+before].term, mterm);
2678 rset = rset_trunc (zh, &scan_info_array[j0].list[ptr[j0]].isam_p, 1,
2679 glist[i+before].term, strlen(glist[i+before].term),
2680 NULL, 0, zapt->term->which);
2683 for (j = j0+1; j<ord_no; j++)
2685 if (ptr[j] < before+after &&
2686 (tst=scan_info_array[j].list[ptr[j]].term) &&
2687 !strcmp (tst, mterm))
2689 rset_bool_parms bool_parms;
2693 rset_trunc (zh, &scan_info_array[j].list[ptr[j]].isam_p, 1,
2694 glist[i+before].term,
2695 strlen(glist[i+before].term), NULL, 0,
2698 bool_parms.key_size = sizeof(struct it_key);
2699 bool_parms.cmp = key_compare_it;
2700 bool_parms.log_item = key_logdump_txt;
2701 bool_parms.rset_l = rset;
2702 bool_parms.rset_r = rset2;
2704 rset = rset_create (rset_kind_or, &bool_parms);
2711 rset_bool_parms bool_parms;
2713 bool_parms.key_size = sizeof(struct it_key);
2714 bool_parms.cmp = key_compare_it;
2715 bool_parms.log_item = key_logdump_txt;
2716 bool_parms.rset_l = rset;
2717 bool_parms.rset_r = rset_dup(limit_set);
2719 rset = rset_create (rset_kind_and, &bool_parms);
2721 count_set (rset, &glist[i+before].occurrences);
2726 *num_entries -= (after-i);
2730 /* consider terms before main term */
2731 for (i = 0; i<ord_no; i++)
2734 for (i = 0; i<before; i++)
2737 const char *mterm = NULL;
2741 for (j = 0; j <ord_no; j++)
2743 if (ptr[j] < before &&
2744 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2745 (!mterm || strcmp (tst, mterm) > 0))
2754 scan_term_untrans (zh, stream->mem, reg_id,
2755 &glist[before-1-i].term, mterm);
2758 (zh, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1,
2759 glist[before-1-i].term, strlen(glist[before-1-i].term),
2760 NULL, 0, zapt->term->which);
2764 for (j = j0+1; j<ord_no; j++)
2766 if (ptr[j] < before &&
2767 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2768 !strcmp (tst, mterm))
2770 rset_bool_parms bool_parms;
2773 rset2 = rset_trunc (zh,
2774 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1,
2775 glist[before-1-i].term,
2776 strlen(glist[before-1-i].term), NULL, 0,
2779 bool_parms.key_size = sizeof(struct it_key);
2780 bool_parms.cmp = key_compare_it;
2781 bool_parms.log_item = key_logdump_txt;
2782 bool_parms.rset_l = rset;
2783 bool_parms.rset_r = rset2;
2785 rset = rset_create (rset_kind_or, &bool_parms);
2792 rset_bool_parms bool_parms;
2794 bool_parms.key_size = sizeof(struct it_key);
2795 bool_parms.cmp = key_compare_it;
2796 bool_parms.log_item = key_logdump_txt;
2797 bool_parms.rset_l = rset;
2798 bool_parms.rset_r = rset_dup(limit_set);
2800 rset = rset_create (rset_kind_and, &bool_parms);
2802 count_set (rset, &glist[before-1-i].occurrences);
2812 *list = glist + i; /* list is set to first 'real' entry */
2814 logf (LOG_DEBUG, "position = %d, num_entries = %d",
2815 *position, *num_entries);
2817 logf (LOG_DEBUG, "scan error: %d", zh->errCode);