2 * Copyright (C) 1995-2002, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
6 * $Id: zrpn.c,v 1.116 2002-07-03 10:05:19 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);
894 /* set was found, but value wasn't defined */
896 sprintf (val_str, "%d", use_value);
898 zh->errString = nmem_strdup (stream, val_str);
903 struct oident oident;
905 oident.proto = PROTO_Z3950;
906 oident.oclass = CLASS_ATTSET;
907 oident.value = curAttributeSet;
908 oid_ent_to_oid (&oident, oid);
911 zh->errString = nmem_strdup (stream, oident.desc);
916 for (local_attr = attp.local_attributes; local_attr;
917 local_attr = local_attr->next)
923 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
928 term_dict[prefix_len++] = '|';
930 term_dict[prefix_len++] = '(';
932 ord_len = key_SU_encode (ord, ord_buf);
933 for (i = 0; i<ord_len; i++)
935 term_dict[prefix_len++] = 1;
936 term_dict[prefix_len++] = ord_buf[i];
942 sprintf (val_str, "%d", use_value);
944 zh->errString = nmem_strdup (stream, val_str);
947 term_dict[prefix_len++] = ')';
948 term_dict[prefix_len++] = 1;
949 term_dict[prefix_len++] = reg_type;
950 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
951 term_dict[prefix_len] = '\0';
953 switch (truncation_value)
955 case -1: /* not specified */
956 case 100: /* do not truncate */
957 if (!string_relation (zh, zapt, &termp, term_dict,
959 reg_type, space_split, term_dst))
961 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_dict+prefix_len);
962 r = dict_lookup_grep (zh->reg->dict, term_dict, 0,
963 grep_info, &max_pos, 0, grep_handle);
965 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
967 case 1: /* right truncation */
968 term_dict[j++] = '(';
969 if (!term_100 (zh->reg->zebra_maps, reg_type,
970 &termp, term_dict + j, space_split, term_dst))
972 strcat (term_dict, ".*)");
973 dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
974 &max_pos, 0, grep_handle);
976 case 2: /* keft truncation */
977 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
978 if (!term_100 (zh->reg->zebra_maps, reg_type,
979 &termp, term_dict + j, space_split, term_dst))
981 strcat (term_dict, ")");
982 dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
983 &max_pos, 0, grep_handle);
985 case 3: /* left&right truncation */
986 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
987 if (!term_100 (zh->reg->zebra_maps, reg_type,
988 &termp, term_dict + j, space_split, term_dst))
990 strcat (term_dict, ".*)");
991 dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
992 &max_pos, 0, grep_handle);
996 case 101: /* process # in term */
997 term_dict[j++] = '(';
998 if (!term_101 (zh->reg->zebra_maps, reg_type,
999 &termp, term_dict + j, space_split, term_dst))
1001 strcat (term_dict, ")");
1002 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1003 &max_pos, 0, grep_handle);
1005 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d", r);
1007 case 102: /* Regexp-1 */
1008 term_dict[j++] = '(';
1009 if (!term_102 (zh->reg->zebra_maps, reg_type,
1010 &termp, term_dict + j, space_split, term_dst))
1012 strcat (term_dict, ")");
1013 logf (LOG_DEBUG, "Regexp-1 tolerance=%d", r);
1014 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1015 &max_pos, 0, grep_handle);
1017 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
1020 case 103: /* Regexp-2 */
1022 term_dict[j++] = '(';
1023 if (!term_103 (zh->reg->zebra_maps, reg_type,
1024 &termp, term_dict + j, &r, space_split, term_dst))
1026 strcat (term_dict, ")");
1027 logf (LOG_DEBUG, "Regexp-2 tolerance=%d", r);
1028 r = dict_lookup_grep (zh->reg->dict, term_dict, r, grep_info,
1029 &max_pos, 2, grep_handle);
1031 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
1034 case 104: /* process # and ! in term */
1035 term_dict[j++] = '(';
1036 if (!term_104 (zh->reg->zebra_maps, reg_type,
1037 &termp, term_dict + j, space_split, term_dst))
1039 strcat (term_dict, ")");
1040 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1041 &max_pos, 0, grep_handle);
1043 logf (LOG_WARN, "dict_lookup_grep err, trunc=#/!: %d", r);
1045 case 105: /* process * and ! in term */
1046 term_dict[j++] = '(';
1047 if (!term_105 (zh->reg->zebra_maps, reg_type,
1048 &termp, term_dict + j, space_split, term_dst, 1))
1050 strcat (term_dict, ")");
1051 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1052 &max_pos, 0, grep_handle);
1054 logf (LOG_WARN, "dict_lookup_grep err, trunc=*/!: %d", r);
1056 case 106: /* process * and ! in term */
1057 term_dict[j++] = '(';
1058 if (!term_105 (zh->reg->zebra_maps, reg_type,
1059 &termp, term_dict + j, space_split, term_dst, 0))
1061 strcat (term_dict, ")");
1062 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info,
1063 &max_pos, 0, grep_handle);
1065 logf (LOG_WARN, "dict_lookup_grep err, trunc=*/!: %d", r);
1070 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1074 static void trans_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1078 Z_Term *term = zapt->term;
1080 sizez = term->u.general->len;
1081 if (sizez > IT_MAX_WORD-1)
1082 sizez = IT_MAX_WORD-1;
1083 memcpy (termz, term->u.general->buf, sizez);
1084 termz[sizez] = '\0';
1087 static void trans_scan_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1088 char *termz, int reg_type)
1090 Z_Term *term = zapt->term;
1092 const char *cp = (const char *) term->u.general->buf;
1093 const char *cp_end = cp + term->u.general->len;
1096 const char *space_map = NULL;
1099 while ((len = (cp_end - cp)) > 0)
1101 map = zebra_maps_input (zh->reg->zebra_maps, reg_type, &cp, len);
1102 if (**map == *CHR_SPACE)
1107 for (src = space_map; *src; src++)
1110 for (src = *map; *src; src++)
1117 static RSET rpn_prox (ZebraHandle zh, RSET *rset, int rset_no,
1118 int ordered, int exclusion, int relation, int distance)
1123 struct it_key **buf;
1125 char prox_term[1024];
1126 int length_prox_term = 0;
1127 int min_nn = 10000000;
1129 const char *flags = NULL;
1131 rsfd = (RSFD *) xmalloc (sizeof(*rsfd)*rset_no);
1132 more = (int *) xmalloc (sizeof(*more)*rset_no);
1133 buf = (struct it_key **) xmalloc (sizeof(*buf)*rset_no);
1136 for (i = 0; i<rset_no; i++)
1139 for (j = 0; j<rset[i]->no_rset_terms; j++)
1141 const char *nflags = rset[i]->rset_terms[j]->flags;
1142 char *term = rset[i]->rset_terms[j]->name;
1143 int lterm = strlen(term);
1144 if (lterm + length_prox_term < sizeof(prox_term)-1)
1146 if (length_prox_term)
1147 prox_term[length_prox_term++] = ' ';
1148 strcpy (prox_term + length_prox_term, term);
1149 length_prox_term += lterm;
1151 if (min_nn > rset[i]->rset_terms[j]->nn)
1152 min_nn = rset[i]->rset_terms[j]->nn;
1156 for (i = 0; i<rset_no; i++)
1161 for (i = 0; i<rset_no; i++)
1163 buf[i] = (struct it_key *) xmalloc (sizeof(**buf));
1164 rsfd[i] = rset_open (rset[i], RSETF_READ);
1165 if (!(more[i] = rset_read (rset[i], rsfd[i], buf[i], &term_index)))
1170 /* at least one is empty ... return null set */
1171 rset_null_parms parms;
1173 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1175 parms.rset_term->nn = 0;
1176 result = rset_create (rset_kind_null, &parms);
1178 else if (ordered && relation == 3 && exclusion == 0 && distance == 1)
1180 /* special proximity case = phrase search ... */
1181 rset_temp_parms parms;
1184 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1186 parms.rset_term->nn = min_nn;
1187 parms.cmp = key_compare_it;
1188 parms.key_size = sizeof (struct it_key);
1189 parms.temp_path = res_get (zh->res, "setTmpDir");
1190 result = rset_create (rset_kind_temp, &parms);
1191 rsfd_result = rset_open (result, RSETF_WRITE);
1195 for (i = 1; i<rset_no; i++)
1204 cmp = key_compare_it (buf[i], buf[i-1]);
1207 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1208 buf[i-1], &term_index);
1213 if (buf[i-1]->seqno+1 != buf[i]->seqno)
1215 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1216 buf[i-1], &term_index);
1222 more[i] = rset_read (rset[i], rsfd[i], buf[i],
1229 rset_write (result, rsfd_result, buf[0]);
1230 more[0] = rset_read (*rset, *rsfd, *buf, &term_index);
1233 rset_close (result, rsfd_result);
1235 else if (rset_no == 2)
1237 /* generic proximity case (two input sets only) ... */
1238 rset_temp_parms parms;
1241 logf (LOG_LOG, "generic prox, dist = %d, relation = %d, ordered =%d, exclusion=%d",
1242 distance, relation, ordered, exclusion);
1243 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1245 parms.rset_term->nn = min_nn;
1246 parms.cmp = key_compare_it;
1247 parms.key_size = sizeof (struct it_key);
1248 parms.temp_path = res_get (zh->res, "setTmpDir");
1249 result = rset_create (rset_kind_temp, &parms);
1250 rsfd_result = rset_open (result, RSETF_WRITE);
1252 while (more[0] && more[1])
1254 int cmp = key_compare_it (buf[0], buf[1]);
1256 more[0] = rset_read (rset[0], rsfd[0], buf[0], &term_index);
1258 more[1] = rset_read (rset[1], rsfd[1], buf[1], &term_index);
1261 int sysno = buf[0]->sysno;
1265 seqno[n++] = buf[0]->seqno;
1266 while ((more[0] = rset_read (rset[0], rsfd[0], buf[0],
1268 sysno == buf[0]->sysno)
1270 seqno[n++] = buf[0]->seqno;
1273 for (i = 0; i<n; i++)
1275 int diff = buf[1]->seqno - seqno[i];
1276 int excl = exclusion;
1277 if (!ordered && diff < 0)
1282 if (diff < distance && diff >= 0)
1286 if (diff <= distance && diff >= 0)
1290 if (diff == distance && diff >= 0)
1294 if (diff >= distance && diff >= 0)
1298 if (diff > distance && diff >= 0)
1302 if (diff != distance && diff >= 0)
1308 rset_write (result, rsfd_result, buf[1]);
1312 } while ((more[1] = rset_read (rset[1], rsfd[1], buf[1],
1314 sysno == buf[1]->sysno);
1317 rset_close (result, rsfd_result);
1321 rset_null_parms parms;
1323 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1325 parms.rset_term->nn = 0;
1326 result = rset_create (rset_kind_null, &parms);
1328 for (i = 0; i<rset_no; i++)
1331 rset_close (rset[i], rsfd[i]);
1341 char *normalize_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1342 const char *termz, NMEM stream, unsigned reg_id)
1345 AttrType truncation;
1346 int truncation_value;
1349 attr_init (&truncation, zapt, 5);
1350 truncation_value = attr_find (&truncation, NULL);
1352 switch (truncation_value)
1372 wrbuf = zebra_replace(zh->reg->zebra_maps, reg_id, ex_list,
1373 termz, strlen(termz));
1375 return nmem_strdup(stream, termz);
1378 char *buf = (char*) nmem_malloc (stream, wrbuf_len(wrbuf)+1);
1379 memcpy (buf, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1380 buf[wrbuf_len(wrbuf)] = '\0';
1385 static void grep_info_delete (struct grep_info *grep_info)
1388 xfree(grep_info->term_no);
1390 xfree (grep_info->isam_p_buf);
1393 static int grep_info_prepare (ZebraHandle zh,
1394 Z_AttributesPlusTerm *zapt,
1395 struct grep_info *grep_info,
1400 int termset_value_numeric;
1401 const char *termset_value_string;
1404 grep_info->term_no = 0;
1406 grep_info->isam_p_size = 0;
1407 grep_info->isam_p_buf = NULL;
1409 grep_info->reg_type = reg_type;
1410 grep_info->termset = 0;
1412 attr_init (&termset, zapt, 8);
1413 termset_value_numeric =
1414 attr_find_ex (&termset, NULL, &termset_value_string);
1415 if (termset_value_numeric != -1)
1418 const char *termset_name = 0;
1419 if (termset_value_numeric != -2)
1422 sprintf (resname, "%d", termset_value_numeric);
1423 termset_name = resname;
1426 termset_name = termset_value_string;
1427 logf (LOG_LOG, "creating termset set %s", termset_name);
1428 grep_info->termset = resultSetAdd (zh, termset_name, 1);
1429 if (!grep_info->termset)
1432 zh->errString = nmem_strdup (stream, termset_name);
1440 static RSET rpn_search_APT_phrase (ZebraHandle zh,
1441 Z_AttributesPlusTerm *zapt,
1442 const char *termz_org,
1443 oid_value attributeSet,
1445 int reg_type, int complete_flag,
1446 const char *rank_type,
1447 int num_bases, char **basenames)
1449 char term_dst[IT_MAX_WORD+1];
1450 RSET rset[60], result;
1452 struct grep_info grep_info;
1453 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1454 const char *termp = termz;
1457 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1461 logf (LOG_DEBUG, "APT_phrase termp=%s", termp);
1462 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1464 reg_type, complete_flag,
1465 num_bases, basenames,
1466 term_dst, rank_type);
1469 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1472 grep_info_delete (&grep_info);
1475 rset_null_parms parms;
1477 parms.rset_term = rset_term_create (termz, -1, rank_type);
1478 return rset_create (rset_kind_null, &parms);
1480 else if (rset_no == 1)
1482 result = rpn_prox (zh, rset, rset_no, 1, 0, 3, 1);
1483 for (i = 0; i<rset_no; i++)
1484 rset_delete (rset[i]);
1488 static RSET rpn_search_APT_or_list (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;
1504 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1508 logf (LOG_DEBUG, "APT_or_list termp=%s", termp);
1509 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1511 reg_type, complete_flag,
1512 num_bases, basenames,
1513 term_dst, rank_type);
1516 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1519 grep_info_delete (&grep_info);
1522 rset_null_parms parms;
1524 parms.rset_term = rset_term_create (termz, -1, rank_type);
1525 return rset_create (rset_kind_null, &parms);
1528 for (i = 1; i<rset_no; i++)
1530 rset_bool_parms bool_parms;
1532 bool_parms.rset_l = result;
1533 bool_parms.rset_r = rset[i];
1534 bool_parms.key_size = sizeof(struct it_key);
1535 bool_parms.cmp = key_compare_it;
1536 result = rset_create (rset_kind_or, &bool_parms);
1541 static RSET rpn_search_APT_and_list (ZebraHandle zh,
1542 Z_AttributesPlusTerm *zapt,
1543 const char *termz_org,
1544 oid_value attributeSet,
1546 int reg_type, int complete_flag,
1547 const char *rank_type,
1548 int num_bases, char **basenames)
1550 char term_dst[IT_MAX_WORD+1];
1551 RSET rset[60], result;
1553 struct grep_info grep_info;
1554 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1555 const char *termp = termz;
1557 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1561 logf (LOG_DEBUG, "APT_and_list termp=%s", termp);
1562 rset[rset_no] = term_trunc (zh, zapt, &termp, attributeSet,
1564 reg_type, complete_flag,
1565 num_bases, basenames,
1566 term_dst, rank_type);
1569 assert (rset[rset_no]);
1570 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1573 grep_info_delete (&grep_info);
1576 rset_null_parms parms;
1578 parms.rset_term = rset_term_create (termz, -1, rank_type);
1579 return rset_create (rset_kind_null, &parms);
1582 for (i = 1; i<rset_no; i++)
1584 rset_bool_parms bool_parms;
1586 bool_parms.rset_l = result;
1587 bool_parms.rset_r = rset[i];
1588 bool_parms.key_size = sizeof(struct it_key);
1589 bool_parms.cmp = key_compare_it;
1590 result = rset_create (rset_kind_and, &bool_parms);
1595 static int numeric_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1596 const char **term_sub,
1598 oid_value attributeSet,
1599 struct grep_info *grep_info,
1608 char *term_tmp = term_dict + strlen(term_dict);
1610 attr_init (&relation, zapt, 2);
1611 relation_value = attr_find (&relation, NULL);
1613 logf (LOG_DEBUG, "numeric relation value=%d", relation_value);
1615 if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub, term_tmp, 1,
1618 term_value = atoi (term_tmp);
1619 switch (relation_value)
1622 logf (LOG_DEBUG, "Relation <");
1623 gen_regular_rel (term_tmp, term_value-1, 1);
1626 logf (LOG_DEBUG, "Relation <=");
1627 gen_regular_rel (term_tmp, term_value, 1);
1630 logf (LOG_DEBUG, "Relation >=");
1631 gen_regular_rel (term_tmp, term_value, 0);
1634 logf (LOG_DEBUG, "Relation >");
1635 gen_regular_rel (term_tmp, term_value+1, 0);
1639 logf (LOG_DEBUG, "Relation =");
1640 sprintf (term_tmp, "(0*%d)", term_value);
1642 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_tmp);
1643 r = dict_lookup_grep (zh->reg->dict, term_dict, 0, grep_info, max_pos,
1646 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1647 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1651 static int numeric_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1652 const char **term_sub,
1653 oid_value attributeSet, struct grep_info *grep_info,
1654 int reg_type, int complete_flag,
1655 int num_bases, char **basenames,
1658 char term_dict[2*IT_MAX_WORD+2];
1662 oid_value curAttributeSet = attributeSet;
1664 struct rpn_char_map_info rcmi;
1666 rpn_char_map_prepare (zh->reg, reg_type, &rcmi);
1667 attr_init (&use, zapt, 1);
1668 use_value = attr_find (&use, &curAttributeSet);
1669 logf (LOG_DEBUG, "numeric_term, use value %d", use_value);
1671 if (use_value == -1)
1674 for (base_no = 0; base_no < num_bases; base_no++)
1677 data1_local_attribute *local_attr;
1678 int max_pos, prefix_len = 0;
1681 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
1683 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1684 curAttributeSet, use_value, r);
1691 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
1693 zh->errCode = 109; /* Database unavailable */
1694 zh->errString = basenames[base_no];
1697 for (local_attr = attp.local_attributes; local_attr;
1698 local_attr = local_attr->next)
1704 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
1709 term_dict[prefix_len++] = '|';
1711 term_dict[prefix_len++] = '(';
1713 ord_len = key_SU_encode (ord, ord_buf);
1714 for (i = 0; i<ord_len; i++)
1716 term_dict[prefix_len++] = 1;
1717 term_dict[prefix_len++] = ord_buf[i];
1725 term_dict[prefix_len++] = ')';
1726 term_dict[prefix_len++] = 1;
1727 term_dict[prefix_len++] = reg_type;
1728 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1729 term_dict[prefix_len] = '\0';
1730 if (!numeric_relation (zh, zapt, &termp, term_dict,
1731 attributeSet, grep_info, &max_pos, reg_type,
1736 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1740 static RSET rpn_search_APT_numeric (ZebraHandle zh,
1741 Z_AttributesPlusTerm *zapt,
1743 oid_value attributeSet,
1745 int reg_type, int complete_flag,
1746 const char *rank_type,
1747 int num_bases, char **basenames)
1749 char term_dst[IT_MAX_WORD+1];
1750 const char *termp = termz;
1751 RSET rset[60], result;
1752 int i, r, rset_no = 0;
1753 struct grep_info grep_info;
1755 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1759 logf (LOG_DEBUG, "APT_numeric termp=%s", termp);
1760 grep_info.isam_p_indx = 0;
1761 r = numeric_term (zh, zapt, &termp, attributeSet, &grep_info,
1762 reg_type, complete_flag, num_bases, basenames,
1766 logf (LOG_DEBUG, "term: %s", term_dst);
1767 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1768 grep_info.isam_p_indx, term_dst,
1769 strlen(term_dst), rank_type,
1770 0 /* preserve position */);
1771 assert (rset[rset_no]);
1772 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1775 grep_info_delete (&grep_info);
1778 rset_null_parms parms;
1780 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1781 return rset_create (rset_kind_null, &parms);
1784 for (i = 1; i<rset_no; i++)
1786 rset_bool_parms bool_parms;
1788 bool_parms.rset_l = result;
1789 bool_parms.rset_r = rset[i];
1790 bool_parms.key_size = sizeof(struct it_key);
1791 bool_parms.cmp = key_compare_it;
1792 result = rset_create (rset_kind_and, &bool_parms);
1797 static RSET rpn_search_APT_local (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1799 oid_value attributeSet,
1801 const char *rank_type)
1806 rset_temp_parms parms;
1808 parms.rset_term = rset_term_create (termz, -1, rank_type);
1809 parms.cmp = key_compare_it;
1810 parms.key_size = sizeof (struct it_key);
1811 parms.temp_path = res_get (zh->res, "setTmpDir");
1812 result = rset_create (rset_kind_temp, &parms);
1813 rsfd = rset_open (result, RSETF_WRITE);
1815 key.sysno = atoi (termz);
1819 rset_write (result, rsfd, &key);
1820 rset_close (result, rsfd);
1824 static RSET rpn_sort_spec (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1825 oid_value attributeSet, NMEM stream,
1826 Z_SortKeySpecList *sort_sequence,
1827 const char *rank_type)
1829 rset_null_parms parms;
1831 int sort_relation_value;
1832 AttrType sort_relation_type;
1837 Z_AttributeElement *ae;
1842 attr_init (&sort_relation_type, zapt, 7);
1843 sort_relation_value = attr_find (&sort_relation_type, &attributeSet);
1845 attr_init (&use_type, zapt, 1);
1846 use_value = attr_find (&use_type, &attributeSet);
1848 if (!sort_sequence->specs)
1850 sort_sequence->num_specs = 10;
1851 sort_sequence->specs = (Z_SortKeySpec **)
1852 nmem_malloc (stream, sort_sequence->num_specs *
1853 sizeof(*sort_sequence->specs));
1854 for (i = 0; i<sort_sequence->num_specs; i++)
1855 sort_sequence->specs[i] = 0;
1857 if (zapt->term->which != Z_Term_general)
1860 i = atoi_n ((char *) zapt->term->u.general->buf,
1861 zapt->term->u.general->len);
1862 if (i >= sort_sequence->num_specs)
1864 sprintf (termz, "%d", i);
1866 oe.proto = PROTO_Z3950;
1867 oe.oclass = CLASS_ATTSET;
1868 oe.value = attributeSet;
1869 if (!oid_ent_to_oid (&oe, oid))
1872 sks = (Z_SortKeySpec *) nmem_malloc (stream, sizeof(*sks));
1873 sks->sortElement = (Z_SortElement *)
1874 nmem_malloc (stream, sizeof(*sks->sortElement));
1875 sks->sortElement->which = Z_SortElement_generic;
1876 sk = sks->sortElement->u.generic = (Z_SortKey *)
1877 nmem_malloc (stream, sizeof(*sk));
1878 sk->which = Z_SortKey_sortAttributes;
1879 sk->u.sortAttributes = (Z_SortAttributes *)
1880 nmem_malloc (stream, sizeof(*sk->u.sortAttributes));
1882 sk->u.sortAttributes->id = oid;
1883 sk->u.sortAttributes->list = (Z_AttributeList *)
1884 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list));
1885 sk->u.sortAttributes->list->num_attributes = 1;
1886 sk->u.sortAttributes->list->attributes = (Z_AttributeElement **)
1887 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list->attributes));
1888 ae = *sk->u.sortAttributes->list->attributes = (Z_AttributeElement *)
1889 nmem_malloc (stream, sizeof(**sk->u.sortAttributes->list->attributes));
1890 ae->attributeSet = 0;
1891 ae->attributeType = (int *)
1892 nmem_malloc (stream, sizeof(*ae->attributeType));
1893 *ae->attributeType = 1;
1894 ae->which = Z_AttributeValue_numeric;
1895 ae->value.numeric = (int *)
1896 nmem_malloc (stream, sizeof(*ae->value.numeric));
1897 *ae->value.numeric = use_value;
1899 sks->sortRelation = (int *)
1900 nmem_malloc (stream, sizeof(*sks->sortRelation));
1901 if (sort_relation_value == 1)
1902 *sks->sortRelation = Z_SortRelation_ascending;
1903 else if (sort_relation_value == 2)
1904 *sks->sortRelation = Z_SortRelation_descending;
1906 *sks->sortRelation = Z_SortRelation_ascending;
1908 sks->caseSensitivity = (int *)
1909 nmem_malloc (stream, sizeof(*sks->caseSensitivity));
1910 *sks->caseSensitivity = 0;
1913 sks->which = Z_SortKeySpec_null;
1914 sks->u.null = odr_nullval ();
1916 sks->missingValueAction = 0;
1919 sort_sequence->specs[i] = sks;
1921 parms.rset_term = rset_term_create (termz, -1, rank_type);
1922 return rset_create (rset_kind_null, &parms);
1925 static RSET rpn_search_xpath (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1926 oid_value attributeSet,
1927 int num_bases, char **basenames,
1928 NMEM stream, const char *rank_type, RSET rset)
1931 const char *use_string = 0;
1932 oid_value curAttributeSet = attributeSet;
1933 char term_dict[2048];
1936 struct grep_info grep_info;
1938 yaz_log (LOG_LOG, "rpn_search_xpath 1");
1939 attr_init (&use, zapt, 1);
1940 attr_find_ex (&use, &curAttributeSet, &use_string);
1942 if (curAttributeSet != VAL_IDXPATH)
1944 yaz_log (LOG_LOG, "rpn_search_xpath - not 1");
1949 yaz_log (LOG_LOG, "rpn_search_xpath - not 2");
1953 dict_grep_cmap (zh->reg->dict, 0, 0);
1954 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1957 yaz_log (LOG_LOG, "rpn_search_xpath 2");
1958 for (base_no = 0; base_no < num_bases; base_no++)
1960 const char *termp = use_string;
1961 rset_between_parms parms;
1962 RSET rset_start_tag, rset_end_tag;
1963 int ord, ord_len, i, r, max_pos;
1966 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
1968 zh->errCode = 109; /* Database unavailable */
1969 zh->errString = basenames[base_no];
1974 ord = zebraExplain_lookupSU (zh->reg->zei, curAttributeSet, 1);
1978 term_dict[prefix_len++] = '|';
1980 term_dict[prefix_len++] = '(';
1982 ord_len = key_SU_encode (ord, ord_buf);
1983 for (i = 0; i<ord_len; i++)
1985 term_dict[prefix_len++] = 1;
1986 term_dict[prefix_len++] = ord_buf[i];
1988 term_dict[prefix_len++] = ')';
1989 term_dict[prefix_len++] = 1;
1990 term_dict[prefix_len++] = reg_type;
1993 strcpy (term_dict+prefix_len, use_string);
1995 grep_info.isam_p_indx = 0;
1996 yaz_log (LOG_LOG, "rpn_search_xpath 3 %s", term_dict+prefix_len);
1997 r = dict_lookup_grep (zh->reg->dict, term_dict, 0,
1998 &grep_info, &max_pos, 0, grep_handle);
1999 yaz_log (LOG_LOG, "%s %d positions", use_string,
2000 grep_info.isam_p_indx);
2002 rset_trunc (zh, grep_info.isam_p_buf,
2003 grep_info.isam_p_indx, use_string, strlen(use_string),
2007 ord = zebraExplain_lookupSU (zh->reg->zei, curAttributeSet, 2);
2011 term_dict[prefix_len++] = '|';
2013 term_dict[prefix_len++] = '(';
2015 ord_len = key_SU_encode (ord, ord_buf);
2016 for (i = 0; i<ord_len; i++)
2018 term_dict[prefix_len++] = 1;
2019 term_dict[prefix_len++] = ord_buf[i];
2021 term_dict[prefix_len++] = ')';
2022 term_dict[prefix_len++] = 1;
2023 term_dict[prefix_len++] = reg_type;
2027 strcpy (term_dict+prefix_len, use_string);
2029 grep_info.isam_p_indx = 0;
2030 r = dict_lookup_grep (zh->reg->dict, term_dict, 0,
2031 &grep_info, &max_pos, 0, grep_handle);
2033 yaz_log (LOG_LOG, "%s %d positions", use_string,
2034 grep_info.isam_p_indx);
2036 rset_trunc (zh, grep_info.isam_p_buf,
2037 grep_info.isam_p_indx, use_string, strlen(use_string),
2040 parms.key_size = sizeof(struct it_key);
2041 parms.cmp = key_compare_it;
2042 parms.rset_l = rset_start_tag;
2043 parms.rset_m = rset;
2044 parms.rset_r = rset_end_tag;
2045 parms.printer = key_print_it;
2046 yaz_log (LOG_LOG, "rpn_search_xpath 4");
2047 rset = rset_create (rset_kind_between, &parms);
2049 grep_info_delete (&grep_info);
2056 static RSET rpn_search_APT (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
2057 oid_value attributeSet, NMEM stream,
2058 Z_SortKeySpecList *sort_sequence,
2059 int num_bases, char **basenames)
2062 char *search_type = NULL;
2063 char rank_type[128];
2066 char termz[IT_MAX_WORD+1];
2069 zebra_maps_attr (zh->reg->zebra_maps, zapt, ®_id, &search_type,
2070 rank_type, &complete_flag, &sort_flag);
2072 logf (LOG_DEBUG, "reg_id=%c", reg_id);
2073 logf (LOG_DEBUG, "complete_flag=%d", complete_flag);
2074 logf (LOG_DEBUG, "search_type=%s", search_type);
2075 logf (LOG_DEBUG, "rank_type=%s", rank_type);
2077 if (zapt->term->which != Z_Term_general)
2082 trans_term (zh, zapt, termz);
2085 return rpn_sort_spec (zh, zapt, attributeSet, stream, sort_sequence,
2088 if (!strcmp (search_type, "phrase"))
2090 rset = rpn_search_APT_phrase (zh, zapt, termz, attributeSet, stream,
2091 reg_id, complete_flag, rank_type,
2092 num_bases, basenames);
2094 else if (!strcmp (search_type, "and-list"))
2096 rset = rpn_search_APT_and_list (zh, zapt, termz, attributeSet, stream,
2097 reg_id, complete_flag, rank_type,
2098 num_bases, basenames);
2100 else if (!strcmp (search_type, "or-list"))
2102 rset = rpn_search_APT_or_list (zh, zapt, termz, attributeSet, stream,
2103 reg_id, complete_flag, rank_type,
2104 num_bases, basenames);
2106 else if (!strcmp (search_type, "local"))
2108 rset = rpn_search_APT_local (zh, zapt, termz, attributeSet, stream,
2111 else if (!strcmp (search_type, "numeric"))
2113 rset = rpn_search_APT_numeric (zh, zapt, termz, attributeSet, stream,
2114 reg_id, complete_flag, rank_type,
2115 num_bases, basenames);
2119 return rpn_search_xpath (zh, zapt, attributeSet, num_bases, basenames,
2120 stream, rank_type, rset);
2123 static RSET rpn_search_structure (ZebraHandle zh, Z_RPNStructure *zs,
2124 oid_value attributeSet, NMEM stream,
2125 Z_SortKeySpecList *sort_sequence,
2126 int num_bases, char **basenames)
2129 if (zs->which == Z_RPNStructure_complex)
2131 Z_Operator *zop = zs->u.complex->roperator;
2132 rset_bool_parms bool_parms;
2134 bool_parms.rset_l = rpn_search_structure (zh, zs->u.complex->s1,
2135 attributeSet, stream,
2137 num_bases, basenames);
2138 if (bool_parms.rset_l == NULL)
2140 bool_parms.rset_r = rpn_search_structure (zh, zs->u.complex->s2,
2141 attributeSet, stream,
2143 num_bases, basenames);
2144 if (bool_parms.rset_r == NULL)
2146 rset_delete (bool_parms.rset_l);
2149 bool_parms.key_size = sizeof(struct it_key);
2150 bool_parms.cmp = key_compare_it;
2154 case Z_Operator_and:
2155 r = rset_create (rset_kind_and, &bool_parms);
2158 r = rset_create (rset_kind_or, &bool_parms);
2160 case Z_Operator_and_not:
2161 r = rset_create (rset_kind_not, &bool_parms);
2163 case Z_Operator_prox:
2165 if (zop->u.prox->which != Z_ProximityOperator_known)
2171 if (zop->u.prox->which != Z_ProxCode_known)
2179 if (*zop->u.prox->u.known != Z_ProxUnit_word)
2181 char *val = (char *) nmem_malloc (stream, 16);
2183 zh->errString = val;
2184 sprintf (val, "%d", *zop->u.prox->u.known);
2188 if (*zop->u.prox->proximityUnitCode != Z_ProxUnit_word)
2190 char *val = (char *) nmem_malloc (stream, 16);
2192 zh->errString = val;
2193 sprintf (val, "%d", *zop->u.prox->proximityUnitCode);
2201 rsets[0] = bool_parms.rset_l;
2202 rsets[1] = bool_parms.rset_r;
2204 r = rpn_prox (zh, rsets, 2,
2205 *zop->u.prox->ordered,
2206 (!zop->u.prox->exclusion ? 0 :
2207 *zop->u.prox->exclusion),
2208 *zop->u.prox->relationType,
2209 *zop->u.prox->distance);
2210 rset_delete (rsets[0]);
2211 rset_delete (rsets[1]);
2219 else if (zs->which == Z_RPNStructure_simple)
2221 if (zs->u.simple->which == Z_Operand_APT)
2223 logf (LOG_DEBUG, "rpn_search_APT");
2224 r = rpn_search_APT (zh, zs->u.simple->u.attributesPlusTerm,
2225 attributeSet, stream, sort_sequence,
2226 num_bases, basenames);
2228 else if (zs->u.simple->which == Z_Operand_resultSetId)
2230 logf (LOG_DEBUG, "rpn_search_ref");
2231 r = resultSetRef (zh, zs->u.simple->u.resultSetId);
2234 r = rset_create (rset_kind_null, NULL);
2237 nmem_strdup (stream, zs->u.simple->u.resultSetId);
2256 RSET rpn_search (ZebraHandle zh, NMEM nmem,
2257 Z_RPNQuery *rpn, int num_bases, char **basenames,
2258 const char *setname,
2263 oid_value attributeSet;
2264 Z_SortKeySpecList *sort_sequence;
2268 zh->errString = NULL;
2271 sort_sequence = (Z_SortKeySpecList *)
2272 nmem_malloc (nmem, sizeof(*sort_sequence));
2273 sort_sequence->num_specs = 10;
2274 sort_sequence->specs = (Z_SortKeySpec **)
2275 nmem_malloc (nmem, sort_sequence->num_specs *
2276 sizeof(*sort_sequence->specs));
2277 for (i = 0; i<sort_sequence->num_specs; i++)
2278 sort_sequence->specs[i] = 0;
2280 attrset = oid_getentbyoid (rpn->attributeSetId);
2281 attributeSet = attrset->value;
2282 rset = rpn_search_structure (zh, rpn->RPNStructure, attributeSet,
2283 nmem, sort_sequence, num_bases, basenames);
2288 logf (LOG_DEBUG, "search error: %d", zh->errCode);
2290 for (i = 0; sort_sequence->specs[i]; i++)
2292 sort_sequence->num_specs = i;
2294 resultSetRank (zh, sset, rset);
2297 logf (LOG_DEBUG, "resultSetSortSingle in rpn_search");
2298 resultSetSortSingle (zh, nmem, sset, rset,
2299 sort_sequence, &sort_status);
2302 logf (LOG_DEBUG, "resultSetSortSingle status = %d", zh->errCode);
2308 struct scan_info_entry {
2314 struct scan_info_entry *list;
2320 static int scan_handle (char *name, const char *info, int pos, void *client)
2322 int len_prefix, idx;
2323 struct scan_info *scan_info = (struct scan_info *) client;
2325 len_prefix = strlen(scan_info->prefix);
2326 if (memcmp (name, scan_info->prefix, len_prefix))
2328 if (pos > 0) idx = scan_info->after - pos + scan_info->before;
2331 scan_info->list[idx].term = (char *)
2332 odr_malloc (scan_info->odr, strlen(name + len_prefix)+1);
2333 strcpy (scan_info->list[idx].term, name + len_prefix);
2334 assert (*info == sizeof(ISAMS_P));
2335 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAMS_P));
2339 static void scan_term_untrans (ZebraHandle zh, NMEM stream, int reg_type,
2340 char **dst, const char *src)
2342 char term_dst[1024];
2344 term_untrans (zh, reg_type, term_dst, src);
2346 *dst = (char *) nmem_malloc (stream, strlen(term_dst)+1);
2347 strcpy (*dst, term_dst);
2350 static void count_set (RSET r, int *count)
2358 logf (LOG_DEBUG, "count_set");
2361 rfd = rset_open (r, RSETF_READ);
2362 while (rset_read (r, rfd, &key, &term_index))
2364 if (key.sysno != psysno)
2371 rset_close (r, rfd);
2372 logf (LOG_DEBUG, "%d keys, %d records", kno, *count);
2375 void rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
2376 oid_value attributeset,
2377 int num_bases, char **basenames,
2378 int *position, int *num_entries, ZebraScanEntry **list,
2382 int pos = *position;
2383 int num = *num_entries;
2387 char termz[IT_MAX_WORD+20];
2390 struct scan_info *scan_info_array;
2391 ZebraScanEntry *glist;
2392 int ords[32], ord_no = 0;
2396 char *search_type = NULL;
2397 char rank_type[128];
2402 if (attributeset == VAL_NONE)
2403 attributeset = VAL_BIB1;
2405 yaz_log (LOG_DEBUG, "position = %d, num = %d set=%d",
2406 pos, num, attributeset);
2408 attr_init (&use, zapt, 1);
2409 use_value = attr_find (&use, &attributeset);
2411 if (zebra_maps_attr (zh->reg->zebra_maps, zapt, ®_id, &search_type,
2412 rank_type, &complete_flag, &sort_flag))
2418 yaz_log (LOG_DEBUG, "use_value = %d", use_value);
2420 if (use_value == -1)
2422 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
2426 data1_local_attribute *local_attr;
2428 if ((r=att_getentbyatt (zh, &attp, attributeset, use_value)))
2430 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
2431 attributeset, use_value);
2439 if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
2441 zh->errString = basenames[base_no];
2442 zh->errCode = 109; /* Database unavailable */
2446 for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
2447 local_attr = local_attr->next)
2451 ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
2454 ords[ord_no++] = ord;
2463 /* prepare dictionary scanning */
2466 scan_info_array = (struct scan_info *)
2467 odr_malloc (stream, ord_no * sizeof(*scan_info_array));
2468 for (i = 0; i < ord_no; i++)
2470 int j, prefix_len = 0;
2471 int before_tmp = before, after_tmp = after;
2472 struct scan_info *scan_info = scan_info_array + i;
2473 struct rpn_char_map_info rcmi;
2475 rpn_char_map_prepare (zh->reg, reg_id, &rcmi);
2477 scan_info->before = before;
2478 scan_info->after = after;
2479 scan_info->odr = stream;
2481 scan_info->list = (struct scan_info_entry *)
2482 odr_malloc (stream, (before+after) * sizeof(*scan_info->list));
2483 for (j = 0; j<before+after; j++)
2484 scan_info->list[j].term = NULL;
2486 prefix_len += key_SU_encode (ords[i], termz + prefix_len);
2487 termz[prefix_len++] = reg_id;
2488 termz[prefix_len] = 0;
2489 strcpy (scan_info->prefix, termz);
2491 trans_scan_term (zh, zapt, termz+prefix_len, reg_id);
2493 dict_scan (zh->reg->dict, termz, &before_tmp, &after_tmp,
2494 scan_info, scan_handle);
2496 glist = (ZebraScanEntry *)
2497 odr_malloc (stream, (before+after)*sizeof(*glist));
2499 /* consider terms after main term */
2500 for (i = 0; i < ord_no; i++)
2504 for (i = 0; i<after; i++)
2507 const char *mterm = NULL;
2511 for (j = 0; j < ord_no; j++)
2513 if (ptr[j] < before+after &&
2514 (tst=scan_info_array[j].list[ptr[j]].term) &&
2515 (!mterm || strcmp (tst, mterm) < 0))
2523 scan_term_untrans (zh, stream->mem, reg_id,
2524 &glist[i+before].term, mterm);
2525 rset = rset_trunc (zh, &scan_info_array[j0].list[ptr[j0]].isam_p, 1,
2526 glist[i+before].term, strlen(glist[i+before].term),
2530 for (j = j0+1; j<ord_no; j++)
2532 if (ptr[j] < before+after &&
2533 (tst=scan_info_array[j].list[ptr[j]].term) &&
2534 !strcmp (tst, mterm))
2536 rset_bool_parms bool_parms;
2540 rset_trunc (zh, &scan_info_array[j].list[ptr[j]].isam_p, 1,
2541 glist[i+before].term,
2542 strlen(glist[i+before].term), NULL, 0);
2544 bool_parms.key_size = sizeof(struct it_key);
2545 bool_parms.cmp = key_compare_it;
2546 bool_parms.rset_l = rset;
2547 bool_parms.rset_r = rset2;
2549 rset = rset_create (rset_kind_or, &bool_parms);
2554 count_set (rset, &glist[i+before].occurrences);
2559 *num_entries -= (after-i);
2563 /* consider terms before main term */
2564 for (i = 0; i<ord_no; i++)
2567 for (i = 0; i<before; i++)
2570 const char *mterm = NULL;
2574 for (j = 0; j <ord_no; j++)
2576 if (ptr[j] < before &&
2577 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2578 (!mterm || strcmp (tst, mterm) > 0))
2587 scan_term_untrans (zh, stream->mem, reg_id,
2588 &glist[before-1-i].term, mterm);
2591 (zh, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1,
2592 glist[before-1-i].term, strlen(glist[before-1-i].term),
2597 for (j = j0+1; j<ord_no; j++)
2599 if (ptr[j] < before &&
2600 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2601 !strcmp (tst, mterm))
2603 rset_bool_parms bool_parms;
2606 rset2 = rset_trunc (zh,
2607 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1,
2608 glist[before-1-i].term,
2609 strlen(glist[before-1-i].term), NULL, 0);
2611 bool_parms.key_size = sizeof(struct it_key);
2612 bool_parms.cmp = key_compare_it;
2613 bool_parms.rset_l = rset;
2614 bool_parms.rset_r = rset2;
2616 rset = rset_create (rset_kind_or, &bool_parms);
2621 count_set (rset, &glist[before-1-i].occurrences);
2631 *list = glist + i; /* list is set to first 'real' entry */
2633 logf (LOG_DEBUG, "position = %d, num_entries = %d",
2634 *position, *num_entries);
2636 logf (LOG_DEBUG, "scan error: %d", zh->errCode);