1 /* $Id: zrpn.c,v 1.148 2004-08-31 14:43:41 heikki Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
34 #include <zebra_xpath.h>
40 #include <rsmultior.h>
41 #include <rsbetween.h>
44 struct rpn_char_map_info {
53 Z_AttributesPlusTerm *zapt;
57 static const char **rpn_char_map_handler (void *vp, const char **from, int len)
59 struct rpn_char_map_info *p = (struct rpn_char_map_info *) vp;
60 const char **out = zebra_maps_input (p->zm, p->reg_type, from, len);
64 const char *outp = *out;
65 yaz_log (LOG_LOG, "---");
68 yaz_log (LOG_LOG, "%02X", *outp);
76 static void rpn_char_map_prepare (struct zebra_register *reg, int reg_type,
77 struct rpn_char_map_info *map_info)
79 map_info->zm = reg->zebra_maps;
80 map_info->reg_type = reg_type;
81 dict_grep_cmap (reg->dict, map_info, rpn_char_map_handler);
84 static int attr_find_ex (AttrType *src, oid_value *attributeSetP,
85 const char **string_value)
89 num_attributes = src->zapt->attributes->num_attributes;
90 while (src->major < num_attributes)
92 Z_AttributeElement *element;
94 element = src->zapt->attributes->attributes[src->major];
95 if (src->type == *element->attributeType)
97 switch (element->which)
99 case Z_AttributeValue_numeric:
101 if (element->attributeSet && attributeSetP)
105 attrset = oid_getentbyoid (element->attributeSet);
106 *attributeSetP = attrset->value;
108 return *element->value.numeric;
110 case Z_AttributeValue_complex:
111 if (src->minor >= element->value.complex->num_list)
113 if (element->attributeSet && attributeSetP)
117 attrset = oid_getentbyoid (element->attributeSet);
118 *attributeSetP = attrset->value;
120 if (element->value.complex->list[src->minor]->which ==
121 Z_StringOrNumeric_numeric)
125 *element->value.complex->list[src->minor-1]->u.numeric;
127 else if (element->value.complex->list[src->minor]->which ==
128 Z_StringOrNumeric_string)
134 element->value.complex->list[src->minor-1]->u.string;
148 static int attr_find (AttrType *src, oid_value *attributeSetP)
150 return attr_find_ex (src, attributeSetP, 0);
153 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
176 static void term_untrans (ZebraHandle zh, int reg_type,
177 char *dst, const char *src)
182 const char *cp = zebra_maps_output (zh->reg->zebra_maps,
184 if (!cp && len < IT_MAX_WORD-1)
187 while (*cp && len < IT_MAX_WORD-1)
193 static void add_isam_p (const char *name, const char *info,
196 if (p->isam_p_indx == p->isam_p_size)
198 ISAMC_P *new_isam_p_buf;
202 p->isam_p_size = 2*p->isam_p_size + 100;
203 new_isam_p_buf = (ISAMC_P *) xmalloc (sizeof(*new_isam_p_buf) *
207 memcpy (new_isam_p_buf, p->isam_p_buf,
208 p->isam_p_indx * sizeof(*p->isam_p_buf));
209 xfree (p->isam_p_buf);
211 p->isam_p_buf = new_isam_p_buf;
214 new_term_no = (int *) xmalloc (sizeof(*new_term_no) *
218 memcpy (new_term_no, p->isam_p_buf,
219 p->isam_p_indx * sizeof(*p->term_no));
222 p->term_no = new_term_no;
225 assert (*info == sizeof(*p->isam_p_buf));
226 memcpy (p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
233 char term_tmp[IT_MAX_WORD];
235 int len = key_SU_decode (&su_code, name);
237 term_untrans (p->zh, p->reg_type, term_tmp, name+len+1);
238 logf (LOG_LOG, "grep: %d %c %s", su_code, name[len], term_tmp);
239 zebraExplain_lookup_ord (p->zh->reg->zei,
240 su_code, &db, &set, &use);
241 logf (LOG_LOG, "grep: set=%d use=%d db=%s", set, use, db);
243 resultSetAddTerm (p->zh, p->termset, name[len], db,
250 static int grep_handle (char *name, const char *info, void *p)
252 add_isam_p (name, info, (struct grep_info *) p);
256 static int term_pre (ZebraMaps zebra_maps, int reg_type, const char **src,
257 const char *ct1, const char *ct2)
259 const char *s1, *s0 = *src;
262 /* skip white space */
265 if (ct1 && strchr (ct1, *s0))
267 if (ct2 && strchr (ct2, *s0))
270 map = zebra_maps_input (zebra_maps, reg_type, &s1, strlen(s1));
271 if (**map != *CHR_SPACE)
279 #define REGEX_CHARS " []()|.*+?!"
281 /* term_100: handle term, where trunc=none (no operators at all) */
282 static int term_100 (ZebraMaps zebra_maps, int reg_type,
283 const char **src, char *dst, int space_split,
291 const char *space_start = 0;
292 const char *space_end = 0;
294 if (!term_pre (zebra_maps, reg_type, src, NULL, NULL))
300 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
303 if (**map == *CHR_SPACE)
306 else /* complete subfield only. */
308 if (**map == *CHR_SPACE)
309 { /* save space mapping for later .. */
314 else if (space_start)
315 { /* reload last space */
316 while (space_start < space_end)
318 if (strchr (REGEX_CHARS, *space_start))
320 dst_term[j++] = *space_start;
321 dst[i++] = *space_start++;
324 space_start = space_end = 0;
327 /* add non-space char */
330 if (strchr(REGEX_CHARS, *s1))
342 /* term_101: handle term, where trunc=Process # */
343 static int term_101 (ZebraMaps zebra_maps, int reg_type,
344 const char **src, char *dst, int space_split,
352 if (!term_pre (zebra_maps, reg_type, src, "#", "#"))
361 dst_term[j++] = *s0++;
366 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
367 if (space_split && **map == *CHR_SPACE)
371 if (strchr(REGEX_CHARS, *s1))
379 dst_term[j++] = '\0';
384 /* term_103: handle term, where trunc=re-2 (regular expressions) */
385 static int term_103 (ZebraMaps zebra_maps, int reg_type, const char **src,
386 char *dst, int *errors, int space_split,
394 if (!term_pre (zebra_maps, reg_type, src, "^\\()[].*+?|", "("))
397 if (errors && *s0 == '+' && s0[1] && s0[2] == '+' && s0[3] &&
400 *errors = s0[1] - '0';
407 if (strchr ("^\\()[].*+?|-", *s0))
415 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
416 if (**map == *CHR_SPACE)
420 if (strchr(REGEX_CHARS, *s1))
433 /* term_103: handle term, where trunc=re-1 (regular expressions) */
434 static int term_102 (ZebraMaps zebra_maps, int reg_type, const char **src,
435 char *dst, int space_split, char *dst_term)
437 return term_103 (zebra_maps, reg_type, src, dst, NULL, space_split,
442 /* term_104: handle term, where trunc=Process # and ! */
443 static int term_104 (ZebraMaps zebra_maps, int reg_type,
444 const char **src, char *dst, int space_split,
452 if (!term_pre (zebra_maps, reg_type, src, "?*#", "?*#"))
459 dst_term[j++] = *s0++;
460 if (*s0 >= '0' && *s0 <= '9')
463 while (*s0 >= '0' && *s0 <= '9')
465 limit = limit * 10 + (*s0 - '0');
466 dst_term[j++] = *s0++;
486 dst_term[j++] = *s0++;
491 dst_term[j++] = *s0++;
495 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
496 if (space_split && **map == *CHR_SPACE)
500 if (strchr(REGEX_CHARS, *s1))
508 dst_term[j++] = '\0';
513 /* term_105/106: handle term, where trunc=Process * and ! and right trunc */
514 static int term_105 (ZebraMaps zebra_maps, int reg_type,
515 const char **src, char *dst, int space_split,
516 char *dst_term, int right_truncate)
523 if (!term_pre (zebra_maps, reg_type, src, "*!", "*!"))
532 dst_term[j++] = *s0++;
537 dst_term[j++] = *s0++;
541 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
542 if (space_split && **map == *CHR_SPACE)
546 if (strchr(REGEX_CHARS, *s1))
560 dst_term[j++] = '\0';
566 /* gen_regular_rel - generate regular expression from relation
567 * val: border value (inclusive)
568 * islt: 1 if <=; 0 if >=.
570 static void gen_regular_rel (char *dst, int val, int islt)
577 logf (LOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
581 strcpy (dst, "(-[0-9]+|(");
589 strcpy (dst, "([0-9]+|-(");
590 dst_p = strlen (dst);
600 dst_p = strlen (dst);
601 sprintf (numstr, "%d", val);
602 for (w = strlen(numstr); --w >= 0; pos++)
621 strcpy (dst + dst_p, numstr);
622 dst_p = strlen(dst) - pos - 1;
650 for (i = 0; i<pos; i++)
663 /* match everything less than 10^(pos-1) */
665 for (i=1; i<pos; i++)
666 strcat (dst, "[0-9]?");
670 /* match everything greater than 10^pos */
671 for (i = 0; i <= pos; i++)
672 strcat (dst, "[0-9]");
673 strcat (dst, "[0-9]*");
678 void string_rel_add_char (char **term_p, const char *src, int *indx)
680 if (src[*indx] == '\\')
681 *(*term_p)++ = src[(*indx)++];
682 *(*term_p)++ = src[(*indx)++];
686 * > abc ([b-].*|a[c-].*|ab[d-].*|abc.+)
687 * ([^-a].*|a[^-b].*ab[^-c].*|abc.+)
688 * >= abc ([b-].*|a[c-].*|ab[c-].*)
689 * ([^-a].*|a[^-b].*|ab[c-].*)
690 * < abc ([-0].*|a[-a].*|ab[-b].*)
691 * ([^a-].*|a[^b-].*|ab[^c-].*)
692 * <= abc ([-0].*|a[-a].*|ab[-b].*|abc)
693 * ([^a-].*|a[^b-].*|ab[^c-].*|abc)
695 static int string_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
696 const char **term_sub, char *term_dict,
697 oid_value attributeSet,
698 int reg_type, int space_split, char *term_dst)
703 char *term_tmp = term_dict + strlen(term_dict);
704 char term_component[2*IT_MAX_WORD+20];
706 attr_init (&relation, zapt, 2);
707 relation_value = attr_find (&relation, NULL);
709 logf (LOG_DEBUG, "string relation value=%d", relation_value);
710 switch (relation_value)
713 if (!term_100 (zh->reg->zebra_maps, reg_type,
714 term_sub, term_component,
715 space_split, term_dst))
717 logf (LOG_DEBUG, "Relation <");
720 for (i = 0; term_component[i]; )
727 string_rel_add_char (&term_tmp, term_component, &j);
732 string_rel_add_char (&term_tmp, term_component, &i);
739 if ((term_tmp - term_dict) > IT_MAX_WORD)
746 if (!term_100 (zh->reg->zebra_maps, reg_type,
747 term_sub, term_component,
748 space_split, term_dst))
750 logf (LOG_DEBUG, "Relation <=");
753 for (i = 0; term_component[i]; )
758 string_rel_add_char (&term_tmp, term_component, &j);
762 string_rel_add_char (&term_tmp, term_component, &i);
771 if ((term_tmp - term_dict) > IT_MAX_WORD)
774 for (i = 0; term_component[i]; )
775 string_rel_add_char (&term_tmp, term_component, &i);
780 if (!term_100 (zh->reg->zebra_maps, reg_type,
781 term_sub, term_component, space_split, term_dst))
783 logf (LOG_DEBUG, "Relation >");
786 for (i = 0; term_component[i];)
791 string_rel_add_char (&term_tmp, term_component, &j);
796 string_rel_add_char (&term_tmp, term_component, &i);
804 if ((term_tmp - term_dict) > IT_MAX_WORD)
807 for (i = 0; term_component[i];)
808 string_rel_add_char (&term_tmp, term_component, &i);
815 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub,
816 term_component, space_split, term_dst))
818 logf (LOG_DEBUG, "Relation >=");
821 for (i = 0; term_component[i];)
828 string_rel_add_char (&term_tmp, term_component, &j);
831 if (term_component[i+1])
835 string_rel_add_char (&term_tmp, term_component, &i);
839 string_rel_add_char (&term_tmp, term_component, &i);
846 if ((term_tmp - term_dict) > IT_MAX_WORD)
854 logf (LOG_DEBUG, "Relation =");
855 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub,
856 term_component, space_split, term_dst))
858 strcat (term_tmp, "(");
859 strcat (term_tmp, term_component);
860 strcat (term_tmp, ")");
865 static int string_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
866 const char **term_sub,
867 oid_value attributeSet, NMEM stream,
868 struct grep_info *grep_info,
869 int reg_type, int complete_flag,
870 int num_bases, char **basenames,
871 char *term_dst, int xpath_use);
873 static RSET term_trunc (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
874 const char **term_sub,
875 oid_value attributeSet, NMEM stream,
876 struct grep_info *grep_info,
877 int reg_type, int complete_flag,
878 int num_bases, char **basenames,
880 const char *rank_type, int xpath_use,
884 grep_info->isam_p_indx = 0;
885 r = string_term (zh, zapt, term_sub, attributeSet, stream, grep_info,
886 reg_type, complete_flag, num_bases, basenames,
887 term_dst, xpath_use);
890 logf (LOG_DEBUG, "term: %s", term_dst);
891 return rset_trunc (zh, grep_info->isam_p_buf,
892 grep_info->isam_p_indx, term_dst,
893 strlen(term_dst), rank_type, 1 /* preserve pos */,
894 zapt->term->which, rset_nmem);
898 static int string_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
899 const char **term_sub,
900 oid_value attributeSet, NMEM stream,
901 struct grep_info *grep_info,
902 int reg_type, int complete_flag,
903 int num_bases, char **basenames,
904 char *term_dst, int xpath_use)
906 char term_dict[2*IT_MAX_WORD+4000];
909 int truncation_value;
912 const char *use_string = 0;
913 oid_value curAttributeSet = attributeSet;
915 struct rpn_char_map_info rcmi;
916 int space_split = complete_flag ? 0 : 1;
918 int bases_ok = 0; /* no of databases with OK attribute */
919 int errCode = 0; /* err code (if any is not OK) */
920 char *errString = 0; /* addinfo */
922 rpn_char_map_prepare (zh->reg, reg_type, &rcmi);
923 attr_init (&use, zapt, 1);
924 use_value = attr_find_ex (&use, &curAttributeSet, &use_string);
925 logf (LOG_DEBUG, "string_term, use value %d", use_value);
926 attr_init (&truncation, zapt, 5);
927 truncation_value = attr_find (&truncation, NULL);
928 logf (LOG_DEBUG, "truncation value %d", truncation_value);
930 if (use_value == -1) /* no attribute - assumy "any" */
932 for (base_no = 0; base_no < num_bases; base_no++)
935 data1_local_attribute id_xpath_attr;
936 data1_local_attribute *local_attr;
937 int max_pos, prefix_len = 0;
941 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
943 zh->errCode = 109; /* Database unavailable */
944 zh->errString = basenames[base_no];
947 if (xpath_use > 0 && use_value == -2)
949 use_value = xpath_use;
950 attp.local_attributes = &id_xpath_attr;
951 attp.attset_ordinal = VAL_IDXPATH;
952 id_xpath_attr.next = 0;
953 id_xpath_attr.local = use_value;
955 else if (curAttributeSet == VAL_IDXPATH)
957 attp.local_attributes = &id_xpath_attr;
958 attp.attset_ordinal = VAL_IDXPATH;
959 id_xpath_attr.next = 0;
960 id_xpath_attr.local = use_value;
964 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value,
967 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
968 curAttributeSet, use_value, r);
971 /* set was found, but value wasn't defined */
974 errString = nmem_strdup(stream, use_string);
978 sprintf (val_str, "%d", use_value);
979 errString = nmem_strdup (stream, val_str);
985 struct oident oident;
987 oident.proto = PROTO_Z3950;
988 oident.oclass = CLASS_ATTSET;
989 oident.value = curAttributeSet;
990 oid_ent_to_oid (&oident, oid);
993 errString = nmem_strdup (stream, oident.desc);
998 for (local_attr = attp.local_attributes; local_attr;
999 local_attr = local_attr->next)
1005 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
1010 term_dict[prefix_len++] = '|';
1012 term_dict[prefix_len++] = '(';
1014 ord_len = key_SU_encode (ord, ord_buf);
1015 for (i = 0; i<ord_len; i++)
1017 term_dict[prefix_len++] = 1;
1018 term_dict[prefix_len++] = ord_buf[i];
1027 sprintf (val_str, "%d", use_value);
1029 errString = nmem_strdup (stream, val_str);
1033 bases_ok++; /* this has OK attributes */
1035 term_dict[prefix_len++] = ')';
1036 term_dict[prefix_len++] = 1;
1037 term_dict[prefix_len++] = reg_type;
1038 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1039 term_dict[prefix_len] = '\0';
1041 switch (truncation_value)
1043 case -1: /* not specified */
1044 case 100: /* do not truncate */
1045 if (!string_relation (zh, zapt, &termp, term_dict,
1047 reg_type, space_split, term_dst))
1049 logf (LOG_LOG, "dict_lookup_grep: %s", term_dict+prefix_len);
1050 r = dict_lookup_grep (zh->reg->dict, term_dict, 0,
1051 grep_info, &max_pos, 0, grep_handle);
1053 logf (LOG_WARN, "dict_lookup_grep fail %d", r);
1055 case 1: /* right truncation */
1056 term_dict[j++] = '(';
1057 if (!term_100 (zh->reg->zebra_maps, reg_type,
1058 &termp, term_dict + j, space_split, term_dst))
1060 strcat (term_dict, ".*)");
1061 dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1062 &max_pos, 0, grep_handle);
1064 case 2: /* keft truncation */
1065 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1066 if (!term_100 (zh->reg->zebra_maps, reg_type,
1067 &termp, term_dict + j, space_split, term_dst))
1069 strcat (term_dict, ")");
1070 dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1071 &max_pos, 0, grep_handle);
1073 case 3: /* left&right truncation */
1074 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1075 if (!term_100 (zh->reg->zebra_maps, reg_type,
1076 &termp, term_dict + j, space_split, term_dst))
1078 strcat (term_dict, ".*)");
1079 dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1080 &max_pos, 0, grep_handle);
1084 case 101: /* process # in term */
1085 term_dict[j++] = '(';
1086 if (!term_101 (zh->reg->zebra_maps, reg_type,
1087 &termp, term_dict + j, space_split, term_dst))
1089 strcat (term_dict, ")");
1090 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1091 &max_pos, 0, grep_handle);
1093 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d", r);
1095 case 102: /* Regexp-1 */
1096 term_dict[j++] = '(';
1097 if (!term_102 (zh->reg->zebra_maps, reg_type,
1098 &termp, term_dict + j, space_split, term_dst))
1100 strcat (term_dict, ")");
1101 logf (LOG_DEBUG, "Regexp-1 tolerance=%d", r);
1102 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1103 &max_pos, 0, grep_handle);
1105 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
1108 case 103: /* Regexp-2 */
1110 term_dict[j++] = '(';
1111 if (!term_103 (zh->reg->zebra_maps, reg_type,
1112 &termp, term_dict + j, &r, space_split, term_dst))
1114 strcat (term_dict, ")");
1115 logf (LOG_DEBUG, "Regexp-2 tolerance=%d", r);
1116 r = dict_lookup_grep (zh->reg->dict, term_dict, r, grep_info,
1117 &max_pos, 2, grep_handle);
1119 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
1122 case 104: /* process # and ! in term */
1123 term_dict[j++] = '(';
1124 if (!term_104 (zh->reg->zebra_maps, reg_type,
1125 &termp, term_dict + j, space_split, term_dst))
1127 strcat (term_dict, ")");
1128 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1129 &max_pos, 0, grep_handle);
1131 logf (LOG_WARN, "dict_lookup_grep err, trunc=#/!: %d", r);
1133 case 105: /* process * and ! in term */
1134 term_dict[j++] = '(';
1135 if (!term_105 (zh->reg->zebra_maps, reg_type,
1136 &termp, term_dict + j, space_split, term_dst, 1))
1138 strcat (term_dict, ")");
1139 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1140 &max_pos, 0, grep_handle);
1142 logf (LOG_WARN, "dict_lookup_grep err, trunc=*/!: %d", r);
1144 case 106: /* process * and ! in term */
1145 term_dict[j++] = '(';
1146 if (!term_105 (zh->reg->zebra_maps, reg_type,
1147 &termp, term_dict + j, space_split, term_dst, 0))
1149 strcat (term_dict, ")");
1150 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1151 &max_pos, 0, grep_handle);
1153 logf (LOG_WARN, "dict_lookup_grep err, trunc=*/!: %d", r);
1159 zh->errCode = errCode;
1160 zh->errString = errString;
1164 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1169 /* convert APT search term to UTF8 */
1170 static int zapt_term_to_utf8 (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1174 Z_Term *term = zapt->term;
1176 switch (term->which)
1178 case Z_Term_general:
1179 if (zh->iconv_to_utf8 != 0)
1181 char *inbuf = term->u.general->buf;
1182 size_t inleft = term->u.general->len;
1183 char *outbuf = termz;
1184 size_t outleft = IT_MAX_WORD-1;
1187 ret = yaz_iconv(zh->iconv_to_utf8, &inbuf, &inleft,
1189 if (ret == (size_t)(-1))
1191 ret = yaz_iconv(zh->iconv_to_utf8, 0, 0, 0, 0);
1199 sizez = term->u.general->len;
1200 if (sizez > IT_MAX_WORD-1)
1201 sizez = IT_MAX_WORD-1;
1202 memcpy (termz, term->u.general->buf, sizez);
1203 termz[sizez] = '\0';
1206 case Z_Term_characterString:
1207 sizez = strlen(term->u.characterString);
1208 if (sizez > IT_MAX_WORD-1)
1209 sizez = IT_MAX_WORD-1;
1210 memcpy (termz, term->u.characterString, sizez);
1211 termz[sizez] = '\0';
1220 /* convert APT SCAN term to internal cmap */
1221 static int trans_scan_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1222 char *termz, int reg_type)
1224 char termz0[IT_MAX_WORD];
1226 if (zapt_term_to_utf8(zh, zapt, termz0))
1227 return -1; /* error */
1231 const char *cp = (const char *) termz0;
1232 const char *cp_end = cp + strlen(cp);
1235 const char *space_map = NULL;
1238 while ((len = (cp_end - cp)) > 0)
1240 map = zebra_maps_input (zh->reg->zebra_maps, reg_type, &cp, len);
1241 if (**map == *CHR_SPACE)
1246 for (src = space_map; *src; src++)
1249 for (src = *map; *src; src++)
1258 char *normalize_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1259 const char *termz, NMEM stream, unsigned reg_id)
1262 AttrType truncation;
1263 int truncation_value;
1266 attr_init (&truncation, zapt, 5);
1267 truncation_value = attr_find (&truncation, NULL);
1269 switch (truncation_value)
1289 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, ex_list,
1290 termz, strlen(termz));
1292 return nmem_strdup(stream, termz);
1295 char *buf = (char*) nmem_malloc (stream, wrbuf_len(wrbuf)+1);
1296 memcpy (buf, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1297 buf[wrbuf_len(wrbuf)] = '\0';
1302 static void grep_info_delete (struct grep_info *grep_info)
1305 xfree(grep_info->term_no);
1307 xfree (grep_info->isam_p_buf);
1310 static int grep_info_prepare (ZebraHandle zh,
1311 Z_AttributesPlusTerm *zapt,
1312 struct grep_info *grep_info,
1317 int termset_value_numeric;
1318 const char *termset_value_string;
1321 grep_info->term_no = 0;
1323 grep_info->isam_p_size = 0;
1324 grep_info->isam_p_buf = NULL;
1326 grep_info->reg_type = reg_type;
1327 grep_info->termset = 0;
1331 attr_init (&termset, zapt, 8);
1332 termset_value_numeric =
1333 attr_find_ex (&termset, NULL, &termset_value_string);
1334 if (termset_value_numeric != -1)
1337 const char *termset_name = 0;
1338 if (termset_value_numeric != -2)
1341 sprintf (resname, "%d", termset_value_numeric);
1342 termset_name = resname;
1345 termset_name = termset_value_string;
1346 logf (LOG_LOG, "creating termset set %s", termset_name);
1347 grep_info->termset = resultSetAdd (zh, termset_name, 1);
1348 if (!grep_info->termset)
1351 zh->errString = nmem_strdup (stream, termset_name);
1359 static RSET rpn_search_APT_phrase (ZebraHandle zh,
1360 Z_AttributesPlusTerm *zapt,
1361 const char *termz_org,
1362 oid_value attributeSet,
1364 int reg_type, int complete_flag,
1365 const char *rank_type, int xpath_use,
1366 int num_bases, char **basenames,
1369 char term_dst[IT_MAX_WORD+1];
1370 RSET rset[60], result;
1372 struct grep_info grep_info;
1373 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1374 const char *termp = termz;
1377 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1381 logf (LOG_DEBUG, "APT_phrase termp=%s", termp);
1382 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1384 reg_type, complete_flag,
1385 num_bases, basenames,
1386 term_dst, rank_type,
1387 xpath_use,rset_nmem);
1390 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1393 grep_info_delete (&grep_info);
1395 return rsnull_create (NULL); /* FIXME - Use a proper nmem */
1396 else if (rset_no == 1)
1399 result = rsprox_create( NULL, /* FIXME - use a proper nmem */
1400 sizeof(struct it_key), key_compare_it, key_get_seq,
1402 1 /* ordered */, 0 /* exclusion */,
1403 3 /* relation */, 1 /* distance */);
1407 static RSET rpn_search_APT_or_list (ZebraHandle zh,
1408 Z_AttributesPlusTerm *zapt,
1409 const char *termz_org,
1410 oid_value attributeSet,
1412 int reg_type, int complete_flag,
1413 const char *rank_type,
1415 int num_bases, char **basenames,
1418 char term_dst[IT_MAX_WORD+1];
1421 struct grep_info grep_info;
1422 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1423 const char *termp = termz;
1425 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1429 logf (LOG_DEBUG, "APT_or_list termp=%s", termp);
1430 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1432 reg_type, complete_flag,
1433 num_bases, basenames,
1434 term_dst, rank_type,
1435 xpath_use,rset_nmem);
1438 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1441 grep_info_delete (&grep_info);
1443 return rsnull_create (rset_nmem);
1444 return rsmultior_create(rset_nmem,
1445 sizeof(struct it_key),
1450 /* The old code, kept for reference. May be deleted soon */
1452 for (i = 1; i<rset_no; i++)
1454 rset_bool_parms bool_parms;
1455 bool_parms.rset_l = result;
1456 bool_parms.rset_r = rset[i];
1457 bool_parms.key_size = sizeof(struct it_key);
1458 bool_parms.cmp = key_compare_it;
1459 bool_parms.log_item = key_logdump_txt;
1460 result = rset_create (rset_kind_or, &bool_parms);
1466 static RSET rpn_search_APT_and_list (ZebraHandle zh,
1467 Z_AttributesPlusTerm *zapt,
1468 const char *termz_org,
1469 oid_value attributeSet,
1471 int reg_type, int complete_flag,
1472 const char *rank_type,
1474 int num_bases, char **basenames,
1477 char term_dst[IT_MAX_WORD+1];
1478 RSET rset[60], result;
1480 struct grep_info grep_info;
1481 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1482 const char *termp = termz;
1484 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1488 logf (LOG_DEBUG, "APT_and_list termp=%s", termp);
1489 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1491 reg_type, complete_flag,
1492 num_bases, basenames,
1493 term_dst, rank_type,
1494 xpath_use, rset_nmem);
1497 assert (rset[rset_no]);
1498 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1501 grep_info_delete (&grep_info);
1503 return rsnull_create (rset_nmem);
1505 /* FIXME - Use a proper rsmultiand */
1506 for (i = 1; i<rset_no; i++)
1509 rset_bool_parms bool_parms;
1510 bool_parms.rset_l = result;
1511 bool_parms.rset_r = rset[i];
1512 bool_parms.key_size = sizeof(struct it_key);
1513 bool_parms.cmp = key_compare_it;
1514 bool_parms.log_item = key_logdump_txt;
1515 result = rset_create (rset_kind_and, &bool_parms);
1517 result= rsbool_create_and(NULL, /* FIXME - use a proper nmem */
1518 sizeof(struct it_key), key_compare_it,
1519 result, rset[i], key_logdump_txt );
1524 static int numeric_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1525 const char **term_sub,
1527 oid_value attributeSet,
1528 struct grep_info *grep_info,
1537 char *term_tmp = term_dict + strlen(term_dict);
1539 attr_init (&relation, zapt, 2);
1540 relation_value = attr_find (&relation, NULL);
1542 logf (LOG_DEBUG, "numeric relation value=%d", relation_value);
1544 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub, term_tmp, 1,
1547 term_value = atoi (term_tmp);
1548 switch (relation_value)
1551 logf (LOG_DEBUG, "Relation <");
1552 gen_regular_rel (term_tmp, term_value-1, 1);
1555 logf (LOG_DEBUG, "Relation <=");
1556 gen_regular_rel (term_tmp, term_value, 1);
1559 logf (LOG_DEBUG, "Relation >=");
1560 gen_regular_rel (term_tmp, term_value, 0);
1563 logf (LOG_DEBUG, "Relation >");
1564 gen_regular_rel (term_tmp, term_value+1, 0);
1568 logf (LOG_DEBUG, "Relation =");
1569 sprintf (term_tmp, "(0*%d)", term_value);
1571 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_tmp);
1572 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info, max_pos,
1575 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1576 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1580 static int numeric_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1581 const char **term_sub,
1582 oid_value attributeSet, struct grep_info *grep_info,
1583 int reg_type, int complete_flag,
1584 int num_bases, char **basenames,
1585 char *term_dst, int xpath_use, NMEM stream)
1587 char term_dict[2*IT_MAX_WORD+2];
1591 const char *use_string = 0;
1592 oid_value curAttributeSet = attributeSet;
1594 struct rpn_char_map_info rcmi;
1596 int bases_ok = 0; /* no of databases with OK attribute */
1597 int errCode = 0; /* err code (if any is not OK) */
1598 char *errString = 0; /* addinfo */
1600 rpn_char_map_prepare (zh->reg, reg_type, &rcmi);
1601 attr_init (&use, zapt, 1);
1602 use_value = attr_find_ex (&use, &curAttributeSet, &use_string);
1604 if (use_value == -1)
1607 for (base_no = 0; base_no < num_bases; base_no++)
1610 data1_local_attribute id_xpath_attr;
1611 data1_local_attribute *local_attr;
1612 int max_pos, prefix_len = 0;
1615 if (use_value == -2) /* string attribute (assume IDXPATH/any) */
1617 use_value = xpath_use;
1618 attp.local_attributes = &id_xpath_attr;
1619 attp.attset_ordinal = VAL_IDXPATH;
1620 id_xpath_attr.next = 0;
1621 id_xpath_attr.local = use_value;
1623 else if (curAttributeSet == VAL_IDXPATH)
1625 attp.local_attributes = &id_xpath_attr;
1626 attp.attset_ordinal = VAL_IDXPATH;
1627 id_xpath_attr.next = 0;
1628 id_xpath_attr.local = use_value;
1632 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value,
1635 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1636 curAttributeSet, use_value, r);
1640 sprintf (val_str, "%d", use_value);
1641 errString = nmem_strdup (stream, val_str);
1649 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
1651 zh->errCode = 109; /* Database unavailable */
1652 zh->errString = basenames[base_no];
1655 for (local_attr = attp.local_attributes; local_attr;
1656 local_attr = local_attr->next)
1662 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
1667 term_dict[prefix_len++] = '|';
1669 term_dict[prefix_len++] = '(';
1671 ord_len = key_SU_encode (ord, ord_buf);
1672 for (i = 0; i<ord_len; i++)
1674 term_dict[prefix_len++] = 1;
1675 term_dict[prefix_len++] = ord_buf[i];
1681 sprintf (val_str, "%d", use_value);
1683 errString = nmem_strdup (stream, val_str);
1687 term_dict[prefix_len++] = ')';
1688 term_dict[prefix_len++] = 1;
1689 term_dict[prefix_len++] = reg_type;
1690 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1691 term_dict[prefix_len] = '\0';
1692 if (!numeric_relation (zh, zapt, &termp, term_dict,
1693 attributeSet, grep_info, &max_pos, reg_type,
1699 zh->errCode = errCode;
1700 zh->errString = errString;
1704 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1708 static RSET rpn_search_APT_numeric (ZebraHandle zh,
1709 Z_AttributesPlusTerm *zapt,
1711 oid_value attributeSet,
1713 int reg_type, int complete_flag,
1714 const char *rank_type, int xpath_use,
1715 int num_bases, char **basenames,
1718 char term_dst[IT_MAX_WORD+1];
1719 const char *termp = termz;
1720 RSET rset[60], result;
1721 int i, r, rset_no = 0;
1722 struct grep_info grep_info;
1724 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1728 logf (LOG_DEBUG, "APT_numeric termp=%s", termp);
1729 grep_info.isam_p_indx = 0;
1730 r = numeric_term (zh, zapt, &termp, attributeSet, &grep_info,
1731 reg_type, complete_flag, num_bases, basenames,
1732 term_dst, xpath_use,
1736 logf (LOG_DEBUG, "term: %s", term_dst);
1737 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1738 grep_info.isam_p_indx, term_dst,
1739 strlen(term_dst), rank_type,
1740 0 /* preserve position */,
1741 zapt->term->which, rset_nmem);
1742 assert (rset[rset_no]);
1743 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1746 grep_info_delete (&grep_info);
1748 return rsnull_create (rset_nmem);
1750 for (i = 1; i<rset_no; i++)
1753 rset_bool_parms bool_parms;
1754 bool_parms.rset_l = result;
1755 bool_parms.rset_r = rset[i];
1756 bool_parms.key_size = sizeof(struct it_key);
1757 bool_parms.cmp = key_compare_it;
1758 bool_parms.log_item = key_logdump_txt;
1759 result = rset_create (rset_kind_and, &bool_parms);
1761 /* FIXME - Use a proper multi-and */
1762 result= rsbool_create_and(NULL, /* FIXME - use a proper nmem */
1763 sizeof(struct it_key), key_compare_it,
1764 result, rset[i], key_logdump_txt );
1769 static RSET rpn_search_APT_local (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1771 oid_value attributeSet,
1773 const char *rank_type, NMEM rset_nmem)
1780 rset_temp_parms parms;
1782 parms.cmp = key_compare_it;
1783 parms.key_size = sizeof (struct it_key);
1784 parms.temp_path = res_get (zh->res, "setTmpDir");
1785 result = rset_create (rset_kind_temp, &parms);
1787 result = rstemp_create( rset_nmem,
1788 sizeof (struct it_key), key_compare_it,
1789 res_get (zh->res, "setTmpDir") );
1790 rsfd = rset_open (result, RSETF_WRITE);
1805 rset_write (rsfd, &key);
1810 static RSET rpn_sort_spec (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1811 oid_value attributeSet, NMEM stream,
1812 Z_SortKeySpecList *sort_sequence,
1813 const char *rank_type)
1816 int sort_relation_value;
1817 AttrType sort_relation_type;
1822 Z_AttributeElement *ae;
1827 attr_init (&sort_relation_type, zapt, 7);
1828 sort_relation_value = attr_find (&sort_relation_type, &attributeSet);
1830 attr_init (&use_type, zapt, 1);
1831 use_value = attr_find (&use_type, &attributeSet);
1833 if (!sort_sequence->specs)
1835 sort_sequence->num_specs = 10;
1836 sort_sequence->specs = (Z_SortKeySpec **)
1837 nmem_malloc (stream, sort_sequence->num_specs *
1838 sizeof(*sort_sequence->specs));
1839 for (i = 0; i<sort_sequence->num_specs; i++)
1840 sort_sequence->specs[i] = 0;
1842 if (zapt->term->which != Z_Term_general)
1845 i = atoi_n ((char *) zapt->term->u.general->buf,
1846 zapt->term->u.general->len);
1847 if (i >= sort_sequence->num_specs)
1849 sprintf (termz, "%d", i);
1851 oe.proto = PROTO_Z3950;
1852 oe.oclass = CLASS_ATTSET;
1853 oe.value = attributeSet;
1854 if (!oid_ent_to_oid (&oe, oid))
1857 sks = (Z_SortKeySpec *) nmem_malloc (stream, sizeof(*sks));
1858 sks->sortElement = (Z_SortElement *)
1859 nmem_malloc (stream, sizeof(*sks->sortElement));
1860 sks->sortElement->which = Z_SortElement_generic;
1861 sk = sks->sortElement->u.generic = (Z_SortKey *)
1862 nmem_malloc (stream, sizeof(*sk));
1863 sk->which = Z_SortKey_sortAttributes;
1864 sk->u.sortAttributes = (Z_SortAttributes *)
1865 nmem_malloc (stream, sizeof(*sk->u.sortAttributes));
1867 sk->u.sortAttributes->id = oid;
1868 sk->u.sortAttributes->list = (Z_AttributeList *)
1869 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list));
1870 sk->u.sortAttributes->list->num_attributes = 1;
1871 sk->u.sortAttributes->list->attributes = (Z_AttributeElement **)
1872 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list->attributes));
1873 ae = *sk->u.sortAttributes->list->attributes = (Z_AttributeElement *)
1874 nmem_malloc (stream, sizeof(**sk->u.sortAttributes->list->attributes));
1875 ae->attributeSet = 0;
1876 ae->attributeType = (int *)
1877 nmem_malloc (stream, sizeof(*ae->attributeType));
1878 *ae->attributeType = 1;
1879 ae->which = Z_AttributeValue_numeric;
1880 ae->value.numeric = (int *)
1881 nmem_malloc (stream, sizeof(*ae->value.numeric));
1882 *ae->value.numeric = use_value;
1884 sks->sortRelation = (int *)
1885 nmem_malloc (stream, sizeof(*sks->sortRelation));
1886 if (sort_relation_value == 1)
1887 *sks->sortRelation = Z_SortKeySpec_ascending;
1888 else if (sort_relation_value == 2)
1889 *sks->sortRelation = Z_SortKeySpec_descending;
1891 *sks->sortRelation = Z_SortKeySpec_ascending;
1893 sks->caseSensitivity = (int *)
1894 nmem_malloc (stream, sizeof(*sks->caseSensitivity));
1895 *sks->caseSensitivity = 0;
1897 sks->which = Z_SortKeySpec_null;
1898 sks->u.null = odr_nullval ();
1899 sort_sequence->specs[i] = sks;
1900 return rsnull_create (NULL); /* FIXME - Use a proper nmem */
1904 static int parse_xpath(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1905 oid_value attributeSet,
1906 struct xpath_location_step *xpath, int max, NMEM mem)
1908 oid_value curAttributeSet = attributeSet;
1910 const char *use_string = 0;
1912 attr_init (&use, zapt, 1);
1913 attr_find_ex (&use, &curAttributeSet, &use_string);
1915 if (!use_string || *use_string != '/')
1918 return zebra_parse_xpath_str(use_string, xpath, max, mem);
1923 static RSET xpath_trunc(ZebraHandle zh, NMEM stream,
1924 int reg_type, const char *term, int use,
1925 oid_value curAttributeSet, NMEM rset_nmem)
1928 struct grep_info grep_info;
1929 char term_dict[2048];
1932 int ord = zebraExplain_lookupSU (zh->reg->zei, curAttributeSet, use);
1933 int ord_len, i, r, max_pos;
1934 int term_type = Z_Term_characterString;
1935 const char *flags = "void";
1937 if (grep_info_prepare (zh, 0 /* zapt */, &grep_info, '0', stream))
1938 return rsnull_create (rset_nmem);
1941 return rsnull_create (rset_nmem);
1943 term_dict[prefix_len++] = '|';
1945 term_dict[prefix_len++] = '(';
1947 ord_len = key_SU_encode (ord, ord_buf);
1948 for (i = 0; i<ord_len; i++)
1950 term_dict[prefix_len++] = 1;
1951 term_dict[prefix_len++] = ord_buf[i];
1953 term_dict[prefix_len++] = ')';
1954 term_dict[prefix_len++] = 1;
1955 term_dict[prefix_len++] = reg_type;
1957 strcpy (term_dict+prefix_len, term);
1959 grep_info.isam_p_indx = 0;
1960 r = dict_lookup_grep (zh->reg->dict, term_dict, 0,
1961 &grep_info, &max_pos, 0, grep_handle);
1962 yaz_log (LOG_LOG, "%s %d positions", term,
1963 grep_info.isam_p_indx);
1964 rset = rset_trunc (zh, grep_info.isam_p_buf,
1965 grep_info.isam_p_indx, term, strlen(term),
1966 flags, 1, term_type,rset_nmem);
1967 grep_info_delete (&grep_info);
1971 static RSET rpn_search_xpath (ZebraHandle zh,
1972 oid_value attributeSet,
1973 int num_bases, char **basenames,
1974 NMEM stream, const char *rank_type, RSET rset,
1975 int xpath_len, struct xpath_location_step *xpath,
1978 oid_value curAttributeSet = attributeSet;
1985 yaz_log (LOG_LOG, "len=%d", xpath_len);
1986 for (i = 0; i<xpath_len; i++)
1988 yaz_log (LOG_LOG, "XPATH %d %s", i, xpath[i].part);
1992 curAttributeSet = VAL_IDXPATH;
2002 a[@attr=value]/b[@other=othervalue]
2004 /e/@a val range(e/,range(@a,freetext(w,1015,val),@a),e/)
2005 /a/b val range(b/a/,freetext(w,1016,val),b/a/)
2006 /a/b/@c val range(b/a/,range(@c,freetext(w,1016,val),@c),b/a/)
2007 /a/b[@c=y] val range(b/a/,freetext(w,1016,val),b/a/,@c=y)
2008 /a[@c=y]/b val range(a/,range(b/a/,freetext(w,1016,val),b/a/),a/,@c=y)
2009 /a[@c=x]/b[@c=y] range(a/,range(b/a/,freetext(w,1016,val),b/a/,@c=y),a/,@c=x)
2013 dict_grep_cmap (zh->reg->dict, 0, 0);
2015 for (base_no = 0; base_no < num_bases; base_no++)
2017 int level = xpath_len;
2020 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
2022 zh->errCode = 109; /* Database unavailable */
2023 zh->errString = basenames[base_no];
2026 while (--level >= 0)
2028 char xpath_rev[128];
2030 RSET rset_start_tag = 0, rset_end_tag = 0, rset_attr = 0;
2034 for (i = level; i >= 1; --i)
2036 const char *cp = xpath[i].part;
2042 memcpy (xpath_rev + len, "[^/]*", 5);
2045 else if (*cp == ' ')
2048 xpath_rev[len++] = 1;
2049 xpath_rev[len++] = ' ';
2053 xpath_rev[len++] = *cp;
2054 xpath_rev[len++] = '/';
2056 else if (i == 1) /* // case */
2058 xpath_rev[len++] = '.';
2059 xpath_rev[len++] = '*';
2064 if (xpath[level].predicate &&
2065 xpath[level].predicate->which == XPATH_PREDICATE_RELATION &&
2066 xpath[level].predicate->u.relation.name[0])
2068 WRBUF wbuf = wrbuf_alloc();
2069 wrbuf_puts(wbuf, xpath[level].predicate->u.relation.name+1);
2070 if (xpath[level].predicate->u.relation.value)
2072 const char *cp = xpath[level].predicate->u.relation.value;
2073 wrbuf_putc(wbuf, '=');
2077 if (strchr(REGEX_CHARS, *cp))
2078 wrbuf_putc(wbuf, '\\');
2079 wrbuf_putc(wbuf, *cp);
2083 wrbuf_puts(wbuf, "");
2084 rset_attr = xpath_trunc (
2085 zh, stream, '0', wrbuf_buf(wbuf), 3,
2086 curAttributeSet,rset_nmem);
2087 wrbuf_free(wbuf, 1);
2094 yaz_log (LOG_LOG, "xpath_rev (%d) = %s", level, xpath_rev);
2095 if (strlen(xpath_rev))
2097 rset_start_tag = xpath_trunc(zh, stream, '0',
2098 xpath_rev, 1, curAttributeSet, rset_nmem);
2100 rset_end_tag = xpath_trunc(zh, stream, '0',
2101 xpath_rev, 2, curAttributeSet, rset_nmem);
2104 parms.key_size = sizeof(struct it_key);
2105 parms.cmp = key_compare_it;
2106 parms.rset_l = rset_start_tag;
2107 parms.rset_m = rset;
2108 parms.rset_r = rset_end_tag;
2109 parms.rset_attr = rset_attr;
2110 parms.printer = key_print_it;
2111 rset = rset_create (rset_kind_between, &parms);
2113 rset=rsbetween_create( rset_nmem,
2114 sizeof(struct it_key), key_compare_it,
2115 rset_start_tag, rset, rset_end_tag, rset_attr,
2127 static RSET rpn_search_APT (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2128 oid_value attributeSet, NMEM stream,
2129 Z_SortKeySpecList *sort_sequence,
2130 int num_bases, char **basenames,
2134 char *search_type = NULL;
2135 char rank_type[128];
2138 char termz[IT_MAX_WORD+1];
2142 struct xpath_location_step xpath[10];
2144 zebra_maps_attr (zh->reg->zebra_maps, zapt, ®_id, &search_type,
2145 rank_type, &complete_flag, &sort_flag);
2147 logf (LOG_DEBUG, "reg_id=%c", reg_id);
2148 logf (LOG_DEBUG, "complete_flag=%d", complete_flag);
2149 logf (LOG_DEBUG, "search_type=%s", search_type);
2150 logf (LOG_DEBUG, "rank_type=%s", rank_type);
2152 if (zapt_term_to_utf8(zh, zapt, termz))
2156 return rpn_sort_spec (zh, zapt, attributeSet, stream, sort_sequence,
2158 xpath_len = parse_xpath(zh, zapt, attributeSet, xpath, 10, stream);
2162 if (xpath[xpath_len-1].part[0] == '@')
2166 if (!strcmp (search_type, "phrase"))
2168 rset = rpn_search_APT_phrase (zh, zapt, termz, attributeSet, stream,
2169 reg_id, complete_flag, rank_type,
2171 num_bases, basenames, rset_nmem);
2173 else if (!strcmp (search_type, "and-list"))
2175 rset = rpn_search_APT_and_list (zh, zapt, termz, attributeSet, stream,
2176 reg_id, complete_flag, rank_type,
2178 num_bases, basenames, rset_nmem);
2180 else if (!strcmp (search_type, "or-list"))
2182 rset = rpn_search_APT_or_list (zh, zapt, termz, attributeSet, stream,
2183 reg_id, complete_flag, rank_type,
2185 num_bases, basenames, rset_nmem);
2187 else if (!strcmp (search_type, "local"))
2189 rset = rpn_search_APT_local (zh, zapt, termz, attributeSet, stream,
2190 rank_type, rset_nmem);
2192 else if (!strcmp (search_type, "numeric"))
2194 rset = rpn_search_APT_numeric (zh, zapt, termz, attributeSet, stream,
2195 reg_id, complete_flag, rank_type,
2197 num_bases, basenames, rset_nmem);
2199 else if (!strcmp (search_type, "always"))
2205 return rpn_search_xpath (zh, attributeSet, num_bases, basenames,
2206 stream, rank_type, rset,
2207 xpath_len, xpath, rset_nmem);
2210 static RSET rpn_search_structure (ZebraHandle zh, Z_RPNStructure *zs,
2211 oid_value attributeSet,
2212 NMEM stream, NMEM rset_nmem,
2213 Z_SortKeySpecList *sort_sequence,
2214 int num_bases, char **basenames)
2217 if (zs->which == Z_RPNStructure_complex)
2219 Z_Operator *zop = zs->u.complex->roperator;
2223 rset_l = rpn_search_structure (zh, zs->u.complex->s1,
2224 attributeSet, stream, rset_nmem,
2226 num_bases, basenames);
2229 rset_r = rpn_search_structure (zh, zs->u.complex->s2,
2230 attributeSet, stream, rset_nmem,
2232 num_bases, basenames);
2235 rset_delete (rset_l);
2241 case Z_Operator_and:
2242 r = rsbool_create_and( rset_nmem,
2243 sizeof(struct it_key), key_compare_it,
2244 rset_l, rset_r, key_logdump_txt );
2247 r = rsbool_create_or( rset_nmem,
2248 sizeof(struct it_key), key_compare_it,
2249 rset_l, rset_r, key_logdump_txt );
2251 case Z_Operator_and_not:
2252 r = rsbool_create_not( rset_nmem,
2253 sizeof(struct it_key), key_compare_it,
2254 rset_l, rset_r, key_logdump_txt );
2256 case Z_Operator_prox:
2257 if (zop->u.prox->which != Z_ProximityOperator_known)
2262 if (*zop->u.prox->u.known != Z_ProxUnit_word)
2264 char *val = (char *) nmem_malloc (stream, 16);
2266 zh->errString = val;
2267 sprintf (val, "%d", *zop->u.prox->u.known);
2272 /* new / old prox */
2275 twosets[0] = rset_l;
2276 twosets[1] = rset_r;
2277 r=rsprox_create(rset_nmem,
2278 sizeof(struct it_key), key_compare_it,
2281 *zop->u.prox->ordered,
2282 (!zop->u.prox->exclusion ?
2283 0 : *zop->u.prox->exclusion),
2284 *zop->u.prox->relationType,
2285 *zop->u.prox->distance );
2287 parms.rset = twosets;
2289 parms.ordered = *zop->u.prox->ordered;
2290 parms.exclusion = (!zop->u.prox->exclusion ? 0 :
2291 *zop->u.prox->exclusion);
2292 parms.relation = *zop->u.prox->relationType;
2293 parms.distance = *zop->u.prox->distance;
2294 parms.key_size = sizeof(struct it_key);
2295 parms.cmp = key_compare_it;
2296 parms.getseq = key_get_seq;
2297 parms.log_item = key_logdump_txt;
2298 r = rset_create(rset_kind_prox, &parms);
2307 else if (zs->which == Z_RPNStructure_simple)
2309 if (zs->u.simple->which == Z_Operand_APT)
2311 logf (LOG_DEBUG, "rpn_search_APT");
2312 r = rpn_search_APT (zh, zs->u.simple->u.attributesPlusTerm,
2313 attributeSet, stream, sort_sequence,
2314 num_bases, basenames,rset_nmem);
2316 else if (zs->u.simple->which == Z_Operand_resultSetId)
2318 logf (LOG_DEBUG, "rpn_search_ref");
2319 r = resultSetRef (zh, zs->u.simple->u.resultSetId);
2322 r = rsnull_create (rset_nmem);
2325 nmem_strdup (stream, zs->u.simple->u.resultSetId);
2346 RSET rpn_search (ZebraHandle zh, NMEM nmem, NMEM rset_nmem,
2347 Z_RPNQuery *rpn, int num_bases, char **basenames,
2348 const char *setname,
2353 oid_value attributeSet;
2354 Z_SortKeySpecList *sort_sequence;
2358 zh->errString = NULL;
2361 sort_sequence = (Z_SortKeySpecList *)
2362 nmem_malloc (nmem, sizeof(*sort_sequence));
2363 sort_sequence->num_specs = 10;
2364 sort_sequence->specs = (Z_SortKeySpec **)
2365 nmem_malloc (nmem, sort_sequence->num_specs *
2366 sizeof(*sort_sequence->specs));
2367 for (i = 0; i<sort_sequence->num_specs; i++)
2368 sort_sequence->specs[i] = 0;
2370 attrset = oid_getentbyoid (rpn->attributeSetId);
2371 attributeSet = attrset->value;
2372 rset = rpn_search_structure (zh, rpn->RPNStructure, attributeSet,
2374 sort_sequence, num_bases, basenames);
2379 logf (LOG_DEBUG, "search error: %d", zh->errCode);
2381 for (i = 0; sort_sequence->specs[i]; i++)
2383 sort_sequence->num_specs = i;
2385 resultSetRank (zh, sset, rset);
2388 logf (LOG_DEBUG, "resultSetSortSingle in rpn_search");
2389 resultSetSortSingle (zh, nmem, sset, rset,
2390 sort_sequence, &sort_status);
2393 logf (LOG_DEBUG, "resultSetSortSingle status = %d", zh->errCode);
2399 struct scan_info_entry {
2405 struct scan_info_entry *list;
2411 static int scan_handle (char *name, const char *info, int pos, void *client)
2413 int len_prefix, idx;
2414 struct scan_info *scan_info = (struct scan_info *) client;
2416 len_prefix = strlen(scan_info->prefix);
2417 if (memcmp (name, scan_info->prefix, len_prefix))
2419 if (pos > 0) idx = scan_info->after - pos + scan_info->before;
2422 scan_info->list[idx].term = (char *)
2423 odr_malloc (scan_info->odr, strlen(name + len_prefix)+1);
2424 strcpy (scan_info->list[idx].term, name + len_prefix);
2425 assert (*info == sizeof(ISAMC_P));
2426 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAMC_P));
2430 static void scan_term_untrans (ZebraHandle zh, NMEM stream, int reg_type,
2431 char **dst, const char *src)
2433 char term_src[IT_MAX_WORD];
2434 char term_dst[IT_MAX_WORD];
2436 term_untrans (zh, reg_type, term_src, src);
2438 if (zh->iconv_from_utf8 != 0)
2441 char *inbuf = term_src;
2442 size_t inleft = strlen(term_src);
2443 char *outbuf = term_dst;
2444 size_t outleft = sizeof(term_dst)-1;
2447 ret = yaz_iconv (zh->iconv_from_utf8, &inbuf, &inleft,
2449 if (ret == (size_t)(-1))
2452 len = outbuf - term_dst;
2453 *dst = nmem_malloc (stream, len + 1);
2455 memcpy (*dst, term_dst, len);
2459 *dst = nmem_strdup (stream, term_src);
2462 static void count_set (RSET r, int *count)
2469 logf (LOG_DEBUG, "count_set");
2472 rfd = rset_open (r, RSETF_READ);
2473 while (rset_read (rfd, &key))
2476 if (key.mem[0] != psysno)
2478 psysno = key.mem[0];
2482 if (key.sysno != psysno)
2491 logf (LOG_DEBUG, "%d keys, %d records", kno, *count);
2494 void rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
2495 oid_value attributeset,
2496 int num_bases, char **basenames,
2497 int *position, int *num_entries, ZebraScanEntry **list,
2498 int *is_partial, RSET limit_set, int return_zero)
2501 int pos = *position;
2502 int num = *num_entries;
2506 char termz[IT_MAX_WORD+20];
2509 const char *use_string = 0;
2510 struct scan_info *scan_info_array;
2511 ZebraScanEntry *glist;
2512 int ords[32], ord_no = 0;
2515 int bases_ok = 0; /* no of databases with OK attribute */
2516 int errCode = 0; /* err code (if any is not OK) */
2517 char *errString = 0; /* addinfo */
2520 char *search_type = NULL;
2521 char rank_type[128];
2524 NMEM rset_nmem=NULL;
2528 if (attributeset == VAL_NONE)
2529 attributeset = VAL_BIB1;
2534 int termset_value_numeric;
2535 const char *termset_value_string;
2536 attr_init (&termset, zapt, 8);
2537 termset_value_numeric =
2538 attr_find_ex (&termset, NULL, &termset_value_string);
2539 if (termset_value_numeric != -1)
2542 const char *termset_name = 0;
2544 if (termset_value_numeric != -2)
2547 sprintf (resname, "%d", termset_value_numeric);
2548 termset_name = resname;
2551 termset_name = termset_value_string;
2553 limit_set = resultSetRef (zh, termset_name);
2557 yaz_log (LOG_DEBUG, "position = %d, num = %d set=%d",
2558 pos, num, attributeset);
2560 attr_init (&use, zapt, 1);
2561 use_value = attr_find_ex (&use, &attributeset, &use_string);
2563 if (zebra_maps_attr (zh->reg->zebra_maps, zapt, ®_id, &search_type,
2564 rank_type, &complete_flag, &sort_flag))
2570 yaz_log (LOG_DEBUG, "use_value = %d", use_value);
2572 if (use_value == -1)
2574 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
2578 data1_local_attribute *local_attr;
2580 if ((r=att_getentbyatt (zh, &attp, attributeset, use_value,
2583 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
2584 attributeset, use_value);
2588 sprintf (val_str, "%d", use_value);
2590 errString = odr_strdup (stream, val_str);
2596 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
2598 zh->errString = basenames[base_no];
2599 zh->errCode = 109; /* Database unavailable */
2604 for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
2605 local_attr = local_attr->next)
2609 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
2612 ords[ord_no++] = ord;
2615 if (!bases_ok && errCode)
2617 zh->errCode = errCode;
2618 zh->errString = errString;
2624 sprintf (val_str, "%d", use_value);
2626 zh->errString = odr_strdup (stream, val_str);
2631 /* prepare dictionary scanning */
2634 scan_info_array = (struct scan_info *)
2635 odr_malloc (stream, ord_no * sizeof(*scan_info_array));
2636 for (i = 0; i < ord_no; i++)
2638 int j, prefix_len = 0;
2639 int before_tmp = before, after_tmp = after;
2640 struct scan_info *scan_info = scan_info_array + i;
2641 struct rpn_char_map_info rcmi;
2643 rpn_char_map_prepare (zh->reg, reg_id, &rcmi);
2645 scan_info->before = before;
2646 scan_info->after = after;
2647 scan_info->odr = stream;
2649 scan_info->list = (struct scan_info_entry *)
2650 odr_malloc (stream, (before+after) * sizeof(*scan_info->list));
2651 for (j = 0; j<before+after; j++)
2652 scan_info->list[j].term = NULL;
2654 prefix_len += key_SU_encode (ords[i], termz + prefix_len);
2655 termz[prefix_len++] = reg_id;
2656 termz[prefix_len] = 0;
2657 strcpy (scan_info->prefix, termz);
2659 if (trans_scan_term (zh, zapt, termz+prefix_len, reg_id))
2662 dict_scan (zh->reg->dict, termz, &before_tmp, &after_tmp,
2663 scan_info, scan_handle);
2665 glist = (ZebraScanEntry *)
2666 odr_malloc (stream, (before+after)*sizeof(*glist));
2668 rset_nmem=nmem_create();
2670 /* consider terms after main term */
2671 for (i = 0; i < ord_no; i++)
2675 for (i = 0; i<after; i++)
2678 const char *mterm = NULL;
2682 for (j = 0; j < ord_no; j++)
2684 if (ptr[j] < before+after &&
2685 (tst=scan_info_array[j].list[ptr[j]].term) &&
2686 (!mterm || strcmp (tst, mterm) < 0))
2694 scan_term_untrans (zh, stream->mem, reg_id,
2695 &glist[i+before].term, mterm);
2696 rset = rset_trunc (zh, &scan_info_array[j0].list[ptr[j0]].isam_p, 1,
2697 glist[i+before].term, strlen(glist[i+before].term),
2698 NULL, 0, zapt->term->which, rset_nmem);
2701 for (j = j0+1; j<ord_no; j++)
2703 if (ptr[j] < before+after &&
2704 (tst=scan_info_array[j].list[ptr[j]].term) &&
2705 !strcmp (tst, mterm))
2710 rset_trunc (zh, &scan_info_array[j].list[ptr[j]].isam_p, 1,
2711 glist[i+before].term,
2712 strlen(glist[i+before].term), NULL, 0,
2713 zapt->term->which,rset_nmem);
2714 rset = rsbool_create_or(rset_nmem,
2715 sizeof(struct it_key), key_compare_it,
2716 rset, rset2, key_logdump_txt);
2722 rset = rsbool_create_and(rset_nmem,
2723 sizeof(struct it_key), key_compare_it,
2724 rset, rset_dup(limit_set),
2726 count_set (rset, &glist[i+before].occurrences);
2731 *num_entries -= (after-i);
2735 /* consider terms before main term */
2736 for (i = 0; i<ord_no; i++)
2739 for (i = 0; i<before; i++)
2742 const char *mterm = NULL;
2746 for (j = 0; j <ord_no; j++)
2748 if (ptr[j] < before &&
2749 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2750 (!mterm || strcmp (tst, mterm) > 0))
2759 scan_term_untrans (zh, stream->mem, reg_id,
2760 &glist[before-1-i].term, mterm);
2763 (zh, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1,
2764 glist[before-1-i].term, strlen(glist[before-1-i].term),
2765 NULL, 0, zapt->term->which,rset_nmem);
2769 for (j = j0+1; j<ord_no; j++)
2771 if (ptr[j] < before &&
2772 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2773 !strcmp (tst, mterm))
2777 rset2 = rset_trunc (zh,
2778 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1,
2779 glist[before-1-i].term,
2780 strlen(glist[before-1-i].term), NULL, 0,
2781 zapt->term->which, rset_nmem);
2782 rset = rsbool_create_and(rset_nmem,
2783 sizeof(struct it_key), key_compare_it,
2790 rset = rsbool_create_and(rset_nmem,
2791 sizeof(struct it_key), key_compare_it,
2792 rset, rset_dup(limit_set),
2794 count_set (rset, &glist[before-1-i].occurrences);
2805 nmem_destroy(rset_nmem);
2806 *list = glist + i; /* list is set to first 'real' entry */
2808 logf (LOG_DEBUG, "position = %d, num_entries = %d",
2809 *position, *num_entries);
2811 logf (LOG_DEBUG, "scan error: %d", zh->errCode);