1 /* $Id: zrpn.c,v 1.136 2004-01-22 11:27:21 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 1", 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];
1018 sprintf (val_str, "%d 2", use_value);
1020 errString = nmem_strdup (stream, val_str);
1024 bases_ok++; /* this has OK attributes */
1026 term_dict[prefix_len++] = ')';
1027 term_dict[prefix_len++] = 1;
1028 term_dict[prefix_len++] = reg_type;
1029 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1030 term_dict[prefix_len] = '\0';
1032 switch (truncation_value)
1034 case -1: /* not specified */
1035 case 100: /* do not truncate */
1036 if (!string_relation (zh, zapt, &termp, term_dict,
1038 reg_type, space_split, term_dst))
1040 logf (LOG_LOG, "dict_lookup_grep: %s", term_dict+prefix_len);
1041 r = dict_lookup_grep (zh->reg->dict, term_dict, 0,
1042 grep_info, &max_pos, 0, grep_handle);
1044 logf (LOG_WARN, "dict_lookup_grep fail %d", r);
1046 case 1: /* right truncation */
1047 term_dict[j++] = '(';
1048 if (!term_100 (zh->reg->zebra_maps, reg_type,
1049 &termp, term_dict + j, space_split, term_dst))
1051 strcat (term_dict, ".*)");
1052 dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1053 &max_pos, 0, grep_handle);
1055 case 2: /* keft truncation */
1056 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1057 if (!term_100 (zh->reg->zebra_maps, reg_type,
1058 &termp, term_dict + j, space_split, term_dst))
1060 strcat (term_dict, ")");
1061 dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1062 &max_pos, 0, grep_handle);
1064 case 3: /* left&right truncation */
1065 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1066 if (!term_100 (zh->reg->zebra_maps, reg_type,
1067 &termp, term_dict + j, space_split, term_dst))
1069 strcat (term_dict, ".*)");
1070 dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1071 &max_pos, 0, grep_handle);
1075 case 101: /* process # in term */
1076 term_dict[j++] = '(';
1077 if (!term_101 (zh->reg->zebra_maps, reg_type,
1078 &termp, term_dict + j, space_split, term_dst))
1080 strcat (term_dict, ")");
1081 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1082 &max_pos, 0, grep_handle);
1084 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d", r);
1086 case 102: /* Regexp-1 */
1087 term_dict[j++] = '(';
1088 if (!term_102 (zh->reg->zebra_maps, reg_type,
1089 &termp, term_dict + j, space_split, term_dst))
1091 strcat (term_dict, ")");
1092 logf (LOG_DEBUG, "Regexp-1 tolerance=%d", r);
1093 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1094 &max_pos, 0, grep_handle);
1096 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
1099 case 103: /* Regexp-2 */
1101 term_dict[j++] = '(';
1102 if (!term_103 (zh->reg->zebra_maps, reg_type,
1103 &termp, term_dict + j, &r, space_split, term_dst))
1105 strcat (term_dict, ")");
1106 logf (LOG_DEBUG, "Regexp-2 tolerance=%d", r);
1107 r = dict_lookup_grep (zh->reg->dict, term_dict, r, grep_info,
1108 &max_pos, 2, grep_handle);
1110 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
1113 case 104: /* process # and ! in term */
1114 term_dict[j++] = '(';
1115 if (!term_104 (zh->reg->zebra_maps, reg_type,
1116 &termp, term_dict + j, space_split, term_dst))
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);
1124 case 105: /* process * and ! in term */
1125 term_dict[j++] = '(';
1126 if (!term_105 (zh->reg->zebra_maps, reg_type,
1127 &termp, term_dict + j, space_split, term_dst, 1))
1129 strcat (term_dict, ")");
1130 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1131 &max_pos, 0, grep_handle);
1133 logf (LOG_WARN, "dict_lookup_grep err, trunc=*/!: %d", r);
1135 case 106: /* process * and ! in term */
1136 term_dict[j++] = '(';
1137 if (!term_105 (zh->reg->zebra_maps, reg_type,
1138 &termp, term_dict + j, space_split, term_dst, 0))
1140 strcat (term_dict, ")");
1141 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1142 &max_pos, 0, grep_handle);
1144 logf (LOG_WARN, "dict_lookup_grep err, trunc=*/!: %d", r);
1150 zh->errCode = errCode;
1151 zh->errString = errString;
1155 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1160 /* convert APT search term to UTF8 */
1161 static int zapt_term_to_utf8 (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1165 Z_Term *term = zapt->term;
1167 switch (term->which)
1169 case Z_Term_general:
1170 if (zh->iconv_to_utf8 != 0)
1172 char *inbuf = term->u.general->buf;
1173 size_t inleft = term->u.general->len;
1174 char *outbuf = termz;
1175 size_t outleft = IT_MAX_WORD-1;
1178 ret = yaz_iconv(zh->iconv_to_utf8, &inbuf, &inleft,
1180 if (ret == (size_t)(-1))
1182 ret = yaz_iconv(zh->iconv_to_utf8, 0, 0, 0, 0);
1190 sizez = term->u.general->len;
1191 if (sizez > IT_MAX_WORD-1)
1192 sizez = IT_MAX_WORD-1;
1193 memcpy (termz, term->u.general->buf, sizez);
1194 termz[sizez] = '\0';
1197 case Z_Term_characterString:
1198 sizez = strlen(term->u.characterString);
1199 if (sizez > IT_MAX_WORD-1)
1200 sizez = IT_MAX_WORD-1;
1201 memcpy (termz, term->u.characterString, sizez);
1202 termz[sizez] = '\0';
1211 /* convert APT SCAN term to internal cmap */
1212 static int trans_scan_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1213 char *termz, int reg_type)
1215 char termz0[IT_MAX_WORD];
1217 if (zapt_term_to_utf8(zh, zapt, termz0))
1218 return -1; /* error */
1222 const char *cp = (const char *) termz0;
1223 const char *cp_end = cp + strlen(cp);
1226 const char *space_map = NULL;
1229 while ((len = (cp_end - cp)) > 0)
1231 map = zebra_maps_input (zh->reg->zebra_maps, reg_type, &cp, len);
1232 if (**map == *CHR_SPACE)
1237 for (src = space_map; *src; src++)
1240 for (src = *map; *src; src++)
1249 static RSET rpn_prox (ZebraHandle zh, RSET *rset, int rset_no,
1250 int ordered, int exclusion, int relation, int distance)
1255 struct it_key **buf;
1257 char prox_term[1024];
1258 int length_prox_term = 0;
1259 int min_nn = 10000000;
1261 int term_type = Z_Term_characterString;
1262 const char *flags = NULL;
1264 rsfd = (RSFD *) xmalloc (sizeof(*rsfd)*rset_no);
1265 more = (int *) xmalloc (sizeof(*more)*rset_no);
1266 buf = (struct it_key **) xmalloc (sizeof(*buf)*rset_no);
1269 for (i = 0; i<rset_no; i++)
1272 for (j = 0; j<rset[i]->no_rset_terms; j++)
1274 const char *nflags = rset[i]->rset_terms[j]->flags;
1275 char *term = rset[i]->rset_terms[j]->name;
1276 int lterm = strlen(term);
1277 if (lterm + length_prox_term < sizeof(prox_term)-1)
1279 if (length_prox_term)
1280 prox_term[length_prox_term++] = ' ';
1281 strcpy (prox_term + length_prox_term, term);
1282 length_prox_term += lterm;
1284 if (min_nn > rset[i]->rset_terms[j]->nn)
1285 min_nn = rset[i]->rset_terms[j]->nn;
1287 term_type = rset[i]->rset_terms[j]->type;
1289 /* only if all term types are of type characterString .. */
1290 /* the resulting term is of that type */
1291 if (term_type != Z_Term_characterString)
1292 term_type = Z_Term_general;
1295 for (i = 0; i<rset_no; i++)
1300 for (i = 0; i<rset_no; i++)
1302 buf[i] = (struct it_key *) xmalloc (sizeof(**buf));
1303 rsfd[i] = rset_open (rset[i], RSETF_READ);
1304 if (!(more[i] = rset_read (rset[i], rsfd[i], buf[i], &term_index)))
1309 /* at least one is empty ... return null set */
1310 rset_null_parms parms;
1312 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1314 parms.rset_term->nn = 0;
1315 result = rset_create (rset_kind_null, &parms);
1317 else if (ordered && relation == 3 && exclusion == 0 && distance == 1)
1319 /* special proximity case = phrase search ... */
1320 rset_temp_parms parms;
1323 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1325 parms.rset_term->nn = min_nn;
1326 parms.cmp = key_compare_it;
1327 parms.key_size = sizeof (struct it_key);
1328 parms.temp_path = res_get (zh->res, "setTmpDir");
1329 result = rset_create (rset_kind_temp, &parms);
1330 rsfd_result = rset_open (result, RSETF_WRITE);
1334 for (i = 1; i<rset_no; i++)
1343 cmp = key_compare_it (buf[i], buf[i-1]);
1346 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1347 buf[i-1], &term_index);
1352 if (buf[i-1]->seqno+1 != buf[i]->seqno)
1354 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1355 buf[i-1], &term_index);
1361 more[i] = rset_read (rset[i], rsfd[i], buf[i],
1368 rset_write (result, rsfd_result, buf[0]);
1369 more[0] = rset_read (*rset, *rsfd, *buf, &term_index);
1372 rset_close (result, rsfd_result);
1374 else if (rset_no == 2)
1376 /* generic proximity case (two input sets only) ... */
1377 rset_temp_parms parms;
1380 yaz_log (LOG_LOG, "generic prox, dist=%d, relation=%d, ordered=%d"
1382 distance, relation, ordered, exclusion);
1383 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1385 parms.rset_term->nn = min_nn;
1386 parms.cmp = key_compare_it;
1387 parms.key_size = sizeof (struct it_key);
1388 parms.temp_path = res_get (zh->res, "setTmpDir");
1389 result = rset_create (rset_kind_temp, &parms);
1390 rsfd_result = rset_open (result, RSETF_WRITE);
1392 while (more[0] && more[1])
1394 int cmp = key_compare_it (buf[0], buf[1]);
1396 more[0] = rset_read (rset[0], rsfd[0], buf[0], &term_index);
1398 more[1] = rset_read (rset[1], rsfd[1], buf[1], &term_index);
1401 int sysno = buf[0]->sysno;
1405 seqno[n++] = buf[0]->seqno;
1406 while ((more[0] = rset_read (rset[0], rsfd[0], buf[0],
1408 sysno == buf[0]->sysno)
1410 seqno[n++] = buf[0]->seqno;
1413 for (i = 0; i<n; i++)
1415 int diff = buf[1]->seqno - seqno[i];
1416 int excl = exclusion;
1417 if (!ordered && 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)
1442 if (diff != distance && diff >= 0)
1448 rset_write (result, rsfd_result, buf[1]);
1452 } while ((more[1] = rset_read (rset[1], rsfd[1], buf[1],
1454 sysno == buf[1]->sysno);
1457 rset_close (result, rsfd_result);
1461 rset_null_parms parms;
1463 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1465 parms.rset_term->nn = 0;
1466 result = rset_create (rset_kind_null, &parms);
1468 for (i = 0; i<rset_no; i++)
1471 rset_close (rset[i], rsfd[i]);
1481 char *normalize_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1482 const char *termz, NMEM stream, unsigned reg_id)
1485 AttrType truncation;
1486 int truncation_value;
1489 attr_init (&truncation, zapt, 5);
1490 truncation_value = attr_find (&truncation, NULL);
1492 switch (truncation_value)
1512 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, ex_list,
1513 termz, strlen(termz));
1515 return nmem_strdup(stream, termz);
1518 char *buf = (char*) nmem_malloc (stream, wrbuf_len(wrbuf)+1);
1519 memcpy (buf, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1520 buf[wrbuf_len(wrbuf)] = '\0';
1525 static void grep_info_delete (struct grep_info *grep_info)
1528 xfree(grep_info->term_no);
1530 xfree (grep_info->isam_p_buf);
1533 static int grep_info_prepare (ZebraHandle zh,
1534 Z_AttributesPlusTerm *zapt,
1535 struct grep_info *grep_info,
1540 int termset_value_numeric;
1541 const char *termset_value_string;
1544 grep_info->term_no = 0;
1546 grep_info->isam_p_size = 0;
1547 grep_info->isam_p_buf = NULL;
1549 grep_info->reg_type = reg_type;
1550 grep_info->termset = 0;
1554 attr_init (&termset, zapt, 8);
1555 termset_value_numeric =
1556 attr_find_ex (&termset, NULL, &termset_value_string);
1557 if (termset_value_numeric != -1)
1560 const char *termset_name = 0;
1561 if (termset_value_numeric != -2)
1564 sprintf (resname, "%d", termset_value_numeric);
1565 termset_name = resname;
1568 termset_name = termset_value_string;
1569 logf (LOG_LOG, "creating termset set %s", termset_name);
1570 grep_info->termset = resultSetAdd (zh, termset_name, 1);
1571 if (!grep_info->termset)
1574 zh->errString = nmem_strdup (stream, termset_name);
1582 static RSET rpn_search_APT_phrase (ZebraHandle zh,
1583 Z_AttributesPlusTerm *zapt,
1584 const char *termz_org,
1585 oid_value attributeSet,
1587 int reg_type, int complete_flag,
1588 const char *rank_type, int xpath_use,
1589 int num_bases, char **basenames)
1591 char term_dst[IT_MAX_WORD+1];
1592 RSET rset[60], result;
1594 struct grep_info grep_info;
1595 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1596 const char *termp = termz;
1599 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1603 logf (LOG_DEBUG, "APT_phrase termp=%s", termp);
1604 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1606 reg_type, complete_flag,
1607 num_bases, basenames,
1608 term_dst, rank_type,
1612 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1615 grep_info_delete (&grep_info);
1618 rset_null_parms parms;
1620 parms.rset_term = rset_term_create (termz, -1, rank_type,
1622 return rset_create (rset_kind_null, &parms);
1624 else if (rset_no == 1)
1626 result = rpn_prox (zh, rset, rset_no, 1, 0, 3, 1);
1627 for (i = 0; i<rset_no; i++)
1628 rset_delete (rset[i]);
1632 static RSET rpn_search_APT_or_list (ZebraHandle zh,
1633 Z_AttributesPlusTerm *zapt,
1634 const char *termz_org,
1635 oid_value attributeSet,
1637 int reg_type, int complete_flag,
1638 const char *rank_type,
1640 int num_bases, char **basenames)
1642 char term_dst[IT_MAX_WORD+1];
1643 RSET rset[60], result;
1645 struct grep_info grep_info;
1646 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1647 const char *termp = termz;
1649 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1653 logf (LOG_DEBUG, "APT_or_list termp=%s", termp);
1654 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1656 reg_type, complete_flag,
1657 num_bases, basenames,
1658 term_dst, rank_type,
1662 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1665 grep_info_delete (&grep_info);
1668 rset_null_parms parms;
1670 parms.rset_term = rset_term_create (termz, -1, rank_type,
1672 return rset_create (rset_kind_null, &parms);
1675 for (i = 1; i<rset_no; i++)
1677 rset_bool_parms bool_parms;
1679 bool_parms.rset_l = result;
1680 bool_parms.rset_r = rset[i];
1681 bool_parms.key_size = sizeof(struct it_key);
1682 bool_parms.cmp = key_compare_it;
1683 result = rset_create (rset_kind_or, &bool_parms);
1688 static RSET rpn_search_APT_and_list (ZebraHandle zh,
1689 Z_AttributesPlusTerm *zapt,
1690 const char *termz_org,
1691 oid_value attributeSet,
1693 int reg_type, int complete_flag,
1694 const char *rank_type,
1696 int num_bases, char **basenames)
1698 char term_dst[IT_MAX_WORD+1];
1699 RSET rset[60], result;
1701 struct grep_info grep_info;
1702 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1703 const char *termp = termz;
1705 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1709 logf (LOG_DEBUG, "APT_and_list termp=%s", termp);
1710 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1712 reg_type, complete_flag,
1713 num_bases, basenames,
1714 term_dst, rank_type,
1718 assert (rset[rset_no]);
1719 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1722 grep_info_delete (&grep_info);
1725 rset_null_parms parms;
1727 parms.rset_term = rset_term_create (termz, -1, rank_type,
1729 return rset_create (rset_kind_null, &parms);
1732 for (i = 1; i<rset_no; i++)
1734 rset_bool_parms bool_parms;
1736 bool_parms.rset_l = result;
1737 bool_parms.rset_r = rset[i];
1738 bool_parms.key_size = sizeof(struct it_key);
1739 bool_parms.cmp = key_compare_it;
1740 result = rset_create (rset_kind_and, &bool_parms);
1745 static int numeric_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1746 const char **term_sub,
1748 oid_value attributeSet,
1749 struct grep_info *grep_info,
1758 char *term_tmp = term_dict + strlen(term_dict);
1760 attr_init (&relation, zapt, 2);
1761 relation_value = attr_find (&relation, NULL);
1763 logf (LOG_DEBUG, "numeric relation value=%d", relation_value);
1765 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub, term_tmp, 1,
1768 term_value = atoi (term_tmp);
1769 switch (relation_value)
1772 logf (LOG_DEBUG, "Relation <");
1773 gen_regular_rel (term_tmp, term_value-1, 1);
1776 logf (LOG_DEBUG, "Relation <=");
1777 gen_regular_rel (term_tmp, term_value, 1);
1780 logf (LOG_DEBUG, "Relation >=");
1781 gen_regular_rel (term_tmp, term_value, 0);
1784 logf (LOG_DEBUG, "Relation >");
1785 gen_regular_rel (term_tmp, term_value+1, 0);
1789 logf (LOG_DEBUG, "Relation =");
1790 sprintf (term_tmp, "(0*%d)", term_value);
1792 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_tmp);
1793 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info, max_pos,
1796 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1797 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1801 static int numeric_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1802 const char **term_sub,
1803 oid_value attributeSet, struct grep_info *grep_info,
1804 int reg_type, int complete_flag,
1805 int num_bases, char **basenames,
1806 char *term_dst, int xpath_use, NMEM stream)
1808 char term_dict[2*IT_MAX_WORD+2];
1812 const char *use_string = 0;
1813 oid_value curAttributeSet = attributeSet;
1815 struct rpn_char_map_info rcmi;
1817 int bases_ok = 0; /* no of databases with OK attribute */
1818 int errCode = 0; /* err code (if any is not OK) */
1819 char *errString = 0; /* addinfo */
1821 rpn_char_map_prepare (zh->reg, reg_type, &rcmi);
1822 attr_init (&use, zapt, 1);
1823 use_value = attr_find_ex (&use, &curAttributeSet, &use_string);
1825 if (use_value == -1)
1828 for (base_no = 0; base_no < num_bases; base_no++)
1831 data1_local_attribute id_xpath_attr;
1832 data1_local_attribute *local_attr;
1833 int max_pos, prefix_len = 0;
1836 if (use_value == -2) /* string attribute (assume IDXPATH/any) */
1838 use_value = xpath_use;
1839 attp.local_attributes = &id_xpath_attr;
1840 attp.attset_ordinal = VAL_IDXPATH;
1841 id_xpath_attr.next = 0;
1842 id_xpath_attr.local = use_value;
1844 else if (curAttributeSet == VAL_IDXPATH)
1846 attp.local_attributes = &id_xpath_attr;
1847 attp.attset_ordinal = VAL_IDXPATH;
1848 id_xpath_attr.next = 0;
1849 id_xpath_attr.local = use_value;
1853 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
1855 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1856 curAttributeSet, use_value, r);
1860 sprintf (val_str, "%d", use_value);
1861 errString = nmem_strdup (stream, val_str);
1869 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
1871 zh->errCode = 109; /* Database unavailable */
1872 zh->errString = basenames[base_no];
1875 for (local_attr = attp.local_attributes; local_attr;
1876 local_attr = local_attr->next)
1882 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
1887 term_dict[prefix_len++] = '|';
1889 term_dict[prefix_len++] = '(';
1891 ord_len = key_SU_encode (ord, ord_buf);
1892 for (i = 0; i<ord_len; i++)
1894 term_dict[prefix_len++] = 1;
1895 term_dict[prefix_len++] = ord_buf[i];
1901 sprintf (val_str, "%d", use_value);
1903 errString = nmem_strdup (stream, val_str);
1907 term_dict[prefix_len++] = ')';
1908 term_dict[prefix_len++] = 1;
1909 term_dict[prefix_len++] = reg_type;
1910 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1911 term_dict[prefix_len] = '\0';
1912 if (!numeric_relation (zh, zapt, &termp, term_dict,
1913 attributeSet, grep_info, &max_pos, reg_type,
1919 zh->errCode = errCode;
1920 zh->errString = errString;
1924 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1928 static RSET rpn_search_APT_numeric (ZebraHandle zh,
1929 Z_AttributesPlusTerm *zapt,
1931 oid_value attributeSet,
1933 int reg_type, int complete_flag,
1934 const char *rank_type, int xpath_use,
1935 int num_bases, char **basenames)
1937 char term_dst[IT_MAX_WORD+1];
1938 const char *termp = termz;
1939 RSET rset[60], result;
1940 int i, r, rset_no = 0;
1941 struct grep_info grep_info;
1943 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1947 logf (LOG_DEBUG, "APT_numeric termp=%s", termp);
1948 grep_info.isam_p_indx = 0;
1949 r = numeric_term (zh, zapt, &termp, attributeSet, &grep_info,
1950 reg_type, complete_flag, num_bases, basenames,
1951 term_dst, xpath_use,
1955 logf (LOG_DEBUG, "term: %s", term_dst);
1956 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1957 grep_info.isam_p_indx, term_dst,
1958 strlen(term_dst), rank_type,
1959 0 /* preserve position */,
1961 assert (rset[rset_no]);
1962 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1965 grep_info_delete (&grep_info);
1968 rset_null_parms parms;
1970 parms.rset_term = rset_term_create (term_dst, -1, rank_type,
1972 return rset_create (rset_kind_null, &parms);
1975 for (i = 1; i<rset_no; i++)
1977 rset_bool_parms bool_parms;
1979 bool_parms.rset_l = result;
1980 bool_parms.rset_r = rset[i];
1981 bool_parms.key_size = sizeof(struct it_key);
1982 bool_parms.cmp = key_compare_it;
1983 result = rset_create (rset_kind_and, &bool_parms);
1988 static RSET rpn_search_APT_local (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1990 oid_value attributeSet,
1992 const char *rank_type)
1997 rset_temp_parms parms;
1999 parms.rset_term = rset_term_create (termz, -1, rank_type,
2001 parms.cmp = key_compare_it;
2002 parms.key_size = sizeof (struct it_key);
2003 parms.temp_path = res_get (zh->res, "setTmpDir");
2004 result = rset_create (rset_kind_temp, &parms);
2005 rsfd = rset_open (result, RSETF_WRITE);
2007 key.sysno = atoi (termz);
2011 rset_write (result, rsfd, &key);
2012 rset_close (result, rsfd);
2016 static RSET rpn_sort_spec (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2017 oid_value attributeSet, NMEM stream,
2018 Z_SortKeySpecList *sort_sequence,
2019 const char *rank_type)
2021 rset_null_parms parms;
2023 int sort_relation_value;
2024 AttrType sort_relation_type;
2029 Z_AttributeElement *ae;
2034 attr_init (&sort_relation_type, zapt, 7);
2035 sort_relation_value = attr_find (&sort_relation_type, &attributeSet);
2037 attr_init (&use_type, zapt, 1);
2038 use_value = attr_find (&use_type, &attributeSet);
2040 if (!sort_sequence->specs)
2042 sort_sequence->num_specs = 10;
2043 sort_sequence->specs = (Z_SortKeySpec **)
2044 nmem_malloc (stream, sort_sequence->num_specs *
2045 sizeof(*sort_sequence->specs));
2046 for (i = 0; i<sort_sequence->num_specs; i++)
2047 sort_sequence->specs[i] = 0;
2049 if (zapt->term->which != Z_Term_general)
2052 i = atoi_n ((char *) zapt->term->u.general->buf,
2053 zapt->term->u.general->len);
2054 if (i >= sort_sequence->num_specs)
2056 sprintf (termz, "%d", i);
2058 oe.proto = PROTO_Z3950;
2059 oe.oclass = CLASS_ATTSET;
2060 oe.value = attributeSet;
2061 if (!oid_ent_to_oid (&oe, oid))
2064 sks = (Z_SortKeySpec *) nmem_malloc (stream, sizeof(*sks));
2065 sks->sortElement = (Z_SortElement *)
2066 nmem_malloc (stream, sizeof(*sks->sortElement));
2067 sks->sortElement->which = Z_SortElement_generic;
2068 sk = sks->sortElement->u.generic = (Z_SortKey *)
2069 nmem_malloc (stream, sizeof(*sk));
2070 sk->which = Z_SortKey_sortAttributes;
2071 sk->u.sortAttributes = (Z_SortAttributes *)
2072 nmem_malloc (stream, sizeof(*sk->u.sortAttributes));
2074 sk->u.sortAttributes->id = oid;
2075 sk->u.sortAttributes->list = (Z_AttributeList *)
2076 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list));
2077 sk->u.sortAttributes->list->num_attributes = 1;
2078 sk->u.sortAttributes->list->attributes = (Z_AttributeElement **)
2079 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list->attributes));
2080 ae = *sk->u.sortAttributes->list->attributes = (Z_AttributeElement *)
2081 nmem_malloc (stream, sizeof(**sk->u.sortAttributes->list->attributes));
2082 ae->attributeSet = 0;
2083 ae->attributeType = (int *)
2084 nmem_malloc (stream, sizeof(*ae->attributeType));
2085 *ae->attributeType = 1;
2086 ae->which = Z_AttributeValue_numeric;
2087 ae->value.numeric = (int *)
2088 nmem_malloc (stream, sizeof(*ae->value.numeric));
2089 *ae->value.numeric = use_value;
2091 sks->sortRelation = (int *)
2092 nmem_malloc (stream, sizeof(*sks->sortRelation));
2093 if (sort_relation_value == 1)
2094 *sks->sortRelation = Z_SortRelation_ascending;
2095 else if (sort_relation_value == 2)
2096 *sks->sortRelation = Z_SortRelation_descending;
2098 *sks->sortRelation = Z_SortRelation_ascending;
2100 sks->caseSensitivity = (int *)
2101 nmem_malloc (stream, sizeof(*sks->caseSensitivity));
2102 *sks->caseSensitivity = 0;
2104 sks->which = Z_SortKeySpec_null;
2105 sks->u.null = odr_nullval ();
2106 sort_sequence->specs[i] = sks;
2108 parms.rset_term = rset_term_create (termz, -1, rank_type,
2110 return rset_create (rset_kind_null, &parms);
2113 /* pop - moved to xpath.c */
2116 struct xpath_predicate {
2119 #define XPATH_PREDICATE_RELATION 1
2125 #define XPATH_PREDICATE_BOOLEAN 2
2128 struct xpath_predicate *left;
2129 struct xpath_predicate *right;
2134 struct xpath_location_step {
2136 struct xpath_predicate *predicate;
2141 static int parse_xpath(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2142 oid_value attributeSet,
2143 struct xpath_location_step *xpath, int max, NMEM mem)
2145 oid_value curAttributeSet = attributeSet;
2147 const char *use_string = 0;
2149 attr_init (&use, zapt, 1);
2150 attr_find_ex (&use, &curAttributeSet, &use_string);
2152 if (!use_string || *use_string != '/')
2155 return zebra_parse_xpath_str(use_string, xpath, max, mem);
2160 static RSET xpath_trunc(ZebraHandle zh, NMEM stream,
2161 int reg_type, const char *term, int use,
2162 oid_value curAttributeSet)
2165 struct grep_info grep_info;
2166 char term_dict[2048];
2169 int ord = zebraExplain_lookupSU (zh->reg->zei, curAttributeSet, use);
2170 int ord_len, i, r, max_pos;
2171 int term_type = Z_Term_characterString;
2172 const char *flags = "void";
2174 if (grep_info_prepare (zh, 0 /* zapt */, &grep_info, '0', stream))
2176 rset_null_parms parms;
2178 parms.rset_term = rset_term_create (term, strlen(term),
2180 parms.rset_term->nn = 0;
2181 return rset_create (rset_kind_null, &parms);
2186 rset_null_parms parms;
2188 parms.rset_term = rset_term_create (term, strlen(term),
2190 parms.rset_term->nn = 0;
2191 return rset_create (rset_kind_null, &parms);
2194 term_dict[prefix_len++] = '|';
2196 term_dict[prefix_len++] = '(';
2198 ord_len = key_SU_encode (ord, ord_buf);
2199 for (i = 0; i<ord_len; i++)
2201 term_dict[prefix_len++] = 1;
2202 term_dict[prefix_len++] = ord_buf[i];
2204 term_dict[prefix_len++] = ')';
2205 term_dict[prefix_len++] = 1;
2206 term_dict[prefix_len++] = reg_type;
2208 strcpy (term_dict+prefix_len, term);
2210 grep_info.isam_p_indx = 0;
2211 r = dict_lookup_grep (zh->reg->dict, term_dict, 0,
2212 &grep_info, &max_pos, 0, grep_handle);
2213 yaz_log (LOG_LOG, "%s %d positions", term,
2214 grep_info.isam_p_indx);
2215 rset = rset_trunc (zh, grep_info.isam_p_buf,
2216 grep_info.isam_p_indx, term, strlen(term),
2217 flags, 1, term_type);
2218 grep_info_delete (&grep_info);
2222 static RSET rpn_search_xpath (ZebraHandle zh,
2223 oid_value attributeSet,
2224 int num_bases, char **basenames,
2225 NMEM stream, const char *rank_type, RSET rset,
2226 int xpath_len, struct xpath_location_step *xpath)
2228 oid_value curAttributeSet = attributeSet;
2235 yaz_log (LOG_LOG, "len=%d", xpath_len);
2236 for (i = 0; i<xpath_len; i++)
2238 yaz_log (LOG_LOG, "XPATH %d %s", i, xpath[i].part);
2242 curAttributeSet = VAL_IDXPATH;
2252 a[@attr=value]/b[@other=othervalue]
2254 /e/@a val range(e/,range(@a,freetext(w,1015,val),@a),e/)
2255 /a/b val range(b/a/,freetext(w,1016,val),b/a/)
2256 /a/b/@c val range(b/a/,range(@c,freetext(w,1016,val),@c),b/a/)
2257 /a/b[@c=y] val range(b/a/,freetext(w,1016,val),b/a/,@c=y)
2258 /a[@c=y]/b val range(a/,range(b/a/,freetext(w,1016,val),b/a/),a/,@c=y)
2259 /a[@c=x]/b[@c=y] range(a/,range(b/a/,freetext(w,1016,val),b/a/,@c=y),a/,@c=x)
2263 dict_grep_cmap (zh->reg->dict, 0, 0);
2265 for (base_no = 0; base_no < num_bases; base_no++)
2267 int level = xpath_len;
2270 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
2272 zh->errCode = 109; /* Database unavailable */
2273 zh->errString = basenames[base_no];
2276 while (--level >= 0)
2278 char xpath_rev[128];
2280 rset_between_parms parms;
2281 RSET rset_start_tag = 0, rset_end_tag = 0, rset_attr = 0;
2285 for (i = level; i >= 1; --i)
2287 const char *cp = xpath[i].part;
2293 memcpy (xpath_rev + len, "[^/]*", 5);
2296 else if (*cp == ' ')
2299 xpath_rev[len++] = 1;
2300 xpath_rev[len++] = ' ';
2304 xpath_rev[len++] = *cp;
2305 xpath_rev[len++] = '/';
2307 else if (i == 1) /* // case */
2309 xpath_rev[len++] = '.';
2310 xpath_rev[len++] = '*';
2315 if (xpath[level].predicate &&
2316 xpath[level].predicate->which == XPATH_PREDICATE_RELATION &&
2317 xpath[level].predicate->u.relation.name[0])
2319 WRBUF wbuf = wrbuf_alloc();
2320 wrbuf_puts(wbuf, xpath[level].predicate->u.relation.name+1);
2321 if (xpath[level].predicate->u.relation.value)
2323 const char *cp = xpath[level].predicate->u.relation.value;
2324 wrbuf_putc(wbuf, '=');
2328 if (strchr(REGEX_CHARS, *cp))
2329 wrbuf_putc(wbuf, '\\');
2330 wrbuf_putc(wbuf, *cp);
2334 wrbuf_puts(wbuf, "");
2335 rset_attr = xpath_trunc (
2336 zh, stream, '0', wrbuf_buf(wbuf), 3, curAttributeSet);
2337 wrbuf_free(wbuf, 1);
2344 yaz_log (LOG_LOG, "xpath_rev (%d) = %s", level, xpath_rev);
2345 if (strlen(xpath_rev))
2347 rset_start_tag = xpath_trunc(zh, stream,
2348 '0', xpath_rev, 1, curAttributeSet);
2350 rset_end_tag = xpath_trunc(zh, stream,
2351 '0', xpath_rev, 2, curAttributeSet);
2353 parms.key_size = sizeof(struct it_key);
2354 parms.cmp = key_compare_it;
2355 parms.rset_l = rset_start_tag;
2356 parms.rset_m = rset;
2357 parms.rset_r = rset_end_tag;
2358 parms.rset_attr = rset_attr;
2359 parms.printer = key_print_it;
2360 rset = rset_create (rset_kind_between, &parms);
2371 static RSET rpn_search_APT (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2372 oid_value attributeSet, NMEM stream,
2373 Z_SortKeySpecList *sort_sequence,
2374 int num_bases, char **basenames)
2377 char *search_type = NULL;
2378 char rank_type[128];
2381 char termz[IT_MAX_WORD+1];
2385 struct xpath_location_step xpath[10];
2387 zebra_maps_attr (zh->reg->zebra_maps, zapt, ®_id, &search_type,
2388 rank_type, &complete_flag, &sort_flag);
2390 logf (LOG_DEBUG, "reg_id=%c", reg_id);
2391 logf (LOG_DEBUG, "complete_flag=%d", complete_flag);
2392 logf (LOG_DEBUG, "search_type=%s", search_type);
2393 logf (LOG_DEBUG, "rank_type=%s", rank_type);
2395 if (zapt_term_to_utf8(zh, zapt, termz))
2399 return rpn_sort_spec (zh, zapt, attributeSet, stream, sort_sequence,
2401 xpath_len = parse_xpath(zh, zapt, attributeSet, xpath, 10, stream);
2405 if (xpath[xpath_len-1].part[0] == '@')
2409 if (!strcmp (search_type, "phrase"))
2411 rset = rpn_search_APT_phrase (zh, zapt, termz, attributeSet, stream,
2412 reg_id, complete_flag, rank_type,
2414 num_bases, basenames);
2416 else if (!strcmp (search_type, "and-list"))
2418 rset = rpn_search_APT_and_list (zh, zapt, termz, attributeSet, stream,
2419 reg_id, complete_flag, rank_type,
2421 num_bases, basenames);
2423 else if (!strcmp (search_type, "or-list"))
2425 rset = rpn_search_APT_or_list (zh, zapt, termz, attributeSet, stream,
2426 reg_id, complete_flag, rank_type,
2428 num_bases, basenames);
2430 else if (!strcmp (search_type, "local"))
2432 rset = rpn_search_APT_local (zh, zapt, termz, attributeSet, stream,
2435 else if (!strcmp (search_type, "numeric"))
2437 rset = rpn_search_APT_numeric (zh, zapt, termz, attributeSet, stream,
2438 reg_id, complete_flag, rank_type,
2440 num_bases, basenames);
2442 else if (!strcmp (search_type, "always"))
2448 return rpn_search_xpath (zh, attributeSet, num_bases, basenames,
2449 stream, rank_type, rset, xpath_len, xpath);
2452 static RSET rpn_search_structure (ZebraHandle zh, Z_RPNStructure *zs,
2453 oid_value attributeSet, NMEM stream,
2454 Z_SortKeySpecList *sort_sequence,
2455 int num_bases, char **basenames)
2458 if (zs->which == Z_RPNStructure_complex)
2460 Z_Operator *zop = zs->u.complex->roperator;
2461 rset_bool_parms bool_parms;
2463 bool_parms.rset_l = rpn_search_structure (zh, zs->u.complex->s1,
2464 attributeSet, stream,
2466 num_bases, basenames);
2467 if (bool_parms.rset_l == NULL)
2469 bool_parms.rset_r = rpn_search_structure (zh, zs->u.complex->s2,
2470 attributeSet, stream,
2472 num_bases, basenames);
2473 if (bool_parms.rset_r == NULL)
2475 rset_delete (bool_parms.rset_l);
2478 bool_parms.key_size = sizeof(struct it_key);
2479 bool_parms.cmp = key_compare_it;
2483 case Z_Operator_and:
2484 r = rset_create (rset_kind_and, &bool_parms);
2487 r = rset_create (rset_kind_or, &bool_parms);
2489 case Z_Operator_and_not:
2490 r = rset_create (rset_kind_not, &bool_parms);
2492 case Z_Operator_prox:
2493 if (zop->u.prox->which != Z_ProximityOperator_known)
2498 if (*zop->u.prox->u.known != Z_ProxUnit_word)
2500 char *val = (char *) nmem_malloc (stream, 16);
2502 zh->errString = val;
2503 sprintf (val, "%d", *zop->u.prox->u.known);
2510 rsets[0] = bool_parms.rset_l;
2511 rsets[1] = bool_parms.rset_r;
2513 r = rpn_prox (zh, rsets, 2,
2514 *zop->u.prox->ordered,
2515 (!zop->u.prox->exclusion ? 0 :
2516 *zop->u.prox->exclusion),
2517 *zop->u.prox->relationType,
2518 *zop->u.prox->distance);
2519 rset_delete (rsets[0]);
2520 rset_delete (rsets[1]);
2528 else if (zs->which == Z_RPNStructure_simple)
2530 if (zs->u.simple->which == Z_Operand_APT)
2532 logf (LOG_DEBUG, "rpn_search_APT");
2533 r = rpn_search_APT (zh, zs->u.simple->u.attributesPlusTerm,
2534 attributeSet, stream, sort_sequence,
2535 num_bases, basenames);
2537 else if (zs->u.simple->which == Z_Operand_resultSetId)
2539 logf (LOG_DEBUG, "rpn_search_ref");
2540 r = resultSetRef (zh, zs->u.simple->u.resultSetId);
2543 r = rset_create (rset_kind_null, NULL);
2546 nmem_strdup (stream, zs->u.simple->u.resultSetId);
2567 RSET rpn_search (ZebraHandle zh, NMEM nmem,
2568 Z_RPNQuery *rpn, int num_bases, char **basenames,
2569 const char *setname,
2574 oid_value attributeSet;
2575 Z_SortKeySpecList *sort_sequence;
2579 zh->errString = NULL;
2582 sort_sequence = (Z_SortKeySpecList *)
2583 nmem_malloc (nmem, sizeof(*sort_sequence));
2584 sort_sequence->num_specs = 10;
2585 sort_sequence->specs = (Z_SortKeySpec **)
2586 nmem_malloc (nmem, sort_sequence->num_specs *
2587 sizeof(*sort_sequence->specs));
2588 for (i = 0; i<sort_sequence->num_specs; i++)
2589 sort_sequence->specs[i] = 0;
2591 attrset = oid_getentbyoid (rpn->attributeSetId);
2592 attributeSet = attrset->value;
2593 rset = rpn_search_structure (zh, rpn->RPNStructure, attributeSet,
2594 nmem, sort_sequence, num_bases, basenames);
2599 logf (LOG_DEBUG, "search error: %d", zh->errCode);
2601 for (i = 0; sort_sequence->specs[i]; i++)
2603 sort_sequence->num_specs = i;
2605 resultSetRank (zh, sset, rset);
2608 logf (LOG_DEBUG, "resultSetSortSingle in rpn_search");
2609 resultSetSortSingle (zh, nmem, sset, rset,
2610 sort_sequence, &sort_status);
2613 logf (LOG_DEBUG, "resultSetSortSingle status = %d", zh->errCode);
2619 struct scan_info_entry {
2625 struct scan_info_entry *list;
2631 static int scan_handle (char *name, const char *info, int pos, void *client)
2633 int len_prefix, idx;
2634 struct scan_info *scan_info = (struct scan_info *) client;
2636 len_prefix = strlen(scan_info->prefix);
2637 if (memcmp (name, scan_info->prefix, len_prefix))
2639 if (pos > 0) idx = scan_info->after - pos + scan_info->before;
2642 scan_info->list[idx].term = (char *)
2643 odr_malloc (scan_info->odr, strlen(name + len_prefix)+1);
2644 strcpy (scan_info->list[idx].term, name + len_prefix);
2645 assert (*info == sizeof(ISAMS_P));
2646 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAMS_P));
2650 static void scan_term_untrans (ZebraHandle zh, NMEM stream, int reg_type,
2651 char **dst, const char *src)
2653 char term_src[IT_MAX_WORD];
2654 char term_dst[IT_MAX_WORD];
2656 term_untrans (zh, reg_type, term_src, src);
2658 if (zh->iconv_from_utf8 != 0)
2661 char *inbuf = term_src;
2662 size_t inleft = strlen(term_src);
2663 char *outbuf = term_dst;
2664 size_t outleft = sizeof(term_dst)-1;
2667 ret = yaz_iconv (zh->iconv_from_utf8, &inbuf, &inleft,
2669 if (ret == (size_t)(-1))
2672 len = outbuf - term_dst;
2673 *dst = nmem_malloc (stream, len + 1);
2675 memcpy (*dst, term_dst, len);
2679 *dst = nmem_strdup (stream, term_src);
2682 static void count_set (RSET r, int *count)
2690 logf (LOG_DEBUG, "count_set");
2693 rfd = rset_open (r, RSETF_READ);
2694 while (rset_read (r, rfd, &key, &term_index))
2696 if (key.sysno != psysno)
2703 rset_close (r, rfd);
2704 logf (LOG_DEBUG, "%d keys, %d records", kno, *count);
2707 void rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
2708 oid_value attributeset,
2709 int num_bases, char **basenames,
2710 int *position, int *num_entries, ZebraScanEntry **list,
2711 int *is_partial, RSET limit_set, int return_zero)
2714 int pos = *position;
2715 int num = *num_entries;
2719 char termz[IT_MAX_WORD+20];
2722 struct scan_info *scan_info_array;
2723 ZebraScanEntry *glist;
2724 int ords[32], ord_no = 0;
2727 int bases_ok = 0; /* no of databases with OK attribute */
2728 int errCode = 0; /* err code (if any is not OK) */
2729 char *errString = 0; /* addinfo */
2732 char *search_type = NULL;
2733 char rank_type[128];
2739 if (attributeset == VAL_NONE)
2740 attributeset = VAL_BIB1;
2745 int termset_value_numeric;
2746 const char *termset_value_string;
2747 attr_init (&termset, zapt, 8);
2748 termset_value_numeric =
2749 attr_find_ex (&termset, NULL, &termset_value_string);
2750 if (termset_value_numeric != -1)
2753 const char *termset_name = 0;
2755 if (termset_value_numeric != -2)
2758 sprintf (resname, "%d", termset_value_numeric);
2759 termset_name = resname;
2762 termset_name = termset_value_string;
2764 limit_set = resultSetRef (zh, termset_name);
2768 yaz_log (LOG_DEBUG, "position = %d, num = %d set=%d",
2769 pos, num, attributeset);
2771 attr_init (&use, zapt, 1);
2772 use_value = attr_find (&use, &attributeset);
2774 if (zebra_maps_attr (zh->reg->zebra_maps, zapt, ®_id, &search_type,
2775 rank_type, &complete_flag, &sort_flag))
2781 yaz_log (LOG_DEBUG, "use_value = %d", use_value);
2783 if (use_value == -1)
2785 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
2789 data1_local_attribute *local_attr;
2791 if ((r=att_getentbyatt (zh, &attp, attributeset, use_value)))
2793 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
2794 attributeset, use_value);
2798 sprintf (val_str, "%d", use_value);
2800 errString = odr_strdup (stream, val_str);
2806 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
2808 zh->errString = basenames[base_no];
2809 zh->errCode = 109; /* Database unavailable */
2814 for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
2815 local_attr = local_attr->next)
2819 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
2822 ords[ord_no++] = ord;
2825 if (!bases_ok && errCode)
2827 zh->errCode = errCode;
2828 zh->errString = errString;
2834 sprintf (val_str, "%d", use_value);
2836 zh->errString = odr_strdup (stream, val_str);
2841 /* prepare dictionary scanning */
2844 scan_info_array = (struct scan_info *)
2845 odr_malloc (stream, ord_no * sizeof(*scan_info_array));
2846 for (i = 0; i < ord_no; i++)
2848 int j, prefix_len = 0;
2849 int before_tmp = before, after_tmp = after;
2850 struct scan_info *scan_info = scan_info_array + i;
2851 struct rpn_char_map_info rcmi;
2853 rpn_char_map_prepare (zh->reg, reg_id, &rcmi);
2855 scan_info->before = before;
2856 scan_info->after = after;
2857 scan_info->odr = stream;
2859 scan_info->list = (struct scan_info_entry *)
2860 odr_malloc (stream, (before+after) * sizeof(*scan_info->list));
2861 for (j = 0; j<before+after; j++)
2862 scan_info->list[j].term = NULL;
2864 prefix_len += key_SU_encode (ords[i], termz + prefix_len);
2865 termz[prefix_len++] = reg_id;
2866 termz[prefix_len] = 0;
2867 strcpy (scan_info->prefix, termz);
2869 if (trans_scan_term (zh, zapt, termz+prefix_len, reg_id))
2872 dict_scan (zh->reg->dict, termz, &before_tmp, &after_tmp,
2873 scan_info, scan_handle);
2875 glist = (ZebraScanEntry *)
2876 odr_malloc (stream, (before+after)*sizeof(*glist));
2878 /* consider terms after main term */
2879 for (i = 0; i < ord_no; i++)
2883 for (i = 0; i<after; i++)
2886 const char *mterm = NULL;
2890 for (j = 0; j < ord_no; j++)
2892 if (ptr[j] < before+after &&
2893 (tst=scan_info_array[j].list[ptr[j]].term) &&
2894 (!mterm || strcmp (tst, mterm) < 0))
2902 scan_term_untrans (zh, stream->mem, reg_id,
2903 &glist[i+before].term, mterm);
2904 rset = rset_trunc (zh, &scan_info_array[j0].list[ptr[j0]].isam_p, 1,
2905 glist[i+before].term, strlen(glist[i+before].term),
2906 NULL, 0, zapt->term->which);
2909 for (j = j0+1; j<ord_no; j++)
2911 if (ptr[j] < before+after &&
2912 (tst=scan_info_array[j].list[ptr[j]].term) &&
2913 !strcmp (tst, mterm))
2915 rset_bool_parms bool_parms;
2919 rset_trunc (zh, &scan_info_array[j].list[ptr[j]].isam_p, 1,
2920 glist[i+before].term,
2921 strlen(glist[i+before].term), NULL, 0,
2924 bool_parms.key_size = sizeof(struct it_key);
2925 bool_parms.cmp = key_compare_it;
2926 bool_parms.rset_l = rset;
2927 bool_parms.rset_r = rset2;
2929 rset = rset_create (rset_kind_or, &bool_parms);
2936 rset_bool_parms bool_parms;
2938 bool_parms.key_size = sizeof(struct it_key);
2939 bool_parms.cmp = key_compare_it;
2940 bool_parms.rset_l = rset;
2941 bool_parms.rset_r = rset_dup(limit_set);
2943 rset = rset_create (rset_kind_and, &bool_parms);
2945 count_set (rset, &glist[i+before].occurrences);
2950 *num_entries -= (after-i);
2954 /* consider terms before main term */
2955 for (i = 0; i<ord_no; i++)
2958 for (i = 0; i<before; i++)
2961 const char *mterm = NULL;
2965 for (j = 0; j <ord_no; j++)
2967 if (ptr[j] < before &&
2968 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2969 (!mterm || strcmp (tst, mterm) > 0))
2978 scan_term_untrans (zh, stream->mem, reg_id,
2979 &glist[before-1-i].term, mterm);
2982 (zh, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1,
2983 glist[before-1-i].term, strlen(glist[before-1-i].term),
2984 NULL, 0, zapt->term->which);
2988 for (j = j0+1; j<ord_no; j++)
2990 if (ptr[j] < before &&
2991 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2992 !strcmp (tst, mterm))
2994 rset_bool_parms bool_parms;
2997 rset2 = rset_trunc (zh,
2998 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1,
2999 glist[before-1-i].term,
3000 strlen(glist[before-1-i].term), NULL, 0,
3003 bool_parms.key_size = sizeof(struct it_key);
3004 bool_parms.cmp = key_compare_it;
3005 bool_parms.rset_l = rset;
3006 bool_parms.rset_r = rset2;
3008 rset = rset_create (rset_kind_or, &bool_parms);
3015 rset_bool_parms bool_parms;
3017 bool_parms.key_size = sizeof(struct it_key);
3018 bool_parms.cmp = key_compare_it;
3019 bool_parms.rset_l = rset;
3020 bool_parms.rset_r = rset_dup(limit_set);
3022 rset = rset_create (rset_kind_and, &bool_parms);
3024 count_set (rset, &glist[before-1-i].occurrences);
3034 *list = glist + i; /* list is set to first 'real' entry */
3036 logf (LOG_DEBUG, "position = %d, num_entries = %d",
3037 *position, *num_entries);
3039 logf (LOG_DEBUG, "scan error: %d", zh->errCode);