1 /* $Id: zrpn.c,v 1.135 2004-01-15 13:31:31 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
34 #include <zebra_xpath.h>
40 #include <rsbetween.h>
42 struct rpn_char_map_info {
51 Z_AttributesPlusTerm *zapt;
55 static const char **rpn_char_map_handler (void *vp, const char **from, int len)
57 struct rpn_char_map_info *p = (struct rpn_char_map_info *) vp;
58 const char **out = zebra_maps_input (p->zm, p->reg_type, from, len);
62 const char *outp = *out;
63 yaz_log (LOG_LOG, "---");
66 yaz_log (LOG_LOG, "%02X", *outp);
74 static void rpn_char_map_prepare (struct zebra_register *reg, int reg_type,
75 struct rpn_char_map_info *map_info)
77 map_info->zm = reg->zebra_maps;
78 map_info->reg_type = reg_type;
79 dict_grep_cmap (reg->dict, map_info, rpn_char_map_handler);
82 static int attr_find_ex (AttrType *src, oid_value *attributeSetP,
83 const char **string_value)
87 num_attributes = src->zapt->attributes->num_attributes;
88 while (src->major < num_attributes)
90 Z_AttributeElement *element;
92 element = src->zapt->attributes->attributes[src->major];
93 if (src->type == *element->attributeType)
95 switch (element->which)
97 case Z_AttributeValue_numeric:
99 if (element->attributeSet && attributeSetP)
103 attrset = oid_getentbyoid (element->attributeSet);
104 *attributeSetP = attrset->value;
106 return *element->value.numeric;
108 case Z_AttributeValue_complex:
109 if (src->minor >= element->value.complex->num_list)
111 if (element->attributeSet && attributeSetP)
115 attrset = oid_getentbyoid (element->attributeSet);
116 *attributeSetP = attrset->value;
118 if (element->value.complex->list[src->minor]->which ==
119 Z_StringOrNumeric_numeric)
123 *element->value.complex->list[src->minor-1]->u.numeric;
125 else if (element->value.complex->list[src->minor]->which ==
126 Z_StringOrNumeric_string)
132 element->value.complex->list[src->minor-1]->u.string;
146 static int attr_find (AttrType *src, oid_value *attributeSetP)
148 return attr_find_ex (src, attributeSetP, 0);
151 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
174 static void term_untrans (ZebraHandle zh, int reg_type,
175 char *dst, const char *src)
180 const char *cp = zebra_maps_output (zh->reg->zebra_maps,
182 if (!cp && len < IT_MAX_WORD-1)
185 while (*cp && len < IT_MAX_WORD-1)
191 static void add_isam_p (const char *name, const char *info,
194 if (p->isam_p_indx == p->isam_p_size)
196 ISAMS_P *new_isam_p_buf;
200 p->isam_p_size = 2*p->isam_p_size + 100;
201 new_isam_p_buf = (ISAMS_P *) xmalloc (sizeof(*new_isam_p_buf) *
205 memcpy (new_isam_p_buf, p->isam_p_buf,
206 p->isam_p_indx * sizeof(*p->isam_p_buf));
207 xfree (p->isam_p_buf);
209 p->isam_p_buf = new_isam_p_buf;
212 new_term_no = (int *) xmalloc (sizeof(*new_term_no) *
216 memcpy (new_term_no, p->isam_p_buf,
217 p->isam_p_indx * sizeof(*p->term_no));
220 p->term_no = new_term_no;
223 assert (*info == sizeof(*p->isam_p_buf));
224 memcpy (p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
231 char term_tmp[IT_MAX_WORD];
233 int len = key_SU_decode (&su_code, name);
235 term_untrans (p->zh, p->reg_type, term_tmp, name+len+1);
236 logf (LOG_LOG, "grep: %d %c %s", su_code, name[len], term_tmp);
237 zebraExplain_lookup_ord (p->zh->reg->zei,
238 su_code, &db, &set, &use);
239 logf (LOG_LOG, "grep: set=%d use=%d db=%s", set, use, db);
241 resultSetAddTerm (p->zh, p->termset, name[len], db,
248 static int grep_handle (char *name, const char *info, void *p)
250 add_isam_p (name, info, (struct grep_info *) p);
254 static int term_pre (ZebraMaps zebra_maps, int reg_type, const char **src,
255 const char *ct1, const char *ct2)
257 const char *s1, *s0 = *src;
260 /* skip white space */
263 if (ct1 && strchr (ct1, *s0))
265 if (ct2 && strchr (ct2, *s0))
268 map = zebra_maps_input (zebra_maps, reg_type, &s1, strlen(s1));
269 if (**map != *CHR_SPACE)
277 #define REGEX_CHARS " []()|.*+?!"
279 /* term_100: handle term, where trunc=none (no operators at all) */
280 static int term_100 (ZebraMaps zebra_maps, int reg_type,
281 const char **src, char *dst, int space_split,
289 const char *space_start = 0;
290 const char *space_end = 0;
292 if (!term_pre (zebra_maps, reg_type, src, NULL, NULL))
298 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
301 if (**map == *CHR_SPACE)
304 else /* complete subfield only. */
306 if (**map == *CHR_SPACE)
307 { /* save space mapping for later .. */
312 else if (space_start)
313 { /* reload last space */
314 while (space_start < space_end)
316 if (strchr (REGEX_CHARS, *space_start))
318 dst_term[j++] = *space_start;
319 dst[i++] = *space_start++;
322 space_start = space_end = 0;
325 /* add non-space char */
328 if (strchr(REGEX_CHARS, *s1))
340 /* term_101: handle term, where trunc=Process # */
341 static int term_101 (ZebraMaps zebra_maps, int reg_type,
342 const char **src, char *dst, int space_split,
350 if (!term_pre (zebra_maps, reg_type, src, "#", "#"))
359 dst_term[j++] = *s0++;
364 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
365 if (space_split && **map == *CHR_SPACE)
369 if (strchr(REGEX_CHARS, *s1))
377 dst_term[j++] = '\0';
382 /* term_103: handle term, where trunc=re-2 (regular expressions) */
383 static int term_103 (ZebraMaps zebra_maps, int reg_type, const char **src,
384 char *dst, int *errors, int space_split,
392 if (!term_pre (zebra_maps, reg_type, src, "^\\()[].*+?|", "("))
395 if (errors && *s0 == '+' && s0[1] && s0[2] == '+' && s0[3] &&
398 *errors = s0[1] - '0';
405 if (strchr ("^\\()[].*+?|-", *s0))
413 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
414 if (**map == *CHR_SPACE)
418 if (strchr(REGEX_CHARS, *s1))
431 /* term_103: handle term, where trunc=re-1 (regular expressions) */
432 static int term_102 (ZebraMaps zebra_maps, int reg_type, const char **src,
433 char *dst, int space_split, char *dst_term)
435 return term_103 (zebra_maps, reg_type, src, dst, NULL, space_split,
440 /* term_104: handle term, where trunc=Process # and ! */
441 static int term_104 (ZebraMaps zebra_maps, int reg_type,
442 const char **src, char *dst, int space_split,
450 if (!term_pre (zebra_maps, reg_type, src, "?*#", "?*#"))
457 dst_term[j++] = *s0++;
458 if (*s0 >= '0' && *s0 <= '9')
461 while (*s0 >= '0' && *s0 <= '9')
463 limit = limit * 10 + (*s0 - '0');
464 dst_term[j++] = *s0++;
484 dst_term[j++] = *s0++;
489 dst_term[j++] = *s0++;
493 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
494 if (space_split && **map == *CHR_SPACE)
498 if (strchr(REGEX_CHARS, *s1))
506 dst_term[j++] = '\0';
511 /* term_105/106: handle term, where trunc=Process * and ! and right trunc */
512 static int term_105 (ZebraMaps zebra_maps, int reg_type,
513 const char **src, char *dst, int space_split,
514 char *dst_term, int right_truncate)
521 if (!term_pre (zebra_maps, reg_type, src, "*!", "*!"))
530 dst_term[j++] = *s0++;
535 dst_term[j++] = *s0++;
539 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
540 if (space_split && **map == *CHR_SPACE)
544 if (strchr(REGEX_CHARS, *s1))
558 dst_term[j++] = '\0';
564 /* gen_regular_rel - generate regular expression from relation
565 * val: border value (inclusive)
566 * islt: 1 if <=; 0 if >=.
568 static void gen_regular_rel (char *dst, int val, int islt)
575 logf (LOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
579 strcpy (dst, "(-[0-9]+|(");
587 strcpy (dst, "([0-9]+|-(");
588 dst_p = strlen (dst);
598 dst_p = strlen (dst);
599 sprintf (numstr, "%d", val);
600 for (w = strlen(numstr); --w >= 0; pos++)
619 strcpy (dst + dst_p, numstr);
620 dst_p = strlen(dst) - pos - 1;
648 for (i = 0; i<pos; i++)
661 /* match everything less than 10^(pos-1) */
663 for (i=1; i<pos; i++)
664 strcat (dst, "[0-9]?");
668 /* match everything greater than 10^pos */
669 for (i = 0; i <= pos; i++)
670 strcat (dst, "[0-9]");
671 strcat (dst, "[0-9]*");
676 void string_rel_add_char (char **term_p, const char *src, int *indx)
678 if (src[*indx] == '\\')
679 *(*term_p)++ = src[(*indx)++];
680 *(*term_p)++ = src[(*indx)++];
684 * > abc ([b-].*|a[c-].*|ab[d-].*|abc.+)
685 * ([^-a].*|a[^-b].*ab[^-c].*|abc.+)
686 * >= abc ([b-].*|a[c-].*|ab[c-].*)
687 * ([^-a].*|a[^-b].*|ab[c-].*)
688 * < abc ([-0].*|a[-a].*|ab[-b].*)
689 * ([^a-].*|a[^b-].*|ab[^c-].*)
690 * <= abc ([-0].*|a[-a].*|ab[-b].*|abc)
691 * ([^a-].*|a[^b-].*|ab[^c-].*|abc)
693 static int string_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
694 const char **term_sub, char *term_dict,
695 oid_value attributeSet,
696 int reg_type, int space_split, char *term_dst)
701 char *term_tmp = term_dict + strlen(term_dict);
702 char term_component[2*IT_MAX_WORD+20];
704 attr_init (&relation, zapt, 2);
705 relation_value = attr_find (&relation, NULL);
707 logf (LOG_DEBUG, "string relation value=%d", relation_value);
708 switch (relation_value)
711 if (!term_100 (zh->reg->zebra_maps, reg_type,
712 term_sub, term_component,
713 space_split, term_dst))
715 logf (LOG_DEBUG, "Relation <");
718 for (i = 0; term_component[i]; )
725 string_rel_add_char (&term_tmp, term_component, &j);
730 string_rel_add_char (&term_tmp, term_component, &i);
737 if ((term_tmp - term_dict) > IT_MAX_WORD)
744 if (!term_100 (zh->reg->zebra_maps, reg_type,
745 term_sub, term_component,
746 space_split, term_dst))
748 logf (LOG_DEBUG, "Relation <=");
751 for (i = 0; term_component[i]; )
756 string_rel_add_char (&term_tmp, term_component, &j);
760 string_rel_add_char (&term_tmp, term_component, &i);
769 if ((term_tmp - term_dict) > IT_MAX_WORD)
772 for (i = 0; term_component[i]; )
773 string_rel_add_char (&term_tmp, term_component, &i);
778 if (!term_100 (zh->reg->zebra_maps, reg_type,
779 term_sub, term_component, space_split, term_dst))
781 logf (LOG_DEBUG, "Relation >");
784 for (i = 0; term_component[i];)
789 string_rel_add_char (&term_tmp, term_component, &j);
794 string_rel_add_char (&term_tmp, term_component, &i);
802 if ((term_tmp - term_dict) > IT_MAX_WORD)
805 for (i = 0; term_component[i];)
806 string_rel_add_char (&term_tmp, term_component, &i);
813 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub,
814 term_component, space_split, term_dst))
816 logf (LOG_DEBUG, "Relation >=");
819 for (i = 0; term_component[i];)
826 string_rel_add_char (&term_tmp, term_component, &j);
829 if (term_component[i+1])
833 string_rel_add_char (&term_tmp, term_component, &i);
837 string_rel_add_char (&term_tmp, term_component, &i);
844 if ((term_tmp - term_dict) > IT_MAX_WORD)
852 logf (LOG_DEBUG, "Relation =");
853 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub,
854 term_component, space_split, term_dst))
856 strcat (term_tmp, "(");
857 strcat (term_tmp, term_component);
858 strcat (term_tmp, ")");
863 static int string_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
864 const char **term_sub,
865 oid_value attributeSet, NMEM stream,
866 struct grep_info *grep_info,
867 int reg_type, int complete_flag,
868 int num_bases, char **basenames,
869 char *term_dst, int xpath_use);
871 static RSET term_trunc (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
872 const char **term_sub,
873 oid_value attributeSet, NMEM stream,
874 struct grep_info *grep_info,
875 int reg_type, int complete_flag,
876 int num_bases, char **basenames,
878 const char *rank_type, int xpath_use)
881 grep_info->isam_p_indx = 0;
882 r = string_term (zh, zapt, term_sub, attributeSet, stream, grep_info,
883 reg_type, complete_flag, num_bases, basenames,
884 term_dst, xpath_use);
887 logf (LOG_DEBUG, "term: %s", term_dst);
888 return rset_trunc (zh, grep_info->isam_p_buf,
889 grep_info->isam_p_indx, term_dst,
890 strlen(term_dst), rank_type, 1 /* preserve pos */,
895 static int string_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
896 const char **term_sub,
897 oid_value attributeSet, NMEM stream,
898 struct grep_info *grep_info,
899 int reg_type, int complete_flag,
900 int num_bases, char **basenames,
901 char *term_dst, int xpath_use)
903 char term_dict[2*IT_MAX_WORD+4000];
906 int truncation_value;
909 const char *use_string = 0;
910 oid_value curAttributeSet = attributeSet;
912 struct rpn_char_map_info rcmi;
913 int space_split = complete_flag ? 0 : 1;
915 int bases_ok = 0; /* no of databases with OK attribute */
916 int errCode = 0; /* err code (if any is not OK) */
917 char *errString = 0; /* addinfo */
919 rpn_char_map_prepare (zh->reg, reg_type, &rcmi);
920 attr_init (&use, zapt, 1);
921 use_value = attr_find_ex (&use, &curAttributeSet, &use_string);
922 logf (LOG_DEBUG, "string_term, use value %d", use_value);
923 attr_init (&truncation, zapt, 5);
924 truncation_value = attr_find (&truncation, NULL);
925 logf (LOG_DEBUG, "truncation value %d", truncation_value);
927 if (use_value == -1) /* no attribute - assumy "any" */
929 for (base_no = 0; base_no < num_bases; base_no++)
932 data1_local_attribute id_xpath_attr;
933 data1_local_attribute *local_attr;
934 int max_pos, prefix_len = 0;
938 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
940 zh->errCode = 109; /* Database unavailable */
941 zh->errString = basenames[base_no];
944 if (use_value == -2) /* string attribute (assume IDXPATH/any) */
946 use_value = xpath_use;
947 attp.local_attributes = &id_xpath_attr;
948 attp.attset_ordinal = VAL_IDXPATH;
949 id_xpath_attr.next = 0;
950 id_xpath_attr.local = use_value;
952 else if (curAttributeSet == VAL_IDXPATH)
954 attp.local_attributes = &id_xpath_attr;
955 attp.attset_ordinal = VAL_IDXPATH;
956 id_xpath_attr.next = 0;
957 id_xpath_attr.local = use_value;
961 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
963 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
964 curAttributeSet, use_value, r);
967 /* set was found, but value wasn't defined */
969 sprintf (val_str, "%d", use_value);
971 errString = nmem_strdup (stream, val_str);
976 struct oident oident;
978 oident.proto = PROTO_Z3950;
979 oident.oclass = CLASS_ATTSET;
980 oident.value = curAttributeSet;
981 oid_ent_to_oid (&oident, oid);
984 errString = nmem_strdup (stream, oident.desc);
989 for (local_attr = attp.local_attributes; local_attr;
990 local_attr = local_attr->next)
996 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
1001 term_dict[prefix_len++] = '|';
1003 term_dict[prefix_len++] = '(';
1005 ord_len = key_SU_encode (ord, ord_buf);
1006 for (i = 0; i<ord_len; i++)
1008 term_dict[prefix_len++] = 1;
1009 term_dict[prefix_len++] = ord_buf[i];
1015 sprintf (val_str, "%d", use_value);
1017 errString = nmem_strdup (stream, val_str);
1020 bases_ok++; /* this has OK attributes */
1022 term_dict[prefix_len++] = ')';
1023 term_dict[prefix_len++] = 1;
1024 term_dict[prefix_len++] = reg_type;
1025 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1026 term_dict[prefix_len] = '\0';
1028 switch (truncation_value)
1030 case -1: /* not specified */
1031 case 100: /* do not truncate */
1032 if (!string_relation (zh, zapt, &termp, term_dict,
1034 reg_type, space_split, term_dst))
1036 logf (LOG_LOG, "dict_lookup_grep: %s", term_dict+prefix_len);
1037 r = dict_lookup_grep (zh->reg->dict, term_dict, 0,
1038 grep_info, &max_pos, 0, grep_handle);
1040 logf (LOG_WARN, "dict_lookup_grep fail %d", r);
1042 case 1: /* right truncation */
1043 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);
1051 case 2: /* keft truncation */
1052 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1053 if (!term_100 (zh->reg->zebra_maps, reg_type,
1054 &termp, term_dict + j, space_split, term_dst))
1056 strcat (term_dict, ")");
1057 dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1058 &max_pos, 0, grep_handle);
1060 case 3: /* left&right truncation */
1061 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1062 if (!term_100 (zh->reg->zebra_maps, reg_type,
1063 &termp, term_dict + j, space_split, term_dst))
1065 strcat (term_dict, ".*)");
1066 dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1067 &max_pos, 0, grep_handle);
1071 case 101: /* process # in term */
1072 term_dict[j++] = '(';
1073 if (!term_101 (zh->reg->zebra_maps, reg_type,
1074 &termp, term_dict + j, space_split, term_dst))
1076 strcat (term_dict, ")");
1077 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1078 &max_pos, 0, grep_handle);
1080 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d", r);
1082 case 102: /* Regexp-1 */
1083 term_dict[j++] = '(';
1084 if (!term_102 (zh->reg->zebra_maps, reg_type,
1085 &termp, term_dict + j, space_split, term_dst))
1087 strcat (term_dict, ")");
1088 logf (LOG_DEBUG, "Regexp-1 tolerance=%d", r);
1089 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1090 &max_pos, 0, grep_handle);
1092 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
1095 case 103: /* Regexp-2 */
1097 term_dict[j++] = '(';
1098 if (!term_103 (zh->reg->zebra_maps, reg_type,
1099 &termp, term_dict + j, &r, space_split, term_dst))
1101 strcat (term_dict, ")");
1102 logf (LOG_DEBUG, "Regexp-2 tolerance=%d", r);
1103 r = dict_lookup_grep (zh->reg->dict, term_dict, r, grep_info,
1104 &max_pos, 2, grep_handle);
1106 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
1109 case 104: /* process # and ! in term */
1110 term_dict[j++] = '(';
1111 if (!term_104 (zh->reg->zebra_maps, reg_type,
1112 &termp, term_dict + j, space_split, term_dst))
1114 strcat (term_dict, ")");
1115 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1116 &max_pos, 0, grep_handle);
1118 logf (LOG_WARN, "dict_lookup_grep err, trunc=#/!: %d", r);
1120 case 105: /* process * and ! in term */
1121 term_dict[j++] = '(';
1122 if (!term_105 (zh->reg->zebra_maps, reg_type,
1123 &termp, term_dict + j, space_split, term_dst, 1))
1125 strcat (term_dict, ")");
1126 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1127 &max_pos, 0, grep_handle);
1129 logf (LOG_WARN, "dict_lookup_grep err, trunc=*/!: %d", r);
1131 case 106: /* process * and ! in term */
1132 term_dict[j++] = '(';
1133 if (!term_105 (zh->reg->zebra_maps, reg_type,
1134 &termp, term_dict + j, space_split, term_dst, 0))
1136 strcat (term_dict, ")");
1137 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1138 &max_pos, 0, grep_handle);
1140 logf (LOG_WARN, "dict_lookup_grep err, trunc=*/!: %d", r);
1146 zh->errCode = errCode;
1147 zh->errString = errString;
1151 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1156 /* convert APT search term to UTF8 */
1157 static int zapt_term_to_utf8 (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1161 Z_Term *term = zapt->term;
1163 switch (term->which)
1165 case Z_Term_general:
1166 if (zh->iconv_to_utf8 != 0)
1168 char *inbuf = term->u.general->buf;
1169 size_t inleft = term->u.general->len;
1170 char *outbuf = termz;
1171 size_t outleft = IT_MAX_WORD-1;
1174 ret = yaz_iconv(zh->iconv_to_utf8, &inbuf, &inleft,
1176 if (ret == (size_t)(-1))
1178 ret = yaz_iconv(zh->iconv_to_utf8, 0, 0, 0, 0);
1186 sizez = term->u.general->len;
1187 if (sizez > IT_MAX_WORD-1)
1188 sizez = IT_MAX_WORD-1;
1189 memcpy (termz, term->u.general->buf, sizez);
1190 termz[sizez] = '\0';
1193 case Z_Term_characterString:
1194 sizez = strlen(term->u.characterString);
1195 if (sizez > IT_MAX_WORD-1)
1196 sizez = IT_MAX_WORD-1;
1197 memcpy (termz, term->u.characterString, sizez);
1198 termz[sizez] = '\0';
1207 /* convert APT SCAN term to internal cmap */
1208 static int trans_scan_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1209 char *termz, int reg_type)
1211 char termz0[IT_MAX_WORD];
1213 if (zapt_term_to_utf8(zh, zapt, termz0))
1214 return -1; /* error */
1218 const char *cp = (const char *) termz0;
1219 const char *cp_end = cp + strlen(cp);
1222 const char *space_map = NULL;
1225 while ((len = (cp_end - cp)) > 0)
1227 map = zebra_maps_input (zh->reg->zebra_maps, reg_type, &cp, len);
1228 if (**map == *CHR_SPACE)
1233 for (src = space_map; *src; src++)
1236 for (src = *map; *src; src++)
1245 static RSET rpn_prox (ZebraHandle zh, RSET *rset, int rset_no,
1246 int ordered, int exclusion, int relation, int distance)
1251 struct it_key **buf;
1253 char prox_term[1024];
1254 int length_prox_term = 0;
1255 int min_nn = 10000000;
1257 int term_type = Z_Term_characterString;
1258 const char *flags = NULL;
1260 rsfd = (RSFD *) xmalloc (sizeof(*rsfd)*rset_no);
1261 more = (int *) xmalloc (sizeof(*more)*rset_no);
1262 buf = (struct it_key **) xmalloc (sizeof(*buf)*rset_no);
1265 for (i = 0; i<rset_no; i++)
1268 for (j = 0; j<rset[i]->no_rset_terms; j++)
1270 const char *nflags = rset[i]->rset_terms[j]->flags;
1271 char *term = rset[i]->rset_terms[j]->name;
1272 int lterm = strlen(term);
1273 if (lterm + length_prox_term < sizeof(prox_term)-1)
1275 if (length_prox_term)
1276 prox_term[length_prox_term++] = ' ';
1277 strcpy (prox_term + length_prox_term, term);
1278 length_prox_term += lterm;
1280 if (min_nn > rset[i]->rset_terms[j]->nn)
1281 min_nn = rset[i]->rset_terms[j]->nn;
1283 term_type = rset[i]->rset_terms[j]->type;
1285 /* only if all term types are of type characterString .. */
1286 /* the resulting term is of that type */
1287 if (term_type != Z_Term_characterString)
1288 term_type = Z_Term_general;
1291 for (i = 0; i<rset_no; i++)
1296 for (i = 0; i<rset_no; i++)
1298 buf[i] = (struct it_key *) xmalloc (sizeof(**buf));
1299 rsfd[i] = rset_open (rset[i], RSETF_READ);
1300 if (!(more[i] = rset_read (rset[i], rsfd[i], buf[i], &term_index)))
1305 /* at least one is empty ... return null set */
1306 rset_null_parms parms;
1308 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1310 parms.rset_term->nn = 0;
1311 result = rset_create (rset_kind_null, &parms);
1313 else if (ordered && relation == 3 && exclusion == 0 && distance == 1)
1315 /* special proximity case = phrase search ... */
1316 rset_temp_parms parms;
1319 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1321 parms.rset_term->nn = min_nn;
1322 parms.cmp = key_compare_it;
1323 parms.key_size = sizeof (struct it_key);
1324 parms.temp_path = res_get (zh->res, "setTmpDir");
1325 result = rset_create (rset_kind_temp, &parms);
1326 rsfd_result = rset_open (result, RSETF_WRITE);
1330 for (i = 1; i<rset_no; i++)
1339 cmp = key_compare_it (buf[i], buf[i-1]);
1342 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1343 buf[i-1], &term_index);
1348 if (buf[i-1]->seqno+1 != buf[i]->seqno)
1350 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1351 buf[i-1], &term_index);
1357 more[i] = rset_read (rset[i], rsfd[i], buf[i],
1364 rset_write (result, rsfd_result, buf[0]);
1365 more[0] = rset_read (*rset, *rsfd, *buf, &term_index);
1368 rset_close (result, rsfd_result);
1370 else if (rset_no == 2)
1372 /* generic proximity case (two input sets only) ... */
1373 rset_temp_parms parms;
1376 yaz_log (LOG_LOG, "generic prox, dist=%d, relation=%d, ordered=%d"
1378 distance, relation, ordered, exclusion);
1379 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1381 parms.rset_term->nn = min_nn;
1382 parms.cmp = key_compare_it;
1383 parms.key_size = sizeof (struct it_key);
1384 parms.temp_path = res_get (zh->res, "setTmpDir");
1385 result = rset_create (rset_kind_temp, &parms);
1386 rsfd_result = rset_open (result, RSETF_WRITE);
1388 while (more[0] && more[1])
1390 int cmp = key_compare_it (buf[0], buf[1]);
1392 more[0] = rset_read (rset[0], rsfd[0], buf[0], &term_index);
1394 more[1] = rset_read (rset[1], rsfd[1], buf[1], &term_index);
1397 int sysno = buf[0]->sysno;
1401 seqno[n++] = buf[0]->seqno;
1402 while ((more[0] = rset_read (rset[0], rsfd[0], buf[0],
1404 sysno == buf[0]->sysno)
1406 seqno[n++] = buf[0]->seqno;
1409 for (i = 0; i<n; i++)
1411 int diff = buf[1]->seqno - seqno[i];
1412 int excl = exclusion;
1413 if (!ordered && diff < 0)
1418 if (diff < distance && diff >= 0)
1422 if (diff <= distance && diff >= 0)
1426 if (diff == distance && diff >= 0)
1430 if (diff >= distance && diff >= 0)
1434 if (diff > distance && diff >= 0)
1438 if (diff != distance && diff >= 0)
1444 rset_write (result, rsfd_result, buf[1]);
1448 } while ((more[1] = rset_read (rset[1], rsfd[1], buf[1],
1450 sysno == buf[1]->sysno);
1453 rset_close (result, rsfd_result);
1457 rset_null_parms parms;
1459 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1461 parms.rset_term->nn = 0;
1462 result = rset_create (rset_kind_null, &parms);
1464 for (i = 0; i<rset_no; i++)
1467 rset_close (rset[i], rsfd[i]);
1477 char *normalize_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1478 const char *termz, NMEM stream, unsigned reg_id)
1481 AttrType truncation;
1482 int truncation_value;
1485 attr_init (&truncation, zapt, 5);
1486 truncation_value = attr_find (&truncation, NULL);
1488 switch (truncation_value)
1508 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, ex_list,
1509 termz, strlen(termz));
1511 return nmem_strdup(stream, termz);
1514 char *buf = (char*) nmem_malloc (stream, wrbuf_len(wrbuf)+1);
1515 memcpy (buf, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1516 buf[wrbuf_len(wrbuf)] = '\0';
1521 static void grep_info_delete (struct grep_info *grep_info)
1524 xfree(grep_info->term_no);
1526 xfree (grep_info->isam_p_buf);
1529 static int grep_info_prepare (ZebraHandle zh,
1530 Z_AttributesPlusTerm *zapt,
1531 struct grep_info *grep_info,
1536 int termset_value_numeric;
1537 const char *termset_value_string;
1540 grep_info->term_no = 0;
1542 grep_info->isam_p_size = 0;
1543 grep_info->isam_p_buf = NULL;
1545 grep_info->reg_type = reg_type;
1546 grep_info->termset = 0;
1550 attr_init (&termset, zapt, 8);
1551 termset_value_numeric =
1552 attr_find_ex (&termset, NULL, &termset_value_string);
1553 if (termset_value_numeric != -1)
1556 const char *termset_name = 0;
1557 if (termset_value_numeric != -2)
1560 sprintf (resname, "%d", termset_value_numeric);
1561 termset_name = resname;
1564 termset_name = termset_value_string;
1565 logf (LOG_LOG, "creating termset set %s", termset_name);
1566 grep_info->termset = resultSetAdd (zh, termset_name, 1);
1567 if (!grep_info->termset)
1570 zh->errString = nmem_strdup (stream, termset_name);
1578 static RSET rpn_search_APT_phrase (ZebraHandle zh,
1579 Z_AttributesPlusTerm *zapt,
1580 const char *termz_org,
1581 oid_value attributeSet,
1583 int reg_type, int complete_flag,
1584 const char *rank_type, int xpath_use,
1585 int num_bases, char **basenames)
1587 char term_dst[IT_MAX_WORD+1];
1588 RSET rset[60], result;
1590 struct grep_info grep_info;
1591 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1592 const char *termp = termz;
1595 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1599 logf (LOG_DEBUG, "APT_phrase termp=%s", termp);
1600 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1602 reg_type, complete_flag,
1603 num_bases, basenames,
1604 term_dst, rank_type,
1608 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1611 grep_info_delete (&grep_info);
1614 rset_null_parms parms;
1616 parms.rset_term = rset_term_create (termz, -1, rank_type,
1618 return rset_create (rset_kind_null, &parms);
1620 else if (rset_no == 1)
1622 result = rpn_prox (zh, rset, rset_no, 1, 0, 3, 1);
1623 for (i = 0; i<rset_no; i++)
1624 rset_delete (rset[i]);
1628 static RSET rpn_search_APT_or_list (ZebraHandle zh,
1629 Z_AttributesPlusTerm *zapt,
1630 const char *termz_org,
1631 oid_value attributeSet,
1633 int reg_type, int complete_flag,
1634 const char *rank_type,
1636 int num_bases, char **basenames)
1638 char term_dst[IT_MAX_WORD+1];
1639 RSET rset[60], result;
1641 struct grep_info grep_info;
1642 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1643 const char *termp = termz;
1645 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1649 logf (LOG_DEBUG, "APT_or_list termp=%s", termp);
1650 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1652 reg_type, complete_flag,
1653 num_bases, basenames,
1654 term_dst, rank_type,
1658 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1661 grep_info_delete (&grep_info);
1664 rset_null_parms parms;
1666 parms.rset_term = rset_term_create (termz, -1, rank_type,
1668 return rset_create (rset_kind_null, &parms);
1671 for (i = 1; i<rset_no; i++)
1673 rset_bool_parms bool_parms;
1675 bool_parms.rset_l = result;
1676 bool_parms.rset_r = rset[i];
1677 bool_parms.key_size = sizeof(struct it_key);
1678 bool_parms.cmp = key_compare_it;
1679 result = rset_create (rset_kind_or, &bool_parms);
1684 static RSET rpn_search_APT_and_list (ZebraHandle zh,
1685 Z_AttributesPlusTerm *zapt,
1686 const char *termz_org,
1687 oid_value attributeSet,
1689 int reg_type, int complete_flag,
1690 const char *rank_type,
1692 int num_bases, char **basenames)
1694 char term_dst[IT_MAX_WORD+1];
1695 RSET rset[60], result;
1697 struct grep_info grep_info;
1698 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1699 const char *termp = termz;
1701 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1705 logf (LOG_DEBUG, "APT_and_list termp=%s", termp);
1706 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1708 reg_type, complete_flag,
1709 num_bases, basenames,
1710 term_dst, rank_type,
1714 assert (rset[rset_no]);
1715 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1718 grep_info_delete (&grep_info);
1721 rset_null_parms parms;
1723 parms.rset_term = rset_term_create (termz, -1, rank_type,
1725 return rset_create (rset_kind_null, &parms);
1728 for (i = 1; i<rset_no; i++)
1730 rset_bool_parms bool_parms;
1732 bool_parms.rset_l = result;
1733 bool_parms.rset_r = rset[i];
1734 bool_parms.key_size = sizeof(struct it_key);
1735 bool_parms.cmp = key_compare_it;
1736 result = rset_create (rset_kind_and, &bool_parms);
1741 static int numeric_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1742 const char **term_sub,
1744 oid_value attributeSet,
1745 struct grep_info *grep_info,
1754 char *term_tmp = term_dict + strlen(term_dict);
1756 attr_init (&relation, zapt, 2);
1757 relation_value = attr_find (&relation, NULL);
1759 logf (LOG_DEBUG, "numeric relation value=%d", relation_value);
1761 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub, term_tmp, 1,
1764 term_value = atoi (term_tmp);
1765 switch (relation_value)
1768 logf (LOG_DEBUG, "Relation <");
1769 gen_regular_rel (term_tmp, term_value-1, 1);
1772 logf (LOG_DEBUG, "Relation <=");
1773 gen_regular_rel (term_tmp, term_value, 1);
1776 logf (LOG_DEBUG, "Relation >=");
1777 gen_regular_rel (term_tmp, term_value, 0);
1780 logf (LOG_DEBUG, "Relation >");
1781 gen_regular_rel (term_tmp, term_value+1, 0);
1785 logf (LOG_DEBUG, "Relation =");
1786 sprintf (term_tmp, "(0*%d)", term_value);
1788 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_tmp);
1789 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info, max_pos,
1792 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1793 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1797 static int numeric_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1798 const char **term_sub,
1799 oid_value attributeSet, struct grep_info *grep_info,
1800 int reg_type, int complete_flag,
1801 int num_bases, char **basenames,
1802 char *term_dst, int xpath_use, NMEM stream)
1804 char term_dict[2*IT_MAX_WORD+2];
1808 const char *use_string = 0;
1809 oid_value curAttributeSet = attributeSet;
1811 struct rpn_char_map_info rcmi;
1813 int bases_ok = 0; /* no of databases with OK attribute */
1814 int errCode = 0; /* err code (if any is not OK) */
1815 char *errString = 0; /* addinfo */
1817 rpn_char_map_prepare (zh->reg, reg_type, &rcmi);
1818 attr_init (&use, zapt, 1);
1819 use_value = attr_find_ex (&use, &curAttributeSet, &use_string);
1821 if (use_value == -1)
1824 for (base_no = 0; base_no < num_bases; base_no++)
1827 data1_local_attribute id_xpath_attr;
1828 data1_local_attribute *local_attr;
1829 int max_pos, prefix_len = 0;
1832 if (use_value == -2) /* string attribute (assume IDXPATH/any) */
1834 use_value = xpath_use;
1835 attp.local_attributes = &id_xpath_attr;
1836 attp.attset_ordinal = VAL_IDXPATH;
1837 id_xpath_attr.next = 0;
1838 id_xpath_attr.local = use_value;
1840 else if (curAttributeSet == VAL_IDXPATH)
1842 attp.local_attributes = &id_xpath_attr;
1843 attp.attset_ordinal = VAL_IDXPATH;
1844 id_xpath_attr.next = 0;
1845 id_xpath_attr.local = use_value;
1849 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
1851 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1852 curAttributeSet, use_value, r);
1856 sprintf (val_str, "%d", use_value);
1857 errString = nmem_strdup (stream, val_str);
1865 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
1867 zh->errCode = 109; /* Database unavailable */
1868 zh->errString = basenames[base_no];
1871 for (local_attr = attp.local_attributes; local_attr;
1872 local_attr = local_attr->next)
1878 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
1883 term_dict[prefix_len++] = '|';
1885 term_dict[prefix_len++] = '(';
1887 ord_len = key_SU_encode (ord, ord_buf);
1888 for (i = 0; i<ord_len; i++)
1890 term_dict[prefix_len++] = 1;
1891 term_dict[prefix_len++] = ord_buf[i];
1897 sprintf (val_str, "%d", use_value);
1899 errString = nmem_strdup (stream, val_str);
1903 term_dict[prefix_len++] = ')';
1904 term_dict[prefix_len++] = 1;
1905 term_dict[prefix_len++] = reg_type;
1906 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1907 term_dict[prefix_len] = '\0';
1908 if (!numeric_relation (zh, zapt, &termp, term_dict,
1909 attributeSet, grep_info, &max_pos, reg_type,
1915 zh->errCode = errCode;
1916 zh->errString = errString;
1920 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1924 static RSET rpn_search_APT_numeric (ZebraHandle zh,
1925 Z_AttributesPlusTerm *zapt,
1927 oid_value attributeSet,
1929 int reg_type, int complete_flag,
1930 const char *rank_type, int xpath_use,
1931 int num_bases, char **basenames)
1933 char term_dst[IT_MAX_WORD+1];
1934 const char *termp = termz;
1935 RSET rset[60], result;
1936 int i, r, rset_no = 0;
1937 struct grep_info grep_info;
1939 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1943 logf (LOG_DEBUG, "APT_numeric termp=%s", termp);
1944 grep_info.isam_p_indx = 0;
1945 r = numeric_term (zh, zapt, &termp, attributeSet, &grep_info,
1946 reg_type, complete_flag, num_bases, basenames,
1947 term_dst, xpath_use,
1951 logf (LOG_DEBUG, "term: %s", term_dst);
1952 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1953 grep_info.isam_p_indx, term_dst,
1954 strlen(term_dst), rank_type,
1955 0 /* preserve position */,
1957 assert (rset[rset_no]);
1958 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1961 grep_info_delete (&grep_info);
1964 rset_null_parms parms;
1966 parms.rset_term = rset_term_create (term_dst, -1, rank_type,
1968 return rset_create (rset_kind_null, &parms);
1971 for (i = 1; i<rset_no; i++)
1973 rset_bool_parms bool_parms;
1975 bool_parms.rset_l = result;
1976 bool_parms.rset_r = rset[i];
1977 bool_parms.key_size = sizeof(struct it_key);
1978 bool_parms.cmp = key_compare_it;
1979 result = rset_create (rset_kind_and, &bool_parms);
1984 static RSET rpn_search_APT_local (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1986 oid_value attributeSet,
1988 const char *rank_type)
1993 rset_temp_parms parms;
1995 parms.rset_term = rset_term_create (termz, -1, rank_type,
1997 parms.cmp = key_compare_it;
1998 parms.key_size = sizeof (struct it_key);
1999 parms.temp_path = res_get (zh->res, "setTmpDir");
2000 result = rset_create (rset_kind_temp, &parms);
2001 rsfd = rset_open (result, RSETF_WRITE);
2003 key.sysno = atoi (termz);
2007 rset_write (result, rsfd, &key);
2008 rset_close (result, rsfd);
2012 static RSET rpn_sort_spec (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2013 oid_value attributeSet, NMEM stream,
2014 Z_SortKeySpecList *sort_sequence,
2015 const char *rank_type)
2017 rset_null_parms parms;
2019 int sort_relation_value;
2020 AttrType sort_relation_type;
2025 Z_AttributeElement *ae;
2030 attr_init (&sort_relation_type, zapt, 7);
2031 sort_relation_value = attr_find (&sort_relation_type, &attributeSet);
2033 attr_init (&use_type, zapt, 1);
2034 use_value = attr_find (&use_type, &attributeSet);
2036 if (!sort_sequence->specs)
2038 sort_sequence->num_specs = 10;
2039 sort_sequence->specs = (Z_SortKeySpec **)
2040 nmem_malloc (stream, sort_sequence->num_specs *
2041 sizeof(*sort_sequence->specs));
2042 for (i = 0; i<sort_sequence->num_specs; i++)
2043 sort_sequence->specs[i] = 0;
2045 if (zapt->term->which != Z_Term_general)
2048 i = atoi_n ((char *) zapt->term->u.general->buf,
2049 zapt->term->u.general->len);
2050 if (i >= sort_sequence->num_specs)
2052 sprintf (termz, "%d", i);
2054 oe.proto = PROTO_Z3950;
2055 oe.oclass = CLASS_ATTSET;
2056 oe.value = attributeSet;
2057 if (!oid_ent_to_oid (&oe, oid))
2060 sks = (Z_SortKeySpec *) nmem_malloc (stream, sizeof(*sks));
2061 sks->sortElement = (Z_SortElement *)
2062 nmem_malloc (stream, sizeof(*sks->sortElement));
2063 sks->sortElement->which = Z_SortElement_generic;
2064 sk = sks->sortElement->u.generic = (Z_SortKey *)
2065 nmem_malloc (stream, sizeof(*sk));
2066 sk->which = Z_SortKey_sortAttributes;
2067 sk->u.sortAttributes = (Z_SortAttributes *)
2068 nmem_malloc (stream, sizeof(*sk->u.sortAttributes));
2070 sk->u.sortAttributes->id = oid;
2071 sk->u.sortAttributes->list = (Z_AttributeList *)
2072 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list));
2073 sk->u.sortAttributes->list->num_attributes = 1;
2074 sk->u.sortAttributes->list->attributes = (Z_AttributeElement **)
2075 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list->attributes));
2076 ae = *sk->u.sortAttributes->list->attributes = (Z_AttributeElement *)
2077 nmem_malloc (stream, sizeof(**sk->u.sortAttributes->list->attributes));
2078 ae->attributeSet = 0;
2079 ae->attributeType = (int *)
2080 nmem_malloc (stream, sizeof(*ae->attributeType));
2081 *ae->attributeType = 1;
2082 ae->which = Z_AttributeValue_numeric;
2083 ae->value.numeric = (int *)
2084 nmem_malloc (stream, sizeof(*ae->value.numeric));
2085 *ae->value.numeric = use_value;
2087 sks->sortRelation = (int *)
2088 nmem_malloc (stream, sizeof(*sks->sortRelation));
2089 if (sort_relation_value == 1)
2090 *sks->sortRelation = Z_SortRelation_ascending;
2091 else if (sort_relation_value == 2)
2092 *sks->sortRelation = Z_SortRelation_descending;
2094 *sks->sortRelation = Z_SortRelation_ascending;
2096 sks->caseSensitivity = (int *)
2097 nmem_malloc (stream, sizeof(*sks->caseSensitivity));
2098 *sks->caseSensitivity = 0;
2100 sks->which = Z_SortKeySpec_null;
2101 sks->u.null = odr_nullval ();
2102 sort_sequence->specs[i] = sks;
2104 parms.rset_term = rset_term_create (termz, -1, rank_type,
2106 return rset_create (rset_kind_null, &parms);
2109 /* pop - moved to xpath.c */
2112 struct xpath_predicate {
2115 #define XPATH_PREDICATE_RELATION 1
2121 #define XPATH_PREDICATE_BOOLEAN 2
2124 struct xpath_predicate *left;
2125 struct xpath_predicate *right;
2130 struct xpath_location_step {
2132 struct xpath_predicate *predicate;
2137 static int parse_xpath(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2138 oid_value attributeSet,
2139 struct xpath_location_step *xpath, int max, NMEM mem)
2141 oid_value curAttributeSet = attributeSet;
2143 const char *use_string = 0;
2145 attr_init (&use, zapt, 1);
2146 attr_find_ex (&use, &curAttributeSet, &use_string);
2148 if (!use_string || *use_string != '/')
2151 return zebra_parse_xpath_str(use_string, xpath, max, mem);
2156 static RSET xpath_trunc(ZebraHandle zh, NMEM stream,
2157 int reg_type, const char *term, int use,
2158 oid_value curAttributeSet)
2161 struct grep_info grep_info;
2162 char term_dict[2048];
2165 int ord = zebraExplain_lookupSU (zh->reg->zei, curAttributeSet, use);
2166 int ord_len, i, r, max_pos;
2167 int term_type = Z_Term_characterString;
2168 const char *flags = "void";
2170 if (grep_info_prepare (zh, 0 /* zapt */, &grep_info, '0', stream))
2172 rset_null_parms parms;
2174 parms.rset_term = rset_term_create (term, strlen(term),
2176 parms.rset_term->nn = 0;
2177 return rset_create (rset_kind_null, &parms);
2182 rset_null_parms parms;
2184 parms.rset_term = rset_term_create (term, strlen(term),
2186 parms.rset_term->nn = 0;
2187 return rset_create (rset_kind_null, &parms);
2190 term_dict[prefix_len++] = '|';
2192 term_dict[prefix_len++] = '(';
2194 ord_len = key_SU_encode (ord, ord_buf);
2195 for (i = 0; i<ord_len; i++)
2197 term_dict[prefix_len++] = 1;
2198 term_dict[prefix_len++] = ord_buf[i];
2200 term_dict[prefix_len++] = ')';
2201 term_dict[prefix_len++] = 1;
2202 term_dict[prefix_len++] = reg_type;
2204 strcpy (term_dict+prefix_len, term);
2206 grep_info.isam_p_indx = 0;
2207 r = dict_lookup_grep (zh->reg->dict, term_dict, 0,
2208 &grep_info, &max_pos, 0, grep_handle);
2209 yaz_log (LOG_LOG, "%s %d positions", term,
2210 grep_info.isam_p_indx);
2211 rset = rset_trunc (zh, grep_info.isam_p_buf,
2212 grep_info.isam_p_indx, term, strlen(term),
2213 flags, 1, term_type);
2214 grep_info_delete (&grep_info);
2218 static RSET rpn_search_xpath (ZebraHandle zh,
2219 oid_value attributeSet,
2220 int num_bases, char **basenames,
2221 NMEM stream, const char *rank_type, RSET rset,
2222 int xpath_len, struct xpath_location_step *xpath)
2224 oid_value curAttributeSet = attributeSet;
2231 yaz_log (LOG_LOG, "len=%d", xpath_len);
2232 for (i = 0; i<xpath_len; i++)
2234 yaz_log (LOG_LOG, "XPATH %d %s", i, xpath[i].part);
2238 curAttributeSet = VAL_IDXPATH;
2248 a[@attr=value]/b[@other=othervalue]
2250 /e/@a val range(e/,range(@a,freetext(w,1015,val),@a),e/)
2251 /a/b val range(b/a/,freetext(w,1016,val),b/a/)
2252 /a/b/@c val range(b/a/,range(@c,freetext(w,1016,val),@c),b/a/)
2253 /a/b[@c=y] val range(b/a/,freetext(w,1016,val),b/a/,@c=y)
2254 /a[@c=y]/b val range(a/,range(b/a/,freetext(w,1016,val),b/a/),a/,@c=y)
2255 /a[@c=x]/b[@c=y] range(a/,range(b/a/,freetext(w,1016,val),b/a/,@c=y),a/,@c=x)
2259 dict_grep_cmap (zh->reg->dict, 0, 0);
2261 for (base_no = 0; base_no < num_bases; base_no++)
2263 int level = xpath_len;
2266 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
2268 zh->errCode = 109; /* Database unavailable */
2269 zh->errString = basenames[base_no];
2272 while (--level >= 0)
2274 char xpath_rev[128];
2276 rset_between_parms parms;
2277 RSET rset_start_tag = 0, rset_end_tag = 0, rset_attr = 0;
2281 for (i = level; i >= 1; --i)
2283 const char *cp = xpath[i].part;
2289 memcpy (xpath_rev + len, "[^/]*", 5);
2292 else if (*cp == ' ')
2295 xpath_rev[len++] = 1;
2296 xpath_rev[len++] = ' ';
2300 xpath_rev[len++] = *cp;
2301 xpath_rev[len++] = '/';
2303 else if (i == 1) /* // case */
2305 xpath_rev[len++] = '.';
2306 xpath_rev[len++] = '*';
2311 if (xpath[level].predicate &&
2312 xpath[level].predicate->which == XPATH_PREDICATE_RELATION &&
2313 xpath[level].predicate->u.relation.name[0])
2315 WRBUF wbuf = wrbuf_alloc();
2316 wrbuf_puts(wbuf, xpath[level].predicate->u.relation.name+1);
2317 if (xpath[level].predicate->u.relation.value)
2319 const char *cp = xpath[level].predicate->u.relation.value;
2320 wrbuf_putc(wbuf, '=');
2324 if (strchr(REGEX_CHARS, *cp))
2325 wrbuf_putc(wbuf, '\\');
2326 wrbuf_putc(wbuf, *cp);
2330 wrbuf_puts(wbuf, "");
2331 rset_attr = xpath_trunc (
2332 zh, stream, '0', wrbuf_buf(wbuf), 3, curAttributeSet);
2333 wrbuf_free(wbuf, 1);
2340 yaz_log (LOG_LOG, "xpath_rev (%d) = %s", level, xpath_rev);
2341 if (strlen(xpath_rev))
2343 rset_start_tag = xpath_trunc(zh, stream,
2344 '0', xpath_rev, 1, curAttributeSet);
2346 rset_end_tag = xpath_trunc(zh, stream,
2347 '0', xpath_rev, 2, curAttributeSet);
2349 parms.key_size = sizeof(struct it_key);
2350 parms.cmp = key_compare_it;
2351 parms.rset_l = rset_start_tag;
2352 parms.rset_m = rset;
2353 parms.rset_r = rset_end_tag;
2354 parms.rset_attr = rset_attr;
2355 parms.printer = key_print_it;
2356 rset = rset_create (rset_kind_between, &parms);
2367 static RSET rpn_search_APT (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2368 oid_value attributeSet, NMEM stream,
2369 Z_SortKeySpecList *sort_sequence,
2370 int num_bases, char **basenames)
2373 char *search_type = NULL;
2374 char rank_type[128];
2377 char termz[IT_MAX_WORD+1];
2381 struct xpath_location_step xpath[10];
2383 zebra_maps_attr (zh->reg->zebra_maps, zapt, ®_id, &search_type,
2384 rank_type, &complete_flag, &sort_flag);
2386 logf (LOG_DEBUG, "reg_id=%c", reg_id);
2387 logf (LOG_DEBUG, "complete_flag=%d", complete_flag);
2388 logf (LOG_DEBUG, "search_type=%s", search_type);
2389 logf (LOG_DEBUG, "rank_type=%s", rank_type);
2391 if (zapt_term_to_utf8(zh, zapt, termz))
2395 return rpn_sort_spec (zh, zapt, attributeSet, stream, sort_sequence,
2397 xpath_len = parse_xpath(zh, zapt, attributeSet, xpath, 10, stream);
2401 if (xpath[xpath_len-1].part[0] == '@')
2405 if (!strcmp (search_type, "phrase"))
2407 rset = rpn_search_APT_phrase (zh, zapt, termz, attributeSet, stream,
2408 reg_id, complete_flag, rank_type,
2410 num_bases, basenames);
2412 else if (!strcmp (search_type, "and-list"))
2414 rset = rpn_search_APT_and_list (zh, zapt, termz, attributeSet, stream,
2415 reg_id, complete_flag, rank_type,
2417 num_bases, basenames);
2419 else if (!strcmp (search_type, "or-list"))
2421 rset = rpn_search_APT_or_list (zh, zapt, termz, attributeSet, stream,
2422 reg_id, complete_flag, rank_type,
2424 num_bases, basenames);
2426 else if (!strcmp (search_type, "local"))
2428 rset = rpn_search_APT_local (zh, zapt, termz, attributeSet, stream,
2431 else if (!strcmp (search_type, "numeric"))
2433 rset = rpn_search_APT_numeric (zh, zapt, termz, attributeSet, stream,
2434 reg_id, complete_flag, rank_type,
2436 num_bases, basenames);
2438 else if (!strcmp (search_type, "always"))
2444 return rpn_search_xpath (zh, attributeSet, num_bases, basenames,
2445 stream, rank_type, rset, xpath_len, xpath);
2448 static RSET rpn_search_structure (ZebraHandle zh, Z_RPNStructure *zs,
2449 oid_value attributeSet, NMEM stream,
2450 Z_SortKeySpecList *sort_sequence,
2451 int num_bases, char **basenames)
2454 if (zs->which == Z_RPNStructure_complex)
2456 Z_Operator *zop = zs->u.complex->roperator;
2457 rset_bool_parms bool_parms;
2459 bool_parms.rset_l = rpn_search_structure (zh, zs->u.complex->s1,
2460 attributeSet, stream,
2462 num_bases, basenames);
2463 if (bool_parms.rset_l == NULL)
2465 bool_parms.rset_r = rpn_search_structure (zh, zs->u.complex->s2,
2466 attributeSet, stream,
2468 num_bases, basenames);
2469 if (bool_parms.rset_r == NULL)
2471 rset_delete (bool_parms.rset_l);
2474 bool_parms.key_size = sizeof(struct it_key);
2475 bool_parms.cmp = key_compare_it;
2479 case Z_Operator_and:
2480 r = rset_create (rset_kind_and, &bool_parms);
2483 r = rset_create (rset_kind_or, &bool_parms);
2485 case Z_Operator_and_not:
2486 r = rset_create (rset_kind_not, &bool_parms);
2488 case Z_Operator_prox:
2489 if (zop->u.prox->which != Z_ProximityOperator_known)
2494 if (*zop->u.prox->u.known != Z_ProxUnit_word)
2496 char *val = (char *) nmem_malloc (stream, 16);
2498 zh->errString = val;
2499 sprintf (val, "%d", *zop->u.prox->u.known);
2506 rsets[0] = bool_parms.rset_l;
2507 rsets[1] = bool_parms.rset_r;
2509 r = rpn_prox (zh, rsets, 2,
2510 *zop->u.prox->ordered,
2511 (!zop->u.prox->exclusion ? 0 :
2512 *zop->u.prox->exclusion),
2513 *zop->u.prox->relationType,
2514 *zop->u.prox->distance);
2515 rset_delete (rsets[0]);
2516 rset_delete (rsets[1]);
2524 else if (zs->which == Z_RPNStructure_simple)
2526 if (zs->u.simple->which == Z_Operand_APT)
2528 logf (LOG_DEBUG, "rpn_search_APT");
2529 r = rpn_search_APT (zh, zs->u.simple->u.attributesPlusTerm,
2530 attributeSet, stream, sort_sequence,
2531 num_bases, basenames);
2533 else if (zs->u.simple->which == Z_Operand_resultSetId)
2535 logf (LOG_DEBUG, "rpn_search_ref");
2536 r = resultSetRef (zh, zs->u.simple->u.resultSetId);
2539 r = rset_create (rset_kind_null, NULL);
2542 nmem_strdup (stream, zs->u.simple->u.resultSetId);
2563 RSET rpn_search (ZebraHandle zh, NMEM nmem,
2564 Z_RPNQuery *rpn, int num_bases, char **basenames,
2565 const char *setname,
2570 oid_value attributeSet;
2571 Z_SortKeySpecList *sort_sequence;
2575 zh->errString = NULL;
2578 sort_sequence = (Z_SortKeySpecList *)
2579 nmem_malloc (nmem, sizeof(*sort_sequence));
2580 sort_sequence->num_specs = 10;
2581 sort_sequence->specs = (Z_SortKeySpec **)
2582 nmem_malloc (nmem, sort_sequence->num_specs *
2583 sizeof(*sort_sequence->specs));
2584 for (i = 0; i<sort_sequence->num_specs; i++)
2585 sort_sequence->specs[i] = 0;
2587 attrset = oid_getentbyoid (rpn->attributeSetId);
2588 attributeSet = attrset->value;
2589 rset = rpn_search_structure (zh, rpn->RPNStructure, attributeSet,
2590 nmem, sort_sequence, num_bases, basenames);
2595 logf (LOG_DEBUG, "search error: %d", zh->errCode);
2597 for (i = 0; sort_sequence->specs[i]; i++)
2599 sort_sequence->num_specs = i;
2601 resultSetRank (zh, sset, rset);
2604 logf (LOG_DEBUG, "resultSetSortSingle in rpn_search");
2605 resultSetSortSingle (zh, nmem, sset, rset,
2606 sort_sequence, &sort_status);
2609 logf (LOG_DEBUG, "resultSetSortSingle status = %d", zh->errCode);
2615 struct scan_info_entry {
2621 struct scan_info_entry *list;
2627 static int scan_handle (char *name, const char *info, int pos, void *client)
2629 int len_prefix, idx;
2630 struct scan_info *scan_info = (struct scan_info *) client;
2632 len_prefix = strlen(scan_info->prefix);
2633 if (memcmp (name, scan_info->prefix, len_prefix))
2635 if (pos > 0) idx = scan_info->after - pos + scan_info->before;
2638 scan_info->list[idx].term = (char *)
2639 odr_malloc (scan_info->odr, strlen(name + len_prefix)+1);
2640 strcpy (scan_info->list[idx].term, name + len_prefix);
2641 assert (*info == sizeof(ISAMS_P));
2642 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAMS_P));
2646 static void scan_term_untrans (ZebraHandle zh, NMEM stream, int reg_type,
2647 char **dst, const char *src)
2649 char term_src[IT_MAX_WORD];
2650 char term_dst[IT_MAX_WORD];
2652 term_untrans (zh, reg_type, term_src, src);
2654 if (zh->iconv_from_utf8 != 0)
2657 char *inbuf = term_src;
2658 size_t inleft = strlen(term_src);
2659 char *outbuf = term_dst;
2660 size_t outleft = sizeof(term_dst)-1;
2663 ret = yaz_iconv (zh->iconv_from_utf8, &inbuf, &inleft,
2665 if (ret == (size_t)(-1))
2668 len = outbuf - term_dst;
2669 *dst = nmem_malloc (stream, len + 1);
2671 memcpy (*dst, term_dst, len);
2675 *dst = nmem_strdup (stream, term_src);
2678 static void count_set (RSET r, int *count)
2686 logf (LOG_DEBUG, "count_set");
2689 rfd = rset_open (r, RSETF_READ);
2690 while (rset_read (r, rfd, &key, &term_index))
2692 if (key.sysno != psysno)
2699 rset_close (r, rfd);
2700 logf (LOG_DEBUG, "%d keys, %d records", kno, *count);
2703 void rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
2704 oid_value attributeset,
2705 int num_bases, char **basenames,
2706 int *position, int *num_entries, ZebraScanEntry **list,
2707 int *is_partial, RSET limit_set, int return_zero)
2710 int pos = *position;
2711 int num = *num_entries;
2715 char termz[IT_MAX_WORD+20];
2718 struct scan_info *scan_info_array;
2719 ZebraScanEntry *glist;
2720 int ords[32], ord_no = 0;
2723 int bases_ok = 0; /* no of databases with OK attribute */
2724 int errCode = 0; /* err code (if any is not OK) */
2725 char *errString = 0; /* addinfo */
2728 char *search_type = NULL;
2729 char rank_type[128];
2735 if (attributeset == VAL_NONE)
2736 attributeset = VAL_BIB1;
2741 int termset_value_numeric;
2742 const char *termset_value_string;
2743 attr_init (&termset, zapt, 8);
2744 termset_value_numeric =
2745 attr_find_ex (&termset, NULL, &termset_value_string);
2746 if (termset_value_numeric != -1)
2749 const char *termset_name = 0;
2751 if (termset_value_numeric != -2)
2754 sprintf (resname, "%d", termset_value_numeric);
2755 termset_name = resname;
2758 termset_name = termset_value_string;
2760 limit_set = resultSetRef (zh, termset_name);
2764 yaz_log (LOG_DEBUG, "position = %d, num = %d set=%d",
2765 pos, num, attributeset);
2767 attr_init (&use, zapt, 1);
2768 use_value = attr_find (&use, &attributeset);
2770 if (zebra_maps_attr (zh->reg->zebra_maps, zapt, ®_id, &search_type,
2771 rank_type, &complete_flag, &sort_flag))
2777 yaz_log (LOG_DEBUG, "use_value = %d", use_value);
2779 if (use_value == -1)
2781 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
2785 data1_local_attribute *local_attr;
2787 if ((r=att_getentbyatt (zh, &attp, attributeset, use_value)))
2789 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
2790 attributeset, use_value);
2794 sprintf (val_str, "%d", use_value);
2796 errString = odr_strdup (stream, val_str);
2802 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
2804 zh->errString = basenames[base_no];
2805 zh->errCode = 109; /* Database unavailable */
2810 for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
2811 local_attr = local_attr->next)
2815 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
2818 ords[ord_no++] = ord;
2821 if (!bases_ok && errCode)
2823 zh->errCode = errCode;
2824 zh->errString = errString;
2830 sprintf (val_str, "%d", use_value);
2832 zh->errString = odr_strdup (stream, val_str);
2837 /* prepare dictionary scanning */
2840 scan_info_array = (struct scan_info *)
2841 odr_malloc (stream, ord_no * sizeof(*scan_info_array));
2842 for (i = 0; i < ord_no; i++)
2844 int j, prefix_len = 0;
2845 int before_tmp = before, after_tmp = after;
2846 struct scan_info *scan_info = scan_info_array + i;
2847 struct rpn_char_map_info rcmi;
2849 rpn_char_map_prepare (zh->reg, reg_id, &rcmi);
2851 scan_info->before = before;
2852 scan_info->after = after;
2853 scan_info->odr = stream;
2855 scan_info->list = (struct scan_info_entry *)
2856 odr_malloc (stream, (before+after) * sizeof(*scan_info->list));
2857 for (j = 0; j<before+after; j++)
2858 scan_info->list[j].term = NULL;
2860 prefix_len += key_SU_encode (ords[i], termz + prefix_len);
2861 termz[prefix_len++] = reg_id;
2862 termz[prefix_len] = 0;
2863 strcpy (scan_info->prefix, termz);
2865 if (trans_scan_term (zh, zapt, termz+prefix_len, reg_id))
2868 dict_scan (zh->reg->dict, termz, &before_tmp, &after_tmp,
2869 scan_info, scan_handle);
2871 glist = (ZebraScanEntry *)
2872 odr_malloc (stream, (before+after)*sizeof(*glist));
2874 /* consider terms after main term */
2875 for (i = 0; i < ord_no; i++)
2879 for (i = 0; i<after; i++)
2882 const char *mterm = NULL;
2886 for (j = 0; j < ord_no; j++)
2888 if (ptr[j] < before+after &&
2889 (tst=scan_info_array[j].list[ptr[j]].term) &&
2890 (!mterm || strcmp (tst, mterm) < 0))
2898 scan_term_untrans (zh, stream->mem, reg_id,
2899 &glist[i+before].term, mterm);
2900 rset = rset_trunc (zh, &scan_info_array[j0].list[ptr[j0]].isam_p, 1,
2901 glist[i+before].term, strlen(glist[i+before].term),
2902 NULL, 0, zapt->term->which);
2905 for (j = j0+1; j<ord_no; j++)
2907 if (ptr[j] < before+after &&
2908 (tst=scan_info_array[j].list[ptr[j]].term) &&
2909 !strcmp (tst, mterm))
2911 rset_bool_parms bool_parms;
2915 rset_trunc (zh, &scan_info_array[j].list[ptr[j]].isam_p, 1,
2916 glist[i+before].term,
2917 strlen(glist[i+before].term), NULL, 0,
2920 bool_parms.key_size = sizeof(struct it_key);
2921 bool_parms.cmp = key_compare_it;
2922 bool_parms.rset_l = rset;
2923 bool_parms.rset_r = rset2;
2925 rset = rset_create (rset_kind_or, &bool_parms);
2932 rset_bool_parms bool_parms;
2934 bool_parms.key_size = sizeof(struct it_key);
2935 bool_parms.cmp = key_compare_it;
2936 bool_parms.rset_l = rset;
2937 bool_parms.rset_r = rset_dup(limit_set);
2939 rset = rset_create (rset_kind_and, &bool_parms);
2941 count_set (rset, &glist[i+before].occurrences);
2946 *num_entries -= (after-i);
2950 /* consider terms before main term */
2951 for (i = 0; i<ord_no; i++)
2954 for (i = 0; i<before; i++)
2957 const char *mterm = NULL;
2961 for (j = 0; j <ord_no; j++)
2963 if (ptr[j] < before &&
2964 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2965 (!mterm || strcmp (tst, mterm) > 0))
2974 scan_term_untrans (zh, stream->mem, reg_id,
2975 &glist[before-1-i].term, mterm);
2978 (zh, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1,
2979 glist[before-1-i].term, strlen(glist[before-1-i].term),
2980 NULL, 0, zapt->term->which);
2984 for (j = j0+1; j<ord_no; j++)
2986 if (ptr[j] < before &&
2987 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2988 !strcmp (tst, mterm))
2990 rset_bool_parms bool_parms;
2993 rset2 = rset_trunc (zh,
2994 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1,
2995 glist[before-1-i].term,
2996 strlen(glist[before-1-i].term), NULL, 0,
2999 bool_parms.key_size = sizeof(struct it_key);
3000 bool_parms.cmp = key_compare_it;
3001 bool_parms.rset_l = rset;
3002 bool_parms.rset_r = rset2;
3004 rset = rset_create (rset_kind_or, &bool_parms);
3011 rset_bool_parms bool_parms;
3013 bool_parms.key_size = sizeof(struct it_key);
3014 bool_parms.cmp = key_compare_it;
3015 bool_parms.rset_l = rset;
3016 bool_parms.rset_r = rset_dup(limit_set);
3018 rset = rset_create (rset_kind_and, &bool_parms);
3020 count_set (rset, &glist[before-1-i].occurrences);
3030 *list = glist + i; /* list is set to first 'real' entry */
3032 logf (LOG_DEBUG, "position = %d, num_entries = %d",
3033 *position, *num_entries);
3035 logf (LOG_DEBUG, "scan error: %d", zh->errCode);