2 * Copyright (C) 1994-1996, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.55 1996-11-04 14:07:44 adam
8 * Moved truncation code to trunc.c.
10 * Revision 1.54 1996/10/29 14:09:52 adam
11 * Use of cisam system - enabled if setting isamc is 1.
13 * Revision 1.53 1996/06/26 09:21:43 adam
14 * Bug fix: local attribute set wasn't obeyed in scan.
16 * Revision 1.52 1996/06/17 14:26:20 adam
17 * Function gen_regular_rel changed to handle negative numbers.
19 * Revision 1.51 1996/06/11 10:54:15 quinn
22 * Revision 1.50 1996/06/07 08:51:53 adam
23 * Bug fix: Character mapping was broken (introducued by last revision).
25 * Revision 1.49 1996/06/04 10:18:11 adam
26 * Search/scan uses character mapping module.
28 * Revision 1.48 1996/05/28 15:15:01 adam
29 * Bug fix: Didn't handle unknown database correctly.
31 * Revision 1.47 1996/05/15 18:36:28 adam
32 * Function trans_term transforms unsearchable characters to blanks.
34 * Revision 1.46 1996/05/15 11:57:56 adam
35 * Fixed bug introduced by set/field mapping in search operations.
37 * Revision 1.45 1996/05/14 11:34:00 adam
38 * Scan support in multiple registers/databases.
40 * Revision 1.44 1996/05/14 06:16:44 adam
41 * Compact use/set bytes used in search service.
43 * Revision 1.43 1996/05/09 09:54:43 adam
44 * Server supports maps from one logical attributes to a list of physical
46 * The extraction process doesn't make space consuming 'any' keys.
48 * Revision 1.42 1996/05/09 07:28:56 quinn
49 * Work towards phrases and multiple registers
51 * Revision 1.41 1996/03/20 09:36:43 adam
52 * Function dict_lookup_grep got extra parameter, init_pos, which marks
53 * from which position in pattern approximate pattern matching should occur.
54 * Approximate pattern matching is used in relevance=re-2.
56 * Revision 1.40 1996/02/02 13:44:44 adam
57 * The public dictionary functions simply use char instead of Dict_char
58 * to represent search strings. Dict_char is used internally only.
60 * Revision 1.39 1996/01/03 16:22:13 quinn
63 * Revision 1.38 1995/12/11 09:12:55 adam
64 * The rec_get function returns NULL if record doesn't exist - will
65 * happen in the server if the result set records have been deleted since
66 * the creation of the set (i.e. the search).
67 * The server saves a result temporarily if it is 'volatile', i.e. the
68 * set is register dependent.
70 * Revision 1.37 1995/12/06 15:05:28 adam
71 * More verbose in count_set.
73 * Revision 1.36 1995/12/06 12:41:27 adam
74 * New command 'stat' for the index program.
75 * Filenames can be read from stdin by specifying '-'.
76 * Bug fix/enhancement of the transformation from terms to regular
77 * expressons in the search engine.
79 * Revision 1.35 1995/11/27 09:29:00 adam
80 * Bug fixes regarding conversion to regular expressions.
82 * Revision 1.34 1995/11/16 17:00:56 adam
83 * Better logging of rpn query.
85 * Revision 1.33 1995/11/01 13:58:28 quinn
86 * Moving data1 to yaz/retrieval
88 * Revision 1.32 1995/10/27 14:00:11 adam
89 * Implemented detection of database availability.
91 * Revision 1.31 1995/10/17 18:02:10 adam
92 * New feature: databases. Implemented as prefix to words in dictionary.
94 * Revision 1.30 1995/10/16 09:32:38 adam
95 * More work on relational op.
97 * Revision 1.29 1995/10/13 16:01:49 adam
100 * Revision 1.28 1995/10/13 12:26:43 adam
101 * Optimization of truncation.
103 * Revision 1.27 1995/10/12 17:07:22 adam
106 * Revision 1.26 1995/10/12 12:40:54 adam
107 * Bug fixes in rpn_prox.
109 * Revision 1.25 1995/10/10 13:59:24 adam
110 * Function rset_open changed its wflag parameter to general flags.
112 * Revision 1.24 1995/10/09 16:18:37 adam
113 * Function dict_lookup_grep got extra client data parameter.
115 * Revision 1.23 1995/10/06 16:33:37 adam
116 * Use attribute mappings.
118 * Revision 1.22 1995/10/06 15:07:39 adam
119 * Structure 'local-number' handled.
121 * Revision 1.21 1995/10/06 13:52:06 adam
122 * Bug fixes. Handler may abort further scanning.
124 * Revision 1.20 1995/10/06 11:06:33 adam
125 * Scan entries include 'occurrences' now.
127 * Revision 1.19 1995/10/06 10:43:56 adam
128 * Scan added. 'occurrences' in scan entries not set yet.
130 * Revision 1.18 1995/10/04 16:57:20 adam
131 * Key input and merge sort in one pass.
133 * Revision 1.17 1995/10/04 12:55:17 adam
134 * Bug fix in ranked search. Use=Any keys inserted.
136 * Revision 1.16 1995/10/02 16:24:40 adam
137 * Use attribute actually used in search requests.
139 * Revision 1.15 1995/10/02 15:18:52 adam
140 * New member in recRetrieveCtrl: diagnostic.
142 * Revision 1.14 1995/09/28 12:10:32 adam
143 * Bug fixes. Field prefix used in queries.
145 * Revision 1.13 1995/09/18 14:17:50 adam
148 * Revision 1.12 1995/09/15 14:45:21 adam
150 * Work on truncation.
152 * Revision 1.11 1995/09/14 11:53:27 adam
153 * First work on regular expressions/truncations.
155 * Revision 1.10 1995/09/11 15:23:26 adam
156 * More work on relevance search.
158 * Revision 1.9 1995/09/11 13:09:35 adam
159 * More work on relevance feedback.
161 * Revision 1.8 1995/09/08 14:52:27 adam
162 * Minor changes. Dictionary is lower case now.
164 * Revision 1.7 1995/09/07 13:58:36 adam
165 * New parameter: result-set file descriptor (RSFD) to support multiple
166 * positions within the same result-set.
167 * Boolean operators: and, or, not implemented.
168 * Result-set references.
170 * Revision 1.6 1995/09/06 16:11:18 adam
171 * Option: only one word key per file.
173 * Revision 1.5 1995/09/06 10:33:04 adam
174 * More work on present. Some log messages removed.
176 * Revision 1.4 1995/09/05 15:28:40 adam
177 * More work on search engine.
179 * Revision 1.3 1995/09/04 15:20:22 adam
182 * Revision 1.2 1995/09/04 12:33:43 adam
183 * Various cleanup. YAZ util used instead.
185 * Revision 1.1 1995/09/04 09:10:40 adam
186 * More work on index add/del/update.
187 * Merge sort implemented.
188 * Initial work on z39 server.
197 #include "attribute.h"
209 Z_AttributesPlusTerm *zapt;
212 static int attr_find (AttrType *src, oid_value *attributeSetP)
214 while (src->major < src->zapt->num_attributes)
216 Z_AttributeElement *element;
218 element = src->zapt->attributeList[src->major];
219 if (src->type == *element->attributeType)
221 switch (element->which)
223 case Z_AttributeValue_numeric:
225 if (element->attributeSet && attributeSetP)
229 attrset = oid_getentbyoid (element->attributeSet);
230 *attributeSetP = attrset->value;
232 return *element->value.numeric;
234 case Z_AttributeValue_complex:
235 if (src->minor >= element->value.complex->num_list ||
236 element->value.complex->list[src->minor]->which !=
237 Z_StringOrNumeric_numeric)
240 if (element->attributeSet && attributeSetP)
244 attrset = oid_getentbyoid (element->attributeSet);
245 *attributeSetP = attrset->value;
247 return *element->value.complex->list[src->minor-1]->u.numeric;
257 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
277 static void add_isam_p (const char *info, struct grep_info *p)
279 if (p->isam_p_indx == p->isam_p_size)
281 ISAM_P *new_isam_p_buf;
286 p->isam_p_size = 2*p->isam_p_size + 100;
287 new_isam_p_buf = xmalloc (sizeof(*new_isam_p_buf) *
291 memcpy (new_isam_p_buf, p->isam_p_buf,
292 p->isam_p_indx * sizeof(*p->isam_p_buf));
293 xfree (p->isam_p_buf);
295 p->isam_p_buf = new_isam_p_buf;
298 new_term_no = xmalloc (sizeof(*new_term_no) *
302 memcpy (new_term_no, p->isam_p_buf,
303 p->isam_p_indx * sizeof(*p->term_no));
306 p->term_no = new_term_no;
309 assert (*info == sizeof(*p->isam_p_buf));
310 memcpy (p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
314 static int grep_handle (char *name, const char *info, void *p)
316 add_isam_p (info, p);
320 /* gen_regular_rel - generate regular expression from relation
321 * val: border value (inclusive)
322 * islt: 1 if <=; 0 if >=.
324 static void gen_regular_rel (char *dst, int val, int islt)
331 logf (LOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
335 strcpy (dst, "(-[0-9]+|");
343 strcpy (dst, "([0-9]+|-");
344 dst_p = strlen (dst);
354 dst_p = strlen (dst);
355 sprintf (numstr, "%d", val);
356 for (w = strlen(numstr); --w >= 0; pos++)
375 strcpy (dst + dst_p, numstr);
376 dst_p = strlen(dst) - pos - 1;
404 for (i = 0; i<pos; i++)
417 for (i=1; i<pos; i++)
418 strcat (dst, "[0-9]?");
422 for (i = 0; i <= pos; i++)
423 strcat (dst, "[0-9]");
424 strcat (dst, "[0-9]*");
429 static int relational_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
430 const char *term_sub,
432 oid_value attributeSet,
433 struct grep_info *grep_info,
441 attr_init (&relation, zapt, 2);
442 relation_value = attr_find (&relation, NULL);
443 term_value = atoi (term_sub);
445 switch (relation_value)
450 logf (LOG_DEBUG, "Relation <");
451 gen_regular_rel (term_dict + strlen(term_dict), term_value-1, 1);
456 logf (LOG_DEBUG, "Relation <=");
457 gen_regular_rel (term_dict + strlen(term_dict), term_value, 1);
462 logf (LOG_DEBUG, "Relation >=");
463 gen_regular_rel (term_dict + strlen(term_dict), term_value, 0);
468 logf (LOG_DEBUG, "Relation >");
469 gen_regular_rel (term_dict + strlen(term_dict), term_value+1, 0);
474 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_dict);
475 r = dict_lookup_grep (zi->dict, term_dict, 0, grep_info, max_pos,
478 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
479 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
483 static void verbatim_char (int ch, int *indx, char *dst)
486 dst[(*indx)++] = '\\';
490 static int field_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
491 const char *term_sub, int regType,
492 oid_value attributeSet, struct grep_info *grep_info,
493 int num_bases, char **basenames)
495 char term_dict[2*IT_MAX_WORD+2];
496 int i, j, r, base_no;
498 int truncation_value;
501 oid_value curAttributeSet = attributeSet;
503 attr_init (&use, zapt, 1);
504 use_value = attr_find (&use, &curAttributeSet);
505 logf (LOG_DEBUG, "use value %d", use_value);
506 attr_init (&truncation, zapt, 5);
507 truncation_value = attr_find (&truncation, NULL);
508 logf (LOG_DEBUG, "truncation value %d", truncation_value);
513 for (base_no = 0; base_no < num_bases; base_no++)
516 data1_local_attribute *local_attr;
517 int max_pos, prefix_len = 0;
519 attp = att_getentbyatt (curAttributeSet, use_value);
522 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
523 curAttributeSet, use_value);
527 if (zebTargetInfo_curDatabase (zi->zti, basenames[base_no]))
529 zi->errCode = 109; /* Database unavailable */
530 zi->errString = basenames[base_no];
533 for (local_attr = attp->local_attributes; local_attr;
534 local_attr = local_attr->next)
538 ord = zebTargetInfo_lookupSU (zi->zti, attp->attset_ordinal,
543 term_dict[prefix_len++] = '|';
545 term_dict[prefix_len++] = '(';
546 term_dict[prefix_len++] = 1;
547 term_dict[prefix_len++] = ord;
554 term_dict[prefix_len++] = ')';
555 term_dict[prefix_len++] = 1;
556 term_dict[prefix_len++] = regType;
557 term_dict[prefix_len] = '\0';
558 if (!relational_term (zi, zapt, term_sub, term_dict,
559 attributeSet, grep_info, &max_pos))
564 switch (truncation_value)
566 case -1: /* not specified */
567 case 100: /* do not truncate */
568 term_dict[j++] = '(';
569 for (i = 0; term_sub[i]; i++)
570 verbatim_char (term_sub[i], &j, term_dict);
571 strcpy (term_dict+j, ")");
572 r = dict_lookup_grep (zi->dict, term_dict, 0, grep_info,
573 &max_pos, 0, grep_handle);
575 logf (LOG_WARN, "dict_lookup_grep err, trunc=none:%d", r);
577 case 1: /* right truncation */
578 term_dict[j++] = '(';
579 for (i = 0; term_sub[i]; i++)
580 verbatim_char (term_sub[i], &j, term_dict);
581 strcpy (term_dict+j, ".*)");
582 dict_lookup_grep (zi->dict, term_dict, 0, grep_info,
583 &max_pos, 0, grep_handle);
585 case 2: /* left truncation */
586 case 3: /* left&right truncation */
589 case 101: /* process # in term */
590 term_dict[j++] = '(';
591 for (i=0; term_sub[i]; i++)
592 if (term_sub[i] == '#' && i > 2)
594 term_dict[j++] = '.';
595 term_dict[j++] = '*';
598 verbatim_char (term_sub[i], &j, term_dict);
599 strcpy (term_dict+j, ")");
600 r = dict_lookup_grep (zi->dict, term_dict, 0, grep_info,
601 &max_pos, 0, grep_handle);
603 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d",
606 case 102: /* regular expression */
607 sprintf (term_dict + j, "(%s)", term_sub);
608 r = dict_lookup_grep (zi->dict, term_dict, 0, grep_info,
609 &max_pos, 0, grep_handle);
611 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
614 case 103: /* regular expression with error correction */
617 if (*cp == '*' && cp[1] && cp[2])
622 sprintf (term_dict + j, "(%s)", cp);
623 r = dict_lookup_grep (zi->dict, term_dict, r, grep_info,
624 &max_pos, j, grep_handle);
626 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
632 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
636 static void trans_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
640 Z_Term *term = zapt->term;
642 sizez = term->u.general->len;
643 if (sizez > IT_MAX_WORD-1)
644 sizez = IT_MAX_WORD-1;
645 memcpy (termz, term->u.general->buf, sizez);
649 static void trans_scan_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
652 Z_Term *term = zapt->term;
654 char *cp = (char*) term->u.general->buf;
655 const char *cp_end = cp + term->u.general->len;
661 while ((len = (cp_end - cp)) > 0)
663 map = map_chrs_input (&cp, len);
664 if (**map == *CHR_SPACE)
672 for (src = *map; *src; src++)
678 static RSET rpn_search_APT_relevance (ZServerInfo *zi,
679 Z_AttributesPlusTerm *zapt,
680 oid_value attributeSet,
681 int num_bases, char **basenames)
683 rset_relevance_parms parms;
684 char termz[IT_MAX_WORD+1];
685 char term_sub[IT_MAX_WORD+1];
686 struct grep_info grep_info;
691 parms.key_size = sizeof(struct it_key);
693 parms.cmp = key_compare;
697 if (zapt->term->which != Z_Term_general)
702 trans_term (zi, zapt, termz);
705 grep_info.term_no = 0;
707 grep_info.isam_p_indx = 0;
708 grep_info.isam_p_size = 0;
709 grep_info.isam_p_buf = NULL;
718 map = map_chrs_input (&p1, strlen(p1));
719 if (**map != *CHR_SPACE)
728 map = map_chrs_input (&p1, strlen(p1));
729 if (**map == *CHR_SPACE)
734 memcpy (term_sub, p0, p2-p0);
735 term_sub[p2-p0] = '\0';
737 if (field_term (zi, zapt, term_sub, 'w', attributeSet, &grep_info,
738 num_bases, basenames))
741 for (; term_index < grep_info.isam_p_indx; term_index++)
742 grep_info.term_no[term_index] = parms.no_terms;
746 parms.term_no = grep_info.term_no;
747 parms.isam_positions = grep_info.isam_p_buf;
748 parms.no_isam_positions = grep_info.isam_p_indx;
749 if (grep_info.isam_p_indx > 0)
750 result = rset_create (rset_kind_relevance, &parms);
752 result = rset_create (rset_kind_null, NULL);
754 xfree(grep_info.term_no);
756 xfree (grep_info.isam_p_buf);
760 static RSET rpn_search_APT_cphrase (ZServerInfo *zi,
761 Z_AttributesPlusTerm *zapt,
762 oid_value attributeSet,
763 int num_bases, char **basenames)
765 char termz[IT_MAX_WORD+1];
766 struct grep_info grep_info;
769 if (zapt->term->which != Z_Term_general)
774 trans_term (zi, zapt, termz);
777 grep_info.term_no = 0;
779 grep_info.isam_p_indx = 0;
780 grep_info.isam_p_size = 0;
781 grep_info.isam_p_buf = NULL;
783 if (field_term (zi, zapt, termz, 'p', attributeSet, &grep_info,
784 num_bases, basenames))
786 result = rset_trunc (zi, grep_info.isam_p_buf, grep_info.isam_p_indx);
788 xfree(grep_info.term_no);
790 xfree (grep_info.isam_p_buf);
794 static RSET rpn_prox (RSET *rset, int rset_no)
802 rset_temp_parms parms;
804 rsfd = xmalloc (sizeof(*rsfd)*rset_no);
805 more = xmalloc (sizeof(*more)*rset_no);
806 buf = xmalloc (sizeof(*buf)*rset_no);
808 for (i = 0; i<rset_no; i++)
810 buf[i] = xmalloc (sizeof(**buf));
811 rsfd[i] = rset_open (rset[i], RSETF_READ|RSETF_SORT_SYSNO);
812 if (!(more[i] = rset_read (rset[i], rsfd[i], buf[i])))
816 rset_close (rset[i], rsfd[i]);
823 return rset_create (rset_kind_null, NULL);
826 parms.key_size = sizeof (struct it_key);
827 result = rset_create (rset_kind_temp, &parms);
828 rsfd_result = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
832 for (i = 1; i<rset_no; i++)
841 cmp = key_compare (buf[i], buf[i-1]);
844 more[i-1] = rset_read (rset[i-1], rsfd[i-1], buf[i-1]);
849 if (buf[i-1]->seqno+1 != buf[i]->seqno)
851 more[i-1] = rset_read (rset[i-1], rsfd[i-1], buf[i-1]);
857 more[i] = rset_read (rset[i], rsfd[i], buf[i]);
863 rset_write (result, rsfd_result, buf[0]);
864 more[0] = rset_read (*rset, *rsfd, *buf);
868 for (i = 0; i<rset_no; i++)
870 rset_close (rset[i], rsfd[i]);
873 rset_close (result, rsfd_result);
880 static RSET rpn_search_APT_phrase (ZServerInfo *zi,
881 Z_AttributesPlusTerm *zapt,
882 oid_value attributeSet,
883 int num_bases, char **basenames)
885 char termz[IT_MAX_WORD+1];
886 char term_sub[IT_MAX_WORD+1];
888 RSET rset[60], result;
890 struct grep_info grep_info;
892 if (zapt->term->which != Z_Term_general)
897 trans_term (zi, zapt, termz);
900 grep_info.term_no = 0;
902 grep_info.isam_p_size = 0;
903 grep_info.isam_p_buf = NULL;
913 map = map_chrs_input (&p1, strlen(p1));
914 if (**map != *CHR_SPACE)
923 map = map_chrs_input (&p1, strlen(p1));
924 if (**map == *CHR_SPACE)
930 memcpy (term_sub, p0, p2-p0);
931 term_sub[p2-p0] = '\0';
934 grep_info.isam_p_indx = 0;
935 if (field_term (zi, zapt, term_sub, 'w', attributeSet, &grep_info,
936 num_bases, basenames))
938 rset[rset_no] = rset_trunc (zi, grep_info.isam_p_buf,
939 grep_info.isam_p_indx);
940 assert (rset[rset_no]);
941 if (++rset_no >= sizeof(rset)/sizeof(*rset))
945 xfree(grep_info.term_no);
947 xfree (grep_info.isam_p_buf);
949 return rset_create (rset_kind_null, NULL);
950 else if (rset_no == 1)
953 result = rpn_prox (rset, rset_no);
954 for (i = 0; i<rset_no; i++)
955 rset_delete (rset[i]);
959 static RSET rpn_search_APT_local (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
960 oid_value attributeSet)
965 rset_temp_parms parms;
966 char termz[IT_MAX_WORD+1];
968 if (zapt->term->which != Z_Term_general)
973 parms.key_size = sizeof (struct it_key);
974 result = rset_create (rset_kind_temp, &parms);
975 rsfd = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
977 trans_term (zi, zapt, termz);
979 key.sysno = atoi (termz);
982 rset_write (result, rsfd, &key);
983 rset_close (result, rsfd);
987 static RSET rpn_search_APT (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
988 oid_value attributeSet,
989 int num_bases, char **basenames)
993 AttrType completeness;
994 int relation_value, structure_value, completeness_value;
996 attr_init (&relation, zapt, 2);
997 attr_init (&structure, zapt, 4);
998 attr_init (&completeness, zapt, 6);
1000 relation_value = attr_find (&relation, NULL);
1001 structure_value = attr_find (&structure, NULL);
1002 completeness_value = attr_find (&completeness, NULL);
1003 switch (structure_value)
1006 if (relation_value == 102) /* relevance relation */
1007 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1008 num_bases, basenames);
1009 if (completeness_value == 2 || completeness_value == 3)
1010 return rpn_search_APT_cphrase (zi, zapt, attributeSet,
1011 num_bases, basenames);
1012 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1013 num_bases, basenames);
1014 case 1: /* phrase */
1015 if (relation_value == 102) /* relevance relation */
1016 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1017 num_bases, basenames);
1018 if (completeness_value == 2 || completeness_value == 3)
1019 return rpn_search_APT_cphrase (zi, zapt, attributeSet,
1020 num_bases, basenames);
1021 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1022 num_bases, basenames);
1025 if (relation_value == 102) /* relevance relation */
1026 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1027 num_bases, basenames);
1028 if (completeness_value == 2 || completeness_value == 3)
1029 return rpn_search_APT_cphrase (zi, zapt, attributeSet,
1030 num_bases, basenames);
1031 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1032 num_bases, basenames);
1037 case 5: /* date - normalized */
1039 case 6: /* word list */
1040 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1041 num_bases, basenames);
1042 case 100: /* date - un-normalized */
1044 case 101: /* name - normalized */
1046 case 102: /* date - un-normalized */
1048 case 103: /* structure */
1052 case 105: /* free-form-text */
1053 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1054 num_bases, basenames);
1055 case 106: /* document-text */
1056 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1057 num_bases, basenames);
1058 case 107: /* local-number */
1059 return rpn_search_APT_local (zi, zapt, attributeSet);
1060 case 108: /* string */
1061 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1062 num_bases, basenames);
1063 case 109: /* numeric string */
1070 static RSET rpn_search_ref (ZServerInfo *zi, Z_ResultSetId *resultSetId)
1074 if (!(s = resultSetGet (zi, resultSetId)))
1075 return rset_create (rset_kind_null, NULL);
1079 static RSET rpn_search_structure (ZServerInfo *zi, Z_RPNStructure *zs,
1080 oid_value attributeSet,
1081 int num_bases, char **basenames)
1084 if (zs->which == Z_RPNStructure_complex)
1086 rset_bool_parms bool_parms;
1089 bool_parms.rset_l = rpn_search_structure (zi, zs->u.complex->s1,
1091 num_bases, basenames);
1092 if (bool_parms.rset_l == NULL)
1094 if (rset_is_ranked(bool_parms.rset_l))
1096 bool_parms.rset_r = rpn_search_structure (zi, zs->u.complex->s2,
1098 num_bases, basenames);
1099 if (bool_parms.rset_r == NULL)
1101 rset_delete (bool_parms.rset_l);
1104 if (rset_is_ranked(bool_parms.rset_r))
1106 bool_parms.key_size = sizeof(struct it_key);
1107 bool_parms.cmp = key_compare;
1109 switch (zs->u.complex->roperator->which)
1111 case Z_Operator_and:
1112 r = rset_create (soft ? rset_kind_sand:rset_kind_and, &bool_parms);
1115 r = rset_create (soft ? rset_kind_sor:rset_kind_or, &bool_parms);
1117 case Z_Operator_and_not:
1118 r = rset_create (soft ? rset_kind_snot:rset_kind_not, &bool_parms);
1124 else if (zs->which == Z_RPNStructure_simple)
1126 if (zs->u.simple->which == Z_Operand_APT)
1128 logf (LOG_DEBUG, "rpn_search_APT");
1129 r = rpn_search_APT (zi, zs->u.simple->u.attributesPlusTerm,
1130 attributeSet, num_bases, basenames);
1132 else if (zs->u.simple->which == Z_Operand_resultSetId)
1134 logf (LOG_DEBUG, "rpn_search_ref");
1135 r = rpn_search_ref (zi, zs->u.simple->u.resultSetId);
1149 void count_set_save (RSET *r, int *count)
1156 rset_temp_parms parms;
1158 logf (LOG_DEBUG, "count_set_save");
1160 parms.key_size = sizeof(struct it_key);
1161 w = rset_create (rset_kind_temp, &parms);
1162 wfd = rset_open (w, RSETF_WRITE|RSETF_SORT_SYSNO);
1163 rfd = rset_open (*r, RSETF_READ|RSETF_SORT_SYSNO);
1164 while (rset_read (*r, rfd, &key))
1166 logf (LOG_DEBUG, "sysno=%-7d seqno=%d", key.sysno, key.seqno);
1167 if (key.sysno != psysno)
1169 rset_write (w, wfd, &key);
1175 rset_close (*r, rfd);
1177 rset_close (w, wfd);
1179 logf (LOG_DEBUG, "%d keys, %d distinct sysnos", kno, *count);
1182 static void count_set (RSET r, int *count)
1189 logf (LOG_DEBUG, "count_set");
1191 rfd = rset_open (r, RSETF_READ|RSETF_SORT_SYSNO);
1192 while (rset_read (r, rfd, &key))
1194 if (key.sysno != psysno)
1201 rset_close (r, rfd);
1202 logf (LOG_DEBUG, "%d keys, %d distinct sysnos", kno, *count);
1205 int rpn_search (ZServerInfo *zi,
1206 Z_RPNQuery *rpn, int num_bases, char **basenames,
1207 const char *setname, int *hits)
1211 oid_value attributeSet;
1213 dict_grep_cmap (zi->dict, map_chrs_input);
1217 zi->errString = NULL;
1219 attrset = oid_getentbyoid (rpn->attributeSetId);
1220 attributeSet = attrset->value;
1221 rset = rpn_search_structure (zi, rpn->RPNStructure, attributeSet,
1222 num_bases, basenames);
1225 if (rset_is_volatile(rset))
1226 count_set_save(&rset,hits);
1228 count_set (rset, hits);
1229 resultSetAdd (zi, setname, 1, rset);
1231 logf (LOG_DEBUG, "search error: %d", zi->errCode);
1235 struct scan_info_entry {
1241 struct scan_info_entry *list;
1247 static int scan_handle (char *name, const char *info, int pos, void *client)
1249 int len_prefix, idx;
1250 struct scan_info *scan_info = client;
1252 len_prefix = strlen(scan_info->prefix);
1253 if (memcmp (name, scan_info->prefix, len_prefix))
1256 idx = scan_info->after - pos + scan_info->before;
1259 logf (LOG_DEBUG, "%-3d %s", idx, name+len_prefix);
1260 scan_info->list[idx].term = odr_malloc (scan_info->odr,
1261 strlen(name + len_prefix)+1);
1262 strcpy (scan_info->list[idx].term, name + len_prefix);
1263 assert (*info == sizeof(ISAM_P));
1264 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAM_P));
1269 static void scan_term_untrans (ODR odr, char **dstp, const char *src)
1271 char *dst = odr_malloc (odr, strlen(src)*2+1);
1276 const char *cp = map_chrs_output (&src);
1283 int rpn_scan (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1284 oid_value attributeset,
1285 int num_bases, char **basenames,
1286 int *position, int *num_entries, struct scan_entry **list,
1290 int pos = *position;
1291 int num = *num_entries;
1295 char termz[IT_MAX_WORD+20];
1298 AttrType completeness;
1299 int completeness_value;
1300 struct scan_info *scan_info_array;
1301 struct scan_entry *glist;
1302 int ords[32], ord_no = 0;
1305 logf (LOG_DEBUG, "scan, position = %d, num = %d", pos, num);
1307 if (attributeset == VAL_NONE)
1308 attributeset = VAL_BIB1;
1310 attr_init (&use, zapt, 1);
1311 use_value = attr_find (&use, &attributeset);
1312 logf (LOG_DEBUG, "use value %d", use_value);
1314 attr_init (&completeness, zapt, 6);
1315 completeness_value = attr_find (&completeness, NULL);
1316 logf (LOG_DEBUG, "completeness value %d", completeness_value);
1318 if (use_value == -1)
1320 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
1323 data1_local_attribute *local_attr;
1325 attp = att_getentbyatt (attributeset, use_value);
1328 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
1329 attributeset, use_value);
1330 return zi->errCode = 114;
1332 if (zebTargetInfo_curDatabase (zi->zti, basenames[base_no]))
1334 zi->errString = basenames[base_no];
1335 return zi->errCode = 109; /* Database unavailable */
1337 for (local_attr = attp->local_attributes; local_attr && ord_no < 32;
1338 local_attr = local_attr->next)
1342 ord = zebTargetInfo_lookupSU (zi->zti, attp->attset_ordinal,
1345 ords[ord_no++] = ord;
1349 return zi->errCode = 113;
1352 scan_info_array = odr_malloc (zi->odr, ord_no * sizeof(*scan_info_array));
1353 for (i = 0; i < ord_no; i++)
1355 int j, prefix_len = 0;
1356 int before_tmp = before, after_tmp = after;
1357 struct scan_info *scan_info = scan_info_array + i;
1359 scan_info->before = before;
1360 scan_info->after = after;
1361 scan_info->odr = zi->odr;
1363 scan_info->list = odr_malloc (zi->odr, (before+after)*
1364 sizeof(*scan_info->list));
1365 for (j = 0; j<before+after; j++)
1366 scan_info->list[j].term = NULL;
1367 termz[prefix_len++] = ords[i];
1368 termz[prefix_len++] =
1369 (completeness_value==2 || completeness_value==3) ? 'p': 'w';
1370 termz[prefix_len] = 0;
1371 strcpy (scan_info->prefix, termz);
1373 trans_scan_term (zi, zapt, termz+prefix_len);
1375 dict_scan (zi->dict, termz, &before_tmp, &after_tmp, scan_info,
1378 glist = odr_malloc (zi->odr, (before+after)*sizeof(*glist));
1379 for (i = 0; i < ord_no; i++)
1382 *status = BEND_SCAN_SUCCESS;
1383 for (i = 0; i<after; i++)
1386 const char *mterm = NULL;
1390 for (j = 0; j < ord_no; j++)
1392 if (ptr[j] < before+after &&
1393 (tst=scan_info_array[j].list[ptr[j]].term) &&
1394 (!mterm || strcmp (tst, mterm) < 0))
1402 scan_term_untrans (zi->odr, &glist[i+before].term, mterm);
1403 rset = rset_trunc (zi, &scan_info_array[j0].list[ptr[j0]].isam_p, 1);
1406 for (j = j0+1; j<ord_no; j++)
1408 if (ptr[j] < before+after &&
1409 (tst=scan_info_array[j].list[ptr[j]].term) &&
1410 !strcmp (tst, mterm))
1412 rset_bool_parms bool_parms;
1416 rset_trunc (zi, &scan_info_array[j].list[ptr[j]].isam_p, 1);
1418 bool_parms.key_size = sizeof(struct it_key);
1419 bool_parms.cmp = key_compare;
1420 bool_parms.rset_l = rset;
1421 bool_parms.rset_r = rset2;
1423 rset = rset_create (rset_kind_or, &bool_parms);
1428 count_set (rset, &glist[i+before].occurrences);
1433 *num_entries -= (after-i);
1434 *status = BEND_SCAN_PARTIAL;
1437 for (i = 0; i<ord_no; i++)
1440 for (i = 0; i<before; i++)
1443 const char *mterm = NULL;
1447 for (j = 0; j <ord_no; j++)
1449 if (ptr[j] < before &&
1450 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
1451 (!mterm || strcmp (tst, mterm) > 0))
1460 scan_term_untrans (zi->odr, &glist[before-1-i].term, mterm);
1463 (zi, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1);
1467 for (j = j0+1; j<ord_no; j++)
1469 if (ptr[j] < before &&
1470 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
1471 !strcmp (tst, mterm))
1473 rset_bool_parms bool_parms;
1476 rset2 = rset_trunc (zi,
1477 &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1);
1479 bool_parms.key_size = sizeof(struct it_key);
1480 bool_parms.cmp = key_compare;
1481 bool_parms.rset_l = rset;
1482 bool_parms.rset_r = rset2;
1484 rset = rset_create (rset_kind_or, &bool_parms);
1489 count_set (rset, &glist[before-1-i].occurrences);
1495 *status = BEND_SCAN_PARTIAL;
1499 *list = glist + i; /* list is set to first 'real' entry */
1501 logf (LOG_DEBUG, "position = %d, num_entries = %d",
1502 *position, *num_entries);
1504 logf (LOG_DEBUG, "scan error: %d", zi->errCode);