2 * Copyright (C) 1994-1996, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.58 1996-12-23 15:30:45 adam
9 * Bug fix: result sets weren't deleted after server shut down.
11 * Revision 1.57 1996/11/11 13:38:02 adam
12 * Added proximity support in search.
14 * Revision 1.56 1996/11/08 11:10:32 adam
15 * Buffers used during file match got bigger.
16 * Compressed ISAM support everywhere.
17 * Bug fixes regarding masking characters in queries.
18 * Redesigned Regexp-2 queries.
20 * Revision 1.55 1996/11/04 14:07:44 adam
21 * Moved truncation code to trunc.c.
23 * Revision 1.54 1996/10/29 14:09:52 adam
24 * Use of cisam system - enabled if setting isamc is 1.
26 * Revision 1.53 1996/06/26 09:21:43 adam
27 * Bug fix: local attribute set wasn't obeyed in scan.
29 * Revision 1.52 1996/06/17 14:26:20 adam
30 * Function gen_regular_rel changed to handle negative numbers.
32 * Revision 1.51 1996/06/11 10:54:15 quinn
35 * Revision 1.50 1996/06/07 08:51:53 adam
36 * Bug fix: Character mapping was broken (introducued by last revision).
38 * Revision 1.49 1996/06/04 10:18:11 adam
39 * Search/scan uses character mapping module.
41 * Revision 1.48 1996/05/28 15:15:01 adam
42 * Bug fix: Didn't handle unknown database correctly.
44 * Revision 1.47 1996/05/15 18:36:28 adam
45 * Function trans_term transforms unsearchable characters to blanks.
47 * Revision 1.46 1996/05/15 11:57:56 adam
48 * Fixed bug introduced by set/field mapping in search operations.
50 * Revision 1.45 1996/05/14 11:34:00 adam
51 * Scan support in multiple registers/databases.
53 * Revision 1.44 1996/05/14 06:16:44 adam
54 * Compact use/set bytes used in search service.
56 * Revision 1.43 1996/05/09 09:54:43 adam
57 * Server supports maps from one logical attributes to a list of physical
59 * The extraction process doesn't make space consuming 'any' keys.
61 * Revision 1.42 1996/05/09 07:28:56 quinn
62 * Work towards phrases and multiple registers
64 * Revision 1.41 1996/03/20 09:36:43 adam
65 * Function dict_lookup_grep got extra parameter, init_pos, which marks
66 * from which position in pattern approximate pattern matching should occur.
67 * Approximate pattern matching is used in relevance=re-2.
69 * Revision 1.40 1996/02/02 13:44:44 adam
70 * The public dictionary functions simply use char instead of Dict_char
71 * to represent search strings. Dict_char is used internally only.
73 * Revision 1.39 1996/01/03 16:22:13 quinn
76 * Revision 1.38 1995/12/11 09:12:55 adam
77 * The rec_get function returns NULL if record doesn't exist - will
78 * happen in the server if the result set records have been deleted since
79 * the creation of the set (i.e. the search).
80 * The server saves a result temporarily if it is 'volatile', i.e. the
81 * set is register dependent.
83 * Revision 1.37 1995/12/06 15:05:28 adam
84 * More verbose in count_set.
86 * Revision 1.36 1995/12/06 12:41:27 adam
87 * New command 'stat' for the index program.
88 * Filenames can be read from stdin by specifying '-'.
89 * Bug fix/enhancement of the transformation from terms to regular
90 * expressons in the search engine.
92 * Revision 1.35 1995/11/27 09:29:00 adam
93 * Bug fixes regarding conversion to regular expressions.
95 * Revision 1.34 1995/11/16 17:00:56 adam
96 * Better logging of rpn query.
98 * Revision 1.33 1995/11/01 13:58:28 quinn
99 * Moving data1 to yaz/retrieval
101 * Revision 1.32 1995/10/27 14:00:11 adam
102 * Implemented detection of database availability.
104 * Revision 1.31 1995/10/17 18:02:10 adam
105 * New feature: databases. Implemented as prefix to words in dictionary.
107 * Revision 1.30 1995/10/16 09:32:38 adam
108 * More work on relational op.
110 * Revision 1.29 1995/10/13 16:01:49 adam
113 * Revision 1.28 1995/10/13 12:26:43 adam
114 * Optimization of truncation.
116 * Revision 1.27 1995/10/12 17:07:22 adam
119 * Revision 1.26 1995/10/12 12:40:54 adam
120 * Bug fixes in rpn_prox.
122 * Revision 1.25 1995/10/10 13:59:24 adam
123 * Function rset_open changed its wflag parameter to general flags.
125 * Revision 1.24 1995/10/09 16:18:37 adam
126 * Function dict_lookup_grep got extra client data parameter.
128 * Revision 1.23 1995/10/06 16:33:37 adam
129 * Use attribute mappings.
131 * Revision 1.22 1995/10/06 15:07:39 adam
132 * Structure 'local-number' handled.
134 * Revision 1.21 1995/10/06 13:52:06 adam
135 * Bug fixes. Handler may abort further scanning.
137 * Revision 1.20 1995/10/06 11:06:33 adam
138 * Scan entries include 'occurrences' now.
140 * Revision 1.19 1995/10/06 10:43:56 adam
141 * Scan added. 'occurrences' in scan entries not set yet.
143 * Revision 1.18 1995/10/04 16:57:20 adam
144 * Key input and merge sort in one pass.
146 * Revision 1.17 1995/10/04 12:55:17 adam
147 * Bug fix in ranked search. Use=Any keys inserted.
149 * Revision 1.16 1995/10/02 16:24:40 adam
150 * Use attribute actually used in search requests.
152 * Revision 1.15 1995/10/02 15:18:52 adam
153 * New member in recRetrieveCtrl: diagnostic.
155 * Revision 1.14 1995/09/28 12:10:32 adam
156 * Bug fixes. Field prefix used in queries.
158 * Revision 1.13 1995/09/18 14:17:50 adam
161 * Revision 1.12 1995/09/15 14:45:21 adam
163 * Work on truncation.
165 * Revision 1.11 1995/09/14 11:53:27 adam
166 * First work on regular expressions/truncations.
168 * Revision 1.10 1995/09/11 15:23:26 adam
169 * More work on relevance search.
171 * Revision 1.9 1995/09/11 13:09:35 adam
172 * More work on relevance feedback.
174 * Revision 1.8 1995/09/08 14:52:27 adam
175 * Minor changes. Dictionary is lower case now.
177 * Revision 1.7 1995/09/07 13:58:36 adam
178 * New parameter: result-set file descriptor (RSFD) to support multiple
179 * positions within the same result-set.
180 * Boolean operators: and, or, not implemented.
181 * Result-set references.
183 * Revision 1.6 1995/09/06 16:11:18 adam
184 * Option: only one word key per file.
186 * Revision 1.5 1995/09/06 10:33:04 adam
187 * More work on present. Some log messages removed.
189 * Revision 1.4 1995/09/05 15:28:40 adam
190 * More work on search engine.
192 * Revision 1.3 1995/09/04 15:20:22 adam
195 * Revision 1.2 1995/09/04 12:33:43 adam
196 * Various cleanup. YAZ util used instead.
198 * Revision 1.1 1995/09/04 09:10:40 adam
199 * More work on index add/del/update.
200 * Merge sort implemented.
201 * Initial work on z39 server.
210 #include "attribute.h"
222 Z_AttributesPlusTerm *zapt;
225 static int attr_find (AttrType *src, oid_value *attributeSetP)
227 while (src->major < src->zapt->num_attributes)
229 Z_AttributeElement *element;
231 element = src->zapt->attributeList[src->major];
232 if (src->type == *element->attributeType)
234 switch (element->which)
236 case Z_AttributeValue_numeric:
238 if (element->attributeSet && attributeSetP)
242 attrset = oid_getentbyoid (element->attributeSet);
243 *attributeSetP = attrset->value;
245 return *element->value.numeric;
247 case Z_AttributeValue_complex:
248 if (src->minor >= element->value.complex->num_list ||
249 element->value.complex->list[src->minor]->which !=
250 Z_StringOrNumeric_numeric)
253 if (element->attributeSet && attributeSetP)
257 attrset = oid_getentbyoid (element->attributeSet);
258 *attributeSetP = attrset->value;
260 return *element->value.complex->list[src->minor-1]->u.numeric;
270 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
290 static void add_isam_p (const char *info, struct grep_info *p)
292 if (p->isam_p_indx == p->isam_p_size)
294 ISAM_P *new_isam_p_buf;
299 p->isam_p_size = 2*p->isam_p_size + 100;
300 new_isam_p_buf = xmalloc (sizeof(*new_isam_p_buf) *
304 memcpy (new_isam_p_buf, p->isam_p_buf,
305 p->isam_p_indx * sizeof(*p->isam_p_buf));
306 xfree (p->isam_p_buf);
308 p->isam_p_buf = new_isam_p_buf;
311 new_term_no = xmalloc (sizeof(*new_term_no) *
315 memcpy (new_term_no, p->isam_p_buf,
316 p->isam_p_indx * sizeof(*p->term_no));
319 p->term_no = new_term_no;
322 assert (*info == sizeof(*p->isam_p_buf));
323 memcpy (p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
327 static int grep_handle (char *name, const char *info, void *p)
329 add_isam_p (info, p);
333 static int term_pre (char **src, const char *ct1, const char *ct2)
335 char *s1, *s0 = *src;
338 /* skip white space */
341 if (ct1 && strchr (ct1, *s0))
343 if (ct2 && strchr (ct2, *s0))
346 map = map_chrs_input (&s1, strlen(s1));
347 if (**map != *CHR_SPACE)
355 static int term_100 (char **src, char *dst)
357 char *s0, *s1, **map;
360 if (!term_pre (src, NULL, NULL))
366 map = map_chrs_input (&s0, strlen(s0));
367 if (**map == *CHR_SPACE)
381 static int term_101 (char **src, char *dst)
383 char *s0, *s1, **map;
386 if (!term_pre (src, "#", "#"))
400 map = map_chrs_input (&s0, strlen(s0));
401 if (**map == *CHR_SPACE)
417 static int term_103 (char **src, char *dst, int *errors)
420 char *s0, *s1, **map;
422 if (!term_pre (src, "\\()[].*+?|", "("))
425 if (errors && *s0 == '+' && s0[1] && s0[2] == '+' && s0[3] &&
428 *errors = s0[1] - '0';
435 if (strchr ("\\()[].*+?|-", *s0))
440 map = map_chrs_input (&s0, strlen(s0));
441 if (**map == *CHR_SPACE)
456 static int term_102 (char **src, char *dst)
458 return term_103 (src, dst, NULL);
461 /* gen_regular_rel - generate regular expression from relation
462 * val: border value (inclusive)
463 * islt: 1 if <=; 0 if >=.
465 static void gen_regular_rel (char *dst, int val, int islt)
472 logf (LOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
476 strcpy (dst, "(-[0-9]+|");
484 strcpy (dst, "([0-9]+|-");
485 dst_p = strlen (dst);
495 dst_p = strlen (dst);
496 sprintf (numstr, "%d", val);
497 for (w = strlen(numstr); --w >= 0; pos++)
516 strcpy (dst + dst_p, numstr);
517 dst_p = strlen(dst) - pos - 1;
545 for (i = 0; i<pos; i++)
558 for (i=1; i<pos; i++)
559 strcat (dst, "[0-9]?");
563 for (i = 0; i <= pos; i++)
564 strcat (dst, "[0-9]");
565 strcat (dst, "[0-9]*");
570 static int relational_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
573 oid_value attributeSet,
574 struct grep_info *grep_info,
582 attr_init (&relation, zapt, 2);
583 relation_value = attr_find (&relation, NULL);
585 switch (relation_value)
588 if (!term_100 (term_sub, term_dict))
590 term_value = atoi (term_dict);
593 logf (LOG_DEBUG, "Relation <");
594 gen_regular_rel (term_dict + strlen(term_dict), term_value-1, 1);
597 if (!term_100 (term_sub, term_dict))
599 term_value = atoi (term_dict);
602 logf (LOG_DEBUG, "Relation <=");
603 gen_regular_rel (term_dict + strlen(term_dict), term_value, 1);
606 if (!term_100 (term_sub, term_dict))
608 term_value = atoi (term_dict);
611 logf (LOG_DEBUG, "Relation >=");
612 gen_regular_rel (term_dict + strlen(term_dict), term_value, 0);
615 if (!term_100 (term_sub, term_dict))
617 term_value = atoi (term_dict);
620 logf (LOG_DEBUG, "Relation >");
621 gen_regular_rel (term_dict + strlen(term_dict), term_value+1, 0);
626 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_dict);
627 r = dict_lookup_grep (zi->dict, term_dict, 0, grep_info, max_pos,
630 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
631 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
635 static int field_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
636 char **term_sub, int regType,
637 oid_value attributeSet, struct grep_info *grep_info,
638 int num_bases, char **basenames)
640 char term_dict[2*IT_MAX_WORD+2];
643 int truncation_value;
646 oid_value curAttributeSet = attributeSet;
649 attr_init (&use, zapt, 1);
650 use_value = attr_find (&use, &curAttributeSet);
651 logf (LOG_DEBUG, "use value %d", use_value);
652 attr_init (&truncation, zapt, 5);
653 truncation_value = attr_find (&truncation, NULL);
654 logf (LOG_DEBUG, "truncation value %d", truncation_value);
659 for (base_no = 0; base_no < num_bases; base_no++)
662 data1_local_attribute *local_attr;
663 int max_pos, prefix_len = 0;
666 attp = att_getentbyatt (curAttributeSet, use_value);
669 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
670 curAttributeSet, use_value);
674 if (zebTargetInfo_curDatabase (zi->zti, basenames[base_no]))
676 zi->errCode = 109; /* Database unavailable */
677 zi->errString = basenames[base_no];
680 for (local_attr = attp->local_attributes; local_attr;
681 local_attr = local_attr->next)
685 ord = zebTargetInfo_lookupSU (zi->zti, attp->attset_ordinal,
690 term_dict[prefix_len++] = '|';
692 term_dict[prefix_len++] = '(';
693 term_dict[prefix_len++] = 1;
694 term_dict[prefix_len++] = ord;
701 term_dict[prefix_len++] = ')';
702 term_dict[prefix_len++] = 1;
703 term_dict[prefix_len++] = regType;
704 term_dict[prefix_len] = '\0';
705 if (!relational_term (zi, zapt, &termp, term_dict,
706 attributeSet, grep_info, &max_pos))
709 switch (truncation_value)
711 case -1: /* not specified */
712 case 100: /* do not truncate */
713 term_dict[j++] = '(';
714 if (!term_100 (&termp, term_dict + j))
716 strcat (term_dict, ")");
717 r = dict_lookup_grep (zi->dict, term_dict, 0, grep_info,
718 &max_pos, 0, grep_handle);
720 logf (LOG_WARN, "dict_lookup_grep err, trunc=none:%d", r);
722 case 1: /* right truncation */
723 term_dict[j++] = '(';
724 if (!term_100 (&termp, term_dict + j))
726 strcat (term_dict, ".*)");
727 dict_lookup_grep (zi->dict, term_dict, 0, grep_info,
728 &max_pos, 0, grep_handle);
730 case 2: /* left truncation */
731 case 3: /* left&right truncation */
734 case 101: /* process # in term */
735 term_dict[j++] = '(';
736 if (!term_101 (&termp, term_dict + j))
738 strcat (term_dict, ")");
739 r = dict_lookup_grep (zi->dict, term_dict, 0, grep_info,
740 &max_pos, 0, grep_handle);
742 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d", r);
744 case 102: /* Regexp-1 */
745 term_dict[j++] = '(';
746 if (!term_102 (&termp, term_dict + j))
748 strcat (term_dict, ")");
749 logf (LOG_DEBUG, "Regexp-1 tolerance=%d", r);
750 r = dict_lookup_grep (zi->dict, term_dict, 0, grep_info,
751 &max_pos, 0, grep_handle);
753 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
756 case 103: /* Regexp-1 */
758 term_dict[j++] = '(';
759 if (!term_103 (&termp, term_dict + j, &r))
761 strcat (term_dict, ")");
762 logf (LOG_DEBUG, "Regexp-2 tolerance=%d", r);
763 r = dict_lookup_grep (zi->dict, term_dict, r, grep_info,
764 &max_pos, 2, grep_handle);
766 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
773 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
777 static void trans_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
781 Z_Term *term = zapt->term;
783 sizez = term->u.general->len;
784 if (sizez > IT_MAX_WORD-1)
785 sizez = IT_MAX_WORD-1;
786 memcpy (termz, term->u.general->buf, sizez);
790 static void trans_scan_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
793 Z_Term *term = zapt->term;
795 char *cp = (char*) term->u.general->buf;
796 const char *cp_end = cp + term->u.general->len;
802 while ((len = (cp_end - cp)) > 0)
804 map = map_chrs_input (&cp, len);
805 if (**map == *CHR_SPACE)
813 for (src = *map; *src; src++)
819 static RSET rpn_search_APT_relevance (ZServerInfo *zi,
820 Z_AttributesPlusTerm *zapt,
821 oid_value attributeSet,
822 int num_bases, char **basenames)
824 rset_relevance_parms parms;
825 char termz[IT_MAX_WORD+1];
827 struct grep_info grep_info;
832 parms.key_size = sizeof(struct it_key);
834 parms.cmp = key_compare_it;
836 parms.isc = zi->isamc;
839 if (zapt->term->which != Z_Term_general)
844 trans_term (zi, zapt, termz);
847 grep_info.term_no = 0;
849 grep_info.isam_p_indx = 0;
850 grep_info.isam_p_size = 0;
851 grep_info.isam_p_buf = NULL;
854 r = field_term (zi, zapt, &termp, 'w', attributeSet, &grep_info,
855 num_bases, basenames);
859 for (; term_index < grep_info.isam_p_indx; term_index++)
860 grep_info.term_no[term_index] = parms.no_terms;
864 parms.term_no = grep_info.term_no;
865 parms.isam_positions = grep_info.isam_p_buf;
866 parms.no_isam_positions = grep_info.isam_p_indx;
867 if (grep_info.isam_p_indx > 0)
868 result = rset_create (rset_kind_relevance, &parms);
870 result = rset_create (rset_kind_null, NULL);
872 xfree(grep_info.term_no);
874 xfree (grep_info.isam_p_buf);
878 static RSET rpn_search_APT_cphrase (ZServerInfo *zi,
879 Z_AttributesPlusTerm *zapt,
880 oid_value attributeSet,
881 int num_bases, char **basenames)
883 char termz[IT_MAX_WORD+1];
884 struct grep_info grep_info;
889 if (zapt->term->which != Z_Term_general)
894 trans_term (zi, zapt, termz);
897 grep_info.term_no = 0;
899 grep_info.isam_p_indx = 0;
900 grep_info.isam_p_size = 0;
901 grep_info.isam_p_buf = NULL;
903 r = field_term (zi, zapt, &termp, 'p', attributeSet, &grep_info,
904 num_bases, basenames);
905 result = rset_trunc (zi, grep_info.isam_p_buf, grep_info.isam_p_indx);
907 xfree(grep_info.term_no);
909 xfree (grep_info.isam_p_buf);
913 static RSET rpn_proximity (RSET rset1, RSET rset2, int ordered,
914 int exclusion, int relation, int distance)
919 struct it_key buf1, buf2;
922 rset_temp_parms parms;
924 rsfd1 = rset_open (rset1, RSETF_READ|RSETF_SORT_SYSNO);
925 more1 = rset_read (rset1, rsfd1, &buf1);
927 rsfd2 = rset_open (rset2, RSETF_READ|RSETF_SORT_SYSNO);
928 more2 = rset_read (rset2, rsfd2, &buf2);
930 parms.key_size = sizeof (struct it_key);
931 result = rset_create (rset_kind_temp, &parms);
932 rsfd_result = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
934 logf (LOG_DEBUG, "rpn_proximity excl=%d ord=%d rel=%d dis=%d",
935 exclusion, ordered, relation, distance);
936 while (more1 && more2)
938 int cmp = key_compare_it (&buf1, &buf2);
940 more1 = rset_read (rset1, rsfd1, &buf1);
942 more2 = rset_read (rset2, rsfd2, &buf2);
945 int sysno = buf1.sysno;
949 seqno[n++] = buf1.seqno;
950 while ((more1 = rset_read (rset1, rsfd1, &buf1)) &&
953 seqno[n++] = buf1.seqno;
956 for (i = 0; i<n; i++)
958 int diff = buf2.seqno - seqno[i];
959 int excl = exclusion;
960 if (!ordered && diff < 0)
962 logf (LOG_DEBUG, "l = %d r = %d", seqno[i], buf2.seqno);
970 if (diff <= distance)
974 if (diff == distance)
978 if (diff >= distance)
986 if (diff != distance)
992 logf (LOG_DEBUG, " match");
993 rset_write (result, rsfd_result, &buf2);
996 } while ((more2 = rset_read (rset2, rsfd2, &buf2)) &&
997 sysno == buf2.sysno);
1000 rset_close (result, rsfd_result);
1001 rset_close (rset1, rsfd1);
1002 rset_close (rset2, rsfd2);
1006 static RSET rpn_prox (RSET *rset, int rset_no)
1011 struct it_key **buf;
1014 rset_temp_parms parms;
1016 rsfd = xmalloc (sizeof(*rsfd)*rset_no);
1017 more = xmalloc (sizeof(*more)*rset_no);
1018 buf = xmalloc (sizeof(*buf)*rset_no);
1020 for (i = 0; i<rset_no; i++)
1022 buf[i] = xmalloc (sizeof(**buf));
1023 rsfd[i] = rset_open (rset[i], RSETF_READ|RSETF_SORT_SYSNO);
1024 if (!(more[i] = rset_read (rset[i], rsfd[i], buf[i])))
1028 rset_close (rset[i], rsfd[i]);
1035 return rset_create (rset_kind_null, NULL);
1038 parms.key_size = sizeof (struct it_key);
1039 result = rset_create (rset_kind_temp, &parms);
1040 rsfd_result = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
1044 for (i = 1; i<rset_no; i++)
1053 cmp = key_compare_it (buf[i], buf[i-1]);
1056 more[i-1] = rset_read (rset[i-1], rsfd[i-1], buf[i-1]);
1061 if (buf[i-1]->seqno+1 != buf[i]->seqno)
1063 more[i-1] = rset_read (rset[i-1], rsfd[i-1], buf[i-1]);
1069 more[i] = rset_read (rset[i], rsfd[i], buf[i]);
1075 rset_write (result, rsfd_result, buf[0]);
1076 more[0] = rset_read (*rset, *rsfd, *buf);
1080 for (i = 0; i<rset_no; i++)
1082 rset_close (rset[i], rsfd[i]);
1085 rset_close (result, rsfd_result);
1092 static RSET rpn_search_APT_phrase (ZServerInfo *zi,
1093 Z_AttributesPlusTerm *zapt,
1094 oid_value attributeSet,
1095 int num_bases, char **basenames)
1097 char termz[IT_MAX_WORD+1];
1098 char *termp = termz;
1099 RSET rset[60], result;
1100 int i, r, rset_no = 0;
1101 struct grep_info grep_info;
1103 if (zapt->term->which != Z_Term_general)
1108 trans_term (zi, zapt, termz);
1111 grep_info.term_no = 0;
1113 grep_info.isam_p_size = 0;
1114 grep_info.isam_p_buf = NULL;
1118 grep_info.isam_p_indx = 0;
1119 r = field_term (zi, zapt, &termp, 'w', attributeSet, &grep_info,
1120 num_bases, basenames);
1123 rset[rset_no] = rset_trunc (zi, grep_info.isam_p_buf,
1124 grep_info.isam_p_indx);
1125 assert (rset[rset_no]);
1126 if (++rset_no >= sizeof(rset)/sizeof(*rset))
1130 xfree(grep_info.term_no);
1132 xfree (grep_info.isam_p_buf);
1134 return rset_create (rset_kind_null, NULL);
1135 else if (rset_no == 1)
1137 result = rpn_prox (rset, rset_no);
1138 for (i = 0; i<rset_no; i++)
1139 rset_delete (rset[i]);
1143 static RSET rpn_search_APT_local (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1144 oid_value attributeSet)
1149 rset_temp_parms parms;
1150 char termz[IT_MAX_WORD+1];
1152 if (zapt->term->which != Z_Term_general)
1157 parms.key_size = sizeof (struct it_key);
1158 result = rset_create (rset_kind_temp, &parms);
1159 rsfd = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
1161 trans_term (zi, zapt, termz);
1163 key.sysno = atoi (termz);
1166 rset_write (result, rsfd, &key);
1167 rset_close (result, rsfd);
1171 static RSET rpn_search_APT (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1172 oid_value attributeSet,
1173 int num_bases, char **basenames)
1177 AttrType completeness;
1178 int relation_value, structure_value, completeness_value;
1180 attr_init (&relation, zapt, 2);
1181 attr_init (&structure, zapt, 4);
1182 attr_init (&completeness, zapt, 6);
1184 relation_value = attr_find (&relation, NULL);
1185 structure_value = attr_find (&structure, NULL);
1186 completeness_value = attr_find (&completeness, NULL);
1187 switch (structure_value)
1190 if (relation_value == 102) /* relevance relation */
1191 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1192 num_bases, basenames);
1193 if (completeness_value == 2 || completeness_value == 3)
1194 return rpn_search_APT_cphrase (zi, zapt, attributeSet,
1195 num_bases, basenames);
1196 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1197 num_bases, basenames);
1198 case 1: /* phrase */
1199 if (relation_value == 102) /* relevance relation */
1200 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1201 num_bases, basenames);
1202 if (completeness_value == 2 || completeness_value == 3)
1203 return rpn_search_APT_cphrase (zi, zapt, attributeSet,
1204 num_bases, basenames);
1205 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1206 num_bases, basenames);
1209 if (relation_value == 102) /* relevance relation */
1210 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1211 num_bases, basenames);
1212 if (completeness_value == 2 || completeness_value == 3)
1213 return rpn_search_APT_cphrase (zi, zapt, attributeSet,
1214 num_bases, basenames);
1215 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1216 num_bases, basenames);
1221 case 5: /* date - normalized */
1223 case 6: /* word list */
1224 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1225 num_bases, basenames);
1226 case 100: /* date - un-normalized */
1228 case 101: /* name - normalized */
1230 case 102: /* date - un-normalized */
1232 case 103: /* structure */
1236 case 105: /* free-form-text */
1237 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1238 num_bases, basenames);
1239 case 106: /* document-text */
1240 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1241 num_bases, basenames);
1242 case 107: /* local-number */
1243 return rpn_search_APT_local (zi, zapt, attributeSet);
1244 case 108: /* string */
1245 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1246 num_bases, basenames);
1247 case 109: /* numeric string */
1254 static RSET rpn_search_ref (ZServerInfo *zi, Z_ResultSetId *resultSetId)
1258 if (!(s = resultSetGet (zi, resultSetId)))
1259 return rset_create (rset_kind_null, NULL);
1263 static RSET rpn_search_structure (ZServerInfo *zi, Z_RPNStructure *zs,
1264 oid_value attributeSet,
1265 int num_bases, char **basenames)
1268 if (zs->which == Z_RPNStructure_complex)
1270 Z_Operator *zop = zs->u.complex->roperator;
1271 rset_bool_parms bool_parms;
1275 bool_parms.rset_l = rpn_search_structure (zi, zs->u.complex->s1,
1277 num_bases, basenames);
1278 if (bool_parms.rset_l == NULL)
1280 if (rset_is_ranked(bool_parms.rset_l))
1282 bool_parms.rset_r = rpn_search_structure (zi, zs->u.complex->s2,
1284 num_bases, basenames);
1285 if (bool_parms.rset_r == NULL)
1287 rset_delete (bool_parms.rset_l);
1290 if (rset_is_ranked(bool_parms.rset_r))
1292 bool_parms.key_size = sizeof(struct it_key);
1293 bool_parms.cmp = key_compare_it;
1297 case Z_Operator_and:
1298 r = rset_create (soft ? rset_kind_sand:rset_kind_and, &bool_parms);
1301 r = rset_create (soft ? rset_kind_sor:rset_kind_or, &bool_parms);
1303 case Z_Operator_and_not:
1304 r = rset_create (soft ? rset_kind_snot:rset_kind_not, &bool_parms);
1306 case Z_Operator_prox:
1307 if (zop->u.prox->which != Z_ProxCode_known)
1312 if (*zop->u.prox->proximityUnitCode != Z_ProxUnit_word)
1314 static char val[16];
1316 zi->errString = val;
1317 sprintf (val, "%d", *zop->u.prox->proximityUnitCode);
1320 r = rpn_proximity (bool_parms.rset_l, bool_parms.rset_r,
1321 *zop->u.prox->ordered,
1322 (!zop->u.prox->exclusion ? 0 :
1323 *zop->u.prox->exclusion),
1324 *zop->u.prox->relationType,
1325 *zop->u.prox->distance);
1332 else if (zs->which == Z_RPNStructure_simple)
1334 if (zs->u.simple->which == Z_Operand_APT)
1336 logf (LOG_DEBUG, "rpn_search_APT");
1337 r = rpn_search_APT (zi, zs->u.simple->u.attributesPlusTerm,
1338 attributeSet, num_bases, basenames);
1340 else if (zs->u.simple->which == Z_Operand_resultSetId)
1342 logf (LOG_DEBUG, "rpn_search_ref");
1343 r = rpn_search_ref (zi, zs->u.simple->u.resultSetId);
1359 void count_set_save (RSET *r, int *count)
1366 rset_temp_parms parms;
1368 logf (LOG_DEBUG, "count_set_save");
1370 parms.key_size = sizeof(struct it_key);
1371 w = rset_create (rset_kind_temp, &parms);
1372 wfd = rset_open (w, RSETF_WRITE|RSETF_SORT_SYSNO);
1373 rfd = rset_open (*r, RSETF_READ|RSETF_SORT_SYSNO);
1374 while (rset_read (*r, rfd, &key))
1376 if (key.sysno != psysno)
1379 rset_write (w, wfd, &key);
1385 rset_close (*r, rfd);
1387 rset_close (w, wfd);
1389 logf (LOG_DEBUG, "%d keys, %d distinct sysnos", kno, *count);
1392 static void count_set (RSET r, int *count)
1399 logf (LOG_DEBUG, "count_set");
1401 rfd = rset_open (r, RSETF_READ|RSETF_SORT_SYSNO);
1402 while (rset_read (r, rfd, &key))
1404 if (key.sysno != psysno)
1411 rset_close (r, rfd);
1412 logf (LOG_DEBUG, "%d keys, %d distinct sysnos", kno, *count);
1415 int rpn_search (ZServerInfo *zi,
1416 Z_RPNQuery *rpn, int num_bases, char **basenames,
1417 const char *setname, int *hits)
1421 oid_value attributeSet;
1423 dict_grep_cmap (zi->dict, map_chrs_input);
1427 zi->errString = NULL;
1429 attrset = oid_getentbyoid (rpn->attributeSetId);
1430 attributeSet = attrset->value;
1431 rset = rpn_search_structure (zi, rpn->RPNStructure, attributeSet,
1432 num_bases, basenames);
1435 if (rset_is_volatile(rset))
1436 count_set_save(&rset,hits);
1438 count_set (rset, hits);
1439 resultSetAdd (zi, setname, 1, rset);
1441 logf (LOG_DEBUG, "search error: %d", zi->errCode);
1445 struct scan_info_entry {
1451 struct scan_info_entry *list;
1457 static int scan_handle (char *name, const char *info, int pos, void *client)
1459 int len_prefix, idx;
1460 struct scan_info *scan_info = client;
1462 len_prefix = strlen(scan_info->prefix);
1463 if (memcmp (name, scan_info->prefix, len_prefix))
1466 idx = scan_info->after - pos + scan_info->before;
1469 logf (LOG_DEBUG, "%-3d %s", idx, name+len_prefix);
1470 scan_info->list[idx].term = odr_malloc (scan_info->odr,
1471 strlen(name + len_prefix)+1);
1472 strcpy (scan_info->list[idx].term, name + len_prefix);
1473 assert (*info == sizeof(ISAM_P));
1474 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAM_P));
1479 static void scan_term_untrans (ODR odr, char **dstp, const char *src)
1481 char *dst = odr_malloc (odr, strlen(src)*2+1);
1486 const char *cp = map_chrs_output (&src);
1493 int rpn_scan (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1494 oid_value attributeset,
1495 int num_bases, char **basenames,
1496 int *position, int *num_entries, struct scan_entry **list,
1500 int pos = *position;
1501 int num = *num_entries;
1505 char termz[IT_MAX_WORD+20];
1508 AttrType completeness;
1509 int completeness_value;
1510 struct scan_info *scan_info_array;
1511 struct scan_entry *glist;
1512 int ords[32], ord_no = 0;
1515 logf (LOG_DEBUG, "scan, position = %d, num = %d", pos, num);
1517 if (attributeset == VAL_NONE)
1518 attributeset = VAL_BIB1;
1520 attr_init (&use, zapt, 1);
1521 use_value = attr_find (&use, &attributeset);
1522 logf (LOG_DEBUG, "use value %d", use_value);
1524 attr_init (&completeness, zapt, 6);
1525 completeness_value = attr_find (&completeness, NULL);
1526 logf (LOG_DEBUG, "completeness value %d", completeness_value);
1528 if (use_value == -1)
1530 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
1533 data1_local_attribute *local_attr;
1535 attp = att_getentbyatt (attributeset, use_value);
1538 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
1539 attributeset, use_value);
1540 return zi->errCode = 114;
1542 if (zebTargetInfo_curDatabase (zi->zti, basenames[base_no]))
1544 zi->errString = basenames[base_no];
1545 return zi->errCode = 109; /* Database unavailable */
1547 for (local_attr = attp->local_attributes; local_attr && ord_no < 32;
1548 local_attr = local_attr->next)
1552 ord = zebTargetInfo_lookupSU (zi->zti, attp->attset_ordinal,
1555 ords[ord_no++] = ord;
1559 return zi->errCode = 113;
1562 scan_info_array = odr_malloc (zi->odr, ord_no * sizeof(*scan_info_array));
1563 for (i = 0; i < ord_no; i++)
1565 int j, prefix_len = 0;
1566 int before_tmp = before, after_tmp = after;
1567 struct scan_info *scan_info = scan_info_array + i;
1569 scan_info->before = before;
1570 scan_info->after = after;
1571 scan_info->odr = zi->odr;
1573 scan_info->list = odr_malloc (zi->odr, (before+after)*
1574 sizeof(*scan_info->list));
1575 for (j = 0; j<before+after; j++)
1576 scan_info->list[j].term = NULL;
1577 termz[prefix_len++] = ords[i];
1578 termz[prefix_len++] =
1579 (completeness_value==2 || completeness_value==3) ? 'p': 'w';
1580 termz[prefix_len] = 0;
1581 strcpy (scan_info->prefix, termz);
1583 trans_scan_term (zi, zapt, termz+prefix_len);
1585 dict_scan (zi->dict, termz, &before_tmp, &after_tmp, scan_info,
1588 glist = odr_malloc (zi->odr, (before+after)*sizeof(*glist));
1589 for (i = 0; i < ord_no; i++)
1592 *status = BEND_SCAN_SUCCESS;
1593 for (i = 0; i<after; i++)
1596 const char *mterm = NULL;
1600 for (j = 0; j < ord_no; j++)
1602 if (ptr[j] < before+after &&
1603 (tst=scan_info_array[j].list[ptr[j]].term) &&
1604 (!mterm || strcmp (tst, mterm) < 0))
1612 scan_term_untrans (zi->odr, &glist[i+before].term, mterm);
1613 rset = rset_trunc (zi, &scan_info_array[j0].list[ptr[j0]].isam_p, 1);
1616 for (j = j0+1; j<ord_no; j++)
1618 if (ptr[j] < before+after &&
1619 (tst=scan_info_array[j].list[ptr[j]].term) &&
1620 !strcmp (tst, mterm))
1622 rset_bool_parms bool_parms;
1626 rset_trunc (zi, &scan_info_array[j].list[ptr[j]].isam_p, 1);
1628 bool_parms.key_size = sizeof(struct it_key);
1629 bool_parms.cmp = key_compare_it;
1630 bool_parms.rset_l = rset;
1631 bool_parms.rset_r = rset2;
1633 rset = rset_create (rset_kind_or, &bool_parms);
1638 count_set (rset, &glist[i+before].occurrences);
1643 *num_entries -= (after-i);
1644 *status = BEND_SCAN_PARTIAL;
1647 for (i = 0; i<ord_no; i++)
1650 for (i = 0; i<before; i++)
1653 const char *mterm = NULL;
1657 for (j = 0; j <ord_no; j++)
1659 if (ptr[j] < before &&
1660 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
1661 (!mterm || strcmp (tst, mterm) > 0))
1670 scan_term_untrans (zi->odr, &glist[before-1-i].term, mterm);
1673 (zi, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1);
1677 for (j = j0+1; j<ord_no; j++)
1679 if (ptr[j] < before &&
1680 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
1681 !strcmp (tst, mterm))
1683 rset_bool_parms bool_parms;
1686 rset2 = rset_trunc (zi,
1687 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1);
1689 bool_parms.key_size = sizeof(struct it_key);
1690 bool_parms.cmp = key_compare_it;
1691 bool_parms.rset_l = rset;
1692 bool_parms.rset_r = rset2;
1694 rset = rset_create (rset_kind_or, &bool_parms);
1699 count_set (rset, &glist[before-1-i].occurrences);
1705 *status = BEND_SCAN_PARTIAL;
1709 *list = glist + i; /* list is set to first 'real' entry */
1711 logf (LOG_DEBUG, "position = %d, num_entries = %d",
1712 *position, *num_entries);
1714 logf (LOG_DEBUG, "scan error: %d", zi->errCode);