1 /* $Id: zrpn.c,v 1.123 2002-09-18 21:01:15 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)
86 num_attributes = src->zapt->attributes->num_attributes;
88 num_attributes = src->zapt->num_attributes;
90 while (src->major < num_attributes)
92 Z_AttributeElement *element;
95 element = src->zapt->attributes->attributes[src->major];
97 element = src->zapt->attributeList[src->major];
99 if (src->type == *element->attributeType)
101 switch (element->which)
103 case Z_AttributeValue_numeric:
105 if (element->attributeSet && attributeSetP)
109 attrset = oid_getentbyoid (element->attributeSet);
110 *attributeSetP = attrset->value;
112 return *element->value.numeric;
114 case Z_AttributeValue_complex:
115 if (src->minor >= element->value.complex->num_list)
117 if (element->attributeSet && attributeSetP)
121 attrset = oid_getentbyoid (element->attributeSet);
122 *attributeSetP = attrset->value;
124 if (element->value.complex->list[src->minor]->which ==
125 Z_StringOrNumeric_numeric)
129 *element->value.complex->list[src->minor-1]->u.numeric;
131 else if (element->value.complex->list[src->minor]->which ==
132 Z_StringOrNumeric_string)
138 element->value.complex->list[src->minor-1]->u.string;
152 static int attr_find (AttrType *src, oid_value *attributeSetP)
154 return attr_find_ex (src, attributeSetP, 0);
157 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
180 static void term_untrans (ZebraHandle zh, int reg_type,
181 char *dst, const char *src)
185 const char *cp = zebra_maps_output (zh->reg->zebra_maps,
196 static void add_isam_p (const char *name, const char *info,
199 if (p->isam_p_indx == p->isam_p_size)
201 ISAMS_P *new_isam_p_buf;
205 p->isam_p_size = 2*p->isam_p_size + 100;
206 new_isam_p_buf = (ISAMS_P *) xmalloc (sizeof(*new_isam_p_buf) *
210 memcpy (new_isam_p_buf, p->isam_p_buf,
211 p->isam_p_indx * sizeof(*p->isam_p_buf));
212 xfree (p->isam_p_buf);
214 p->isam_p_buf = new_isam_p_buf;
217 new_term_no = (int *) xmalloc (sizeof(*new_term_no) *
221 memcpy (new_term_no, p->isam_p_buf,
222 p->isam_p_indx * sizeof(*p->term_no));
225 p->term_no = new_term_no;
228 assert (*info == sizeof(*p->isam_p_buf));
229 memcpy (p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
238 int len = key_SU_decode (&su_code, name);
240 term_untrans (p->zh, p->reg_type, term_tmp, name+len+1);
241 logf (LOG_LOG, "grep: %d %c %s", su_code, name[len], term_tmp);
242 zebraExplain_lookup_ord (p->zh->reg->zei,
243 su_code, &db, &set, &use);
244 logf (LOG_LOG, "grep: set=%d use=%d db=%s", set, use, db);
246 resultSetAddTerm (p->zh, p->termset, name[len], db,
253 static int grep_handle (char *name, const char *info, void *p)
255 add_isam_p (name, info, (struct grep_info *) p);
259 static int term_pre (ZebraMaps zebra_maps, int reg_type, const char **src,
260 const char *ct1, const char *ct2)
262 const char *s1, *s0 = *src;
265 /* skip white space */
268 if (ct1 && strchr (ct1, *s0))
270 if (ct2 && strchr (ct2, *s0))
273 map = zebra_maps_input (zebra_maps, reg_type, &s1, strlen(s1));
274 if (**map != *CHR_SPACE)
282 #define REGEX_CHARS " []()|.*+!"
284 /* term_100: handle term, where trunc=none (no operators at all) */
285 static int term_100 (ZebraMaps zebra_maps, int reg_type,
286 const char **src, char *dst, int space_split,
294 const char *space_start = 0;
295 const char *space_end = 0;
297 if (!term_pre (zebra_maps, reg_type, src, NULL, NULL))
303 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
306 if (**map == *CHR_SPACE)
309 else /* complete subfield only. */
311 if (**map == *CHR_SPACE)
312 { /* save space mapping for later .. */
317 else if (space_start)
318 { /* reload last space */
319 while (space_start < space_end)
321 if (strchr (REGEX_CHARS, *space_start))
323 dst_term[j++] = *space_start;
324 dst[i++] = *space_start++;
327 space_start = space_end = 0;
330 /* add non-space char */
333 if (strchr(REGEX_CHARS, *s1))
345 /* term_101: handle term, where trunc=Process # */
346 static int term_101 (ZebraMaps zebra_maps, int reg_type,
347 const char **src, char *dst, int space_split,
355 if (!term_pre (zebra_maps, reg_type, src, "#", "#"))
364 dst_term[j++] = *s0++;
369 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
370 if (space_split && **map == *CHR_SPACE)
374 if (strchr(REGEX_CHARS, *s1))
382 dst_term[j++] = '\0';
387 /* term_103: handle term, where trunc=re-2 (regular expressions) */
388 static int term_103 (ZebraMaps zebra_maps, int reg_type, const char **src,
389 char *dst, int *errors, int space_split,
397 if (!term_pre (zebra_maps, reg_type, src, "^\\()[].*+?|", "("))
400 if (errors && *s0 == '+' && s0[1] && s0[2] == '+' && s0[3] &&
403 *errors = s0[1] - '0';
410 if (strchr ("^\\()[].*+?|-", *s0))
418 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
419 if (**map == *CHR_SPACE)
423 if (strchr(REGEX_CHARS, *s1))
436 /* term_103: handle term, where trunc=re-1 (regular expressions) */
437 static int term_102 (ZebraMaps zebra_maps, int reg_type, const char **src,
438 char *dst, int space_split, char *dst_term)
440 return term_103 (zebra_maps, reg_type, src, dst, NULL, space_split,
445 /* term_104: handle term, where trunc=Process # and ! */
446 static int term_104 (ZebraMaps zebra_maps, int reg_type,
447 const char **src, char *dst, int space_split,
455 if (!term_pre (zebra_maps, reg_type, src, "#!", "#!"))
464 dst_term[j++] = *s0++;
469 dst_term[j++] = *s0++;
473 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
474 if (space_split && **map == *CHR_SPACE)
478 if (strchr(REGEX_CHARS, *s1))
486 dst_term[j++] = '\0';
491 /* term_105/106: handle term, where trunc=Process * and ! and right trunc */
492 static int term_105 (ZebraMaps zebra_maps, int reg_type,
493 const char **src, char *dst, int space_split,
494 char *dst_term, int right_truncate)
501 if (!term_pre (zebra_maps, reg_type, src, "*!", "*!"))
510 dst_term[j++] = *s0++;
515 dst_term[j++] = *s0++;
519 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
520 if (space_split && **map == *CHR_SPACE)
524 if (strchr(REGEX_CHARS, *s1))
538 dst_term[j++] = '\0';
544 /* gen_regular_rel - generate regular expression from relation
545 * val: border value (inclusive)
546 * islt: 1 if <=; 0 if >=.
548 static void gen_regular_rel (char *dst, int val, int islt)
555 logf (LOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
559 strcpy (dst, "(-[0-9]+|(");
567 strcpy (dst, "([0-9]+|-(");
568 dst_p = strlen (dst);
578 dst_p = strlen (dst);
579 sprintf (numstr, "%d", val);
580 for (w = strlen(numstr); --w >= 0; pos++)
599 strcpy (dst + dst_p, numstr);
600 dst_p = strlen(dst) - pos - 1;
628 for (i = 0; i<pos; i++)
641 /* match everything less than 10^(pos-1) */
643 for (i=1; i<pos; i++)
644 strcat (dst, "[0-9]?");
648 /* match everything greater than 10^pos */
649 for (i = 0; i <= pos; i++)
650 strcat (dst, "[0-9]");
651 strcat (dst, "[0-9]*");
656 void string_rel_add_char (char **term_p, const char *src, int *indx)
658 if (src[*indx] == '\\')
659 *(*term_p)++ = src[(*indx)++];
660 *(*term_p)++ = src[(*indx)++];
664 * > abc ([b-].*|a[c-].*|ab[d-].*|abc.+)
665 * ([^-a].*|a[^-b].*ab[^-c].*|abc.+)
666 * >= abc ([b-].*|a[c-].*|ab[c-].*)
667 * ([^-a].*|a[^-b].*|ab[c-].*)
668 * < abc ([-0].*|a[-a].*|ab[-b].*)
669 * ([^a-].*|a[^b-].*|ab[^c-].*)
670 * <= abc ([-0].*|a[-a].*|ab[-b].*|abc)
671 * ([^a-].*|a[^b-].*|ab[^c-].*|abc)
673 static int string_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
674 const char **term_sub, char *term_dict,
675 oid_value attributeSet,
676 int reg_type, int space_split, char *term_dst)
681 char *term_tmp = term_dict + strlen(term_dict);
682 char term_component[256];
684 attr_init (&relation, zapt, 2);
685 relation_value = attr_find (&relation, NULL);
687 logf (LOG_DEBUG, "string relation value=%d", relation_value);
688 switch (relation_value)
691 if (!term_100 (zh->reg->zebra_maps, reg_type,
692 term_sub, term_component,
693 space_split, term_dst))
695 logf (LOG_DEBUG, "Relation <");
698 for (i = 0; term_component[i]; )
705 string_rel_add_char (&term_tmp, term_component, &j);
710 string_rel_add_char (&term_tmp, term_component, &i);
721 if (!term_100 (zh->reg->zebra_maps, reg_type,
722 term_sub, term_component,
723 space_split, term_dst))
725 logf (LOG_DEBUG, "Relation <=");
728 for (i = 0; term_component[i]; )
733 string_rel_add_char (&term_tmp, term_component, &j);
737 string_rel_add_char (&term_tmp, term_component, &i);
746 for (i = 0; term_component[i]; )
747 string_rel_add_char (&term_tmp, term_component, &i);
752 if (!term_100 (zh->reg->zebra_maps, reg_type,
753 term_sub, term_component, space_split, term_dst))
755 logf (LOG_DEBUG, "Relation >");
758 for (i = 0; term_component[i];)
763 string_rel_add_char (&term_tmp, term_component, &j);
768 string_rel_add_char (&term_tmp, term_component, &i);
776 for (i = 0; term_component[i];)
777 string_rel_add_char (&term_tmp, term_component, &i);
784 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub,
785 term_component, space_split, term_dst))
787 logf (LOG_DEBUG, "Relation >=");
790 for (i = 0; term_component[i];)
797 string_rel_add_char (&term_tmp, term_component, &j);
800 if (term_component[i+1])
804 string_rel_add_char (&term_tmp, term_component, &i);
808 string_rel_add_char (&term_tmp, term_component, &i);
820 logf (LOG_DEBUG, "Relation =");
821 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub,
822 term_component, space_split, term_dst))
824 strcat (term_tmp, "(");
825 strcat (term_tmp, term_component);
826 strcat (term_tmp, ")");
831 static int string_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
832 const char **term_sub,
833 oid_value attributeSet, NMEM stream,
834 struct grep_info *grep_info,
835 int reg_type, int complete_flag,
836 int num_bases, char **basenames,
837 char *term_dst, int xpath_use);
839 static RSET term_trunc (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
840 const char **term_sub,
841 oid_value attributeSet, NMEM stream,
842 struct grep_info *grep_info,
843 int reg_type, int complete_flag,
844 int num_bases, char **basenames,
846 const char *rank_type, int xpath_use)
849 grep_info->isam_p_indx = 0;
850 r = string_term (zh, zapt, term_sub, attributeSet, stream, grep_info,
851 reg_type, complete_flag, num_bases, basenames,
852 term_dst, xpath_use);
855 logf (LOG_DEBUG, "term: %s", term_dst);
856 return rset_trunc (zh, grep_info->isam_p_buf,
857 grep_info->isam_p_indx, term_dst,
858 strlen(term_dst), rank_type, 1 /* preserve pos */,
863 static int string_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
864 const char **term_sub,
865 oid_value attributeSet, NMEM stream,
866 struct grep_info *grep_info,
867 int reg_type, int complete_flag,
868 int num_bases, char **basenames,
869 char *term_dst, int xpath_use)
871 char term_dict[2*IT_MAX_WORD+4000];
874 int truncation_value;
877 const char *use_string = 0;
878 oid_value curAttributeSet = attributeSet;
880 struct rpn_char_map_info rcmi;
881 int space_split = complete_flag ? 0 : 1;
883 rpn_char_map_prepare (zh->reg, reg_type, &rcmi);
884 attr_init (&use, zapt, 1);
885 use_value = attr_find_ex (&use, &curAttributeSet, &use_string);
886 logf (LOG_DEBUG, "string_term, use value %d", use_value);
887 attr_init (&truncation, zapt, 5);
888 truncation_value = attr_find (&truncation, NULL);
889 logf (LOG_DEBUG, "truncation value %d", truncation_value);
891 if (use_value == -1) /* no attribute - assumy "any" */
893 for (base_no = 0; base_no < num_bases; base_no++)
896 data1_local_attribute id_xpath_attr;
897 data1_local_attribute *local_attr;
898 int max_pos, prefix_len = 0;
902 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
904 zh->errCode = 109; /* Database unavailable */
905 zh->errString = basenames[base_no];
908 if (use_value == -2) /* string attribute (assume IDXPATH/any) */
910 use_value = xpath_use;
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;
916 else if (curAttributeSet == VAL_IDXPATH)
918 attp.local_attributes = &id_xpath_attr;
919 attp.attset_ordinal = VAL_IDXPATH;
920 id_xpath_attr.next = 0;
921 id_xpath_attr.local = use_value;
925 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
927 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
928 curAttributeSet, use_value, r);
931 /* set was found, but value wasn't defined */
933 sprintf (val_str, "%d (1)", use_value);
935 zh->errString = nmem_strdup (stream, val_str);
940 struct oident oident;
942 oident.proto = PROTO_Z3950;
943 oident.oclass = CLASS_ATTSET;
944 oident.value = curAttributeSet;
945 oid_ent_to_oid (&oident, oid);
948 zh->errString = nmem_strdup (stream, oident.desc);
953 for (local_attr = attp.local_attributes; local_attr;
954 local_attr = local_attr->next)
960 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
965 term_dict[prefix_len++] = '|';
967 term_dict[prefix_len++] = '(';
969 ord_len = key_SU_encode (ord, ord_buf);
970 for (i = 0; i<ord_len; i++)
972 term_dict[prefix_len++] = 1;
973 term_dict[prefix_len++] = ord_buf[i];
979 sprintf (val_str, "%d (2)", use_value);
981 zh->errString = nmem_strdup (stream, val_str);
984 term_dict[prefix_len++] = ')';
985 term_dict[prefix_len++] = 1;
986 term_dict[prefix_len++] = reg_type;
987 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
988 term_dict[prefix_len] = '\0';
990 switch (truncation_value)
992 case -1: /* not specified */
993 case 100: /* do not truncate */
994 if (!string_relation (zh, zapt, &termp, term_dict,
996 reg_type, space_split, term_dst))
998 logf (LOG_LOG, "dict_lookup_grep: %s", term_dict+prefix_len);
999 r = dict_lookup_grep (zh->reg->dict, term_dict, 0,
1000 grep_info, &max_pos, 0, grep_handle);
1002 logf (LOG_WARN, "dict_lookup_grep fail %d", r);
1004 case 1: /* right truncation */
1005 term_dict[j++] = '(';
1006 if (!term_100 (zh->reg->zebra_maps, reg_type,
1007 &termp, term_dict + j, space_split, term_dst))
1009 strcat (term_dict, ".*)");
1010 dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1011 &max_pos, 0, grep_handle);
1013 case 2: /* keft truncation */
1014 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1015 if (!term_100 (zh->reg->zebra_maps, reg_type,
1016 &termp, term_dict + j, space_split, term_dst))
1018 strcat (term_dict, ")");
1019 dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1020 &max_pos, 0, grep_handle);
1022 case 3: /* left&right truncation */
1023 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1024 if (!term_100 (zh->reg->zebra_maps, reg_type,
1025 &termp, term_dict + j, space_split, term_dst))
1027 strcat (term_dict, ".*)");
1028 dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1029 &max_pos, 0, grep_handle);
1033 case 101: /* process # in term */
1034 term_dict[j++] = '(';
1035 if (!term_101 (zh->reg->zebra_maps, reg_type,
1036 &termp, term_dict + j, space_split, term_dst))
1038 strcat (term_dict, ")");
1039 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1040 &max_pos, 0, grep_handle);
1042 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d", r);
1044 case 102: /* Regexp-1 */
1045 term_dict[j++] = '(';
1046 if (!term_102 (zh->reg->zebra_maps, reg_type,
1047 &termp, term_dict + j, space_split, term_dst))
1049 strcat (term_dict, ")");
1050 logf (LOG_DEBUG, "Regexp-1 tolerance=%d", r);
1051 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1052 &max_pos, 0, grep_handle);
1054 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
1057 case 103: /* Regexp-2 */
1059 term_dict[j++] = '(';
1060 if (!term_103 (zh->reg->zebra_maps, reg_type,
1061 &termp, term_dict + j, &r, space_split, term_dst))
1063 strcat (term_dict, ")");
1064 logf (LOG_DEBUG, "Regexp-2 tolerance=%d", r);
1065 r = dict_lookup_grep (zh->reg->dict, term_dict, r, grep_info,
1066 &max_pos, 2, grep_handle);
1068 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
1071 case 104: /* process # and ! in term */
1072 term_dict[j++] = '(';
1073 if (!term_104 (zh->reg->zebra_maps, reg_type,
1074 &termp, term_dict + j, space_split, term_dst))
1076 strcat (term_dict, ")");
1077 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1078 &max_pos, 0, grep_handle);
1080 logf (LOG_WARN, "dict_lookup_grep err, trunc=#/!: %d", r);
1082 case 105: /* process * and ! in term */
1083 term_dict[j++] = '(';
1084 if (!term_105 (zh->reg->zebra_maps, reg_type,
1085 &termp, term_dict + j, space_split, term_dst, 1))
1087 strcat (term_dict, ")");
1088 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1089 &max_pos, 0, grep_handle);
1091 logf (LOG_WARN, "dict_lookup_grep err, trunc=*/!: %d", r);
1093 case 106: /* process * and ! in term */
1094 term_dict[j++] = '(';
1095 if (!term_105 (zh->reg->zebra_maps, reg_type,
1096 &termp, term_dict + j, space_split, term_dst, 0))
1098 strcat (term_dict, ")");
1099 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1100 &max_pos, 0, grep_handle);
1102 logf (LOG_WARN, "dict_lookup_grep err, trunc=*/!: %d", r);
1107 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1112 static int trans_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1116 Z_Term *term = zapt->term;
1118 switch (term->which)
1120 case Z_Term_general:
1121 if (zh->iconv_to_utf8 != 0)
1123 char *inbuf = term->u.general->buf;
1124 size_t inleft = term->u.general->len;
1125 char *outbuf = termz;
1126 size_t outleft = IT_MAX_WORD-1;
1129 yaz_log (LOG_DEBUG, "converting general from ISO-8859-1");
1130 ret = yaz_iconv(zh->iconv_to_utf8, &inbuf, &inleft,
1132 if (ret == (size_t)(-1))
1134 ret = yaz_iconv(zh->iconv_to_utf8, 0, 0, 0, 0);
1141 sizez = term->u.general->len;
1142 if (sizez > IT_MAX_WORD-1)
1143 sizez = IT_MAX_WORD-1;
1144 memcpy (termz, term->u.general->buf, sizez);
1145 termz[sizez] = '\0';
1147 case Z_Term_characterString:
1148 sizez = strlen(term->u.characterString);
1149 if (sizez > IT_MAX_WORD-1)
1150 sizez = IT_MAX_WORD-1;
1151 memcpy (termz, term->u.characterString, sizez);
1152 termz[sizez] = '\0';
1160 static void trans_scan_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1161 char *termz, int reg_type)
1163 Z_Term *term = zapt->term;
1165 const char *cp = (const char *) term->u.general->buf;
1166 const char *cp_end = cp + term->u.general->len;
1169 const char *space_map = NULL;
1172 while ((len = (cp_end - cp)) > 0)
1174 map = zebra_maps_input (zh->reg->zebra_maps, reg_type, &cp, len);
1175 if (**map == *CHR_SPACE)
1180 for (src = space_map; *src; src++)
1183 for (src = *map; *src; src++)
1190 static RSET rpn_prox (ZebraHandle zh, RSET *rset, int rset_no,
1191 int ordered, int exclusion, int relation, int distance)
1196 struct it_key **buf;
1198 char prox_term[1024];
1199 int length_prox_term = 0;
1200 int min_nn = 10000000;
1202 int term_type = Z_Term_characterString;
1203 const char *flags = NULL;
1205 rsfd = (RSFD *) xmalloc (sizeof(*rsfd)*rset_no);
1206 more = (int *) xmalloc (sizeof(*more)*rset_no);
1207 buf = (struct it_key **) xmalloc (sizeof(*buf)*rset_no);
1210 for (i = 0; i<rset_no; i++)
1213 for (j = 0; j<rset[i]->no_rset_terms; j++)
1215 const char *nflags = rset[i]->rset_terms[j]->flags;
1216 char *term = rset[i]->rset_terms[j]->name;
1217 int lterm = strlen(term);
1218 if (lterm + length_prox_term < sizeof(prox_term)-1)
1220 if (length_prox_term)
1221 prox_term[length_prox_term++] = ' ';
1222 strcpy (prox_term + length_prox_term, term);
1223 length_prox_term += lterm;
1225 if (min_nn > rset[i]->rset_terms[j]->nn)
1226 min_nn = rset[i]->rset_terms[j]->nn;
1228 term_type = rset[i]->rset_terms[j]->type;
1230 /* only if all term types are of type characterString .. */
1231 /* the resulting term is of that type */
1232 if (term_type != Z_Term_characterString)
1233 term_type = Z_Term_general;
1236 for (i = 0; i<rset_no; i++)
1241 for (i = 0; i<rset_no; i++)
1243 buf[i] = (struct it_key *) xmalloc (sizeof(**buf));
1244 rsfd[i] = rset_open (rset[i], RSETF_READ);
1245 if (!(more[i] = rset_read (rset[i], rsfd[i], buf[i], &term_index)))
1250 /* at least one is empty ... return null set */
1251 rset_null_parms parms;
1253 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1255 parms.rset_term->nn = 0;
1256 result = rset_create (rset_kind_null, &parms);
1258 else if (ordered && relation == 3 && exclusion == 0 && distance == 1)
1260 /* special proximity case = phrase search ... */
1261 rset_temp_parms parms;
1264 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1266 parms.rset_term->nn = min_nn;
1267 parms.cmp = key_compare_it;
1268 parms.key_size = sizeof (struct it_key);
1269 parms.temp_path = res_get (zh->res, "setTmpDir");
1270 result = rset_create (rset_kind_temp, &parms);
1271 rsfd_result = rset_open (result, RSETF_WRITE);
1275 for (i = 1; i<rset_no; i++)
1284 cmp = key_compare_it (buf[i], buf[i-1]);
1287 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1288 buf[i-1], &term_index);
1293 if (buf[i-1]->seqno+1 != buf[i]->seqno)
1295 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1296 buf[i-1], &term_index);
1302 more[i] = rset_read (rset[i], rsfd[i], buf[i],
1309 rset_write (result, rsfd_result, buf[0]);
1310 more[0] = rset_read (*rset, *rsfd, *buf, &term_index);
1313 rset_close (result, rsfd_result);
1315 else if (rset_no == 2)
1317 /* generic proximity case (two input sets only) ... */
1318 rset_temp_parms parms;
1321 logf (LOG_LOG, "generic prox, dist = %d, relation = %d, ordered =%d, exclusion=%d",
1322 distance, relation, ordered, exclusion);
1323 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1325 parms.rset_term->nn = min_nn;
1326 parms.cmp = key_compare_it;
1327 parms.key_size = sizeof (struct it_key);
1328 parms.temp_path = res_get (zh->res, "setTmpDir");
1329 result = rset_create (rset_kind_temp, &parms);
1330 rsfd_result = rset_open (result, RSETF_WRITE);
1332 while (more[0] && more[1])
1334 int cmp = key_compare_it (buf[0], buf[1]);
1336 more[0] = rset_read (rset[0], rsfd[0], buf[0], &term_index);
1338 more[1] = rset_read (rset[1], rsfd[1], buf[1], &term_index);
1341 int sysno = buf[0]->sysno;
1345 seqno[n++] = buf[0]->seqno;
1346 while ((more[0] = rset_read (rset[0], rsfd[0], buf[0],
1348 sysno == buf[0]->sysno)
1350 seqno[n++] = buf[0]->seqno;
1353 for (i = 0; i<n; i++)
1355 int diff = buf[1]->seqno - seqno[i];
1356 int excl = exclusion;
1357 if (!ordered && diff < 0)
1362 if (diff < distance && 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)
1388 rset_write (result, rsfd_result, buf[1]);
1392 } while ((more[1] = rset_read (rset[1], rsfd[1], buf[1],
1394 sysno == buf[1]->sysno);
1397 rset_close (result, rsfd_result);
1401 rset_null_parms parms;
1403 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1405 parms.rset_term->nn = 0;
1406 result = rset_create (rset_kind_null, &parms);
1408 for (i = 0; i<rset_no; i++)
1411 rset_close (rset[i], rsfd[i]);
1421 char *normalize_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1422 const char *termz, NMEM stream, unsigned reg_id)
1425 AttrType truncation;
1426 int truncation_value;
1429 attr_init (&truncation, zapt, 5);
1430 truncation_value = attr_find (&truncation, NULL);
1432 switch (truncation_value)
1452 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, ex_list,
1453 termz, strlen(termz));
1455 return nmem_strdup(stream, termz);
1458 char *buf = (char*) nmem_malloc (stream, wrbuf_len(wrbuf)+1);
1459 memcpy (buf, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1460 buf[wrbuf_len(wrbuf)] = '\0';
1465 static void grep_info_delete (struct grep_info *grep_info)
1468 xfree(grep_info->term_no);
1470 xfree (grep_info->isam_p_buf);
1473 static int grep_info_prepare (ZebraHandle zh,
1474 Z_AttributesPlusTerm *zapt,
1475 struct grep_info *grep_info,
1480 int termset_value_numeric;
1481 const char *termset_value_string;
1484 grep_info->term_no = 0;
1486 grep_info->isam_p_size = 0;
1487 grep_info->isam_p_buf = NULL;
1489 grep_info->reg_type = reg_type;
1490 grep_info->termset = 0;
1494 attr_init (&termset, zapt, 8);
1495 termset_value_numeric =
1496 attr_find_ex (&termset, NULL, &termset_value_string);
1497 if (termset_value_numeric != -1)
1500 const char *termset_name = 0;
1501 if (termset_value_numeric != -2)
1504 sprintf (resname, "%d", termset_value_numeric);
1505 termset_name = resname;
1508 termset_name = termset_value_string;
1509 logf (LOG_LOG, "creating termset set %s", termset_name);
1510 grep_info->termset = resultSetAdd (zh, termset_name, 1);
1511 if (!grep_info->termset)
1514 zh->errString = nmem_strdup (stream, termset_name);
1522 static RSET rpn_search_APT_phrase (ZebraHandle zh,
1523 Z_AttributesPlusTerm *zapt,
1524 const char *termz_org,
1525 oid_value attributeSet,
1527 int reg_type, int complete_flag,
1528 const char *rank_type, int xpath_use,
1529 int num_bases, char **basenames)
1531 char term_dst[IT_MAX_WORD+1];
1532 RSET rset[60], result;
1534 struct grep_info grep_info;
1535 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1536 const char *termp = termz;
1539 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1543 logf (LOG_DEBUG, "APT_phrase termp=%s", termp);
1544 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1546 reg_type, complete_flag,
1547 num_bases, basenames,
1548 term_dst, rank_type,
1552 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1555 grep_info_delete (&grep_info);
1558 rset_null_parms parms;
1560 parms.rset_term = rset_term_create (termz, -1, rank_type,
1562 return rset_create (rset_kind_null, &parms);
1564 else if (rset_no == 1)
1566 result = rpn_prox (zh, rset, rset_no, 1, 0, 3, 1);
1567 for (i = 0; i<rset_no; i++)
1568 rset_delete (rset[i]);
1572 static RSET rpn_search_APT_or_list (ZebraHandle zh,
1573 Z_AttributesPlusTerm *zapt,
1574 const char *termz_org,
1575 oid_value attributeSet,
1577 int reg_type, int complete_flag,
1578 const char *rank_type,
1580 int num_bases, char **basenames)
1582 char term_dst[IT_MAX_WORD+1];
1583 RSET rset[60], result;
1585 struct grep_info grep_info;
1586 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1587 const char *termp = termz;
1589 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1593 logf (LOG_DEBUG, "APT_or_list termp=%s", termp);
1594 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1596 reg_type, complete_flag,
1597 num_bases, basenames,
1598 term_dst, rank_type,
1602 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1605 grep_info_delete (&grep_info);
1608 rset_null_parms parms;
1610 parms.rset_term = rset_term_create (termz, -1, rank_type,
1612 return rset_create (rset_kind_null, &parms);
1615 for (i = 1; i<rset_no; i++)
1617 rset_bool_parms bool_parms;
1619 bool_parms.rset_l = result;
1620 bool_parms.rset_r = rset[i];
1621 bool_parms.key_size = sizeof(struct it_key);
1622 bool_parms.cmp = key_compare_it;
1623 result = rset_create (rset_kind_or, &bool_parms);
1628 static RSET rpn_search_APT_and_list (ZebraHandle zh,
1629 Z_AttributesPlusTerm *zapt,
1630 const char *termz_org,
1631 oid_value attributeSet,
1633 int reg_type, int complete_flag,
1634 const char *rank_type,
1636 int num_bases, char **basenames)
1638 char term_dst[IT_MAX_WORD+1];
1639 RSET rset[60], result;
1641 struct grep_info grep_info;
1642 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1643 const char *termp = termz;
1645 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1649 logf (LOG_DEBUG, "APT_and_list termp=%s", termp);
1650 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1652 reg_type, complete_flag,
1653 num_bases, basenames,
1654 term_dst, rank_type,
1658 assert (rset[rset_no]);
1659 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1662 grep_info_delete (&grep_info);
1665 rset_null_parms parms;
1667 parms.rset_term = rset_term_create (termz, -1, rank_type,
1669 return rset_create (rset_kind_null, &parms);
1672 for (i = 1; i<rset_no; i++)
1674 rset_bool_parms bool_parms;
1676 bool_parms.rset_l = result;
1677 bool_parms.rset_r = rset[i];
1678 bool_parms.key_size = sizeof(struct it_key);
1679 bool_parms.cmp = key_compare_it;
1680 result = rset_create (rset_kind_and, &bool_parms);
1685 static int numeric_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1686 const char **term_sub,
1688 oid_value attributeSet,
1689 struct grep_info *grep_info,
1698 char *term_tmp = term_dict + strlen(term_dict);
1700 attr_init (&relation, zapt, 2);
1701 relation_value = attr_find (&relation, NULL);
1703 logf (LOG_DEBUG, "numeric relation value=%d", relation_value);
1705 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub, term_tmp, 1,
1708 term_value = atoi (term_tmp);
1709 switch (relation_value)
1712 logf (LOG_DEBUG, "Relation <");
1713 gen_regular_rel (term_tmp, term_value-1, 1);
1716 logf (LOG_DEBUG, "Relation <=");
1717 gen_regular_rel (term_tmp, term_value, 1);
1720 logf (LOG_DEBUG, "Relation >=");
1721 gen_regular_rel (term_tmp, term_value, 0);
1724 logf (LOG_DEBUG, "Relation >");
1725 gen_regular_rel (term_tmp, term_value+1, 0);
1729 logf (LOG_DEBUG, "Relation =");
1730 sprintf (term_tmp, "(0*%d)", term_value);
1732 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_tmp);
1733 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info, max_pos,
1736 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1737 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1741 static int numeric_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1742 const char **term_sub,
1743 oid_value attributeSet, struct grep_info *grep_info,
1744 int reg_type, int complete_flag,
1745 int num_bases, char **basenames,
1748 char term_dict[2*IT_MAX_WORD+2];
1752 oid_value curAttributeSet = attributeSet;
1754 struct rpn_char_map_info rcmi;
1756 rpn_char_map_prepare (zh->reg, reg_type, &rcmi);
1757 attr_init (&use, zapt, 1);
1758 use_value = attr_find (&use, &curAttributeSet);
1759 logf (LOG_DEBUG, "numeric_term, use value %d", use_value);
1761 if (use_value == -1)
1764 for (base_no = 0; base_no < num_bases; base_no++)
1767 data1_local_attribute *local_attr;
1768 int max_pos, prefix_len = 0;
1771 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
1773 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1774 curAttributeSet, use_value, r);
1781 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
1783 zh->errCode = 109; /* Database unavailable */
1784 zh->errString = basenames[base_no];
1787 for (local_attr = attp.local_attributes; local_attr;
1788 local_attr = local_attr->next)
1794 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
1799 term_dict[prefix_len++] = '|';
1801 term_dict[prefix_len++] = '(';
1803 ord_len = key_SU_encode (ord, ord_buf);
1804 for (i = 0; i<ord_len; i++)
1806 term_dict[prefix_len++] = 1;
1807 term_dict[prefix_len++] = ord_buf[i];
1815 term_dict[prefix_len++] = ')';
1816 term_dict[prefix_len++] = 1;
1817 term_dict[prefix_len++] = reg_type;
1818 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1819 term_dict[prefix_len] = '\0';
1820 if (!numeric_relation (zh, zapt, &termp, term_dict,
1821 attributeSet, grep_info, &max_pos, reg_type,
1826 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1830 static RSET rpn_search_APT_numeric (ZebraHandle zh,
1831 Z_AttributesPlusTerm *zapt,
1833 oid_value attributeSet,
1835 int reg_type, int complete_flag,
1836 const char *rank_type,
1837 int num_bases, char **basenames)
1839 char term_dst[IT_MAX_WORD+1];
1840 const char *termp = termz;
1841 RSET rset[60], result;
1842 int i, r, rset_no = 0;
1843 struct grep_info grep_info;
1845 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1849 logf (LOG_DEBUG, "APT_numeric termp=%s", termp);
1850 grep_info.isam_p_indx = 0;
1851 r = numeric_term (zh, zapt, &termp, attributeSet, &grep_info,
1852 reg_type, complete_flag, num_bases, basenames,
1856 logf (LOG_DEBUG, "term: %s", term_dst);
1857 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1858 grep_info.isam_p_indx, term_dst,
1859 strlen(term_dst), rank_type,
1860 0 /* preserve position */,
1862 assert (rset[rset_no]);
1863 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1866 grep_info_delete (&grep_info);
1869 rset_null_parms parms;
1871 parms.rset_term = rset_term_create (term_dst, -1, rank_type,
1873 return rset_create (rset_kind_null, &parms);
1876 for (i = 1; i<rset_no; i++)
1878 rset_bool_parms bool_parms;
1880 bool_parms.rset_l = result;
1881 bool_parms.rset_r = rset[i];
1882 bool_parms.key_size = sizeof(struct it_key);
1883 bool_parms.cmp = key_compare_it;
1884 result = rset_create (rset_kind_and, &bool_parms);
1889 static RSET rpn_search_APT_local (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1891 oid_value attributeSet,
1893 const char *rank_type)
1898 rset_temp_parms parms;
1900 parms.rset_term = rset_term_create (termz, -1, rank_type,
1902 parms.cmp = key_compare_it;
1903 parms.key_size = sizeof (struct it_key);
1904 parms.temp_path = res_get (zh->res, "setTmpDir");
1905 result = rset_create (rset_kind_temp, &parms);
1906 rsfd = rset_open (result, RSETF_WRITE);
1908 key.sysno = atoi (termz);
1912 rset_write (result, rsfd, &key);
1913 rset_close (result, rsfd);
1917 static RSET rpn_sort_spec (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1918 oid_value attributeSet, NMEM stream,
1919 Z_SortKeySpecList *sort_sequence,
1920 const char *rank_type)
1922 rset_null_parms parms;
1924 int sort_relation_value;
1925 AttrType sort_relation_type;
1930 Z_AttributeElement *ae;
1935 attr_init (&sort_relation_type, zapt, 7);
1936 sort_relation_value = attr_find (&sort_relation_type, &attributeSet);
1938 attr_init (&use_type, zapt, 1);
1939 use_value = attr_find (&use_type, &attributeSet);
1941 if (!sort_sequence->specs)
1943 sort_sequence->num_specs = 10;
1944 sort_sequence->specs = (Z_SortKeySpec **)
1945 nmem_malloc (stream, sort_sequence->num_specs *
1946 sizeof(*sort_sequence->specs));
1947 for (i = 0; i<sort_sequence->num_specs; i++)
1948 sort_sequence->specs[i] = 0;
1950 if (zapt->term->which != Z_Term_general)
1953 i = atoi_n ((char *) zapt->term->u.general->buf,
1954 zapt->term->u.general->len);
1955 if (i >= sort_sequence->num_specs)
1957 sprintf (termz, "%d", i);
1959 oe.proto = PROTO_Z3950;
1960 oe.oclass = CLASS_ATTSET;
1961 oe.value = attributeSet;
1962 if (!oid_ent_to_oid (&oe, oid))
1965 sks = (Z_SortKeySpec *) nmem_malloc (stream, sizeof(*sks));
1966 sks->sortElement = (Z_SortElement *)
1967 nmem_malloc (stream, sizeof(*sks->sortElement));
1968 sks->sortElement->which = Z_SortElement_generic;
1969 sk = sks->sortElement->u.generic = (Z_SortKey *)
1970 nmem_malloc (stream, sizeof(*sk));
1971 sk->which = Z_SortKey_sortAttributes;
1972 sk->u.sortAttributes = (Z_SortAttributes *)
1973 nmem_malloc (stream, sizeof(*sk->u.sortAttributes));
1975 sk->u.sortAttributes->id = oid;
1976 sk->u.sortAttributes->list = (Z_AttributeList *)
1977 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list));
1978 sk->u.sortAttributes->list->num_attributes = 1;
1979 sk->u.sortAttributes->list->attributes = (Z_AttributeElement **)
1980 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list->attributes));
1981 ae = *sk->u.sortAttributes->list->attributes = (Z_AttributeElement *)
1982 nmem_malloc (stream, sizeof(**sk->u.sortAttributes->list->attributes));
1983 ae->attributeSet = 0;
1984 ae->attributeType = (int *)
1985 nmem_malloc (stream, sizeof(*ae->attributeType));
1986 *ae->attributeType = 1;
1987 ae->which = Z_AttributeValue_numeric;
1988 ae->value.numeric = (int *)
1989 nmem_malloc (stream, sizeof(*ae->value.numeric));
1990 *ae->value.numeric = use_value;
1992 sks->sortRelation = (int *)
1993 nmem_malloc (stream, sizeof(*sks->sortRelation));
1994 if (sort_relation_value == 1)
1995 *sks->sortRelation = Z_SortRelation_ascending;
1996 else if (sort_relation_value == 2)
1997 *sks->sortRelation = Z_SortRelation_descending;
1999 *sks->sortRelation = Z_SortRelation_ascending;
2001 sks->caseSensitivity = (int *)
2002 nmem_malloc (stream, sizeof(*sks->caseSensitivity));
2003 *sks->caseSensitivity = 0;
2006 sks->which = Z_SortKeySpec_null;
2007 sks->u.null = odr_nullval ();
2009 sks->missingValueAction = 0;
2012 sort_sequence->specs[i] = sks;
2014 parms.rset_term = rset_term_create (termz, -1, rank_type,
2016 return rset_create (rset_kind_null, &parms);
2019 struct xpath_predicate {
2022 #define XPATH_PREDICATE_RELATION 1
2028 #define XPATH_PREDICATE_BOOLEAN 2
2031 struct xpath_predicate *left;
2032 struct xpath_predicate *right;
2037 struct xpath_location_step {
2039 struct xpath_predicate *predicate;
2042 static int parse_xpath(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2043 oid_value attributeSet,
2044 struct xpath_location_step *xpath, NMEM mem)
2046 oid_value curAttributeSet = attributeSet;
2048 const char *use_string = 0;
2052 attr_init (&use, zapt, 1);
2053 attr_find_ex (&use, &curAttributeSet, &use_string);
2055 if (!use_string || *use_string != '/')
2061 while (*cp && !strchr("/[",*cp))
2066 xpath[no].predicate = 0;
2067 xpath[no].part = nmem_malloc (mem, i+1);
2068 memcpy (xpath[no].part, cp - i, i);
2069 xpath[no].part[i] = 0;
2073 struct xpath_predicate *p = xpath[no].predicate =
2074 nmem_malloc (mem, sizeof(struct xpath_predicate));
2076 p->which = XPATH_PREDICATE_RELATION;
2081 for (i = 0; *cp && !strchr("><=] ", *cp); i++)
2083 p->u.relation.name = nmem_malloc (mem, i+1);
2084 memcpy (p->u.relation.name, cp - i, i);
2085 p->u.relation.name[i] = 0;
2090 for (i = 0; *cp && strchr(">=<!", *cp); i++)
2093 p->u.relation.op = nmem_malloc (mem, i+1);
2095 memcpy (p->u.relation.op, cp - i, i);
2096 p->u.relation.op[i] = 0;
2101 if (strchr("\"'", *cp))
2104 for (i = 0; *cp && !strchr("\"'", *cp); i++)
2107 p->u.relation.value = nmem_malloc (mem, i+1);
2109 memcpy (p->u.relation.value, cp - i, i);
2110 p->u.relation.value[i] = 0;
2111 yaz_log (LOG_LOG, "value=%s", p->u.relation.value);
2117 for (i = 0; *cp && !strchr(" ]", *cp); i++)
2119 p->u.relation.value = nmem_malloc (mem, i+1);
2121 memcpy (p->u.relation.value, cp - i, i);
2122 p->u.relation.value[i] = 0;
2129 } /* end of ] predicate */
2139 static RSET xpath_trunc(ZebraHandle zh, NMEM stream,
2140 int reg_type, const char *term, int use,
2141 oid_value curAttributeSet)
2144 struct grep_info grep_info;
2145 char term_dict[2048];
2148 int ord = zebraExplain_lookupSU (zh->reg->zei, curAttributeSet, use);
2149 int ord_len, i, r, max_pos;
2150 int term_type = Z_Term_characterString;
2151 const char *flags = "void";
2153 if (grep_info_prepare (zh, 0 /* zapt */, &grep_info, '0', stream))
2155 rset_null_parms parms;
2157 parms.rset_term = rset_term_create (term, strlen(term),
2159 parms.rset_term->nn = 0;
2160 return rset_create (rset_kind_null, &parms);
2165 rset_null_parms parms;
2167 parms.rset_term = rset_term_create (term, strlen(term),
2169 parms.rset_term->nn = 0;
2170 return rset_create (rset_kind_null, &parms);
2173 term_dict[prefix_len++] = '|';
2175 term_dict[prefix_len++] = '(';
2177 ord_len = key_SU_encode (ord, ord_buf);
2178 for (i = 0; i<ord_len; i++)
2180 term_dict[prefix_len++] = 1;
2181 term_dict[prefix_len++] = ord_buf[i];
2183 term_dict[prefix_len++] = ')';
2184 term_dict[prefix_len++] = 1;
2185 term_dict[prefix_len++] = reg_type;
2187 strcpy (term_dict+prefix_len, term);
2189 grep_info.isam_p_indx = 0;
2190 r = dict_lookup_grep (zh->reg->dict, term_dict, 0,
2191 &grep_info, &max_pos, 0, grep_handle);
2192 yaz_log (LOG_LOG, "%s %d positions", term,
2193 grep_info.isam_p_indx);
2194 rset = rset_trunc (zh, grep_info.isam_p_buf,
2195 grep_info.isam_p_indx, term, strlen(term),
2196 flags, 1, term_type);
2197 grep_info_delete (&grep_info);
2201 static RSET rpn_search_xpath (ZebraHandle zh,
2202 oid_value attributeSet,
2203 int num_bases, char **basenames,
2204 NMEM stream, const char *rank_type, RSET rset,
2205 int xpath_len, struct xpath_location_step *xpath)
2207 oid_value curAttributeSet = attributeSet;
2214 yaz_log (LOG_LOG, "len=%d", xpath_len);
2215 for (i = 0; i<xpath_len; i++)
2217 yaz_log (LOG_LOG, "XPATH %d %s", i, xpath[i].part);
2221 curAttributeSet = VAL_IDXPATH;
2231 a[@attr=value]/b[@other=othervalue]
2233 /e/@a val range(e/,range(@a,freetext(w,1015,val),@a),e/)
2234 /a/b val range(b/a/,freetext(w,1016,val),b/a/)
2235 /a/b/@c val range(b/a/,range(@c,freetext(w,1016,val),@c),b/a/)
2236 /a/b[@c=y] val range(b/a/,freetext(w,1016,val),b/a/,@c=y)
2237 /a[@c=y]/b val range(a/,range(b/a/,freetext(w,1016,val),b/a/),a/,@c=y)
2238 /a[@c=x]/b[@c=y] range(a/,range(b/a/,freetext(w,1016,val),b/a/,@c=y),a/,@c=x)
2242 dict_grep_cmap (zh->reg->dict, 0, 0);
2244 for (base_no = 0; base_no < num_bases; base_no++)
2246 int level = xpath_len;
2249 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
2251 zh->errCode = 109; /* Database unavailable */
2252 zh->errString = basenames[base_no];
2255 while (--level >= 0)
2257 char xpath_rev[128];
2259 rset_between_parms parms;
2260 RSET rset_start_tag = 0, rset_end_tag = 0, rset_attr = 0;
2264 for (i = level; i >= 1; --i)
2266 const char *cp = xpath[i].part;
2272 memcpy (xpath_rev + len, "[^/]*", 5);
2275 else if (*cp == ' ')
2278 xpath_rev[len++] = 1;
2279 xpath_rev[len++] = ' ';
2283 xpath_rev[len++] = *cp;
2284 xpath_rev[len++] = '/';
2286 else if (i == 1) /* // case */
2288 xpath_rev[len++] = '.';
2289 xpath_rev[len++] = '*';
2294 if (xpath[level].predicate &&
2295 xpath[level].predicate->which == XPATH_PREDICATE_RELATION &&
2296 xpath[level].predicate->u.relation.name[0])
2298 char predicate_str[128];
2300 strcpy (predicate_str,
2301 xpath[level].predicate->u.relation.name+1);
2302 if (xpath[level].predicate->u.relation.value)
2304 strcat (predicate_str, "=");
2305 strcat (predicate_str,
2306 xpath[level].predicate->u.relation.value);
2308 rset_attr = xpath_trunc (
2309 zh, stream, '0', predicate_str, 3, curAttributeSet);
2316 yaz_log (LOG_LOG, "xpath_rev (%d) = %s", level, xpath_rev);
2317 if (strlen(xpath_rev))
2319 rset_start_tag = xpath_trunc(zh, stream,
2320 '0', xpath_rev, 1, curAttributeSet);
2322 rset_end_tag = xpath_trunc(zh, stream,
2323 '0', xpath_rev, 2, curAttributeSet);
2325 parms.key_size = sizeof(struct it_key);
2326 parms.cmp = key_compare_it;
2327 parms.rset_l = rset_start_tag;
2328 parms.rset_m = rset;
2329 parms.rset_r = rset_end_tag;
2330 parms.rset_attr = rset_attr;
2331 parms.printer = key_print_it;
2332 rset = rset_create (rset_kind_between, &parms);
2343 static RSET rpn_search_APT (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2344 oid_value attributeSet, NMEM stream,
2345 Z_SortKeySpecList *sort_sequence,
2346 int num_bases, char **basenames)
2349 char *search_type = NULL;
2350 char rank_type[128];
2353 char termz[IT_MAX_WORD+1];
2357 struct xpath_location_step xpath[10];
2359 zebra_maps_attr (zh->reg->zebra_maps, zapt, ®_id, &search_type,
2360 rank_type, &complete_flag, &sort_flag);
2362 logf (LOG_DEBUG, "reg_id=%c", reg_id);
2363 logf (LOG_DEBUG, "complete_flag=%d", complete_flag);
2364 logf (LOG_DEBUG, "search_type=%s", search_type);
2365 logf (LOG_DEBUG, "rank_type=%s", rank_type);
2367 if (trans_term (zh, zapt, termz))
2371 return rpn_sort_spec (zh, zapt, attributeSet, stream, sort_sequence,
2373 xpath_len = parse_xpath(zh, zapt, attributeSet, xpath, stream);
2377 if (xpath[xpath_len-1].part[0] == '@')
2381 if (!strcmp (search_type, "phrase"))
2383 rset = rpn_search_APT_phrase (zh, zapt, termz, attributeSet, stream,
2384 reg_id, complete_flag, rank_type,
2386 num_bases, basenames);
2388 else if (!strcmp (search_type, "and-list"))
2390 rset = rpn_search_APT_and_list (zh, zapt, termz, attributeSet, stream,
2391 reg_id, complete_flag, rank_type,
2393 num_bases, basenames);
2395 else if (!strcmp (search_type, "or-list"))
2397 rset = rpn_search_APT_or_list (zh, zapt, termz, attributeSet, stream,
2398 reg_id, complete_flag, rank_type,
2400 num_bases, basenames);
2402 else if (!strcmp (search_type, "local"))
2404 rset = rpn_search_APT_local (zh, zapt, termz, attributeSet, stream,
2407 else if (!strcmp (search_type, "numeric"))
2409 rset = rpn_search_APT_numeric (zh, zapt, termz, attributeSet, stream,
2410 reg_id, complete_flag, rank_type,
2411 num_bases, basenames);
2413 else if (!strcmp (search_type, "always"))
2419 return rpn_search_xpath (zh, attributeSet, num_bases, basenames,
2420 stream, rank_type, rset, xpath_len, xpath);
2423 static RSET rpn_search_structure (ZebraHandle zh, Z_RPNStructure *zs,
2424 oid_value attributeSet, NMEM stream,
2425 Z_SortKeySpecList *sort_sequence,
2426 int num_bases, char **basenames)
2429 if (zs->which == Z_RPNStructure_complex)
2431 Z_Operator *zop = zs->u.complex->roperator;
2432 rset_bool_parms bool_parms;
2434 bool_parms.rset_l = rpn_search_structure (zh, zs->u.complex->s1,
2435 attributeSet, stream,
2437 num_bases, basenames);
2438 if (bool_parms.rset_l == NULL)
2440 bool_parms.rset_r = rpn_search_structure (zh, zs->u.complex->s2,
2441 attributeSet, stream,
2443 num_bases, basenames);
2444 if (bool_parms.rset_r == NULL)
2446 rset_delete (bool_parms.rset_l);
2449 bool_parms.key_size = sizeof(struct it_key);
2450 bool_parms.cmp = key_compare_it;
2454 case Z_Operator_and:
2455 r = rset_create (rset_kind_and, &bool_parms);
2458 r = rset_create (rset_kind_or, &bool_parms);
2460 case Z_Operator_and_not:
2461 r = rset_create (rset_kind_not, &bool_parms);
2463 case Z_Operator_prox:
2465 if (zop->u.prox->which != Z_ProximityOperator_known)
2471 if (zop->u.prox->which != Z_ProxCode_known)
2479 if (*zop->u.prox->u.known != Z_ProxUnit_word)
2481 char *val = (char *) nmem_malloc (stream, 16);
2483 zh->errString = val;
2484 sprintf (val, "%d", *zop->u.prox->u.known);
2488 if (*zop->u.prox->proximityUnitCode != Z_ProxUnit_word)
2490 char *val = (char *) nmem_malloc (stream, 16);
2492 zh->errString = val;
2493 sprintf (val, "%d", *zop->u.prox->proximityUnitCode);
2501 rsets[0] = bool_parms.rset_l;
2502 rsets[1] = bool_parms.rset_r;
2504 r = rpn_prox (zh, rsets, 2,
2505 *zop->u.prox->ordered,
2506 (!zop->u.prox->exclusion ? 0 :
2507 *zop->u.prox->exclusion),
2508 *zop->u.prox->relationType,
2509 *zop->u.prox->distance);
2510 rset_delete (rsets[0]);
2511 rset_delete (rsets[1]);
2519 else if (zs->which == Z_RPNStructure_simple)
2521 if (zs->u.simple->which == Z_Operand_APT)
2523 logf (LOG_DEBUG, "rpn_search_APT");
2524 r = rpn_search_APT (zh, zs->u.simple->u.attributesPlusTerm,
2525 attributeSet, stream, sort_sequence,
2526 num_bases, basenames);
2528 else if (zs->u.simple->which == Z_Operand_resultSetId)
2530 logf (LOG_DEBUG, "rpn_search_ref");
2531 r = resultSetRef (zh, zs->u.simple->u.resultSetId);
2534 r = rset_create (rset_kind_null, NULL);
2537 nmem_strdup (stream, zs->u.simple->u.resultSetId);
2556 RSET rpn_search (ZebraHandle zh, NMEM nmem,
2557 Z_RPNQuery *rpn, int num_bases, char **basenames,
2558 const char *setname,
2563 oid_value attributeSet;
2564 Z_SortKeySpecList *sort_sequence;
2568 zh->errString = NULL;
2571 sort_sequence = (Z_SortKeySpecList *)
2572 nmem_malloc (nmem, sizeof(*sort_sequence));
2573 sort_sequence->num_specs = 10;
2574 sort_sequence->specs = (Z_SortKeySpec **)
2575 nmem_malloc (nmem, sort_sequence->num_specs *
2576 sizeof(*sort_sequence->specs));
2577 for (i = 0; i<sort_sequence->num_specs; i++)
2578 sort_sequence->specs[i] = 0;
2580 attrset = oid_getentbyoid (rpn->attributeSetId);
2581 attributeSet = attrset->value;
2582 rset = rpn_search_structure (zh, rpn->RPNStructure, attributeSet,
2583 nmem, sort_sequence, num_bases, basenames);
2588 logf (LOG_DEBUG, "search error: %d", zh->errCode);
2590 for (i = 0; sort_sequence->specs[i]; i++)
2592 sort_sequence->num_specs = i;
2594 resultSetRank (zh, sset, rset);
2597 logf (LOG_DEBUG, "resultSetSortSingle in rpn_search");
2598 resultSetSortSingle (zh, nmem, sset, rset,
2599 sort_sequence, &sort_status);
2602 logf (LOG_DEBUG, "resultSetSortSingle status = %d", zh->errCode);
2608 struct scan_info_entry {
2614 struct scan_info_entry *list;
2620 static int scan_handle (char *name, const char *info, int pos, void *client)
2622 int len_prefix, idx;
2623 struct scan_info *scan_info = (struct scan_info *) client;
2625 len_prefix = strlen(scan_info->prefix);
2626 if (memcmp (name, scan_info->prefix, len_prefix))
2628 if (pos > 0) idx = scan_info->after - pos + scan_info->before;
2631 scan_info->list[idx].term = (char *)
2632 odr_malloc (scan_info->odr, strlen(name + len_prefix)+1);
2633 strcpy (scan_info->list[idx].term, name + len_prefix);
2634 assert (*info == sizeof(ISAMS_P));
2635 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAMS_P));
2639 static void scan_term_untrans (ZebraHandle zh, NMEM stream, int reg_type,
2640 char **dst, const char *src)
2642 char term_dst[1024];
2644 term_untrans (zh, reg_type, term_dst, src);
2646 *dst = (char *) nmem_malloc (stream, strlen(term_dst)+1);
2647 strcpy (*dst, term_dst);
2650 static void count_set (RSET r, int *count)
2658 logf (LOG_DEBUG, "count_set");
2661 rfd = rset_open (r, RSETF_READ);
2662 while (rset_read (r, rfd, &key, &term_index))
2664 if (key.sysno != psysno)
2671 rset_close (r, rfd);
2672 logf (LOG_DEBUG, "%d keys, %d records", kno, *count);
2675 void rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
2676 oid_value attributeset,
2677 int num_bases, char **basenames,
2678 int *position, int *num_entries, ZebraScanEntry **list,
2682 int pos = *position;
2683 int num = *num_entries;
2687 char termz[IT_MAX_WORD+20];
2690 struct scan_info *scan_info_array;
2691 ZebraScanEntry *glist;
2692 int ords[32], ord_no = 0;
2696 char *search_type = NULL;
2697 char rank_type[128];
2702 if (attributeset == VAL_NONE)
2703 attributeset = VAL_BIB1;
2705 yaz_log (LOG_DEBUG, "position = %d, num = %d set=%d",
2706 pos, num, attributeset);
2708 attr_init (&use, zapt, 1);
2709 use_value = attr_find (&use, &attributeset);
2711 if (zebra_maps_attr (zh->reg->zebra_maps, zapt, ®_id, &search_type,
2712 rank_type, &complete_flag, &sort_flag))
2718 yaz_log (LOG_DEBUG, "use_value = %d", use_value);
2720 if (use_value == -1)
2722 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
2726 data1_local_attribute *local_attr;
2728 if ((r=att_getentbyatt (zh, &attp, attributeset, use_value)))
2730 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
2731 attributeset, use_value);
2739 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
2741 zh->errString = basenames[base_no];
2742 zh->errCode = 109; /* Database unavailable */
2746 for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
2747 local_attr = local_attr->next)
2751 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
2754 ords[ord_no++] = ord;
2763 /* prepare dictionary scanning */
2766 scan_info_array = (struct scan_info *)
2767 odr_malloc (stream, ord_no * sizeof(*scan_info_array));
2768 for (i = 0; i < ord_no; i++)
2770 int j, prefix_len = 0;
2771 int before_tmp = before, after_tmp = after;
2772 struct scan_info *scan_info = scan_info_array + i;
2773 struct rpn_char_map_info rcmi;
2775 rpn_char_map_prepare (zh->reg, reg_id, &rcmi);
2777 scan_info->before = before;
2778 scan_info->after = after;
2779 scan_info->odr = stream;
2781 scan_info->list = (struct scan_info_entry *)
2782 odr_malloc (stream, (before+after) * sizeof(*scan_info->list));
2783 for (j = 0; j<before+after; j++)
2784 scan_info->list[j].term = NULL;
2786 prefix_len += key_SU_encode (ords[i], termz + prefix_len);
2787 termz[prefix_len++] = reg_id;
2788 termz[prefix_len] = 0;
2789 strcpy (scan_info->prefix, termz);
2791 trans_scan_term (zh, zapt, termz+prefix_len, reg_id);
2793 dict_scan (zh->reg->dict, termz, &before_tmp, &after_tmp,
2794 scan_info, scan_handle);
2796 glist = (ZebraScanEntry *)
2797 odr_malloc (stream, (before+after)*sizeof(*glist));
2799 /* consider terms after main term */
2800 for (i = 0; i < ord_no; i++)
2804 for (i = 0; i<after; i++)
2807 const char *mterm = NULL;
2811 for (j = 0; j < ord_no; j++)
2813 if (ptr[j] < before+after &&
2814 (tst=scan_info_array[j].list[ptr[j]].term) &&
2815 (!mterm || strcmp (tst, mterm) < 0))
2823 scan_term_untrans (zh, stream->mem, reg_id,
2824 &glist[i+before].term, mterm);
2825 rset = rset_trunc (zh, &scan_info_array[j0].list[ptr[j0]].isam_p, 1,
2826 glist[i+before].term, strlen(glist[i+before].term),
2827 NULL, 0, zapt->term->which);
2830 for (j = j0+1; j<ord_no; j++)
2832 if (ptr[j] < before+after &&
2833 (tst=scan_info_array[j].list[ptr[j]].term) &&
2834 !strcmp (tst, mterm))
2836 rset_bool_parms bool_parms;
2840 rset_trunc (zh, &scan_info_array[j].list[ptr[j]].isam_p, 1,
2841 glist[i+before].term,
2842 strlen(glist[i+before].term), NULL, 0,
2845 bool_parms.key_size = sizeof(struct it_key);
2846 bool_parms.cmp = key_compare_it;
2847 bool_parms.rset_l = rset;
2848 bool_parms.rset_r = rset2;
2850 rset = rset_create (rset_kind_or, &bool_parms);
2855 count_set (rset, &glist[i+before].occurrences);
2860 *num_entries -= (after-i);
2864 /* consider terms before main term */
2865 for (i = 0; i<ord_no; i++)
2868 for (i = 0; i<before; i++)
2871 const char *mterm = NULL;
2875 for (j = 0; j <ord_no; j++)
2877 if (ptr[j] < before &&
2878 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2879 (!mterm || strcmp (tst, mterm) > 0))
2888 scan_term_untrans (zh, stream->mem, reg_id,
2889 &glist[before-1-i].term, mterm);
2892 (zh, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1,
2893 glist[before-1-i].term, strlen(glist[before-1-i].term),
2894 NULL, 0, zapt->term->which);
2898 for (j = j0+1; j<ord_no; j++)
2900 if (ptr[j] < before &&
2901 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2902 !strcmp (tst, mterm))
2904 rset_bool_parms bool_parms;
2907 rset2 = rset_trunc (zh,
2908 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1,
2909 glist[before-1-i].term,
2910 strlen(glist[before-1-i].term), NULL, 0,
2913 bool_parms.key_size = sizeof(struct it_key);
2914 bool_parms.cmp = key_compare_it;
2915 bool_parms.rset_l = rset;
2916 bool_parms.rset_r = rset2;
2918 rset = rset_create (rset_kind_or, &bool_parms);
2923 count_set (rset, &glist[before-1-i].occurrences);
2933 *list = glist + i; /* list is set to first 'real' entry */
2935 logf (LOG_DEBUG, "position = %d, num_entries = %d",
2936 *position, *num_entries);
2938 logf (LOG_DEBUG, "scan error: %d", zh->errCode);