1 /* $Id: zrpn.c,v 1.133 2003-04-15 20:48:04 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003
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>
42 struct rpn_char_map_info {
51 Z_AttributesPlusTerm *zapt;
55 static const char **rpn_char_map_handler (void *vp, const char **from, int len)
57 struct rpn_char_map_info *p = (struct rpn_char_map_info *) vp;
58 const char **out = zebra_maps_input (p->zm, p->reg_type, from, len);
62 const char *outp = *out;
63 yaz_log (LOG_LOG, "---");
66 yaz_log (LOG_LOG, "%02X", *outp);
74 static void rpn_char_map_prepare (struct zebra_register *reg, int reg_type,
75 struct rpn_char_map_info *map_info)
77 map_info->zm = reg->zebra_maps;
78 map_info->reg_type = reg_type;
79 dict_grep_cmap (reg->dict, map_info, rpn_char_map_handler);
82 static int attr_find_ex (AttrType *src, oid_value *attributeSetP,
83 const char **string_value)
87 num_attributes = src->zapt->attributes->num_attributes;
88 while (src->major < num_attributes)
90 Z_AttributeElement *element;
92 element = src->zapt->attributes->attributes[src->major];
93 if (src->type == *element->attributeType)
95 switch (element->which)
97 case Z_AttributeValue_numeric:
99 if (element->attributeSet && attributeSetP)
103 attrset = oid_getentbyoid (element->attributeSet);
104 *attributeSetP = attrset->value;
106 return *element->value.numeric;
108 case Z_AttributeValue_complex:
109 if (src->minor >= element->value.complex->num_list)
111 if (element->attributeSet && attributeSetP)
115 attrset = oid_getentbyoid (element->attributeSet);
116 *attributeSetP = attrset->value;
118 if (element->value.complex->list[src->minor]->which ==
119 Z_StringOrNumeric_numeric)
123 *element->value.complex->list[src->minor-1]->u.numeric;
125 else if (element->value.complex->list[src->minor]->which ==
126 Z_StringOrNumeric_string)
132 element->value.complex->list[src->minor-1]->u.string;
146 static int attr_find (AttrType *src, oid_value *attributeSetP)
148 return attr_find_ex (src, attributeSetP, 0);
151 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
174 static void term_untrans (ZebraHandle zh, int reg_type,
175 char *dst, const char *src)
180 const char *cp = zebra_maps_output (zh->reg->zebra_maps,
182 if (!cp && len < IT_MAX_WORD-1)
185 while (*cp && len < IT_MAX_WORD-1)
191 static void add_isam_p (const char *name, const char *info,
194 if (p->isam_p_indx == p->isam_p_size)
196 ISAMS_P *new_isam_p_buf;
200 p->isam_p_size = 2*p->isam_p_size + 100;
201 new_isam_p_buf = (ISAMS_P *) xmalloc (sizeof(*new_isam_p_buf) *
205 memcpy (new_isam_p_buf, p->isam_p_buf,
206 p->isam_p_indx * sizeof(*p->isam_p_buf));
207 xfree (p->isam_p_buf);
209 p->isam_p_buf = new_isam_p_buf;
212 new_term_no = (int *) xmalloc (sizeof(*new_term_no) *
216 memcpy (new_term_no, p->isam_p_buf,
217 p->isam_p_indx * sizeof(*p->term_no));
220 p->term_no = new_term_no;
223 assert (*info == sizeof(*p->isam_p_buf));
224 memcpy (p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
231 char term_tmp[IT_MAX_WORD];
233 int len = key_SU_decode (&su_code, name);
235 term_untrans (p->zh, p->reg_type, term_tmp, name+len+1);
236 logf (LOG_LOG, "grep: %d %c %s", su_code, name[len], term_tmp);
237 zebraExplain_lookup_ord (p->zh->reg->zei,
238 su_code, &db, &set, &use);
239 logf (LOG_LOG, "grep: set=%d use=%d db=%s", set, use, db);
241 resultSetAddTerm (p->zh, p->termset, name[len], db,
248 static int grep_handle (char *name, const char *info, void *p)
250 add_isam_p (name, info, (struct grep_info *) p);
254 static int term_pre (ZebraMaps zebra_maps, int reg_type, const char **src,
255 const char *ct1, const char *ct2)
257 const char *s1, *s0 = *src;
260 /* skip white space */
263 if (ct1 && strchr (ct1, *s0))
265 if (ct2 && strchr (ct2, *s0))
268 map = zebra_maps_input (zebra_maps, reg_type, &s1, strlen(s1));
269 if (**map != *CHR_SPACE)
277 #define REGEX_CHARS " []()|.*+?!"
279 /* term_100: handle term, where trunc=none (no operators at all) */
280 static int term_100 (ZebraMaps zebra_maps, int reg_type,
281 const char **src, char *dst, int space_split,
289 const char *space_start = 0;
290 const char *space_end = 0;
292 if (!term_pre (zebra_maps, reg_type, src, NULL, NULL))
298 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
301 if (**map == *CHR_SPACE)
304 else /* complete subfield only. */
306 if (**map == *CHR_SPACE)
307 { /* save space mapping for later .. */
312 else if (space_start)
313 { /* reload last space */
314 while (space_start < space_end)
316 if (strchr (REGEX_CHARS, *space_start))
318 dst_term[j++] = *space_start;
319 dst[i++] = *space_start++;
322 space_start = space_end = 0;
325 /* add non-space char */
328 if (strchr(REGEX_CHARS, *s1))
340 /* term_101: handle term, where trunc=Process # */
341 static int term_101 (ZebraMaps zebra_maps, int reg_type,
342 const char **src, char *dst, int space_split,
350 if (!term_pre (zebra_maps, reg_type, src, "#", "#"))
359 dst_term[j++] = *s0++;
364 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
365 if (space_split && **map == *CHR_SPACE)
369 if (strchr(REGEX_CHARS, *s1))
377 dst_term[j++] = '\0';
382 /* term_103: handle term, where trunc=re-2 (regular expressions) */
383 static int term_103 (ZebraMaps zebra_maps, int reg_type, const char **src,
384 char *dst, int *errors, int space_split,
392 if (!term_pre (zebra_maps, reg_type, src, "^\\()[].*+?|", "("))
395 if (errors && *s0 == '+' && s0[1] && s0[2] == '+' && s0[3] &&
398 *errors = s0[1] - '0';
405 if (strchr ("^\\()[].*+?|-", *s0))
413 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
414 if (**map == *CHR_SPACE)
418 if (strchr(REGEX_CHARS, *s1))
431 /* term_103: handle term, where trunc=re-1 (regular expressions) */
432 static int term_102 (ZebraMaps zebra_maps, int reg_type, const char **src,
433 char *dst, int space_split, char *dst_term)
435 return term_103 (zebra_maps, reg_type, src, dst, NULL, space_split,
440 /* term_104: handle term, where trunc=Process # and ! */
441 static int term_104 (ZebraMaps zebra_maps, int reg_type,
442 const char **src, char *dst, int space_split,
450 if (!term_pre (zebra_maps, reg_type, src, "?*#", "?*#"))
457 dst_term[j++] = *s0++;
458 if (*s0 >= '0' && *s0 <= '9')
461 while (*s0 >= '0' && *s0 <= '9')
463 limit = limit * 10 + (*s0 - '0');
464 dst_term[j++] = *s0++;
484 dst_term[j++] = *s0++;
489 dst_term[j++] = *s0++;
493 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
494 if (space_split && **map == *CHR_SPACE)
498 if (strchr(REGEX_CHARS, *s1))
506 dst_term[j++] = '\0';
511 /* term_105/106: handle term, where trunc=Process * and ! and right trunc */
512 static int term_105 (ZebraMaps zebra_maps, int reg_type,
513 const char **src, char *dst, int space_split,
514 char *dst_term, int right_truncate)
521 if (!term_pre (zebra_maps, reg_type, src, "*!", "*!"))
530 dst_term[j++] = *s0++;
535 dst_term[j++] = *s0++;
539 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
540 if (space_split && **map == *CHR_SPACE)
544 if (strchr(REGEX_CHARS, *s1))
558 dst_term[j++] = '\0';
564 /* gen_regular_rel - generate regular expression from relation
565 * val: border value (inclusive)
566 * islt: 1 if <=; 0 if >=.
568 static void gen_regular_rel (char *dst, int val, int islt)
575 logf (LOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
579 strcpy (dst, "(-[0-9]+|(");
587 strcpy (dst, "([0-9]+|-(");
588 dst_p = strlen (dst);
598 dst_p = strlen (dst);
599 sprintf (numstr, "%d", val);
600 for (w = strlen(numstr); --w >= 0; pos++)
619 strcpy (dst + dst_p, numstr);
620 dst_p = strlen(dst) - pos - 1;
648 for (i = 0; i<pos; i++)
661 /* match everything less than 10^(pos-1) */
663 for (i=1; i<pos; i++)
664 strcat (dst, "[0-9]?");
668 /* match everything greater than 10^pos */
669 for (i = 0; i <= pos; i++)
670 strcat (dst, "[0-9]");
671 strcat (dst, "[0-9]*");
676 void string_rel_add_char (char **term_p, const char *src, int *indx)
678 if (src[*indx] == '\\')
679 *(*term_p)++ = src[(*indx)++];
680 *(*term_p)++ = src[(*indx)++];
684 * > abc ([b-].*|a[c-].*|ab[d-].*|abc.+)
685 * ([^-a].*|a[^-b].*ab[^-c].*|abc.+)
686 * >= abc ([b-].*|a[c-].*|ab[c-].*)
687 * ([^-a].*|a[^-b].*|ab[c-].*)
688 * < abc ([-0].*|a[-a].*|ab[-b].*)
689 * ([^a-].*|a[^b-].*|ab[^c-].*)
690 * <= abc ([-0].*|a[-a].*|ab[-b].*|abc)
691 * ([^a-].*|a[^b-].*|ab[^c-].*|abc)
693 static int string_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
694 const char **term_sub, char *term_dict,
695 oid_value attributeSet,
696 int reg_type, int space_split, char *term_dst)
701 char *term_tmp = term_dict + strlen(term_dict);
702 char term_component[2*IT_MAX_WORD+20];
704 attr_init (&relation, zapt, 2);
705 relation_value = attr_find (&relation, NULL);
707 logf (LOG_DEBUG, "string relation value=%d", relation_value);
708 switch (relation_value)
711 if (!term_100 (zh->reg->zebra_maps, reg_type,
712 term_sub, term_component,
713 space_split, term_dst))
715 logf (LOG_DEBUG, "Relation <");
718 for (i = 0; term_component[i]; )
725 string_rel_add_char (&term_tmp, term_component, &j);
730 string_rel_add_char (&term_tmp, term_component, &i);
737 if ((term_tmp - term_dict) > IT_MAX_WORD)
744 if (!term_100 (zh->reg->zebra_maps, reg_type,
745 term_sub, term_component,
746 space_split, term_dst))
748 logf (LOG_DEBUG, "Relation <=");
751 for (i = 0; term_component[i]; )
756 string_rel_add_char (&term_tmp, term_component, &j);
760 string_rel_add_char (&term_tmp, term_component, &i);
769 if ((term_tmp - term_dict) > IT_MAX_WORD)
772 for (i = 0; term_component[i]; )
773 string_rel_add_char (&term_tmp, term_component, &i);
778 if (!term_100 (zh->reg->zebra_maps, reg_type,
779 term_sub, term_component, space_split, term_dst))
781 logf (LOG_DEBUG, "Relation >");
784 for (i = 0; term_component[i];)
789 string_rel_add_char (&term_tmp, term_component, &j);
794 string_rel_add_char (&term_tmp, term_component, &i);
802 if ((term_tmp - term_dict) > IT_MAX_WORD)
805 for (i = 0; term_component[i];)
806 string_rel_add_char (&term_tmp, term_component, &i);
813 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub,
814 term_component, space_split, term_dst))
816 logf (LOG_DEBUG, "Relation >=");
819 for (i = 0; term_component[i];)
826 string_rel_add_char (&term_tmp, term_component, &j);
829 if (term_component[i+1])
833 string_rel_add_char (&term_tmp, term_component, &i);
837 string_rel_add_char (&term_tmp, term_component, &i);
844 if ((term_tmp - term_dict) > IT_MAX_WORD)
852 logf (LOG_DEBUG, "Relation =");
853 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub,
854 term_component, space_split, term_dst))
856 strcat (term_tmp, "(");
857 strcat (term_tmp, term_component);
858 strcat (term_tmp, ")");
863 static int string_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
864 const char **term_sub,
865 oid_value attributeSet, NMEM stream,
866 struct grep_info *grep_info,
867 int reg_type, int complete_flag,
868 int num_bases, char **basenames,
869 char *term_dst, int xpath_use);
871 static RSET term_trunc (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
872 const char **term_sub,
873 oid_value attributeSet, NMEM stream,
874 struct grep_info *grep_info,
875 int reg_type, int complete_flag,
876 int num_bases, char **basenames,
878 const char *rank_type, int xpath_use)
881 grep_info->isam_p_indx = 0;
882 r = string_term (zh, zapt, term_sub, attributeSet, stream, grep_info,
883 reg_type, complete_flag, num_bases, basenames,
884 term_dst, xpath_use);
887 logf (LOG_DEBUG, "term: %s", term_dst);
888 return rset_trunc (zh, grep_info->isam_p_buf,
889 grep_info->isam_p_indx, term_dst,
890 strlen(term_dst), rank_type, 1 /* preserve pos */,
895 static int string_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
896 const char **term_sub,
897 oid_value attributeSet, NMEM stream,
898 struct grep_info *grep_info,
899 int reg_type, int complete_flag,
900 int num_bases, char **basenames,
901 char *term_dst, int xpath_use)
903 char term_dict[2*IT_MAX_WORD+4000];
906 int truncation_value;
909 const char *use_string = 0;
910 oid_value curAttributeSet = attributeSet;
912 struct rpn_char_map_info rcmi;
913 int space_split = complete_flag ? 0 : 1;
915 rpn_char_map_prepare (zh->reg, reg_type, &rcmi);
916 attr_init (&use, zapt, 1);
917 use_value = attr_find_ex (&use, &curAttributeSet, &use_string);
918 logf (LOG_DEBUG, "string_term, use value %d", use_value);
919 attr_init (&truncation, zapt, 5);
920 truncation_value = attr_find (&truncation, NULL);
921 logf (LOG_DEBUG, "truncation value %d", truncation_value);
923 if (use_value == -1) /* no attribute - assumy "any" */
925 for (base_no = 0; base_no < num_bases; base_no++)
928 data1_local_attribute id_xpath_attr;
929 data1_local_attribute *local_attr;
930 int max_pos, prefix_len = 0;
934 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
936 zh->errCode = 109; /* Database unavailable */
937 zh->errString = basenames[base_no];
940 if (use_value == -2) /* string attribute (assume IDXPATH/any) */
942 use_value = xpath_use;
943 attp.local_attributes = &id_xpath_attr;
944 attp.attset_ordinal = VAL_IDXPATH;
945 id_xpath_attr.next = 0;
946 id_xpath_attr.local = use_value;
948 else if (curAttributeSet == VAL_IDXPATH)
950 attp.local_attributes = &id_xpath_attr;
951 attp.attset_ordinal = VAL_IDXPATH;
952 id_xpath_attr.next = 0;
953 id_xpath_attr.local = use_value;
957 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
959 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
960 curAttributeSet, use_value, r);
963 /* set was found, but value wasn't defined */
965 sprintf (val_str, "%d", use_value);
967 zh->errString = nmem_strdup (stream, val_str);
972 struct oident oident;
974 oident.proto = PROTO_Z3950;
975 oident.oclass = CLASS_ATTSET;
976 oident.value = curAttributeSet;
977 oid_ent_to_oid (&oident, oid);
980 zh->errString = nmem_strdup (stream, oident.desc);
985 for (local_attr = attp.local_attributes; local_attr;
986 local_attr = local_attr->next)
992 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
997 term_dict[prefix_len++] = '|';
999 term_dict[prefix_len++] = '(';
1001 ord_len = key_SU_encode (ord, ord_buf);
1002 for (i = 0; i<ord_len; i++)
1004 term_dict[prefix_len++] = 1;
1005 term_dict[prefix_len++] = ord_buf[i];
1011 sprintf (val_str, "%d", use_value);
1013 zh->errString = nmem_strdup (stream, val_str);
1016 term_dict[prefix_len++] = ')';
1017 term_dict[prefix_len++] = 1;
1018 term_dict[prefix_len++] = reg_type;
1019 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1020 term_dict[prefix_len] = '\0';
1022 switch (truncation_value)
1024 case -1: /* not specified */
1025 case 100: /* do not truncate */
1026 if (!string_relation (zh, zapt, &termp, term_dict,
1028 reg_type, space_split, term_dst))
1030 logf (LOG_LOG, "dict_lookup_grep: %s", term_dict+prefix_len);
1031 r = dict_lookup_grep (zh->reg->dict, term_dict, 0,
1032 grep_info, &max_pos, 0, grep_handle);
1034 logf (LOG_WARN, "dict_lookup_grep fail %d", r);
1036 case 1: /* right truncation */
1037 term_dict[j++] = '(';
1038 if (!term_100 (zh->reg->zebra_maps, reg_type,
1039 &termp, term_dict + j, space_split, term_dst))
1041 strcat (term_dict, ".*)");
1042 dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1043 &max_pos, 0, grep_handle);
1045 case 2: /* keft truncation */
1046 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1047 if (!term_100 (zh->reg->zebra_maps, reg_type,
1048 &termp, term_dict + j, space_split, term_dst))
1050 strcat (term_dict, ")");
1051 dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1052 &max_pos, 0, grep_handle);
1054 case 3: /* left&right truncation */
1055 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1056 if (!term_100 (zh->reg->zebra_maps, reg_type,
1057 &termp, term_dict + j, space_split, term_dst))
1059 strcat (term_dict, ".*)");
1060 dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1061 &max_pos, 0, grep_handle);
1065 case 101: /* process # in term */
1066 term_dict[j++] = '(';
1067 if (!term_101 (zh->reg->zebra_maps, reg_type,
1068 &termp, term_dict + j, space_split, term_dst))
1070 strcat (term_dict, ")");
1071 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1072 &max_pos, 0, grep_handle);
1074 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d", r);
1076 case 102: /* Regexp-1 */
1077 term_dict[j++] = '(';
1078 if (!term_102 (zh->reg->zebra_maps, reg_type,
1079 &termp, term_dict + j, space_split, term_dst))
1081 strcat (term_dict, ")");
1082 logf (LOG_DEBUG, "Regexp-1 tolerance=%d", r);
1083 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1084 &max_pos, 0, grep_handle);
1086 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
1089 case 103: /* Regexp-2 */
1091 term_dict[j++] = '(';
1092 if (!term_103 (zh->reg->zebra_maps, reg_type,
1093 &termp, term_dict + j, &r, space_split, term_dst))
1095 strcat (term_dict, ")");
1096 logf (LOG_DEBUG, "Regexp-2 tolerance=%d", r);
1097 r = dict_lookup_grep (zh->reg->dict, term_dict, r, grep_info,
1098 &max_pos, 2, grep_handle);
1100 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
1103 case 104: /* process # and ! in term */
1104 term_dict[j++] = '(';
1105 if (!term_104 (zh->reg->zebra_maps, reg_type,
1106 &termp, term_dict + j, space_split, term_dst))
1108 strcat (term_dict, ")");
1109 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1110 &max_pos, 0, grep_handle);
1112 logf (LOG_WARN, "dict_lookup_grep err, trunc=#/!: %d", r);
1114 case 105: /* process * and ! in term */
1115 term_dict[j++] = '(';
1116 if (!term_105 (zh->reg->zebra_maps, reg_type,
1117 &termp, term_dict + j, space_split, term_dst, 1))
1119 strcat (term_dict, ")");
1120 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1121 &max_pos, 0, grep_handle);
1123 logf (LOG_WARN, "dict_lookup_grep err, trunc=*/!: %d", r);
1125 case 106: /* process * and ! in term */
1126 term_dict[j++] = '(';
1127 if (!term_105 (zh->reg->zebra_maps, reg_type,
1128 &termp, term_dict + j, space_split, term_dst, 0))
1130 strcat (term_dict, ")");
1131 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1132 &max_pos, 0, grep_handle);
1134 logf (LOG_WARN, "dict_lookup_grep err, trunc=*/!: %d", r);
1139 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1144 /* convert APT search term to UTF8 */
1145 static int zapt_term_to_utf8 (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1149 Z_Term *term = zapt->term;
1151 switch (term->which)
1153 case Z_Term_general:
1154 if (zh->iconv_to_utf8 != 0)
1156 char *inbuf = term->u.general->buf;
1157 size_t inleft = term->u.general->len;
1158 char *outbuf = termz;
1159 size_t outleft = IT_MAX_WORD-1;
1162 ret = yaz_iconv(zh->iconv_to_utf8, &inbuf, &inleft,
1164 if (ret == (size_t)(-1))
1166 ret = yaz_iconv(zh->iconv_to_utf8, 0, 0, 0, 0);
1174 sizez = term->u.general->len;
1175 if (sizez > IT_MAX_WORD-1)
1176 sizez = IT_MAX_WORD-1;
1177 memcpy (termz, term->u.general->buf, sizez);
1178 termz[sizez] = '\0';
1181 case Z_Term_characterString:
1182 sizez = strlen(term->u.characterString);
1183 if (sizez > IT_MAX_WORD-1)
1184 sizez = IT_MAX_WORD-1;
1185 memcpy (termz, term->u.characterString, sizez);
1186 termz[sizez] = '\0';
1195 /* convert APT SCAN term to internal cmap */
1196 static int trans_scan_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1197 char *termz, int reg_type)
1199 char termz0[IT_MAX_WORD];
1201 if (zapt_term_to_utf8(zh, zapt, termz0))
1202 return -1; /* error */
1206 const char *cp = (const char *) termz0;
1207 const char *cp_end = cp + strlen(cp);
1210 const char *space_map = NULL;
1213 while ((len = (cp_end - cp)) > 0)
1215 map = zebra_maps_input (zh->reg->zebra_maps, reg_type, &cp, len);
1216 if (**map == *CHR_SPACE)
1221 for (src = space_map; *src; src++)
1224 for (src = *map; *src; src++)
1233 static RSET rpn_prox (ZebraHandle zh, RSET *rset, int rset_no,
1234 int ordered, int exclusion, int relation, int distance)
1239 struct it_key **buf;
1241 char prox_term[1024];
1242 int length_prox_term = 0;
1243 int min_nn = 10000000;
1245 int term_type = Z_Term_characterString;
1246 const char *flags = NULL;
1248 rsfd = (RSFD *) xmalloc (sizeof(*rsfd)*rset_no);
1249 more = (int *) xmalloc (sizeof(*more)*rset_no);
1250 buf = (struct it_key **) xmalloc (sizeof(*buf)*rset_no);
1253 for (i = 0; i<rset_no; i++)
1256 for (j = 0; j<rset[i]->no_rset_terms; j++)
1258 const char *nflags = rset[i]->rset_terms[j]->flags;
1259 char *term = rset[i]->rset_terms[j]->name;
1260 int lterm = strlen(term);
1261 if (lterm + length_prox_term < sizeof(prox_term)-1)
1263 if (length_prox_term)
1264 prox_term[length_prox_term++] = ' ';
1265 strcpy (prox_term + length_prox_term, term);
1266 length_prox_term += lterm;
1268 if (min_nn > rset[i]->rset_terms[j]->nn)
1269 min_nn = rset[i]->rset_terms[j]->nn;
1271 term_type = rset[i]->rset_terms[j]->type;
1273 /* only if all term types are of type characterString .. */
1274 /* the resulting term is of that type */
1275 if (term_type != Z_Term_characterString)
1276 term_type = Z_Term_general;
1279 for (i = 0; i<rset_no; i++)
1284 for (i = 0; i<rset_no; i++)
1286 buf[i] = (struct it_key *) xmalloc (sizeof(**buf));
1287 rsfd[i] = rset_open (rset[i], RSETF_READ);
1288 if (!(more[i] = rset_read (rset[i], rsfd[i], buf[i], &term_index)))
1293 /* at least one is empty ... return null set */
1294 rset_null_parms parms;
1296 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1298 parms.rset_term->nn = 0;
1299 result = rset_create (rset_kind_null, &parms);
1301 else if (ordered && relation == 3 && exclusion == 0 && distance == 1)
1303 /* special proximity case = phrase search ... */
1304 rset_temp_parms parms;
1307 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1309 parms.rset_term->nn = min_nn;
1310 parms.cmp = key_compare_it;
1311 parms.key_size = sizeof (struct it_key);
1312 parms.temp_path = res_get (zh->res, "setTmpDir");
1313 result = rset_create (rset_kind_temp, &parms);
1314 rsfd_result = rset_open (result, RSETF_WRITE);
1318 for (i = 1; i<rset_no; i++)
1327 cmp = key_compare_it (buf[i], buf[i-1]);
1330 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1331 buf[i-1], &term_index);
1336 if (buf[i-1]->seqno+1 != buf[i]->seqno)
1338 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1339 buf[i-1], &term_index);
1345 more[i] = rset_read (rset[i], rsfd[i], buf[i],
1352 rset_write (result, rsfd_result, buf[0]);
1353 more[0] = rset_read (*rset, *rsfd, *buf, &term_index);
1356 rset_close (result, rsfd_result);
1358 else if (rset_no == 2)
1360 /* generic proximity case (two input sets only) ... */
1361 rset_temp_parms parms;
1364 yaz_log (LOG_LOG, "generic prox, dist=%d, relation=%d, ordered=%d"
1366 distance, relation, ordered, exclusion);
1367 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1369 parms.rset_term->nn = min_nn;
1370 parms.cmp = key_compare_it;
1371 parms.key_size = sizeof (struct it_key);
1372 parms.temp_path = res_get (zh->res, "setTmpDir");
1373 result = rset_create (rset_kind_temp, &parms);
1374 rsfd_result = rset_open (result, RSETF_WRITE);
1376 while (more[0] && more[1])
1378 int cmp = key_compare_it (buf[0], buf[1]);
1380 more[0] = rset_read (rset[0], rsfd[0], buf[0], &term_index);
1382 more[1] = rset_read (rset[1], rsfd[1], buf[1], &term_index);
1385 int sysno = buf[0]->sysno;
1389 seqno[n++] = buf[0]->seqno;
1390 while ((more[0] = rset_read (rset[0], rsfd[0], buf[0],
1392 sysno == buf[0]->sysno)
1394 seqno[n++] = buf[0]->seqno;
1397 for (i = 0; i<n; i++)
1399 int diff = buf[1]->seqno - seqno[i];
1400 int excl = exclusion;
1401 if (!ordered && diff < 0)
1406 if (diff < distance && diff >= 0)
1410 if (diff <= distance && diff >= 0)
1414 if (diff == distance && diff >= 0)
1418 if (diff >= distance && diff >= 0)
1422 if (diff > distance && diff >= 0)
1426 if (diff != distance && diff >= 0)
1432 rset_write (result, rsfd_result, buf[1]);
1436 } while ((more[1] = rset_read (rset[1], rsfd[1], buf[1],
1438 sysno == buf[1]->sysno);
1441 rset_close (result, rsfd_result);
1445 rset_null_parms parms;
1447 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1449 parms.rset_term->nn = 0;
1450 result = rset_create (rset_kind_null, &parms);
1452 for (i = 0; i<rset_no; i++)
1455 rset_close (rset[i], rsfd[i]);
1465 char *normalize_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1466 const char *termz, NMEM stream, unsigned reg_id)
1469 AttrType truncation;
1470 int truncation_value;
1473 attr_init (&truncation, zapt, 5);
1474 truncation_value = attr_find (&truncation, NULL);
1476 switch (truncation_value)
1496 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, ex_list,
1497 termz, strlen(termz));
1499 return nmem_strdup(stream, termz);
1502 char *buf = (char*) nmem_malloc (stream, wrbuf_len(wrbuf)+1);
1503 memcpy (buf, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1504 buf[wrbuf_len(wrbuf)] = '\0';
1509 static void grep_info_delete (struct grep_info *grep_info)
1512 xfree(grep_info->term_no);
1514 xfree (grep_info->isam_p_buf);
1517 static int grep_info_prepare (ZebraHandle zh,
1518 Z_AttributesPlusTerm *zapt,
1519 struct grep_info *grep_info,
1524 int termset_value_numeric;
1525 const char *termset_value_string;
1528 grep_info->term_no = 0;
1530 grep_info->isam_p_size = 0;
1531 grep_info->isam_p_buf = NULL;
1533 grep_info->reg_type = reg_type;
1534 grep_info->termset = 0;
1538 attr_init (&termset, zapt, 8);
1539 termset_value_numeric =
1540 attr_find_ex (&termset, NULL, &termset_value_string);
1541 if (termset_value_numeric != -1)
1544 const char *termset_name = 0;
1545 if (termset_value_numeric != -2)
1548 sprintf (resname, "%d", termset_value_numeric);
1549 termset_name = resname;
1552 termset_name = termset_value_string;
1553 logf (LOG_LOG, "creating termset set %s", termset_name);
1554 grep_info->termset = resultSetAdd (zh, termset_name, 1);
1555 if (!grep_info->termset)
1558 zh->errString = nmem_strdup (stream, termset_name);
1566 static RSET rpn_search_APT_phrase (ZebraHandle zh,
1567 Z_AttributesPlusTerm *zapt,
1568 const char *termz_org,
1569 oid_value attributeSet,
1571 int reg_type, int complete_flag,
1572 const char *rank_type, int xpath_use,
1573 int num_bases, char **basenames)
1575 char term_dst[IT_MAX_WORD+1];
1576 RSET rset[60], result;
1578 struct grep_info grep_info;
1579 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1580 const char *termp = termz;
1583 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1587 logf (LOG_DEBUG, "APT_phrase termp=%s", termp);
1588 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1590 reg_type, complete_flag,
1591 num_bases, basenames,
1592 term_dst, rank_type,
1596 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1599 grep_info_delete (&grep_info);
1602 rset_null_parms parms;
1604 parms.rset_term = rset_term_create (termz, -1, rank_type,
1606 return rset_create (rset_kind_null, &parms);
1608 else if (rset_no == 1)
1610 result = rpn_prox (zh, rset, rset_no, 1, 0, 3, 1);
1611 for (i = 0; i<rset_no; i++)
1612 rset_delete (rset[i]);
1616 static RSET rpn_search_APT_or_list (ZebraHandle zh,
1617 Z_AttributesPlusTerm *zapt,
1618 const char *termz_org,
1619 oid_value attributeSet,
1621 int reg_type, int complete_flag,
1622 const char *rank_type,
1624 int num_bases, char **basenames)
1626 char term_dst[IT_MAX_WORD+1];
1627 RSET rset[60], result;
1629 struct grep_info grep_info;
1630 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1631 const char *termp = termz;
1633 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1637 logf (LOG_DEBUG, "APT_or_list termp=%s", termp);
1638 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1640 reg_type, complete_flag,
1641 num_bases, basenames,
1642 term_dst, rank_type,
1646 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1649 grep_info_delete (&grep_info);
1652 rset_null_parms parms;
1654 parms.rset_term = rset_term_create (termz, -1, rank_type,
1656 return rset_create (rset_kind_null, &parms);
1659 for (i = 1; i<rset_no; i++)
1661 rset_bool_parms bool_parms;
1663 bool_parms.rset_l = result;
1664 bool_parms.rset_r = rset[i];
1665 bool_parms.key_size = sizeof(struct it_key);
1666 bool_parms.cmp = key_compare_it;
1667 result = rset_create (rset_kind_or, &bool_parms);
1672 static RSET rpn_search_APT_and_list (ZebraHandle zh,
1673 Z_AttributesPlusTerm *zapt,
1674 const char *termz_org,
1675 oid_value attributeSet,
1677 int reg_type, int complete_flag,
1678 const char *rank_type,
1680 int num_bases, char **basenames)
1682 char term_dst[IT_MAX_WORD+1];
1683 RSET rset[60], result;
1685 struct grep_info grep_info;
1686 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1687 const char *termp = termz;
1689 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1693 logf (LOG_DEBUG, "APT_and_list termp=%s", termp);
1694 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1696 reg_type, complete_flag,
1697 num_bases, basenames,
1698 term_dst, rank_type,
1702 assert (rset[rset_no]);
1703 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1706 grep_info_delete (&grep_info);
1709 rset_null_parms parms;
1711 parms.rset_term = rset_term_create (termz, -1, rank_type,
1713 return rset_create (rset_kind_null, &parms);
1716 for (i = 1; i<rset_no; i++)
1718 rset_bool_parms bool_parms;
1720 bool_parms.rset_l = result;
1721 bool_parms.rset_r = rset[i];
1722 bool_parms.key_size = sizeof(struct it_key);
1723 bool_parms.cmp = key_compare_it;
1724 result = rset_create (rset_kind_and, &bool_parms);
1729 static int numeric_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1730 const char **term_sub,
1732 oid_value attributeSet,
1733 struct grep_info *grep_info,
1742 char *term_tmp = term_dict + strlen(term_dict);
1744 attr_init (&relation, zapt, 2);
1745 relation_value = attr_find (&relation, NULL);
1747 logf (LOG_DEBUG, "numeric relation value=%d", relation_value);
1749 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub, term_tmp, 1,
1752 term_value = atoi (term_tmp);
1753 switch (relation_value)
1756 logf (LOG_DEBUG, "Relation <");
1757 gen_regular_rel (term_tmp, term_value-1, 1);
1760 logf (LOG_DEBUG, "Relation <=");
1761 gen_regular_rel (term_tmp, term_value, 1);
1764 logf (LOG_DEBUG, "Relation >=");
1765 gen_regular_rel (term_tmp, term_value, 0);
1768 logf (LOG_DEBUG, "Relation >");
1769 gen_regular_rel (term_tmp, term_value+1, 0);
1773 logf (LOG_DEBUG, "Relation =");
1774 sprintf (term_tmp, "(0*%d)", term_value);
1776 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_tmp);
1777 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info, max_pos,
1780 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1781 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1785 static int numeric_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1786 const char **term_sub,
1787 oid_value attributeSet, struct grep_info *grep_info,
1788 int reg_type, int complete_flag,
1789 int num_bases, char **basenames,
1790 char *term_dst, int xpath_use, NMEM stream)
1792 char term_dict[2*IT_MAX_WORD+2];
1796 const char *use_string = 0;
1797 oid_value curAttributeSet = attributeSet;
1799 struct rpn_char_map_info rcmi;
1801 rpn_char_map_prepare (zh->reg, reg_type, &rcmi);
1802 attr_init (&use, zapt, 1);
1803 use_value = attr_find_ex (&use, &curAttributeSet, &use_string);
1805 if (use_value == -1)
1808 for (base_no = 0; base_no < num_bases; base_no++)
1811 data1_local_attribute id_xpath_attr;
1812 data1_local_attribute *local_attr;
1813 int max_pos, prefix_len = 0;
1816 if (use_value == -2) /* string attribute (assume IDXPATH/any) */
1818 use_value = xpath_use;
1819 attp.local_attributes = &id_xpath_attr;
1820 attp.attset_ordinal = VAL_IDXPATH;
1821 id_xpath_attr.next = 0;
1822 id_xpath_attr.local = use_value;
1824 else if (curAttributeSet == VAL_IDXPATH)
1826 attp.local_attributes = &id_xpath_attr;
1827 attp.attset_ordinal = VAL_IDXPATH;
1828 id_xpath_attr.next = 0;
1829 id_xpath_attr.local = use_value;
1833 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
1835 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1836 curAttributeSet, use_value, r);
1840 sprintf (val_str, "%d", use_value);
1841 zh->errString = nmem_strdup (stream, val_str);
1849 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
1851 zh->errCode = 109; /* Database unavailable */
1852 zh->errString = basenames[base_no];
1855 for (local_attr = attp.local_attributes; local_attr;
1856 local_attr = local_attr->next)
1862 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
1867 term_dict[prefix_len++] = '|';
1869 term_dict[prefix_len++] = '(';
1871 ord_len = key_SU_encode (ord, ord_buf);
1872 for (i = 0; i<ord_len; i++)
1874 term_dict[prefix_len++] = 1;
1875 term_dict[prefix_len++] = ord_buf[i];
1881 sprintf (val_str, "%d", use_value);
1883 zh->errString = nmem_strdup (stream, val_str);
1886 term_dict[prefix_len++] = ')';
1887 term_dict[prefix_len++] = 1;
1888 term_dict[prefix_len++] = reg_type;
1889 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1890 term_dict[prefix_len] = '\0';
1891 if (!numeric_relation (zh, zapt, &termp, term_dict,
1892 attributeSet, grep_info, &max_pos, reg_type,
1897 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1901 static RSET rpn_search_APT_numeric (ZebraHandle zh,
1902 Z_AttributesPlusTerm *zapt,
1904 oid_value attributeSet,
1906 int reg_type, int complete_flag,
1907 const char *rank_type, int xpath_use,
1908 int num_bases, char **basenames)
1910 char term_dst[IT_MAX_WORD+1];
1911 const char *termp = termz;
1912 RSET rset[60], result;
1913 int i, r, rset_no = 0;
1914 struct grep_info grep_info;
1916 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1920 logf (LOG_DEBUG, "APT_numeric termp=%s", termp);
1921 grep_info.isam_p_indx = 0;
1922 r = numeric_term (zh, zapt, &termp, attributeSet, &grep_info,
1923 reg_type, complete_flag, num_bases, basenames,
1924 term_dst, xpath_use,
1928 logf (LOG_DEBUG, "term: %s", term_dst);
1929 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1930 grep_info.isam_p_indx, term_dst,
1931 strlen(term_dst), rank_type,
1932 0 /* preserve position */,
1934 assert (rset[rset_no]);
1935 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1938 grep_info_delete (&grep_info);
1941 rset_null_parms parms;
1943 parms.rset_term = rset_term_create (term_dst, -1, rank_type,
1945 return rset_create (rset_kind_null, &parms);
1948 for (i = 1; i<rset_no; i++)
1950 rset_bool_parms bool_parms;
1952 bool_parms.rset_l = result;
1953 bool_parms.rset_r = rset[i];
1954 bool_parms.key_size = sizeof(struct it_key);
1955 bool_parms.cmp = key_compare_it;
1956 result = rset_create (rset_kind_and, &bool_parms);
1961 static RSET rpn_search_APT_local (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1963 oid_value attributeSet,
1965 const char *rank_type)
1970 rset_temp_parms parms;
1972 parms.rset_term = rset_term_create (termz, -1, rank_type,
1974 parms.cmp = key_compare_it;
1975 parms.key_size = sizeof (struct it_key);
1976 parms.temp_path = res_get (zh->res, "setTmpDir");
1977 result = rset_create (rset_kind_temp, &parms);
1978 rsfd = rset_open (result, RSETF_WRITE);
1980 key.sysno = atoi (termz);
1984 rset_write (result, rsfd, &key);
1985 rset_close (result, rsfd);
1989 static RSET rpn_sort_spec (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1990 oid_value attributeSet, NMEM stream,
1991 Z_SortKeySpecList *sort_sequence,
1992 const char *rank_type)
1994 rset_null_parms parms;
1996 int sort_relation_value;
1997 AttrType sort_relation_type;
2002 Z_AttributeElement *ae;
2007 attr_init (&sort_relation_type, zapt, 7);
2008 sort_relation_value = attr_find (&sort_relation_type, &attributeSet);
2010 attr_init (&use_type, zapt, 1);
2011 use_value = attr_find (&use_type, &attributeSet);
2013 if (!sort_sequence->specs)
2015 sort_sequence->num_specs = 10;
2016 sort_sequence->specs = (Z_SortKeySpec **)
2017 nmem_malloc (stream, sort_sequence->num_specs *
2018 sizeof(*sort_sequence->specs));
2019 for (i = 0; i<sort_sequence->num_specs; i++)
2020 sort_sequence->specs[i] = 0;
2022 if (zapt->term->which != Z_Term_general)
2025 i = atoi_n ((char *) zapt->term->u.general->buf,
2026 zapt->term->u.general->len);
2027 if (i >= sort_sequence->num_specs)
2029 sprintf (termz, "%d", i);
2031 oe.proto = PROTO_Z3950;
2032 oe.oclass = CLASS_ATTSET;
2033 oe.value = attributeSet;
2034 if (!oid_ent_to_oid (&oe, oid))
2037 sks = (Z_SortKeySpec *) nmem_malloc (stream, sizeof(*sks));
2038 sks->sortElement = (Z_SortElement *)
2039 nmem_malloc (stream, sizeof(*sks->sortElement));
2040 sks->sortElement->which = Z_SortElement_generic;
2041 sk = sks->sortElement->u.generic = (Z_SortKey *)
2042 nmem_malloc (stream, sizeof(*sk));
2043 sk->which = Z_SortKey_sortAttributes;
2044 sk->u.sortAttributes = (Z_SortAttributes *)
2045 nmem_malloc (stream, sizeof(*sk->u.sortAttributes));
2047 sk->u.sortAttributes->id = oid;
2048 sk->u.sortAttributes->list = (Z_AttributeList *)
2049 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list));
2050 sk->u.sortAttributes->list->num_attributes = 1;
2051 sk->u.sortAttributes->list->attributes = (Z_AttributeElement **)
2052 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list->attributes));
2053 ae = *sk->u.sortAttributes->list->attributes = (Z_AttributeElement *)
2054 nmem_malloc (stream, sizeof(**sk->u.sortAttributes->list->attributes));
2055 ae->attributeSet = 0;
2056 ae->attributeType = (int *)
2057 nmem_malloc (stream, sizeof(*ae->attributeType));
2058 *ae->attributeType = 1;
2059 ae->which = Z_AttributeValue_numeric;
2060 ae->value.numeric = (int *)
2061 nmem_malloc (stream, sizeof(*ae->value.numeric));
2062 *ae->value.numeric = use_value;
2064 sks->sortRelation = (int *)
2065 nmem_malloc (stream, sizeof(*sks->sortRelation));
2066 if (sort_relation_value == 1)
2067 *sks->sortRelation = Z_SortRelation_ascending;
2068 else if (sort_relation_value == 2)
2069 *sks->sortRelation = Z_SortRelation_descending;
2071 *sks->sortRelation = Z_SortRelation_ascending;
2073 sks->caseSensitivity = (int *)
2074 nmem_malloc (stream, sizeof(*sks->caseSensitivity));
2075 *sks->caseSensitivity = 0;
2077 sks->which = Z_SortKeySpec_null;
2078 sks->u.null = odr_nullval ();
2079 sort_sequence->specs[i] = sks;
2081 parms.rset_term = rset_term_create (termz, -1, rank_type,
2083 return rset_create (rset_kind_null, &parms);
2086 /* pop - moved to xpath.c */
2089 struct xpath_predicate {
2092 #define XPATH_PREDICATE_RELATION 1
2098 #define XPATH_PREDICATE_BOOLEAN 2
2101 struct xpath_predicate *left;
2102 struct xpath_predicate *right;
2107 struct xpath_location_step {
2109 struct xpath_predicate *predicate;
2114 static int parse_xpath(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2115 oid_value attributeSet,
2116 struct xpath_location_step *xpath, int max, NMEM mem)
2118 oid_value curAttributeSet = attributeSet;
2120 const char *use_string = 0;
2122 attr_init (&use, zapt, 1);
2123 attr_find_ex (&use, &curAttributeSet, &use_string);
2125 if (!use_string || *use_string != '/')
2128 return zebra_parse_xpath_str(use_string, xpath, max, mem);
2133 static RSET xpath_trunc(ZebraHandle zh, NMEM stream,
2134 int reg_type, const char *term, int use,
2135 oid_value curAttributeSet)
2138 struct grep_info grep_info;
2139 char term_dict[2048];
2142 int ord = zebraExplain_lookupSU (zh->reg->zei, curAttributeSet, use);
2143 int ord_len, i, r, max_pos;
2144 int term_type = Z_Term_characterString;
2145 const char *flags = "void";
2147 if (grep_info_prepare (zh, 0 /* zapt */, &grep_info, '0', stream))
2149 rset_null_parms parms;
2151 parms.rset_term = rset_term_create (term, strlen(term),
2153 parms.rset_term->nn = 0;
2154 return rset_create (rset_kind_null, &parms);
2159 rset_null_parms parms;
2161 parms.rset_term = rset_term_create (term, strlen(term),
2163 parms.rset_term->nn = 0;
2164 return rset_create (rset_kind_null, &parms);
2167 term_dict[prefix_len++] = '|';
2169 term_dict[prefix_len++] = '(';
2171 ord_len = key_SU_encode (ord, ord_buf);
2172 for (i = 0; i<ord_len; i++)
2174 term_dict[prefix_len++] = 1;
2175 term_dict[prefix_len++] = ord_buf[i];
2177 term_dict[prefix_len++] = ')';
2178 term_dict[prefix_len++] = 1;
2179 term_dict[prefix_len++] = reg_type;
2181 strcpy (term_dict+prefix_len, term);
2183 grep_info.isam_p_indx = 0;
2184 r = dict_lookup_grep (zh->reg->dict, term_dict, 0,
2185 &grep_info, &max_pos, 0, grep_handle);
2186 yaz_log (LOG_LOG, "%s %d positions", term,
2187 grep_info.isam_p_indx);
2188 rset = rset_trunc (zh, grep_info.isam_p_buf,
2189 grep_info.isam_p_indx, term, strlen(term),
2190 flags, 1, term_type);
2191 grep_info_delete (&grep_info);
2195 static RSET rpn_search_xpath (ZebraHandle zh,
2196 oid_value attributeSet,
2197 int num_bases, char **basenames,
2198 NMEM stream, const char *rank_type, RSET rset,
2199 int xpath_len, struct xpath_location_step *xpath)
2201 oid_value curAttributeSet = attributeSet;
2208 yaz_log (LOG_LOG, "len=%d", xpath_len);
2209 for (i = 0; i<xpath_len; i++)
2211 yaz_log (LOG_LOG, "XPATH %d %s", i, xpath[i].part);
2215 curAttributeSet = VAL_IDXPATH;
2225 a[@attr=value]/b[@other=othervalue]
2227 /e/@a val range(e/,range(@a,freetext(w,1015,val),@a),e/)
2228 /a/b val range(b/a/,freetext(w,1016,val),b/a/)
2229 /a/b/@c val range(b/a/,range(@c,freetext(w,1016,val),@c),b/a/)
2230 /a/b[@c=y] val range(b/a/,freetext(w,1016,val),b/a/,@c=y)
2231 /a[@c=y]/b val range(a/,range(b/a/,freetext(w,1016,val),b/a/),a/,@c=y)
2232 /a[@c=x]/b[@c=y] range(a/,range(b/a/,freetext(w,1016,val),b/a/,@c=y),a/,@c=x)
2236 dict_grep_cmap (zh->reg->dict, 0, 0);
2238 for (base_no = 0; base_no < num_bases; base_no++)
2240 int level = xpath_len;
2243 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
2245 zh->errCode = 109; /* Database unavailable */
2246 zh->errString = basenames[base_no];
2249 while (--level >= 0)
2251 char xpath_rev[128];
2253 rset_between_parms parms;
2254 RSET rset_start_tag = 0, rset_end_tag = 0, rset_attr = 0;
2258 for (i = level; i >= 1; --i)
2260 const char *cp = xpath[i].part;
2266 memcpy (xpath_rev + len, "[^/]*", 5);
2269 else if (*cp == ' ')
2272 xpath_rev[len++] = 1;
2273 xpath_rev[len++] = ' ';
2277 xpath_rev[len++] = *cp;
2278 xpath_rev[len++] = '/';
2280 else if (i == 1) /* // case */
2282 xpath_rev[len++] = '.';
2283 xpath_rev[len++] = '*';
2288 if (xpath[level].predicate &&
2289 xpath[level].predicate->which == XPATH_PREDICATE_RELATION &&
2290 xpath[level].predicate->u.relation.name[0])
2292 char predicate_str[128];
2294 strcpy (predicate_str,
2295 xpath[level].predicate->u.relation.name+1);
2296 if (xpath[level].predicate->u.relation.value)
2298 strcat (predicate_str, "=");
2299 strcat (predicate_str,
2300 xpath[level].predicate->u.relation.value);
2302 rset_attr = xpath_trunc (
2303 zh, stream, '0', predicate_str, 3, curAttributeSet);
2310 yaz_log (LOG_LOG, "xpath_rev (%d) = %s", level, xpath_rev);
2311 if (strlen(xpath_rev))
2313 rset_start_tag = xpath_trunc(zh, stream,
2314 '0', xpath_rev, 1, curAttributeSet);
2316 rset_end_tag = xpath_trunc(zh, stream,
2317 '0', xpath_rev, 2, curAttributeSet);
2319 parms.key_size = sizeof(struct it_key);
2320 parms.cmp = key_compare_it;
2321 parms.rset_l = rset_start_tag;
2322 parms.rset_m = rset;
2323 parms.rset_r = rset_end_tag;
2324 parms.rset_attr = rset_attr;
2325 parms.printer = key_print_it;
2326 rset = rset_create (rset_kind_between, &parms);
2337 static RSET rpn_search_APT (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2338 oid_value attributeSet, NMEM stream,
2339 Z_SortKeySpecList *sort_sequence,
2340 int num_bases, char **basenames)
2343 char *search_type = NULL;
2344 char rank_type[128];
2347 char termz[IT_MAX_WORD+1];
2351 struct xpath_location_step xpath[10];
2353 zebra_maps_attr (zh->reg->zebra_maps, zapt, ®_id, &search_type,
2354 rank_type, &complete_flag, &sort_flag);
2356 logf (LOG_DEBUG, "reg_id=%c", reg_id);
2357 logf (LOG_DEBUG, "complete_flag=%d", complete_flag);
2358 logf (LOG_DEBUG, "search_type=%s", search_type);
2359 logf (LOG_DEBUG, "rank_type=%s", rank_type);
2361 if (zapt_term_to_utf8(zh, zapt, termz))
2365 return rpn_sort_spec (zh, zapt, attributeSet, stream, sort_sequence,
2367 xpath_len = parse_xpath(zh, zapt, attributeSet, xpath, 10, stream);
2371 if (xpath[xpath_len-1].part[0] == '@')
2375 if (!strcmp (search_type, "phrase"))
2377 rset = rpn_search_APT_phrase (zh, zapt, termz, attributeSet, stream,
2378 reg_id, complete_flag, rank_type,
2380 num_bases, basenames);
2382 else if (!strcmp (search_type, "and-list"))
2384 rset = rpn_search_APT_and_list (zh, zapt, termz, attributeSet, stream,
2385 reg_id, complete_flag, rank_type,
2387 num_bases, basenames);
2389 else if (!strcmp (search_type, "or-list"))
2391 rset = rpn_search_APT_or_list (zh, zapt, termz, attributeSet, stream,
2392 reg_id, complete_flag, rank_type,
2394 num_bases, basenames);
2396 else if (!strcmp (search_type, "local"))
2398 rset = rpn_search_APT_local (zh, zapt, termz, attributeSet, stream,
2401 else if (!strcmp (search_type, "numeric"))
2403 rset = rpn_search_APT_numeric (zh, zapt, termz, attributeSet, stream,
2404 reg_id, complete_flag, rank_type,
2406 num_bases, basenames);
2408 else if (!strcmp (search_type, "always"))
2414 return rpn_search_xpath (zh, attributeSet, num_bases, basenames,
2415 stream, rank_type, rset, xpath_len, xpath);
2418 static RSET rpn_search_structure (ZebraHandle zh, Z_RPNStructure *zs,
2419 oid_value attributeSet, NMEM stream,
2420 Z_SortKeySpecList *sort_sequence,
2421 int num_bases, char **basenames)
2424 if (zs->which == Z_RPNStructure_complex)
2426 Z_Operator *zop = zs->u.complex->roperator;
2427 rset_bool_parms bool_parms;
2429 bool_parms.rset_l = rpn_search_structure (zh, zs->u.complex->s1,
2430 attributeSet, stream,
2432 num_bases, basenames);
2433 if (bool_parms.rset_l == NULL)
2435 bool_parms.rset_r = rpn_search_structure (zh, zs->u.complex->s2,
2436 attributeSet, stream,
2438 num_bases, basenames);
2439 if (bool_parms.rset_r == NULL)
2441 rset_delete (bool_parms.rset_l);
2444 bool_parms.key_size = sizeof(struct it_key);
2445 bool_parms.cmp = key_compare_it;
2449 case Z_Operator_and:
2450 r = rset_create (rset_kind_and, &bool_parms);
2453 r = rset_create (rset_kind_or, &bool_parms);
2455 case Z_Operator_and_not:
2456 r = rset_create (rset_kind_not, &bool_parms);
2458 case Z_Operator_prox:
2459 if (zop->u.prox->which != Z_ProximityOperator_known)
2464 if (*zop->u.prox->u.known != Z_ProxUnit_word)
2466 char *val = (char *) nmem_malloc (stream, 16);
2468 zh->errString = val;
2469 sprintf (val, "%d", *zop->u.prox->u.known);
2476 rsets[0] = bool_parms.rset_l;
2477 rsets[1] = bool_parms.rset_r;
2479 r = rpn_prox (zh, rsets, 2,
2480 *zop->u.prox->ordered,
2481 (!zop->u.prox->exclusion ? 0 :
2482 *zop->u.prox->exclusion),
2483 *zop->u.prox->relationType,
2484 *zop->u.prox->distance);
2485 rset_delete (rsets[0]);
2486 rset_delete (rsets[1]);
2494 else if (zs->which == Z_RPNStructure_simple)
2496 if (zs->u.simple->which == Z_Operand_APT)
2498 logf (LOG_DEBUG, "rpn_search_APT");
2499 r = rpn_search_APT (zh, zs->u.simple->u.attributesPlusTerm,
2500 attributeSet, stream, sort_sequence,
2501 num_bases, basenames);
2503 else if (zs->u.simple->which == Z_Operand_resultSetId)
2505 logf (LOG_DEBUG, "rpn_search_ref");
2506 r = resultSetRef (zh, zs->u.simple->u.resultSetId);
2509 r = rset_create (rset_kind_null, NULL);
2512 nmem_strdup (stream, zs->u.simple->u.resultSetId);
2533 RSET rpn_search (ZebraHandle zh, NMEM nmem,
2534 Z_RPNQuery *rpn, int num_bases, char **basenames,
2535 const char *setname,
2540 oid_value attributeSet;
2541 Z_SortKeySpecList *sort_sequence;
2545 zh->errString = NULL;
2548 sort_sequence = (Z_SortKeySpecList *)
2549 nmem_malloc (nmem, sizeof(*sort_sequence));
2550 sort_sequence->num_specs = 10;
2551 sort_sequence->specs = (Z_SortKeySpec **)
2552 nmem_malloc (nmem, sort_sequence->num_specs *
2553 sizeof(*sort_sequence->specs));
2554 for (i = 0; i<sort_sequence->num_specs; i++)
2555 sort_sequence->specs[i] = 0;
2557 attrset = oid_getentbyoid (rpn->attributeSetId);
2558 attributeSet = attrset->value;
2559 rset = rpn_search_structure (zh, rpn->RPNStructure, attributeSet,
2560 nmem, sort_sequence, num_bases, basenames);
2565 logf (LOG_DEBUG, "search error: %d", zh->errCode);
2567 for (i = 0; sort_sequence->specs[i]; i++)
2569 sort_sequence->num_specs = i;
2571 resultSetRank (zh, sset, rset);
2574 logf (LOG_DEBUG, "resultSetSortSingle in rpn_search");
2575 resultSetSortSingle (zh, nmem, sset, rset,
2576 sort_sequence, &sort_status);
2579 logf (LOG_DEBUG, "resultSetSortSingle status = %d", zh->errCode);
2585 struct scan_info_entry {
2591 struct scan_info_entry *list;
2597 static int scan_handle (char *name, const char *info, int pos, void *client)
2599 int len_prefix, idx;
2600 struct scan_info *scan_info = (struct scan_info *) client;
2602 len_prefix = strlen(scan_info->prefix);
2603 if (memcmp (name, scan_info->prefix, len_prefix))
2605 if (pos > 0) idx = scan_info->after - pos + scan_info->before;
2608 scan_info->list[idx].term = (char *)
2609 odr_malloc (scan_info->odr, strlen(name + len_prefix)+1);
2610 strcpy (scan_info->list[idx].term, name + len_prefix);
2611 assert (*info == sizeof(ISAMS_P));
2612 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAMS_P));
2616 static void scan_term_untrans (ZebraHandle zh, NMEM stream, int reg_type,
2617 char **dst, const char *src)
2619 char term_src[IT_MAX_WORD];
2620 char term_dst[IT_MAX_WORD];
2622 term_untrans (zh, reg_type, term_src, src);
2624 if (zh->iconv_from_utf8 != 0)
2627 char *inbuf = term_src;
2628 size_t inleft = strlen(term_src);
2629 char *outbuf = term_dst;
2630 size_t outleft = sizeof(term_dst)-1;
2633 ret = yaz_iconv (zh->iconv_from_utf8, &inbuf, &inleft,
2635 if (ret == (size_t)(-1))
2638 len = outbuf - term_dst;
2639 *dst = nmem_malloc (stream, len + 1);
2641 memcpy (*dst, term_dst, len);
2645 *dst = nmem_strdup (stream, term_src);
2648 static void count_set (RSET r, int *count)
2656 logf (LOG_DEBUG, "count_set");
2659 rfd = rset_open (r, RSETF_READ);
2660 while (rset_read (r, rfd, &key, &term_index))
2662 if (key.sysno != psysno)
2669 rset_close (r, rfd);
2670 logf (LOG_DEBUG, "%d keys, %d records", kno, *count);
2673 void rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
2674 oid_value attributeset,
2675 int num_bases, char **basenames,
2676 int *position, int *num_entries, ZebraScanEntry **list,
2677 int *is_partial, RSET limit_set, int return_zero)
2680 int pos = *position;
2681 int num = *num_entries;
2685 char termz[IT_MAX_WORD+20];
2688 struct scan_info *scan_info_array;
2689 ZebraScanEntry *glist;
2690 int ords[32], ord_no = 0;
2694 char *search_type = NULL;
2695 char rank_type[128];
2701 if (attributeset == VAL_NONE)
2702 attributeset = VAL_BIB1;
2707 int termset_value_numeric;
2708 const char *termset_value_string;
2709 attr_init (&termset, zapt, 8);
2710 termset_value_numeric =
2711 attr_find_ex (&termset, NULL, &termset_value_string);
2712 if (termset_value_numeric != -1)
2715 const char *termset_name = 0;
2717 if (termset_value_numeric != -2)
2720 sprintf (resname, "%d", termset_value_numeric);
2721 termset_name = resname;
2724 termset_name = termset_value_string;
2726 limit_set = resultSetRef (zh, termset_name);
2730 yaz_log (LOG_DEBUG, "position = %d, num = %d set=%d",
2731 pos, num, attributeset);
2733 attr_init (&use, zapt, 1);
2734 use_value = attr_find (&use, &attributeset);
2736 if (zebra_maps_attr (zh->reg->zebra_maps, zapt, ®_id, &search_type,
2737 rank_type, &complete_flag, &sort_flag))
2743 yaz_log (LOG_DEBUG, "use_value = %d", use_value);
2745 if (use_value == -1)
2747 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
2751 data1_local_attribute *local_attr;
2753 if ((r=att_getentbyatt (zh, &attp, attributeset, use_value)))
2755 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
2756 attributeset, use_value);
2760 sprintf (val_str, "%d", use_value);
2762 zh->errString = odr_strdup (stream, val_str);
2769 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
2771 zh->errString = basenames[base_no];
2772 zh->errCode = 109; /* Database unavailable */
2776 for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
2777 local_attr = local_attr->next)
2781 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
2784 ords[ord_no++] = ord;
2793 /* prepare dictionary scanning */
2796 scan_info_array = (struct scan_info *)
2797 odr_malloc (stream, ord_no * sizeof(*scan_info_array));
2798 for (i = 0; i < ord_no; i++)
2800 int j, prefix_len = 0;
2801 int before_tmp = before, after_tmp = after;
2802 struct scan_info *scan_info = scan_info_array + i;
2803 struct rpn_char_map_info rcmi;
2805 rpn_char_map_prepare (zh->reg, reg_id, &rcmi);
2807 scan_info->before = before;
2808 scan_info->after = after;
2809 scan_info->odr = stream;
2811 scan_info->list = (struct scan_info_entry *)
2812 odr_malloc (stream, (before+after) * sizeof(*scan_info->list));
2813 for (j = 0; j<before+after; j++)
2814 scan_info->list[j].term = NULL;
2816 prefix_len += key_SU_encode (ords[i], termz + prefix_len);
2817 termz[prefix_len++] = reg_id;
2818 termz[prefix_len] = 0;
2819 strcpy (scan_info->prefix, termz);
2821 if (trans_scan_term (zh, zapt, termz+prefix_len, reg_id))
2824 dict_scan (zh->reg->dict, termz, &before_tmp, &after_tmp,
2825 scan_info, scan_handle);
2827 glist = (ZebraScanEntry *)
2828 odr_malloc (stream, (before+after)*sizeof(*glist));
2830 /* consider terms after main term */
2831 for (i = 0; i < ord_no; i++)
2835 for (i = 0; i<after; i++)
2838 const char *mterm = NULL;
2842 for (j = 0; j < ord_no; j++)
2844 if (ptr[j] < before+after &&
2845 (tst=scan_info_array[j].list[ptr[j]].term) &&
2846 (!mterm || strcmp (tst, mterm) < 0))
2854 scan_term_untrans (zh, stream->mem, reg_id,
2855 &glist[i+before].term, mterm);
2856 rset = rset_trunc (zh, &scan_info_array[j0].list[ptr[j0]].isam_p, 1,
2857 glist[i+before].term, strlen(glist[i+before].term),
2858 NULL, 0, zapt->term->which);
2861 for (j = j0+1; j<ord_no; j++)
2863 if (ptr[j] < before+after &&
2864 (tst=scan_info_array[j].list[ptr[j]].term) &&
2865 !strcmp (tst, mterm))
2867 rset_bool_parms bool_parms;
2871 rset_trunc (zh, &scan_info_array[j].list[ptr[j]].isam_p, 1,
2872 glist[i+before].term,
2873 strlen(glist[i+before].term), NULL, 0,
2876 bool_parms.key_size = sizeof(struct it_key);
2877 bool_parms.cmp = key_compare_it;
2878 bool_parms.rset_l = rset;
2879 bool_parms.rset_r = rset2;
2881 rset = rset_create (rset_kind_or, &bool_parms);
2888 rset_bool_parms bool_parms;
2890 bool_parms.key_size = sizeof(struct it_key);
2891 bool_parms.cmp = key_compare_it;
2892 bool_parms.rset_l = rset;
2893 bool_parms.rset_r = rset_dup(limit_set);
2895 rset = rset_create (rset_kind_and, &bool_parms);
2897 count_set (rset, &glist[i+before].occurrences);
2902 *num_entries -= (after-i);
2906 /* consider terms before main term */
2907 for (i = 0; i<ord_no; i++)
2910 for (i = 0; i<before; i++)
2913 const char *mterm = NULL;
2917 for (j = 0; j <ord_no; j++)
2919 if (ptr[j] < before &&
2920 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2921 (!mterm || strcmp (tst, mterm) > 0))
2930 scan_term_untrans (zh, stream->mem, reg_id,
2931 &glist[before-1-i].term, mterm);
2934 (zh, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1,
2935 glist[before-1-i].term, strlen(glist[before-1-i].term),
2936 NULL, 0, zapt->term->which);
2940 for (j = j0+1; j<ord_no; j++)
2942 if (ptr[j] < before &&
2943 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2944 !strcmp (tst, mterm))
2946 rset_bool_parms bool_parms;
2949 rset2 = rset_trunc (zh,
2950 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1,
2951 glist[before-1-i].term,
2952 strlen(glist[before-1-i].term), NULL, 0,
2955 bool_parms.key_size = sizeof(struct it_key);
2956 bool_parms.cmp = key_compare_it;
2957 bool_parms.rset_l = rset;
2958 bool_parms.rset_r = rset2;
2960 rset = rset_create (rset_kind_or, &bool_parms);
2967 rset_bool_parms bool_parms;
2969 bool_parms.key_size = sizeof(struct it_key);
2970 bool_parms.cmp = key_compare_it;
2971 bool_parms.rset_l = rset;
2972 bool_parms.rset_r = rset_dup(limit_set);
2974 rset = rset_create (rset_kind_and, &bool_parms);
2976 count_set (rset, &glist[before-1-i].occurrences);
2986 *list = glist + i; /* list is set to first 'real' entry */
2988 logf (LOG_DEBUG, "position = %d, num_entries = %d",
2989 *position, *num_entries);
2991 logf (LOG_DEBUG, "scan error: %d", zh->errCode);