2 * Copyright (C) 1994-1997, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.70 1997-10-31 12:34:43 adam
8 * Changed a few log statements.
10 * Revision 1.69 1997/10/29 12:05:02 adam
11 * Server produces diagnostic "Unsupported Attribute Set" when appropriate.
13 * Revision 1.68 1997/10/27 14:33:06 adam
14 * Moved towards generic character mapping depending on "structure"
15 * field in abstract syntax file. Fixed a few memory leaks. Fixed
16 * bug with negative integers when doing searches with relational
19 * Revision 1.67 1997/09/29 09:06:10 adam
20 * Removed one static var in order to make this module thread safe.
22 * Revision 1.66 1997/09/25 14:58:03 adam
25 * Revision 1.65 1997/09/22 12:39:06 adam
26 * Added get_pos method for the ranked result sets.
28 * Revision 1.64 1997/09/18 08:59:20 adam
29 * Extra generic handle for the character mapping routines.
31 * Revision 1.63 1997/09/17 12:19:18 adam
32 * Zebra version corresponds to YAZ version 1.4.
33 * Changed Zebra server so that it doesn't depend on global common_resource.
35 * Revision 1.62 1997/09/05 15:30:09 adam
36 * Changed prototype for chr_map_input - added const.
37 * Added support for C++, headers uses extern "C" for public definitions.
39 * Revision 1.61 1997/02/10 10:21:14 adam
40 * Bug fix: in search terms character (^) wasn't observed.
42 * Revision 1.60 1997/01/31 11:10:34 adam
43 * Bug fix: Leading and trailing white space weren't removed in scan tokens.
45 * Revision 1.59 1997/01/17 11:31:46 adam
46 * Bug fix: complete phrase search didn't work.
48 * Revision 1.58 1996/12/23 15:30:45 adam
50 * Bug fix: result sets weren't deleted after server shut down.
52 * Revision 1.57 1996/11/11 13:38:02 adam
53 * Added proximity support in search.
55 * Revision 1.56 1996/11/08 11:10:32 adam
56 * Buffers used during file match got bigger.
57 * Compressed ISAM support everywhere.
58 * Bug fixes regarding masking characters in queries.
59 * Redesigned Regexp-2 queries.
61 * Revision 1.55 1996/11/04 14:07:44 adam
62 * Moved truncation code to trunc.c.
64 * Revision 1.54 1996/10/29 14:09:52 adam
65 * Use of cisam system - enabled if setting isamc is 1.
67 * Revision 1.53 1996/06/26 09:21:43 adam
68 * Bug fix: local attribute set wasn't obeyed in scan.
70 * Revision 1.52 1996/06/17 14:26:20 adam
71 * Function gen_regular_rel changed to handle negative numbers.
73 * Revision 1.51 1996/06/11 10:54:15 quinn
76 * Revision 1.50 1996/06/07 08:51:53 adam
77 * Bug fix: Character mapping was broken (introducued by last revision).
79 * Revision 1.49 1996/06/04 10:18:11 adam
80 * Search/scan uses character mapping module.
82 * Revision 1.48 1996/05/28 15:15:01 adam
83 * Bug fix: Didn't handle unknown database correctly.
85 * Revision 1.47 1996/05/15 18:36:28 adam
86 * Function trans_term transforms unsearchable characters to blanks.
88 * Revision 1.46 1996/05/15 11:57:56 adam
89 * Fixed bug introduced by set/field mapping in search operations.
91 * Revision 1.45 1996/05/14 11:34:00 adam
92 * Scan support in multiple registers/databases.
94 * Revision 1.44 1996/05/14 06:16:44 adam
95 * Compact use/set bytes used in search service.
97 * Revision 1.43 1996/05/09 09:54:43 adam
98 * Server supports maps from one logical attributes to a list of physical
100 * The extraction process doesn't make space consuming 'any' keys.
102 * Revision 1.42 1996/05/09 07:28:56 quinn
103 * Work towards phrases and multiple registers
105 * Revision 1.41 1996/03/20 09:36:43 adam
106 * Function dict_lookup_grep got extra parameter, init_pos, which marks
107 * from which position in pattern approximate pattern matching should occur.
108 * Approximate pattern matching is used in relevance=re-2.
110 * Revision 1.40 1996/02/02 13:44:44 adam
111 * The public dictionary functions simply use char instead of Dict_char
112 * to represent search strings. Dict_char is used internally only.
114 * Revision 1.39 1996/01/03 16:22:13 quinn
115 * operator->roperator
117 * Revision 1.38 1995/12/11 09:12:55 adam
118 * The rec_get function returns NULL if record doesn't exist - will
119 * happen in the server if the result set records have been deleted since
120 * the creation of the set (i.e. the search).
121 * The server saves a result temporarily if it is 'volatile', i.e. the
122 * set is register dependent.
124 * Revision 1.37 1995/12/06 15:05:28 adam
125 * More verbose in count_set.
127 * Revision 1.36 1995/12/06 12:41:27 adam
128 * New command 'stat' for the index program.
129 * Filenames can be read from stdin by specifying '-'.
130 * Bug fix/enhancement of the transformation from terms to regular
131 * expressons in the search engine.
133 * Revision 1.35 1995/11/27 09:29:00 adam
134 * Bug fixes regarding conversion to regular expressions.
136 * Revision 1.34 1995/11/16 17:00:56 adam
137 * Better logging of rpn query.
139 * Revision 1.33 1995/11/01 13:58:28 quinn
140 * Moving data1 to yaz/retrieval
142 * Revision 1.32 1995/10/27 14:00:11 adam
143 * Implemented detection of database availability.
145 * Revision 1.31 1995/10/17 18:02:10 adam
146 * New feature: databases. Implemented as prefix to words in dictionary.
148 * Revision 1.30 1995/10/16 09:32:38 adam
149 * More work on relational op.
151 * Revision 1.29 1995/10/13 16:01:49 adam
154 * Revision 1.28 1995/10/13 12:26:43 adam
155 * Optimization of truncation.
157 * Revision 1.27 1995/10/12 17:07:22 adam
160 * Revision 1.26 1995/10/12 12:40:54 adam
161 * Bug fixes in rpn_prox.
163 * Revision 1.25 1995/10/10 13:59:24 adam
164 * Function rset_open changed its wflag parameter to general flags.
166 * Revision 1.24 1995/10/09 16:18:37 adam
167 * Function dict_lookup_grep got extra client data parameter.
169 * Revision 1.23 1995/10/06 16:33:37 adam
170 * Use attribute mappings.
172 * Revision 1.22 1995/10/06 15:07:39 adam
173 * Structure 'local-number' handled.
175 * Revision 1.21 1995/10/06 13:52:06 adam
176 * Bug fixes. Handler may abort further scanning.
178 * Revision 1.20 1995/10/06 11:06:33 adam
179 * Scan entries include 'occurrences' now.
181 * Revision 1.19 1995/10/06 10:43:56 adam
182 * Scan added. 'occurrences' in scan entries not set yet.
184 * Revision 1.18 1995/10/04 16:57:20 adam
185 * Key input and merge sort in one pass.
187 * Revision 1.17 1995/10/04 12:55:17 adam
188 * Bug fix in ranked search. Use=Any keys inserted.
190 * Revision 1.16 1995/10/02 16:24:40 adam
191 * Use attribute actually used in search requests.
193 * Revision 1.15 1995/10/02 15:18:52 adam
194 * New member in recRetrieveCtrl: diagnostic.
196 * Revision 1.14 1995/09/28 12:10:32 adam
197 * Bug fixes. Field prefix used in queries.
199 * Revision 1.13 1995/09/18 14:17:50 adam
202 * Revision 1.12 1995/09/15 14:45:21 adam
204 * Work on truncation.
206 * Revision 1.11 1995/09/14 11:53:27 adam
207 * First work on regular expressions/truncations.
209 * Revision 1.10 1995/09/11 15:23:26 adam
210 * More work on relevance search.
212 * Revision 1.9 1995/09/11 13:09:35 adam
213 * More work on relevance feedback.
215 * Revision 1.8 1995/09/08 14:52:27 adam
216 * Minor changes. Dictionary is lower case now.
218 * Revision 1.7 1995/09/07 13:58:36 adam
219 * New parameter: result-set file descriptor (RSFD) to support multiple
220 * positions within the same result-set.
221 * Boolean operators: and, or, not implemented.
222 * Result-set references.
224 * Revision 1.6 1995/09/06 16:11:18 adam
225 * Option: only one word key per file.
227 * Revision 1.5 1995/09/06 10:33:04 adam
228 * More work on present. Some log messages removed.
230 * Revision 1.4 1995/09/05 15:28:40 adam
231 * More work on search engine.
233 * Revision 1.3 1995/09/04 15:20:22 adam
236 * Revision 1.2 1995/09/04 12:33:43 adam
237 * Various cleanup. YAZ util used instead.
239 * Revision 1.1 1995/09/04 09:10:40 adam
240 * More work on index add/del/update.
241 * Merge sort implemented.
242 * Initial work on z39 server.
262 struct rpn_char_map_info {
267 static const char **rpn_char_map_handler (void *vp, const char **from, int len)
269 struct rpn_char_map_info *p = vp;
270 return zebra_maps_input (p->zm, p->reg_type, from, len);
273 static void rpn_char_map_prepare (ZServerInfo *zi, int reg_type,
274 struct rpn_char_map_info *map_info)
276 map_info->zm = zi->zebra_maps;
277 map_info->reg_type = reg_type;
278 dict_grep_cmap (zi->dict, map_info, rpn_char_map_handler);
285 Z_AttributesPlusTerm *zapt;
288 static int attr_find (AttrType *src, oid_value *attributeSetP)
290 while (src->major < src->zapt->num_attributes)
292 Z_AttributeElement *element;
294 element = src->zapt->attributeList[src->major];
295 if (src->type == *element->attributeType)
297 switch (element->which)
299 case Z_AttributeValue_numeric:
301 if (element->attributeSet && attributeSetP)
305 attrset = oid_getentbyoid (element->attributeSet);
306 *attributeSetP = attrset->value;
308 return *element->value.numeric;
310 case Z_AttributeValue_complex:
311 if (src->minor >= element->value.complex->num_list ||
312 element->value.complex->list[src->minor]->which !=
313 Z_StringOrNumeric_numeric)
316 if (element->attributeSet && attributeSetP)
320 attrset = oid_getentbyoid (element->attributeSet);
321 *attributeSetP = attrset->value;
323 return *element->value.complex->list[src->minor-1]->u.numeric;
333 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
353 static void add_isam_p (const char *info, struct grep_info *p)
355 if (p->isam_p_indx == p->isam_p_size)
357 ISAM_P *new_isam_p_buf;
362 p->isam_p_size = 2*p->isam_p_size + 100;
363 new_isam_p_buf = xmalloc (sizeof(*new_isam_p_buf) *
367 memcpy (new_isam_p_buf, p->isam_p_buf,
368 p->isam_p_indx * sizeof(*p->isam_p_buf));
369 xfree (p->isam_p_buf);
371 p->isam_p_buf = new_isam_p_buf;
374 new_term_no = xmalloc (sizeof(*new_term_no) *
378 memcpy (new_term_no, p->isam_p_buf,
379 p->isam_p_indx * sizeof(*p->term_no));
382 p->term_no = new_term_no;
385 assert (*info == sizeof(*p->isam_p_buf));
386 memcpy (p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
390 static int grep_handle (char *name, const char *info, void *p)
392 add_isam_p (info, p);
396 static int term_pre (ZebraMaps zebra_maps, int reg_type, const char **src,
397 const char *ct1, const char *ct2)
399 const char *s1, *s0 = *src;
402 /* skip white space */
405 if (ct1 && strchr (ct1, *s0))
407 if (ct2 && strchr (ct2, *s0))
410 map = zebra_maps_input (zebra_maps, reg_type, &s1, strlen(s1));
411 if (**map != *CHR_SPACE)
419 static int term_100 (ZebraMaps zebra_maps, int reg_type,
420 const char **src, char *dst, int space_split)
426 if (!term_pre (zebra_maps, reg_type, src, NULL, NULL))
432 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
433 if (space_split && **map == *CHR_SPACE)
437 if (!isalnum (*s1) && *s1 != '-')
447 static int term_101 (ZebraMaps zebra_maps, int reg_type,
448 const char **src, char *dst, int space_split)
454 if (!term_pre (zebra_maps, reg_type, src, "#", "#"))
468 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
469 if (space_split && **map == *CHR_SPACE)
485 static int term_103 (ZebraMaps zebra_maps, int reg_type, const char **src,
486 char *dst, int *errors, int space_split)
492 if (!term_pre (zebra_maps, reg_type, src, "^\\()[].*+?|", "("))
495 if (errors && *s0 == '+' && s0[1] && s0[2] == '+' && s0[3] &&
498 *errors = s0[1] - '0';
505 if (strchr ("^\\()[].*+?|-", *s0))
510 map = zebra_maps_input (zebra_maps, reg_type, &s0, strlen(s0));
511 if (**map == *CHR_SPACE)
526 static int term_102 (ZebraMaps zebra_maps, int reg_type, const char **src,
527 char *dst, int space_split)
529 return term_103 (zebra_maps, reg_type, src, dst, NULL, space_split);
532 /* gen_regular_rel - generate regular expression from relation
533 * val: border value (inclusive)
534 * islt: 1 if <=; 0 if >=.
536 static void gen_regular_rel (char *dst, int val, int islt)
543 logf (LOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
547 strcpy (dst, "(-[0-9]+|(");
555 strcpy (dst, "([0-9]+|-(");
556 dst_p = strlen (dst);
566 dst_p = strlen (dst);
567 sprintf (numstr, "%d", val);
568 for (w = strlen(numstr); --w >= 0; pos++)
587 strcpy (dst + dst_p, numstr);
588 dst_p = strlen(dst) - pos - 1;
616 for (i = 0; i<pos; i++)
629 for (i=1; i<pos; i++)
630 strcat (dst, "[0-9]?");
634 for (i = 0; i <= pos; i++)
635 strcat (dst, "[0-9]");
636 strcat (dst, "[0-9]*");
641 static int relational_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
642 const char **term_sub,
644 oid_value attributeSet,
645 struct grep_info *grep_info,
653 char *term_tmp = term_dict + strlen(term_dict);
655 attr_init (&relation, zapt, 2);
656 relation_value = attr_find (&relation, NULL);
658 logf (LOG_DEBUG, "relation value=%d", relation_value);
659 switch (relation_value)
662 if (!term_100 (zi->zebra_maps, reg_type, term_sub, term_tmp, 1))
664 term_value = atoi (term_tmp);
665 logf (LOG_DEBUG, "Relation <");
666 gen_regular_rel (term_tmp, term_value-1, 1);
669 if (!term_100 (zi->zebra_maps, reg_type, term_sub, term_tmp, 1))
671 term_value = atoi (term_tmp);
672 logf (LOG_DEBUG, "Relation <=");
673 gen_regular_rel (term_tmp, term_value, 1);
676 if (!term_100 (zi->zebra_maps, reg_type, term_sub, term_tmp, 1))
678 term_value = atoi (term_tmp);
679 logf (LOG_DEBUG, "Relation >=");
680 gen_regular_rel (term_tmp, term_value, 0);
683 if (!term_100 (zi->zebra_maps, reg_type, term_sub, term_tmp, 1))
685 term_value = atoi (term_tmp);
686 logf (LOG_DEBUG, "Relation >");
687 gen_regular_rel (term_tmp, term_value+1, 0);
692 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_tmp);
693 r = dict_lookup_grep (zi->dict, term_dict, 0, grep_info, max_pos,
696 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
697 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
701 static int field_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
702 const char **term_sub,
703 oid_value attributeSet, struct grep_info *grep_info,
704 int reg_type, int complete_flag,
705 int num_bases, char **basenames)
707 char term_dict[2*IT_MAX_WORD+2];
710 int truncation_value;
713 oid_value curAttributeSet = attributeSet;
715 struct rpn_char_map_info rcmi;
716 int space_split = complete_flag ? 0 : 1;
718 rpn_char_map_prepare (zi, reg_type, &rcmi);
719 attr_init (&use, zapt, 1);
720 use_value = attr_find (&use, &curAttributeSet);
721 logf (LOG_DEBUG, "field_term, use value %d", use_value);
722 attr_init (&truncation, zapt, 5);
723 truncation_value = attr_find (&truncation, NULL);
724 logf (LOG_DEBUG, "truncation value %d", truncation_value);
729 for (base_no = 0; base_no < num_bases; base_no++)
732 data1_local_attribute *local_attr;
733 int max_pos, prefix_len = 0;
736 if ((r=att_getentbyatt (zi, &attp, curAttributeSet, use_value)))
738 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
739 curAttributeSet, use_value, r);
746 if (zebTargetInfo_curDatabase (zi->zti, basenames[base_no]))
748 zi->errCode = 109; /* Database unavailable */
749 zi->errString = basenames[base_no];
752 for (local_attr = attp.local_attributes; local_attr;
753 local_attr = local_attr->next)
757 ord = zebTargetInfo_lookupSU (zi->zti, attp.attset_ordinal,
762 term_dict[prefix_len++] = '|';
764 term_dict[prefix_len++] = '(';
765 term_dict[prefix_len++] = 1;
766 term_dict[prefix_len++] = ord;
773 term_dict[prefix_len++] = ')';
774 term_dict[prefix_len++] = 1;
775 term_dict[prefix_len++] = reg_type;
776 logf (LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
777 term_dict[prefix_len] = '\0';
778 if (!relational_term (zi, zapt, &termp, term_dict,
779 attributeSet, grep_info, &max_pos, reg_type))
782 switch (truncation_value)
784 case -1: /* not specified */
785 case 100: /* do not truncate */
786 term_dict[j++] = '(';
787 if (!term_100 (zi->zebra_maps, reg_type,
788 &termp, term_dict + j, space_split))
790 strcat (term_dict, ")");
791 r = dict_lookup_grep (zi->dict, term_dict, 0, grep_info,
792 &max_pos, 0, grep_handle);
794 logf (LOG_WARN, "dict_lookup_grep err, trunc=none:%d", r);
796 case 1: /* right truncation */
797 term_dict[j++] = '(';
798 if (!term_100 (zi->zebra_maps, reg_type,
799 &termp, term_dict + j, space_split))
801 strcat (term_dict, ".*)");
802 dict_lookup_grep (zi->dict, term_dict, 0, grep_info,
803 &max_pos, 0, grep_handle);
805 case 2: /* left truncation */
806 case 3: /* left&right truncation */
809 case 101: /* process # in term */
810 term_dict[j++] = '(';
811 if (!term_101 (zi->zebra_maps, reg_type,
812 &termp, term_dict + j, space_split))
814 strcat (term_dict, ")");
815 r = dict_lookup_grep (zi->dict, term_dict, 0, grep_info,
816 &max_pos, 0, grep_handle);
818 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d", r);
820 case 102: /* Regexp-1 */
821 term_dict[j++] = '(';
822 if (!term_102 (zi->zebra_maps, reg_type,
823 &termp, term_dict + j, space_split))
825 strcat (term_dict, ")");
826 logf (LOG_DEBUG, "Regexp-1 tolerance=%d", r);
827 r = dict_lookup_grep (zi->dict, term_dict, 0, grep_info,
828 &max_pos, 0, grep_handle);
830 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
833 case 103: /* Regexp-2 */
835 term_dict[j++] = '(';
836 if (!term_103 (zi->zebra_maps, reg_type,
837 &termp, term_dict + j, &r, space_split))
839 strcat (term_dict, ")");
840 logf (LOG_DEBUG, "Regexp-2 tolerance=%d", r);
841 r = dict_lookup_grep (zi->dict, term_dict, r, grep_info,
842 &max_pos, 2, grep_handle);
844 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
851 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
855 static void trans_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
859 Z_Term *term = zapt->term;
861 sizez = term->u.general->len;
862 if (sizez > IT_MAX_WORD-1)
863 sizez = IT_MAX_WORD-1;
864 memcpy (termz, term->u.general->buf, sizez);
868 static void trans_scan_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
869 char *termz, int reg_type)
871 Z_Term *term = zapt->term;
873 const char *cp = (const char *) term->u.general->buf;
874 const char *cp_end = cp + term->u.general->len;
877 const char *space_map = NULL;
880 while ((len = (cp_end - cp)) > 0)
882 map = zebra_maps_input (zi->zebra_maps, reg_type, &cp, len);
883 if (**map == *CHR_SPACE)
888 for (src = space_map; *src; src++)
891 for (src = *map; *src; src++)
898 static RSET rpn_search_APT_relevance (ZServerInfo *zi,
899 Z_AttributesPlusTerm *zapt,
900 oid_value attributeSet,
901 int reg_type, int complete_flag,
902 int num_bases, char **basenames)
904 rset_relevance_parms parms;
905 char termz[IT_MAX_WORD+1];
906 const char *termp = termz;
907 struct grep_info grep_info;
912 parms.key_size = sizeof(struct it_key);
913 parms.max_rec = 1000;
914 parms.cmp = key_compare_it;
915 parms.get_pos = key_get_pos;
917 parms.isc = zi->isamc;
920 if (zapt->term->which != Z_Term_general)
925 trans_term (zi, zapt, termz);
928 grep_info.term_no = 0;
930 grep_info.isam_p_indx = 0;
931 grep_info.isam_p_size = 0;
932 grep_info.isam_p_buf = NULL;
935 r = field_term (zi, zapt, &termp, attributeSet, &grep_info,
936 reg_type, complete_flag, num_bases, basenames);
940 for (; term_index < grep_info.isam_p_indx; term_index++)
941 grep_info.term_no[term_index] = parms.no_terms;
945 parms.term_no = grep_info.term_no;
946 parms.isam_positions = grep_info.isam_p_buf;
947 parms.no_isam_positions = grep_info.isam_p_indx;
948 if (grep_info.isam_p_indx > 0)
949 result = rset_create (rset_kind_relevance, &parms);
951 result = rset_create (rset_kind_null, NULL);
953 xfree(grep_info.term_no);
955 xfree (grep_info.isam_p_buf);
959 static RSET rpn_proximity (ZServerInfo *zi, RSET rset1, RSET rset2,
961 int exclusion, int relation, int distance)
966 struct it_key buf1, buf2;
969 rset_temp_parms parms;
971 rsfd1 = rset_open (rset1, RSETF_READ|RSETF_SORT_SYSNO);
972 more1 = rset_read (rset1, rsfd1, &buf1);
974 rsfd2 = rset_open (rset2, RSETF_READ|RSETF_SORT_SYSNO);
975 more2 = rset_read (rset2, rsfd2, &buf2);
977 parms.key_size = sizeof (struct it_key);
978 parms.temp_path = res_get (zi->res, "setTmpDir");
979 result = rset_create (rset_kind_temp, &parms);
980 rsfd_result = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
982 logf (LOG_DEBUG, "rpn_proximity excl=%d ord=%d rel=%d dis=%d",
983 exclusion, ordered, relation, distance);
984 while (more1 && more2)
986 int cmp = key_compare_it (&buf1, &buf2);
988 more1 = rset_read (rset1, rsfd1, &buf1);
990 more2 = rset_read (rset2, rsfd2, &buf2);
993 int sysno = buf1.sysno;
997 seqno[n++] = buf1.seqno;
998 while ((more1 = rset_read (rset1, rsfd1, &buf1)) &&
1001 seqno[n++] = buf1.seqno;
1004 for (i = 0; i<n; i++)
1006 int diff = buf2.seqno - seqno[i];
1007 int excl = exclusion;
1008 if (!ordered && diff < 0)
1013 if (diff < distance)
1017 if (diff <= distance)
1021 if (diff == distance)
1025 if (diff >= distance)
1029 if (diff > distance)
1033 if (diff != distance)
1038 rset_write (result, rsfd_result, &buf2);
1040 } while ((more2 = rset_read (rset2, rsfd2, &buf2)) &&
1041 sysno == buf2.sysno);
1044 rset_close (result, rsfd_result);
1045 rset_close (rset1, rsfd1);
1046 rset_close (rset2, rsfd2);
1050 static RSET rpn_prox (ZServerInfo *zi, RSET *rset, int rset_no)
1055 struct it_key **buf;
1058 rset_temp_parms parms;
1060 rsfd = xmalloc (sizeof(*rsfd)*rset_no);
1061 more = xmalloc (sizeof(*more)*rset_no);
1062 buf = xmalloc (sizeof(*buf)*rset_no);
1064 for (i = 0; i<rset_no; i++)
1066 buf[i] = xmalloc (sizeof(**buf));
1067 rsfd[i] = rset_open (rset[i], RSETF_READ|RSETF_SORT_SYSNO);
1068 if (!(more[i] = rset_read (rset[i], rsfd[i], buf[i])))
1072 rset_close (rset[i], rsfd[i]);
1079 return rset_create (rset_kind_null, NULL);
1082 parms.key_size = sizeof (struct it_key);
1083 parms.temp_path = res_get (zi->res, "setTmpDir");
1084 result = rset_create (rset_kind_temp, &parms);
1085 rsfd_result = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
1089 for (i = 1; i<rset_no; i++)
1098 cmp = key_compare_it (buf[i], buf[i-1]);
1101 more[i-1] = rset_read (rset[i-1], rsfd[i-1], buf[i-1]);
1106 if (buf[i-1]->seqno+1 != buf[i]->seqno)
1108 more[i-1] = rset_read (rset[i-1], rsfd[i-1], buf[i-1]);
1114 more[i] = rset_read (rset[i], rsfd[i], buf[i]);
1120 rset_write (result, rsfd_result, buf[0]);
1121 more[0] = rset_read (*rset, *rsfd, *buf);
1125 for (i = 0; i<rset_no; i++)
1127 rset_close (rset[i], rsfd[i]);
1130 rset_close (result, rsfd_result);
1137 static RSET rpn_search_APT_phrase (ZServerInfo *zi,
1138 Z_AttributesPlusTerm *zapt,
1139 oid_value attributeSet,
1140 int reg_type, int complete_flag,
1141 int num_bases, char **basenames)
1143 char termz[IT_MAX_WORD+1];
1144 const char *termp = termz;
1145 RSET rset[60], result;
1146 int i, r, rset_no = 0;
1147 struct grep_info grep_info;
1149 if (zapt->term->which != Z_Term_general)
1154 trans_term (zi, zapt, termz);
1157 grep_info.term_no = 0;
1159 grep_info.isam_p_size = 0;
1160 grep_info.isam_p_buf = NULL;
1164 logf (LOG_DEBUG, "APT_phrase termp=%s", termp);
1165 grep_info.isam_p_indx = 0;
1166 r = field_term (zi, zapt, &termp, attributeSet, &grep_info,
1167 reg_type, complete_flag, num_bases, basenames);
1170 rset[rset_no] = rset_trunc (zi, grep_info.isam_p_buf,
1171 grep_info.isam_p_indx);
1172 assert (rset[rset_no]);
1173 if (++rset_no >= sizeof(rset)/sizeof(*rset))
1177 xfree(grep_info.term_no);
1179 xfree (grep_info.isam_p_buf);
1181 return rset_create (rset_kind_null, NULL);
1182 else if (rset_no == 1)
1184 result = rpn_prox (zi, rset, rset_no);
1185 for (i = 0; i<rset_no; i++)
1186 rset_delete (rset[i]);
1190 static RSET rpn_search_APT_local (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1191 oid_value attributeSet)
1196 rset_temp_parms parms;
1197 char termz[IT_MAX_WORD+1];
1199 if (zapt->term->which != Z_Term_general)
1204 parms.key_size = sizeof (struct it_key);
1205 parms.temp_path = res_get (zi->res, "setTmpDir");
1206 result = rset_create (rset_kind_temp, &parms);
1207 rsfd = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
1209 trans_term (zi, zapt, termz);
1211 key.sysno = atoi (termz);
1214 rset_write (result, rsfd, &key);
1215 rset_close (result, rsfd);
1219 static RSET rpn_search_APT (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1220 oid_value attributeSet,
1221 int num_bases, char **basenames)
1224 char *search_type = NULL;
1227 zebra_maps_attr (zi->zebra_maps, zapt, ®_type, &search_type,
1230 logf (LOG_DEBUG, "reg_type=%c", reg_type);
1231 logf (LOG_DEBUG, "complete_flag=%d", complete_flag);
1232 logf (LOG_DEBUG, "search_type=%s", search_type);
1233 if (!strcmp (search_type, "phrase"))
1235 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1236 reg_type, complete_flag,
1237 num_bases, basenames);
1239 else if (!strcmp (search_type, "ranked"))
1241 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1242 reg_type, complete_flag,
1243 num_bases, basenames);
1245 else if (!strcmp (search_type, "local"))
1247 return rpn_search_APT_local (zi, zapt, attributeSet);
1253 static RSET rpn_search_ref (ZServerInfo *zi, Z_ResultSetId *resultSetId)
1257 if (!(s = resultSetGet (zi, resultSetId)))
1258 return rset_create (rset_kind_null, NULL);
1262 static RSET rpn_search_structure (ZServerInfo *zi, Z_RPNStructure *zs,
1263 oid_value attributeSet,
1264 int num_bases, char **basenames)
1267 if (zs->which == Z_RPNStructure_complex)
1269 Z_Operator *zop = zs->u.complex->roperator;
1270 rset_bool_parms bool_parms;
1274 bool_parms.rset_l = rpn_search_structure (zi, zs->u.complex->s1,
1276 num_bases, basenames);
1277 if (bool_parms.rset_l == NULL)
1279 if (rset_is_ranked(bool_parms.rset_l))
1281 bool_parms.rset_r = rpn_search_structure (zi, zs->u.complex->s2,
1283 num_bases, basenames);
1284 if (bool_parms.rset_r == NULL)
1286 rset_delete (bool_parms.rset_l);
1289 if (rset_is_ranked(bool_parms.rset_r))
1291 bool_parms.key_size = sizeof(struct it_key);
1292 bool_parms.cmp = key_compare_it;
1296 case Z_Operator_and:
1297 r = rset_create (soft ? rset_kind_sand:rset_kind_and, &bool_parms);
1300 r = rset_create (soft ? rset_kind_sor:rset_kind_or, &bool_parms);
1302 case Z_Operator_and_not:
1303 r = rset_create (soft ? rset_kind_snot:rset_kind_not, &bool_parms);
1305 case Z_Operator_prox:
1306 if (zop->u.prox->which != Z_ProxCode_known)
1311 if (*zop->u.prox->proximityUnitCode != Z_ProxUnit_word)
1313 char *val = odr_malloc (zi->odr, 16);
1315 zi->errString = val;
1316 sprintf (val, "%d", *zop->u.prox->proximityUnitCode);
1319 r = rpn_proximity (zi, bool_parms.rset_l, bool_parms.rset_r,
1320 *zop->u.prox->ordered,
1321 (!zop->u.prox->exclusion ? 0 :
1322 *zop->u.prox->exclusion),
1323 *zop->u.prox->relationType,
1324 *zop->u.prox->distance);
1331 else if (zs->which == Z_RPNStructure_simple)
1333 if (zs->u.simple->which == Z_Operand_APT)
1335 logf (LOG_DEBUG, "rpn_search_APT");
1336 r = rpn_search_APT (zi, zs->u.simple->u.attributesPlusTerm,
1337 attributeSet, num_bases, basenames);
1339 else if (zs->u.simple->which == Z_Operand_resultSetId)
1341 logf (LOG_DEBUG, "rpn_search_ref");
1342 r = rpn_search_ref (zi, zs->u.simple->u.resultSetId);
1358 void count_set_save (ZServerInfo *zi, RSET *r, int *count)
1365 rset_temp_parms parms;
1366 int maxResultSetSize = atoi (res_get_def (zi->res,
1367 "maxResultSetSize", "400"));
1368 logf (LOG_DEBUG, "count_set_save");
1370 parms.key_size = sizeof(struct it_key);
1371 parms.temp_path = res_get (zi->res, "setTmpDir");
1372 w = rset_create (rset_kind_temp, &parms);
1373 wfd = rset_open (w, RSETF_WRITE|RSETF_SORT_SYSNO);
1374 rfd = rset_open (*r, RSETF_READ|RSETF_SORT_SYSNO);
1375 while (rset_read (*r, rfd, &key))
1377 if (key.sysno != psysno)
1379 if (*count < maxResultSetSize)
1380 rset_write (w, wfd, &key);
1386 rset_close (*r, rfd);
1388 rset_close (w, wfd);
1390 logf (LOG_DEBUG, "%d keys, %d distinct sysnos", kno, *count);
1393 static void count_set (RSET r, int *count)
1400 logf (LOG_DEBUG, "count_set");
1402 rfd = rset_open (r, RSETF_READ|RSETF_SORT_SYSNO);
1403 while (rset_read (r, rfd, &key))
1405 if (key.sysno != psysno)
1412 rset_close (r, rfd);
1413 logf (LOG_DEBUG, "%d keys, %d distinct sysnos", kno, *count);
1416 int rpn_search (ZServerInfo *zi,
1417 Z_RPNQuery *rpn, int num_bases, char **basenames,
1418 const char *setname, int *hits)
1422 oid_value attributeSet;
1427 zi->errString = NULL;
1429 attrset = oid_getentbyoid (rpn->attributeSetId);
1430 attributeSet = attrset->value;
1431 rset = rpn_search_structure (zi, rpn->RPNStructure, attributeSet,
1432 num_bases, basenames);
1435 if (rset_is_volatile(rset))
1436 count_set_save(zi, &rset,hits);
1438 count_set (rset, hits);
1439 resultSetAdd (zi, setname, 1, rset);
1441 logf (LOG_DEBUG, "search error: %d", zi->errCode);
1445 struct scan_info_entry {
1451 struct scan_info_entry *list;
1457 static int scan_handle (char *name, const char *info, int pos, void *client)
1459 int len_prefix, idx;
1460 struct scan_info *scan_info = client;
1462 len_prefix = strlen(scan_info->prefix);
1463 if (memcmp (name, scan_info->prefix, len_prefix))
1466 idx = scan_info->after - pos + scan_info->before;
1469 scan_info->list[idx].term = odr_malloc (scan_info->odr,
1470 strlen(name + len_prefix)+1);
1471 strcpy (scan_info->list[idx].term, name + len_prefix);
1472 assert (*info == sizeof(ISAM_P));
1473 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAM_P));
1478 static void scan_term_untrans (ZServerInfo *zi, int reg_type,
1479 char **dstp, const char *src)
1481 char *dst = odr_malloc (zi->odr, strlen(src)*2+1);
1486 const char *cp = zebra_maps_output (zi->zebra_maps, reg_type, &src);
1493 int rpn_scan (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1494 oid_value attributeset,
1495 int num_bases, char **basenames,
1496 int *position, int *num_entries, struct scan_entry **list,
1500 int pos = *position;
1501 int num = *num_entries;
1505 char termz[IT_MAX_WORD+20];
1508 struct scan_info *scan_info_array;
1509 struct scan_entry *glist;
1510 int ords[32], ord_no = 0;
1514 char *search_type = NULL;
1517 logf (LOG_DEBUG, "scan, position = %d, num = %d", pos, num);
1519 if (attributeset == VAL_NONE)
1520 attributeset = VAL_BIB1;
1522 attr_init (&use, zapt, 1);
1523 use_value = attr_find (&use, &attributeset);
1524 logf (LOG_DEBUG, "use value %d", use_value);
1526 if (zebra_maps_attr (zi->zebra_maps, zapt, ®_type, &search_type,
1533 if (use_value == -1)
1535 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
1539 data1_local_attribute *local_attr;
1541 if ((r=att_getentbyatt (zi, &attp, attributeset, use_value)))
1543 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
1544 attributeset, use_value);
1550 if (zebTargetInfo_curDatabase (zi->zti, basenames[base_no]))
1552 zi->errString = basenames[base_no];
1553 return zi->errCode = 109; /* Database unavailable */
1555 for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
1556 local_attr = local_attr->next)
1560 ord = zebTargetInfo_lookupSU (zi->zti, attp.attset_ordinal,
1563 ords[ord_no++] = ord;
1567 return zi->errCode = 113;
1570 scan_info_array = odr_malloc (zi->odr, ord_no * sizeof(*scan_info_array));
1571 for (i = 0; i < ord_no; i++)
1573 int j, prefix_len = 0;
1574 int before_tmp = before, after_tmp = after;
1575 struct scan_info *scan_info = scan_info_array + i;
1576 struct rpn_char_map_info rcmi;
1578 rpn_char_map_prepare (zi, reg_type, &rcmi);
1580 scan_info->before = before;
1581 scan_info->after = after;
1582 scan_info->odr = zi->odr;
1584 scan_info->list = odr_malloc (zi->odr, (before+after)*
1585 sizeof(*scan_info->list));
1586 for (j = 0; j<before+after; j++)
1587 scan_info->list[j].term = NULL;
1588 termz[prefix_len++] = ords[i];
1589 termz[prefix_len++] = reg_type;
1590 termz[prefix_len] = 0;
1591 strcpy (scan_info->prefix, termz);
1593 trans_scan_term (zi, zapt, termz+prefix_len, reg_type);
1595 dict_scan (zi->dict, termz, &before_tmp, &after_tmp, scan_info,
1598 glist = odr_malloc (zi->odr, (before+after)*sizeof(*glist));
1599 for (i = 0; i < ord_no; i++)
1602 *status = BEND_SCAN_SUCCESS;
1603 for (i = 0; i<after; i++)
1606 const char *mterm = NULL;
1610 for (j = 0; j < ord_no; j++)
1612 if (ptr[j] < before+after &&
1613 (tst=scan_info_array[j].list[ptr[j]].term) &&
1614 (!mterm || strcmp (tst, mterm) < 0))
1622 scan_term_untrans (zi, reg_type, &glist[i+before].term, mterm);
1623 rset = rset_trunc (zi, &scan_info_array[j0].list[ptr[j0]].isam_p, 1);
1626 for (j = j0+1; j<ord_no; j++)
1628 if (ptr[j] < before+after &&
1629 (tst=scan_info_array[j].list[ptr[j]].term) &&
1630 !strcmp (tst, mterm))
1632 rset_bool_parms bool_parms;
1636 rset_trunc (zi, &scan_info_array[j].list[ptr[j]].isam_p, 1);
1638 bool_parms.key_size = sizeof(struct it_key);
1639 bool_parms.cmp = key_compare_it;
1640 bool_parms.rset_l = rset;
1641 bool_parms.rset_r = rset2;
1643 rset = rset_create (rset_kind_or, &bool_parms);
1648 count_set (rset, &glist[i+before].occurrences);
1653 *num_entries -= (after-i);
1654 *status = BEND_SCAN_PARTIAL;
1657 for (i = 0; i<ord_no; i++)
1660 for (i = 0; i<before; i++)
1663 const char *mterm = NULL;
1667 for (j = 0; j <ord_no; j++)
1669 if (ptr[j] < before &&
1670 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
1671 (!mterm || strcmp (tst, mterm) > 0))
1680 scan_term_untrans (zi, reg_type, &glist[before-1-i].term, mterm);
1683 (zi, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1);
1687 for (j = j0+1; j<ord_no; j++)
1689 if (ptr[j] < before &&
1690 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
1691 !strcmp (tst, mterm))
1693 rset_bool_parms bool_parms;
1696 rset2 = rset_trunc (zi,
1697 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1);
1699 bool_parms.key_size = sizeof(struct it_key);
1700 bool_parms.cmp = key_compare_it;
1701 bool_parms.rset_l = rset;
1702 bool_parms.rset_r = rset2;
1704 rset = rset_create (rset_kind_or, &bool_parms);
1709 count_set (rset, &glist[before-1-i].occurrences);
1715 *status = BEND_SCAN_PARTIAL;
1719 *list = glist + i; /* list is set to first 'real' entry */
1721 logf (LOG_DEBUG, "position = %d, num_entries = %d",
1722 *position, *num_entries);
1724 logf (LOG_DEBUG, "scan error: %d", zi->errCode);