2 * Copyright (C) 1995-2002, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
6 * $Id: zrpn.c,v 1.110 2002-03-20 20:24:29 adam Exp $
24 struct rpn_char_map_info {
29 static const char **rpn_char_map_handler (void *vp, const char **from, int len)
31 struct rpn_char_map_info *p = (struct rpn_char_map_info *) vp;
32 return zebra_maps_input (p->zm, p->reg_type, from, len);
35 static void rpn_char_map_prepare (ZebraHandle zh, int reg_type,
36 struct rpn_char_map_info *map_info)
38 map_info->zm = zh->service->zebra_maps;
39 map_info->reg_type = reg_type;
40 dict_grep_cmap (zh->service->dict, map_info, rpn_char_map_handler);
47 Z_AttributesPlusTerm *zapt;
50 static int attr_find_ex (AttrType *src, oid_value *attributeSetP,
51 const char **string_value)
56 num_attributes = src->zapt->attributes->num_attributes;
58 num_attributes = src->zapt->num_attributes;
60 while (src->major < num_attributes)
62 Z_AttributeElement *element;
65 element = src->zapt->attributes->attributes[src->major];
67 element = src->zapt->attributeList[src->major];
69 if (src->type == *element->attributeType)
71 switch (element->which)
73 case Z_AttributeValue_numeric:
75 if (element->attributeSet && attributeSetP)
79 attrset = oid_getentbyoid (element->attributeSet);
80 *attributeSetP = attrset->value;
82 return *element->value.numeric;
84 case Z_AttributeValue_complex:
85 if (src->minor >= element->value.complex->num_list)
87 if (element->value.complex->list[src->minor]->which ==
88 Z_StringOrNumeric_numeric)
91 if (element->attributeSet && attributeSetP)
95 attrset = oid_getentbyoid (element->attributeSet);
96 *attributeSetP = attrset->value;
99 *element->value.complex->list[src->minor-1]->u.numeric;
101 else if (element->value.complex->list[src->minor]->which ==
102 Z_StringOrNumeric_string)
108 element->value.complex->list[src->minor-1]->u.string;
122 static int attr_find (AttrType *src, oid_value *attributeSetP)
124 return attr_find_ex (src, attributeSetP, 0);
127 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
150 static void term_untrans (ZebraHandle zh, int reg_type,
151 char *dst, const char *src)
155 const char *cp = zebra_maps_output (zh->service->zebra_maps,
166 static void add_isam_p (const char *name, const char *info,
169 if (p->isam_p_indx == p->isam_p_size)
171 ISAMS_P *new_isam_p_buf;
175 p->isam_p_size = 2*p->isam_p_size + 100;
176 new_isam_p_buf = (ISAMS_P *) xmalloc (sizeof(*new_isam_p_buf) *
180 memcpy (new_isam_p_buf, p->isam_p_buf,
181 p->isam_p_indx * sizeof(*p->isam_p_buf));
182 xfree (p->isam_p_buf);
184 p->isam_p_buf = new_isam_p_buf;
187 new_term_no = (int *) xmalloc (sizeof(*new_term_no) *
191 memcpy (new_term_no, p->isam_p_buf,
192 p->isam_p_indx * sizeof(*p->term_no));
195 p->term_no = new_term_no;
198 assert (*info == sizeof(*p->isam_p_buf));
199 memcpy (p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
208 int len = key_SU_decode (&su_code, name);
210 term_untrans (p->zh, p->reg_type, term_tmp, name+len+1);
211 logf (LOG_LOG, "grep: %d %c %s", su_code, name[len], term_tmp);
212 zebraExplain_lookup_ord (p->zh->service->zei,
213 su_code, &db, &set, &use);
214 logf (LOG_LOG, "grep: set=%d use=%d db=%s", set, use, db);
216 resultSetAddTerm (p->zh, p->termset, name[len], db,
223 static int grep_handle (char *name, const char *info, void *p)
225 add_isam_p (name, info, (struct grep_info *) p);
229 static int term_pre (ZebraMaps zebra_maps, int reg_type, const char **src,
230 const char *ct1, const char *ct2)
232 const char *s1, *s0 = *src;
235 /* skip white space */
238 if (ct1 && strchr (ct1, *s0))
240 if (ct2 && strchr (ct2, *s0))
243 map = zebra_maps_input (zebra_maps, reg_type, &s1, strlen(s1));
244 if (**map != *CHR_SPACE)
252 /* term_100: handle term, where trunc=none (no operators at all) */
253 static int term_100 (ZebraMaps zebra_maps, int reg_type,
254 const char **src, char *dst, int space_split,
262 const char *space_start = 0;
263 const char *space_end = 0;
265 if (!term_pre (zebra_maps, reg_type, src, NULL, NULL))
271 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
274 if (**map == *CHR_SPACE)
277 else /* complete subfield only. */
279 if (**map == *CHR_SPACE)
280 { /* save space mapping for later .. */
285 else if (space_start)
286 { /* reload last space */
287 while (space_start < space_end)
289 if (!isalnum (*space_start) && *space_start != '-')
291 dst_term[j++] = *space_start;
292 dst[i++] = *space_start++;
295 space_start = space_end = 0;
298 /* add non-space char */
301 if (!isalnum (*s1) && *s1 != '-')
313 /* term_101: handle term, where trunc=Process # */
314 static int term_101 (ZebraMaps zebra_maps, int reg_type,
315 const char **src, char *dst, int space_split,
323 if (!term_pre (zebra_maps, reg_type, src, "#", "#"))
332 dst_term[j++] = *s0++;
337 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
338 if (space_split && **map == *CHR_SPACE)
350 dst_term[j++] = '\0';
355 /* term_103: handle term, where trunc=re-2 (regular expressions) */
356 static int term_103 (ZebraMaps zebra_maps, int reg_type, const char **src,
357 char *dst, int *errors, int space_split,
365 if (!term_pre (zebra_maps, reg_type, src, "^\\()[].*+?|", "("))
368 if (errors && *s0 == '+' && s0[1] && s0[2] == '+' && s0[3] &&
371 *errors = s0[1] - '0';
378 if (strchr ("^\\()[].*+?|-", *s0))
386 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
387 if (**map == *CHR_SPACE)
404 /* term_103: handle term, where trunc=re-1 (regular expressions) */
405 static int term_102 (ZebraMaps zebra_maps, int reg_type, const char **src,
406 char *dst, int space_split, char *dst_term)
408 return term_103 (zebra_maps, reg_type, src, dst, NULL, space_split,
413 /* term_104: handle term, where trunc=Process # and ! */
414 static int term_104 (ZebraMaps zebra_maps, int reg_type,
415 const char **src, char *dst, int space_split,
423 if (!term_pre (zebra_maps, reg_type, src, "#!", "#!"))
432 dst_term[j++] = *s0++;
437 dst_term[j++] = *s0++;
441 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
442 if (space_split && **map == *CHR_SPACE)
454 dst_term[j++] = '\0';
459 /* term_105/106: handle term, where trunc=Process * and ! and right trunc */
460 static int term_105 (ZebraMaps zebra_maps, int reg_type,
461 const char **src, char *dst, int space_split,
462 char *dst_term, int right_truncate)
469 if (!term_pre (zebra_maps, reg_type, src, "*!", "*!"))
478 dst_term[j++] = *s0++;
483 dst_term[j++] = *s0++;
487 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
488 if (space_split && **map == *CHR_SPACE)
506 dst_term[j++] = '\0';
512 /* gen_regular_rel - generate regular expression from relation
513 * val: border value (inclusive)
514 * islt: 1 if <=; 0 if >=.
516 static void gen_regular_rel (char *dst, int val, int islt)
523 logf (LOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
527 strcpy (dst, "(-[0-9]+|(");
535 strcpy (dst, "([0-9]+|-(");
536 dst_p = strlen (dst);
546 dst_p = strlen (dst);
547 sprintf (numstr, "%d", val);
548 for (w = strlen(numstr); --w >= 0; pos++)
567 strcpy (dst + dst_p, numstr);
568 dst_p = strlen(dst) - pos - 1;
596 for (i = 0; i<pos; i++)
609 /* match everything less than 10^(pos-1) */
611 for (i=1; i<pos; i++)
612 strcat (dst, "[0-9]?");
616 /* match everything greater than 10^pos */
617 for (i = 0; i <= pos; i++)
618 strcat (dst, "[0-9]");
619 strcat (dst, "[0-9]*");
624 void string_rel_add_char (char **term_p, const char *src, int *indx)
626 if (src[*indx] == '\\')
627 *(*term_p)++ = src[(*indx)++];
628 *(*term_p)++ = src[(*indx)++];
632 * > abc ([b-].*|a[c-].*|ab[d-].*|abc.+)
633 * ([^-a].*|a[^-b].*ab[^-c].*|abc.+)
634 * >= abc ([b-].*|a[c-].*|ab[c-].*)
635 * ([^-a].*|a[^-b].*|ab[c-].*)
636 * < abc ([-0].*|a[-a].*|ab[-b].*)
637 * ([^a-].*|a[^b-].*|ab[^c-].*)
638 * <= abc ([-0].*|a[-a].*|ab[-b].*|abc)
639 * ([^a-].*|a[^b-].*|ab[^c-].*|abc)
641 static int string_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
642 const char **term_sub, char *term_dict,
643 oid_value attributeSet,
644 int reg_type, int space_split, char *term_dst)
649 char *term_tmp = term_dict + strlen(term_dict);
650 char term_component[256];
652 attr_init (&relation, zapt, 2);
653 relation_value = attr_find (&relation, NULL);
655 logf (LOG_DEBUG, "string relation value=%d", relation_value);
656 switch (relation_value)
659 if (!term_100 (zh->service->zebra_maps, reg_type,
660 term_sub, term_component,
661 space_split, term_dst))
663 logf (LOG_DEBUG, "Relation <");
666 for (i = 0; term_component[i]; )
673 string_rel_add_char (&term_tmp, term_component, &j);
678 string_rel_add_char (&term_tmp, term_component, &i);
689 if (!term_100 (zh->service->zebra_maps, reg_type,
690 term_sub, term_component,
691 space_split, term_dst))
693 logf (LOG_DEBUG, "Relation <=");
696 for (i = 0; term_component[i]; )
701 string_rel_add_char (&term_tmp, term_component, &j);
705 string_rel_add_char (&term_tmp, term_component, &i);
714 for (i = 0; term_component[i]; )
715 string_rel_add_char (&term_tmp, term_component, &i);
720 if (!term_100 (zh->service->zebra_maps, reg_type,
721 term_sub, term_component, space_split, term_dst))
723 logf (LOG_DEBUG, "Relation >");
726 for (i = 0; term_component[i];)
731 string_rel_add_char (&term_tmp, term_component, &j);
736 string_rel_add_char (&term_tmp, term_component, &i);
744 for (i = 0; term_component[i];)
745 string_rel_add_char (&term_tmp, term_component, &i);
752 if (!term_100 (zh->service->zebra_maps, reg_type, term_sub,
753 term_component, space_split, term_dst))
755 logf (LOG_DEBUG, "Relation >=");
758 for (i = 0; term_component[i];)
765 string_rel_add_char (&term_tmp, term_component, &j);
768 if (term_component[i+1])
772 string_rel_add_char (&term_tmp, term_component, &i);
776 string_rel_add_char (&term_tmp, term_component, &i);
788 logf (LOG_DEBUG, "Relation =");
789 if (!term_100 (zh->service->zebra_maps, reg_type, term_sub,
790 term_component, space_split, term_dst))
792 strcat (term_tmp, "(");
793 strcat (term_tmp, term_component);
794 strcat (term_tmp, ")");
799 static int string_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
800 const char **term_sub,
801 oid_value attributeSet, NMEM stream,
802 struct grep_info *grep_info,
803 int reg_type, int complete_flag,
804 int num_bases, char **basenames,
807 char term_dict[2*IT_MAX_WORD+4000];
810 int truncation_value;
813 oid_value curAttributeSet = attributeSet;
815 struct rpn_char_map_info rcmi;
816 int space_split = complete_flag ? 0 : 1;
818 rpn_char_map_prepare (zh, reg_type, &rcmi);
819 attr_init (&use, zapt, 1);
820 use_value = attr_find (&use, &curAttributeSet);
821 logf (LOG_DEBUG, "string_term, use value %d", use_value);
822 attr_init (&truncation, zapt, 5);
823 truncation_value = attr_find (&truncation, NULL);
824 logf (LOG_DEBUG, "truncation value %d", truncation_value);
829 for (base_no = 0; base_no < num_bases; base_no++)
832 data1_local_attribute *local_attr;
833 int max_pos, prefix_len = 0;
836 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
838 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
839 curAttributeSet, use_value, r);
843 sprintf (val_str, "%d", use_value);
845 zh->errString = nmem_strdup (stream, val_str);
850 struct oident oident;
852 oident.proto = PROTO_Z3950;
853 oident.oclass = CLASS_ATTSET;
854 oident.value = curAttributeSet;
855 oid_ent_to_oid (&oident, oid);
858 zh->errString = nmem_strdup (stream, oident.desc);
862 if (zebraExplain_curDatabase (zh->service->zei, basenames[base_no]))
864 zh->errCode = 109; /* Database unavailable */
865 zh->errString = basenames[base_no];
868 for (local_attr = attp.local_attributes; local_attr;
869 local_attr = local_attr->next)
875 ord = zebraExplain_lookupSU (zh->service->zei, attp.attset_ordinal,
880 term_dict[prefix_len++] = '|';
882 term_dict[prefix_len++] = '(';
884 ord_len = key_SU_encode (ord, ord_buf);
885 for (i = 0; i<ord_len; i++)
887 term_dict[prefix_len++] = 1;
888 term_dict[prefix_len++] = ord_buf[i];
894 sprintf (val_str, "%d", use_value);
896 zh->errString = nmem_strdup (stream, val_str);
899 term_dict[prefix_len++] = ')';
900 term_dict[prefix_len++] = 1;
901 term_dict[prefix_len++] = reg_type;
902 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
903 term_dict[prefix_len] = '\0';
905 switch (truncation_value)
907 case -1: /* not specified */
908 case 100: /* do not truncate */
909 if (!string_relation (zh, zapt, &termp, term_dict,
911 reg_type, space_split, term_dst))
913 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_dict+prefix_len);
914 r = dict_lookup_grep (zh->service->dict, term_dict, 0,
915 grep_info, &max_pos, 0, grep_handle);
917 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
919 case 1: /* right truncation */
920 term_dict[j++] = '(';
921 if (!term_100 (zh->service->zebra_maps, reg_type,
922 &termp, term_dict + j, space_split, term_dst))
924 strcat (term_dict, ".*)");
925 dict_lookup_grep (zh->service->dict, term_dict, 0, grep_info,
926 &max_pos, 0, grep_handle);
928 case 2: /* keft truncation */
929 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
930 if (!term_100 (zh->service->zebra_maps, reg_type,
931 &termp, term_dict + j, space_split, term_dst))
933 strcat (term_dict, ")");
934 dict_lookup_grep (zh->service->dict, term_dict, 0, grep_info,
935 &max_pos, 0, grep_handle);
937 case 3: /* left&right truncation */
938 term_dict[j++] = '('; term_dict[j++] = '.'; term_dict[j++] = '*';
939 if (!term_100 (zh->service->zebra_maps, reg_type,
940 &termp, term_dict + j, space_split, term_dst))
942 strcat (term_dict, ".*)");
943 dict_lookup_grep (zh->service->dict, term_dict, 0, grep_info,
944 &max_pos, 0, grep_handle);
948 case 101: /* process # in term */
949 term_dict[j++] = '(';
950 if (!term_101 (zh->service->zebra_maps, reg_type,
951 &termp, term_dict + j, space_split, term_dst))
953 strcat (term_dict, ")");
954 r = dict_lookup_grep (zh->service->dict, term_dict, 0, grep_info,
955 &max_pos, 0, grep_handle);
957 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d", r);
959 case 102: /* Regexp-1 */
960 term_dict[j++] = '(';
961 if (!term_102 (zh->service->zebra_maps, reg_type,
962 &termp, term_dict + j, space_split, term_dst))
964 strcat (term_dict, ")");
965 logf (LOG_DEBUG, "Regexp-1 tolerance=%d", r);
966 r = dict_lookup_grep (zh->service->dict, term_dict, 0, grep_info,
967 &max_pos, 0, grep_handle);
969 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
972 case 103: /* Regexp-2 */
974 term_dict[j++] = '(';
975 if (!term_103 (zh->service->zebra_maps, reg_type,
976 &termp, term_dict + j, &r, space_split, term_dst))
978 strcat (term_dict, ")");
979 logf (LOG_DEBUG, "Regexp-2 tolerance=%d", r);
980 r = dict_lookup_grep (zh->service->dict, term_dict, r, grep_info,
981 &max_pos, 2, grep_handle);
983 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
986 case 104: /* process # and ! in term */
987 term_dict[j++] = '(';
988 if (!term_104 (zh->service->zebra_maps, reg_type,
989 &termp, term_dict + j, space_split, term_dst))
991 strcat (term_dict, ")");
992 r = dict_lookup_grep (zh->service->dict, term_dict, 0, grep_info,
993 &max_pos, 0, grep_handle);
995 logf (LOG_WARN, "dict_lookup_grep err, trunc=#/!: %d", r);
997 case 105: /* process * and ! in term */
998 term_dict[j++] = '(';
999 if (!term_105 (zh->service->zebra_maps, reg_type,
1000 &termp, term_dict + j, space_split, term_dst, 1))
1002 strcat (term_dict, ")");
1003 r = dict_lookup_grep (zh->service->dict, term_dict, 0, grep_info,
1004 &max_pos, 0, grep_handle);
1006 logf (LOG_WARN, "dict_lookup_grep err, trunc=*/!: %d", r);
1008 case 106: /* process * and ! in term */
1009 term_dict[j++] = '(';
1010 if (!term_105 (zh->service->zebra_maps, reg_type,
1011 &termp, term_dict + j, space_split, term_dst, 0))
1013 strcat (term_dict, ")");
1014 r = dict_lookup_grep (zh->service->dict, term_dict, 0, grep_info,
1015 &max_pos, 0, grep_handle);
1017 logf (LOG_WARN, "dict_lookup_grep err, trunc=*/!: %d", r);
1022 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1026 static void trans_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1030 Z_Term *term = zapt->term;
1032 sizez = term->u.general->len;
1033 if (sizez > IT_MAX_WORD-1)
1034 sizez = IT_MAX_WORD-1;
1035 memcpy (termz, term->u.general->buf, sizez);
1036 termz[sizez] = '\0';
1039 static void trans_scan_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1040 char *termz, int reg_type)
1042 Z_Term *term = zapt->term;
1044 const char *cp = (const char *) term->u.general->buf;
1045 const char *cp_end = cp + term->u.general->len;
1048 const char *space_map = NULL;
1051 while ((len = (cp_end - cp)) > 0)
1053 map = zebra_maps_input (zh->service->zebra_maps, reg_type, &cp, len);
1054 if (**map == *CHR_SPACE)
1059 for (src = space_map; *src; src++)
1062 for (src = *map; *src; src++)
1069 static RSET rpn_prox (ZebraHandle zh, RSET *rset, int rset_no,
1070 int ordered, int exclusion, int relation, int distance)
1075 struct it_key **buf;
1077 char prox_term[1024];
1078 int length_prox_term = 0;
1079 int min_nn = 10000000;
1081 const char *flags = NULL;
1083 rsfd = (RSFD *) xmalloc (sizeof(*rsfd)*rset_no);
1084 more = (int *) xmalloc (sizeof(*more)*rset_no);
1085 buf = (struct it_key **) xmalloc (sizeof(*buf)*rset_no);
1088 for (i = 0; i<rset_no; i++)
1091 for (j = 0; j<rset[i]->no_rset_terms; j++)
1093 const char *nflags = rset[i]->rset_terms[j]->flags;
1094 char *term = rset[i]->rset_terms[j]->name;
1095 int lterm = strlen(term);
1096 if (lterm + length_prox_term < sizeof(prox_term)-1)
1098 if (length_prox_term)
1099 prox_term[length_prox_term++] = ' ';
1100 strcpy (prox_term + length_prox_term, term);
1101 length_prox_term += lterm;
1103 if (min_nn > rset[i]->rset_terms[j]->nn)
1104 min_nn = rset[i]->rset_terms[j]->nn;
1108 for (i = 0; i<rset_no; i++)
1113 for (i = 0; i<rset_no; i++)
1115 buf[i] = (struct it_key *) xmalloc (sizeof(**buf));
1116 rsfd[i] = rset_open (rset[i], RSETF_READ);
1117 if (!(more[i] = rset_read (rset[i], rsfd[i], buf[i], &term_index)))
1122 /* at least one is empty ... return null set */
1123 rset_null_parms parms;
1125 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1127 parms.rset_term->nn = 0;
1128 result = rset_create (rset_kind_null, &parms);
1130 else if (ordered && relation == 3 && exclusion == 0 && distance == 1)
1132 /* special proximity case = phrase search ... */
1133 rset_temp_parms parms;
1136 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1138 parms.rset_term->nn = min_nn;
1139 parms.cmp = key_compare_it;
1140 parms.key_size = sizeof (struct it_key);
1141 parms.temp_path = res_get (zh->service->res, "setTmpDir");
1142 result = rset_create (rset_kind_temp, &parms);
1143 rsfd_result = rset_open (result, RSETF_WRITE);
1147 for (i = 1; i<rset_no; i++)
1156 cmp = key_compare_it (buf[i], buf[i-1]);
1159 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1160 buf[i-1], &term_index);
1165 if (buf[i-1]->seqno+1 != buf[i]->seqno)
1167 more[i-1] = rset_read (rset[i-1], rsfd[i-1],
1168 buf[i-1], &term_index);
1174 more[i] = rset_read (rset[i], rsfd[i], buf[i],
1181 rset_write (result, rsfd_result, buf[0]);
1182 more[0] = rset_read (*rset, *rsfd, *buf, &term_index);
1185 rset_close (result, rsfd_result);
1187 else if (rset_no == 2)
1189 /* generic proximity case (two input sets only) ... */
1190 rset_temp_parms parms;
1193 logf (LOG_LOG, "generic prox, dist = %d, relation = %d, ordered =%d, exclusion=%d",
1194 distance, relation, ordered, exclusion);
1195 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1197 parms.rset_term->nn = min_nn;
1198 parms.cmp = key_compare_it;
1199 parms.key_size = sizeof (struct it_key);
1200 parms.temp_path = res_get (zh->service->res, "setTmpDir");
1201 result = rset_create (rset_kind_temp, &parms);
1202 rsfd_result = rset_open (result, RSETF_WRITE);
1204 while (more[0] && more[1])
1206 int cmp = key_compare_it (buf[0], buf[1]);
1208 more[0] = rset_read (rset[0], rsfd[0], buf[0], &term_index);
1210 more[1] = rset_read (rset[1], rsfd[1], buf[1], &term_index);
1213 int sysno = buf[0]->sysno;
1217 seqno[n++] = buf[0]->seqno;
1218 while ((more[0] = rset_read (rset[0], rsfd[0], buf[0],
1220 sysno == buf[0]->sysno)
1222 seqno[n++] = buf[0]->seqno;
1225 for (i = 0; i<n; i++)
1227 int diff = buf[1]->seqno - seqno[i];
1228 int excl = exclusion;
1229 if (!ordered && diff < 0)
1234 if (diff < distance && diff >= 0)
1238 if (diff <= distance && diff >= 0)
1242 if (diff == distance && diff >= 0)
1246 if (diff >= distance && diff >= 0)
1250 if (diff > distance && diff >= 0)
1254 if (diff != distance && diff >= 0)
1260 rset_write (result, rsfd_result, buf[1]);
1264 } while ((more[1] = rset_read (rset[1], rsfd[1], buf[1],
1266 sysno == buf[1]->sysno);
1269 rset_close (result, rsfd_result);
1273 rset_null_parms parms;
1275 parms.rset_term = rset_term_create (prox_term, length_prox_term,
1277 parms.rset_term->nn = 0;
1278 result = rset_create (rset_kind_null, &parms);
1280 for (i = 0; i<rset_no; i++)
1283 rset_close (rset[i], rsfd[i]);
1293 char *normalize_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1294 const char *termz, NMEM stream, unsigned reg_id)
1297 AttrType truncation;
1298 int truncation_value;
1301 attr_init (&truncation, zapt, 5);
1302 truncation_value = attr_find (&truncation, NULL);
1304 switch (truncation_value)
1324 wrbuf = zebra_replace(zh->service->zebra_maps, reg_id, ex_list,
1325 termz, strlen(termz));
1327 return nmem_strdup(stream, termz);
1330 char *buf = (char*) nmem_malloc (stream, wrbuf_len(wrbuf)+1);
1331 memcpy (buf, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1332 buf[wrbuf_len(wrbuf)] = '\0';
1337 static int grep_info_prepare (ZebraHandle zh,
1338 Z_AttributesPlusTerm *zapt,
1339 struct grep_info *grep_info,
1344 int termset_value_numeric;
1345 const char *termset_value_string;
1348 grep_info->term_no = 0;
1350 grep_info->isam_p_size = 0;
1351 grep_info->isam_p_buf = NULL;
1353 grep_info->reg_type = reg_type;
1354 grep_info->termset = 0;
1356 attr_init (&termset, zapt, 8);
1357 termset_value_numeric =
1358 attr_find_ex (&termset, NULL, &termset_value_string);
1359 if (termset_value_numeric != -1)
1362 const char *termset_name = 0;
1363 if (termset_value_numeric != -2)
1366 sprintf (resname, "%d", termset_value_numeric);
1367 termset_name = resname;
1370 termset_name = termset_value_string;
1371 logf (LOG_LOG, "creating termset set %s", termset_name);
1372 grep_info->termset = resultSetAdd (zh, termset_name, 1);
1373 if (!grep_info->termset)
1376 zh->errString = nmem_strdup (stream, termset_name);
1384 static RSET rpn_search_APT_phrase (ZebraHandle zh,
1385 Z_AttributesPlusTerm *zapt,
1386 const char *termz_org,
1387 oid_value attributeSet,
1389 int reg_type, int complete_flag,
1390 const char *rank_type,
1391 int num_bases, char **basenames)
1393 char term_dst[IT_MAX_WORD+1];
1394 RSET rset[60], result;
1395 int i, r, rset_no = 0;
1396 struct grep_info grep_info;
1397 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1398 const char *termp = termz;
1400 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1404 logf (LOG_DEBUG, "APT_phrase termp=%s", termp);
1405 grep_info.isam_p_indx = 0;
1406 r = string_term (zh, zapt, &termp, attributeSet, stream, &grep_info,
1407 reg_type, complete_flag, num_bases, basenames,
1411 logf (LOG_DEBUG, "term: %s", term_dst);
1412 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1413 grep_info.isam_p_indx, term_dst,
1414 strlen(term_dst), rank_type);
1415 assert (rset[rset_no]);
1416 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1420 xfree(grep_info.term_no);
1422 xfree (grep_info.isam_p_buf);
1425 rset_null_parms parms;
1427 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1428 return rset_create (rset_kind_null, &parms);
1430 else if (rset_no == 1)
1432 result = rpn_prox (zh, rset, rset_no, 1, 0, 3, 1);
1433 for (i = 0; i<rset_no; i++)
1434 rset_delete (rset[i]);
1438 static RSET rpn_search_APT_or_list (ZebraHandle zh,
1439 Z_AttributesPlusTerm *zapt,
1440 const char *termz_org,
1441 oid_value attributeSet,
1443 int reg_type, int complete_flag,
1444 const char *rank_type,
1445 int num_bases, char **basenames)
1447 char term_dst[IT_MAX_WORD+1];
1448 RSET rset[60], result;
1449 int i, r, rset_no = 0;
1450 struct grep_info grep_info;
1451 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1452 const char *termp = termz;
1454 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1458 logf (LOG_DEBUG, "APT_or_list termp=%s", termp);
1459 grep_info.isam_p_indx = 0;
1460 r = string_term (zh, zapt, &termp, attributeSet, stream, &grep_info,
1461 reg_type, complete_flag, num_bases, basenames,
1465 logf (LOG_DEBUG, "term: %s", term_dst);
1466 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1467 grep_info.isam_p_indx, term_dst,
1468 strlen(term_dst), rank_type);
1469 assert (rset[rset_no]);
1470 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1474 xfree(grep_info.term_no);
1476 xfree (grep_info.isam_p_buf);
1479 rset_null_parms parms;
1481 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1482 return rset_create (rset_kind_null, &parms);
1485 for (i = 1; i<rset_no; i++)
1487 rset_bool_parms bool_parms;
1489 bool_parms.rset_l = result;
1490 bool_parms.rset_r = rset[i];
1491 bool_parms.key_size = sizeof(struct it_key);
1492 bool_parms.cmp = key_compare_it;
1493 result = rset_create (rset_kind_or, &bool_parms);
1498 static RSET rpn_search_APT_and_list (ZebraHandle zh,
1499 Z_AttributesPlusTerm *zapt,
1500 const char *termz_org,
1501 oid_value attributeSet,
1503 int reg_type, int complete_flag,
1504 const char *rank_type,
1505 int num_bases, char **basenames)
1507 char term_dst[IT_MAX_WORD+1];
1508 RSET rset[60], result;
1509 int i, r, rset_no = 0;
1510 struct grep_info grep_info;
1511 char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
1512 const char *termp = termz;
1514 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1518 logf (LOG_DEBUG, "APT_and_list termp=%s", termp);
1519 grep_info.isam_p_indx = 0;
1520 r = string_term (zh, zapt, &termp, attributeSet, stream, &grep_info,
1521 reg_type, complete_flag, num_bases, basenames,
1525 logf (LOG_DEBUG, "term: %s", term_dst);
1526 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1527 grep_info.isam_p_indx, term_dst,
1528 strlen(term_dst), rank_type);
1529 assert (rset[rset_no]);
1530 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1534 xfree(grep_info.term_no);
1536 xfree (grep_info.isam_p_buf);
1539 rset_null_parms parms;
1541 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1542 return rset_create (rset_kind_null, &parms);
1545 for (i = 1; i<rset_no; i++)
1547 rset_bool_parms bool_parms;
1549 bool_parms.rset_l = result;
1550 bool_parms.rset_r = rset[i];
1551 bool_parms.key_size = sizeof(struct it_key);
1552 bool_parms.cmp = key_compare_it;
1553 result = rset_create (rset_kind_and, &bool_parms);
1558 static int numeric_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1559 const char **term_sub,
1561 oid_value attributeSet,
1562 struct grep_info *grep_info,
1571 char *term_tmp = term_dict + strlen(term_dict);
1573 attr_init (&relation, zapt, 2);
1574 relation_value = attr_find (&relation, NULL);
1576 logf (LOG_DEBUG, "numeric relation value=%d", relation_value);
1578 if (!term_100 (zh->service->zebra_maps, reg_type, term_sub, term_tmp, 1,
1581 term_value = atoi (term_tmp);
1582 switch (relation_value)
1585 logf (LOG_DEBUG, "Relation <");
1586 gen_regular_rel (term_tmp, term_value-1, 1);
1589 logf (LOG_DEBUG, "Relation <=");
1590 gen_regular_rel (term_tmp, term_value, 1);
1593 logf (LOG_DEBUG, "Relation >=");
1594 gen_regular_rel (term_tmp, term_value, 0);
1597 logf (LOG_DEBUG, "Relation >");
1598 gen_regular_rel (term_tmp, term_value+1, 0);
1602 logf (LOG_DEBUG, "Relation =");
1603 sprintf (term_tmp, "(0*%d)", term_value);
1605 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_tmp);
1606 r = dict_lookup_grep (zh->service->dict, term_dict, 0, grep_info, max_pos,
1609 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
1610 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1614 static int numeric_term (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1615 const char **term_sub,
1616 oid_value attributeSet, struct grep_info *grep_info,
1617 int reg_type, int complete_flag,
1618 int num_bases, char **basenames,
1621 char term_dict[2*IT_MAX_WORD+2];
1625 oid_value curAttributeSet = attributeSet;
1627 struct rpn_char_map_info rcmi;
1629 rpn_char_map_prepare (zh, reg_type, &rcmi);
1630 attr_init (&use, zapt, 1);
1631 use_value = attr_find (&use, &curAttributeSet);
1632 logf (LOG_DEBUG, "numeric_term, use value %d", use_value);
1634 if (use_value == -1)
1637 for (base_no = 0; base_no < num_bases; base_no++)
1640 data1_local_attribute *local_attr;
1641 int max_pos, prefix_len = 0;
1644 if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value)))
1646 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
1647 curAttributeSet, use_value, r);
1654 if (zebraExplain_curDatabase (zh->service->zei, basenames[base_no]))
1656 zh->errCode = 109; /* Database unavailable */
1657 zh->errString = basenames[base_no];
1660 for (local_attr = attp.local_attributes; local_attr;
1661 local_attr = local_attr->next)
1667 ord = zebraExplain_lookupSU (zh->service->zei, attp.attset_ordinal,
1672 term_dict[prefix_len++] = '|';
1674 term_dict[prefix_len++] = '(';
1676 ord_len = key_SU_encode (ord, ord_buf);
1677 for (i = 0; i<ord_len; i++)
1679 term_dict[prefix_len++] = 1;
1680 term_dict[prefix_len++] = ord_buf[i];
1688 term_dict[prefix_len++] = ')';
1689 term_dict[prefix_len++] = 1;
1690 term_dict[prefix_len++] = reg_type;
1691 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
1692 term_dict[prefix_len] = '\0';
1693 if (!numeric_relation (zh, zapt, &termp, term_dict,
1694 attributeSet, grep_info, &max_pos, reg_type,
1699 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
1703 static RSET rpn_search_APT_numeric (ZebraHandle zh,
1704 Z_AttributesPlusTerm *zapt,
1706 oid_value attributeSet,
1708 int reg_type, int complete_flag,
1709 const char *rank_type,
1710 int num_bases, char **basenames)
1712 char term_dst[IT_MAX_WORD+1];
1713 const char *termp = termz;
1714 RSET rset[60], result;
1715 int i, r, rset_no = 0;
1716 struct grep_info grep_info;
1718 if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
1722 logf (LOG_DEBUG, "APT_numeric termp=%s", termp);
1723 grep_info.isam_p_indx = 0;
1724 r = numeric_term (zh, zapt, &termp, attributeSet, &grep_info,
1725 reg_type, complete_flag, num_bases, basenames,
1729 logf (LOG_DEBUG, "term: %s", term_dst);
1730 rset[rset_no] = rset_trunc (zh, grep_info.isam_p_buf,
1731 grep_info.isam_p_indx, term_dst,
1732 strlen(term_dst), rank_type);
1733 assert (rset[rset_no]);
1734 if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
1738 xfree(grep_info.term_no);
1740 xfree (grep_info.isam_p_buf);
1743 rset_null_parms parms;
1745 parms.rset_term = rset_term_create (term_dst, -1, rank_type);
1746 return rset_create (rset_kind_null, &parms);
1749 for (i = 1; i<rset_no; i++)
1751 rset_bool_parms bool_parms;
1753 bool_parms.rset_l = result;
1754 bool_parms.rset_r = rset[i];
1755 bool_parms.key_size = sizeof(struct it_key);
1756 bool_parms.cmp = key_compare_it;
1757 result = rset_create (rset_kind_and, &bool_parms);
1762 static RSET rpn_search_APT_local (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1764 oid_value attributeSet,
1766 const char *rank_type)
1771 rset_temp_parms parms;
1773 parms.rset_term = rset_term_create (termz, -1, rank_type);
1774 parms.cmp = key_compare_it;
1775 parms.key_size = sizeof (struct it_key);
1776 parms.temp_path = res_get (zh->service->res, "setTmpDir");
1777 result = rset_create (rset_kind_temp, &parms);
1778 rsfd = rset_open (result, RSETF_WRITE);
1780 key.sysno = atoi (termz);
1784 rset_write (result, rsfd, &key);
1785 rset_close (result, rsfd);
1789 static RSET rpn_sort_spec (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1790 oid_value attributeSet, NMEM stream,
1791 Z_SortKeySpecList *sort_sequence,
1792 const char *rank_type)
1794 rset_null_parms parms;
1796 int sort_relation_value;
1797 AttrType sort_relation_type;
1802 Z_AttributeElement *ae;
1806 attr_init (&sort_relation_type, zapt, 7);
1807 sort_relation_value = attr_find (&sort_relation_type, &attributeSet);
1809 attr_init (&use_type, zapt, 1);
1810 use_value = attr_find (&use_type, &attributeSet);
1812 if (!sort_sequence->specs)
1814 sort_sequence->num_specs = 10;
1815 sort_sequence->specs = (Z_SortKeySpec **)
1816 nmem_malloc (stream, sort_sequence->num_specs *
1817 sizeof(*sort_sequence->specs));
1818 for (i = 0; i<sort_sequence->num_specs; i++)
1819 sort_sequence->specs[i] = 0;
1821 if (zapt->term->which != Z_Term_general)
1824 i = atoi_n ((char *) zapt->term->u.general->buf,
1825 zapt->term->u.general->len);
1826 if (i >= sort_sequence->num_specs)
1829 oe.proto = PROTO_Z3950;
1830 oe.oclass = CLASS_ATTSET;
1831 oe.value = attributeSet;
1832 if (!oid_ent_to_oid (&oe, oid))
1835 sks = (Z_SortKeySpec *) nmem_malloc (stream, sizeof(*sks));
1836 sks->sortElement = (Z_SortElement *)
1837 nmem_malloc (stream, sizeof(*sks->sortElement));
1838 sks->sortElement->which = Z_SortElement_generic;
1839 sk = sks->sortElement->u.generic = (Z_SortKey *)
1840 nmem_malloc (stream, sizeof(*sk));
1841 sk->which = Z_SortKey_sortAttributes;
1842 sk->u.sortAttributes = (Z_SortAttributes *)
1843 nmem_malloc (stream, sizeof(*sk->u.sortAttributes));
1845 sk->u.sortAttributes->id = oid;
1846 sk->u.sortAttributes->list = (Z_AttributeList *)
1847 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list));
1848 sk->u.sortAttributes->list->num_attributes = 1;
1849 sk->u.sortAttributes->list->attributes = (Z_AttributeElement **)
1850 nmem_malloc (stream, sizeof(*sk->u.sortAttributes->list->attributes));
1851 ae = *sk->u.sortAttributes->list->attributes = (Z_AttributeElement *)
1852 nmem_malloc (stream, sizeof(**sk->u.sortAttributes->list->attributes));
1853 ae->attributeSet = 0;
1854 ae->attributeType = (int *)
1855 nmem_malloc (stream, sizeof(*ae->attributeType));
1856 *ae->attributeType = 1;
1857 ae->which = Z_AttributeValue_numeric;
1858 ae->value.numeric = (int *)
1859 nmem_malloc (stream, sizeof(*ae->value.numeric));
1860 *ae->value.numeric = use_value;
1862 sks->sortRelation = (int *)
1863 nmem_malloc (stream, sizeof(*sks->sortRelation));
1864 if (sort_relation_value == 1)
1865 *sks->sortRelation = Z_SortRelation_ascending;
1866 else if (sort_relation_value == 2)
1867 *sks->sortRelation = Z_SortRelation_descending;
1869 *sks->sortRelation = Z_SortRelation_ascending;
1871 sks->caseSensitivity = (int *)
1872 nmem_malloc (stream, sizeof(*sks->caseSensitivity));
1873 *sks->caseSensitivity = 0;
1876 sks->which = Z_SortKeySpec_null;
1877 sks->u.null = odr_nullval ();
1879 sks->missingValueAction = 0;
1882 sort_sequence->specs[i] = sks;
1884 parms.rset_term = rset_term_create ("", -1, rank_type);
1885 return rset_create (rset_kind_null, &parms);
1889 static RSET rpn_search_APT (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
1890 oid_value attributeSet, NMEM stream,
1891 Z_SortKeySpecList *sort_sequence,
1892 int num_bases, char **basenames)
1895 char *search_type = NULL;
1896 char rank_type[128];
1899 char termz[IT_MAX_WORD+1];
1901 zebra_maps_attr (zh->service->zebra_maps, zapt, ®_id, &search_type,
1902 rank_type, &complete_flag, &sort_flag);
1904 logf (LOG_DEBUG, "reg_id=%c", reg_id);
1905 logf (LOG_DEBUG, "complete_flag=%d", complete_flag);
1906 logf (LOG_DEBUG, "search_type=%s", search_type);
1907 logf (LOG_DEBUG, "rank_type=%s", rank_type);
1909 if (zapt->term->which != Z_Term_general)
1914 trans_term (zh, zapt, termz);
1917 return rpn_sort_spec (zh, zapt, attributeSet, stream, sort_sequence,
1920 if (!strcmp (search_type, "phrase"))
1922 return rpn_search_APT_phrase (zh, zapt, termz, attributeSet, stream,
1923 reg_id, complete_flag, rank_type,
1924 num_bases, basenames);
1926 else if (!strcmp (search_type, "and-list"))
1928 return rpn_search_APT_and_list (zh, zapt, termz, attributeSet, stream,
1929 reg_id, complete_flag, rank_type,
1930 num_bases, basenames);
1932 else if (!strcmp (search_type, "or-list"))
1934 return rpn_search_APT_or_list (zh, zapt, termz, attributeSet, stream,
1935 reg_id, complete_flag, rank_type,
1936 num_bases, basenames);
1938 else if (!strcmp (search_type, "local"))
1940 return rpn_search_APT_local (zh, zapt, termz, attributeSet, stream,
1943 else if (!strcmp (search_type, "numeric"))
1945 return rpn_search_APT_numeric (zh, zapt, termz, attributeSet, stream,
1946 reg_id, complete_flag, rank_type,
1947 num_bases, basenames);
1953 static RSET rpn_search_structure (ZebraHandle zh, Z_RPNStructure *zs,
1954 oid_value attributeSet, NMEM stream,
1955 Z_SortKeySpecList *sort_sequence,
1956 int num_bases, char **basenames)
1959 if (zs->which == Z_RPNStructure_complex)
1961 Z_Operator *zop = zs->u.complex->roperator;
1962 rset_bool_parms bool_parms;
1964 bool_parms.rset_l = rpn_search_structure (zh, zs->u.complex->s1,
1965 attributeSet, stream,
1967 num_bases, basenames);
1968 if (bool_parms.rset_l == NULL)
1970 bool_parms.rset_r = rpn_search_structure (zh, zs->u.complex->s2,
1971 attributeSet, stream,
1973 num_bases, basenames);
1974 if (bool_parms.rset_r == NULL)
1976 rset_delete (bool_parms.rset_l);
1979 bool_parms.key_size = sizeof(struct it_key);
1980 bool_parms.cmp = key_compare_it;
1984 case Z_Operator_and:
1985 r = rset_create (rset_kind_and, &bool_parms);
1988 r = rset_create (rset_kind_or, &bool_parms);
1990 case Z_Operator_and_not:
1991 r = rset_create (rset_kind_not, &bool_parms);
1993 case Z_Operator_prox:
1995 if (zop->u.prox->which != Z_ProximityOperator_known)
2001 if (zop->u.prox->which != Z_ProxCode_known)
2009 if (*zop->u.prox->u.known != Z_ProxUnit_word)
2011 char *val = (char *) nmem_malloc (stream, 16);
2013 zh->errString = val;
2014 sprintf (val, "%d", *zop->u.prox->u.known);
2018 if (*zop->u.prox->proximityUnitCode != Z_ProxUnit_word)
2020 char *val = (char *) nmem_malloc (stream, 16);
2022 zh->errString = val;
2023 sprintf (val, "%d", *zop->u.prox->proximityUnitCode);
2031 rsets[0] = bool_parms.rset_l;
2032 rsets[1] = bool_parms.rset_r;
2034 r = rpn_prox (zh, rsets, 2,
2035 *zop->u.prox->ordered,
2036 (!zop->u.prox->exclusion ? 0 :
2037 *zop->u.prox->exclusion),
2038 *zop->u.prox->relationType,
2039 *zop->u.prox->distance);
2040 rset_delete (rsets[0]);
2041 rset_delete (rsets[1]);
2049 else if (zs->which == Z_RPNStructure_simple)
2051 if (zs->u.simple->which == Z_Operand_APT)
2053 logf (LOG_DEBUG, "rpn_search_APT");
2054 r = rpn_search_APT (zh, zs->u.simple->u.attributesPlusTerm,
2055 attributeSet, stream, sort_sequence,
2056 num_bases, basenames);
2058 else if (zs->u.simple->which == Z_Operand_resultSetId)
2060 logf (LOG_DEBUG, "rpn_search_ref");
2061 r = resultSetRef (zh, zs->u.simple->u.resultSetId);
2064 r = rset_create (rset_kind_null, NULL);
2067 nmem_strdup (stream, zs->u.simple->u.resultSetId);
2086 RSET rpn_search (ZebraHandle zh, NMEM nmem,
2087 Z_RPNQuery *rpn, int num_bases, char **basenames,
2088 const char *setname,
2093 oid_value attributeSet;
2094 Z_SortKeySpecList *sort_sequence;
2098 zh->errString = NULL;
2101 sort_sequence = (Z_SortKeySpecList *)
2102 nmem_malloc (nmem, sizeof(*sort_sequence));
2103 sort_sequence->num_specs = 10;
2104 sort_sequence->specs = (Z_SortKeySpec **)
2105 nmem_malloc (nmem, sort_sequence->num_specs *
2106 sizeof(*sort_sequence->specs));
2107 for (i = 0; i<sort_sequence->num_specs; i++)
2108 sort_sequence->specs[i] = 0;
2110 attrset = oid_getentbyoid (rpn->attributeSetId);
2111 attributeSet = attrset->value;
2112 rset = rpn_search_structure (zh, rpn->RPNStructure, attributeSet,
2113 nmem, sort_sequence, num_bases, basenames);
2118 logf (LOG_DEBUG, "search error: %d", zh->errCode);
2120 for (i = 0; sort_sequence->specs[i]; i++)
2122 sort_sequence->num_specs = i;
2124 resultSetRank (zh, sset, rset);
2127 logf (LOG_DEBUG, "resultSetSortSingle in rpn_search");
2128 resultSetSortSingle (zh, nmem, sset, rset,
2129 sort_sequence, &sort_status);
2132 logf (LOG_DEBUG, "resultSetSortSingle status = %d", zh->errCode);
2138 struct scan_info_entry {
2144 struct scan_info_entry *list;
2150 static int scan_handle (char *name, const char *info, int pos, void *client)
2152 int len_prefix, idx;
2153 struct scan_info *scan_info = (struct scan_info *) client;
2155 len_prefix = strlen(scan_info->prefix);
2156 if (memcmp (name, scan_info->prefix, len_prefix))
2158 if (pos > 0) idx = scan_info->after - pos + scan_info->before;
2161 scan_info->list[idx].term = (char *)
2162 odr_malloc (scan_info->odr, strlen(name + len_prefix)+1);
2163 strcpy (scan_info->list[idx].term, name + len_prefix);
2164 assert (*info == sizeof(ISAMS_P));
2165 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAMS_P));
2169 static void scan_term_untrans (ZebraHandle zh, NMEM stream, int reg_type,
2170 char **dst, const char *src)
2172 char term_dst[1024];
2174 term_untrans (zh, reg_type, term_dst, src);
2176 *dst = (char *) nmem_malloc (stream, strlen(term_dst)+1);
2177 strcpy (*dst, term_dst);
2180 static void count_set (RSET r, int *count)
2188 logf (LOG_DEBUG, "count_set");
2191 rfd = rset_open (r, RSETF_READ);
2192 while (rset_read (r, rfd, &key, &term_index))
2194 if (key.sysno != psysno)
2201 rset_close (r, rfd);
2202 logf (LOG_DEBUG, "%d keys, %d records", kno, *count);
2205 void rpn_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
2206 oid_value attributeset,
2207 int num_bases, char **basenames,
2208 int *position, int *num_entries, ZebraScanEntry **list,
2212 int pos = *position;
2213 int num = *num_entries;
2217 char termz[IT_MAX_WORD+20];
2220 struct scan_info *scan_info_array;
2221 ZebraScanEntry *glist;
2222 int ords[32], ord_no = 0;
2226 char *search_type = NULL;
2227 char rank_type[128];
2232 if (attributeset == VAL_NONE)
2233 attributeset = VAL_BIB1;
2235 yaz_log (LOG_DEBUG, "position = %d, num = %d set=%d",
2236 pos, num, attributeset);
2238 attr_init (&use, zapt, 1);
2239 use_value = attr_find (&use, &attributeset);
2241 if (zebra_maps_attr (zh->service->zebra_maps, zapt, ®_id, &search_type,
2242 rank_type, &complete_flag, &sort_flag))
2248 yaz_log (LOG_DEBUG, "use_value = %d", use_value);
2250 if (use_value == -1)
2252 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
2256 data1_local_attribute *local_attr;
2258 if ((r=att_getentbyatt (zh, &attp, attributeset, use_value)))
2260 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
2261 attributeset, use_value);
2269 if (zebraExplain_curDatabase (zh->service->zei, basenames[base_no]))
2271 zh->errString = basenames[base_no];
2272 zh->errCode = 109; /* Database unavailable */
2276 for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
2277 local_attr = local_attr->next)
2281 ord = zebraExplain_lookupSU (zh->service->zei, attp.attset_ordinal,
2284 ords[ord_no++] = ord;
2293 /* prepare dictionary scanning */
2296 scan_info_array = (struct scan_info *)
2297 odr_malloc (stream, ord_no * sizeof(*scan_info_array));
2298 for (i = 0; i < ord_no; i++)
2300 int j, prefix_len = 0;
2301 int before_tmp = before, after_tmp = after;
2302 struct scan_info *scan_info = scan_info_array + i;
2303 struct rpn_char_map_info rcmi;
2305 rpn_char_map_prepare (zh, reg_id, &rcmi);
2307 scan_info->before = before;
2308 scan_info->after = after;
2309 scan_info->odr = stream;
2311 scan_info->list = (struct scan_info_entry *)
2312 odr_malloc (stream, (before+after) * sizeof(*scan_info->list));
2313 for (j = 0; j<before+after; j++)
2314 scan_info->list[j].term = NULL;
2316 prefix_len += key_SU_encode (ords[i], termz + prefix_len);
2317 termz[prefix_len++] = reg_id;
2318 termz[prefix_len] = 0;
2319 strcpy (scan_info->prefix, termz);
2321 trans_scan_term (zh, zapt, termz+prefix_len, reg_id);
2323 dict_scan (zh->service->dict, termz, &before_tmp, &after_tmp,
2324 scan_info, scan_handle);
2326 glist = (ZebraScanEntry *)
2327 odr_malloc (stream, (before+after)*sizeof(*glist));
2329 /* consider terms after main term */
2330 for (i = 0; i < ord_no; i++)
2334 for (i = 0; i<after; i++)
2337 const char *mterm = NULL;
2341 for (j = 0; j < ord_no; j++)
2343 if (ptr[j] < before+after &&
2344 (tst=scan_info_array[j].list[ptr[j]].term) &&
2345 (!mterm || strcmp (tst, mterm) < 0))
2353 scan_term_untrans (zh, stream->mem, reg_id,
2354 &glist[i+before].term, mterm);
2355 rset = rset_trunc (zh, &scan_info_array[j0].list[ptr[j0]].isam_p, 1,
2356 glist[i+before].term, strlen(glist[i+before].term),
2360 for (j = j0+1; j<ord_no; j++)
2362 if (ptr[j] < before+after &&
2363 (tst=scan_info_array[j].list[ptr[j]].term) &&
2364 !strcmp (tst, mterm))
2366 rset_bool_parms bool_parms;
2370 rset_trunc (zh, &scan_info_array[j].list[ptr[j]].isam_p, 1,
2371 glist[i+before].term,
2372 strlen(glist[i+before].term), NULL);
2374 bool_parms.key_size = sizeof(struct it_key);
2375 bool_parms.cmp = key_compare_it;
2376 bool_parms.rset_l = rset;
2377 bool_parms.rset_r = rset2;
2379 rset = rset_create (rset_kind_or, &bool_parms);
2384 count_set (rset, &glist[i+before].occurrences);
2389 *num_entries -= (after-i);
2393 /* consider terms before main term */
2394 for (i = 0; i<ord_no; i++)
2397 for (i = 0; i<before; i++)
2400 const char *mterm = NULL;
2404 for (j = 0; j <ord_no; j++)
2406 if (ptr[j] < before &&
2407 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2408 (!mterm || strcmp (tst, mterm) > 0))
2417 scan_term_untrans (zh, stream->mem, reg_id,
2418 &glist[before-1-i].term, mterm);
2421 (zh, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1,
2422 glist[before-1-i].term, strlen(glist[before-1-i].term),
2427 for (j = j0+1; j<ord_no; j++)
2429 if (ptr[j] < before &&
2430 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
2431 !strcmp (tst, mterm))
2433 rset_bool_parms bool_parms;
2436 rset2 = rset_trunc (zh,
2437 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1,
2438 glist[before-1-i].term,
2439 strlen(glist[before-1-i].term), NULL);
2441 bool_parms.key_size = sizeof(struct it_key);
2442 bool_parms.cmp = key_compare_it;
2443 bool_parms.rset_l = rset;
2444 bool_parms.rset_r = rset2;
2446 rset = rset_create (rset_kind_or, &bool_parms);
2451 count_set (rset, &glist[before-1-i].occurrences);
2461 *list = glist + i; /* list is set to first 'real' entry */
2463 logf (LOG_DEBUG, "position = %d, num_entries = %d",
2464 *position, *num_entries);
2466 logf (LOG_DEBUG, "scan error: %d", zh->errCode);