2 * Copyright (C) 1994-1997, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.62 1997-09-05 15:30:09 adam
8 * Changed prototype for chr_map_input - added const.
9 * Added support for C++, headers uses extern "C" for public definitions.
11 * Revision 1.61 1997/02/10 10:21:14 adam
12 * Bug fix: in search terms character (^) wasn't observed.
14 * Revision 1.60 1997/01/31 11:10:34 adam
15 * Bug fix: Leading and trailing white space weren't removed in scan tokens.
17 * Revision 1.59 1997/01/17 11:31:46 adam
18 * Bug fix: complete phrase search didn't work.
20 * Revision 1.58 1996/12/23 15:30:45 adam
22 * Bug fix: result sets weren't deleted after server shut down.
24 * Revision 1.57 1996/11/11 13:38:02 adam
25 * Added proximity support in search.
27 * Revision 1.56 1996/11/08 11:10:32 adam
28 * Buffers used during file match got bigger.
29 * Compressed ISAM support everywhere.
30 * Bug fixes regarding masking characters in queries.
31 * Redesigned Regexp-2 queries.
33 * Revision 1.55 1996/11/04 14:07:44 adam
34 * Moved truncation code to trunc.c.
36 * Revision 1.54 1996/10/29 14:09:52 adam
37 * Use of cisam system - enabled if setting isamc is 1.
39 * Revision 1.53 1996/06/26 09:21:43 adam
40 * Bug fix: local attribute set wasn't obeyed in scan.
42 * Revision 1.52 1996/06/17 14:26:20 adam
43 * Function gen_regular_rel changed to handle negative numbers.
45 * Revision 1.51 1996/06/11 10:54:15 quinn
48 * Revision 1.50 1996/06/07 08:51:53 adam
49 * Bug fix: Character mapping was broken (introducued by last revision).
51 * Revision 1.49 1996/06/04 10:18:11 adam
52 * Search/scan uses character mapping module.
54 * Revision 1.48 1996/05/28 15:15:01 adam
55 * Bug fix: Didn't handle unknown database correctly.
57 * Revision 1.47 1996/05/15 18:36:28 adam
58 * Function trans_term transforms unsearchable characters to blanks.
60 * Revision 1.46 1996/05/15 11:57:56 adam
61 * Fixed bug introduced by set/field mapping in search operations.
63 * Revision 1.45 1996/05/14 11:34:00 adam
64 * Scan support in multiple registers/databases.
66 * Revision 1.44 1996/05/14 06:16:44 adam
67 * Compact use/set bytes used in search service.
69 * Revision 1.43 1996/05/09 09:54:43 adam
70 * Server supports maps from one logical attributes to a list of physical
72 * The extraction process doesn't make space consuming 'any' keys.
74 * Revision 1.42 1996/05/09 07:28:56 quinn
75 * Work towards phrases and multiple registers
77 * Revision 1.41 1996/03/20 09:36:43 adam
78 * Function dict_lookup_grep got extra parameter, init_pos, which marks
79 * from which position in pattern approximate pattern matching should occur.
80 * Approximate pattern matching is used in relevance=re-2.
82 * Revision 1.40 1996/02/02 13:44:44 adam
83 * The public dictionary functions simply use char instead of Dict_char
84 * to represent search strings. Dict_char is used internally only.
86 * Revision 1.39 1996/01/03 16:22:13 quinn
89 * Revision 1.38 1995/12/11 09:12:55 adam
90 * The rec_get function returns NULL if record doesn't exist - will
91 * happen in the server if the result set records have been deleted since
92 * the creation of the set (i.e. the search).
93 * The server saves a result temporarily if it is 'volatile', i.e. the
94 * set is register dependent.
96 * Revision 1.37 1995/12/06 15:05:28 adam
97 * More verbose in count_set.
99 * Revision 1.36 1995/12/06 12:41:27 adam
100 * New command 'stat' for the index program.
101 * Filenames can be read from stdin by specifying '-'.
102 * Bug fix/enhancement of the transformation from terms to regular
103 * expressons in the search engine.
105 * Revision 1.35 1995/11/27 09:29:00 adam
106 * Bug fixes regarding conversion to regular expressions.
108 * Revision 1.34 1995/11/16 17:00:56 adam
109 * Better logging of rpn query.
111 * Revision 1.33 1995/11/01 13:58:28 quinn
112 * Moving data1 to yaz/retrieval
114 * Revision 1.32 1995/10/27 14:00:11 adam
115 * Implemented detection of database availability.
117 * Revision 1.31 1995/10/17 18:02:10 adam
118 * New feature: databases. Implemented as prefix to words in dictionary.
120 * Revision 1.30 1995/10/16 09:32:38 adam
121 * More work on relational op.
123 * Revision 1.29 1995/10/13 16:01:49 adam
126 * Revision 1.28 1995/10/13 12:26:43 adam
127 * Optimization of truncation.
129 * Revision 1.27 1995/10/12 17:07:22 adam
132 * Revision 1.26 1995/10/12 12:40:54 adam
133 * Bug fixes in rpn_prox.
135 * Revision 1.25 1995/10/10 13:59:24 adam
136 * Function rset_open changed its wflag parameter to general flags.
138 * Revision 1.24 1995/10/09 16:18:37 adam
139 * Function dict_lookup_grep got extra client data parameter.
141 * Revision 1.23 1995/10/06 16:33:37 adam
142 * Use attribute mappings.
144 * Revision 1.22 1995/10/06 15:07:39 adam
145 * Structure 'local-number' handled.
147 * Revision 1.21 1995/10/06 13:52:06 adam
148 * Bug fixes. Handler may abort further scanning.
150 * Revision 1.20 1995/10/06 11:06:33 adam
151 * Scan entries include 'occurrences' now.
153 * Revision 1.19 1995/10/06 10:43:56 adam
154 * Scan added. 'occurrences' in scan entries not set yet.
156 * Revision 1.18 1995/10/04 16:57:20 adam
157 * Key input and merge sort in one pass.
159 * Revision 1.17 1995/10/04 12:55:17 adam
160 * Bug fix in ranked search. Use=Any keys inserted.
162 * Revision 1.16 1995/10/02 16:24:40 adam
163 * Use attribute actually used in search requests.
165 * Revision 1.15 1995/10/02 15:18:52 adam
166 * New member in recRetrieveCtrl: diagnostic.
168 * Revision 1.14 1995/09/28 12:10:32 adam
169 * Bug fixes. Field prefix used in queries.
171 * Revision 1.13 1995/09/18 14:17:50 adam
174 * Revision 1.12 1995/09/15 14:45:21 adam
176 * Work on truncation.
178 * Revision 1.11 1995/09/14 11:53:27 adam
179 * First work on regular expressions/truncations.
181 * Revision 1.10 1995/09/11 15:23:26 adam
182 * More work on relevance search.
184 * Revision 1.9 1995/09/11 13:09:35 adam
185 * More work on relevance feedback.
187 * Revision 1.8 1995/09/08 14:52:27 adam
188 * Minor changes. Dictionary is lower case now.
190 * Revision 1.7 1995/09/07 13:58:36 adam
191 * New parameter: result-set file descriptor (RSFD) to support multiple
192 * positions within the same result-set.
193 * Boolean operators: and, or, not implemented.
194 * Result-set references.
196 * Revision 1.6 1995/09/06 16:11:18 adam
197 * Option: only one word key per file.
199 * Revision 1.5 1995/09/06 10:33:04 adam
200 * More work on present. Some log messages removed.
202 * Revision 1.4 1995/09/05 15:28:40 adam
203 * More work on search engine.
205 * Revision 1.3 1995/09/04 15:20:22 adam
208 * Revision 1.2 1995/09/04 12:33:43 adam
209 * Various cleanup. YAZ util used instead.
211 * Revision 1.1 1995/09/04 09:10:40 adam
212 * More work on index add/del/update.
213 * Merge sort implemented.
214 * Initial work on z39 server.
223 #include "attribute.h"
235 Z_AttributesPlusTerm *zapt;
238 static int attr_find (AttrType *src, oid_value *attributeSetP)
240 while (src->major < src->zapt->num_attributes)
242 Z_AttributeElement *element;
244 element = src->zapt->attributeList[src->major];
245 if (src->type == *element->attributeType)
247 switch (element->which)
249 case Z_AttributeValue_numeric:
251 if (element->attributeSet && attributeSetP)
255 attrset = oid_getentbyoid (element->attributeSet);
256 *attributeSetP = attrset->value;
258 return *element->value.numeric;
260 case Z_AttributeValue_complex:
261 if (src->minor >= element->value.complex->num_list ||
262 element->value.complex->list[src->minor]->which !=
263 Z_StringOrNumeric_numeric)
266 if (element->attributeSet && attributeSetP)
270 attrset = oid_getentbyoid (element->attributeSet);
271 *attributeSetP = attrset->value;
273 return *element->value.complex->list[src->minor-1]->u.numeric;
283 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
303 static void add_isam_p (const char *info, struct grep_info *p)
305 if (p->isam_p_indx == p->isam_p_size)
307 ISAM_P *new_isam_p_buf;
312 p->isam_p_size = 2*p->isam_p_size + 100;
313 new_isam_p_buf = xmalloc (sizeof(*new_isam_p_buf) *
317 memcpy (new_isam_p_buf, p->isam_p_buf,
318 p->isam_p_indx * sizeof(*p->isam_p_buf));
319 xfree (p->isam_p_buf);
321 p->isam_p_buf = new_isam_p_buf;
324 new_term_no = xmalloc (sizeof(*new_term_no) *
328 memcpy (new_term_no, p->isam_p_buf,
329 p->isam_p_indx * sizeof(*p->term_no));
332 p->term_no = new_term_no;
335 assert (*info == sizeof(*p->isam_p_buf));
336 memcpy (p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
340 static int grep_handle (char *name, const char *info, void *p)
342 add_isam_p (info, p);
346 static int term_pre (const char **src, const char *ct1, const char *ct2)
348 const char *s1, *s0 = *src;
351 /* skip white space */
354 if (ct1 && strchr (ct1, *s0))
356 if (ct2 && strchr (ct2, *s0))
359 map = map_chrs_input (&s1, strlen(s1));
360 if (**map != *CHR_SPACE)
368 static int term_100 (const char **src, char *dst, int space_split)
374 if (!term_pre (src, NULL, NULL))
380 map = map_chrs_input (&s0, strlen(s0));
381 if (space_split && **map == *CHR_SPACE)
395 static int term_101 (const char **src, char *dst, int space_split)
401 if (!term_pre (src, "#", "#"))
415 map = map_chrs_input (&s0, strlen(s0));
416 if (space_split && **map == *CHR_SPACE)
432 static int term_103 (const char **src, char *dst, int *errors, int space_split)
438 if (!term_pre (src, "^\\()[].*+?|", "("))
441 if (errors && *s0 == '+' && s0[1] && s0[2] == '+' && s0[3] &&
444 *errors = s0[1] - '0';
451 if (strchr ("^\\()[].*+?|-", *s0))
456 map = map_chrs_input (&s0, strlen(s0));
457 if (**map == *CHR_SPACE)
472 static int term_102 (const char **src, char *dst, int space_split)
474 return term_103 (src, dst, NULL, space_split);
477 /* gen_regular_rel - generate regular expression from relation
478 * val: border value (inclusive)
479 * islt: 1 if <=; 0 if >=.
481 static void gen_regular_rel (char *dst, int val, int islt)
488 logf (LOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
492 strcpy (dst, "(-[0-9]+|");
500 strcpy (dst, "([0-9]+|-");
501 dst_p = strlen (dst);
511 dst_p = strlen (dst);
512 sprintf (numstr, "%d", val);
513 for (w = strlen(numstr); --w >= 0; pos++)
532 strcpy (dst + dst_p, numstr);
533 dst_p = strlen(dst) - pos - 1;
561 for (i = 0; i<pos; i++)
574 for (i=1; i<pos; i++)
575 strcat (dst, "[0-9]?");
579 for (i = 0; i <= pos; i++)
580 strcat (dst, "[0-9]");
581 strcat (dst, "[0-9]*");
586 static int relational_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
587 const char **term_sub,
589 oid_value attributeSet,
590 struct grep_info *grep_info,
598 attr_init (&relation, zapt, 2);
599 relation_value = attr_find (&relation, NULL);
601 switch (relation_value)
604 if (!term_100 (term_sub, term_dict, 1))
606 term_value = atoi (term_dict);
609 logf (LOG_DEBUG, "Relation <");
610 gen_regular_rel (term_dict + strlen(term_dict), term_value-1, 1);
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);
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, 0);
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+1, 0);
642 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_dict);
643 r = dict_lookup_grep (zi->dict, term_dict, 0, grep_info, max_pos,
646 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
647 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
651 static int field_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
652 const char **term_sub, int regType,
653 oid_value attributeSet, struct grep_info *grep_info,
654 int num_bases, char **basenames, int space_split)
656 char term_dict[2*IT_MAX_WORD+2];
659 int truncation_value;
662 oid_value curAttributeSet = attributeSet;
665 attr_init (&use, zapt, 1);
666 use_value = attr_find (&use, &curAttributeSet);
667 logf (LOG_DEBUG, "field_term, use value %d", use_value);
668 attr_init (&truncation, zapt, 5);
669 truncation_value = attr_find (&truncation, NULL);
670 logf (LOG_DEBUG, "truncation value %d", truncation_value);
675 for (base_no = 0; base_no < num_bases; base_no++)
678 data1_local_attribute *local_attr;
679 int max_pos, prefix_len = 0;
682 attp = att_getentbyatt (curAttributeSet, use_value);
685 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
686 curAttributeSet, use_value);
690 if (zebTargetInfo_curDatabase (zi->zti, basenames[base_no]))
692 zi->errCode = 109; /* Database unavailable */
693 zi->errString = basenames[base_no];
696 for (local_attr = attp->local_attributes; local_attr;
697 local_attr = local_attr->next)
701 ord = zebTargetInfo_lookupSU (zi->zti, attp->attset_ordinal,
706 term_dict[prefix_len++] = '|';
708 term_dict[prefix_len++] = '(';
709 term_dict[prefix_len++] = 1;
710 term_dict[prefix_len++] = ord;
717 term_dict[prefix_len++] = ')';
718 term_dict[prefix_len++] = 1;
719 term_dict[prefix_len++] = regType;
720 term_dict[prefix_len] = '\0';
721 if (!relational_term (zi, zapt, &termp, term_dict,
722 attributeSet, grep_info, &max_pos))
725 switch (truncation_value)
727 case -1: /* not specified */
728 case 100: /* do not truncate */
729 term_dict[j++] = '(';
730 if (!term_100 (&termp, term_dict + j, space_split))
732 strcat (term_dict, ")");
733 r = dict_lookup_grep (zi->dict, term_dict, 0, grep_info,
734 &max_pos, 0, grep_handle);
736 logf (LOG_WARN, "dict_lookup_grep err, trunc=none:%d", r);
738 case 1: /* right truncation */
739 term_dict[j++] = '(';
740 if (!term_100 (&termp, term_dict + j, space_split))
742 strcat (term_dict, ".*)");
743 dict_lookup_grep (zi->dict, term_dict, 0, grep_info,
744 &max_pos, 0, grep_handle);
746 case 2: /* left truncation */
747 case 3: /* left&right truncation */
750 case 101: /* process # in term */
751 term_dict[j++] = '(';
752 if (!term_101 (&termp, term_dict + j, space_split))
754 strcat (term_dict, ")");
755 r = dict_lookup_grep (zi->dict, term_dict, 0, grep_info,
756 &max_pos, 0, grep_handle);
758 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d", r);
760 case 102: /* Regexp-1 */
761 term_dict[j++] = '(';
762 if (!term_102 (&termp, term_dict + j, space_split))
764 strcat (term_dict, ")");
765 logf (LOG_DEBUG, "Regexp-1 tolerance=%d", r);
766 r = dict_lookup_grep (zi->dict, term_dict, 0, grep_info,
767 &max_pos, 0, grep_handle);
769 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
772 case 103: /* Regexp-1 */
774 term_dict[j++] = '(';
775 if (!term_103 (&termp, term_dict + j, &r, space_split))
777 strcat (term_dict, ")");
778 logf (LOG_DEBUG, "Regexp-2 tolerance=%d", r);
779 r = dict_lookup_grep (zi->dict, term_dict, r, grep_info,
780 &max_pos, 2, grep_handle);
782 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
789 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
793 static void trans_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
797 Z_Term *term = zapt->term;
799 sizez = term->u.general->len;
800 if (sizez > IT_MAX_WORD-1)
801 sizez = IT_MAX_WORD-1;
802 memcpy (termz, term->u.general->buf, sizez);
806 static void trans_scan_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
809 Z_Term *term = zapt->term;
811 const char *cp = (const char *) term->u.general->buf;
812 const char *cp_end = cp + term->u.general->len;
815 const char *space_map = NULL;
818 while ((len = (cp_end - cp)) > 0)
820 map = map_chrs_input (&cp, len);
821 if (**map == *CHR_SPACE)
826 for (src = space_map; *src; src++)
829 for (src = *map; *src; src++)
836 static RSET rpn_search_APT_relevance (ZServerInfo *zi,
837 Z_AttributesPlusTerm *zapt,
838 oid_value attributeSet,
839 int num_bases, char **basenames)
841 rset_relevance_parms parms;
842 char termz[IT_MAX_WORD+1];
843 const char *termp = termz;
844 struct grep_info grep_info;
849 parms.key_size = sizeof(struct it_key);
850 parms.max_rec = 1000;
851 parms.cmp = key_compare_it;
853 parms.isc = zi->isamc;
856 if (zapt->term->which != Z_Term_general)
861 trans_term (zi, zapt, termz);
864 grep_info.term_no = 0;
866 grep_info.isam_p_indx = 0;
867 grep_info.isam_p_size = 0;
868 grep_info.isam_p_buf = NULL;
871 r = field_term (zi, zapt, &termp, 'w', attributeSet, &grep_info,
872 num_bases, basenames, 1);
876 for (; term_index < grep_info.isam_p_indx; term_index++)
877 grep_info.term_no[term_index] = parms.no_terms;
881 parms.term_no = grep_info.term_no;
882 parms.isam_positions = grep_info.isam_p_buf;
883 parms.no_isam_positions = grep_info.isam_p_indx;
884 if (grep_info.isam_p_indx > 0)
885 result = rset_create (rset_kind_relevance, &parms);
887 result = rset_create (rset_kind_null, NULL);
889 xfree(grep_info.term_no);
891 xfree (grep_info.isam_p_buf);
895 static RSET rpn_search_APT_cphrase (ZServerInfo *zi,
896 Z_AttributesPlusTerm *zapt,
897 oid_value attributeSet,
898 int num_bases, char **basenames)
900 char termz[IT_MAX_WORD+1];
901 struct grep_info grep_info;
903 const char *termp = termz;
906 if (zapt->term->which != Z_Term_general)
911 trans_term (zi, zapt, termz);
914 grep_info.term_no = 0;
916 grep_info.isam_p_indx = 0;
917 grep_info.isam_p_size = 0;
918 grep_info.isam_p_buf = NULL;
920 r = field_term (zi, zapt, &termp, 'p', attributeSet, &grep_info,
921 num_bases, basenames, 0);
922 result = rset_trunc (zi, grep_info.isam_p_buf, grep_info.isam_p_indx);
924 xfree(grep_info.term_no);
926 xfree (grep_info.isam_p_buf);
930 static RSET rpn_proximity (RSET rset1, RSET rset2, int ordered,
931 int exclusion, int relation, int distance)
936 struct it_key buf1, buf2;
939 rset_temp_parms parms;
941 rsfd1 = rset_open (rset1, RSETF_READ|RSETF_SORT_SYSNO);
942 more1 = rset_read (rset1, rsfd1, &buf1);
944 rsfd2 = rset_open (rset2, RSETF_READ|RSETF_SORT_SYSNO);
945 more2 = rset_read (rset2, rsfd2, &buf2);
947 parms.key_size = sizeof (struct it_key);
948 result = rset_create (rset_kind_temp, &parms);
949 rsfd_result = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
951 logf (LOG_DEBUG, "rpn_proximity excl=%d ord=%d rel=%d dis=%d",
952 exclusion, ordered, relation, distance);
953 while (more1 && more2)
955 int cmp = key_compare_it (&buf1, &buf2);
957 more1 = rset_read (rset1, rsfd1, &buf1);
959 more2 = rset_read (rset2, rsfd2, &buf2);
962 int sysno = buf1.sysno;
966 seqno[n++] = buf1.seqno;
967 while ((more1 = rset_read (rset1, rsfd1, &buf1)) &&
970 seqno[n++] = buf1.seqno;
973 for (i = 0; i<n; i++)
975 int diff = buf2.seqno - seqno[i];
976 int excl = exclusion;
977 if (!ordered && diff < 0)
986 if (diff <= distance)
990 if (diff == distance)
994 if (diff >= distance)
1002 if (diff != distance)
1007 rset_write (result, rsfd_result, &buf2);
1009 } while ((more2 = rset_read (rset2, rsfd2, &buf2)) &&
1010 sysno == buf2.sysno);
1013 rset_close (result, rsfd_result);
1014 rset_close (rset1, rsfd1);
1015 rset_close (rset2, rsfd2);
1019 static RSET rpn_prox (RSET *rset, int rset_no)
1024 struct it_key **buf;
1027 rset_temp_parms parms;
1029 rsfd = xmalloc (sizeof(*rsfd)*rset_no);
1030 more = xmalloc (sizeof(*more)*rset_no);
1031 buf = xmalloc (sizeof(*buf)*rset_no);
1033 for (i = 0; i<rset_no; i++)
1035 buf[i] = xmalloc (sizeof(**buf));
1036 rsfd[i] = rset_open (rset[i], RSETF_READ|RSETF_SORT_SYSNO);
1037 if (!(more[i] = rset_read (rset[i], rsfd[i], buf[i])))
1041 rset_close (rset[i], rsfd[i]);
1048 return rset_create (rset_kind_null, NULL);
1051 parms.key_size = sizeof (struct it_key);
1052 result = rset_create (rset_kind_temp, &parms);
1053 rsfd_result = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
1057 for (i = 1; i<rset_no; i++)
1066 cmp = key_compare_it (buf[i], buf[i-1]);
1069 more[i-1] = rset_read (rset[i-1], rsfd[i-1], buf[i-1]);
1074 if (buf[i-1]->seqno+1 != buf[i]->seqno)
1076 more[i-1] = rset_read (rset[i-1], rsfd[i-1], buf[i-1]);
1082 more[i] = rset_read (rset[i], rsfd[i], buf[i]);
1088 rset_write (result, rsfd_result, buf[0]);
1089 more[0] = rset_read (*rset, *rsfd, *buf);
1093 for (i = 0; i<rset_no; i++)
1095 rset_close (rset[i], rsfd[i]);
1098 rset_close (result, rsfd_result);
1105 static RSET rpn_search_APT_phrase (ZServerInfo *zi,
1106 Z_AttributesPlusTerm *zapt,
1107 oid_value attributeSet,
1108 int num_bases, char **basenames)
1110 char termz[IT_MAX_WORD+1];
1111 const char *termp = termz;
1112 RSET rset[60], result;
1113 int i, r, rset_no = 0;
1114 struct grep_info grep_info;
1116 if (zapt->term->which != Z_Term_general)
1121 trans_term (zi, zapt, termz);
1124 grep_info.term_no = 0;
1126 grep_info.isam_p_size = 0;
1127 grep_info.isam_p_buf = NULL;
1131 grep_info.isam_p_indx = 0;
1132 r = field_term (zi, zapt, &termp, 'w', attributeSet, &grep_info,
1133 num_bases, basenames, 1);
1136 rset[rset_no] = rset_trunc (zi, grep_info.isam_p_buf,
1137 grep_info.isam_p_indx);
1138 assert (rset[rset_no]);
1139 if (++rset_no >= sizeof(rset)/sizeof(*rset))
1143 xfree(grep_info.term_no);
1145 xfree (grep_info.isam_p_buf);
1147 return rset_create (rset_kind_null, NULL);
1148 else if (rset_no == 1)
1150 result = rpn_prox (rset, rset_no);
1151 for (i = 0; i<rset_no; i++)
1152 rset_delete (rset[i]);
1156 static RSET rpn_search_APT_local (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1157 oid_value attributeSet)
1162 rset_temp_parms parms;
1163 char termz[IT_MAX_WORD+1];
1165 if (zapt->term->which != Z_Term_general)
1170 parms.key_size = sizeof (struct it_key);
1171 result = rset_create (rset_kind_temp, &parms);
1172 rsfd = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
1174 trans_term (zi, zapt, termz);
1176 key.sysno = atoi (termz);
1179 rset_write (result, rsfd, &key);
1180 rset_close (result, rsfd);
1184 static RSET rpn_search_APT (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1185 oid_value attributeSet,
1186 int num_bases, char **basenames)
1190 AttrType completeness;
1191 int relation_value, structure_value, completeness_value;
1193 attr_init (&relation, zapt, 2);
1194 attr_init (&structure, zapt, 4);
1195 attr_init (&completeness, zapt, 6);
1197 relation_value = attr_find (&relation, NULL);
1198 structure_value = attr_find (&structure, NULL);
1199 completeness_value = attr_find (&completeness, NULL);
1200 switch (structure_value)
1203 if (relation_value == 102) /* relevance relation */
1204 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1205 num_bases, basenames);
1206 if (completeness_value == 2 || completeness_value == 3)
1207 return rpn_search_APT_cphrase (zi, zapt, attributeSet,
1208 num_bases, basenames);
1209 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1210 num_bases, basenames);
1211 case 1: /* phrase */
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);
1222 if (relation_value == 102) /* relevance relation */
1223 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1224 num_bases, basenames);
1225 if (completeness_value == 2 || completeness_value == 3)
1226 return rpn_search_APT_cphrase (zi, zapt, attributeSet,
1227 num_bases, basenames);
1228 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1229 num_bases, basenames);
1234 case 5: /* date - normalized */
1236 case 6: /* word list */
1237 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1238 num_bases, basenames);
1239 case 100: /* date - un-normalized */
1241 case 101: /* name - normalized */
1243 case 102: /* date - un-normalized */
1245 case 103: /* structure */
1249 case 105: /* free-form-text */
1250 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1251 num_bases, basenames);
1252 case 106: /* document-text */
1253 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1254 num_bases, basenames);
1255 case 107: /* local-number */
1256 return rpn_search_APT_local (zi, zapt, attributeSet);
1257 case 108: /* string */
1258 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1259 num_bases, basenames);
1260 case 109: /* numeric string */
1267 static RSET rpn_search_ref (ZServerInfo *zi, Z_ResultSetId *resultSetId)
1271 if (!(s = resultSetGet (zi, resultSetId)))
1272 return rset_create (rset_kind_null, NULL);
1276 static RSET rpn_search_structure (ZServerInfo *zi, Z_RPNStructure *zs,
1277 oid_value attributeSet,
1278 int num_bases, char **basenames)
1281 if (zs->which == Z_RPNStructure_complex)
1283 Z_Operator *zop = zs->u.complex->roperator;
1284 rset_bool_parms bool_parms;
1288 bool_parms.rset_l = rpn_search_structure (zi, zs->u.complex->s1,
1290 num_bases, basenames);
1291 if (bool_parms.rset_l == NULL)
1293 if (rset_is_ranked(bool_parms.rset_l))
1295 bool_parms.rset_r = rpn_search_structure (zi, zs->u.complex->s2,
1297 num_bases, basenames);
1298 if (bool_parms.rset_r == NULL)
1300 rset_delete (bool_parms.rset_l);
1303 if (rset_is_ranked(bool_parms.rset_r))
1305 bool_parms.key_size = sizeof(struct it_key);
1306 bool_parms.cmp = key_compare_it;
1310 case Z_Operator_and:
1311 r = rset_create (soft ? rset_kind_sand:rset_kind_and, &bool_parms);
1314 r = rset_create (soft ? rset_kind_sor:rset_kind_or, &bool_parms);
1316 case Z_Operator_and_not:
1317 r = rset_create (soft ? rset_kind_snot:rset_kind_not, &bool_parms);
1319 case Z_Operator_prox:
1320 if (zop->u.prox->which != Z_ProxCode_known)
1325 if (*zop->u.prox->proximityUnitCode != Z_ProxUnit_word)
1327 static char val[16];
1329 zi->errString = val;
1330 sprintf (val, "%d", *zop->u.prox->proximityUnitCode);
1333 r = rpn_proximity (bool_parms.rset_l, bool_parms.rset_r,
1334 *zop->u.prox->ordered,
1335 (!zop->u.prox->exclusion ? 0 :
1336 *zop->u.prox->exclusion),
1337 *zop->u.prox->relationType,
1338 *zop->u.prox->distance);
1345 else if (zs->which == Z_RPNStructure_simple)
1347 if (zs->u.simple->which == Z_Operand_APT)
1349 logf (LOG_DEBUG, "rpn_search_APT");
1350 r = rpn_search_APT (zi, zs->u.simple->u.attributesPlusTerm,
1351 attributeSet, num_bases, basenames);
1353 else if (zs->u.simple->which == Z_Operand_resultSetId)
1355 logf (LOG_DEBUG, "rpn_search_ref");
1356 r = rpn_search_ref (zi, zs->u.simple->u.resultSetId);
1372 void count_set_save (RSET *r, int *count)
1379 rset_temp_parms parms;
1380 int maxResultSetSize = atoi (res_get_def (common_resource,
1381 "maxResultSetSize", "400"));
1382 logf (LOG_DEBUG, "count_set_save");
1384 parms.key_size = sizeof(struct it_key);
1385 w = rset_create (rset_kind_temp, &parms);
1386 wfd = rset_open (w, RSETF_WRITE|RSETF_SORT_SYSNO);
1387 rfd = rset_open (*r, RSETF_READ|RSETF_SORT_SYSNO);
1388 while (rset_read (*r, rfd, &key))
1390 if (key.sysno != psysno)
1392 if (*count < maxResultSetSize)
1393 rset_write (w, wfd, &key);
1399 rset_close (*r, rfd);
1401 rset_close (w, wfd);
1403 logf (LOG_DEBUG, "%d keys, %d distinct sysnos", kno, *count);
1406 static void count_set (RSET r, int *count)
1413 logf (LOG_DEBUG, "count_set");
1415 rfd = rset_open (r, RSETF_READ|RSETF_SORT_SYSNO);
1416 while (rset_read (r, rfd, &key))
1418 if (key.sysno != psysno)
1425 rset_close (r, rfd);
1426 logf (LOG_DEBUG, "%d keys, %d distinct sysnos", kno, *count);
1429 int rpn_search (ZServerInfo *zi,
1430 Z_RPNQuery *rpn, int num_bases, char **basenames,
1431 const char *setname, int *hits)
1435 oid_value attributeSet;
1437 dict_grep_cmap (zi->dict, map_chrs_input);
1441 zi->errString = NULL;
1443 attrset = oid_getentbyoid (rpn->attributeSetId);
1444 attributeSet = attrset->value;
1445 rset = rpn_search_structure (zi, rpn->RPNStructure, attributeSet,
1446 num_bases, basenames);
1449 if (rset_is_volatile(rset))
1450 count_set_save(&rset,hits);
1452 count_set (rset, hits);
1453 resultSetAdd (zi, setname, 1, rset);
1455 logf (LOG_DEBUG, "search error: %d", zi->errCode);
1459 struct scan_info_entry {
1465 struct scan_info_entry *list;
1471 static int scan_handle (char *name, const char *info, int pos, void *client)
1473 int len_prefix, idx;
1474 struct scan_info *scan_info = client;
1476 len_prefix = strlen(scan_info->prefix);
1477 if (memcmp (name, scan_info->prefix, len_prefix))
1480 idx = scan_info->after - pos + scan_info->before;
1483 scan_info->list[idx].term = odr_malloc (scan_info->odr,
1484 strlen(name + len_prefix)+1);
1485 strcpy (scan_info->list[idx].term, name + len_prefix);
1486 assert (*info == sizeof(ISAM_P));
1487 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAM_P));
1492 static void scan_term_untrans (ODR odr, char **dstp, const char *src)
1494 char *dst = odr_malloc (odr, strlen(src)*2+1);
1499 const char *cp = map_chrs_output (&src);
1506 int rpn_scan (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1507 oid_value attributeset,
1508 int num_bases, char **basenames,
1509 int *position, int *num_entries, struct scan_entry **list,
1513 int pos = *position;
1514 int num = *num_entries;
1518 char termz[IT_MAX_WORD+20];
1521 AttrType completeness;
1522 int completeness_value;
1523 struct scan_info *scan_info_array;
1524 struct scan_entry *glist;
1525 int ords[32], ord_no = 0;
1528 logf (LOG_DEBUG, "scan, position = %d, num = %d", pos, num);
1530 if (attributeset == VAL_NONE)
1531 attributeset = VAL_BIB1;
1533 attr_init (&use, zapt, 1);
1534 use_value = attr_find (&use, &attributeset);
1535 logf (LOG_DEBUG, "use value %d", use_value);
1537 attr_init (&completeness, zapt, 6);
1538 completeness_value = attr_find (&completeness, NULL);
1539 logf (LOG_DEBUG, "completeness value %d", completeness_value);
1541 if (use_value == -1)
1543 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
1546 data1_local_attribute *local_attr;
1548 attp = att_getentbyatt (attributeset, use_value);
1551 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
1552 attributeset, use_value);
1553 return zi->errCode = 114;
1555 if (zebTargetInfo_curDatabase (zi->zti, basenames[base_no]))
1557 zi->errString = basenames[base_no];
1558 return zi->errCode = 109; /* Database unavailable */
1560 for (local_attr = attp->local_attributes; local_attr && ord_no < 32;
1561 local_attr = local_attr->next)
1565 ord = zebTargetInfo_lookupSU (zi->zti, attp->attset_ordinal,
1568 ords[ord_no++] = ord;
1572 return zi->errCode = 113;
1575 scan_info_array = odr_malloc (zi->odr, ord_no * sizeof(*scan_info_array));
1576 for (i = 0; i < ord_no; i++)
1578 int j, prefix_len = 0;
1579 int before_tmp = before, after_tmp = after;
1580 struct scan_info *scan_info = scan_info_array + i;
1582 scan_info->before = before;
1583 scan_info->after = after;
1584 scan_info->odr = zi->odr;
1586 scan_info->list = odr_malloc (zi->odr, (before+after)*
1587 sizeof(*scan_info->list));
1588 for (j = 0; j<before+after; j++)
1589 scan_info->list[j].term = NULL;
1590 termz[prefix_len++] = ords[i];
1591 termz[prefix_len++] =
1592 (completeness_value==2 || completeness_value==3) ? 'p': 'w';
1593 termz[prefix_len] = 0;
1594 strcpy (scan_info->prefix, termz);
1596 trans_scan_term (zi, zapt, termz+prefix_len);
1598 dict_scan (zi->dict, termz, &before_tmp, &after_tmp, scan_info,
1601 glist = odr_malloc (zi->odr, (before+after)*sizeof(*glist));
1602 for (i = 0; i < ord_no; i++)
1605 *status = BEND_SCAN_SUCCESS;
1606 for (i = 0; i<after; i++)
1609 const char *mterm = NULL;
1613 for (j = 0; j < ord_no; j++)
1615 if (ptr[j] < before+after &&
1616 (tst=scan_info_array[j].list[ptr[j]].term) &&
1617 (!mterm || strcmp (tst, mterm) < 0))
1625 scan_term_untrans (zi->odr, &glist[i+before].term, mterm);
1626 rset = rset_trunc (zi, &scan_info_array[j0].list[ptr[j0]].isam_p, 1);
1629 for (j = j0+1; j<ord_no; j++)
1631 if (ptr[j] < before+after &&
1632 (tst=scan_info_array[j].list[ptr[j]].term) &&
1633 !strcmp (tst, mterm))
1635 rset_bool_parms bool_parms;
1639 rset_trunc (zi, &scan_info_array[j].list[ptr[j]].isam_p, 1);
1641 bool_parms.key_size = sizeof(struct it_key);
1642 bool_parms.cmp = key_compare_it;
1643 bool_parms.rset_l = rset;
1644 bool_parms.rset_r = rset2;
1646 rset = rset_create (rset_kind_or, &bool_parms);
1651 count_set (rset, &glist[i+before].occurrences);
1656 *num_entries -= (after-i);
1657 *status = BEND_SCAN_PARTIAL;
1660 for (i = 0; i<ord_no; i++)
1663 for (i = 0; i<before; i++)
1666 const char *mterm = NULL;
1670 for (j = 0; j <ord_no; j++)
1672 if (ptr[j] < before &&
1673 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
1674 (!mterm || strcmp (tst, mterm) > 0))
1683 scan_term_untrans (zi->odr, &glist[before-1-i].term, mterm);
1686 (zi, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1);
1690 for (j = j0+1; j<ord_no; j++)
1692 if (ptr[j] < before &&
1693 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
1694 !strcmp (tst, mterm))
1696 rset_bool_parms bool_parms;
1699 rset2 = rset_trunc (zi,
1700 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1);
1702 bool_parms.key_size = sizeof(struct it_key);
1703 bool_parms.cmp = key_compare_it;
1704 bool_parms.rset_l = rset;
1705 bool_parms.rset_r = rset2;
1707 rset = rset_create (rset_kind_or, &bool_parms);
1712 count_set (rset, &glist[before-1-i].occurrences);
1718 *status = BEND_SCAN_PARTIAL;
1722 *list = glist + i; /* list is set to first 'real' entry */
1724 logf (LOG_DEBUG, "position = %d, num_entries = %d",
1725 *position, *num_entries);
1727 logf (LOG_DEBUG, "scan error: %d", zi->errCode);