1 /* $Id: zrpn.c,v 1.122 2002-08-28 19:52:29 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:
1108 if (zh->iconv_to_utf8 != 0)
1110 char *inbuf = term->u.general->buf;
1111 size_t inleft = term->u.general->len;
1112 char *outbuf = termz;
1113 size_t outleft = IT_MAX_WORD-1;
1116 yaz_log (LOG_DEBUG, "converting general from ISO-8859-1");
1117 ret = yaz_iconv(zh->iconv_to_utf8, &inbuf, &inleft,
1119 if (ret == (size_t)(-1))
1121 ret = yaz_iconv(zh->iconv_to_utf8, 0, 0, 0, 0);
1128 sizez = term->u.general->len;
1129 if (sizez > IT_MAX_WORD-1)
1130 sizez = IT_MAX_WORD-1;
1131 memcpy (termz, term->u.general->buf, sizez);
1132 termz[sizez] = '\0';
1134 case Z_Term_characterString:
1135 sizez = strlen(term->u.characterString);
1136 if (sizez > IT_MAX_WORD-1)
1137 sizez = IT_MAX_WORD-1;
1138 memcpy (termz, term->u.characterString, sizez);
1139 termz[sizez] = '\0';
1147 static void trans_scan_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1148 char *termz, int reg_type)
1150 Z_Term *term = zapt->term;
1152 const char *cp = (const char *) term->u.general->buf;
1153 const char *cp_end = cp + term->u.general->len;
1156 const char *space_map = NULL;
1159 while ((len = (cp_end - cp)) > 0)
1161 map = zebra_maps_input (zh->reg->zebra_maps, reg_type, &cp, len);
1162 if (**map == *CHR_SPACE)
1167 for (src = space_map; *src; src++)
1170 for (src = *map; *src; src++)
1177 static RSET rpn_prox (ZebraHandle zh, RSET *rset, int rset_no,
1178 int ordered, int exclusion, int relation, int distance)
1183 struct it_key **buf;
1185 char prox_term[1024];
1186 int length_prox_term = 0;
1187 int min_nn = 10000000;
1189 int term_type = Z_Term_characterString;
1190 const char *flags = NULL;
1192 rsfd = (RSFD *) xmalloc (sizeof(*rsfd)*rset_no);
1193 more = (int *) xmalloc (sizeof(*more)*rset_no);
1194 buf = (struct it_key **) xmalloc (sizeof(*buf)*rset_no);
1197 for (i = 0; i<rset_no; i++)
1200 for (j = 0; j<rset[i]->no_rset_terms; j++)
1202 const char *nflags = rset[i]->rset_terms[j]->flags;
1203 char *term = rset[i]->rset_terms[j]->name;
1204 int lterm = strlen(term);
1205 if (lterm + length_prox_term < sizeof(prox_term)-1)
1207 if (length_prox_term)
1208 prox_term[length_prox_term++] = ' ';
1209 strcpy (prox_term + length_prox_term, term);
1210 length_prox_term += lterm;
1212 if (min_nn > rset[i]->rset_terms[j]->nn)
1213 min_nn = rset[i]->rset_terms[j]->nn;
1215 term_type = rset[i]->rset_terms[j]->type;
1217 /* only if all term types are of type characterString .. */
1218 /* the resulting term is of that type */
1219 if (term_type != Z_Term_characterString)
1220 term_type = Z_Term_general;
1223 for (i = 0; i<rset_no; i++)
1228 for (i = 0; i<rset_no; i++)
1230 buf[i] = (struct it_key *) xmalloc (sizeof(**buf));
1231 rsfd[i] = rset_open (rset[i], RSETF_READ);
1232 if (!(more[i] = rset_read (rset[i], rsfd[i], buf[i], &term_index)))
1237 /* at least one is empty ... return null set */
1238 rset_null_parms parms;
1240 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1242 parms.rset_term->nn = 0;
1243 result = rset_create (rset_kind_null, &parms);
1245 else if (ordered && relation == 3 && exclusion == 0 && distance == 1)
1247 /* special proximity case = phrase search ... */
1248 rset_temp_parms parms;
1251 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1253 parms.rset_term->nn = min_nn;
1254 parms.cmp = key_compare_it;
1255 parms.key_size = sizeof (struct it_key);
1256 parms.temp_path = res_get (zh->res, "setTmpDir");
1257 result = rset_create (rset_kind_temp, &parms);
1258 rsfd_result = rset_open (result, RSETF_WRITE);
1262 for (i = 1; i<rset_no; i++)
1271 cmp = key_compare_it (buf[i], buf[i-1]);
1274 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1275 buf[i-1], &term_index);
1280 if (buf[i-1]->seqno+1 != buf[i]->seqno)
1282 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1283 buf[i-1], &term_index);
1289 more[i] = rset_read (rset[i], rsfd[i], buf[i],
1296 rset_write (result, rsfd_result, buf[0]);
1297 more[0] = rset_read (*rset, *rsfd, *buf, &term_index);
1300 rset_close (result, rsfd_result);
1302 else if (rset_no == 2)
1304 /* generic proximity case (two input sets only) ... */
1305 rset_temp_parms parms;
1308 logf (LOG_LOG, "generic prox, dist = %d, relation = %d, ordered =%d, exclusion=%d",
1309 distance, relation, ordered, exclusion);
1310 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1312 parms.rset_term->nn = min_nn;
1313 parms.cmp = key_compare_it;
1314 parms.key_size = sizeof (struct it_key);
1315 parms.temp_path = res_get (zh->res, "setTmpDir");
1316 result = rset_create (rset_kind_temp, &parms);
1317 rsfd_result = rset_open (result, RSETF_WRITE);
1319 while (more[0] && more[1])
1321 int cmp = key_compare_it (buf[0], buf[1]);
1323 more[0] = rset_read (rset[0], rsfd[0], buf[0], &term_index);
1325 more[1] = rset_read (rset[1], rsfd[1], buf[1], &term_index);
1328 int sysno = buf[0]->sysno;
1332 seqno[n++] = buf[0]->seqno;
1333 while ((more[0] = rset_read (rset[0], rsfd[0], buf[0],
1335 sysno == buf[0]->sysno)
1337 seqno[n++] = buf[0]->seqno;
1340 for (i = 0; i<n; i++)
1342 int diff = buf[1]->seqno - seqno[i];
1343 int excl = exclusion;
1344 if (!ordered && diff < 0)
1349 if (diff < distance && diff >= 0)
1353 if (diff <= distance && diff >= 0)
1357 if (diff == distance && diff >= 0)
1361 if (diff >= distance && diff >= 0)
1365 if (diff > distance && diff >= 0)
1369 if (diff != distance && diff >= 0)
1375 rset_write (result, rsfd_result, buf[1]);
1379 } while ((more[1] = rset_read (rset[1], rsfd[1], buf[1],
1381 sysno == buf[1]->sysno);
1384 rset_close (result, rsfd_result);
1388 rset_null_parms parms;
1390 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1392 parms.rset_term->nn = 0;
1393 result = rset_create (rset_kind_null, &parms);
1395 for (i = 0; i<rset_no; i++)
1398 rset_close (rset[i], rsfd[i]);
1408 char *normalize_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1409 const char *termz, NMEM stream, unsigned reg_id)
1412 AttrType truncation;
1413 int truncation_value;
1416 attr_init (&truncation, zapt, 5);
1417 truncation_value = attr_find (&truncation, NULL);
1419 switch (truncation_value)
1439 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, ex_list,
1440 termz, strlen(termz));
1442 return nmem_strdup(stream, termz);
1445 char *buf = (char*) nmem_malloc (stream, wrbuf_len(wrbuf)+1);
1446 memcpy (buf, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1447 buf[wrbuf_len(wrbuf)] = '\0';
1452 static void grep_info_delete (struct grep_info *grep_info)
1455 xfree(grep_info->term_no);
1457 xfree (grep_info->isam_p_buf);
1460 static int grep_info_prepare (ZebraHandle zh,
1461 Z_AttributesPlusTerm *zapt,
1462 struct grep_info *grep_info,
1467 int termset_value_numeric;
1468 const char *termset_value_string;
1471 grep_info->term_no = 0;
1473 grep_info->isam_p_size = 0;
1474 grep_info->isam_p_buf = NULL;
1476 grep_info->reg_type = reg_type;
1477 grep_info->termset = 0;
1481 attr_init (&termset, zapt, 8);
1482 termset_value_numeric =
1483 attr_find_ex (&termset, NULL, &termset_value_string);
1484 if (termset_value_numeric != -1)
1487 const char *termset_name = 0;
1488 if (termset_value_numeric != -2)
1491 sprintf (resname, "%d", termset_value_numeric);
1492 termset_name = resname;
1495 termset_name = termset_value_string;
1496 logf (LOG_LOG, "creating termset set %s", termset_name);
1497 grep_info->termset = resultSetAdd (zh, termset_name, 1);
1498 if (!grep_info->termset)
1501 zh->errString = nmem_strdup (stream, termset_name);
1509 static RSET rpn_search_APT_phrase (ZebraHandle zh,
1510 Z_AttributesPlusTerm *zapt,
1511 const char *termz_org,
1512 oid_value attributeSet,
1514 int reg_type, int complete_flag,
1515 const char *rank_type, int xpath_use,
1516 int num_bases, char **basenames)
1518 char term_dst[IT_MAX_WORD+1];
1519 RSET rset[60], result;
1521 struct grep_info grep_info;
1522 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1523 const char *termp = termz;
1526 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1530 logf (LOG_DEBUG, "APT_phrase termp=%s", termp);
1531 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1533 reg_type, complete_flag,
1534 num_bases, basenames,
1535 term_dst, rank_type,
1539 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1542 grep_info_delete (&grep_info);
1545 rset_null_parms parms;
1547 parms.rset_term = rset_term_create (termz, -1, rank_type,
1549 return rset_create (rset_kind_null, &parms);
1551 else if (rset_no == 1)
1553 result = rpn_prox (zh, rset, rset_no, 1, 0, 3, 1);
1554 for (i = 0; i<rset_no; i++)
1555 rset_delete (rset[i]);
1559 static RSET rpn_search_APT_or_list (ZebraHandle zh,
1560 Z_AttributesPlusTerm *zapt,
1561 const char *termz_org,
1562 oid_value attributeSet,
1564 int reg_type, int complete_flag,
1565 const char *rank_type,
1567 int num_bases, char **basenames)
1569 char term_dst[IT_MAX_WORD+1];
1570 RSET rset[60], result;
1572 struct grep_info grep_info;
1573 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1574 const char *termp = termz;
1576 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1580 logf (LOG_DEBUG, "APT_or_list termp=%s", termp);
1581 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1583 reg_type, complete_flag,
1584 num_bases, basenames,
1585 term_dst, rank_type,
1589 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1592 grep_info_delete (&grep_info);
1595 rset_null_parms parms;
1597 parms.rset_term = rset_term_create (termz, -1, rank_type,
1599 return rset_create (rset_kind_null, &parms);
1602 for (i = 1; i<rset_no; i++)
1604 rset_bool_parms bool_parms;
1606 bool_parms.rset_l = result;
1607 bool_parms.rset_r = rset[i];
1608 bool_parms.key_size = sizeof(struct it_key);
1609 bool_parms.cmp = key_compare_it;
1610 result = rset_create (rset_kind_or, &bool_parms);
1615 static RSET rpn_search_APT_and_list (ZebraHandle zh,
1616 Z_AttributesPlusTerm *zapt,
1617 const char *termz_org,
1618 oid_value attributeSet,
1620 int reg_type, int complete_flag,
1621 const char *rank_type,
1623 int num_bases, char **basenames)
1625 char term_dst[IT_MAX_WORD+1];
1626 RSET rset[60], result;
1628 struct grep_info grep_info;
1629 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1630 const char *termp = termz;
1632 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1636 logf (LOG_DEBUG, "APT_and_list termp=%s", termp);
1637 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1639 reg_type, complete_flag,
1640 num_bases, basenames,
1641 term_dst, rank_type,
1645 assert (rset[rset_no]);
1646 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1649 grep_info_delete (&grep_info);
1652 rset_null_parms parms;
1654 parms.rset_term = rset_term_create (termz, -1, rank_type,
1656 return rset_create (rset_kind_null, &parms);
1659 for (i = 1; i<rset_no; i++)
1661 rset_bool_parms bool_parms;
1663 bool_parms.rset_l = result;
1664 bool_parms.rset_r = rset[i];
1665 bool_parms.key_size = sizeof(struct it_key);
1666 bool_parms.cmp = key_compare_it;
1667 result = rset_create (rset_kind_and, &bool_parms);
1672 static int numeric_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1673 const char **term_sub,
1675 oid_value attributeSet,
1676 struct grep_info *grep_info,
1685 char *term_tmp = term_dict + strlen(term_dict);
1687 attr_init (&relation, zapt, 2);
1688 relation_value = attr_find (&relation, NULL);
1690 logf (LOG_DEBUG, "numeric relation value=%d", relation_value);
1692 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub, term_tmp, 1,
1695 term_value = atoi (term_tmp);
1696 switch (relation_value)
1699 logf (LOG_DEBUG, "Relation <");
1700 gen_regular_rel (term_tmp, term_value-1, 1);
1703 logf (LOG_DEBUG, "Relation <=");
1704 gen_regular_rel (term_tmp, term_value, 1);
1707 logf (LOG_DEBUG, "Relation >=");
1708 gen_regular_rel (term_tmp, term_value, 0);
1711 logf (LOG_DEBUG, "Relation >");
1712 gen_regular_rel (term_tmp, term_value+1, 0);
1716 logf (LOG_DEBUG, "Relation =");
1717 sprintf (term_tmp, "(0*%d)", term_value);
1719 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_tmp);
1720 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info, max_pos,
1723 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1724 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1728 static int numeric_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1729 const char **term_sub,
1730 oid_value attributeSet, struct grep_info *grep_info,
1731 int reg_type, int complete_flag,
1732 int num_bases, char **basenames,
1735 char term_dict[2*IT_MAX_WORD+2];
1739 oid_value curAttributeSet = attributeSet;
1741 struct rpn_char_map_info rcmi;
1743 rpn_char_map_prepare (zh->reg, reg_type, &rcmi);
1744 attr_init (&use, zapt, 1);
1745 use_value = attr_find (&use, &curAttributeSet);
1746 logf (LOG_DEBUG, "numeric_term, use value %d", use_value);
1748 if (use_value == -1)
1751 for (base_no = 0; base_no < num_bases; base_no++)
1754 data1_local_attribute *local_attr;
1755 int max_pos, prefix_len = 0;
1758 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
1760 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1761 curAttributeSet, use_value, r);
1768 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
1770 zh->errCode = 109; /* Database unavailable */
1771 zh->errString = basenames[base_no];
1774 for (local_attr = attp.local_attributes; local_attr;
1775 local_attr = local_attr->next)
1781 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
1786 term_dict[prefix_len++] = '|';
1788 term_dict[prefix_len++] = '(';
1790 ord_len = key_SU_encode (ord, ord_buf);
1791 for (i = 0; i<ord_len; i++)
1793 term_dict[prefix_len++] = 1;
1794 term_dict[prefix_len++] = ord_buf[i];
1802 term_dict[prefix_len++] = ')';
1803 term_dict[prefix_len++] = 1;
1804 term_dict[prefix_len++] = reg_type;
1805 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1806 term_dict[prefix_len] = '\0';
1807 if (!numeric_relation (zh, zapt, &termp, term_dict,
1808 attributeSet, grep_info, &max_pos, reg_type,
1813 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1817 static RSET rpn_search_APT_numeric (ZebraHandle zh,
1818 Z_AttributesPlusTerm *zapt,
1820 oid_value attributeSet,
1822 int reg_type, int complete_flag,
1823 const char *rank_type,
1824 int num_bases, char **basenames)
1826 char term_dst[IT_MAX_WORD+1];
1827 const char *termp = termz;
1828 RSET rset[60], result;
1829 int i, r, rset_no = 0;
1830 struct grep_info grep_info;
1832 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1836 logf (LOG_DEBUG, "APT_numeric termp=%s", termp);
1837 grep_info.isam_p_indx = 0;
1838 r = numeric_term (zh, zapt, &termp, attributeSet, &grep_info,
1839 reg_type, complete_flag, num_bases, basenames,
1843 logf (LOG_DEBUG, "term: %s", term_dst);
1844 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1845 grep_info.isam_p_indx, term_dst,
1846 strlen(term_dst), rank_type,
1847 0 /* preserve position */,
1849 assert (rset[rset_no]);
1850 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1853 grep_info_delete (&grep_info);
1856 rset_null_parms parms;
1858 parms.rset_term = rset_term_create (term_dst, -1, rank_type,
1860 return rset_create (rset_kind_null, &parms);
1863 for (i = 1; i<rset_no; i++)
1865 rset_bool_parms bool_parms;
1867 bool_parms.rset_l = result;
1868 bool_parms.rset_r = rset[i];
1869 bool_parms.key_size = sizeof(struct it_key);
1870 bool_parms.cmp = key_compare_it;
1871 result = rset_create (rset_kind_and, &bool_parms);
1876 static RSET rpn_search_APT_local (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1878 oid_value attributeSet,
1880 const char *rank_type)
1885 rset_temp_parms parms;
1887 parms.rset_term = rset_term_create (termz, -1, rank_type,
1889 parms.cmp = key_compare_it;
1890 parms.key_size = sizeof (struct it_key);
1891 parms.temp_path = res_get (zh->res, "setTmpDir");
1892 result = rset_create (rset_kind_temp, &parms);
1893 rsfd = rset_open (result, RSETF_WRITE);
1895 key.sysno = atoi (termz);
1899 rset_write (result, rsfd, &key);
1900 rset_close (result, rsfd);
1904 static RSET rpn_sort_spec (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1905 oid_value attributeSet, NMEM stream,
1906 Z_SortKeySpecList *sort_sequence,
1907 const char *rank_type)
1909 rset_null_parms parms;
1911 int sort_relation_value;
1912 AttrType sort_relation_type;
1917 Z_AttributeElement *ae;
1922 attr_init (&sort_relation_type, zapt, 7);
1923 sort_relation_value = attr_find (&sort_relation_type, &attributeSet);
1925 attr_init (&use_type, zapt, 1);
1926 use_value = attr_find (&use_type, &attributeSet);
1928 if (!sort_sequence->specs)
1930 sort_sequence->num_specs = 10;
1931 sort_sequence->specs = (Z_SortKeySpec **)
1932 nmem_malloc (stream, sort_sequence->num_specs *
1933 sizeof(*sort_sequence->specs));
1934 for (i = 0; i<sort_sequence->num_specs; i++)
1935 sort_sequence->specs[i] = 0;
1937 if (zapt->term->which != Z_Term_general)
1940 i = atoi_n ((char *) zapt->term->u.general->buf,
1941 zapt->term->u.general->len);
1942 if (i >= sort_sequence->num_specs)
1944 sprintf (termz, "%d", i);
1946 oe.proto = PROTO_Z3950;
1947 oe.oclass = CLASS_ATTSET;
1948 oe.value = attributeSet;
1949 if (!oid_ent_to_oid (&oe, oid))
1952 sks = (Z_SortKeySpec *) nmem_malloc (stream, sizeof(*sks));
1953 sks->sortElement = (Z_SortElement *)
1954 nmem_malloc (stream, sizeof(*sks->sortElement));
1955 sks->sortElement->which = Z_SortElement_generic;
1956 sk = sks->sortElement->u.generic = (Z_SortKey *)
1957 nmem_malloc (stream, sizeof(*sk));
1958 sk->which = Z_SortKey_sortAttributes;
1959 sk->u.sortAttributes = (Z_SortAttributes *)
1960 nmem_malloc (stream, sizeof(*sk->u.sortAttributes));
1962 sk->u.sortAttributes->id = oid;
1963 sk->u.sortAttributes->list = (Z_AttributeList *)
1964 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list));
1965 sk->u.sortAttributes->list->num_attributes = 1;
1966 sk->u.sortAttributes->list->attributes = (Z_AttributeElement **)
1967 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list->attributes));
1968 ae = *sk->u.sortAttributes->list->attributes = (Z_AttributeElement *)
1969 nmem_malloc (stream, sizeof(**sk->u.sortAttributes->list->attributes));
1970 ae->attributeSet = 0;
1971 ae->attributeType = (int *)
1972 nmem_malloc (stream, sizeof(*ae->attributeType));
1973 *ae->attributeType = 1;
1974 ae->which = Z_AttributeValue_numeric;
1975 ae->value.numeric = (int *)
1976 nmem_malloc (stream, sizeof(*ae->value.numeric));
1977 *ae->value.numeric = use_value;
1979 sks->sortRelation = (int *)
1980 nmem_malloc (stream, sizeof(*sks->sortRelation));
1981 if (sort_relation_value == 1)
1982 *sks->sortRelation = Z_SortRelation_ascending;
1983 else if (sort_relation_value == 2)
1984 *sks->sortRelation = Z_SortRelation_descending;
1986 *sks->sortRelation = Z_SortRelation_ascending;
1988 sks->caseSensitivity = (int *)
1989 nmem_malloc (stream, sizeof(*sks->caseSensitivity));
1990 *sks->caseSensitivity = 0;
1993 sks->which = Z_SortKeySpec_null;
1994 sks->u.null = odr_nullval ();
1996 sks->missingValueAction = 0;
1999 sort_sequence->specs[i] = sks;
2001 parms.rset_term = rset_term_create (termz, -1, rank_type,
2003 return rset_create (rset_kind_null, &parms);
2006 struct xpath_predicate {
2009 #define XPATH_PREDICATE_RELATION 1
2015 #define XPATH_PREDICATE_BOOLEAN 2
2018 struct xpath_predicate *left;
2019 struct xpath_predicate *right;
2024 struct xpath_location_step {
2026 struct xpath_predicate *predicate;
2029 static int parse_xpath(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2030 oid_value attributeSet,
2031 struct xpath_location_step *xpath, NMEM mem)
2033 oid_value curAttributeSet = attributeSet;
2035 const char *use_string = 0;
2039 attr_init (&use, zapt, 1);
2040 attr_find_ex (&use, &curAttributeSet, &use_string);
2042 if (!use_string || *use_string != '/')
2048 while (*cp && !strchr("/[",*cp))
2053 xpath[no].predicate = 0;
2054 xpath[no].part = nmem_malloc (mem, i+1);
2055 memcpy (xpath[no].part, cp - i, i);
2056 xpath[no].part[i] = 0;
2060 struct xpath_predicate *p = xpath[no].predicate =
2061 nmem_malloc (mem, sizeof(struct xpath_predicate));
2063 p->which = XPATH_PREDICATE_RELATION;
2068 for (i = 0; *cp && !strchr("><=] ", *cp); i++)
2070 p->u.relation.name = nmem_malloc (mem, i+1);
2071 memcpy (p->u.relation.name, cp - i, i);
2072 p->u.relation.name[i] = 0;
2077 for (i = 0; *cp && strchr(">=<!", *cp); i++)
2080 p->u.relation.op = nmem_malloc (mem, i+1);
2082 memcpy (p->u.relation.op, cp - i, i);
2083 p->u.relation.op[i] = 0;
2088 if (strchr("\"'", *cp))
2091 for (i = 0; *cp && !strchr("\"'", *cp); i++)
2094 p->u.relation.value = nmem_malloc (mem, i+1);
2096 memcpy (p->u.relation.value, cp - i, i);
2097 p->u.relation.value[i] = 0;
2098 yaz_log (LOG_LOG, "value=%s", p->u.relation.value);
2104 for (i = 0; *cp && !strchr(" ]", *cp); i++)
2106 p->u.relation.value = nmem_malloc (mem, i+1);
2108 memcpy (p->u.relation.value, cp - i, i);
2109 p->u.relation.value[i] = 0;
2116 } /* end of ] predicate */
2126 static RSET xpath_trunc(ZebraHandle zh, NMEM stream,
2127 int reg_type, const char *term, int use,
2128 oid_value curAttributeSet)
2131 struct grep_info grep_info;
2132 char term_dict[2048];
2135 int ord = zebraExplain_lookupSU (zh->reg->zei, curAttributeSet, use);
2136 int ord_len, i, r, max_pos;
2137 int term_type = Z_Term_characterString;
2138 const char *flags = "void";
2140 if (grep_info_prepare (zh, 0 /* zapt */, &grep_info, '0', stream))
2142 rset_null_parms parms;
2144 parms.rset_term = rset_term_create (term, strlen(term),
2146 parms.rset_term->nn = 0;
2147 return rset_create (rset_kind_null, &parms);
2152 rset_null_parms parms;
2154 parms.rset_term = rset_term_create (term, strlen(term),
2156 parms.rset_term->nn = 0;
2157 return rset_create (rset_kind_null, &parms);
2160 term_dict[prefix_len++] = '|';
2162 term_dict[prefix_len++] = '(';
2164 ord_len = key_SU_encode (ord, ord_buf);
2165 for (i = 0; i<ord_len; i++)
2167 term_dict[prefix_len++] = 1;
2168 term_dict[prefix_len++] = ord_buf[i];
2170 term_dict[prefix_len++] = ')';
2171 term_dict[prefix_len++] = 1;
2172 term_dict[prefix_len++] = reg_type;
2174 strcpy (term_dict+prefix_len, term);
2176 grep_info.isam_p_indx = 0;
2177 r = dict_lookup_grep (zh->reg->dict, term_dict, 0,
2178 &grep_info, &max_pos, 0, grep_handle);
2179 yaz_log (LOG_LOG, "%s %d positions", term,
2180 grep_info.isam_p_indx);
2181 rset = rset_trunc (zh, grep_info.isam_p_buf,
2182 grep_info.isam_p_indx, term, strlen(term),
2183 flags, 1, term_type);
2184 grep_info_delete (&grep_info);
2188 static RSET rpn_search_xpath (ZebraHandle zh,
2189 oid_value attributeSet,
2190 int num_bases, char **basenames,
2191 NMEM stream, const char *rank_type, RSET rset,
2192 int xpath_len, struct xpath_location_step *xpath)
2194 oid_value curAttributeSet = attributeSet;
2201 yaz_log (LOG_LOG, "len=%d", xpath_len);
2202 for (i = 0; i<xpath_len; i++)
2204 yaz_log (LOG_LOG, "XPATH %d %s", i, xpath[i].part);
2208 curAttributeSet = VAL_IDXPATH;
2218 a[@attr=value]/b[@other=othervalue]
2220 /e/@a val range(e/,range(@a,freetext(w,1015,val),@a),e/)
2221 /a/b val range(b/a/,freetext(w,1016,val),b/a/)
2222 /a/b/@c val range(b/a/,range(@c,freetext(w,1016,val),@c),b/a/)
2223 /a/b[@c=y] val range(b/a/,freetext(w,1016,val),b/a/,@c=y)
2224 /a[@c=y]/b val range(a/,range(b/a/,freetext(w,1016,val),b/a/),a/,@c=y)
2225 /a[@c=x]/b[@c=y] range(a/,range(b/a/,freetext(w,1016,val),b/a/,@c=y),a/,@c=x)
2229 dict_grep_cmap (zh->reg->dict, 0, 0);
2231 for (base_no = 0; base_no < num_bases; base_no++)
2233 int level = xpath_len;
2236 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
2238 zh->errCode = 109; /* Database unavailable */
2239 zh->errString = basenames[base_no];
2242 while (--level >= 0)
2244 char xpath_rev[128];
2246 rset_between_parms parms;
2247 RSET rset_start_tag = 0, rset_end_tag = 0, rset_attr = 0;
2251 for (i = level; i >= 1; --i)
2253 const char *cp = xpath[i].part;
2259 memcpy (xpath_rev + len, "[^/]*", 5);
2262 else if (*cp == ' ')
2265 xpath_rev[len++] = 1;
2266 xpath_rev[len++] = ' ';
2270 xpath_rev[len++] = *cp;
2271 xpath_rev[len++] = '/';
2273 else if (i == 1) /* // case */
2275 xpath_rev[len++] = '.';
2276 xpath_rev[len++] = '*';
2281 if (xpath[level].predicate &&
2282 xpath[level].predicate->which == XPATH_PREDICATE_RELATION &&
2283 xpath[level].predicate->u.relation.name[0])
2285 char predicate_str[128];
2287 strcpy (predicate_str,
2288 xpath[level].predicate->u.relation.name+1);
2289 if (xpath[level].predicate->u.relation.value)
2291 strcat (predicate_str, "=");
2292 strcat (predicate_str,
2293 xpath[level].predicate->u.relation.value);
2295 rset_attr = xpath_trunc (
2296 zh, stream, '0', predicate_str, 3, curAttributeSet);
2303 yaz_log (LOG_LOG, "xpath_rev (%d) = %s", level, xpath_rev);
2304 if (strlen(xpath_rev))
2306 rset_start_tag = xpath_trunc(zh, stream,
2307 '0', xpath_rev, 1, curAttributeSet);
2309 rset_end_tag = xpath_trunc(zh, stream,
2310 '0', xpath_rev, 2, curAttributeSet);
2312 parms.key_size = sizeof(struct it_key);
2313 parms.cmp = key_compare_it;
2314 parms.rset_l = rset_start_tag;
2315 parms.rset_m = rset;
2316 parms.rset_r = rset_end_tag;
2317 parms.rset_attr = rset_attr;
2318 parms.printer = key_print_it;
2319 rset = rset_create (rset_kind_between, &parms);
2330 static RSET rpn_search_APT (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2331 oid_value attributeSet, NMEM stream,
2332 Z_SortKeySpecList *sort_sequence,
2333 int num_bases, char **basenames)
2336 char *search_type = NULL;
2337 char rank_type[128];
2340 char termz[IT_MAX_WORD+1];
2344 struct xpath_location_step xpath[10];
2346 zebra_maps_attr (zh->reg->zebra_maps, zapt, ®_id, &search_type,
2347 rank_type, &complete_flag, &sort_flag);
2349 logf (LOG_DEBUG, "reg_id=%c", reg_id);
2350 logf (LOG_DEBUG, "complete_flag=%d", complete_flag);
2351 logf (LOG_DEBUG, "search_type=%s", search_type);
2352 logf (LOG_DEBUG, "rank_type=%s", rank_type);
2354 if (trans_term (zh, zapt, termz))
2358 return rpn_sort_spec (zh, zapt, attributeSet, stream, sort_sequence,
2360 xpath_len = parse_xpath(zh, zapt, attributeSet, xpath, stream);
2364 if (xpath[xpath_len-1].part[0] == '@')
2368 if (!strcmp (search_type, "phrase"))
2370 rset = rpn_search_APT_phrase (zh, zapt, termz, attributeSet, stream,
2371 reg_id, complete_flag, rank_type,
2373 num_bases, basenames);
2375 else if (!strcmp (search_type, "and-list"))
2377 rset = rpn_search_APT_and_list (zh, zapt, termz, attributeSet, stream,
2378 reg_id, complete_flag, rank_type,
2380 num_bases, basenames);
2382 else if (!strcmp (search_type, "or-list"))
2384 rset = rpn_search_APT_or_list (zh, zapt, termz, attributeSet, stream,
2385 reg_id, complete_flag, rank_type,
2387 num_bases, basenames);
2389 else if (!strcmp (search_type, "local"))
2391 rset = rpn_search_APT_local (zh, zapt, termz, attributeSet, stream,
2394 else if (!strcmp (search_type, "numeric"))
2396 rset = rpn_search_APT_numeric (zh, zapt, termz, attributeSet, stream,
2397 reg_id, complete_flag, rank_type,
2398 num_bases, basenames);
2400 else if (!strcmp (search_type, "always"))
2406 return rpn_search_xpath (zh, attributeSet, num_bases, basenames,
2407 stream, rank_type, rset, xpath_len, xpath);
2410 static RSET rpn_search_structure (ZebraHandle zh, Z_RPNStructure *zs,
2411 oid_value attributeSet, NMEM stream,
2412 Z_SortKeySpecList *sort_sequence,
2413 int num_bases, char **basenames)
2416 if (zs->which == Z_RPNStructure_complex)
2418 Z_Operator *zop = zs->u.complex->roperator;
2419 rset_bool_parms bool_parms;
2421 bool_parms.rset_l = rpn_search_structure (zh, zs->u.complex->s1,
2422 attributeSet, stream,
2424 num_bases, basenames);
2425 if (bool_parms.rset_l == NULL)
2427 bool_parms.rset_r = rpn_search_structure (zh, zs->u.complex->s2,
2428 attributeSet, stream,
2430 num_bases, basenames);
2431 if (bool_parms.rset_r == NULL)
2433 rset_delete (bool_parms.rset_l);
2436 bool_parms.key_size = sizeof(struct it_key);
2437 bool_parms.cmp = key_compare_it;
2441 case Z_Operator_and:
2442 r = rset_create (rset_kind_and, &bool_parms);
2445 r = rset_create (rset_kind_or, &bool_parms);
2447 case Z_Operator_and_not:
2448 r = rset_create (rset_kind_not, &bool_parms);
2450 case Z_Operator_prox:
2452 if (zop->u.prox->which != Z_ProximityOperator_known)
2458 if (zop->u.prox->which != Z_ProxCode_known)
2466 if (*zop->u.prox->u.known != Z_ProxUnit_word)
2468 char *val = (char *) nmem_malloc (stream, 16);
2470 zh->errString = val;
2471 sprintf (val, "%d", *zop->u.prox->u.known);
2475 if (*zop->u.prox->proximityUnitCode != Z_ProxUnit_word)
2477 char *val = (char *) nmem_malloc (stream, 16);
2479 zh->errString = val;
2480 sprintf (val, "%d", *zop->u.prox->proximityUnitCode);
2488 rsets[0] = bool_parms.rset_l;
2489 rsets[1] = bool_parms.rset_r;
2491 r = rpn_prox (zh, rsets, 2,
2492 *zop->u.prox->ordered,
2493 (!zop->u.prox->exclusion ? 0 :
2494 *zop->u.prox->exclusion),
2495 *zop->u.prox->relationType,
2496 *zop->u.prox->distance);
2497 rset_delete (rsets[0]);
2498 rset_delete (rsets[1]);
2506 else if (zs->which == Z_RPNStructure_simple)
2508 if (zs->u.simple->which == Z_Operand_APT)
2510 logf (LOG_DEBUG, "rpn_search_APT");
2511 r = rpn_search_APT (zh, zs->u.simple->u.attributesPlusTerm,
2512 attributeSet, stream, sort_sequence,
2513 num_bases, basenames);
2515 else if (zs->u.simple->which == Z_Operand_resultSetId)
2517 logf (LOG_DEBUG, "rpn_search_ref");
2518 r = resultSetRef (zh, zs->u.simple->u.resultSetId);
2521 r = rset_create (rset_kind_null, NULL);
2524 nmem_strdup (stream, zs->u.simple->u.resultSetId);
2543 RSET rpn_search (ZebraHandle zh, NMEM nmem,
2544 Z_RPNQuery *rpn, int num_bases, char **basenames,
2545 const char *setname,
2550 oid_value attributeSet;
2551 Z_SortKeySpecList *sort_sequence;
2555 zh->errString = NULL;
2558 sort_sequence = (Z_SortKeySpecList *)
2559 nmem_malloc (nmem, sizeof(*sort_sequence));
2560 sort_sequence->num_specs = 10;
2561 sort_sequence->specs = (Z_SortKeySpec **)
2562 nmem_malloc (nmem, sort_sequence->num_specs *
2563 sizeof(*sort_sequence->specs));
2564 for (i = 0; i<sort_sequence->num_specs; i++)
2565 sort_sequence->specs[i] = 0;
2567 attrset = oid_getentbyoid (rpn->attributeSetId);
2568 attributeSet = attrset->value;
2569 rset = rpn_search_structure (zh, rpn->RPNStructure, attributeSet,
2570 nmem, sort_sequence, num_bases, basenames);
2575 logf (LOG_DEBUG, "search error: %d", zh->errCode);
2577 for (i = 0; sort_sequence->specs[i]; i++)
2579 sort_sequence->num_specs = i;
2581 resultSetRank (zh, sset, rset);
2584 logf (LOG_DEBUG, "resultSetSortSingle in rpn_search");
2585 resultSetSortSingle (zh, nmem, sset, rset,
2586 sort_sequence, &sort_status);
2589 logf (LOG_DEBUG, "resultSetSortSingle status = %d", zh->errCode);
2595 struct scan_info_entry {
2601 struct scan_info_entry *list;
2607 static int scan_handle (char *name, const char *info, int pos, void *client)
2609 int len_prefix, idx;
2610 struct scan_info *scan_info = (struct scan_info *) client;
2612 len_prefix = strlen(scan_info->prefix);
2613 if (memcmp (name, scan_info->prefix, len_prefix))
2615 if (pos > 0) idx = scan_info->after - pos + scan_info->before;
2618 scan_info->list[idx].term = (char *)
2619 odr_malloc (scan_info->odr, strlen(name + len_prefix)+1);
2620 strcpy (scan_info->list[idx].term, name + len_prefix);
2621 assert (*info == sizeof(ISAMS_P));
2622 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAMS_P));
2626 static void scan_term_untrans (ZebraHandle zh, NMEM stream, int reg_type,
2627 char **dst, const char *src)
2629 char term_dst[1024];
2631 term_untrans (zh, reg_type, term_dst, src);
2633 *dst = (char *) nmem_malloc (stream, strlen(term_dst)+1);
2634 strcpy (*dst, term_dst);
2637 static void count_set (RSET r, int *count)
2645 logf (LOG_DEBUG, "count_set");
2648 rfd = rset_open (r, RSETF_READ);
2649 while (rset_read (r, rfd, &key, &term_index))
2651 if (key.sysno != psysno)
2658 rset_close (r, rfd);
2659 logf (LOG_DEBUG, "%d keys, %d records", kno, *count);
2662 void rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
2663 oid_value attributeset,
2664 int num_bases, char **basenames,
2665 int *position, int *num_entries, ZebraScanEntry **list,
2669 int pos = *position;
2670 int num = *num_entries;
2674 char termz[IT_MAX_WORD+20];
2677 struct scan_info *scan_info_array;
2678 ZebraScanEntry *glist;
2679 int ords[32], ord_no = 0;
2683 char *search_type = NULL;
2684 char rank_type[128];
2689 if (attributeset == VAL_NONE)
2690 attributeset = VAL_BIB1;
2692 yaz_log (LOG_DEBUG, "position = %d, num = %d set=%d",
2693 pos, num, attributeset);
2695 attr_init (&use, zapt, 1);
2696 use_value = attr_find (&use, &attributeset);
2698 if (zebra_maps_attr (zh->reg->zebra_maps, zapt, ®_id, &search_type,
2699 rank_type, &complete_flag, &sort_flag))
2705 yaz_log (LOG_DEBUG, "use_value = %d", use_value);
2707 if (use_value == -1)
2709 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
2713 data1_local_attribute *local_attr;
2715 if ((r=att_getentbyatt (zh, &attp, attributeset, use_value)))
2717 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
2718 attributeset, use_value);
2726 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
2728 zh->errString = basenames[base_no];
2729 zh->errCode = 109; /* Database unavailable */
2733 for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
2734 local_attr = local_attr->next)
2738 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
2741 ords[ord_no++] = ord;
2750 /* prepare dictionary scanning */
2753 scan_info_array = (struct scan_info *)
2754 odr_malloc (stream, ord_no * sizeof(*scan_info_array));
2755 for (i = 0; i < ord_no; i++)
2757 int j, prefix_len = 0;
2758 int before_tmp = before, after_tmp = after;
2759 struct scan_info *scan_info = scan_info_array + i;
2760 struct rpn_char_map_info rcmi;
2762 rpn_char_map_prepare (zh->reg, reg_id, &rcmi);
2764 scan_info->before = before;
2765 scan_info->after = after;
2766 scan_info->odr = stream;
2768 scan_info->list = (struct scan_info_entry *)
2769 odr_malloc (stream, (before+after) * sizeof(*scan_info->list));
2770 for (j = 0; j<before+after; j++)
2771 scan_info->list[j].term = NULL;
2773 prefix_len += key_SU_encode (ords[i], termz + prefix_len);
2774 termz[prefix_len++] = reg_id;
2775 termz[prefix_len] = 0;
2776 strcpy (scan_info->prefix, termz);
2778 trans_scan_term (zh, zapt, termz+prefix_len, reg_id);
2780 dict_scan (zh->reg->dict, termz, &before_tmp, &after_tmp,
2781 scan_info, scan_handle);
2783 glist = (ZebraScanEntry *)
2784 odr_malloc (stream, (before+after)*sizeof(*glist));
2786 /* consider terms after main term */
2787 for (i = 0; i < ord_no; i++)
2791 for (i = 0; i<after; i++)
2794 const char *mterm = NULL;
2798 for (j = 0; j < ord_no; j++)
2800 if (ptr[j] < before+after &&
2801 (tst=scan_info_array[j].list[ptr[j]].term) &&
2802 (!mterm || strcmp (tst, mterm) < 0))
2810 scan_term_untrans (zh, stream->mem, reg_id,
2811 &glist[i+before].term, mterm);
2812 rset = rset_trunc (zh, &scan_info_array[j0].list[ptr[j0]].isam_p, 1,
2813 glist[i+before].term, strlen(glist[i+before].term),
2814 NULL, 0, zapt->term->which);
2817 for (j = j0+1; j<ord_no; j++)
2819 if (ptr[j] < before+after &&
2820 (tst=scan_info_array[j].list[ptr[j]].term) &&
2821 !strcmp (tst, mterm))
2823 rset_bool_parms bool_parms;
2827 rset_trunc (zh, &scan_info_array[j].list[ptr[j]].isam_p, 1,
2828 glist[i+before].term,
2829 strlen(glist[i+before].term), NULL, 0,
2832 bool_parms.key_size = sizeof(struct it_key);
2833 bool_parms.cmp = key_compare_it;
2834 bool_parms.rset_l = rset;
2835 bool_parms.rset_r = rset2;
2837 rset = rset_create (rset_kind_or, &bool_parms);
2842 count_set (rset, &glist[i+before].occurrences);
2847 *num_entries -= (after-i);
2851 /* consider terms before main term */
2852 for (i = 0; i<ord_no; i++)
2855 for (i = 0; i<before; i++)
2858 const char *mterm = NULL;
2862 for (j = 0; j <ord_no; j++)
2864 if (ptr[j] < before &&
2865 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2866 (!mterm || strcmp (tst, mterm) > 0))
2875 scan_term_untrans (zh, stream->mem, reg_id,
2876 &glist[before-1-i].term, mterm);
2879 (zh, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1,
2880 glist[before-1-i].term, strlen(glist[before-1-i].term),
2881 NULL, 0, zapt->term->which);
2885 for (j = j0+1; j<ord_no; j++)
2887 if (ptr[j] < before &&
2888 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2889 !strcmp (tst, mterm))
2891 rset_bool_parms bool_parms;
2894 rset2 = rset_trunc (zh,
2895 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1,
2896 glist[before-1-i].term,
2897 strlen(glist[before-1-i].term), NULL, 0,
2900 bool_parms.key_size = sizeof(struct it_key);
2901 bool_parms.cmp = key_compare_it;
2902 bool_parms.rset_l = rset;
2903 bool_parms.rset_r = rset2;
2905 rset = rset_create (rset_kind_or, &bool_parms);
2910 count_set (rset, &glist[before-1-i].occurrences);
2920 *list = glist + i; /* list is set to first 'real' entry */
2922 logf (LOG_DEBUG, "position = %d, num_entries = %d",
2923 *position, *num_entries);
2925 logf (LOG_DEBUG, "scan error: %d", zh->errCode);