1 /* $Id: zrpn.c,v 1.127 2003-02-04 12:06:47 pop Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
34 #include <zebra_xpath.h>
40 #include <rsbetween.h>
42 struct rpn_char_map_info {
51 Z_AttributesPlusTerm *zapt;
55 static const char **rpn_char_map_handler (void *vp, const char **from, int len)
57 struct rpn_char_map_info *p = (struct rpn_char_map_info *) vp;
58 const char **out = zebra_maps_input (p->zm, p->reg_type, from, len);
62 const char *outp = *out;
63 yaz_log (LOG_LOG, "---");
66 yaz_log (LOG_LOG, "%02X", *outp);
74 static void rpn_char_map_prepare (struct zebra_register *reg, int reg_type,
75 struct rpn_char_map_info *map_info)
77 map_info->zm = reg->zebra_maps;
78 map_info->reg_type = reg_type;
79 dict_grep_cmap (reg->dict, map_info, rpn_char_map_handler);
82 static int attr_find_ex (AttrType *src, oid_value *attributeSetP,
83 const char **string_value)
87 num_attributes = src->zapt->attributes->num_attributes;
88 while (src->major < num_attributes)
90 Z_AttributeElement *element;
92 element = src->zapt->attributes->attributes[src->major];
93 if (src->type == *element->attributeType)
95 switch (element->which)
97 case Z_AttributeValue_numeric:
99 if (element->attributeSet && attributeSetP)
103 attrset = oid_getentbyoid (element->attributeSet);
104 *attributeSetP = attrset->value;
106 return *element->value.numeric;
108 case Z_AttributeValue_complex:
109 if (src->minor >= element->value.complex->num_list)
111 if (element->attributeSet && attributeSetP)
115 attrset = oid_getentbyoid (element->attributeSet);
116 *attributeSetP = attrset->value;
118 if (element->value.complex->list[src->minor]->which ==
119 Z_StringOrNumeric_numeric)
123 *element->value.complex->list[src->minor-1]->u.numeric;
125 else if (element->value.complex->list[src->minor]->which ==
126 Z_StringOrNumeric_string)
132 element->value.complex->list[src->minor-1]->u.string;
146 static int attr_find (AttrType *src, oid_value *attributeSetP)
148 return attr_find_ex (src, attributeSetP, 0);
151 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
174 static void term_untrans (ZebraHandle zh, int reg_type,
175 char *dst, const char *src)
180 const char *cp = zebra_maps_output (zh->reg->zebra_maps,
182 if (!cp && len < IT_MAX_WORD-1)
185 while (*cp && len < IT_MAX_WORD-1)
191 static void add_isam_p (const char *name, const char *info,
194 if (p->isam_p_indx == p->isam_p_size)
196 ISAMS_P *new_isam_p_buf;
200 p->isam_p_size = 2*p->isam_p_size + 100;
201 new_isam_p_buf = (ISAMS_P *) xmalloc (sizeof(*new_isam_p_buf) *
205 memcpy (new_isam_p_buf, p->isam_p_buf,
206 p->isam_p_indx * sizeof(*p->isam_p_buf));
207 xfree (p->isam_p_buf);
209 p->isam_p_buf = new_isam_p_buf;
212 new_term_no = (int *) xmalloc (sizeof(*new_term_no) *
216 memcpy (new_term_no, p->isam_p_buf,
217 p->isam_p_indx * sizeof(*p->term_no));
220 p->term_no = new_term_no;
223 assert (*info == sizeof(*p->isam_p_buf));
224 memcpy (p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
231 char term_tmp[IT_MAX_WORD];
233 int len = key_SU_decode (&su_code, name);
235 term_untrans (p->zh, p->reg_type, term_tmp, name+len+1);
236 logf (LOG_LOG, "grep: %d %c %s", su_code, name[len], term_tmp);
237 zebraExplain_lookup_ord (p->zh->reg->zei,
238 su_code, &db, &set, &use);
239 logf (LOG_LOG, "grep: set=%d use=%d db=%s", set, use, db);
241 resultSetAddTerm (p->zh, p->termset, name[len], db,
248 static int grep_handle (char *name, const char *info, void *p)
250 add_isam_p (name, info, (struct grep_info *) p);
254 static int term_pre (ZebraMaps zebra_maps, int reg_type, const char **src,
255 const char *ct1, const char *ct2)
257 const char *s1, *s0 = *src;
260 /* skip white space */
263 if (ct1 && strchr (ct1, *s0))
265 if (ct2 && strchr (ct2, *s0))
268 map = zebra_maps_input (zebra_maps, reg_type, &s1, strlen(s1));
269 if (**map != *CHR_SPACE)
277 #define REGEX_CHARS " []()|.*+?!"
279 /* term_100: handle term, where trunc=none (no operators at all) */
280 static int term_100 (ZebraMaps zebra_maps, int reg_type,
281 const char **src, char *dst, int space_split,
289 const char *space_start = 0;
290 const char *space_end = 0;
292 if (!term_pre (zebra_maps, reg_type, src, NULL, NULL))
298 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
301 if (**map == *CHR_SPACE)
304 else /* complete subfield only. */
306 if (**map == *CHR_SPACE)
307 { /* save space mapping for later .. */
312 else if (space_start)
313 { /* reload last space */
314 while (space_start < space_end)
316 if (strchr (REGEX_CHARS, *space_start))
318 dst_term[j++] = *space_start;
319 dst[i++] = *space_start++;
322 space_start = space_end = 0;
325 /* add non-space char */
328 if (strchr(REGEX_CHARS, *s1))
340 /* term_101: handle term, where trunc=Process # */
341 static int term_101 (ZebraMaps zebra_maps, int reg_type,
342 const char **src, char *dst, int space_split,
350 if (!term_pre (zebra_maps, reg_type, src, "#", "#"))
359 dst_term[j++] = *s0++;
364 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
365 if (space_split && **map == *CHR_SPACE)
369 if (strchr(REGEX_CHARS, *s1))
377 dst_term[j++] = '\0';
382 /* term_103: handle term, where trunc=re-2 (regular expressions) */
383 static int term_103 (ZebraMaps zebra_maps, int reg_type, const char **src,
384 char *dst, int *errors, int space_split,
392 if (!term_pre (zebra_maps, reg_type, src, "^\\()[].*+?|", "("))
395 if (errors && *s0 == '+' && s0[1] && s0[2] == '+' && s0[3] &&
398 *errors = s0[1] - '0';
405 if (strchr ("^\\()[].*+?|-", *s0))
413 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
414 if (**map == *CHR_SPACE)
418 if (strchr(REGEX_CHARS, *s1))
431 /* term_103: handle term, where trunc=re-1 (regular expressions) */
432 static int term_102 (ZebraMaps zebra_maps, int reg_type, const char **src,
433 char *dst, int space_split, char *dst_term)
435 return term_103 (zebra_maps, reg_type, src, dst, NULL, space_split,
440 /* term_104: handle term, where trunc=Process # and ! */
441 static int term_104 (ZebraMaps zebra_maps, int reg_type,
442 const char **src, char *dst, int space_split,
450 if (!term_pre (zebra_maps, reg_type, src, "#!", "#!"))
459 dst_term[j++] = *s0++;
464 dst_term[j++] = *s0++;
468 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
469 if (space_split && **map == *CHR_SPACE)
473 if (strchr(REGEX_CHARS, *s1))
481 dst_term[j++] = '\0';
486 /* term_105/106: handle term, where trunc=Process * and ! and right trunc */
487 static int term_105 (ZebraMaps zebra_maps, int reg_type,
488 const char **src, char *dst, int space_split,
489 char *dst_term, int right_truncate)
496 if (!term_pre (zebra_maps, reg_type, src, "*!", "*!"))
505 dst_term[j++] = *s0++;
510 dst_term[j++] = *s0++;
514 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
515 if (space_split && **map == *CHR_SPACE)
519 if (strchr(REGEX_CHARS, *s1))
533 dst_term[j++] = '\0';
539 /* gen_regular_rel - generate regular expression from relation
540 * val: border value (inclusive)
541 * islt: 1 if <=; 0 if >=.
543 static void gen_regular_rel (char *dst, int val, int islt)
550 logf (LOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
554 strcpy (dst, "(-[0-9]+|(");
562 strcpy (dst, "([0-9]+|-(");
563 dst_p = strlen (dst);
573 dst_p = strlen (dst);
574 sprintf (numstr, "%d", val);
575 for (w = strlen(numstr); --w >= 0; pos++)
594 strcpy (dst + dst_p, numstr);
595 dst_p = strlen(dst) - pos - 1;
623 for (i = 0; i<pos; i++)
636 /* match everything less than 10^(pos-1) */
638 for (i=1; i<pos; i++)
639 strcat (dst, "[0-9]?");
643 /* match everything greater than 10^pos */
644 for (i = 0; i <= pos; i++)
645 strcat (dst, "[0-9]");
646 strcat (dst, "[0-9]*");
651 void string_rel_add_char (char **term_p, const char *src, int *indx)
653 if (src[*indx] == '\\')
654 *(*term_p)++ = src[(*indx)++];
655 *(*term_p)++ = src[(*indx)++];
659 * > abc ([b-].*|a[c-].*|ab[d-].*|abc.+)
660 * ([^-a].*|a[^-b].*ab[^-c].*|abc.+)
661 * >= abc ([b-].*|a[c-].*|ab[c-].*)
662 * ([^-a].*|a[^-b].*|ab[c-].*)
663 * < abc ([-0].*|a[-a].*|ab[-b].*)
664 * ([^a-].*|a[^b-].*|ab[^c-].*)
665 * <= abc ([-0].*|a[-a].*|ab[-b].*|abc)
666 * ([^a-].*|a[^b-].*|ab[^c-].*|abc)
668 static int string_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
669 const char **term_sub, char *term_dict,
670 oid_value attributeSet,
671 int reg_type, int space_split, char *term_dst)
676 char *term_tmp = term_dict + strlen(term_dict);
677 char term_component[256];
679 attr_init (&relation, zapt, 2);
680 relation_value = attr_find (&relation, NULL);
682 logf (LOG_DEBUG, "string relation value=%d", relation_value);
683 switch (relation_value)
686 if (!term_100 (zh->reg->zebra_maps, reg_type,
687 term_sub, term_component,
688 space_split, term_dst))
690 logf (LOG_DEBUG, "Relation <");
693 for (i = 0; term_component[i]; )
700 string_rel_add_char (&term_tmp, term_component, &j);
705 string_rel_add_char (&term_tmp, term_component, &i);
716 if (!term_100 (zh->reg->zebra_maps, reg_type,
717 term_sub, term_component,
718 space_split, term_dst))
720 logf (LOG_DEBUG, "Relation <=");
723 for (i = 0; term_component[i]; )
728 string_rel_add_char (&term_tmp, term_component, &j);
732 string_rel_add_char (&term_tmp, term_component, &i);
741 for (i = 0; term_component[i]; )
742 string_rel_add_char (&term_tmp, term_component, &i);
747 if (!term_100 (zh->reg->zebra_maps, reg_type,
748 term_sub, term_component, space_split, term_dst))
750 logf (LOG_DEBUG, "Relation >");
753 for (i = 0; term_component[i];)
758 string_rel_add_char (&term_tmp, term_component, &j);
763 string_rel_add_char (&term_tmp, term_component, &i);
771 for (i = 0; term_component[i];)
772 string_rel_add_char (&term_tmp, term_component, &i);
779 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub,
780 term_component, space_split, term_dst))
782 logf (LOG_DEBUG, "Relation >=");
785 for (i = 0; term_component[i];)
792 string_rel_add_char (&term_tmp, term_component, &j);
795 if (term_component[i+1])
799 string_rel_add_char (&term_tmp, term_component, &i);
803 string_rel_add_char (&term_tmp, term_component, &i);
815 logf (LOG_DEBUG, "Relation =");
816 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub,
817 term_component, space_split, term_dst))
819 strcat (term_tmp, "(");
820 strcat (term_tmp, term_component);
821 strcat (term_tmp, ")");
826 static int string_term (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,
832 char *term_dst, int xpath_use);
834 static RSET term_trunc (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
835 const char **term_sub,
836 oid_value attributeSet, NMEM stream,
837 struct grep_info *grep_info,
838 int reg_type, int complete_flag,
839 int num_bases, char **basenames,
841 const char *rank_type, int xpath_use)
844 grep_info->isam_p_indx = 0;
845 r = string_term (zh, zapt, term_sub, attributeSet, stream, grep_info,
846 reg_type, complete_flag, num_bases, basenames,
847 term_dst, xpath_use);
850 logf (LOG_DEBUG, "term: %s", term_dst);
851 return rset_trunc (zh, grep_info->isam_p_buf,
852 grep_info->isam_p_indx, term_dst,
853 strlen(term_dst), rank_type, 1 /* preserve pos */,
858 static int string_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
859 const char **term_sub,
860 oid_value attributeSet, NMEM stream,
861 struct grep_info *grep_info,
862 int reg_type, int complete_flag,
863 int num_bases, char **basenames,
864 char *term_dst, int xpath_use)
866 char term_dict[2*IT_MAX_WORD+4000];
869 int truncation_value;
872 const char *use_string = 0;
873 oid_value curAttributeSet = attributeSet;
875 struct rpn_char_map_info rcmi;
876 int space_split = complete_flag ? 0 : 1;
878 rpn_char_map_prepare (zh->reg, reg_type, &rcmi);
879 attr_init (&use, zapt, 1);
880 use_value = attr_find_ex (&use, &curAttributeSet, &use_string);
881 logf (LOG_DEBUG, "string_term, use value %d", use_value);
882 attr_init (&truncation, zapt, 5);
883 truncation_value = attr_find (&truncation, NULL);
884 logf (LOG_DEBUG, "truncation value %d", truncation_value);
886 if (use_value == -1) /* no attribute - assumy "any" */
888 for (base_no = 0; base_no < num_bases; base_no++)
891 data1_local_attribute id_xpath_attr;
892 data1_local_attribute *local_attr;
893 int max_pos, prefix_len = 0;
897 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
899 zh->errCode = 109; /* Database unavailable */
900 zh->errString = basenames[base_no];
903 if (use_value == -2) /* string attribute (assume IDXPATH/any) */
905 use_value = xpath_use;
906 attp.local_attributes = &id_xpath_attr;
907 attp.attset_ordinal = VAL_IDXPATH;
908 id_xpath_attr.next = 0;
909 id_xpath_attr.local = use_value;
911 else if (curAttributeSet == VAL_IDXPATH)
913 attp.local_attributes = &id_xpath_attr;
914 attp.attset_ordinal = VAL_IDXPATH;
915 id_xpath_attr.next = 0;
916 id_xpath_attr.local = use_value;
920 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
922 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
923 curAttributeSet, use_value, r);
926 /* set was found, but value wasn't defined */
928 sprintf (val_str, "%d (1)", use_value);
930 zh->errString = nmem_strdup (stream, val_str);
935 struct oident oident;
937 oident.proto = PROTO_Z3950;
938 oident.oclass = CLASS_ATTSET;
939 oident.value = curAttributeSet;
940 oid_ent_to_oid (&oident, oid);
943 zh->errString = nmem_strdup (stream, oident.desc);
948 for (local_attr = attp.local_attributes; local_attr;
949 local_attr = local_attr->next)
955 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
960 term_dict[prefix_len++] = '|';
962 term_dict[prefix_len++] = '(';
964 ord_len = key_SU_encode (ord, ord_buf);
965 for (i = 0; i<ord_len; i++)
967 term_dict[prefix_len++] = 1;
968 term_dict[prefix_len++] = ord_buf[i];
974 sprintf (val_str, "%d (2)", use_value);
976 zh->errString = nmem_strdup (stream, val_str);
979 term_dict[prefix_len++] = ')';
980 term_dict[prefix_len++] = 1;
981 term_dict[prefix_len++] = reg_type;
982 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
983 term_dict[prefix_len] = '\0';
985 switch (truncation_value)
987 case -1: /* not specified */
988 case 100: /* do not truncate */
989 if (!string_relation (zh, zapt, &termp, term_dict,
991 reg_type, space_split, term_dst))
993 logf (LOG_LOG, "dict_lookup_grep: %s", term_dict+prefix_len);
994 r = dict_lookup_grep (zh->reg->dict, term_dict, 0,
995 grep_info, &max_pos, 0, grep_handle);
997 logf (LOG_WARN, "dict_lookup_grep fail %d", r);
999 case 1: /* right truncation */
1000 term_dict[j++] = '(';
1001 if (!term_100 (zh->reg->zebra_maps, reg_type,
1002 &termp, term_dict + j, space_split, term_dst))
1004 strcat (term_dict, ".*)");
1005 dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1006 &max_pos, 0, grep_handle);
1008 case 2: /* keft truncation */
1009 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1010 if (!term_100 (zh->reg->zebra_maps, reg_type,
1011 &termp, term_dict + j, space_split, term_dst))
1013 strcat (term_dict, ")");
1014 dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1015 &max_pos, 0, grep_handle);
1017 case 3: /* left&right truncation */
1018 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1019 if (!term_100 (zh->reg->zebra_maps, reg_type,
1020 &termp, term_dict + j, space_split, term_dst))
1022 strcat (term_dict, ".*)");
1023 dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1024 &max_pos, 0, grep_handle);
1028 case 101: /* process # in term */
1029 term_dict[j++] = '(';
1030 if (!term_101 (zh->reg->zebra_maps, reg_type,
1031 &termp, term_dict + j, space_split, term_dst))
1033 strcat (term_dict, ")");
1034 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1035 &max_pos, 0, grep_handle);
1037 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d", r);
1039 case 102: /* Regexp-1 */
1040 term_dict[j++] = '(';
1041 if (!term_102 (zh->reg->zebra_maps, reg_type,
1042 &termp, term_dict + j, space_split, term_dst))
1044 strcat (term_dict, ")");
1045 logf (LOG_DEBUG, "Regexp-1 tolerance=%d", r);
1046 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1047 &max_pos, 0, grep_handle);
1049 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
1052 case 103: /* Regexp-2 */
1054 term_dict[j++] = '(';
1055 if (!term_103 (zh->reg->zebra_maps, reg_type,
1056 &termp, term_dict + j, &r, space_split, term_dst))
1058 strcat (term_dict, ")");
1059 logf (LOG_DEBUG, "Regexp-2 tolerance=%d", r);
1060 r = dict_lookup_grep (zh->reg->dict, term_dict, r, grep_info,
1061 &max_pos, 2, grep_handle);
1063 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
1066 case 104: /* process # and ! in term */
1067 term_dict[j++] = '(';
1068 if (!term_104 (zh->reg->zebra_maps, reg_type,
1069 &termp, term_dict + j, space_split, term_dst))
1071 strcat (term_dict, ")");
1072 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1073 &max_pos, 0, grep_handle);
1075 logf (LOG_WARN, "dict_lookup_grep err, trunc=#/!: %d", r);
1077 case 105: /* process * and ! in term */
1078 term_dict[j++] = '(';
1079 if (!term_105 (zh->reg->zebra_maps, reg_type,
1080 &termp, term_dict + j, space_split, term_dst, 1))
1082 strcat (term_dict, ")");
1083 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1084 &max_pos, 0, grep_handle);
1086 logf (LOG_WARN, "dict_lookup_grep err, trunc=*/!: %d", r);
1088 case 106: /* process * and ! in term */
1089 term_dict[j++] = '(';
1090 if (!term_105 (zh->reg->zebra_maps, reg_type,
1091 &termp, term_dict + j, space_split, term_dst, 0))
1093 strcat (term_dict, ")");
1094 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1095 &max_pos, 0, grep_handle);
1097 logf (LOG_WARN, "dict_lookup_grep err, trunc=*/!: %d", r);
1102 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1107 /* convert APT search term to UTF8 */
1108 static int zapt_term_to_utf8 (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1112 Z_Term *term = zapt->term;
1114 switch (term->which)
1116 case Z_Term_general:
1117 if (zh->iconv_to_utf8 != 0)
1119 char *inbuf = term->u.general->buf;
1120 size_t inleft = term->u.general->len;
1121 char *outbuf = termz;
1122 size_t outleft = IT_MAX_WORD-1;
1125 ret = yaz_iconv(zh->iconv_to_utf8, &inbuf, &inleft,
1127 if (ret == (size_t)(-1))
1129 ret = yaz_iconv(zh->iconv_to_utf8, 0, 0, 0, 0);
1137 sizez = term->u.general->len;
1138 if (sizez > IT_MAX_WORD-1)
1139 sizez = IT_MAX_WORD-1;
1140 memcpy (termz, term->u.general->buf, sizez);
1141 termz[sizez] = '\0';
1144 case Z_Term_characterString:
1145 sizez = strlen(term->u.characterString);
1146 if (sizez > IT_MAX_WORD-1)
1147 sizez = IT_MAX_WORD-1;
1148 memcpy (termz, term->u.characterString, sizez);
1149 termz[sizez] = '\0';
1158 /* convert APT SCAN term to internal cmap */
1159 static int trans_scan_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1160 char *termz, int reg_type)
1162 char termz0[IT_MAX_WORD];
1164 if (zapt_term_to_utf8(zh, zapt, termz0))
1165 return -1; /* error */
1169 const char *cp = (const char *) termz0;
1170 const char *cp_end = cp + strlen(cp);
1173 const char *space_map = NULL;
1176 while ((len = (cp_end - cp)) > 0)
1178 map = zebra_maps_input (zh->reg->zebra_maps, reg_type, &cp, len);
1179 if (**map == *CHR_SPACE)
1184 for (src = space_map; *src; src++)
1187 for (src = *map; *src; src++)
1196 static RSET rpn_prox (ZebraHandle zh, RSET *rset, int rset_no,
1197 int ordered, int exclusion, int relation, int distance)
1202 struct it_key **buf;
1204 char prox_term[1024];
1205 int length_prox_term = 0;
1206 int min_nn = 10000000;
1208 int term_type = Z_Term_characterString;
1209 const char *flags = NULL;
1211 rsfd = (RSFD *) xmalloc (sizeof(*rsfd)*rset_no);
1212 more = (int *) xmalloc (sizeof(*more)*rset_no);
1213 buf = (struct it_key **) xmalloc (sizeof(*buf)*rset_no);
1216 for (i = 0; i<rset_no; i++)
1219 for (j = 0; j<rset[i]->no_rset_terms; j++)
1221 const char *nflags = rset[i]->rset_terms[j]->flags;
1222 char *term = rset[i]->rset_terms[j]->name;
1223 int lterm = strlen(term);
1224 if (lterm + length_prox_term < sizeof(prox_term)-1)
1226 if (length_prox_term)
1227 prox_term[length_prox_term++] = ' ';
1228 strcpy (prox_term + length_prox_term, term);
1229 length_prox_term += lterm;
1231 if (min_nn > rset[i]->rset_terms[j]->nn)
1232 min_nn = rset[i]->rset_terms[j]->nn;
1234 term_type = rset[i]->rset_terms[j]->type;
1236 /* only if all term types are of type characterString .. */
1237 /* the resulting term is of that type */
1238 if (term_type != Z_Term_characterString)
1239 term_type = Z_Term_general;
1242 for (i = 0; i<rset_no; i++)
1247 for (i = 0; i<rset_no; i++)
1249 buf[i] = (struct it_key *) xmalloc (sizeof(**buf));
1250 rsfd[i] = rset_open (rset[i], RSETF_READ);
1251 if (!(more[i] = rset_read (rset[i], rsfd[i], buf[i], &term_index)))
1256 /* at least one is empty ... return null set */
1257 rset_null_parms parms;
1259 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1261 parms.rset_term->nn = 0;
1262 result = rset_create (rset_kind_null, &parms);
1264 else if (ordered && relation == 3 && exclusion == 0 && distance == 1)
1266 /* special proximity case = phrase search ... */
1267 rset_temp_parms parms;
1270 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1272 parms.rset_term->nn = min_nn;
1273 parms.cmp = key_compare_it;
1274 parms.key_size = sizeof (struct it_key);
1275 parms.temp_path = res_get (zh->res, "setTmpDir");
1276 result = rset_create (rset_kind_temp, &parms);
1277 rsfd_result = rset_open (result, RSETF_WRITE);
1281 for (i = 1; i<rset_no; i++)
1290 cmp = key_compare_it (buf[i], buf[i-1]);
1293 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1294 buf[i-1], &term_index);
1299 if (buf[i-1]->seqno+1 != buf[i]->seqno)
1301 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1302 buf[i-1], &term_index);
1308 more[i] = rset_read (rset[i], rsfd[i], buf[i],
1315 rset_write (result, rsfd_result, buf[0]);
1316 more[0] = rset_read (*rset, *rsfd, *buf, &term_index);
1319 rset_close (result, rsfd_result);
1321 else if (rset_no == 2)
1323 /* generic proximity case (two input sets only) ... */
1324 rset_temp_parms parms;
1327 logf (LOG_LOG, "generic prox, dist = %d, relation = %d, ordered =%d, exclusion=%d",
1328 distance, relation, ordered, exclusion);
1329 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1331 parms.rset_term->nn = min_nn;
1332 parms.cmp = key_compare_it;
1333 parms.key_size = sizeof (struct it_key);
1334 parms.temp_path = res_get (zh->res, "setTmpDir");
1335 result = rset_create (rset_kind_temp, &parms);
1336 rsfd_result = rset_open (result, RSETF_WRITE);
1338 while (more[0] && more[1])
1340 int cmp = key_compare_it (buf[0], buf[1]);
1342 more[0] = rset_read (rset[0], rsfd[0], buf[0], &term_index);
1344 more[1] = rset_read (rset[1], rsfd[1], buf[1], &term_index);
1347 int sysno = buf[0]->sysno;
1351 seqno[n++] = buf[0]->seqno;
1352 while ((more[0] = rset_read (rset[0], rsfd[0], buf[0],
1354 sysno == buf[0]->sysno)
1356 seqno[n++] = buf[0]->seqno;
1359 for (i = 0; i<n; i++)
1361 int diff = buf[1]->seqno - seqno[i];
1362 int excl = exclusion;
1363 if (!ordered && diff < 0)
1368 if (diff < distance && diff >= 0)
1372 if (diff <= distance && diff >= 0)
1376 if (diff == distance && diff >= 0)
1380 if (diff >= distance && diff >= 0)
1384 if (diff > distance && diff >= 0)
1388 if (diff != distance && diff >= 0)
1394 rset_write (result, rsfd_result, buf[1]);
1398 } while ((more[1] = rset_read (rset[1], rsfd[1], buf[1],
1400 sysno == buf[1]->sysno);
1403 rset_close (result, rsfd_result);
1407 rset_null_parms parms;
1409 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1411 parms.rset_term->nn = 0;
1412 result = rset_create (rset_kind_null, &parms);
1414 for (i = 0; i<rset_no; i++)
1417 rset_close (rset[i], rsfd[i]);
1427 char *normalize_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1428 const char *termz, NMEM stream, unsigned reg_id)
1431 AttrType truncation;
1432 int truncation_value;
1435 attr_init (&truncation, zapt, 5);
1436 truncation_value = attr_find (&truncation, NULL);
1438 switch (truncation_value)
1458 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, ex_list,
1459 termz, strlen(termz));
1461 return nmem_strdup(stream, termz);
1464 char *buf = (char*) nmem_malloc (stream, wrbuf_len(wrbuf)+1);
1465 memcpy (buf, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1466 buf[wrbuf_len(wrbuf)] = '\0';
1471 static void grep_info_delete (struct grep_info *grep_info)
1474 xfree(grep_info->term_no);
1476 xfree (grep_info->isam_p_buf);
1479 static int grep_info_prepare (ZebraHandle zh,
1480 Z_AttributesPlusTerm *zapt,
1481 struct grep_info *grep_info,
1486 int termset_value_numeric;
1487 const char *termset_value_string;
1490 grep_info->term_no = 0;
1492 grep_info->isam_p_size = 0;
1493 grep_info->isam_p_buf = NULL;
1495 grep_info->reg_type = reg_type;
1496 grep_info->termset = 0;
1500 attr_init (&termset, zapt, 8);
1501 termset_value_numeric =
1502 attr_find_ex (&termset, NULL, &termset_value_string);
1503 if (termset_value_numeric != -1)
1506 const char *termset_name = 0;
1507 if (termset_value_numeric != -2)
1510 sprintf (resname, "%d", termset_value_numeric);
1511 termset_name = resname;
1514 termset_name = termset_value_string;
1515 logf (LOG_LOG, "creating termset set %s", termset_name);
1516 grep_info->termset = resultSetAdd (zh, termset_name, 1);
1517 if (!grep_info->termset)
1520 zh->errString = nmem_strdup (stream, termset_name);
1528 static RSET rpn_search_APT_phrase (ZebraHandle zh,
1529 Z_AttributesPlusTerm *zapt,
1530 const char *termz_org,
1531 oid_value attributeSet,
1533 int reg_type, int complete_flag,
1534 const char *rank_type, int xpath_use,
1535 int num_bases, char **basenames)
1537 char term_dst[IT_MAX_WORD+1];
1538 RSET rset[60], result;
1540 struct grep_info grep_info;
1541 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1542 const char *termp = termz;
1545 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1549 logf (LOG_DEBUG, "APT_phrase termp=%s", termp);
1550 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1552 reg_type, complete_flag,
1553 num_bases, basenames,
1554 term_dst, rank_type,
1558 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1561 grep_info_delete (&grep_info);
1564 rset_null_parms parms;
1566 parms.rset_term = rset_term_create (termz, -1, rank_type,
1568 return rset_create (rset_kind_null, &parms);
1570 else if (rset_no == 1)
1572 result = rpn_prox (zh, rset, rset_no, 1, 0, 3, 1);
1573 for (i = 0; i<rset_no; i++)
1574 rset_delete (rset[i]);
1578 static RSET rpn_search_APT_or_list (ZebraHandle zh,
1579 Z_AttributesPlusTerm *zapt,
1580 const char *termz_org,
1581 oid_value attributeSet,
1583 int reg_type, int complete_flag,
1584 const char *rank_type,
1586 int num_bases, char **basenames)
1588 char term_dst[IT_MAX_WORD+1];
1589 RSET rset[60], result;
1591 struct grep_info grep_info;
1592 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1593 const char *termp = termz;
1595 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1599 logf (LOG_DEBUG, "APT_or_list termp=%s", termp);
1600 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1602 reg_type, complete_flag,
1603 num_bases, basenames,
1604 term_dst, rank_type,
1608 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1611 grep_info_delete (&grep_info);
1614 rset_null_parms parms;
1616 parms.rset_term = rset_term_create (termz, -1, rank_type,
1618 return rset_create (rset_kind_null, &parms);
1621 for (i = 1; i<rset_no; i++)
1623 rset_bool_parms bool_parms;
1625 bool_parms.rset_l = result;
1626 bool_parms.rset_r = rset[i];
1627 bool_parms.key_size = sizeof(struct it_key);
1628 bool_parms.cmp = key_compare_it;
1629 result = rset_create (rset_kind_or, &bool_parms);
1634 static RSET rpn_search_APT_and_list (ZebraHandle zh,
1635 Z_AttributesPlusTerm *zapt,
1636 const char *termz_org,
1637 oid_value attributeSet,
1639 int reg_type, int complete_flag,
1640 const char *rank_type,
1642 int num_bases, char **basenames)
1644 char term_dst[IT_MAX_WORD+1];
1645 RSET rset[60], result;
1647 struct grep_info grep_info;
1648 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1649 const char *termp = termz;
1651 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1655 logf (LOG_DEBUG, "APT_and_list termp=%s", termp);
1656 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1658 reg_type, complete_flag,
1659 num_bases, basenames,
1660 term_dst, rank_type,
1664 assert (rset[rset_no]);
1665 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1668 grep_info_delete (&grep_info);
1671 rset_null_parms parms;
1673 parms.rset_term = rset_term_create (termz, -1, rank_type,
1675 return rset_create (rset_kind_null, &parms);
1678 for (i = 1; i<rset_no; i++)
1680 rset_bool_parms bool_parms;
1682 bool_parms.rset_l = result;
1683 bool_parms.rset_r = rset[i];
1684 bool_parms.key_size = sizeof(struct it_key);
1685 bool_parms.cmp = key_compare_it;
1686 result = rset_create (rset_kind_and, &bool_parms);
1691 static int numeric_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1692 const char **term_sub,
1694 oid_value attributeSet,
1695 struct grep_info *grep_info,
1704 char *term_tmp = term_dict + strlen(term_dict);
1706 attr_init (&relation, zapt, 2);
1707 relation_value = attr_find (&relation, NULL);
1709 logf (LOG_DEBUG, "numeric relation value=%d", relation_value);
1711 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub, term_tmp, 1,
1714 term_value = atoi (term_tmp);
1715 switch (relation_value)
1718 logf (LOG_DEBUG, "Relation <");
1719 gen_regular_rel (term_tmp, term_value-1, 1);
1722 logf (LOG_DEBUG, "Relation <=");
1723 gen_regular_rel (term_tmp, term_value, 1);
1726 logf (LOG_DEBUG, "Relation >=");
1727 gen_regular_rel (term_tmp, term_value, 0);
1730 logf (LOG_DEBUG, "Relation >");
1731 gen_regular_rel (term_tmp, term_value+1, 0);
1735 logf (LOG_DEBUG, "Relation =");
1736 sprintf (term_tmp, "(0*%d)", term_value);
1738 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_tmp);
1739 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info, max_pos,
1742 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1743 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1747 static int numeric_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1748 const char **term_sub,
1749 oid_value attributeSet, struct grep_info *grep_info,
1750 int reg_type, int complete_flag,
1751 int num_bases, char **basenames,
1752 char *term_dst, int xpath_use)
1754 char term_dict[2*IT_MAX_WORD+2];
1758 const char *use_string = 0;
1759 oid_value curAttributeSet = attributeSet;
1761 struct rpn_char_map_info rcmi;
1763 rpn_char_map_prepare (zh->reg, reg_type, &rcmi);
1764 attr_init (&use, zapt, 1);
1765 use_value = attr_find_ex (&use, &curAttributeSet, &use_string);
1767 if (use_value == -1)
1770 for (base_no = 0; base_no < num_bases; base_no++)
1773 data1_local_attribute id_xpath_attr;
1774 data1_local_attribute *local_attr;
1775 int max_pos, prefix_len = 0;
1778 if (use_value == -2) /* string attribute (assume IDXPATH/any) */
1780 use_value = xpath_use;
1781 attp.local_attributes = &id_xpath_attr;
1782 attp.attset_ordinal = VAL_IDXPATH;
1783 id_xpath_attr.next = 0;
1784 id_xpath_attr.local = use_value;
1786 else if (curAttributeSet == VAL_IDXPATH)
1788 attp.local_attributes = &id_xpath_attr;
1789 attp.attset_ordinal = VAL_IDXPATH;
1790 id_xpath_attr.next = 0;
1791 id_xpath_attr.local = use_value;
1795 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
1797 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1798 curAttributeSet, use_value, r);
1806 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
1808 zh->errCode = 109; /* Database unavailable */
1809 zh->errString = basenames[base_no];
1812 for (local_attr = attp.local_attributes; local_attr;
1813 local_attr = local_attr->next)
1819 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
1824 term_dict[prefix_len++] = '|';
1826 term_dict[prefix_len++] = '(';
1828 ord_len = key_SU_encode (ord, ord_buf);
1829 for (i = 0; i<ord_len; i++)
1831 term_dict[prefix_len++] = 1;
1832 term_dict[prefix_len++] = ord_buf[i];
1840 term_dict[prefix_len++] = ')';
1841 term_dict[prefix_len++] = 1;
1842 term_dict[prefix_len++] = reg_type;
1843 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1844 term_dict[prefix_len] = '\0';
1845 if (!numeric_relation (zh, zapt, &termp, term_dict,
1846 attributeSet, grep_info, &max_pos, reg_type,
1851 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1855 static RSET rpn_search_APT_numeric (ZebraHandle zh,
1856 Z_AttributesPlusTerm *zapt,
1858 oid_value attributeSet,
1860 int reg_type, int complete_flag,
1861 const char *rank_type, int xpath_use,
1862 int num_bases, char **basenames)
1864 char term_dst[IT_MAX_WORD+1];
1865 const char *termp = termz;
1866 RSET rset[60], result;
1867 int i, r, rset_no = 0;
1868 struct grep_info grep_info;
1870 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1874 logf (LOG_DEBUG, "APT_numeric termp=%s", termp);
1875 grep_info.isam_p_indx = 0;
1876 r = numeric_term (zh, zapt, &termp, attributeSet, &grep_info,
1877 reg_type, complete_flag, num_bases, basenames,
1878 term_dst, xpath_use);
1881 logf (LOG_DEBUG, "term: %s", term_dst);
1882 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1883 grep_info.isam_p_indx, term_dst,
1884 strlen(term_dst), rank_type,
1885 0 /* preserve position */,
1887 assert (rset[rset_no]);
1888 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1891 grep_info_delete (&grep_info);
1894 rset_null_parms parms;
1896 parms.rset_term = rset_term_create (term_dst, -1, rank_type,
1898 return rset_create (rset_kind_null, &parms);
1901 for (i = 1; i<rset_no; i++)
1903 rset_bool_parms bool_parms;
1905 bool_parms.rset_l = result;
1906 bool_parms.rset_r = rset[i];
1907 bool_parms.key_size = sizeof(struct it_key);
1908 bool_parms.cmp = key_compare_it;
1909 result = rset_create (rset_kind_and, &bool_parms);
1914 static RSET rpn_search_APT_local (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1916 oid_value attributeSet,
1918 const char *rank_type)
1923 rset_temp_parms parms;
1925 parms.rset_term = rset_term_create (termz, -1, rank_type,
1927 parms.cmp = key_compare_it;
1928 parms.key_size = sizeof (struct it_key);
1929 parms.temp_path = res_get (zh->res, "setTmpDir");
1930 result = rset_create (rset_kind_temp, &parms);
1931 rsfd = rset_open (result, RSETF_WRITE);
1933 key.sysno = atoi (termz);
1937 rset_write (result, rsfd, &key);
1938 rset_close (result, rsfd);
1942 static RSET rpn_sort_spec (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1943 oid_value attributeSet, NMEM stream,
1944 Z_SortKeySpecList *sort_sequence,
1945 const char *rank_type)
1947 rset_null_parms parms;
1949 int sort_relation_value;
1950 AttrType sort_relation_type;
1955 Z_AttributeElement *ae;
1960 attr_init (&sort_relation_type, zapt, 7);
1961 sort_relation_value = attr_find (&sort_relation_type, &attributeSet);
1963 attr_init (&use_type, zapt, 1);
1964 use_value = attr_find (&use_type, &attributeSet);
1966 if (!sort_sequence->specs)
1968 sort_sequence->num_specs = 10;
1969 sort_sequence->specs = (Z_SortKeySpec **)
1970 nmem_malloc (stream, sort_sequence->num_specs *
1971 sizeof(*sort_sequence->specs));
1972 for (i = 0; i<sort_sequence->num_specs; i++)
1973 sort_sequence->specs[i] = 0;
1975 if (zapt->term->which != Z_Term_general)
1978 i = atoi_n ((char *) zapt->term->u.general->buf,
1979 zapt->term->u.general->len);
1980 if (i >= sort_sequence->num_specs)
1982 sprintf (termz, "%d", i);
1984 oe.proto = PROTO_Z3950;
1985 oe.oclass = CLASS_ATTSET;
1986 oe.value = attributeSet;
1987 if (!oid_ent_to_oid (&oe, oid))
1990 sks = (Z_SortKeySpec *) nmem_malloc (stream, sizeof(*sks));
1991 sks->sortElement = (Z_SortElement *)
1992 nmem_malloc (stream, sizeof(*sks->sortElement));
1993 sks->sortElement->which = Z_SortElement_generic;
1994 sk = sks->sortElement->u.generic = (Z_SortKey *)
1995 nmem_malloc (stream, sizeof(*sk));
1996 sk->which = Z_SortKey_sortAttributes;
1997 sk->u.sortAttributes = (Z_SortAttributes *)
1998 nmem_malloc (stream, sizeof(*sk->u.sortAttributes));
2000 sk->u.sortAttributes->id = oid;
2001 sk->u.sortAttributes->list = (Z_AttributeList *)
2002 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list));
2003 sk->u.sortAttributes->list->num_attributes = 1;
2004 sk->u.sortAttributes->list->attributes = (Z_AttributeElement **)
2005 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list->attributes));
2006 ae = *sk->u.sortAttributes->list->attributes = (Z_AttributeElement *)
2007 nmem_malloc (stream, sizeof(**sk->u.sortAttributes->list->attributes));
2008 ae->attributeSet = 0;
2009 ae->attributeType = (int *)
2010 nmem_malloc (stream, sizeof(*ae->attributeType));
2011 *ae->attributeType = 1;
2012 ae->which = Z_AttributeValue_numeric;
2013 ae->value.numeric = (int *)
2014 nmem_malloc (stream, sizeof(*ae->value.numeric));
2015 *ae->value.numeric = use_value;
2017 sks->sortRelation = (int *)
2018 nmem_malloc (stream, sizeof(*sks->sortRelation));
2019 if (sort_relation_value == 1)
2020 *sks->sortRelation = Z_SortRelation_ascending;
2021 else if (sort_relation_value == 2)
2022 *sks->sortRelation = Z_SortRelation_descending;
2024 *sks->sortRelation = Z_SortRelation_ascending;
2026 sks->caseSensitivity = (int *)
2027 nmem_malloc (stream, sizeof(*sks->caseSensitivity));
2028 *sks->caseSensitivity = 0;
2030 sks->which = Z_SortKeySpec_null;
2031 sks->u.null = odr_nullval ();
2032 sort_sequence->specs[i] = sks;
2034 parms.rset_term = rset_term_create (termz, -1, rank_type,
2036 return rset_create (rset_kind_null, &parms);
2039 /* pop - moved to xpath.c */
2042 struct xpath_predicate {
2045 #define XPATH_PREDICATE_RELATION 1
2051 #define XPATH_PREDICATE_BOOLEAN 2
2054 struct xpath_predicate *left;
2055 struct xpath_predicate *right;
2060 struct xpath_location_step {
2062 struct xpath_predicate *predicate;
2067 static int parse_xpath(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2068 oid_value attributeSet,
2069 struct xpath_location_step *xpath, NMEM mem)
2071 oid_value curAttributeSet = attributeSet;
2073 const char *use_string = 0;
2076 attr_init (&use, zapt, 1);
2077 attr_find_ex (&use, &curAttributeSet, &use_string);
2079 if (!use_string || *use_string != '/')
2082 return (parse_xpath_str(use_string, xpath, mem));
2087 static RSET xpath_trunc(ZebraHandle zh, NMEM stream,
2088 int reg_type, const char *term, int use,
2089 oid_value curAttributeSet)
2092 struct grep_info grep_info;
2093 char term_dict[2048];
2096 int ord = zebraExplain_lookupSU (zh->reg->zei, curAttributeSet, use);
2097 int ord_len, i, r, max_pos;
2098 int term_type = Z_Term_characterString;
2099 const char *flags = "void";
2101 if (grep_info_prepare (zh, 0 /* zapt */, &grep_info, '0', stream))
2103 rset_null_parms parms;
2105 parms.rset_term = rset_term_create (term, strlen(term),
2107 parms.rset_term->nn = 0;
2108 return rset_create (rset_kind_null, &parms);
2113 rset_null_parms parms;
2115 parms.rset_term = rset_term_create (term, strlen(term),
2117 parms.rset_term->nn = 0;
2118 return rset_create (rset_kind_null, &parms);
2121 term_dict[prefix_len++] = '|';
2123 term_dict[prefix_len++] = '(';
2125 ord_len = key_SU_encode (ord, ord_buf);
2126 for (i = 0; i<ord_len; i++)
2128 term_dict[prefix_len++] = 1;
2129 term_dict[prefix_len++] = ord_buf[i];
2131 term_dict[prefix_len++] = ')';
2132 term_dict[prefix_len++] = 1;
2133 term_dict[prefix_len++] = reg_type;
2135 strcpy (term_dict+prefix_len, term);
2137 grep_info.isam_p_indx = 0;
2138 r = dict_lookup_grep (zh->reg->dict, term_dict, 0,
2139 &grep_info, &max_pos, 0, grep_handle);
2140 yaz_log (LOG_LOG, "%s %d positions", term,
2141 grep_info.isam_p_indx);
2142 rset = rset_trunc (zh, grep_info.isam_p_buf,
2143 grep_info.isam_p_indx, term, strlen(term),
2144 flags, 1, term_type);
2145 grep_info_delete (&grep_info);
2149 static RSET rpn_search_xpath (ZebraHandle zh,
2150 oid_value attributeSet,
2151 int num_bases, char **basenames,
2152 NMEM stream, const char *rank_type, RSET rset,
2153 int xpath_len, struct xpath_location_step *xpath)
2155 oid_value curAttributeSet = attributeSet;
2162 yaz_log (LOG_LOG, "len=%d", xpath_len);
2163 for (i = 0; i<xpath_len; i++)
2165 yaz_log (LOG_LOG, "XPATH %d %s", i, xpath[i].part);
2169 curAttributeSet = VAL_IDXPATH;
2179 a[@attr=value]/b[@other=othervalue]
2181 /e/@a val range(e/,range(@a,freetext(w,1015,val),@a),e/)
2182 /a/b val range(b/a/,freetext(w,1016,val),b/a/)
2183 /a/b/@c val range(b/a/,range(@c,freetext(w,1016,val),@c),b/a/)
2184 /a/b[@c=y] val range(b/a/,freetext(w,1016,val),b/a/,@c=y)
2185 /a[@c=y]/b val range(a/,range(b/a/,freetext(w,1016,val),b/a/),a/,@c=y)
2186 /a[@c=x]/b[@c=y] range(a/,range(b/a/,freetext(w,1016,val),b/a/,@c=y),a/,@c=x)
2190 dict_grep_cmap (zh->reg->dict, 0, 0);
2192 for (base_no = 0; base_no < num_bases; base_no++)
2194 int level = xpath_len;
2197 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
2199 zh->errCode = 109; /* Database unavailable */
2200 zh->errString = basenames[base_no];
2203 while (--level >= 0)
2205 char xpath_rev[128];
2207 rset_between_parms parms;
2208 RSET rset_start_tag = 0, rset_end_tag = 0, rset_attr = 0;
2212 for (i = level; i >= 1; --i)
2214 const char *cp = xpath[i].part;
2220 memcpy (xpath_rev + len, "[^/]*", 5);
2223 else if (*cp == ' ')
2226 xpath_rev[len++] = 1;
2227 xpath_rev[len++] = ' ';
2231 xpath_rev[len++] = *cp;
2232 xpath_rev[len++] = '/';
2234 else if (i == 1) /* // case */
2236 xpath_rev[len++] = '.';
2237 xpath_rev[len++] = '*';
2242 if (xpath[level].predicate &&
2243 xpath[level].predicate->which == XPATH_PREDICATE_RELATION &&
2244 xpath[level].predicate->u.relation.name[0])
2246 char predicate_str[128];
2248 strcpy (predicate_str,
2249 xpath[level].predicate->u.relation.name+1);
2250 if (xpath[level].predicate->u.relation.value)
2252 strcat (predicate_str, "=");
2253 strcat (predicate_str,
2254 xpath[level].predicate->u.relation.value);
2256 rset_attr = xpath_trunc (
2257 zh, stream, '0', predicate_str, 3, curAttributeSet);
2264 yaz_log (LOG_LOG, "xpath_rev (%d) = %s", level, xpath_rev);
2265 if (strlen(xpath_rev))
2267 rset_start_tag = xpath_trunc(zh, stream,
2268 '0', xpath_rev, 1, curAttributeSet);
2270 rset_end_tag = xpath_trunc(zh, stream,
2271 '0', xpath_rev, 2, curAttributeSet);
2273 parms.key_size = sizeof(struct it_key);
2274 parms.cmp = key_compare_it;
2275 parms.rset_l = rset_start_tag;
2276 parms.rset_m = rset;
2277 parms.rset_r = rset_end_tag;
2278 parms.rset_attr = rset_attr;
2279 parms.printer = key_print_it;
2280 rset = rset_create (rset_kind_between, &parms);
2291 static RSET rpn_search_APT (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2292 oid_value attributeSet, NMEM stream,
2293 Z_SortKeySpecList *sort_sequence,
2294 int num_bases, char **basenames)
2297 char *search_type = NULL;
2298 char rank_type[128];
2301 char termz[IT_MAX_WORD+1];
2305 struct xpath_location_step xpath[10];
2307 zebra_maps_attr (zh->reg->zebra_maps, zapt, ®_id, &search_type,
2308 rank_type, &complete_flag, &sort_flag);
2310 logf (LOG_DEBUG, "reg_id=%c", reg_id);
2311 logf (LOG_DEBUG, "complete_flag=%d", complete_flag);
2312 logf (LOG_DEBUG, "search_type=%s", search_type);
2313 logf (LOG_DEBUG, "rank_type=%s", rank_type);
2315 if (zapt_term_to_utf8(zh, zapt, termz))
2319 return rpn_sort_spec (zh, zapt, attributeSet, stream, sort_sequence,
2321 xpath_len = parse_xpath(zh, zapt, attributeSet, xpath, stream);
2325 if (xpath[xpath_len-1].part[0] == '@')
2329 if (!strcmp (search_type, "phrase"))
2331 rset = rpn_search_APT_phrase (zh, zapt, termz, attributeSet, stream,
2332 reg_id, complete_flag, rank_type,
2334 num_bases, basenames);
2336 else if (!strcmp (search_type, "and-list"))
2338 rset = rpn_search_APT_and_list (zh, zapt, termz, attributeSet, stream,
2339 reg_id, complete_flag, rank_type,
2341 num_bases, basenames);
2343 else if (!strcmp (search_type, "or-list"))
2345 rset = rpn_search_APT_or_list (zh, zapt, termz, attributeSet, stream,
2346 reg_id, complete_flag, rank_type,
2348 num_bases, basenames);
2350 else if (!strcmp (search_type, "local"))
2352 rset = rpn_search_APT_local (zh, zapt, termz, attributeSet, stream,
2355 else if (!strcmp (search_type, "numeric"))
2357 rset = rpn_search_APT_numeric (zh, zapt, termz, attributeSet, stream,
2358 reg_id, complete_flag, rank_type,
2360 num_bases, basenames);
2362 else if (!strcmp (search_type, "always"))
2368 return rpn_search_xpath (zh, attributeSet, num_bases, basenames,
2369 stream, rank_type, rset, xpath_len, xpath);
2372 static RSET rpn_search_structure (ZebraHandle zh, Z_RPNStructure *zs,
2373 oid_value attributeSet, NMEM stream,
2374 Z_SortKeySpecList *sort_sequence,
2375 int num_bases, char **basenames)
2378 if (zs->which == Z_RPNStructure_complex)
2380 Z_Operator *zop = zs->u.complex->roperator;
2381 rset_bool_parms bool_parms;
2383 bool_parms.rset_l = rpn_search_structure (zh, zs->u.complex->s1,
2384 attributeSet, stream,
2386 num_bases, basenames);
2387 if (bool_parms.rset_l == NULL)
2389 bool_parms.rset_r = rpn_search_structure (zh, zs->u.complex->s2,
2390 attributeSet, stream,
2392 num_bases, basenames);
2393 if (bool_parms.rset_r == NULL)
2395 rset_delete (bool_parms.rset_l);
2398 bool_parms.key_size = sizeof(struct it_key);
2399 bool_parms.cmp = key_compare_it;
2403 case Z_Operator_and:
2404 r = rset_create (rset_kind_and, &bool_parms);
2407 r = rset_create (rset_kind_or, &bool_parms);
2409 case Z_Operator_and_not:
2410 r = rset_create (rset_kind_not, &bool_parms);
2412 case Z_Operator_prox:
2413 if (zop->u.prox->which != Z_ProximityOperator_known)
2418 if (*zop->u.prox->u.known != Z_ProxUnit_word)
2420 char *val = (char *) nmem_malloc (stream, 16);
2422 zh->errString = val;
2423 sprintf (val, "%d", *zop->u.prox->u.known);
2430 rsets[0] = bool_parms.rset_l;
2431 rsets[1] = bool_parms.rset_r;
2433 r = rpn_prox (zh, rsets, 2,
2434 *zop->u.prox->ordered,
2435 (!zop->u.prox->exclusion ? 0 :
2436 *zop->u.prox->exclusion),
2437 *zop->u.prox->relationType,
2438 *zop->u.prox->distance);
2439 rset_delete (rsets[0]);
2440 rset_delete (rsets[1]);
2448 else if (zs->which == Z_RPNStructure_simple)
2450 if (zs->u.simple->which == Z_Operand_APT)
2452 logf (LOG_DEBUG, "rpn_search_APT");
2453 r = rpn_search_APT (zh, zs->u.simple->u.attributesPlusTerm,
2454 attributeSet, stream, sort_sequence,
2455 num_bases, basenames);
2457 else if (zs->u.simple->which == Z_Operand_resultSetId)
2459 logf (LOG_DEBUG, "rpn_search_ref");
2460 r = resultSetRef (zh, zs->u.simple->u.resultSetId);
2463 r = rset_create (rset_kind_null, NULL);
2466 nmem_strdup (stream, zs->u.simple->u.resultSetId);
2485 RSET rpn_search (ZebraHandle zh, NMEM nmem,
2486 Z_RPNQuery *rpn, int num_bases, char **basenames,
2487 const char *setname,
2492 oid_value attributeSet;
2493 Z_SortKeySpecList *sort_sequence;
2497 zh->errString = NULL;
2500 sort_sequence = (Z_SortKeySpecList *)
2501 nmem_malloc (nmem, sizeof(*sort_sequence));
2502 sort_sequence->num_specs = 10;
2503 sort_sequence->specs = (Z_SortKeySpec **)
2504 nmem_malloc (nmem, sort_sequence->num_specs *
2505 sizeof(*sort_sequence->specs));
2506 for (i = 0; i<sort_sequence->num_specs; i++)
2507 sort_sequence->specs[i] = 0;
2509 attrset = oid_getentbyoid (rpn->attributeSetId);
2510 attributeSet = attrset->value;
2511 rset = rpn_search_structure (zh, rpn->RPNStructure, attributeSet,
2512 nmem, sort_sequence, num_bases, basenames);
2517 logf (LOG_DEBUG, "search error: %d", zh->errCode);
2519 for (i = 0; sort_sequence->specs[i]; i++)
2521 sort_sequence->num_specs = i;
2523 resultSetRank (zh, sset, rset);
2526 logf (LOG_DEBUG, "resultSetSortSingle in rpn_search");
2527 resultSetSortSingle (zh, nmem, sset, rset,
2528 sort_sequence, &sort_status);
2531 logf (LOG_DEBUG, "resultSetSortSingle status = %d", zh->errCode);
2537 struct scan_info_entry {
2543 struct scan_info_entry *list;
2549 static int scan_handle (char *name, const char *info, int pos, void *client)
2551 int len_prefix, idx;
2552 struct scan_info *scan_info = (struct scan_info *) client;
2554 len_prefix = strlen(scan_info->prefix);
2555 if (memcmp (name, scan_info->prefix, len_prefix))
2557 if (pos > 0) idx = scan_info->after - pos + scan_info->before;
2560 scan_info->list[idx].term = (char *)
2561 odr_malloc (scan_info->odr, strlen(name + len_prefix)+1);
2562 strcpy (scan_info->list[idx].term, name + len_prefix);
2563 assert (*info == sizeof(ISAMS_P));
2564 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAMS_P));
2568 static void scan_term_untrans (ZebraHandle zh, NMEM stream, int reg_type,
2569 char **dst, const char *src)
2571 char term_src[IT_MAX_WORD];
2572 char term_dst[IT_MAX_WORD];
2574 term_untrans (zh, reg_type, term_src, src);
2576 if (zh->iconv_from_utf8 != 0)
2579 char *inbuf = term_src;
2580 size_t inleft = strlen(term_src);
2581 char *outbuf = term_dst;
2582 size_t outleft = sizeof(term_dst)-1;
2585 ret = yaz_iconv (zh->iconv_from_utf8, &inbuf, &inleft,
2587 if (ret == (size_t)(-1))
2590 len = outbuf - term_dst;
2591 *dst = nmem_malloc (stream, len + 1);
2593 memcpy (*dst, term_dst, len);
2597 *dst = nmem_strdup (stream, term_src);
2600 static void count_set (RSET r, int *count)
2608 logf (LOG_DEBUG, "count_set");
2611 rfd = rset_open (r, RSETF_READ);
2612 while (rset_read (r, rfd, &key, &term_index))
2614 if (key.sysno != psysno)
2621 rset_close (r, rfd);
2622 logf (LOG_DEBUG, "%d keys, %d records", kno, *count);
2625 void rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
2626 oid_value attributeset,
2627 int num_bases, char **basenames,
2628 int *position, int *num_entries, ZebraScanEntry **list,
2632 int pos = *position;
2633 int num = *num_entries;
2637 char termz[IT_MAX_WORD+20];
2640 struct scan_info *scan_info_array;
2641 ZebraScanEntry *glist;
2642 int ords[32], ord_no = 0;
2646 char *search_type = NULL;
2647 char rank_type[128];
2652 if (attributeset == VAL_NONE)
2653 attributeset = VAL_BIB1;
2655 yaz_log (LOG_DEBUG, "position = %d, num = %d set=%d",
2656 pos, num, attributeset);
2658 attr_init (&use, zapt, 1);
2659 use_value = attr_find (&use, &attributeset);
2661 if (zebra_maps_attr (zh->reg->zebra_maps, zapt, ®_id, &search_type,
2662 rank_type, &complete_flag, &sort_flag))
2668 yaz_log (LOG_DEBUG, "use_value = %d", use_value);
2670 if (use_value == -1)
2672 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
2676 data1_local_attribute *local_attr;
2678 if ((r=att_getentbyatt (zh, &attp, attributeset, use_value)))
2680 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
2681 attributeset, use_value);
2689 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
2691 zh->errString = basenames[base_no];
2692 zh->errCode = 109; /* Database unavailable */
2696 for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
2697 local_attr = local_attr->next)
2701 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
2704 ords[ord_no++] = ord;
2713 /* prepare dictionary scanning */
2716 scan_info_array = (struct scan_info *)
2717 odr_malloc (stream, ord_no * sizeof(*scan_info_array));
2718 for (i = 0; i < ord_no; i++)
2720 int j, prefix_len = 0;
2721 int before_tmp = before, after_tmp = after;
2722 struct scan_info *scan_info = scan_info_array + i;
2723 struct rpn_char_map_info rcmi;
2725 rpn_char_map_prepare (zh->reg, reg_id, &rcmi);
2727 scan_info->before = before;
2728 scan_info->after = after;
2729 scan_info->odr = stream;
2731 scan_info->list = (struct scan_info_entry *)
2732 odr_malloc (stream, (before+after) * sizeof(*scan_info->list));
2733 for (j = 0; j<before+after; j++)
2734 scan_info->list[j].term = NULL;
2736 prefix_len += key_SU_encode (ords[i], termz + prefix_len);
2737 termz[prefix_len++] = reg_id;
2738 termz[prefix_len] = 0;
2739 strcpy (scan_info->prefix, termz);
2741 if (trans_scan_term (zh, zapt, termz+prefix_len, reg_id))
2744 dict_scan (zh->reg->dict, termz, &before_tmp, &after_tmp,
2745 scan_info, scan_handle);
2747 glist = (ZebraScanEntry *)
2748 odr_malloc (stream, (before+after)*sizeof(*glist));
2750 /* consider terms after main term */
2751 for (i = 0; i < ord_no; i++)
2755 for (i = 0; i<after; i++)
2758 const char *mterm = NULL;
2762 for (j = 0; j < ord_no; j++)
2764 if (ptr[j] < before+after &&
2765 (tst=scan_info_array[j].list[ptr[j]].term) &&
2766 (!mterm || strcmp (tst, mterm) < 0))
2774 scan_term_untrans (zh, stream->mem, reg_id,
2775 &glist[i+before].term, mterm);
2776 rset = rset_trunc (zh, &scan_info_array[j0].list[ptr[j0]].isam_p, 1,
2777 glist[i+before].term, strlen(glist[i+before].term),
2778 NULL, 0, zapt->term->which);
2781 for (j = j0+1; j<ord_no; j++)
2783 if (ptr[j] < before+after &&
2784 (tst=scan_info_array[j].list[ptr[j]].term) &&
2785 !strcmp (tst, mterm))
2787 rset_bool_parms bool_parms;
2791 rset_trunc (zh, &scan_info_array[j].list[ptr[j]].isam_p, 1,
2792 glist[i+before].term,
2793 strlen(glist[i+before].term), NULL, 0,
2796 bool_parms.key_size = sizeof(struct it_key);
2797 bool_parms.cmp = key_compare_it;
2798 bool_parms.rset_l = rset;
2799 bool_parms.rset_r = rset2;
2801 rset = rset_create (rset_kind_or, &bool_parms);
2806 count_set (rset, &glist[i+before].occurrences);
2811 *num_entries -= (after-i);
2815 /* consider terms before main term */
2816 for (i = 0; i<ord_no; i++)
2819 for (i = 0; i<before; i++)
2822 const char *mterm = NULL;
2826 for (j = 0; j <ord_no; j++)
2828 if (ptr[j] < before &&
2829 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2830 (!mterm || strcmp (tst, mterm) > 0))
2839 scan_term_untrans (zh, stream->mem, reg_id,
2840 &glist[before-1-i].term, mterm);
2843 (zh, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1,
2844 glist[before-1-i].term, strlen(glist[before-1-i].term),
2845 NULL, 0, zapt->term->which);
2849 for (j = j0+1; j<ord_no; j++)
2851 if (ptr[j] < before &&
2852 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2853 !strcmp (tst, mterm))
2855 rset_bool_parms bool_parms;
2858 rset2 = rset_trunc (zh,
2859 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1,
2860 glist[before-1-i].term,
2861 strlen(glist[before-1-i].term), NULL, 0,
2864 bool_parms.key_size = sizeof(struct it_key);
2865 bool_parms.cmp = key_compare_it;
2866 bool_parms.rset_l = rset;
2867 bool_parms.rset_r = rset2;
2869 rset = rset_create (rset_kind_or, &bool_parms);
2874 count_set (rset, &glist[before-1-i].occurrences);
2884 *list = glist + i; /* list is set to first 'real' entry */
2886 logf (LOG_DEBUG, "position = %d, num_entries = %d",
2887 *position, *num_entries);
2889 logf (LOG_DEBUG, "scan error: %d", zh->errCode);