1 /* $Id: zrpn.c,v 1.145 2004-08-24 14:25:16 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)
883 grep_info->isam_p_indx = 0;
884 r = string_term (zh, zapt, term_sub, attributeSet, stream, grep_info,
885 reg_type, complete_flag, num_bases, basenames,
886 term_dst, xpath_use);
889 logf (LOG_DEBUG, "term: %s", term_dst);
890 return rset_trunc (zh, grep_info->isam_p_buf,
891 grep_info->isam_p_indx, term_dst,
892 strlen(term_dst), rank_type, 1 /* preserve pos */,
897 static int string_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
898 const char **term_sub,
899 oid_value attributeSet, NMEM stream,
900 struct grep_info *grep_info,
901 int reg_type, int complete_flag,
902 int num_bases, char **basenames,
903 char *term_dst, int xpath_use)
905 char term_dict[2*IT_MAX_WORD+4000];
908 int truncation_value;
911 const char *use_string = 0;
912 oid_value curAttributeSet = attributeSet;
914 struct rpn_char_map_info rcmi;
915 int space_split = complete_flag ? 0 : 1;
917 int bases_ok = 0; /* no of databases with OK attribute */
918 int errCode = 0; /* err code (if any is not OK) */
919 char *errString = 0; /* addinfo */
921 rpn_char_map_prepare (zh->reg, reg_type, &rcmi);
922 attr_init (&use, zapt, 1);
923 use_value = attr_find_ex (&use, &curAttributeSet, &use_string);
924 logf (LOG_DEBUG, "string_term, use value %d", use_value);
925 attr_init (&truncation, zapt, 5);
926 truncation_value = attr_find (&truncation, NULL);
927 logf (LOG_DEBUG, "truncation value %d", truncation_value);
929 if (use_value == -1) /* no attribute - assumy "any" */
931 for (base_no = 0; base_no < num_bases; base_no++)
934 data1_local_attribute id_xpath_attr;
935 data1_local_attribute *local_attr;
936 int max_pos, prefix_len = 0;
940 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
942 zh->errCode = 109; /* Database unavailable */
943 zh->errString = basenames[base_no];
946 if (xpath_use > 0 && use_value == -2)
948 use_value = xpath_use;
949 attp.local_attributes = &id_xpath_attr;
950 attp.attset_ordinal = VAL_IDXPATH;
951 id_xpath_attr.next = 0;
952 id_xpath_attr.local = use_value;
954 else if (curAttributeSet == VAL_IDXPATH)
956 attp.local_attributes = &id_xpath_attr;
957 attp.attset_ordinal = VAL_IDXPATH;
958 id_xpath_attr.next = 0;
959 id_xpath_attr.local = use_value;
963 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value,
966 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
967 curAttributeSet, use_value, r);
970 /* set was found, but value wasn't defined */
973 errString = nmem_strdup(stream, use_string);
977 sprintf (val_str, "%d", use_value);
978 errString = nmem_strdup (stream, val_str);
984 struct oident oident;
986 oident.proto = PROTO_Z3950;
987 oident.oclass = CLASS_ATTSET;
988 oident.value = curAttributeSet;
989 oid_ent_to_oid (&oident, oid);
992 errString = nmem_strdup (stream, oident.desc);
997 for (local_attr = attp.local_attributes; local_attr;
998 local_attr = local_attr->next)
1004 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
1009 term_dict[prefix_len++] = '|';
1011 term_dict[prefix_len++] = '(';
1013 ord_len = key_SU_encode (ord, ord_buf);
1014 for (i = 0; i<ord_len; i++)
1016 term_dict[prefix_len++] = 1;
1017 term_dict[prefix_len++] = ord_buf[i];
1026 sprintf (val_str, "%d", use_value);
1028 errString = nmem_strdup (stream, val_str);
1032 bases_ok++; /* this has OK attributes */
1034 term_dict[prefix_len++] = ')';
1035 term_dict[prefix_len++] = 1;
1036 term_dict[prefix_len++] = reg_type;
1037 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1038 term_dict[prefix_len] = '\0';
1040 switch (truncation_value)
1042 case -1: /* not specified */
1043 case 100: /* do not truncate */
1044 if (!string_relation (zh, zapt, &termp, term_dict,
1046 reg_type, space_split, term_dst))
1048 logf (LOG_LOG, "dict_lookup_grep: %s", term_dict+prefix_len);
1049 r = dict_lookup_grep (zh->reg->dict, term_dict, 0,
1050 grep_info, &max_pos, 0, grep_handle);
1052 logf (LOG_WARN, "dict_lookup_grep fail %d", r);
1054 case 1: /* right truncation */
1055 term_dict[j++] = '(';
1056 if (!term_100 (zh->reg->zebra_maps, reg_type,
1057 &termp, term_dict + j, space_split, term_dst))
1059 strcat (term_dict, ".*)");
1060 dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1061 &max_pos, 0, grep_handle);
1063 case 2: /* keft truncation */
1064 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1065 if (!term_100 (zh->reg->zebra_maps, reg_type,
1066 &termp, term_dict + j, space_split, term_dst))
1068 strcat (term_dict, ")");
1069 dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1070 &max_pos, 0, grep_handle);
1072 case 3: /* left&right truncation */
1073 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
1074 if (!term_100 (zh->reg->zebra_maps, reg_type,
1075 &termp, term_dict + j, space_split, term_dst))
1077 strcat (term_dict, ".*)");
1078 dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1079 &max_pos, 0, grep_handle);
1083 case 101: /* process # in term */
1084 term_dict[j++] = '(';
1085 if (!term_101 (zh->reg->zebra_maps, reg_type,
1086 &termp, term_dict + j, space_split, term_dst))
1088 strcat (term_dict, ")");
1089 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1090 &max_pos, 0, grep_handle);
1092 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d", r);
1094 case 102: /* Regexp-1 */
1095 term_dict[j++] = '(';
1096 if (!term_102 (zh->reg->zebra_maps, reg_type,
1097 &termp, term_dict + j, space_split, term_dst))
1099 strcat (term_dict, ")");
1100 logf (LOG_DEBUG, "Regexp-1 tolerance=%d", r);
1101 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1102 &max_pos, 0, grep_handle);
1104 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
1107 case 103: /* Regexp-2 */
1109 term_dict[j++] = '(';
1110 if (!term_103 (zh->reg->zebra_maps, reg_type,
1111 &termp, term_dict + j, &r, space_split, term_dst))
1113 strcat (term_dict, ")");
1114 logf (LOG_DEBUG, "Regexp-2 tolerance=%d", r);
1115 r = dict_lookup_grep (zh->reg->dict, term_dict, r, grep_info,
1116 &max_pos, 2, grep_handle);
1118 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
1121 case 104: /* process # and ! in term */
1122 term_dict[j++] = '(';
1123 if (!term_104 (zh->reg->zebra_maps, reg_type,
1124 &termp, term_dict + j, space_split, term_dst))
1126 strcat (term_dict, ")");
1127 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1128 &max_pos, 0, grep_handle);
1130 logf (LOG_WARN, "dict_lookup_grep err, trunc=#/!: %d", r);
1132 case 105: /* process * and ! in term */
1133 term_dict[j++] = '(';
1134 if (!term_105 (zh->reg->zebra_maps, reg_type,
1135 &termp, term_dict + j, space_split, term_dst, 1))
1137 strcat (term_dict, ")");
1138 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1139 &max_pos, 0, grep_handle);
1141 logf (LOG_WARN, "dict_lookup_grep err, trunc=*/!: %d", r);
1143 case 106: /* process * and ! in term */
1144 term_dict[j++] = '(';
1145 if (!term_105 (zh->reg->zebra_maps, reg_type,
1146 &termp, term_dict + j, space_split, term_dst, 0))
1148 strcat (term_dict, ")");
1149 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1150 &max_pos, 0, grep_handle);
1152 logf (LOG_WARN, "dict_lookup_grep err, trunc=*/!: %d", r);
1158 zh->errCode = errCode;
1159 zh->errString = errString;
1163 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1168 /* convert APT search term to UTF8 */
1169 static int zapt_term_to_utf8 (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1173 Z_Term *term = zapt->term;
1175 switch (term->which)
1177 case Z_Term_general:
1178 if (zh->iconv_to_utf8 != 0)
1180 char *inbuf = term->u.general->buf;
1181 size_t inleft = term->u.general->len;
1182 char *outbuf = termz;
1183 size_t outleft = IT_MAX_WORD-1;
1186 ret = yaz_iconv(zh->iconv_to_utf8, &inbuf, &inleft,
1188 if (ret == (size_t)(-1))
1190 ret = yaz_iconv(zh->iconv_to_utf8, 0, 0, 0, 0);
1198 sizez = term->u.general->len;
1199 if (sizez > IT_MAX_WORD-1)
1200 sizez = IT_MAX_WORD-1;
1201 memcpy (termz, term->u.general->buf, sizez);
1202 termz[sizez] = '\0';
1205 case Z_Term_characterString:
1206 sizez = strlen(term->u.characterString);
1207 if (sizez > IT_MAX_WORD-1)
1208 sizez = IT_MAX_WORD-1;
1209 memcpy (termz, term->u.characterString, sizez);
1210 termz[sizez] = '\0';
1219 /* convert APT SCAN term to internal cmap */
1220 static int trans_scan_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1221 char *termz, int reg_type)
1223 char termz0[IT_MAX_WORD];
1225 if (zapt_term_to_utf8(zh, zapt, termz0))
1226 return -1; /* error */
1230 const char *cp = (const char *) termz0;
1231 const char *cp_end = cp + strlen(cp);
1234 const char *space_map = NULL;
1237 while ((len = (cp_end - cp)) > 0)
1239 map = zebra_maps_input (zh->reg->zebra_maps, reg_type, &cp, len);
1240 if (**map == *CHR_SPACE)
1245 for (src = space_map; *src; src++)
1248 for (src = *map; *src; src++)
1257 char *normalize_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1258 const char *termz, NMEM stream, unsigned reg_id)
1261 AttrType truncation;
1262 int truncation_value;
1265 attr_init (&truncation, zapt, 5);
1266 truncation_value = attr_find (&truncation, NULL);
1268 switch (truncation_value)
1288 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, ex_list,
1289 termz, strlen(termz));
1291 return nmem_strdup(stream, termz);
1294 char *buf = (char*) nmem_malloc (stream, wrbuf_len(wrbuf)+1);
1295 memcpy (buf, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1296 buf[wrbuf_len(wrbuf)] = '\0';
1301 static void grep_info_delete (struct grep_info *grep_info)
1304 xfree(grep_info->term_no);
1306 xfree (grep_info->isam_p_buf);
1309 static int grep_info_prepare (ZebraHandle zh,
1310 Z_AttributesPlusTerm *zapt,
1311 struct grep_info *grep_info,
1316 int termset_value_numeric;
1317 const char *termset_value_string;
1320 grep_info->term_no = 0;
1322 grep_info->isam_p_size = 0;
1323 grep_info->isam_p_buf = NULL;
1325 grep_info->reg_type = reg_type;
1326 grep_info->termset = 0;
1330 attr_init (&termset, zapt, 8);
1331 termset_value_numeric =
1332 attr_find_ex (&termset, NULL, &termset_value_string);
1333 if (termset_value_numeric != -1)
1336 const char *termset_name = 0;
1337 if (termset_value_numeric != -2)
1340 sprintf (resname, "%d", termset_value_numeric);
1341 termset_name = resname;
1344 termset_name = termset_value_string;
1345 logf (LOG_LOG, "creating termset set %s", termset_name);
1346 grep_info->termset = resultSetAdd (zh, termset_name, 1);
1347 if (!grep_info->termset)
1350 zh->errString = nmem_strdup (stream, termset_name);
1358 static RSET rpn_search_APT_phrase (ZebraHandle zh,
1359 Z_AttributesPlusTerm *zapt,
1360 const char *termz_org,
1361 oid_value attributeSet,
1363 int reg_type, int complete_flag,
1364 const char *rank_type, int xpath_use,
1365 int num_bases, char **basenames)
1367 char term_dst[IT_MAX_WORD+1];
1368 RSET rset[60], result;
1370 struct grep_info grep_info;
1371 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1372 const char *termp = termz;
1375 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1379 logf (LOG_DEBUG, "APT_phrase termp=%s", termp);
1380 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1382 reg_type, complete_flag,
1383 num_bases, basenames,
1384 term_dst, rank_type,
1388 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1391 grep_info_delete (&grep_info);
1393 return rsnull_create (NULL); /* FIXME - Use a proper nmem */
1394 else if (rset_no == 1)
1397 result = rsprox_create( NULL, /* FIXME - use a proper nmem */
1398 sizeof(struct it_key), key_compare_it, key_get_seq,
1400 1 /* ordered */, 0 /* exclusion */,
1401 3 /* relation */, 1 /* distance */);
1405 static RSET rpn_search_APT_or_list (ZebraHandle zh,
1406 Z_AttributesPlusTerm *zapt,
1407 const char *termz_org,
1408 oid_value attributeSet,
1410 int reg_type, int complete_flag,
1411 const char *rank_type,
1413 int num_bases, char **basenames)
1415 char term_dst[IT_MAX_WORD+1];
1418 struct grep_info grep_info;
1419 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1420 const char *termp = termz;
1422 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1426 logf (LOG_DEBUG, "APT_or_list termp=%s", termp);
1427 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1429 reg_type, complete_flag,
1430 num_bases, basenames,
1431 term_dst, rank_type,
1435 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1438 grep_info_delete (&grep_info);
1440 return rsnull_create (NULL); /* FIXME - Use a proper nmem */
1441 return rsmultior_create(NULL, /* FIXME */
1442 sizeof(struct it_key),
1447 /* The old code, kept for reference. May be deleted soon */
1449 for (i = 1; i<rset_no; i++)
1451 rset_bool_parms bool_parms;
1452 bool_parms.rset_l = result;
1453 bool_parms.rset_r = rset[i];
1454 bool_parms.key_size = sizeof(struct it_key);
1455 bool_parms.cmp = key_compare_it;
1456 bool_parms.log_item = key_logdump_txt;
1457 result = rset_create (rset_kind_or, &bool_parms);
1463 static RSET rpn_search_APT_and_list (ZebraHandle zh,
1464 Z_AttributesPlusTerm *zapt,
1465 const char *termz_org,
1466 oid_value attributeSet,
1468 int reg_type, int complete_flag,
1469 const char *rank_type,
1471 int num_bases, char **basenames)
1473 char term_dst[IT_MAX_WORD+1];
1474 RSET rset[60], result;
1476 struct grep_info grep_info;
1477 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1478 const char *termp = termz;
1480 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1484 logf (LOG_DEBUG, "APT_and_list termp=%s", termp);
1485 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1487 reg_type, complete_flag,
1488 num_bases, basenames,
1489 term_dst, rank_type,
1493 assert (rset[rset_no]);
1494 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1497 grep_info_delete (&grep_info);
1499 return rsnull_create (NULL); /* FIXME - Use a proper nmem */
1501 /* FIXME - Use a proper rsmultiand */
1502 for (i = 1; i<rset_no; i++)
1505 rset_bool_parms bool_parms;
1506 bool_parms.rset_l = result;
1507 bool_parms.rset_r = rset[i];
1508 bool_parms.key_size = sizeof(struct it_key);
1509 bool_parms.cmp = key_compare_it;
1510 bool_parms.log_item = key_logdump_txt;
1511 result = rset_create (rset_kind_and, &bool_parms);
1513 result= rsbool_create_and(NULL, /* FIXME - use a proper nmem */
1514 sizeof(struct it_key), key_compare_it,
1515 result, rset[i], key_logdump_txt );
1520 static int numeric_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1521 const char **term_sub,
1523 oid_value attributeSet,
1524 struct grep_info *grep_info,
1533 char *term_tmp = term_dict + strlen(term_dict);
1535 attr_init (&relation, zapt, 2);
1536 relation_value = attr_find (&relation, NULL);
1538 logf (LOG_DEBUG, "numeric relation value=%d", relation_value);
1540 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub, term_tmp, 1,
1543 term_value = atoi (term_tmp);
1544 switch (relation_value)
1547 logf (LOG_DEBUG, "Relation <");
1548 gen_regular_rel (term_tmp, term_value-1, 1);
1551 logf (LOG_DEBUG, "Relation <=");
1552 gen_regular_rel (term_tmp, term_value, 1);
1555 logf (LOG_DEBUG, "Relation >=");
1556 gen_regular_rel (term_tmp, term_value, 0);
1559 logf (LOG_DEBUG, "Relation >");
1560 gen_regular_rel (term_tmp, term_value+1, 0);
1564 logf (LOG_DEBUG, "Relation =");
1565 sprintf (term_tmp, "(0*%d)", term_value);
1567 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_tmp);
1568 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info, max_pos,
1571 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1572 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1576 static int numeric_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1577 const char **term_sub,
1578 oid_value attributeSet, struct grep_info *grep_info,
1579 int reg_type, int complete_flag,
1580 int num_bases, char **basenames,
1581 char *term_dst, int xpath_use, NMEM stream)
1583 char term_dict[2*IT_MAX_WORD+2];
1587 const char *use_string = 0;
1588 oid_value curAttributeSet = attributeSet;
1590 struct rpn_char_map_info rcmi;
1592 int bases_ok = 0; /* no of databases with OK attribute */
1593 int errCode = 0; /* err code (if any is not OK) */
1594 char *errString = 0; /* addinfo */
1596 rpn_char_map_prepare (zh->reg, reg_type, &rcmi);
1597 attr_init (&use, zapt, 1);
1598 use_value = attr_find_ex (&use, &curAttributeSet, &use_string);
1600 if (use_value == -1)
1603 for (base_no = 0; base_no < num_bases; base_no++)
1606 data1_local_attribute id_xpath_attr;
1607 data1_local_attribute *local_attr;
1608 int max_pos, prefix_len = 0;
1611 if (use_value == -2) /* string attribute (assume IDXPATH/any) */
1613 use_value = xpath_use;
1614 attp.local_attributes = &id_xpath_attr;
1615 attp.attset_ordinal = VAL_IDXPATH;
1616 id_xpath_attr.next = 0;
1617 id_xpath_attr.local = use_value;
1619 else if (curAttributeSet == VAL_IDXPATH)
1621 attp.local_attributes = &id_xpath_attr;
1622 attp.attset_ordinal = VAL_IDXPATH;
1623 id_xpath_attr.next = 0;
1624 id_xpath_attr.local = use_value;
1628 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value,
1631 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1632 curAttributeSet, use_value, r);
1636 sprintf (val_str, "%d", use_value);
1637 errString = nmem_strdup (stream, val_str);
1645 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
1647 zh->errCode = 109; /* Database unavailable */
1648 zh->errString = basenames[base_no];
1651 for (local_attr = attp.local_attributes; local_attr;
1652 local_attr = local_attr->next)
1658 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
1663 term_dict[prefix_len++] = '|';
1665 term_dict[prefix_len++] = '(';
1667 ord_len = key_SU_encode (ord, ord_buf);
1668 for (i = 0; i<ord_len; i++)
1670 term_dict[prefix_len++] = 1;
1671 term_dict[prefix_len++] = ord_buf[i];
1677 sprintf (val_str, "%d", use_value);
1679 errString = nmem_strdup (stream, val_str);
1683 term_dict[prefix_len++] = ')';
1684 term_dict[prefix_len++] = 1;
1685 term_dict[prefix_len++] = reg_type;
1686 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1687 term_dict[prefix_len] = '\0';
1688 if (!numeric_relation (zh, zapt, &termp, term_dict,
1689 attributeSet, grep_info, &max_pos, reg_type,
1695 zh->errCode = errCode;
1696 zh->errString = errString;
1700 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1704 static RSET rpn_search_APT_numeric (ZebraHandle zh,
1705 Z_AttributesPlusTerm *zapt,
1707 oid_value attributeSet,
1709 int reg_type, int complete_flag,
1710 const char *rank_type, int xpath_use,
1711 int num_bases, char **basenames)
1713 char term_dst[IT_MAX_WORD+1];
1714 const char *termp = termz;
1715 RSET rset[60], result;
1716 int i, r, rset_no = 0;
1717 struct grep_info grep_info;
1719 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1723 logf (LOG_DEBUG, "APT_numeric termp=%s", termp);
1724 grep_info.isam_p_indx = 0;
1725 r = numeric_term (zh, zapt, &termp, attributeSet, &grep_info,
1726 reg_type, complete_flag, num_bases, basenames,
1727 term_dst, xpath_use,
1731 logf (LOG_DEBUG, "term: %s", term_dst);
1732 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1733 grep_info.isam_p_indx, term_dst,
1734 strlen(term_dst), rank_type,
1735 0 /* preserve position */,
1737 assert (rset[rset_no]);
1738 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1741 grep_info_delete (&grep_info);
1743 return rsnull_create (NULL); /* FIXME - Use a proper nmem */
1745 for (i = 1; i<rset_no; i++)
1748 rset_bool_parms bool_parms;
1749 bool_parms.rset_l = result;
1750 bool_parms.rset_r = rset[i];
1751 bool_parms.key_size = sizeof(struct it_key);
1752 bool_parms.cmp = key_compare_it;
1753 bool_parms.log_item = key_logdump_txt;
1754 result = rset_create (rset_kind_and, &bool_parms);
1756 /* FIXME - Use a proper multi-and */
1757 result= rsbool_create_and(NULL, /* FIXME - use a proper nmem */
1758 sizeof(struct it_key), key_compare_it,
1759 result, rset[i], key_logdump_txt );
1764 static RSET rpn_search_APT_local (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1766 oid_value attributeSet,
1768 const char *rank_type)
1775 rset_temp_parms parms;
1777 parms.cmp = key_compare_it;
1778 parms.key_size = sizeof (struct it_key);
1779 parms.temp_path = res_get (zh->res, "setTmpDir");
1780 result = rset_create (rset_kind_temp, &parms);
1782 result = rstemp_create( NULL, /* FIXME - use a proper nmem */
1783 sizeof (struct it_key), key_compare_it,
1784 res_get (zh->res, "setTmpDir") );
1785 rsfd = rset_open (result, RSETF_WRITE);
1800 rset_write (result, rsfd, &key);
1801 rset_close (result, rsfd);
1805 static RSET rpn_sort_spec (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1806 oid_value attributeSet, NMEM stream,
1807 Z_SortKeySpecList *sort_sequence,
1808 const char *rank_type)
1811 int sort_relation_value;
1812 AttrType sort_relation_type;
1817 Z_AttributeElement *ae;
1822 attr_init (&sort_relation_type, zapt, 7);
1823 sort_relation_value = attr_find (&sort_relation_type, &attributeSet);
1825 attr_init (&use_type, zapt, 1);
1826 use_value = attr_find (&use_type, &attributeSet);
1828 if (!sort_sequence->specs)
1830 sort_sequence->num_specs = 10;
1831 sort_sequence->specs = (Z_SortKeySpec **)
1832 nmem_malloc (stream, sort_sequence->num_specs *
1833 sizeof(*sort_sequence->specs));
1834 for (i = 0; i<sort_sequence->num_specs; i++)
1835 sort_sequence->specs[i] = 0;
1837 if (zapt->term->which != Z_Term_general)
1840 i = atoi_n ((char *) zapt->term->u.general->buf,
1841 zapt->term->u.general->len);
1842 if (i >= sort_sequence->num_specs)
1844 sprintf (termz, "%d", i);
1846 oe.proto = PROTO_Z3950;
1847 oe.oclass = CLASS_ATTSET;
1848 oe.value = attributeSet;
1849 if (!oid_ent_to_oid (&oe, oid))
1852 sks = (Z_SortKeySpec *) nmem_malloc (stream, sizeof(*sks));
1853 sks->sortElement = (Z_SortElement *)
1854 nmem_malloc (stream, sizeof(*sks->sortElement));
1855 sks->sortElement->which = Z_SortElement_generic;
1856 sk = sks->sortElement->u.generic = (Z_SortKey *)
1857 nmem_malloc (stream, sizeof(*sk));
1858 sk->which = Z_SortKey_sortAttributes;
1859 sk->u.sortAttributes = (Z_SortAttributes *)
1860 nmem_malloc (stream, sizeof(*sk->u.sortAttributes));
1862 sk->u.sortAttributes->id = oid;
1863 sk->u.sortAttributes->list = (Z_AttributeList *)
1864 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list));
1865 sk->u.sortAttributes->list->num_attributes = 1;
1866 sk->u.sortAttributes->list->attributes = (Z_AttributeElement **)
1867 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list->attributes));
1868 ae = *sk->u.sortAttributes->list->attributes = (Z_AttributeElement *)
1869 nmem_malloc (stream, sizeof(**sk->u.sortAttributes->list->attributes));
1870 ae->attributeSet = 0;
1871 ae->attributeType = (int *)
1872 nmem_malloc (stream, sizeof(*ae->attributeType));
1873 *ae->attributeType = 1;
1874 ae->which = Z_AttributeValue_numeric;
1875 ae->value.numeric = (int *)
1876 nmem_malloc (stream, sizeof(*ae->value.numeric));
1877 *ae->value.numeric = use_value;
1879 sks->sortRelation = (int *)
1880 nmem_malloc (stream, sizeof(*sks->sortRelation));
1881 if (sort_relation_value == 1)
1882 *sks->sortRelation = Z_SortKeySpec_ascending;
1883 else if (sort_relation_value == 2)
1884 *sks->sortRelation = Z_SortKeySpec_descending;
1886 *sks->sortRelation = Z_SortKeySpec_ascending;
1888 sks->caseSensitivity = (int *)
1889 nmem_malloc (stream, sizeof(*sks->caseSensitivity));
1890 *sks->caseSensitivity = 0;
1892 sks->which = Z_SortKeySpec_null;
1893 sks->u.null = odr_nullval ();
1894 sort_sequence->specs[i] = sks;
1895 return rsnull_create (NULL); /* FIXME - Use a proper nmem */
1899 static int parse_xpath(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1900 oid_value attributeSet,
1901 struct xpath_location_step *xpath, int max, NMEM mem)
1903 oid_value curAttributeSet = attributeSet;
1905 const char *use_string = 0;
1907 attr_init (&use, zapt, 1);
1908 attr_find_ex (&use, &curAttributeSet, &use_string);
1910 if (!use_string || *use_string != '/')
1913 return zebra_parse_xpath_str(use_string, xpath, max, mem);
1918 static RSET xpath_trunc(ZebraHandle zh, NMEM stream,
1919 int reg_type, const char *term, int use,
1920 oid_value curAttributeSet)
1923 struct grep_info grep_info;
1924 char term_dict[2048];
1927 int ord = zebraExplain_lookupSU (zh->reg->zei, curAttributeSet, use);
1928 int ord_len, i, r, max_pos;
1929 int term_type = Z_Term_characterString;
1930 const char *flags = "void";
1932 if (grep_info_prepare (zh, 0 /* zapt */, &grep_info, '0', stream))
1933 return rsnull_create (NULL); /* FIXME - Use a proper nmem */
1936 return rsnull_create (NULL); /* FIXME - Use a proper nmem */
1938 term_dict[prefix_len++] = '|';
1940 term_dict[prefix_len++] = '(';
1942 ord_len = key_SU_encode (ord, ord_buf);
1943 for (i = 0; i<ord_len; i++)
1945 term_dict[prefix_len++] = 1;
1946 term_dict[prefix_len++] = ord_buf[i];
1948 term_dict[prefix_len++] = ')';
1949 term_dict[prefix_len++] = 1;
1950 term_dict[prefix_len++] = reg_type;
1952 strcpy (term_dict+prefix_len, term);
1954 grep_info.isam_p_indx = 0;
1955 r = dict_lookup_grep (zh->reg->dict, term_dict, 0,
1956 &grep_info, &max_pos, 0, grep_handle);
1957 yaz_log (LOG_LOG, "%s %d positions", term,
1958 grep_info.isam_p_indx);
1959 rset = rset_trunc (zh, grep_info.isam_p_buf,
1960 grep_info.isam_p_indx, term, strlen(term),
1961 flags, 1, term_type);
1962 grep_info_delete (&grep_info);
1966 static RSET rpn_search_xpath (ZebraHandle zh,
1967 oid_value attributeSet,
1968 int num_bases, char **basenames,
1969 NMEM stream, const char *rank_type, RSET rset,
1970 int xpath_len, struct xpath_location_step *xpath)
1972 oid_value curAttributeSet = attributeSet;
1979 yaz_log (LOG_LOG, "len=%d", xpath_len);
1980 for (i = 0; i<xpath_len; i++)
1982 yaz_log (LOG_LOG, "XPATH %d %s", i, xpath[i].part);
1986 curAttributeSet = VAL_IDXPATH;
1996 a[@attr=value]/b[@other=othervalue]
1998 /e/@a val range(e/,range(@a,freetext(w,1015,val),@a),e/)
1999 /a/b val range(b/a/,freetext(w,1016,val),b/a/)
2000 /a/b/@c val range(b/a/,range(@c,freetext(w,1016,val),@c),b/a/)
2001 /a/b[@c=y] val range(b/a/,freetext(w,1016,val),b/a/,@c=y)
2002 /a[@c=y]/b val range(a/,range(b/a/,freetext(w,1016,val),b/a/),a/,@c=y)
2003 /a[@c=x]/b[@c=y] range(a/,range(b/a/,freetext(w,1016,val),b/a/,@c=y),a/,@c=x)
2007 dict_grep_cmap (zh->reg->dict, 0, 0);
2009 for (base_no = 0; base_no < num_bases; base_no++)
2011 int level = xpath_len;
2014 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
2016 zh->errCode = 109; /* Database unavailable */
2017 zh->errString = basenames[base_no];
2020 while (--level >= 0)
2022 char xpath_rev[128];
2024 RSET rset_start_tag = 0, rset_end_tag = 0, rset_attr = 0;
2028 for (i = level; i >= 1; --i)
2030 const char *cp = xpath[i].part;
2036 memcpy (xpath_rev + len, "[^/]*", 5);
2039 else if (*cp == ' ')
2042 xpath_rev[len++] = 1;
2043 xpath_rev[len++] = ' ';
2047 xpath_rev[len++] = *cp;
2048 xpath_rev[len++] = '/';
2050 else if (i == 1) /* // case */
2052 xpath_rev[len++] = '.';
2053 xpath_rev[len++] = '*';
2058 if (xpath[level].predicate &&
2059 xpath[level].predicate->which == XPATH_PREDICATE_RELATION &&
2060 xpath[level].predicate->u.relation.name[0])
2062 WRBUF wbuf = wrbuf_alloc();
2063 wrbuf_puts(wbuf, xpath[level].predicate->u.relation.name+1);
2064 if (xpath[level].predicate->u.relation.value)
2066 const char *cp = xpath[level].predicate->u.relation.value;
2067 wrbuf_putc(wbuf, '=');
2071 if (strchr(REGEX_CHARS, *cp))
2072 wrbuf_putc(wbuf, '\\');
2073 wrbuf_putc(wbuf, *cp);
2077 wrbuf_puts(wbuf, "");
2078 rset_attr = xpath_trunc (
2079 zh, stream, '0', wrbuf_buf(wbuf), 3, curAttributeSet);
2080 wrbuf_free(wbuf, 1);
2087 yaz_log (LOG_LOG, "xpath_rev (%d) = %s", level, xpath_rev);
2088 if (strlen(xpath_rev))
2090 rset_start_tag = xpath_trunc(zh, stream,
2091 '0', xpath_rev, 1, curAttributeSet);
2093 rset_end_tag = xpath_trunc(zh, stream,
2094 '0', xpath_rev, 2, curAttributeSet);
2097 parms.key_size = sizeof(struct it_key);
2098 parms.cmp = key_compare_it;
2099 parms.rset_l = rset_start_tag;
2100 parms.rset_m = rset;
2101 parms.rset_r = rset_end_tag;
2102 parms.rset_attr = rset_attr;
2103 parms.printer = key_print_it;
2104 rset = rset_create (rset_kind_between, &parms);
2106 rset=rsbetween_create( NULL, /* FIXME */
2107 sizeof(struct it_key), key_compare_it,
2108 rset_start_tag, rset, rset_end_tag, rset_attr,
2120 static RSET rpn_search_APT (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2121 oid_value attributeSet, NMEM stream,
2122 Z_SortKeySpecList *sort_sequence,
2123 int num_bases, char **basenames)
2126 char *search_type = NULL;
2127 char rank_type[128];
2130 char termz[IT_MAX_WORD+1];
2134 struct xpath_location_step xpath[10];
2136 zebra_maps_attr (zh->reg->zebra_maps, zapt, ®_id, &search_type,
2137 rank_type, &complete_flag, &sort_flag);
2139 logf (LOG_DEBUG, "reg_id=%c", reg_id);
2140 logf (LOG_DEBUG, "complete_flag=%d", complete_flag);
2141 logf (LOG_DEBUG, "search_type=%s", search_type);
2142 logf (LOG_DEBUG, "rank_type=%s", rank_type);
2144 if (zapt_term_to_utf8(zh, zapt, termz))
2148 return rpn_sort_spec (zh, zapt, attributeSet, stream, sort_sequence,
2150 xpath_len = parse_xpath(zh, zapt, attributeSet, xpath, 10, stream);
2154 if (xpath[xpath_len-1].part[0] == '@')
2158 if (!strcmp (search_type, "phrase"))
2160 rset = rpn_search_APT_phrase (zh, zapt, termz, attributeSet, stream,
2161 reg_id, complete_flag, rank_type,
2163 num_bases, basenames);
2165 else if (!strcmp (search_type, "and-list"))
2167 rset = rpn_search_APT_and_list (zh, zapt, termz, attributeSet, stream,
2168 reg_id, complete_flag, rank_type,
2170 num_bases, basenames);
2172 else if (!strcmp (search_type, "or-list"))
2174 rset = rpn_search_APT_or_list (zh, zapt, termz, attributeSet, stream,
2175 reg_id, complete_flag, rank_type,
2177 num_bases, basenames);
2179 else if (!strcmp (search_type, "local"))
2181 rset = rpn_search_APT_local (zh, zapt, termz, attributeSet, stream,
2184 else if (!strcmp (search_type, "numeric"))
2186 rset = rpn_search_APT_numeric (zh, zapt, termz, attributeSet, stream,
2187 reg_id, complete_flag, rank_type,
2189 num_bases, basenames);
2191 else if (!strcmp (search_type, "always"))
2197 return rpn_search_xpath (zh, attributeSet, num_bases, basenames,
2198 stream, rank_type, rset, xpath_len, xpath);
2201 static RSET rpn_search_structure (ZebraHandle zh, Z_RPNStructure *zs,
2202 oid_value attributeSet, NMEM stream,
2203 Z_SortKeySpecList *sort_sequence,
2204 int num_bases, char **basenames)
2207 if (zs->which == Z_RPNStructure_complex)
2209 Z_Operator *zop = zs->u.complex->roperator;
2213 rset_l = rpn_search_structure (zh, zs->u.complex->s1,
2214 attributeSet, stream,
2216 num_bases, basenames);
2219 rset_r = rpn_search_structure (zh, zs->u.complex->s2,
2220 attributeSet, stream,
2222 num_bases, basenames);
2225 rset_delete (rset_l);
2231 case Z_Operator_and:
2232 r = rsbool_create_and( NULL, /* FIXME - use a proper nmem */
2233 sizeof(struct it_key), key_compare_it,
2234 rset_l, rset_r, key_logdump_txt );
2237 r = rsbool_create_or( NULL, /* FIXME - use a proper nmem */
2238 sizeof(struct it_key), key_compare_it,
2239 rset_l, rset_r, key_logdump_txt );
2241 case Z_Operator_and_not:
2242 r = rsbool_create_not( NULL, /* FIXME - use a proper nmem */
2243 sizeof(struct it_key), key_compare_it,
2244 rset_l, rset_r, key_logdump_txt );
2246 case Z_Operator_prox:
2247 if (zop->u.prox->which != Z_ProximityOperator_known)
2252 if (*zop->u.prox->u.known != Z_ProxUnit_word)
2254 char *val = (char *) nmem_malloc (stream, 16);
2256 zh->errString = val;
2257 sprintf (val, "%d", *zop->u.prox->u.known);
2262 /* new / old prox */
2265 twosets[0] = rset_l;
2266 twosets[1] = rset_r;
2267 r=rsprox_create(NULL, /* FIXME - use a proper nmem */
2268 sizeof(struct it_key), key_compare_it,
2271 *zop->u.prox->ordered,
2272 (!zop->u.prox->exclusion ?
2273 0 : *zop->u.prox->exclusion),
2274 *zop->u.prox->relationType,
2275 *zop->u.prox->distance );
2277 parms.rset = twosets;
2279 parms.ordered = *zop->u.prox->ordered;
2280 parms.exclusion = (!zop->u.prox->exclusion ? 0 :
2281 *zop->u.prox->exclusion);
2282 parms.relation = *zop->u.prox->relationType;
2283 parms.distance = *zop->u.prox->distance;
2284 parms.key_size = sizeof(struct it_key);
2285 parms.cmp = key_compare_it;
2286 parms.getseq = key_get_seq;
2287 parms.log_item = key_logdump_txt;
2288 r = rset_create(rset_kind_prox, &parms);
2297 else if (zs->which == Z_RPNStructure_simple)
2299 if (zs->u.simple->which == Z_Operand_APT)
2301 logf (LOG_DEBUG, "rpn_search_APT");
2302 r = rpn_search_APT (zh, zs->u.simple->u.attributesPlusTerm,
2303 attributeSet, stream, sort_sequence,
2304 num_bases, basenames);
2306 else if (zs->u.simple->which == Z_Operand_resultSetId)
2308 logf (LOG_DEBUG, "rpn_search_ref");
2309 r = resultSetRef (zh, zs->u.simple->u.resultSetId);
2312 r = rsnull_create (NULL); /* FIXME - Use a proper nmem */
2315 nmem_strdup (stream, zs->u.simple->u.resultSetId);
2336 RSET rpn_search (ZebraHandle zh, NMEM nmem,
2337 Z_RPNQuery *rpn, int num_bases, char **basenames,
2338 const char *setname,
2343 oid_value attributeSet;
2344 Z_SortKeySpecList *sort_sequence;
2348 zh->errString = NULL;
2351 sort_sequence = (Z_SortKeySpecList *)
2352 nmem_malloc (nmem, sizeof(*sort_sequence));
2353 sort_sequence->num_specs = 10;
2354 sort_sequence->specs = (Z_SortKeySpec **)
2355 nmem_malloc (nmem, sort_sequence->num_specs *
2356 sizeof(*sort_sequence->specs));
2357 for (i = 0; i<sort_sequence->num_specs; i++)
2358 sort_sequence->specs[i] = 0;
2360 attrset = oid_getentbyoid (rpn->attributeSetId);
2361 attributeSet = attrset->value;
2362 rset = rpn_search_structure (zh, rpn->RPNStructure, attributeSet,
2363 nmem, sort_sequence, num_bases, basenames);
2368 logf (LOG_DEBUG, "search error: %d", zh->errCode);
2370 for (i = 0; sort_sequence->specs[i]; i++)
2372 sort_sequence->num_specs = i;
2374 resultSetRank (zh, sset, rset);
2377 logf (LOG_DEBUG, "resultSetSortSingle in rpn_search");
2378 resultSetSortSingle (zh, nmem, sset, rset,
2379 sort_sequence, &sort_status);
2382 logf (LOG_DEBUG, "resultSetSortSingle status = %d", zh->errCode);
2388 struct scan_info_entry {
2394 struct scan_info_entry *list;
2400 static int scan_handle (char *name, const char *info, int pos, void *client)
2402 int len_prefix, idx;
2403 struct scan_info *scan_info = (struct scan_info *) client;
2405 len_prefix = strlen(scan_info->prefix);
2406 if (memcmp (name, scan_info->prefix, len_prefix))
2408 if (pos > 0) idx = scan_info->after - pos + scan_info->before;
2411 scan_info->list[idx].term = (char *)
2412 odr_malloc (scan_info->odr, strlen(name + len_prefix)+1);
2413 strcpy (scan_info->list[idx].term, name + len_prefix);
2414 assert (*info == sizeof(ISAMC_P));
2415 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAMC_P));
2419 static void scan_term_untrans (ZebraHandle zh, NMEM stream, int reg_type,
2420 char **dst, const char *src)
2422 char term_src[IT_MAX_WORD];
2423 char term_dst[IT_MAX_WORD];
2425 term_untrans (zh, reg_type, term_src, src);
2427 if (zh->iconv_from_utf8 != 0)
2430 char *inbuf = term_src;
2431 size_t inleft = strlen(term_src);
2432 char *outbuf = term_dst;
2433 size_t outleft = sizeof(term_dst)-1;
2436 ret = yaz_iconv (zh->iconv_from_utf8, &inbuf, &inleft,
2438 if (ret == (size_t)(-1))
2441 len = outbuf - term_dst;
2442 *dst = nmem_malloc (stream, len + 1);
2444 memcpy (*dst, term_dst, len);
2448 *dst = nmem_strdup (stream, term_src);
2451 static void count_set (RSET r, int *count)
2458 logf (LOG_DEBUG, "count_set");
2461 rfd = rset_open (r, RSETF_READ);
2462 while (rset_read (r, rfd, &key))
2465 if (key.mem[0] != psysno)
2467 psysno = key.mem[0];
2471 if (key.sysno != psysno)
2479 rset_close (r, rfd);
2480 logf (LOG_DEBUG, "%d keys, %d records", kno, *count);
2483 void rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
2484 oid_value attributeset,
2485 int num_bases, char **basenames,
2486 int *position, int *num_entries, ZebraScanEntry **list,
2487 int *is_partial, RSET limit_set, int return_zero)
2490 int pos = *position;
2491 int num = *num_entries;
2495 char termz[IT_MAX_WORD+20];
2498 const char *use_string = 0;
2499 struct scan_info *scan_info_array;
2500 ZebraScanEntry *glist;
2501 int ords[32], ord_no = 0;
2504 int bases_ok = 0; /* no of databases with OK attribute */
2505 int errCode = 0; /* err code (if any is not OK) */
2506 char *errString = 0; /* addinfo */
2509 char *search_type = NULL;
2510 char rank_type[128];
2516 if (attributeset == VAL_NONE)
2517 attributeset = VAL_BIB1;
2522 int termset_value_numeric;
2523 const char *termset_value_string;
2524 attr_init (&termset, zapt, 8);
2525 termset_value_numeric =
2526 attr_find_ex (&termset, NULL, &termset_value_string);
2527 if (termset_value_numeric != -1)
2530 const char *termset_name = 0;
2532 if (termset_value_numeric != -2)
2535 sprintf (resname, "%d", termset_value_numeric);
2536 termset_name = resname;
2539 termset_name = termset_value_string;
2541 limit_set = resultSetRef (zh, termset_name);
2545 yaz_log (LOG_DEBUG, "position = %d, num = %d set=%d",
2546 pos, num, attributeset);
2548 attr_init (&use, zapt, 1);
2549 use_value = attr_find_ex (&use, &attributeset, &use_string);
2551 if (zebra_maps_attr (zh->reg->zebra_maps, zapt, ®_id, &search_type,
2552 rank_type, &complete_flag, &sort_flag))
2558 yaz_log (LOG_DEBUG, "use_value = %d", use_value);
2560 if (use_value == -1)
2562 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
2566 data1_local_attribute *local_attr;
2568 if ((r=att_getentbyatt (zh, &attp, attributeset, use_value,
2571 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
2572 attributeset, use_value);
2576 sprintf (val_str, "%d", use_value);
2578 errString = odr_strdup (stream, val_str);
2584 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
2586 zh->errString = basenames[base_no];
2587 zh->errCode = 109; /* Database unavailable */
2592 for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
2593 local_attr = local_attr->next)
2597 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
2600 ords[ord_no++] = ord;
2603 if (!bases_ok && errCode)
2605 zh->errCode = errCode;
2606 zh->errString = errString;
2612 sprintf (val_str, "%d", use_value);
2614 zh->errString = odr_strdup (stream, val_str);
2619 /* prepare dictionary scanning */
2622 scan_info_array = (struct scan_info *)
2623 odr_malloc (stream, ord_no * sizeof(*scan_info_array));
2624 for (i = 0; i < ord_no; i++)
2626 int j, prefix_len = 0;
2627 int before_tmp = before, after_tmp = after;
2628 struct scan_info *scan_info = scan_info_array + i;
2629 struct rpn_char_map_info rcmi;
2631 rpn_char_map_prepare (zh->reg, reg_id, &rcmi);
2633 scan_info->before = before;
2634 scan_info->after = after;
2635 scan_info->odr = stream;
2637 scan_info->list = (struct scan_info_entry *)
2638 odr_malloc (stream, (before+after) * sizeof(*scan_info->list));
2639 for (j = 0; j<before+after; j++)
2640 scan_info->list[j].term = NULL;
2642 prefix_len += key_SU_encode (ords[i], termz + prefix_len);
2643 termz[prefix_len++] = reg_id;
2644 termz[prefix_len] = 0;
2645 strcpy (scan_info->prefix, termz);
2647 if (trans_scan_term (zh, zapt, termz+prefix_len, reg_id))
2650 dict_scan (zh->reg->dict, termz, &before_tmp, &after_tmp,
2651 scan_info, scan_handle);
2653 glist = (ZebraScanEntry *)
2654 odr_malloc (stream, (before+after)*sizeof(*glist));
2656 /* consider terms after main term */
2657 for (i = 0; i < ord_no; i++)
2661 for (i = 0; i<after; i++)
2664 const char *mterm = NULL;
2668 for (j = 0; j < ord_no; j++)
2670 if (ptr[j] < before+after &&
2671 (tst=scan_info_array[j].list[ptr[j]].term) &&
2672 (!mterm || strcmp (tst, mterm) < 0))
2680 scan_term_untrans (zh, stream->mem, reg_id,
2681 &glist[i+before].term, mterm);
2682 rset = rset_trunc (zh, &scan_info_array[j0].list[ptr[j0]].isam_p, 1,
2683 glist[i+before].term, strlen(glist[i+before].term),
2684 NULL, 0, zapt->term->which);
2687 for (j = j0+1; j<ord_no; j++)
2689 if (ptr[j] < before+after &&
2690 (tst=scan_info_array[j].list[ptr[j]].term) &&
2691 !strcmp (tst, mterm))
2696 rset_trunc (zh, &scan_info_array[j].list[ptr[j]].isam_p, 1,
2697 glist[i+before].term,
2698 strlen(glist[i+before].term), NULL, 0,
2701 bool_parms.key_size = sizeof(struct it_key);
2702 bool_parms.cmp = key_compare_it;
2703 bool_parms.log_item = key_logdump_txt;
2704 bool_parms.rset_l = rset;
2705 bool_parms.rset_r = rset2;
2706 rset = rset_create (rset_kind_or, &bool_parms);
2708 rset = rsbool_create_or(NULL, /* FIXME -use a proper nmem */
2709 sizeof(struct it_key), key_compare_it,
2710 rset, rset2, key_logdump_txt);
2718 rset_bool_parms bool_parms;
2719 bool_parms.key_size = sizeof(struct it_key);
2720 bool_parms.cmp = key_compare_it;
2721 bool_parms.log_item = key_logdump_txt;
2722 bool_parms.rset_l = rset;
2723 bool_parms.rset_r = rset_dup(limit_set);
2724 rset = rset_create (rset_kind_and, &bool_parms);
2726 rset = rsbool_create_and(NULL, /* FIXME -use a proper nmem */
2727 sizeof(struct it_key), key_compare_it,
2728 rset, rset_dup(limit_set),
2731 count_set (rset, &glist[i+before].occurrences);
2736 *num_entries -= (after-i);
2740 /* consider terms before main term */
2741 for (i = 0; i<ord_no; i++)
2744 for (i = 0; i<before; i++)
2747 const char *mterm = NULL;
2751 for (j = 0; j <ord_no; j++)
2753 if (ptr[j] < before &&
2754 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2755 (!mterm || strcmp (tst, mterm) > 0))
2764 scan_term_untrans (zh, stream->mem, reg_id,
2765 &glist[before-1-i].term, mterm);
2768 (zh, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1,
2769 glist[before-1-i].term, strlen(glist[before-1-i].term),
2770 NULL, 0, zapt->term->which);
2774 for (j = j0+1; j<ord_no; j++)
2776 if (ptr[j] < before &&
2777 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2778 !strcmp (tst, mterm))
2782 rset2 = rset_trunc (zh,
2783 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1,
2784 glist[before-1-i].term,
2785 strlen(glist[before-1-i].term), NULL, 0,
2788 bool_parms.key_size = sizeof(struct it_key);
2789 bool_parms.cmp = key_compare_it;
2790 bool_parms.log_item = key_logdump_txt;
2791 bool_parms.rset_l = rset;
2792 bool_parms.rset_r = rset2;
2793 rset = rset_create (rset_kind_or, &bool_parms);
2795 rset = rsbool_create_and(NULL, /* FIXME -use a proper nmem */
2796 sizeof(struct it_key), key_compare_it,
2805 rset_bool_parms bool_parms;
2807 bool_parms.key_size = sizeof(struct it_key);
2808 bool_parms.cmp = key_compare_it;
2809 bool_parms.log_item = key_logdump_txt;
2810 bool_parms.rset_l = rset;
2811 bool_parms.rset_r = rset_dup(limit_set);
2813 rset = rset_create (rset_kind_and, &bool_parms);
2815 rset = rsbool_create_and(NULL, /* FIXME -use a proper nmem */
2816 sizeof(struct it_key), key_compare_it,
2817 rset, rset_dup(limit_set),
2820 count_set (rset, &glist[before-1-i].occurrences);
2830 *list = glist + i; /* list is set to first 'real' entry */
2832 logf (LOG_DEBUG, "position = %d, num_entries = %d",
2833 *position, *num_entries);
2835 logf (LOG_DEBUG, "scan error: %d", zh->errCode);