1 /* $Id: zrpn.c,v 1.120 2002-08-02 19:26:56 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
39 #include <rsbetween.h>
41 struct rpn_char_map_info {
46 static const char **rpn_char_map_handler (void *vp, const char **from, int len)
48 struct rpn_char_map_info *p = (struct rpn_char_map_info *) vp;
49 return zebra_maps_input (p->zm, p->reg_type, from, len);
52 static void rpn_char_map_prepare (struct zebra_register *reg, int reg_type,
53 struct rpn_char_map_info *map_info)
55 map_info->zm = reg->zebra_maps;
56 map_info->reg_type = reg_type;
57 dict_grep_cmap (reg->dict, map_info, rpn_char_map_handler);
64 Z_AttributesPlusTerm *zapt;
67 static int attr_find_ex (AttrType *src, oid_value *attributeSetP,
68 const char **string_value)
73 num_attributes = src->zapt->attributes->num_attributes;
75 num_attributes = src->zapt->num_attributes;
77 while (src->major < num_attributes)
79 Z_AttributeElement *element;
82 element = src->zapt->attributes->attributes[src->major];
84 element = src->zapt->attributeList[src->major];
86 if (src->type == *element->attributeType)
88 switch (element->which)
90 case Z_AttributeValue_numeric:
92 if (element->attributeSet && attributeSetP)
96 attrset = oid_getentbyoid (element->attributeSet);
97 *attributeSetP = attrset->value;
99 return *element->value.numeric;
101 case Z_AttributeValue_complex:
102 if (src->minor >= element->value.complex->num_list)
104 if (element->attributeSet && attributeSetP)
108 attrset = oid_getentbyoid (element->attributeSet);
109 *attributeSetP = attrset->value;
111 if (element->value.complex->list[src->minor]->which ==
112 Z_StringOrNumeric_numeric)
116 *element->value.complex->list[src->minor-1]->u.numeric;
118 else if (element->value.complex->list[src->minor]->which ==
119 Z_StringOrNumeric_string)
125 element->value.complex->list[src->minor-1]->u.string;
139 static int attr_find (AttrType *src, oid_value *attributeSetP)
141 return attr_find_ex (src, attributeSetP, 0);
144 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
167 static void term_untrans (ZebraHandle zh, int reg_type,
168 char *dst, const char *src)
172 const char *cp = zebra_maps_output (zh->reg->zebra_maps,
183 static void add_isam_p (const char *name, const char *info,
186 if (p->isam_p_indx == p->isam_p_size)
188 ISAMS_P *new_isam_p_buf;
192 p->isam_p_size = 2*p->isam_p_size + 100;
193 new_isam_p_buf = (ISAMS_P *) xmalloc (sizeof(*new_isam_p_buf) *
197 memcpy (new_isam_p_buf, p->isam_p_buf,
198 p->isam_p_indx * sizeof(*p->isam_p_buf));
199 xfree (p->isam_p_buf);
201 p->isam_p_buf = new_isam_p_buf;
204 new_term_no = (int *) xmalloc (sizeof(*new_term_no) *
208 memcpy (new_term_no, p->isam_p_buf,
209 p->isam_p_indx * sizeof(*p->term_no));
212 p->term_no = new_term_no;
215 assert (*info == sizeof(*p->isam_p_buf));
216 memcpy (p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
225 int len = key_SU_decode (&su_code, name);
227 term_untrans (p->zh, p->reg_type, term_tmp, name+len+1);
228 logf (LOG_LOG, "grep: %d %c %s", su_code, name[len], term_tmp);
229 zebraExplain_lookup_ord (p->zh->reg->zei,
230 su_code, &db, &set, &use);
231 logf (LOG_LOG, "grep: set=%d use=%d db=%s", set, use, db);
233 resultSetAddTerm (p->zh, p->termset, name[len], db,
240 static int grep_handle (char *name, const char *info, void *p)
242 add_isam_p (name, info, (struct grep_info *) p);
246 static int term_pre (ZebraMaps zebra_maps, int reg_type, const char **src,
247 const char *ct1, const char *ct2)
249 const char *s1, *s0 = *src;
252 /* skip white space */
255 if (ct1 && strchr (ct1, *s0))
257 if (ct2 && strchr (ct2, *s0))
260 map = zebra_maps_input (zebra_maps, reg_type, &s1, strlen(s1));
261 if (**map != *CHR_SPACE)
269 #define REGEX_CHARS "[]()|.*+!"
271 /* term_100: handle term, where trunc=none (no operators at all) */
272 static int term_100 (ZebraMaps zebra_maps, int reg_type,
273 const char **src, char *dst, int space_split,
281 const char *space_start = 0;
282 const char *space_end = 0;
284 if (!term_pre (zebra_maps, reg_type, src, NULL, NULL))
290 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
293 if (**map == *CHR_SPACE)
296 else /* complete subfield only. */
298 if (**map == *CHR_SPACE)
299 { /* save space mapping for later .. */
304 else if (space_start)
305 { /* reload last space */
306 while (space_start < space_end)
308 if (strchr (REGEX_CHARS, *space_start))
310 dst_term[j++] = *space_start;
311 dst[i++] = *space_start++;
314 space_start = space_end = 0;
317 /* add non-space char */
320 if (strchr(REGEX_CHARS, *s1))
332 /* term_101: handle term, where trunc=Process # */
333 static int term_101 (ZebraMaps zebra_maps, int reg_type,
334 const char **src, char *dst, int space_split,
342 if (!term_pre (zebra_maps, reg_type, src, "#", "#"))
351 dst_term[j++] = *s0++;
356 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
357 if (space_split && **map == *CHR_SPACE)
361 if (strchr(REGEX_CHARS, *s1))
369 dst_term[j++] = '\0';
374 /* term_103: handle term, where trunc=re-2 (regular expressions) */
375 static int term_103 (ZebraMaps zebra_maps, int reg_type, const char **src,
376 char *dst, int *errors, int space_split,
384 if (!term_pre (zebra_maps, reg_type, src, "^\\()[].*+?|", "("))
387 if (errors && *s0 == '+' && s0[1] && s0[2] == '+' && s0[3] &&
390 *errors = s0[1] - '0';
397 if (strchr ("^\\()[].*+?|-", *s0))
405 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
406 if (**map == *CHR_SPACE)
410 if (strchr(REGEX_CHARS, *s1))
423 /* term_103: handle term, where trunc=re-1 (regular expressions) */
424 static int term_102 (ZebraMaps zebra_maps, int reg_type, const char **src,
425 char *dst, int space_split, char *dst_term)
427 return term_103 (zebra_maps, reg_type, src, dst, NULL, space_split,
432 /* term_104: handle term, where trunc=Process # and ! */
433 static int term_104 (ZebraMaps zebra_maps, int reg_type,
434 const char **src, char *dst, int space_split,
442 if (!term_pre (zebra_maps, reg_type, src, "#!", "#!"))
451 dst_term[j++] = *s0++;
456 dst_term[j++] = *s0++;
460 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
461 if (space_split && **map == *CHR_SPACE)
465 if (strchr(REGEX_CHARS, *s1))
473 dst_term[j++] = '\0';
478 /* term_105/106: handle term, where trunc=Process * and ! and right trunc */
479 static int term_105 (ZebraMaps zebra_maps, int reg_type,
480 const char **src, char *dst, int space_split,
481 char *dst_term, int right_truncate)
488 if (!term_pre (zebra_maps, reg_type, src, "*!", "*!"))
497 dst_term[j++] = *s0++;
502 dst_term[j++] = *s0++;
506 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
507 if (space_split && **map == *CHR_SPACE)
511 if (strchr(REGEX_CHARS, *s1))
525 dst_term[j++] = '\0';
531 /* gen_regular_rel - generate regular expression from relation
532 * val: border value (inclusive)
533 * islt: 1 if <=; 0 if >=.
535 static void gen_regular_rel (char *dst, int val, int islt)
542 logf (LOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
546 strcpy (dst, "(-[0-9]+|(");
554 strcpy (dst, "([0-9]+|-(");
555 dst_p = strlen (dst);
565 dst_p = strlen (dst);
566 sprintf (numstr, "%d", val);
567 for (w = strlen(numstr); --w >= 0; pos++)
586 strcpy (dst + dst_p, numstr);
587 dst_p = strlen(dst) - pos - 1;
615 for (i = 0; i<pos; i++)
628 /* match everything less than 10^(pos-1) */
630 for (i=1; i<pos; i++)
631 strcat (dst, "[0-9]?");
635 /* match everything greater than 10^pos */
636 for (i = 0; i <= pos; i++)
637 strcat (dst, "[0-9]");
638 strcat (dst, "[0-9]*");
643 void string_rel_add_char (char **term_p, const char *src, int *indx)
645 if (src[*indx] == '\\')
646 *(*term_p)++ = src[(*indx)++];
647 *(*term_p)++ = src[(*indx)++];
651 * > abc ([b-].*|a[c-].*|ab[d-].*|abc.+)
652 * ([^-a].*|a[^-b].*ab[^-c].*|abc.+)
653 * >= abc ([b-].*|a[c-].*|ab[c-].*)
654 * ([^-a].*|a[^-b].*|ab[c-].*)
655 * < abc ([-0].*|a[-a].*|ab[-b].*)
656 * ([^a-].*|a[^b-].*|ab[^c-].*)
657 * <= abc ([-0].*|a[-a].*|ab[-b].*|abc)
658 * ([^a-].*|a[^b-].*|ab[^c-].*|abc)
660 static int string_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
661 const char **term_sub, char *term_dict,
662 oid_value attributeSet,
663 int reg_type, int space_split, char *term_dst)
668 char *term_tmp = term_dict + strlen(term_dict);
669 char term_component[256];
671 attr_init (&relation, zapt, 2);
672 relation_value = attr_find (&relation, NULL);
674 logf (LOG_DEBUG, "string relation value=%d", relation_value);
675 switch (relation_value)
678 if (!term_100 (zh->reg->zebra_maps, reg_type,
679 term_sub, term_component,
680 space_split, term_dst))
682 logf (LOG_DEBUG, "Relation <");
685 for (i = 0; term_component[i]; )
692 string_rel_add_char (&term_tmp, term_component, &j);
697 string_rel_add_char (&term_tmp, term_component, &i);
708 if (!term_100 (zh->reg->zebra_maps, reg_type,
709 term_sub, term_component,
710 space_split, term_dst))
712 logf (LOG_DEBUG, "Relation <=");
715 for (i = 0; term_component[i]; )
720 string_rel_add_char (&term_tmp, term_component, &j);
724 string_rel_add_char (&term_tmp, term_component, &i);
733 for (i = 0; term_component[i]; )
734 string_rel_add_char (&term_tmp, term_component, &i);
739 if (!term_100 (zh->reg->zebra_maps, reg_type,
740 term_sub, term_component, space_split, term_dst))
742 logf (LOG_DEBUG, "Relation >");
745 for (i = 0; term_component[i];)
750 string_rel_add_char (&term_tmp, term_component, &j);
755 string_rel_add_char (&term_tmp, term_component, &i);
763 for (i = 0; term_component[i];)
764 string_rel_add_char (&term_tmp, term_component, &i);
771 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub,
772 term_component, space_split, term_dst))
774 logf (LOG_DEBUG, "Relation >=");
777 for (i = 0; term_component[i];)
784 string_rel_add_char (&term_tmp, term_component, &j);
787 if (term_component[i+1])
791 string_rel_add_char (&term_tmp, term_component, &i);
795 string_rel_add_char (&term_tmp, term_component, &i);
807 logf (LOG_DEBUG, "Relation =");
808 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub,
809 term_component, space_split, term_dst))
811 strcat (term_tmp, "(");
812 strcat (term_tmp, term_component);
813 strcat (term_tmp, ")");
818 static int string_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
819 const char **term_sub,
820 oid_value attributeSet, NMEM stream,
821 struct grep_info *grep_info,
822 int reg_type, int complete_flag,
823 int num_bases, char **basenames,
824 char *term_dst, int xpath_use);
826 static RSET term_trunc (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
827 const char **term_sub,
828 oid_value attributeSet, NMEM stream,
829 struct grep_info *grep_info,
830 int reg_type, int complete_flag,
831 int num_bases, char **basenames,
833 const char *rank_type, int xpath_use)
836 grep_info->isam_p_indx = 0;
837 r = string_term (zh, zapt, term_sub, attributeSet, stream, grep_info,
838 reg_type, complete_flag, num_bases, basenames,
839 term_dst, xpath_use);
842 logf (LOG_DEBUG, "term: %s", term_dst);
843 return rset_trunc (zh, grep_info->isam_p_buf,
844 grep_info->isam_p_indx, term_dst,
845 strlen(term_dst), rank_type, 1 /* preserve pos */,
850 static int string_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
851 const char **term_sub,
852 oid_value attributeSet, NMEM stream,
853 struct grep_info *grep_info,
854 int reg_type, int complete_flag,
855 int num_bases, char **basenames,
856 char *term_dst, int xpath_use)
858 char term_dict[2*IT_MAX_WORD+4000];
861 int truncation_value;
864 const char *use_string = 0;
865 oid_value curAttributeSet = attributeSet;
867 struct rpn_char_map_info rcmi;
868 int space_split = complete_flag ? 0 : 1;
870 rpn_char_map_prepare (zh->reg, reg_type, &rcmi);
871 attr_init (&use, zapt, 1);
872 use_value = attr_find_ex (&use, &curAttributeSet, &use_string);
873 logf (LOG_DEBUG, "string_term, use value %d", use_value);
874 attr_init (&truncation, zapt, 5);
875 truncation_value = attr_find (&truncation, NULL);
876 logf (LOG_DEBUG, "truncation value %d", truncation_value);
878 if (use_value == -1) /* no attribute - assumy "any" */
880 for (base_no = 0; base_no < num_bases; base_no++)
883 data1_local_attribute id_xpath_attr;
884 data1_local_attribute *local_attr;
885 int max_pos, prefix_len = 0;
889 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
891 zh->errCode = 109; /* Database unavailable */
892 zh->errString = basenames[base_no];
895 if (use_value == -2) /* string attribute (assume IDXPATH/any) */
897 use_value = xpath_use;
898 attp.local_attributes = &id_xpath_attr;
899 attp.attset_ordinal = VAL_IDXPATH;
900 id_xpath_attr.next = 0;
901 id_xpath_attr.local = use_value;
903 else if (curAttributeSet == VAL_IDXPATH)
905 attp.local_attributes = &id_xpath_attr;
906 attp.attset_ordinal = VAL_IDXPATH;
907 id_xpath_attr.next = 0;
908 id_xpath_attr.local = use_value;
912 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
914 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
915 curAttributeSet, use_value, r);
918 /* set was found, but value wasn't defined */
920 sprintf (val_str, "%d (1)", use_value);
922 zh->errString = nmem_strdup (stream, val_str);
927 struct oident oident;
929 oident.proto = PROTO_Z3950;
930 oident.oclass = CLASS_ATTSET;
931 oident.value = curAttributeSet;
932 oid_ent_to_oid (&oident, oid);
935 zh->errString = nmem_strdup (stream, oident.desc);
940 for (local_attr = attp.local_attributes; local_attr;
941 local_attr = local_attr->next)
947 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
952 term_dict[prefix_len++] = '|';
954 term_dict[prefix_len++] = '(';
956 ord_len = key_SU_encode (ord, ord_buf);
957 for (i = 0; i<ord_len; i++)
959 term_dict[prefix_len++] = 1;
960 term_dict[prefix_len++] = ord_buf[i];
966 sprintf (val_str, "%d (2)", use_value);
968 zh->errString = nmem_strdup (stream, val_str);
971 term_dict[prefix_len++] = ')';
972 term_dict[prefix_len++] = 1;
973 term_dict[prefix_len++] = reg_type;
974 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
975 term_dict[prefix_len] = '\0';
977 switch (truncation_value)
979 case -1: /* not specified */
980 case 100: /* do not truncate */
981 if (!string_relation (zh, zapt, &termp, term_dict,
983 reg_type, space_split, term_dst))
985 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_dict+prefix_len);
986 r = dict_lookup_grep (zh->reg->dict, term_dict, 0,
987 grep_info, &max_pos, 0, grep_handle);
989 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
991 case 1: /* right truncation */
992 term_dict[j++] = '(';
993 if (!term_100 (zh->reg->zebra_maps, reg_type,
994 &termp, term_dict + j, space_split, term_dst))
996 strcat (term_dict, ".*)");
997 dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
998 &max_pos, 0, grep_handle);
1000 case 2: /* keft truncation */
1001 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1002 if (!term_100 (zh->reg->zebra_maps, reg_type,
1003 &termp, term_dict + j, space_split, term_dst))
1005 strcat (term_dict, ")");
1006 dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1007 &max_pos, 0, grep_handle);
1009 case 3: /* left&right truncation */
1010 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1011 if (!term_100 (zh->reg->zebra_maps, reg_type,
1012 &termp, term_dict + j, space_split, term_dst))
1014 strcat (term_dict, ".*)");
1015 dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1016 &max_pos, 0, grep_handle);
1020 case 101: /* process # in term */
1021 term_dict[j++] = '(';
1022 if (!term_101 (zh->reg->zebra_maps, reg_type,
1023 &termp, term_dict + j, space_split, term_dst))
1025 strcat (term_dict, ")");
1026 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1027 &max_pos, 0, grep_handle);
1029 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d", r);
1031 case 102: /* Regexp-1 */
1032 term_dict[j++] = '(';
1033 if (!term_102 (zh->reg->zebra_maps, reg_type,
1034 &termp, term_dict + j, space_split, term_dst))
1036 strcat (term_dict, ")");
1037 logf (LOG_DEBUG, "Regexp-1 tolerance=%d", r);
1038 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1039 &max_pos, 0, grep_handle);
1041 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
1044 case 103: /* Regexp-2 */
1046 term_dict[j++] = '(';
1047 if (!term_103 (zh->reg->zebra_maps, reg_type,
1048 &termp, term_dict + j, &r, space_split, term_dst))
1050 strcat (term_dict, ")");
1051 logf (LOG_DEBUG, "Regexp-2 tolerance=%d", r);
1052 r = dict_lookup_grep (zh->reg->dict, term_dict, r, grep_info,
1053 &max_pos, 2, grep_handle);
1055 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
1058 case 104: /* process # and ! in term */
1059 term_dict[j++] = '(';
1060 if (!term_104 (zh->reg->zebra_maps, reg_type,
1061 &termp, term_dict + j, space_split, term_dst))
1063 strcat (term_dict, ")");
1064 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1065 &max_pos, 0, grep_handle);
1067 logf (LOG_WARN, "dict_lookup_grep err, trunc=#/!: %d", r);
1069 case 105: /* process * and ! in term */
1070 term_dict[j++] = '(';
1071 if (!term_105 (zh->reg->zebra_maps, reg_type,
1072 &termp, term_dict + j, space_split, term_dst, 1))
1074 strcat (term_dict, ")");
1075 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1076 &max_pos, 0, grep_handle);
1078 logf (LOG_WARN, "dict_lookup_grep err, trunc=*/!: %d", r);
1080 case 106: /* process * and ! in term */
1081 term_dict[j++] = '(';
1082 if (!term_105 (zh->reg->zebra_maps, reg_type,
1083 &termp, term_dict + j, space_split, term_dst, 0))
1085 strcat (term_dict, ")");
1086 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1087 &max_pos, 0, grep_handle);
1089 logf (LOG_WARN, "dict_lookup_grep err, trunc=*/!: %d", r);
1094 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1099 static int trans_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1103 Z_Term *term = zapt->term;
1105 switch (term->which)
1107 case Z_Term_general:
1109 if (zh->iconv_to_utf8 != (iconv_t)(-1))
1111 char *inbuf = term->u.general->buf;
1112 size_t inleft = term->u.general->len;
1113 char *outbuf = termz;
1114 size_t outleft = IT_MAX_WORD-1;
1117 yaz_log (LOG_DEBUG, "converting general from ISO-8859-1");
1118 ret = iconv(zh->iconv_to_utf8, &inbuf, &inleft,
1120 if (ret == (size_t)(-1))
1122 ret = iconv(zh->iconv_to_utf8, 0, 0, 0, 0);
1130 sizez = term->u.general->len;
1131 if (sizez > IT_MAX_WORD-1)
1132 sizez = IT_MAX_WORD-1;
1133 memcpy (termz, term->u.general->buf, sizez);
1134 termz[sizez] = '\0';
1136 case Z_Term_characterString:
1137 sizez = strlen(term->u.characterString);
1138 if (sizez > IT_MAX_WORD-1)
1139 sizez = IT_MAX_WORD-1;
1140 memcpy (termz, term->u.characterString, sizez);
1141 termz[sizez] = '\0';
1149 static void trans_scan_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1150 char *termz, int reg_type)
1152 Z_Term *term = zapt->term;
1154 const char *cp = (const char *) term->u.general->buf;
1155 const char *cp_end = cp + term->u.general->len;
1158 const char *space_map = NULL;
1161 while ((len = (cp_end - cp)) > 0)
1163 map = zebra_maps_input (zh->reg->zebra_maps, reg_type, &cp, len);
1164 if (**map == *CHR_SPACE)
1169 for (src = space_map; *src; src++)
1172 for (src = *map; *src; src++)
1179 static RSET rpn_prox (ZebraHandle zh, RSET *rset, int rset_no,
1180 int ordered, int exclusion, int relation, int distance)
1185 struct it_key **buf;
1187 char prox_term[1024];
1188 int length_prox_term = 0;
1189 int min_nn = 10000000;
1191 int term_type = Z_Term_characterString;
1192 const char *flags = NULL;
1194 rsfd = (RSFD *) xmalloc (sizeof(*rsfd)*rset_no);
1195 more = (int *) xmalloc (sizeof(*more)*rset_no);
1196 buf = (struct it_key **) xmalloc (sizeof(*buf)*rset_no);
1199 for (i = 0; i<rset_no; i++)
1202 for (j = 0; j<rset[i]->no_rset_terms; j++)
1204 const char *nflags = rset[i]->rset_terms[j]->flags;
1205 char *term = rset[i]->rset_terms[j]->name;
1206 int lterm = strlen(term);
1207 if (lterm + length_prox_term < sizeof(prox_term)-1)
1209 if (length_prox_term)
1210 prox_term[length_prox_term++] = ' ';
1211 strcpy (prox_term + length_prox_term, term);
1212 length_prox_term += lterm;
1214 if (min_nn > rset[i]->rset_terms[j]->nn)
1215 min_nn = rset[i]->rset_terms[j]->nn;
1217 term_type = rset[i]->rset_terms[j]->type;
1219 /* only if all term types are of type characterString .. */
1220 /* the resulting term is of that type */
1221 if (term_type != Z_Term_characterString)
1222 term_type = Z_Term_general;
1225 for (i = 0; i<rset_no; i++)
1230 for (i = 0; i<rset_no; i++)
1232 buf[i] = (struct it_key *) xmalloc (sizeof(**buf));
1233 rsfd[i] = rset_open (rset[i], RSETF_READ);
1234 if (!(more[i] = rset_read (rset[i], rsfd[i], buf[i], &term_index)))
1239 /* at least one is empty ... return null set */
1240 rset_null_parms parms;
1242 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1244 parms.rset_term->nn = 0;
1245 result = rset_create (rset_kind_null, &parms);
1247 else if (ordered && relation == 3 && exclusion == 0 && distance == 1)
1249 /* special proximity case = phrase search ... */
1250 rset_temp_parms parms;
1253 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1255 parms.rset_term->nn = min_nn;
1256 parms.cmp = key_compare_it;
1257 parms.key_size = sizeof (struct it_key);
1258 parms.temp_path = res_get (zh->res, "setTmpDir");
1259 result = rset_create (rset_kind_temp, &parms);
1260 rsfd_result = rset_open (result, RSETF_WRITE);
1264 for (i = 1; i<rset_no; i++)
1273 cmp = key_compare_it (buf[i], buf[i-1]);
1276 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1277 buf[i-1], &term_index);
1282 if (buf[i-1]->seqno+1 != buf[i]->seqno)
1284 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1285 buf[i-1], &term_index);
1291 more[i] = rset_read (rset[i], rsfd[i], buf[i],
1298 rset_write (result, rsfd_result, buf[0]);
1299 more[0] = rset_read (*rset, *rsfd, *buf, &term_index);
1302 rset_close (result, rsfd_result);
1304 else if (rset_no == 2)
1306 /* generic proximity case (two input sets only) ... */
1307 rset_temp_parms parms;
1310 logf (LOG_LOG, "generic prox, dist = %d, relation = %d, ordered =%d, exclusion=%d",
1311 distance, relation, ordered, exclusion);
1312 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1314 parms.rset_term->nn = min_nn;
1315 parms.cmp = key_compare_it;
1316 parms.key_size = sizeof (struct it_key);
1317 parms.temp_path = res_get (zh->res, "setTmpDir");
1318 result = rset_create (rset_kind_temp, &parms);
1319 rsfd_result = rset_open (result, RSETF_WRITE);
1321 while (more[0] && more[1])
1323 int cmp = key_compare_it (buf[0], buf[1]);
1325 more[0] = rset_read (rset[0], rsfd[0], buf[0], &term_index);
1327 more[1] = rset_read (rset[1], rsfd[1], buf[1], &term_index);
1330 int sysno = buf[0]->sysno;
1334 seqno[n++] = buf[0]->seqno;
1335 while ((more[0] = rset_read (rset[0], rsfd[0], buf[0],
1337 sysno == buf[0]->sysno)
1339 seqno[n++] = buf[0]->seqno;
1342 for (i = 0; i<n; i++)
1344 int diff = buf[1]->seqno - seqno[i];
1345 int excl = exclusion;
1346 if (!ordered && diff < 0)
1351 if (diff < distance && diff >= 0)
1355 if (diff <= distance && diff >= 0)
1359 if (diff == distance && diff >= 0)
1363 if (diff >= distance && diff >= 0)
1367 if (diff > distance && diff >= 0)
1371 if (diff != distance && diff >= 0)
1377 rset_write (result, rsfd_result, buf[1]);
1381 } while ((more[1] = rset_read (rset[1], rsfd[1], buf[1],
1383 sysno == buf[1]->sysno);
1386 rset_close (result, rsfd_result);
1390 rset_null_parms parms;
1392 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1394 parms.rset_term->nn = 0;
1395 result = rset_create (rset_kind_null, &parms);
1397 for (i = 0; i<rset_no; i++)
1400 rset_close (rset[i], rsfd[i]);
1410 char *normalize_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1411 const char *termz, NMEM stream, unsigned reg_id)
1414 AttrType truncation;
1415 int truncation_value;
1418 attr_init (&truncation, zapt, 5);
1419 truncation_value = attr_find (&truncation, NULL);
1421 switch (truncation_value)
1441 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, ex_list,
1442 termz, strlen(termz));
1444 return nmem_strdup(stream, termz);
1447 char *buf = (char*) nmem_malloc (stream, wrbuf_len(wrbuf)+1);
1448 memcpy (buf, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1449 buf[wrbuf_len(wrbuf)] = '\0';
1454 static void grep_info_delete (struct grep_info *grep_info)
1457 xfree(grep_info->term_no);
1459 xfree (grep_info->isam_p_buf);
1462 static int grep_info_prepare (ZebraHandle zh,
1463 Z_AttributesPlusTerm *zapt,
1464 struct grep_info *grep_info,
1469 int termset_value_numeric;
1470 const char *termset_value_string;
1473 grep_info->term_no = 0;
1475 grep_info->isam_p_size = 0;
1476 grep_info->isam_p_buf = NULL;
1478 grep_info->reg_type = reg_type;
1479 grep_info->termset = 0;
1483 attr_init (&termset, zapt, 8);
1484 termset_value_numeric =
1485 attr_find_ex (&termset, NULL, &termset_value_string);
1486 if (termset_value_numeric != -1)
1489 const char *termset_name = 0;
1490 if (termset_value_numeric != -2)
1493 sprintf (resname, "%d", termset_value_numeric);
1494 termset_name = resname;
1497 termset_name = termset_value_string;
1498 logf (LOG_LOG, "creating termset set %s", termset_name);
1499 grep_info->termset = resultSetAdd (zh, termset_name, 1);
1500 if (!grep_info->termset)
1503 zh->errString = nmem_strdup (stream, termset_name);
1511 static RSET rpn_search_APT_phrase (ZebraHandle zh,
1512 Z_AttributesPlusTerm *zapt,
1513 const char *termz_org,
1514 oid_value attributeSet,
1516 int reg_type, int complete_flag,
1517 const char *rank_type, int xpath_use,
1518 int num_bases, char **basenames)
1520 char term_dst[IT_MAX_WORD+1];
1521 RSET rset[60], result;
1523 struct grep_info grep_info;
1524 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1525 const char *termp = termz;
1528 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1532 logf (LOG_DEBUG, "APT_phrase termp=%s", termp);
1533 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1535 reg_type, complete_flag,
1536 num_bases, basenames,
1537 term_dst, rank_type,
1541 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1544 grep_info_delete (&grep_info);
1547 rset_null_parms parms;
1549 parms.rset_term = rset_term_create (termz, -1, rank_type,
1551 return rset_create (rset_kind_null, &parms);
1553 else if (rset_no == 1)
1555 result = rpn_prox (zh, rset, rset_no, 1, 0, 3, 1);
1556 for (i = 0; i<rset_no; i++)
1557 rset_delete (rset[i]);
1561 static RSET rpn_search_APT_or_list (ZebraHandle zh,
1562 Z_AttributesPlusTerm *zapt,
1563 const char *termz_org,
1564 oid_value attributeSet,
1566 int reg_type, int complete_flag,
1567 const char *rank_type,
1569 int num_bases, char **basenames)
1571 char term_dst[IT_MAX_WORD+1];
1572 RSET rset[60], result;
1574 struct grep_info grep_info;
1575 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1576 const char *termp = termz;
1578 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1582 logf (LOG_DEBUG, "APT_or_list termp=%s", termp);
1583 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1585 reg_type, complete_flag,
1586 num_bases, basenames,
1587 term_dst, rank_type,
1591 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1594 grep_info_delete (&grep_info);
1597 rset_null_parms parms;
1599 parms.rset_term = rset_term_create (termz, -1, rank_type,
1601 return rset_create (rset_kind_null, &parms);
1604 for (i = 1; i<rset_no; i++)
1606 rset_bool_parms bool_parms;
1608 bool_parms.rset_l = result;
1609 bool_parms.rset_r = rset[i];
1610 bool_parms.key_size = sizeof(struct it_key);
1611 bool_parms.cmp = key_compare_it;
1612 result = rset_create (rset_kind_or, &bool_parms);
1617 static RSET rpn_search_APT_and_list (ZebraHandle zh,
1618 Z_AttributesPlusTerm *zapt,
1619 const char *termz_org,
1620 oid_value attributeSet,
1622 int reg_type, int complete_flag,
1623 const char *rank_type,
1625 int num_bases, char **basenames)
1627 char term_dst[IT_MAX_WORD+1];
1628 RSET rset[60], result;
1630 struct grep_info grep_info;
1631 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1632 const char *termp = termz;
1634 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1638 logf (LOG_DEBUG, "APT_and_list termp=%s", termp);
1639 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1641 reg_type, complete_flag,
1642 num_bases, basenames,
1643 term_dst, rank_type,
1647 assert (rset[rset_no]);
1648 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1651 grep_info_delete (&grep_info);
1654 rset_null_parms parms;
1656 parms.rset_term = rset_term_create (termz, -1, rank_type,
1658 return rset_create (rset_kind_null, &parms);
1661 for (i = 1; i<rset_no; i++)
1663 rset_bool_parms bool_parms;
1665 bool_parms.rset_l = result;
1666 bool_parms.rset_r = rset[i];
1667 bool_parms.key_size = sizeof(struct it_key);
1668 bool_parms.cmp = key_compare_it;
1669 result = rset_create (rset_kind_and, &bool_parms);
1674 static int numeric_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1675 const char **term_sub,
1677 oid_value attributeSet,
1678 struct grep_info *grep_info,
1687 char *term_tmp = term_dict + strlen(term_dict);
1689 attr_init (&relation, zapt, 2);
1690 relation_value = attr_find (&relation, NULL);
1692 logf (LOG_DEBUG, "numeric relation value=%d", relation_value);
1694 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub, term_tmp, 1,
1697 term_value = atoi (term_tmp);
1698 switch (relation_value)
1701 logf (LOG_DEBUG, "Relation <");
1702 gen_regular_rel (term_tmp, term_value-1, 1);
1705 logf (LOG_DEBUG, "Relation <=");
1706 gen_regular_rel (term_tmp, term_value, 1);
1709 logf (LOG_DEBUG, "Relation >=");
1710 gen_regular_rel (term_tmp, term_value, 0);
1713 logf (LOG_DEBUG, "Relation >");
1714 gen_regular_rel (term_tmp, term_value+1, 0);
1718 logf (LOG_DEBUG, "Relation =");
1719 sprintf (term_tmp, "(0*%d)", term_value);
1721 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_tmp);
1722 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info, max_pos,
1725 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1726 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1730 static int numeric_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1731 const char **term_sub,
1732 oid_value attributeSet, struct grep_info *grep_info,
1733 int reg_type, int complete_flag,
1734 int num_bases, char **basenames,
1737 char term_dict[2*IT_MAX_WORD+2];
1741 oid_value curAttributeSet = attributeSet;
1743 struct rpn_char_map_info rcmi;
1745 rpn_char_map_prepare (zh->reg, reg_type, &rcmi);
1746 attr_init (&use, zapt, 1);
1747 use_value = attr_find (&use, &curAttributeSet);
1748 logf (LOG_DEBUG, "numeric_term, use value %d", use_value);
1750 if (use_value == -1)
1753 for (base_no = 0; base_no < num_bases; base_no++)
1756 data1_local_attribute *local_attr;
1757 int max_pos, prefix_len = 0;
1760 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
1762 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1763 curAttributeSet, use_value, r);
1770 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
1772 zh->errCode = 109; /* Database unavailable */
1773 zh->errString = basenames[base_no];
1776 for (local_attr = attp.local_attributes; local_attr;
1777 local_attr = local_attr->next)
1783 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
1788 term_dict[prefix_len++] = '|';
1790 term_dict[prefix_len++] = '(';
1792 ord_len = key_SU_encode (ord, ord_buf);
1793 for (i = 0; i<ord_len; i++)
1795 term_dict[prefix_len++] = 1;
1796 term_dict[prefix_len++] = ord_buf[i];
1804 term_dict[prefix_len++] = ')';
1805 term_dict[prefix_len++] = 1;
1806 term_dict[prefix_len++] = reg_type;
1807 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1808 term_dict[prefix_len] = '\0';
1809 if (!numeric_relation (zh, zapt, &termp, term_dict,
1810 attributeSet, grep_info, &max_pos, reg_type,
1815 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1819 static RSET rpn_search_APT_numeric (ZebraHandle zh,
1820 Z_AttributesPlusTerm *zapt,
1822 oid_value attributeSet,
1824 int reg_type, int complete_flag,
1825 const char *rank_type,
1826 int num_bases, char **basenames)
1828 char term_dst[IT_MAX_WORD+1];
1829 const char *termp = termz;
1830 RSET rset[60], result;
1831 int i, r, rset_no = 0;
1832 struct grep_info grep_info;
1834 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1838 logf (LOG_DEBUG, "APT_numeric termp=%s", termp);
1839 grep_info.isam_p_indx = 0;
1840 r = numeric_term (zh, zapt, &termp, attributeSet, &grep_info,
1841 reg_type, complete_flag, num_bases, basenames,
1845 logf (LOG_DEBUG, "term: %s", term_dst);
1846 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1847 grep_info.isam_p_indx, term_dst,
1848 strlen(term_dst), rank_type,
1849 0 /* preserve position */,
1851 assert (rset[rset_no]);
1852 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1855 grep_info_delete (&grep_info);
1858 rset_null_parms parms;
1860 parms.rset_term = rset_term_create (term_dst, -1, rank_type,
1862 return rset_create (rset_kind_null, &parms);
1865 for (i = 1; i<rset_no; i++)
1867 rset_bool_parms bool_parms;
1869 bool_parms.rset_l = result;
1870 bool_parms.rset_r = rset[i];
1871 bool_parms.key_size = sizeof(struct it_key);
1872 bool_parms.cmp = key_compare_it;
1873 result = rset_create (rset_kind_and, &bool_parms);
1878 static RSET rpn_search_APT_local (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1880 oid_value attributeSet,
1882 const char *rank_type)
1887 rset_temp_parms parms;
1889 parms.rset_term = rset_term_create (termz, -1, rank_type,
1891 parms.cmp = key_compare_it;
1892 parms.key_size = sizeof (struct it_key);
1893 parms.temp_path = res_get (zh->res, "setTmpDir");
1894 result = rset_create (rset_kind_temp, &parms);
1895 rsfd = rset_open (result, RSETF_WRITE);
1897 key.sysno = atoi (termz);
1901 rset_write (result, rsfd, &key);
1902 rset_close (result, rsfd);
1906 static RSET rpn_sort_spec (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1907 oid_value attributeSet, NMEM stream,
1908 Z_SortKeySpecList *sort_sequence,
1909 const char *rank_type)
1911 rset_null_parms parms;
1913 int sort_relation_value;
1914 AttrType sort_relation_type;
1919 Z_AttributeElement *ae;
1924 attr_init (&sort_relation_type, zapt, 7);
1925 sort_relation_value = attr_find (&sort_relation_type, &attributeSet);
1927 attr_init (&use_type, zapt, 1);
1928 use_value = attr_find (&use_type, &attributeSet);
1930 if (!sort_sequence->specs)
1932 sort_sequence->num_specs = 10;
1933 sort_sequence->specs = (Z_SortKeySpec **)
1934 nmem_malloc (stream, sort_sequence->num_specs *
1935 sizeof(*sort_sequence->specs));
1936 for (i = 0; i<sort_sequence->num_specs; i++)
1937 sort_sequence->specs[i] = 0;
1939 if (zapt->term->which != Z_Term_general)
1942 i = atoi_n ((char *) zapt->term->u.general->buf,
1943 zapt->term->u.general->len);
1944 if (i >= sort_sequence->num_specs)
1946 sprintf (termz, "%d", i);
1948 oe.proto = PROTO_Z3950;
1949 oe.oclass = CLASS_ATTSET;
1950 oe.value = attributeSet;
1951 if (!oid_ent_to_oid (&oe, oid))
1954 sks = (Z_SortKeySpec *) nmem_malloc (stream, sizeof(*sks));
1955 sks->sortElement = (Z_SortElement *)
1956 nmem_malloc (stream, sizeof(*sks->sortElement));
1957 sks->sortElement->which = Z_SortElement_generic;
1958 sk = sks->sortElement->u.generic = (Z_SortKey *)
1959 nmem_malloc (stream, sizeof(*sk));
1960 sk->which = Z_SortKey_sortAttributes;
1961 sk->u.sortAttributes = (Z_SortAttributes *)
1962 nmem_malloc (stream, sizeof(*sk->u.sortAttributes));
1964 sk->u.sortAttributes->id = oid;
1965 sk->u.sortAttributes->list = (Z_AttributeList *)
1966 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list));
1967 sk->u.sortAttributes->list->num_attributes = 1;
1968 sk->u.sortAttributes->list->attributes = (Z_AttributeElement **)
1969 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list->attributes));
1970 ae = *sk->u.sortAttributes->list->attributes = (Z_AttributeElement *)
1971 nmem_malloc (stream, sizeof(**sk->u.sortAttributes->list->attributes));
1972 ae->attributeSet = 0;
1973 ae->attributeType = (int *)
1974 nmem_malloc (stream, sizeof(*ae->attributeType));
1975 *ae->attributeType = 1;
1976 ae->which = Z_AttributeValue_numeric;
1977 ae->value.numeric = (int *)
1978 nmem_malloc (stream, sizeof(*ae->value.numeric));
1979 *ae->value.numeric = use_value;
1981 sks->sortRelation = (int *)
1982 nmem_malloc (stream, sizeof(*sks->sortRelation));
1983 if (sort_relation_value == 1)
1984 *sks->sortRelation = Z_SortRelation_ascending;
1985 else if (sort_relation_value == 2)
1986 *sks->sortRelation = Z_SortRelation_descending;
1988 *sks->sortRelation = Z_SortRelation_ascending;
1990 sks->caseSensitivity = (int *)
1991 nmem_malloc (stream, sizeof(*sks->caseSensitivity));
1992 *sks->caseSensitivity = 0;
1995 sks->which = Z_SortKeySpec_null;
1996 sks->u.null = odr_nullval ();
1998 sks->missingValueAction = 0;
2001 sort_sequence->specs[i] = sks;
2003 parms.rset_term = rset_term_create (termz, -1, rank_type,
2005 return rset_create (rset_kind_null, &parms);
2008 struct xpath_predicate {
2011 #define XPATH_PREDICATE_RELATION 1
2017 #define XPATH_PREDICATE_BOOLEAN 2
2020 struct xpath_predicate *left;
2021 struct xpath_predicate *right;
2026 struct xpath_location_step {
2028 struct xpath_predicate *predicate;
2031 static int parse_xpath(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2032 oid_value attributeSet,
2033 struct xpath_location_step *xpath, NMEM mem)
2035 oid_value curAttributeSet = attributeSet;
2037 const char *use_string = 0;
2041 attr_init (&use, zapt, 1);
2042 attr_find_ex (&use, &curAttributeSet, &use_string);
2044 if (!use_string || *use_string != '/')
2050 while (*cp && !strchr("/[",*cp))
2055 xpath[no].predicate = 0;
2056 xpath[no].part = nmem_malloc (mem, i+1);
2057 memcpy (xpath[no].part, cp - i, i);
2058 xpath[no].part[i] = 0;
2062 struct xpath_predicate *p = xpath[no].predicate =
2063 nmem_malloc (mem, sizeof(struct xpath_predicate));
2065 p->which = XPATH_PREDICATE_RELATION;
2070 for (i = 0; *cp && !strchr("><=] ", *cp); i++)
2072 p->u.relation.name = nmem_malloc (mem, i+1);
2073 memcpy (p->u.relation.name, cp - i, i);
2074 p->u.relation.name[i] = 0;
2079 for (i = 0; *cp && strchr(">=<!", *cp); i++)
2082 p->u.relation.op = nmem_malloc (mem, i+1);
2084 memcpy (p->u.relation.op, cp - i, i);
2085 p->u.relation.op[i] = 0;
2090 if (strchr("\"'", *cp))
2093 for (i = 0; *cp && !strchr("\"'", *cp); i++)
2096 p->u.relation.value = nmem_malloc (mem, i+1);
2098 memcpy (p->u.relation.value, cp - i, i);
2099 p->u.relation.value[i] = 0;
2100 yaz_log (LOG_LOG, "value=%s", p->u.relation.value);
2106 for (i = 0; *cp && !strchr(" ]", *cp); i++)
2108 p->u.relation.value = nmem_malloc (mem, i+1);
2110 memcpy (p->u.relation.value, cp - i, i);
2111 p->u.relation.value[i] = 0;
2118 } /* end of ] predicate */
2128 static RSET xpath_trunc(ZebraHandle zh, NMEM stream,
2129 int reg_type, const char *term, int use,
2130 oid_value curAttributeSet)
2133 struct grep_info grep_info;
2134 char term_dict[2048];
2137 int ord = zebraExplain_lookupSU (zh->reg->zei, curAttributeSet, use);
2138 int ord_len, i, r, max_pos;
2140 if (grep_info_prepare (zh, 0 /* zapt */, &grep_info, '0', stream))
2146 term_dict[prefix_len++] = '|';
2148 term_dict[prefix_len++] = '(';
2150 ord_len = key_SU_encode (ord, ord_buf);
2151 for (i = 0; i<ord_len; i++)
2153 term_dict[prefix_len++] = 1;
2154 term_dict[prefix_len++] = ord_buf[i];
2156 term_dict[prefix_len++] = ')';
2157 term_dict[prefix_len++] = 1;
2158 term_dict[prefix_len++] = reg_type;
2160 strcpy (term_dict+prefix_len, term);
2162 grep_info.isam_p_indx = 0;
2163 r = dict_lookup_grep (zh->reg->dict, term_dict, 0,
2164 &grep_info, &max_pos, 0, grep_handle);
2165 yaz_log (LOG_LOG, "%s %d positions", term,
2166 grep_info.isam_p_indx);
2167 rset = rset_trunc (zh, grep_info.isam_p_buf,
2168 grep_info.isam_p_indx, term, strlen(term),
2169 "void", 1, Z_Term_characterString);
2170 grep_info_delete (&grep_info);
2174 static RSET rpn_search_xpath (ZebraHandle zh,
2175 oid_value attributeSet,
2176 int num_bases, char **basenames,
2177 NMEM stream, const char *rank_type, RSET rset,
2178 int xpath_len, struct xpath_location_step *xpath)
2180 oid_value curAttributeSet = attributeSet;
2187 yaz_log (LOG_LOG, "len=%d", xpath_len);
2188 for (i = 0; i<xpath_len; i++)
2190 yaz_log (LOG_LOG, "XPATH %d %s", i, xpath[i].part);
2194 curAttributeSet = VAL_IDXPATH;
2204 a[@attr=value]/b[@other=othervalue]
2206 /e/@a val range(e/,range(@a,freetext(w,1015,val),@a),e/)
2207 /a/b val range(b/a/,freetext(w,1016,val),b/a/)
2208 /a/b/@c val range(b/a/,range(@c,freetext(w,1016,val),@c),b/a/)
2209 /a/b[@c=y] val range(b/a/,freetext(w,1016,val),b/a/,@c=y)
2210 /a[@c=y]/b val range(a/,range(b/a/,freetext(w,1016,val),b/a/),a/,@c=y)
2211 /a[@c=x]/b[@c=y] range(a/,range(b/a/,freetext(w,1016,val),b/a/,@c=y),a/,@c=x)
2215 dict_grep_cmap (zh->reg->dict, 0, 0);
2217 for (base_no = 0; base_no < num_bases; base_no++)
2219 int level = xpath_len;
2222 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
2224 zh->errCode = 109; /* Database unavailable */
2225 zh->errString = basenames[base_no];
2228 while (--level >= 0)
2230 char xpath_rev[128];
2232 rset_between_parms parms;
2233 RSET rset_start_tag = 0, rset_end_tag = 0, rset_attr = 0;
2237 for (i = level; i >= 1; --i)
2239 const char *cp = xpath[i].part;
2245 memcpy (xpath_rev + len, "[^/]*", 5);
2248 else if (*cp == ' ')
2251 xpath_rev[len++] = 1;
2252 xpath_rev[len++] = ' ';
2256 xpath_rev[len++] = *cp;
2257 xpath_rev[len++] = '/';
2259 else if (i == 1) /* // case */
2261 xpath_rev[len++] = '.';
2262 xpath_rev[len++] = '*';
2267 if (xpath[level].predicate &&
2268 xpath[level].predicate->which == XPATH_PREDICATE_RELATION &&
2269 xpath[level].predicate->u.relation.name[0])
2271 char predicate_str[128];
2273 strcpy (predicate_str,
2274 xpath[level].predicate->u.relation.name+1);
2275 if (xpath[level].predicate->u.relation.value)
2277 strcat (predicate_str, "=");
2278 strcat (predicate_str,
2279 xpath[level].predicate->u.relation.value);
2281 rset_attr = xpath_trunc (
2282 zh, stream, '0', predicate_str, 3, curAttributeSet);
2289 yaz_log (LOG_LOG, "xpath_rev (%d) = %s", level, xpath_rev);
2290 if (strlen(xpath_rev))
2292 rset_start_tag = xpath_trunc(zh, stream,
2293 '0', xpath_rev, 1, curAttributeSet);
2295 rset_end_tag = xpath_trunc(zh, stream,
2296 '0', xpath_rev, 2, curAttributeSet);
2298 parms.key_size = sizeof(struct it_key);
2299 parms.cmp = key_compare_it;
2300 parms.rset_l = rset_start_tag;
2301 parms.rset_m = rset;
2302 parms.rset_r = rset_end_tag;
2303 parms.rset_attr = rset_attr;
2304 parms.printer = key_print_it;
2305 rset = rset_create (rset_kind_between, &parms);
2316 static RSET rpn_search_APT (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2317 oid_value attributeSet, NMEM stream,
2318 Z_SortKeySpecList *sort_sequence,
2319 int num_bases, char **basenames)
2322 char *search_type = NULL;
2323 char rank_type[128];
2326 char termz[IT_MAX_WORD+1];
2330 struct xpath_location_step xpath[10];
2332 zebra_maps_attr (zh->reg->zebra_maps, zapt, ®_id, &search_type,
2333 rank_type, &complete_flag, &sort_flag);
2335 logf (LOG_DEBUG, "reg_id=%c", reg_id);
2336 logf (LOG_DEBUG, "complete_flag=%d", complete_flag);
2337 logf (LOG_DEBUG, "search_type=%s", search_type);
2338 logf (LOG_DEBUG, "rank_type=%s", rank_type);
2340 if (trans_term (zh, zapt, termz))
2344 return rpn_sort_spec (zh, zapt, attributeSet, stream, sort_sequence,
2346 xpath_len = parse_xpath(zh, zapt, attributeSet, xpath, stream);
2350 if (xpath[xpath_len-1].part[0] == '@')
2354 if (!strcmp (search_type, "phrase"))
2356 rset = rpn_search_APT_phrase (zh, zapt, termz, attributeSet, stream,
2357 reg_id, complete_flag, rank_type,
2359 num_bases, basenames);
2361 else if (!strcmp (search_type, "and-list"))
2363 rset = rpn_search_APT_and_list (zh, zapt, termz, attributeSet, stream,
2364 reg_id, complete_flag, rank_type,
2366 num_bases, basenames);
2368 else if (!strcmp (search_type, "or-list"))
2370 rset = rpn_search_APT_or_list (zh, zapt, termz, attributeSet, stream,
2371 reg_id, complete_flag, rank_type,
2373 num_bases, basenames);
2375 else if (!strcmp (search_type, "local"))
2377 rset = rpn_search_APT_local (zh, zapt, termz, attributeSet, stream,
2380 else if (!strcmp (search_type, "numeric"))
2382 rset = rpn_search_APT_numeric (zh, zapt, termz, attributeSet, stream,
2383 reg_id, complete_flag, rank_type,
2384 num_bases, basenames);
2386 else if (!strcmp (search_type, "always"))
2392 return rpn_search_xpath (zh, attributeSet, num_bases, basenames,
2393 stream, rank_type, rset, xpath_len, xpath);
2396 static RSET rpn_search_structure (ZebraHandle zh, Z_RPNStructure *zs,
2397 oid_value attributeSet, NMEM stream,
2398 Z_SortKeySpecList *sort_sequence,
2399 int num_bases, char **basenames)
2402 if (zs->which == Z_RPNStructure_complex)
2404 Z_Operator *zop = zs->u.complex->roperator;
2405 rset_bool_parms bool_parms;
2407 bool_parms.rset_l = rpn_search_structure (zh, zs->u.complex->s1,
2408 attributeSet, stream,
2410 num_bases, basenames);
2411 if (bool_parms.rset_l == NULL)
2413 bool_parms.rset_r = rpn_search_structure (zh, zs->u.complex->s2,
2414 attributeSet, stream,
2416 num_bases, basenames);
2417 if (bool_parms.rset_r == NULL)
2419 rset_delete (bool_parms.rset_l);
2422 bool_parms.key_size = sizeof(struct it_key);
2423 bool_parms.cmp = key_compare_it;
2427 case Z_Operator_and:
2428 r = rset_create (rset_kind_and, &bool_parms);
2431 r = rset_create (rset_kind_or, &bool_parms);
2433 case Z_Operator_and_not:
2434 r = rset_create (rset_kind_not, &bool_parms);
2436 case Z_Operator_prox:
2438 if (zop->u.prox->which != Z_ProximityOperator_known)
2444 if (zop->u.prox->which != Z_ProxCode_known)
2452 if (*zop->u.prox->u.known != Z_ProxUnit_word)
2454 char *val = (char *) nmem_malloc (stream, 16);
2456 zh->errString = val;
2457 sprintf (val, "%d", *zop->u.prox->u.known);
2461 if (*zop->u.prox->proximityUnitCode != Z_ProxUnit_word)
2463 char *val = (char *) nmem_malloc (stream, 16);
2465 zh->errString = val;
2466 sprintf (val, "%d", *zop->u.prox->proximityUnitCode);
2474 rsets[0] = bool_parms.rset_l;
2475 rsets[1] = bool_parms.rset_r;
2477 r = rpn_prox (zh, rsets, 2,
2478 *zop->u.prox->ordered,
2479 (!zop->u.prox->exclusion ? 0 :
2480 *zop->u.prox->exclusion),
2481 *zop->u.prox->relationType,
2482 *zop->u.prox->distance);
2483 rset_delete (rsets[0]);
2484 rset_delete (rsets[1]);
2492 else if (zs->which == Z_RPNStructure_simple)
2494 if (zs->u.simple->which == Z_Operand_APT)
2496 logf (LOG_DEBUG, "rpn_search_APT");
2497 r = rpn_search_APT (zh, zs->u.simple->u.attributesPlusTerm,
2498 attributeSet, stream, sort_sequence,
2499 num_bases, basenames);
2501 else if (zs->u.simple->which == Z_Operand_resultSetId)
2503 logf (LOG_DEBUG, "rpn_search_ref");
2504 r = resultSetRef (zh, zs->u.simple->u.resultSetId);
2507 r = rset_create (rset_kind_null, NULL);
2510 nmem_strdup (stream, zs->u.simple->u.resultSetId);
2529 RSET rpn_search (ZebraHandle zh, NMEM nmem,
2530 Z_RPNQuery *rpn, int num_bases, char **basenames,
2531 const char *setname,
2536 oid_value attributeSet;
2537 Z_SortKeySpecList *sort_sequence;
2541 zh->errString = NULL;
2544 sort_sequence = (Z_SortKeySpecList *)
2545 nmem_malloc (nmem, sizeof(*sort_sequence));
2546 sort_sequence->num_specs = 10;
2547 sort_sequence->specs = (Z_SortKeySpec **)
2548 nmem_malloc (nmem, sort_sequence->num_specs *
2549 sizeof(*sort_sequence->specs));
2550 for (i = 0; i<sort_sequence->num_specs; i++)
2551 sort_sequence->specs[i] = 0;
2553 attrset = oid_getentbyoid (rpn->attributeSetId);
2554 attributeSet = attrset->value;
2555 rset = rpn_search_structure (zh, rpn->RPNStructure, attributeSet,
2556 nmem, sort_sequence, num_bases, basenames);
2561 logf (LOG_DEBUG, "search error: %d", zh->errCode);
2563 for (i = 0; sort_sequence->specs[i]; i++)
2565 sort_sequence->num_specs = i;
2567 resultSetRank (zh, sset, rset);
2570 logf (LOG_DEBUG, "resultSetSortSingle in rpn_search");
2571 resultSetSortSingle (zh, nmem, sset, rset,
2572 sort_sequence, &sort_status);
2575 logf (LOG_DEBUG, "resultSetSortSingle status = %d", zh->errCode);
2581 struct scan_info_entry {
2587 struct scan_info_entry *list;
2593 static int scan_handle (char *name, const char *info, int pos, void *client)
2595 int len_prefix, idx;
2596 struct scan_info *scan_info = (struct scan_info *) client;
2598 len_prefix = strlen(scan_info->prefix);
2599 if (memcmp (name, scan_info->prefix, len_prefix))
2601 if (pos > 0) idx = scan_info->after - pos + scan_info->before;
2604 scan_info->list[idx].term = (char *)
2605 odr_malloc (scan_info->odr, strlen(name + len_prefix)+1);
2606 strcpy (scan_info->list[idx].term, name + len_prefix);
2607 assert (*info == sizeof(ISAMS_P));
2608 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAMS_P));
2612 static void scan_term_untrans (ZebraHandle zh, NMEM stream, int reg_type,
2613 char **dst, const char *src)
2615 char term_dst[1024];
2617 term_untrans (zh, reg_type, term_dst, src);
2619 *dst = (char *) nmem_malloc (stream, strlen(term_dst)+1);
2620 strcpy (*dst, term_dst);
2623 static void count_set (RSET r, int *count)
2631 logf (LOG_DEBUG, "count_set");
2634 rfd = rset_open (r, RSETF_READ);
2635 while (rset_read (r, rfd, &key, &term_index))
2637 if (key.sysno != psysno)
2644 rset_close (r, rfd);
2645 logf (LOG_DEBUG, "%d keys, %d records", kno, *count);
2648 void rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
2649 oid_value attributeset,
2650 int num_bases, char **basenames,
2651 int *position, int *num_entries, ZebraScanEntry **list,
2655 int pos = *position;
2656 int num = *num_entries;
2660 char termz[IT_MAX_WORD+20];
2663 struct scan_info *scan_info_array;
2664 ZebraScanEntry *glist;
2665 int ords[32], ord_no = 0;
2669 char *search_type = NULL;
2670 char rank_type[128];
2675 if (attributeset == VAL_NONE)
2676 attributeset = VAL_BIB1;
2678 yaz_log (LOG_DEBUG, "position = %d, num = %d set=%d",
2679 pos, num, attributeset);
2681 attr_init (&use, zapt, 1);
2682 use_value = attr_find (&use, &attributeset);
2684 if (zebra_maps_attr (zh->reg->zebra_maps, zapt, ®_id, &search_type,
2685 rank_type, &complete_flag, &sort_flag))
2691 yaz_log (LOG_DEBUG, "use_value = %d", use_value);
2693 if (use_value == -1)
2695 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
2699 data1_local_attribute *local_attr;
2701 if ((r=att_getentbyatt (zh, &attp, attributeset, use_value)))
2703 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
2704 attributeset, use_value);
2712 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
2714 zh->errString = basenames[base_no];
2715 zh->errCode = 109; /* Database unavailable */
2719 for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
2720 local_attr = local_attr->next)
2724 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
2727 ords[ord_no++] = ord;
2736 /* prepare dictionary scanning */
2739 scan_info_array = (struct scan_info *)
2740 odr_malloc (stream, ord_no * sizeof(*scan_info_array));
2741 for (i = 0; i < ord_no; i++)
2743 int j, prefix_len = 0;
2744 int before_tmp = before, after_tmp = after;
2745 struct scan_info *scan_info = scan_info_array + i;
2746 struct rpn_char_map_info rcmi;
2748 rpn_char_map_prepare (zh->reg, reg_id, &rcmi);
2750 scan_info->before = before;
2751 scan_info->after = after;
2752 scan_info->odr = stream;
2754 scan_info->list = (struct scan_info_entry *)
2755 odr_malloc (stream, (before+after) * sizeof(*scan_info->list));
2756 for (j = 0; j<before+after; j++)
2757 scan_info->list[j].term = NULL;
2759 prefix_len += key_SU_encode (ords[i], termz + prefix_len);
2760 termz[prefix_len++] = reg_id;
2761 termz[prefix_len] = 0;
2762 strcpy (scan_info->prefix, termz);
2764 trans_scan_term (zh, zapt, termz+prefix_len, reg_id);
2766 dict_scan (zh->reg->dict, termz, &before_tmp, &after_tmp,
2767 scan_info, scan_handle);
2769 glist = (ZebraScanEntry *)
2770 odr_malloc (stream, (before+after)*sizeof(*glist));
2772 /* consider terms after main term */
2773 for (i = 0; i < ord_no; i++)
2777 for (i = 0; i<after; i++)
2780 const char *mterm = NULL;
2784 for (j = 0; j < ord_no; j++)
2786 if (ptr[j] < before+after &&
2787 (tst=scan_info_array[j].list[ptr[j]].term) &&
2788 (!mterm || strcmp (tst, mterm) < 0))
2796 scan_term_untrans (zh, stream->mem, reg_id,
2797 &glist[i+before].term, mterm);
2798 rset = rset_trunc (zh, &scan_info_array[j0].list[ptr[j0]].isam_p, 1,
2799 glist[i+before].term, strlen(glist[i+before].term),
2800 NULL, 0, zapt->term->which);
2803 for (j = j0+1; j<ord_no; j++)
2805 if (ptr[j] < before+after &&
2806 (tst=scan_info_array[j].list[ptr[j]].term) &&
2807 !strcmp (tst, mterm))
2809 rset_bool_parms bool_parms;
2813 rset_trunc (zh, &scan_info_array[j].list[ptr[j]].isam_p, 1,
2814 glist[i+before].term,
2815 strlen(glist[i+before].term), NULL, 0,
2818 bool_parms.key_size = sizeof(struct it_key);
2819 bool_parms.cmp = key_compare_it;
2820 bool_parms.rset_l = rset;
2821 bool_parms.rset_r = rset2;
2823 rset = rset_create (rset_kind_or, &bool_parms);
2828 count_set (rset, &glist[i+before].occurrences);
2833 *num_entries -= (after-i);
2837 /* consider terms before main term */
2838 for (i = 0; i<ord_no; i++)
2841 for (i = 0; i<before; i++)
2844 const char *mterm = NULL;
2848 for (j = 0; j <ord_no; j++)
2850 if (ptr[j] < before &&
2851 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2852 (!mterm || strcmp (tst, mterm) > 0))
2861 scan_term_untrans (zh, stream->mem, reg_id,
2862 &glist[before-1-i].term, mterm);
2865 (zh, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1,
2866 glist[before-1-i].term, strlen(glist[before-1-i].term),
2867 NULL, 0, zapt->term->which);
2871 for (j = j0+1; j<ord_no; j++)
2873 if (ptr[j] < before &&
2874 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2875 !strcmp (tst, mterm))
2877 rset_bool_parms bool_parms;
2880 rset2 = rset_trunc (zh,
2881 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1,
2882 glist[before-1-i].term,
2883 strlen(glist[before-1-i].term), NULL, 0,
2886 bool_parms.key_size = sizeof(struct it_key);
2887 bool_parms.cmp = key_compare_it;
2888 bool_parms.rset_l = rset;
2889 bool_parms.rset_r = rset2;
2891 rset = rset_create (rset_kind_or, &bool_parms);
2896 count_set (rset, &glist[before-1-i].occurrences);
2906 *list = glist + i; /* list is set to first 'real' entry */
2908 logf (LOG_DEBUG, "position = %d, num_entries = %d",
2909 *position, *num_entries);
2911 logf (LOG_DEBUG, "scan error: %d", zh->errCode);