2 * Copyright (C) 1994-1997, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.64 1997-09-18 08:59:20 adam
8 * Extra generic handle for the character mapping routines.
10 * Revision 1.63 1997/09/17 12:19:18 adam
11 * Zebra version corresponds to YAZ version 1.4.
12 * Changed Zebra server so that it doesn't depend on global common_resource.
14 * Revision 1.62 1997/09/05 15:30:09 adam
15 * Changed prototype for chr_map_input - added const.
16 * Added support for C++, headers uses extern "C" for public definitions.
18 * Revision 1.61 1997/02/10 10:21:14 adam
19 * Bug fix: in search terms character (^) wasn't observed.
21 * Revision 1.60 1997/01/31 11:10:34 adam
22 * Bug fix: Leading and trailing white space weren't removed in scan tokens.
24 * Revision 1.59 1997/01/17 11:31:46 adam
25 * Bug fix: complete phrase search didn't work.
27 * Revision 1.58 1996/12/23 15:30:45 adam
29 * Bug fix: result sets weren't deleted after server shut down.
31 * Revision 1.57 1996/11/11 13:38:02 adam
32 * Added proximity support in search.
34 * Revision 1.56 1996/11/08 11:10:32 adam
35 * Buffers used during file match got bigger.
36 * Compressed ISAM support everywhere.
37 * Bug fixes regarding masking characters in queries.
38 * Redesigned Regexp-2 queries.
40 * Revision 1.55 1996/11/04 14:07:44 adam
41 * Moved truncation code to trunc.c.
43 * Revision 1.54 1996/10/29 14:09:52 adam
44 * Use of cisam system - enabled if setting isamc is 1.
46 * Revision 1.53 1996/06/26 09:21:43 adam
47 * Bug fix: local attribute set wasn't obeyed in scan.
49 * Revision 1.52 1996/06/17 14:26:20 adam
50 * Function gen_regular_rel changed to handle negative numbers.
52 * Revision 1.51 1996/06/11 10:54:15 quinn
55 * Revision 1.50 1996/06/07 08:51:53 adam
56 * Bug fix: Character mapping was broken (introducued by last revision).
58 * Revision 1.49 1996/06/04 10:18:11 adam
59 * Search/scan uses character mapping module.
61 * Revision 1.48 1996/05/28 15:15:01 adam
62 * Bug fix: Didn't handle unknown database correctly.
64 * Revision 1.47 1996/05/15 18:36:28 adam
65 * Function trans_term transforms unsearchable characters to blanks.
67 * Revision 1.46 1996/05/15 11:57:56 adam
68 * Fixed bug introduced by set/field mapping in search operations.
70 * Revision 1.45 1996/05/14 11:34:00 adam
71 * Scan support in multiple registers/databases.
73 * Revision 1.44 1996/05/14 06:16:44 adam
74 * Compact use/set bytes used in search service.
76 * Revision 1.43 1996/05/09 09:54:43 adam
77 * Server supports maps from one logical attributes to a list of physical
79 * The extraction process doesn't make space consuming 'any' keys.
81 * Revision 1.42 1996/05/09 07:28:56 quinn
82 * Work towards phrases and multiple registers
84 * Revision 1.41 1996/03/20 09:36:43 adam
85 * Function dict_lookup_grep got extra parameter, init_pos, which marks
86 * from which position in pattern approximate pattern matching should occur.
87 * Approximate pattern matching is used in relevance=re-2.
89 * Revision 1.40 1996/02/02 13:44:44 adam
90 * The public dictionary functions simply use char instead of Dict_char
91 * to represent search strings. Dict_char is used internally only.
93 * Revision 1.39 1996/01/03 16:22:13 quinn
96 * Revision 1.38 1995/12/11 09:12:55 adam
97 * The rec_get function returns NULL if record doesn't exist - will
98 * happen in the server if the result set records have been deleted since
99 * the creation of the set (i.e. the search).
100 * The server saves a result temporarily if it is 'volatile', i.e. the
101 * set is register dependent.
103 * Revision 1.37 1995/12/06 15:05:28 adam
104 * More verbose in count_set.
106 * Revision 1.36 1995/12/06 12:41:27 adam
107 * New command 'stat' for the index program.
108 * Filenames can be read from stdin by specifying '-'.
109 * Bug fix/enhancement of the transformation from terms to regular
110 * expressons in the search engine.
112 * Revision 1.35 1995/11/27 09:29:00 adam
113 * Bug fixes regarding conversion to regular expressions.
115 * Revision 1.34 1995/11/16 17:00:56 adam
116 * Better logging of rpn query.
118 * Revision 1.33 1995/11/01 13:58:28 quinn
119 * Moving data1 to yaz/retrieval
121 * Revision 1.32 1995/10/27 14:00:11 adam
122 * Implemented detection of database availability.
124 * Revision 1.31 1995/10/17 18:02:10 adam
125 * New feature: databases. Implemented as prefix to words in dictionary.
127 * Revision 1.30 1995/10/16 09:32:38 adam
128 * More work on relational op.
130 * Revision 1.29 1995/10/13 16:01:49 adam
133 * Revision 1.28 1995/10/13 12:26:43 adam
134 * Optimization of truncation.
136 * Revision 1.27 1995/10/12 17:07:22 adam
139 * Revision 1.26 1995/10/12 12:40:54 adam
140 * Bug fixes in rpn_prox.
142 * Revision 1.25 1995/10/10 13:59:24 adam
143 * Function rset_open changed its wflag parameter to general flags.
145 * Revision 1.24 1995/10/09 16:18:37 adam
146 * Function dict_lookup_grep got extra client data parameter.
148 * Revision 1.23 1995/10/06 16:33:37 adam
149 * Use attribute mappings.
151 * Revision 1.22 1995/10/06 15:07:39 adam
152 * Structure 'local-number' handled.
154 * Revision 1.21 1995/10/06 13:52:06 adam
155 * Bug fixes. Handler may abort further scanning.
157 * Revision 1.20 1995/10/06 11:06:33 adam
158 * Scan entries include 'occurrences' now.
160 * Revision 1.19 1995/10/06 10:43:56 adam
161 * Scan added. 'occurrences' in scan entries not set yet.
163 * Revision 1.18 1995/10/04 16:57:20 adam
164 * Key input and merge sort in one pass.
166 * Revision 1.17 1995/10/04 12:55:17 adam
167 * Bug fix in ranked search. Use=Any keys inserted.
169 * Revision 1.16 1995/10/02 16:24:40 adam
170 * Use attribute actually used in search requests.
172 * Revision 1.15 1995/10/02 15:18:52 adam
173 * New member in recRetrieveCtrl: diagnostic.
175 * Revision 1.14 1995/09/28 12:10:32 adam
176 * Bug fixes. Field prefix used in queries.
178 * Revision 1.13 1995/09/18 14:17:50 adam
181 * Revision 1.12 1995/09/15 14:45:21 adam
183 * Work on truncation.
185 * Revision 1.11 1995/09/14 11:53:27 adam
186 * First work on regular expressions/truncations.
188 * Revision 1.10 1995/09/11 15:23:26 adam
189 * More work on relevance search.
191 * Revision 1.9 1995/09/11 13:09:35 adam
192 * More work on relevance feedback.
194 * Revision 1.8 1995/09/08 14:52:27 adam
195 * Minor changes. Dictionary is lower case now.
197 * Revision 1.7 1995/09/07 13:58:36 adam
198 * New parameter: result-set file descriptor (RSFD) to support multiple
199 * positions within the same result-set.
200 * Boolean operators: and, or, not implemented.
201 * Result-set references.
203 * Revision 1.6 1995/09/06 16:11:18 adam
204 * Option: only one word key per file.
206 * Revision 1.5 1995/09/06 10:33:04 adam
207 * More work on present. Some log messages removed.
209 * Revision 1.4 1995/09/05 15:28:40 adam
210 * More work on search engine.
212 * Revision 1.3 1995/09/04 15:20:22 adam
215 * Revision 1.2 1995/09/04 12:33:43 adam
216 * Various cleanup. YAZ util used instead.
218 * Revision 1.1 1995/09/04 09:10:40 adam
219 * More work on index add/del/update.
220 * Merge sort implemented.
221 * Initial work on z39 server.
241 Z_AttributesPlusTerm *zapt;
244 static int attr_find (AttrType *src, oid_value *attributeSetP)
246 while (src->major < src->zapt->num_attributes)
248 Z_AttributeElement *element;
250 element = src->zapt->attributeList[src->major];
251 if (src->type == *element->attributeType)
253 switch (element->which)
255 case Z_AttributeValue_numeric:
257 if (element->attributeSet && attributeSetP)
261 attrset = oid_getentbyoid (element->attributeSet);
262 *attributeSetP = attrset->value;
264 return *element->value.numeric;
266 case Z_AttributeValue_complex:
267 if (src->minor >= element->value.complex->num_list ||
268 element->value.complex->list[src->minor]->which !=
269 Z_StringOrNumeric_numeric)
272 if (element->attributeSet && attributeSetP)
276 attrset = oid_getentbyoid (element->attributeSet);
277 *attributeSetP = attrset->value;
279 return *element->value.complex->list[src->minor-1]->u.numeric;
289 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
309 static void add_isam_p (const char *info, struct grep_info *p)
311 if (p->isam_p_indx == p->isam_p_size)
313 ISAM_P *new_isam_p_buf;
318 p->isam_p_size = 2*p->isam_p_size + 100;
319 new_isam_p_buf = xmalloc (sizeof(*new_isam_p_buf) *
323 memcpy (new_isam_p_buf, p->isam_p_buf,
324 p->isam_p_indx * sizeof(*p->isam_p_buf));
325 xfree (p->isam_p_buf);
327 p->isam_p_buf = new_isam_p_buf;
330 new_term_no = xmalloc (sizeof(*new_term_no) *
334 memcpy (new_term_no, p->isam_p_buf,
335 p->isam_p_indx * sizeof(*p->term_no));
338 p->term_no = new_term_no;
341 assert (*info == sizeof(*p->isam_p_buf));
342 memcpy (p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
346 static int grep_handle (char *name, const char *info, void *p)
348 add_isam_p (info, p);
352 static int term_pre (const char **src, const char *ct1, const char *ct2)
354 const char *s1, *s0 = *src;
357 /* skip white space */
360 if (ct1 && strchr (ct1, *s0))
362 if (ct2 && strchr (ct2, *s0))
365 map = map_chrs_input (0, &s1, strlen(s1));
366 if (**map != *CHR_SPACE)
374 static int term_100 (const char **src, char *dst, int space_split)
380 if (!term_pre (src, NULL, NULL))
386 map = map_chrs_input (0, &s0, strlen(s0));
387 if (space_split && **map == *CHR_SPACE)
401 static int term_101 (const char **src, char *dst, int space_split)
407 if (!term_pre (src, "#", "#"))
421 map = map_chrs_input (0, &s0, strlen(s0));
422 if (space_split && **map == *CHR_SPACE)
438 static int term_103 (const char **src, char *dst, int *errors, int space_split)
444 if (!term_pre (src, "^\\()[].*+?|", "("))
447 if (errors && *s0 == '+' && s0[1] && s0[2] == '+' && s0[3] &&
450 *errors = s0[1] - '0';
457 if (strchr ("^\\()[].*+?|-", *s0))
462 map = map_chrs_input (0, &s0, strlen(s0));
463 if (**map == *CHR_SPACE)
478 static int term_102 (const char **src, char *dst, int space_split)
480 return term_103 (src, dst, NULL, space_split);
483 /* gen_regular_rel - generate regular expression from relation
484 * val: border value (inclusive)
485 * islt: 1 if <=; 0 if >=.
487 static void gen_regular_rel (char *dst, int val, int islt)
494 logf (LOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
498 strcpy (dst, "(-[0-9]+|");
506 strcpy (dst, "([0-9]+|-");
507 dst_p = strlen (dst);
517 dst_p = strlen (dst);
518 sprintf (numstr, "%d", val);
519 for (w = strlen(numstr); --w >= 0; pos++)
538 strcpy (dst + dst_p, numstr);
539 dst_p = strlen(dst) - pos - 1;
567 for (i = 0; i<pos; i++)
580 for (i=1; i<pos; i++)
581 strcat (dst, "[0-9]?");
585 for (i = 0; i <= pos; i++)
586 strcat (dst, "[0-9]");
587 strcat (dst, "[0-9]*");
592 static int relational_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
593 const char **term_sub,
595 oid_value attributeSet,
596 struct grep_info *grep_info,
604 attr_init (&relation, zapt, 2);
605 relation_value = attr_find (&relation, NULL);
607 switch (relation_value)
610 if (!term_100 (term_sub, term_dict, 1))
612 term_value = atoi (term_dict);
615 logf (LOG_DEBUG, "Relation <");
616 gen_regular_rel (term_dict + strlen(term_dict), term_value-1, 1);
619 if (!term_100 (term_sub, term_dict, 1))
621 term_value = atoi (term_dict);
624 logf (LOG_DEBUG, "Relation <=");
625 gen_regular_rel (term_dict + strlen(term_dict), term_value, 1);
628 if (!term_100 (term_sub, term_dict, 1))
630 term_value = atoi (term_dict);
633 logf (LOG_DEBUG, "Relation >=");
634 gen_regular_rel (term_dict + strlen(term_dict), term_value, 0);
637 if (!term_100 (term_sub, term_dict, 1))
639 term_value = atoi (term_dict);
642 logf (LOG_DEBUG, "Relation >");
643 gen_regular_rel (term_dict + strlen(term_dict), term_value+1, 0);
648 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_dict);
649 r = dict_lookup_grep (zi->dict, term_dict, 0, grep_info, max_pos,
652 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
653 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
657 static int field_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
658 const char **term_sub, int regType,
659 oid_value attributeSet, struct grep_info *grep_info,
660 int num_bases, char **basenames, int space_split)
662 char term_dict[2*IT_MAX_WORD+2];
665 int truncation_value;
668 oid_value curAttributeSet = attributeSet;
671 attr_init (&use, zapt, 1);
672 use_value = attr_find (&use, &curAttributeSet);
673 logf (LOG_DEBUG, "field_term, use value %d", use_value);
674 attr_init (&truncation, zapt, 5);
675 truncation_value = attr_find (&truncation, NULL);
676 logf (LOG_DEBUG, "truncation value %d", truncation_value);
681 for (base_no = 0; base_no < num_bases; base_no++)
684 data1_local_attribute *local_attr;
685 int max_pos, prefix_len = 0;
688 if (!att_getentbyatt (zi, &attp, curAttributeSet, use_value))
690 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
691 curAttributeSet, use_value);
695 if (zebTargetInfo_curDatabase (zi->zti, basenames[base_no]))
697 zi->errCode = 109; /* Database unavailable */
698 zi->errString = basenames[base_no];
701 for (local_attr = attp.local_attributes; local_attr;
702 local_attr = local_attr->next)
706 ord = zebTargetInfo_lookupSU (zi->zti, attp.attset_ordinal,
711 term_dict[prefix_len++] = '|';
713 term_dict[prefix_len++] = '(';
714 term_dict[prefix_len++] = 1;
715 term_dict[prefix_len++] = ord;
722 term_dict[prefix_len++] = ')';
723 term_dict[prefix_len++] = 1;
724 term_dict[prefix_len++] = regType;
725 term_dict[prefix_len] = '\0';
726 if (!relational_term (zi, zapt, &termp, term_dict,
727 attributeSet, grep_info, &max_pos))
730 switch (truncation_value)
732 case -1: /* not specified */
733 case 100: /* do not truncate */
734 term_dict[j++] = '(';
735 if (!term_100 (&termp, term_dict + j, space_split))
737 strcat (term_dict, ")");
738 r = dict_lookup_grep (zi->dict, term_dict, 0, grep_info,
739 &max_pos, 0, grep_handle);
741 logf (LOG_WARN, "dict_lookup_grep err, trunc=none:%d", r);
743 case 1: /* right truncation */
744 term_dict[j++] = '(';
745 if (!term_100 (&termp, term_dict + j, space_split))
747 strcat (term_dict, ".*)");
748 dict_lookup_grep (zi->dict, term_dict, 0, grep_info,
749 &max_pos, 0, grep_handle);
751 case 2: /* left truncation */
752 case 3: /* left&right truncation */
755 case 101: /* process # in term */
756 term_dict[j++] = '(';
757 if (!term_101 (&termp, term_dict + j, space_split))
759 strcat (term_dict, ")");
760 r = dict_lookup_grep (zi->dict, term_dict, 0, grep_info,
761 &max_pos, 0, grep_handle);
763 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d", r);
765 case 102: /* Regexp-1 */
766 term_dict[j++] = '(';
767 if (!term_102 (&termp, term_dict + j, space_split))
769 strcat (term_dict, ")");
770 logf (LOG_DEBUG, "Regexp-1 tolerance=%d", r);
771 r = dict_lookup_grep (zi->dict, term_dict, 0, grep_info,
772 &max_pos, 0, grep_handle);
774 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
777 case 103: /* Regexp-1 */
779 term_dict[j++] = '(';
780 if (!term_103 (&termp, term_dict + j, &r, space_split))
782 strcat (term_dict, ")");
783 logf (LOG_DEBUG, "Regexp-2 tolerance=%d", r);
784 r = dict_lookup_grep (zi->dict, term_dict, r, grep_info,
785 &max_pos, 2, grep_handle);
787 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
794 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
798 static void trans_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
802 Z_Term *term = zapt->term;
804 sizez = term->u.general->len;
805 if (sizez > IT_MAX_WORD-1)
806 sizez = IT_MAX_WORD-1;
807 memcpy (termz, term->u.general->buf, sizez);
811 static void trans_scan_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
814 Z_Term *term = zapt->term;
816 const char *cp = (const char *) term->u.general->buf;
817 const char *cp_end = cp + term->u.general->len;
820 const char *space_map = NULL;
823 while ((len = (cp_end - cp)) > 0)
825 map = map_chrs_input (0, &cp, len);
826 if (**map == *CHR_SPACE)
831 for (src = space_map; *src; src++)
834 for (src = *map; *src; src++)
841 static RSET rpn_search_APT_relevance (ZServerInfo *zi,
842 Z_AttributesPlusTerm *zapt,
843 oid_value attributeSet,
844 int num_bases, char **basenames)
846 rset_relevance_parms parms;
847 char termz[IT_MAX_WORD+1];
848 const char *termp = termz;
849 struct grep_info grep_info;
854 parms.key_size = sizeof(struct it_key);
855 parms.max_rec = 1000;
856 parms.cmp = key_compare_it;
858 parms.isc = zi->isamc;
861 if (zapt->term->which != Z_Term_general)
866 trans_term (zi, zapt, termz);
869 grep_info.term_no = 0;
871 grep_info.isam_p_indx = 0;
872 grep_info.isam_p_size = 0;
873 grep_info.isam_p_buf = NULL;
876 r = field_term (zi, zapt, &termp, 'w', attributeSet, &grep_info,
877 num_bases, basenames, 1);
881 for (; term_index < grep_info.isam_p_indx; term_index++)
882 grep_info.term_no[term_index] = parms.no_terms;
886 parms.term_no = grep_info.term_no;
887 parms.isam_positions = grep_info.isam_p_buf;
888 parms.no_isam_positions = grep_info.isam_p_indx;
889 if (grep_info.isam_p_indx > 0)
890 result = rset_create (rset_kind_relevance, &parms);
892 result = rset_create (rset_kind_null, NULL);
894 xfree(grep_info.term_no);
896 xfree (grep_info.isam_p_buf);
900 static RSET rpn_search_APT_cphrase (ZServerInfo *zi,
901 Z_AttributesPlusTerm *zapt,
902 oid_value attributeSet,
903 int num_bases, char **basenames)
905 char termz[IT_MAX_WORD+1];
906 struct grep_info grep_info;
908 const char *termp = termz;
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;
925 r = field_term (zi, zapt, &termp, 'p', attributeSet, &grep_info,
926 num_bases, basenames, 0);
927 result = rset_trunc (zi, grep_info.isam_p_buf, grep_info.isam_p_indx);
929 xfree(grep_info.term_no);
931 xfree (grep_info.isam_p_buf);
935 static RSET rpn_proximity (ZServerInfo *zi, RSET rset1, RSET rset2,
937 int exclusion, int relation, int distance)
942 struct it_key buf1, buf2;
945 rset_temp_parms parms;
947 rsfd1 = rset_open (rset1, RSETF_READ|RSETF_SORT_SYSNO);
948 more1 = rset_read (rset1, rsfd1, &buf1);
950 rsfd2 = rset_open (rset2, RSETF_READ|RSETF_SORT_SYSNO);
951 more2 = rset_read (rset2, rsfd2, &buf2);
953 parms.key_size = sizeof (struct it_key);
954 parms.temp_path = res_get (zi->res, "setTmpDir");
955 result = rset_create (rset_kind_temp, &parms);
956 rsfd_result = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
958 logf (LOG_DEBUG, "rpn_proximity excl=%d ord=%d rel=%d dis=%d",
959 exclusion, ordered, relation, distance);
960 while (more1 && more2)
962 int cmp = key_compare_it (&buf1, &buf2);
964 more1 = rset_read (rset1, rsfd1, &buf1);
966 more2 = rset_read (rset2, rsfd2, &buf2);
969 int sysno = buf1.sysno;
973 seqno[n++] = buf1.seqno;
974 while ((more1 = rset_read (rset1, rsfd1, &buf1)) &&
977 seqno[n++] = buf1.seqno;
980 for (i = 0; i<n; i++)
982 int diff = buf2.seqno - seqno[i];
983 int excl = exclusion;
984 if (!ordered && diff < 0)
993 if (diff <= distance)
997 if (diff == distance)
1001 if (diff >= distance)
1005 if (diff > distance)
1009 if (diff != distance)
1014 rset_write (result, rsfd_result, &buf2);
1016 } while ((more2 = rset_read (rset2, rsfd2, &buf2)) &&
1017 sysno == buf2.sysno);
1020 rset_close (result, rsfd_result);
1021 rset_close (rset1, rsfd1);
1022 rset_close (rset2, rsfd2);
1026 static RSET rpn_prox (ZServerInfo *zi, RSET *rset, int rset_no)
1031 struct it_key **buf;
1034 rset_temp_parms parms;
1036 rsfd = xmalloc (sizeof(*rsfd)*rset_no);
1037 more = xmalloc (sizeof(*more)*rset_no);
1038 buf = xmalloc (sizeof(*buf)*rset_no);
1040 for (i = 0; i<rset_no; i++)
1042 buf[i] = xmalloc (sizeof(**buf));
1043 rsfd[i] = rset_open (rset[i], RSETF_READ|RSETF_SORT_SYSNO);
1044 if (!(more[i] = rset_read (rset[i], rsfd[i], buf[i])))
1048 rset_close (rset[i], rsfd[i]);
1055 return rset_create (rset_kind_null, NULL);
1058 parms.key_size = sizeof (struct it_key);
1059 parms.temp_path = res_get (zi->res, "setTmpDir");
1060 result = rset_create (rset_kind_temp, &parms);
1061 rsfd_result = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
1065 for (i = 1; i<rset_no; i++)
1074 cmp = key_compare_it (buf[i], buf[i-1]);
1077 more[i-1] = rset_read (rset[i-1], rsfd[i-1], buf[i-1]);
1082 if (buf[i-1]->seqno+1 != buf[i]->seqno)
1084 more[i-1] = rset_read (rset[i-1], rsfd[i-1], buf[i-1]);
1090 more[i] = rset_read (rset[i], rsfd[i], buf[i]);
1096 rset_write (result, rsfd_result, buf[0]);
1097 more[0] = rset_read (*rset, *rsfd, *buf);
1101 for (i = 0; i<rset_no; i++)
1103 rset_close (rset[i], rsfd[i]);
1106 rset_close (result, rsfd_result);
1113 static RSET rpn_search_APT_phrase (ZServerInfo *zi,
1114 Z_AttributesPlusTerm *zapt,
1115 oid_value attributeSet,
1116 int num_bases, char **basenames)
1118 char termz[IT_MAX_WORD+1];
1119 const char *termp = termz;
1120 RSET rset[60], result;
1121 int i, r, rset_no = 0;
1122 struct grep_info grep_info;
1124 if (zapt->term->which != Z_Term_general)
1129 trans_term (zi, zapt, termz);
1132 grep_info.term_no = 0;
1134 grep_info.isam_p_size = 0;
1135 grep_info.isam_p_buf = NULL;
1139 grep_info.isam_p_indx = 0;
1140 r = field_term (zi, zapt, &termp, 'w', attributeSet, &grep_info,
1141 num_bases, basenames, 1);
1144 rset[rset_no] = rset_trunc (zi, grep_info.isam_p_buf,
1145 grep_info.isam_p_indx);
1146 assert (rset[rset_no]);
1147 if (++rset_no >= sizeof(rset)/sizeof(*rset))
1151 xfree(grep_info.term_no);
1153 xfree (grep_info.isam_p_buf);
1155 return rset_create (rset_kind_null, NULL);
1156 else if (rset_no == 1)
1158 result = rpn_prox (zi, rset, rset_no);
1159 for (i = 0; i<rset_no; i++)
1160 rset_delete (rset[i]);
1164 static RSET rpn_search_APT_local (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1165 oid_value attributeSet)
1170 rset_temp_parms parms;
1171 char termz[IT_MAX_WORD+1];
1173 if (zapt->term->which != Z_Term_general)
1178 parms.key_size = sizeof (struct it_key);
1179 parms.temp_path = res_get (zi->res, "setTmpDir");
1180 result = rset_create (rset_kind_temp, &parms);
1181 rsfd = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
1183 trans_term (zi, zapt, termz);
1185 key.sysno = atoi (termz);
1188 rset_write (result, rsfd, &key);
1189 rset_close (result, rsfd);
1193 static RSET rpn_search_APT (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1194 oid_value attributeSet,
1195 int num_bases, char **basenames)
1199 AttrType completeness;
1200 int relation_value, structure_value, completeness_value;
1202 attr_init (&relation, zapt, 2);
1203 attr_init (&structure, zapt, 4);
1204 attr_init (&completeness, zapt, 6);
1206 relation_value = attr_find (&relation, NULL);
1207 structure_value = attr_find (&structure, NULL);
1208 completeness_value = attr_find (&completeness, NULL);
1209 switch (structure_value)
1212 if (relation_value == 102) /* relevance relation */
1213 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1214 num_bases, basenames);
1215 if (completeness_value == 2 || completeness_value == 3)
1216 return rpn_search_APT_cphrase (zi, zapt, attributeSet,
1217 num_bases, basenames);
1218 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1219 num_bases, basenames);
1220 case 1: /* phrase */
1221 if (relation_value == 102) /* relevance relation */
1222 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1223 num_bases, basenames);
1224 if (completeness_value == 2 || completeness_value == 3)
1225 return rpn_search_APT_cphrase (zi, zapt, attributeSet,
1226 num_bases, basenames);
1227 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1228 num_bases, basenames);
1231 if (relation_value == 102) /* relevance relation */
1232 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1233 num_bases, basenames);
1234 if (completeness_value == 2 || completeness_value == 3)
1235 return rpn_search_APT_cphrase (zi, zapt, attributeSet,
1236 num_bases, basenames);
1237 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1238 num_bases, basenames);
1243 case 5: /* date - normalized */
1245 case 6: /* word list */
1246 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1247 num_bases, basenames);
1248 case 100: /* date - un-normalized */
1250 case 101: /* name - normalized */
1252 case 102: /* date - un-normalized */
1254 case 103: /* structure */
1258 case 105: /* free-form-text */
1259 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1260 num_bases, basenames);
1261 case 106: /* document-text */
1262 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1263 num_bases, basenames);
1264 case 107: /* local-number */
1265 return rpn_search_APT_local (zi, zapt, attributeSet);
1266 case 108: /* string */
1267 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1268 num_bases, basenames);
1269 case 109: /* numeric string */
1276 static RSET rpn_search_ref (ZServerInfo *zi, Z_ResultSetId *resultSetId)
1280 if (!(s = resultSetGet (zi, resultSetId)))
1281 return rset_create (rset_kind_null, NULL);
1285 static RSET rpn_search_structure (ZServerInfo *zi, Z_RPNStructure *zs,
1286 oid_value attributeSet,
1287 int num_bases, char **basenames)
1290 if (zs->which == Z_RPNStructure_complex)
1292 Z_Operator *zop = zs->u.complex->roperator;
1293 rset_bool_parms bool_parms;
1297 bool_parms.rset_l = rpn_search_structure (zi, zs->u.complex->s1,
1299 num_bases, basenames);
1300 if (bool_parms.rset_l == NULL)
1302 if (rset_is_ranked(bool_parms.rset_l))
1304 bool_parms.rset_r = rpn_search_structure (zi, zs->u.complex->s2,
1306 num_bases, basenames);
1307 if (bool_parms.rset_r == NULL)
1309 rset_delete (bool_parms.rset_l);
1312 if (rset_is_ranked(bool_parms.rset_r))
1314 bool_parms.key_size = sizeof(struct it_key);
1315 bool_parms.cmp = key_compare_it;
1319 case Z_Operator_and:
1320 r = rset_create (soft ? rset_kind_sand:rset_kind_and, &bool_parms);
1323 r = rset_create (soft ? rset_kind_sor:rset_kind_or, &bool_parms);
1325 case Z_Operator_and_not:
1326 r = rset_create (soft ? rset_kind_snot:rset_kind_not, &bool_parms);
1328 case Z_Operator_prox:
1329 if (zop->u.prox->which != Z_ProxCode_known)
1334 if (*zop->u.prox->proximityUnitCode != Z_ProxUnit_word)
1336 static char val[16];
1338 zi->errString = val;
1339 sprintf (val, "%d", *zop->u.prox->proximityUnitCode);
1342 r = rpn_proximity (zi, bool_parms.rset_l, bool_parms.rset_r,
1343 *zop->u.prox->ordered,
1344 (!zop->u.prox->exclusion ? 0 :
1345 *zop->u.prox->exclusion),
1346 *zop->u.prox->relationType,
1347 *zop->u.prox->distance);
1354 else if (zs->which == Z_RPNStructure_simple)
1356 if (zs->u.simple->which == Z_Operand_APT)
1358 logf (LOG_DEBUG, "rpn_search_APT");
1359 r = rpn_search_APT (zi, zs->u.simple->u.attributesPlusTerm,
1360 attributeSet, num_bases, basenames);
1362 else if (zs->u.simple->which == Z_Operand_resultSetId)
1364 logf (LOG_DEBUG, "rpn_search_ref");
1365 r = rpn_search_ref (zi, zs->u.simple->u.resultSetId);
1381 void count_set_save (ZServerInfo *zi, RSET *r, int *count)
1388 rset_temp_parms parms;
1389 int maxResultSetSize = atoi (res_get_def (zi->res,
1390 "maxResultSetSize", "400"));
1391 logf (LOG_DEBUG, "count_set_save");
1393 parms.key_size = sizeof(struct it_key);
1394 parms.temp_path = res_get (zi->res, "setTmpDir");
1395 w = rset_create (rset_kind_temp, &parms);
1396 wfd = rset_open (w, RSETF_WRITE|RSETF_SORT_SYSNO);
1397 rfd = rset_open (*r, RSETF_READ|RSETF_SORT_SYSNO);
1398 while (rset_read (*r, rfd, &key))
1400 if (key.sysno != psysno)
1402 if (*count < maxResultSetSize)
1403 rset_write (w, wfd, &key);
1409 rset_close (*r, rfd);
1411 rset_close (w, wfd);
1413 logf (LOG_DEBUG, "%d keys, %d distinct sysnos", kno, *count);
1416 static void count_set (RSET r, int *count)
1423 logf (LOG_DEBUG, "count_set");
1425 rfd = rset_open (r, RSETF_READ|RSETF_SORT_SYSNO);
1426 while (rset_read (r, rfd, &key))
1428 if (key.sysno != psysno)
1435 rset_close (r, rfd);
1436 logf (LOG_DEBUG, "%d keys, %d distinct sysnos", kno, *count);
1439 int rpn_search (ZServerInfo *zi,
1440 Z_RPNQuery *rpn, int num_bases, char **basenames,
1441 const char *setname, int *hits)
1445 oid_value attributeSet;
1447 dict_grep_cmap (zi->dict, 0, map_chrs_input);
1451 zi->errString = NULL;
1453 attrset = oid_getentbyoid (rpn->attributeSetId);
1454 attributeSet = attrset->value;
1455 rset = rpn_search_structure (zi, rpn->RPNStructure, attributeSet,
1456 num_bases, basenames);
1459 if (rset_is_volatile(rset))
1460 count_set_save(zi, &rset,hits);
1462 count_set (rset, hits);
1463 resultSetAdd (zi, setname, 1, rset);
1465 logf (LOG_DEBUG, "search error: %d", zi->errCode);
1469 struct scan_info_entry {
1475 struct scan_info_entry *list;
1481 static int scan_handle (char *name, const char *info, int pos, void *client)
1483 int len_prefix, idx;
1484 struct scan_info *scan_info = client;
1486 len_prefix = strlen(scan_info->prefix);
1487 if (memcmp (name, scan_info->prefix, len_prefix))
1490 idx = scan_info->after - pos + scan_info->before;
1493 scan_info->list[idx].term = odr_malloc (scan_info->odr,
1494 strlen(name + len_prefix)+1);
1495 strcpy (scan_info->list[idx].term, name + len_prefix);
1496 assert (*info == sizeof(ISAM_P));
1497 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAM_P));
1502 static void scan_term_untrans (ODR odr, char **dstp, const char *src)
1504 char *dst = odr_malloc (odr, strlen(src)*2+1);
1509 const char *cp = map_chrs_output (&src);
1516 int rpn_scan (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1517 oid_value attributeset,
1518 int num_bases, char **basenames,
1519 int *position, int *num_entries, struct scan_entry **list,
1523 int pos = *position;
1524 int num = *num_entries;
1528 char termz[IT_MAX_WORD+20];
1531 AttrType completeness;
1532 int completeness_value;
1533 struct scan_info *scan_info_array;
1534 struct scan_entry *glist;
1535 int ords[32], ord_no = 0;
1538 logf (LOG_DEBUG, "scan, position = %d, num = %d", pos, num);
1540 if (attributeset == VAL_NONE)
1541 attributeset = VAL_BIB1;
1543 attr_init (&use, zapt, 1);
1544 use_value = attr_find (&use, &attributeset);
1545 logf (LOG_DEBUG, "use value %d", use_value);
1547 attr_init (&completeness, zapt, 6);
1548 completeness_value = attr_find (&completeness, NULL);
1549 logf (LOG_DEBUG, "completeness value %d", completeness_value);
1551 if (use_value == -1)
1553 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
1556 data1_local_attribute *local_attr;
1558 if (!att_getentbyatt (zi, &attp, attributeset, use_value))
1560 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
1561 attributeset, use_value);
1562 return zi->errCode = 114;
1564 if (zebTargetInfo_curDatabase (zi->zti, basenames[base_no]))
1566 zi->errString = basenames[base_no];
1567 return zi->errCode = 109; /* Database unavailable */
1569 for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
1570 local_attr = local_attr->next)
1574 ord = zebTargetInfo_lookupSU (zi->zti, attp.attset_ordinal,
1577 ords[ord_no++] = ord;
1581 return zi->errCode = 113;
1584 scan_info_array = odr_malloc (zi->odr, ord_no * sizeof(*scan_info_array));
1585 for (i = 0; i < ord_no; i++)
1587 int j, prefix_len = 0;
1588 int before_tmp = before, after_tmp = after;
1589 struct scan_info *scan_info = scan_info_array + i;
1591 scan_info->before = before;
1592 scan_info->after = after;
1593 scan_info->odr = zi->odr;
1595 scan_info->list = odr_malloc (zi->odr, (before+after)*
1596 sizeof(*scan_info->list));
1597 for (j = 0; j<before+after; j++)
1598 scan_info->list[j].term = NULL;
1599 termz[prefix_len++] = ords[i];
1600 termz[prefix_len++] =
1601 (completeness_value==2 || completeness_value==3) ? 'p': 'w';
1602 termz[prefix_len] = 0;
1603 strcpy (scan_info->prefix, termz);
1605 trans_scan_term (zi, zapt, termz+prefix_len);
1607 dict_scan (zi->dict, termz, &before_tmp, &after_tmp, scan_info,
1610 glist = odr_malloc (zi->odr, (before+after)*sizeof(*glist));
1611 for (i = 0; i < ord_no; i++)
1614 *status = BEND_SCAN_SUCCESS;
1615 for (i = 0; i<after; i++)
1618 const char *mterm = NULL;
1622 for (j = 0; j < ord_no; j++)
1624 if (ptr[j] < before+after &&
1625 (tst=scan_info_array[j].list[ptr[j]].term) &&
1626 (!mterm || strcmp (tst, mterm) < 0))
1634 scan_term_untrans (zi->odr, &glist[i+before].term, mterm);
1635 rset = rset_trunc (zi, &scan_info_array[j0].list[ptr[j0]].isam_p, 1);
1638 for (j = j0+1; j<ord_no; j++)
1640 if (ptr[j] < before+after &&
1641 (tst=scan_info_array[j].list[ptr[j]].term) &&
1642 !strcmp (tst, mterm))
1644 rset_bool_parms bool_parms;
1648 rset_trunc (zi, &scan_info_array[j].list[ptr[j]].isam_p, 1);
1650 bool_parms.key_size = sizeof(struct it_key);
1651 bool_parms.cmp = key_compare_it;
1652 bool_parms.rset_l = rset;
1653 bool_parms.rset_r = rset2;
1655 rset = rset_create (rset_kind_or, &bool_parms);
1660 count_set (rset, &glist[i+before].occurrences);
1665 *num_entries -= (after-i);
1666 *status = BEND_SCAN_PARTIAL;
1669 for (i = 0; i<ord_no; i++)
1672 for (i = 0; i<before; i++)
1675 const char *mterm = NULL;
1679 for (j = 0; j <ord_no; j++)
1681 if (ptr[j] < before &&
1682 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
1683 (!mterm || strcmp (tst, mterm) > 0))
1692 scan_term_untrans (zi->odr, &glist[before-1-i].term, mterm);
1695 (zi, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1);
1699 for (j = j0+1; j<ord_no; j++)
1701 if (ptr[j] < before &&
1702 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
1703 !strcmp (tst, mterm))
1705 rset_bool_parms bool_parms;
1708 rset2 = rset_trunc (zi,
1709 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1);
1711 bool_parms.key_size = sizeof(struct it_key);
1712 bool_parms.cmp = key_compare_it;
1713 bool_parms.rset_l = rset;
1714 bool_parms.rset_r = rset2;
1716 rset = rset_create (rset_kind_or, &bool_parms);
1721 count_set (rset, &glist[before-1-i].occurrences);
1727 *status = BEND_SCAN_PARTIAL;
1731 *list = glist + i; /* list is set to first 'real' entry */
1733 logf (LOG_DEBUG, "position = %d, num_entries = %d",
1734 *position, *num_entries);
1736 logf (LOG_DEBUG, "scan error: %d", zi->errCode);