2 * Copyright (C) 1994-1997, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.68 1997-10-27 14:33:06 adam
8 * Moved towards generic character mapping depending on "structure"
9 * field in abstract syntax file. Fixed a few memory leaks. Fixed
10 * bug with negative integers when doing searches with relational
13 * Revision 1.67 1997/09/29 09:06:10 adam
14 * Removed one static var in order to make this module thread safe.
16 * Revision 1.66 1997/09/25 14:58:03 adam
19 * Revision 1.65 1997/09/22 12:39:06 adam
20 * Added get_pos method for the ranked result sets.
22 * Revision 1.64 1997/09/18 08:59:20 adam
23 * Extra generic handle for the character mapping routines.
25 * Revision 1.63 1997/09/17 12:19:18 adam
26 * Zebra version corresponds to YAZ version 1.4.
27 * Changed Zebra server so that it doesn't depend on global common_resource.
29 * Revision 1.62 1997/09/05 15:30:09 adam
30 * Changed prototype for chr_map_input - added const.
31 * Added support for C++, headers uses extern "C" for public definitions.
33 * Revision 1.61 1997/02/10 10:21:14 adam
34 * Bug fix: in search terms character (^) wasn't observed.
36 * Revision 1.60 1997/01/31 11:10:34 adam
37 * Bug fix: Leading and trailing white space weren't removed in scan tokens.
39 * Revision 1.59 1997/01/17 11:31:46 adam
40 * Bug fix: complete phrase search didn't work.
42 * Revision 1.58 1996/12/23 15:30:45 adam
44 * Bug fix: result sets weren't deleted after server shut down.
46 * Revision 1.57 1996/11/11 13:38:02 adam
47 * Added proximity support in search.
49 * Revision 1.56 1996/11/08 11:10:32 adam
50 * Buffers used during file match got bigger.
51 * Compressed ISAM support everywhere.
52 * Bug fixes regarding masking characters in queries.
53 * Redesigned Regexp-2 queries.
55 * Revision 1.55 1996/11/04 14:07:44 adam
56 * Moved truncation code to trunc.c.
58 * Revision 1.54 1996/10/29 14:09:52 adam
59 * Use of cisam system - enabled if setting isamc is 1.
61 * Revision 1.53 1996/06/26 09:21:43 adam
62 * Bug fix: local attribute set wasn't obeyed in scan.
64 * Revision 1.52 1996/06/17 14:26:20 adam
65 * Function gen_regular_rel changed to handle negative numbers.
67 * Revision 1.51 1996/06/11 10:54:15 quinn
70 * Revision 1.50 1996/06/07 08:51:53 adam
71 * Bug fix: Character mapping was broken (introducued by last revision).
73 * Revision 1.49 1996/06/04 10:18:11 adam
74 * Search/scan uses character mapping module.
76 * Revision 1.48 1996/05/28 15:15:01 adam
77 * Bug fix: Didn't handle unknown database correctly.
79 * Revision 1.47 1996/05/15 18:36:28 adam
80 * Function trans_term transforms unsearchable characters to blanks.
82 * Revision 1.46 1996/05/15 11:57:56 adam
83 * Fixed bug introduced by set/field mapping in search operations.
85 * Revision 1.45 1996/05/14 11:34:00 adam
86 * Scan support in multiple registers/databases.
88 * Revision 1.44 1996/05/14 06:16:44 adam
89 * Compact use/set bytes used in search service.
91 * Revision 1.43 1996/05/09 09:54:43 adam
92 * Server supports maps from one logical attributes to a list of physical
94 * The extraction process doesn't make space consuming 'any' keys.
96 * Revision 1.42 1996/05/09 07:28:56 quinn
97 * Work towards phrases and multiple registers
99 * Revision 1.41 1996/03/20 09:36:43 adam
100 * Function dict_lookup_grep got extra parameter, init_pos, which marks
101 * from which position in pattern approximate pattern matching should occur.
102 * Approximate pattern matching is used in relevance=re-2.
104 * Revision 1.40 1996/02/02 13:44:44 adam
105 * The public dictionary functions simply use char instead of Dict_char
106 * to represent search strings. Dict_char is used internally only.
108 * Revision 1.39 1996/01/03 16:22:13 quinn
109 * operator->roperator
111 * Revision 1.38 1995/12/11 09:12:55 adam
112 * The rec_get function returns NULL if record doesn't exist - will
113 * happen in the server if the result set records have been deleted since
114 * the creation of the set (i.e. the search).
115 * The server saves a result temporarily if it is 'volatile', i.e. the
116 * set is register dependent.
118 * Revision 1.37 1995/12/06 15:05:28 adam
119 * More verbose in count_set.
121 * Revision 1.36 1995/12/06 12:41:27 adam
122 * New command 'stat' for the index program.
123 * Filenames can be read from stdin by specifying '-'.
124 * Bug fix/enhancement of the transformation from terms to regular
125 * expressons in the search engine.
127 * Revision 1.35 1995/11/27 09:29:00 adam
128 * Bug fixes regarding conversion to regular expressions.
130 * Revision 1.34 1995/11/16 17:00:56 adam
131 * Better logging of rpn query.
133 * Revision 1.33 1995/11/01 13:58:28 quinn
134 * Moving data1 to yaz/retrieval
136 * Revision 1.32 1995/10/27 14:00:11 adam
137 * Implemented detection of database availability.
139 * Revision 1.31 1995/10/17 18:02:10 adam
140 * New feature: databases. Implemented as prefix to words in dictionary.
142 * Revision 1.30 1995/10/16 09:32:38 adam
143 * More work on relational op.
145 * Revision 1.29 1995/10/13 16:01:49 adam
148 * Revision 1.28 1995/10/13 12:26:43 adam
149 * Optimization of truncation.
151 * Revision 1.27 1995/10/12 17:07:22 adam
154 * Revision 1.26 1995/10/12 12:40:54 adam
155 * Bug fixes in rpn_prox.
157 * Revision 1.25 1995/10/10 13:59:24 adam
158 * Function rset_open changed its wflag parameter to general flags.
160 * Revision 1.24 1995/10/09 16:18:37 adam
161 * Function dict_lookup_grep got extra client data parameter.
163 * Revision 1.23 1995/10/06 16:33:37 adam
164 * Use attribute mappings.
166 * Revision 1.22 1995/10/06 15:07:39 adam
167 * Structure 'local-number' handled.
169 * Revision 1.21 1995/10/06 13:52:06 adam
170 * Bug fixes. Handler may abort further scanning.
172 * Revision 1.20 1995/10/06 11:06:33 adam
173 * Scan entries include 'occurrences' now.
175 * Revision 1.19 1995/10/06 10:43:56 adam
176 * Scan added. 'occurrences' in scan entries not set yet.
178 * Revision 1.18 1995/10/04 16:57:20 adam
179 * Key input and merge sort in one pass.
181 * Revision 1.17 1995/10/04 12:55:17 adam
182 * Bug fix in ranked search. Use=Any keys inserted.
184 * Revision 1.16 1995/10/02 16:24:40 adam
185 * Use attribute actually used in search requests.
187 * Revision 1.15 1995/10/02 15:18:52 adam
188 * New member in recRetrieveCtrl: diagnostic.
190 * Revision 1.14 1995/09/28 12:10:32 adam
191 * Bug fixes. Field prefix used in queries.
193 * Revision 1.13 1995/09/18 14:17:50 adam
196 * Revision 1.12 1995/09/15 14:45:21 adam
198 * Work on truncation.
200 * Revision 1.11 1995/09/14 11:53:27 adam
201 * First work on regular expressions/truncations.
203 * Revision 1.10 1995/09/11 15:23:26 adam
204 * More work on relevance search.
206 * Revision 1.9 1995/09/11 13:09:35 adam
207 * More work on relevance feedback.
209 * Revision 1.8 1995/09/08 14:52:27 adam
210 * Minor changes. Dictionary is lower case now.
212 * Revision 1.7 1995/09/07 13:58:36 adam
213 * New parameter: result-set file descriptor (RSFD) to support multiple
214 * positions within the same result-set.
215 * Boolean operators: and, or, not implemented.
216 * Result-set references.
218 * Revision 1.6 1995/09/06 16:11:18 adam
219 * Option: only one word key per file.
221 * Revision 1.5 1995/09/06 10:33:04 adam
222 * More work on present. Some log messages removed.
224 * Revision 1.4 1995/09/05 15:28:40 adam
225 * More work on search engine.
227 * Revision 1.3 1995/09/04 15:20:22 adam
230 * Revision 1.2 1995/09/04 12:33:43 adam
231 * Various cleanup. YAZ util used instead.
233 * Revision 1.1 1995/09/04 09:10:40 adam
234 * More work on index add/del/update.
235 * Merge sort implemented.
236 * Initial work on z39 server.
256 struct rpn_char_map_info {
261 static const char **rpn_char_map_handler (void *vp, const char **from, int len)
263 struct rpn_char_map_info *p = vp;
264 return zebra_maps_input (p->zm, p->reg_type, from, len);
267 static void rpn_char_map_prepare (ZServerInfo *zi, int reg_type,
268 struct rpn_char_map_info *map_info)
270 map_info->zm = zi->zebra_maps;
271 map_info->reg_type = reg_type;
272 dict_grep_cmap (zi->dict, map_info, rpn_char_map_handler);
279 Z_AttributesPlusTerm *zapt;
282 static int attr_find (AttrType *src, oid_value *attributeSetP)
284 while (src->major < src->zapt->num_attributes)
286 Z_AttributeElement *element;
288 element = src->zapt->attributeList[src->major];
289 if (src->type == *element->attributeType)
291 switch (element->which)
293 case Z_AttributeValue_numeric:
295 if (element->attributeSet && attributeSetP)
299 attrset = oid_getentbyoid (element->attributeSet);
300 *attributeSetP = attrset->value;
302 return *element->value.numeric;
304 case Z_AttributeValue_complex:
305 if (src->minor >= element->value.complex->num_list ||
306 element->value.complex->list[src->minor]->which !=
307 Z_StringOrNumeric_numeric)
310 if (element->attributeSet && attributeSetP)
314 attrset = oid_getentbyoid (element->attributeSet);
315 *attributeSetP = attrset->value;
317 return *element->value.complex->list[src->minor-1]->u.numeric;
327 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
347 static void add_isam_p (const char *info, struct grep_info *p)
349 if (p->isam_p_indx == p->isam_p_size)
351 ISAM_P *new_isam_p_buf;
356 p->isam_p_size = 2*p->isam_p_size + 100;
357 new_isam_p_buf = xmalloc (sizeof(*new_isam_p_buf) *
361 memcpy (new_isam_p_buf, p->isam_p_buf,
362 p->isam_p_indx * sizeof(*p->isam_p_buf));
363 xfree (p->isam_p_buf);
365 p->isam_p_buf = new_isam_p_buf;
368 new_term_no = xmalloc (sizeof(*new_term_no) *
372 memcpy (new_term_no, p->isam_p_buf,
373 p->isam_p_indx * sizeof(*p->term_no));
376 p->term_no = new_term_no;
379 assert (*info == sizeof(*p->isam_p_buf));
380 memcpy (p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
384 static int grep_handle (char *name, const char *info, void *p)
386 add_isam_p (info, p);
390 static int term_pre (ZebraMaps zebra_maps, int reg_type, const char **src,
391 const char *ct1, const char *ct2)
393 const char *s1, *s0 = *src;
396 /* skip white space */
399 if (ct1 && strchr (ct1, *s0))
401 if (ct2 && strchr (ct2, *s0))
404 map = zebra_maps_input (zebra_maps, reg_type, &s1, strlen(s1));
405 if (**map != *CHR_SPACE)
413 static int term_100 (ZebraMaps zebra_maps, int reg_type,
414 const char **src, char *dst, int space_split)
420 if (!term_pre (zebra_maps, reg_type, src, NULL, NULL))
426 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
427 if (space_split && **map == *CHR_SPACE)
431 if (!isalnum (*s1) && *s1 != '-')
441 static int term_101 (ZebraMaps zebra_maps, int reg_type,
442 const char **src, char *dst, int space_split)
448 if (!term_pre (zebra_maps, reg_type, src, "#", "#"))
462 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
463 if (space_split && **map == *CHR_SPACE)
479 static int term_103 (ZebraMaps zebra_maps, int reg_type, const char **src,
480 char *dst, int *errors, int space_split)
486 if (!term_pre (zebra_maps, reg_type, src, "^\\()[].*+?|", "("))
489 if (errors && *s0 == '+' && s0[1] && s0[2] == '+' && s0[3] &&
492 *errors = s0[1] - '0';
499 if (strchr ("^\\()[].*+?|-", *s0))
504 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
505 if (**map == *CHR_SPACE)
520 static int term_102 (ZebraMaps zebra_maps, int reg_type, const char **src,
521 char *dst, int space_split)
523 return term_103 (zebra_maps, reg_type, src, dst, NULL, space_split);
526 /* gen_regular_rel - generate regular expression from relation
527 * val: border value (inclusive)
528 * islt: 1 if <=; 0 if >=.
530 static void gen_regular_rel (char *dst, int val, int islt)
537 logf (LOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
541 strcpy (dst, "(-[0-9]+|(");
549 strcpy (dst, "([0-9]+|-(");
550 dst_p = strlen (dst);
560 dst_p = strlen (dst);
561 sprintf (numstr, "%d", val);
562 for (w = strlen(numstr); --w >= 0; pos++)
581 strcpy (dst + dst_p, numstr);
582 dst_p = strlen(dst) - pos - 1;
610 for (i = 0; i<pos; i++)
623 for (i=1; i<pos; i++)
624 strcat (dst, "[0-9]?");
628 for (i = 0; i <= pos; i++)
629 strcat (dst, "[0-9]");
630 strcat (dst, "[0-9]*");
635 static int relational_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
636 const char **term_sub,
638 oid_value attributeSet,
639 struct grep_info *grep_info,
647 char *term_tmp = term_dict + strlen(term_dict);
649 attr_init (&relation, zapt, 2);
650 relation_value = attr_find (&relation, NULL);
652 logf (LOG_DEBUG, "relation value=%d", relation_value);
653 switch (relation_value)
656 if (!term_100 (zi->zebra_maps, reg_type, term_sub, term_tmp, 1))
658 term_value = atoi (term_tmp);
659 logf (LOG_DEBUG, "Relation <");
660 gen_regular_rel (term_tmp, term_value-1, 1);
663 if (!term_100 (zi->zebra_maps, reg_type, term_sub, term_tmp, 1))
665 term_value = atoi (term_tmp);
666 logf (LOG_DEBUG, "Relation <=");
667 gen_regular_rel (term_tmp, term_value, 1);
670 if (!term_100 (zi->zebra_maps, reg_type, term_sub, term_tmp, 1))
672 term_value = atoi (term_tmp);
673 logf (LOG_DEBUG, "Relation >=");
674 gen_regular_rel (term_tmp, term_value, 0);
677 if (!term_100 (zi->zebra_maps, reg_type, term_sub, term_tmp, 1))
679 term_value = atoi (term_tmp);
680 logf (LOG_DEBUG, "Relation >");
681 gen_regular_rel (term_tmp, term_value+1, 0);
686 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_tmp);
687 r = dict_lookup_grep (zi->dict, term_dict, 0, grep_info, max_pos,
690 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
691 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
695 static int field_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
696 const char **term_sub,
697 oid_value attributeSet, struct grep_info *grep_info,
698 int reg_type, int complete_flag,
699 int num_bases, char **basenames)
701 char term_dict[2*IT_MAX_WORD+2];
704 int truncation_value;
707 oid_value curAttributeSet = attributeSet;
709 struct rpn_char_map_info rcmi;
710 int space_split = complete_flag ? 0 : 1;
712 rpn_char_map_prepare (zi, reg_type, &rcmi);
713 attr_init (&use, zapt, 1);
714 use_value = attr_find (&use, &curAttributeSet);
715 logf (LOG_DEBUG, "field_term, use value %d", use_value);
716 attr_init (&truncation, zapt, 5);
717 truncation_value = attr_find (&truncation, NULL);
718 logf (LOG_DEBUG, "truncation value %d", truncation_value);
723 for (base_no = 0; base_no < num_bases; base_no++)
726 data1_local_attribute *local_attr;
727 int max_pos, prefix_len = 0;
730 if (!att_getentbyatt (zi, &attp, curAttributeSet, use_value))
732 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
733 curAttributeSet, use_value);
737 if (zebTargetInfo_curDatabase (zi->zti, basenames[base_no]))
739 zi->errCode = 109; /* Database unavailable */
740 zi->errString = basenames[base_no];
743 for (local_attr = attp.local_attributes; local_attr;
744 local_attr = local_attr->next)
748 ord = zebTargetInfo_lookupSU (zi->zti, attp.attset_ordinal,
753 term_dict[prefix_len++] = '|';
755 term_dict[prefix_len++] = '(';
756 term_dict[prefix_len++] = 1;
757 term_dict[prefix_len++] = ord;
764 term_dict[prefix_len++] = ')';
765 term_dict[prefix_len++] = 1;
766 term_dict[prefix_len++] = reg_type;
767 logf (LOG_LOG, "reg_type = %d", term_dict[prefix_len-1]);
768 term_dict[prefix_len] = '\0';
769 if (!relational_term (zi, zapt, &termp, term_dict,
770 attributeSet, grep_info, &max_pos, reg_type))
773 switch (truncation_value)
775 case -1: /* not specified */
776 case 100: /* do not truncate */
777 term_dict[j++] = '(';
778 if (!term_100 (zi->zebra_maps, reg_type,
779 &termp, term_dict + j, space_split))
781 strcat (term_dict, ")");
782 r = dict_lookup_grep (zi->dict, term_dict, 0, grep_info,
783 &max_pos, 0, grep_handle);
785 logf (LOG_WARN, "dict_lookup_grep err, trunc=none:%d", r);
787 case 1: /* right truncation */
788 term_dict[j++] = '(';
789 if (!term_100 (zi->zebra_maps, reg_type,
790 &termp, term_dict + j, space_split))
792 strcat (term_dict, ".*)");
793 dict_lookup_grep (zi->dict, term_dict, 0, grep_info,
794 &max_pos, 0, grep_handle);
796 case 2: /* left truncation */
797 case 3: /* left&right truncation */
800 case 101: /* process # in term */
801 term_dict[j++] = '(';
802 if (!term_101 (zi->zebra_maps, reg_type,
803 &termp, term_dict + j, space_split))
805 strcat (term_dict, ")");
806 r = dict_lookup_grep (zi->dict, term_dict, 0, grep_info,
807 &max_pos, 0, grep_handle);
809 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d", r);
811 case 102: /* Regexp-1 */
812 term_dict[j++] = '(';
813 if (!term_102 (zi->zebra_maps, reg_type,
814 &termp, term_dict + j, space_split))
816 strcat (term_dict, ")");
817 logf (LOG_DEBUG, "Regexp-1 tolerance=%d", r);
818 r = dict_lookup_grep (zi->dict, term_dict, 0, grep_info,
819 &max_pos, 0, grep_handle);
821 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
824 case 103: /* Regexp-2 */
826 term_dict[j++] = '(';
827 if (!term_103 (zi->zebra_maps, reg_type,
828 &termp, term_dict + j, &r, space_split))
830 strcat (term_dict, ")");
831 logf (LOG_DEBUG, "Regexp-2 tolerance=%d", r);
832 r = dict_lookup_grep (zi->dict, term_dict, r, grep_info,
833 &max_pos, 2, grep_handle);
835 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
842 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
846 static void trans_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
850 Z_Term *term = zapt->term;
852 sizez = term->u.general->len;
853 if (sizez > IT_MAX_WORD-1)
854 sizez = IT_MAX_WORD-1;
855 memcpy (termz, term->u.general->buf, sizez);
859 static void trans_scan_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
860 char *termz, int reg_type)
862 Z_Term *term = zapt->term;
864 const char *cp = (const char *) term->u.general->buf;
865 const char *cp_end = cp + term->u.general->len;
868 const char *space_map = NULL;
871 while ((len = (cp_end - cp)) > 0)
873 map = zebra_maps_input (zi->zebra_maps, reg_type, &cp, len);
874 if (**map == *CHR_SPACE)
879 for (src = space_map; *src; src++)
882 for (src = *map; *src; src++)
889 static RSET rpn_search_APT_relevance (ZServerInfo *zi,
890 Z_AttributesPlusTerm *zapt,
891 oid_value attributeSet,
892 int reg_type, int complete_flag,
893 int num_bases, char **basenames)
895 rset_relevance_parms parms;
896 char termz[IT_MAX_WORD+1];
897 const char *termp = termz;
898 struct grep_info grep_info;
903 parms.key_size = sizeof(struct it_key);
904 parms.max_rec = 1000;
905 parms.cmp = key_compare_it;
906 parms.get_pos = key_get_pos;
908 parms.isc = zi->isamc;
911 if (zapt->term->which != Z_Term_general)
916 trans_term (zi, zapt, termz);
919 grep_info.term_no = 0;
921 grep_info.isam_p_indx = 0;
922 grep_info.isam_p_size = 0;
923 grep_info.isam_p_buf = NULL;
926 r = field_term (zi, zapt, &termp, attributeSet, &grep_info,
927 reg_type, complete_flag, num_bases, basenames);
931 for (; term_index < grep_info.isam_p_indx; term_index++)
932 grep_info.term_no[term_index] = parms.no_terms;
936 parms.term_no = grep_info.term_no;
937 parms.isam_positions = grep_info.isam_p_buf;
938 parms.no_isam_positions = grep_info.isam_p_indx;
939 if (grep_info.isam_p_indx > 0)
940 result = rset_create (rset_kind_relevance, &parms);
942 result = rset_create (rset_kind_null, NULL);
944 xfree(grep_info.term_no);
946 xfree (grep_info.isam_p_buf);
950 static RSET rpn_proximity (ZServerInfo *zi, RSET rset1, RSET rset2,
952 int exclusion, int relation, int distance)
957 struct it_key buf1, buf2;
960 rset_temp_parms parms;
962 rsfd1 = rset_open (rset1, RSETF_READ|RSETF_SORT_SYSNO);
963 more1 = rset_read (rset1, rsfd1, &buf1);
965 rsfd2 = rset_open (rset2, RSETF_READ|RSETF_SORT_SYSNO);
966 more2 = rset_read (rset2, rsfd2, &buf2);
968 parms.key_size = sizeof (struct it_key);
969 parms.temp_path = res_get (zi->res, "setTmpDir");
970 result = rset_create (rset_kind_temp, &parms);
971 rsfd_result = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
973 logf (LOG_DEBUG, "rpn_proximity excl=%d ord=%d rel=%d dis=%d",
974 exclusion, ordered, relation, distance);
975 while (more1 && more2)
977 int cmp = key_compare_it (&buf1, &buf2);
979 more1 = rset_read (rset1, rsfd1, &buf1);
981 more2 = rset_read (rset2, rsfd2, &buf2);
984 int sysno = buf1.sysno;
988 seqno[n++] = buf1.seqno;
989 while ((more1 = rset_read (rset1, rsfd1, &buf1)) &&
992 seqno[n++] = buf1.seqno;
995 for (i = 0; i<n; i++)
997 int diff = buf2.seqno - seqno[i];
998 int excl = exclusion;
999 if (!ordered && diff < 0)
1004 if (diff < distance)
1008 if (diff <= distance)
1012 if (diff == distance)
1016 if (diff >= distance)
1020 if (diff > distance)
1024 if (diff != distance)
1029 rset_write (result, rsfd_result, &buf2);
1031 } while ((more2 = rset_read (rset2, rsfd2, &buf2)) &&
1032 sysno == buf2.sysno);
1035 rset_close (result, rsfd_result);
1036 rset_close (rset1, rsfd1);
1037 rset_close (rset2, rsfd2);
1041 static RSET rpn_prox (ZServerInfo *zi, RSET *rset, int rset_no)
1046 struct it_key **buf;
1049 rset_temp_parms parms;
1051 rsfd = xmalloc (sizeof(*rsfd)*rset_no);
1052 more = xmalloc (sizeof(*more)*rset_no);
1053 buf = xmalloc (sizeof(*buf)*rset_no);
1055 for (i = 0; i<rset_no; i++)
1057 buf[i] = xmalloc (sizeof(**buf));
1058 rsfd[i] = rset_open (rset[i], RSETF_READ|RSETF_SORT_SYSNO);
1059 if (!(more[i] = rset_read (rset[i], rsfd[i], buf[i])))
1063 rset_close (rset[i], rsfd[i]);
1070 return rset_create (rset_kind_null, NULL);
1073 parms.key_size = sizeof (struct it_key);
1074 parms.temp_path = res_get (zi->res, "setTmpDir");
1075 result = rset_create (rset_kind_temp, &parms);
1076 rsfd_result = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
1080 for (i = 1; i<rset_no; i++)
1089 cmp = key_compare_it (buf[i], buf[i-1]);
1092 more[i-1] = rset_read (rset[i-1], rsfd[i-1], buf[i-1]);
1097 if (buf[i-1]->seqno+1 != buf[i]->seqno)
1099 more[i-1] = rset_read (rset[i-1], rsfd[i-1], buf[i-1]);
1105 more[i] = rset_read (rset[i], rsfd[i], buf[i]);
1111 rset_write (result, rsfd_result, buf[0]);
1112 more[0] = rset_read (*rset, *rsfd, *buf);
1116 for (i = 0; i<rset_no; i++)
1118 rset_close (rset[i], rsfd[i]);
1121 rset_close (result, rsfd_result);
1128 static RSET rpn_search_APT_phrase (ZServerInfo *zi,
1129 Z_AttributesPlusTerm *zapt,
1130 oid_value attributeSet,
1131 int reg_type, int complete_flag,
1132 int num_bases, char **basenames)
1134 char termz[IT_MAX_WORD+1];
1135 const char *termp = termz;
1136 RSET rset[60], result;
1137 int i, r, rset_no = 0;
1138 struct grep_info grep_info;
1140 if (zapt->term->which != Z_Term_general)
1145 trans_term (zi, zapt, termz);
1148 grep_info.term_no = 0;
1150 grep_info.isam_p_size = 0;
1151 grep_info.isam_p_buf = NULL;
1155 logf (LOG_LOG, "APT_phrase termp=%s", termp);
1156 grep_info.isam_p_indx = 0;
1157 r = field_term (zi, zapt, &termp, attributeSet, &grep_info,
1158 reg_type, complete_flag, num_bases, basenames);
1161 rset[rset_no] = rset_trunc (zi, grep_info.isam_p_buf,
1162 grep_info.isam_p_indx);
1163 assert (rset[rset_no]);
1164 if (++rset_no >= sizeof(rset)/sizeof(*rset))
1168 xfree(grep_info.term_no);
1170 xfree (grep_info.isam_p_buf);
1172 return rset_create (rset_kind_null, NULL);
1173 else if (rset_no == 1)
1175 result = rpn_prox (zi, rset, rset_no);
1176 for (i = 0; i<rset_no; i++)
1177 rset_delete (rset[i]);
1181 static RSET rpn_search_APT_local (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1182 oid_value attributeSet)
1187 rset_temp_parms parms;
1188 char termz[IT_MAX_WORD+1];
1190 if (zapt->term->which != Z_Term_general)
1195 parms.key_size = sizeof (struct it_key);
1196 parms.temp_path = res_get (zi->res, "setTmpDir");
1197 result = rset_create (rset_kind_temp, &parms);
1198 rsfd = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
1200 trans_term (zi, zapt, termz);
1202 key.sysno = atoi (termz);
1205 rset_write (result, rsfd, &key);
1206 rset_close (result, rsfd);
1210 static RSET rpn_search_APT (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1211 oid_value attributeSet,
1212 int num_bases, char **basenames)
1215 char *search_type = NULL;
1218 zebra_maps_attr (zi->zebra_maps, zapt, ®_type, &search_type,
1221 logf (LOG_DEBUG, "reg_type=%c", reg_type);
1222 logf (LOG_DEBUG, "complete_flag=%d", complete_flag);
1223 logf (LOG_DEBUG, "search_type=%s", search_type);
1224 if (!strcmp (search_type, "phrase"))
1226 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1227 reg_type, complete_flag,
1228 num_bases, basenames);
1230 else if (!strcmp (search_type, "ranked"))
1232 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1233 reg_type, complete_flag,
1234 num_bases, basenames);
1236 else if (!strcmp (search_type, "local"))
1238 return rpn_search_APT_local (zi, zapt, attributeSet);
1244 static RSET rpn_search_ref (ZServerInfo *zi, Z_ResultSetId *resultSetId)
1248 if (!(s = resultSetGet (zi, resultSetId)))
1249 return rset_create (rset_kind_null, NULL);
1253 static RSET rpn_search_structure (ZServerInfo *zi, Z_RPNStructure *zs,
1254 oid_value attributeSet,
1255 int num_bases, char **basenames)
1258 if (zs->which == Z_RPNStructure_complex)
1260 Z_Operator *zop = zs->u.complex->roperator;
1261 rset_bool_parms bool_parms;
1265 bool_parms.rset_l = rpn_search_structure (zi, zs->u.complex->s1,
1267 num_bases, basenames);
1268 if (bool_parms.rset_l == NULL)
1270 if (rset_is_ranked(bool_parms.rset_l))
1272 bool_parms.rset_r = rpn_search_structure (zi, zs->u.complex->s2,
1274 num_bases, basenames);
1275 if (bool_parms.rset_r == NULL)
1277 rset_delete (bool_parms.rset_l);
1280 if (rset_is_ranked(bool_parms.rset_r))
1282 bool_parms.key_size = sizeof(struct it_key);
1283 bool_parms.cmp = key_compare_it;
1287 case Z_Operator_and:
1288 r = rset_create (soft ? rset_kind_sand:rset_kind_and, &bool_parms);
1291 r = rset_create (soft ? rset_kind_sor:rset_kind_or, &bool_parms);
1293 case Z_Operator_and_not:
1294 r = rset_create (soft ? rset_kind_snot:rset_kind_not, &bool_parms);
1296 case Z_Operator_prox:
1297 if (zop->u.prox->which != Z_ProxCode_known)
1302 if (*zop->u.prox->proximityUnitCode != Z_ProxUnit_word)
1304 char *val = odr_malloc (zi->odr, 16);
1306 zi->errString = val;
1307 sprintf (val, "%d", *zop->u.prox->proximityUnitCode);
1310 r = rpn_proximity (zi, bool_parms.rset_l, bool_parms.rset_r,
1311 *zop->u.prox->ordered,
1312 (!zop->u.prox->exclusion ? 0 :
1313 *zop->u.prox->exclusion),
1314 *zop->u.prox->relationType,
1315 *zop->u.prox->distance);
1322 else if (zs->which == Z_RPNStructure_simple)
1324 if (zs->u.simple->which == Z_Operand_APT)
1326 logf (LOG_DEBUG, "rpn_search_APT");
1327 r = rpn_search_APT (zi, zs->u.simple->u.attributesPlusTerm,
1328 attributeSet, num_bases, basenames);
1330 else if (zs->u.simple->which == Z_Operand_resultSetId)
1332 logf (LOG_DEBUG, "rpn_search_ref");
1333 r = rpn_search_ref (zi, zs->u.simple->u.resultSetId);
1349 void count_set_save (ZServerInfo *zi, RSET *r, int *count)
1356 rset_temp_parms parms;
1357 int maxResultSetSize = atoi (res_get_def (zi->res,
1358 "maxResultSetSize", "400"));
1359 logf (LOG_DEBUG, "count_set_save");
1361 parms.key_size = sizeof(struct it_key);
1362 parms.temp_path = res_get (zi->res, "setTmpDir");
1363 w = rset_create (rset_kind_temp, &parms);
1364 wfd = rset_open (w, RSETF_WRITE|RSETF_SORT_SYSNO);
1365 rfd = rset_open (*r, RSETF_READ|RSETF_SORT_SYSNO);
1366 while (rset_read (*r, rfd, &key))
1368 if (key.sysno != psysno)
1370 if (*count < maxResultSetSize)
1371 rset_write (w, wfd, &key);
1377 rset_close (*r, rfd);
1379 rset_close (w, wfd);
1381 logf (LOG_DEBUG, "%d keys, %d distinct sysnos", kno, *count);
1384 static void count_set (RSET r, int *count)
1391 logf (LOG_DEBUG, "count_set");
1393 rfd = rset_open (r, RSETF_READ|RSETF_SORT_SYSNO);
1394 while (rset_read (r, rfd, &key))
1396 if (key.sysno != psysno)
1403 rset_close (r, rfd);
1404 logf (LOG_DEBUG, "%d keys, %d distinct sysnos", kno, *count);
1407 int rpn_search (ZServerInfo *zi,
1408 Z_RPNQuery *rpn, int num_bases, char **basenames,
1409 const char *setname, int *hits)
1413 oid_value attributeSet;
1418 zi->errString = NULL;
1420 attrset = oid_getentbyoid (rpn->attributeSetId);
1421 attributeSet = attrset->value;
1422 rset = rpn_search_structure (zi, rpn->RPNStructure, attributeSet,
1423 num_bases, basenames);
1426 if (rset_is_volatile(rset))
1427 count_set_save(zi, &rset,hits);
1429 count_set (rset, hits);
1430 resultSetAdd (zi, setname, 1, rset);
1432 logf (LOG_DEBUG, "search error: %d", zi->errCode);
1436 struct scan_info_entry {
1442 struct scan_info_entry *list;
1448 static int scan_handle (char *name, const char *info, int pos, void *client)
1450 int len_prefix, idx;
1451 struct scan_info *scan_info = client;
1453 len_prefix = strlen(scan_info->prefix);
1454 if (memcmp (name, scan_info->prefix, len_prefix))
1457 idx = scan_info->after - pos + scan_info->before;
1460 scan_info->list[idx].term = odr_malloc (scan_info->odr,
1461 strlen(name + len_prefix)+1);
1462 strcpy (scan_info->list[idx].term, name + len_prefix);
1463 assert (*info == sizeof(ISAM_P));
1464 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAM_P));
1469 static void scan_term_untrans (ZServerInfo *zi, int reg_type,
1470 char **dstp, const char *src)
1472 char *dst = odr_malloc (zi->odr, strlen(src)*2+1);
1477 const char *cp = zebra_maps_output (zi->zebra_maps, reg_type, &src);
1484 int rpn_scan (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1485 oid_value attributeset,
1486 int num_bases, char **basenames,
1487 int *position, int *num_entries, struct scan_entry **list,
1491 int pos = *position;
1492 int num = *num_entries;
1496 char termz[IT_MAX_WORD+20];
1499 struct scan_info *scan_info_array;
1500 struct scan_entry *glist;
1501 int ords[32], ord_no = 0;
1505 char *search_type = NULL;
1508 logf (LOG_DEBUG, "scan, position = %d, num = %d", pos, num);
1510 if (attributeset == VAL_NONE)
1511 attributeset = VAL_BIB1;
1513 attr_init (&use, zapt, 1);
1514 use_value = attr_find (&use, &attributeset);
1515 logf (LOG_DEBUG, "use value %d", use_value);
1517 if (zebra_maps_attr (zi->zebra_maps, zapt, ®_type, &search_type,
1524 if (use_value == -1)
1526 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
1529 data1_local_attribute *local_attr;
1531 if (!att_getentbyatt (zi, &attp, attributeset, use_value))
1533 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
1534 attributeset, use_value);
1535 return zi->errCode = 114;
1537 if (zebTargetInfo_curDatabase (zi->zti, basenames[base_no]))
1539 zi->errString = basenames[base_no];
1540 return zi->errCode = 109; /* Database unavailable */
1542 for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
1543 local_attr = local_attr->next)
1547 ord = zebTargetInfo_lookupSU (zi->zti, attp.attset_ordinal,
1550 ords[ord_no++] = ord;
1554 return zi->errCode = 113;
1557 scan_info_array = odr_malloc (zi->odr, ord_no * sizeof(*scan_info_array));
1558 for (i = 0; i < ord_no; i++)
1560 int j, prefix_len = 0;
1561 int before_tmp = before, after_tmp = after;
1562 struct scan_info *scan_info = scan_info_array + i;
1563 struct rpn_char_map_info rcmi;
1565 rpn_char_map_prepare (zi, reg_type, &rcmi);
1567 scan_info->before = before;
1568 scan_info->after = after;
1569 scan_info->odr = zi->odr;
1571 scan_info->list = odr_malloc (zi->odr, (before+after)*
1572 sizeof(*scan_info->list));
1573 for (j = 0; j<before+after; j++)
1574 scan_info->list[j].term = NULL;
1575 termz[prefix_len++] = ords[i];
1576 termz[prefix_len++] = reg_type;
1577 termz[prefix_len] = 0;
1578 strcpy (scan_info->prefix, termz);
1580 trans_scan_term (zi, zapt, termz+prefix_len, reg_type);
1582 dict_scan (zi->dict, termz, &before_tmp, &after_tmp, scan_info,
1585 glist = odr_malloc (zi->odr, (before+after)*sizeof(*glist));
1586 for (i = 0; i < ord_no; i++)
1589 *status = BEND_SCAN_SUCCESS;
1590 for (i = 0; i<after; i++)
1593 const char *mterm = NULL;
1597 for (j = 0; j < ord_no; j++)
1599 if (ptr[j] < before+after &&
1600 (tst=scan_info_array[j].list[ptr[j]].term) &&
1601 (!mterm || strcmp (tst, mterm) < 0))
1609 scan_term_untrans (zi, reg_type, &glist[i+before].term, mterm);
1610 rset = rset_trunc (zi, &scan_info_array[j0].list[ptr[j0]].isam_p, 1);
1613 for (j = j0+1; j<ord_no; j++)
1615 if (ptr[j] < before+after &&
1616 (tst=scan_info_array[j].list[ptr[j]].term) &&
1617 !strcmp (tst, mterm))
1619 rset_bool_parms bool_parms;
1623 rset_trunc (zi, &scan_info_array[j].list[ptr[j]].isam_p, 1);
1625 bool_parms.key_size = sizeof(struct it_key);
1626 bool_parms.cmp = key_compare_it;
1627 bool_parms.rset_l = rset;
1628 bool_parms.rset_r = rset2;
1630 rset = rset_create (rset_kind_or, &bool_parms);
1635 count_set (rset, &glist[i+before].occurrences);
1640 *num_entries -= (after-i);
1641 *status = BEND_SCAN_PARTIAL;
1644 for (i = 0; i<ord_no; i++)
1647 for (i = 0; i<before; i++)
1650 const char *mterm = NULL;
1654 for (j = 0; j <ord_no; j++)
1656 if (ptr[j] < before &&
1657 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
1658 (!mterm || strcmp (tst, mterm) > 0))
1667 scan_term_untrans (zi, reg_type, &glist[before-1-i].term, mterm);
1670 (zi, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1);
1674 for (j = j0+1; j<ord_no; j++)
1676 if (ptr[j] < before &&
1677 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
1678 !strcmp (tst, mterm))
1680 rset_bool_parms bool_parms;
1683 rset2 = rset_trunc (zi,
1684 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1);
1686 bool_parms.key_size = sizeof(struct it_key);
1687 bool_parms.cmp = key_compare_it;
1688 bool_parms.rset_l = rset;
1689 bool_parms.rset_r = rset2;
1691 rset = rset_create (rset_kind_or, &bool_parms);
1696 count_set (rset, &glist[before-1-i].occurrences);
1702 *status = BEND_SCAN_PARTIAL;
1706 *list = glist + i; /* list is set to first 'real' entry */
1708 logf (LOG_DEBUG, "position = %d, num_entries = %d",
1709 *position, *num_entries);
1711 logf (LOG_DEBUG, "scan error: %d", zi->errCode);