2 * Copyright (C) 1994-1996, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.49 1996-06-04 10:18:11 adam
8 * Search/scan uses character mapping module.
10 * Revision 1.48 1996/05/28 15:15:01 adam
11 * Bug fix: Didn't handle unknown database correctly.
13 * Revision 1.47 1996/05/15 18:36:28 adam
14 * Function trans_term transforms unsearchable characters to blanks.
16 * Revision 1.46 1996/05/15 11:57:56 adam
17 * Fixed bug introduced by set/field mapping in search operations.
19 * Revision 1.45 1996/05/14 11:34:00 adam
20 * Scan support in multiple registers/databases.
22 * Revision 1.44 1996/05/14 06:16:44 adam
23 * Compact use/set bytes used in search service.
25 * Revision 1.43 1996/05/09 09:54:43 adam
26 * Server supports maps from one logical attributes to a list of physical
28 * The extraction process doesn't make space consuming 'any' keys.
30 * Revision 1.42 1996/05/09 07:28:56 quinn
31 * Work towards phrases and multiple registers
33 * Revision 1.41 1996/03/20 09:36:43 adam
34 * Function dict_lookup_grep got extra parameter, init_pos, which marks
35 * from which position in pattern approximate pattern matching should occur.
36 * Approximate pattern matching is used in relevance=re-2.
38 * Revision 1.40 1996/02/02 13:44:44 adam
39 * The public dictionary functions simply use char instead of Dict_char
40 * to represent search strings. Dict_char is used internally only.
42 * Revision 1.39 1996/01/03 16:22:13 quinn
45 * Revision 1.38 1995/12/11 09:12:55 adam
46 * The rec_get function returns NULL if record doesn't exist - will
47 * happen in the server if the result set records have been deleted since
48 * the creation of the set (i.e. the search).
49 * The server saves a result temporarily if it is 'volatile', i.e. the
50 * set is register dependent.
52 * Revision 1.37 1995/12/06 15:05:28 adam
53 * More verbose in count_set.
55 * Revision 1.36 1995/12/06 12:41:27 adam
56 * New command 'stat' for the index program.
57 * Filenames can be read from stdin by specifying '-'.
58 * Bug fix/enhancement of the transformation from terms to regular
59 * expressons in the search engine.
61 * Revision 1.35 1995/11/27 09:29:00 adam
62 * Bug fixes regarding conversion to regular expressions.
64 * Revision 1.34 1995/11/16 17:00:56 adam
65 * Better logging of rpn query.
67 * Revision 1.33 1995/11/01 13:58:28 quinn
68 * Moving data1 to yaz/retrieval
70 * Revision 1.32 1995/10/27 14:00:11 adam
71 * Implemented detection of database availability.
73 * Revision 1.31 1995/10/17 18:02:10 adam
74 * New feature: databases. Implemented as prefix to words in dictionary.
76 * Revision 1.30 1995/10/16 09:32:38 adam
77 * More work on relational op.
79 * Revision 1.29 1995/10/13 16:01:49 adam
82 * Revision 1.28 1995/10/13 12:26:43 adam
83 * Optimization of truncation.
85 * Revision 1.27 1995/10/12 17:07:22 adam
88 * Revision 1.26 1995/10/12 12:40:54 adam
89 * Bug fixes in rpn_prox.
91 * Revision 1.25 1995/10/10 13:59:24 adam
92 * Function rset_open changed its wflag parameter to general flags.
94 * Revision 1.24 1995/10/09 16:18:37 adam
95 * Function dict_lookup_grep got extra client data parameter.
97 * Revision 1.23 1995/10/06 16:33:37 adam
98 * Use attribute mappings.
100 * Revision 1.22 1995/10/06 15:07:39 adam
101 * Structure 'local-number' handled.
103 * Revision 1.21 1995/10/06 13:52:06 adam
104 * Bug fixes. Handler may abort further scanning.
106 * Revision 1.20 1995/10/06 11:06:33 adam
107 * Scan entries include 'occurrences' now.
109 * Revision 1.19 1995/10/06 10:43:56 adam
110 * Scan added. 'occurrences' in scan entries not set yet.
112 * Revision 1.18 1995/10/04 16:57:20 adam
113 * Key input and merge sort in one pass.
115 * Revision 1.17 1995/10/04 12:55:17 adam
116 * Bug fix in ranked search. Use=Any keys inserted.
118 * Revision 1.16 1995/10/02 16:24:40 adam
119 * Use attribute actually used in search requests.
121 * Revision 1.15 1995/10/02 15:18:52 adam
122 * New member in recRetrieveCtrl: diagnostic.
124 * Revision 1.14 1995/09/28 12:10:32 adam
125 * Bug fixes. Field prefix used in queries.
127 * Revision 1.13 1995/09/18 14:17:50 adam
130 * Revision 1.12 1995/09/15 14:45:21 adam
132 * Work on truncation.
134 * Revision 1.11 1995/09/14 11:53:27 adam
135 * First work on regular expressions/truncations.
137 * Revision 1.10 1995/09/11 15:23:26 adam
138 * More work on relevance search.
140 * Revision 1.9 1995/09/11 13:09:35 adam
141 * More work on relevance feedback.
143 * Revision 1.8 1995/09/08 14:52:27 adam
144 * Minor changes. Dictionary is lower case now.
146 * Revision 1.7 1995/09/07 13:58:36 adam
147 * New parameter: result-set file descriptor (RSFD) to support multiple
148 * positions within the same result-set.
149 * Boolean operators: and, or, not implemented.
150 * Result-set references.
152 * Revision 1.6 1995/09/06 16:11:18 adam
153 * Option: only one word key per file.
155 * Revision 1.5 1995/09/06 10:33:04 adam
156 * More work on present. Some log messages removed.
158 * Revision 1.4 1995/09/05 15:28:40 adam
159 * More work on search engine.
161 * Revision 1.3 1995/09/04 15:20:22 adam
164 * Revision 1.2 1995/09/04 12:33:43 adam
165 * Various cleanup. YAZ util used instead.
167 * Revision 1.1 1995/09/04 09:10:40 adam
168 * More work on index add/del/update.
169 * Merge sort implemented.
170 * Initial work on z39 server.
179 #include "attribute.h"
192 Z_AttributesPlusTerm *zapt;
195 static int attr_find (AttrType *src, oid_value *attributeSetP)
197 while (src->major < src->zapt->num_attributes)
199 Z_AttributeElement *element;
201 element = src->zapt->attributeList[src->major];
202 if (src->type == *element->attributeType)
204 switch (element->which)
206 case Z_AttributeValue_numeric:
208 if (element->attributeSet && attributeSetP)
212 attrset = oid_getentbyoid (element->attributeSet);
213 *attributeSetP = attrset->value;
215 return *element->value.numeric;
217 case Z_AttributeValue_complex:
218 if (src->minor >= element->value.complex->num_list ||
219 element->value.complex->list[src->minor]->which !=
220 Z_StringOrNumeric_numeric)
223 if (element->attributeSet && attributeSetP)
227 attrset = oid_getentbyoid (element->attributeSet);
228 *attributeSetP = attrset->value;
230 return *element->value.complex->list[src->minor-1]->u.numeric;
240 static void attr_init (AttrType *src, Z_AttributesPlusTerm *zapt,
254 int (*cmp)(const void *p1, const void *p2);
261 static void heap_swap (struct trunc_info *ti, int i1, int i2)
266 ti->ptr[i1] = ti->ptr[i2];
270 static void heap_delete (struct trunc_info *ti)
272 int cur = 1, child = 2;
274 heap_swap (ti, 1, ti->heapnum--);
275 while (child <= ti->heapnum) {
276 if (child < ti->heapnum &&
277 (*ti->cmp)(ti->heap[ti->ptr[child]],
278 ti->heap[ti->ptr[1+child]]) > 0)
280 if ((*ti->cmp)(ti->heap[ti->ptr[cur]],
281 ti->heap[ti->ptr[child]]) > 0)
283 heap_swap (ti, cur, child);
292 static void heap_insert (struct trunc_info *ti, const char *buf, int indx)
296 cur = ++(ti->heapnum);
297 memcpy (ti->heap[ti->ptr[cur]], buf, ti->keysize);
298 ti->indx[ti->ptr[cur]] = indx;
300 while (parent && (*ti->cmp)(ti->heap[ti->ptr[parent]],
301 ti->heap[ti->ptr[cur]]) > 0)
303 heap_swap (ti, cur, parent);
310 struct trunc_info *heap_init (int size, int key_size,
311 int (*cmp)(const void *p1, const void *p2))
313 struct trunc_info *ti = xmalloc (sizeof(*ti));
318 ti->keysize = key_size;
320 ti->indx = xmalloc (size * sizeof(*ti->indx));
321 ti->heap = xmalloc (size * sizeof(*ti->heap));
322 ti->ptr = xmalloc (size * sizeof(*ti->ptr));
323 ti->swapbuf = xmalloc (ti->keysize);
324 ti->tmpbuf = xmalloc (ti->keysize);
325 ti->buf = xmalloc (size * ti->keysize);
326 for (i = size; --i >= 0; )
329 ti->heap[i] = ti->buf + ti->keysize * i;
334 static void heap_close (struct trunc_info *ti)
344 static RSET rset_trunc_r (ISAM isam, ISAM_P *isam_p, int from, int to,
349 rset_temp_parms parms;
351 parms.key_size = sizeof(struct it_key);
352 result = rset_create (rset_kind_temp, &parms);
353 result_rsfd = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
355 if (to - from > merge_chunk)
359 int i, i_add = (to-from)/merge_chunk + 1;
360 struct trunc_info *ti;
362 int rsmax = (to-from)/i_add + 1;
364 rset = xmalloc (sizeof(*rset) * rsmax);
365 rsfd = xmalloc (sizeof(*rsfd) * rsmax);
367 for (i = from; i < to; i += i_add)
370 rset[rscur] = rset_trunc_r (isam, isam_p, i, i+i_add,
373 rset[rscur] = rset_trunc_r (isam, isam_p, i, to,
377 ti = heap_init (rscur, sizeof(struct it_key), key_compare);
378 for (i = rscur; --i >= 0; )
380 rsfd[i] = rset_open (rset[i], RSETF_READ|RSETF_SORT_SYSNO);
381 if (rset_read (rset[i], rsfd[i], ti->tmpbuf))
382 heap_insert (ti, ti->tmpbuf, i);
385 rset_close (rset[i], rsfd[i]);
386 rset_delete (rset[i]);
391 int n = ti->indx[ti->ptr[1]];
393 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
397 if (!rset_read (rset[n], rsfd[n], ti->tmpbuf))
400 rset_close (rset[n], rsfd[n]);
401 rset_delete (rset[n]);
404 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
407 heap_insert (ti, ti->tmpbuf, n);
420 struct trunc_info *ti;
422 ispt = xmalloc (sizeof(*ispt) * (to-from));
424 ti = heap_init (to-from, sizeof(struct it_key),
426 for (i = to-from; --i >= 0; )
428 ispt[i] = is_position (isam, isam_p[from+i]);
429 if (is_readkey (ispt[i], ti->tmpbuf))
430 heap_insert (ti, ti->tmpbuf, i);
432 is_pt_free (ispt[i]);
436 int n = ti->indx[ti->ptr[1]];
438 rset_write (result, result_rsfd, ti->heap[ti->ptr[1]]);
440 /* section that preserve all keys */
442 if (is_readkey (ispt[n], ti->tmpbuf))
443 heap_insert (ti, ti->tmpbuf, n);
445 is_pt_free (ispt[n]);
447 /* section that preserve all keys with unique sysnos */
450 if (!is_readkey (ispt[n], ti->tmpbuf))
453 is_pt_free (ispt[n]);
456 if ((*ti->cmp)(ti->tmpbuf, ti->heap[ti->ptr[1]]) > 1)
459 heap_insert (ti, ti->tmpbuf, n);
468 rset_close (result, result_rsfd);
472 static int isam_trunc_cmp (const void *p1, const void *p2)
474 ISAM_P i1 = *(ISAM_P*) p1;
475 ISAM_P i2 = *(ISAM_P*) p2;
478 d = is_type (i1) - is_type (i2);
481 return is_block (i1) - is_block (i2);
484 static RSET rset_trunc (ISAM isam, ISAM_P *isam_p, int no)
487 qsort (isam_p, no, sizeof(*isam_p), isam_trunc_cmp);
488 return rset_trunc_r (isam, isam_p, 0, no, 100);
497 static void add_isam_p (const char *info, struct grep_info *p)
499 if (p->isam_p_indx == p->isam_p_size)
501 ISAM_P *new_isam_p_buf;
503 p->isam_p_size = 2*p->isam_p_size + 100;
504 new_isam_p_buf = xmalloc (sizeof(*new_isam_p_buf) *
508 memcpy (new_isam_p_buf, p->isam_p_buf,
509 p->isam_p_indx * sizeof(*p->isam_p_buf));
510 xfree (p->isam_p_buf);
512 p->isam_p_buf = new_isam_p_buf;
514 assert (*info == sizeof(*p->isam_p_buf));
515 memcpy (p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
519 static int grep_handle (char *name, const char *info, void *p)
521 logf (LOG_DEBUG, "dict name: %s", name);
522 add_isam_p (info, p);
526 static void gen_regular_rel (char *dst, int val, int islt)
534 sprintf (numstr, "%d", val);
535 for (w = strlen(numstr); --w >= 0; pos++)
554 strcpy (dst + dst_p, numstr);
555 dst_p = strlen(dst) - pos - 1;
583 for (i = 0; i<pos; i++)
596 for (i=1; i<pos; i++)
597 strcat (dst, "[0-9]?");
601 for (i = 0; i <= pos; i++)
602 strcat (dst, "[0-9]");
603 strcat (dst, "[0-9]*");
608 static int relational_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
609 const char *term_sub,
611 oid_value attributeSet,
612 struct grep_info *grep_info,
620 attr_init (&relation, zapt, 2);
621 relation_value = attr_find (&relation, NULL);
622 term_value = atoi (term_sub);
624 switch (relation_value)
629 logf (LOG_DEBUG, "Relation <");
630 gen_regular_rel (term_dict + strlen(term_dict), term_value-1, 1);
635 logf (LOG_DEBUG, "Relation <=");
636 gen_regular_rel (term_dict + strlen(term_dict), term_value, 1);
641 logf (LOG_DEBUG, "Relation >=");
642 gen_regular_rel (term_dict + strlen(term_dict), term_value, 0);
647 logf (LOG_DEBUG, "Relation >");
648 gen_regular_rel (term_dict + strlen(term_dict), term_value+1, 0);
653 logf (LOG_DEBUG, "dict_lookup_grep: %s", term_dict);
654 r = dict_lookup_grep (zi->wordDict, term_dict, 0, grep_info, max_pos,
657 logf (LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
658 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
662 static void verbatim_char (int ch, int *indx, char *dst)
665 dst[(*indx)++] = '\\';
669 static int field_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
670 const char *term_sub, int regType,
671 oid_value attributeSet, struct grep_info *grep_info,
672 int num_bases, char **basenames)
674 char term_dict[2*IT_MAX_WORD+2];
675 int i, j, r, base_no;
677 int truncation_value;
680 oid_value curAttributeSet = attributeSet;
682 attr_init (&use, zapt, 1);
683 use_value = attr_find (&use, &curAttributeSet);
684 logf (LOG_DEBUG, "use value %d", use_value);
685 attr_init (&truncation, zapt, 5);
686 truncation_value = attr_find (&truncation, NULL);
687 logf (LOG_DEBUG, "truncation value %d", truncation_value);
692 for (base_no = 0; base_no < num_bases; base_no++)
695 data1_local_attribute *local_attr;
696 int max_pos, prefix_len = 0;
698 attp = att_getentbyatt (curAttributeSet, use_value);
701 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
702 curAttributeSet, use_value);
706 if (zebTargetInfo_curDatabase (zi->zti, basenames[base_no]))
708 zi->errCode = 109; /* Database unavailable */
709 zi->errString = basenames[base_no];
712 for (local_attr = attp->local_attributes; local_attr;
713 local_attr = local_attr->next)
717 ord = zebTargetInfo_lookupSU (zi->zti, attp->attset_ordinal,
722 term_dict[prefix_len++] = '|';
724 term_dict[prefix_len++] = '(';
725 term_dict[prefix_len++] = 1;
726 term_dict[prefix_len++] = ord;
733 term_dict[prefix_len++] = ')';
734 term_dict[prefix_len++] = 1;
735 term_dict[prefix_len++] = regType;
736 term_dict[prefix_len] = '\0';
737 if (!relational_term (zi, zapt, term_sub, term_dict,
738 attributeSet, grep_info, &max_pos))
743 switch (truncation_value)
745 case -1: /* not specified */
746 case 100: /* do not truncate */
747 term_dict[j++] = '(';
748 for (i = 0; term_sub[i]; i++)
749 verbatim_char (term_sub[i], &j, term_dict);
750 strcpy (term_dict+j, ")");
751 r = dict_lookup_grep (zi->wordDict, term_dict, 0, grep_info,
752 &max_pos, 0, grep_handle);
754 logf (LOG_WARN, "dict_lookup_grep err, trunc=none:%d", r);
756 case 1: /* right truncation */
757 term_dict[j++] = '(';
758 for (i = 0; term_sub[i]; i++)
759 verbatim_char (term_sub[i], &j, term_dict);
760 strcpy (term_dict+j, ".*)");
761 dict_lookup_grep (zi->wordDict, term_dict, 0, grep_info,
762 &max_pos, 0, grep_handle);
764 case 2: /* left truncation */
765 case 3: /* left&right truncation */
768 case 101: /* process # in term */
769 term_dict[j++] = '(';
770 for (i=0; term_sub[i]; i++)
771 if (term_sub[i] == '#' && i > 2)
773 term_dict[j++] = '.';
774 term_dict[j++] = '*';
777 verbatim_char (term_sub[i], &j, term_dict);
778 strcpy (term_dict+j, ")");
779 r = dict_lookup_grep (zi->wordDict, term_dict, 0, grep_info,
780 &max_pos, 0, grep_handle);
782 logf (LOG_WARN, "dict_lookup_grep err, trunc=#: %d",
785 case 102: /* regular expression */
786 sprintf (term_dict + j, "(%s)", term_sub);
787 r = dict_lookup_grep (zi->wordDict, term_dict, 0, grep_info,
788 &max_pos, 0, grep_handle);
790 logf (LOG_WARN, "dict_lookup_grep err, trunc=regular: %d",
793 case 103: /* regular expression with error correction */
796 if (*cp == '*' && cp[1] && cp[2])
801 sprintf (term_dict + j, "(%s)", cp);
802 r = dict_lookup_grep (zi->wordDict, term_dict, r, grep_info,
803 &max_pos, j, grep_handle);
805 logf (LOG_WARN, "dict_lookup_grep err, trunc=eregular: %d",
811 logf (LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
815 static void trans_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
819 Z_Term *term = zapt->term;
821 sizez = term->u.general->len;
822 if (sizez > IT_MAX_WORD-1)
823 sizez = IT_MAX_WORD-1;
824 memcpy (termz, term->u.general->buf, sizez);
828 static void trans_scan_term (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
831 Z_Term *term = zapt->term;
833 const char *cp = (const char *) term->u.general->buf;
834 const char *cp_end = cp + term->u.general->len;
840 while ((len = (cp_end - cp)) > 0)
842 map = map_chrs_input (&cp, len);
843 if (**map == *CHR_SPACE)
851 for (src = *map; *src; src++)
857 static RSET rpn_search_APT_relevance (ZServerInfo *zi,
858 Z_AttributesPlusTerm *zapt,
859 oid_value attributeSet,
860 int num_bases, char **basenames)
862 rset_relevance_parms parms;
863 char termz[IT_MAX_WORD+1];
864 char term_sub[IT_MAX_WORD+1];
865 struct grep_info grep_info;
866 char *p0 = termz, *p1 = NULL;
869 parms.key_size = sizeof(struct it_key);
871 parms.cmp = key_compare;
872 parms.is = zi->wordIsam;
874 if (zapt->term->which != Z_Term_general)
879 trans_term (zi, zapt, termz);
881 grep_info.isam_p_indx = 0;
882 grep_info.isam_p_size = 0;
883 grep_info.isam_p_buf = NULL;
891 map = map_chrs_input (&p1, strlen(p1));
892 if (**map == *CHR_SPACE)
898 memcpy (term_sub, p0, p2-p0);
899 term_sub[p2-p0] = '\0';
902 map = map_chrs_input (&p1, strlen(p1));
903 if (**map == *CHR_SPACE)
906 if (field_term (zi, zapt, term_sub, 'w', attributeSet, &grep_info,
907 num_bases, basenames))
913 parms.isam_positions = grep_info.isam_p_buf;
914 parms.no_isam_positions = grep_info.isam_p_indx;
915 if (grep_info.isam_p_indx > 0)
916 result = rset_create (rset_kind_relevance, &parms);
918 result = rset_create (rset_kind_null, NULL);
919 xfree (grep_info.isam_p_buf);
923 static RSET rpn_search_APT_cphrase (ZServerInfo *zi,
924 Z_AttributesPlusTerm *zapt,
925 oid_value attributeSet,
926 int num_bases, char **basenames)
928 rset_isam_parms parms;
929 char termz[IT_MAX_WORD+1];
930 struct grep_info grep_info;
933 if (zapt->term->which != Z_Term_general)
938 trans_term (zi, zapt, termz);
940 grep_info.isam_p_indx = 0;
941 grep_info.isam_p_size = 0;
942 grep_info.isam_p_buf = NULL;
944 if (field_term (zi, zapt, termz, 'p', attributeSet, &grep_info,
945 num_bases, basenames))
947 if (grep_info.isam_p_indx < 1)
948 result = rset_create (rset_kind_null, NULL);
949 else if (grep_info.isam_p_indx == 1)
951 parms.is = zi->wordIsam;
952 parms.pos = *grep_info.isam_p_buf;
953 result = rset_create (rset_kind_isam, &parms);
956 result = rset_trunc (zi->wordIsam, grep_info.isam_p_buf,
957 grep_info.isam_p_indx);
958 xfree (grep_info.isam_p_buf);
962 static RSET rpn_prox (RSET *rset, int rset_no)
970 rset_temp_parms parms;
972 rsfd = xmalloc (sizeof(*rsfd)*rset_no);
973 more = xmalloc (sizeof(*more)*rset_no);
974 buf = xmalloc (sizeof(*buf)*rset_no);
976 for (i = 0; i<rset_no; i++)
978 buf[i] = xmalloc (sizeof(**buf));
979 rsfd[i] = rset_open (rset[i], RSETF_READ|RSETF_SORT_SYSNO);
980 if (!(more[i] = rset_read (rset[i], rsfd[i], buf[i])))
984 rset_close (rset[i], rsfd[i]);
991 return rset_create (rset_kind_null, NULL);
994 parms.key_size = sizeof (struct it_key);
995 result = rset_create (rset_kind_temp, &parms);
996 rsfd_result = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
1000 for (i = 1; i<rset_no; i++)
1009 cmp = key_compare (buf[i], buf[i-1]);
1012 more[i-1] = rset_read (rset[i-1], rsfd[i-1], buf[i-1]);
1017 if (buf[i-1]->seqno+1 != buf[i]->seqno)
1019 more[i-1] = rset_read (rset[i-1], rsfd[i-1], buf[i-1]);
1025 more[i] = rset_read (rset[i], rsfd[i], buf[i]);
1031 rset_write (result, rsfd_result, buf[0]);
1032 more[0] = rset_read (*rset, *rsfd, *buf);
1036 for (i = 0; i<rset_no; i++)
1038 rset_close (rset[i], rsfd[i]);
1041 rset_close (result, rsfd_result);
1048 static RSET rpn_search_APT_phrase (ZServerInfo *zi,
1049 Z_AttributesPlusTerm *zapt,
1050 oid_value attributeSet,
1051 int num_bases, char **basenames)
1053 char termz[IT_MAX_WORD+1];
1054 char term_sub[IT_MAX_WORD+1];
1055 char *p0 = termz, *p1 = NULL;
1056 RSET rset[60], result;
1058 struct grep_info grep_info;
1060 if (zapt->term->which != Z_Term_general)
1065 trans_term (zi, zapt, termz);
1067 grep_info.isam_p_size = 0;
1068 grep_info.isam_p_buf = NULL;
1077 map = map_chrs_input (&p1, strlen(p1));
1078 if (**map == *CHR_SPACE)
1084 memcpy (term_sub, p0, p2-p0);
1085 term_sub[p2-p0] = '\0';
1089 map = map_chrs_input (&p1, strlen(p1));
1090 if (**map != *CHR_SPACE)
1094 grep_info.isam_p_indx = 0;
1095 if (field_term (zi, zapt, term_sub, 'w', attributeSet, &grep_info,
1096 num_bases, basenames))
1098 if (grep_info.isam_p_indx == 0)
1099 rset[rset_no] = rset_create (rset_kind_null, NULL);
1100 else if (grep_info.isam_p_indx > 1)
1101 rset[rset_no] = rset_trunc (zi->wordIsam,
1102 grep_info.isam_p_buf,
1103 grep_info.isam_p_indx);
1106 rset_isam_parms parms;
1108 parms.is = zi->wordIsam;
1109 parms.pos = *grep_info.isam_p_buf;
1110 rset[rset_no] = rset_create (rset_kind_isam, &parms);
1112 assert (rset[rset_no]);
1113 if (++rset_no >= sizeof(rset)/sizeof(*rset))
1118 xfree (grep_info.isam_p_buf);
1120 return rset_create (rset_kind_null, NULL);
1121 else if (rset_no == 1)
1124 result = rpn_prox (rset, rset_no);
1125 for (i = 0; i<rset_no; i++)
1126 rset_delete (rset[i]);
1130 static RSET rpn_search_APT_local (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1131 oid_value attributeSet)
1136 rset_temp_parms parms;
1137 char termz[IT_MAX_WORD+1];
1139 if (zapt->term->which != Z_Term_general)
1144 parms.key_size = sizeof (struct it_key);
1145 result = rset_create (rset_kind_temp, &parms);
1146 rsfd = rset_open (result, RSETF_WRITE|RSETF_SORT_SYSNO);
1148 trans_term (zi, zapt, termz);
1150 key.sysno = atoi (termz);
1153 rset_write (result, rsfd, &key);
1154 rset_close (result, rsfd);
1158 static RSET rpn_search_APT (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1159 oid_value attributeSet,
1160 int num_bases, char **basenames)
1164 AttrType completeness;
1165 int relation_value, structure_value, completeness_value;
1167 attr_init (&relation, zapt, 2);
1168 attr_init (&structure, zapt, 4);
1169 attr_init (&completeness, zapt, 6);
1171 relation_value = attr_find (&relation, NULL);
1172 structure_value = attr_find (&structure, NULL);
1173 completeness_value = attr_find (&completeness, NULL);
1174 switch (structure_value)
1177 if (relation_value == 102) /* relevance relation */
1178 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1179 num_bases, basenames);
1180 if (completeness_value == 2 || completeness_value == 3)
1181 return rpn_search_APT_cphrase (zi, zapt, attributeSet,
1182 num_bases, basenames);
1183 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1184 num_bases, basenames);
1185 case 1: /* phrase */
1186 if (relation_value == 102) /* relevance relation */
1187 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1188 num_bases, basenames);
1189 if (completeness_value == 2 || completeness_value == 3)
1190 return rpn_search_APT_cphrase (zi, zapt, attributeSet,
1191 num_bases, basenames);
1192 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1193 num_bases, basenames);
1196 if (relation_value == 102) /* relevance relation */
1197 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1198 num_bases, basenames);
1199 if (completeness_value == 2 || completeness_value == 3)
1200 return rpn_search_APT_cphrase (zi, zapt, attributeSet,
1201 num_bases, basenames);
1202 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1203 num_bases, basenames);
1208 case 5: /* date - normalized */
1210 case 6: /* word list */
1211 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1212 num_bases, basenames);
1213 case 100: /* date - un-normalized */
1215 case 101: /* name - normalized */
1217 case 102: /* date - un-normalized */
1219 case 103: /* structure */
1223 case 105: /* free-form-text */
1224 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1225 num_bases, basenames);
1226 case 106: /* document-text */
1227 return rpn_search_APT_relevance (zi, zapt, attributeSet,
1228 num_bases, basenames);
1229 case 107: /* local-number */
1230 return rpn_search_APT_local (zi, zapt, attributeSet);
1231 case 108: /* string */
1232 return rpn_search_APT_phrase (zi, zapt, attributeSet,
1233 num_bases, basenames);
1234 case 109: /* numeric string */
1241 static RSET rpn_search_ref (ZServerInfo *zi, Z_ResultSetId *resultSetId)
1245 if (!(s = resultSetGet (zi, resultSetId)))
1246 return rset_create (rset_kind_null, NULL);
1250 static RSET rpn_search_structure (ZServerInfo *zi, Z_RPNStructure *zs,
1251 oid_value attributeSet,
1252 int num_bases, char **basenames)
1255 if (zs->which == Z_RPNStructure_complex)
1257 rset_bool_parms bool_parms;
1260 bool_parms.rset_l = rpn_search_structure (zi, zs->u.complex->s1,
1262 num_bases, basenames);
1263 if (bool_parms.rset_l == NULL)
1265 if (rset_is_ranked(bool_parms.rset_l))
1267 bool_parms.rset_r = rpn_search_structure (zi, zs->u.complex->s2,
1269 num_bases, basenames);
1270 if (bool_parms.rset_r == NULL)
1272 rset_delete (bool_parms.rset_l);
1275 if (rset_is_ranked(bool_parms.rset_r))
1277 bool_parms.key_size = sizeof(struct it_key);
1278 bool_parms.cmp = key_compare;
1280 switch (zs->u.complex->roperator->which)
1282 case Z_Operator_and:
1283 r = rset_create (soft ? rset_kind_sand:rset_kind_and, &bool_parms);
1286 r = rset_create (soft ? rset_kind_sor:rset_kind_or, &bool_parms);
1288 case Z_Operator_and_not:
1289 r = rset_create (soft ? rset_kind_snot:rset_kind_not, &bool_parms);
1295 else if (zs->which == Z_RPNStructure_simple)
1297 if (zs->u.simple->which == Z_Operand_APT)
1299 logf (LOG_DEBUG, "rpn_search_APT");
1300 r = rpn_search_APT (zi, zs->u.simple->u.attributesPlusTerm,
1301 attributeSet, num_bases, basenames);
1303 else if (zs->u.simple->which == Z_Operand_resultSetId)
1305 logf (LOG_DEBUG, "rpn_search_ref");
1306 r = rpn_search_ref (zi, zs->u.simple->u.resultSetId);
1320 void count_set_save (RSET *r, int *count)
1327 rset_temp_parms parms;
1329 logf (LOG_DEBUG, "count_set_save");
1331 parms.key_size = sizeof(struct it_key);
1332 w = rset_create (rset_kind_temp, &parms);
1333 wfd = rset_open (w, RSETF_WRITE|RSETF_SORT_SYSNO);
1334 rfd = rset_open (*r, RSETF_READ|RSETF_SORT_SYSNO);
1335 while (rset_read (*r, rfd, &key))
1337 if (key.sysno != psysno)
1339 rset_write (w, wfd, &key);
1345 rset_close (*r, rfd);
1347 rset_close (w, wfd);
1349 logf (LOG_DEBUG, "%d keys, %d distinct sysnos", kno, *count);
1352 static void count_set (RSET r, int *count)
1359 logf (LOG_DEBUG, "count_set");
1361 rfd = rset_open (r, RSETF_READ|RSETF_SORT_SYSNO);
1362 while (rset_read (r, rfd, &key))
1364 if (key.sysno != psysno)
1371 rset_close (r, rfd);
1372 logf (LOG_DEBUG, "%d keys, %d distinct sysnos", kno, *count);
1375 int rpn_search (ZServerInfo *zi,
1376 Z_RPNQuery *rpn, int num_bases, char **basenames,
1377 const char *setname, int *hits)
1381 oid_value attributeSet;
1383 dict_grep_cmap (zi->wordDict, map_chrs_input);
1387 zi->errString = NULL;
1389 attrset = oid_getentbyoid (rpn->attributeSetId);
1390 attributeSet = attrset->value;
1391 rset = rpn_search_structure (zi, rpn->RPNStructure, attributeSet,
1392 num_bases, basenames);
1395 if (rset_is_volatile(rset))
1396 count_set_save(&rset,hits);
1398 count_set (rset, hits);
1399 resultSetAdd (zi, setname, 1, rset);
1401 logf (LOG_DEBUG, "search error: %d", zi->errCode);
1405 struct scan_info_entry {
1411 struct scan_info_entry *list;
1417 static int scan_handle (char *name, const char *info, int pos, void *client)
1419 int len_prefix, idx;
1420 struct scan_info *scan_info = client;
1422 len_prefix = strlen(scan_info->prefix);
1423 if (memcmp (name, scan_info->prefix, len_prefix))
1426 idx = scan_info->after - pos + scan_info->before;
1429 logf (LOG_DEBUG, "%-3d %s", idx, name+len_prefix);
1430 scan_info->list[idx].term = odr_malloc (scan_info->odr,
1431 strlen(name + len_prefix)+1);
1432 strcpy (scan_info->list[idx].term, name + len_prefix);
1433 assert (*info == sizeof(ISAM_P));
1434 memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAM_P));
1439 static void scan_term_untrans (ODR odr, char **dstp, const char *src)
1441 char *dst = odr_malloc (odr, strlen(src)*2+1);
1446 const char *cp = map_chrs_output (&src);
1453 int rpn_scan (ZServerInfo *zi, Z_AttributesPlusTerm *zapt,
1454 oid_value attributeset,
1455 int num_bases, char **basenames,
1456 int *position, int *num_entries, struct scan_entry **list,
1460 int pos = *position;
1461 int num = *num_entries;
1465 char termz[IT_MAX_WORD+20];
1468 AttrType completeness;
1469 int completeness_value;
1470 struct scan_info *scan_info_array;
1471 struct scan_entry *glist;
1472 int ords[32], ord_no = 0;
1475 logf (LOG_DEBUG, "scan, position = %d, num = %d", pos, num);
1477 attr_init (&use, zapt, 1);
1478 use_value = attr_find (&use, NULL);
1479 logf (LOG_DEBUG, "use value %d", use_value);
1481 attr_init (&completeness, zapt, 6);
1482 completeness_value = attr_find (&completeness, NULL);
1483 logf (LOG_DEBUG, "completeness value %d", completeness_value);
1485 if (attributeset == VAL_NONE)
1486 attributeset = VAL_BIB1;
1488 if (use_value == -1)
1490 for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
1493 data1_local_attribute *local_attr;
1495 attp = att_getentbyatt (attributeset, use_value);
1498 logf (LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
1499 attributeset, use_value);
1500 return zi->errCode = 114;
1502 if (zebTargetInfo_curDatabase (zi->zti, basenames[base_no]))
1504 zi->errString = basenames[base_no];
1505 return zi->errCode = 109; /* Database unavailable */
1507 for (local_attr = attp->local_attributes; local_attr && ord_no < 32;
1508 local_attr = local_attr->next)
1512 ord = zebTargetInfo_lookupSU (zi->zti, attp->attset_ordinal,
1515 ords[ord_no++] = ord;
1519 return zi->errCode = 113;
1522 scan_info_array = odr_malloc (zi->odr, ord_no * sizeof(*scan_info_array));
1523 for (i = 0; i < ord_no; i++)
1525 int j, prefix_len = 0;
1526 int before_tmp = before, after_tmp = after;
1527 struct scan_info *scan_info = scan_info_array + i;
1529 scan_info->before = before;
1530 scan_info->after = after;
1531 scan_info->odr = zi->odr;
1533 scan_info->list = odr_malloc (zi->odr, (before+after)*
1534 sizeof(*scan_info->list));
1535 for (j = 0; j<before+after; j++)
1536 scan_info->list[j].term = NULL;
1537 termz[prefix_len++] = ords[i];
1538 termz[prefix_len++] =
1539 (completeness_value==2 || completeness_value==3) ? 'p': 'w';
1540 termz[prefix_len] = 0;
1541 strcpy (scan_info->prefix, termz);
1543 trans_scan_term (zi, zapt, termz+prefix_len);
1545 dict_scan (zi->wordDict, termz, &before_tmp, &after_tmp, scan_info,
1548 glist = odr_malloc (zi->odr, (before+after)*sizeof(*glist));
1549 for (i = 0; i < ord_no; i++)
1552 *status = BEND_SCAN_SUCCESS;
1553 for (i = 0; i<after; i++)
1556 const char *mterm = NULL;
1559 rset_isam_parms parms;
1561 for (j = 0; j < ord_no; j++)
1563 if (ptr[j] < before+after &&
1564 (tst=scan_info_array[j].list[ptr[j]].term) &&
1565 (!mterm || strcmp (tst, mterm) < 0))
1573 scan_term_untrans (zi->odr, &glist[i+before].term, mterm);
1574 parms.is = zi->wordIsam;
1575 parms.pos = scan_info_array[j0].list[ptr[j0]].isam_p;
1576 rset = rset_create (rset_kind_isam, &parms);
1579 for (j = j0+1; j<ord_no; j++)
1581 if (ptr[j] < before+after &&
1582 (tst=scan_info_array[j].list[ptr[j]].term) &&
1583 !strcmp (tst, mterm))
1585 rset_isam_parms parms;
1586 rset_bool_parms bool_parms;
1589 parms.is = zi->wordIsam;
1590 parms.pos = scan_info_array[j].list[ptr[j]].isam_p;
1591 rset2 = rset_create (rset_kind_isam, &parms);
1593 bool_parms.key_size = sizeof(struct it_key);
1594 bool_parms.cmp = key_compare;
1595 bool_parms.rset_l = rset;
1596 bool_parms.rset_r = rset2;
1598 rset = rset_create (rset_kind_or, &bool_parms);
1603 count_set (rset, &glist[i+before].occurrences);
1608 *num_entries -= (after-i);
1609 *status = BEND_SCAN_PARTIAL;
1612 for (i = 0; i<ord_no; i++)
1615 for (i = 0; i<before; i++)
1618 const char *mterm = NULL;
1621 rset_isam_parms parms;
1623 for (j = 0; j <ord_no; j++)
1625 if (ptr[j] < before &&
1626 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
1627 (!mterm || strcmp (tst, mterm) > 0))
1636 scan_term_untrans (zi->odr, &glist[before-1-i].term, mterm);
1638 parms.is = zi->wordIsam;
1639 parms.pos = scan_info_array[j0].list[before-1-ptr[j0]].isam_p;
1640 rset = rset_create (rset_kind_isam, &parms);
1644 for (j = j0+1; j<ord_no; j++)
1646 if (ptr[j] < before &&
1647 (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
1648 !strcmp (tst, mterm))
1650 rset_isam_parms parms;
1651 rset_bool_parms bool_parms;
1654 parms.is = zi->wordIsam;
1655 parms.pos = scan_info_array[j].list[before-1-ptr[j]].isam_p;
1656 rset2 = rset_create (rset_kind_isam, &parms);
1658 bool_parms.key_size = sizeof(struct it_key);
1659 bool_parms.cmp = key_compare;
1660 bool_parms.rset_l = rset;
1661 bool_parms.rset_r = rset2;
1663 rset = rset_create (rset_kind_or, &bool_parms);
1668 count_set (rset, &glist[before-1-i].occurrences);
1674 *status = BEND_SCAN_PARTIAL;
1678 *list = glist + i; /* list is set to first 'real' entry */
1680 logf (LOG_DEBUG, "position = %d, num_entries = %d",
1681 *position, *num_entries);
1683 logf (LOG_DEBUG, "scan error: %d", zi->errCode);