2 * Copyright (C) 1994-1996, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.50 1996-06-07 08:51:53 adam
8 * Bug fix: Character mapping was broken (introducued by last revision).
10 * Revision 1.49 1996/06/04 10:18:11 adam
11 * Search/scan uses character mapping module.
13 * Revision 1.48 1996/05/28 15:15:01 adam
14 * Bug fix: Didn't handle unknown database correctly.
16 * Revision 1.47 1996/05/15 18:36:28 adam
17 * Function trans_term transforms unsearchable characters to blanks.
19 * Revision 1.46 1996/05/15 11:57:56 adam
20 * Fixed bug introduced by set/field mapping in search operations.
22 * Revision 1.45 1996/05/14 11:34:00 adam
23 * Scan support in multiple registers/databases.
25 * Revision 1.44 1996/05/14 06:16:44 adam
26 * Compact use/set bytes used in search service.
28 * Revision 1.43 1996/05/09 09:54:43 adam
29 * Server supports maps from one logical attributes to a list of physical
31 * The extraction process doesn't make space consuming 'any' keys.
33 * Revision 1.42 1996/05/09 07:28:56 quinn
34 * Work towards phrases and multiple registers
36 * Revision 1.41 1996/03/20 09:36:43 adam
37 * Function dict_lookup_grep got extra parameter, init_pos, which marks
38 * from which position in pattern approximate pattern matching should occur.
39 * Approximate pattern matching is used in relevance=re-2.
41 * Revision 1.40 1996/02/02 13:44:44 adam
42 * The public dictionary functions simply use char instead of Dict_char
43 * to represent search strings. Dict_char is used internally only.
45 * Revision 1.39 1996/01/03 16:22:13 quinn
48 * Revision 1.38 1995/12/11 09:12:55 adam
49 * The rec_get function returns NULL if record doesn't exist - will
50 * happen in the server if the result set records have been deleted since
51 * the creation of the set (i.e. the search).
52 * The server saves a result temporarily if it is 'volatile', i.e. the
53 * set is register dependent.
55 * Revision 1.37 1995/12/06 15:05:28 adam
56 * More verbose in count_set.
58 * Revision 1.36 1995/12/06 12:41:27 adam
59 * New command 'stat' for the index program.
60 * Filenames can be read from stdin by specifying '-'.
61 * Bug fix/enhancement of the transformation from terms to regular
62 * expressons in the search engine.
64 * Revision 1.35 1995/11/27 09:29:00 adam
65 * Bug fixes regarding conversion to regular expressions.
67 * Revision 1.34 1995/11/16 17:00:56 adam
68 * Better logging of rpn query.
70 * Revision 1.33 1995/11/01 13:58:28 quinn
71 * Moving data1 to yaz/retrieval
73 * Revision 1.32 1995/10/27 14:00:11 adam
74 * Implemented detection of database availability.
76 * Revision 1.31 1995/10/17 18:02:10 adam
77 * New feature: databases. Implemented as prefix to words in dictionary.
79 * Revision 1.30 1995/10/16 09:32:38 adam
80 * More work on relational op.
82 * Revision 1.29 1995/10/13 16:01:49 adam
85 * Revision 1.28 1995/10/13 12:26:43 adam
86 * Optimization of truncation.
88 * Revision 1.27 1995/10/12 17:07:22 adam
91 * Revision 1.26 1995/10/12 12:40:54 adam
92 * Bug fixes in rpn_prox.
94 * Revision 1.25 1995/10/10 13:59:24 adam
95 * Function rset_open changed its wflag parameter to general flags.
97 * Revision 1.24 1995/10/09 16:18:37 adam
98 * Function dict_lookup_grep got extra client data parameter.
100 * Revision 1.23 1995/10/06 16:33:37 adam
101 * Use attribute mappings.
103 * Revision 1.22 1995/10/06 15:07:39 adam
104 * Structure 'local-number' handled.
106 * Revision 1.21 1995/10/06 13:52:06 adam
107 * Bug fixes. Handler may abort further scanning.
109 * Revision 1.20 1995/10/06 11:06:33 adam
110 * Scan entries include 'occurrences' now.
112 * Revision 1.19 1995/10/06 10:43:56 adam
113 * Scan added. 'occurrences' in scan entries not set yet.
115 * Revision 1.18 1995/10/04 16:57:20 adam
116 * Key input and merge sort in one pass.
118 * Revision 1.17 1995/10/04 12:55:17 adam
119 * Bug fix in ranked search. Use=Any keys inserted.
121 * Revision 1.16 1995/10/02 16:24:40 adam
122 * Use attribute actually used in search requests.
124 * Revision 1.15 1995/10/02 15:18:52 adam
125 * New member in recRetrieveCtrl: diagnostic.
127 * Revision 1.14 1995/09/28 12:10:32 adam
128 * Bug fixes. Field prefix used in queries.
130 * Revision 1.13 1995/09/18 14:17:50 adam
133 * Revision 1.12 1995/09/15 14:45:21 adam
135 * Work on truncation.
137 * Revision 1.11 1995/09/14 11:53:27 adam
138 * First work on regular expressions/truncations.
140 * Revision 1.10 1995/09/11 15:23:26 adam
141 * More work on relevance search.
143 * Revision 1.9 1995/09/11 13:09:35 adam
144 * More work on relevance feedback.
146 * Revision 1.8 1995/09/08 14:52:27 adam
147 * Minor changes. Dictionary is lower case now.
149 * Revision 1.7 1995/09/07 13:58:36 adam
150 * New parameter: result-set file descriptor (RSFD) to support multiple
151 * positions within the same result-set.
152 * Boolean operators: and, or, not implemented.
153 * Result-set references.
155 * Revision 1.6 1995/09/06 16:11:18 adam
156 * Option: only one word key per file.
158 * Revision 1.5 1995/09/06 10:33:04 adam
159 * More work on present. Some log messages removed.
161 * Revision 1.4 1995/09/05 15:28:40 adam
162 * More work on search engine.
164 * Revision 1.3 1995/09/04 15:20:22 adam
167 * Revision 1.2 1995/09/04 12:33:43 adam
168 * Various cleanup. YAZ util used instead.
170 * Revision 1.1 1995/09/04 09:10:40 adam
171 * More work on index add/del/update.
172 * Merge sort implemented.
173 * Initial work on z39 server.
182 #include "attribute.h"
195 Z_AttributesPlusTerm *zapt;
198 static int attr_find (AttrType *src, oid_value *attributeSetP)
200 while (src->major < src->zapt->num_attributes)
202 Z_AttributeElement *element;
204 element = src->zapt->attributeList[src->major];
205 if (src->type == *element->attributeType)
207 switch (element->which)
209 case Z_AttributeValue_numeric:
211 if (element->attributeSet && attributeSetP)
215 attrset = oid_getentbyoid (element->attributeSet);
216 *attributeSetP = attrset->value;
218 return *element->value.numeric;
220 case Z_AttributeValue_complex:
221 if (src->minor >= element->value.complex->num_list ||
222 element->value.complex->list[src->minor]->which !=
223 Z_StringOrNumeric_numeric)
226 if (element->attributeSet && attributeSetP)
230 attrset = oid_getentbyoid (element->attributeSet);
231 *attributeSetP = attrset->value;
233 return *element->value.complex->list[src->minor-1]->u.numeric;
243 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
257 int (*cmp)(const void *p1, const void *p2);
264 static void heap_swap (struct trunc_info *ti, int i1, int i2)
269 ti->ptr[i1] = ti->ptr[i2];
273 static void heap_delete (struct trunc_info *ti)
275 int cur = 1, child = 2;
277 heap_swap (ti, 1, ti->heapnum--);
278 while (child <= ti->heapnum) {
279 if (child < ti->heapnum &&
280 (*ti->cmp)(ti->heap[ti->ptr[child]],
281 ti->heap[ti->ptr[1+child]]) > 0)
283 if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
284 ti->heap[ti->ptr[child]]) > 0)
286 heap_swap (ti, cur, child);
295 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
299 cur = ++(ti->heapnum);
300 memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
301 ti->indx[ti->ptr[cur]] = indx;
303 while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
304 ti->heap[ti->ptr[cur]]) > 0)
306 heap_swap (ti, cur, parent);
313 struct trunc_info *heap_init (int size, int key_size,
314 int (*cmp)(const void *p1, const void *p2))
316 struct trunc_info *ti = xmalloc (sizeof(*ti));
321 ti->keysize = key_size;
323 ti->indx = xmalloc (size * sizeof(*ti->indx));
324 ti->heap = xmalloc (size * sizeof(*ti->heap));
325 ti->ptr = xmalloc (size * sizeof(*ti->ptr));
326 ti->swapbuf = xmalloc (ti->keysize);
327 ti->tmpbuf = xmalloc (ti->keysize);
328 ti->buf = xmalloc (size * ti->keysize);
329 for (i = size; --i >= 0; )
332 ti->heap[i] = ti->buf + ti->keysize * i;
337 static void heap_close (struct trunc_info *ti)
347 static RSET rset_trunc_r (ISAM isam, ISAM_P *isam_p, int from, int to,
352 rset_temp_parms parms;
354 parms.key_size = sizeof(struct it_key);
355 result = rset_create (rset_kind_temp, &parms);
356 result_rsfd = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
358 if (to - from > merge_chunk)
362 int i, i_add = (to-from)/merge_chunk + 1;
363 struct trunc_info *ti;
365 int rsmax = (to-from)/i_add + 1;
367 rset = xmalloc (sizeof(*rset) * rsmax);
368 rsfd = xmalloc (sizeof(*rsfd) * rsmax);
370 for (i = from; i < to; i += i_add)
373 rset[rscur] = rset_trunc_r (isam, isam_p, i, i+i_add,
376 rset[rscur] = rset_trunc_r (isam, isam_p, i, to,
380 ti = heap_init (rscur, sizeof(struct it_key), key_compare);
381 for (i = rscur; --i >= 0; )
383 rsfd[i] = rset_open (rset[i], RSETF_READ|RSETF_SORT_SYSNO);
384 if (rset_read (rset[i], rsfd[i], ti->tmpbuf))
385 heap_insert (ti, ti->tmpbuf, i);
388 rset_close (rset[i], rsfd[i]);
389 rset_delete (rset[i]);
394 int n = ti->indx[ti->ptr[1]];
396 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
400 if (!rset_read (rset[n], rsfd[n], ti->tmpbuf))
403 rset_close (rset[n], rsfd[n]);
404 rset_delete (rset[n]);
407 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
410 heap_insert (ti, ti->tmpbuf, n);
423 struct trunc_info *ti;
425 ispt = xmalloc (sizeof(*ispt) * (to-from));
427 ti = heap_init (to-from, sizeof(struct it_key),
429 for (i = to-from; --i >= 0; )
431 ispt[i] = is_position (isam, isam_p[from+i]);
432 if (is_readkey (ispt[i], ti->tmpbuf))
433 heap_insert (ti, ti->tmpbuf, i);
435 is_pt_free (ispt[i]);
439 int n = ti->indx[ti->ptr[1]];
441 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
443 /* section that preserve all keys */
445 if (is_readkey (ispt[n], ti->tmpbuf))
446 heap_insert (ti, ti->tmpbuf, n);
448 is_pt_free (ispt[n]);
450 /* section that preserve all keys with unique sysnos */
453 if (!is_readkey (ispt[n], ti->tmpbuf))
456 is_pt_free (ispt[n]);
459 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
462 heap_insert (ti, ti->tmpbuf, n);
471 rset_close (result, result_rsfd);
475 static int isam_trunc_cmp (const void *p1, const void *p2)
477 ISAM_P i1 = *(ISAM_P*) p1;
478 ISAM_P i2 = *(ISAM_P*) p2;
481 d = is_type (i1) - is_type (i2);
484 return is_block (i1) - is_block (i2);
487 static RSET rset_trunc (ISAM isam, ISAM_P *isam_p, int no)
490 qsort (isam_p, no, sizeof(*isam_p), isam_trunc_cmp);
491 return rset_trunc_r (isam, isam_p, 0, no, 100);
500 static void add_isam_p (const char *info, struct grep_info *p)
502 if (p->isam_p_indx == p->isam_p_size)
504 ISAM_P *new_isam_p_buf;
506 p->isam_p_size = 2*p->isam_p_size + 100;
507 new_isam_p_buf = xmalloc (sizeof(*new_isam_p_buf) *
511 memcpy (new_isam_p_buf, p->isam_p_buf,
512 p->isam_p_indx * sizeof(*p->isam_p_buf));
513 xfree (p->isam_p_buf);
515 p->isam_p_buf = new_isam_p_buf;
517 assert (*info == sizeof(*p->isam_p_buf));
518 memcpy (p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
522 static int grep_handle (char *name, const char *info, void *p)
524 logf (LOG_DEBUG, "dict name: %s", name);
525 add_isam_p (info, p);
529 static void gen_regular_rel (char *dst, int val, int islt)
537 sprintf (numstr, "%d", val);
538 for (w = strlen(numstr); --w >= 0; pos++)
557 strcpy (dst + dst_p, numstr);
558 dst_p = strlen(dst) - pos - 1;
586 for (i = 0; i<pos; i++)
599 for (i=1; i<pos; i++)
600 strcat (dst, "[0-9]?");
604 for (i = 0; i <= pos; i++)
605 strcat (dst, "[0-9]");
606 strcat (dst, "[0-9]*");
611 static int relational_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
612 const char *term_sub,
614 oid_value attributeSet,
615 struct grep_info *grep_info,
623 attr_init (&relation, zapt, 2);
624 relation_value = attr_find (&relation, NULL);
625 term_value = atoi (term_sub);
627 switch (relation_value)
632 logf (LOG_DEBUG, "Relation <");
633 gen_regular_rel (term_dict + strlen(term_dict), term_value-1, 1);
638 logf (LOG_DEBUG, "Relation <=");
639 gen_regular_rel (term_dict + strlen(term_dict), term_value, 1);
644 logf (LOG_DEBUG, "Relation >=");
645 gen_regular_rel (term_dict + strlen(term_dict), term_value, 0);
650 logf (LOG_DEBUG, "Relation >");
651 gen_regular_rel (term_dict + strlen(term_dict), term_value+1, 0);
656 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_dict);
657 r = dict_lookup_grep (zi->wordDict, term_dict, 0, grep_info, max_pos,
660 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
661 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
665 static void verbatim_char (int ch, int *indx, char *dst)
668 dst[(*indx)++] = '\\';
672 static int field_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
673 const char *term_sub, int regType,
674 oid_value attributeSet, struct grep_info *grep_info,
675 int num_bases, char **basenames)
677 char term_dict[2*IT_MAX_WORD+2];
678 int i, j, r, base_no;
680 int truncation_value;
683 oid_value curAttributeSet = attributeSet;
685 attr_init (&use, zapt, 1);
686 use_value = attr_find (&use, &curAttributeSet);
687 logf (LOG_DEBUG, "use value %d", use_value);
688 attr_init (&truncation, zapt, 5);
689 truncation_value = attr_find (&truncation, NULL);
690 logf (LOG_DEBUG, "truncation value %d", truncation_value);
695 for (base_no = 0; base_no < num_bases; base_no++)
698 data1_local_attribute *local_attr;
699 int max_pos, prefix_len = 0;
701 attp = att_getentbyatt (curAttributeSet, use_value);
704 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
705 curAttributeSet, use_value);
709 if (zebTargetInfo_curDatabase (zi->zti, basenames[base_no]))
711 zi->errCode = 109; /* Database unavailable */
712 zi->errString = basenames[base_no];
715 for (local_attr = attp->local_attributes; local_attr;
716 local_attr = local_attr->next)
720 ord = zebTargetInfo_lookupSU (zi->zti, attp->attset_ordinal,
725 term_dict[prefix_len++] = '|';
727 term_dict[prefix_len++] = '(';
728 term_dict[prefix_len++] = 1;
729 term_dict[prefix_len++] = ord;
736 term_dict[prefix_len++] = ')';
737 term_dict[prefix_len++] = 1;
738 term_dict[prefix_len++] = regType;
739 term_dict[prefix_len] = '\0';
740 if (!relational_term (zi, zapt, term_sub, term_dict,
741 attributeSet, grep_info, &max_pos))
746 switch (truncation_value)
748 case -1: /* not specified */
749 case 100: /* do not truncate */
750 term_dict[j++] = '(';
751 for (i = 0; term_sub[i]; i++)
752 verbatim_char (term_sub[i], &j, term_dict);
753 strcpy (term_dict+j, ")");
754 r = dict_lookup_grep (zi->wordDict, term_dict, 0, grep_info,
755 &max_pos, 0, grep_handle);
757 logf (LOG_WARN, "dict_lookup_grep err, trunc=none:%d", r);
759 case 1: /* right truncation */
760 term_dict[j++] = '(';
761 for (i = 0; term_sub[i]; i++)
762 verbatim_char (term_sub[i], &j, term_dict);
763 strcpy (term_dict+j, ".*)");
764 dict_lookup_grep (zi->wordDict, term_dict, 0, grep_info,
765 &max_pos, 0, grep_handle);
767 case 2: /* left truncation */
768 case 3: /* left&right truncation */
771 case 101: /* process # in term */
772 term_dict[j++] = '(';
773 for (i=0; term_sub[i]; i++)
774 if (term_sub[i] == '#' && i > 2)
776 term_dict[j++] = '.';
777 term_dict[j++] = '*';
780 verbatim_char (term_sub[i], &j, term_dict);
781 strcpy (term_dict+j, ")");
782 r = dict_lookup_grep (zi->wordDict, term_dict, 0, grep_info,
783 &max_pos, 0, grep_handle);
785 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d",
788 case 102: /* regular expression */
789 sprintf (term_dict + j, "(%s)", term_sub);
790 r = dict_lookup_grep (zi->wordDict, term_dict, 0, grep_info,
791 &max_pos, 0, grep_handle);
793 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
796 case 103: /* regular expression with error correction */
799 if (*cp == '*' && cp[1] && cp[2])
804 sprintf (term_dict + j, "(%s)", cp);
805 r = dict_lookup_grep (zi->wordDict, term_dict, r, grep_info,
806 &max_pos, j, grep_handle);
808 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
814 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
818 static void trans_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
822 Z_Term *term = zapt->term;
824 sizez = term->u.general->len;
825 if (sizez > IT_MAX_WORD-1)
826 sizez = IT_MAX_WORD-1;
827 memcpy (termz, term->u.general->buf, sizez);
831 static void trans_scan_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
834 Z_Term *term = zapt->term;
836 const char *cp = (const char *) term->u.general->buf;
837 const char *cp_end = cp + term->u.general->len;
843 while ((len = (cp_end - cp)) > 0)
845 map = map_chrs_input (&cp, len);
846 if (**map == *CHR_SPACE)
854 for (src = *map; *src; src++)
860 static RSET rpn_search_APT_relevance (ZServerInfo *zi,
861 Z_AttributesPlusTerm *zapt,
862 oid_value attributeSet,
863 int num_bases, char **basenames)
865 rset_relevance_parms parms;
866 char termz[IT_MAX_WORD+1];
867 char term_sub[IT_MAX_WORD+1];
868 struct grep_info grep_info;
872 parms.key_size = sizeof(struct it_key);
874 parms.cmp = key_compare;
875 parms.is = zi->wordIsam;
877 if (zapt->term->which != Z_Term_general)
882 trans_term (zi, zapt, termz);
884 grep_info.isam_p_indx = 0;
885 grep_info.isam_p_size = 0;
886 grep_info.isam_p_buf = NULL;
895 map = map_chrs_input (&p1, strlen(p1));
896 if (**map != *CHR_SPACE)
905 map = map_chrs_input (&p1, strlen(p1));
906 if (**map == *CHR_SPACE)
911 memcpy (term_sub, p0, p2-p0);
912 term_sub[p2-p0] = '\0';
914 if (field_term (zi, zapt, term_sub, 'w', attributeSet, &grep_info,
915 num_bases, basenames))
918 parms.isam_positions = grep_info.isam_p_buf;
919 parms.no_isam_positions = grep_info.isam_p_indx;
920 if (grep_info.isam_p_indx > 0)
921 result = rset_create (rset_kind_relevance, &parms);
923 result = rset_create (rset_kind_null, NULL);
924 xfree (grep_info.isam_p_buf);
928 static RSET rpn_search_APT_cphrase (ZServerInfo *zi,
929 Z_AttributesPlusTerm *zapt,
930 oid_value attributeSet,
931 int num_bases, char **basenames)
933 rset_isam_parms parms;
934 char termz[IT_MAX_WORD+1];
935 struct grep_info grep_info;
938 if (zapt->term->which != Z_Term_general)
943 trans_term (zi, zapt, termz);
945 grep_info.isam_p_indx = 0;
946 grep_info.isam_p_size = 0;
947 grep_info.isam_p_buf = NULL;
949 if (field_term (zi, zapt, termz, 'p', attributeSet, &grep_info,
950 num_bases, basenames))
952 if (grep_info.isam_p_indx < 1)
953 result = rset_create (rset_kind_null, NULL);
954 else if (grep_info.isam_p_indx == 1)
956 parms.is = zi->wordIsam;
957 parms.pos = *grep_info.isam_p_buf;
958 result = rset_create (rset_kind_isam, &parms);
961 result = rset_trunc (zi->wordIsam, grep_info.isam_p_buf,
962 grep_info.isam_p_indx);
963 xfree (grep_info.isam_p_buf);
967 static RSET rpn_prox (RSET *rset, int rset_no)
975 rset_temp_parms parms;
977 rsfd = xmalloc (sizeof(*rsfd)*rset_no);
978 more = xmalloc (sizeof(*more)*rset_no);
979 buf = xmalloc (sizeof(*buf)*rset_no);
981 for (i = 0; i<rset_no; i++)
983 buf[i] = xmalloc (sizeof(**buf));
984 rsfd[i] = rset_open (rset[i], RSETF_READ|RSETF_SORT_SYSNO);
985 if (!(more[i] = rset_read (rset[i], rsfd[i], buf[i])))
989 rset_close (rset[i], rsfd[i]);
996 return rset_create (rset_kind_null, NULL);
999 parms.key_size = sizeof (struct it_key);
1000 result = rset_create (rset_kind_temp, &parms);
1001 rsfd_result = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
1005 for (i = 1; i<rset_no; i++)
1014 cmp = key_compare (buf[i], buf[i-1]);
1017 more[i-1] = rset_read (rset[i-1], rsfd[i-1], buf[i-1]);
1022 if (buf[i-1]->seqno+1 != buf[i]->seqno)
1024 more[i-1] = rset_read (rset[i-1], rsfd[i-1], buf[i-1]);
1030 more[i] = rset_read (rset[i], rsfd[i], buf[i]);
1036 rset_write (result, rsfd_result, buf[0]);
1037 more[0] = rset_read (*rset, *rsfd, *buf);
1041 for (i = 0; i<rset_no; i++)
1043 rset_close (rset[i], rsfd[i]);
1046 rset_close (result, rsfd_result);
1053 static RSET rpn_search_APT_phrase (ZServerInfo *zi,
1054 Z_AttributesPlusTerm *zapt,
1055 oid_value attributeSet,
1056 int num_bases, char **basenames)
1058 char termz[IT_MAX_WORD+1];
1059 char term_sub[IT_MAX_WORD+1];
1061 RSET rset[60], result;
1063 struct grep_info grep_info;
1065 if (zapt->term->which != Z_Term_general)
1070 trans_term (zi, zapt, termz);
1072 grep_info.isam_p_size = 0;
1073 grep_info.isam_p_buf = NULL;
1083 map = map_chrs_input (&p1, strlen(p1));
1084 if (**map != *CHR_SPACE)
1093 map = map_chrs_input (&p1, strlen(p1));
1094 if (**map == *CHR_SPACE)
1100 memcpy (term_sub, p0, p2-p0);
1101 term_sub[p2-p0] = '\0';
1104 grep_info.isam_p_indx = 0;
1105 if (field_term (zi, zapt, term_sub, 'w', attributeSet, &grep_info,
1106 num_bases, basenames))
1108 if (grep_info.isam_p_indx == 0)
1109 rset[rset_no] = rset_create (rset_kind_null, NULL);
1110 else if (grep_info.isam_p_indx > 1)
1111 rset[rset_no] = rset_trunc (zi->wordIsam,
1112 grep_info.isam_p_buf,
1113 grep_info.isam_p_indx);
1116 rset_isam_parms parms;
1118 parms.is = zi->wordIsam;
1119 parms.pos = *grep_info.isam_p_buf;
1120 rset[rset_no] = rset_create (rset_kind_isam, &parms);
1122 assert (rset[rset_no]);
1123 if (++rset_no >= sizeof(rset)/sizeof(*rset))
1126 xfree (grep_info.isam_p_buf);
1128 return rset_create (rset_kind_null, NULL);
1129 else if (rset_no == 1)
1132 result = rpn_prox (rset, rset_no);
1133 for (i = 0; i<rset_no; i++)
1134 rset_delete (rset[i]);
1138 static RSET rpn_search_APT_local (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1139 oid_value attributeSet)
1144 rset_temp_parms parms;
1145 char termz[IT_MAX_WORD+1];
1147 if (zapt->term->which != Z_Term_general)
1152 parms.key_size = sizeof (struct it_key);
1153 result = rset_create (rset_kind_temp, &parms);
1154 rsfd = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
1156 trans_term (zi, zapt, termz);
1158 key.sysno = atoi (termz);
1161 rset_write (result, rsfd, &key);
1162 rset_close (result, rsfd);
1166 static RSET rpn_search_APT (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1167 oid_value attributeSet,
1168 int num_bases, char **basenames)
1172 AttrType completeness;
1173 int relation_value, structure_value, completeness_value;
1175 attr_init (&relation, zapt, 2);
1176 attr_init (&structure, zapt, 4);
1177 attr_init (&completeness, zapt, 6);
1179 relation_value = attr_find (&relation, NULL);
1180 structure_value = attr_find (&structure, NULL);
1181 completeness_value = attr_find (&completeness, NULL);
1182 switch (structure_value)
1185 if (relation_value == 102) /* relevance relation */
1186 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1187 num_bases, basenames);
1188 if (completeness_value == 2 || completeness_value == 3)
1189 return rpn_search_APT_cphrase (zi, zapt, attributeSet,
1190 num_bases, basenames);
1191 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1192 num_bases, basenames);
1193 case 1: /* phrase */
1194 if (relation_value == 102) /* relevance relation */
1195 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1196 num_bases, basenames);
1197 if (completeness_value == 2 || completeness_value == 3)
1198 return rpn_search_APT_cphrase (zi, zapt, attributeSet,
1199 num_bases, basenames);
1200 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1201 num_bases, basenames);
1204 if (relation_value == 102) /* relevance relation */
1205 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1206 num_bases, basenames);
1207 if (completeness_value == 2 || completeness_value == 3)
1208 return rpn_search_APT_cphrase (zi, zapt, attributeSet,
1209 num_bases, basenames);
1210 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1211 num_bases, basenames);
1216 case 5: /* date - normalized */
1218 case 6: /* word list */
1219 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1220 num_bases, basenames);
1221 case 100: /* date - un-normalized */
1223 case 101: /* name - normalized */
1225 case 102: /* date - un-normalized */
1227 case 103: /* structure */
1231 case 105: /* free-form-text */
1232 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1233 num_bases, basenames);
1234 case 106: /* document-text */
1235 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1236 num_bases, basenames);
1237 case 107: /* local-number */
1238 return rpn_search_APT_local (zi, zapt, attributeSet);
1239 case 108: /* string */
1240 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1241 num_bases, basenames);
1242 case 109: /* numeric string */
1249 static RSET rpn_search_ref (ZServerInfo *zi, Z_ResultSetId *resultSetId)
1253 if (!(s = resultSetGet (zi, resultSetId)))
1254 return rset_create (rset_kind_null, NULL);
1258 static RSET rpn_search_structure (ZServerInfo *zi, Z_RPNStructure *zs,
1259 oid_value attributeSet,
1260 int num_bases, char **basenames)
1263 if (zs->which == Z_RPNStructure_complex)
1265 rset_bool_parms bool_parms;
1268 bool_parms.rset_l = rpn_search_structure (zi, zs->u.complex->s1,
1270 num_bases, basenames);
1271 if (bool_parms.rset_l == NULL)
1273 if (rset_is_ranked(bool_parms.rset_l))
1275 bool_parms.rset_r = rpn_search_structure (zi, zs->u.complex->s2,
1277 num_bases, basenames);
1278 if (bool_parms.rset_r == NULL)
1280 rset_delete (bool_parms.rset_l);
1283 if (rset_is_ranked(bool_parms.rset_r))
1285 bool_parms.key_size = sizeof(struct it_key);
1286 bool_parms.cmp = key_compare;
1288 switch (zs->u.complex->roperator->which)
1290 case Z_Operator_and:
1291 r = rset_create (soft ? rset_kind_sand:rset_kind_and, &bool_parms);
1294 r = rset_create (soft ? rset_kind_sor:rset_kind_or, &bool_parms);
1296 case Z_Operator_and_not:
1297 r = rset_create (soft ? rset_kind_snot:rset_kind_not, &bool_parms);
1303 else if (zs->which == Z_RPNStructure_simple)
1305 if (zs->u.simple->which == Z_Operand_APT)
1307 logf (LOG_DEBUG, "rpn_search_APT");
1308 r = rpn_search_APT (zi, zs->u.simple->u.attributesPlusTerm,
1309 attributeSet, num_bases, basenames);
1311 else if (zs->u.simple->which == Z_Operand_resultSetId)
1313 logf (LOG_DEBUG, "rpn_search_ref");
1314 r = rpn_search_ref (zi, zs->u.simple->u.resultSetId);
1328 void count_set_save (RSET *r, int *count)
1335 rset_temp_parms parms;
1337 logf (LOG_DEBUG, "count_set_save");
1339 parms.key_size = sizeof(struct it_key);
1340 w = rset_create (rset_kind_temp, &parms);
1341 wfd = rset_open (w, RSETF_WRITE|RSETF_SORT_SYSNO);
1342 rfd = rset_open (*r, RSETF_READ|RSETF_SORT_SYSNO);
1343 while (rset_read (*r, rfd, &key))
1345 if (key.sysno != psysno)
1347 rset_write (w, wfd, &key);
1353 rset_close (*r, rfd);
1355 rset_close (w, wfd);
1357 logf (LOG_DEBUG, "%d keys, %d distinct sysnos", kno, *count);
1360 static void count_set (RSET r, int *count)
1367 logf (LOG_DEBUG, "count_set");
1369 rfd = rset_open (r, RSETF_READ|RSETF_SORT_SYSNO);
1370 while (rset_read (r, rfd, &key))
1372 if (key.sysno != psysno)
1379 rset_close (r, rfd);
1380 logf (LOG_DEBUG, "%d keys, %d distinct sysnos", kno, *count);
1383 int rpn_search (ZServerInfo *zi,
1384 Z_RPNQuery *rpn, int num_bases, char **basenames,
1385 const char *setname, int *hits)
1389 oid_value attributeSet;
1391 dict_grep_cmap (zi->wordDict, map_chrs_input);
1395 zi->errString = NULL;
1397 attrset = oid_getentbyoid (rpn->attributeSetId);
1398 attributeSet = attrset->value;
1399 rset = rpn_search_structure (zi, rpn->RPNStructure, attributeSet,
1400 num_bases, basenames);
1403 if (rset_is_volatile(rset))
1404 count_set_save(&rset,hits);
1406 count_set (rset, hits);
1407 resultSetAdd (zi, setname, 1, rset);
1409 logf (LOG_DEBUG, "search error: %d", zi->errCode);
1413 struct scan_info_entry {
1419 struct scan_info_entry *list;
1425 static int scan_handle (char *name, const char *info, int pos, void *client)
1427 int len_prefix, idx;
1428 struct scan_info *scan_info = client;
1430 len_prefix = strlen(scan_info->prefix);
1431 if (memcmp (name, scan_info->prefix, len_prefix))
1434 idx = scan_info->after - pos + scan_info->before;
1437 logf (LOG_DEBUG, "%-3d %s", idx, name+len_prefix);
1438 scan_info->list[idx].term = odr_malloc (scan_info->odr,
1439 strlen(name + len_prefix)+1);
1440 strcpy (scan_info->list[idx].term, name + len_prefix);
1441 assert (*info == sizeof(ISAM_P));
1442 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAM_P));
1447 static void scan_term_untrans (ODR odr, char **dstp, const char *src)
1449 char *dst = odr_malloc (odr, strlen(src)*2+1);
1454 const char *cp = map_chrs_output (&src);
1461 int rpn_scan (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1462 oid_value attributeset,
1463 int num_bases, char **basenames,
1464 int *position, int *num_entries, struct scan_entry **list,
1468 int pos = *position;
1469 int num = *num_entries;
1473 char termz[IT_MAX_WORD+20];
1476 AttrType completeness;
1477 int completeness_value;
1478 struct scan_info *scan_info_array;
1479 struct scan_entry *glist;
1480 int ords[32], ord_no = 0;
1483 logf (LOG_DEBUG, "scan, position = %d, num = %d", pos, num);
1485 attr_init (&use, zapt, 1);
1486 use_value = attr_find (&use, NULL);
1487 logf (LOG_DEBUG, "use value %d", use_value);
1489 attr_init (&completeness, zapt, 6);
1490 completeness_value = attr_find (&completeness, NULL);
1491 logf (LOG_DEBUG, "completeness value %d", completeness_value);
1493 if (attributeset == VAL_NONE)
1494 attributeset = VAL_BIB1;
1496 if (use_value == -1)
1498 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
1501 data1_local_attribute *local_attr;
1503 attp = att_getentbyatt (attributeset, use_value);
1506 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
1507 attributeset, use_value);
1508 return zi->errCode = 114;
1510 if (zebTargetInfo_curDatabase (zi->zti, basenames[base_no]))
1512 zi->errString = basenames[base_no];
1513 return zi->errCode = 109; /* Database unavailable */
1515 for (local_attr = attp->local_attributes; local_attr && ord_no < 32;
1516 local_attr = local_attr->next)
1520 ord = zebTargetInfo_lookupSU (zi->zti, attp->attset_ordinal,
1523 ords[ord_no++] = ord;
1527 return zi->errCode = 113;
1530 scan_info_array = odr_malloc (zi->odr, ord_no * sizeof(*scan_info_array));
1531 for (i = 0; i < ord_no; i++)
1533 int j, prefix_len = 0;
1534 int before_tmp = before, after_tmp = after;
1535 struct scan_info *scan_info = scan_info_array + i;
1537 scan_info->before = before;
1538 scan_info->after = after;
1539 scan_info->odr = zi->odr;
1541 scan_info->list = odr_malloc (zi->odr, (before+after)*
1542 sizeof(*scan_info->list));
1543 for (j = 0; j<before+after; j++)
1544 scan_info->list[j].term = NULL;
1545 termz[prefix_len++] = ords[i];
1546 termz[prefix_len++] =
1547 (completeness_value==2 || completeness_value==3) ? 'p': 'w';
1548 termz[prefix_len] = 0;
1549 strcpy (scan_info->prefix, termz);
1551 trans_scan_term (zi, zapt, termz+prefix_len);
1553 dict_scan (zi->wordDict, termz, &before_tmp, &after_tmp, scan_info,
1556 glist = odr_malloc (zi->odr, (before+after)*sizeof(*glist));
1557 for (i = 0; i < ord_no; i++)
1560 *status = BEND_SCAN_SUCCESS;
1561 for (i = 0; i<after; i++)
1564 const char *mterm = NULL;
1567 rset_isam_parms parms;
1569 for (j = 0; j < ord_no; j++)
1571 if (ptr[j] < before+after &&
1572 (tst=scan_info_array[j].list[ptr[j]].term) &&
1573 (!mterm || strcmp (tst, mterm) < 0))
1581 scan_term_untrans (zi->odr, &glist[i+before].term, mterm);
1582 parms.is = zi->wordIsam;
1583 parms.pos = scan_info_array[j0].list[ptr[j0]].isam_p;
1584 rset = rset_create (rset_kind_isam, &parms);
1587 for (j = j0+1; j<ord_no; j++)
1589 if (ptr[j] < before+after &&
1590 (tst=scan_info_array[j].list[ptr[j]].term) &&
1591 !strcmp (tst, mterm))
1593 rset_isam_parms parms;
1594 rset_bool_parms bool_parms;
1597 parms.is = zi->wordIsam;
1598 parms.pos = scan_info_array[j].list[ptr[j]].isam_p;
1599 rset2 = rset_create (rset_kind_isam, &parms);
1601 bool_parms.key_size = sizeof(struct it_key);
1602 bool_parms.cmp = key_compare;
1603 bool_parms.rset_l = rset;
1604 bool_parms.rset_r = rset2;
1606 rset = rset_create (rset_kind_or, &bool_parms);
1611 count_set (rset, &glist[i+before].occurrences);
1616 *num_entries -= (after-i);
1617 *status = BEND_SCAN_PARTIAL;
1620 for (i = 0; i<ord_no; i++)
1623 for (i = 0; i<before; i++)
1626 const char *mterm = NULL;
1629 rset_isam_parms parms;
1631 for (j = 0; j <ord_no; j++)
1633 if (ptr[j] < before &&
1634 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
1635 (!mterm || strcmp (tst, mterm) > 0))
1644 scan_term_untrans (zi->odr, &glist[before-1-i].term, mterm);
1646 parms.is = zi->wordIsam;
1647 parms.pos = scan_info_array[j0].list[before-1-ptr[j0]].isam_p;
1648 rset = rset_create (rset_kind_isam, &parms);
1652 for (j = j0+1; j<ord_no; j++)
1654 if (ptr[j] < before &&
1655 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
1656 !strcmp (tst, mterm))
1658 rset_isam_parms parms;
1659 rset_bool_parms bool_parms;
1662 parms.is = zi->wordIsam;
1663 parms.pos = scan_info_array[j].list[before-1-ptr[j]].isam_p;
1664 rset2 = rset_create (rset_kind_isam, &parms);
1666 bool_parms.key_size = sizeof(struct it_key);
1667 bool_parms.cmp = key_compare;
1668 bool_parms.rset_l = rset;
1669 bool_parms.rset_r = rset2;
1671 rset = rset_create (rset_kind_or, &bool_parms);
1676 count_set (rset, &glist[before-1-i].occurrences);
1682 *status = BEND_SCAN_PARTIAL;
1686 *list = glist + i; /* list is set to first 'real' entry */
1688 logf (LOG_DEBUG, "position = %d, num_entries = %d",
1689 *position, *num_entries);
1691 logf (LOG_DEBUG, "scan error: %d", zi->errCode);