1 /* $Id: zrpn.c,v 1.132 2003-03-31 22:14:18 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[256];
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);
741 if (!term_100 (zh->reg->zebra_maps, reg_type,
742 term_sub, term_component,
743 space_split, term_dst))
745 logf (LOG_DEBUG, "Relation <=");
748 for (i = 0; term_component[i]; )
753 string_rel_add_char (&term_tmp, term_component, &j);
757 string_rel_add_char (&term_tmp, term_component, &i);
766 for (i = 0; term_component[i]; )
767 string_rel_add_char (&term_tmp, term_component, &i);
772 if (!term_100 (zh->reg->zebra_maps, reg_type,
773 term_sub, term_component, space_split, term_dst))
775 logf (LOG_DEBUG, "Relation >");
778 for (i = 0; term_component[i];)
783 string_rel_add_char (&term_tmp, term_component, &j);
788 string_rel_add_char (&term_tmp, term_component, &i);
796 for (i = 0; term_component[i];)
797 string_rel_add_char (&term_tmp, term_component, &i);
804 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub,
805 term_component, space_split, term_dst))
807 logf (LOG_DEBUG, "Relation >=");
810 for (i = 0; term_component[i];)
817 string_rel_add_char (&term_tmp, term_component, &j);
820 if (term_component[i+1])
824 string_rel_add_char (&term_tmp, term_component, &i);
828 string_rel_add_char (&term_tmp, term_component, &i);
840 logf (LOG_DEBUG, "Relation =");
841 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub,
842 term_component, space_split, term_dst))
844 strcat (term_tmp, "(");
845 strcat (term_tmp, term_component);
846 strcat (term_tmp, ")");
851 static int string_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
852 const char **term_sub,
853 oid_value attributeSet, NMEM stream,
854 struct grep_info *grep_info,
855 int reg_type, int complete_flag,
856 int num_bases, char **basenames,
857 char *term_dst, int xpath_use);
859 static RSET term_trunc (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
860 const char **term_sub,
861 oid_value attributeSet, NMEM stream,
862 struct grep_info *grep_info,
863 int reg_type, int complete_flag,
864 int num_bases, char **basenames,
866 const char *rank_type, int xpath_use)
869 grep_info->isam_p_indx = 0;
870 r = string_term (zh, zapt, term_sub, attributeSet, stream, grep_info,
871 reg_type, complete_flag, num_bases, basenames,
872 term_dst, xpath_use);
875 logf (LOG_DEBUG, "term: %s", term_dst);
876 return rset_trunc (zh, grep_info->isam_p_buf,
877 grep_info->isam_p_indx, term_dst,
878 strlen(term_dst), rank_type, 1 /* preserve pos */,
883 static int string_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
884 const char **term_sub,
885 oid_value attributeSet, NMEM stream,
886 struct grep_info *grep_info,
887 int reg_type, int complete_flag,
888 int num_bases, char **basenames,
889 char *term_dst, int xpath_use)
891 char term_dict[2*IT_MAX_WORD+4000];
894 int truncation_value;
897 const char *use_string = 0;
898 oid_value curAttributeSet = attributeSet;
900 struct rpn_char_map_info rcmi;
901 int space_split = complete_flag ? 0 : 1;
903 rpn_char_map_prepare (zh->reg, reg_type, &rcmi);
904 attr_init (&use, zapt, 1);
905 use_value = attr_find_ex (&use, &curAttributeSet, &use_string);
906 logf (LOG_DEBUG, "string_term, use value %d", use_value);
907 attr_init (&truncation, zapt, 5);
908 truncation_value = attr_find (&truncation, NULL);
909 logf (LOG_DEBUG, "truncation value %d", truncation_value);
911 if (use_value == -1) /* no attribute - assumy "any" */
913 for (base_no = 0; base_no < num_bases; base_no++)
916 data1_local_attribute id_xpath_attr;
917 data1_local_attribute *local_attr;
918 int max_pos, prefix_len = 0;
922 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
924 zh->errCode = 109; /* Database unavailable */
925 zh->errString = basenames[base_no];
928 if (use_value == -2) /* string attribute (assume IDXPATH/any) */
930 use_value = xpath_use;
931 attp.local_attributes = &id_xpath_attr;
932 attp.attset_ordinal = VAL_IDXPATH;
933 id_xpath_attr.next = 0;
934 id_xpath_attr.local = use_value;
936 else if (curAttributeSet == VAL_IDXPATH)
938 attp.local_attributes = &id_xpath_attr;
939 attp.attset_ordinal = VAL_IDXPATH;
940 id_xpath_attr.next = 0;
941 id_xpath_attr.local = use_value;
945 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
947 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
948 curAttributeSet, use_value, r);
951 /* set was found, but value wasn't defined */
953 sprintf (val_str, "%d", use_value);
955 zh->errString = nmem_strdup (stream, val_str);
960 struct oident oident;
962 oident.proto = PROTO_Z3950;
963 oident.oclass = CLASS_ATTSET;
964 oident.value = curAttributeSet;
965 oid_ent_to_oid (&oident, oid);
968 zh->errString = nmem_strdup (stream, oident.desc);
973 for (local_attr = attp.local_attributes; local_attr;
974 local_attr = local_attr->next)
980 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
985 term_dict[prefix_len++] = '|';
987 term_dict[prefix_len++] = '(';
989 ord_len = key_SU_encode (ord, ord_buf);
990 for (i = 0; i<ord_len; i++)
992 term_dict[prefix_len++] = 1;
993 term_dict[prefix_len++] = ord_buf[i];
999 sprintf (val_str, "%d", use_value);
1001 zh->errString = nmem_strdup (stream, val_str);
1004 term_dict[prefix_len++] = ')';
1005 term_dict[prefix_len++] = 1;
1006 term_dict[prefix_len++] = reg_type;
1007 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1008 term_dict[prefix_len] = '\0';
1010 switch (truncation_value)
1012 case -1: /* not specified */
1013 case 100: /* do not truncate */
1014 if (!string_relation (zh, zapt, &termp, term_dict,
1016 reg_type, space_split, term_dst))
1018 logf (LOG_LOG, "dict_lookup_grep: %s", term_dict+prefix_len);
1019 r = dict_lookup_grep (zh->reg->dict, term_dict, 0,
1020 grep_info, &max_pos, 0, grep_handle);
1022 logf (LOG_WARN, "dict_lookup_grep fail %d", r);
1024 case 1: /* right truncation */
1025 term_dict[j++] = '(';
1026 if (!term_100 (zh->reg->zebra_maps, reg_type,
1027 &termp, term_dict + j, space_split, term_dst))
1029 strcat (term_dict, ".*)");
1030 dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1031 &max_pos, 0, grep_handle);
1033 case 2: /* keft truncation */
1034 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1035 if (!term_100 (zh->reg->zebra_maps, reg_type,
1036 &termp, term_dict + j, space_split, term_dst))
1038 strcat (term_dict, ")");
1039 dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1040 &max_pos, 0, grep_handle);
1042 case 3: /* left&right truncation */
1043 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1044 if (!term_100 (zh->reg->zebra_maps, reg_type,
1045 &termp, term_dict + j, space_split, term_dst))
1047 strcat (term_dict, ".*)");
1048 dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1049 &max_pos, 0, grep_handle);
1053 case 101: /* process # in term */
1054 term_dict[j++] = '(';
1055 if (!term_101 (zh->reg->zebra_maps, reg_type,
1056 &termp, term_dict + j, space_split, term_dst))
1058 strcat (term_dict, ")");
1059 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1060 &max_pos, 0, grep_handle);
1062 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d", r);
1064 case 102: /* Regexp-1 */
1065 term_dict[j++] = '(';
1066 if (!term_102 (zh->reg->zebra_maps, reg_type,
1067 &termp, term_dict + j, space_split, term_dst))
1069 strcat (term_dict, ")");
1070 logf (LOG_DEBUG, "Regexp-1 tolerance=%d", r);
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=regular: %d",
1077 case 103: /* Regexp-2 */
1079 term_dict[j++] = '(';
1080 if (!term_103 (zh->reg->zebra_maps, reg_type,
1081 &termp, term_dict + j, &r, space_split, term_dst))
1083 strcat (term_dict, ")");
1084 logf (LOG_DEBUG, "Regexp-2 tolerance=%d", r);
1085 r = dict_lookup_grep (zh->reg->dict, term_dict, r, grep_info,
1086 &max_pos, 2, grep_handle);
1088 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
1091 case 104: /* process # and ! in term */
1092 term_dict[j++] = '(';
1093 if (!term_104 (zh->reg->zebra_maps, reg_type,
1094 &termp, term_dict + j, space_split, term_dst))
1096 strcat (term_dict, ")");
1097 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1098 &max_pos, 0, grep_handle);
1100 logf (LOG_WARN, "dict_lookup_grep err, trunc=#/!: %d", r);
1102 case 105: /* process * and ! in term */
1103 term_dict[j++] = '(';
1104 if (!term_105 (zh->reg->zebra_maps, reg_type,
1105 &termp, term_dict + j, space_split, term_dst, 1))
1107 strcat (term_dict, ")");
1108 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1109 &max_pos, 0, grep_handle);
1111 logf (LOG_WARN, "dict_lookup_grep err, trunc=*/!: %d", r);
1113 case 106: /* process * and ! in term */
1114 term_dict[j++] = '(';
1115 if (!term_105 (zh->reg->zebra_maps, reg_type,
1116 &termp, term_dict + j, space_split, term_dst, 0))
1118 strcat (term_dict, ")");
1119 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1120 &max_pos, 0, grep_handle);
1122 logf (LOG_WARN, "dict_lookup_grep err, trunc=*/!: %d", r);
1127 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1132 /* convert APT search term to UTF8 */
1133 static int zapt_term_to_utf8 (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1137 Z_Term *term = zapt->term;
1139 switch (term->which)
1141 case Z_Term_general:
1142 if (zh->iconv_to_utf8 != 0)
1144 char *inbuf = term->u.general->buf;
1145 size_t inleft = term->u.general->len;
1146 char *outbuf = termz;
1147 size_t outleft = IT_MAX_WORD-1;
1150 ret = yaz_iconv(zh->iconv_to_utf8, &inbuf, &inleft,
1152 if (ret == (size_t)(-1))
1154 ret = yaz_iconv(zh->iconv_to_utf8, 0, 0, 0, 0);
1162 sizez = term->u.general->len;
1163 if (sizez > IT_MAX_WORD-1)
1164 sizez = IT_MAX_WORD-1;
1165 memcpy (termz, term->u.general->buf, sizez);
1166 termz[sizez] = '\0';
1169 case Z_Term_characterString:
1170 sizez = strlen(term->u.characterString);
1171 if (sizez > IT_MAX_WORD-1)
1172 sizez = IT_MAX_WORD-1;
1173 memcpy (termz, term->u.characterString, sizez);
1174 termz[sizez] = '\0';
1183 /* convert APT SCAN term to internal cmap */
1184 static int trans_scan_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1185 char *termz, int reg_type)
1187 char termz0[IT_MAX_WORD];
1189 if (zapt_term_to_utf8(zh, zapt, termz0))
1190 return -1; /* error */
1194 const char *cp = (const char *) termz0;
1195 const char *cp_end = cp + strlen(cp);
1198 const char *space_map = NULL;
1201 while ((len = (cp_end - cp)) > 0)
1203 map = zebra_maps_input (zh->reg->zebra_maps, reg_type, &cp, len);
1204 if (**map == *CHR_SPACE)
1209 for (src = space_map; *src; src++)
1212 for (src = *map; *src; src++)
1221 static RSET rpn_prox (ZebraHandle zh, RSET *rset, int rset_no,
1222 int ordered, int exclusion, int relation, int distance)
1227 struct it_key **buf;
1229 char prox_term[1024];
1230 int length_prox_term = 0;
1231 int min_nn = 10000000;
1233 int term_type = Z_Term_characterString;
1234 const char *flags = NULL;
1236 rsfd = (RSFD *) xmalloc (sizeof(*rsfd)*rset_no);
1237 more = (int *) xmalloc (sizeof(*more)*rset_no);
1238 buf = (struct it_key **) xmalloc (sizeof(*buf)*rset_no);
1241 for (i = 0; i<rset_no; i++)
1244 for (j = 0; j<rset[i]->no_rset_terms; j++)
1246 const char *nflags = rset[i]->rset_terms[j]->flags;
1247 char *term = rset[i]->rset_terms[j]->name;
1248 int lterm = strlen(term);
1249 if (lterm + length_prox_term < sizeof(prox_term)-1)
1251 if (length_prox_term)
1252 prox_term[length_prox_term++] = ' ';
1253 strcpy (prox_term + length_prox_term, term);
1254 length_prox_term += lterm;
1256 if (min_nn > rset[i]->rset_terms[j]->nn)
1257 min_nn = rset[i]->rset_terms[j]->nn;
1259 term_type = rset[i]->rset_terms[j]->type;
1261 /* only if all term types are of type characterString .. */
1262 /* the resulting term is of that type */
1263 if (term_type != Z_Term_characterString)
1264 term_type = Z_Term_general;
1267 for (i = 0; i<rset_no; i++)
1272 for (i = 0; i<rset_no; i++)
1274 buf[i] = (struct it_key *) xmalloc (sizeof(**buf));
1275 rsfd[i] = rset_open (rset[i], RSETF_READ);
1276 if (!(more[i] = rset_read (rset[i], rsfd[i], buf[i], &term_index)))
1281 /* at least one is empty ... return null set */
1282 rset_null_parms parms;
1284 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1286 parms.rset_term->nn = 0;
1287 result = rset_create (rset_kind_null, &parms);
1289 else if (ordered && relation == 3 && exclusion == 0 && distance == 1)
1291 /* special proximity case = phrase search ... */
1292 rset_temp_parms parms;
1295 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1297 parms.rset_term->nn = min_nn;
1298 parms.cmp = key_compare_it;
1299 parms.key_size = sizeof (struct it_key);
1300 parms.temp_path = res_get (zh->res, "setTmpDir");
1301 result = rset_create (rset_kind_temp, &parms);
1302 rsfd_result = rset_open (result, RSETF_WRITE);
1306 for (i = 1; i<rset_no; i++)
1315 cmp = key_compare_it (buf[i], buf[i-1]);
1318 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1319 buf[i-1], &term_index);
1324 if (buf[i-1]->seqno+1 != buf[i]->seqno)
1326 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1327 buf[i-1], &term_index);
1333 more[i] = rset_read (rset[i], rsfd[i], buf[i],
1340 rset_write (result, rsfd_result, buf[0]);
1341 more[0] = rset_read (*rset, *rsfd, *buf, &term_index);
1344 rset_close (result, rsfd_result);
1346 else if (rset_no == 2)
1348 /* generic proximity case (two input sets only) ... */
1349 rset_temp_parms parms;
1352 yaz_log (LOG_LOG, "generic prox, dist=%d, relation=%d, ordered=%d"
1354 distance, relation, ordered, exclusion);
1355 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1357 parms.rset_term->nn = min_nn;
1358 parms.cmp = key_compare_it;
1359 parms.key_size = sizeof (struct it_key);
1360 parms.temp_path = res_get (zh->res, "setTmpDir");
1361 result = rset_create (rset_kind_temp, &parms);
1362 rsfd_result = rset_open (result, RSETF_WRITE);
1364 while (more[0] && more[1])
1366 int cmp = key_compare_it (buf[0], buf[1]);
1368 more[0] = rset_read (rset[0], rsfd[0], buf[0], &term_index);
1370 more[1] = rset_read (rset[1], rsfd[1], buf[1], &term_index);
1373 int sysno = buf[0]->sysno;
1377 seqno[n++] = buf[0]->seqno;
1378 while ((more[0] = rset_read (rset[0], rsfd[0], buf[0],
1380 sysno == buf[0]->sysno)
1382 seqno[n++] = buf[0]->seqno;
1385 for (i = 0; i<n; i++)
1387 int diff = buf[1]->seqno - seqno[i];
1388 int excl = exclusion;
1389 if (!ordered && diff < 0)
1394 if (diff < distance && diff >= 0)
1398 if (diff <= distance && diff >= 0)
1402 if (diff == distance && diff >= 0)
1406 if (diff >= distance && diff >= 0)
1410 if (diff > distance && diff >= 0)
1414 if (diff != distance && diff >= 0)
1420 rset_write (result, rsfd_result, buf[1]);
1424 } while ((more[1] = rset_read (rset[1], rsfd[1], buf[1],
1426 sysno == buf[1]->sysno);
1429 rset_close (result, rsfd_result);
1433 rset_null_parms parms;
1435 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1437 parms.rset_term->nn = 0;
1438 result = rset_create (rset_kind_null, &parms);
1440 for (i = 0; i<rset_no; i++)
1443 rset_close (rset[i], rsfd[i]);
1453 char *normalize_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1454 const char *termz, NMEM stream, unsigned reg_id)
1457 AttrType truncation;
1458 int truncation_value;
1461 attr_init (&truncation, zapt, 5);
1462 truncation_value = attr_find (&truncation, NULL);
1464 switch (truncation_value)
1484 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, ex_list,
1485 termz, strlen(termz));
1487 return nmem_strdup(stream, termz);
1490 char *buf = (char*) nmem_malloc (stream, wrbuf_len(wrbuf)+1);
1491 memcpy (buf, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1492 buf[wrbuf_len(wrbuf)] = '\0';
1497 static void grep_info_delete (struct grep_info *grep_info)
1500 xfree(grep_info->term_no);
1502 xfree (grep_info->isam_p_buf);
1505 static int grep_info_prepare (ZebraHandle zh,
1506 Z_AttributesPlusTerm *zapt,
1507 struct grep_info *grep_info,
1512 int termset_value_numeric;
1513 const char *termset_value_string;
1516 grep_info->term_no = 0;
1518 grep_info->isam_p_size = 0;
1519 grep_info->isam_p_buf = NULL;
1521 grep_info->reg_type = reg_type;
1522 grep_info->termset = 0;
1526 attr_init (&termset, zapt, 8);
1527 termset_value_numeric =
1528 attr_find_ex (&termset, NULL, &termset_value_string);
1529 if (termset_value_numeric != -1)
1532 const char *termset_name = 0;
1533 if (termset_value_numeric != -2)
1536 sprintf (resname, "%d", termset_value_numeric);
1537 termset_name = resname;
1540 termset_name = termset_value_string;
1541 logf (LOG_LOG, "creating termset set %s", termset_name);
1542 grep_info->termset = resultSetAdd (zh, termset_name, 1);
1543 if (!grep_info->termset)
1546 zh->errString = nmem_strdup (stream, termset_name);
1554 static RSET rpn_search_APT_phrase (ZebraHandle zh,
1555 Z_AttributesPlusTerm *zapt,
1556 const char *termz_org,
1557 oid_value attributeSet,
1559 int reg_type, int complete_flag,
1560 const char *rank_type, int xpath_use,
1561 int num_bases, char **basenames)
1563 char term_dst[IT_MAX_WORD+1];
1564 RSET rset[60], result;
1566 struct grep_info grep_info;
1567 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1568 const char *termp = termz;
1571 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1575 logf (LOG_DEBUG, "APT_phrase termp=%s", termp);
1576 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1578 reg_type, complete_flag,
1579 num_bases, basenames,
1580 term_dst, rank_type,
1584 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1587 grep_info_delete (&grep_info);
1590 rset_null_parms parms;
1592 parms.rset_term = rset_term_create (termz, -1, rank_type,
1594 return rset_create (rset_kind_null, &parms);
1596 else if (rset_no == 1)
1598 result = rpn_prox (zh, rset, rset_no, 1, 0, 3, 1);
1599 for (i = 0; i<rset_no; i++)
1600 rset_delete (rset[i]);
1604 static RSET rpn_search_APT_or_list (ZebraHandle zh,
1605 Z_AttributesPlusTerm *zapt,
1606 const char *termz_org,
1607 oid_value attributeSet,
1609 int reg_type, int complete_flag,
1610 const char *rank_type,
1612 int num_bases, char **basenames)
1614 char term_dst[IT_MAX_WORD+1];
1615 RSET rset[60], result;
1617 struct grep_info grep_info;
1618 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1619 const char *termp = termz;
1621 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1625 logf (LOG_DEBUG, "APT_or_list termp=%s", termp);
1626 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1628 reg_type, complete_flag,
1629 num_bases, basenames,
1630 term_dst, rank_type,
1634 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1637 grep_info_delete (&grep_info);
1640 rset_null_parms parms;
1642 parms.rset_term = rset_term_create (termz, -1, rank_type,
1644 return rset_create (rset_kind_null, &parms);
1647 for (i = 1; i<rset_no; i++)
1649 rset_bool_parms bool_parms;
1651 bool_parms.rset_l = result;
1652 bool_parms.rset_r = rset[i];
1653 bool_parms.key_size = sizeof(struct it_key);
1654 bool_parms.cmp = key_compare_it;
1655 result = rset_create (rset_kind_or, &bool_parms);
1660 static RSET rpn_search_APT_and_list (ZebraHandle zh,
1661 Z_AttributesPlusTerm *zapt,
1662 const char *termz_org,
1663 oid_value attributeSet,
1665 int reg_type, int complete_flag,
1666 const char *rank_type,
1668 int num_bases, char **basenames)
1670 char term_dst[IT_MAX_WORD+1];
1671 RSET rset[60], result;
1673 struct grep_info grep_info;
1674 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1675 const char *termp = termz;
1677 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1681 logf (LOG_DEBUG, "APT_and_list termp=%s", termp);
1682 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1684 reg_type, complete_flag,
1685 num_bases, basenames,
1686 term_dst, rank_type,
1690 assert (rset[rset_no]);
1691 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1694 grep_info_delete (&grep_info);
1697 rset_null_parms parms;
1699 parms.rset_term = rset_term_create (termz, -1, rank_type,
1701 return rset_create (rset_kind_null, &parms);
1704 for (i = 1; i<rset_no; i++)
1706 rset_bool_parms bool_parms;
1708 bool_parms.rset_l = result;
1709 bool_parms.rset_r = rset[i];
1710 bool_parms.key_size = sizeof(struct it_key);
1711 bool_parms.cmp = key_compare_it;
1712 result = rset_create (rset_kind_and, &bool_parms);
1717 static int numeric_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1718 const char **term_sub,
1720 oid_value attributeSet,
1721 struct grep_info *grep_info,
1730 char *term_tmp = term_dict + strlen(term_dict);
1732 attr_init (&relation, zapt, 2);
1733 relation_value = attr_find (&relation, NULL);
1735 logf (LOG_DEBUG, "numeric relation value=%d", relation_value);
1737 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub, term_tmp, 1,
1740 term_value = atoi (term_tmp);
1741 switch (relation_value)
1744 logf (LOG_DEBUG, "Relation <");
1745 gen_regular_rel (term_tmp, term_value-1, 1);
1748 logf (LOG_DEBUG, "Relation <=");
1749 gen_regular_rel (term_tmp, term_value, 1);
1752 logf (LOG_DEBUG, "Relation >=");
1753 gen_regular_rel (term_tmp, term_value, 0);
1756 logf (LOG_DEBUG, "Relation >");
1757 gen_regular_rel (term_tmp, term_value+1, 0);
1761 logf (LOG_DEBUG, "Relation =");
1762 sprintf (term_tmp, "(0*%d)", term_value);
1764 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_tmp);
1765 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info, max_pos,
1768 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1769 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1773 static int numeric_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1774 const char **term_sub,
1775 oid_value attributeSet, struct grep_info *grep_info,
1776 int reg_type, int complete_flag,
1777 int num_bases, char **basenames,
1778 char *term_dst, int xpath_use, NMEM stream)
1780 char term_dict[2*IT_MAX_WORD+2];
1784 const char *use_string = 0;
1785 oid_value curAttributeSet = attributeSet;
1787 struct rpn_char_map_info rcmi;
1789 rpn_char_map_prepare (zh->reg, reg_type, &rcmi);
1790 attr_init (&use, zapt, 1);
1791 use_value = attr_find_ex (&use, &curAttributeSet, &use_string);
1793 if (use_value == -1)
1796 for (base_no = 0; base_no < num_bases; base_no++)
1799 data1_local_attribute id_xpath_attr;
1800 data1_local_attribute *local_attr;
1801 int max_pos, prefix_len = 0;
1804 if (use_value == -2) /* string attribute (assume IDXPATH/any) */
1806 use_value = xpath_use;
1807 attp.local_attributes = &id_xpath_attr;
1808 attp.attset_ordinal = VAL_IDXPATH;
1809 id_xpath_attr.next = 0;
1810 id_xpath_attr.local = use_value;
1812 else if (curAttributeSet == VAL_IDXPATH)
1814 attp.local_attributes = &id_xpath_attr;
1815 attp.attset_ordinal = VAL_IDXPATH;
1816 id_xpath_attr.next = 0;
1817 id_xpath_attr.local = use_value;
1821 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
1823 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1824 curAttributeSet, use_value, r);
1828 sprintf (val_str, "%d", use_value);
1829 zh->errString = nmem_strdup (stream, val_str);
1837 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
1839 zh->errCode = 109; /* Database unavailable */
1840 zh->errString = basenames[base_no];
1843 for (local_attr = attp.local_attributes; local_attr;
1844 local_attr = local_attr->next)
1850 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
1855 term_dict[prefix_len++] = '|';
1857 term_dict[prefix_len++] = '(';
1859 ord_len = key_SU_encode (ord, ord_buf);
1860 for (i = 0; i<ord_len; i++)
1862 term_dict[prefix_len++] = 1;
1863 term_dict[prefix_len++] = ord_buf[i];
1869 sprintf (val_str, "%d", use_value);
1871 zh->errString = nmem_strdup (stream, val_str);
1874 term_dict[prefix_len++] = ')';
1875 term_dict[prefix_len++] = 1;
1876 term_dict[prefix_len++] = reg_type;
1877 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1878 term_dict[prefix_len] = '\0';
1879 if (!numeric_relation (zh, zapt, &termp, term_dict,
1880 attributeSet, grep_info, &max_pos, reg_type,
1885 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1889 static RSET rpn_search_APT_numeric (ZebraHandle zh,
1890 Z_AttributesPlusTerm *zapt,
1892 oid_value attributeSet,
1894 int reg_type, int complete_flag,
1895 const char *rank_type, int xpath_use,
1896 int num_bases, char **basenames)
1898 char term_dst[IT_MAX_WORD+1];
1899 const char *termp = termz;
1900 RSET rset[60], result;
1901 int i, r, rset_no = 0;
1902 struct grep_info grep_info;
1904 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1908 logf (LOG_DEBUG, "APT_numeric termp=%s", termp);
1909 grep_info.isam_p_indx = 0;
1910 r = numeric_term (zh, zapt, &termp, attributeSet, &grep_info,
1911 reg_type, complete_flag, num_bases, basenames,
1912 term_dst, xpath_use,
1916 logf (LOG_DEBUG, "term: %s", term_dst);
1917 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1918 grep_info.isam_p_indx, term_dst,
1919 strlen(term_dst), rank_type,
1920 0 /* preserve position */,
1922 assert (rset[rset_no]);
1923 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1926 grep_info_delete (&grep_info);
1929 rset_null_parms parms;
1931 parms.rset_term = rset_term_create (term_dst, -1, rank_type,
1933 return rset_create (rset_kind_null, &parms);
1936 for (i = 1; i<rset_no; i++)
1938 rset_bool_parms bool_parms;
1940 bool_parms.rset_l = result;
1941 bool_parms.rset_r = rset[i];
1942 bool_parms.key_size = sizeof(struct it_key);
1943 bool_parms.cmp = key_compare_it;
1944 result = rset_create (rset_kind_and, &bool_parms);
1949 static RSET rpn_search_APT_local (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1951 oid_value attributeSet,
1953 const char *rank_type)
1958 rset_temp_parms parms;
1960 parms.rset_term = rset_term_create (termz, -1, rank_type,
1962 parms.cmp = key_compare_it;
1963 parms.key_size = sizeof (struct it_key);
1964 parms.temp_path = res_get (zh->res, "setTmpDir");
1965 result = rset_create (rset_kind_temp, &parms);
1966 rsfd = rset_open (result, RSETF_WRITE);
1968 key.sysno = atoi (termz);
1972 rset_write (result, rsfd, &key);
1973 rset_close (result, rsfd);
1977 static RSET rpn_sort_spec (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1978 oid_value attributeSet, NMEM stream,
1979 Z_SortKeySpecList *sort_sequence,
1980 const char *rank_type)
1982 rset_null_parms parms;
1984 int sort_relation_value;
1985 AttrType sort_relation_type;
1990 Z_AttributeElement *ae;
1995 attr_init (&sort_relation_type, zapt, 7);
1996 sort_relation_value = attr_find (&sort_relation_type, &attributeSet);
1998 attr_init (&use_type, zapt, 1);
1999 use_value = attr_find (&use_type, &attributeSet);
2001 if (!sort_sequence->specs)
2003 sort_sequence->num_specs = 10;
2004 sort_sequence->specs = (Z_SortKeySpec **)
2005 nmem_malloc (stream, sort_sequence->num_specs *
2006 sizeof(*sort_sequence->specs));
2007 for (i = 0; i<sort_sequence->num_specs; i++)
2008 sort_sequence->specs[i] = 0;
2010 if (zapt->term->which != Z_Term_general)
2013 i = atoi_n ((char *) zapt->term->u.general->buf,
2014 zapt->term->u.general->len);
2015 if (i >= sort_sequence->num_specs)
2017 sprintf (termz, "%d", i);
2019 oe.proto = PROTO_Z3950;
2020 oe.oclass = CLASS_ATTSET;
2021 oe.value = attributeSet;
2022 if (!oid_ent_to_oid (&oe, oid))
2025 sks = (Z_SortKeySpec *) nmem_malloc (stream, sizeof(*sks));
2026 sks->sortElement = (Z_SortElement *)
2027 nmem_malloc (stream, sizeof(*sks->sortElement));
2028 sks->sortElement->which = Z_SortElement_generic;
2029 sk = sks->sortElement->u.generic = (Z_SortKey *)
2030 nmem_malloc (stream, sizeof(*sk));
2031 sk->which = Z_SortKey_sortAttributes;
2032 sk->u.sortAttributes = (Z_SortAttributes *)
2033 nmem_malloc (stream, sizeof(*sk->u.sortAttributes));
2035 sk->u.sortAttributes->id = oid;
2036 sk->u.sortAttributes->list = (Z_AttributeList *)
2037 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list));
2038 sk->u.sortAttributes->list->num_attributes = 1;
2039 sk->u.sortAttributes->list->attributes = (Z_AttributeElement **)
2040 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list->attributes));
2041 ae = *sk->u.sortAttributes->list->attributes = (Z_AttributeElement *)
2042 nmem_malloc (stream, sizeof(**sk->u.sortAttributes->list->attributes));
2043 ae->attributeSet = 0;
2044 ae->attributeType = (int *)
2045 nmem_malloc (stream, sizeof(*ae->attributeType));
2046 *ae->attributeType = 1;
2047 ae->which = Z_AttributeValue_numeric;
2048 ae->value.numeric = (int *)
2049 nmem_malloc (stream, sizeof(*ae->value.numeric));
2050 *ae->value.numeric = use_value;
2052 sks->sortRelation = (int *)
2053 nmem_malloc (stream, sizeof(*sks->sortRelation));
2054 if (sort_relation_value == 1)
2055 *sks->sortRelation = Z_SortRelation_ascending;
2056 else if (sort_relation_value == 2)
2057 *sks->sortRelation = Z_SortRelation_descending;
2059 *sks->sortRelation = Z_SortRelation_ascending;
2061 sks->caseSensitivity = (int *)
2062 nmem_malloc (stream, sizeof(*sks->caseSensitivity));
2063 *sks->caseSensitivity = 0;
2065 sks->which = Z_SortKeySpec_null;
2066 sks->u.null = odr_nullval ();
2067 sort_sequence->specs[i] = sks;
2069 parms.rset_term = rset_term_create (termz, -1, rank_type,
2071 return rset_create (rset_kind_null, &parms);
2074 /* pop - moved to xpath.c */
2077 struct xpath_predicate {
2080 #define XPATH_PREDICATE_RELATION 1
2086 #define XPATH_PREDICATE_BOOLEAN 2
2089 struct xpath_predicate *left;
2090 struct xpath_predicate *right;
2095 struct xpath_location_step {
2097 struct xpath_predicate *predicate;
2102 static int parse_xpath(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2103 oid_value attributeSet,
2104 struct xpath_location_step *xpath, int max, NMEM mem)
2106 oid_value curAttributeSet = attributeSet;
2108 const char *use_string = 0;
2110 attr_init (&use, zapt, 1);
2111 attr_find_ex (&use, &curAttributeSet, &use_string);
2113 if (!use_string || *use_string != '/')
2116 return zebra_parse_xpath_str(use_string, xpath, max, mem);
2121 static RSET xpath_trunc(ZebraHandle zh, NMEM stream,
2122 int reg_type, const char *term, int use,
2123 oid_value curAttributeSet)
2126 struct grep_info grep_info;
2127 char term_dict[2048];
2130 int ord = zebraExplain_lookupSU (zh->reg->zei, curAttributeSet, use);
2131 int ord_len, i, r, max_pos;
2132 int term_type = Z_Term_characterString;
2133 const char *flags = "void";
2135 if (grep_info_prepare (zh, 0 /* zapt */, &grep_info, '0', stream))
2137 rset_null_parms parms;
2139 parms.rset_term = rset_term_create (term, strlen(term),
2141 parms.rset_term->nn = 0;
2142 return rset_create (rset_kind_null, &parms);
2147 rset_null_parms parms;
2149 parms.rset_term = rset_term_create (term, strlen(term),
2151 parms.rset_term->nn = 0;
2152 return rset_create (rset_kind_null, &parms);
2155 term_dict[prefix_len++] = '|';
2157 term_dict[prefix_len++] = '(';
2159 ord_len = key_SU_encode (ord, ord_buf);
2160 for (i = 0; i<ord_len; i++)
2162 term_dict[prefix_len++] = 1;
2163 term_dict[prefix_len++] = ord_buf[i];
2165 term_dict[prefix_len++] = ')';
2166 term_dict[prefix_len++] = 1;
2167 term_dict[prefix_len++] = reg_type;
2169 strcpy (term_dict+prefix_len, term);
2171 grep_info.isam_p_indx = 0;
2172 r = dict_lookup_grep (zh->reg->dict, term_dict, 0,
2173 &grep_info, &max_pos, 0, grep_handle);
2174 yaz_log (LOG_LOG, "%s %d positions", term,
2175 grep_info.isam_p_indx);
2176 rset = rset_trunc (zh, grep_info.isam_p_buf,
2177 grep_info.isam_p_indx, term, strlen(term),
2178 flags, 1, term_type);
2179 grep_info_delete (&grep_info);
2183 static RSET rpn_search_xpath (ZebraHandle zh,
2184 oid_value attributeSet,
2185 int num_bases, char **basenames,
2186 NMEM stream, const char *rank_type, RSET rset,
2187 int xpath_len, struct xpath_location_step *xpath)
2189 oid_value curAttributeSet = attributeSet;
2196 yaz_log (LOG_LOG, "len=%d", xpath_len);
2197 for (i = 0; i<xpath_len; i++)
2199 yaz_log (LOG_LOG, "XPATH %d %s", i, xpath[i].part);
2203 curAttributeSet = VAL_IDXPATH;
2213 a[@attr=value]/b[@other=othervalue]
2215 /e/@a val range(e/,range(@a,freetext(w,1015,val),@a),e/)
2216 /a/b val range(b/a/,freetext(w,1016,val),b/a/)
2217 /a/b/@c val range(b/a/,range(@c,freetext(w,1016,val),@c),b/a/)
2218 /a/b[@c=y] val range(b/a/,freetext(w,1016,val),b/a/,@c=y)
2219 /a[@c=y]/b val range(a/,range(b/a/,freetext(w,1016,val),b/a/),a/,@c=y)
2220 /a[@c=x]/b[@c=y] range(a/,range(b/a/,freetext(w,1016,val),b/a/,@c=y),a/,@c=x)
2224 dict_grep_cmap (zh->reg->dict, 0, 0);
2226 for (base_no = 0; base_no < num_bases; base_no++)
2228 int level = xpath_len;
2231 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
2233 zh->errCode = 109; /* Database unavailable */
2234 zh->errString = basenames[base_no];
2237 while (--level >= 0)
2239 char xpath_rev[128];
2241 rset_between_parms parms;
2242 RSET rset_start_tag = 0, rset_end_tag = 0, rset_attr = 0;
2246 for (i = level; i >= 1; --i)
2248 const char *cp = xpath[i].part;
2254 memcpy (xpath_rev + len, "[^/]*", 5);
2257 else if (*cp == ' ')
2260 xpath_rev[len++] = 1;
2261 xpath_rev[len++] = ' ';
2265 xpath_rev[len++] = *cp;
2266 xpath_rev[len++] = '/';
2268 else if (i == 1) /* // case */
2270 xpath_rev[len++] = '.';
2271 xpath_rev[len++] = '*';
2276 if (xpath[level].predicate &&
2277 xpath[level].predicate->which == XPATH_PREDICATE_RELATION &&
2278 xpath[level].predicate->u.relation.name[0])
2280 char predicate_str[128];
2282 strcpy (predicate_str,
2283 xpath[level].predicate->u.relation.name+1);
2284 if (xpath[level].predicate->u.relation.value)
2286 strcat (predicate_str, "=");
2287 strcat (predicate_str,
2288 xpath[level].predicate->u.relation.value);
2290 rset_attr = xpath_trunc (
2291 zh, stream, '0', predicate_str, 3, curAttributeSet);
2298 yaz_log (LOG_LOG, "xpath_rev (%d) = %s", level, xpath_rev);
2299 if (strlen(xpath_rev))
2301 rset_start_tag = xpath_trunc(zh, stream,
2302 '0', xpath_rev, 1, curAttributeSet);
2304 rset_end_tag = xpath_trunc(zh, stream,
2305 '0', xpath_rev, 2, curAttributeSet);
2307 parms.key_size = sizeof(struct it_key);
2308 parms.cmp = key_compare_it;
2309 parms.rset_l = rset_start_tag;
2310 parms.rset_m = rset;
2311 parms.rset_r = rset_end_tag;
2312 parms.rset_attr = rset_attr;
2313 parms.printer = key_print_it;
2314 rset = rset_create (rset_kind_between, &parms);
2325 static RSET rpn_search_APT (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2326 oid_value attributeSet, NMEM stream,
2327 Z_SortKeySpecList *sort_sequence,
2328 int num_bases, char **basenames)
2331 char *search_type = NULL;
2332 char rank_type[128];
2335 char termz[IT_MAX_WORD+1];
2339 struct xpath_location_step xpath[10];
2341 zebra_maps_attr (zh->reg->zebra_maps, zapt, ®_id, &search_type,
2342 rank_type, &complete_flag, &sort_flag);
2344 logf (LOG_DEBUG, "reg_id=%c", reg_id);
2345 logf (LOG_DEBUG, "complete_flag=%d", complete_flag);
2346 logf (LOG_DEBUG, "search_type=%s", search_type);
2347 logf (LOG_DEBUG, "rank_type=%s", rank_type);
2349 if (zapt_term_to_utf8(zh, zapt, termz))
2353 return rpn_sort_spec (zh, zapt, attributeSet, stream, sort_sequence,
2355 xpath_len = parse_xpath(zh, zapt, attributeSet, xpath, 10, stream);
2359 if (xpath[xpath_len-1].part[0] == '@')
2363 if (!strcmp (search_type, "phrase"))
2365 rset = rpn_search_APT_phrase (zh, zapt, termz, attributeSet, stream,
2366 reg_id, complete_flag, rank_type,
2368 num_bases, basenames);
2370 else if (!strcmp (search_type, "and-list"))
2372 rset = rpn_search_APT_and_list (zh, zapt, termz, attributeSet, stream,
2373 reg_id, complete_flag, rank_type,
2375 num_bases, basenames);
2377 else if (!strcmp (search_type, "or-list"))
2379 rset = rpn_search_APT_or_list (zh, zapt, termz, attributeSet, stream,
2380 reg_id, complete_flag, rank_type,
2382 num_bases, basenames);
2384 else if (!strcmp (search_type, "local"))
2386 rset = rpn_search_APT_local (zh, zapt, termz, attributeSet, stream,
2389 else if (!strcmp (search_type, "numeric"))
2391 rset = rpn_search_APT_numeric (zh, zapt, termz, attributeSet, stream,
2392 reg_id, complete_flag, rank_type,
2394 num_bases, basenames);
2396 else if (!strcmp (search_type, "always"))
2402 return rpn_search_xpath (zh, attributeSet, num_bases, basenames,
2403 stream, rank_type, rset, xpath_len, xpath);
2406 static RSET rpn_search_structure (ZebraHandle zh, Z_RPNStructure *zs,
2407 oid_value attributeSet, NMEM stream,
2408 Z_SortKeySpecList *sort_sequence,
2409 int num_bases, char **basenames)
2412 if (zs->which == Z_RPNStructure_complex)
2414 Z_Operator *zop = zs->u.complex->roperator;
2415 rset_bool_parms bool_parms;
2417 bool_parms.rset_l = rpn_search_structure (zh, zs->u.complex->s1,
2418 attributeSet, stream,
2420 num_bases, basenames);
2421 if (bool_parms.rset_l == NULL)
2423 bool_parms.rset_r = rpn_search_structure (zh, zs->u.complex->s2,
2424 attributeSet, stream,
2426 num_bases, basenames);
2427 if (bool_parms.rset_r == NULL)
2429 rset_delete (bool_parms.rset_l);
2432 bool_parms.key_size = sizeof(struct it_key);
2433 bool_parms.cmp = key_compare_it;
2437 case Z_Operator_and:
2438 r = rset_create (rset_kind_and, &bool_parms);
2441 r = rset_create (rset_kind_or, &bool_parms);
2443 case Z_Operator_and_not:
2444 r = rset_create (rset_kind_not, &bool_parms);
2446 case Z_Operator_prox:
2447 if (zop->u.prox->which != Z_ProximityOperator_known)
2452 if (*zop->u.prox->u.known != Z_ProxUnit_word)
2454 char *val = (char *) nmem_malloc (stream, 16);
2456 zh->errString = val;
2457 sprintf (val, "%d", *zop->u.prox->u.known);
2464 rsets[0] = bool_parms.rset_l;
2465 rsets[1] = bool_parms.rset_r;
2467 r = rpn_prox (zh, rsets, 2,
2468 *zop->u.prox->ordered,
2469 (!zop->u.prox->exclusion ? 0 :
2470 *zop->u.prox->exclusion),
2471 *zop->u.prox->relationType,
2472 *zop->u.prox->distance);
2473 rset_delete (rsets[0]);
2474 rset_delete (rsets[1]);
2482 else if (zs->which == Z_RPNStructure_simple)
2484 if (zs->u.simple->which == Z_Operand_APT)
2486 logf (LOG_DEBUG, "rpn_search_APT");
2487 r = rpn_search_APT (zh, zs->u.simple->u.attributesPlusTerm,
2488 attributeSet, stream, sort_sequence,
2489 num_bases, basenames);
2491 else if (zs->u.simple->which == Z_Operand_resultSetId)
2493 logf (LOG_DEBUG, "rpn_search_ref");
2494 r = resultSetRef (zh, zs->u.simple->u.resultSetId);
2497 r = rset_create (rset_kind_null, NULL);
2500 nmem_strdup (stream, zs->u.simple->u.resultSetId);
2521 RSET rpn_search (ZebraHandle zh, NMEM nmem,
2522 Z_RPNQuery *rpn, int num_bases, char **basenames,
2523 const char *setname,
2528 oid_value attributeSet;
2529 Z_SortKeySpecList *sort_sequence;
2533 zh->errString = NULL;
2536 sort_sequence = (Z_SortKeySpecList *)
2537 nmem_malloc (nmem, sizeof(*sort_sequence));
2538 sort_sequence->num_specs = 10;
2539 sort_sequence->specs = (Z_SortKeySpec **)
2540 nmem_malloc (nmem, sort_sequence->num_specs *
2541 sizeof(*sort_sequence->specs));
2542 for (i = 0; i<sort_sequence->num_specs; i++)
2543 sort_sequence->specs[i] = 0;
2545 attrset = oid_getentbyoid (rpn->attributeSetId);
2546 attributeSet = attrset->value;
2547 rset = rpn_search_structure (zh, rpn->RPNStructure, attributeSet,
2548 nmem, sort_sequence, num_bases, basenames);
2553 logf (LOG_DEBUG, "search error: %d", zh->errCode);
2555 for (i = 0; sort_sequence->specs[i]; i++)
2557 sort_sequence->num_specs = i;
2559 resultSetRank (zh, sset, rset);
2562 logf (LOG_DEBUG, "resultSetSortSingle in rpn_search");
2563 resultSetSortSingle (zh, nmem, sset, rset,
2564 sort_sequence, &sort_status);
2567 logf (LOG_DEBUG, "resultSetSortSingle status = %d", zh->errCode);
2573 struct scan_info_entry {
2579 struct scan_info_entry *list;
2585 static int scan_handle (char *name, const char *info, int pos, void *client)
2587 int len_prefix, idx;
2588 struct scan_info *scan_info = (struct scan_info *) client;
2590 len_prefix = strlen(scan_info->prefix);
2591 if (memcmp (name, scan_info->prefix, len_prefix))
2593 if (pos > 0) idx = scan_info->after - pos + scan_info->before;
2596 scan_info->list[idx].term = (char *)
2597 odr_malloc (scan_info->odr, strlen(name + len_prefix)+1);
2598 strcpy (scan_info->list[idx].term, name + len_prefix);
2599 assert (*info == sizeof(ISAMS_P));
2600 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAMS_P));
2604 static void scan_term_untrans (ZebraHandle zh, NMEM stream, int reg_type,
2605 char **dst, const char *src)
2607 char term_src[IT_MAX_WORD];
2608 char term_dst[IT_MAX_WORD];
2610 term_untrans (zh, reg_type, term_src, src);
2612 if (zh->iconv_from_utf8 != 0)
2615 char *inbuf = term_src;
2616 size_t inleft = strlen(term_src);
2617 char *outbuf = term_dst;
2618 size_t outleft = sizeof(term_dst)-1;
2621 ret = yaz_iconv (zh->iconv_from_utf8, &inbuf, &inleft,
2623 if (ret == (size_t)(-1))
2626 len = outbuf - term_dst;
2627 *dst = nmem_malloc (stream, len + 1);
2629 memcpy (*dst, term_dst, len);
2633 *dst = nmem_strdup (stream, term_src);
2636 static void count_set (RSET r, int *count)
2644 logf (LOG_DEBUG, "count_set");
2647 rfd = rset_open (r, RSETF_READ);
2648 while (rset_read (r, rfd, &key, &term_index))
2650 if (key.sysno != psysno)
2657 rset_close (r, rfd);
2658 logf (LOG_DEBUG, "%d keys, %d records", kno, *count);
2661 void rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
2662 oid_value attributeset,
2663 int num_bases, char **basenames,
2664 int *position, int *num_entries, ZebraScanEntry **list,
2665 int *is_partial, RSET limit_set, int return_zero)
2668 int pos = *position;
2669 int num = *num_entries;
2673 char termz[IT_MAX_WORD+20];
2676 struct scan_info *scan_info_array;
2677 ZebraScanEntry *glist;
2678 int ords[32], ord_no = 0;
2682 char *search_type = NULL;
2683 char rank_type[128];
2689 if (attributeset == VAL_NONE)
2690 attributeset = VAL_BIB1;
2695 int termset_value_numeric;
2696 const char *termset_value_string;
2697 attr_init (&termset, zapt, 8);
2698 termset_value_numeric =
2699 attr_find_ex (&termset, NULL, &termset_value_string);
2700 if (termset_value_numeric != -1)
2703 const char *termset_name = 0;
2705 if (termset_value_numeric != -2)
2708 sprintf (resname, "%d", termset_value_numeric);
2709 termset_name = resname;
2712 termset_name = termset_value_string;
2714 limit_set = resultSetRef (zh, termset_name);
2718 yaz_log (LOG_DEBUG, "position = %d, num = %d set=%d",
2719 pos, num, attributeset);
2721 attr_init (&use, zapt, 1);
2722 use_value = attr_find (&use, &attributeset);
2724 if (zebra_maps_attr (zh->reg->zebra_maps, zapt, ®_id, &search_type,
2725 rank_type, &complete_flag, &sort_flag))
2731 yaz_log (LOG_DEBUG, "use_value = %d", use_value);
2733 if (use_value == -1)
2735 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
2739 data1_local_attribute *local_attr;
2741 if ((r=att_getentbyatt (zh, &attp, attributeset, use_value)))
2743 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
2744 attributeset, use_value);
2748 sprintf (val_str, "%d", use_value);
2750 zh->errString = odr_strdup (stream, val_str);
2757 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
2759 zh->errString = basenames[base_no];
2760 zh->errCode = 109; /* Database unavailable */
2764 for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
2765 local_attr = local_attr->next)
2769 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
2772 ords[ord_no++] = ord;
2781 /* prepare dictionary scanning */
2784 scan_info_array = (struct scan_info *)
2785 odr_malloc (stream, ord_no * sizeof(*scan_info_array));
2786 for (i = 0; i < ord_no; i++)
2788 int j, prefix_len = 0;
2789 int before_tmp = before, after_tmp = after;
2790 struct scan_info *scan_info = scan_info_array + i;
2791 struct rpn_char_map_info rcmi;
2793 rpn_char_map_prepare (zh->reg, reg_id, &rcmi);
2795 scan_info->before = before;
2796 scan_info->after = after;
2797 scan_info->odr = stream;
2799 scan_info->list = (struct scan_info_entry *)
2800 odr_malloc (stream, (before+after) * sizeof(*scan_info->list));
2801 for (j = 0; j<before+after; j++)
2802 scan_info->list[j].term = NULL;
2804 prefix_len += key_SU_encode (ords[i], termz + prefix_len);
2805 termz[prefix_len++] = reg_id;
2806 termz[prefix_len] = 0;
2807 strcpy (scan_info->prefix, termz);
2809 if (trans_scan_term (zh, zapt, termz+prefix_len, reg_id))
2812 dict_scan (zh->reg->dict, termz, &before_tmp, &after_tmp,
2813 scan_info, scan_handle);
2815 glist = (ZebraScanEntry *)
2816 odr_malloc (stream, (before+after)*sizeof(*glist));
2818 /* consider terms after main term */
2819 for (i = 0; i < ord_no; i++)
2823 for (i = 0; i<after; i++)
2826 const char *mterm = NULL;
2830 for (j = 0; j < ord_no; j++)
2832 if (ptr[j] < before+after &&
2833 (tst=scan_info_array[j].list[ptr[j]].term) &&
2834 (!mterm || strcmp (tst, mterm) < 0))
2842 scan_term_untrans (zh, stream->mem, reg_id,
2843 &glist[i+before].term, mterm);
2844 rset = rset_trunc (zh, &scan_info_array[j0].list[ptr[j0]].isam_p, 1,
2845 glist[i+before].term, strlen(glist[i+before].term),
2846 NULL, 0, zapt->term->which);
2849 for (j = j0+1; j<ord_no; j++)
2851 if (ptr[j] < before+after &&
2852 (tst=scan_info_array[j].list[ptr[j]].term) &&
2853 !strcmp (tst, mterm))
2855 rset_bool_parms bool_parms;
2859 rset_trunc (zh, &scan_info_array[j].list[ptr[j]].isam_p, 1,
2860 glist[i+before].term,
2861 strlen(glist[i+before].term), NULL, 0,
2864 bool_parms.key_size = sizeof(struct it_key);
2865 bool_parms.cmp = key_compare_it;
2866 bool_parms.rset_l = rset;
2867 bool_parms.rset_r = rset2;
2869 rset = rset_create (rset_kind_or, &bool_parms);
2876 rset_bool_parms bool_parms;
2878 bool_parms.key_size = sizeof(struct it_key);
2879 bool_parms.cmp = key_compare_it;
2880 bool_parms.rset_l = rset;
2881 bool_parms.rset_r = rset_dup(limit_set);
2883 rset = rset_create (rset_kind_and, &bool_parms);
2885 count_set (rset, &glist[i+before].occurrences);
2890 *num_entries -= (after-i);
2894 /* consider terms before main term */
2895 for (i = 0; i<ord_no; i++)
2898 for (i = 0; i<before; i++)
2901 const char *mterm = NULL;
2905 for (j = 0; j <ord_no; j++)
2907 if (ptr[j] < before &&
2908 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2909 (!mterm || strcmp (tst, mterm) > 0))
2918 scan_term_untrans (zh, stream->mem, reg_id,
2919 &glist[before-1-i].term, mterm);
2922 (zh, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1,
2923 glist[before-1-i].term, strlen(glist[before-1-i].term),
2924 NULL, 0, zapt->term->which);
2928 for (j = j0+1; j<ord_no; j++)
2930 if (ptr[j] < before &&
2931 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2932 !strcmp (tst, mterm))
2934 rset_bool_parms bool_parms;
2937 rset2 = rset_trunc (zh,
2938 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1,
2939 glist[before-1-i].term,
2940 strlen(glist[before-1-i].term), NULL, 0,
2943 bool_parms.key_size = sizeof(struct it_key);
2944 bool_parms.cmp = key_compare_it;
2945 bool_parms.rset_l = rset;
2946 bool_parms.rset_r = rset2;
2948 rset = rset_create (rset_kind_or, &bool_parms);
2955 rset_bool_parms bool_parms;
2957 bool_parms.key_size = sizeof(struct it_key);
2958 bool_parms.cmp = key_compare_it;
2959 bool_parms.rset_l = rset;
2960 bool_parms.rset_r = rset_dup(limit_set);
2962 rset = rset_create (rset_kind_and, &bool_parms);
2964 count_set (rset, &glist[before-1-i].occurrences);
2974 *list = glist + i; /* list is set to first 'real' entry */
2976 logf (LOG_DEBUG, "position = %d, num_entries = %d",
2977 *position, *num_entries);
2979 logf (LOG_DEBUG, "scan error: %d", zh->errCode);