1 /* $Id: zrpn.c,v 1.126 2002-12-16 22:59:34 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 const char **out = zebra_maps_input (p->zm, p->reg_type, from, len);
53 const char *outp = *out;
54 yaz_log (LOG_LOG, "---");
57 yaz_log (LOG_LOG, "%02X", *outp);
65 static void rpn_char_map_prepare (struct zebra_register *reg, int reg_type,
66 struct rpn_char_map_info *map_info)
68 map_info->zm = reg->zebra_maps;
69 map_info->reg_type = reg_type;
70 dict_grep_cmap (reg->dict, map_info, rpn_char_map_handler);
77 Z_AttributesPlusTerm *zapt;
80 static int attr_find_ex (AttrType *src, oid_value *attributeSetP,
81 const char **string_value)
85 num_attributes = src->zapt->attributes->num_attributes;
86 while (src->major < num_attributes)
88 Z_AttributeElement *element;
90 element = src->zapt->attributes->attributes[src->major];
91 if (src->type == *element->attributeType)
93 switch (element->which)
95 case Z_AttributeValue_numeric:
97 if (element->attributeSet && attributeSetP)
101 attrset = oid_getentbyoid (element->attributeSet);
102 *attributeSetP = attrset->value;
104 return *element->value.numeric;
106 case Z_AttributeValue_complex:
107 if (src->minor >= element->value.complex->num_list)
109 if (element->attributeSet && attributeSetP)
113 attrset = oid_getentbyoid (element->attributeSet);
114 *attributeSetP = attrset->value;
116 if (element->value.complex->list[src->minor]->which ==
117 Z_StringOrNumeric_numeric)
121 *element->value.complex->list[src->minor-1]->u.numeric;
123 else if (element->value.complex->list[src->minor]->which ==
124 Z_StringOrNumeric_string)
130 element->value.complex->list[src->minor-1]->u.string;
144 static int attr_find (AttrType *src, oid_value *attributeSetP)
146 return attr_find_ex (src, attributeSetP, 0);
149 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
172 static void term_untrans (ZebraHandle zh, int reg_type,
173 char *dst, const char *src)
178 const char *cp = zebra_maps_output (zh->reg->zebra_maps,
180 if (!cp && len < IT_MAX_WORD-1)
183 while (*cp && len < IT_MAX_WORD-1)
189 static void add_isam_p (const char *name, const char *info,
192 if (p->isam_p_indx == p->isam_p_size)
194 ISAMS_P *new_isam_p_buf;
198 p->isam_p_size = 2*p->isam_p_size + 100;
199 new_isam_p_buf = (ISAMS_P *) xmalloc (sizeof(*new_isam_p_buf) *
203 memcpy (new_isam_p_buf, p->isam_p_buf,
204 p->isam_p_indx * sizeof(*p->isam_p_buf));
205 xfree (p->isam_p_buf);
207 p->isam_p_buf = new_isam_p_buf;
210 new_term_no = (int *) xmalloc (sizeof(*new_term_no) *
214 memcpy (new_term_no, p->isam_p_buf,
215 p->isam_p_indx * sizeof(*p->term_no));
218 p->term_no = new_term_no;
221 assert (*info == sizeof(*p->isam_p_buf));
222 memcpy (p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
229 char term_tmp[IT_MAX_WORD];
231 int len = key_SU_decode (&su_code, name);
233 term_untrans (p->zh, p->reg_type, term_tmp, name+len+1);
234 logf (LOG_LOG, "grep: %d %c %s", su_code, name[len], term_tmp);
235 zebraExplain_lookup_ord (p->zh->reg->zei,
236 su_code, &db, &set, &use);
237 logf (LOG_LOG, "grep: set=%d use=%d db=%s", set, use, db);
239 resultSetAddTerm (p->zh, p->termset, name[len], db,
246 static int grep_handle (char *name, const char *info, void *p)
248 add_isam_p (name, info, (struct grep_info *) p);
252 static int term_pre (ZebraMaps zebra_maps, int reg_type, const char **src,
253 const char *ct1, const char *ct2)
255 const char *s1, *s0 = *src;
258 /* skip white space */
261 if (ct1 && strchr (ct1, *s0))
263 if (ct2 && strchr (ct2, *s0))
266 map = zebra_maps_input (zebra_maps, reg_type, &s1, strlen(s1));
267 if (**map != *CHR_SPACE)
275 #define REGEX_CHARS " []()|.*+?!"
277 /* term_100: handle term, where trunc=none (no operators at all) */
278 static int term_100 (ZebraMaps zebra_maps, int reg_type,
279 const char **src, char *dst, int space_split,
287 const char *space_start = 0;
288 const char *space_end = 0;
290 if (!term_pre (zebra_maps, reg_type, src, NULL, NULL))
296 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
299 if (**map == *CHR_SPACE)
302 else /* complete subfield only. */
304 if (**map == *CHR_SPACE)
305 { /* save space mapping for later .. */
310 else if (space_start)
311 { /* reload last space */
312 while (space_start < space_end)
314 if (strchr (REGEX_CHARS, *space_start))
316 dst_term[j++] = *space_start;
317 dst[i++] = *space_start++;
320 space_start = space_end = 0;
323 /* add non-space char */
326 if (strchr(REGEX_CHARS, *s1))
338 /* term_101: handle term, where trunc=Process # */
339 static int term_101 (ZebraMaps zebra_maps, int reg_type,
340 const char **src, char *dst, int space_split,
348 if (!term_pre (zebra_maps, reg_type, src, "#", "#"))
357 dst_term[j++] = *s0++;
362 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
363 if (space_split && **map == *CHR_SPACE)
367 if (strchr(REGEX_CHARS, *s1))
375 dst_term[j++] = '\0';
380 /* term_103: handle term, where trunc=re-2 (regular expressions) */
381 static int term_103 (ZebraMaps zebra_maps, int reg_type, const char **src,
382 char *dst, int *errors, int space_split,
390 if (!term_pre (zebra_maps, reg_type, src, "^\\()[].*+?|", "("))
393 if (errors && *s0 == '+' && s0[1] && s0[2] == '+' && s0[3] &&
396 *errors = s0[1] - '0';
403 if (strchr ("^\\()[].*+?|-", *s0))
411 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
412 if (**map == *CHR_SPACE)
416 if (strchr(REGEX_CHARS, *s1))
429 /* term_103: handle term, where trunc=re-1 (regular expressions) */
430 static int term_102 (ZebraMaps zebra_maps, int reg_type, const char **src,
431 char *dst, int space_split, char *dst_term)
433 return term_103 (zebra_maps, reg_type, src, dst, NULL, space_split,
438 /* term_104: handle term, where trunc=Process # and ! */
439 static int term_104 (ZebraMaps zebra_maps, int reg_type,
440 const char **src, char *dst, int space_split,
448 if (!term_pre (zebra_maps, reg_type, src, "#!", "#!"))
457 dst_term[j++] = *s0++;
462 dst_term[j++] = *s0++;
466 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
467 if (space_split && **map == *CHR_SPACE)
471 if (strchr(REGEX_CHARS, *s1))
479 dst_term[j++] = '\0';
484 /* term_105/106: handle term, where trunc=Process * and ! and right trunc */
485 static int term_105 (ZebraMaps zebra_maps, int reg_type,
486 const char **src, char *dst, int space_split,
487 char *dst_term, int right_truncate)
494 if (!term_pre (zebra_maps, reg_type, src, "*!", "*!"))
503 dst_term[j++] = *s0++;
508 dst_term[j++] = *s0++;
512 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
513 if (space_split && **map == *CHR_SPACE)
517 if (strchr(REGEX_CHARS, *s1))
531 dst_term[j++] = '\0';
537 /* gen_regular_rel - generate regular expression from relation
538 * val: border value (inclusive)
539 * islt: 1 if <=; 0 if >=.
541 static void gen_regular_rel (char *dst, int val, int islt)
548 logf (LOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
552 strcpy (dst, "(-[0-9]+|(");
560 strcpy (dst, "([0-9]+|-(");
561 dst_p = strlen (dst);
571 dst_p = strlen (dst);
572 sprintf (numstr, "%d", val);
573 for (w = strlen(numstr); --w >= 0; pos++)
592 strcpy (dst + dst_p, numstr);
593 dst_p = strlen(dst) - pos - 1;
621 for (i = 0; i<pos; i++)
634 /* match everything less than 10^(pos-1) */
636 for (i=1; i<pos; i++)
637 strcat (dst, "[0-9]?");
641 /* match everything greater than 10^pos */
642 for (i = 0; i <= pos; i++)
643 strcat (dst, "[0-9]");
644 strcat (dst, "[0-9]*");
649 void string_rel_add_char (char **term_p, const char *src, int *indx)
651 if (src[*indx] == '\\')
652 *(*term_p)++ = src[(*indx)++];
653 *(*term_p)++ = src[(*indx)++];
657 * > abc ([b-].*|a[c-].*|ab[d-].*|abc.+)
658 * ([^-a].*|a[^-b].*ab[^-c].*|abc.+)
659 * >= abc ([b-].*|a[c-].*|ab[c-].*)
660 * ([^-a].*|a[^-b].*|ab[c-].*)
661 * < abc ([-0].*|a[-a].*|ab[-b].*)
662 * ([^a-].*|a[^b-].*|ab[^c-].*)
663 * <= abc ([-0].*|a[-a].*|ab[-b].*|abc)
664 * ([^a-].*|a[^b-].*|ab[^c-].*|abc)
666 static int string_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
667 const char **term_sub, char *term_dict,
668 oid_value attributeSet,
669 int reg_type, int space_split, char *term_dst)
674 char *term_tmp = term_dict + strlen(term_dict);
675 char term_component[256];
677 attr_init (&relation, zapt, 2);
678 relation_value = attr_find (&relation, NULL);
680 logf (LOG_DEBUG, "string relation value=%d", relation_value);
681 switch (relation_value)
684 if (!term_100 (zh->reg->zebra_maps, reg_type,
685 term_sub, term_component,
686 space_split, term_dst))
688 logf (LOG_DEBUG, "Relation <");
691 for (i = 0; term_component[i]; )
698 string_rel_add_char (&term_tmp, term_component, &j);
703 string_rel_add_char (&term_tmp, term_component, &i);
714 if (!term_100 (zh->reg->zebra_maps, reg_type,
715 term_sub, term_component,
716 space_split, term_dst))
718 logf (LOG_DEBUG, "Relation <=");
721 for (i = 0; term_component[i]; )
726 string_rel_add_char (&term_tmp, term_component, &j);
730 string_rel_add_char (&term_tmp, term_component, &i);
739 for (i = 0; term_component[i]; )
740 string_rel_add_char (&term_tmp, term_component, &i);
745 if (!term_100 (zh->reg->zebra_maps, reg_type,
746 term_sub, term_component, space_split, term_dst))
748 logf (LOG_DEBUG, "Relation >");
751 for (i = 0; term_component[i];)
756 string_rel_add_char (&term_tmp, term_component, &j);
761 string_rel_add_char (&term_tmp, term_component, &i);
769 for (i = 0; term_component[i];)
770 string_rel_add_char (&term_tmp, term_component, &i);
777 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub,
778 term_component, space_split, term_dst))
780 logf (LOG_DEBUG, "Relation >=");
783 for (i = 0; term_component[i];)
790 string_rel_add_char (&term_tmp, term_component, &j);
793 if (term_component[i+1])
797 string_rel_add_char (&term_tmp, term_component, &i);
801 string_rel_add_char (&term_tmp, term_component, &i);
813 logf (LOG_DEBUG, "Relation =");
814 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub,
815 term_component, space_split, term_dst))
817 strcat (term_tmp, "(");
818 strcat (term_tmp, term_component);
819 strcat (term_tmp, ")");
824 static int string_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
825 const char **term_sub,
826 oid_value attributeSet, NMEM stream,
827 struct grep_info *grep_info,
828 int reg_type, int complete_flag,
829 int num_bases, char **basenames,
830 char *term_dst, int xpath_use);
832 static RSET term_trunc (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
833 const char **term_sub,
834 oid_value attributeSet, NMEM stream,
835 struct grep_info *grep_info,
836 int reg_type, int complete_flag,
837 int num_bases, char **basenames,
839 const char *rank_type, int xpath_use)
842 grep_info->isam_p_indx = 0;
843 r = string_term (zh, zapt, term_sub, attributeSet, stream, grep_info,
844 reg_type, complete_flag, num_bases, basenames,
845 term_dst, xpath_use);
848 logf (LOG_DEBUG, "term: %s", term_dst);
849 return rset_trunc (zh, grep_info->isam_p_buf,
850 grep_info->isam_p_indx, term_dst,
851 strlen(term_dst), rank_type, 1 /* preserve pos */,
856 static int string_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
857 const char **term_sub,
858 oid_value attributeSet, NMEM stream,
859 struct grep_info *grep_info,
860 int reg_type, int complete_flag,
861 int num_bases, char **basenames,
862 char *term_dst, int xpath_use)
864 char term_dict[2*IT_MAX_WORD+4000];
867 int truncation_value;
870 const char *use_string = 0;
871 oid_value curAttributeSet = attributeSet;
873 struct rpn_char_map_info rcmi;
874 int space_split = complete_flag ? 0 : 1;
876 rpn_char_map_prepare (zh->reg, reg_type, &rcmi);
877 attr_init (&use, zapt, 1);
878 use_value = attr_find_ex (&use, &curAttributeSet, &use_string);
879 logf (LOG_DEBUG, "string_term, use value %d", use_value);
880 attr_init (&truncation, zapt, 5);
881 truncation_value = attr_find (&truncation, NULL);
882 logf (LOG_DEBUG, "truncation value %d", truncation_value);
884 if (use_value == -1) /* no attribute - assumy "any" */
886 for (base_no = 0; base_no < num_bases; base_no++)
889 data1_local_attribute id_xpath_attr;
890 data1_local_attribute *local_attr;
891 int max_pos, prefix_len = 0;
895 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
897 zh->errCode = 109; /* Database unavailable */
898 zh->errString = basenames[base_no];
901 if (use_value == -2) /* string attribute (assume IDXPATH/any) */
903 use_value = xpath_use;
904 attp.local_attributes = &id_xpath_attr;
905 attp.attset_ordinal = VAL_IDXPATH;
906 id_xpath_attr.next = 0;
907 id_xpath_attr.local = use_value;
909 else if (curAttributeSet == VAL_IDXPATH)
911 attp.local_attributes = &id_xpath_attr;
912 attp.attset_ordinal = VAL_IDXPATH;
913 id_xpath_attr.next = 0;
914 id_xpath_attr.local = use_value;
918 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
920 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
921 curAttributeSet, use_value, r);
924 /* set was found, but value wasn't defined */
926 sprintf (val_str, "%d (1)", use_value);
928 zh->errString = nmem_strdup (stream, val_str);
933 struct oident oident;
935 oident.proto = PROTO_Z3950;
936 oident.oclass = CLASS_ATTSET;
937 oident.value = curAttributeSet;
938 oid_ent_to_oid (&oident, oid);
941 zh->errString = nmem_strdup (stream, oident.desc);
946 for (local_attr = attp.local_attributes; local_attr;
947 local_attr = local_attr->next)
953 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
958 term_dict[prefix_len++] = '|';
960 term_dict[prefix_len++] = '(';
962 ord_len = key_SU_encode (ord, ord_buf);
963 for (i = 0; i<ord_len; i++)
965 term_dict[prefix_len++] = 1;
966 term_dict[prefix_len++] = ord_buf[i];
972 sprintf (val_str, "%d (2)", use_value);
974 zh->errString = nmem_strdup (stream, val_str);
977 term_dict[prefix_len++] = ')';
978 term_dict[prefix_len++] = 1;
979 term_dict[prefix_len++] = reg_type;
980 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
981 term_dict[prefix_len] = '\0';
983 switch (truncation_value)
985 case -1: /* not specified */
986 case 100: /* do not truncate */
987 if (!string_relation (zh, zapt, &termp, term_dict,
989 reg_type, space_split, term_dst))
991 logf (LOG_LOG, "dict_lookup_grep: %s", term_dict+prefix_len);
992 r = dict_lookup_grep (zh->reg->dict, term_dict, 0,
993 grep_info, &max_pos, 0, grep_handle);
995 logf (LOG_WARN, "dict_lookup_grep fail %d", r);
997 case 1: /* right truncation */
998 term_dict[j++] = '(';
999 if (!term_100 (zh->reg->zebra_maps, reg_type,
1000 &termp, term_dict + j, space_split, term_dst))
1002 strcat (term_dict, ".*)");
1003 dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1004 &max_pos, 0, grep_handle);
1006 case 2: /* keft truncation */
1007 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1008 if (!term_100 (zh->reg->zebra_maps, reg_type,
1009 &termp, term_dict + j, space_split, term_dst))
1011 strcat (term_dict, ")");
1012 dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1013 &max_pos, 0, grep_handle);
1015 case 3: /* left&right truncation */
1016 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1017 if (!term_100 (zh->reg->zebra_maps, reg_type,
1018 &termp, term_dict + j, space_split, term_dst))
1020 strcat (term_dict, ".*)");
1021 dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1022 &max_pos, 0, grep_handle);
1026 case 101: /* process # in term */
1027 term_dict[j++] = '(';
1028 if (!term_101 (zh->reg->zebra_maps, reg_type,
1029 &termp, term_dict + j, space_split, term_dst))
1031 strcat (term_dict, ")");
1032 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1033 &max_pos, 0, grep_handle);
1035 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d", r);
1037 case 102: /* Regexp-1 */
1038 term_dict[j++] = '(';
1039 if (!term_102 (zh->reg->zebra_maps, reg_type,
1040 &termp, term_dict + j, space_split, term_dst))
1042 strcat (term_dict, ")");
1043 logf (LOG_DEBUG, "Regexp-1 tolerance=%d", r);
1044 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1045 &max_pos, 0, grep_handle);
1047 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
1050 case 103: /* Regexp-2 */
1052 term_dict[j++] = '(';
1053 if (!term_103 (zh->reg->zebra_maps, reg_type,
1054 &termp, term_dict + j, &r, space_split, term_dst))
1056 strcat (term_dict, ")");
1057 logf (LOG_DEBUG, "Regexp-2 tolerance=%d", r);
1058 r = dict_lookup_grep (zh->reg->dict, term_dict, r, grep_info,
1059 &max_pos, 2, grep_handle);
1061 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
1064 case 104: /* process # and ! in term */
1065 term_dict[j++] = '(';
1066 if (!term_104 (zh->reg->zebra_maps, reg_type,
1067 &termp, term_dict + j, space_split, term_dst))
1069 strcat (term_dict, ")");
1070 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1071 &max_pos, 0, grep_handle);
1073 logf (LOG_WARN, "dict_lookup_grep err, trunc=#/!: %d", r);
1075 case 105: /* process * and ! in term */
1076 term_dict[j++] = '(';
1077 if (!term_105 (zh->reg->zebra_maps, reg_type,
1078 &termp, term_dict + j, space_split, term_dst, 1))
1080 strcat (term_dict, ")");
1081 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1082 &max_pos, 0, grep_handle);
1084 logf (LOG_WARN, "dict_lookup_grep err, trunc=*/!: %d", r);
1086 case 106: /* process * and ! in term */
1087 term_dict[j++] = '(';
1088 if (!term_105 (zh->reg->zebra_maps, reg_type,
1089 &termp, term_dict + j, space_split, term_dst, 0))
1091 strcat (term_dict, ")");
1092 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1093 &max_pos, 0, grep_handle);
1095 logf (LOG_WARN, "dict_lookup_grep err, trunc=*/!: %d", r);
1100 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1105 /* convert APT search term to UTF8 */
1106 static int zapt_term_to_utf8 (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1110 Z_Term *term = zapt->term;
1112 switch (term->which)
1114 case Z_Term_general:
1115 if (zh->iconv_to_utf8 != 0)
1117 char *inbuf = term->u.general->buf;
1118 size_t inleft = term->u.general->len;
1119 char *outbuf = termz;
1120 size_t outleft = IT_MAX_WORD-1;
1123 ret = yaz_iconv(zh->iconv_to_utf8, &inbuf, &inleft,
1125 if (ret == (size_t)(-1))
1127 ret = yaz_iconv(zh->iconv_to_utf8, 0, 0, 0, 0);
1135 sizez = term->u.general->len;
1136 if (sizez > IT_MAX_WORD-1)
1137 sizez = IT_MAX_WORD-1;
1138 memcpy (termz, term->u.general->buf, sizez);
1139 termz[sizez] = '\0';
1142 case Z_Term_characterString:
1143 sizez = strlen(term->u.characterString);
1144 if (sizez > IT_MAX_WORD-1)
1145 sizez = IT_MAX_WORD-1;
1146 memcpy (termz, term->u.characterString, sizez);
1147 termz[sizez] = '\0';
1156 /* convert APT SCAN term to internal cmap */
1157 static int trans_scan_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1158 char *termz, int reg_type)
1160 char termz0[IT_MAX_WORD];
1162 if (zapt_term_to_utf8(zh, zapt, termz0))
1163 return -1; /* error */
1167 const char *cp = (const char *) termz0;
1168 const char *cp_end = cp + strlen(cp);
1171 const char *space_map = NULL;
1174 while ((len = (cp_end - cp)) > 0)
1176 map = zebra_maps_input (zh->reg->zebra_maps, reg_type, &cp, len);
1177 if (**map == *CHR_SPACE)
1182 for (src = space_map; *src; src++)
1185 for (src = *map; *src; src++)
1194 static RSET rpn_prox (ZebraHandle zh, RSET *rset, int rset_no,
1195 int ordered, int exclusion, int relation, int distance)
1200 struct it_key **buf;
1202 char prox_term[1024];
1203 int length_prox_term = 0;
1204 int min_nn = 10000000;
1206 int term_type = Z_Term_characterString;
1207 const char *flags = NULL;
1209 rsfd = (RSFD *) xmalloc (sizeof(*rsfd)*rset_no);
1210 more = (int *) xmalloc (sizeof(*more)*rset_no);
1211 buf = (struct it_key **) xmalloc (sizeof(*buf)*rset_no);
1214 for (i = 0; i<rset_no; i++)
1217 for (j = 0; j<rset[i]->no_rset_terms; j++)
1219 const char *nflags = rset[i]->rset_terms[j]->flags;
1220 char *term = rset[i]->rset_terms[j]->name;
1221 int lterm = strlen(term);
1222 if (lterm + length_prox_term < sizeof(prox_term)-1)
1224 if (length_prox_term)
1225 prox_term[length_prox_term++] = ' ';
1226 strcpy (prox_term + length_prox_term, term);
1227 length_prox_term += lterm;
1229 if (min_nn > rset[i]->rset_terms[j]->nn)
1230 min_nn = rset[i]->rset_terms[j]->nn;
1232 term_type = rset[i]->rset_terms[j]->type;
1234 /* only if all term types are of type characterString .. */
1235 /* the resulting term is of that type */
1236 if (term_type != Z_Term_characterString)
1237 term_type = Z_Term_general;
1240 for (i = 0; i<rset_no; i++)
1245 for (i = 0; i<rset_no; i++)
1247 buf[i] = (struct it_key *) xmalloc (sizeof(**buf));
1248 rsfd[i] = rset_open (rset[i], RSETF_READ);
1249 if (!(more[i] = rset_read (rset[i], rsfd[i], buf[i], &term_index)))
1254 /* at least one is empty ... return null set */
1255 rset_null_parms parms;
1257 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1259 parms.rset_term->nn = 0;
1260 result = rset_create (rset_kind_null, &parms);
1262 else if (ordered && relation == 3 && exclusion == 0 && distance == 1)
1264 /* special proximity case = phrase search ... */
1265 rset_temp_parms parms;
1268 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1270 parms.rset_term->nn = min_nn;
1271 parms.cmp = key_compare_it;
1272 parms.key_size = sizeof (struct it_key);
1273 parms.temp_path = res_get (zh->res, "setTmpDir");
1274 result = rset_create (rset_kind_temp, &parms);
1275 rsfd_result = rset_open (result, RSETF_WRITE);
1279 for (i = 1; i<rset_no; i++)
1288 cmp = key_compare_it (buf[i], buf[i-1]);
1291 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1292 buf[i-1], &term_index);
1297 if (buf[i-1]->seqno+1 != buf[i]->seqno)
1299 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1300 buf[i-1], &term_index);
1306 more[i] = rset_read (rset[i], rsfd[i], buf[i],
1313 rset_write (result, rsfd_result, buf[0]);
1314 more[0] = rset_read (*rset, *rsfd, *buf, &term_index);
1317 rset_close (result, rsfd_result);
1319 else if (rset_no == 2)
1321 /* generic proximity case (two input sets only) ... */
1322 rset_temp_parms parms;
1325 logf (LOG_LOG, "generic prox, dist = %d, relation = %d, ordered =%d, exclusion=%d",
1326 distance, relation, ordered, exclusion);
1327 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1329 parms.rset_term->nn = min_nn;
1330 parms.cmp = key_compare_it;
1331 parms.key_size = sizeof (struct it_key);
1332 parms.temp_path = res_get (zh->res, "setTmpDir");
1333 result = rset_create (rset_kind_temp, &parms);
1334 rsfd_result = rset_open (result, RSETF_WRITE);
1336 while (more[0] && more[1])
1338 int cmp = key_compare_it (buf[0], buf[1]);
1340 more[0] = rset_read (rset[0], rsfd[0], buf[0], &term_index);
1342 more[1] = rset_read (rset[1], rsfd[1], buf[1], &term_index);
1345 int sysno = buf[0]->sysno;
1349 seqno[n++] = buf[0]->seqno;
1350 while ((more[0] = rset_read (rset[0], rsfd[0], buf[0],
1352 sysno == buf[0]->sysno)
1354 seqno[n++] = buf[0]->seqno;
1357 for (i = 0; i<n; i++)
1359 int diff = buf[1]->seqno - seqno[i];
1360 int excl = exclusion;
1361 if (!ordered && diff < 0)
1366 if (diff < distance && diff >= 0)
1370 if (diff <= distance && diff >= 0)
1374 if (diff == distance && diff >= 0)
1378 if (diff >= distance && diff >= 0)
1382 if (diff > distance && diff >= 0)
1386 if (diff != distance && diff >= 0)
1392 rset_write (result, rsfd_result, buf[1]);
1396 } while ((more[1] = rset_read (rset[1], rsfd[1], buf[1],
1398 sysno == buf[1]->sysno);
1401 rset_close (result, rsfd_result);
1405 rset_null_parms parms;
1407 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1409 parms.rset_term->nn = 0;
1410 result = rset_create (rset_kind_null, &parms);
1412 for (i = 0; i<rset_no; i++)
1415 rset_close (rset[i], rsfd[i]);
1425 char *normalize_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1426 const char *termz, NMEM stream, unsigned reg_id)
1429 AttrType truncation;
1430 int truncation_value;
1433 attr_init (&truncation, zapt, 5);
1434 truncation_value = attr_find (&truncation, NULL);
1436 switch (truncation_value)
1456 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, ex_list,
1457 termz, strlen(termz));
1459 return nmem_strdup(stream, termz);
1462 char *buf = (char*) nmem_malloc (stream, wrbuf_len(wrbuf)+1);
1463 memcpy (buf, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1464 buf[wrbuf_len(wrbuf)] = '\0';
1469 static void grep_info_delete (struct grep_info *grep_info)
1472 xfree(grep_info->term_no);
1474 xfree (grep_info->isam_p_buf);
1477 static int grep_info_prepare (ZebraHandle zh,
1478 Z_AttributesPlusTerm *zapt,
1479 struct grep_info *grep_info,
1484 int termset_value_numeric;
1485 const char *termset_value_string;
1488 grep_info->term_no = 0;
1490 grep_info->isam_p_size = 0;
1491 grep_info->isam_p_buf = NULL;
1493 grep_info->reg_type = reg_type;
1494 grep_info->termset = 0;
1498 attr_init (&termset, zapt, 8);
1499 termset_value_numeric =
1500 attr_find_ex (&termset, NULL, &termset_value_string);
1501 if (termset_value_numeric != -1)
1504 const char *termset_name = 0;
1505 if (termset_value_numeric != -2)
1508 sprintf (resname, "%d", termset_value_numeric);
1509 termset_name = resname;
1512 termset_name = termset_value_string;
1513 logf (LOG_LOG, "creating termset set %s", termset_name);
1514 grep_info->termset = resultSetAdd (zh, termset_name, 1);
1515 if (!grep_info->termset)
1518 zh->errString = nmem_strdup (stream, termset_name);
1526 static RSET rpn_search_APT_phrase (ZebraHandle zh,
1527 Z_AttributesPlusTerm *zapt,
1528 const char *termz_org,
1529 oid_value attributeSet,
1531 int reg_type, int complete_flag,
1532 const char *rank_type, int xpath_use,
1533 int num_bases, char **basenames)
1535 char term_dst[IT_MAX_WORD+1];
1536 RSET rset[60], result;
1538 struct grep_info grep_info;
1539 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1540 const char *termp = termz;
1543 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1547 logf (LOG_DEBUG, "APT_phrase termp=%s", termp);
1548 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1550 reg_type, complete_flag,
1551 num_bases, basenames,
1552 term_dst, rank_type,
1556 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1559 grep_info_delete (&grep_info);
1562 rset_null_parms parms;
1564 parms.rset_term = rset_term_create (termz, -1, rank_type,
1566 return rset_create (rset_kind_null, &parms);
1568 else if (rset_no == 1)
1570 result = rpn_prox (zh, rset, rset_no, 1, 0, 3, 1);
1571 for (i = 0; i<rset_no; i++)
1572 rset_delete (rset[i]);
1576 static RSET rpn_search_APT_or_list (ZebraHandle zh,
1577 Z_AttributesPlusTerm *zapt,
1578 const char *termz_org,
1579 oid_value attributeSet,
1581 int reg_type, int complete_flag,
1582 const char *rank_type,
1584 int num_bases, char **basenames)
1586 char term_dst[IT_MAX_WORD+1];
1587 RSET rset[60], result;
1589 struct grep_info grep_info;
1590 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1591 const char *termp = termz;
1593 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1597 logf (LOG_DEBUG, "APT_or_list termp=%s", termp);
1598 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1600 reg_type, complete_flag,
1601 num_bases, basenames,
1602 term_dst, rank_type,
1606 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1609 grep_info_delete (&grep_info);
1612 rset_null_parms parms;
1614 parms.rset_term = rset_term_create (termz, -1, rank_type,
1616 return rset_create (rset_kind_null, &parms);
1619 for (i = 1; i<rset_no; i++)
1621 rset_bool_parms bool_parms;
1623 bool_parms.rset_l = result;
1624 bool_parms.rset_r = rset[i];
1625 bool_parms.key_size = sizeof(struct it_key);
1626 bool_parms.cmp = key_compare_it;
1627 result = rset_create (rset_kind_or, &bool_parms);
1632 static RSET rpn_search_APT_and_list (ZebraHandle zh,
1633 Z_AttributesPlusTerm *zapt,
1634 const char *termz_org,
1635 oid_value attributeSet,
1637 int reg_type, int complete_flag,
1638 const char *rank_type,
1640 int num_bases, char **basenames)
1642 char term_dst[IT_MAX_WORD+1];
1643 RSET rset[60], result;
1645 struct grep_info grep_info;
1646 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1647 const char *termp = termz;
1649 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1653 logf (LOG_DEBUG, "APT_and_list termp=%s", termp);
1654 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1656 reg_type, complete_flag,
1657 num_bases, basenames,
1658 term_dst, rank_type,
1662 assert (rset[rset_no]);
1663 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1666 grep_info_delete (&grep_info);
1669 rset_null_parms parms;
1671 parms.rset_term = rset_term_create (termz, -1, rank_type,
1673 return rset_create (rset_kind_null, &parms);
1676 for (i = 1; i<rset_no; i++)
1678 rset_bool_parms bool_parms;
1680 bool_parms.rset_l = result;
1681 bool_parms.rset_r = rset[i];
1682 bool_parms.key_size = sizeof(struct it_key);
1683 bool_parms.cmp = key_compare_it;
1684 result = rset_create (rset_kind_and, &bool_parms);
1689 static int numeric_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1690 const char **term_sub,
1692 oid_value attributeSet,
1693 struct grep_info *grep_info,
1702 char *term_tmp = term_dict + strlen(term_dict);
1704 attr_init (&relation, zapt, 2);
1705 relation_value = attr_find (&relation, NULL);
1707 logf (LOG_DEBUG, "numeric relation value=%d", relation_value);
1709 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub, term_tmp, 1,
1712 term_value = atoi (term_tmp);
1713 switch (relation_value)
1716 logf (LOG_DEBUG, "Relation <");
1717 gen_regular_rel (term_tmp, term_value-1, 1);
1720 logf (LOG_DEBUG, "Relation <=");
1721 gen_regular_rel (term_tmp, term_value, 1);
1724 logf (LOG_DEBUG, "Relation >=");
1725 gen_regular_rel (term_tmp, term_value, 0);
1728 logf (LOG_DEBUG, "Relation >");
1729 gen_regular_rel (term_tmp, term_value+1, 0);
1733 logf (LOG_DEBUG, "Relation =");
1734 sprintf (term_tmp, "(0*%d)", term_value);
1736 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_tmp);
1737 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info, max_pos,
1740 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1741 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1745 static int numeric_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1746 const char **term_sub,
1747 oid_value attributeSet, struct grep_info *grep_info,
1748 int reg_type, int complete_flag,
1749 int num_bases, char **basenames,
1750 char *term_dst, int xpath_use)
1752 char term_dict[2*IT_MAX_WORD+2];
1756 const char *use_string = 0;
1757 oid_value curAttributeSet = attributeSet;
1759 struct rpn_char_map_info rcmi;
1761 rpn_char_map_prepare (zh->reg, reg_type, &rcmi);
1762 attr_init (&use, zapt, 1);
1763 use_value = attr_find_ex (&use, &curAttributeSet, &use_string);
1765 if (use_value == -1)
1768 for (base_no = 0; base_no < num_bases; base_no++)
1771 data1_local_attribute id_xpath_attr;
1772 data1_local_attribute *local_attr;
1773 int max_pos, prefix_len = 0;
1776 if (use_value == -2) /* string attribute (assume IDXPATH/any) */
1778 use_value = xpath_use;
1779 attp.local_attributes = &id_xpath_attr;
1780 attp.attset_ordinal = VAL_IDXPATH;
1781 id_xpath_attr.next = 0;
1782 id_xpath_attr.local = use_value;
1784 else if (curAttributeSet == VAL_IDXPATH)
1786 attp.local_attributes = &id_xpath_attr;
1787 attp.attset_ordinal = VAL_IDXPATH;
1788 id_xpath_attr.next = 0;
1789 id_xpath_attr.local = use_value;
1793 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
1795 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1796 curAttributeSet, use_value, r);
1804 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
1806 zh->errCode = 109; /* Database unavailable */
1807 zh->errString = basenames[base_no];
1810 for (local_attr = attp.local_attributes; local_attr;
1811 local_attr = local_attr->next)
1817 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
1822 term_dict[prefix_len++] = '|';
1824 term_dict[prefix_len++] = '(';
1826 ord_len = key_SU_encode (ord, ord_buf);
1827 for (i = 0; i<ord_len; i++)
1829 term_dict[prefix_len++] = 1;
1830 term_dict[prefix_len++] = ord_buf[i];
1838 term_dict[prefix_len++] = ')';
1839 term_dict[prefix_len++] = 1;
1840 term_dict[prefix_len++] = reg_type;
1841 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1842 term_dict[prefix_len] = '\0';
1843 if (!numeric_relation (zh, zapt, &termp, term_dict,
1844 attributeSet, grep_info, &max_pos, reg_type,
1849 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1853 static RSET rpn_search_APT_numeric (ZebraHandle zh,
1854 Z_AttributesPlusTerm *zapt,
1856 oid_value attributeSet,
1858 int reg_type, int complete_flag,
1859 const char *rank_type, int xpath_use,
1860 int num_bases, char **basenames)
1862 char term_dst[IT_MAX_WORD+1];
1863 const char *termp = termz;
1864 RSET rset[60], result;
1865 int i, r, rset_no = 0;
1866 struct grep_info grep_info;
1868 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1872 logf (LOG_DEBUG, "APT_numeric termp=%s", termp);
1873 grep_info.isam_p_indx = 0;
1874 r = numeric_term (zh, zapt, &termp, attributeSet, &grep_info,
1875 reg_type, complete_flag, num_bases, basenames,
1876 term_dst, xpath_use);
1879 logf (LOG_DEBUG, "term: %s", term_dst);
1880 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1881 grep_info.isam_p_indx, term_dst,
1882 strlen(term_dst), rank_type,
1883 0 /* preserve position */,
1885 assert (rset[rset_no]);
1886 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1889 grep_info_delete (&grep_info);
1892 rset_null_parms parms;
1894 parms.rset_term = rset_term_create (term_dst, -1, rank_type,
1896 return rset_create (rset_kind_null, &parms);
1899 for (i = 1; i<rset_no; i++)
1901 rset_bool_parms bool_parms;
1903 bool_parms.rset_l = result;
1904 bool_parms.rset_r = rset[i];
1905 bool_parms.key_size = sizeof(struct it_key);
1906 bool_parms.cmp = key_compare_it;
1907 result = rset_create (rset_kind_and, &bool_parms);
1912 static RSET rpn_search_APT_local (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1914 oid_value attributeSet,
1916 const char *rank_type)
1921 rset_temp_parms parms;
1923 parms.rset_term = rset_term_create (termz, -1, rank_type,
1925 parms.cmp = key_compare_it;
1926 parms.key_size = sizeof (struct it_key);
1927 parms.temp_path = res_get (zh->res, "setTmpDir");
1928 result = rset_create (rset_kind_temp, &parms);
1929 rsfd = rset_open (result, RSETF_WRITE);
1931 key.sysno = atoi (termz);
1935 rset_write (result, rsfd, &key);
1936 rset_close (result, rsfd);
1940 static RSET rpn_sort_spec (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1941 oid_value attributeSet, NMEM stream,
1942 Z_SortKeySpecList *sort_sequence,
1943 const char *rank_type)
1945 rset_null_parms parms;
1947 int sort_relation_value;
1948 AttrType sort_relation_type;
1953 Z_AttributeElement *ae;
1958 attr_init (&sort_relation_type, zapt, 7);
1959 sort_relation_value = attr_find (&sort_relation_type, &attributeSet);
1961 attr_init (&use_type, zapt, 1);
1962 use_value = attr_find (&use_type, &attributeSet);
1964 if (!sort_sequence->specs)
1966 sort_sequence->num_specs = 10;
1967 sort_sequence->specs = (Z_SortKeySpec **)
1968 nmem_malloc (stream, sort_sequence->num_specs *
1969 sizeof(*sort_sequence->specs));
1970 for (i = 0; i<sort_sequence->num_specs; i++)
1971 sort_sequence->specs[i] = 0;
1973 if (zapt->term->which != Z_Term_general)
1976 i = atoi_n ((char *) zapt->term->u.general->buf,
1977 zapt->term->u.general->len);
1978 if (i >= sort_sequence->num_specs)
1980 sprintf (termz, "%d", i);
1982 oe.proto = PROTO_Z3950;
1983 oe.oclass = CLASS_ATTSET;
1984 oe.value = attributeSet;
1985 if (!oid_ent_to_oid (&oe, oid))
1988 sks = (Z_SortKeySpec *) nmem_malloc (stream, sizeof(*sks));
1989 sks->sortElement = (Z_SortElement *)
1990 nmem_malloc (stream, sizeof(*sks->sortElement));
1991 sks->sortElement->which = Z_SortElement_generic;
1992 sk = sks->sortElement->u.generic = (Z_SortKey *)
1993 nmem_malloc (stream, sizeof(*sk));
1994 sk->which = Z_SortKey_sortAttributes;
1995 sk->u.sortAttributes = (Z_SortAttributes *)
1996 nmem_malloc (stream, sizeof(*sk->u.sortAttributes));
1998 sk->u.sortAttributes->id = oid;
1999 sk->u.sortAttributes->list = (Z_AttributeList *)
2000 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list));
2001 sk->u.sortAttributes->list->num_attributes = 1;
2002 sk->u.sortAttributes->list->attributes = (Z_AttributeElement **)
2003 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list->attributes));
2004 ae = *sk->u.sortAttributes->list->attributes = (Z_AttributeElement *)
2005 nmem_malloc (stream, sizeof(**sk->u.sortAttributes->list->attributes));
2006 ae->attributeSet = 0;
2007 ae->attributeType = (int *)
2008 nmem_malloc (stream, sizeof(*ae->attributeType));
2009 *ae->attributeType = 1;
2010 ae->which = Z_AttributeValue_numeric;
2011 ae->value.numeric = (int *)
2012 nmem_malloc (stream, sizeof(*ae->value.numeric));
2013 *ae->value.numeric = use_value;
2015 sks->sortRelation = (int *)
2016 nmem_malloc (stream, sizeof(*sks->sortRelation));
2017 if (sort_relation_value == 1)
2018 *sks->sortRelation = Z_SortRelation_ascending;
2019 else if (sort_relation_value == 2)
2020 *sks->sortRelation = Z_SortRelation_descending;
2022 *sks->sortRelation = Z_SortRelation_ascending;
2024 sks->caseSensitivity = (int *)
2025 nmem_malloc (stream, sizeof(*sks->caseSensitivity));
2026 *sks->caseSensitivity = 0;
2028 sks->which = Z_SortKeySpec_null;
2029 sks->u.null = odr_nullval ();
2030 sort_sequence->specs[i] = sks;
2032 parms.rset_term = rset_term_create (termz, -1, rank_type,
2034 return rset_create (rset_kind_null, &parms);
2037 struct xpath_predicate {
2040 #define XPATH_PREDICATE_RELATION 1
2046 #define XPATH_PREDICATE_BOOLEAN 2
2049 struct xpath_predicate *left;
2050 struct xpath_predicate *right;
2055 struct xpath_location_step {
2057 struct xpath_predicate *predicate;
2060 static int parse_xpath(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2061 oid_value attributeSet,
2062 struct xpath_location_step *xpath, NMEM mem)
2064 oid_value curAttributeSet = attributeSet;
2066 const char *use_string = 0;
2070 attr_init (&use, zapt, 1);
2071 attr_find_ex (&use, &curAttributeSet, &use_string);
2073 if (!use_string || *use_string != '/')
2079 while (*cp && !strchr("/[",*cp))
2084 xpath[no].predicate = 0;
2085 xpath[no].part = nmem_malloc (mem, i+1);
2086 memcpy (xpath[no].part, cp - i, i);
2087 xpath[no].part[i] = 0;
2091 struct xpath_predicate *p = xpath[no].predicate =
2092 nmem_malloc (mem, sizeof(struct xpath_predicate));
2094 p->which = XPATH_PREDICATE_RELATION;
2099 for (i = 0; *cp && !strchr("><=] ", *cp); i++)
2101 p->u.relation.name = nmem_malloc (mem, i+1);
2102 memcpy (p->u.relation.name, cp - i, i);
2103 p->u.relation.name[i] = 0;
2108 for (i = 0; *cp && strchr(">=<!", *cp); i++)
2111 p->u.relation.op = nmem_malloc (mem, i+1);
2113 memcpy (p->u.relation.op, cp - i, i);
2114 p->u.relation.op[i] = 0;
2119 if (strchr("\"'", *cp))
2122 for (i = 0; *cp && !strchr("\"'", *cp); i++)
2125 p->u.relation.value = nmem_malloc (mem, i+1);
2127 memcpy (p->u.relation.value, cp - i, i);
2128 p->u.relation.value[i] = 0;
2129 yaz_log (LOG_LOG, "value=%s", p->u.relation.value);
2135 for (i = 0; *cp && !strchr(" ]", *cp); i++)
2137 p->u.relation.value = nmem_malloc (mem, i+1);
2139 memcpy (p->u.relation.value, cp - i, i);
2140 p->u.relation.value[i] = 0;
2147 } /* end of ] predicate */
2157 static RSET xpath_trunc(ZebraHandle zh, NMEM stream,
2158 int reg_type, const char *term, int use,
2159 oid_value curAttributeSet)
2162 struct grep_info grep_info;
2163 char term_dict[2048];
2166 int ord = zebraExplain_lookupSU (zh->reg->zei, curAttributeSet, use);
2167 int ord_len, i, r, max_pos;
2168 int term_type = Z_Term_characterString;
2169 const char *flags = "void";
2171 if (grep_info_prepare (zh, 0 /* zapt */, &grep_info, '0', stream))
2173 rset_null_parms parms;
2175 parms.rset_term = rset_term_create (term, strlen(term),
2177 parms.rset_term->nn = 0;
2178 return rset_create (rset_kind_null, &parms);
2183 rset_null_parms parms;
2185 parms.rset_term = rset_term_create (term, strlen(term),
2187 parms.rset_term->nn = 0;
2188 return rset_create (rset_kind_null, &parms);
2191 term_dict[prefix_len++] = '|';
2193 term_dict[prefix_len++] = '(';
2195 ord_len = key_SU_encode (ord, ord_buf);
2196 for (i = 0; i<ord_len; i++)
2198 term_dict[prefix_len++] = 1;
2199 term_dict[prefix_len++] = ord_buf[i];
2201 term_dict[prefix_len++] = ')';
2202 term_dict[prefix_len++] = 1;
2203 term_dict[prefix_len++] = reg_type;
2205 strcpy (term_dict+prefix_len, term);
2207 grep_info.isam_p_indx = 0;
2208 r = dict_lookup_grep (zh->reg->dict, term_dict, 0,
2209 &grep_info, &max_pos, 0, grep_handle);
2210 yaz_log (LOG_LOG, "%s %d positions", term,
2211 grep_info.isam_p_indx);
2212 rset = rset_trunc (zh, grep_info.isam_p_buf,
2213 grep_info.isam_p_indx, term, strlen(term),
2214 flags, 1, term_type);
2215 grep_info_delete (&grep_info);
2219 static RSET rpn_search_xpath (ZebraHandle zh,
2220 oid_value attributeSet,
2221 int num_bases, char **basenames,
2222 NMEM stream, const char *rank_type, RSET rset,
2223 int xpath_len, struct xpath_location_step *xpath)
2225 oid_value curAttributeSet = attributeSet;
2232 yaz_log (LOG_LOG, "len=%d", xpath_len);
2233 for (i = 0; i<xpath_len; i++)
2235 yaz_log (LOG_LOG, "XPATH %d %s", i, xpath[i].part);
2239 curAttributeSet = VAL_IDXPATH;
2249 a[@attr=value]/b[@other=othervalue]
2251 /e/@a val range(e/,range(@a,freetext(w,1015,val),@a),e/)
2252 /a/b val range(b/a/,freetext(w,1016,val),b/a/)
2253 /a/b/@c val range(b/a/,range(@c,freetext(w,1016,val),@c),b/a/)
2254 /a/b[@c=y] val range(b/a/,freetext(w,1016,val),b/a/,@c=y)
2255 /a[@c=y]/b val range(a/,range(b/a/,freetext(w,1016,val),b/a/),a/,@c=y)
2256 /a[@c=x]/b[@c=y] range(a/,range(b/a/,freetext(w,1016,val),b/a/,@c=y),a/,@c=x)
2260 dict_grep_cmap (zh->reg->dict, 0, 0);
2262 for (base_no = 0; base_no < num_bases; base_no++)
2264 int level = xpath_len;
2267 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
2269 zh->errCode = 109; /* Database unavailable */
2270 zh->errString = basenames[base_no];
2273 while (--level >= 0)
2275 char xpath_rev[128];
2277 rset_between_parms parms;
2278 RSET rset_start_tag = 0, rset_end_tag = 0, rset_attr = 0;
2282 for (i = level; i >= 1; --i)
2284 const char *cp = xpath[i].part;
2290 memcpy (xpath_rev + len, "[^/]*", 5);
2293 else if (*cp == ' ')
2296 xpath_rev[len++] = 1;
2297 xpath_rev[len++] = ' ';
2301 xpath_rev[len++] = *cp;
2302 xpath_rev[len++] = '/';
2304 else if (i == 1) /* // case */
2306 xpath_rev[len++] = '.';
2307 xpath_rev[len++] = '*';
2312 if (xpath[level].predicate &&
2313 xpath[level].predicate->which == XPATH_PREDICATE_RELATION &&
2314 xpath[level].predicate->u.relation.name[0])
2316 char predicate_str[128];
2318 strcpy (predicate_str,
2319 xpath[level].predicate->u.relation.name+1);
2320 if (xpath[level].predicate->u.relation.value)
2322 strcat (predicate_str, "=");
2323 strcat (predicate_str,
2324 xpath[level].predicate->u.relation.value);
2326 rset_attr = xpath_trunc (
2327 zh, stream, '0', predicate_str, 3, curAttributeSet);
2334 yaz_log (LOG_LOG, "xpath_rev (%d) = %s", level, xpath_rev);
2335 if (strlen(xpath_rev))
2337 rset_start_tag = xpath_trunc(zh, stream,
2338 '0', xpath_rev, 1, curAttributeSet);
2340 rset_end_tag = xpath_trunc(zh, stream,
2341 '0', xpath_rev, 2, curAttributeSet);
2343 parms.key_size = sizeof(struct it_key);
2344 parms.cmp = key_compare_it;
2345 parms.rset_l = rset_start_tag;
2346 parms.rset_m = rset;
2347 parms.rset_r = rset_end_tag;
2348 parms.rset_attr = rset_attr;
2349 parms.printer = key_print_it;
2350 rset = rset_create (rset_kind_between, &parms);
2361 static RSET rpn_search_APT (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2362 oid_value attributeSet, NMEM stream,
2363 Z_SortKeySpecList *sort_sequence,
2364 int num_bases, char **basenames)
2367 char *search_type = NULL;
2368 char rank_type[128];
2371 char termz[IT_MAX_WORD+1];
2375 struct xpath_location_step xpath[10];
2377 zebra_maps_attr (zh->reg->zebra_maps, zapt, ®_id, &search_type,
2378 rank_type, &complete_flag, &sort_flag);
2380 logf (LOG_DEBUG, "reg_id=%c", reg_id);
2381 logf (LOG_DEBUG, "complete_flag=%d", complete_flag);
2382 logf (LOG_DEBUG, "search_type=%s", search_type);
2383 logf (LOG_DEBUG, "rank_type=%s", rank_type);
2385 if (zapt_term_to_utf8(zh, zapt, termz))
2389 return rpn_sort_spec (zh, zapt, attributeSet, stream, sort_sequence,
2391 xpath_len = parse_xpath(zh, zapt, attributeSet, xpath, stream);
2395 if (xpath[xpath_len-1].part[0] == '@')
2399 if (!strcmp (search_type, "phrase"))
2401 rset = rpn_search_APT_phrase (zh, zapt, termz, attributeSet, stream,
2402 reg_id, complete_flag, rank_type,
2404 num_bases, basenames);
2406 else if (!strcmp (search_type, "and-list"))
2408 rset = rpn_search_APT_and_list (zh, zapt, termz, attributeSet, stream,
2409 reg_id, complete_flag, rank_type,
2411 num_bases, basenames);
2413 else if (!strcmp (search_type, "or-list"))
2415 rset = rpn_search_APT_or_list (zh, zapt, termz, attributeSet, stream,
2416 reg_id, complete_flag, rank_type,
2418 num_bases, basenames);
2420 else if (!strcmp (search_type, "local"))
2422 rset = rpn_search_APT_local (zh, zapt, termz, attributeSet, stream,
2425 else if (!strcmp (search_type, "numeric"))
2427 rset = rpn_search_APT_numeric (zh, zapt, termz, attributeSet, stream,
2428 reg_id, complete_flag, rank_type,
2430 num_bases, basenames);
2432 else if (!strcmp (search_type, "always"))
2438 return rpn_search_xpath (zh, attributeSet, num_bases, basenames,
2439 stream, rank_type, rset, xpath_len, xpath);
2442 static RSET rpn_search_structure (ZebraHandle zh, Z_RPNStructure *zs,
2443 oid_value attributeSet, NMEM stream,
2444 Z_SortKeySpecList *sort_sequence,
2445 int num_bases, char **basenames)
2448 if (zs->which == Z_RPNStructure_complex)
2450 Z_Operator *zop = zs->u.complex->roperator;
2451 rset_bool_parms bool_parms;
2453 bool_parms.rset_l = rpn_search_structure (zh, zs->u.complex->s1,
2454 attributeSet, stream,
2456 num_bases, basenames);
2457 if (bool_parms.rset_l == NULL)
2459 bool_parms.rset_r = rpn_search_structure (zh, zs->u.complex->s2,
2460 attributeSet, stream,
2462 num_bases, basenames);
2463 if (bool_parms.rset_r == NULL)
2465 rset_delete (bool_parms.rset_l);
2468 bool_parms.key_size = sizeof(struct it_key);
2469 bool_parms.cmp = key_compare_it;
2473 case Z_Operator_and:
2474 r = rset_create (rset_kind_and, &bool_parms);
2477 r = rset_create (rset_kind_or, &bool_parms);
2479 case Z_Operator_and_not:
2480 r = rset_create (rset_kind_not, &bool_parms);
2482 case Z_Operator_prox:
2483 if (zop->u.prox->which != Z_ProximityOperator_known)
2488 if (*zop->u.prox->u.known != Z_ProxUnit_word)
2490 char *val = (char *) nmem_malloc (stream, 16);
2492 zh->errString = val;
2493 sprintf (val, "%d", *zop->u.prox->u.known);
2500 rsets[0] = bool_parms.rset_l;
2501 rsets[1] = bool_parms.rset_r;
2503 r = rpn_prox (zh, rsets, 2,
2504 *zop->u.prox->ordered,
2505 (!zop->u.prox->exclusion ? 0 :
2506 *zop->u.prox->exclusion),
2507 *zop->u.prox->relationType,
2508 *zop->u.prox->distance);
2509 rset_delete (rsets[0]);
2510 rset_delete (rsets[1]);
2518 else if (zs->which == Z_RPNStructure_simple)
2520 if (zs->u.simple->which == Z_Operand_APT)
2522 logf (LOG_DEBUG, "rpn_search_APT");
2523 r = rpn_search_APT (zh, zs->u.simple->u.attributesPlusTerm,
2524 attributeSet, stream, sort_sequence,
2525 num_bases, basenames);
2527 else if (zs->u.simple->which == Z_Operand_resultSetId)
2529 logf (LOG_DEBUG, "rpn_search_ref");
2530 r = resultSetRef (zh, zs->u.simple->u.resultSetId);
2533 r = rset_create (rset_kind_null, NULL);
2536 nmem_strdup (stream, zs->u.simple->u.resultSetId);
2555 RSET rpn_search (ZebraHandle zh, NMEM nmem,
2556 Z_RPNQuery *rpn, int num_bases, char **basenames,
2557 const char *setname,
2562 oid_value attributeSet;
2563 Z_SortKeySpecList *sort_sequence;
2567 zh->errString = NULL;
2570 sort_sequence = (Z_SortKeySpecList *)
2571 nmem_malloc (nmem, sizeof(*sort_sequence));
2572 sort_sequence->num_specs = 10;
2573 sort_sequence->specs = (Z_SortKeySpec **)
2574 nmem_malloc (nmem, sort_sequence->num_specs *
2575 sizeof(*sort_sequence->specs));
2576 for (i = 0; i<sort_sequence->num_specs; i++)
2577 sort_sequence->specs[i] = 0;
2579 attrset = oid_getentbyoid (rpn->attributeSetId);
2580 attributeSet = attrset->value;
2581 rset = rpn_search_structure (zh, rpn->RPNStructure, attributeSet,
2582 nmem, sort_sequence, num_bases, basenames);
2587 logf (LOG_DEBUG, "search error: %d", zh->errCode);
2589 for (i = 0; sort_sequence->specs[i]; i++)
2591 sort_sequence->num_specs = i;
2593 resultSetRank (zh, sset, rset);
2596 logf (LOG_DEBUG, "resultSetSortSingle in rpn_search");
2597 resultSetSortSingle (zh, nmem, sset, rset,
2598 sort_sequence, &sort_status);
2601 logf (LOG_DEBUG, "resultSetSortSingle status = %d", zh->errCode);
2607 struct scan_info_entry {
2613 struct scan_info_entry *list;
2619 static int scan_handle (char *name, const char *info, int pos, void *client)
2621 int len_prefix, idx;
2622 struct scan_info *scan_info = (struct scan_info *) client;
2624 len_prefix = strlen(scan_info->prefix);
2625 if (memcmp (name, scan_info->prefix, len_prefix))
2627 if (pos > 0) idx = scan_info->after - pos + scan_info->before;
2630 scan_info->list[idx].term = (char *)
2631 odr_malloc (scan_info->odr, strlen(name + len_prefix)+1);
2632 strcpy (scan_info->list[idx].term, name + len_prefix);
2633 assert (*info == sizeof(ISAMS_P));
2634 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAMS_P));
2638 static void scan_term_untrans (ZebraHandle zh, NMEM stream, int reg_type,
2639 char **dst, const char *src)
2641 char term_src[IT_MAX_WORD];
2642 char term_dst[IT_MAX_WORD];
2644 term_untrans (zh, reg_type, term_src, src);
2646 if (zh->iconv_from_utf8 != 0)
2649 char *inbuf = term_src;
2650 size_t inleft = strlen(term_src);
2651 char *outbuf = term_dst;
2652 size_t outleft = sizeof(term_dst)-1;
2655 ret = yaz_iconv (zh->iconv_from_utf8, &inbuf, &inleft,
2657 if (ret == (size_t)(-1))
2660 len = outbuf - term_dst;
2661 *dst = nmem_malloc (stream, len + 1);
2663 memcpy (*dst, term_dst, len);
2667 *dst = nmem_strdup (stream, term_src);
2670 static void count_set (RSET r, int *count)
2678 logf (LOG_DEBUG, "count_set");
2681 rfd = rset_open (r, RSETF_READ);
2682 while (rset_read (r, rfd, &key, &term_index))
2684 if (key.sysno != psysno)
2691 rset_close (r, rfd);
2692 logf (LOG_DEBUG, "%d keys, %d records", kno, *count);
2695 void rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
2696 oid_value attributeset,
2697 int num_bases, char **basenames,
2698 int *position, int *num_entries, ZebraScanEntry **list,
2702 int pos = *position;
2703 int num = *num_entries;
2707 char termz[IT_MAX_WORD+20];
2710 struct scan_info *scan_info_array;
2711 ZebraScanEntry *glist;
2712 int ords[32], ord_no = 0;
2716 char *search_type = NULL;
2717 char rank_type[128];
2722 if (attributeset == VAL_NONE)
2723 attributeset = VAL_BIB1;
2725 yaz_log (LOG_DEBUG, "position = %d, num = %d set=%d",
2726 pos, num, attributeset);
2728 attr_init (&use, zapt, 1);
2729 use_value = attr_find (&use, &attributeset);
2731 if (zebra_maps_attr (zh->reg->zebra_maps, zapt, ®_id, &search_type,
2732 rank_type, &complete_flag, &sort_flag))
2738 yaz_log (LOG_DEBUG, "use_value = %d", use_value);
2740 if (use_value == -1)
2742 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
2746 data1_local_attribute *local_attr;
2748 if ((r=att_getentbyatt (zh, &attp, attributeset, use_value)))
2750 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
2751 attributeset, use_value);
2759 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
2761 zh->errString = basenames[base_no];
2762 zh->errCode = 109; /* Database unavailable */
2766 for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
2767 local_attr = local_attr->next)
2771 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
2774 ords[ord_no++] = ord;
2783 /* prepare dictionary scanning */
2786 scan_info_array = (struct scan_info *)
2787 odr_malloc (stream, ord_no * sizeof(*scan_info_array));
2788 for (i = 0; i < ord_no; i++)
2790 int j, prefix_len = 0;
2791 int before_tmp = before, after_tmp = after;
2792 struct scan_info *scan_info = scan_info_array + i;
2793 struct rpn_char_map_info rcmi;
2795 rpn_char_map_prepare (zh->reg, reg_id, &rcmi);
2797 scan_info->before = before;
2798 scan_info->after = after;
2799 scan_info->odr = stream;
2801 scan_info->list = (struct scan_info_entry *)
2802 odr_malloc (stream, (before+after) * sizeof(*scan_info->list));
2803 for (j = 0; j<before+after; j++)
2804 scan_info->list[j].term = NULL;
2806 prefix_len += key_SU_encode (ords[i], termz + prefix_len);
2807 termz[prefix_len++] = reg_id;
2808 termz[prefix_len] = 0;
2809 strcpy (scan_info->prefix, termz);
2811 if (trans_scan_term (zh, zapt, termz+prefix_len, reg_id))
2814 dict_scan (zh->reg->dict, termz, &before_tmp, &after_tmp,
2815 scan_info, scan_handle);
2817 glist = (ZebraScanEntry *)
2818 odr_malloc (stream, (before+after)*sizeof(*glist));
2820 /* consider terms after main term */
2821 for (i = 0; i < ord_no; i++)
2825 for (i = 0; i<after; i++)
2828 const char *mterm = NULL;
2832 for (j = 0; j < ord_no; j++)
2834 if (ptr[j] < before+after &&
2835 (tst=scan_info_array[j].list[ptr[j]].term) &&
2836 (!mterm || strcmp (tst, mterm) < 0))
2844 scan_term_untrans (zh, stream->mem, reg_id,
2845 &glist[i+before].term, mterm);
2846 rset = rset_trunc (zh, &scan_info_array[j0].list[ptr[j0]].isam_p, 1,
2847 glist[i+before].term, strlen(glist[i+before].term),
2848 NULL, 0, zapt->term->which);
2851 for (j = j0+1; j<ord_no; j++)
2853 if (ptr[j] < before+after &&
2854 (tst=scan_info_array[j].list[ptr[j]].term) &&
2855 !strcmp (tst, mterm))
2857 rset_bool_parms bool_parms;
2861 rset_trunc (zh, &scan_info_array[j].list[ptr[j]].isam_p, 1,
2862 glist[i+before].term,
2863 strlen(glist[i+before].term), NULL, 0,
2866 bool_parms.key_size = sizeof(struct it_key);
2867 bool_parms.cmp = key_compare_it;
2868 bool_parms.rset_l = rset;
2869 bool_parms.rset_r = rset2;
2871 rset = rset_create (rset_kind_or, &bool_parms);
2876 count_set (rset, &glist[i+before].occurrences);
2881 *num_entries -= (after-i);
2885 /* consider terms before main term */
2886 for (i = 0; i<ord_no; i++)
2889 for (i = 0; i<before; i++)
2892 const char *mterm = NULL;
2896 for (j = 0; j <ord_no; j++)
2898 if (ptr[j] < before &&
2899 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2900 (!mterm || strcmp (tst, mterm) > 0))
2909 scan_term_untrans (zh, stream->mem, reg_id,
2910 &glist[before-1-i].term, mterm);
2913 (zh, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1,
2914 glist[before-1-i].term, strlen(glist[before-1-i].term),
2915 NULL, 0, zapt->term->which);
2919 for (j = j0+1; j<ord_no; j++)
2921 if (ptr[j] < before &&
2922 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2923 !strcmp (tst, mterm))
2925 rset_bool_parms bool_parms;
2928 rset2 = rset_trunc (zh,
2929 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1,
2930 glist[before-1-i].term,
2931 strlen(glist[before-1-i].term), NULL, 0,
2934 bool_parms.key_size = sizeof(struct it_key);
2935 bool_parms.cmp = key_compare_it;
2936 bool_parms.rset_l = rset;
2937 bool_parms.rset_r = rset2;
2939 rset = rset_create (rset_kind_or, &bool_parms);
2944 count_set (rset, &glist[before-1-i].occurrences);
2954 *list = glist + i; /* list is set to first 'real' entry */
2956 logf (LOG_DEBUG, "position = %d, num_entries = %d",
2957 *position, *num_entries);
2959 logf (LOG_DEBUG, "scan error: %d", zh->errCode);