2 * Copyright (C) 1994-1997, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.65 1997-09-22 12:39:06 adam
8 * Added get_pos method for the ranked result sets.
10 * Revision 1.64 1997/09/18 08:59:20 adam
11 * Extra generic handle for the character mapping routines.
13 * Revision 1.63 1997/09/17 12:19:18 adam
14 * Zebra version corresponds to YAZ version 1.4.
15 * Changed Zebra server so that it doesn't depend on global common_resource.
17 * Revision 1.62 1997/09/05 15:30:09 adam
18 * Changed prototype for chr_map_input - added const.
19 * Added support for C++, headers uses extern "C" for public definitions.
21 * Revision 1.61 1997/02/10 10:21:14 adam
22 * Bug fix: in search terms character (^) wasn't observed.
24 * Revision 1.60 1997/01/31 11:10:34 adam
25 * Bug fix: Leading and trailing white space weren't removed in scan tokens.
27 * Revision 1.59 1997/01/17 11:31:46 adam
28 * Bug fix: complete phrase search didn't work.
30 * Revision 1.58 1996/12/23 15:30:45 adam
32 * Bug fix: result sets weren't deleted after server shut down.
34 * Revision 1.57 1996/11/11 13:38:02 adam
35 * Added proximity support in search.
37 * Revision 1.56 1996/11/08 11:10:32 adam
38 * Buffers used during file match got bigger.
39 * Compressed ISAM support everywhere.
40 * Bug fixes regarding masking characters in queries.
41 * Redesigned Regexp-2 queries.
43 * Revision 1.55 1996/11/04 14:07:44 adam
44 * Moved truncation code to trunc.c.
46 * Revision 1.54 1996/10/29 14:09:52 adam
47 * Use of cisam system - enabled if setting isamc is 1.
49 * Revision 1.53 1996/06/26 09:21:43 adam
50 * Bug fix: local attribute set wasn't obeyed in scan.
52 * Revision 1.52 1996/06/17 14:26:20 adam
53 * Function gen_regular_rel changed to handle negative numbers.
55 * Revision 1.51 1996/06/11 10:54:15 quinn
58 * Revision 1.50 1996/06/07 08:51:53 adam
59 * Bug fix: Character mapping was broken (introducued by last revision).
61 * Revision 1.49 1996/06/04 10:18:11 adam
62 * Search/scan uses character mapping module.
64 * Revision 1.48 1996/05/28 15:15:01 adam
65 * Bug fix: Didn't handle unknown database correctly.
67 * Revision 1.47 1996/05/15 18:36:28 adam
68 * Function trans_term transforms unsearchable characters to blanks.
70 * Revision 1.46 1996/05/15 11:57:56 adam
71 * Fixed bug introduced by set/field mapping in search operations.
73 * Revision 1.45 1996/05/14 11:34:00 adam
74 * Scan support in multiple registers/databases.
76 * Revision 1.44 1996/05/14 06:16:44 adam
77 * Compact use/set bytes used in search service.
79 * Revision 1.43 1996/05/09 09:54:43 adam
80 * Server supports maps from one logical attributes to a list of physical
82 * The extraction process doesn't make space consuming 'any' keys.
84 * Revision 1.42 1996/05/09 07:28:56 quinn
85 * Work towards phrases and multiple registers
87 * Revision 1.41 1996/03/20 09:36:43 adam
88 * Function dict_lookup_grep got extra parameter, init_pos, which marks
89 * from which position in pattern approximate pattern matching should occur.
90 * Approximate pattern matching is used in relevance=re-2.
92 * Revision 1.40 1996/02/02 13:44:44 adam
93 * The public dictionary functions simply use char instead of Dict_char
94 * to represent search strings. Dict_char is used internally only.
96 * Revision 1.39 1996/01/03 16:22:13 quinn
99 * Revision 1.38 1995/12/11 09:12:55 adam
100 * The rec_get function returns NULL if record doesn't exist - will
101 * happen in the server if the result set records have been deleted since
102 * the creation of the set (i.e. the search).
103 * The server saves a result temporarily if it is 'volatile', i.e. the
104 * set is register dependent.
106 * Revision 1.37 1995/12/06 15:05:28 adam
107 * More verbose in count_set.
109 * Revision 1.36 1995/12/06 12:41:27 adam
110 * New command 'stat' for the index program.
111 * Filenames can be read from stdin by specifying '-'.
112 * Bug fix/enhancement of the transformation from terms to regular
113 * expressons in the search engine.
115 * Revision 1.35 1995/11/27 09:29:00 adam
116 * Bug fixes regarding conversion to regular expressions.
118 * Revision 1.34 1995/11/16 17:00:56 adam
119 * Better logging of rpn query.
121 * Revision 1.33 1995/11/01 13:58:28 quinn
122 * Moving data1 to yaz/retrieval
124 * Revision 1.32 1995/10/27 14:00:11 adam
125 * Implemented detection of database availability.
127 * Revision 1.31 1995/10/17 18:02:10 adam
128 * New feature: databases. Implemented as prefix to words in dictionary.
130 * Revision 1.30 1995/10/16 09:32:38 adam
131 * More work on relational op.
133 * Revision 1.29 1995/10/13 16:01:49 adam
136 * Revision 1.28 1995/10/13 12:26:43 adam
137 * Optimization of truncation.
139 * Revision 1.27 1995/10/12 17:07:22 adam
142 * Revision 1.26 1995/10/12 12:40:54 adam
143 * Bug fixes in rpn_prox.
145 * Revision 1.25 1995/10/10 13:59:24 adam
146 * Function rset_open changed its wflag parameter to general flags.
148 * Revision 1.24 1995/10/09 16:18:37 adam
149 * Function dict_lookup_grep got extra client data parameter.
151 * Revision 1.23 1995/10/06 16:33:37 adam
152 * Use attribute mappings.
154 * Revision 1.22 1995/10/06 15:07:39 adam
155 * Structure 'local-number' handled.
157 * Revision 1.21 1995/10/06 13:52:06 adam
158 * Bug fixes. Handler may abort further scanning.
160 * Revision 1.20 1995/10/06 11:06:33 adam
161 * Scan entries include 'occurrences' now.
163 * Revision 1.19 1995/10/06 10:43:56 adam
164 * Scan added. 'occurrences' in scan entries not set yet.
166 * Revision 1.18 1995/10/04 16:57:20 adam
167 * Key input and merge sort in one pass.
169 * Revision 1.17 1995/10/04 12:55:17 adam
170 * Bug fix in ranked search. Use=Any keys inserted.
172 * Revision 1.16 1995/10/02 16:24:40 adam
173 * Use attribute actually used in search requests.
175 * Revision 1.15 1995/10/02 15:18:52 adam
176 * New member in recRetrieveCtrl: diagnostic.
178 * Revision 1.14 1995/09/28 12:10:32 adam
179 * Bug fixes. Field prefix used in queries.
181 * Revision 1.13 1995/09/18 14:17:50 adam
184 * Revision 1.12 1995/09/15 14:45:21 adam
186 * Work on truncation.
188 * Revision 1.11 1995/09/14 11:53:27 adam
189 * First work on regular expressions/truncations.
191 * Revision 1.10 1995/09/11 15:23:26 adam
192 * More work on relevance search.
194 * Revision 1.9 1995/09/11 13:09:35 adam
195 * More work on relevance feedback.
197 * Revision 1.8 1995/09/08 14:52:27 adam
198 * Minor changes. Dictionary is lower case now.
200 * Revision 1.7 1995/09/07 13:58:36 adam
201 * New parameter: result-set file descriptor (RSFD) to support multiple
202 * positions within the same result-set.
203 * Boolean operators: and, or, not implemented.
204 * Result-set references.
206 * Revision 1.6 1995/09/06 16:11:18 adam
207 * Option: only one word key per file.
209 * Revision 1.5 1995/09/06 10:33:04 adam
210 * More work on present. Some log messages removed.
212 * Revision 1.4 1995/09/05 15:28:40 adam
213 * More work on search engine.
215 * Revision 1.3 1995/09/04 15:20:22 adam
218 * Revision 1.2 1995/09/04 12:33:43 adam
219 * Various cleanup. YAZ util used instead.
221 * Revision 1.1 1995/09/04 09:10:40 adam
222 * More work on index add/del/update.
223 * Merge sort implemented.
224 * Initial work on z39 server.
244 Z_AttributesPlusTerm *zapt;
247 static int attr_find (AttrType *src, oid_value *attributeSetP)
249 while (src->major < src->zapt->num_attributes)
251 Z_AttributeElement *element;
253 element = src->zapt->attributeList[src->major];
254 if (src->type == *element->attributeType)
256 switch (element->which)
258 case Z_AttributeValue_numeric:
260 if (element->attributeSet && attributeSetP)
264 attrset = oid_getentbyoid (element->attributeSet);
265 *attributeSetP = attrset->value;
267 return *element->value.numeric;
269 case Z_AttributeValue_complex:
270 if (src->minor >= element->value.complex->num_list ||
271 element->value.complex->list[src->minor]->which !=
272 Z_StringOrNumeric_numeric)
275 if (element->attributeSet && attributeSetP)
279 attrset = oid_getentbyoid (element->attributeSet);
280 *attributeSetP = attrset->value;
282 return *element->value.complex->list[src->minor-1]->u.numeric;
292 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
312 static void add_isam_p (const char *info, struct grep_info *p)
314 if (p->isam_p_indx == p->isam_p_size)
316 ISAM_P *new_isam_p_buf;
321 p->isam_p_size = 2*p->isam_p_size + 100;
322 new_isam_p_buf = xmalloc (sizeof(*new_isam_p_buf) *
326 memcpy (new_isam_p_buf, p->isam_p_buf,
327 p->isam_p_indx * sizeof(*p->isam_p_buf));
328 xfree (p->isam_p_buf);
330 p->isam_p_buf = new_isam_p_buf;
333 new_term_no = xmalloc (sizeof(*new_term_no) *
337 memcpy (new_term_no, p->isam_p_buf,
338 p->isam_p_indx * sizeof(*p->term_no));
341 p->term_no = new_term_no;
344 assert (*info == sizeof(*p->isam_p_buf));
345 memcpy (p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
349 static int grep_handle (char *name, const char *info, void *p)
351 add_isam_p (info, p);
355 static int term_pre (const char **src, const char *ct1, const char *ct2)
357 const char *s1, *s0 = *src;
360 /* skip white space */
363 if (ct1 && strchr (ct1, *s0))
365 if (ct2 && strchr (ct2, *s0))
368 map = map_chrs_input (0, &s1, strlen(s1));
369 if (**map != *CHR_SPACE)
377 static int term_100 (const char **src, char *dst, int space_split)
383 if (!term_pre (src, NULL, NULL))
389 map = map_chrs_input (0, &s0, strlen(s0));
390 if (space_split && **map == *CHR_SPACE)
404 static int term_101 (const char **src, char *dst, int space_split)
410 if (!term_pre (src, "#", "#"))
424 map = map_chrs_input (0, &s0, strlen(s0));
425 if (space_split && **map == *CHR_SPACE)
441 static int term_103 (const char **src, char *dst, int *errors, int space_split)
447 if (!term_pre (src, "^\\()[].*+?|", "("))
450 if (errors && *s0 == '+' && s0[1] && s0[2] == '+' && s0[3] &&
453 *errors = s0[1] - '0';
460 if (strchr ("^\\()[].*+?|-", *s0))
465 map = map_chrs_input (0, &s0, strlen(s0));
466 if (**map == *CHR_SPACE)
481 static int term_102 (const char **src, char *dst, int space_split)
483 return term_103 (src, dst, NULL, space_split);
486 /* gen_regular_rel - generate regular expression from relation
487 * val: border value (inclusive)
488 * islt: 1 if <=; 0 if >=.
490 static void gen_regular_rel (char *dst, int val, int islt)
497 logf (LOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
501 strcpy (dst, "(-[0-9]+|");
509 strcpy (dst, "([0-9]+|-");
510 dst_p = strlen (dst);
520 dst_p = strlen (dst);
521 sprintf (numstr, "%d", val);
522 for (w = strlen(numstr); --w >= 0; pos++)
541 strcpy (dst + dst_p, numstr);
542 dst_p = strlen(dst) - pos - 1;
570 for (i = 0; i<pos; i++)
583 for (i=1; i<pos; i++)
584 strcat (dst, "[0-9]?");
588 for (i = 0; i <= pos; i++)
589 strcat (dst, "[0-9]");
590 strcat (dst, "[0-9]*");
595 static int relational_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
596 const char **term_sub,
598 oid_value attributeSet,
599 struct grep_info *grep_info,
607 attr_init (&relation, zapt, 2);
608 relation_value = attr_find (&relation, NULL);
610 switch (relation_value)
613 if (!term_100 (term_sub, term_dict, 1))
615 term_value = atoi (term_dict);
618 logf (LOG_DEBUG, "Relation <");
619 gen_regular_rel (term_dict + strlen(term_dict), term_value-1, 1);
622 if (!term_100 (term_sub, term_dict, 1))
624 term_value = atoi (term_dict);
627 logf (LOG_DEBUG, "Relation <=");
628 gen_regular_rel (term_dict + strlen(term_dict), term_value, 1);
631 if (!term_100 (term_sub, term_dict, 1))
633 term_value = atoi (term_dict);
636 logf (LOG_DEBUG, "Relation >=");
637 gen_regular_rel (term_dict + strlen(term_dict), term_value, 0);
640 if (!term_100 (term_sub, term_dict, 1))
642 term_value = atoi (term_dict);
645 logf (LOG_DEBUG, "Relation >");
646 gen_regular_rel (term_dict + strlen(term_dict), term_value+1, 0);
651 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_dict);
652 r = dict_lookup_grep (zi->dict, term_dict, 0, grep_info, max_pos,
655 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
656 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
660 static int field_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
661 const char **term_sub, int regType,
662 oid_value attributeSet, struct grep_info *grep_info,
663 int num_bases, char **basenames, int space_split)
665 char term_dict[2*IT_MAX_WORD+2];
668 int truncation_value;
671 oid_value curAttributeSet = attributeSet;
674 attr_init (&use, zapt, 1);
675 use_value = attr_find (&use, &curAttributeSet);
676 logf (LOG_DEBUG, "field_term, use value %d", use_value);
677 attr_init (&truncation, zapt, 5);
678 truncation_value = attr_find (&truncation, NULL);
679 logf (LOG_DEBUG, "truncation value %d", truncation_value);
684 for (base_no = 0; base_no < num_bases; base_no++)
687 data1_local_attribute *local_attr;
688 int max_pos, prefix_len = 0;
691 if (!att_getentbyatt (zi, &attp, curAttributeSet, use_value))
693 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
694 curAttributeSet, use_value);
698 if (zebTargetInfo_curDatabase (zi->zti, basenames[base_no]))
700 zi->errCode = 109; /* Database unavailable */
701 zi->errString = basenames[base_no];
704 for (local_attr = attp.local_attributes; local_attr;
705 local_attr = local_attr->next)
709 ord = zebTargetInfo_lookupSU (zi->zti, attp.attset_ordinal,
714 term_dict[prefix_len++] = '|';
716 term_dict[prefix_len++] = '(';
717 term_dict[prefix_len++] = 1;
718 term_dict[prefix_len++] = ord;
725 term_dict[prefix_len++] = ')';
726 term_dict[prefix_len++] = 1;
727 term_dict[prefix_len++] = regType;
728 term_dict[prefix_len] = '\0';
729 if (!relational_term (zi, zapt, &termp, term_dict,
730 attributeSet, grep_info, &max_pos))
733 switch (truncation_value)
735 case -1: /* not specified */
736 case 100: /* do not truncate */
737 term_dict[j++] = '(';
738 if (!term_100 (&termp, term_dict + j, space_split))
740 strcat (term_dict, ")");
741 r = dict_lookup_grep (zi->dict, term_dict, 0, grep_info,
742 &max_pos, 0, grep_handle);
744 logf (LOG_WARN, "dict_lookup_grep err, trunc=none:%d", r);
746 case 1: /* right truncation */
747 term_dict[j++] = '(';
748 if (!term_100 (&termp, term_dict + j, space_split))
750 strcat (term_dict, ".*)");
751 dict_lookup_grep (zi->dict, term_dict, 0, grep_info,
752 &max_pos, 0, grep_handle);
754 case 2: /* left truncation */
755 case 3: /* left&right truncation */
758 case 101: /* process # in term */
759 term_dict[j++] = '(';
760 if (!term_101 (&termp, term_dict + j, space_split))
762 strcat (term_dict, ")");
763 r = dict_lookup_grep (zi->dict, term_dict, 0, grep_info,
764 &max_pos, 0, grep_handle);
766 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d", r);
768 case 102: /* Regexp-1 */
769 term_dict[j++] = '(';
770 if (!term_102 (&termp, term_dict + j, space_split))
772 strcat (term_dict, ")");
773 logf (LOG_DEBUG, "Regexp-1 tolerance=%d", r);
774 r = dict_lookup_grep (zi->dict, term_dict, 0, grep_info,
775 &max_pos, 0, grep_handle);
777 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
780 case 103: /* Regexp-1 */
782 term_dict[j++] = '(';
783 if (!term_103 (&termp, term_dict + j, &r, space_split))
785 strcat (term_dict, ")");
786 logf (LOG_DEBUG, "Regexp-2 tolerance=%d", r);
787 r = dict_lookup_grep (zi->dict, term_dict, r, grep_info,
788 &max_pos, 2, grep_handle);
790 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
797 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
801 static void trans_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
805 Z_Term *term = zapt->term;
807 sizez = term->u.general->len;
808 if (sizez > IT_MAX_WORD-1)
809 sizez = IT_MAX_WORD-1;
810 memcpy (termz, term->u.general->buf, sizez);
814 static void trans_scan_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
817 Z_Term *term = zapt->term;
819 const char *cp = (const char *) term->u.general->buf;
820 const char *cp_end = cp + term->u.general->len;
823 const char *space_map = NULL;
826 while ((len = (cp_end - cp)) > 0)
828 map = map_chrs_input (0, &cp, len);
829 if (**map == *CHR_SPACE)
834 for (src = space_map; *src; src++)
837 for (src = *map; *src; src++)
844 static RSET rpn_search_APT_relevance (ZServerInfo *zi,
845 Z_AttributesPlusTerm *zapt,
846 oid_value attributeSet,
847 int num_bases, char **basenames)
849 rset_relevance_parms parms;
850 char termz[IT_MAX_WORD+1];
851 const char *termp = termz;
852 struct grep_info grep_info;
857 parms.key_size = sizeof(struct it_key);
858 parms.max_rec = 1000;
859 parms.cmp = key_compare_it;
860 parms.get_pos = key_get_pos;
862 parms.isc = zi->isamc;
865 if (zapt->term->which != Z_Term_general)
870 trans_term (zi, zapt, termz);
873 grep_info.term_no = 0;
875 grep_info.isam_p_indx = 0;
876 grep_info.isam_p_size = 0;
877 grep_info.isam_p_buf = NULL;
880 r = field_term (zi, zapt, &termp, 'w', attributeSet, &grep_info,
881 num_bases, basenames, 1);
885 for (; term_index < grep_info.isam_p_indx; term_index++)
886 grep_info.term_no[term_index] = parms.no_terms;
890 parms.term_no = grep_info.term_no;
891 parms.isam_positions = grep_info.isam_p_buf;
892 parms.no_isam_positions = grep_info.isam_p_indx;
893 if (grep_info.isam_p_indx > 0)
894 result = rset_create (rset_kind_relevance, &parms);
896 result = rset_create (rset_kind_null, NULL);
898 xfree(grep_info.term_no);
900 xfree (grep_info.isam_p_buf);
904 static RSET rpn_search_APT_cphrase (ZServerInfo *zi,
905 Z_AttributesPlusTerm *zapt,
906 oid_value attributeSet,
907 int num_bases, char **basenames)
909 char termz[IT_MAX_WORD+1];
910 struct grep_info grep_info;
912 const char *termp = termz;
915 if (zapt->term->which != Z_Term_general)
920 trans_term (zi, zapt, termz);
923 grep_info.term_no = 0;
925 grep_info.isam_p_indx = 0;
926 grep_info.isam_p_size = 0;
927 grep_info.isam_p_buf = NULL;
929 r = field_term (zi, zapt, &termp, 'p', attributeSet, &grep_info,
930 num_bases, basenames, 0);
931 result = rset_trunc (zi, grep_info.isam_p_buf, grep_info.isam_p_indx);
933 xfree(grep_info.term_no);
935 xfree (grep_info.isam_p_buf);
939 static RSET rpn_proximity (ZServerInfo *zi, RSET rset1, RSET rset2,
941 int exclusion, int relation, int distance)
946 struct it_key buf1, buf2;
949 rset_temp_parms parms;
951 rsfd1 = rset_open (rset1, RSETF_READ|RSETF_SORT_SYSNO);
952 more1 = rset_read (rset1, rsfd1, &buf1);
954 rsfd2 = rset_open (rset2, RSETF_READ|RSETF_SORT_SYSNO);
955 more2 = rset_read (rset2, rsfd2, &buf2);
957 parms.key_size = sizeof (struct it_key);
958 parms.temp_path = res_get (zi->res, "setTmpDir");
959 result = rset_create (rset_kind_temp, &parms);
960 rsfd_result = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
962 logf (LOG_DEBUG, "rpn_proximity excl=%d ord=%d rel=%d dis=%d",
963 exclusion, ordered, relation, distance);
964 while (more1 && more2)
966 int cmp = key_compare_it (&buf1, &buf2);
968 more1 = rset_read (rset1, rsfd1, &buf1);
970 more2 = rset_read (rset2, rsfd2, &buf2);
973 int sysno = buf1.sysno;
977 seqno[n++] = buf1.seqno;
978 while ((more1 = rset_read (rset1, rsfd1, &buf1)) &&
981 seqno[n++] = buf1.seqno;
984 for (i = 0; i<n; i++)
986 int diff = buf2.seqno - seqno[i];
987 int excl = exclusion;
988 if (!ordered && diff < 0)
997 if (diff <= distance)
1001 if (diff == distance)
1005 if (diff >= distance)
1009 if (diff > distance)
1013 if (diff != distance)
1018 rset_write (result, rsfd_result, &buf2);
1020 } while ((more2 = rset_read (rset2, rsfd2, &buf2)) &&
1021 sysno == buf2.sysno);
1024 rset_close (result, rsfd_result);
1025 rset_close (rset1, rsfd1);
1026 rset_close (rset2, rsfd2);
1030 static RSET rpn_prox (ZServerInfo *zi, RSET *rset, int rset_no)
1035 struct it_key **buf;
1038 rset_temp_parms parms;
1040 rsfd = xmalloc (sizeof(*rsfd)*rset_no);
1041 more = xmalloc (sizeof(*more)*rset_no);
1042 buf = xmalloc (sizeof(*buf)*rset_no);
1044 for (i = 0; i<rset_no; i++)
1046 buf[i] = xmalloc (sizeof(**buf));
1047 rsfd[i] = rset_open (rset[i], RSETF_READ|RSETF_SORT_SYSNO);
1048 if (!(more[i] = rset_read (rset[i], rsfd[i], buf[i])))
1052 rset_close (rset[i], rsfd[i]);
1059 return rset_create (rset_kind_null, NULL);
1062 parms.key_size = sizeof (struct it_key);
1063 parms.temp_path = res_get (zi->res, "setTmpDir");
1064 result = rset_create (rset_kind_temp, &parms);
1065 rsfd_result = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
1069 for (i = 1; i<rset_no; i++)
1078 cmp = key_compare_it (buf[i], buf[i-1]);
1081 more[i-1] = rset_read (rset[i-1], rsfd[i-1], buf[i-1]);
1086 if (buf[i-1]->seqno+1 != buf[i]->seqno)
1088 more[i-1] = rset_read (rset[i-1], rsfd[i-1], buf[i-1]);
1094 more[i] = rset_read (rset[i], rsfd[i], buf[i]);
1100 rset_write (result, rsfd_result, buf[0]);
1101 more[0] = rset_read (*rset, *rsfd, *buf);
1105 for (i = 0; i<rset_no; i++)
1107 rset_close (rset[i], rsfd[i]);
1110 rset_close (result, rsfd_result);
1117 static RSET rpn_search_APT_phrase (ZServerInfo *zi,
1118 Z_AttributesPlusTerm *zapt,
1119 oid_value attributeSet,
1120 int num_bases, char **basenames)
1122 char termz[IT_MAX_WORD+1];
1123 const char *termp = termz;
1124 RSET rset[60], result;
1125 int i, r, rset_no = 0;
1126 struct grep_info grep_info;
1128 if (zapt->term->which != Z_Term_general)
1133 trans_term (zi, zapt, termz);
1136 grep_info.term_no = 0;
1138 grep_info.isam_p_size = 0;
1139 grep_info.isam_p_buf = NULL;
1143 grep_info.isam_p_indx = 0;
1144 r = field_term (zi, zapt, &termp, 'w', attributeSet, &grep_info,
1145 num_bases, basenames, 1);
1148 rset[rset_no] = rset_trunc (zi, grep_info.isam_p_buf,
1149 grep_info.isam_p_indx);
1150 assert (rset[rset_no]);
1151 if (++rset_no >= sizeof(rset)/sizeof(*rset))
1155 xfree(grep_info.term_no);
1157 xfree (grep_info.isam_p_buf);
1159 return rset_create (rset_kind_null, NULL);
1160 else if (rset_no == 1)
1162 result = rpn_prox (zi, rset, rset_no);
1163 for (i = 0; i<rset_no; i++)
1164 rset_delete (rset[i]);
1168 static RSET rpn_search_APT_local (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1169 oid_value attributeSet)
1174 rset_temp_parms parms;
1175 char termz[IT_MAX_WORD+1];
1177 if (zapt->term->which != Z_Term_general)
1182 parms.key_size = sizeof (struct it_key);
1183 parms.temp_path = res_get (zi->res, "setTmpDir");
1184 result = rset_create (rset_kind_temp, &parms);
1185 rsfd = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
1187 trans_term (zi, zapt, termz);
1189 key.sysno = atoi (termz);
1192 rset_write (result, rsfd, &key);
1193 rset_close (result, rsfd);
1197 static RSET rpn_search_APT (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1198 oid_value attributeSet,
1199 int num_bases, char **basenames)
1203 AttrType completeness;
1204 int relation_value, structure_value, completeness_value;
1206 attr_init (&relation, zapt, 2);
1207 attr_init (&structure, zapt, 4);
1208 attr_init (&completeness, zapt, 6);
1210 relation_value = attr_find (&relation, NULL);
1211 structure_value = attr_find (&structure, NULL);
1212 completeness_value = attr_find (&completeness, NULL);
1213 switch (structure_value)
1216 if (relation_value == 102) /* relevance relation */
1217 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1218 num_bases, basenames);
1219 if (completeness_value == 2 || completeness_value == 3)
1220 return rpn_search_APT_cphrase (zi, zapt, attributeSet,
1221 num_bases, basenames);
1222 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1223 num_bases, basenames);
1224 case 1: /* phrase */
1225 if (relation_value == 102) /* relevance relation */
1226 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1227 num_bases, basenames);
1228 if (completeness_value == 2 || completeness_value == 3)
1229 return rpn_search_APT_cphrase (zi, zapt, attributeSet,
1230 num_bases, basenames);
1231 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1232 num_bases, basenames);
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);
1247 case 5: /* date - normalized */
1249 case 6: /* word list */
1250 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1251 num_bases, basenames);
1252 case 100: /* date - un-normalized */
1254 case 101: /* name - normalized */
1256 case 102: /* date - un-normalized */
1258 case 103: /* structure */
1262 case 105: /* free-form-text */
1263 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1264 num_bases, basenames);
1265 case 106: /* document-text */
1266 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1267 num_bases, basenames);
1268 case 107: /* local-number */
1269 return rpn_search_APT_local (zi, zapt, attributeSet);
1270 case 108: /* string */
1271 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1272 num_bases, basenames);
1273 case 109: /* numeric string */
1280 static RSET rpn_search_ref (ZServerInfo *zi, Z_ResultSetId *resultSetId)
1284 if (!(s = resultSetGet (zi, resultSetId)))
1285 return rset_create (rset_kind_null, NULL);
1289 static RSET rpn_search_structure (ZServerInfo *zi, Z_RPNStructure *zs,
1290 oid_value attributeSet,
1291 int num_bases, char **basenames)
1294 if (zs->which == Z_RPNStructure_complex)
1296 Z_Operator *zop = zs->u.complex->roperator;
1297 rset_bool_parms bool_parms;
1301 bool_parms.rset_l = rpn_search_structure (zi, zs->u.complex->s1,
1303 num_bases, basenames);
1304 if (bool_parms.rset_l == NULL)
1306 if (rset_is_ranked(bool_parms.rset_l))
1308 bool_parms.rset_r = rpn_search_structure (zi, zs->u.complex->s2,
1310 num_bases, basenames);
1311 if (bool_parms.rset_r == NULL)
1313 rset_delete (bool_parms.rset_l);
1316 if (rset_is_ranked(bool_parms.rset_r))
1318 bool_parms.key_size = sizeof(struct it_key);
1319 bool_parms.cmp = key_compare_it;
1323 case Z_Operator_and:
1324 r = rset_create (soft ? rset_kind_sand:rset_kind_and, &bool_parms);
1327 r = rset_create (soft ? rset_kind_sor:rset_kind_or, &bool_parms);
1329 case Z_Operator_and_not:
1330 r = rset_create (soft ? rset_kind_snot:rset_kind_not, &bool_parms);
1332 case Z_Operator_prox:
1333 if (zop->u.prox->which != Z_ProxCode_known)
1338 if (*zop->u.prox->proximityUnitCode != Z_ProxUnit_word)
1340 static char val[16];
1342 zi->errString = val;
1343 sprintf (val, "%d", *zop->u.prox->proximityUnitCode);
1346 r = rpn_proximity (zi, bool_parms.rset_l, bool_parms.rset_r,
1347 *zop->u.prox->ordered,
1348 (!zop->u.prox->exclusion ? 0 :
1349 *zop->u.prox->exclusion),
1350 *zop->u.prox->relationType,
1351 *zop->u.prox->distance);
1358 else if (zs->which == Z_RPNStructure_simple)
1360 if (zs->u.simple->which == Z_Operand_APT)
1362 logf (LOG_DEBUG, "rpn_search_APT");
1363 r = rpn_search_APT (zi, zs->u.simple->u.attributesPlusTerm,
1364 attributeSet, num_bases, basenames);
1366 else if (zs->u.simple->which == Z_Operand_resultSetId)
1368 logf (LOG_DEBUG, "rpn_search_ref");
1369 r = rpn_search_ref (zi, zs->u.simple->u.resultSetId);
1385 void count_set_save (ZServerInfo *zi, RSET *r, int *count)
1392 rset_temp_parms parms;
1393 int maxResultSetSize = atoi (res_get_def (zi->res,
1394 "maxResultSetSize", "400"));
1395 logf (LOG_DEBUG, "count_set_save");
1397 parms.key_size = sizeof(struct it_key);
1398 parms.temp_path = res_get (zi->res, "setTmpDir");
1399 w = rset_create (rset_kind_temp, &parms);
1400 wfd = rset_open (w, RSETF_WRITE|RSETF_SORT_SYSNO);
1401 rfd = rset_open (*r, RSETF_READ|RSETF_SORT_SYSNO);
1402 while (rset_read (*r, rfd, &key))
1404 if (key.sysno != psysno)
1406 if (*count < maxResultSetSize)
1407 rset_write (w, wfd, &key);
1413 rset_close (*r, rfd);
1415 rset_close (w, wfd);
1417 logf (LOG_DEBUG, "%d keys, %d distinct sysnos", kno, *count);
1420 static void count_set (RSET r, int *count)
1427 logf (LOG_DEBUG, "count_set");
1429 rfd = rset_open (r, RSETF_READ|RSETF_SORT_SYSNO);
1430 while (rset_read (r, rfd, &key))
1432 if (key.sysno != psysno)
1439 rset_close (r, rfd);
1440 logf (LOG_DEBUG, "%d keys, %d distinct sysnos", kno, *count);
1443 int rpn_search (ZServerInfo *zi,
1444 Z_RPNQuery *rpn, int num_bases, char **basenames,
1445 const char *setname, int *hits)
1449 oid_value attributeSet;
1451 dict_grep_cmap (zi->dict, 0, map_chrs_input);
1455 zi->errString = NULL;
1457 attrset = oid_getentbyoid (rpn->attributeSetId);
1458 attributeSet = attrset->value;
1459 rset = rpn_search_structure (zi, rpn->RPNStructure, attributeSet,
1460 num_bases, basenames);
1463 if (rset_is_volatile(rset))
1464 count_set_save(zi, &rset,hits);
1466 count_set (rset, hits);
1467 resultSetAdd (zi, setname, 1, rset);
1469 logf (LOG_DEBUG, "search error: %d", zi->errCode);
1473 struct scan_info_entry {
1479 struct scan_info_entry *list;
1485 static int scan_handle (char *name, const char *info, int pos, void *client)
1487 int len_prefix, idx;
1488 struct scan_info *scan_info = client;
1490 len_prefix = strlen(scan_info->prefix);
1491 if (memcmp (name, scan_info->prefix, len_prefix))
1494 idx = scan_info->after - pos + scan_info->before;
1497 scan_info->list[idx].term = odr_malloc (scan_info->odr,
1498 strlen(name + len_prefix)+1);
1499 strcpy (scan_info->list[idx].term, name + len_prefix);
1500 assert (*info == sizeof(ISAM_P));
1501 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAM_P));
1506 static void scan_term_untrans (ODR odr, char **dstp, const char *src)
1508 char *dst = odr_malloc (odr, strlen(src)*2+1);
1513 const char *cp = map_chrs_output (&src);
1520 int rpn_scan (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1521 oid_value attributeset,
1522 int num_bases, char **basenames,
1523 int *position, int *num_entries, struct scan_entry **list,
1527 int pos = *position;
1528 int num = *num_entries;
1532 char termz[IT_MAX_WORD+20];
1535 AttrType completeness;
1536 int completeness_value;
1537 struct scan_info *scan_info_array;
1538 struct scan_entry *glist;
1539 int ords[32], ord_no = 0;
1542 logf (LOG_DEBUG, "scan, position = %d, num = %d", pos, num);
1544 if (attributeset == VAL_NONE)
1545 attributeset = VAL_BIB1;
1547 attr_init (&use, zapt, 1);
1548 use_value = attr_find (&use, &attributeset);
1549 logf (LOG_DEBUG, "use value %d", use_value);
1551 attr_init (&completeness, zapt, 6);
1552 completeness_value = attr_find (&completeness, NULL);
1553 logf (LOG_DEBUG, "completeness value %d", completeness_value);
1555 if (use_value == -1)
1557 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
1560 data1_local_attribute *local_attr;
1562 if (!att_getentbyatt (zi, &attp, attributeset, use_value))
1564 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
1565 attributeset, use_value);
1566 return zi->errCode = 114;
1568 if (zebTargetInfo_curDatabase (zi->zti, basenames[base_no]))
1570 zi->errString = basenames[base_no];
1571 return zi->errCode = 109; /* Database unavailable */
1573 for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
1574 local_attr = local_attr->next)
1578 ord = zebTargetInfo_lookupSU (zi->zti, attp.attset_ordinal,
1581 ords[ord_no++] = ord;
1585 return zi->errCode = 113;
1588 scan_info_array = odr_malloc (zi->odr, ord_no * sizeof(*scan_info_array));
1589 for (i = 0; i < ord_no; i++)
1591 int j, prefix_len = 0;
1592 int before_tmp = before, after_tmp = after;
1593 struct scan_info *scan_info = scan_info_array + i;
1595 scan_info->before = before;
1596 scan_info->after = after;
1597 scan_info->odr = zi->odr;
1599 scan_info->list = odr_malloc (zi->odr, (before+after)*
1600 sizeof(*scan_info->list));
1601 for (j = 0; j<before+after; j++)
1602 scan_info->list[j].term = NULL;
1603 termz[prefix_len++] = ords[i];
1604 termz[prefix_len++] =
1605 (completeness_value==2 || completeness_value==3) ? 'p': 'w';
1606 termz[prefix_len] = 0;
1607 strcpy (scan_info->prefix, termz);
1609 trans_scan_term (zi, zapt, termz+prefix_len);
1611 dict_scan (zi->dict, termz, &before_tmp, &after_tmp, scan_info,
1614 glist = odr_malloc (zi->odr, (before+after)*sizeof(*glist));
1615 for (i = 0; i < ord_no; i++)
1618 *status = BEND_SCAN_SUCCESS;
1619 for (i = 0; i<after; i++)
1622 const char *mterm = NULL;
1626 for (j = 0; j < ord_no; j++)
1628 if (ptr[j] < before+after &&
1629 (tst=scan_info_array[j].list[ptr[j]].term) &&
1630 (!mterm || strcmp (tst, mterm) < 0))
1638 scan_term_untrans (zi->odr, &glist[i+before].term, mterm);
1639 rset = rset_trunc (zi, &scan_info_array[j0].list[ptr[j0]].isam_p, 1);
1642 for (j = j0+1; j<ord_no; j++)
1644 if (ptr[j] < before+after &&
1645 (tst=scan_info_array[j].list[ptr[j]].term) &&
1646 !strcmp (tst, mterm))
1648 rset_bool_parms bool_parms;
1652 rset_trunc (zi, &scan_info_array[j].list[ptr[j]].isam_p, 1);
1654 bool_parms.key_size = sizeof(struct it_key);
1655 bool_parms.cmp = key_compare_it;
1656 bool_parms.rset_l = rset;
1657 bool_parms.rset_r = rset2;
1659 rset = rset_create (rset_kind_or, &bool_parms);
1664 count_set (rset, &glist[i+before].occurrences);
1669 *num_entries -= (after-i);
1670 *status = BEND_SCAN_PARTIAL;
1673 for (i = 0; i<ord_no; i++)
1676 for (i = 0; i<before; i++)
1679 const char *mterm = NULL;
1683 for (j = 0; j <ord_no; j++)
1685 if (ptr[j] < before &&
1686 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
1687 (!mterm || strcmp (tst, mterm) > 0))
1696 scan_term_untrans (zi->odr, &glist[before-1-i].term, mterm);
1699 (zi, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1);
1703 for (j = j0+1; j<ord_no; j++)
1705 if (ptr[j] < before &&
1706 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
1707 !strcmp (tst, mterm))
1709 rset_bool_parms bool_parms;
1712 rset2 = rset_trunc (zi,
1713 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1);
1715 bool_parms.key_size = sizeof(struct it_key);
1716 bool_parms.cmp = key_compare_it;
1717 bool_parms.rset_l = rset;
1718 bool_parms.rset_r = rset2;
1720 rset = rset_create (rset_kind_or, &bool_parms);
1725 count_set (rset, &glist[before-1-i].occurrences);
1731 *status = BEND_SCAN_PARTIAL;
1735 *list = glist + i; /* list is set to first 'real' entry */
1737 logf (LOG_DEBUG, "position = %d, num_entries = %d",
1738 *position, *num_entries);
1740 logf (LOG_DEBUG, "scan error: %d", zi->errCode);