1 /* $Id: zrpn.c,v 1.128 2003-02-26 21:46:37 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
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 (1)", 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 (2)", 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 logf (LOG_LOG, "generic prox, dist = %d, relation = %d, ordered =%d, exclusion=%d",
1353 distance, relation, ordered, exclusion);
1354 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1356 parms.rset_term->nn = min_nn;
1357 parms.cmp = key_compare_it;
1358 parms.key_size = sizeof (struct it_key);
1359 parms.temp_path = res_get (zh->res, "setTmpDir");
1360 result = rset_create (rset_kind_temp, &parms);
1361 rsfd_result = rset_open (result, RSETF_WRITE);
1363 while (more[0] && more[1])
1365 int cmp = key_compare_it (buf[0], buf[1]);
1367 more[0] = rset_read (rset[0], rsfd[0], buf[0], &term_index);
1369 more[1] = rset_read (rset[1], rsfd[1], buf[1], &term_index);
1372 int sysno = buf[0]->sysno;
1376 seqno[n++] = buf[0]->seqno;
1377 while ((more[0] = rset_read (rset[0], rsfd[0], buf[0],
1379 sysno == buf[0]->sysno)
1381 seqno[n++] = buf[0]->seqno;
1384 for (i = 0; i<n; i++)
1386 int diff = buf[1]->seqno - seqno[i];
1387 int excl = exclusion;
1388 if (!ordered && diff < 0)
1393 if (diff < distance && diff >= 0)
1397 if (diff <= distance && diff >= 0)
1401 if (diff == distance && diff >= 0)
1405 if (diff >= distance && diff >= 0)
1409 if (diff > distance && diff >= 0)
1413 if (diff != distance && diff >= 0)
1419 rset_write (result, rsfd_result, buf[1]);
1423 } while ((more[1] = rset_read (rset[1], rsfd[1], buf[1],
1425 sysno == buf[1]->sysno);
1428 rset_close (result, rsfd_result);
1432 rset_null_parms parms;
1434 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1436 parms.rset_term->nn = 0;
1437 result = rset_create (rset_kind_null, &parms);
1439 for (i = 0; i<rset_no; i++)
1442 rset_close (rset[i], rsfd[i]);
1452 char *normalize_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1453 const char *termz, NMEM stream, unsigned reg_id)
1456 AttrType truncation;
1457 int truncation_value;
1460 attr_init (&truncation, zapt, 5);
1461 truncation_value = attr_find (&truncation, NULL);
1463 switch (truncation_value)
1483 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, ex_list,
1484 termz, strlen(termz));
1486 return nmem_strdup(stream, termz);
1489 char *buf = (char*) nmem_malloc (stream, wrbuf_len(wrbuf)+1);
1490 memcpy (buf, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1491 buf[wrbuf_len(wrbuf)] = '\0';
1496 static void grep_info_delete (struct grep_info *grep_info)
1499 xfree(grep_info->term_no);
1501 xfree (grep_info->isam_p_buf);
1504 static int grep_info_prepare (ZebraHandle zh,
1505 Z_AttributesPlusTerm *zapt,
1506 struct grep_info *grep_info,
1511 int termset_value_numeric;
1512 const char *termset_value_string;
1515 grep_info->term_no = 0;
1517 grep_info->isam_p_size = 0;
1518 grep_info->isam_p_buf = NULL;
1520 grep_info->reg_type = reg_type;
1521 grep_info->termset = 0;
1525 attr_init (&termset, zapt, 8);
1526 termset_value_numeric =
1527 attr_find_ex (&termset, NULL, &termset_value_string);
1528 if (termset_value_numeric != -1)
1531 const char *termset_name = 0;
1532 if (termset_value_numeric != -2)
1535 sprintf (resname, "%d", termset_value_numeric);
1536 termset_name = resname;
1539 termset_name = termset_value_string;
1540 logf (LOG_LOG, "creating termset set %s", termset_name);
1541 grep_info->termset = resultSetAdd (zh, termset_name, 1);
1542 if (!grep_info->termset)
1545 zh->errString = nmem_strdup (stream, termset_name);
1553 static RSET rpn_search_APT_phrase (ZebraHandle zh,
1554 Z_AttributesPlusTerm *zapt,
1555 const char *termz_org,
1556 oid_value attributeSet,
1558 int reg_type, int complete_flag,
1559 const char *rank_type, int xpath_use,
1560 int num_bases, char **basenames)
1562 char term_dst[IT_MAX_WORD+1];
1563 RSET rset[60], result;
1565 struct grep_info grep_info;
1566 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1567 const char *termp = termz;
1570 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1574 logf (LOG_DEBUG, "APT_phrase termp=%s", termp);
1575 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1577 reg_type, complete_flag,
1578 num_bases, basenames,
1579 term_dst, rank_type,
1583 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1586 grep_info_delete (&grep_info);
1589 rset_null_parms parms;
1591 parms.rset_term = rset_term_create (termz, -1, rank_type,
1593 return rset_create (rset_kind_null, &parms);
1595 else if (rset_no == 1)
1597 result = rpn_prox (zh, rset, rset_no, 1, 0, 3, 1);
1598 for (i = 0; i<rset_no; i++)
1599 rset_delete (rset[i]);
1603 static RSET rpn_search_APT_or_list (ZebraHandle zh,
1604 Z_AttributesPlusTerm *zapt,
1605 const char *termz_org,
1606 oid_value attributeSet,
1608 int reg_type, int complete_flag,
1609 const char *rank_type,
1611 int num_bases, char **basenames)
1613 char term_dst[IT_MAX_WORD+1];
1614 RSET rset[60], result;
1616 struct grep_info grep_info;
1617 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1618 const char *termp = termz;
1620 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1624 logf (LOG_DEBUG, "APT_or_list termp=%s", termp);
1625 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1627 reg_type, complete_flag,
1628 num_bases, basenames,
1629 term_dst, rank_type,
1633 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1636 grep_info_delete (&grep_info);
1639 rset_null_parms parms;
1641 parms.rset_term = rset_term_create (termz, -1, rank_type,
1643 return rset_create (rset_kind_null, &parms);
1646 for (i = 1; i<rset_no; i++)
1648 rset_bool_parms bool_parms;
1650 bool_parms.rset_l = result;
1651 bool_parms.rset_r = rset[i];
1652 bool_parms.key_size = sizeof(struct it_key);
1653 bool_parms.cmp = key_compare_it;
1654 result = rset_create (rset_kind_or, &bool_parms);
1659 static RSET rpn_search_APT_and_list (ZebraHandle zh,
1660 Z_AttributesPlusTerm *zapt,
1661 const char *termz_org,
1662 oid_value attributeSet,
1664 int reg_type, int complete_flag,
1665 const char *rank_type,
1667 int num_bases, char **basenames)
1669 char term_dst[IT_MAX_WORD+1];
1670 RSET rset[60], result;
1672 struct grep_info grep_info;
1673 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1674 const char *termp = termz;
1676 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1680 logf (LOG_DEBUG, "APT_and_list termp=%s", termp);
1681 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1683 reg_type, complete_flag,
1684 num_bases, basenames,
1685 term_dst, rank_type,
1689 assert (rset[rset_no]);
1690 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1693 grep_info_delete (&grep_info);
1696 rset_null_parms parms;
1698 parms.rset_term = rset_term_create (termz, -1, rank_type,
1700 return rset_create (rset_kind_null, &parms);
1703 for (i = 1; i<rset_no; i++)
1705 rset_bool_parms bool_parms;
1707 bool_parms.rset_l = result;
1708 bool_parms.rset_r = rset[i];
1709 bool_parms.key_size = sizeof(struct it_key);
1710 bool_parms.cmp = key_compare_it;
1711 result = rset_create (rset_kind_and, &bool_parms);
1716 static int numeric_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1717 const char **term_sub,
1719 oid_value attributeSet,
1720 struct grep_info *grep_info,
1729 char *term_tmp = term_dict + strlen(term_dict);
1731 attr_init (&relation, zapt, 2);
1732 relation_value = attr_find (&relation, NULL);
1734 logf (LOG_DEBUG, "numeric relation value=%d", relation_value);
1736 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub, term_tmp, 1,
1739 term_value = atoi (term_tmp);
1740 switch (relation_value)
1743 logf (LOG_DEBUG, "Relation <");
1744 gen_regular_rel (term_tmp, term_value-1, 1);
1747 logf (LOG_DEBUG, "Relation <=");
1748 gen_regular_rel (term_tmp, term_value, 1);
1751 logf (LOG_DEBUG, "Relation >=");
1752 gen_regular_rel (term_tmp, term_value, 0);
1755 logf (LOG_DEBUG, "Relation >");
1756 gen_regular_rel (term_tmp, term_value+1, 0);
1760 logf (LOG_DEBUG, "Relation =");
1761 sprintf (term_tmp, "(0*%d)", term_value);
1763 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_tmp);
1764 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info, max_pos,
1767 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1768 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1772 static int numeric_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1773 const char **term_sub,
1774 oid_value attributeSet, struct grep_info *grep_info,
1775 int reg_type, int complete_flag,
1776 int num_bases, char **basenames,
1777 char *term_dst, int xpath_use)
1779 char term_dict[2*IT_MAX_WORD+2];
1783 const char *use_string = 0;
1784 oid_value curAttributeSet = attributeSet;
1786 struct rpn_char_map_info rcmi;
1788 rpn_char_map_prepare (zh->reg, reg_type, &rcmi);
1789 attr_init (&use, zapt, 1);
1790 use_value = attr_find_ex (&use, &curAttributeSet, &use_string);
1792 if (use_value == -1)
1795 for (base_no = 0; base_no < num_bases; base_no++)
1798 data1_local_attribute id_xpath_attr;
1799 data1_local_attribute *local_attr;
1800 int max_pos, prefix_len = 0;
1803 if (use_value == -2) /* string attribute (assume IDXPATH/any) */
1805 use_value = xpath_use;
1806 attp.local_attributes = &id_xpath_attr;
1807 attp.attset_ordinal = VAL_IDXPATH;
1808 id_xpath_attr.next = 0;
1809 id_xpath_attr.local = use_value;
1811 else if (curAttributeSet == VAL_IDXPATH)
1813 attp.local_attributes = &id_xpath_attr;
1814 attp.attset_ordinal = VAL_IDXPATH;
1815 id_xpath_attr.next = 0;
1816 id_xpath_attr.local = use_value;
1820 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
1822 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1823 curAttributeSet, use_value, r);
1831 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
1833 zh->errCode = 109; /* Database unavailable */
1834 zh->errString = basenames[base_no];
1837 for (local_attr = attp.local_attributes; local_attr;
1838 local_attr = local_attr->next)
1844 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
1849 term_dict[prefix_len++] = '|';
1851 term_dict[prefix_len++] = '(';
1853 ord_len = key_SU_encode (ord, ord_buf);
1854 for (i = 0; i<ord_len; i++)
1856 term_dict[prefix_len++] = 1;
1857 term_dict[prefix_len++] = ord_buf[i];
1865 term_dict[prefix_len++] = ')';
1866 term_dict[prefix_len++] = 1;
1867 term_dict[prefix_len++] = reg_type;
1868 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1869 term_dict[prefix_len] = '\0';
1870 if (!numeric_relation (zh, zapt, &termp, term_dict,
1871 attributeSet, grep_info, &max_pos, reg_type,
1876 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1880 static RSET rpn_search_APT_numeric (ZebraHandle zh,
1881 Z_AttributesPlusTerm *zapt,
1883 oid_value attributeSet,
1885 int reg_type, int complete_flag,
1886 const char *rank_type, int xpath_use,
1887 int num_bases, char **basenames)
1889 char term_dst[IT_MAX_WORD+1];
1890 const char *termp = termz;
1891 RSET rset[60], result;
1892 int i, r, rset_no = 0;
1893 struct grep_info grep_info;
1895 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1899 logf (LOG_DEBUG, "APT_numeric termp=%s", termp);
1900 grep_info.isam_p_indx = 0;
1901 r = numeric_term (zh, zapt, &termp, attributeSet, &grep_info,
1902 reg_type, complete_flag, num_bases, basenames,
1903 term_dst, xpath_use);
1906 logf (LOG_DEBUG, "term: %s", term_dst);
1907 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1908 grep_info.isam_p_indx, term_dst,
1909 strlen(term_dst), rank_type,
1910 0 /* preserve position */,
1912 assert (rset[rset_no]);
1913 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1916 grep_info_delete (&grep_info);
1919 rset_null_parms parms;
1921 parms.rset_term = rset_term_create (term_dst, -1, rank_type,
1923 return rset_create (rset_kind_null, &parms);
1926 for (i = 1; i<rset_no; i++)
1928 rset_bool_parms bool_parms;
1930 bool_parms.rset_l = result;
1931 bool_parms.rset_r = rset[i];
1932 bool_parms.key_size = sizeof(struct it_key);
1933 bool_parms.cmp = key_compare_it;
1934 result = rset_create (rset_kind_and, &bool_parms);
1939 static RSET rpn_search_APT_local (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1941 oid_value attributeSet,
1943 const char *rank_type)
1948 rset_temp_parms parms;
1950 parms.rset_term = rset_term_create (termz, -1, rank_type,
1952 parms.cmp = key_compare_it;
1953 parms.key_size = sizeof (struct it_key);
1954 parms.temp_path = res_get (zh->res, "setTmpDir");
1955 result = rset_create (rset_kind_temp, &parms);
1956 rsfd = rset_open (result, RSETF_WRITE);
1958 key.sysno = atoi (termz);
1962 rset_write (result, rsfd, &key);
1963 rset_close (result, rsfd);
1967 static RSET rpn_sort_spec (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1968 oid_value attributeSet, NMEM stream,
1969 Z_SortKeySpecList *sort_sequence,
1970 const char *rank_type)
1972 rset_null_parms parms;
1974 int sort_relation_value;
1975 AttrType sort_relation_type;
1980 Z_AttributeElement *ae;
1985 attr_init (&sort_relation_type, zapt, 7);
1986 sort_relation_value = attr_find (&sort_relation_type, &attributeSet);
1988 attr_init (&use_type, zapt, 1);
1989 use_value = attr_find (&use_type, &attributeSet);
1991 if (!sort_sequence->specs)
1993 sort_sequence->num_specs = 10;
1994 sort_sequence->specs = (Z_SortKeySpec **)
1995 nmem_malloc (stream, sort_sequence->num_specs *
1996 sizeof(*sort_sequence->specs));
1997 for (i = 0; i<sort_sequence->num_specs; i++)
1998 sort_sequence->specs[i] = 0;
2000 if (zapt->term->which != Z_Term_general)
2003 i = atoi_n ((char *) zapt->term->u.general->buf,
2004 zapt->term->u.general->len);
2005 if (i >= sort_sequence->num_specs)
2007 sprintf (termz, "%d", i);
2009 oe.proto = PROTO_Z3950;
2010 oe.oclass = CLASS_ATTSET;
2011 oe.value = attributeSet;
2012 if (!oid_ent_to_oid (&oe, oid))
2015 sks = (Z_SortKeySpec *) nmem_malloc (stream, sizeof(*sks));
2016 sks->sortElement = (Z_SortElement *)
2017 nmem_malloc (stream, sizeof(*sks->sortElement));
2018 sks->sortElement->which = Z_SortElement_generic;
2019 sk = sks->sortElement->u.generic = (Z_SortKey *)
2020 nmem_malloc (stream, sizeof(*sk));
2021 sk->which = Z_SortKey_sortAttributes;
2022 sk->u.sortAttributes = (Z_SortAttributes *)
2023 nmem_malloc (stream, sizeof(*sk->u.sortAttributes));
2025 sk->u.sortAttributes->id = oid;
2026 sk->u.sortAttributes->list = (Z_AttributeList *)
2027 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list));
2028 sk->u.sortAttributes->list->num_attributes = 1;
2029 sk->u.sortAttributes->list->attributes = (Z_AttributeElement **)
2030 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list->attributes));
2031 ae = *sk->u.sortAttributes->list->attributes = (Z_AttributeElement *)
2032 nmem_malloc (stream, sizeof(**sk->u.sortAttributes->list->attributes));
2033 ae->attributeSet = 0;
2034 ae->attributeType = (int *)
2035 nmem_malloc (stream, sizeof(*ae->attributeType));
2036 *ae->attributeType = 1;
2037 ae->which = Z_AttributeValue_numeric;
2038 ae->value.numeric = (int *)
2039 nmem_malloc (stream, sizeof(*ae->value.numeric));
2040 *ae->value.numeric = use_value;
2042 sks->sortRelation = (int *)
2043 nmem_malloc (stream, sizeof(*sks->sortRelation));
2044 if (sort_relation_value == 1)
2045 *sks->sortRelation = Z_SortRelation_ascending;
2046 else if (sort_relation_value == 2)
2047 *sks->sortRelation = Z_SortRelation_descending;
2049 *sks->sortRelation = Z_SortRelation_ascending;
2051 sks->caseSensitivity = (int *)
2052 nmem_malloc (stream, sizeof(*sks->caseSensitivity));
2053 *sks->caseSensitivity = 0;
2055 sks->which = Z_SortKeySpec_null;
2056 sks->u.null = odr_nullval ();
2057 sort_sequence->specs[i] = sks;
2059 parms.rset_term = rset_term_create (termz, -1, rank_type,
2061 return rset_create (rset_kind_null, &parms);
2064 /* pop - moved to xpath.c */
2067 struct xpath_predicate {
2070 #define XPATH_PREDICATE_RELATION 1
2076 #define XPATH_PREDICATE_BOOLEAN 2
2079 struct xpath_predicate *left;
2080 struct xpath_predicate *right;
2085 struct xpath_location_step {
2087 struct xpath_predicate *predicate;
2092 static int parse_xpath(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2093 oid_value attributeSet,
2094 struct xpath_location_step *xpath, NMEM mem)
2096 oid_value curAttributeSet = attributeSet;
2098 const char *use_string = 0;
2101 attr_init (&use, zapt, 1);
2102 attr_find_ex (&use, &curAttributeSet, &use_string);
2104 if (!use_string || *use_string != '/')
2107 return (parse_xpath_str(use_string, xpath, mem));
2112 static RSET xpath_trunc(ZebraHandle zh, NMEM stream,
2113 int reg_type, const char *term, int use,
2114 oid_value curAttributeSet)
2117 struct grep_info grep_info;
2118 char term_dict[2048];
2121 int ord = zebraExplain_lookupSU (zh->reg->zei, curAttributeSet, use);
2122 int ord_len, i, r, max_pos;
2123 int term_type = Z_Term_characterString;
2124 const char *flags = "void";
2126 if (grep_info_prepare (zh, 0 /* zapt */, &grep_info, '0', stream))
2128 rset_null_parms parms;
2130 parms.rset_term = rset_term_create (term, strlen(term),
2132 parms.rset_term->nn = 0;
2133 return rset_create (rset_kind_null, &parms);
2138 rset_null_parms parms;
2140 parms.rset_term = rset_term_create (term, strlen(term),
2142 parms.rset_term->nn = 0;
2143 return rset_create (rset_kind_null, &parms);
2146 term_dict[prefix_len++] = '|';
2148 term_dict[prefix_len++] = '(';
2150 ord_len = key_SU_encode (ord, ord_buf);
2151 for (i = 0; i<ord_len; i++)
2153 term_dict[prefix_len++] = 1;
2154 term_dict[prefix_len++] = ord_buf[i];
2156 term_dict[prefix_len++] = ')';
2157 term_dict[prefix_len++] = 1;
2158 term_dict[prefix_len++] = reg_type;
2160 strcpy (term_dict+prefix_len, term);
2162 grep_info.isam_p_indx = 0;
2163 r = dict_lookup_grep (zh->reg->dict, term_dict, 0,
2164 &grep_info, &max_pos, 0, grep_handle);
2165 yaz_log (LOG_LOG, "%s %d positions", term,
2166 grep_info.isam_p_indx);
2167 rset = rset_trunc (zh, grep_info.isam_p_buf,
2168 grep_info.isam_p_indx, term, strlen(term),
2169 flags, 1, term_type);
2170 grep_info_delete (&grep_info);
2174 static RSET rpn_search_xpath (ZebraHandle zh,
2175 oid_value attributeSet,
2176 int num_bases, char **basenames,
2177 NMEM stream, const char *rank_type, RSET rset,
2178 int xpath_len, struct xpath_location_step *xpath)
2180 oid_value curAttributeSet = attributeSet;
2187 yaz_log (LOG_LOG, "len=%d", xpath_len);
2188 for (i = 0; i<xpath_len; i++)
2190 yaz_log (LOG_LOG, "XPATH %d %s", i, xpath[i].part);
2194 curAttributeSet = VAL_IDXPATH;
2204 a[@attr=value]/b[@other=othervalue]
2206 /e/@a val range(e/,range(@a,freetext(w,1015,val),@a),e/)
2207 /a/b val range(b/a/,freetext(w,1016,val),b/a/)
2208 /a/b/@c val range(b/a/,range(@c,freetext(w,1016,val),@c),b/a/)
2209 /a/b[@c=y] val range(b/a/,freetext(w,1016,val),b/a/,@c=y)
2210 /a[@c=y]/b val range(a/,range(b/a/,freetext(w,1016,val),b/a/),a/,@c=y)
2211 /a[@c=x]/b[@c=y] range(a/,range(b/a/,freetext(w,1016,val),b/a/,@c=y),a/,@c=x)
2215 dict_grep_cmap (zh->reg->dict, 0, 0);
2217 for (base_no = 0; base_no < num_bases; base_no++)
2219 int level = xpath_len;
2222 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
2224 zh->errCode = 109; /* Database unavailable */
2225 zh->errString = basenames[base_no];
2228 while (--level >= 0)
2230 char xpath_rev[128];
2232 rset_between_parms parms;
2233 RSET rset_start_tag = 0, rset_end_tag = 0, rset_attr = 0;
2237 for (i = level; i >= 1; --i)
2239 const char *cp = xpath[i].part;
2245 memcpy (xpath_rev + len, "[^/]*", 5);
2248 else if (*cp == ' ')
2251 xpath_rev[len++] = 1;
2252 xpath_rev[len++] = ' ';
2256 xpath_rev[len++] = *cp;
2257 xpath_rev[len++] = '/';
2259 else if (i == 1) /* // case */
2261 xpath_rev[len++] = '.';
2262 xpath_rev[len++] = '*';
2267 if (xpath[level].predicate &&
2268 xpath[level].predicate->which == XPATH_PREDICATE_RELATION &&
2269 xpath[level].predicate->u.relation.name[0])
2271 char predicate_str[128];
2273 strcpy (predicate_str,
2274 xpath[level].predicate->u.relation.name+1);
2275 if (xpath[level].predicate->u.relation.value)
2277 strcat (predicate_str, "=");
2278 strcat (predicate_str,
2279 xpath[level].predicate->u.relation.value);
2281 rset_attr = xpath_trunc (
2282 zh, stream, '0', predicate_str, 3, curAttributeSet);
2289 yaz_log (LOG_LOG, "xpath_rev (%d) = %s", level, xpath_rev);
2290 if (strlen(xpath_rev))
2292 rset_start_tag = xpath_trunc(zh, stream,
2293 '0', xpath_rev, 1, curAttributeSet);
2295 rset_end_tag = xpath_trunc(zh, stream,
2296 '0', xpath_rev, 2, curAttributeSet);
2298 parms.key_size = sizeof(struct it_key);
2299 parms.cmp = key_compare_it;
2300 parms.rset_l = rset_start_tag;
2301 parms.rset_m = rset;
2302 parms.rset_r = rset_end_tag;
2303 parms.rset_attr = rset_attr;
2304 parms.printer = key_print_it;
2305 rset = rset_create (rset_kind_between, &parms);
2316 static RSET rpn_search_APT (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2317 oid_value attributeSet, NMEM stream,
2318 Z_SortKeySpecList *sort_sequence,
2319 int num_bases, char **basenames)
2322 char *search_type = NULL;
2323 char rank_type[128];
2326 char termz[IT_MAX_WORD+1];
2330 struct xpath_location_step xpath[10];
2332 zebra_maps_attr (zh->reg->zebra_maps, zapt, ®_id, &search_type,
2333 rank_type, &complete_flag, &sort_flag);
2335 logf (LOG_DEBUG, "reg_id=%c", reg_id);
2336 logf (LOG_DEBUG, "complete_flag=%d", complete_flag);
2337 logf (LOG_DEBUG, "search_type=%s", search_type);
2338 logf (LOG_DEBUG, "rank_type=%s", rank_type);
2340 if (zapt_term_to_utf8(zh, zapt, termz))
2344 return rpn_sort_spec (zh, zapt, attributeSet, stream, sort_sequence,
2346 xpath_len = parse_xpath(zh, zapt, attributeSet, xpath, stream);
2350 if (xpath[xpath_len-1].part[0] == '@')
2354 if (!strcmp (search_type, "phrase"))
2356 rset = rpn_search_APT_phrase (zh, zapt, termz, attributeSet, stream,
2357 reg_id, complete_flag, rank_type,
2359 num_bases, basenames);
2361 else if (!strcmp (search_type, "and-list"))
2363 rset = rpn_search_APT_and_list (zh, zapt, termz, attributeSet, stream,
2364 reg_id, complete_flag, rank_type,
2366 num_bases, basenames);
2368 else if (!strcmp (search_type, "or-list"))
2370 rset = rpn_search_APT_or_list (zh, zapt, termz, attributeSet, stream,
2371 reg_id, complete_flag, rank_type,
2373 num_bases, basenames);
2375 else if (!strcmp (search_type, "local"))
2377 rset = rpn_search_APT_local (zh, zapt, termz, attributeSet, stream,
2380 else if (!strcmp (search_type, "numeric"))
2382 rset = rpn_search_APT_numeric (zh, zapt, termz, attributeSet, stream,
2383 reg_id, complete_flag, rank_type,
2385 num_bases, basenames);
2387 else if (!strcmp (search_type, "always"))
2393 return rpn_search_xpath (zh, attributeSet, num_bases, basenames,
2394 stream, rank_type, rset, xpath_len, xpath);
2397 static RSET rpn_search_structure (ZebraHandle zh, Z_RPNStructure *zs,
2398 oid_value attributeSet, NMEM stream,
2399 Z_SortKeySpecList *sort_sequence,
2400 int num_bases, char **basenames)
2403 if (zs->which == Z_RPNStructure_complex)
2405 Z_Operator *zop = zs->u.complex->roperator;
2406 rset_bool_parms bool_parms;
2408 bool_parms.rset_l = rpn_search_structure (zh, zs->u.complex->s1,
2409 attributeSet, stream,
2411 num_bases, basenames);
2412 if (bool_parms.rset_l == NULL)
2414 bool_parms.rset_r = rpn_search_structure (zh, zs->u.complex->s2,
2415 attributeSet, stream,
2417 num_bases, basenames);
2418 if (bool_parms.rset_r == NULL)
2420 rset_delete (bool_parms.rset_l);
2423 bool_parms.key_size = sizeof(struct it_key);
2424 bool_parms.cmp = key_compare_it;
2428 case Z_Operator_and:
2429 r = rset_create (rset_kind_and, &bool_parms);
2432 r = rset_create (rset_kind_or, &bool_parms);
2434 case Z_Operator_and_not:
2435 r = rset_create (rset_kind_not, &bool_parms);
2437 case Z_Operator_prox:
2438 if (zop->u.prox->which != Z_ProximityOperator_known)
2443 if (*zop->u.prox->u.known != Z_ProxUnit_word)
2445 char *val = (char *) nmem_malloc (stream, 16);
2447 zh->errString = val;
2448 sprintf (val, "%d", *zop->u.prox->u.known);
2455 rsets[0] = bool_parms.rset_l;
2456 rsets[1] = bool_parms.rset_r;
2458 r = rpn_prox (zh, rsets, 2,
2459 *zop->u.prox->ordered,
2460 (!zop->u.prox->exclusion ? 0 :
2461 *zop->u.prox->exclusion),
2462 *zop->u.prox->relationType,
2463 *zop->u.prox->distance);
2464 rset_delete (rsets[0]);
2465 rset_delete (rsets[1]);
2473 else if (zs->which == Z_RPNStructure_simple)
2475 if (zs->u.simple->which == Z_Operand_APT)
2477 logf (LOG_DEBUG, "rpn_search_APT");
2478 r = rpn_search_APT (zh, zs->u.simple->u.attributesPlusTerm,
2479 attributeSet, stream, sort_sequence,
2480 num_bases, basenames);
2482 else if (zs->u.simple->which == Z_Operand_resultSetId)
2484 logf (LOG_DEBUG, "rpn_search_ref");
2485 r = resultSetRef (zh, zs->u.simple->u.resultSetId);
2488 r = rset_create (rset_kind_null, NULL);
2491 nmem_strdup (stream, zs->u.simple->u.resultSetId);
2510 RSET rpn_search (ZebraHandle zh, NMEM nmem,
2511 Z_RPNQuery *rpn, int num_bases, char **basenames,
2512 const char *setname,
2517 oid_value attributeSet;
2518 Z_SortKeySpecList *sort_sequence;
2522 zh->errString = NULL;
2525 sort_sequence = (Z_SortKeySpecList *)
2526 nmem_malloc (nmem, sizeof(*sort_sequence));
2527 sort_sequence->num_specs = 10;
2528 sort_sequence->specs = (Z_SortKeySpec **)
2529 nmem_malloc (nmem, sort_sequence->num_specs *
2530 sizeof(*sort_sequence->specs));
2531 for (i = 0; i<sort_sequence->num_specs; i++)
2532 sort_sequence->specs[i] = 0;
2534 attrset = oid_getentbyoid (rpn->attributeSetId);
2535 attributeSet = attrset->value;
2536 rset = rpn_search_structure (zh, rpn->RPNStructure, attributeSet,
2537 nmem, sort_sequence, num_bases, basenames);
2542 logf (LOG_DEBUG, "search error: %d", zh->errCode);
2544 for (i = 0; sort_sequence->specs[i]; i++)
2546 sort_sequence->num_specs = i;
2548 resultSetRank (zh, sset, rset);
2551 logf (LOG_DEBUG, "resultSetSortSingle in rpn_search");
2552 resultSetSortSingle (zh, nmem, sset, rset,
2553 sort_sequence, &sort_status);
2556 logf (LOG_DEBUG, "resultSetSortSingle status = %d", zh->errCode);
2562 struct scan_info_entry {
2568 struct scan_info_entry *list;
2574 static int scan_handle (char *name, const char *info, int pos, void *client)
2576 int len_prefix, idx;
2577 struct scan_info *scan_info = (struct scan_info *) client;
2579 len_prefix = strlen(scan_info->prefix);
2580 if (memcmp (name, scan_info->prefix, len_prefix))
2582 if (pos > 0) idx = scan_info->after - pos + scan_info->before;
2585 scan_info->list[idx].term = (char *)
2586 odr_malloc (scan_info->odr, strlen(name + len_prefix)+1);
2587 strcpy (scan_info->list[idx].term, name + len_prefix);
2588 assert (*info == sizeof(ISAMS_P));
2589 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAMS_P));
2593 static void scan_term_untrans (ZebraHandle zh, NMEM stream, int reg_type,
2594 char **dst, const char *src)
2596 char term_src[IT_MAX_WORD];
2597 char term_dst[IT_MAX_WORD];
2599 term_untrans (zh, reg_type, term_src, src);
2601 if (zh->iconv_from_utf8 != 0)
2604 char *inbuf = term_src;
2605 size_t inleft = strlen(term_src);
2606 char *outbuf = term_dst;
2607 size_t outleft = sizeof(term_dst)-1;
2610 ret = yaz_iconv (zh->iconv_from_utf8, &inbuf, &inleft,
2612 if (ret == (size_t)(-1))
2615 len = outbuf - term_dst;
2616 *dst = nmem_malloc (stream, len + 1);
2618 memcpy (*dst, term_dst, len);
2622 *dst = nmem_strdup (stream, term_src);
2625 static void count_set (RSET r, int *count)
2633 logf (LOG_DEBUG, "count_set");
2636 rfd = rset_open (r, RSETF_READ);
2637 while (rset_read (r, rfd, &key, &term_index))
2639 if (key.sysno != psysno)
2646 rset_close (r, rfd);
2647 logf (LOG_DEBUG, "%d keys, %d records", kno, *count);
2650 void rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
2651 oid_value attributeset,
2652 int num_bases, char **basenames,
2653 int *position, int *num_entries, ZebraScanEntry **list,
2657 int pos = *position;
2658 int num = *num_entries;
2662 char termz[IT_MAX_WORD+20];
2665 struct scan_info *scan_info_array;
2666 ZebraScanEntry *glist;
2667 int ords[32], ord_no = 0;
2671 char *search_type = NULL;
2672 char rank_type[128];
2677 if (attributeset == VAL_NONE)
2678 attributeset = VAL_BIB1;
2680 yaz_log (LOG_DEBUG, "position = %d, num = %d set=%d",
2681 pos, num, attributeset);
2683 attr_init (&use, zapt, 1);
2684 use_value = attr_find (&use, &attributeset);
2686 if (zebra_maps_attr (zh->reg->zebra_maps, zapt, ®_id, &search_type,
2687 rank_type, &complete_flag, &sort_flag))
2693 yaz_log (LOG_DEBUG, "use_value = %d", use_value);
2695 if (use_value == -1)
2697 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
2701 data1_local_attribute *local_attr;
2703 if ((r=att_getentbyatt (zh, &attp, attributeset, use_value)))
2705 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
2706 attributeset, use_value);
2714 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
2716 zh->errString = basenames[base_no];
2717 zh->errCode = 109; /* Database unavailable */
2721 for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
2722 local_attr = local_attr->next)
2726 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
2729 ords[ord_no++] = ord;
2738 /* prepare dictionary scanning */
2741 scan_info_array = (struct scan_info *)
2742 odr_malloc (stream, ord_no * sizeof(*scan_info_array));
2743 for (i = 0; i < ord_no; i++)
2745 int j, prefix_len = 0;
2746 int before_tmp = before, after_tmp = after;
2747 struct scan_info *scan_info = scan_info_array + i;
2748 struct rpn_char_map_info rcmi;
2750 rpn_char_map_prepare (zh->reg, reg_id, &rcmi);
2752 scan_info->before = before;
2753 scan_info->after = after;
2754 scan_info->odr = stream;
2756 scan_info->list = (struct scan_info_entry *)
2757 odr_malloc (stream, (before+after) * sizeof(*scan_info->list));
2758 for (j = 0; j<before+after; j++)
2759 scan_info->list[j].term = NULL;
2761 prefix_len += key_SU_encode (ords[i], termz + prefix_len);
2762 termz[prefix_len++] = reg_id;
2763 termz[prefix_len] = 0;
2764 strcpy (scan_info->prefix, termz);
2766 if (trans_scan_term (zh, zapt, termz+prefix_len, reg_id))
2769 dict_scan (zh->reg->dict, termz, &before_tmp, &after_tmp,
2770 scan_info, scan_handle);
2772 glist = (ZebraScanEntry *)
2773 odr_malloc (stream, (before+after)*sizeof(*glist));
2775 /* consider terms after main term */
2776 for (i = 0; i < ord_no; i++)
2780 for (i = 0; i<after; i++)
2783 const char *mterm = NULL;
2787 for (j = 0; j < ord_no; j++)
2789 if (ptr[j] < before+after &&
2790 (tst=scan_info_array[j].list[ptr[j]].term) &&
2791 (!mterm || strcmp (tst, mterm) < 0))
2799 scan_term_untrans (zh, stream->mem, reg_id,
2800 &glist[i+before].term, mterm);
2801 rset = rset_trunc (zh, &scan_info_array[j0].list[ptr[j0]].isam_p, 1,
2802 glist[i+before].term, strlen(glist[i+before].term),
2803 NULL, 0, zapt->term->which);
2806 for (j = j0+1; j<ord_no; j++)
2808 if (ptr[j] < before+after &&
2809 (tst=scan_info_array[j].list[ptr[j]].term) &&
2810 !strcmp (tst, mterm))
2812 rset_bool_parms bool_parms;
2816 rset_trunc (zh, &scan_info_array[j].list[ptr[j]].isam_p, 1,
2817 glist[i+before].term,
2818 strlen(glist[i+before].term), NULL, 0,
2821 bool_parms.key_size = sizeof(struct it_key);
2822 bool_parms.cmp = key_compare_it;
2823 bool_parms.rset_l = rset;
2824 bool_parms.rset_r = rset2;
2826 rset = rset_create (rset_kind_or, &bool_parms);
2831 count_set (rset, &glist[i+before].occurrences);
2836 *num_entries -= (after-i);
2840 /* consider terms before main term */
2841 for (i = 0; i<ord_no; i++)
2844 for (i = 0; i<before; i++)
2847 const char *mterm = NULL;
2851 for (j = 0; j <ord_no; j++)
2853 if (ptr[j] < before &&
2854 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2855 (!mterm || strcmp (tst, mterm) > 0))
2864 scan_term_untrans (zh, stream->mem, reg_id,
2865 &glist[before-1-i].term, mterm);
2868 (zh, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1,
2869 glist[before-1-i].term, strlen(glist[before-1-i].term),
2870 NULL, 0, zapt->term->which);
2874 for (j = j0+1; j<ord_no; j++)
2876 if (ptr[j] < before &&
2877 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2878 !strcmp (tst, mterm))
2880 rset_bool_parms bool_parms;
2883 rset2 = rset_trunc (zh,
2884 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1,
2885 glist[before-1-i].term,
2886 strlen(glist[before-1-i].term), NULL, 0,
2889 bool_parms.key_size = sizeof(struct it_key);
2890 bool_parms.cmp = key_compare_it;
2891 bool_parms.rset_l = rset;
2892 bool_parms.rset_r = rset2;
2894 rset = rset_create (rset_kind_or, &bool_parms);
2899 count_set (rset, &glist[before-1-i].occurrences);
2909 *list = glist + i; /* list is set to first 'real' entry */
2911 logf (LOG_DEBUG, "position = %d, num_entries = %d",
2912 *position, *num_entries);
2914 logf (LOG_DEBUG, "scan error: %d", zh->errCode);