2 * Copyright (C) 1995-2002, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
6 * $Id: zrpn.c,v 1.117 2002-07-25 13:06:43 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,
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)
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,
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,
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 if (use_value == -2) /* string attribute - assumy "any" */
867 for (base_no = 0; base_no < num_bases; base_no++)
870 data1_local_attribute id_xpath_attr;
871 data1_local_attribute *local_attr;
872 int max_pos, prefix_len = 0;
876 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
878 zh->errCode = 109; /* Database unavailable */
879 zh->errString = basenames[base_no];
882 if (curAttributeSet == VAL_IDXPATH)
884 attp.local_attributes = &id_xpath_attr;
885 attp.attset_ordinal = curAttributeSet;
886 id_xpath_attr.next = 0;
887 id_xpath_attr.local = use_value;
891 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
893 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
894 curAttributeSet, use_value, r);
897 /* set was found, but value wasn't defined */
899 sprintf (val_str, "%d", use_value);
901 zh->errString = nmem_strdup (stream, val_str);
906 struct oident oident;
908 oident.proto = PROTO_Z3950;
909 oident.oclass = CLASS_ATTSET;
910 oident.value = curAttributeSet;
911 oid_ent_to_oid (&oident, oid);
914 zh->errString = nmem_strdup (stream, oident.desc);
919 for (local_attr = attp.local_attributes; local_attr;
920 local_attr = local_attr->next)
926 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
931 term_dict[prefix_len++] = '|';
933 term_dict[prefix_len++] = '(';
935 ord_len = key_SU_encode (ord, ord_buf);
936 for (i = 0; i<ord_len; i++)
938 term_dict[prefix_len++] = 1;
939 term_dict[prefix_len++] = ord_buf[i];
945 sprintf (val_str, "%d", use_value);
947 zh->errString = nmem_strdup (stream, val_str);
950 term_dict[prefix_len++] = ')';
951 term_dict[prefix_len++] = 1;
952 term_dict[prefix_len++] = reg_type;
953 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
954 term_dict[prefix_len] = '\0';
956 switch (truncation_value)
958 case -1: /* not specified */
959 case 100: /* do not truncate */
960 if (!string_relation (zh, zapt, &termp, term_dict,
962 reg_type, space_split, term_dst))
964 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_dict+prefix_len);
965 r = dict_lookup_grep (zh->reg->dict, term_dict, 0,
966 grep_info, &max_pos, 0, grep_handle);
968 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
970 case 1: /* right truncation */
971 term_dict[j++] = '(';
972 if (!term_100 (zh->reg->zebra_maps, reg_type,
973 &termp, term_dict + j, space_split, term_dst))
975 strcat (term_dict, ".*)");
976 dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
977 &max_pos, 0, grep_handle);
979 case 2: /* keft truncation */
980 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
981 if (!term_100 (zh->reg->zebra_maps, reg_type,
982 &termp, term_dict + j, space_split, term_dst))
984 strcat (term_dict, ")");
985 dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
986 &max_pos, 0, grep_handle);
988 case 3: /* left&right truncation */
989 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
990 if (!term_100 (zh->reg->zebra_maps, reg_type,
991 &termp, term_dict + j, space_split, term_dst))
993 strcat (term_dict, ".*)");
994 dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
995 &max_pos, 0, grep_handle);
999 case 101: /* process # in term */
1000 term_dict[j++] = '(';
1001 if (!term_101 (zh->reg->zebra_maps, reg_type,
1002 &termp, term_dict + j, space_split, term_dst))
1004 strcat (term_dict, ")");
1005 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1006 &max_pos, 0, grep_handle);
1008 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d", r);
1010 case 102: /* Regexp-1 */
1011 term_dict[j++] = '(';
1012 if (!term_102 (zh->reg->zebra_maps, reg_type,
1013 &termp, term_dict + j, space_split, term_dst))
1015 strcat (term_dict, ")");
1016 logf (LOG_DEBUG, "Regexp-1 tolerance=%d", r);
1017 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1018 &max_pos, 0, grep_handle);
1020 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
1023 case 103: /* Regexp-2 */
1025 term_dict[j++] = '(';
1026 if (!term_103 (zh->reg->zebra_maps, reg_type,
1027 &termp, term_dict + j, &r, space_split, term_dst))
1029 strcat (term_dict, ")");
1030 logf (LOG_DEBUG, "Regexp-2 tolerance=%d", r);
1031 r = dict_lookup_grep (zh->reg->dict, term_dict, r, grep_info,
1032 &max_pos, 2, grep_handle);
1034 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
1037 case 104: /* process # and ! in term */
1038 term_dict[j++] = '(';
1039 if (!term_104 (zh->reg->zebra_maps, reg_type,
1040 &termp, term_dict + j, space_split, term_dst))
1042 strcat (term_dict, ")");
1043 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1044 &max_pos, 0, grep_handle);
1046 logf (LOG_WARN, "dict_lookup_grep err, trunc=#/!: %d", r);
1048 case 105: /* process * and ! in term */
1049 term_dict[j++] = '(';
1050 if (!term_105 (zh->reg->zebra_maps, reg_type,
1051 &termp, term_dict + j, space_split, term_dst, 1))
1053 strcat (term_dict, ")");
1054 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1055 &max_pos, 0, grep_handle);
1057 logf (LOG_WARN, "dict_lookup_grep err, trunc=*/!: %d", r);
1059 case 106: /* process * and ! in term */
1060 term_dict[j++] = '(';
1061 if (!term_105 (zh->reg->zebra_maps, reg_type,
1062 &termp, term_dict + j, space_split, term_dst, 0))
1064 strcat (term_dict, ")");
1065 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1066 &max_pos, 0, grep_handle);
1068 logf (LOG_WARN, "dict_lookup_grep err, trunc=*/!: %d", r);
1073 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1078 static int trans_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1082 Z_Term *term = zapt->term;
1084 switch (term->which)
1086 case Z_Term_general:
1088 if (zh->iconv_to_utf8 != (iconv_t)(-1))
1090 char *inbuf = term->u.general->buf;
1091 size_t inleft = term->u.general->len;
1092 char *outbuf = termz;
1093 size_t outleft = IT_MAX_WORD-1;
1096 yaz_log (LOG_DEBUG, "converting general from ISO-8859-1");
1097 ret = iconv(zh->iconv_to_utf8, &inbuf, &inleft,
1099 if (ret == (size_t)(-1))
1101 ret = iconv(zh->iconv_to_utf8, 0, 0, 0, 0);
1109 sizez = term->u.general->len;
1110 if (sizez > IT_MAX_WORD-1)
1111 sizez = IT_MAX_WORD-1;
1112 memcpy (termz, term->u.general->buf, sizez);
1113 termz[sizez] = '\0';
1115 case Z_Term_characterString:
1116 sizez = strlen(term->u.characterString);
1117 if (sizez > IT_MAX_WORD-1)
1118 sizez = IT_MAX_WORD-1;
1119 memcpy (termz, term->u.characterString, sizez);
1120 termz[sizez] = '\0';
1128 static void trans_scan_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1129 char *termz, int reg_type)
1131 Z_Term *term = zapt->term;
1133 const char *cp = (const char *) term->u.general->buf;
1134 const char *cp_end = cp + term->u.general->len;
1137 const char *space_map = NULL;
1140 while ((len = (cp_end - cp)) > 0)
1142 map = zebra_maps_input (zh->reg->zebra_maps, reg_type, &cp, len);
1143 if (**map == *CHR_SPACE)
1148 for (src = space_map; *src; src++)
1151 for (src = *map; *src; src++)
1158 static RSET rpn_prox (ZebraHandle zh, RSET *rset, int rset_no,
1159 int ordered, int exclusion, int relation, int distance)
1164 struct it_key **buf;
1166 char prox_term[1024];
1167 int length_prox_term = 0;
1168 int min_nn = 10000000;
1170 int term_type = Z_Term_characterString;
1171 const char *flags = NULL;
1173 rsfd = (RSFD *) xmalloc (sizeof(*rsfd)*rset_no);
1174 more = (int *) xmalloc (sizeof(*more)*rset_no);
1175 buf = (struct it_key **) xmalloc (sizeof(*buf)*rset_no);
1178 for (i = 0; i<rset_no; i++)
1181 for (j = 0; j<rset[i]->no_rset_terms; j++)
1183 const char *nflags = rset[i]->rset_terms[j]->flags;
1184 char *term = rset[i]->rset_terms[j]->name;
1185 int lterm = strlen(term);
1186 if (lterm + length_prox_term < sizeof(prox_term)-1)
1188 if (length_prox_term)
1189 prox_term[length_prox_term++] = ' ';
1190 strcpy (prox_term + length_prox_term, term);
1191 length_prox_term += lterm;
1193 if (min_nn > rset[i]->rset_terms[j]->nn)
1194 min_nn = rset[i]->rset_terms[j]->nn;
1196 term_type = rset[i]->rset_terms[j]->type;
1198 /* only if all term types are of type characterString .. */
1199 /* the resulting term is of that type */
1200 if (term_type != Z_Term_characterString)
1201 term_type = Z_Term_general;
1204 for (i = 0; i<rset_no; i++)
1209 for (i = 0; i<rset_no; i++)
1211 buf[i] = (struct it_key *) xmalloc (sizeof(**buf));
1212 rsfd[i] = rset_open (rset[i], RSETF_READ);
1213 if (!(more[i] = rset_read (rset[i], rsfd[i], buf[i], &term_index)))
1218 /* at least one is empty ... return null set */
1219 rset_null_parms parms;
1221 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1223 parms.rset_term->nn = 0;
1224 result = rset_create (rset_kind_null, &parms);
1226 else if (ordered && relation == 3 && exclusion == 0 && distance == 1)
1228 /* special proximity case = phrase search ... */
1229 rset_temp_parms parms;
1232 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1234 parms.rset_term->nn = min_nn;
1235 parms.cmp = key_compare_it;
1236 parms.key_size = sizeof (struct it_key);
1237 parms.temp_path = res_get (zh->res, "setTmpDir");
1238 result = rset_create (rset_kind_temp, &parms);
1239 rsfd_result = rset_open (result, RSETF_WRITE);
1243 for (i = 1; i<rset_no; i++)
1252 cmp = key_compare_it (buf[i], buf[i-1]);
1255 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1256 buf[i-1], &term_index);
1261 if (buf[i-1]->seqno+1 != buf[i]->seqno)
1263 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1264 buf[i-1], &term_index);
1270 more[i] = rset_read (rset[i], rsfd[i], buf[i],
1277 rset_write (result, rsfd_result, buf[0]);
1278 more[0] = rset_read (*rset, *rsfd, *buf, &term_index);
1281 rset_close (result, rsfd_result);
1283 else if (rset_no == 2)
1285 /* generic proximity case (two input sets only) ... */
1286 rset_temp_parms parms;
1289 logf (LOG_LOG, "generic prox, dist = %d, relation = %d, ordered =%d, exclusion=%d",
1290 distance, relation, ordered, exclusion);
1291 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1293 parms.rset_term->nn = min_nn;
1294 parms.cmp = key_compare_it;
1295 parms.key_size = sizeof (struct it_key);
1296 parms.temp_path = res_get (zh->res, "setTmpDir");
1297 result = rset_create (rset_kind_temp, &parms);
1298 rsfd_result = rset_open (result, RSETF_WRITE);
1300 while (more[0] && more[1])
1302 int cmp = key_compare_it (buf[0], buf[1]);
1304 more[0] = rset_read (rset[0], rsfd[0], buf[0], &term_index);
1306 more[1] = rset_read (rset[1], rsfd[1], buf[1], &term_index);
1309 int sysno = buf[0]->sysno;
1313 seqno[n++] = buf[0]->seqno;
1314 while ((more[0] = rset_read (rset[0], rsfd[0], buf[0],
1316 sysno == buf[0]->sysno)
1318 seqno[n++] = buf[0]->seqno;
1321 for (i = 0; i<n; i++)
1323 int diff = buf[1]->seqno - seqno[i];
1324 int excl = exclusion;
1325 if (!ordered && diff < 0)
1330 if (diff < distance && diff >= 0)
1334 if (diff <= distance && diff >= 0)
1338 if (diff == distance && diff >= 0)
1342 if (diff >= distance && diff >= 0)
1346 if (diff > distance && diff >= 0)
1350 if (diff != distance && diff >= 0)
1356 rset_write (result, rsfd_result, buf[1]);
1360 } while ((more[1] = rset_read (rset[1], rsfd[1], buf[1],
1362 sysno == buf[1]->sysno);
1365 rset_close (result, rsfd_result);
1369 rset_null_parms parms;
1371 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1373 parms.rset_term->nn = 0;
1374 result = rset_create (rset_kind_null, &parms);
1376 for (i = 0; i<rset_no; i++)
1379 rset_close (rset[i], rsfd[i]);
1389 char *normalize_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1390 const char *termz, NMEM stream, unsigned reg_id)
1393 AttrType truncation;
1394 int truncation_value;
1397 attr_init (&truncation, zapt, 5);
1398 truncation_value = attr_find (&truncation, NULL);
1400 switch (truncation_value)
1420 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, ex_list,
1421 termz, strlen(termz));
1423 return nmem_strdup(stream, termz);
1426 char *buf = (char*) nmem_malloc (stream, wrbuf_len(wrbuf)+1);
1427 memcpy (buf, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1428 buf[wrbuf_len(wrbuf)] = '\0';
1433 static void grep_info_delete (struct grep_info *grep_info)
1436 xfree(grep_info->term_no);
1438 xfree (grep_info->isam_p_buf);
1441 static int grep_info_prepare (ZebraHandle zh,
1442 Z_AttributesPlusTerm *zapt,
1443 struct grep_info *grep_info,
1448 int termset_value_numeric;
1449 const char *termset_value_string;
1452 grep_info->term_no = 0;
1454 grep_info->isam_p_size = 0;
1455 grep_info->isam_p_buf = NULL;
1457 grep_info->reg_type = reg_type;
1458 grep_info->termset = 0;
1460 attr_init (&termset, zapt, 8);
1461 termset_value_numeric =
1462 attr_find_ex (&termset, NULL, &termset_value_string);
1463 if (termset_value_numeric != -1)
1466 const char *termset_name = 0;
1467 if (termset_value_numeric != -2)
1470 sprintf (resname, "%d", termset_value_numeric);
1471 termset_name = resname;
1474 termset_name = termset_value_string;
1475 logf (LOG_LOG, "creating termset set %s", termset_name);
1476 grep_info->termset = resultSetAdd (zh, termset_name, 1);
1477 if (!grep_info->termset)
1480 zh->errString = nmem_strdup (stream, termset_name);
1488 static RSET rpn_search_APT_phrase (ZebraHandle zh,
1489 Z_AttributesPlusTerm *zapt,
1490 const char *termz_org,
1491 oid_value attributeSet,
1493 int reg_type, int complete_flag,
1494 const char *rank_type,
1495 int num_bases, char **basenames)
1497 char term_dst[IT_MAX_WORD+1];
1498 RSET rset[60], result;
1500 struct grep_info grep_info;
1501 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1502 const char *termp = termz;
1505 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1509 logf (LOG_DEBUG, "APT_phrase termp=%s", termp);
1510 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1512 reg_type, complete_flag,
1513 num_bases, basenames,
1514 term_dst, rank_type);
1517 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1520 grep_info_delete (&grep_info);
1523 rset_null_parms parms;
1525 parms.rset_term = rset_term_create (termz, -1, rank_type,
1527 return rset_create (rset_kind_null, &parms);
1529 else if (rset_no == 1)
1531 result = rpn_prox (zh, rset, rset_no, 1, 0, 3, 1);
1532 for (i = 0; i<rset_no; i++)
1533 rset_delete (rset[i]);
1537 static RSET rpn_search_APT_or_list (ZebraHandle zh,
1538 Z_AttributesPlusTerm *zapt,
1539 const char *termz_org,
1540 oid_value attributeSet,
1542 int reg_type, int complete_flag,
1543 const char *rank_type,
1544 int num_bases, char **basenames)
1546 char term_dst[IT_MAX_WORD+1];
1547 RSET rset[60], result;
1549 struct grep_info grep_info;
1550 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1551 const char *termp = termz;
1553 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1557 logf (LOG_DEBUG, "APT_or_list termp=%s", termp);
1558 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1560 reg_type, complete_flag,
1561 num_bases, basenames,
1562 term_dst, rank_type);
1565 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1568 grep_info_delete (&grep_info);
1571 rset_null_parms parms;
1573 parms.rset_term = rset_term_create (termz, -1, rank_type,
1575 return rset_create (rset_kind_null, &parms);
1578 for (i = 1; i<rset_no; i++)
1580 rset_bool_parms bool_parms;
1582 bool_parms.rset_l = result;
1583 bool_parms.rset_r = rset[i];
1584 bool_parms.key_size = sizeof(struct it_key);
1585 bool_parms.cmp = key_compare_it;
1586 result = rset_create (rset_kind_or, &bool_parms);
1591 static RSET rpn_search_APT_and_list (ZebraHandle zh,
1592 Z_AttributesPlusTerm *zapt,
1593 const char *termz_org,
1594 oid_value attributeSet,
1596 int reg_type, int complete_flag,
1597 const char *rank_type,
1598 int num_bases, char **basenames)
1600 char term_dst[IT_MAX_WORD+1];
1601 RSET rset[60], result;
1603 struct grep_info grep_info;
1604 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1605 const char *termp = termz;
1607 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1611 logf (LOG_DEBUG, "APT_and_list termp=%s", termp);
1612 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1614 reg_type, complete_flag,
1615 num_bases, basenames,
1616 term_dst, rank_type);
1619 assert (rset[rset_no]);
1620 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1623 grep_info_delete (&grep_info);
1626 rset_null_parms parms;
1628 parms.rset_term = rset_term_create (termz, -1, rank_type,
1630 return rset_create (rset_kind_null, &parms);
1633 for (i = 1; i<rset_no; i++)
1635 rset_bool_parms bool_parms;
1637 bool_parms.rset_l = result;
1638 bool_parms.rset_r = rset[i];
1639 bool_parms.key_size = sizeof(struct it_key);
1640 bool_parms.cmp = key_compare_it;
1641 result = rset_create (rset_kind_and, &bool_parms);
1646 static int numeric_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1647 const char **term_sub,
1649 oid_value attributeSet,
1650 struct grep_info *grep_info,
1659 char *term_tmp = term_dict + strlen(term_dict);
1661 attr_init (&relation, zapt, 2);
1662 relation_value = attr_find (&relation, NULL);
1664 logf (LOG_DEBUG, "numeric relation value=%d", relation_value);
1666 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub, term_tmp, 1,
1669 term_value = atoi (term_tmp);
1670 switch (relation_value)
1673 logf (LOG_DEBUG, "Relation <");
1674 gen_regular_rel (term_tmp, term_value-1, 1);
1677 logf (LOG_DEBUG, "Relation <=");
1678 gen_regular_rel (term_tmp, term_value, 1);
1681 logf (LOG_DEBUG, "Relation >=");
1682 gen_regular_rel (term_tmp, term_value, 0);
1685 logf (LOG_DEBUG, "Relation >");
1686 gen_regular_rel (term_tmp, term_value+1, 0);
1690 logf (LOG_DEBUG, "Relation =");
1691 sprintf (term_tmp, "(0*%d)", term_value);
1693 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_tmp);
1694 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info, max_pos,
1697 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1698 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1702 static int numeric_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1703 const char **term_sub,
1704 oid_value attributeSet, struct grep_info *grep_info,
1705 int reg_type, int complete_flag,
1706 int num_bases, char **basenames,
1709 char term_dict[2*IT_MAX_WORD+2];
1713 oid_value curAttributeSet = attributeSet;
1715 struct rpn_char_map_info rcmi;
1717 rpn_char_map_prepare (zh->reg, reg_type, &rcmi);
1718 attr_init (&use, zapt, 1);
1719 use_value = attr_find (&use, &curAttributeSet);
1720 logf (LOG_DEBUG, "numeric_term, use value %d", use_value);
1722 if (use_value == -1)
1725 for (base_no = 0; base_no < num_bases; base_no++)
1728 data1_local_attribute *local_attr;
1729 int max_pos, prefix_len = 0;
1732 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
1734 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1735 curAttributeSet, use_value, r);
1742 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
1744 zh->errCode = 109; /* Database unavailable */
1745 zh->errString = basenames[base_no];
1748 for (local_attr = attp.local_attributes; local_attr;
1749 local_attr = local_attr->next)
1755 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
1760 term_dict[prefix_len++] = '|';
1762 term_dict[prefix_len++] = '(';
1764 ord_len = key_SU_encode (ord, ord_buf);
1765 for (i = 0; i<ord_len; i++)
1767 term_dict[prefix_len++] = 1;
1768 term_dict[prefix_len++] = ord_buf[i];
1776 term_dict[prefix_len++] = ')';
1777 term_dict[prefix_len++] = 1;
1778 term_dict[prefix_len++] = reg_type;
1779 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1780 term_dict[prefix_len] = '\0';
1781 if (!numeric_relation (zh, zapt, &termp, term_dict,
1782 attributeSet, grep_info, &max_pos, reg_type,
1787 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1791 static RSET rpn_search_APT_numeric (ZebraHandle zh,
1792 Z_AttributesPlusTerm *zapt,
1794 oid_value attributeSet,
1796 int reg_type, int complete_flag,
1797 const char *rank_type,
1798 int num_bases, char **basenames)
1800 char term_dst[IT_MAX_WORD+1];
1801 const char *termp = termz;
1802 RSET rset[60], result;
1803 int i, r, rset_no = 0;
1804 struct grep_info grep_info;
1806 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1810 logf (LOG_DEBUG, "APT_numeric termp=%s", termp);
1811 grep_info.isam_p_indx = 0;
1812 r = numeric_term (zh, zapt, &termp, attributeSet, &grep_info,
1813 reg_type, complete_flag, num_bases, basenames,
1817 logf (LOG_DEBUG, "term: %s", term_dst);
1818 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1819 grep_info.isam_p_indx, term_dst,
1820 strlen(term_dst), rank_type,
1821 0 /* preserve position */,
1823 assert (rset[rset_no]);
1824 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1827 grep_info_delete (&grep_info);
1830 rset_null_parms parms;
1832 parms.rset_term = rset_term_create (term_dst, -1, rank_type,
1834 return rset_create (rset_kind_null, &parms);
1837 for (i = 1; i<rset_no; i++)
1839 rset_bool_parms bool_parms;
1841 bool_parms.rset_l = result;
1842 bool_parms.rset_r = rset[i];
1843 bool_parms.key_size = sizeof(struct it_key);
1844 bool_parms.cmp = key_compare_it;
1845 result = rset_create (rset_kind_and, &bool_parms);
1850 static RSET rpn_search_APT_local (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1852 oid_value attributeSet,
1854 const char *rank_type)
1859 rset_temp_parms parms;
1861 parms.rset_term = rset_term_create (termz, -1, rank_type,
1863 parms.cmp = key_compare_it;
1864 parms.key_size = sizeof (struct it_key);
1865 parms.temp_path = res_get (zh->res, "setTmpDir");
1866 result = rset_create (rset_kind_temp, &parms);
1867 rsfd = rset_open (result, RSETF_WRITE);
1869 key.sysno = atoi (termz);
1873 rset_write (result, rsfd, &key);
1874 rset_close (result, rsfd);
1878 static RSET rpn_sort_spec (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1879 oid_value attributeSet, NMEM stream,
1880 Z_SortKeySpecList *sort_sequence,
1881 const char *rank_type)
1883 rset_null_parms parms;
1885 int sort_relation_value;
1886 AttrType sort_relation_type;
1891 Z_AttributeElement *ae;
1896 attr_init (&sort_relation_type, zapt, 7);
1897 sort_relation_value = attr_find (&sort_relation_type, &attributeSet);
1899 attr_init (&use_type, zapt, 1);
1900 use_value = attr_find (&use_type, &attributeSet);
1902 if (!sort_sequence->specs)
1904 sort_sequence->num_specs = 10;
1905 sort_sequence->specs = (Z_SortKeySpec **)
1906 nmem_malloc (stream, sort_sequence->num_specs *
1907 sizeof(*sort_sequence->specs));
1908 for (i = 0; i<sort_sequence->num_specs; i++)
1909 sort_sequence->specs[i] = 0;
1911 if (zapt->term->which != Z_Term_general)
1914 i = atoi_n ((char *) zapt->term->u.general->buf,
1915 zapt->term->u.general->len);
1916 if (i >= sort_sequence->num_specs)
1918 sprintf (termz, "%d", i);
1920 oe.proto = PROTO_Z3950;
1921 oe.oclass = CLASS_ATTSET;
1922 oe.value = attributeSet;
1923 if (!oid_ent_to_oid (&oe, oid))
1926 sks = (Z_SortKeySpec *) nmem_malloc (stream, sizeof(*sks));
1927 sks->sortElement = (Z_SortElement *)
1928 nmem_malloc (stream, sizeof(*sks->sortElement));
1929 sks->sortElement->which = Z_SortElement_generic;
1930 sk = sks->sortElement->u.generic = (Z_SortKey *)
1931 nmem_malloc (stream, sizeof(*sk));
1932 sk->which = Z_SortKey_sortAttributes;
1933 sk->u.sortAttributes = (Z_SortAttributes *)
1934 nmem_malloc (stream, sizeof(*sk->u.sortAttributes));
1936 sk->u.sortAttributes->id = oid;
1937 sk->u.sortAttributes->list = (Z_AttributeList *)
1938 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list));
1939 sk->u.sortAttributes->list->num_attributes = 1;
1940 sk->u.sortAttributes->list->attributes = (Z_AttributeElement **)
1941 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list->attributes));
1942 ae = *sk->u.sortAttributes->list->attributes = (Z_AttributeElement *)
1943 nmem_malloc (stream, sizeof(**sk->u.sortAttributes->list->attributes));
1944 ae->attributeSet = 0;
1945 ae->attributeType = (int *)
1946 nmem_malloc (stream, sizeof(*ae->attributeType));
1947 *ae->attributeType = 1;
1948 ae->which = Z_AttributeValue_numeric;
1949 ae->value.numeric = (int *)
1950 nmem_malloc (stream, sizeof(*ae->value.numeric));
1951 *ae->value.numeric = use_value;
1953 sks->sortRelation = (int *)
1954 nmem_malloc (stream, sizeof(*sks->sortRelation));
1955 if (sort_relation_value == 1)
1956 *sks->sortRelation = Z_SortRelation_ascending;
1957 else if (sort_relation_value == 2)
1958 *sks->sortRelation = Z_SortRelation_descending;
1960 *sks->sortRelation = Z_SortRelation_ascending;
1962 sks->caseSensitivity = (int *)
1963 nmem_malloc (stream, sizeof(*sks->caseSensitivity));
1964 *sks->caseSensitivity = 0;
1967 sks->which = Z_SortKeySpec_null;
1968 sks->u.null = odr_nullval ();
1970 sks->missingValueAction = 0;
1973 sort_sequence->specs[i] = sks;
1975 parms.rset_term = rset_term_create (termz, -1, rank_type,
1977 return rset_create (rset_kind_null, &parms);
1980 static RSET rpn_search_xpath (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1981 oid_value attributeSet,
1982 int num_bases, char **basenames,
1983 NMEM stream, const char *rank_type, RSET rset)
1986 const char *use_string = 0;
1987 oid_value curAttributeSet = attributeSet;
1988 char term_dict[2048];
1991 struct grep_info grep_info;
1993 yaz_log (LOG_LOG, "rpn_search_xpath 1");
1994 attr_init (&use, zapt, 1);
1995 attr_find_ex (&use, &curAttributeSet, &use_string);
1997 if (curAttributeSet != VAL_IDXPATH)
1999 yaz_log (LOG_LOG, "rpn_search_xpath - not 1");
2004 yaz_log (LOG_LOG, "rpn_search_xpath - not 2");
2008 dict_grep_cmap (zh->reg->dict, 0, 0);
2009 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
2012 yaz_log (LOG_LOG, "rpn_search_xpath 2");
2013 for (base_no = 0; base_no < num_bases; base_no++)
2015 const char *termp = use_string;
2016 rset_between_parms parms;
2017 RSET rset_start_tag, rset_end_tag;
2018 int ord, ord_len, i, r, max_pos;
2021 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
2023 zh->errCode = 109; /* Database unavailable */
2024 zh->errString = basenames[base_no];
2029 ord = zebraExplain_lookupSU (zh->reg->zei, curAttributeSet, 1);
2033 term_dict[prefix_len++] = '|';
2035 term_dict[prefix_len++] = '(';
2037 ord_len = key_SU_encode (ord, ord_buf);
2038 for (i = 0; i<ord_len; i++)
2040 term_dict[prefix_len++] = 1;
2041 term_dict[prefix_len++] = ord_buf[i];
2043 term_dict[prefix_len++] = ')';
2044 term_dict[prefix_len++] = 1;
2045 term_dict[prefix_len++] = reg_type;
2048 strcpy (term_dict+prefix_len, use_string);
2050 grep_info.isam_p_indx = 0;
2051 yaz_log (LOG_LOG, "rpn_search_xpath 3 %s", term_dict+prefix_len);
2052 r = dict_lookup_grep (zh->reg->dict, term_dict, 0,
2053 &grep_info, &max_pos, 0, grep_handle);
2054 yaz_log (LOG_LOG, "%s %d positions", use_string,
2055 grep_info.isam_p_indx);
2057 rset_trunc (zh, grep_info.isam_p_buf,
2058 grep_info.isam_p_indx, use_string, strlen(use_string),
2059 rank_type, 1, zapt->term->which);
2062 ord = zebraExplain_lookupSU (zh->reg->zei, curAttributeSet, 2);
2066 term_dict[prefix_len++] = '|';
2068 term_dict[prefix_len++] = '(';
2070 ord_len = key_SU_encode (ord, ord_buf);
2071 for (i = 0; i<ord_len; i++)
2073 term_dict[prefix_len++] = 1;
2074 term_dict[prefix_len++] = ord_buf[i];
2076 term_dict[prefix_len++] = ')';
2077 term_dict[prefix_len++] = 1;
2078 term_dict[prefix_len++] = reg_type;
2082 strcpy (term_dict+prefix_len, use_string);
2084 grep_info.isam_p_indx = 0;
2085 r = dict_lookup_grep (zh->reg->dict, term_dict, 0,
2086 &grep_info, &max_pos, 0, grep_handle);
2088 yaz_log (LOG_LOG, "%s %d positions", use_string,
2089 grep_info.isam_p_indx);
2091 rset_trunc (zh, grep_info.isam_p_buf,
2092 grep_info.isam_p_indx, use_string, strlen(use_string),
2093 rank_type, 1, zapt->term->which);
2095 parms.key_size = sizeof(struct it_key);
2096 parms.cmp = key_compare_it;
2097 parms.rset_l = rset_start_tag;
2098 parms.rset_m = rset;
2099 parms.rset_r = rset_end_tag;
2100 parms.printer = key_print_it;
2101 yaz_log (LOG_LOG, "rpn_search_xpath 4");
2102 rset = rset_create (rset_kind_between, &parms);
2104 grep_info_delete (&grep_info);
2111 static RSET rpn_search_APT (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2112 oid_value attributeSet, NMEM stream,
2113 Z_SortKeySpecList *sort_sequence,
2114 int num_bases, char **basenames)
2117 char *search_type = NULL;
2118 char rank_type[128];
2121 char termz[IT_MAX_WORD+1];
2124 zebra_maps_attr (zh->reg->zebra_maps, zapt, ®_id, &search_type,
2125 rank_type, &complete_flag, &sort_flag);
2127 logf (LOG_DEBUG, "reg_id=%c", reg_id);
2128 logf (LOG_DEBUG, "complete_flag=%d", complete_flag);
2129 logf (LOG_DEBUG, "search_type=%s", search_type);
2130 logf (LOG_DEBUG, "rank_type=%s", rank_type);
2132 if (trans_term (zh, zapt, termz))
2136 return rpn_sort_spec (zh, zapt, attributeSet, stream, sort_sequence,
2139 if (!strcmp (search_type, "phrase"))
2141 rset = rpn_search_APT_phrase (zh, zapt, termz, attributeSet, stream,
2142 reg_id, complete_flag, rank_type,
2143 num_bases, basenames);
2145 else if (!strcmp (search_type, "and-list"))
2147 rset = rpn_search_APT_and_list (zh, zapt, termz, attributeSet, stream,
2148 reg_id, complete_flag, rank_type,
2149 num_bases, basenames);
2151 else if (!strcmp (search_type, "or-list"))
2153 rset = rpn_search_APT_or_list (zh, zapt, termz, attributeSet, stream,
2154 reg_id, complete_flag, rank_type,
2155 num_bases, basenames);
2157 else if (!strcmp (search_type, "local"))
2159 rset = rpn_search_APT_local (zh, zapt, termz, attributeSet, stream,
2162 else if (!strcmp (search_type, "numeric"))
2164 rset = rpn_search_APT_numeric (zh, zapt, termz, attributeSet, stream,
2165 reg_id, complete_flag, rank_type,
2166 num_bases, basenames);
2170 return rpn_search_xpath (zh, zapt, attributeSet, num_bases, basenames,
2171 stream, rank_type, rset);
2174 static RSET rpn_search_structure (ZebraHandle zh, Z_RPNStructure *zs,
2175 oid_value attributeSet, NMEM stream,
2176 Z_SortKeySpecList *sort_sequence,
2177 int num_bases, char **basenames)
2180 if (zs->which == Z_RPNStructure_complex)
2182 Z_Operator *zop = zs->u.complex->roperator;
2183 rset_bool_parms bool_parms;
2185 bool_parms.rset_l = rpn_search_structure (zh, zs->u.complex->s1,
2186 attributeSet, stream,
2188 num_bases, basenames);
2189 if (bool_parms.rset_l == NULL)
2191 bool_parms.rset_r = rpn_search_structure (zh, zs->u.complex->s2,
2192 attributeSet, stream,
2194 num_bases, basenames);
2195 if (bool_parms.rset_r == NULL)
2197 rset_delete (bool_parms.rset_l);
2200 bool_parms.key_size = sizeof(struct it_key);
2201 bool_parms.cmp = key_compare_it;
2205 case Z_Operator_and:
2206 r = rset_create (rset_kind_and, &bool_parms);
2209 r = rset_create (rset_kind_or, &bool_parms);
2211 case Z_Operator_and_not:
2212 r = rset_create (rset_kind_not, &bool_parms);
2214 case Z_Operator_prox:
2216 if (zop->u.prox->which != Z_ProximityOperator_known)
2222 if (zop->u.prox->which != Z_ProxCode_known)
2230 if (*zop->u.prox->u.known != Z_ProxUnit_word)
2232 char *val = (char *) nmem_malloc (stream, 16);
2234 zh->errString = val;
2235 sprintf (val, "%d", *zop->u.prox->u.known);
2239 if (*zop->u.prox->proximityUnitCode != Z_ProxUnit_word)
2241 char *val = (char *) nmem_malloc (stream, 16);
2243 zh->errString = val;
2244 sprintf (val, "%d", *zop->u.prox->proximityUnitCode);
2252 rsets[0] = bool_parms.rset_l;
2253 rsets[1] = bool_parms.rset_r;
2255 r = rpn_prox (zh, rsets, 2,
2256 *zop->u.prox->ordered,
2257 (!zop->u.prox->exclusion ? 0 :
2258 *zop->u.prox->exclusion),
2259 *zop->u.prox->relationType,
2260 *zop->u.prox->distance);
2261 rset_delete (rsets[0]);
2262 rset_delete (rsets[1]);
2270 else if (zs->which == Z_RPNStructure_simple)
2272 if (zs->u.simple->which == Z_Operand_APT)
2274 logf (LOG_DEBUG, "rpn_search_APT");
2275 r = rpn_search_APT (zh, zs->u.simple->u.attributesPlusTerm,
2276 attributeSet, stream, sort_sequence,
2277 num_bases, basenames);
2279 else if (zs->u.simple->which == Z_Operand_resultSetId)
2281 logf (LOG_DEBUG, "rpn_search_ref");
2282 r = resultSetRef (zh, zs->u.simple->u.resultSetId);
2285 r = rset_create (rset_kind_null, NULL);
2288 nmem_strdup (stream, zs->u.simple->u.resultSetId);
2307 RSET rpn_search (ZebraHandle zh, NMEM nmem,
2308 Z_RPNQuery *rpn, int num_bases, char **basenames,
2309 const char *setname,
2314 oid_value attributeSet;
2315 Z_SortKeySpecList *sort_sequence;
2319 zh->errString = NULL;
2322 sort_sequence = (Z_SortKeySpecList *)
2323 nmem_malloc (nmem, sizeof(*sort_sequence));
2324 sort_sequence->num_specs = 10;
2325 sort_sequence->specs = (Z_SortKeySpec **)
2326 nmem_malloc (nmem, sort_sequence->num_specs *
2327 sizeof(*sort_sequence->specs));
2328 for (i = 0; i<sort_sequence->num_specs; i++)
2329 sort_sequence->specs[i] = 0;
2331 attrset = oid_getentbyoid (rpn->attributeSetId);
2332 attributeSet = attrset->value;
2333 rset = rpn_search_structure (zh, rpn->RPNStructure, attributeSet,
2334 nmem, sort_sequence, num_bases, basenames);
2339 logf (LOG_DEBUG, "search error: %d", zh->errCode);
2341 for (i = 0; sort_sequence->specs[i]; i++)
2343 sort_sequence->num_specs = i;
2345 resultSetRank (zh, sset, rset);
2348 logf (LOG_DEBUG, "resultSetSortSingle in rpn_search");
2349 resultSetSortSingle (zh, nmem, sset, rset,
2350 sort_sequence, &sort_status);
2353 logf (LOG_DEBUG, "resultSetSortSingle status = %d", zh->errCode);
2359 struct scan_info_entry {
2365 struct scan_info_entry *list;
2371 static int scan_handle (char *name, const char *info, int pos, void *client)
2373 int len_prefix, idx;
2374 struct scan_info *scan_info = (struct scan_info *) client;
2376 len_prefix = strlen(scan_info->prefix);
2377 if (memcmp (name, scan_info->prefix, len_prefix))
2379 if (pos > 0) idx = scan_info->after - pos + scan_info->before;
2382 scan_info->list[idx].term = (char *)
2383 odr_malloc (scan_info->odr, strlen(name + len_prefix)+1);
2384 strcpy (scan_info->list[idx].term, name + len_prefix);
2385 assert (*info == sizeof(ISAMS_P));
2386 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAMS_P));
2390 static void scan_term_untrans (ZebraHandle zh, NMEM stream, int reg_type,
2391 char **dst, const char *src)
2393 char term_dst[1024];
2395 term_untrans (zh, reg_type, term_dst, src);
2397 *dst = (char *) nmem_malloc (stream, strlen(term_dst)+1);
2398 strcpy (*dst, term_dst);
2401 static void count_set (RSET r, int *count)
2409 logf (LOG_DEBUG, "count_set");
2412 rfd = rset_open (r, RSETF_READ);
2413 while (rset_read (r, rfd, &key, &term_index))
2415 if (key.sysno != psysno)
2422 rset_close (r, rfd);
2423 logf (LOG_DEBUG, "%d keys, %d records", kno, *count);
2426 void rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
2427 oid_value attributeset,
2428 int num_bases, char **basenames,
2429 int *position, int *num_entries, ZebraScanEntry **list,
2433 int pos = *position;
2434 int num = *num_entries;
2438 char termz[IT_MAX_WORD+20];
2441 struct scan_info *scan_info_array;
2442 ZebraScanEntry *glist;
2443 int ords[32], ord_no = 0;
2447 char *search_type = NULL;
2448 char rank_type[128];
2453 if (attributeset == VAL_NONE)
2454 attributeset = VAL_BIB1;
2456 yaz_log (LOG_DEBUG, "position = %d, num = %d set=%d",
2457 pos, num, attributeset);
2459 attr_init (&use, zapt, 1);
2460 use_value = attr_find (&use, &attributeset);
2462 if (zebra_maps_attr (zh->reg->zebra_maps, zapt, ®_id, &search_type,
2463 rank_type, &complete_flag, &sort_flag))
2469 yaz_log (LOG_DEBUG, "use_value = %d", use_value);
2471 if (use_value == -1)
2473 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
2477 data1_local_attribute *local_attr;
2479 if ((r=att_getentbyatt (zh, &attp, attributeset, use_value)))
2481 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
2482 attributeset, use_value);
2490 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
2492 zh->errString = basenames[base_no];
2493 zh->errCode = 109; /* Database unavailable */
2497 for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
2498 local_attr = local_attr->next)
2502 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
2505 ords[ord_no++] = ord;
2514 /* prepare dictionary scanning */
2517 scan_info_array = (struct scan_info *)
2518 odr_malloc (stream, ord_no * sizeof(*scan_info_array));
2519 for (i = 0; i < ord_no; i++)
2521 int j, prefix_len = 0;
2522 int before_tmp = before, after_tmp = after;
2523 struct scan_info *scan_info = scan_info_array + i;
2524 struct rpn_char_map_info rcmi;
2526 rpn_char_map_prepare (zh->reg, reg_id, &rcmi);
2528 scan_info->before = before;
2529 scan_info->after = after;
2530 scan_info->odr = stream;
2532 scan_info->list = (struct scan_info_entry *)
2533 odr_malloc (stream, (before+after) * sizeof(*scan_info->list));
2534 for (j = 0; j<before+after; j++)
2535 scan_info->list[j].term = NULL;
2537 prefix_len += key_SU_encode (ords[i], termz + prefix_len);
2538 termz[prefix_len++] = reg_id;
2539 termz[prefix_len] = 0;
2540 strcpy (scan_info->prefix, termz);
2542 trans_scan_term (zh, zapt, termz+prefix_len, reg_id);
2544 dict_scan (zh->reg->dict, termz, &before_tmp, &after_tmp,
2545 scan_info, scan_handle);
2547 glist = (ZebraScanEntry *)
2548 odr_malloc (stream, (before+after)*sizeof(*glist));
2550 /* consider terms after main term */
2551 for (i = 0; i < ord_no; i++)
2555 for (i = 0; i<after; i++)
2558 const char *mterm = NULL;
2562 for (j = 0; j < ord_no; j++)
2564 if (ptr[j] < before+after &&
2565 (tst=scan_info_array[j].list[ptr[j]].term) &&
2566 (!mterm || strcmp (tst, mterm) < 0))
2574 scan_term_untrans (zh, stream->mem, reg_id,
2575 &glist[i+before].term, mterm);
2576 rset = rset_trunc (zh, &scan_info_array[j0].list[ptr[j0]].isam_p, 1,
2577 glist[i+before].term, strlen(glist[i+before].term),
2578 NULL, 0, zapt->term->which);
2581 for (j = j0+1; j<ord_no; j++)
2583 if (ptr[j] < before+after &&
2584 (tst=scan_info_array[j].list[ptr[j]].term) &&
2585 !strcmp (tst, mterm))
2587 rset_bool_parms bool_parms;
2591 rset_trunc (zh, &scan_info_array[j].list[ptr[j]].isam_p, 1,
2592 glist[i+before].term,
2593 strlen(glist[i+before].term), NULL, 0,
2596 bool_parms.key_size = sizeof(struct it_key);
2597 bool_parms.cmp = key_compare_it;
2598 bool_parms.rset_l = rset;
2599 bool_parms.rset_r = rset2;
2601 rset = rset_create (rset_kind_or, &bool_parms);
2606 count_set (rset, &glist[i+before].occurrences);
2611 *num_entries -= (after-i);
2615 /* consider terms before main term */
2616 for (i = 0; i<ord_no; i++)
2619 for (i = 0; i<before; i++)
2622 const char *mterm = NULL;
2626 for (j = 0; j <ord_no; j++)
2628 if (ptr[j] < before &&
2629 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2630 (!mterm || strcmp (tst, mterm) > 0))
2639 scan_term_untrans (zh, stream->mem, reg_id,
2640 &glist[before-1-i].term, mterm);
2643 (zh, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1,
2644 glist[before-1-i].term, strlen(glist[before-1-i].term),
2645 NULL, 0, zapt->term->which);
2649 for (j = j0+1; j<ord_no; j++)
2651 if (ptr[j] < before &&
2652 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2653 !strcmp (tst, mterm))
2655 rset_bool_parms bool_parms;
2658 rset2 = rset_trunc (zh,
2659 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1,
2660 glist[before-1-i].term,
2661 strlen(glist[before-1-i].term), NULL, 0,
2664 bool_parms.key_size = sizeof(struct it_key);
2665 bool_parms.cmp = key_compare_it;
2666 bool_parms.rset_l = rset;
2667 bool_parms.rset_r = rset2;
2669 rset = rset_create (rset_kind_or, &bool_parms);
2674 count_set (rset, &glist[before-1-i].occurrences);
2684 *list = glist + i; /* list is set to first 'real' entry */
2686 logf (LOG_DEBUG, "position = %d, num_entries = %d",
2687 *position, *num_entries);
2689 logf (LOG_DEBUG, "scan error: %d", zh->errCode);