2 * Copyright (C) 1995-2002, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
6 * $Id: zrpn.c,v 1.115 2002-04-13 18:16: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 /* term_100: handle term, where trunc=none (no operators at all) */
254 static int term_100 (ZebraMaps zebra_maps, int reg_type,
255 const char **src, char *dst, int space_split,
263 const char *space_start = 0;
264 const char *space_end = 0;
266 if (!term_pre (zebra_maps, reg_type, src, NULL, NULL))
272 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
275 if (**map == *CHR_SPACE)
278 else /* complete subfield only. */
280 if (**map == *CHR_SPACE)
281 { /* save space mapping for later .. */
286 else if (space_start)
287 { /* reload last space */
288 while (space_start < space_end)
290 if (!isalnum (*space_start) && *space_start != '-')
292 dst_term[j++] = *space_start;
293 dst[i++] = *space_start++;
296 space_start = space_end = 0;
299 /* add non-space char */
302 if (!isalnum (*s1) && *s1 != '-')
314 /* term_101: handle term, where trunc=Process # */
315 static int term_101 (ZebraMaps zebra_maps, int reg_type,
316 const char **src, char *dst, int space_split,
324 if (!term_pre (zebra_maps, reg_type, src, "#", "#"))
333 dst_term[j++] = *s0++;
338 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
339 if (space_split && **map == *CHR_SPACE)
351 dst_term[j++] = '\0';
356 /* term_103: handle term, where trunc=re-2 (regular expressions) */
357 static int term_103 (ZebraMaps zebra_maps, int reg_type, const char **src,
358 char *dst, int *errors, int space_split,
366 if (!term_pre (zebra_maps, reg_type, src, "^\\()[].*+?|", "("))
369 if (errors && *s0 == '+' && s0[1] && s0[2] == '+' && s0[3] &&
372 *errors = s0[1] - '0';
379 if (strchr ("^\\()[].*+?|-", *s0))
387 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
388 if (**map == *CHR_SPACE)
405 /* term_103: handle term, where trunc=re-1 (regular expressions) */
406 static int term_102 (ZebraMaps zebra_maps, int reg_type, const char **src,
407 char *dst, int space_split, char *dst_term)
409 return term_103 (zebra_maps, reg_type, src, dst, NULL, space_split,
414 /* term_104: handle term, where trunc=Process # and ! */
415 static int term_104 (ZebraMaps zebra_maps, int reg_type,
416 const char **src, char *dst, int space_split,
424 if (!term_pre (zebra_maps, reg_type, src, "#!", "#!"))
433 dst_term[j++] = *s0++;
438 dst_term[j++] = *s0++;
442 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
443 if (space_split && **map == *CHR_SPACE)
455 dst_term[j++] = '\0';
460 /* term_105/106: handle term, where trunc=Process * and ! and right trunc */
461 static int term_105 (ZebraMaps zebra_maps, int reg_type,
462 const char **src, char *dst, int space_split,
463 char *dst_term, int right_truncate)
470 if (!term_pre (zebra_maps, reg_type, src, "*!", "*!"))
479 dst_term[j++] = *s0++;
484 dst_term[j++] = *s0++;
488 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
489 if (space_split && **map == *CHR_SPACE)
507 dst_term[j++] = '\0';
513 /* gen_regular_rel - generate regular expression from relation
514 * val: border value (inclusive)
515 * islt: 1 if <=; 0 if >=.
517 static void gen_regular_rel (char *dst, int val, int islt)
524 logf (LOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
528 strcpy (dst, "(-[0-9]+|(");
536 strcpy (dst, "([0-9]+|-(");
537 dst_p = strlen (dst);
547 dst_p = strlen (dst);
548 sprintf (numstr, "%d", val);
549 for (w = strlen(numstr); --w >= 0; pos++)
568 strcpy (dst + dst_p, numstr);
569 dst_p = strlen(dst) - pos - 1;
597 for (i = 0; i<pos; i++)
610 /* match everything less than 10^(pos-1) */
612 for (i=1; i<pos; i++)
613 strcat (dst, "[0-9]?");
617 /* match everything greater than 10^pos */
618 for (i = 0; i <= pos; i++)
619 strcat (dst, "[0-9]");
620 strcat (dst, "[0-9]*");
625 void string_rel_add_char (char **term_p, const char *src, int *indx)
627 if (src[*indx] == '\\')
628 *(*term_p)++ = src[(*indx)++];
629 *(*term_p)++ = src[(*indx)++];
633 * > abc ([b-].*|a[c-].*|ab[d-].*|abc.+)
634 * ([^-a].*|a[^-b].*ab[^-c].*|abc.+)
635 * >= abc ([b-].*|a[c-].*|ab[c-].*)
636 * ([^-a].*|a[^-b].*|ab[c-].*)
637 * < abc ([-0].*|a[-a].*|ab[-b].*)
638 * ([^a-].*|a[^b-].*|ab[^c-].*)
639 * <= abc ([-0].*|a[-a].*|ab[-b].*|abc)
640 * ([^a-].*|a[^b-].*|ab[^c-].*|abc)
642 static int string_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
643 const char **term_sub, char *term_dict,
644 oid_value attributeSet,
645 int reg_type, int space_split, char *term_dst)
650 char *term_tmp = term_dict + strlen(term_dict);
651 char term_component[256];
653 attr_init (&relation, zapt, 2);
654 relation_value = attr_find (&relation, NULL);
656 logf (LOG_DEBUG, "string relation value=%d", relation_value);
657 switch (relation_value)
660 if (!term_100 (zh->reg->zebra_maps, reg_type,
661 term_sub, term_component,
662 space_split, term_dst))
664 logf (LOG_DEBUG, "Relation <");
667 for (i = 0; term_component[i]; )
674 string_rel_add_char (&term_tmp, term_component, &j);
679 string_rel_add_char (&term_tmp, term_component, &i);
690 if (!term_100 (zh->reg->zebra_maps, reg_type,
691 term_sub, term_component,
692 space_split, term_dst))
694 logf (LOG_DEBUG, "Relation <=");
697 for (i = 0; term_component[i]; )
702 string_rel_add_char (&term_tmp, term_component, &j);
706 string_rel_add_char (&term_tmp, term_component, &i);
715 for (i = 0; term_component[i]; )
716 string_rel_add_char (&term_tmp, term_component, &i);
721 if (!term_100 (zh->reg->zebra_maps, reg_type,
722 term_sub, term_component, space_split, term_dst))
724 logf (LOG_DEBUG, "Relation >");
727 for (i = 0; term_component[i];)
732 string_rel_add_char (&term_tmp, term_component, &j);
737 string_rel_add_char (&term_tmp, term_component, &i);
745 for (i = 0; term_component[i];)
746 string_rel_add_char (&term_tmp, term_component, &i);
753 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub,
754 term_component, space_split, term_dst))
756 logf (LOG_DEBUG, "Relation >=");
759 for (i = 0; term_component[i];)
766 string_rel_add_char (&term_tmp, term_component, &j);
769 if (term_component[i+1])
773 string_rel_add_char (&term_tmp, term_component, &i);
777 string_rel_add_char (&term_tmp, term_component, &i);
789 logf (LOG_DEBUG, "Relation =");
790 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub,
791 term_component, space_split, term_dst))
793 strcat (term_tmp, "(");
794 strcat (term_tmp, term_component);
795 strcat (term_tmp, ")");
800 static int string_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
801 const char **term_sub,
802 oid_value attributeSet, NMEM stream,
803 struct grep_info *grep_info,
804 int reg_type, int complete_flag,
805 int num_bases, char **basenames,
808 static RSET term_trunc (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
809 const char **term_sub,
810 oid_value attributeSet, NMEM stream,
811 struct grep_info *grep_info,
812 int reg_type, int complete_flag,
813 int num_bases, char **basenames,
815 const char *rank_type)
818 grep_info->isam_p_indx = 0;
819 r = string_term (zh, zapt, term_sub, attributeSet, stream, grep_info,
820 reg_type, complete_flag, num_bases, basenames,
824 logf (LOG_DEBUG, "term: %s", term_dst);
825 return rset_trunc (zh, grep_info->isam_p_buf,
826 grep_info->isam_p_indx, term_dst,
827 strlen(term_dst), rank_type, 1 /* preserve pos */);
831 static int string_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
832 const char **term_sub,
833 oid_value attributeSet, NMEM stream,
834 struct grep_info *grep_info,
835 int reg_type, int complete_flag,
836 int num_bases, char **basenames,
839 char term_dict[2*IT_MAX_WORD+4000];
842 int truncation_value;
845 const char *use_string = 0;
846 oid_value curAttributeSet = attributeSet;
848 struct rpn_char_map_info rcmi;
849 int space_split = complete_flag ? 0 : 1;
851 rpn_char_map_prepare (zh->reg, reg_type, &rcmi);
852 attr_init (&use, zapt, 1);
853 use_value = attr_find_ex (&use, &curAttributeSet, &use_string);
854 logf (LOG_DEBUG, "string_term, use value %d", use_value);
855 attr_init (&truncation, zapt, 5);
856 truncation_value = attr_find (&truncation, NULL);
857 logf (LOG_DEBUG, "truncation value %d", truncation_value);
859 if (use_value == -1) /* no attribute - assumy "any" */
861 if (use_value == -2) /* string 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 (curAttributeSet == VAL_IDXPATH)
881 attp.local_attributes = &id_xpath_attr;
882 attp.attset_ordinal = curAttributeSet;
883 id_xpath_attr.next = 0;
884 id_xpath_attr.local = use_value;
888 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
890 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
891 curAttributeSet, use_value, r);
895 sprintf (val_str, "%d", use_value);
897 zh->errString = nmem_strdup (stream, val_str);
902 struct oident oident;
904 oident.proto = PROTO_Z3950;
905 oident.oclass = CLASS_ATTSET;
906 oident.value = curAttributeSet;
907 oid_ent_to_oid (&oident, oid);
910 zh->errString = nmem_strdup (stream, oident.desc);
915 for (local_attr = attp.local_attributes; local_attr;
916 local_attr = local_attr->next)
922 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
927 term_dict[prefix_len++] = '|';
929 term_dict[prefix_len++] = '(';
931 ord_len = key_SU_encode (ord, ord_buf);
932 for (i = 0; i<ord_len; i++)
934 term_dict[prefix_len++] = 1;
935 term_dict[prefix_len++] = ord_buf[i];
941 sprintf (val_str, "%d", use_value);
943 zh->errString = nmem_strdup (stream, val_str);
946 term_dict[prefix_len++] = ')';
947 term_dict[prefix_len++] = 1;
948 term_dict[prefix_len++] = reg_type;
949 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
950 term_dict[prefix_len] = '\0';
952 switch (truncation_value)
954 case -1: /* not specified */
955 case 100: /* do not truncate */
956 if (!string_relation (zh, zapt, &termp, term_dict,
958 reg_type, space_split, term_dst))
960 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_dict+prefix_len);
961 r = dict_lookup_grep (zh->reg->dict, term_dict, 0,
962 grep_info, &max_pos, 0, grep_handle);
964 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
966 case 1: /* right truncation */
967 term_dict[j++] = '(';
968 if (!term_100 (zh->reg->zebra_maps, reg_type,
969 &termp, term_dict + j, space_split, term_dst))
971 strcat (term_dict, ".*)");
972 dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
973 &max_pos, 0, grep_handle);
975 case 2: /* keft truncation */
976 term_dict[j++] = '('; term_dict[j++] = '.'; 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 3: /* left&right 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);
995 case 101: /* process # in term */
996 term_dict[j++] = '(';
997 if (!term_101 (zh->reg->zebra_maps, reg_type,
998 &termp, term_dict + j, space_split, term_dst))
1000 strcat (term_dict, ")");
1001 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1002 &max_pos, 0, grep_handle);
1004 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d", r);
1006 case 102: /* Regexp-1 */
1007 term_dict[j++] = '(';
1008 if (!term_102 (zh->reg->zebra_maps, reg_type,
1009 &termp, term_dict + j, space_split, term_dst))
1011 strcat (term_dict, ")");
1012 logf (LOG_DEBUG, "Regexp-1 tolerance=%d", r);
1013 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1014 &max_pos, 0, grep_handle);
1016 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
1019 case 103: /* Regexp-2 */
1021 term_dict[j++] = '(';
1022 if (!term_103 (zh->reg->zebra_maps, reg_type,
1023 &termp, term_dict + j, &r, space_split, term_dst))
1025 strcat (term_dict, ")");
1026 logf (LOG_DEBUG, "Regexp-2 tolerance=%d", r);
1027 r = dict_lookup_grep (zh->reg->dict, term_dict, r, grep_info,
1028 &max_pos, 2, grep_handle);
1030 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
1033 case 104: /* process # and ! in term */
1034 term_dict[j++] = '(';
1035 if (!term_104 (zh->reg->zebra_maps, reg_type,
1036 &termp, term_dict + j, space_split, term_dst))
1038 strcat (term_dict, ")");
1039 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1040 &max_pos, 0, grep_handle);
1042 logf (LOG_WARN, "dict_lookup_grep err, trunc=#/!: %d", r);
1044 case 105: /* process * and ! in term */
1045 term_dict[j++] = '(';
1046 if (!term_105 (zh->reg->zebra_maps, reg_type,
1047 &termp, term_dict + j, space_split, term_dst, 1))
1049 strcat (term_dict, ")");
1050 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1051 &max_pos, 0, grep_handle);
1053 logf (LOG_WARN, "dict_lookup_grep err, trunc=*/!: %d", r);
1055 case 106: /* process * and ! in term */
1056 term_dict[j++] = '(';
1057 if (!term_105 (zh->reg->zebra_maps, reg_type,
1058 &termp, term_dict + j, space_split, term_dst, 0))
1060 strcat (term_dict, ")");
1061 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1062 &max_pos, 0, grep_handle);
1064 logf (LOG_WARN, "dict_lookup_grep err, trunc=*/!: %d", r);
1069 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1073 static void trans_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1077 Z_Term *term = zapt->term;
1079 sizez = term->u.general->len;
1080 if (sizez > IT_MAX_WORD-1)
1081 sizez = IT_MAX_WORD-1;
1082 memcpy (termz, term->u.general->buf, sizez);
1083 termz[sizez] = '\0';
1086 static void trans_scan_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1087 char *termz, int reg_type)
1089 Z_Term *term = zapt->term;
1091 const char *cp = (const char *) term->u.general->buf;
1092 const char *cp_end = cp + term->u.general->len;
1095 const char *space_map = NULL;
1098 while ((len = (cp_end - cp)) > 0)
1100 map = zebra_maps_input (zh->reg->zebra_maps, reg_type, &cp, len);
1101 if (**map == *CHR_SPACE)
1106 for (src = space_map; *src; src++)
1109 for (src = *map; *src; src++)
1116 static RSET rpn_prox (ZebraHandle zh, RSET *rset, int rset_no,
1117 int ordered, int exclusion, int relation, int distance)
1122 struct it_key **buf;
1124 char prox_term[1024];
1125 int length_prox_term = 0;
1126 int min_nn = 10000000;
1128 const char *flags = NULL;
1130 rsfd = (RSFD *) xmalloc (sizeof(*rsfd)*rset_no);
1131 more = (int *) xmalloc (sizeof(*more)*rset_no);
1132 buf = (struct it_key **) xmalloc (sizeof(*buf)*rset_no);
1135 for (i = 0; i<rset_no; i++)
1138 for (j = 0; j<rset[i]->no_rset_terms; j++)
1140 const char *nflags = rset[i]->rset_terms[j]->flags;
1141 char *term = rset[i]->rset_terms[j]->name;
1142 int lterm = strlen(term);
1143 if (lterm + length_prox_term < sizeof(prox_term)-1)
1145 if (length_prox_term)
1146 prox_term[length_prox_term++] = ' ';
1147 strcpy (prox_term + length_prox_term, term);
1148 length_prox_term += lterm;
1150 if (min_nn > rset[i]->rset_terms[j]->nn)
1151 min_nn = rset[i]->rset_terms[j]->nn;
1155 for (i = 0; i<rset_no; i++)
1160 for (i = 0; i<rset_no; i++)
1162 buf[i] = (struct it_key *) xmalloc (sizeof(**buf));
1163 rsfd[i] = rset_open (rset[i], RSETF_READ);
1164 if (!(more[i] = rset_read (rset[i], rsfd[i], buf[i], &term_index)))
1169 /* at least one is empty ... return null set */
1170 rset_null_parms parms;
1172 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1174 parms.rset_term->nn = 0;
1175 result = rset_create (rset_kind_null, &parms);
1177 else if (ordered && relation == 3 && exclusion == 0 && distance == 1)
1179 /* special proximity case = phrase search ... */
1180 rset_temp_parms parms;
1183 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1185 parms.rset_term->nn = min_nn;
1186 parms.cmp = key_compare_it;
1187 parms.key_size = sizeof (struct it_key);
1188 parms.temp_path = res_get (zh->res, "setTmpDir");
1189 result = rset_create (rset_kind_temp, &parms);
1190 rsfd_result = rset_open (result, RSETF_WRITE);
1194 for (i = 1; i<rset_no; i++)
1203 cmp = key_compare_it (buf[i], buf[i-1]);
1206 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1207 buf[i-1], &term_index);
1212 if (buf[i-1]->seqno+1 != buf[i]->seqno)
1214 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1215 buf[i-1], &term_index);
1221 more[i] = rset_read (rset[i], rsfd[i], buf[i],
1228 rset_write (result, rsfd_result, buf[0]);
1229 more[0] = rset_read (*rset, *rsfd, *buf, &term_index);
1232 rset_close (result, rsfd_result);
1234 else if (rset_no == 2)
1236 /* generic proximity case (two input sets only) ... */
1237 rset_temp_parms parms;
1240 logf (LOG_LOG, "generic prox, dist = %d, relation = %d, ordered =%d, exclusion=%d",
1241 distance, relation, ordered, exclusion);
1242 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1244 parms.rset_term->nn = min_nn;
1245 parms.cmp = key_compare_it;
1246 parms.key_size = sizeof (struct it_key);
1247 parms.temp_path = res_get (zh->res, "setTmpDir");
1248 result = rset_create (rset_kind_temp, &parms);
1249 rsfd_result = rset_open (result, RSETF_WRITE);
1251 while (more[0] && more[1])
1253 int cmp = key_compare_it (buf[0], buf[1]);
1255 more[0] = rset_read (rset[0], rsfd[0], buf[0], &term_index);
1257 more[1] = rset_read (rset[1], rsfd[1], buf[1], &term_index);
1260 int sysno = buf[0]->sysno;
1264 seqno[n++] = buf[0]->seqno;
1265 while ((more[0] = rset_read (rset[0], rsfd[0], buf[0],
1267 sysno == buf[0]->sysno)
1269 seqno[n++] = buf[0]->seqno;
1272 for (i = 0; i<n; i++)
1274 int diff = buf[1]->seqno - seqno[i];
1275 int excl = exclusion;
1276 if (!ordered && diff < 0)
1281 if (diff < distance && diff >= 0)
1285 if (diff <= distance && diff >= 0)
1289 if (diff == distance && diff >= 0)
1293 if (diff >= distance && diff >= 0)
1297 if (diff > distance && diff >= 0)
1301 if (diff != distance && diff >= 0)
1307 rset_write (result, rsfd_result, buf[1]);
1311 } while ((more[1] = rset_read (rset[1], rsfd[1], buf[1],
1313 sysno == buf[1]->sysno);
1316 rset_close (result, rsfd_result);
1320 rset_null_parms parms;
1322 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1324 parms.rset_term->nn = 0;
1325 result = rset_create (rset_kind_null, &parms);
1327 for (i = 0; i<rset_no; i++)
1330 rset_close (rset[i], rsfd[i]);
1340 char *normalize_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1341 const char *termz, NMEM stream, unsigned reg_id)
1344 AttrType truncation;
1345 int truncation_value;
1348 attr_init (&truncation, zapt, 5);
1349 truncation_value = attr_find (&truncation, NULL);
1351 switch (truncation_value)
1371 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, ex_list,
1372 termz, strlen(termz));
1374 return nmem_strdup(stream, termz);
1377 char *buf = (char*) nmem_malloc (stream, wrbuf_len(wrbuf)+1);
1378 memcpy (buf, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1379 buf[wrbuf_len(wrbuf)] = '\0';
1384 static void grep_info_delete (struct grep_info *grep_info)
1387 xfree(grep_info->term_no);
1389 xfree (grep_info->isam_p_buf);
1392 static int grep_info_prepare (ZebraHandle zh,
1393 Z_AttributesPlusTerm *zapt,
1394 struct grep_info *grep_info,
1399 int termset_value_numeric;
1400 const char *termset_value_string;
1403 grep_info->term_no = 0;
1405 grep_info->isam_p_size = 0;
1406 grep_info->isam_p_buf = NULL;
1408 grep_info->reg_type = reg_type;
1409 grep_info->termset = 0;
1411 attr_init (&termset, zapt, 8);
1412 termset_value_numeric =
1413 attr_find_ex (&termset, NULL, &termset_value_string);
1414 if (termset_value_numeric != -1)
1417 const char *termset_name = 0;
1418 if (termset_value_numeric != -2)
1421 sprintf (resname, "%d", termset_value_numeric);
1422 termset_name = resname;
1425 termset_name = termset_value_string;
1426 logf (LOG_LOG, "creating termset set %s", termset_name);
1427 grep_info->termset = resultSetAdd (zh, termset_name, 1);
1428 if (!grep_info->termset)
1431 zh->errString = nmem_strdup (stream, termset_name);
1439 static RSET rpn_search_APT_phrase (ZebraHandle zh,
1440 Z_AttributesPlusTerm *zapt,
1441 const char *termz_org,
1442 oid_value attributeSet,
1444 int reg_type, int complete_flag,
1445 const char *rank_type,
1446 int num_bases, char **basenames)
1448 char term_dst[IT_MAX_WORD+1];
1449 RSET rset[60], result;
1451 struct grep_info grep_info;
1452 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1453 const char *termp = termz;
1456 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1460 logf (LOG_DEBUG, "APT_phrase termp=%s", termp);
1461 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1463 reg_type, complete_flag,
1464 num_bases, basenames,
1465 term_dst, rank_type);
1468 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1471 grep_info_delete (&grep_info);
1474 rset_null_parms parms;
1476 parms.rset_term = rset_term_create (termz, -1, rank_type);
1477 return rset_create (rset_kind_null, &parms);
1479 else if (rset_no == 1)
1481 result = rpn_prox (zh, rset, rset_no, 1, 0, 3, 1);
1482 for (i = 0; i<rset_no; i++)
1483 rset_delete (rset[i]);
1487 static RSET rpn_search_APT_or_list (ZebraHandle zh,
1488 Z_AttributesPlusTerm *zapt,
1489 const char *termz_org,
1490 oid_value attributeSet,
1492 int reg_type, int complete_flag,
1493 const char *rank_type,
1494 int num_bases, char **basenames)
1496 char term_dst[IT_MAX_WORD+1];
1497 RSET rset[60], result;
1499 struct grep_info grep_info;
1500 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1501 const char *termp = termz;
1503 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1507 logf (LOG_DEBUG, "APT_or_list termp=%s", termp);
1508 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1510 reg_type, complete_flag,
1511 num_bases, basenames,
1512 term_dst, rank_type);
1515 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1518 grep_info_delete (&grep_info);
1521 rset_null_parms parms;
1523 parms.rset_term = rset_term_create (termz, -1, rank_type);
1524 return rset_create (rset_kind_null, &parms);
1527 for (i = 1; i<rset_no; i++)
1529 rset_bool_parms bool_parms;
1531 bool_parms.rset_l = result;
1532 bool_parms.rset_r = rset[i];
1533 bool_parms.key_size = sizeof(struct it_key);
1534 bool_parms.cmp = key_compare_it;
1535 result = rset_create (rset_kind_or, &bool_parms);
1540 static RSET rpn_search_APT_and_list (ZebraHandle zh,
1541 Z_AttributesPlusTerm *zapt,
1542 const char *termz_org,
1543 oid_value attributeSet,
1545 int reg_type, int complete_flag,
1546 const char *rank_type,
1547 int num_bases, char **basenames)
1549 char term_dst[IT_MAX_WORD+1];
1550 RSET rset[60], result;
1552 struct grep_info grep_info;
1553 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1554 const char *termp = termz;
1556 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1560 logf (LOG_DEBUG, "APT_and_list termp=%s", termp);
1561 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1563 reg_type, complete_flag,
1564 num_bases, basenames,
1565 term_dst, rank_type);
1568 assert (rset[rset_no]);
1569 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1572 grep_info_delete (&grep_info);
1575 rset_null_parms parms;
1577 parms.rset_term = rset_term_create (termz, -1, rank_type);
1578 return rset_create (rset_kind_null, &parms);
1581 for (i = 1; i<rset_no; i++)
1583 rset_bool_parms bool_parms;
1585 bool_parms.rset_l = result;
1586 bool_parms.rset_r = rset[i];
1587 bool_parms.key_size = sizeof(struct it_key);
1588 bool_parms.cmp = key_compare_it;
1589 result = rset_create (rset_kind_and, &bool_parms);
1594 static int numeric_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1595 const char **term_sub,
1597 oid_value attributeSet,
1598 struct grep_info *grep_info,
1607 char *term_tmp = term_dict + strlen(term_dict);
1609 attr_init (&relation, zapt, 2);
1610 relation_value = attr_find (&relation, NULL);
1612 logf (LOG_DEBUG, "numeric relation value=%d", relation_value);
1614 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub, term_tmp, 1,
1617 term_value = atoi (term_tmp);
1618 switch (relation_value)
1621 logf (LOG_DEBUG, "Relation <");
1622 gen_regular_rel (term_tmp, term_value-1, 1);
1625 logf (LOG_DEBUG, "Relation <=");
1626 gen_regular_rel (term_tmp, term_value, 1);
1629 logf (LOG_DEBUG, "Relation >=");
1630 gen_regular_rel (term_tmp, term_value, 0);
1633 logf (LOG_DEBUG, "Relation >");
1634 gen_regular_rel (term_tmp, term_value+1, 0);
1638 logf (LOG_DEBUG, "Relation =");
1639 sprintf (term_tmp, "(0*%d)", term_value);
1641 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_tmp);
1642 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info, max_pos,
1645 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1646 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1650 static int numeric_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1651 const char **term_sub,
1652 oid_value attributeSet, struct grep_info *grep_info,
1653 int reg_type, int complete_flag,
1654 int num_bases, char **basenames,
1657 char term_dict[2*IT_MAX_WORD+2];
1661 oid_value curAttributeSet = attributeSet;
1663 struct rpn_char_map_info rcmi;
1665 rpn_char_map_prepare (zh->reg, reg_type, &rcmi);
1666 attr_init (&use, zapt, 1);
1667 use_value = attr_find (&use, &curAttributeSet);
1668 logf (LOG_DEBUG, "numeric_term, use value %d", use_value);
1670 if (use_value == -1)
1673 for (base_no = 0; base_no < num_bases; base_no++)
1676 data1_local_attribute *local_attr;
1677 int max_pos, prefix_len = 0;
1680 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
1682 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1683 curAttributeSet, use_value, r);
1690 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
1692 zh->errCode = 109; /* Database unavailable */
1693 zh->errString = basenames[base_no];
1696 for (local_attr = attp.local_attributes; local_attr;
1697 local_attr = local_attr->next)
1703 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
1708 term_dict[prefix_len++] = '|';
1710 term_dict[prefix_len++] = '(';
1712 ord_len = key_SU_encode (ord, ord_buf);
1713 for (i = 0; i<ord_len; i++)
1715 term_dict[prefix_len++] = 1;
1716 term_dict[prefix_len++] = ord_buf[i];
1724 term_dict[prefix_len++] = ')';
1725 term_dict[prefix_len++] = 1;
1726 term_dict[prefix_len++] = reg_type;
1727 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1728 term_dict[prefix_len] = '\0';
1729 if (!numeric_relation (zh, zapt, &termp, term_dict,
1730 attributeSet, grep_info, &max_pos, reg_type,
1735 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1739 static RSET rpn_search_APT_numeric (ZebraHandle zh,
1740 Z_AttributesPlusTerm *zapt,
1742 oid_value attributeSet,
1744 int reg_type, int complete_flag,
1745 const char *rank_type,
1746 int num_bases, char **basenames)
1748 char term_dst[IT_MAX_WORD+1];
1749 const char *termp = termz;
1750 RSET rset[60], result;
1751 int i, r, rset_no = 0;
1752 struct grep_info grep_info;
1754 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1758 logf (LOG_DEBUG, "APT_numeric termp=%s", termp);
1759 grep_info.isam_p_indx = 0;
1760 r = numeric_term (zh, zapt, &termp, attributeSet, &grep_info,
1761 reg_type, complete_flag, num_bases, basenames,
1765 logf (LOG_DEBUG, "term: %s", term_dst);
1766 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1767 grep_info.isam_p_indx, term_dst,
1768 strlen(term_dst), rank_type,
1769 0 /* preserve position */);
1770 assert (rset[rset_no]);
1771 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1774 grep_info_delete (&grep_info);
1777 rset_null_parms parms;
1779 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1780 return rset_create (rset_kind_null, &parms);
1783 for (i = 1; i<rset_no; i++)
1785 rset_bool_parms bool_parms;
1787 bool_parms.rset_l = result;
1788 bool_parms.rset_r = rset[i];
1789 bool_parms.key_size = sizeof(struct it_key);
1790 bool_parms.cmp = key_compare_it;
1791 result = rset_create (rset_kind_and, &bool_parms);
1796 static RSET rpn_search_APT_local (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1798 oid_value attributeSet,
1800 const char *rank_type)
1805 rset_temp_parms parms;
1807 parms.rset_term = rset_term_create (termz, -1, rank_type);
1808 parms.cmp = key_compare_it;
1809 parms.key_size = sizeof (struct it_key);
1810 parms.temp_path = res_get (zh->res, "setTmpDir");
1811 result = rset_create (rset_kind_temp, &parms);
1812 rsfd = rset_open (result, RSETF_WRITE);
1814 key.sysno = atoi (termz);
1818 rset_write (result, rsfd, &key);
1819 rset_close (result, rsfd);
1823 static RSET rpn_sort_spec (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1824 oid_value attributeSet, NMEM stream,
1825 Z_SortKeySpecList *sort_sequence,
1826 const char *rank_type)
1828 rset_null_parms parms;
1830 int sort_relation_value;
1831 AttrType sort_relation_type;
1836 Z_AttributeElement *ae;
1841 attr_init (&sort_relation_type, zapt, 7);
1842 sort_relation_value = attr_find (&sort_relation_type, &attributeSet);
1844 attr_init (&use_type, zapt, 1);
1845 use_value = attr_find (&use_type, &attributeSet);
1847 if (!sort_sequence->specs)
1849 sort_sequence->num_specs = 10;
1850 sort_sequence->specs = (Z_SortKeySpec **)
1851 nmem_malloc (stream, sort_sequence->num_specs *
1852 sizeof(*sort_sequence->specs));
1853 for (i = 0; i<sort_sequence->num_specs; i++)
1854 sort_sequence->specs[i] = 0;
1856 if (zapt->term->which != Z_Term_general)
1859 i = atoi_n ((char *) zapt->term->u.general->buf,
1860 zapt->term->u.general->len);
1861 if (i >= sort_sequence->num_specs)
1863 sprintf (termz, "%d", i);
1865 oe.proto = PROTO_Z3950;
1866 oe.oclass = CLASS_ATTSET;
1867 oe.value = attributeSet;
1868 if (!oid_ent_to_oid (&oe, oid))
1871 sks = (Z_SortKeySpec *) nmem_malloc (stream, sizeof(*sks));
1872 sks->sortElement = (Z_SortElement *)
1873 nmem_malloc (stream, sizeof(*sks->sortElement));
1874 sks->sortElement->which = Z_SortElement_generic;
1875 sk = sks->sortElement->u.generic = (Z_SortKey *)
1876 nmem_malloc (stream, sizeof(*sk));
1877 sk->which = Z_SortKey_sortAttributes;
1878 sk->u.sortAttributes = (Z_SortAttributes *)
1879 nmem_malloc (stream, sizeof(*sk->u.sortAttributes));
1881 sk->u.sortAttributes->id = oid;
1882 sk->u.sortAttributes->list = (Z_AttributeList *)
1883 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list));
1884 sk->u.sortAttributes->list->num_attributes = 1;
1885 sk->u.sortAttributes->list->attributes = (Z_AttributeElement **)
1886 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list->attributes));
1887 ae = *sk->u.sortAttributes->list->attributes = (Z_AttributeElement *)
1888 nmem_malloc (stream, sizeof(**sk->u.sortAttributes->list->attributes));
1889 ae->attributeSet = 0;
1890 ae->attributeType = (int *)
1891 nmem_malloc (stream, sizeof(*ae->attributeType));
1892 *ae->attributeType = 1;
1893 ae->which = Z_AttributeValue_numeric;
1894 ae->value.numeric = (int *)
1895 nmem_malloc (stream, sizeof(*ae->value.numeric));
1896 *ae->value.numeric = use_value;
1898 sks->sortRelation = (int *)
1899 nmem_malloc (stream, sizeof(*sks->sortRelation));
1900 if (sort_relation_value == 1)
1901 *sks->sortRelation = Z_SortRelation_ascending;
1902 else if (sort_relation_value == 2)
1903 *sks->sortRelation = Z_SortRelation_descending;
1905 *sks->sortRelation = Z_SortRelation_ascending;
1907 sks->caseSensitivity = (int *)
1908 nmem_malloc (stream, sizeof(*sks->caseSensitivity));
1909 *sks->caseSensitivity = 0;
1912 sks->which = Z_SortKeySpec_null;
1913 sks->u.null = odr_nullval ();
1915 sks->missingValueAction = 0;
1918 sort_sequence->specs[i] = sks;
1920 parms.rset_term = rset_term_create (termz, -1, rank_type);
1921 return rset_create (rset_kind_null, &parms);
1924 static RSET rpn_search_xpath (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1925 oid_value attributeSet,
1926 int num_bases, char **basenames,
1927 NMEM stream, const char *rank_type, RSET rset)
1930 const char *use_string = 0;
1931 oid_value curAttributeSet = attributeSet;
1932 char term_dict[2048];
1935 struct grep_info grep_info;
1937 yaz_log (LOG_LOG, "rpn_search_xpath 1");
1938 attr_init (&use, zapt, 1);
1939 attr_find_ex (&use, &curAttributeSet, &use_string);
1941 if (curAttributeSet != VAL_IDXPATH)
1943 yaz_log (LOG_LOG, "rpn_search_xpath - not 1");
1948 yaz_log (LOG_LOG, "rpn_search_xpath - not 2");
1952 dict_grep_cmap (zh->reg->dict, 0, 0);
1953 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1956 yaz_log (LOG_LOG, "rpn_search_xpath 2");
1957 for (base_no = 0; base_no < num_bases; base_no++)
1959 const char *termp = use_string;
1960 rset_between_parms parms;
1961 RSET rset_start_tag, rset_end_tag;
1962 int ord, ord_len, i, r, max_pos;
1965 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
1967 zh->errCode = 109; /* Database unavailable */
1968 zh->errString = basenames[base_no];
1973 ord = zebraExplain_lookupSU (zh->reg->zei, curAttributeSet, 1);
1977 term_dict[prefix_len++] = '|';
1979 term_dict[prefix_len++] = '(';
1981 ord_len = key_SU_encode (ord, ord_buf);
1982 for (i = 0; i<ord_len; i++)
1984 term_dict[prefix_len++] = 1;
1985 term_dict[prefix_len++] = ord_buf[i];
1987 term_dict[prefix_len++] = ')';
1988 term_dict[prefix_len++] = 1;
1989 term_dict[prefix_len++] = reg_type;
1992 strcpy (term_dict+prefix_len, use_string);
1994 grep_info.isam_p_indx = 0;
1995 yaz_log (LOG_LOG, "rpn_search_xpath 3 %s", term_dict+prefix_len);
1996 r = dict_lookup_grep (zh->reg->dict, term_dict, 0,
1997 &grep_info, &max_pos, 0, grep_handle);
1998 yaz_log (LOG_LOG, "%s %d positions", use_string,
1999 grep_info.isam_p_indx);
2001 rset_trunc (zh, grep_info.isam_p_buf,
2002 grep_info.isam_p_indx, use_string, strlen(use_string),
2006 ord = zebraExplain_lookupSU (zh->reg->zei, curAttributeSet, 2);
2010 term_dict[prefix_len++] = '|';
2012 term_dict[prefix_len++] = '(';
2014 ord_len = key_SU_encode (ord, ord_buf);
2015 for (i = 0; i<ord_len; i++)
2017 term_dict[prefix_len++] = 1;
2018 term_dict[prefix_len++] = ord_buf[i];
2020 term_dict[prefix_len++] = ')';
2021 term_dict[prefix_len++] = 1;
2022 term_dict[prefix_len++] = reg_type;
2026 strcpy (term_dict+prefix_len, use_string);
2028 grep_info.isam_p_indx = 0;
2029 r = dict_lookup_grep (zh->reg->dict, term_dict, 0,
2030 &grep_info, &max_pos, 0, grep_handle);
2032 yaz_log (LOG_LOG, "%s %d positions", use_string,
2033 grep_info.isam_p_indx);
2035 rset_trunc (zh, grep_info.isam_p_buf,
2036 grep_info.isam_p_indx, use_string, strlen(use_string),
2039 parms.key_size = sizeof(struct it_key);
2040 parms.cmp = key_compare_it;
2041 parms.rset_l = rset_start_tag;
2042 parms.rset_m = rset;
2043 parms.rset_r = rset_end_tag;
2044 parms.printer = key_print_it;
2045 yaz_log (LOG_LOG, "rpn_search_xpath 4");
2046 rset = rset_create (rset_kind_between, &parms);
2048 grep_info_delete (&grep_info);
2055 static RSET rpn_search_APT (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2056 oid_value attributeSet, NMEM stream,
2057 Z_SortKeySpecList *sort_sequence,
2058 int num_bases, char **basenames)
2061 char *search_type = NULL;
2062 char rank_type[128];
2065 char termz[IT_MAX_WORD+1];
2068 zebra_maps_attr (zh->reg->zebra_maps, zapt, ®_id, &search_type,
2069 rank_type, &complete_flag, &sort_flag);
2071 logf (LOG_DEBUG, "reg_id=%c", reg_id);
2072 logf (LOG_DEBUG, "complete_flag=%d", complete_flag);
2073 logf (LOG_DEBUG, "search_type=%s", search_type);
2074 logf (LOG_DEBUG, "rank_type=%s", rank_type);
2076 if (zapt->term->which != Z_Term_general)
2081 trans_term (zh, zapt, termz);
2084 return rpn_sort_spec (zh, zapt, attributeSet, stream, sort_sequence,
2087 if (!strcmp (search_type, "phrase"))
2089 rset = rpn_search_APT_phrase (zh, zapt, termz, attributeSet, stream,
2090 reg_id, complete_flag, rank_type,
2091 num_bases, basenames);
2093 else if (!strcmp (search_type, "and-list"))
2095 rset = rpn_search_APT_and_list (zh, zapt, termz, attributeSet, stream,
2096 reg_id, complete_flag, rank_type,
2097 num_bases, basenames);
2099 else if (!strcmp (search_type, "or-list"))
2101 rset = rpn_search_APT_or_list (zh, zapt, termz, attributeSet, stream,
2102 reg_id, complete_flag, rank_type,
2103 num_bases, basenames);
2105 else if (!strcmp (search_type, "local"))
2107 rset = rpn_search_APT_local (zh, zapt, termz, attributeSet, stream,
2110 else if (!strcmp (search_type, "numeric"))
2112 rset = rpn_search_APT_numeric (zh, zapt, termz, attributeSet, stream,
2113 reg_id, complete_flag, rank_type,
2114 num_bases, basenames);
2118 return rpn_search_xpath (zh, zapt, attributeSet, num_bases, basenames,
2119 stream, rank_type, rset);
2122 static RSET rpn_search_structure (ZebraHandle zh, Z_RPNStructure *zs,
2123 oid_value attributeSet, NMEM stream,
2124 Z_SortKeySpecList *sort_sequence,
2125 int num_bases, char **basenames)
2128 if (zs->which == Z_RPNStructure_complex)
2130 Z_Operator *zop = zs->u.complex->roperator;
2131 rset_bool_parms bool_parms;
2133 bool_parms.rset_l = rpn_search_structure (zh, zs->u.complex->s1,
2134 attributeSet, stream,
2136 num_bases, basenames);
2137 if (bool_parms.rset_l == NULL)
2139 bool_parms.rset_r = rpn_search_structure (zh, zs->u.complex->s2,
2140 attributeSet, stream,
2142 num_bases, basenames);
2143 if (bool_parms.rset_r == NULL)
2145 rset_delete (bool_parms.rset_l);
2148 bool_parms.key_size = sizeof(struct it_key);
2149 bool_parms.cmp = key_compare_it;
2153 case Z_Operator_and:
2154 r = rset_create (rset_kind_and, &bool_parms);
2157 r = rset_create (rset_kind_or, &bool_parms);
2159 case Z_Operator_and_not:
2160 r = rset_create (rset_kind_not, &bool_parms);
2162 case Z_Operator_prox:
2164 if (zop->u.prox->which != Z_ProximityOperator_known)
2170 if (zop->u.prox->which != Z_ProxCode_known)
2178 if (*zop->u.prox->u.known != Z_ProxUnit_word)
2180 char *val = (char *) nmem_malloc (stream, 16);
2182 zh->errString = val;
2183 sprintf (val, "%d", *zop->u.prox->u.known);
2187 if (*zop->u.prox->proximityUnitCode != Z_ProxUnit_word)
2189 char *val = (char *) nmem_malloc (stream, 16);
2191 zh->errString = val;
2192 sprintf (val, "%d", *zop->u.prox->proximityUnitCode);
2200 rsets[0] = bool_parms.rset_l;
2201 rsets[1] = bool_parms.rset_r;
2203 r = rpn_prox (zh, rsets, 2,
2204 *zop->u.prox->ordered,
2205 (!zop->u.prox->exclusion ? 0 :
2206 *zop->u.prox->exclusion),
2207 *zop->u.prox->relationType,
2208 *zop->u.prox->distance);
2209 rset_delete (rsets[0]);
2210 rset_delete (rsets[1]);
2218 else if (zs->which == Z_RPNStructure_simple)
2220 if (zs->u.simple->which == Z_Operand_APT)
2222 logf (LOG_DEBUG, "rpn_search_APT");
2223 r = rpn_search_APT (zh, zs->u.simple->u.attributesPlusTerm,
2224 attributeSet, stream, sort_sequence,
2225 num_bases, basenames);
2227 else if (zs->u.simple->which == Z_Operand_resultSetId)
2229 logf (LOG_DEBUG, "rpn_search_ref");
2230 r = resultSetRef (zh, zs->u.simple->u.resultSetId);
2233 r = rset_create (rset_kind_null, NULL);
2236 nmem_strdup (stream, zs->u.simple->u.resultSetId);
2255 RSET rpn_search (ZebraHandle zh, NMEM nmem,
2256 Z_RPNQuery *rpn, int num_bases, char **basenames,
2257 const char *setname,
2262 oid_value attributeSet;
2263 Z_SortKeySpecList *sort_sequence;
2267 zh->errString = NULL;
2270 sort_sequence = (Z_SortKeySpecList *)
2271 nmem_malloc (nmem, sizeof(*sort_sequence));
2272 sort_sequence->num_specs = 10;
2273 sort_sequence->specs = (Z_SortKeySpec **)
2274 nmem_malloc (nmem, sort_sequence->num_specs *
2275 sizeof(*sort_sequence->specs));
2276 for (i = 0; i<sort_sequence->num_specs; i++)
2277 sort_sequence->specs[i] = 0;
2279 attrset = oid_getentbyoid (rpn->attributeSetId);
2280 attributeSet = attrset->value;
2281 rset = rpn_search_structure (zh, rpn->RPNStructure, attributeSet,
2282 nmem, sort_sequence, num_bases, basenames);
2287 logf (LOG_DEBUG, "search error: %d", zh->errCode);
2289 for (i = 0; sort_sequence->specs[i]; i++)
2291 sort_sequence->num_specs = i;
2293 resultSetRank (zh, sset, rset);
2296 logf (LOG_DEBUG, "resultSetSortSingle in rpn_search");
2297 resultSetSortSingle (zh, nmem, sset, rset,
2298 sort_sequence, &sort_status);
2301 logf (LOG_DEBUG, "resultSetSortSingle status = %d", zh->errCode);
2307 struct scan_info_entry {
2313 struct scan_info_entry *list;
2319 static int scan_handle (char *name, const char *info, int pos, void *client)
2321 int len_prefix, idx;
2322 struct scan_info *scan_info = (struct scan_info *) client;
2324 len_prefix = strlen(scan_info->prefix);
2325 if (memcmp (name, scan_info->prefix, len_prefix))
2327 if (pos > 0) idx = scan_info->after - pos + scan_info->before;
2330 scan_info->list[idx].term = (char *)
2331 odr_malloc (scan_info->odr, strlen(name + len_prefix)+1);
2332 strcpy (scan_info->list[idx].term, name + len_prefix);
2333 assert (*info == sizeof(ISAMS_P));
2334 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAMS_P));
2338 static void scan_term_untrans (ZebraHandle zh, NMEM stream, int reg_type,
2339 char **dst, const char *src)
2341 char term_dst[1024];
2343 term_untrans (zh, reg_type, term_dst, src);
2345 *dst = (char *) nmem_malloc (stream, strlen(term_dst)+1);
2346 strcpy (*dst, term_dst);
2349 static void count_set (RSET r, int *count)
2357 logf (LOG_DEBUG, "count_set");
2360 rfd = rset_open (r, RSETF_READ);
2361 while (rset_read (r, rfd, &key, &term_index))
2363 if (key.sysno != psysno)
2370 rset_close (r, rfd);
2371 logf (LOG_DEBUG, "%d keys, %d records", kno, *count);
2374 void rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
2375 oid_value attributeset,
2376 int num_bases, char **basenames,
2377 int *position, int *num_entries, ZebraScanEntry **list,
2381 int pos = *position;
2382 int num = *num_entries;
2386 char termz[IT_MAX_WORD+20];
2389 struct scan_info *scan_info_array;
2390 ZebraScanEntry *glist;
2391 int ords[32], ord_no = 0;
2395 char *search_type = NULL;
2396 char rank_type[128];
2401 if (attributeset == VAL_NONE)
2402 attributeset = VAL_BIB1;
2404 yaz_log (LOG_DEBUG, "position = %d, num = %d set=%d",
2405 pos, num, attributeset);
2407 attr_init (&use, zapt, 1);
2408 use_value = attr_find (&use, &attributeset);
2410 if (zebra_maps_attr (zh->reg->zebra_maps, zapt, ®_id, &search_type,
2411 rank_type, &complete_flag, &sort_flag))
2417 yaz_log (LOG_DEBUG, "use_value = %d", use_value);
2419 if (use_value == -1)
2421 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
2425 data1_local_attribute *local_attr;
2427 if ((r=att_getentbyatt (zh, &attp, attributeset, use_value)))
2429 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
2430 attributeset, use_value);
2438 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
2440 zh->errString = basenames[base_no];
2441 zh->errCode = 109; /* Database unavailable */
2445 for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
2446 local_attr = local_attr->next)
2450 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
2453 ords[ord_no++] = ord;
2462 /* prepare dictionary scanning */
2465 scan_info_array = (struct scan_info *)
2466 odr_malloc (stream, ord_no * sizeof(*scan_info_array));
2467 for (i = 0; i < ord_no; i++)
2469 int j, prefix_len = 0;
2470 int before_tmp = before, after_tmp = after;
2471 struct scan_info *scan_info = scan_info_array + i;
2472 struct rpn_char_map_info rcmi;
2474 rpn_char_map_prepare (zh->reg, reg_id, &rcmi);
2476 scan_info->before = before;
2477 scan_info->after = after;
2478 scan_info->odr = stream;
2480 scan_info->list = (struct scan_info_entry *)
2481 odr_malloc (stream, (before+after) * sizeof(*scan_info->list));
2482 for (j = 0; j<before+after; j++)
2483 scan_info->list[j].term = NULL;
2485 prefix_len += key_SU_encode (ords[i], termz + prefix_len);
2486 termz[prefix_len++] = reg_id;
2487 termz[prefix_len] = 0;
2488 strcpy (scan_info->prefix, termz);
2490 trans_scan_term (zh, zapt, termz+prefix_len, reg_id);
2492 dict_scan (zh->reg->dict, termz, &before_tmp, &after_tmp,
2493 scan_info, scan_handle);
2495 glist = (ZebraScanEntry *)
2496 odr_malloc (stream, (before+after)*sizeof(*glist));
2498 /* consider terms after main term */
2499 for (i = 0; i < ord_no; i++)
2503 for (i = 0; i<after; i++)
2506 const char *mterm = NULL;
2510 for (j = 0; j < ord_no; j++)
2512 if (ptr[j] < before+after &&
2513 (tst=scan_info_array[j].list[ptr[j]].term) &&
2514 (!mterm || strcmp (tst, mterm) < 0))
2522 scan_term_untrans (zh, stream->mem, reg_id,
2523 &glist[i+before].term, mterm);
2524 rset = rset_trunc (zh, &scan_info_array[j0].list[ptr[j0]].isam_p, 1,
2525 glist[i+before].term, strlen(glist[i+before].term),
2529 for (j = j0+1; j<ord_no; j++)
2531 if (ptr[j] < before+after &&
2532 (tst=scan_info_array[j].list[ptr[j]].term) &&
2533 !strcmp (tst, mterm))
2535 rset_bool_parms bool_parms;
2539 rset_trunc (zh, &scan_info_array[j].list[ptr[j]].isam_p, 1,
2540 glist[i+before].term,
2541 strlen(glist[i+before].term), NULL, 0);
2543 bool_parms.key_size = sizeof(struct it_key);
2544 bool_parms.cmp = key_compare_it;
2545 bool_parms.rset_l = rset;
2546 bool_parms.rset_r = rset2;
2548 rset = rset_create (rset_kind_or, &bool_parms);
2553 count_set (rset, &glist[i+before].occurrences);
2558 *num_entries -= (after-i);
2562 /* consider terms before main term */
2563 for (i = 0; i<ord_no; i++)
2566 for (i = 0; i<before; i++)
2569 const char *mterm = NULL;
2573 for (j = 0; j <ord_no; j++)
2575 if (ptr[j] < before &&
2576 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2577 (!mterm || strcmp (tst, mterm) > 0))
2586 scan_term_untrans (zh, stream->mem, reg_id,
2587 &glist[before-1-i].term, mterm);
2590 (zh, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1,
2591 glist[before-1-i].term, strlen(glist[before-1-i].term),
2596 for (j = j0+1; j<ord_no; j++)
2598 if (ptr[j] < before &&
2599 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2600 !strcmp (tst, mterm))
2602 rset_bool_parms bool_parms;
2605 rset2 = rset_trunc (zh,
2606 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1,
2607 glist[before-1-i].term,
2608 strlen(glist[before-1-i].term), NULL, 0);
2610 bool_parms.key_size = sizeof(struct it_key);
2611 bool_parms.cmp = key_compare_it;
2612 bool_parms.rset_l = rset;
2613 bool_parms.rset_r = rset2;
2615 rset = rset_create (rset_kind_or, &bool_parms);
2620 count_set (rset, &glist[before-1-i].occurrences);
2630 *list = glist + i; /* list is set to first 'real' entry */
2632 logf (LOG_DEBUG, "position = %d, num_entries = %d",
2633 *position, *num_entries);
2635 logf (LOG_DEBUG, "scan error: %d", zh->errCode);