2 * Copyright (C) 1994-1997, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.67 1997-09-29 09:06:10 adam
8 * Removed one static var in order to make this module thread safe.
10 * Revision 1.66 1997/09/25 14:58:03 adam
13 * Revision 1.65 1997/09/22 12:39:06 adam
14 * Added get_pos method for the ranked result sets.
16 * Revision 1.64 1997/09/18 08:59:20 adam
17 * Extra generic handle for the character mapping routines.
19 * Revision 1.63 1997/09/17 12:19:18 adam
20 * Zebra version corresponds to YAZ version 1.4.
21 * Changed Zebra server so that it doesn't depend on global common_resource.
23 * Revision 1.62 1997/09/05 15:30:09 adam
24 * Changed prototype for chr_map_input - added const.
25 * Added support for C++, headers uses extern "C" for public definitions.
27 * Revision 1.61 1997/02/10 10:21:14 adam
28 * Bug fix: in search terms character (^) wasn't observed.
30 * Revision 1.60 1997/01/31 11:10:34 adam
31 * Bug fix: Leading and trailing white space weren't removed in scan tokens.
33 * Revision 1.59 1997/01/17 11:31:46 adam
34 * Bug fix: complete phrase search didn't work.
36 * Revision 1.58 1996/12/23 15:30:45 adam
38 * Bug fix: result sets weren't deleted after server shut down.
40 * Revision 1.57 1996/11/11 13:38:02 adam
41 * Added proximity support in search.
43 * Revision 1.56 1996/11/08 11:10:32 adam
44 * Buffers used during file match got bigger.
45 * Compressed ISAM support everywhere.
46 * Bug fixes regarding masking characters in queries.
47 * Redesigned Regexp-2 queries.
49 * Revision 1.55 1996/11/04 14:07:44 adam
50 * Moved truncation code to trunc.c.
52 * Revision 1.54 1996/10/29 14:09:52 adam
53 * Use of cisam system - enabled if setting isamc is 1.
55 * Revision 1.53 1996/06/26 09:21:43 adam
56 * Bug fix: local attribute set wasn't obeyed in scan.
58 * Revision 1.52 1996/06/17 14:26:20 adam
59 * Function gen_regular_rel changed to handle negative numbers.
61 * Revision 1.51 1996/06/11 10:54:15 quinn
64 * Revision 1.50 1996/06/07 08:51:53 adam
65 * Bug fix: Character mapping was broken (introducued by last revision).
67 * Revision 1.49 1996/06/04 10:18:11 adam
68 * Search/scan uses character mapping module.
70 * Revision 1.48 1996/05/28 15:15:01 adam
71 * Bug fix: Didn't handle unknown database correctly.
73 * Revision 1.47 1996/05/15 18:36:28 adam
74 * Function trans_term transforms unsearchable characters to blanks.
76 * Revision 1.46 1996/05/15 11:57:56 adam
77 * Fixed bug introduced by set/field mapping in search operations.
79 * Revision 1.45 1996/05/14 11:34:00 adam
80 * Scan support in multiple registers/databases.
82 * Revision 1.44 1996/05/14 06:16:44 adam
83 * Compact use/set bytes used in search service.
85 * Revision 1.43 1996/05/09 09:54:43 adam
86 * Server supports maps from one logical attributes to a list of physical
88 * The extraction process doesn't make space consuming 'any' keys.
90 * Revision 1.42 1996/05/09 07:28:56 quinn
91 * Work towards phrases and multiple registers
93 * Revision 1.41 1996/03/20 09:36:43 adam
94 * Function dict_lookup_grep got extra parameter, init_pos, which marks
95 * from which position in pattern approximate pattern matching should occur.
96 * Approximate pattern matching is used in relevance=re-2.
98 * Revision 1.40 1996/02/02 13:44:44 adam
99 * The public dictionary functions simply use char instead of Dict_char
100 * to represent search strings. Dict_char is used internally only.
102 * Revision 1.39 1996/01/03 16:22:13 quinn
103 * operator->roperator
105 * Revision 1.38 1995/12/11 09:12:55 adam
106 * The rec_get function returns NULL if record doesn't exist - will
107 * happen in the server if the result set records have been deleted since
108 * the creation of the set (i.e. the search).
109 * The server saves a result temporarily if it is 'volatile', i.e. the
110 * set is register dependent.
112 * Revision 1.37 1995/12/06 15:05:28 adam
113 * More verbose in count_set.
115 * Revision 1.36 1995/12/06 12:41:27 adam
116 * New command 'stat' for the index program.
117 * Filenames can be read from stdin by specifying '-'.
118 * Bug fix/enhancement of the transformation from terms to regular
119 * expressons in the search engine.
121 * Revision 1.35 1995/11/27 09:29:00 adam
122 * Bug fixes regarding conversion to regular expressions.
124 * Revision 1.34 1995/11/16 17:00:56 adam
125 * Better logging of rpn query.
127 * Revision 1.33 1995/11/01 13:58:28 quinn
128 * Moving data1 to yaz/retrieval
130 * Revision 1.32 1995/10/27 14:00:11 adam
131 * Implemented detection of database availability.
133 * Revision 1.31 1995/10/17 18:02:10 adam
134 * New feature: databases. Implemented as prefix to words in dictionary.
136 * Revision 1.30 1995/10/16 09:32:38 adam
137 * More work on relational op.
139 * Revision 1.29 1995/10/13 16:01:49 adam
142 * Revision 1.28 1995/10/13 12:26:43 adam
143 * Optimization of truncation.
145 * Revision 1.27 1995/10/12 17:07:22 adam
148 * Revision 1.26 1995/10/12 12:40:54 adam
149 * Bug fixes in rpn_prox.
151 * Revision 1.25 1995/10/10 13:59:24 adam
152 * Function rset_open changed its wflag parameter to general flags.
154 * Revision 1.24 1995/10/09 16:18:37 adam
155 * Function dict_lookup_grep got extra client data parameter.
157 * Revision 1.23 1995/10/06 16:33:37 adam
158 * Use attribute mappings.
160 * Revision 1.22 1995/10/06 15:07:39 adam
161 * Structure 'local-number' handled.
163 * Revision 1.21 1995/10/06 13:52:06 adam
164 * Bug fixes. Handler may abort further scanning.
166 * Revision 1.20 1995/10/06 11:06:33 adam
167 * Scan entries include 'occurrences' now.
169 * Revision 1.19 1995/10/06 10:43:56 adam
170 * Scan added. 'occurrences' in scan entries not set yet.
172 * Revision 1.18 1995/10/04 16:57:20 adam
173 * Key input and merge sort in one pass.
175 * Revision 1.17 1995/10/04 12:55:17 adam
176 * Bug fix in ranked search. Use=Any keys inserted.
178 * Revision 1.16 1995/10/02 16:24:40 adam
179 * Use attribute actually used in search requests.
181 * Revision 1.15 1995/10/02 15:18:52 adam
182 * New member in recRetrieveCtrl: diagnostic.
184 * Revision 1.14 1995/09/28 12:10:32 adam
185 * Bug fixes. Field prefix used in queries.
187 * Revision 1.13 1995/09/18 14:17:50 adam
190 * Revision 1.12 1995/09/15 14:45:21 adam
192 * Work on truncation.
194 * Revision 1.11 1995/09/14 11:53:27 adam
195 * First work on regular expressions/truncations.
197 * Revision 1.10 1995/09/11 15:23:26 adam
198 * More work on relevance search.
200 * Revision 1.9 1995/09/11 13:09:35 adam
201 * More work on relevance feedback.
203 * Revision 1.8 1995/09/08 14:52:27 adam
204 * Minor changes. Dictionary is lower case now.
206 * Revision 1.7 1995/09/07 13:58:36 adam
207 * New parameter: result-set file descriptor (RSFD) to support multiple
208 * positions within the same result-set.
209 * Boolean operators: and, or, not implemented.
210 * Result-set references.
212 * Revision 1.6 1995/09/06 16:11:18 adam
213 * Option: only one word key per file.
215 * Revision 1.5 1995/09/06 10:33:04 adam
216 * More work on present. Some log messages removed.
218 * Revision 1.4 1995/09/05 15:28:40 adam
219 * More work on search engine.
221 * Revision 1.3 1995/09/04 15:20:22 adam
224 * Revision 1.2 1995/09/04 12:33:43 adam
225 * Various cleanup. YAZ util used instead.
227 * Revision 1.1 1995/09/04 09:10:40 adam
228 * More work on index add/del/update.
229 * Merge sort implemented.
230 * Initial work on z39 server.
254 Z_AttributesPlusTerm *zapt;
257 static int attr_find (AttrType *src, oid_value *attributeSetP)
259 while (src->major < src->zapt->num_attributes)
261 Z_AttributeElement *element;
263 element = src->zapt->attributeList[src->major];
264 if (src->type == *element->attributeType)
266 switch (element->which)
268 case Z_AttributeValue_numeric:
270 if (element->attributeSet && attributeSetP)
274 attrset = oid_getentbyoid (element->attributeSet);
275 *attributeSetP = attrset->value;
277 return *element->value.numeric;
279 case Z_AttributeValue_complex:
280 if (src->minor >= element->value.complex->num_list ||
281 element->value.complex->list[src->minor]->which !=
282 Z_StringOrNumeric_numeric)
285 if (element->attributeSet && attributeSetP)
289 attrset = oid_getentbyoid (element->attributeSet);
290 *attributeSetP = attrset->value;
292 return *element->value.complex->list[src->minor-1]->u.numeric;
302 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
322 static void add_isam_p (const char *info, struct grep_info *p)
324 if (p->isam_p_indx == p->isam_p_size)
326 ISAM_P *new_isam_p_buf;
331 p->isam_p_size = 2*p->isam_p_size + 100;
332 new_isam_p_buf = xmalloc (sizeof(*new_isam_p_buf) *
336 memcpy (new_isam_p_buf, p->isam_p_buf,
337 p->isam_p_indx * sizeof(*p->isam_p_buf));
338 xfree (p->isam_p_buf);
340 p->isam_p_buf = new_isam_p_buf;
343 new_term_no = xmalloc (sizeof(*new_term_no) *
347 memcpy (new_term_no, p->isam_p_buf,
348 p->isam_p_indx * sizeof(*p->term_no));
351 p->term_no = new_term_no;
354 assert (*info == sizeof(*p->isam_p_buf));
355 memcpy (p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
359 static int grep_handle (char *name, const char *info, void *p)
361 add_isam_p (info, p);
365 static int term_pre (const char **src, const char *ct1, const char *ct2)
367 const char *s1, *s0 = *src;
370 /* skip white space */
373 if (ct1 && strchr (ct1, *s0))
375 if (ct2 && strchr (ct2, *s0))
378 map = map_chrs_input (0, &s1, strlen(s1));
379 if (**map != *CHR_SPACE)
387 static int term_100 (const char **src, char *dst, int space_split)
393 if (!term_pre (src, NULL, NULL))
399 map = map_chrs_input (0, &s0, strlen(s0));
400 if (space_split && **map == *CHR_SPACE)
414 static int term_101 (const char **src, char *dst, int space_split)
420 if (!term_pre (src, "#", "#"))
434 map = map_chrs_input (0, &s0, strlen(s0));
435 if (space_split && **map == *CHR_SPACE)
451 static int term_103 (const char **src, char *dst, int *errors, int space_split)
457 if (!term_pre (src, "^\\()[].*+?|", "("))
460 if (errors && *s0 == '+' && s0[1] && s0[2] == '+' && s0[3] &&
463 *errors = s0[1] - '0';
470 if (strchr ("^\\()[].*+?|-", *s0))
475 map = map_chrs_input (0, &s0, strlen(s0));
476 if (**map == *CHR_SPACE)
491 static int term_102 (const char **src, char *dst, int space_split)
493 return term_103 (src, dst, NULL, space_split);
496 /* gen_regular_rel - generate regular expression from relation
497 * val: border value (inclusive)
498 * islt: 1 if <=; 0 if >=.
500 static void gen_regular_rel (char *dst, int val, int islt)
507 logf (LOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
511 strcpy (dst, "(-[0-9]+|");
519 strcpy (dst, "([0-9]+|-");
520 dst_p = strlen (dst);
530 dst_p = strlen (dst);
531 sprintf (numstr, "%d", val);
532 for (w = strlen(numstr); --w >= 0; pos++)
551 strcpy (dst + dst_p, numstr);
552 dst_p = strlen(dst) - pos - 1;
580 for (i = 0; i<pos; i++)
593 for (i=1; i<pos; i++)
594 strcat (dst, "[0-9]?");
598 for (i = 0; i <= pos; i++)
599 strcat (dst, "[0-9]");
600 strcat (dst, "[0-9]*");
605 static int relational_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
606 const char **term_sub,
608 oid_value attributeSet,
609 struct grep_info *grep_info,
617 attr_init (&relation, zapt, 2);
618 relation_value = attr_find (&relation, NULL);
620 switch (relation_value)
623 if (!term_100 (term_sub, term_dict, 1))
625 term_value = atoi (term_dict);
628 logf (LOG_DEBUG, "Relation <");
629 gen_regular_rel (term_dict + strlen(term_dict), term_value-1, 1);
632 if (!term_100 (term_sub, term_dict, 1))
634 term_value = atoi (term_dict);
637 logf (LOG_DEBUG, "Relation <=");
638 gen_regular_rel (term_dict + strlen(term_dict), term_value, 1);
641 if (!term_100 (term_sub, term_dict, 1))
643 term_value = atoi (term_dict);
646 logf (LOG_DEBUG, "Relation >=");
647 gen_regular_rel (term_dict + strlen(term_dict), term_value, 0);
650 if (!term_100 (term_sub, term_dict, 1))
652 term_value = atoi (term_dict);
655 logf (LOG_DEBUG, "Relation >");
656 gen_regular_rel (term_dict + strlen(term_dict), term_value+1, 0);
661 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_dict);
662 r = dict_lookup_grep (zi->dict, term_dict, 0, grep_info, max_pos,
665 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
666 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
670 static int field_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
671 const char **term_sub, int regType,
672 oid_value attributeSet, struct grep_info *grep_info,
673 int num_bases, char **basenames, int space_split)
675 char term_dict[2*IT_MAX_WORD+2];
678 int truncation_value;
681 oid_value curAttributeSet = attributeSet;
684 attr_init (&use, zapt, 1);
685 use_value = attr_find (&use, &curAttributeSet);
686 logf (LOG_DEBUG, "field_term, use value %d", use_value);
687 attr_init (&truncation, zapt, 5);
688 truncation_value = attr_find (&truncation, NULL);
689 logf (LOG_DEBUG, "truncation value %d", truncation_value);
694 for (base_no = 0; base_no < num_bases; base_no++)
697 data1_local_attribute *local_attr;
698 int max_pos, prefix_len = 0;
701 if (!att_getentbyatt (zi, &attp, curAttributeSet, use_value))
703 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
704 curAttributeSet, use_value);
708 if (zebTargetInfo_curDatabase (zi->zti, basenames[base_no]))
710 zi->errCode = 109; /* Database unavailable */
711 zi->errString = basenames[base_no];
714 for (local_attr = attp.local_attributes; local_attr;
715 local_attr = local_attr->next)
719 ord = zebTargetInfo_lookupSU (zi->zti, attp.attset_ordinal,
724 term_dict[prefix_len++] = '|';
726 term_dict[prefix_len++] = '(';
727 term_dict[prefix_len++] = 1;
728 term_dict[prefix_len++] = ord;
735 term_dict[prefix_len++] = ')';
736 term_dict[prefix_len++] = 1;
737 term_dict[prefix_len++] = regType;
738 term_dict[prefix_len] = '\0';
739 if (!relational_term (zi, zapt, &termp, term_dict,
740 attributeSet, grep_info, &max_pos))
743 switch (truncation_value)
745 case -1: /* not specified */
746 case 100: /* do not truncate */
747 term_dict[j++] = '(';
748 if (!term_100 (&termp, term_dict + j, space_split))
750 strcat (term_dict, ")");
751 r = dict_lookup_grep (zi->dict, term_dict, 0, grep_info,
752 &max_pos, 0, grep_handle);
754 logf (LOG_WARN, "dict_lookup_grep err, trunc=none:%d", r);
756 case 1: /* right truncation */
757 term_dict[j++] = '(';
758 if (!term_100 (&termp, term_dict + j, space_split))
760 strcat (term_dict, ".*)");
761 dict_lookup_grep (zi->dict, term_dict, 0, grep_info,
762 &max_pos, 0, grep_handle);
764 case 2: /* left truncation */
765 case 3: /* left&right truncation */
768 case 101: /* process # in term */
769 term_dict[j++] = '(';
770 if (!term_101 (&termp, term_dict + j, space_split))
772 strcat (term_dict, ")");
773 r = dict_lookup_grep (zi->dict, term_dict, 0, grep_info,
774 &max_pos, 0, grep_handle);
776 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d", r);
778 case 102: /* Regexp-1 */
779 term_dict[j++] = '(';
780 if (!term_102 (&termp, term_dict + j, space_split))
782 strcat (term_dict, ")");
783 logf (LOG_DEBUG, "Regexp-1 tolerance=%d", r);
784 r = dict_lookup_grep (zi->dict, term_dict, 0, grep_info,
785 &max_pos, 0, grep_handle);
787 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
790 case 103: /* Regexp-1 */
792 term_dict[j++] = '(';
793 if (!term_103 (&termp, term_dict + j, &r, space_split))
795 strcat (term_dict, ")");
796 logf (LOG_DEBUG, "Regexp-2 tolerance=%d", r);
797 r = dict_lookup_grep (zi->dict, term_dict, r, grep_info,
798 &max_pos, 2, grep_handle);
800 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
807 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
811 static void trans_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
815 Z_Term *term = zapt->term;
817 sizez = term->u.general->len;
818 if (sizez > IT_MAX_WORD-1)
819 sizez = IT_MAX_WORD-1;
820 memcpy (termz, term->u.general->buf, sizez);
824 static void trans_scan_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
827 Z_Term *term = zapt->term;
829 const char *cp = (const char *) term->u.general->buf;
830 const char *cp_end = cp + term->u.general->len;
833 const char *space_map = NULL;
836 while ((len = (cp_end - cp)) > 0)
838 map = map_chrs_input (0, &cp, len);
839 if (**map == *CHR_SPACE)
844 for (src = space_map; *src; src++)
847 for (src = *map; *src; src++)
854 static RSET rpn_search_APT_relevance (ZServerInfo *zi,
855 Z_AttributesPlusTerm *zapt,
856 oid_value attributeSet,
857 int num_bases, char **basenames)
859 rset_relevance_parms parms;
860 char termz[IT_MAX_WORD+1];
861 const char *termp = termz;
862 struct grep_info grep_info;
867 parms.key_size = sizeof(struct it_key);
868 parms.max_rec = 1000;
869 parms.cmp = key_compare_it;
870 parms.get_pos = key_get_pos;
872 parms.isc = zi->isamc;
875 if (zapt->term->which != Z_Term_general)
880 trans_term (zi, zapt, termz);
883 grep_info.term_no = 0;
885 grep_info.isam_p_indx = 0;
886 grep_info.isam_p_size = 0;
887 grep_info.isam_p_buf = NULL;
890 r = field_term (zi, zapt, &termp, 'w', attributeSet, &grep_info,
891 num_bases, basenames, 1);
895 for (; term_index < grep_info.isam_p_indx; term_index++)
896 grep_info.term_no[term_index] = parms.no_terms;
900 parms.term_no = grep_info.term_no;
901 parms.isam_positions = grep_info.isam_p_buf;
902 parms.no_isam_positions = grep_info.isam_p_indx;
903 if (grep_info.isam_p_indx > 0)
904 result = rset_create (rset_kind_relevance, &parms);
906 result = rset_create (rset_kind_null, NULL);
908 xfree(grep_info.term_no);
910 xfree (grep_info.isam_p_buf);
914 static RSET rpn_search_APT_cphrase (ZServerInfo *zi,
915 Z_AttributesPlusTerm *zapt,
916 oid_value attributeSet,
917 int num_bases, char **basenames)
919 char termz[IT_MAX_WORD+1];
920 struct grep_info grep_info;
922 const char *termp = termz;
925 if (zapt->term->which != Z_Term_general)
930 trans_term (zi, zapt, termz);
933 grep_info.term_no = 0;
935 grep_info.isam_p_indx = 0;
936 grep_info.isam_p_size = 0;
937 grep_info.isam_p_buf = NULL;
939 r = field_term (zi, zapt, &termp, 'p', attributeSet, &grep_info,
940 num_bases, basenames, 0);
941 result = rset_trunc (zi, grep_info.isam_p_buf, grep_info.isam_p_indx);
943 xfree(grep_info.term_no);
945 xfree (grep_info.isam_p_buf);
949 static RSET rpn_proximity (ZServerInfo *zi, RSET rset1, RSET rset2,
951 int exclusion, int relation, int distance)
956 struct it_key buf1, buf2;
959 rset_temp_parms parms;
961 rsfd1 = rset_open (rset1, RSETF_READ|RSETF_SORT_SYSNO);
962 more1 = rset_read (rset1, rsfd1, &buf1);
964 rsfd2 = rset_open (rset2, RSETF_READ|RSETF_SORT_SYSNO);
965 more2 = rset_read (rset2, rsfd2, &buf2);
967 parms.key_size = sizeof (struct it_key);
968 parms.temp_path = res_get (zi->res, "setTmpDir");
969 result = rset_create (rset_kind_temp, &parms);
970 rsfd_result = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
972 logf (LOG_DEBUG, "rpn_proximity excl=%d ord=%d rel=%d dis=%d",
973 exclusion, ordered, relation, distance);
974 while (more1 && more2)
976 int cmp = key_compare_it (&buf1, &buf2);
978 more1 = rset_read (rset1, rsfd1, &buf1);
980 more2 = rset_read (rset2, rsfd2, &buf2);
983 int sysno = buf1.sysno;
987 seqno[n++] = buf1.seqno;
988 while ((more1 = rset_read (rset1, rsfd1, &buf1)) &&
991 seqno[n++] = buf1.seqno;
994 for (i = 0; i<n; i++)
996 int diff = buf2.seqno - seqno[i];
997 int excl = exclusion;
998 if (!ordered && diff < 0)
1003 if (diff < distance)
1007 if (diff <= distance)
1011 if (diff == distance)
1015 if (diff >= distance)
1019 if (diff > distance)
1023 if (diff != distance)
1028 rset_write (result, rsfd_result, &buf2);
1030 } while ((more2 = rset_read (rset2, rsfd2, &buf2)) &&
1031 sysno == buf2.sysno);
1034 rset_close (result, rsfd_result);
1035 rset_close (rset1, rsfd1);
1036 rset_close (rset2, rsfd2);
1040 static RSET rpn_prox (ZServerInfo *zi, RSET *rset, int rset_no)
1045 struct it_key **buf;
1048 rset_temp_parms parms;
1050 rsfd = xmalloc (sizeof(*rsfd)*rset_no);
1051 more = xmalloc (sizeof(*more)*rset_no);
1052 buf = xmalloc (sizeof(*buf)*rset_no);
1054 for (i = 0; i<rset_no; i++)
1056 buf[i] = xmalloc (sizeof(**buf));
1057 rsfd[i] = rset_open (rset[i], RSETF_READ|RSETF_SORT_SYSNO);
1058 if (!(more[i] = rset_read (rset[i], rsfd[i], buf[i])))
1062 rset_close (rset[i], rsfd[i]);
1069 return rset_create (rset_kind_null, NULL);
1072 parms.key_size = sizeof (struct it_key);
1073 parms.temp_path = res_get (zi->res, "setTmpDir");
1074 result = rset_create (rset_kind_temp, &parms);
1075 rsfd_result = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
1079 for (i = 1; i<rset_no; i++)
1088 cmp = key_compare_it (buf[i], buf[i-1]);
1091 more[i-1] = rset_read (rset[i-1], rsfd[i-1], buf[i-1]);
1096 if (buf[i-1]->seqno+1 != buf[i]->seqno)
1098 more[i-1] = rset_read (rset[i-1], rsfd[i-1], buf[i-1]);
1104 more[i] = rset_read (rset[i], rsfd[i], buf[i]);
1110 rset_write (result, rsfd_result, buf[0]);
1111 more[0] = rset_read (*rset, *rsfd, *buf);
1115 for (i = 0; i<rset_no; i++)
1117 rset_close (rset[i], rsfd[i]);
1120 rset_close (result, rsfd_result);
1127 static RSET rpn_search_APT_phrase (ZServerInfo *zi,
1128 Z_AttributesPlusTerm *zapt,
1129 oid_value attributeSet,
1130 int num_bases, char **basenames)
1132 char termz[IT_MAX_WORD+1];
1133 const char *termp = termz;
1134 RSET rset[60], result;
1135 int i, r, rset_no = 0;
1136 struct grep_info grep_info;
1138 if (zapt->term->which != Z_Term_general)
1143 trans_term (zi, zapt, termz);
1146 grep_info.term_no = 0;
1148 grep_info.isam_p_size = 0;
1149 grep_info.isam_p_buf = NULL;
1153 grep_info.isam_p_indx = 0;
1154 r = field_term (zi, zapt, &termp, 'w', attributeSet, &grep_info,
1155 num_bases, basenames, 1);
1158 rset[rset_no] = rset_trunc (zi, grep_info.isam_p_buf,
1159 grep_info.isam_p_indx);
1160 assert (rset[rset_no]);
1161 if (++rset_no >= sizeof(rset)/sizeof(*rset))
1165 xfree(grep_info.term_no);
1167 xfree (grep_info.isam_p_buf);
1169 return rset_create (rset_kind_null, NULL);
1170 else if (rset_no == 1)
1172 result = rpn_prox (zi, rset, rset_no);
1173 for (i = 0; i<rset_no; i++)
1174 rset_delete (rset[i]);
1178 static RSET rpn_search_APT_local (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1179 oid_value attributeSet)
1184 rset_temp_parms parms;
1185 char termz[IT_MAX_WORD+1];
1187 if (zapt->term->which != Z_Term_general)
1192 parms.key_size = sizeof (struct it_key);
1193 parms.temp_path = res_get (zi->res, "setTmpDir");
1194 result = rset_create (rset_kind_temp, &parms);
1195 rsfd = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
1197 trans_term (zi, zapt, termz);
1199 key.sysno = atoi (termz);
1202 rset_write (result, rsfd, &key);
1203 rset_close (result, rsfd);
1207 static RSET rpn_search_APT (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1208 oid_value attributeSet,
1209 int num_bases, char **basenames)
1213 AttrType completeness;
1214 int relation_value, structure_value, completeness_value;
1216 attr_init (&relation, zapt, 2);
1217 attr_init (&structure, zapt, 4);
1218 attr_init (&completeness, zapt, 6);
1220 relation_value = attr_find (&relation, NULL);
1221 structure_value = attr_find (&structure, NULL);
1222 completeness_value = attr_find (&completeness, NULL);
1223 switch (structure_value)
1226 if (relation_value == 102) /* relevance relation */
1227 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1228 num_bases, basenames);
1229 if (completeness_value == 2 || completeness_value == 3)
1230 return rpn_search_APT_cphrase (zi, zapt, attributeSet,
1231 num_bases, basenames);
1232 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1233 num_bases, basenames);
1234 case 1: /* phrase */
1235 if (relation_value == 102) /* relevance relation */
1236 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1237 num_bases, basenames);
1238 if (completeness_value == 2 || completeness_value == 3)
1239 return rpn_search_APT_cphrase (zi, zapt, attributeSet,
1240 num_bases, basenames);
1241 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1242 num_bases, basenames);
1245 if (relation_value == 102) /* relevance relation */
1246 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1247 num_bases, basenames);
1248 if (completeness_value == 2 || completeness_value == 3)
1249 return rpn_search_APT_cphrase (zi, zapt, attributeSet,
1250 num_bases, basenames);
1251 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1252 num_bases, basenames);
1257 case 5: /* date - normalized */
1259 case 6: /* word list */
1260 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1261 num_bases, basenames);
1262 case 100: /* date - un-normalized */
1264 case 101: /* name - normalized */
1266 case 102: /* date - un-normalized */
1268 case 103: /* structure */
1272 case 105: /* free-form-text */
1273 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1274 num_bases, basenames);
1275 case 106: /* document-text */
1276 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1277 num_bases, basenames);
1278 case 107: /* local-number */
1279 return rpn_search_APT_local (zi, zapt, attributeSet);
1280 case 108: /* string */
1281 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1282 num_bases, basenames);
1283 case 109: /* numeric string */
1290 static RSET rpn_search_ref (ZServerInfo *zi, Z_ResultSetId *resultSetId)
1294 if (!(s = resultSetGet (zi, resultSetId)))
1295 return rset_create (rset_kind_null, NULL);
1299 static RSET rpn_search_structure (ZServerInfo *zi, Z_RPNStructure *zs,
1300 oid_value attributeSet,
1301 int num_bases, char **basenames)
1304 if (zs->which == Z_RPNStructure_complex)
1306 Z_Operator *zop = zs->u.complex->roperator;
1307 rset_bool_parms bool_parms;
1311 bool_parms.rset_l = rpn_search_structure (zi, zs->u.complex->s1,
1313 num_bases, basenames);
1314 if (bool_parms.rset_l == NULL)
1316 if (rset_is_ranked(bool_parms.rset_l))
1318 bool_parms.rset_r = rpn_search_structure (zi, zs->u.complex->s2,
1320 num_bases, basenames);
1321 if (bool_parms.rset_r == NULL)
1323 rset_delete (bool_parms.rset_l);
1326 if (rset_is_ranked(bool_parms.rset_r))
1328 bool_parms.key_size = sizeof(struct it_key);
1329 bool_parms.cmp = key_compare_it;
1333 case Z_Operator_and:
1334 r = rset_create (soft ? rset_kind_sand:rset_kind_and, &bool_parms);
1337 r = rset_create (soft ? rset_kind_sor:rset_kind_or, &bool_parms);
1339 case Z_Operator_and_not:
1340 r = rset_create (soft ? rset_kind_snot:rset_kind_not, &bool_parms);
1342 case Z_Operator_prox:
1343 if (zop->u.prox->which != Z_ProxCode_known)
1348 if (*zop->u.prox->proximityUnitCode != Z_ProxUnit_word)
1350 char *val = odr_malloc (zi->odr, 16);
1352 zi->errString = val;
1353 sprintf (val, "%d", *zop->u.prox->proximityUnitCode);
1356 r = rpn_proximity (zi, bool_parms.rset_l, bool_parms.rset_r,
1357 *zop->u.prox->ordered,
1358 (!zop->u.prox->exclusion ? 0 :
1359 *zop->u.prox->exclusion),
1360 *zop->u.prox->relationType,
1361 *zop->u.prox->distance);
1368 else if (zs->which == Z_RPNStructure_simple)
1370 if (zs->u.simple->which == Z_Operand_APT)
1372 logf (LOG_DEBUG, "rpn_search_APT");
1373 r = rpn_search_APT (zi, zs->u.simple->u.attributesPlusTerm,
1374 attributeSet, num_bases, basenames);
1376 else if (zs->u.simple->which == Z_Operand_resultSetId)
1378 logf (LOG_DEBUG, "rpn_search_ref");
1379 r = rpn_search_ref (zi, zs->u.simple->u.resultSetId);
1395 void count_set_save (ZServerInfo *zi, RSET *r, int *count)
1402 rset_temp_parms parms;
1403 int maxResultSetSize = atoi (res_get_def (zi->res,
1404 "maxResultSetSize", "400"));
1405 logf (LOG_DEBUG, "count_set_save");
1407 parms.key_size = sizeof(struct it_key);
1408 parms.temp_path = res_get (zi->res, "setTmpDir");
1409 w = rset_create (rset_kind_temp, &parms);
1410 wfd = rset_open (w, RSETF_WRITE|RSETF_SORT_SYSNO);
1411 rfd = rset_open (*r, RSETF_READ|RSETF_SORT_SYSNO);
1412 while (rset_read (*r, rfd, &key))
1414 if (key.sysno != psysno)
1416 if (*count < maxResultSetSize)
1417 rset_write (w, wfd, &key);
1423 rset_close (*r, rfd);
1425 rset_close (w, wfd);
1427 logf (LOG_DEBUG, "%d keys, %d distinct sysnos", kno, *count);
1430 static void count_set (RSET r, int *count)
1437 logf (LOG_DEBUG, "count_set");
1439 rfd = rset_open (r, RSETF_READ|RSETF_SORT_SYSNO);
1440 while (rset_read (r, rfd, &key))
1442 if (key.sysno != psysno)
1449 rset_close (r, rfd);
1450 logf (LOG_DEBUG, "%d keys, %d distinct sysnos", kno, *count);
1453 int rpn_search (ZServerInfo *zi,
1454 Z_RPNQuery *rpn, int num_bases, char **basenames,
1455 const char *setname, int *hits)
1459 oid_value attributeSet;
1461 dict_grep_cmap (zi->dict, 0, map_chrs_input);
1465 zi->errString = NULL;
1467 attrset = oid_getentbyoid (rpn->attributeSetId);
1468 attributeSet = attrset->value;
1469 rset = rpn_search_structure (zi, rpn->RPNStructure, attributeSet,
1470 num_bases, basenames);
1473 if (rset_is_volatile(rset))
1474 count_set_save(zi, &rset,hits);
1476 count_set (rset, hits);
1477 resultSetAdd (zi, setname, 1, rset);
1479 logf (LOG_DEBUG, "search error: %d", zi->errCode);
1483 struct scan_info_entry {
1489 struct scan_info_entry *list;
1495 static int scan_handle (char *name, const char *info, int pos, void *client)
1497 int len_prefix, idx;
1498 struct scan_info *scan_info = client;
1500 len_prefix = strlen(scan_info->prefix);
1501 if (memcmp (name, scan_info->prefix, len_prefix))
1504 idx = scan_info->after - pos + scan_info->before;
1507 scan_info->list[idx].term = odr_malloc (scan_info->odr,
1508 strlen(name + len_prefix)+1);
1509 strcpy (scan_info->list[idx].term, name + len_prefix);
1510 assert (*info == sizeof(ISAM_P));
1511 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAM_P));
1516 static void scan_term_untrans (ODR odr, char **dstp, const char *src)
1518 char *dst = odr_malloc (odr, strlen(src)*2+1);
1523 const char *cp = map_chrs_output (&src);
1530 int rpn_scan (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1531 oid_value attributeset,
1532 int num_bases, char **basenames,
1533 int *position, int *num_entries, struct scan_entry **list,
1537 int pos = *position;
1538 int num = *num_entries;
1542 char termz[IT_MAX_WORD+20];
1545 AttrType completeness;
1546 int completeness_value;
1547 struct scan_info *scan_info_array;
1548 struct scan_entry *glist;
1549 int ords[32], ord_no = 0;
1552 logf (LOG_DEBUG, "scan, position = %d, num = %d", pos, num);
1554 if (attributeset == VAL_NONE)
1555 attributeset = VAL_BIB1;
1557 attr_init (&use, zapt, 1);
1558 use_value = attr_find (&use, &attributeset);
1559 logf (LOG_DEBUG, "use value %d", use_value);
1561 attr_init (&completeness, zapt, 6);
1562 completeness_value = attr_find (&completeness, NULL);
1563 logf (LOG_DEBUG, "completeness value %d", completeness_value);
1565 if (use_value == -1)
1567 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
1570 data1_local_attribute *local_attr;
1572 if (!att_getentbyatt (zi, &attp, attributeset, use_value))
1574 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
1575 attributeset, use_value);
1576 return zi->errCode = 114;
1578 if (zebTargetInfo_curDatabase (zi->zti, basenames[base_no]))
1580 zi->errString = basenames[base_no];
1581 return zi->errCode = 109; /* Database unavailable */
1583 for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
1584 local_attr = local_attr->next)
1588 ord = zebTargetInfo_lookupSU (zi->zti, attp.attset_ordinal,
1591 ords[ord_no++] = ord;
1595 return zi->errCode = 113;
1598 scan_info_array = odr_malloc (zi->odr, ord_no * sizeof(*scan_info_array));
1599 for (i = 0; i < ord_no; i++)
1601 int j, prefix_len = 0;
1602 int before_tmp = before, after_tmp = after;
1603 struct scan_info *scan_info = scan_info_array + i;
1605 scan_info->before = before;
1606 scan_info->after = after;
1607 scan_info->odr = zi->odr;
1609 scan_info->list = odr_malloc (zi->odr, (before+after)*
1610 sizeof(*scan_info->list));
1611 for (j = 0; j<before+after; j++)
1612 scan_info->list[j].term = NULL;
1613 termz[prefix_len++] = ords[i];
1614 termz[prefix_len++] =
1615 (completeness_value==2 || completeness_value==3) ? 'p': 'w';
1616 termz[prefix_len] = 0;
1617 strcpy (scan_info->prefix, termz);
1619 trans_scan_term (zi, zapt, termz+prefix_len);
1621 dict_scan (zi->dict, termz, &before_tmp, &after_tmp, scan_info,
1624 glist = odr_malloc (zi->odr, (before+after)*sizeof(*glist));
1625 for (i = 0; i < ord_no; i++)
1628 *status = BEND_SCAN_SUCCESS;
1629 for (i = 0; i<after; i++)
1632 const char *mterm = NULL;
1636 for (j = 0; j < ord_no; j++)
1638 if (ptr[j] < before+after &&
1639 (tst=scan_info_array[j].list[ptr[j]].term) &&
1640 (!mterm || strcmp (tst, mterm) < 0))
1648 scan_term_untrans (zi->odr, &glist[i+before].term, mterm);
1649 rset = rset_trunc (zi, &scan_info_array[j0].list[ptr[j0]].isam_p, 1);
1652 for (j = j0+1; j<ord_no; j++)
1654 if (ptr[j] < before+after &&
1655 (tst=scan_info_array[j].list[ptr[j]].term) &&
1656 !strcmp (tst, mterm))
1658 rset_bool_parms bool_parms;
1662 rset_trunc (zi, &scan_info_array[j].list[ptr[j]].isam_p, 1);
1664 bool_parms.key_size = sizeof(struct it_key);
1665 bool_parms.cmp = key_compare_it;
1666 bool_parms.rset_l = rset;
1667 bool_parms.rset_r = rset2;
1669 rset = rset_create (rset_kind_or, &bool_parms);
1674 count_set (rset, &glist[i+before].occurrences);
1679 *num_entries -= (after-i);
1680 *status = BEND_SCAN_PARTIAL;
1683 for (i = 0; i<ord_no; i++)
1686 for (i = 0; i<before; i++)
1689 const char *mterm = NULL;
1693 for (j = 0; j <ord_no; j++)
1695 if (ptr[j] < before &&
1696 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
1697 (!mterm || strcmp (tst, mterm) > 0))
1706 scan_term_untrans (zi->odr, &glist[before-1-i].term, mterm);
1709 (zi, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1);
1713 for (j = j0+1; j<ord_no; j++)
1715 if (ptr[j] < before &&
1716 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
1717 !strcmp (tst, mterm))
1719 rset_bool_parms bool_parms;
1722 rset2 = rset_trunc (zi,
1723 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1);
1725 bool_parms.key_size = sizeof(struct it_key);
1726 bool_parms.cmp = key_compare_it;
1727 bool_parms.rset_l = rset;
1728 bool_parms.rset_r = rset2;
1730 rset = rset_create (rset_kind_or, &bool_parms);
1735 count_set (rset, &glist[before-1-i].occurrences);
1741 *status = BEND_SCAN_PARTIAL;
1745 *list = glist + i; /* list is set to first 'real' entry */
1747 logf (LOG_DEBUG, "position = %d, num_entries = %d",
1748 *position, *num_entries);
1750 logf (LOG_DEBUG, "scan error: %d", zi->errCode);