2 * Copyright (C) 1995-2002, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
6 * $Id: zrpn.c,v 1.118 2002-08-01 08:53:35 adam Exp $
23 #include <rsbetween.h>
25 struct rpn_char_map_info {
30 static const char **rpn_char_map_handler (void *vp, const char **from, int len)
32 struct rpn_char_map_info *p = (struct rpn_char_map_info *) vp;
33 return zebra_maps_input (p->zm, p->reg_type, from, len);
36 static void rpn_char_map_prepare (struct zebra_register *reg, int reg_type,
37 struct rpn_char_map_info *map_info)
39 map_info->zm = reg->zebra_maps;
40 map_info->reg_type = reg_type;
41 dict_grep_cmap (reg->dict, map_info, rpn_char_map_handler);
48 Z_AttributesPlusTerm *zapt;
51 static int attr_find_ex (AttrType *src, oid_value *attributeSetP,
52 const char **string_value)
57 num_attributes = src->zapt->attributes->num_attributes;
59 num_attributes = src->zapt->num_attributes;
61 while (src->major < num_attributes)
63 Z_AttributeElement *element;
66 element = src->zapt->attributes->attributes[src->major];
68 element = src->zapt->attributeList[src->major];
70 if (src->type == *element->attributeType)
72 switch (element->which)
74 case Z_AttributeValue_numeric:
76 if (element->attributeSet && attributeSetP)
80 attrset = oid_getentbyoid (element->attributeSet);
81 *attributeSetP = attrset->value;
83 return *element->value.numeric;
85 case Z_AttributeValue_complex:
86 if (src->minor >= element->value.complex->num_list)
88 if (element->attributeSet && attributeSetP)
92 attrset = oid_getentbyoid (element->attributeSet);
93 *attributeSetP = attrset->value;
95 if (element->value.complex->list[src->minor]->which ==
96 Z_StringOrNumeric_numeric)
100 *element->value.complex->list[src->minor-1]->u.numeric;
102 else if (element->value.complex->list[src->minor]->which ==
103 Z_StringOrNumeric_string)
109 element->value.complex->list[src->minor-1]->u.string;
123 static int attr_find (AttrType *src, oid_value *attributeSetP)
125 return attr_find_ex (src, attributeSetP, 0);
128 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
151 static void term_untrans (ZebraHandle zh, int reg_type,
152 char *dst, const char *src)
156 const char *cp = zebra_maps_output (zh->reg->zebra_maps,
167 static void add_isam_p (const char *name, const char *info,
170 if (p->isam_p_indx == p->isam_p_size)
172 ISAMS_P *new_isam_p_buf;
176 p->isam_p_size = 2*p->isam_p_size + 100;
177 new_isam_p_buf = (ISAMS_P *) xmalloc (sizeof(*new_isam_p_buf) *
181 memcpy (new_isam_p_buf, p->isam_p_buf,
182 p->isam_p_indx * sizeof(*p->isam_p_buf));
183 xfree (p->isam_p_buf);
185 p->isam_p_buf = new_isam_p_buf;
188 new_term_no = (int *) xmalloc (sizeof(*new_term_no) *
192 memcpy (new_term_no, p->isam_p_buf,
193 p->isam_p_indx * sizeof(*p->term_no));
196 p->term_no = new_term_no;
199 assert (*info == sizeof(*p->isam_p_buf));
200 memcpy (p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
209 int len = key_SU_decode (&su_code, name);
211 term_untrans (p->zh, p->reg_type, term_tmp, name+len+1);
212 logf (LOG_LOG, "grep: %d %c %s", su_code, name[len], term_tmp);
213 zebraExplain_lookup_ord (p->zh->reg->zei,
214 su_code, &db, &set, &use);
215 logf (LOG_LOG, "grep: set=%d use=%d db=%s", set, use, db);
217 resultSetAddTerm (p->zh, p->termset, name[len], db,
224 static int grep_handle (char *name, const char *info, void *p)
226 add_isam_p (name, info, (struct grep_info *) p);
230 static int term_pre (ZebraMaps zebra_maps, int reg_type, const char **src,
231 const char *ct1, const char *ct2)
233 const char *s1, *s0 = *src;
236 /* skip white space */
239 if (ct1 && strchr (ct1, *s0))
241 if (ct2 && strchr (ct2, *s0))
244 map = zebra_maps_input (zebra_maps, reg_type, &s1, strlen(s1));
245 if (**map != *CHR_SPACE)
253 #define REGEX_CHARS "[]()|.*+!"
255 /* term_100: handle term, where trunc=none (no operators at all) */
256 static int term_100 (ZebraMaps zebra_maps, int reg_type,
257 const char **src, char *dst, int space_split,
265 const char *space_start = 0;
266 const char *space_end = 0;
268 if (!term_pre (zebra_maps, reg_type, src, NULL, NULL))
274 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
277 if (**map == *CHR_SPACE)
280 else /* complete subfield only. */
282 if (**map == *CHR_SPACE)
283 { /* save space mapping for later .. */
288 else if (space_start)
289 { /* reload last space */
290 while (space_start < space_end)
292 if (strchr (REGEX_CHARS, *space_start))
294 dst_term[j++] = *space_start;
295 dst[i++] = *space_start++;
298 space_start = space_end = 0;
301 /* add non-space char */
304 if (strchr(REGEX_CHARS, *s1))
316 /* term_101: handle term, where trunc=Process # */
317 static int term_101 (ZebraMaps zebra_maps, int reg_type,
318 const char **src, char *dst, int space_split,
326 if (!term_pre (zebra_maps, reg_type, src, "#", "#"))
335 dst_term[j++] = *s0++;
340 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
341 if (space_split && **map == *CHR_SPACE)
345 if (strchr(REGEX_CHARS, *s1))
353 dst_term[j++] = '\0';
358 /* term_103: handle term, where trunc=re-2 (regular expressions) */
359 static int term_103 (ZebraMaps zebra_maps, int reg_type, const char **src,
360 char *dst, int *errors, int space_split,
368 if (!term_pre (zebra_maps, reg_type, src, "^\\()[].*+?|", "("))
371 if (errors && *s0 == '+' && s0[1] && s0[2] == '+' && s0[3] &&
374 *errors = s0[1] - '0';
381 if (strchr ("^\\()[].*+?|-", *s0))
389 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
390 if (**map == *CHR_SPACE)
394 if (strchr(REGEX_CHARS, *s1))
407 /* term_103: handle term, where trunc=re-1 (regular expressions) */
408 static int term_102 (ZebraMaps zebra_maps, int reg_type, const char **src,
409 char *dst, int space_split, char *dst_term)
411 return term_103 (zebra_maps, reg_type, src, dst, NULL, space_split,
416 /* term_104: handle term, where trunc=Process # and ! */
417 static int term_104 (ZebraMaps zebra_maps, int reg_type,
418 const char **src, char *dst, int space_split,
426 if (!term_pre (zebra_maps, reg_type, src, "#!", "#!"))
435 dst_term[j++] = *s0++;
440 dst_term[j++] = *s0++;
444 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
445 if (space_split && **map == *CHR_SPACE)
449 if (strchr(REGEX_CHARS, *s1))
457 dst_term[j++] = '\0';
462 /* term_105/106: handle term, where trunc=Process * and ! and right trunc */
463 static int term_105 (ZebraMaps zebra_maps, int reg_type,
464 const char **src, char *dst, int space_split,
465 char *dst_term, int right_truncate)
472 if (!term_pre (zebra_maps, reg_type, src, "*!", "*!"))
481 dst_term[j++] = *s0++;
486 dst_term[j++] = *s0++;
490 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
491 if (space_split && **map == *CHR_SPACE)
495 if (strchr(REGEX_CHARS, *s1))
509 dst_term[j++] = '\0';
515 /* gen_regular_rel - generate regular expression from relation
516 * val: border value (inclusive)
517 * islt: 1 if <=; 0 if >=.
519 static void gen_regular_rel (char *dst, int val, int islt)
526 logf (LOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
530 strcpy (dst, "(-[0-9]+|(");
538 strcpy (dst, "([0-9]+|-(");
539 dst_p = strlen (dst);
549 dst_p = strlen (dst);
550 sprintf (numstr, "%d", val);
551 for (w = strlen(numstr); --w >= 0; pos++)
570 strcpy (dst + dst_p, numstr);
571 dst_p = strlen(dst) - pos - 1;
599 for (i = 0; i<pos; i++)
612 /* match everything less than 10^(pos-1) */
614 for (i=1; i<pos; i++)
615 strcat (dst, "[0-9]?");
619 /* match everything greater than 10^pos */
620 for (i = 0; i <= pos; i++)
621 strcat (dst, "[0-9]");
622 strcat (dst, "[0-9]*");
627 void string_rel_add_char (char **term_p, const char *src, int *indx)
629 if (src[*indx] == '\\')
630 *(*term_p)++ = src[(*indx)++];
631 *(*term_p)++ = src[(*indx)++];
635 * > abc ([b-].*|a[c-].*|ab[d-].*|abc.+)
636 * ([^-a].*|a[^-b].*ab[^-c].*|abc.+)
637 * >= abc ([b-].*|a[c-].*|ab[c-].*)
638 * ([^-a].*|a[^-b].*|ab[c-].*)
639 * < abc ([-0].*|a[-a].*|ab[-b].*)
640 * ([^a-].*|a[^b-].*|ab[^c-].*)
641 * <= abc ([-0].*|a[-a].*|ab[-b].*|abc)
642 * ([^a-].*|a[^b-].*|ab[^c-].*|abc)
644 static int string_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
645 const char **term_sub, char *term_dict,
646 oid_value attributeSet,
647 int reg_type, int space_split, char *term_dst)
652 char *term_tmp = term_dict + strlen(term_dict);
653 char term_component[256];
655 attr_init (&relation, zapt, 2);
656 relation_value = attr_find (&relation, NULL);
658 logf (LOG_DEBUG, "string relation value=%d", relation_value);
659 switch (relation_value)
662 if (!term_100 (zh->reg->zebra_maps, reg_type,
663 term_sub, term_component,
664 space_split, term_dst))
666 logf (LOG_DEBUG, "Relation <");
669 for (i = 0; term_component[i]; )
676 string_rel_add_char (&term_tmp, term_component, &j);
681 string_rel_add_char (&term_tmp, term_component, &i);
692 if (!term_100 (zh->reg->zebra_maps, reg_type,
693 term_sub, term_component,
694 space_split, term_dst))
696 logf (LOG_DEBUG, "Relation <=");
699 for (i = 0; term_component[i]; )
704 string_rel_add_char (&term_tmp, term_component, &j);
708 string_rel_add_char (&term_tmp, term_component, &i);
717 for (i = 0; term_component[i]; )
718 string_rel_add_char (&term_tmp, term_component, &i);
723 if (!term_100 (zh->reg->zebra_maps, reg_type,
724 term_sub, term_component, space_split, term_dst))
726 logf (LOG_DEBUG, "Relation >");
729 for (i = 0; term_component[i];)
734 string_rel_add_char (&term_tmp, term_component, &j);
739 string_rel_add_char (&term_tmp, term_component, &i);
747 for (i = 0; term_component[i];)
748 string_rel_add_char (&term_tmp, term_component, &i);
755 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub,
756 term_component, space_split, term_dst))
758 logf (LOG_DEBUG, "Relation >=");
761 for (i = 0; term_component[i];)
768 string_rel_add_char (&term_tmp, term_component, &j);
771 if (term_component[i+1])
775 string_rel_add_char (&term_tmp, term_component, &i);
779 string_rel_add_char (&term_tmp, term_component, &i);
791 logf (LOG_DEBUG, "Relation =");
792 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub,
793 term_component, space_split, term_dst))
795 strcat (term_tmp, "(");
796 strcat (term_tmp, term_component);
797 strcat (term_tmp, ")");
802 static int string_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
803 const char **term_sub,
804 oid_value attributeSet, NMEM stream,
805 struct grep_info *grep_info,
806 int reg_type, int complete_flag,
807 int num_bases, char **basenames,
808 char *term_dst, int xpath_use);
810 static RSET term_trunc (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
811 const char **term_sub,
812 oid_value attributeSet, NMEM stream,
813 struct grep_info *grep_info,
814 int reg_type, int complete_flag,
815 int num_bases, char **basenames,
817 const char *rank_type, int xpath_use)
820 grep_info->isam_p_indx = 0;
821 r = string_term (zh, zapt, term_sub, attributeSet, stream, grep_info,
822 reg_type, complete_flag, num_bases, basenames,
823 term_dst, xpath_use);
826 logf (LOG_DEBUG, "term: %s", term_dst);
827 return rset_trunc (zh, grep_info->isam_p_buf,
828 grep_info->isam_p_indx, term_dst,
829 strlen(term_dst), rank_type, 1 /* preserve pos */,
834 static int string_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
835 const char **term_sub,
836 oid_value attributeSet, NMEM stream,
837 struct grep_info *grep_info,
838 int reg_type, int complete_flag,
839 int num_bases, char **basenames,
840 char *term_dst, int xpath_use)
842 char term_dict[2*IT_MAX_WORD+4000];
845 int truncation_value;
848 const char *use_string = 0;
849 oid_value curAttributeSet = attributeSet;
851 struct rpn_char_map_info rcmi;
852 int space_split = complete_flag ? 0 : 1;
854 rpn_char_map_prepare (zh->reg, reg_type, &rcmi);
855 attr_init (&use, zapt, 1);
856 use_value = attr_find_ex (&use, &curAttributeSet, &use_string);
857 logf (LOG_DEBUG, "string_term, use value %d", use_value);
858 attr_init (&truncation, zapt, 5);
859 truncation_value = attr_find (&truncation, NULL);
860 logf (LOG_DEBUG, "truncation value %d", truncation_value);
862 if (use_value == -1) /* no attribute - assumy "any" */
864 for (base_no = 0; base_no < num_bases; base_no++)
867 data1_local_attribute id_xpath_attr;
868 data1_local_attribute *local_attr;
869 int max_pos, prefix_len = 0;
873 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
875 zh->errCode = 109; /* Database unavailable */
876 zh->errString = basenames[base_no];
879 if (use_value == -2) /* string attribute (assume IDXPATH/any) */
881 use_value = xpath_use;
882 attp.local_attributes = &id_xpath_attr;
883 attp.attset_ordinal = VAL_IDXPATH;
884 id_xpath_attr.next = 0;
885 id_xpath_attr.local = use_value;
887 else if (curAttributeSet == VAL_IDXPATH)
889 attp.local_attributes = &id_xpath_attr;
890 attp.attset_ordinal = VAL_IDXPATH;
891 id_xpath_attr.next = 0;
892 id_xpath_attr.local = use_value;
896 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
898 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
899 curAttributeSet, use_value, r);
902 /* set was found, but value wasn't defined */
904 sprintf (val_str, "%d (1)", use_value);
906 zh->errString = nmem_strdup (stream, val_str);
911 struct oident oident;
913 oident.proto = PROTO_Z3950;
914 oident.oclass = CLASS_ATTSET;
915 oident.value = curAttributeSet;
916 oid_ent_to_oid (&oident, oid);
919 zh->errString = nmem_strdup (stream, oident.desc);
924 for (local_attr = attp.local_attributes; local_attr;
925 local_attr = local_attr->next)
931 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
936 term_dict[prefix_len++] = '|';
938 term_dict[prefix_len++] = '(';
940 ord_len = key_SU_encode (ord, ord_buf);
941 for (i = 0; i<ord_len; i++)
943 term_dict[prefix_len++] = 1;
944 term_dict[prefix_len++] = ord_buf[i];
950 sprintf (val_str, "%d (2)", use_value);
952 zh->errString = nmem_strdup (stream, val_str);
955 term_dict[prefix_len++] = ')';
956 term_dict[prefix_len++] = 1;
957 term_dict[prefix_len++] = reg_type;
958 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
959 term_dict[prefix_len] = '\0';
961 switch (truncation_value)
963 case -1: /* not specified */
964 case 100: /* do not truncate */
965 if (!string_relation (zh, zapt, &termp, term_dict,
967 reg_type, space_split, term_dst))
969 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_dict+prefix_len);
970 r = dict_lookup_grep (zh->reg->dict, term_dict, 0,
971 grep_info, &max_pos, 0, grep_handle);
973 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
975 case 1: /* right truncation */
976 term_dict[j++] = '(';
977 if (!term_100 (zh->reg->zebra_maps, reg_type,
978 &termp, term_dict + j, space_split, term_dst))
980 strcat (term_dict, ".*)");
981 dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
982 &max_pos, 0, grep_handle);
984 case 2: /* keft truncation */
985 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
986 if (!term_100 (zh->reg->zebra_maps, reg_type,
987 &termp, term_dict + j, space_split, term_dst))
989 strcat (term_dict, ")");
990 dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
991 &max_pos, 0, grep_handle);
993 case 3: /* left&right truncation */
994 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
995 if (!term_100 (zh->reg->zebra_maps, reg_type,
996 &termp, term_dict + j, space_split, term_dst))
998 strcat (term_dict, ".*)");
999 dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1000 &max_pos, 0, grep_handle);
1004 case 101: /* process # in term */
1005 term_dict[j++] = '(';
1006 if (!term_101 (zh->reg->zebra_maps, reg_type,
1007 &termp, term_dict + j, space_split, term_dst))
1009 strcat (term_dict, ")");
1010 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1011 &max_pos, 0, grep_handle);
1013 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d", r);
1015 case 102: /* Regexp-1 */
1016 term_dict[j++] = '(';
1017 if (!term_102 (zh->reg->zebra_maps, reg_type,
1018 &termp, term_dict + j, space_split, term_dst))
1020 strcat (term_dict, ")");
1021 logf (LOG_DEBUG, "Regexp-1 tolerance=%d", r);
1022 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1023 &max_pos, 0, grep_handle);
1025 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
1028 case 103: /* Regexp-2 */
1030 term_dict[j++] = '(';
1031 if (!term_103 (zh->reg->zebra_maps, reg_type,
1032 &termp, term_dict + j, &r, space_split, term_dst))
1034 strcat (term_dict, ")");
1035 logf (LOG_DEBUG, "Regexp-2 tolerance=%d", r);
1036 r = dict_lookup_grep (zh->reg->dict, term_dict, r, grep_info,
1037 &max_pos, 2, grep_handle);
1039 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
1042 case 104: /* process # and ! in term */
1043 term_dict[j++] = '(';
1044 if (!term_104 (zh->reg->zebra_maps, reg_type,
1045 &termp, term_dict + j, space_split, term_dst))
1047 strcat (term_dict, ")");
1048 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1049 &max_pos, 0, grep_handle);
1051 logf (LOG_WARN, "dict_lookup_grep err, trunc=#/!: %d", r);
1053 case 105: /* process * and ! in term */
1054 term_dict[j++] = '(';
1055 if (!term_105 (zh->reg->zebra_maps, reg_type,
1056 &termp, term_dict + j, space_split, term_dst, 1))
1058 strcat (term_dict, ")");
1059 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1060 &max_pos, 0, grep_handle);
1062 logf (LOG_WARN, "dict_lookup_grep err, trunc=*/!: %d", r);
1064 case 106: /* process * and ! in term */
1065 term_dict[j++] = '(';
1066 if (!term_105 (zh->reg->zebra_maps, reg_type,
1067 &termp, term_dict + j, space_split, term_dst, 0))
1069 strcat (term_dict, ")");
1070 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1071 &max_pos, 0, grep_handle);
1073 logf (LOG_WARN, "dict_lookup_grep err, trunc=*/!: %d", r);
1078 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1083 static int trans_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1087 Z_Term *term = zapt->term;
1089 switch (term->which)
1091 case Z_Term_general:
1093 if (zh->iconv_to_utf8 != (iconv_t)(-1))
1095 char *inbuf = term->u.general->buf;
1096 size_t inleft = term->u.general->len;
1097 char *outbuf = termz;
1098 size_t outleft = IT_MAX_WORD-1;
1101 yaz_log (LOG_DEBUG, "converting general from ISO-8859-1");
1102 ret = iconv(zh->iconv_to_utf8, &inbuf, &inleft,
1104 if (ret == (size_t)(-1))
1106 ret = iconv(zh->iconv_to_utf8, 0, 0, 0, 0);
1114 sizez = term->u.general->len;
1115 if (sizez > IT_MAX_WORD-1)
1116 sizez = IT_MAX_WORD-1;
1117 memcpy (termz, term->u.general->buf, sizez);
1118 termz[sizez] = '\0';
1120 case Z_Term_characterString:
1121 sizez = strlen(term->u.characterString);
1122 if (sizez > IT_MAX_WORD-1)
1123 sizez = IT_MAX_WORD-1;
1124 memcpy (termz, term->u.characterString, sizez);
1125 termz[sizez] = '\0';
1133 static void trans_scan_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1134 char *termz, int reg_type)
1136 Z_Term *term = zapt->term;
1138 const char *cp = (const char *) term->u.general->buf;
1139 const char *cp_end = cp + term->u.general->len;
1142 const char *space_map = NULL;
1145 while ((len = (cp_end - cp)) > 0)
1147 map = zebra_maps_input (zh->reg->zebra_maps, reg_type, &cp, len);
1148 if (**map == *CHR_SPACE)
1153 for (src = space_map; *src; src++)
1156 for (src = *map; *src; src++)
1163 static RSET rpn_prox (ZebraHandle zh, RSET *rset, int rset_no,
1164 int ordered, int exclusion, int relation, int distance)
1169 struct it_key **buf;
1171 char prox_term[1024];
1172 int length_prox_term = 0;
1173 int min_nn = 10000000;
1175 int term_type = Z_Term_characterString;
1176 const char *flags = NULL;
1178 rsfd = (RSFD *) xmalloc (sizeof(*rsfd)*rset_no);
1179 more = (int *) xmalloc (sizeof(*more)*rset_no);
1180 buf = (struct it_key **) xmalloc (sizeof(*buf)*rset_no);
1183 for (i = 0; i<rset_no; i++)
1186 for (j = 0; j<rset[i]->no_rset_terms; j++)
1188 const char *nflags = rset[i]->rset_terms[j]->flags;
1189 char *term = rset[i]->rset_terms[j]->name;
1190 int lterm = strlen(term);
1191 if (lterm + length_prox_term < sizeof(prox_term)-1)
1193 if (length_prox_term)
1194 prox_term[length_prox_term++] = ' ';
1195 strcpy (prox_term + length_prox_term, term);
1196 length_prox_term += lterm;
1198 if (min_nn > rset[i]->rset_terms[j]->nn)
1199 min_nn = rset[i]->rset_terms[j]->nn;
1201 term_type = rset[i]->rset_terms[j]->type;
1203 /* only if all term types are of type characterString .. */
1204 /* the resulting term is of that type */
1205 if (term_type != Z_Term_characterString)
1206 term_type = Z_Term_general;
1209 for (i = 0; i<rset_no; i++)
1214 for (i = 0; i<rset_no; i++)
1216 buf[i] = (struct it_key *) xmalloc (sizeof(**buf));
1217 rsfd[i] = rset_open (rset[i], RSETF_READ);
1218 if (!(more[i] = rset_read (rset[i], rsfd[i], buf[i], &term_index)))
1223 /* at least one is empty ... return null set */
1224 rset_null_parms parms;
1226 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1228 parms.rset_term->nn = 0;
1229 result = rset_create (rset_kind_null, &parms);
1231 else if (ordered && relation == 3 && exclusion == 0 && distance == 1)
1233 /* special proximity case = phrase search ... */
1234 rset_temp_parms parms;
1237 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1239 parms.rset_term->nn = min_nn;
1240 parms.cmp = key_compare_it;
1241 parms.key_size = sizeof (struct it_key);
1242 parms.temp_path = res_get (zh->res, "setTmpDir");
1243 result = rset_create (rset_kind_temp, &parms);
1244 rsfd_result = rset_open (result, RSETF_WRITE);
1248 for (i = 1; i<rset_no; i++)
1257 cmp = key_compare_it (buf[i], buf[i-1]);
1260 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1261 buf[i-1], &term_index);
1266 if (buf[i-1]->seqno+1 != buf[i]->seqno)
1268 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1269 buf[i-1], &term_index);
1275 more[i] = rset_read (rset[i], rsfd[i], buf[i],
1282 rset_write (result, rsfd_result, buf[0]);
1283 more[0] = rset_read (*rset, *rsfd, *buf, &term_index);
1286 rset_close (result, rsfd_result);
1288 else if (rset_no == 2)
1290 /* generic proximity case (two input sets only) ... */
1291 rset_temp_parms parms;
1294 logf (LOG_LOG, "generic prox, dist = %d, relation = %d, ordered =%d, exclusion=%d",
1295 distance, relation, ordered, exclusion);
1296 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1298 parms.rset_term->nn = min_nn;
1299 parms.cmp = key_compare_it;
1300 parms.key_size = sizeof (struct it_key);
1301 parms.temp_path = res_get (zh->res, "setTmpDir");
1302 result = rset_create (rset_kind_temp, &parms);
1303 rsfd_result = rset_open (result, RSETF_WRITE);
1305 while (more[0] && more[1])
1307 int cmp = key_compare_it (buf[0], buf[1]);
1309 more[0] = rset_read (rset[0], rsfd[0], buf[0], &term_index);
1311 more[1] = rset_read (rset[1], rsfd[1], buf[1], &term_index);
1314 int sysno = buf[0]->sysno;
1318 seqno[n++] = buf[0]->seqno;
1319 while ((more[0] = rset_read (rset[0], rsfd[0], buf[0],
1321 sysno == buf[0]->sysno)
1323 seqno[n++] = buf[0]->seqno;
1326 for (i = 0; i<n; i++)
1328 int diff = buf[1]->seqno - seqno[i];
1329 int excl = exclusion;
1330 if (!ordered && diff < 0)
1335 if (diff < distance && diff >= 0)
1339 if (diff <= distance && diff >= 0)
1343 if (diff == distance && diff >= 0)
1347 if (diff >= distance && diff >= 0)
1351 if (diff > distance && diff >= 0)
1355 if (diff != distance && diff >= 0)
1361 rset_write (result, rsfd_result, buf[1]);
1365 } while ((more[1] = rset_read (rset[1], rsfd[1], buf[1],
1367 sysno == buf[1]->sysno);
1370 rset_close (result, rsfd_result);
1374 rset_null_parms parms;
1376 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1378 parms.rset_term->nn = 0;
1379 result = rset_create (rset_kind_null, &parms);
1381 for (i = 0; i<rset_no; i++)
1384 rset_close (rset[i], rsfd[i]);
1394 char *normalize_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1395 const char *termz, NMEM stream, unsigned reg_id)
1398 AttrType truncation;
1399 int truncation_value;
1402 attr_init (&truncation, zapt, 5);
1403 truncation_value = attr_find (&truncation, NULL);
1405 switch (truncation_value)
1425 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, ex_list,
1426 termz, strlen(termz));
1428 return nmem_strdup(stream, termz);
1431 char *buf = (char*) nmem_malloc (stream, wrbuf_len(wrbuf)+1);
1432 memcpy (buf, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1433 buf[wrbuf_len(wrbuf)] = '\0';
1438 static void grep_info_delete (struct grep_info *grep_info)
1441 xfree(grep_info->term_no);
1443 xfree (grep_info->isam_p_buf);
1446 static int grep_info_prepare (ZebraHandle zh,
1447 Z_AttributesPlusTerm *zapt,
1448 struct grep_info *grep_info,
1453 int termset_value_numeric;
1454 const char *termset_value_string;
1457 grep_info->term_no = 0;
1459 grep_info->isam_p_size = 0;
1460 grep_info->isam_p_buf = NULL;
1462 grep_info->reg_type = reg_type;
1463 grep_info->termset = 0;
1467 attr_init (&termset, zapt, 8);
1468 termset_value_numeric =
1469 attr_find_ex (&termset, NULL, &termset_value_string);
1470 if (termset_value_numeric != -1)
1473 const char *termset_name = 0;
1474 if (termset_value_numeric != -2)
1477 sprintf (resname, "%d", termset_value_numeric);
1478 termset_name = resname;
1481 termset_name = termset_value_string;
1482 logf (LOG_LOG, "creating termset set %s", termset_name);
1483 grep_info->termset = resultSetAdd (zh, termset_name, 1);
1484 if (!grep_info->termset)
1487 zh->errString = nmem_strdup (stream, termset_name);
1495 static RSET rpn_search_APT_phrase (ZebraHandle zh,
1496 Z_AttributesPlusTerm *zapt,
1497 const char *termz_org,
1498 oid_value attributeSet,
1500 int reg_type, int complete_flag,
1501 const char *rank_type, int xpath_use,
1502 int num_bases, char **basenames)
1504 char term_dst[IT_MAX_WORD+1];
1505 RSET rset[60], result;
1507 struct grep_info grep_info;
1508 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1509 const char *termp = termz;
1512 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1516 logf (LOG_DEBUG, "APT_phrase termp=%s", termp);
1517 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1519 reg_type, complete_flag,
1520 num_bases, basenames,
1521 term_dst, rank_type,
1525 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1528 grep_info_delete (&grep_info);
1531 rset_null_parms parms;
1533 parms.rset_term = rset_term_create (termz, -1, rank_type,
1535 return rset_create (rset_kind_null, &parms);
1537 else if (rset_no == 1)
1539 result = rpn_prox (zh, rset, rset_no, 1, 0, 3, 1);
1540 for (i = 0; i<rset_no; i++)
1541 rset_delete (rset[i]);
1545 static RSET rpn_search_APT_or_list (ZebraHandle zh,
1546 Z_AttributesPlusTerm *zapt,
1547 const char *termz_org,
1548 oid_value attributeSet,
1550 int reg_type, int complete_flag,
1551 const char *rank_type,
1553 int num_bases, char **basenames)
1555 char term_dst[IT_MAX_WORD+1];
1556 RSET rset[60], result;
1558 struct grep_info grep_info;
1559 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1560 const char *termp = termz;
1562 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1566 logf (LOG_DEBUG, "APT_or_list termp=%s", termp);
1567 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1569 reg_type, complete_flag,
1570 num_bases, basenames,
1571 term_dst, rank_type,
1575 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1578 grep_info_delete (&grep_info);
1581 rset_null_parms parms;
1583 parms.rset_term = rset_term_create (termz, -1, rank_type,
1585 return rset_create (rset_kind_null, &parms);
1588 for (i = 1; i<rset_no; i++)
1590 rset_bool_parms bool_parms;
1592 bool_parms.rset_l = result;
1593 bool_parms.rset_r = rset[i];
1594 bool_parms.key_size = sizeof(struct it_key);
1595 bool_parms.cmp = key_compare_it;
1596 result = rset_create (rset_kind_or, &bool_parms);
1601 static RSET rpn_search_APT_and_list (ZebraHandle zh,
1602 Z_AttributesPlusTerm *zapt,
1603 const char *termz_org,
1604 oid_value attributeSet,
1606 int reg_type, int complete_flag,
1607 const char *rank_type,
1609 int num_bases, char **basenames)
1611 char term_dst[IT_MAX_WORD+1];
1612 RSET rset[60], result;
1614 struct grep_info grep_info;
1615 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1616 const char *termp = termz;
1618 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1622 logf (LOG_DEBUG, "APT_and_list termp=%s", termp);
1623 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1625 reg_type, complete_flag,
1626 num_bases, basenames,
1627 term_dst, rank_type,
1631 assert (rset[rset_no]);
1632 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1635 grep_info_delete (&grep_info);
1638 rset_null_parms parms;
1640 parms.rset_term = rset_term_create (termz, -1, rank_type,
1642 return rset_create (rset_kind_null, &parms);
1645 for (i = 1; i<rset_no; i++)
1647 rset_bool_parms bool_parms;
1649 bool_parms.rset_l = result;
1650 bool_parms.rset_r = rset[i];
1651 bool_parms.key_size = sizeof(struct it_key);
1652 bool_parms.cmp = key_compare_it;
1653 result = rset_create (rset_kind_and, &bool_parms);
1658 static int numeric_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1659 const char **term_sub,
1661 oid_value attributeSet,
1662 struct grep_info *grep_info,
1671 char *term_tmp = term_dict + strlen(term_dict);
1673 attr_init (&relation, zapt, 2);
1674 relation_value = attr_find (&relation, NULL);
1676 logf (LOG_DEBUG, "numeric relation value=%d", relation_value);
1678 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub, term_tmp, 1,
1681 term_value = atoi (term_tmp);
1682 switch (relation_value)
1685 logf (LOG_DEBUG, "Relation <");
1686 gen_regular_rel (term_tmp, term_value-1, 1);
1689 logf (LOG_DEBUG, "Relation <=");
1690 gen_regular_rel (term_tmp, term_value, 1);
1693 logf (LOG_DEBUG, "Relation >=");
1694 gen_regular_rel (term_tmp, term_value, 0);
1697 logf (LOG_DEBUG, "Relation >");
1698 gen_regular_rel (term_tmp, term_value+1, 0);
1702 logf (LOG_DEBUG, "Relation =");
1703 sprintf (term_tmp, "(0*%d)", term_value);
1705 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_tmp);
1706 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info, max_pos,
1709 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1710 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1714 static int numeric_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1715 const char **term_sub,
1716 oid_value attributeSet, struct grep_info *grep_info,
1717 int reg_type, int complete_flag,
1718 int num_bases, char **basenames,
1721 char term_dict[2*IT_MAX_WORD+2];
1725 oid_value curAttributeSet = attributeSet;
1727 struct rpn_char_map_info rcmi;
1729 rpn_char_map_prepare (zh->reg, reg_type, &rcmi);
1730 attr_init (&use, zapt, 1);
1731 use_value = attr_find (&use, &curAttributeSet);
1732 logf (LOG_DEBUG, "numeric_term, use value %d", use_value);
1734 if (use_value == -1)
1737 for (base_no = 0; base_no < num_bases; base_no++)
1740 data1_local_attribute *local_attr;
1741 int max_pos, prefix_len = 0;
1744 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
1746 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1747 curAttributeSet, use_value, r);
1754 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
1756 zh->errCode = 109; /* Database unavailable */
1757 zh->errString = basenames[base_no];
1760 for (local_attr = attp.local_attributes; local_attr;
1761 local_attr = local_attr->next)
1767 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
1772 term_dict[prefix_len++] = '|';
1774 term_dict[prefix_len++] = '(';
1776 ord_len = key_SU_encode (ord, ord_buf);
1777 for (i = 0; i<ord_len; i++)
1779 term_dict[prefix_len++] = 1;
1780 term_dict[prefix_len++] = ord_buf[i];
1788 term_dict[prefix_len++] = ')';
1789 term_dict[prefix_len++] = 1;
1790 term_dict[prefix_len++] = reg_type;
1791 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1792 term_dict[prefix_len] = '\0';
1793 if (!numeric_relation (zh, zapt, &termp, term_dict,
1794 attributeSet, grep_info, &max_pos, reg_type,
1799 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1803 static RSET rpn_search_APT_numeric (ZebraHandle zh,
1804 Z_AttributesPlusTerm *zapt,
1806 oid_value attributeSet,
1808 int reg_type, int complete_flag,
1809 const char *rank_type,
1810 int num_bases, char **basenames)
1812 char term_dst[IT_MAX_WORD+1];
1813 const char *termp = termz;
1814 RSET rset[60], result;
1815 int i, r, rset_no = 0;
1816 struct grep_info grep_info;
1818 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1822 logf (LOG_DEBUG, "APT_numeric termp=%s", termp);
1823 grep_info.isam_p_indx = 0;
1824 r = numeric_term (zh, zapt, &termp, attributeSet, &grep_info,
1825 reg_type, complete_flag, num_bases, basenames,
1829 logf (LOG_DEBUG, "term: %s", term_dst);
1830 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1831 grep_info.isam_p_indx, term_dst,
1832 strlen(term_dst), rank_type,
1833 0 /* preserve position */,
1835 assert (rset[rset_no]);
1836 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1839 grep_info_delete (&grep_info);
1842 rset_null_parms parms;
1844 parms.rset_term = rset_term_create (term_dst, -1, rank_type,
1846 return rset_create (rset_kind_null, &parms);
1849 for (i = 1; i<rset_no; i++)
1851 rset_bool_parms bool_parms;
1853 bool_parms.rset_l = result;
1854 bool_parms.rset_r = rset[i];
1855 bool_parms.key_size = sizeof(struct it_key);
1856 bool_parms.cmp = key_compare_it;
1857 result = rset_create (rset_kind_and, &bool_parms);
1862 static RSET rpn_search_APT_local (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1864 oid_value attributeSet,
1866 const char *rank_type)
1871 rset_temp_parms parms;
1873 parms.rset_term = rset_term_create (termz, -1, rank_type,
1875 parms.cmp = key_compare_it;
1876 parms.key_size = sizeof (struct it_key);
1877 parms.temp_path = res_get (zh->res, "setTmpDir");
1878 result = rset_create (rset_kind_temp, &parms);
1879 rsfd = rset_open (result, RSETF_WRITE);
1881 key.sysno = atoi (termz);
1885 rset_write (result, rsfd, &key);
1886 rset_close (result, rsfd);
1890 static RSET rpn_sort_spec (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1891 oid_value attributeSet, NMEM stream,
1892 Z_SortKeySpecList *sort_sequence,
1893 const char *rank_type)
1895 rset_null_parms parms;
1897 int sort_relation_value;
1898 AttrType sort_relation_type;
1903 Z_AttributeElement *ae;
1908 attr_init (&sort_relation_type, zapt, 7);
1909 sort_relation_value = attr_find (&sort_relation_type, &attributeSet);
1911 attr_init (&use_type, zapt, 1);
1912 use_value = attr_find (&use_type, &attributeSet);
1914 if (!sort_sequence->specs)
1916 sort_sequence->num_specs = 10;
1917 sort_sequence->specs = (Z_SortKeySpec **)
1918 nmem_malloc (stream, sort_sequence->num_specs *
1919 sizeof(*sort_sequence->specs));
1920 for (i = 0; i<sort_sequence->num_specs; i++)
1921 sort_sequence->specs[i] = 0;
1923 if (zapt->term->which != Z_Term_general)
1926 i = atoi_n ((char *) zapt->term->u.general->buf,
1927 zapt->term->u.general->len);
1928 if (i >= sort_sequence->num_specs)
1930 sprintf (termz, "%d", i);
1932 oe.proto = PROTO_Z3950;
1933 oe.oclass = CLASS_ATTSET;
1934 oe.value = attributeSet;
1935 if (!oid_ent_to_oid (&oe, oid))
1938 sks = (Z_SortKeySpec *) nmem_malloc (stream, sizeof(*sks));
1939 sks->sortElement = (Z_SortElement *)
1940 nmem_malloc (stream, sizeof(*sks->sortElement));
1941 sks->sortElement->which = Z_SortElement_generic;
1942 sk = sks->sortElement->u.generic = (Z_SortKey *)
1943 nmem_malloc (stream, sizeof(*sk));
1944 sk->which = Z_SortKey_sortAttributes;
1945 sk->u.sortAttributes = (Z_SortAttributes *)
1946 nmem_malloc (stream, sizeof(*sk->u.sortAttributes));
1948 sk->u.sortAttributes->id = oid;
1949 sk->u.sortAttributes->list = (Z_AttributeList *)
1950 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list));
1951 sk->u.sortAttributes->list->num_attributes = 1;
1952 sk->u.sortAttributes->list->attributes = (Z_AttributeElement **)
1953 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list->attributes));
1954 ae = *sk->u.sortAttributes->list->attributes = (Z_AttributeElement *)
1955 nmem_malloc (stream, sizeof(**sk->u.sortAttributes->list->attributes));
1956 ae->attributeSet = 0;
1957 ae->attributeType = (int *)
1958 nmem_malloc (stream, sizeof(*ae->attributeType));
1959 *ae->attributeType = 1;
1960 ae->which = Z_AttributeValue_numeric;
1961 ae->value.numeric = (int *)
1962 nmem_malloc (stream, sizeof(*ae->value.numeric));
1963 *ae->value.numeric = use_value;
1965 sks->sortRelation = (int *)
1966 nmem_malloc (stream, sizeof(*sks->sortRelation));
1967 if (sort_relation_value == 1)
1968 *sks->sortRelation = Z_SortRelation_ascending;
1969 else if (sort_relation_value == 2)
1970 *sks->sortRelation = Z_SortRelation_descending;
1972 *sks->sortRelation = Z_SortRelation_ascending;
1974 sks->caseSensitivity = (int *)
1975 nmem_malloc (stream, sizeof(*sks->caseSensitivity));
1976 *sks->caseSensitivity = 0;
1979 sks->which = Z_SortKeySpec_null;
1980 sks->u.null = odr_nullval ();
1982 sks->missingValueAction = 0;
1985 sort_sequence->specs[i] = sks;
1987 parms.rset_term = rset_term_create (termz, -1, rank_type,
1989 return rset_create (rset_kind_null, &parms);
1992 struct xpath_predicate {
1995 #define XPATH_PREDICATE_RELATION 1
2001 #define XPATH_PREDICATE_BOOLEAN 2
2004 struct xpath_predicate *left;
2005 struct xpath_predicate *right;
2010 struct xpath_location_step {
2012 struct xpath_predicate *predicate;
2015 static int parse_xpath(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2016 oid_value attributeSet,
2017 struct xpath_location_step *xpath, NMEM mem)
2019 oid_value curAttributeSet = attributeSet;
2021 const char *use_string = 0;
2025 attr_init (&use, zapt, 1);
2026 attr_find_ex (&use, &curAttributeSet, &use_string);
2028 if (!use_string || *use_string != '/')
2034 while (*cp && !strchr("/[",*cp))
2039 xpath[no].predicate = 0;
2040 xpath[no].part = nmem_malloc (mem, i+1);
2041 memcpy (xpath[no].part, cp - i, i);
2042 xpath[no].part[i] = 0;
2046 struct xpath_predicate *p = xpath[no].predicate =
2047 nmem_malloc (mem, sizeof(struct xpath_predicate));
2049 p->which = XPATH_PREDICATE_RELATION;
2054 for (i = 0; *cp && !strchr("><=] ", *cp); i++)
2056 p->u.relation.name = nmem_malloc (mem, i+1);
2057 memcpy (p->u.relation.name, cp - i, i);
2058 p->u.relation.name[i] = 0;
2063 for (i = 0; *cp && strchr(">=<!", *cp); i++)
2066 p->u.relation.op = nmem_malloc (mem, i+1);
2068 memcpy (p->u.relation.op, cp - i, i);
2069 p->u.relation.op[i] = 0;
2074 if (strchr("\"'", *cp))
2077 for (i = 0; *cp && !strchr("\"'", *cp); i++)
2080 p->u.relation.value = nmem_malloc (mem, i+1);
2082 memcpy (p->u.relation.value, cp - i, i);
2083 p->u.relation.value[i] = 0;
2089 for (i = 0; *cp && !strchr(" ]", *cp); i++)
2091 p->u.relation.value = nmem_malloc (mem, i+1);
2093 memcpy (p->u.relation.value, cp - i, i);
2094 p->u.relation.value[i] = 0;
2101 } /* end of ] predicate */
2111 static RSET xpath_trunc(ZebraHandle zh, NMEM stream,
2112 int reg_type, const char *term, int use,
2113 oid_value curAttributeSet)
2116 struct grep_info grep_info;
2117 char term_dict[2048];
2120 int ord = zebraExplain_lookupSU (zh->reg->zei, curAttributeSet, use);
2121 int ord_len, i, r, max_pos;
2123 if (grep_info_prepare (zh, 0 /* zapt */, &grep_info, '0', stream))
2129 term_dict[prefix_len++] = '|';
2131 term_dict[prefix_len++] = '(';
2133 ord_len = key_SU_encode (ord, ord_buf);
2134 for (i = 0; i<ord_len; i++)
2136 term_dict[prefix_len++] = 1;
2137 term_dict[prefix_len++] = ord_buf[i];
2139 term_dict[prefix_len++] = ')';
2140 term_dict[prefix_len++] = 1;
2141 term_dict[prefix_len++] = reg_type;
2143 strcpy (term_dict+prefix_len, term);
2145 grep_info.isam_p_indx = 0;
2146 r = dict_lookup_grep (zh->reg->dict, term_dict, 0,
2147 &grep_info, &max_pos, 0, grep_handle);
2148 yaz_log (LOG_LOG, "%s %d positions", term,
2149 grep_info.isam_p_indx);
2150 rset = rset_trunc (zh, grep_info.isam_p_buf,
2151 grep_info.isam_p_indx, term, strlen(term),
2152 "void", 1, Z_Term_characterString);
2153 grep_info_delete (&grep_info);
2157 static RSET rpn_search_xpath (ZebraHandle zh,
2158 oid_value attributeSet,
2159 int num_bases, char **basenames,
2160 NMEM stream, const char *rank_type, RSET rset,
2161 int xpath_len, struct xpath_location_step *xpath)
2163 oid_value curAttributeSet = attributeSet;
2170 yaz_log (LOG_LOG, "len=%d", xpath_len);
2171 for (i = 0; i<xpath_len; i++)
2173 yaz_log (LOG_LOG, "XPATH %d %s", i, xpath[i].part);
2177 curAttributeSet = VAL_IDXPATH;
2187 a[@attr=value]/b[@other=othervalue]
2189 /e/@a val range(e/,range(@a,freetext(w,1015,val),@a),e/)
2190 /a/b val range(b/a/,freetext(w,1016,val),b/a/)
2191 /a/b/@c val range(b/a/,range(@c,freetext(w,1016,val),@c),b/a/)
2192 /a/b[@c=y] val range(b/a/,freetext(w,1016,val),b/a/,@c=y)
2193 /a[@c=y]/b val range(a/,range(b/a/,freetext(w,1016,val),b/a/),a/,@c=y)
2194 /a[@c=x]/b[@c=y] range(a/,range(b/a/,freetext(w,1016,val),b/a/,@c=y),a/,@c=x)
2198 dict_grep_cmap (zh->reg->dict, 0, 0);
2200 for (base_no = 0; base_no < num_bases; base_no++)
2202 int level = xpath_len;
2205 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
2207 zh->errCode = 109; /* Database unavailable */
2208 zh->errString = basenames[base_no];
2211 if (level > 0 && xpath[level-1].part[0] == '@')
2213 rset_between_parms parms;
2214 RSET rset_start_attr, rset_end_attr;
2216 rset_start_attr = xpath_trunc(zh, stream,
2217 '0', xpath[level].part+1,
2218 3, curAttributeSet);
2220 rset_end_attr = xpath_trunc(zh, stream,
2221 '0', xpath[level].part+1,
2222 4, curAttributeSet);
2224 parms.key_size = sizeof(struct it_key);
2225 parms.cmp = key_compare_it;
2226 parms.rset_l = rset_start_attr;
2227 parms.rset_m = rset;
2228 parms.rset_r = rset_end_attr;
2229 parms.rset_attr = 0;
2230 parms.printer = key_print_it;
2231 rset = rset_create (rset_kind_between, &parms);
2233 while (--level >= 0)
2235 char xpath_rev[128];
2237 rset_between_parms parms;
2238 RSET rset_start_tag = 0, rset_end_tag = 0, rset_attr = 0;
2242 for (i = level; i >= 1; --i)
2244 const char *cp = xpath[i].part;
2250 memcpy (xpath_rev + len, "[^/]*", 5);
2254 xpath_rev[len++] = *cp;
2255 xpath_rev[len++] = '/';
2257 else if (i == 1) /* // case */
2259 xpath_rev[len++] = '.';
2260 xpath_rev[len++] = '*';
2265 if (xpath[level].predicate &&
2266 xpath[level].predicate->which == XPATH_PREDICATE_RELATION &&
2267 xpath[level].predicate->u.relation.name[0])
2269 char predicate_str[128];
2271 strcpy (predicate_str,
2272 xpath[level].predicate->u.relation.name+1);
2273 if (xpath[level].predicate->u.relation.value)
2275 strcat (predicate_str, "=");
2276 strcat (predicate_str,
2277 xpath[level].predicate->u.relation.value);
2279 rset_attr = xpath_trunc (
2280 zh, stream, '0', predicate_str, 3, curAttributeSet);
2287 yaz_log (LOG_LOG, "xpath_rev (%d) = %s", level, xpath_rev);
2288 if (strlen(xpath_rev))
2290 rset_start_tag = xpath_trunc(zh, stream,
2291 '0', xpath_rev, 1, curAttributeSet);
2293 rset_end_tag = xpath_trunc(zh, stream,
2294 '0', xpath_rev, 2, curAttributeSet);
2296 parms.key_size = sizeof(struct it_key);
2297 parms.cmp = key_compare_it;
2298 parms.rset_l = rset_start_tag;
2299 parms.rset_m = rset;
2300 parms.rset_r = rset_end_tag;
2301 parms.rset_attr = rset_attr;
2302 parms.printer = key_print_it;
2303 rset = rset_create (rset_kind_between, &parms);
2314 static RSET rpn_search_APT (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2315 oid_value attributeSet, NMEM stream,
2316 Z_SortKeySpecList *sort_sequence,
2317 int num_bases, char **basenames)
2320 char *search_type = NULL;
2321 char rank_type[128];
2324 char termz[IT_MAX_WORD+1];
2328 struct xpath_location_step xpath[10];
2330 zebra_maps_attr (zh->reg->zebra_maps, zapt, ®_id, &search_type,
2331 rank_type, &complete_flag, &sort_flag);
2333 logf (LOG_DEBUG, "reg_id=%c", reg_id);
2334 logf (LOG_DEBUG, "complete_flag=%d", complete_flag);
2335 logf (LOG_DEBUG, "search_type=%s", search_type);
2336 logf (LOG_DEBUG, "rank_type=%s", rank_type);
2338 if (trans_term (zh, zapt, termz))
2342 return rpn_sort_spec (zh, zapt, attributeSet, stream, sort_sequence,
2344 xpath_len = parse_xpath(zh, zapt, attributeSet, xpath, stream);
2348 if (xpath[xpath_len-1].part[0] == '@')
2352 if (!strcmp (search_type, "phrase"))
2354 rset = rpn_search_APT_phrase (zh, zapt, termz, attributeSet, stream,
2355 reg_id, complete_flag, rank_type,
2357 num_bases, basenames);
2359 else if (!strcmp (search_type, "and-list"))
2361 rset = rpn_search_APT_and_list (zh, zapt, termz, attributeSet, stream,
2362 reg_id, complete_flag, rank_type,
2364 num_bases, basenames);
2366 else if (!strcmp (search_type, "or-list"))
2368 rset = rpn_search_APT_or_list (zh, zapt, termz, attributeSet, stream,
2369 reg_id, complete_flag, rank_type,
2371 num_bases, basenames);
2373 else if (!strcmp (search_type, "local"))
2375 rset = rpn_search_APT_local (zh, zapt, termz, attributeSet, stream,
2378 else if (!strcmp (search_type, "numeric"))
2380 rset = rpn_search_APT_numeric (zh, zapt, termz, attributeSet, stream,
2381 reg_id, complete_flag, rank_type,
2382 num_bases, basenames);
2384 else if (!strcmp (search_type, "always"))
2390 return rpn_search_xpath (zh, attributeSet, num_bases, basenames,
2391 stream, rank_type, rset, xpath_len, xpath);
2394 static RSET rpn_search_structure (ZebraHandle zh, Z_RPNStructure *zs,
2395 oid_value attributeSet, NMEM stream,
2396 Z_SortKeySpecList *sort_sequence,
2397 int num_bases, char **basenames)
2400 if (zs->which == Z_RPNStructure_complex)
2402 Z_Operator *zop = zs->u.complex->roperator;
2403 rset_bool_parms bool_parms;
2405 bool_parms.rset_l = rpn_search_structure (zh, zs->u.complex->s1,
2406 attributeSet, stream,
2408 num_bases, basenames);
2409 if (bool_parms.rset_l == NULL)
2411 bool_parms.rset_r = rpn_search_structure (zh, zs->u.complex->s2,
2412 attributeSet, stream,
2414 num_bases, basenames);
2415 if (bool_parms.rset_r == NULL)
2417 rset_delete (bool_parms.rset_l);
2420 bool_parms.key_size = sizeof(struct it_key);
2421 bool_parms.cmp = key_compare_it;
2425 case Z_Operator_and:
2426 r = rset_create (rset_kind_and, &bool_parms);
2429 r = rset_create (rset_kind_or, &bool_parms);
2431 case Z_Operator_and_not:
2432 r = rset_create (rset_kind_not, &bool_parms);
2434 case Z_Operator_prox:
2436 if (zop->u.prox->which != Z_ProximityOperator_known)
2442 if (zop->u.prox->which != Z_ProxCode_known)
2450 if (*zop->u.prox->u.known != Z_ProxUnit_word)
2452 char *val = (char *) nmem_malloc (stream, 16);
2454 zh->errString = val;
2455 sprintf (val, "%d", *zop->u.prox->u.known);
2459 if (*zop->u.prox->proximityUnitCode != Z_ProxUnit_word)
2461 char *val = (char *) nmem_malloc (stream, 16);
2463 zh->errString = val;
2464 sprintf (val, "%d", *zop->u.prox->proximityUnitCode);
2472 rsets[0] = bool_parms.rset_l;
2473 rsets[1] = bool_parms.rset_r;
2475 r = rpn_prox (zh, rsets, 2,
2476 *zop->u.prox->ordered,
2477 (!zop->u.prox->exclusion ? 0 :
2478 *zop->u.prox->exclusion),
2479 *zop->u.prox->relationType,
2480 *zop->u.prox->distance);
2481 rset_delete (rsets[0]);
2482 rset_delete (rsets[1]);
2490 else if (zs->which == Z_RPNStructure_simple)
2492 if (zs->u.simple->which == Z_Operand_APT)
2494 logf (LOG_DEBUG, "rpn_search_APT");
2495 r = rpn_search_APT (zh, zs->u.simple->u.attributesPlusTerm,
2496 attributeSet, stream, sort_sequence,
2497 num_bases, basenames);
2499 else if (zs->u.simple->which == Z_Operand_resultSetId)
2501 logf (LOG_DEBUG, "rpn_search_ref");
2502 r = resultSetRef (zh, zs->u.simple->u.resultSetId);
2505 r = rset_create (rset_kind_null, NULL);
2508 nmem_strdup (stream, zs->u.simple->u.resultSetId);
2527 RSET rpn_search (ZebraHandle zh, NMEM nmem,
2528 Z_RPNQuery *rpn, int num_bases, char **basenames,
2529 const char *setname,
2534 oid_value attributeSet;
2535 Z_SortKeySpecList *sort_sequence;
2539 zh->errString = NULL;
2542 sort_sequence = (Z_SortKeySpecList *)
2543 nmem_malloc (nmem, sizeof(*sort_sequence));
2544 sort_sequence->num_specs = 10;
2545 sort_sequence->specs = (Z_SortKeySpec **)
2546 nmem_malloc (nmem, sort_sequence->num_specs *
2547 sizeof(*sort_sequence->specs));
2548 for (i = 0; i<sort_sequence->num_specs; i++)
2549 sort_sequence->specs[i] = 0;
2551 attrset = oid_getentbyoid (rpn->attributeSetId);
2552 attributeSet = attrset->value;
2553 rset = rpn_search_structure (zh, rpn->RPNStructure, attributeSet,
2554 nmem, sort_sequence, num_bases, basenames);
2559 logf (LOG_DEBUG, "search error: %d", zh->errCode);
2561 for (i = 0; sort_sequence->specs[i]; i++)
2563 sort_sequence->num_specs = i;
2565 resultSetRank (zh, sset, rset);
2568 logf (LOG_DEBUG, "resultSetSortSingle in rpn_search");
2569 resultSetSortSingle (zh, nmem, sset, rset,
2570 sort_sequence, &sort_status);
2573 logf (LOG_DEBUG, "resultSetSortSingle status = %d", zh->errCode);
2579 struct scan_info_entry {
2585 struct scan_info_entry *list;
2591 static int scan_handle (char *name, const char *info, int pos, void *client)
2593 int len_prefix, idx;
2594 struct scan_info *scan_info = (struct scan_info *) client;
2596 len_prefix = strlen(scan_info->prefix);
2597 if (memcmp (name, scan_info->prefix, len_prefix))
2599 if (pos > 0) idx = scan_info->after - pos + scan_info->before;
2602 scan_info->list[idx].term = (char *)
2603 odr_malloc (scan_info->odr, strlen(name + len_prefix)+1);
2604 strcpy (scan_info->list[idx].term, name + len_prefix);
2605 assert (*info == sizeof(ISAMS_P));
2606 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAMS_P));
2610 static void scan_term_untrans (ZebraHandle zh, NMEM stream, int reg_type,
2611 char **dst, const char *src)
2613 char term_dst[1024];
2615 term_untrans (zh, reg_type, term_dst, src);
2617 *dst = (char *) nmem_malloc (stream, strlen(term_dst)+1);
2618 strcpy (*dst, term_dst);
2621 static void count_set (RSET r, int *count)
2629 logf (LOG_DEBUG, "count_set");
2632 rfd = rset_open (r, RSETF_READ);
2633 while (rset_read (r, rfd, &key, &term_index))
2635 if (key.sysno != psysno)
2642 rset_close (r, rfd);
2643 logf (LOG_DEBUG, "%d keys, %d records", kno, *count);
2646 void rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
2647 oid_value attributeset,
2648 int num_bases, char **basenames,
2649 int *position, int *num_entries, ZebraScanEntry **list,
2653 int pos = *position;
2654 int num = *num_entries;
2658 char termz[IT_MAX_WORD+20];
2661 struct scan_info *scan_info_array;
2662 ZebraScanEntry *glist;
2663 int ords[32], ord_no = 0;
2667 char *search_type = NULL;
2668 char rank_type[128];
2673 if (attributeset == VAL_NONE)
2674 attributeset = VAL_BIB1;
2676 yaz_log (LOG_DEBUG, "position = %d, num = %d set=%d",
2677 pos, num, attributeset);
2679 attr_init (&use, zapt, 1);
2680 use_value = attr_find (&use, &attributeset);
2682 if (zebra_maps_attr (zh->reg->zebra_maps, zapt, ®_id, &search_type,
2683 rank_type, &complete_flag, &sort_flag))
2689 yaz_log (LOG_DEBUG, "use_value = %d", use_value);
2691 if (use_value == -1)
2693 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
2697 data1_local_attribute *local_attr;
2699 if ((r=att_getentbyatt (zh, &attp, attributeset, use_value)))
2701 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
2702 attributeset, use_value);
2710 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
2712 zh->errString = basenames[base_no];
2713 zh->errCode = 109; /* Database unavailable */
2717 for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
2718 local_attr = local_attr->next)
2722 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
2725 ords[ord_no++] = ord;
2734 /* prepare dictionary scanning */
2737 scan_info_array = (struct scan_info *)
2738 odr_malloc (stream, ord_no * sizeof(*scan_info_array));
2739 for (i = 0; i < ord_no; i++)
2741 int j, prefix_len = 0;
2742 int before_tmp = before, after_tmp = after;
2743 struct scan_info *scan_info = scan_info_array + i;
2744 struct rpn_char_map_info rcmi;
2746 rpn_char_map_prepare (zh->reg, reg_id, &rcmi);
2748 scan_info->before = before;
2749 scan_info->after = after;
2750 scan_info->odr = stream;
2752 scan_info->list = (struct scan_info_entry *)
2753 odr_malloc (stream, (before+after) * sizeof(*scan_info->list));
2754 for (j = 0; j<before+after; j++)
2755 scan_info->list[j].term = NULL;
2757 prefix_len += key_SU_encode (ords[i], termz + prefix_len);
2758 termz[prefix_len++] = reg_id;
2759 termz[prefix_len] = 0;
2760 strcpy (scan_info->prefix, termz);
2762 trans_scan_term (zh, zapt, termz+prefix_len, reg_id);
2764 dict_scan (zh->reg->dict, termz, &before_tmp, &after_tmp,
2765 scan_info, scan_handle);
2767 glist = (ZebraScanEntry *)
2768 odr_malloc (stream, (before+after)*sizeof(*glist));
2770 /* consider terms after main term */
2771 for (i = 0; i < ord_no; i++)
2775 for (i = 0; i<after; i++)
2778 const char *mterm = NULL;
2782 for (j = 0; j < ord_no; j++)
2784 if (ptr[j] < before+after &&
2785 (tst=scan_info_array[j].list[ptr[j]].term) &&
2786 (!mterm || strcmp (tst, mterm) < 0))
2794 scan_term_untrans (zh, stream->mem, reg_id,
2795 &glist[i+before].term, mterm);
2796 rset = rset_trunc (zh, &scan_info_array[j0].list[ptr[j0]].isam_p, 1,
2797 glist[i+before].term, strlen(glist[i+before].term),
2798 NULL, 0, zapt->term->which);
2801 for (j = j0+1; j<ord_no; j++)
2803 if (ptr[j] < before+after &&
2804 (tst=scan_info_array[j].list[ptr[j]].term) &&
2805 !strcmp (tst, mterm))
2807 rset_bool_parms bool_parms;
2811 rset_trunc (zh, &scan_info_array[j].list[ptr[j]].isam_p, 1,
2812 glist[i+before].term,
2813 strlen(glist[i+before].term), NULL, 0,
2816 bool_parms.key_size = sizeof(struct it_key);
2817 bool_parms.cmp = key_compare_it;
2818 bool_parms.rset_l = rset;
2819 bool_parms.rset_r = rset2;
2821 rset = rset_create (rset_kind_or, &bool_parms);
2826 count_set (rset, &glist[i+before].occurrences);
2831 *num_entries -= (after-i);
2835 /* consider terms before main term */
2836 for (i = 0; i<ord_no; i++)
2839 for (i = 0; i<before; i++)
2842 const char *mterm = NULL;
2846 for (j = 0; j <ord_no; j++)
2848 if (ptr[j] < before &&
2849 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2850 (!mterm || strcmp (tst, mterm) > 0))
2859 scan_term_untrans (zh, stream->mem, reg_id,
2860 &glist[before-1-i].term, mterm);
2863 (zh, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1,
2864 glist[before-1-i].term, strlen(glist[before-1-i].term),
2865 NULL, 0, zapt->term->which);
2869 for (j = j0+1; j<ord_no; j++)
2871 if (ptr[j] < before &&
2872 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2873 !strcmp (tst, mterm))
2875 rset_bool_parms bool_parms;
2878 rset2 = rset_trunc (zh,
2879 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1,
2880 glist[before-1-i].term,
2881 strlen(glist[before-1-i].term), NULL, 0,
2884 bool_parms.key_size = sizeof(struct it_key);
2885 bool_parms.cmp = key_compare_it;
2886 bool_parms.rset_l = rset;
2887 bool_parms.rset_r = rset2;
2889 rset = rset_create (rset_kind_or, &bool_parms);
2894 count_set (rset, &glist[before-1-i].occurrences);
2904 *list = glist + i; /* list is set to first 'real' entry */
2906 logf (LOG_DEBUG, "position = %d, num_entries = %d",
2907 *position, *num_entries);
2909 logf (LOG_DEBUG, "scan error: %d", zh->errCode);