1 /* $Id: zrpn.c,v 1.149 2004-09-01 15:01:32 heikki Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
34 #include <zebra_xpath.h>
40 static const struct key_control it_ctrl={
41 sizeof(struct it_key),
43 key_logdump_txt, /* FIXME - clean up these functions */
47 const struct key_control *key_it_ctrl=&it_ctrl;
49 struct rpn_char_map_info {
58 Z_AttributesPlusTerm *zapt;
62 static const char **rpn_char_map_handler (void *vp, const char **from, int len)
64 struct rpn_char_map_info *p = (struct rpn_char_map_info *) vp;
65 const char **out = zebra_maps_input (p->zm, p->reg_type, from, len);
69 const char *outp = *out;
70 yaz_log (LOG_LOG, "---");
73 yaz_log (LOG_LOG, "%02X", *outp);
81 static void rpn_char_map_prepare (struct zebra_register *reg, int reg_type,
82 struct rpn_char_map_info *map_info)
84 map_info->zm = reg->zebra_maps;
85 map_info->reg_type = reg_type;
86 dict_grep_cmap (reg->dict, map_info, rpn_char_map_handler);
89 static int attr_find_ex (AttrType *src, oid_value *attributeSetP,
90 const char **string_value)
94 num_attributes = src->zapt->attributes->num_attributes;
95 while (src->major < num_attributes)
97 Z_AttributeElement *element;
99 element = src->zapt->attributes->attributes[src->major];
100 if (src->type == *element->attributeType)
102 switch (element->which)
104 case Z_AttributeValue_numeric:
106 if (element->attributeSet && attributeSetP)
110 attrset = oid_getentbyoid (element->attributeSet);
111 *attributeSetP = attrset->value;
113 return *element->value.numeric;
115 case Z_AttributeValue_complex:
116 if (src->minor >= element->value.complex->num_list)
118 if (element->attributeSet && attributeSetP)
122 attrset = oid_getentbyoid (element->attributeSet);
123 *attributeSetP = attrset->value;
125 if (element->value.complex->list[src->minor]->which ==
126 Z_StringOrNumeric_numeric)
130 *element->value.complex->list[src->minor-1]->u.numeric;
132 else if (element->value.complex->list[src->minor]->which ==
133 Z_StringOrNumeric_string)
139 element->value.complex->list[src->minor-1]->u.string;
153 static int attr_find (AttrType *src, oid_value *attributeSetP)
155 return attr_find_ex (src, attributeSetP, 0);
158 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
181 static void term_untrans (ZebraHandle zh, int reg_type,
182 char *dst, const char *src)
187 const char *cp = zebra_maps_output (zh->reg->zebra_maps,
189 if (!cp && len < IT_MAX_WORD-1)
192 while (*cp && len < IT_MAX_WORD-1)
198 static void add_isam_p (const char *name, const char *info,
201 if (p->isam_p_indx == p->isam_p_size)
203 ISAMC_P *new_isam_p_buf;
207 p->isam_p_size = 2*p->isam_p_size + 100;
208 new_isam_p_buf = (ISAMC_P *) xmalloc (sizeof(*new_isam_p_buf) *
212 memcpy (new_isam_p_buf, p->isam_p_buf,
213 p->isam_p_indx * sizeof(*p->isam_p_buf));
214 xfree (p->isam_p_buf);
216 p->isam_p_buf = new_isam_p_buf;
219 new_term_no = (int *) xmalloc (sizeof(*new_term_no) *
223 memcpy (new_term_no, p->isam_p_buf,
224 p->isam_p_indx * sizeof(*p->term_no));
227 p->term_no = new_term_no;
230 assert (*info == sizeof(*p->isam_p_buf));
231 memcpy (p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
238 char term_tmp[IT_MAX_WORD];
240 int len = key_SU_decode (&su_code, name);
242 term_untrans (p->zh, p->reg_type, term_tmp, name+len+1);
243 logf (LOG_LOG, "grep: %d %c %s", su_code, name[len], term_tmp);
244 zebraExplain_lookup_ord (p->zh->reg->zei,
245 su_code, &db, &set, &use);
246 logf (LOG_LOG, "grep: set=%d use=%d db=%s", set, use, db);
248 resultSetAddTerm (p->zh, p->termset, name[len], db,
255 static int grep_handle (char *name, const char *info, void *p)
257 add_isam_p (name, info, (struct grep_info *) p);
261 static int term_pre (ZebraMaps zebra_maps, int reg_type, const char **src,
262 const char *ct1, const char *ct2)
264 const char *s1, *s0 = *src;
267 /* skip white space */
270 if (ct1 && strchr (ct1, *s0))
272 if (ct2 && strchr (ct2, *s0))
275 map = zebra_maps_input (zebra_maps, reg_type, &s1, strlen(s1));
276 if (**map != *CHR_SPACE)
284 #define REGEX_CHARS " []()|.*+?!"
286 /* term_100: handle term, where trunc=none (no operators at all) */
287 static int term_100 (ZebraMaps zebra_maps, int reg_type,
288 const char **src, char *dst, int space_split,
296 const char *space_start = 0;
297 const char *space_end = 0;
299 if (!term_pre (zebra_maps, reg_type, src, NULL, NULL))
305 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
308 if (**map == *CHR_SPACE)
311 else /* complete subfield only. */
313 if (**map == *CHR_SPACE)
314 { /* save space mapping for later .. */
319 else if (space_start)
320 { /* reload last space */
321 while (space_start < space_end)
323 if (strchr (REGEX_CHARS, *space_start))
325 dst_term[j++] = *space_start;
326 dst[i++] = *space_start++;
329 space_start = space_end = 0;
332 /* add non-space char */
335 if (strchr(REGEX_CHARS, *s1))
347 /* term_101: handle term, where trunc=Process # */
348 static int term_101 (ZebraMaps zebra_maps, int reg_type,
349 const char **src, char *dst, int space_split,
357 if (!term_pre (zebra_maps, reg_type, src, "#", "#"))
366 dst_term[j++] = *s0++;
371 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
372 if (space_split && **map == *CHR_SPACE)
376 if (strchr(REGEX_CHARS, *s1))
384 dst_term[j++] = '\0';
389 /* term_103: handle term, where trunc=re-2 (regular expressions) */
390 static int term_103 (ZebraMaps zebra_maps, int reg_type, const char **src,
391 char *dst, int *errors, int space_split,
399 if (!term_pre (zebra_maps, reg_type, src, "^\\()[].*+?|", "("))
402 if (errors && *s0 == '+' && s0[1] && s0[2] == '+' && s0[3] &&
405 *errors = s0[1] - '0';
412 if (strchr ("^\\()[].*+?|-", *s0))
420 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
421 if (**map == *CHR_SPACE)
425 if (strchr(REGEX_CHARS, *s1))
438 /* term_103: handle term, where trunc=re-1 (regular expressions) */
439 static int term_102 (ZebraMaps zebra_maps, int reg_type, const char **src,
440 char *dst, int space_split, char *dst_term)
442 return term_103 (zebra_maps, reg_type, src, dst, NULL, space_split,
447 /* term_104: handle term, where trunc=Process # and ! */
448 static int term_104 (ZebraMaps zebra_maps, int reg_type,
449 const char **src, char *dst, int space_split,
457 if (!term_pre (zebra_maps, reg_type, src, "?*#", "?*#"))
464 dst_term[j++] = *s0++;
465 if (*s0 >= '0' && *s0 <= '9')
468 while (*s0 >= '0' && *s0 <= '9')
470 limit = limit * 10 + (*s0 - '0');
471 dst_term[j++] = *s0++;
491 dst_term[j++] = *s0++;
496 dst_term[j++] = *s0++;
500 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
501 if (space_split && **map == *CHR_SPACE)
505 if (strchr(REGEX_CHARS, *s1))
513 dst_term[j++] = '\0';
518 /* term_105/106: handle term, where trunc=Process * and ! and right trunc */
519 static int term_105 (ZebraMaps zebra_maps, int reg_type,
520 const char **src, char *dst, int space_split,
521 char *dst_term, int right_truncate)
528 if (!term_pre (zebra_maps, reg_type, src, "*!", "*!"))
537 dst_term[j++] = *s0++;
542 dst_term[j++] = *s0++;
546 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
547 if (space_split && **map == *CHR_SPACE)
551 if (strchr(REGEX_CHARS, *s1))
565 dst_term[j++] = '\0';
571 /* gen_regular_rel - generate regular expression from relation
572 * val: border value (inclusive)
573 * islt: 1 if <=; 0 if >=.
575 static void gen_regular_rel (char *dst, int val, int islt)
582 logf (LOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
586 strcpy (dst, "(-[0-9]+|(");
594 strcpy (dst, "([0-9]+|-(");
595 dst_p = strlen (dst);
605 dst_p = strlen (dst);
606 sprintf (numstr, "%d", val);
607 for (w = strlen(numstr); --w >= 0; pos++)
626 strcpy (dst + dst_p, numstr);
627 dst_p = strlen(dst) - pos - 1;
655 for (i = 0; i<pos; i++)
668 /* match everything less than 10^(pos-1) */
670 for (i=1; i<pos; i++)
671 strcat (dst, "[0-9]?");
675 /* match everything greater than 10^pos */
676 for (i = 0; i <= pos; i++)
677 strcat (dst, "[0-9]");
678 strcat (dst, "[0-9]*");
683 void string_rel_add_char (char **term_p, const char *src, int *indx)
685 if (src[*indx] == '\\')
686 *(*term_p)++ = src[(*indx)++];
687 *(*term_p)++ = src[(*indx)++];
691 * > abc ([b-].*|a[c-].*|ab[d-].*|abc.+)
692 * ([^-a].*|a[^-b].*ab[^-c].*|abc.+)
693 * >= abc ([b-].*|a[c-].*|ab[c-].*)
694 * ([^-a].*|a[^-b].*|ab[c-].*)
695 * < abc ([-0].*|a[-a].*|ab[-b].*)
696 * ([^a-].*|a[^b-].*|ab[^c-].*)
697 * <= abc ([-0].*|a[-a].*|ab[-b].*|abc)
698 * ([^a-].*|a[^b-].*|ab[^c-].*|abc)
700 static int string_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
701 const char **term_sub, char *term_dict,
702 oid_value attributeSet,
703 int reg_type, int space_split, char *term_dst)
708 char *term_tmp = term_dict + strlen(term_dict);
709 char term_component[2*IT_MAX_WORD+20];
711 attr_init (&relation, zapt, 2);
712 relation_value = attr_find (&relation, NULL);
714 logf (LOG_DEBUG, "string relation value=%d", relation_value);
715 switch (relation_value)
718 if (!term_100 (zh->reg->zebra_maps, reg_type,
719 term_sub, term_component,
720 space_split, term_dst))
722 logf (LOG_DEBUG, "Relation <");
725 for (i = 0; term_component[i]; )
732 string_rel_add_char (&term_tmp, term_component, &j);
737 string_rel_add_char (&term_tmp, term_component, &i);
744 if ((term_tmp - term_dict) > IT_MAX_WORD)
751 if (!term_100 (zh->reg->zebra_maps, reg_type,
752 term_sub, term_component,
753 space_split, term_dst))
755 logf (LOG_DEBUG, "Relation <=");
758 for (i = 0; term_component[i]; )
763 string_rel_add_char (&term_tmp, term_component, &j);
767 string_rel_add_char (&term_tmp, term_component, &i);
776 if ((term_tmp - term_dict) > IT_MAX_WORD)
779 for (i = 0; term_component[i]; )
780 string_rel_add_char (&term_tmp, term_component, &i);
785 if (!term_100 (zh->reg->zebra_maps, reg_type,
786 term_sub, term_component, space_split, term_dst))
788 logf (LOG_DEBUG, "Relation >");
791 for (i = 0; term_component[i];)
796 string_rel_add_char (&term_tmp, term_component, &j);
801 string_rel_add_char (&term_tmp, term_component, &i);
809 if ((term_tmp - term_dict) > IT_MAX_WORD)
812 for (i = 0; term_component[i];)
813 string_rel_add_char (&term_tmp, term_component, &i);
820 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub,
821 term_component, space_split, term_dst))
823 logf (LOG_DEBUG, "Relation >=");
826 for (i = 0; term_component[i];)
833 string_rel_add_char (&term_tmp, term_component, &j);
836 if (term_component[i+1])
840 string_rel_add_char (&term_tmp, term_component, &i);
844 string_rel_add_char (&term_tmp, term_component, &i);
851 if ((term_tmp - term_dict) > IT_MAX_WORD)
859 logf (LOG_DEBUG, "Relation =");
860 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub,
861 term_component, space_split, term_dst))
863 strcat (term_tmp, "(");
864 strcat (term_tmp, term_component);
865 strcat (term_tmp, ")");
870 static int string_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
871 const char **term_sub,
872 oid_value attributeSet, NMEM stream,
873 struct grep_info *grep_info,
874 int reg_type, int complete_flag,
875 int num_bases, char **basenames,
876 char *term_dst, int xpath_use);
878 static RSET term_trunc (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
879 const char **term_sub,
880 oid_value attributeSet, NMEM stream,
881 struct grep_info *grep_info,
882 int reg_type, int complete_flag,
883 int num_bases, char **basenames,
885 const char *rank_type, int xpath_use,
889 grep_info->isam_p_indx = 0;
890 r = string_term (zh, zapt, term_sub, attributeSet, stream, grep_info,
891 reg_type, complete_flag, num_bases, basenames,
892 term_dst, xpath_use);
895 logf (LOG_DEBUG, "term: %s", term_dst);
896 return rset_trunc (zh, grep_info->isam_p_buf,
897 grep_info->isam_p_indx, term_dst,
898 strlen(term_dst), rank_type, 1 /* preserve pos */,
899 zapt->term->which, rset_nmem,key_it_ctrl);
903 static int string_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
904 const char **term_sub,
905 oid_value attributeSet, NMEM stream,
906 struct grep_info *grep_info,
907 int reg_type, int complete_flag,
908 int num_bases, char **basenames,
909 char *term_dst, int xpath_use)
911 char term_dict[2*IT_MAX_WORD+4000];
914 int truncation_value;
917 const char *use_string = 0;
918 oid_value curAttributeSet = attributeSet;
920 struct rpn_char_map_info rcmi;
921 int space_split = complete_flag ? 0 : 1;
923 int bases_ok = 0; /* no of databases with OK attribute */
924 int errCode = 0; /* err code (if any is not OK) */
925 char *errString = 0; /* addinfo */
927 rpn_char_map_prepare (zh->reg, reg_type, &rcmi);
928 attr_init (&use, zapt, 1);
929 use_value = attr_find_ex (&use, &curAttributeSet, &use_string);
930 logf (LOG_DEBUG, "string_term, use value %d", use_value);
931 attr_init (&truncation, zapt, 5);
932 truncation_value = attr_find (&truncation, NULL);
933 logf (LOG_DEBUG, "truncation value %d", truncation_value);
935 if (use_value == -1) /* no attribute - assumy "any" */
937 for (base_no = 0; base_no < num_bases; base_no++)
940 data1_local_attribute id_xpath_attr;
941 data1_local_attribute *local_attr;
942 int max_pos, prefix_len = 0;
946 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
948 zh->errCode = 109; /* Database unavailable */
949 zh->errString = basenames[base_no];
952 if (xpath_use > 0 && use_value == -2)
954 use_value = xpath_use;
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;
960 else if (curAttributeSet == VAL_IDXPATH)
962 attp.local_attributes = &id_xpath_attr;
963 attp.attset_ordinal = VAL_IDXPATH;
964 id_xpath_attr.next = 0;
965 id_xpath_attr.local = use_value;
969 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value,
972 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
973 curAttributeSet, use_value, r);
976 /* set was found, but value wasn't defined */
979 errString = nmem_strdup(stream, use_string);
983 sprintf (val_str, "%d", use_value);
984 errString = nmem_strdup (stream, val_str);
990 struct oident oident;
992 oident.proto = PROTO_Z3950;
993 oident.oclass = CLASS_ATTSET;
994 oident.value = curAttributeSet;
995 oid_ent_to_oid (&oident, oid);
998 errString = nmem_strdup (stream, oident.desc);
1003 for (local_attr = attp.local_attributes; local_attr;
1004 local_attr = local_attr->next)
1010 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
1015 term_dict[prefix_len++] = '|';
1017 term_dict[prefix_len++] = '(';
1019 ord_len = key_SU_encode (ord, ord_buf);
1020 for (i = 0; i<ord_len; i++)
1022 term_dict[prefix_len++] = 1;
1023 term_dict[prefix_len++] = ord_buf[i];
1032 sprintf (val_str, "%d", use_value);
1034 errString = nmem_strdup (stream, val_str);
1038 bases_ok++; /* this has OK attributes */
1040 term_dict[prefix_len++] = ')';
1041 term_dict[prefix_len++] = 1;
1042 term_dict[prefix_len++] = reg_type;
1043 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1044 term_dict[prefix_len] = '\0';
1046 switch (truncation_value)
1048 case -1: /* not specified */
1049 case 100: /* do not truncate */
1050 if (!string_relation (zh, zapt, &termp, term_dict,
1052 reg_type, space_split, term_dst))
1054 logf (LOG_LOG, "dict_lookup_grep: %s", term_dict+prefix_len);
1055 r = dict_lookup_grep (zh->reg->dict, term_dict, 0,
1056 grep_info, &max_pos, 0, grep_handle);
1058 logf (LOG_WARN, "dict_lookup_grep fail %d", r);
1060 case 1: /* right truncation */
1061 term_dict[j++] = '(';
1062 if (!term_100 (zh->reg->zebra_maps, reg_type,
1063 &termp, term_dict + j, space_split, term_dst))
1065 strcat (term_dict, ".*)");
1066 dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1067 &max_pos, 0, grep_handle);
1069 case 2: /* keft truncation */
1070 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1071 if (!term_100 (zh->reg->zebra_maps, reg_type,
1072 &termp, term_dict + j, space_split, term_dst))
1074 strcat (term_dict, ")");
1075 dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1076 &max_pos, 0, grep_handle);
1078 case 3: /* left&right truncation */
1079 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1080 if (!term_100 (zh->reg->zebra_maps, reg_type,
1081 &termp, term_dict + j, space_split, term_dst))
1083 strcat (term_dict, ".*)");
1084 dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1085 &max_pos, 0, grep_handle);
1089 case 101: /* process # in term */
1090 term_dict[j++] = '(';
1091 if (!term_101 (zh->reg->zebra_maps, reg_type,
1092 &termp, term_dict + j, space_split, term_dst))
1094 strcat (term_dict, ")");
1095 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1096 &max_pos, 0, grep_handle);
1098 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d", r);
1100 case 102: /* Regexp-1 */
1101 term_dict[j++] = '(';
1102 if (!term_102 (zh->reg->zebra_maps, reg_type,
1103 &termp, term_dict + j, space_split, term_dst))
1105 strcat (term_dict, ")");
1106 logf (LOG_DEBUG, "Regexp-1 tolerance=%d", r);
1107 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1108 &max_pos, 0, grep_handle);
1110 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
1113 case 103: /* Regexp-2 */
1115 term_dict[j++] = '(';
1116 if (!term_103 (zh->reg->zebra_maps, reg_type,
1117 &termp, term_dict + j, &r, space_split, term_dst))
1119 strcat (term_dict, ")");
1120 logf (LOG_DEBUG, "Regexp-2 tolerance=%d", r);
1121 r = dict_lookup_grep (zh->reg->dict, term_dict, r, grep_info,
1122 &max_pos, 2, grep_handle);
1124 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
1127 case 104: /* process # and ! in term */
1128 term_dict[j++] = '(';
1129 if (!term_104 (zh->reg->zebra_maps, reg_type,
1130 &termp, term_dict + j, space_split, term_dst))
1132 strcat (term_dict, ")");
1133 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1134 &max_pos, 0, grep_handle);
1136 logf (LOG_WARN, "dict_lookup_grep err, trunc=#/!: %d", r);
1138 case 105: /* process * and ! in term */
1139 term_dict[j++] = '(';
1140 if (!term_105 (zh->reg->zebra_maps, reg_type,
1141 &termp, term_dict + j, space_split, term_dst, 1))
1143 strcat (term_dict, ")");
1144 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1145 &max_pos, 0, grep_handle);
1147 logf (LOG_WARN, "dict_lookup_grep err, trunc=*/!: %d", r);
1149 case 106: /* process * and ! in term */
1150 term_dict[j++] = '(';
1151 if (!term_105 (zh->reg->zebra_maps, reg_type,
1152 &termp, term_dict + j, space_split, term_dst, 0))
1154 strcat (term_dict, ")");
1155 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1156 &max_pos, 0, grep_handle);
1158 logf (LOG_WARN, "dict_lookup_grep err, trunc=*/!: %d", r);
1164 zh->errCode = errCode;
1165 zh->errString = errString;
1169 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1174 /* convert APT search term to UTF8 */
1175 static int zapt_term_to_utf8 (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1179 Z_Term *term = zapt->term;
1181 switch (term->which)
1183 case Z_Term_general:
1184 if (zh->iconv_to_utf8 != 0)
1186 char *inbuf = term->u.general->buf;
1187 size_t inleft = term->u.general->len;
1188 char *outbuf = termz;
1189 size_t outleft = IT_MAX_WORD-1;
1192 ret = yaz_iconv(zh->iconv_to_utf8, &inbuf, &inleft,
1194 if (ret == (size_t)(-1))
1196 ret = yaz_iconv(zh->iconv_to_utf8, 0, 0, 0, 0);
1204 sizez = term->u.general->len;
1205 if (sizez > IT_MAX_WORD-1)
1206 sizez = IT_MAX_WORD-1;
1207 memcpy (termz, term->u.general->buf, sizez);
1208 termz[sizez] = '\0';
1211 case Z_Term_characterString:
1212 sizez = strlen(term->u.characterString);
1213 if (sizez > IT_MAX_WORD-1)
1214 sizez = IT_MAX_WORD-1;
1215 memcpy (termz, term->u.characterString, sizez);
1216 termz[sizez] = '\0';
1225 /* convert APT SCAN term to internal cmap */
1226 static int trans_scan_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1227 char *termz, int reg_type)
1229 char termz0[IT_MAX_WORD];
1231 if (zapt_term_to_utf8(zh, zapt, termz0))
1232 return -1; /* error */
1236 const char *cp = (const char *) termz0;
1237 const char *cp_end = cp + strlen(cp);
1240 const char *space_map = NULL;
1243 while ((len = (cp_end - cp)) > 0)
1245 map = zebra_maps_input (zh->reg->zebra_maps, reg_type, &cp, len);
1246 if (**map == *CHR_SPACE)
1251 for (src = space_map; *src; src++)
1254 for (src = *map; *src; src++)
1263 char *normalize_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1264 const char *termz, NMEM stream, unsigned reg_id)
1267 AttrType truncation;
1268 int truncation_value;
1271 attr_init (&truncation, zapt, 5);
1272 truncation_value = attr_find (&truncation, NULL);
1274 switch (truncation_value)
1294 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, ex_list,
1295 termz, strlen(termz));
1297 return nmem_strdup(stream, termz);
1300 char *buf = (char*) nmem_malloc (stream, wrbuf_len(wrbuf)+1);
1301 memcpy (buf, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1302 buf[wrbuf_len(wrbuf)] = '\0';
1307 static void grep_info_delete (struct grep_info *grep_info)
1310 xfree(grep_info->term_no);
1312 xfree (grep_info->isam_p_buf);
1315 static int grep_info_prepare (ZebraHandle zh,
1316 Z_AttributesPlusTerm *zapt,
1317 struct grep_info *grep_info,
1322 int termset_value_numeric;
1323 const char *termset_value_string;
1326 grep_info->term_no = 0;
1328 grep_info->isam_p_size = 0;
1329 grep_info->isam_p_buf = NULL;
1331 grep_info->reg_type = reg_type;
1332 grep_info->termset = 0;
1336 attr_init (&termset, zapt, 8);
1337 termset_value_numeric =
1338 attr_find_ex (&termset, NULL, &termset_value_string);
1339 if (termset_value_numeric != -1)
1342 const char *termset_name = 0;
1343 if (termset_value_numeric != -2)
1346 sprintf (resname, "%d", termset_value_numeric);
1347 termset_name = resname;
1350 termset_name = termset_value_string;
1351 logf (LOG_LOG, "creating termset set %s", termset_name);
1352 grep_info->termset = resultSetAdd (zh, termset_name, 1);
1353 if (!grep_info->termset)
1356 zh->errString = nmem_strdup (stream, termset_name);
1364 static RSET rpn_search_APT_phrase (ZebraHandle zh,
1365 Z_AttributesPlusTerm *zapt,
1366 const char *termz_org,
1367 oid_value attributeSet,
1369 int reg_type, int complete_flag,
1370 const char *rank_type, int xpath_use,
1371 int num_bases, char **basenames,
1374 char term_dst[IT_MAX_WORD+1];
1375 RSET rset[60], result;
1377 struct grep_info grep_info;
1378 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1379 const char *termp = termz;
1382 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1386 logf (LOG_DEBUG, "APT_phrase termp=%s", termp);
1387 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1389 reg_type, complete_flag,
1390 num_bases, basenames,
1391 term_dst, rank_type,
1392 xpath_use,rset_nmem);
1395 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1398 grep_info_delete (&grep_info);
1400 return rsnull_create (rset_nmem,key_it_ctrl);
1401 else if (rset_no == 1)
1404 result = rsprox_create( rset_nmem, key_it_ctrl,
1406 1 /* ordered */, 0 /* exclusion */,
1407 3 /* relation */, 1 /* distance */);
1411 static RSET rpn_search_APT_or_list (ZebraHandle zh,
1412 Z_AttributesPlusTerm *zapt,
1413 const char *termz_org,
1414 oid_value attributeSet,
1416 int reg_type, int complete_flag,
1417 const char *rank_type,
1419 int num_bases, char **basenames,
1422 char term_dst[IT_MAX_WORD+1];
1425 struct grep_info grep_info;
1426 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1427 const char *termp = termz;
1429 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1433 logf (LOG_DEBUG, "APT_or_list termp=%s", termp);
1434 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1436 reg_type, complete_flag,
1437 num_bases, basenames,
1438 term_dst, rank_type,
1439 xpath_use,rset_nmem);
1442 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1445 grep_info_delete (&grep_info);
1447 return rsnull_create (rset_nmem,key_it_ctrl);
1448 return rsmultior_create(rset_nmem, key_it_ctrl,
1452 static RSET rpn_search_APT_and_list (ZebraHandle zh,
1453 Z_AttributesPlusTerm *zapt,
1454 const char *termz_org,
1455 oid_value attributeSet,
1457 int reg_type, int complete_flag,
1458 const char *rank_type,
1460 int num_bases, char **basenames,
1463 char term_dst[IT_MAX_WORD+1];
1464 RSET rset[60], result;
1466 struct grep_info grep_info;
1467 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1468 const char *termp = termz;
1470 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1474 logf (LOG_DEBUG, "APT_and_list termp=%s", termp);
1475 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1477 reg_type, complete_flag,
1478 num_bases, basenames,
1479 term_dst, rank_type,
1480 xpath_use, rset_nmem);
1483 assert (rset[rset_no]);
1484 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1487 grep_info_delete (&grep_info);
1489 return rsnull_create (rset_nmem,key_it_ctrl);
1491 /* FIXME - Use a proper rsmultiand */
1492 for (i = 1; i<rset_no; i++)
1493 result= rsbool_create_and(rset_nmem,key_it_ctrl,
1498 static int numeric_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1499 const char **term_sub,
1501 oid_value attributeSet,
1502 struct grep_info *grep_info,
1511 char *term_tmp = term_dict + strlen(term_dict);
1513 attr_init (&relation, zapt, 2);
1514 relation_value = attr_find (&relation, NULL);
1516 logf (LOG_DEBUG, "numeric relation value=%d", relation_value);
1518 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub, term_tmp, 1,
1521 term_value = atoi (term_tmp);
1522 switch (relation_value)
1525 logf (LOG_DEBUG, "Relation <");
1526 gen_regular_rel (term_tmp, term_value-1, 1);
1529 logf (LOG_DEBUG, "Relation <=");
1530 gen_regular_rel (term_tmp, term_value, 1);
1533 logf (LOG_DEBUG, "Relation >=");
1534 gen_regular_rel (term_tmp, term_value, 0);
1537 logf (LOG_DEBUG, "Relation >");
1538 gen_regular_rel (term_tmp, term_value+1, 0);
1542 logf (LOG_DEBUG, "Relation =");
1543 sprintf (term_tmp, "(0*%d)", term_value);
1545 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_tmp);
1546 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info, max_pos,
1549 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1550 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1554 static int numeric_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1555 const char **term_sub,
1556 oid_value attributeSet, struct grep_info *grep_info,
1557 int reg_type, int complete_flag,
1558 int num_bases, char **basenames,
1559 char *term_dst, int xpath_use, NMEM stream)
1561 char term_dict[2*IT_MAX_WORD+2];
1565 const char *use_string = 0;
1566 oid_value curAttributeSet = attributeSet;
1568 struct rpn_char_map_info rcmi;
1570 int bases_ok = 0; /* no of databases with OK attribute */
1571 int errCode = 0; /* err code (if any is not OK) */
1572 char *errString = 0; /* addinfo */
1574 rpn_char_map_prepare (zh->reg, reg_type, &rcmi);
1575 attr_init (&use, zapt, 1);
1576 use_value = attr_find_ex (&use, &curAttributeSet, &use_string);
1578 if (use_value == -1)
1581 for (base_no = 0; base_no < num_bases; base_no++)
1584 data1_local_attribute id_xpath_attr;
1585 data1_local_attribute *local_attr;
1586 int max_pos, prefix_len = 0;
1589 if (use_value == -2) /* string attribute (assume IDXPATH/any) */
1591 use_value = xpath_use;
1592 attp.local_attributes = &id_xpath_attr;
1593 attp.attset_ordinal = VAL_IDXPATH;
1594 id_xpath_attr.next = 0;
1595 id_xpath_attr.local = use_value;
1597 else if (curAttributeSet == VAL_IDXPATH)
1599 attp.local_attributes = &id_xpath_attr;
1600 attp.attset_ordinal = VAL_IDXPATH;
1601 id_xpath_attr.next = 0;
1602 id_xpath_attr.local = use_value;
1606 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value,
1609 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1610 curAttributeSet, use_value, r);
1614 sprintf (val_str, "%d", use_value);
1615 errString = nmem_strdup (stream, val_str);
1623 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
1625 zh->errCode = 109; /* Database unavailable */
1626 zh->errString = basenames[base_no];
1629 for (local_attr = attp.local_attributes; local_attr;
1630 local_attr = local_attr->next)
1636 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
1641 term_dict[prefix_len++] = '|';
1643 term_dict[prefix_len++] = '(';
1645 ord_len = key_SU_encode (ord, ord_buf);
1646 for (i = 0; i<ord_len; i++)
1648 term_dict[prefix_len++] = 1;
1649 term_dict[prefix_len++] = ord_buf[i];
1655 sprintf (val_str, "%d", use_value);
1657 errString = nmem_strdup (stream, val_str);
1661 term_dict[prefix_len++] = ')';
1662 term_dict[prefix_len++] = 1;
1663 term_dict[prefix_len++] = reg_type;
1664 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1665 term_dict[prefix_len] = '\0';
1666 if (!numeric_relation (zh, zapt, &termp, term_dict,
1667 attributeSet, grep_info, &max_pos, reg_type,
1673 zh->errCode = errCode;
1674 zh->errString = errString;
1678 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1682 static RSET rpn_search_APT_numeric (ZebraHandle zh,
1683 Z_AttributesPlusTerm *zapt,
1685 oid_value attributeSet,
1687 int reg_type, int complete_flag,
1688 const char *rank_type, int xpath_use,
1689 int num_bases, char **basenames,
1692 char term_dst[IT_MAX_WORD+1];
1693 const char *termp = termz;
1694 RSET rset[60], result;
1695 int i, r, rset_no = 0;
1696 struct grep_info grep_info;
1698 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1702 logf (LOG_DEBUG, "APT_numeric termp=%s", termp);
1703 grep_info.isam_p_indx = 0;
1704 r = numeric_term (zh, zapt, &termp, attributeSet, &grep_info,
1705 reg_type, complete_flag, num_bases, basenames,
1706 term_dst, xpath_use,
1710 logf (LOG_DEBUG, "term: %s", term_dst);
1711 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1712 grep_info.isam_p_indx, term_dst,
1713 strlen(term_dst), rank_type,
1714 0 /* preserve position */,
1715 zapt->term->which, rset_nmem, key_it_ctrl);
1716 assert (rset[rset_no]);
1717 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1720 grep_info_delete (&grep_info);
1722 return rsnull_create (rset_nmem,key_it_ctrl);
1724 for (i = 1; i<rset_no; i++)
1726 /* FIXME - Use a proper multi-and */
1727 result= rsbool_create_and(rset_nmem,key_it_ctrl,
1733 static RSET rpn_search_APT_local (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1735 oid_value attributeSet,
1737 const char *rank_type, NMEM rset_nmem)
1744 rset_temp_parms parms;
1746 parms.cmp = key_compare_it;
1747 parms.key_size = sizeof (struct it_key);
1748 parms.temp_path = res_get (zh->res, "setTmpDir");
1749 result = rset_create (rset_kind_temp, &parms);
1751 result = rstemp_create( rset_nmem,key_it_ctrl,
1752 res_get (zh->res, "setTmpDir") );
1753 rsfd = rset_open (result, RSETF_WRITE);
1768 rset_write (rsfd, &key);
1773 static RSET rpn_sort_spec (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1774 oid_value attributeSet, NMEM stream,
1775 Z_SortKeySpecList *sort_sequence,
1776 const char *rank_type)
1779 int sort_relation_value;
1780 AttrType sort_relation_type;
1785 Z_AttributeElement *ae;
1790 attr_init (&sort_relation_type, zapt, 7);
1791 sort_relation_value = attr_find (&sort_relation_type, &attributeSet);
1793 attr_init (&use_type, zapt, 1);
1794 use_value = attr_find (&use_type, &attributeSet);
1796 if (!sort_sequence->specs)
1798 sort_sequence->num_specs = 10;
1799 sort_sequence->specs = (Z_SortKeySpec **)
1800 nmem_malloc (stream, sort_sequence->num_specs *
1801 sizeof(*sort_sequence->specs));
1802 for (i = 0; i<sort_sequence->num_specs; i++)
1803 sort_sequence->specs[i] = 0;
1805 if (zapt->term->which != Z_Term_general)
1808 i = atoi_n ((char *) zapt->term->u.general->buf,
1809 zapt->term->u.general->len);
1810 if (i >= sort_sequence->num_specs)
1812 sprintf (termz, "%d", i);
1814 oe.proto = PROTO_Z3950;
1815 oe.oclass = CLASS_ATTSET;
1816 oe.value = attributeSet;
1817 if (!oid_ent_to_oid (&oe, oid))
1820 sks = (Z_SortKeySpec *) nmem_malloc (stream, sizeof(*sks));
1821 sks->sortElement = (Z_SortElement *)
1822 nmem_malloc (stream, sizeof(*sks->sortElement));
1823 sks->sortElement->which = Z_SortElement_generic;
1824 sk = sks->sortElement->u.generic = (Z_SortKey *)
1825 nmem_malloc (stream, sizeof(*sk));
1826 sk->which = Z_SortKey_sortAttributes;
1827 sk->u.sortAttributes = (Z_SortAttributes *)
1828 nmem_malloc (stream, sizeof(*sk->u.sortAttributes));
1830 sk->u.sortAttributes->id = oid;
1831 sk->u.sortAttributes->list = (Z_AttributeList *)
1832 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list));
1833 sk->u.sortAttributes->list->num_attributes = 1;
1834 sk->u.sortAttributes->list->attributes = (Z_AttributeElement **)
1835 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list->attributes));
1836 ae = *sk->u.sortAttributes->list->attributes = (Z_AttributeElement *)
1837 nmem_malloc (stream, sizeof(**sk->u.sortAttributes->list->attributes));
1838 ae->attributeSet = 0;
1839 ae->attributeType = (int *)
1840 nmem_malloc (stream, sizeof(*ae->attributeType));
1841 *ae->attributeType = 1;
1842 ae->which = Z_AttributeValue_numeric;
1843 ae->value.numeric = (int *)
1844 nmem_malloc (stream, sizeof(*ae->value.numeric));
1845 *ae->value.numeric = use_value;
1847 sks->sortRelation = (int *)
1848 nmem_malloc (stream, sizeof(*sks->sortRelation));
1849 if (sort_relation_value == 1)
1850 *sks->sortRelation = Z_SortKeySpec_ascending;
1851 else if (sort_relation_value == 2)
1852 *sks->sortRelation = Z_SortKeySpec_descending;
1854 *sks->sortRelation = Z_SortKeySpec_ascending;
1856 sks->caseSensitivity = (int *)
1857 nmem_malloc (stream, sizeof(*sks->caseSensitivity));
1858 *sks->caseSensitivity = 0;
1860 sks->which = Z_SortKeySpec_null;
1861 sks->u.null = odr_nullval ();
1862 sort_sequence->specs[i] = sks;
1863 return rsnull_create (NULL,key_it_ctrl);
1864 /* FIXME - nmem?? */
1868 static int parse_xpath(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1869 oid_value attributeSet,
1870 struct xpath_location_step *xpath, int max, NMEM mem)
1872 oid_value curAttributeSet = attributeSet;
1874 const char *use_string = 0;
1876 attr_init (&use, zapt, 1);
1877 attr_find_ex (&use, &curAttributeSet, &use_string);
1879 if (!use_string || *use_string != '/')
1882 return zebra_parse_xpath_str(use_string, xpath, max, mem);
1887 static RSET xpath_trunc(ZebraHandle zh, NMEM stream,
1888 int reg_type, const char *term, int use,
1889 oid_value curAttributeSet, NMEM rset_nmem)
1892 struct grep_info grep_info;
1893 char term_dict[2048];
1896 int ord = zebraExplain_lookupSU (zh->reg->zei, curAttributeSet, use);
1897 int ord_len, i, r, max_pos;
1898 int term_type = Z_Term_characterString;
1899 const char *flags = "void";
1901 if (grep_info_prepare (zh, 0 /* zapt */, &grep_info, '0', stream))
1902 return rsnull_create (rset_nmem,key_it_ctrl);
1905 return rsnull_create (rset_nmem,key_it_ctrl);
1907 term_dict[prefix_len++] = '|';
1909 term_dict[prefix_len++] = '(';
1911 ord_len = key_SU_encode (ord, ord_buf);
1912 for (i = 0; i<ord_len; i++)
1914 term_dict[prefix_len++] = 1;
1915 term_dict[prefix_len++] = ord_buf[i];
1917 term_dict[prefix_len++] = ')';
1918 term_dict[prefix_len++] = 1;
1919 term_dict[prefix_len++] = reg_type;
1921 strcpy (term_dict+prefix_len, term);
1923 grep_info.isam_p_indx = 0;
1924 r = dict_lookup_grep (zh->reg->dict, term_dict, 0,
1925 &grep_info, &max_pos, 0, grep_handle);
1926 yaz_log (LOG_LOG, "%s %d positions", term,
1927 grep_info.isam_p_indx);
1928 rset = rset_trunc (zh, grep_info.isam_p_buf,
1929 grep_info.isam_p_indx, term, strlen(term),
1930 flags, 1, term_type,rset_nmem,key_it_ctrl);
1931 grep_info_delete (&grep_info);
1935 static RSET rpn_search_xpath (ZebraHandle zh,
1936 oid_value attributeSet,
1937 int num_bases, char **basenames,
1938 NMEM stream, const char *rank_type, RSET rset,
1939 int xpath_len, struct xpath_location_step *xpath,
1942 oid_value curAttributeSet = attributeSet;
1949 yaz_log (LOG_LOG, "len=%d", xpath_len);
1950 for (i = 0; i<xpath_len; i++)
1952 yaz_log (LOG_LOG, "XPATH %d %s", i, xpath[i].part);
1956 curAttributeSet = VAL_IDXPATH;
1966 a[@attr=value]/b[@other=othervalue]
1968 /e/@a val range(e/,range(@a,freetext(w,1015,val),@a),e/)
1969 /a/b val range(b/a/,freetext(w,1016,val),b/a/)
1970 /a/b/@c val range(b/a/,range(@c,freetext(w,1016,val),@c),b/a/)
1971 /a/b[@c=y] val range(b/a/,freetext(w,1016,val),b/a/,@c=y)
1972 /a[@c=y]/b val range(a/,range(b/a/,freetext(w,1016,val),b/a/),a/,@c=y)
1973 /a[@c=x]/b[@c=y] range(a/,range(b/a/,freetext(w,1016,val),b/a/,@c=y),a/,@c=x)
1977 dict_grep_cmap (zh->reg->dict, 0, 0);
1979 for (base_no = 0; base_no < num_bases; base_no++)
1981 int level = xpath_len;
1984 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
1986 zh->errCode = 109; /* Database unavailable */
1987 zh->errString = basenames[base_no];
1990 while (--level >= 0)
1992 char xpath_rev[128];
1994 RSET rset_start_tag = 0, rset_end_tag = 0, rset_attr = 0;
1998 for (i = level; i >= 1; --i)
2000 const char *cp = xpath[i].part;
2006 memcpy (xpath_rev + len, "[^/]*", 5);
2009 else if (*cp == ' ')
2012 xpath_rev[len++] = 1;
2013 xpath_rev[len++] = ' ';
2017 xpath_rev[len++] = *cp;
2018 xpath_rev[len++] = '/';
2020 else if (i == 1) /* // case */
2022 xpath_rev[len++] = '.';
2023 xpath_rev[len++] = '*';
2028 if (xpath[level].predicate &&
2029 xpath[level].predicate->which == XPATH_PREDICATE_RELATION &&
2030 xpath[level].predicate->u.relation.name[0])
2032 WRBUF wbuf = wrbuf_alloc();
2033 wrbuf_puts(wbuf, xpath[level].predicate->u.relation.name+1);
2034 if (xpath[level].predicate->u.relation.value)
2036 const char *cp = xpath[level].predicate->u.relation.value;
2037 wrbuf_putc(wbuf, '=');
2041 if (strchr(REGEX_CHARS, *cp))
2042 wrbuf_putc(wbuf, '\\');
2043 wrbuf_putc(wbuf, *cp);
2047 wrbuf_puts(wbuf, "");
2048 rset_attr = xpath_trunc (
2049 zh, stream, '0', wrbuf_buf(wbuf), 3,
2050 curAttributeSet,rset_nmem);
2051 wrbuf_free(wbuf, 1);
2058 yaz_log (LOG_LOG, "xpath_rev (%d) = %s", level, xpath_rev);
2059 if (strlen(xpath_rev))
2061 rset_start_tag = xpath_trunc(zh, stream, '0',
2062 xpath_rev, 1, curAttributeSet, rset_nmem);
2064 rset_end_tag = xpath_trunc(zh, stream, '0',
2065 xpath_rev, 2, curAttributeSet, rset_nmem);
2068 parms.key_size = sizeof(struct it_key);
2069 parms.cmp = key_compare_it;
2070 parms.rset_l = rset_start_tag;
2071 parms.rset_m = rset;
2072 parms.rset_r = rset_end_tag;
2073 parms.rset_attr = rset_attr;
2074 parms.printer = key_print_it;
2075 rset = rset_create (rset_kind_between, &parms);
2077 rset=rsbetween_create( rset_nmem,key_it_ctrl,
2078 rset_start_tag, rset, rset_end_tag, rset_attr);
2089 static RSET rpn_search_APT (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2090 oid_value attributeSet, NMEM stream,
2091 Z_SortKeySpecList *sort_sequence,
2092 int num_bases, char **basenames,
2096 char *search_type = NULL;
2097 char rank_type[128];
2100 char termz[IT_MAX_WORD+1];
2104 struct xpath_location_step xpath[10];
2106 zebra_maps_attr (zh->reg->zebra_maps, zapt, ®_id, &search_type,
2107 rank_type, &complete_flag, &sort_flag);
2109 logf (LOG_DEBUG, "reg_id=%c", reg_id);
2110 logf (LOG_DEBUG, "complete_flag=%d", complete_flag);
2111 logf (LOG_DEBUG, "search_type=%s", search_type);
2112 logf (LOG_DEBUG, "rank_type=%s", rank_type);
2114 if (zapt_term_to_utf8(zh, zapt, termz))
2118 return rpn_sort_spec (zh, zapt, attributeSet, stream, sort_sequence,
2120 xpath_len = parse_xpath(zh, zapt, attributeSet, xpath, 10, stream);
2124 if (xpath[xpath_len-1].part[0] == '@')
2128 if (!strcmp (search_type, "phrase"))
2130 rset = rpn_search_APT_phrase (zh, zapt, termz, attributeSet, stream,
2131 reg_id, complete_flag, rank_type,
2133 num_bases, basenames, rset_nmem);
2135 else if (!strcmp (search_type, "and-list"))
2137 rset = rpn_search_APT_and_list (zh, zapt, termz, attributeSet, stream,
2138 reg_id, complete_flag, rank_type,
2140 num_bases, basenames, rset_nmem);
2142 else if (!strcmp (search_type, "or-list"))
2144 rset = rpn_search_APT_or_list (zh, zapt, termz, attributeSet, stream,
2145 reg_id, complete_flag, rank_type,
2147 num_bases, basenames, rset_nmem);
2149 else if (!strcmp (search_type, "local"))
2151 rset = rpn_search_APT_local (zh, zapt, termz, attributeSet, stream,
2152 rank_type, rset_nmem);
2154 else if (!strcmp (search_type, "numeric"))
2156 rset = rpn_search_APT_numeric (zh, zapt, termz, attributeSet, stream,
2157 reg_id, complete_flag, rank_type,
2159 num_bases, basenames, rset_nmem);
2161 else if (!strcmp (search_type, "always"))
2167 return rpn_search_xpath (zh, attributeSet, num_bases, basenames,
2168 stream, rank_type, rset,
2169 xpath_len, xpath, rset_nmem);
2172 static RSET rpn_search_structure (ZebraHandle zh, Z_RPNStructure *zs,
2173 oid_value attributeSet,
2174 NMEM stream, NMEM rset_nmem,
2175 Z_SortKeySpecList *sort_sequence,
2176 int num_bases, char **basenames)
2179 if (zs->which == Z_RPNStructure_complex)
2181 Z_Operator *zop = zs->u.complex->roperator;
2185 rset_l = rpn_search_structure (zh, zs->u.complex->s1,
2186 attributeSet, stream, rset_nmem,
2188 num_bases, basenames);
2191 rset_r = rpn_search_structure (zh, zs->u.complex->s2,
2192 attributeSet, stream, rset_nmem,
2194 num_bases, basenames);
2197 rset_delete (rset_l);
2203 case Z_Operator_and:
2204 r = rsbool_create_and(rset_nmem,key_it_ctrl, rset_l,rset_r );
2207 r = rsbool_create_or(rset_nmem,key_it_ctrl, rset_l,rset_r );
2209 case Z_Operator_and_not:
2210 r = rsbool_create_not(rset_nmem,key_it_ctrl, rset_l,rset_r );
2212 case Z_Operator_prox:
2213 if (zop->u.prox->which != Z_ProximityOperator_known)
2218 if (*zop->u.prox->u.known != Z_ProxUnit_word)
2220 char *val = (char *) nmem_malloc (stream, 16);
2222 zh->errString = val;
2223 sprintf (val, "%d", *zop->u.prox->u.known);
2228 /* new / old prox */
2231 twosets[0] = rset_l;
2232 twosets[1] = rset_r;
2233 r=rsprox_create(rset_nmem,key_it_ctrl,
2235 *zop->u.prox->ordered,
2236 (!zop->u.prox->exclusion ?
2237 0 : *zop->u.prox->exclusion),
2238 *zop->u.prox->relationType,
2239 *zop->u.prox->distance );
2247 else if (zs->which == Z_RPNStructure_simple)
2249 if (zs->u.simple->which == Z_Operand_APT)
2251 logf (LOG_DEBUG, "rpn_search_APT");
2252 r = rpn_search_APT (zh, zs->u.simple->u.attributesPlusTerm,
2253 attributeSet, stream, sort_sequence,
2254 num_bases, basenames,rset_nmem);
2256 else if (zs->u.simple->which == Z_Operand_resultSetId)
2258 logf (LOG_DEBUG, "rpn_search_ref");
2259 r = resultSetRef (zh, zs->u.simple->u.resultSetId);
2262 r = rsnull_create (rset_nmem,key_it_ctrl);
2265 nmem_strdup (stream, zs->u.simple->u.resultSetId);
2286 RSET rpn_search (ZebraHandle zh, NMEM nmem, NMEM rset_nmem,
2287 Z_RPNQuery *rpn, int num_bases, char **basenames,
2288 const char *setname,
2293 oid_value attributeSet;
2294 Z_SortKeySpecList *sort_sequence;
2298 zh->errString = NULL;
2301 sort_sequence = (Z_SortKeySpecList *)
2302 nmem_malloc (nmem, sizeof(*sort_sequence));
2303 sort_sequence->num_specs = 10;
2304 sort_sequence->specs = (Z_SortKeySpec **)
2305 nmem_malloc (nmem, sort_sequence->num_specs *
2306 sizeof(*sort_sequence->specs));
2307 for (i = 0; i<sort_sequence->num_specs; i++)
2308 sort_sequence->specs[i] = 0;
2310 attrset = oid_getentbyoid (rpn->attributeSetId);
2311 attributeSet = attrset->value;
2312 rset = rpn_search_structure (zh, rpn->RPNStructure, attributeSet,
2314 sort_sequence, num_bases, basenames);
2319 logf (LOG_DEBUG, "search error: %d", zh->errCode);
2321 for (i = 0; sort_sequence->specs[i]; i++)
2323 sort_sequence->num_specs = i;
2325 resultSetRank (zh, sset, rset);
2328 logf (LOG_DEBUG, "resultSetSortSingle in rpn_search");
2329 resultSetSortSingle (zh, nmem, sset, rset,
2330 sort_sequence, &sort_status);
2333 logf (LOG_DEBUG, "resultSetSortSingle status = %d", zh->errCode);
2339 struct scan_info_entry {
2345 struct scan_info_entry *list;
2351 static int scan_handle (char *name, const char *info, int pos, void *client)
2353 int len_prefix, idx;
2354 struct scan_info *scan_info = (struct scan_info *) client;
2356 len_prefix = strlen(scan_info->prefix);
2357 if (memcmp (name, scan_info->prefix, len_prefix))
2359 if (pos > 0) idx = scan_info->after - pos + scan_info->before;
2362 scan_info->list[idx].term = (char *)
2363 odr_malloc (scan_info->odr, strlen(name + len_prefix)+1);
2364 strcpy (scan_info->list[idx].term, name + len_prefix);
2365 assert (*info == sizeof(ISAMC_P));
2366 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAMC_P));
2370 static void scan_term_untrans (ZebraHandle zh, NMEM stream, int reg_type,
2371 char **dst, const char *src)
2373 char term_src[IT_MAX_WORD];
2374 char term_dst[IT_MAX_WORD];
2376 term_untrans (zh, reg_type, term_src, src);
2378 if (zh->iconv_from_utf8 != 0)
2381 char *inbuf = term_src;
2382 size_t inleft = strlen(term_src);
2383 char *outbuf = term_dst;
2384 size_t outleft = sizeof(term_dst)-1;
2387 ret = yaz_iconv (zh->iconv_from_utf8, &inbuf, &inleft,
2389 if (ret == (size_t)(-1))
2392 len = outbuf - term_dst;
2393 *dst = nmem_malloc (stream, len + 1);
2395 memcpy (*dst, term_dst, len);
2399 *dst = nmem_strdup (stream, term_src);
2402 static void count_set (RSET r, int *count)
2409 logf (LOG_DEBUG, "count_set");
2412 rfd = rset_open (r, RSETF_READ);
2413 while (rset_read (rfd, &key))
2416 if (key.mem[0] != psysno)
2418 psysno = key.mem[0];
2422 if (key.sysno != psysno)
2431 logf (LOG_DEBUG, "%d keys, %d records", kno, *count);
2434 void rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
2435 oid_value attributeset,
2436 int num_bases, char **basenames,
2437 int *position, int *num_entries, ZebraScanEntry **list,
2438 int *is_partial, RSET limit_set, int return_zero)
2441 int pos = *position;
2442 int num = *num_entries;
2446 char termz[IT_MAX_WORD+20];
2449 const char *use_string = 0;
2450 struct scan_info *scan_info_array;
2451 ZebraScanEntry *glist;
2452 int ords[32], ord_no = 0;
2455 int bases_ok = 0; /* no of databases with OK attribute */
2456 int errCode = 0; /* err code (if any is not OK) */
2457 char *errString = 0; /* addinfo */
2460 char *search_type = NULL;
2461 char rank_type[128];
2464 NMEM rset_nmem=NULL;
2468 if (attributeset == VAL_NONE)
2469 attributeset = VAL_BIB1;
2474 int termset_value_numeric;
2475 const char *termset_value_string;
2476 attr_init (&termset, zapt, 8);
2477 termset_value_numeric =
2478 attr_find_ex (&termset, NULL, &termset_value_string);
2479 if (termset_value_numeric != -1)
2482 const char *termset_name = 0;
2484 if (termset_value_numeric != -2)
2487 sprintf (resname, "%d", termset_value_numeric);
2488 termset_name = resname;
2491 termset_name = termset_value_string;
2493 limit_set = resultSetRef (zh, termset_name);
2497 yaz_log (LOG_DEBUG, "position = %d, num = %d set=%d",
2498 pos, num, attributeset);
2500 attr_init (&use, zapt, 1);
2501 use_value = attr_find_ex (&use, &attributeset, &use_string);
2503 if (zebra_maps_attr (zh->reg->zebra_maps, zapt, ®_id, &search_type,
2504 rank_type, &complete_flag, &sort_flag))
2510 yaz_log (LOG_DEBUG, "use_value = %d", use_value);
2512 if (use_value == -1)
2514 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
2518 data1_local_attribute *local_attr;
2520 if ((r=att_getentbyatt (zh, &attp, attributeset, use_value,
2523 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
2524 attributeset, use_value);
2528 sprintf (val_str, "%d", use_value);
2530 errString = odr_strdup (stream, val_str);
2536 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
2538 zh->errString = basenames[base_no];
2539 zh->errCode = 109; /* Database unavailable */
2544 for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
2545 local_attr = local_attr->next)
2549 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
2552 ords[ord_no++] = ord;
2555 if (!bases_ok && errCode)
2557 zh->errCode = errCode;
2558 zh->errString = errString;
2564 sprintf (val_str, "%d", use_value);
2566 zh->errString = odr_strdup (stream, val_str);
2571 /* prepare dictionary scanning */
2574 scan_info_array = (struct scan_info *)
2575 odr_malloc (stream, ord_no * sizeof(*scan_info_array));
2576 for (i = 0; i < ord_no; i++)
2578 int j, prefix_len = 0;
2579 int before_tmp = before, after_tmp = after;
2580 struct scan_info *scan_info = scan_info_array + i;
2581 struct rpn_char_map_info rcmi;
2583 rpn_char_map_prepare (zh->reg, reg_id, &rcmi);
2585 scan_info->before = before;
2586 scan_info->after = after;
2587 scan_info->odr = stream;
2589 scan_info->list = (struct scan_info_entry *)
2590 odr_malloc (stream, (before+after) * sizeof(*scan_info->list));
2591 for (j = 0; j<before+after; j++)
2592 scan_info->list[j].term = NULL;
2594 prefix_len += key_SU_encode (ords[i], termz + prefix_len);
2595 termz[prefix_len++] = reg_id;
2596 termz[prefix_len] = 0;
2597 strcpy (scan_info->prefix, termz);
2599 if (trans_scan_term (zh, zapt, termz+prefix_len, reg_id))
2602 dict_scan (zh->reg->dict, termz, &before_tmp, &after_tmp,
2603 scan_info, scan_handle);
2605 glist = (ZebraScanEntry *)
2606 odr_malloc (stream, (before+after)*sizeof(*glist));
2608 rset_nmem=nmem_create();
2610 /* consider terms after main term */
2611 for (i = 0; i < ord_no; i++)
2615 for (i = 0; i<after; i++)
2618 const char *mterm = NULL;
2622 for (j = 0; j < ord_no; j++)
2624 if (ptr[j] < before+after &&
2625 (tst=scan_info_array[j].list[ptr[j]].term) &&
2626 (!mterm || strcmp (tst, mterm) < 0))
2634 scan_term_untrans (zh, stream->mem, reg_id,
2635 &glist[i+before].term, mterm);
2636 rset = rset_trunc (zh, &scan_info_array[j0].list[ptr[j0]].isam_p, 1,
2637 glist[i+before].term, strlen(glist[i+before].term),
2638 NULL, 0, zapt->term->which, rset_nmem, key_it_ctrl);
2641 for (j = j0+1; j<ord_no; j++)
2643 if (ptr[j] < before+after &&
2644 (tst=scan_info_array[j].list[ptr[j]].term) &&
2645 !strcmp (tst, mterm))
2650 rset_trunc (zh, &scan_info_array[j].list[ptr[j]].isam_p, 1,
2651 glist[i+before].term,
2652 strlen(glist[i+before].term), NULL, 0,
2653 zapt->term->which,rset_nmem,key_it_ctrl);
2654 rset = rsbool_create_or(rset_nmem,key_it_ctrl,
2656 /* FIXME - Use a proper multi-or */
2662 rset = rsbool_create_and(rset_nmem,key_it_ctrl,
2663 rset, rset_dup(limit_set));
2664 count_set (rset, &glist[i+before].occurrences);
2669 *num_entries -= (after-i);
2673 /* consider terms before main term */
2674 for (i = 0; i<ord_no; i++)
2677 for (i = 0; i<before; i++)
2680 const char *mterm = NULL;
2684 for (j = 0; j <ord_no; j++)
2686 if (ptr[j] < before &&
2687 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2688 (!mterm || strcmp (tst, mterm) > 0))
2697 scan_term_untrans (zh, stream->mem, reg_id,
2698 &glist[before-1-i].term, mterm);
2701 (zh, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1,
2702 glist[before-1-i].term, strlen(glist[before-1-i].term),
2703 NULL, 0, zapt->term->which,rset_nmem,key_it_ctrl);
2707 for (j = j0+1; j<ord_no; j++)
2709 if (ptr[j] < before &&
2710 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2711 !strcmp (tst, mterm))
2715 rset2 = rset_trunc (zh,
2716 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1,
2717 glist[before-1-i].term,
2718 strlen(glist[before-1-i].term), NULL, 0,
2719 zapt->term->which, rset_nmem,key_it_ctrl);
2720 rset = rsbool_create_and(rset_nmem,key_it_ctrl,
2722 /* FIXME - multi-and ?? */
2727 rset = rsbool_create_and(rset_nmem,key_it_ctrl,
2728 rset, rset_dup(limit_set));
2729 count_set (rset, &glist[before-1-i].occurrences);
2740 nmem_destroy(rset_nmem);
2741 *list = glist + i; /* list is set to first 'real' entry */
2743 logf (LOG_DEBUG, "position = %d, num_entries = %d",
2744 *position, *num_entries);
2746 logf (LOG_DEBUG, "scan error: %d", zh->errCode);